annotate Framework/Toolbox/TextRenderer.cpp @ 1223:04fd875b91f4

fix
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 07 Dec 2019 18:41:54 +0100
parents df76d3083031
children 355dba5352d7
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
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium
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
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
47 std::auto_ptr<GlyphBitmapAlphabet> alphabet(new GlyphBitmapAlphabet);
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
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
60 return alphabet->RenderText(utf8String);
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
61 }
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
62
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
63
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
64 Orthanc::ImageAccessor* TextRenderer::RenderWithAlpha(Orthanc::EmbeddedResources::FileResourceId resource,
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
65 unsigned int fontSize,
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
66 const std::string& utf8String,
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
67 uint8_t foreground)
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
68 {
1086
Alain Mazy <alain@mazy.be>
parents: 1085
diff changeset
69 std::auto_ptr<Orthanc::ImageAccessor> renderedText8(Render(resource, fontSize, utf8String));
1085
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
70 std::auto_ptr<Orthanc::Image> target(new Orthanc::Image(Orthanc::PixelFormat_RGBA32, renderedText8->GetWidth(), renderedText8->GetHeight(), true));
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
71
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
72 Orthanc::ImageProcessing::Set(*target, foreground, foreground, foreground, *renderedText8);
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
73 return target.release();
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
74 }
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
75
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
76
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
77 // 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
78 //
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
79 // // renders text in color + a border with alpha in a RGBA32 image
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
80 // Orthanc::ImageAccessor* TextRenderer::RenderWithAlpha(Orthanc::EmbeddedResources::FileResourceId resource,
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
81 // unsigned int fontSize,
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
82 // const std::string& utf8String,
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
83 // uint8_t foreground,
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
84 // uint8_t borderColor)
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
85 // {
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
86 // std::auto_ptr<Orthanc::ImageAccessor> renderedBorderAlpha(RenderWithAlpha(resource, fontSize, utf8String, borderColor));
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
87 // std::auto_ptr<Orthanc::ImageAccessor> renderedTextAlpha(RenderWithAlpha(resource, fontSize, utf8String, foreground));
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
88
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
89 // unsigned int textWidth = renderedBorderAlpha->GetWidth();
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
90 // unsigned int textHeight = renderedBorderAlpha->GetHeight();
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
91
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
92 // Scene2D targetScene;
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
93 // std::auto_ptr<ColorTextureSceneLayer> borderLayerLeft(new ColorTextureSceneLayer(*renderedBorderAlpha));
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
94 // std::auto_ptr<ColorTextureSceneLayer> borderLayerRight(new ColorTextureSceneLayer(*renderedBorderAlpha));
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
95 // std::auto_ptr<ColorTextureSceneLayer> borderLayerTop(new ColorTextureSceneLayer(*renderedBorderAlpha));
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
96 // std::auto_ptr<ColorTextureSceneLayer> borderLayerBottom(new ColorTextureSceneLayer(*renderedBorderAlpha));
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
97 // std::auto_ptr<ColorTextureSceneLayer> textLayerCenter(new ColorTextureSceneLayer(*renderedTextAlpha));
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
98
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
99 // borderLayerLeft->SetOrigin(0, 1);
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
100 // borderLayerRight->SetOrigin(2, 1);
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
101 // borderLayerTop->SetOrigin(1, 0);
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
102 // borderLayerBottom->SetOrigin(1, 2);
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
103 // textLayerCenter->SetOrigin(1, 1);
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
104 // targetScene.SetLayer(1, borderLayerLeft.release());
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
105 // targetScene.SetLayer(2, borderLayerRight.release());
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
106 // targetScene.SetLayer(3, borderLayerTop.release());
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
107 // targetScene.SetLayer(4, borderLayerBottom.release());
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
108 // targetScene.SetLayer(5, textLayerCenter.release());
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
109
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
110 // targetScene.FitContent(textWidth + 2, textHeight + 2);
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
111 // CairoCompositor compositor(targetScene, textWidth + 2, textHeight + 2);
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
112 // compositor.Refresh();
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
113
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
114 // Orthanc::ImageAccessor canvas;
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
115 // compositor.GetCanvas().GetReadOnlyAccessor(canvas);
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
116
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
117 // std::auto_ptr<Orthanc::Image> output(new Orthanc::Image(Orthanc::PixelFormat_RGBA32, canvas.GetWidth(), canvas.GetHeight(), false));
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
118 // Orthanc::ImageProcessing::Convert(*output, canvas);
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
119 // return output.release();
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
120 // }
63539e826355 Added TextRenderer
Alain Mazy <alain@mazy.be>
parents:
diff changeset
121 }