Mercurial > hg > orthanc
comparison OrthancFramework/Sources/Images/Font.cpp @ 4044:d25f4c0fa160 framework
splitting code into OrthancFramework and OrthancServer
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 10 Jun 2020 20:30:34 +0200 |
parents | Core/Images/Font.cpp@058b5ade8acd |
children | bf7b9edf6b81 |
comparison
equal
deleted
inserted
replaced
4043:6c6239aec462 | 4044:d25f4c0fa160 |
---|---|
1 /** | |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
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 General Public License as | |
9 * published by the Free Software Foundation, either version 3 of the | |
10 * License, or (at your option) any later version. | |
11 * | |
12 * In addition, as a special exception, the copyright holders of this | |
13 * program give permission to link the code of its release with the | |
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
15 * that use the same license as the "OpenSSL" library), and distribute | |
16 * the linked executables. You must obey the GNU General Public License | |
17 * in all respects for all of the code used other than "OpenSSL". If you | |
18 * modify file(s) with this exception, you may extend this exception to | |
19 * your version of the file(s), but you are not obligated to do so. If | |
20 * you do not wish to do so, delete this exception statement from your | |
21 * version. If you delete this exception statement from all source files | |
22 * in the program, then also delete it here. | |
23 * | |
24 * This program is distributed in the hope that it will be useful, but | |
25 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
27 * General Public License for more details. | |
28 * | |
29 * You should have received a copy of the GNU General Public License | |
30 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
31 **/ | |
32 | |
33 | |
34 #include "../PrecompiledHeaders.h" | |
35 #include "Font.h" | |
36 | |
37 #if !defined(ORTHANC_ENABLE_LOCALE) | |
38 # error ORTHANC_ENABLE_LOCALE must be defined to use this file | |
39 #endif | |
40 | |
41 #if ORTHANC_SANDBOXED == 0 | |
42 # include "../SystemToolbox.h" | |
43 #endif | |
44 | |
45 #include "../OrthancException.h" | |
46 #include "../Toolbox.h" | |
47 #include "Image.h" | |
48 #include "ImageProcessing.h" | |
49 | |
50 #include <stdio.h> | |
51 #include <memory> | |
52 #include <boost/lexical_cast.hpp> | |
53 | |
54 namespace Orthanc | |
55 { | |
56 Font::~Font() | |
57 { | |
58 for (Characters::iterator it = characters_.begin(); | |
59 it != characters_.end(); ++it) | |
60 { | |
61 delete it->second; | |
62 } | |
63 } | |
64 | |
65 | |
66 void Font::LoadFromMemory(const std::string& font) | |
67 { | |
68 Json::Value v; | |
69 Json::Reader reader; | |
70 if (!reader.parse(font, v) || | |
71 v.type() != Json::objectValue || | |
72 !v.isMember("Name") || | |
73 !v.isMember("Size") || | |
74 !v.isMember("Characters") || | |
75 v["Name"].type() != Json::stringValue || | |
76 v["Size"].type() != Json::intValue || | |
77 v["Characters"].type() != Json::objectValue) | |
78 { | |
79 throw OrthancException(ErrorCode_BadFont); | |
80 } | |
81 | |
82 name_ = v["Name"].asString(); | |
83 size_ = v["Size"].asUInt(); | |
84 maxHeight_ = 0; | |
85 | |
86 Json::Value::Members characters = v["Characters"].getMemberNames(); | |
87 | |
88 for (size_t i = 0; i < characters.size(); i++) | |
89 { | |
90 const Json::Value& info = v["Characters"][characters[i]]; | |
91 if (info.type() != Json::objectValue || | |
92 !info.isMember("Advance") || | |
93 !info.isMember("Bitmap") || | |
94 !info.isMember("Height") || | |
95 !info.isMember("Top") || | |
96 !info.isMember("Width") || | |
97 info["Advance"].type() != Json::intValue || | |
98 info["Bitmap"].type() != Json::arrayValue || | |
99 info["Height"].type() != Json::intValue || | |
100 info["Top"].type() != Json::intValue || | |
101 info["Width"].type() != Json::intValue) | |
102 { | |
103 throw OrthancException(ErrorCode_BadFont); | |
104 } | |
105 | |
106 std::unique_ptr<Character> c(new Character); | |
107 | |
108 c->advance_ = info["Advance"].asUInt(); | |
109 c->height_ = info["Height"].asUInt(); | |
110 c->top_ = info["Top"].asUInt(); | |
111 c->width_ = info["Width"].asUInt(); | |
112 c->bitmap_.resize(info["Bitmap"].size()); | |
113 | |
114 if (c->height_ > maxHeight_) | |
115 { | |
116 maxHeight_ = c->height_; | |
117 } | |
118 | |
119 for (Json::Value::ArrayIndex j = 0; j < info["Bitmap"].size(); j++) | |
120 { | |
121 if (info["Bitmap"][j].type() != Json::intValue) | |
122 { | |
123 throw OrthancException(ErrorCode_BadFont); | |
124 } | |
125 | |
126 int value = info["Bitmap"][j].asInt(); | |
127 if (value < 0 || value > 255) | |
128 { | |
129 throw OrthancException(ErrorCode_BadFont); | |
130 } | |
131 | |
132 c->bitmap_[j] = static_cast<uint8_t>(value); | |
133 } | |
134 | |
135 int index = boost::lexical_cast<int>(characters[i]); | |
136 if (index < 0 || index > 255) | |
137 { | |
138 throw OrthancException(ErrorCode_BadFont); | |
139 } | |
140 | |
141 characters_[static_cast<char>(index)] = c.release(); | |
142 } | |
143 } | |
144 | |
145 | |
146 #if ORTHANC_SANDBOXED == 0 | |
147 void Font::LoadFromFile(const std::string& path) | |
148 { | |
149 std::string font; | |
150 SystemToolbox::ReadFile(font, path); | |
151 LoadFromMemory(font); | |
152 } | |
153 #endif | |
154 | |
155 | |
156 static unsigned int MyMin(unsigned int a, | |
157 unsigned int b) | |
158 { | |
159 return a < b ? a : b; | |
160 } | |
161 | |
162 | |
163 void Font::DrawCharacter(ImageAccessor& target, | |
164 const Character& character, | |
165 int x, | |
166 int y, | |
167 const uint8_t color[4]) const | |
168 { | |
169 // Compute the bounds of the character | |
170 if (x >= static_cast<int>(target.GetWidth()) || | |
171 y >= static_cast<int>(target.GetHeight())) | |
172 { | |
173 // The character is out of the image | |
174 return; | |
175 } | |
176 | |
177 unsigned int left = x < 0 ? -x : 0; | |
178 unsigned int top = y < 0 ? -y : 0; | |
179 unsigned int width = MyMin(character.width_, target.GetWidth() - x); | |
180 unsigned int height = MyMin(character.height_, target.GetHeight() - y); | |
181 | |
182 unsigned int bpp = target.GetBytesPerPixel(); | |
183 | |
184 // Blit the font bitmap OVER the target image | |
185 // https://en.wikipedia.org/wiki/Alpha_compositing | |
186 | |
187 for (unsigned int cy = top; cy < height; cy++) | |
188 { | |
189 uint8_t* p = reinterpret_cast<uint8_t*>(target.GetRow(y + cy)) + (x + left) * bpp; | |
190 unsigned int pos = cy * character.width_ + left; | |
191 | |
192 switch (target.GetFormat()) | |
193 { | |
194 case PixelFormat_Grayscale8: | |
195 { | |
196 assert(bpp == 1); | |
197 for (unsigned int cx = left; cx < width; cx++, pos++, p++) | |
198 { | |
199 uint16_t alpha = character.bitmap_[pos]; | |
200 uint16_t value = alpha * static_cast<uint16_t>(color[0]) + (255 - alpha) * static_cast<uint16_t>(*p); | |
201 *p = static_cast<uint8_t>(value >> 8); | |
202 } | |
203 | |
204 break; | |
205 } | |
206 | |
207 case PixelFormat_RGB24: | |
208 { | |
209 assert(bpp == 3); | |
210 for (unsigned int cx = left; cx < width; cx++, pos++, p += 3) | |
211 { | |
212 uint16_t alpha = character.bitmap_[pos]; | |
213 for (uint8_t i = 0; i < 3; i++) | |
214 { | |
215 uint16_t value = alpha * static_cast<uint16_t>(color[i]) + (255 - alpha) * static_cast<uint16_t>(p[i]); | |
216 p[i] = static_cast<uint8_t>(value >> 8); | |
217 } | |
218 } | |
219 | |
220 break; | |
221 } | |
222 | |
223 case PixelFormat_RGBA32: | |
224 case PixelFormat_BGRA32: | |
225 { | |
226 assert(bpp == 4); | |
227 | |
228 for (unsigned int cx = left; cx < width; cx++, pos++, p += 4) | |
229 { | |
230 float alpha = static_cast<float>(character.bitmap_[pos]) / 255.0f; | |
231 float beta = (1.0f - alpha) * static_cast<float>(p[3]) / 255.0f; | |
232 float denom = 1.0f / (alpha + beta); | |
233 | |
234 for (uint8_t i = 0; i < 3; i++) | |
235 { | |
236 p[i] = static_cast<uint8_t>((alpha * static_cast<float>(color[i]) + | |
237 beta * static_cast<float>(p[i])) * denom); | |
238 } | |
239 | |
240 p[3] = static_cast<uint8_t>(255.0f * (alpha + beta)); | |
241 } | |
242 | |
243 break; | |
244 } | |
245 | |
246 default: | |
247 throw OrthancException(ErrorCode_NotImplemented); | |
248 } | |
249 } | |
250 | |
251 } | |
252 | |
253 | |
254 void Font::DrawInternal(ImageAccessor& target, | |
255 const std::string& utf8, | |
256 int x, | |
257 int y, | |
258 const uint8_t color[4]) const | |
259 { | |
260 if (target.GetFormat() != PixelFormat_Grayscale8 && | |
261 target.GetFormat() != PixelFormat_RGB24 && | |
262 target.GetFormat() != PixelFormat_RGBA32 && | |
263 target.GetFormat() != PixelFormat_BGRA32) | |
264 { | |
265 throw OrthancException(ErrorCode_NotImplemented); | |
266 } | |
267 | |
268 int a = x; | |
269 | |
270 #if ORTHANC_ENABLE_LOCALE == 1 | |
271 std::string s = Toolbox::ConvertFromUtf8(utf8, Encoding_Latin1); | |
272 #else | |
273 // If the locale support is disabled, simply drop non-ASCII | |
274 // characters from the source UTF-8 string | |
275 std::string s = Toolbox::ConvertToAscii(utf8); | |
276 #endif | |
277 | |
278 for (size_t i = 0; i < s.size(); i++) | |
279 { | |
280 if (s[i] == '\n') | |
281 { | |
282 // Go to the next line | |
283 a = x; | |
284 y += maxHeight_ + 1; | |
285 } | |
286 else | |
287 { | |
288 Characters::const_iterator c = characters_.find(s[i]); | |
289 if (c != characters_.end()) | |
290 { | |
291 DrawCharacter(target, *c->second, a, y + static_cast<int>(c->second->top_), color); | |
292 a += c->second->advance_; | |
293 } | |
294 } | |
295 } | |
296 } | |
297 | |
298 | |
299 void Font::Draw(ImageAccessor& target, | |
300 const std::string& utf8, | |
301 int x, | |
302 int y, | |
303 uint8_t grayscale) const | |
304 { | |
305 uint8_t color[4] = { grayscale, grayscale, grayscale, 255 }; | |
306 DrawInternal(target, utf8, x, y, color); | |
307 } | |
308 | |
309 | |
310 void Font::Draw(ImageAccessor& target, | |
311 const std::string& utf8, | |
312 int x, | |
313 int y, | |
314 uint8_t r, | |
315 uint8_t g, | |
316 uint8_t b) const | |
317 { | |
318 uint8_t color[4]; | |
319 | |
320 switch (target.GetFormat()) | |
321 { | |
322 case PixelFormat_BGRA32: | |
323 color[0] = b; | |
324 color[1] = g; | |
325 color[2] = r; | |
326 color[3] = 255; | |
327 break; | |
328 | |
329 default: | |
330 color[0] = r; | |
331 color[1] = g; | |
332 color[2] = b; | |
333 color[3] = 255; | |
334 break; | |
335 } | |
336 | |
337 DrawInternal(target, utf8, x, y, color); | |
338 } | |
339 | |
340 | |
341 void Font::ComputeTextExtent(unsigned int& width, | |
342 unsigned int& height, | |
343 const std::string& utf8) const | |
344 { | |
345 width = 0; | |
346 height = 0; | |
347 | |
348 #if ORTHANC_ENABLE_LOCALE == 1 | |
349 std::string s = Toolbox::ConvertFromUtf8(utf8, Encoding_Latin1); | |
350 #else | |
351 // If the locale support is disabled, simply drop non-ASCII | |
352 // characters from the source UTF-8 string | |
353 std::string s = Toolbox::ConvertToAscii(utf8); | |
354 #endif | |
355 | |
356 // Compute the text extent | |
357 unsigned int x = 0; | |
358 unsigned int y = 0; | |
359 | |
360 for (size_t i = 0; i < s.size(); i++) | |
361 { | |
362 if (s[i] == '\n') | |
363 { | |
364 // Go to the next line | |
365 x = 0; | |
366 y += (maxHeight_ + 1); | |
367 } | |
368 else | |
369 { | |
370 Characters::const_iterator c = characters_.find(s[i]); | |
371 if (c != characters_.end()) | |
372 { | |
373 x += c->second->advance_; | |
374 | |
375 unsigned int bottom = y + c->second->top_ + c->second->height_; | |
376 if (bottom > height) | |
377 { | |
378 height = bottom; | |
379 } | |
380 | |
381 if (x > width) | |
382 { | |
383 width = x; | |
384 } | |
385 } | |
386 } | |
387 } | |
388 } | |
389 | |
390 | |
391 ImageAccessor* Font::Render(const std::string& utf8, | |
392 PixelFormat format, | |
393 uint8_t r, | |
394 uint8_t g, | |
395 uint8_t b) const | |
396 { | |
397 unsigned int width, height; | |
398 ComputeTextExtent(width, height, utf8); | |
399 | |
400 std::unique_ptr<ImageAccessor> target(new Image(format, width, height, false)); | |
401 ImageProcessing::Set(*target, 0, 0, 0, 255); | |
402 Draw(*target, utf8, 0, 0, r, g, b); | |
403 | |
404 return target.release(); | |
405 } | |
406 | |
407 | |
408 ImageAccessor* Font::RenderAlpha(const std::string& utf8) const | |
409 { | |
410 unsigned int width, height; | |
411 ComputeTextExtent(width, height, utf8); | |
412 | |
413 std::unique_ptr<ImageAccessor> target(new Image(PixelFormat_Grayscale8, width, height, false)); | |
414 ImageProcessing::Set(*target, 0); | |
415 Draw(*target, utf8, 0, 0, 255); | |
416 | |
417 return target.release(); | |
418 } | |
419 } |