comparison OrthancStone/Sources/Toolbox/TextRenderer.cpp @ 1512:244ad1e4e76a

reorganization of folders
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 07 Jul 2020 16:21:02 +0200
parents Framework/Toolbox/TextRenderer.cpp@28c64c246312
children 8563ea5d8ae4
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 "TextRenderer.h"
23
24 #include "../Scene2D/CairoCompositor.h"
25 #include "../Scene2D/ColorTextureSceneLayer.h"
26 #include "../Scene2D/FloatTextureSceneLayer.h"
27 #include "../Scene2D/TextSceneLayer.h"
28 #include "../Fonts/GlyphBitmapAlphabet.h"
29 #include "../Fonts/FontRenderer.h"
30
31 #include <Images/Image.h>
32 #include <Images/ImageProcessing.h>
33 #include <Images/PngWriter.h>
34 #include <Toolbox.h>
35
36 namespace OrthancStone
37 {
38 Orthanc::ImageAccessor* TextRenderer::Render(const std::string& ttf,
39 unsigned int fontSize,
40 const std::string& utf8String)
41 {
42 FontRenderer renderer;
43 renderer.LoadFont(ttf, fontSize);
44
45 // add each char to be rendered to the alphabet
46 std::unique_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
60 std::unique_ptr<Orthanc::ImageAccessor> renderedText(alphabet->RenderText(utf8String));
61
62 // add a blank line on top of the text (to improve bilinear filtering of the topmost line)
63 std::unique_ptr<Orthanc::Image> renderedTextExtended(new Orthanc::Image(renderedText->GetFormat(), renderedText->GetWidth(), renderedText->GetHeight() + 1, true));
64
65 Orthanc::ImageAccessor textRegion;
66 Orthanc::ImageAccessor firstLineRegion;
67
68 renderedTextExtended->GetRegion(firstLineRegion, 0, 0, renderedText->GetWidth(), 1);
69 Orthanc::ImageProcessing::Set(firstLineRegion, 0);
70
71 renderedTextExtended->GetRegion(textRegion, 0, 1, renderedText->GetWidth(), renderedText->GetHeight());
72 Orthanc::ImageProcessing::Copy(textRegion, *renderedText);
73
74 return renderedTextExtended.release();
75 }
76
77
78 Orthanc::ImageAccessor* TextRenderer::RenderWithAlpha(const std::string& ttf,
79 unsigned int fontSize,
80 const std::string& utf8String,
81 uint8_t foreground)
82 {
83 std::unique_ptr<Orthanc::ImageAccessor> renderedText8(Render(ttf, fontSize, utf8String));
84 std::unique_ptr<Orthanc::Image> target(new Orthanc::Image(Orthanc::PixelFormat_RGBA32, renderedText8->GetWidth(), renderedText8->GetHeight(), true));
85
86 Orthanc::ImageProcessing::Set(*target, foreground, foreground, foreground, *renderedText8);
87 return target.release();
88 }
89
90
91 // currently disabled because the background is actually not transparent once we use the Cairo Compositor !
92 //
93 // // renders text in color + a border with alpha in a RGBA32 image
94 // Orthanc::ImageAccessor* TextRenderer::RenderWithAlpha(const std::string& ttf,
95 // unsigned int fontSize,
96 // const std::string& utf8String,
97 // uint8_t foreground,
98 // uint8_t borderColor)
99 // {
100 // std::unique_ptr<Orthanc::ImageAccessor> renderedBorderAlpha(RenderWithAlpha(ttf, fontSize, utf8String, borderColor));
101 // std::unique_ptr<Orthanc::ImageAccessor> renderedTextAlpha(RenderWithAlpha(ttf, fontSize, utf8String, foreground));
102
103 // unsigned int textWidth = renderedBorderAlpha->GetWidth();
104 // unsigned int textHeight = renderedBorderAlpha->GetHeight();
105
106 // Scene2D targetScene;
107 // std::unique_ptr<ColorTextureSceneLayer> borderLayerLeft(new ColorTextureSceneLayer(*renderedBorderAlpha));
108 // std::unique_ptr<ColorTextureSceneLayer> borderLayerRight(new ColorTextureSceneLayer(*renderedBorderAlpha));
109 // std::unique_ptr<ColorTextureSceneLayer> borderLayerTop(new ColorTextureSceneLayer(*renderedBorderAlpha));
110 // std::unique_ptr<ColorTextureSceneLayer> borderLayerBottom(new ColorTextureSceneLayer(*renderedBorderAlpha));
111 // std::unique_ptr<ColorTextureSceneLayer> textLayerCenter(new ColorTextureSceneLayer(*renderedTextAlpha));
112
113 // borderLayerLeft->SetOrigin(0, 1);
114 // borderLayerRight->SetOrigin(2, 1);
115 // borderLayerTop->SetOrigin(1, 0);
116 // borderLayerBottom->SetOrigin(1, 2);
117 // textLayerCenter->SetOrigin(1, 1);
118 // targetScene.SetLayer(1, borderLayerLeft.release());
119 // targetScene.SetLayer(2, borderLayerRight.release());
120 // targetScene.SetLayer(3, borderLayerTop.release());
121 // targetScene.SetLayer(4, borderLayerBottom.release());
122 // targetScene.SetLayer(5, textLayerCenter.release());
123
124 // targetScene.FitContent(textWidth + 2, textHeight + 2);
125 // CairoCompositor compositor(targetScene, textWidth + 2, textHeight + 2);
126 // compositor.Refresh();
127
128 // Orthanc::ImageAccessor canvas;
129 // compositor.GetCanvas().GetReadOnlyAccessor(canvas);
130
131 // std::unique_ptr<Orthanc::Image> output(new Orthanc::Image(Orthanc::PixelFormat_RGBA32, canvas.GetWidth(), canvas.GetHeight(), false));
132 // Orthanc::ImageProcessing::Convert(*output, canvas);
133 // return output.release();
134 // }
135 }