annotate Framework/Toolbox/TextRenderer.cpp @ 1324:4d8d642f7036 broker

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