comparison Framework/Toolbox/TextRenderer.cpp @ 1085:63539e826355

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