Mercurial > hg > orthanc-stone
annotate Framework/Scene2D/CairoCompositor.cpp @ 1039:3a35d8397559
merge
author | Alain Mazy <alain@mazy.be> |
---|---|
date | Mon, 07 Oct 2019 22:37:24 +0200 |
parents | 1091b2adeb5a |
children | d10d2acb8a02 2d8ab34c8c91 |
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 | |
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU Affero General Public License | |
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 | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
21 | |
22 #include "CairoCompositor.h" | |
23 | |
24 #include "Internals/CairoColorTextureRenderer.h" | |
25 #include "Internals/CairoFloatTextureRenderer.h" | |
26 #include "Internals/CairoInfoPanelRenderer.h" | |
768
55411e7da2f7
LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
27 #include "Internals/CairoLookupTableTextureRenderer.h" |
597 | 28 #include "Internals/CairoPolylineRenderer.h" |
29 #include "Internals/CairoTextRenderer.h" | |
30 | |
31 #include <Core/OrthancException.h> | |
32 | |
33 namespace OrthancStone | |
34 { | |
35 cairo_t* CairoCompositor::GetCairoContext() | |
36 { | |
37 if (context_.get() == NULL) | |
38 { | |
39 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
40 } | |
41 else | |
42 { | |
43 return context_->GetObject(); | |
44 } | |
45 } | |
46 | |
47 Internals::CompositorHelper::ILayerRenderer* CairoCompositor::Create(const ISceneLayer& layer) | |
48 { | |
49 switch (layer.GetType()) | |
50 { | |
51 case ISceneLayer::Type_Polyline: | |
52 return new Internals::CairoPolylineRenderer(*this, layer); | |
53 | |
54 case ISceneLayer::Type_InfoPanel: | |
55 return new Internals::CairoInfoPanelRenderer(*this, layer); | |
56 | |
57 case ISceneLayer::Type_ColorTexture: | |
58 return new Internals::CairoColorTextureRenderer(*this, layer); | |
59 | |
60 case ISceneLayer::Type_FloatTexture: | |
61 return new Internals::CairoFloatTextureRenderer(*this, layer); | |
62 | |
768
55411e7da2f7
LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
63 case ISceneLayer::Type_LookupTableTexture: |
55411e7da2f7
LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
64 return new Internals::CairoLookupTableTextureRenderer(*this, layer); |
55411e7da2f7
LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
65 |
597 | 66 case ISceneLayer::Type_Text: |
67 { | |
68 const TextSceneLayer& l = dynamic_cast<const TextSceneLayer&>(layer); | |
69 | |
70 Fonts::const_iterator found = fonts_.find(l.GetFontIndex()); | |
71 if (found == fonts_.end()) | |
72 { | |
73 return NULL; | |
74 } | |
75 else | |
76 { | |
77 assert(found->second != NULL); | |
78 return new Internals::CairoTextRenderer(*this, *found->second, l); | |
79 } | |
80 } | |
81 | |
82 default: | |
83 return NULL; | |
84 } | |
85 } | |
86 | |
87 | |
600
6129b1e5ba42
BasicScene SDL sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
598
diff
changeset
|
88 CairoCompositor::CairoCompositor(const Scene2D& scene, |
597 | 89 unsigned int canvasWidth, |
90 unsigned int canvasHeight) : | |
91 helper_(scene, *this) | |
92 { | |
909 | 93 UpdateSize(canvasWidth, canvasHeight); |
94 } | |
95 | |
96 void CairoCompositor::UpdateSize(unsigned int canvasWidth, | |
97 unsigned int canvasHeight) | |
98 { | |
597 | 99 canvas_.SetSize(canvasWidth, canvasHeight, false); |
100 } | |
101 | |
102 CairoCompositor::~CairoCompositor() | |
103 { | |
104 for (Fonts::iterator it = fonts_.begin(); it != fonts_.end(); ++it) | |
105 { | |
106 assert(it->second != NULL); | |
107 delete it->second; | |
108 } | |
109 } | |
110 | |
111 | |
112 void CairoCompositor::SetFont(size_t index, | |
113 GlyphBitmapAlphabet* dict) // Takes ownership | |
114 { | |
115 if (dict == NULL) | |
116 { | |
117 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
118 } | |
119 else | |
120 { | |
121 std::auto_ptr<GlyphBitmapAlphabet> protection(dict); | |
122 | |
123 Fonts::iterator found = fonts_.find(index); | |
124 | |
125 if (found == fonts_.end()) | |
126 { | |
127 fonts_[index] = protection.release(); | |
128 } | |
129 else | |
130 { | |
131 assert(found->second != NULL); | |
132 delete found->second; | |
133 | |
134 found->second = protection.release(); | |
135 } | |
136 } | |
137 } | |
138 | |
139 | |
140 #if ORTHANC_ENABLE_LOCALE == 1 | |
141 void CairoCompositor::SetFont(size_t index, | |
142 Orthanc::EmbeddedResources::FileResourceId resource, | |
143 unsigned int fontSize, | |
144 Orthanc::Encoding codepage) | |
145 { | |
146 FontRenderer renderer; | |
598 | 147 renderer.LoadFont(resource, fontSize); |
597 | 148 |
149 std::auto_ptr<GlyphBitmapAlphabet> alphabet(new GlyphBitmapAlphabet); | |
150 alphabet->LoadCodepage(renderer, codepage); | |
151 | |
152 SetFont(index, alphabet.release()); | |
153 } | |
154 #endif | |
155 | |
156 | |
157 void CairoCompositor::Refresh() | |
158 { | |
159 context_.reset(new CairoContext(canvas_)); | |
160 | |
161 // https://www.cairographics.org/FAQ/#clear_a_surface | |
162 cairo_set_source_rgba(context_->GetObject(), 0, 0, 0, 255); | |
163 cairo_paint(context_->GetObject()); | |
164 | |
165 helper_.Refresh(canvas_.GetWidth(), canvas_.GetHeight()); | |
166 context_.reset(); | |
167 } | |
168 | |
169 | |
170 Orthanc::ImageAccessor* CairoCompositor::RenderText(size_t fontIndex, | |
171 const std::string& utf8) const | |
172 { | |
173 Fonts::const_iterator found = fonts_.find(fontIndex); | |
174 | |
175 if (found == fonts_.end()) | |
176 { | |
177 return NULL; | |
178 } | |
179 else | |
180 { | |
181 assert(found->second != NULL); | |
182 return found->second->RenderText(utf8); | |
183 } | |
184 } | |
185 } |