comparison OrthancStone/Sources/Scene2D/CairoCompositor.cpp @ 1512:244ad1e4e76a

reorganization of folders
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 07 Jul 2020 16:21:02 +0200
parents Framework/Scene2D/CairoCompositor.cpp@28c64c246312
children 92fca2b3ba3d
comparison
equal deleted inserted replaced
1511:9dfeee74c1e6 1512:244ad1e4e76a
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-2020 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"
27 #include "Internals/CairoLookupTableTextureRenderer.h"
28 #include "Internals/CairoPolylineRenderer.h"
29 #include "Internals/CairoTextRenderer.h"
30
31 #include <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
63 case ISceneLayer::Type_LookupTableTexture:
64 return new Internals::CairoLookupTableTextureRenderer(*this, layer);
65
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
88 CairoCompositor::CairoCompositor(unsigned int canvasWidth,
89 unsigned int canvasHeight)
90 {
91 ResetScene();
92 UpdateSize(canvasWidth, canvasHeight);
93 }
94
95 void CairoCompositor::UpdateSize(unsigned int canvasWidth,
96 unsigned int canvasHeight)
97 {
98 canvas_.SetSize(canvasWidth, canvasHeight, false);
99 }
100
101 CairoCompositor::~CairoCompositor()
102 {
103 for (Fonts::iterator it = fonts_.begin(); it != fonts_.end(); ++it)
104 {
105 assert(it->second != NULL);
106 delete it->second;
107 }
108 }
109
110
111 void CairoCompositor::SetFont(size_t index,
112 GlyphBitmapAlphabet* dict) // Takes ownership
113 {
114 if (dict == NULL)
115 {
116 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
117 }
118 else
119 {
120 std::unique_ptr<GlyphBitmapAlphabet> protection(dict);
121
122 Fonts::iterator found = fonts_.find(index);
123
124 if (found == fonts_.end())
125 {
126 fonts_[index] = protection.release();
127 }
128 else
129 {
130 assert(found->second != NULL);
131 delete found->second;
132
133 found->second = protection.release();
134 }
135 }
136 }
137
138
139 #if ORTHANC_ENABLE_LOCALE == 1
140 void CairoCompositor::SetFont(size_t index,
141 const std::string& ttf,
142 unsigned int fontSize,
143 Orthanc::Encoding codepage)
144 {
145 FontRenderer renderer;
146 renderer.LoadFont(ttf, fontSize);
147
148 std::unique_ptr<GlyphBitmapAlphabet> alphabet(new GlyphBitmapAlphabet);
149 alphabet->LoadCodepage(renderer, codepage);
150
151 SetFont(index, alphabet.release());
152 }
153 #endif
154
155
156 void CairoCompositor::Refresh(const Scene2D& scene)
157 {
158 context_.reset(new CairoContext(canvas_));
159
160 // https://www.cairographics.org/FAQ/#clear_a_surface
161 cairo_set_source_rgba(context_->GetObject(), 0, 0, 0, 255);
162 cairo_paint(context_->GetObject());
163
164 helper_->Refresh(scene, canvas_.GetWidth(), canvas_.GetHeight());
165 context_.reset();
166 }
167
168
169 Orthanc::ImageAccessor* CairoCompositor::RenderText(size_t fontIndex,
170 const std::string& utf8) const
171 {
172 Fonts::const_iterator found = fonts_.find(fontIndex);
173
174 if (found == fonts_.end())
175 {
176 return NULL;
177 }
178 else
179 {
180 assert(found->second != NULL);
181 return found->second->RenderText(utf8);
182 }
183 }
184 }