Mercurial > hg > orthanc-stone
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 |
rev | line source |
---|---|
1085 | 1 /** |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
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 | 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 | |
1093 | 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" | |
1085 | 30 #include <Core/Images/PngWriter.h> |
1086 | 31 #include <Core/Toolbox.h> |
1085 | 32 |
33 #include "Core/Images/Image.h" | |
34 #include "Core/Images/ImageProcessing.h" | |
35 | |
36 namespace OrthancStone | |
37 { | |
38 Orthanc::ImageAccessor* TextRenderer::Render(Orthanc::EmbeddedResources::FileResourceId font, | |
39 unsigned int fontSize, | |
40 const std::string& utf8String | |
41 ) | |
42 { | |
43 FontRenderer renderer; | |
44 renderer.LoadFont(font, fontSize); | |
45 | |
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 | 48 |
49 size_t posInString = 0; | |
50 uint32_t unicode; | |
51 size_t utf8CharLength; | |
52 | |
53 while (posInString < utf8String.size()) | |
54 { | |
55 Orthanc::Toolbox::Utf8ToUnicodeCharacter(unicode, utf8CharLength, utf8String, posInString); | |
56 alphabet->AddUnicodeCharacter(renderer, unicode); | |
57 posInString += utf8CharLength; | |
58 } | |
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 | 76 } |
77 | |
78 | |
79 Orthanc::ImageAccessor* TextRenderer::RenderWithAlpha(Orthanc::EmbeddedResources::FileResourceId resource, | |
80 unsigned int fontSize, | |
81 const std::string& utf8String, | |
82 uint8_t foreground) | |
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 | 86 |
87 Orthanc::ImageProcessing::Set(*target, foreground, foreground, foreground, *renderedText8); | |
88 return target.release(); | |
89 } | |
90 | |
91 | |
92 // currently disabled because the background is actually not transparent once we use the Cairo Compositor ! | |
93 // | |
94 // // renders text in color + a border with alpha in a RGBA32 image | |
95 // Orthanc::ImageAccessor* TextRenderer::RenderWithAlpha(Orthanc::EmbeddedResources::FileResourceId resource, | |
96 // unsigned int fontSize, | |
97 // const std::string& utf8String, | |
98 // uint8_t foreground, | |
99 // uint8_t borderColor) | |
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 | 103 |
104 // unsigned int textWidth = renderedBorderAlpha->GetWidth(); | |
105 // unsigned int textHeight = renderedBorderAlpha->GetHeight(); | |
106 | |
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 | 113 |
114 // borderLayerLeft->SetOrigin(0, 1); | |
115 // borderLayerRight->SetOrigin(2, 1); | |
116 // borderLayerTop->SetOrigin(1, 0); | |
117 // borderLayerBottom->SetOrigin(1, 2); | |
118 // textLayerCenter->SetOrigin(1, 1); | |
119 // targetScene.SetLayer(1, borderLayerLeft.release()); | |
120 // targetScene.SetLayer(2, borderLayerRight.release()); | |
121 // targetScene.SetLayer(3, borderLayerTop.release()); | |
122 // targetScene.SetLayer(4, borderLayerBottom.release()); | |
123 // targetScene.SetLayer(5, textLayerCenter.release()); | |
124 | |
125 // targetScene.FitContent(textWidth + 2, textHeight + 2); | |
126 // CairoCompositor compositor(targetScene, textWidth + 2, textHeight + 2); | |
127 // compositor.Refresh(); | |
128 | |
129 // Orthanc::ImageAccessor canvas; | |
130 // compositor.GetCanvas().GetReadOnlyAccessor(canvas); | |
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 | 133 // Orthanc::ImageProcessing::Convert(*output, canvas); |
134 // return output.release(); | |
135 // } | |
136 } |