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