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