Mercurial > hg > orthanc-stone
annotate OrthancStone/Sources/Toolbox/TextRenderer.cpp @ 1677:51bab5188a13
start multiple preset windowings
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 23 Nov 2020 19:24:18 +0100 |
parents | 8563ea5d8ae4 |
children | 9ac2a65d4172 |
rev | line source |
---|---|
1463 | 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 | |
1598
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1512
diff
changeset
|
8 * modify it under the terms of the GNU Lesser General Public License |
1463 | 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 | |
1598
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1512
diff
changeset
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1512
diff
changeset
|
15 * Lesser General Public License for more details. |
1463 | 16 * |
1598
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1512
diff
changeset
|
17 * You should have received a copy of the GNU Lesser General Public |
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1512
diff
changeset
|
18 * License along with this program. If not, see |
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1512
diff
changeset
|
19 * <http://www.gnu.org/licenses/>. |
1463 | 20 **/ |
21 | |
22 | |
23 #include "TextRenderer.h" | |
24 | |
25 #include "../Scene2D/CairoCompositor.h" | |
26 #include "../Scene2D/ColorTextureSceneLayer.h" | |
27 #include "../Scene2D/FloatTextureSceneLayer.h" | |
28 #include "../Scene2D/TextSceneLayer.h" | |
29 #include "../Fonts/GlyphBitmapAlphabet.h" | |
30 #include "../Fonts/FontRenderer.h" | |
31 | |
32 #include <Images/Image.h> | |
33 #include <Images/ImageProcessing.h> | |
34 #include <Images/PngWriter.h> | |
35 #include <Toolbox.h> | |
36 | |
37 namespace OrthancStone | |
38 { | |
1471
28c64c246312
working on a shared library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1463
diff
changeset
|
39 Orthanc::ImageAccessor* TextRenderer::Render(const std::string& ttf, |
1463 | 40 unsigned int fontSize, |
1471
28c64c246312
working on a shared library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1463
diff
changeset
|
41 const std::string& utf8String) |
1463 | 42 { |
43 FontRenderer renderer; | |
1471
28c64c246312
working on a shared library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1463
diff
changeset
|
44 renderer.LoadFont(ttf, fontSize); |
1463 | 45 |
46 // add each char to be rendered to the alphabet | |
47 std::unique_ptr<GlyphBitmapAlphabet> alphabet(new GlyphBitmapAlphabet); | |
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 | |
60 | |
61 std::unique_ptr<Orthanc::ImageAccessor> renderedText(alphabet->RenderText(utf8String)); | |
62 | |
63 // add a blank line on top of the text (to improve bilinear filtering of the topmost line) | |
64 std::unique_ptr<Orthanc::Image> renderedTextExtended(new Orthanc::Image(renderedText->GetFormat(), renderedText->GetWidth(), renderedText->GetHeight() + 1, true)); | |
65 | |
66 Orthanc::ImageAccessor textRegion; | |
67 Orthanc::ImageAccessor firstLineRegion; | |
68 | |
69 renderedTextExtended->GetRegion(firstLineRegion, 0, 0, renderedText->GetWidth(), 1); | |
70 Orthanc::ImageProcessing::Set(firstLineRegion, 0); | |
71 | |
72 renderedTextExtended->GetRegion(textRegion, 0, 1, renderedText->GetWidth(), renderedText->GetHeight()); | |
73 Orthanc::ImageProcessing::Copy(textRegion, *renderedText); | |
74 | |
75 return renderedTextExtended.release(); | |
76 } | |
77 | |
78 | |
1471
28c64c246312
working on a shared library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1463
diff
changeset
|
79 Orthanc::ImageAccessor* TextRenderer::RenderWithAlpha(const std::string& ttf, |
1463 | 80 unsigned int fontSize, |
81 const std::string& utf8String, | |
82 uint8_t foreground) | |
83 { | |
1471
28c64c246312
working on a shared library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1463
diff
changeset
|
84 std::unique_ptr<Orthanc::ImageAccessor> renderedText8(Render(ttf, fontSize, utf8String)); |
1463 | 85 std::unique_ptr<Orthanc::Image> target(new Orthanc::Image(Orthanc::PixelFormat_RGBA32, renderedText8->GetWidth(), renderedText8->GetHeight(), true)); |
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 | |
1471
28c64c246312
working on a shared library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1463
diff
changeset
|
95 // Orthanc::ImageAccessor* TextRenderer::RenderWithAlpha(const std::string& ttf, |
1463 | 96 // unsigned int fontSize, |
97 // const std::string& utf8String, | |
98 // uint8_t foreground, | |
99 // uint8_t borderColor) | |
100 // { | |
1471
28c64c246312
working on a shared library
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1463
diff
changeset
|
101 // 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
|
102 // std::unique_ptr<Orthanc::ImageAccessor> renderedTextAlpha(RenderWithAlpha(ttf, fontSize, utf8String, foreground)); |
1463 | 103 |
104 // unsigned int textWidth = renderedBorderAlpha->GetWidth(); | |
105 // unsigned int textHeight = renderedBorderAlpha->GetHeight(); | |
106 | |
107 // Scene2D targetScene; | |
108 // std::unique_ptr<ColorTextureSceneLayer> borderLayerLeft(new ColorTextureSceneLayer(*renderedBorderAlpha)); | |
109 // std::unique_ptr<ColorTextureSceneLayer> borderLayerRight(new ColorTextureSceneLayer(*renderedBorderAlpha)); | |
110 // std::unique_ptr<ColorTextureSceneLayer> borderLayerTop(new ColorTextureSceneLayer(*renderedBorderAlpha)); | |
111 // std::unique_ptr<ColorTextureSceneLayer> borderLayerBottom(new ColorTextureSceneLayer(*renderedBorderAlpha)); | |
112 // std::unique_ptr<ColorTextureSceneLayer> textLayerCenter(new ColorTextureSceneLayer(*renderedTextAlpha)); | |
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 | |
132 // std::unique_ptr<Orthanc::Image> output(new Orthanc::Image(Orthanc::PixelFormat_RGBA32, canvas.GetWidth(), canvas.GetHeight(), false)); | |
133 // Orthanc::ImageProcessing::Convert(*output, canvas); | |
134 // return output.release(); | |
135 // } | |
136 } |