annotate Framework/Scene2D/Internals/CairoTextRenderer.cpp @ 931:fc38c4ab17e3 toa2019072401

Merge
author Benjamin Golinvaux <bgo@osimis.io>
date Wed, 24 Jul 2019 18:46:06 +0200
parents 6e888cf6a48b
children 2d8ab34c8c91
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
597
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Stone of Orthanc
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * modify it under the terms of the GNU Affero General Public License
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * as published by the Free Software Foundation, either version 3 of
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * the License, or (at your option) any later version.
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * Affero General Public License for more details.
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 *
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * You should have received a copy of the GNU Affero General Public License
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 **/
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 #include "CairoTextRenderer.h"
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 #include <Core/OrthancException.h>
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 namespace OrthancStone
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 {
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 namespace Internals
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 {
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 CairoTextRenderer::CairoTextRenderer(ICairoContextProvider& target,
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 const GlyphBitmapAlphabet& alphabet,
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 const TextSceneLayer& layer) :
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 CairoBaseRenderer(target, layer)
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 {
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 std::auto_ptr<Orthanc::ImageAccessor> source(alphabet.RenderText(layer.GetText()));
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 if (source.get() != NULL)
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 {
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 text_.SetSize(source->GetWidth(), source->GetHeight(), true);
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 Orthanc::ImageAccessor target;
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 text_.GetWriteableAccessor(target);
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 if (source->GetFormat() != Orthanc::PixelFormat_Grayscale8 ||
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 target.GetFormat() != Orthanc::PixelFormat_BGRA32)
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 {
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 }
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 const unsigned int width = source->GetWidth();
804
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 597
diff changeset
51 const Color& color = layer.GetColor();
597
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 for (unsigned int y = 0; y < source->GetHeight(); y++)
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 {
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 const uint8_t* p = reinterpret_cast<const uint8_t*>(source->GetConstRow(y));
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y));
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 for (unsigned int x = 0; x < width; x++)
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 {
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 unsigned int alpha = *p;
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 // Premultiplied alpha
804
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 597
diff changeset
63 q[0] = static_cast<uint8_t>((color.GetBlue() * alpha) / 255);
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 597
diff changeset
64 q[1] = static_cast<uint8_t>((color.GetGreen() * alpha) / 255);
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 597
diff changeset
65 q[2] = static_cast<uint8_t>((color.GetRed() * alpha) / 255);
597
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 q[3] = *p;
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 p++;
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 q += 4;
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 }
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 }
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 cairo_surface_mark_dirty(text_.GetObject());
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 }
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 }
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77
888
6e888cf6a48b renderers now have access to canvas width/height
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 804
diff changeset
78 void CairoTextRenderer::Render(const AffineTransform2D& transform,
6e888cf6a48b renderers now have access to canvas width/height
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 804
diff changeset
79 unsigned int canvasWidth,
6e888cf6a48b renderers now have access to canvas width/height
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 804
diff changeset
80 unsigned int canvasHeight)
597
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 {
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 if (text_.GetWidth() != 0 &&
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 text_.GetHeight() != 0)
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 {
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 const TextSceneLayer& layer = GetLayer<TextSceneLayer>();
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 cairo_t* cr = GetCairoContext();
804
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 597
diff changeset
88 cairo_set_source_rgb(cr, layer.GetColor().GetRedAsFloat(),
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 597
diff changeset
89 layer.GetColor().GetGreenAsFloat(),
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 597
diff changeset
90 layer.GetColor().GetBlueAsFloat());
597
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 double dx, dy; // In pixels
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 ComputeAnchorTranslation(dx, dy, layer.GetAnchor(), text_.GetWidth(),
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 text_.GetHeight(), layer.GetBorder());
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 double x = layer.GetX();
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 double y = layer.GetY();
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 transform.Apply(x, y);
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 cairo_save(cr);
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 cairo_matrix_t t;
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 cairo_matrix_init_identity(&t);
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 cairo_matrix_translate(&t, x + dx, y + dy);
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 cairo_transform(cr, &t);
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 cairo_set_source_surface(cr, text_.GetObject(), 0, 0);
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 cairo_pattern_set_filter(cairo_get_source(cr), CAIRO_FILTER_BILINEAR);
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 cairo_paint(cr);
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 cairo_restore(cr);
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 }
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 }
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 }
9e51fb773bbd CairoCompositor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 }