Mercurial > hg > orthanc-stone
annotate OrthancStone/Sources/Scene2D/CairoCompositor.cpp @ 1886:ca89fec8c48f
test
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 14 Jan 2022 19:10:30 +0100 |
parents | 7053b8a0aaec |
children | 07964689cb0b |
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 | |
1871
7053b8a0aaec
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1870
diff
changeset
|
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium |
7053b8a0aaec
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1870
diff
changeset
|
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
597 | 7 * |
8 * 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
|
9 * modify it under the terms of the GNU Lesser General Public License |
597 | 10 * as published by the Free Software Foundation, either version 3 of |
11 * the License, or (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, but | |
14 * 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
|
15 * 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
|
16 * Lesser General Public License for more details. |
1596
4fb8fdf03314
removed annoying whitespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1576
diff
changeset
|
17 * |
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
|
18 * 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
|
19 * 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
|
20 * <http://www.gnu.org/licenses/>. |
597 | 21 **/ |
22 | |
23 | |
24 #include "CairoCompositor.h" | |
25 | |
1614
ad9b425f27ae
new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1611
diff
changeset
|
26 #include "Internals/CairoArrowRenderer.h" |
597 | 27 #include "Internals/CairoColorTextureRenderer.h" |
28 #include "Internals/CairoFloatTextureRenderer.h" | |
29 #include "Internals/CairoInfoPanelRenderer.h" | |
768
55411e7da2f7
LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
30 #include "Internals/CairoLookupTableTextureRenderer.h" |
597 | 31 #include "Internals/CairoPolylineRenderer.h" |
32 #include "Internals/CairoTextRenderer.h" | |
1611
787db80a5a1b
new class MacroLayerRenderer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1598
diff
changeset
|
33 #include "Internals/MacroLayerRenderer.h" |
597 | 34 |
1455
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1300
diff
changeset
|
35 #include <OrthancException.h> |
597 | 36 |
37 namespace OrthancStone | |
38 { | |
39 cairo_t* CairoCompositor::GetCairoContext() | |
40 { | |
41 if (context_.get() == NULL) | |
42 { | |
43 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
44 } | |
45 else | |
46 { | |
47 return context_->GetObject(); | |
48 } | |
49 } | |
50 | |
51 Internals::CompositorHelper::ILayerRenderer* CairoCompositor::Create(const ISceneLayer& layer) | |
52 { | |
53 switch (layer.GetType()) | |
54 { | |
55 case ISceneLayer::Type_Polyline: | |
56 return new Internals::CairoPolylineRenderer(*this, layer); | |
57 | |
58 case ISceneLayer::Type_InfoPanel: | |
59 return new Internals::CairoInfoPanelRenderer(*this, layer); | |
60 | |
61 case ISceneLayer::Type_ColorTexture: | |
62 return new Internals::CairoColorTextureRenderer(*this, layer); | |
63 | |
64 case ISceneLayer::Type_FloatTexture: | |
65 return new Internals::CairoFloatTextureRenderer(*this, layer); | |
66 | |
768
55411e7da2f7
LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
67 case ISceneLayer::Type_LookupTableTexture: |
55411e7da2f7
LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
68 return new Internals::CairoLookupTableTextureRenderer(*this, layer); |
55411e7da2f7
LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
69 |
597 | 70 case ISceneLayer::Type_Text: |
71 { | |
72 const TextSceneLayer& l = dynamic_cast<const TextSceneLayer&>(layer); | |
73 | |
74 Fonts::const_iterator found = fonts_.find(l.GetFontIndex()); | |
75 if (found == fonts_.end()) | |
76 { | |
77 return NULL; | |
78 } | |
79 else | |
80 { | |
81 assert(found->second != NULL); | |
82 return new Internals::CairoTextRenderer(*this, *found->second, l); | |
83 } | |
84 } | |
85 | |
1611
787db80a5a1b
new class MacroLayerRenderer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1598
diff
changeset
|
86 case ISceneLayer::Type_Macro: |
787db80a5a1b
new class MacroLayerRenderer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1598
diff
changeset
|
87 return new Internals::MacroLayerRenderer(*this, layer); |
787db80a5a1b
new class MacroLayerRenderer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1598
diff
changeset
|
88 |
1614
ad9b425f27ae
new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1611
diff
changeset
|
89 case ISceneLayer::Type_Arrow: |
ad9b425f27ae
new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1611
diff
changeset
|
90 return new Internals::CairoArrowRenderer(*this, layer); |
ad9b425f27ae
new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1611
diff
changeset
|
91 |
597 | 92 default: |
93 return NULL; | |
94 } | |
95 } | |
96 | |
97 | |
1211
d10d2acb8a02
compositors do not keep a reference to the scene anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
947
diff
changeset
|
98 CairoCompositor::CairoCompositor(unsigned int canvasWidth, |
d10d2acb8a02
compositors do not keep a reference to the scene anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
947
diff
changeset
|
99 unsigned int canvasHeight) |
597 | 100 { |
1211
d10d2acb8a02
compositors do not keep a reference to the scene anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
947
diff
changeset
|
101 ResetScene(); |
1576
92fca2b3ba3d
sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1512
diff
changeset
|
102 canvas_.SetSize(canvasWidth, canvasHeight, false); |
909 | 103 } |
104 | |
1576
92fca2b3ba3d
sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1512
diff
changeset
|
105 void CairoCompositor::SetCanvasSize(unsigned int canvasWidth, |
92fca2b3ba3d
sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1512
diff
changeset
|
106 unsigned int canvasHeight) |
909 | 107 { |
597 | 108 canvas_.SetSize(canvasWidth, canvasHeight, false); |
109 } | |
110 | |
111 CairoCompositor::~CairoCompositor() | |
112 { | |
113 for (Fonts::iterator it = fonts_.begin(); it != fonts_.end(); ++it) | |
114 { | |
115 assert(it->second != NULL); | |
116 delete it->second; | |
117 } | |
118 } | |
119 | |
120 | |
121 void CairoCompositor::SetFont(size_t index, | |
122 GlyphBitmapAlphabet* dict) // Takes ownership | |
123 { | |
124 if (dict == NULL) | |
125 { | |
126 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
127 } | |
128 else | |
129 { | |
1298
8a0a62189f46
replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1270
diff
changeset
|
130 std::unique_ptr<GlyphBitmapAlphabet> protection(dict); |
597 | 131 |
132 Fonts::iterator found = fonts_.find(index); | |
133 | |
134 if (found == fonts_.end()) | |
135 { | |
136 fonts_[index] = protection.release(); | |
137 } | |
138 else | |
139 { | |
140 assert(found->second != NULL); | |
141 delete found->second; | |
142 | |
143 found->second = protection.release(); | |
144 } | |
145 } | |
146 } | |
147 | |
148 | |
149 #if ORTHANC_ENABLE_LOCALE == 1 | |
150 void CairoCompositor::SetFont(size_t index, | |
1471
28c64c246312
working on a shared library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1455
diff
changeset
|
151 const std::string& ttf, |
597 | 152 unsigned int fontSize, |
153 Orthanc::Encoding codepage) | |
154 { | |
155 FontRenderer renderer; | |
1471
28c64c246312
working on a shared library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1455
diff
changeset
|
156 renderer.LoadFont(ttf, fontSize); |
597 | 157 |
1298
8a0a62189f46
replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1270
diff
changeset
|
158 std::unique_ptr<GlyphBitmapAlphabet> alphabet(new GlyphBitmapAlphabet); |
597 | 159 alphabet->LoadCodepage(renderer, codepage); |
160 | |
161 SetFont(index, alphabet.release()); | |
162 } | |
163 #endif | |
164 | |
165 | |
1211
d10d2acb8a02
compositors do not keep a reference to the scene anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
947
diff
changeset
|
166 void CairoCompositor::Refresh(const Scene2D& scene) |
597 | 167 { |
168 context_.reset(new CairoContext(canvas_)); | |
169 | |
170 // https://www.cairographics.org/FAQ/#clear_a_surface | |
171 cairo_set_source_rgba(context_->GetObject(), 0, 0, 0, 255); | |
172 cairo_paint(context_->GetObject()); | |
173 | |
1211
d10d2acb8a02
compositors do not keep a reference to the scene anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
947
diff
changeset
|
174 helper_->Refresh(scene, canvas_.GetWidth(), canvas_.GetHeight()); |
597 | 175 context_.reset(); |
176 } | |
177 | |
178 | |
179 Orthanc::ImageAccessor* CairoCompositor::RenderText(size_t fontIndex, | |
180 const std::string& utf8) const | |
181 { | |
182 Fonts::const_iterator found = fonts_.find(fontIndex); | |
183 | |
184 if (found == fonts_.end()) | |
185 { | |
186 return NULL; | |
187 } | |
188 else | |
189 { | |
190 assert(found->second != NULL); | |
191 return found->second->RenderText(utf8); | |
192 } | |
193 } | |
1792
373d7f7e796e
ICompositor::ComputeTextBoundingBox()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1739
diff
changeset
|
194 |
373d7f7e796e
ICompositor::ComputeTextBoundingBox()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1739
diff
changeset
|
195 |
373d7f7e796e
ICompositor::ComputeTextBoundingBox()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1739
diff
changeset
|
196 #if ORTHANC_ENABLE_LOCALE == 1 |
373d7f7e796e
ICompositor::ComputeTextBoundingBox()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1739
diff
changeset
|
197 TextBoundingBox* CairoCompositor::ComputeTextBoundingBox(size_t fontIndex, |
373d7f7e796e
ICompositor::ComputeTextBoundingBox()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1739
diff
changeset
|
198 const std::string& utf8) |
373d7f7e796e
ICompositor::ComputeTextBoundingBox()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1739
diff
changeset
|
199 { |
373d7f7e796e
ICompositor::ComputeTextBoundingBox()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1739
diff
changeset
|
200 Fonts::const_iterator found = fonts_.find(fontIndex); |
373d7f7e796e
ICompositor::ComputeTextBoundingBox()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1739
diff
changeset
|
201 |
373d7f7e796e
ICompositor::ComputeTextBoundingBox()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1739
diff
changeset
|
202 if (found == fonts_.end()) |
373d7f7e796e
ICompositor::ComputeTextBoundingBox()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1739
diff
changeset
|
203 { |
373d7f7e796e
ICompositor::ComputeTextBoundingBox()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1739
diff
changeset
|
204 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange, |
373d7f7e796e
ICompositor::ComputeTextBoundingBox()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1739
diff
changeset
|
205 "No such font: " + boost::lexical_cast<std::string>(fontIndex)); |
373d7f7e796e
ICompositor::ComputeTextBoundingBox()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1739
diff
changeset
|
206 } |
373d7f7e796e
ICompositor::ComputeTextBoundingBox()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1739
diff
changeset
|
207 else |
373d7f7e796e
ICompositor::ComputeTextBoundingBox()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1739
diff
changeset
|
208 { |
373d7f7e796e
ICompositor::ComputeTextBoundingBox()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1739
diff
changeset
|
209 assert(found->second != NULL); |
373d7f7e796e
ICompositor::ComputeTextBoundingBox()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1739
diff
changeset
|
210 return new TextBoundingBox(found->second->GetAlphabet(), utf8); |
373d7f7e796e
ICompositor::ComputeTextBoundingBox()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1739
diff
changeset
|
211 } |
373d7f7e796e
ICompositor::ComputeTextBoundingBox()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1739
diff
changeset
|
212 } |
373d7f7e796e
ICompositor::ComputeTextBoundingBox()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1739
diff
changeset
|
213 #endif |
597 | 214 } |