Mercurial > hg > orthanc-stone
annotate Framework/Scene2D/CairoCompositor.cpp @ 879:12b591d5d63c am-dev
some Qt integration (wip)
author | Alain Mazy <alain@mazy.be> |
---|---|
date | Fri, 05 Jul 2019 14:52:43 +0200 |
parents | 55411e7da2f7 |
children | 7a7e4e1f558f |
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 | |
48 Internals::CompositorHelper::ILayerRenderer* CairoCompositor::Create(const ISceneLayer& layer) | |
49 { | |
50 switch (layer.GetType()) | |
51 { | |
52 case ISceneLayer::Type_Polyline: | |
53 return new Internals::CairoPolylineRenderer(*this, layer); | |
54 | |
55 case ISceneLayer::Type_InfoPanel: | |
56 return new Internals::CairoInfoPanelRenderer(*this, layer); | |
57 | |
58 case ISceneLayer::Type_ColorTexture: | |
59 return new Internals::CairoColorTextureRenderer(*this, layer); | |
60 | |
61 case ISceneLayer::Type_FloatTexture: | |
62 return new Internals::CairoFloatTextureRenderer(*this, layer); | |
63 | |
768
55411e7da2f7
LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
64 case ISceneLayer::Type_LookupTableTexture: |
55411e7da2f7
LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
65 return new Internals::CairoLookupTableTextureRenderer(*this, layer); |
55411e7da2f7
LookupTableTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
66 |
597 | 67 case ISceneLayer::Type_Text: |
68 { | |
69 const TextSceneLayer& l = dynamic_cast<const TextSceneLayer&>(layer); | |
70 | |
71 Fonts::const_iterator found = fonts_.find(l.GetFontIndex()); | |
72 if (found == fonts_.end()) | |
73 { | |
74 return NULL; | |
75 } | |
76 else | |
77 { | |
78 assert(found->second != NULL); | |
79 return new Internals::CairoTextRenderer(*this, *found->second, l); | |
80 } | |
81 } | |
82 | |
83 default: | |
84 return NULL; | |
85 } | |
86 } | |
87 | |
88 | |
600
6129b1e5ba42
BasicScene SDL sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
598
diff
changeset
|
89 CairoCompositor::CairoCompositor(const Scene2D& scene, |
597 | 90 unsigned int canvasWidth, |
91 unsigned int canvasHeight) : | |
92 helper_(scene, *this) | |
93 { | |
94 canvas_.SetSize(canvasWidth, canvasHeight, false); | |
95 } | |
96 | |
97 | |
98 CairoCompositor::~CairoCompositor() | |
99 { | |
100 for (Fonts::iterator it = fonts_.begin(); it != fonts_.end(); ++it) | |
101 { | |
102 assert(it->second != NULL); | |
103 delete it->second; | |
104 } | |
105 } | |
106 | |
107 | |
108 void CairoCompositor::SetFont(size_t index, | |
109 GlyphBitmapAlphabet* dict) // Takes ownership | |
110 { | |
111 if (dict == NULL) | |
112 { | |
113 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
114 } | |
115 else | |
116 { | |
117 std::auto_ptr<GlyphBitmapAlphabet> protection(dict); | |
118 | |
119 Fonts::iterator found = fonts_.find(index); | |
120 | |
121 if (found == fonts_.end()) | |
122 { | |
123 fonts_[index] = protection.release(); | |
124 } | |
125 else | |
126 { | |
127 assert(found->second != NULL); | |
128 delete found->second; | |
129 | |
130 found->second = protection.release(); | |
131 } | |
132 } | |
133 } | |
134 | |
135 | |
136 #if ORTHANC_ENABLE_LOCALE == 1 | |
137 void CairoCompositor::SetFont(size_t index, | |
138 Orthanc::EmbeddedResources::FileResourceId resource, | |
139 unsigned int fontSize, | |
140 Orthanc::Encoding codepage) | |
141 { | |
142 FontRenderer renderer; | |
598 | 143 renderer.LoadFont(resource, fontSize); |
597 | 144 |
145 std::auto_ptr<GlyphBitmapAlphabet> alphabet(new GlyphBitmapAlphabet); | |
146 alphabet->LoadCodepage(renderer, codepage); | |
147 | |
148 SetFont(index, alphabet.release()); | |
149 } | |
150 #endif | |
151 | |
152 | |
153 void CairoCompositor::Refresh() | |
154 { | |
155 context_.reset(new CairoContext(canvas_)); | |
156 | |
157 // https://www.cairographics.org/FAQ/#clear_a_surface | |
158 cairo_set_source_rgba(context_->GetObject(), 0, 0, 0, 255); | |
159 cairo_paint(context_->GetObject()); | |
160 | |
161 helper_.Refresh(canvas_.GetWidth(), canvas_.GetHeight()); | |
162 context_.reset(); | |
163 } | |
164 | |
165 | |
166 Orthanc::ImageAccessor* CairoCompositor::RenderText(size_t fontIndex, | |
167 const std::string& utf8) const | |
168 { | |
169 Fonts::const_iterator found = fonts_.find(fontIndex); | |
170 | |
171 if (found == fonts_.end()) | |
172 { | |
173 return NULL; | |
174 } | |
175 else | |
176 { | |
177 assert(found->second != NULL); | |
178 return found->second->RenderText(utf8); | |
179 } | |
180 } | |
181 } |