comparison Core/Images/Font.cpp @ 2900:668d5ad73c74

Font::Render()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 22 Oct 2018 14:20:21 +0200
parents 52b017d22a4f
children 93c65e3a6bb1
comparison
equal deleted inserted replaced
2899:5dd649de253d 2900:668d5ad73c74
40 40
41 #if ORTHANC_SANDBOXED == 0 41 #if ORTHANC_SANDBOXED == 0
42 # include "../SystemToolbox.h" 42 # include "../SystemToolbox.h"
43 #endif 43 #endif
44 44
45 #include "../OrthancException.h"
45 #include "../Toolbox.h" 46 #include "../Toolbox.h"
46 #include "../OrthancException.h" 47 #include "Image.h"
48 #include "ImageProcessing.h"
47 49
48 #include <stdio.h> 50 #include <stdio.h>
49 #include <memory> 51 #include <memory>
50 #include <boost/lexical_cast.hpp> 52 #include <boost/lexical_cast.hpp>
51 53
333 } 335 }
334 336
335 DrawInternal(target, utf8, x, y, color); 337 DrawInternal(target, utf8, x, y, color);
336 } 338 }
337 339
340
341 void Font::ComputeTextExtent(unsigned int& width,
342 unsigned int& height,
343 const std::string& utf8) const
344 {
345 width = 0;
346
347 #if ORTHANC_ENABLE_LOCALE == 1
348 std::string s = Toolbox::ConvertFromUtf8(utf8, Encoding_Latin1);
349 #else
350 // If the locale support is disabled, simply drop non-ASCII
351 // characters from the source UTF-8 string
352 std::string s = Toolbox::ConvertToAscii(utf8);
353 #endif
354
355 // Compute the text extent
356 unsigned int x = 0;
357 unsigned int countLines = 0;
358
359 for (size_t i = 0; i < s.size(); i++)
360 {
361 if (s[i] == '\n')
362 {
363 // Go to the next line
364 x = 0;
365
366 countLines ++;
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 if (countLines == 0)
376 {
377 countLines = 1;
378 }
379
380 if (x > width)
381 {
382 width = x;
383 }
384 }
385 }
386 }
387
388 height = countLines * (maxHeight_ + 1);
389 }
390
391
392 ImageAccessor* Font::Render(const std::string& utf8,
393 PixelFormat format,
394 uint8_t r,
395 uint8_t g,
396 uint8_t b) const
397 {
398 unsigned int width, height;
399 ComputeTextExtent(width, height, utf8);
400
401 std::auto_ptr<ImageAccessor> target(new Image(format, width, height, false));
402 ImageProcessing::Set(*target, 0, 0, 0, 255);
403 Draw(*target, utf8, 0, 0, r, g, b);
404
405 return target.release();
406 }
407
408
409 ImageAccessor* Font::RenderAlpha(const std::string& utf8) const
410 {
411 unsigned int width, height;
412 ComputeTextExtent(width, height, utf8);
413
414 std::auto_ptr<ImageAccessor> target(new Image(PixelFormat_Grayscale8, width, height, false));
415 ImageProcessing::Set(*target, 0);
416 Draw(*target, utf8, 0, 0, 255);
417
418 return target.release();
419 }
338 } 420 }