annotate Framework/Toolbox/TextRenderer.cpp @ 1488:7f16987131e1

Missing include + docs + public getter for volume geometry in multiframe loader
author Benjamin Golinvaux <bgo@osimis.io>
date Tue, 23 Jun 2020 13:44:23 +0200
parents 28c64c246312
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1463
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
1 /**
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
2 * Stone of Orthanc
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
4 * Department, University Hospital of Liege, Belgium
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
6 *
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
7 * This program is free software: you can redistribute it and/or
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
8 * modify it under the terms of the GNU Affero General Public License
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
9 * as published by the Free Software Foundation, either version 3 of
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
10 * the License, or (at your option) any later version.
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
11 *
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
12 * This program is distributed in the hope that it will be useful, but
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
15 * Affero General Public License for more details.
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
16 *
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
17 * You should have received a copy of the GNU Affero General Public License
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
19 **/
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
20
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
21
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
22 #include "TextRenderer.h"
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
23
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
24 #include "../Scene2D/CairoCompositor.h"
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
25 #include "../Scene2D/ColorTextureSceneLayer.h"
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
26 #include "../Scene2D/FloatTextureSceneLayer.h"
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
27 #include "../Scene2D/TextSceneLayer.h"
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
28 #include "../Fonts/GlyphBitmapAlphabet.h"
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
29 #include "../Fonts/FontRenderer.h"
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
30
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
31 #include <Images/Image.h>
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
32 #include <Images/ImageProcessing.h>
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
33 #include <Images/PngWriter.h>
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
34 #include <Toolbox.h>
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
35
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
36 namespace OrthancStone
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
37 {
1471
28c64c246312 working on a shared library
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1463
diff changeset
38 Orthanc::ImageAccessor* TextRenderer::Render(const std::string& ttf,
1463
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
39 unsigned int fontSize,
1471
28c64c246312 working on a shared library
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1463
diff changeset
40 const std::string& utf8String)
1463
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
41 {
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
42 FontRenderer renderer;
1471
28c64c246312 working on a shared library
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1463
diff changeset
43 renderer.LoadFont(ttf, fontSize);
1463
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
44
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
45 // add each char to be rendered to the alphabet
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
46 std::unique_ptr<GlyphBitmapAlphabet> alphabet(new GlyphBitmapAlphabet);
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
47
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
48 size_t posInString = 0;
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
49 uint32_t unicode;
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
50 size_t utf8CharLength;
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
51
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
52 while (posInString < utf8String.size())
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
53 {
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
54 Orthanc::Toolbox::Utf8ToUnicodeCharacter(unicode, utf8CharLength, utf8String, posInString);
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
55 alphabet->AddUnicodeCharacter(renderer, unicode);
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
56 posInString += utf8CharLength;
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
57 }
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
58
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
59
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
60 std::unique_ptr<Orthanc::ImageAccessor> renderedText(alphabet->RenderText(utf8String));
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
61
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
62 // add a blank line on top of the text (to improve bilinear filtering of the topmost line)
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
63 std::unique_ptr<Orthanc::Image> renderedTextExtended(new Orthanc::Image(renderedText->GetFormat(), renderedText->GetWidth(), renderedText->GetHeight() + 1, true));
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
64
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
65 Orthanc::ImageAccessor textRegion;
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
66 Orthanc::ImageAccessor firstLineRegion;
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
67
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
68 renderedTextExtended->GetRegion(firstLineRegion, 0, 0, renderedText->GetWidth(), 1);
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
69 Orthanc::ImageProcessing::Set(firstLineRegion, 0);
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
70
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
71 renderedTextExtended->GetRegion(textRegion, 0, 1, renderedText->GetWidth(), renderedText->GetHeight());
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
72 Orthanc::ImageProcessing::Copy(textRegion, *renderedText);
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
73
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
74 return renderedTextExtended.release();
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
75 }
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
76
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
77
1471
28c64c246312 working on a shared library
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1463
diff changeset
78 Orthanc::ImageAccessor* TextRenderer::RenderWithAlpha(const std::string& ttf,
1463
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
79 unsigned int fontSize,
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
80 const std::string& utf8String,
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
81 uint8_t foreground)
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
82 {
1471
28c64c246312 working on a shared library
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1463
diff changeset
83 std::unique_ptr<Orthanc::ImageAccessor> renderedText8(Render(ttf, fontSize, utf8String));
1463
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
84 std::unique_ptr<Orthanc::Image> target(new Orthanc::Image(Orthanc::PixelFormat_RGBA32, renderedText8->GetWidth(), renderedText8->GetHeight(), true));
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
85
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
86 Orthanc::ImageProcessing::Set(*target, foreground, foreground, foreground, *renderedText8);
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
87 return target.release();
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
88 }
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
89
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
90
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
91 // currently disabled because the background is actually not transparent once we use the Cairo Compositor !
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
92 //
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
93 // // renders text in color + a border with alpha in a RGBA32 image
1471
28c64c246312 working on a shared library
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1463
diff changeset
94 // Orthanc::ImageAccessor* TextRenderer::RenderWithAlpha(const std::string& ttf,
1463
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
95 // unsigned int fontSize,
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
96 // const std::string& utf8String,
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
97 // uint8_t foreground,
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
98 // uint8_t borderColor)
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
99 // {
1471
28c64c246312 working on a shared library
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1463
diff changeset
100 // std::unique_ptr<Orthanc::ImageAccessor> renderedBorderAlpha(RenderWithAlpha(ttf, fontSize, utf8String, borderColor));
28c64c246312 working on a shared library
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1463
diff changeset
101 // std::unique_ptr<Orthanc::ImageAccessor> renderedTextAlpha(RenderWithAlpha(ttf, fontSize, utf8String, foreground));
1463
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
102
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
103 // unsigned int textWidth = renderedBorderAlpha->GetWidth();
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
104 // unsigned int textHeight = renderedBorderAlpha->GetHeight();
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
105
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
106 // Scene2D targetScene;
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
107 // std::unique_ptr<ColorTextureSceneLayer> borderLayerLeft(new ColorTextureSceneLayer(*renderedBorderAlpha));
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
108 // std::unique_ptr<ColorTextureSceneLayer> borderLayerRight(new ColorTextureSceneLayer(*renderedBorderAlpha));
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
109 // std::unique_ptr<ColorTextureSceneLayer> borderLayerTop(new ColorTextureSceneLayer(*renderedBorderAlpha));
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
110 // std::unique_ptr<ColorTextureSceneLayer> borderLayerBottom(new ColorTextureSceneLayer(*renderedBorderAlpha));
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
111 // std::unique_ptr<ColorTextureSceneLayer> textLayerCenter(new ColorTextureSceneLayer(*renderedTextAlpha));
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
112
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
113 // borderLayerLeft->SetOrigin(0, 1);
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
114 // borderLayerRight->SetOrigin(2, 1);
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
115 // borderLayerTop->SetOrigin(1, 0);
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
116 // borderLayerBottom->SetOrigin(1, 2);
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
117 // textLayerCenter->SetOrigin(1, 1);
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
118 // targetScene.SetLayer(1, borderLayerLeft.release());
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
119 // targetScene.SetLayer(2, borderLayerRight.release());
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
120 // targetScene.SetLayer(3, borderLayerTop.release());
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
121 // targetScene.SetLayer(4, borderLayerBottom.release());
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
122 // targetScene.SetLayer(5, textLayerCenter.release());
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
123
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
124 // targetScene.FitContent(textWidth + 2, textHeight + 2);
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
125 // CairoCompositor compositor(targetScene, textWidth + 2, textHeight + 2);
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
126 // compositor.Refresh();
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
127
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
128 // Orthanc::ImageAccessor canvas;
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
129 // compositor.GetCanvas().GetReadOnlyAccessor(canvas);
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
130
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
131 // std::unique_ptr<Orthanc::Image> output(new Orthanc::Image(Orthanc::PixelFormat_RGBA32, canvas.GetWidth(), canvas.GetHeight(), false));
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
132 // Orthanc::ImageProcessing::Convert(*output, canvas);
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
133 // return output.release();
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
134 // }
182bf3106ee2 dos2unix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1455
diff changeset
135 }