comparison Framework/Toolbox/TextRenderer.cpp @ 1089:998d9e4402e0 broker

integration mainline->broker
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 23 Oct 2019 11:04:47 +0200
parents b8521df3944a
children df76d3083031
comparison
equal deleted inserted replaced
1088:fe723ea10d98 1089:998d9e4402e0
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 "TextRenderer.h"
23
24 #include <Framework/Scene2D/CairoCompositor.h>
25 #include <Framework/Scene2D/ColorTextureSceneLayer.h>
26 #include <Framework/Scene2D/FloatTextureSceneLayer.h>
27 #include <Framework/Scene2D/TextSceneLayer.h>
28 #include <Framework/Fonts/GlyphBitmapAlphabet.h>
29 #include <Framework/Fonts/FontRenderer.h>
30 #include <Core/Images/PngWriter.h>
31 #include <Core/Toolbox.h>
32
33 #include "Core/Images/Image.h"
34 #include "Core/Images/ImageProcessing.h"
35
36 namespace OrthancStone
37 {
38 Orthanc::ImageAccessor* TextRenderer::Render(Orthanc::EmbeddedResources::FileResourceId font,
39 unsigned int fontSize,
40 const std::string& utf8String
41 )
42 {
43 FontRenderer renderer;
44 renderer.LoadFont(font, fontSize);
45
46 // add each char to be rendered to the alphabet
47 std::auto_ptr<GlyphBitmapAlphabet> alphabet(new GlyphBitmapAlphabet);
48
49 size_t posInString = 0;
50 uint32_t unicode;
51 size_t utf8CharLength;
52
53 while (posInString < utf8String.size())
54 {
55 Orthanc::Toolbox::Utf8ToUnicodeCharacter(unicode, utf8CharLength, utf8String, posInString);
56 alphabet->AddUnicodeCharacter(renderer, unicode);
57 posInString += utf8CharLength;
58 }
59
60 return alphabet->RenderText(utf8String);
61 }
62
63
64 Orthanc::ImageAccessor* TextRenderer::RenderWithAlpha(Orthanc::EmbeddedResources::FileResourceId resource,
65 unsigned int fontSize,
66 const std::string& utf8String,
67 uint8_t foreground)
68 {
69 std::auto_ptr<Orthanc::ImageAccessor> renderedText8(Render(resource, fontSize, utf8String));
70 std::auto_ptr<Orthanc::Image> target(new Orthanc::Image(Orthanc::PixelFormat_RGBA32, renderedText8->GetWidth(), renderedText8->GetHeight(), true));
71
72 Orthanc::ImageProcessing::Set(*target, foreground, foreground, foreground, *renderedText8);
73 return target.release();
74 }
75
76
77 // currently disabled because the background is actually not transparent once we use the Cairo Compositor !
78 //
79 // // renders text in color + a border with alpha in a RGBA32 image
80 // Orthanc::ImageAccessor* TextRenderer::RenderWithAlpha(Orthanc::EmbeddedResources::FileResourceId resource,
81 // unsigned int fontSize,
82 // const std::string& utf8String,
83 // uint8_t foreground,
84 // uint8_t borderColor)
85 // {
86 // std::auto_ptr<Orthanc::ImageAccessor> renderedBorderAlpha(RenderWithAlpha(resource, fontSize, utf8String, borderColor));
87 // std::auto_ptr<Orthanc::ImageAccessor> renderedTextAlpha(RenderWithAlpha(resource, fontSize, utf8String, foreground));
88
89 // unsigned int textWidth = renderedBorderAlpha->GetWidth();
90 // unsigned int textHeight = renderedBorderAlpha->GetHeight();
91
92 // Scene2D targetScene;
93 // std::auto_ptr<ColorTextureSceneLayer> borderLayerLeft(new ColorTextureSceneLayer(*renderedBorderAlpha));
94 // std::auto_ptr<ColorTextureSceneLayer> borderLayerRight(new ColorTextureSceneLayer(*renderedBorderAlpha));
95 // std::auto_ptr<ColorTextureSceneLayer> borderLayerTop(new ColorTextureSceneLayer(*renderedBorderAlpha));
96 // std::auto_ptr<ColorTextureSceneLayer> borderLayerBottom(new ColorTextureSceneLayer(*renderedBorderAlpha));
97 // std::auto_ptr<ColorTextureSceneLayer> textLayerCenter(new ColorTextureSceneLayer(*renderedTextAlpha));
98
99 // borderLayerLeft->SetOrigin(0, 1);
100 // borderLayerRight->SetOrigin(2, 1);
101 // borderLayerTop->SetOrigin(1, 0);
102 // borderLayerBottom->SetOrigin(1, 2);
103 // textLayerCenter->SetOrigin(1, 1);
104 // targetScene.SetLayer(1, borderLayerLeft.release());
105 // targetScene.SetLayer(2, borderLayerRight.release());
106 // targetScene.SetLayer(3, borderLayerTop.release());
107 // targetScene.SetLayer(4, borderLayerBottom.release());
108 // targetScene.SetLayer(5, textLayerCenter.release());
109
110 // targetScene.FitContent(textWidth + 2, textHeight + 2);
111 // CairoCompositor compositor(targetScene, textWidth + 2, textHeight + 2);
112 // compositor.Refresh();
113
114 // Orthanc::ImageAccessor canvas;
115 // compositor.GetCanvas().GetReadOnlyAccessor(canvas);
116
117 // std::auto_ptr<Orthanc::Image> output(new Orthanc::Image(Orthanc::PixelFormat_RGBA32, canvas.GetWidth(), canvas.GetHeight(), false));
118 // Orthanc::ImageProcessing::Convert(*output, canvas);
119 // return output.release();
120 // }
121 }