comparison OrthancStone/Sources/Fonts/GlyphAlphabet.cpp @ 1512:244ad1e4e76a

reorganization of folders
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 07 Jul 2020 16:21:02 +0200
parents Framework/Fonts/GlyphAlphabet.cpp@30deba7bc8e2
children 4fb8fdf03314
comparison
equal deleted inserted replaced
1511:9dfeee74c1e6 1512:244ad1e4e76a
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
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 "GlyphAlphabet.h"
23
24 #include <OrthancException.h>
25 #include <Toolbox.h>
26
27
28 namespace OrthancStone
29 {
30 void GlyphAlphabet::Clear()
31 {
32 for (Content::const_iterator it = content_.begin(); it != content_.end(); ++it)
33 {
34 assert(it->second != NULL);
35 delete it->second;
36 }
37 content_.clear();
38 lineHeight_ = 0;
39 }
40
41
42 void GlyphAlphabet::Register(uint32_t unicode,
43 const Glyph& glyph,
44 Orthanc::IDynamicObject* payload)
45 {
46 std::unique_ptr<Orthanc::IDynamicObject> protection(payload);
47
48 // Don't add twice the same character
49 if (content_.find(unicode) == content_.end())
50 {
51 std::unique_ptr<Glyph> raii(new Glyph(glyph));
52
53 if (payload != NULL)
54 {
55 raii->SetPayload(protection.release());
56 }
57
58 content_[unicode] = raii.release();
59
60 lineHeight_ = std::max(lineHeight_, glyph.GetLineHeight());
61 }
62 }
63
64
65 void GlyphAlphabet::Register(FontRenderer& renderer,
66 uint32_t unicode)
67 {
68 std::unique_ptr<Glyph> glyph(renderer.Render(unicode));
69
70 if (glyph.get() != NULL)
71 {
72 Register(unicode, *glyph, glyph->ReleasePayload());
73 }
74 }
75
76
77 #if ORTHANC_ENABLE_LOCALE == 1
78 bool GlyphAlphabet::GetUnicodeFromCodepage(uint32_t& unicode,
79 unsigned int index,
80 Orthanc::Encoding encoding)
81 {
82 if (index > 255)
83 {
84 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
85 }
86
87 std::string character;
88 character.resize(1);
89 character[0] = static_cast<unsigned char>(index);
90
91 std::string utf8 = Orthanc::Toolbox::ConvertToUtf8(character, encoding, false /* no code extensions */);
92
93 if (utf8.empty())
94 {
95 // This character is not available in this codepage
96 return false;
97 }
98 else
99 {
100 size_t length;
101 Orthanc::Toolbox::Utf8ToUnicodeCharacter(unicode, length, utf8, 0);
102 assert(length != 0);
103 return true;
104 }
105 }
106 #endif
107
108
109 void GlyphAlphabet::Apply(IGlyphVisitor& visitor) const
110 {
111 for (Content::const_iterator it = content_.begin(); it != content_.end(); ++it)
112 {
113 assert(it->second != NULL);
114 visitor.Visit(it->first, *it->second);
115 }
116 }
117
118
119 void GlyphAlphabet::Apply(ITextVisitor& visitor,
120 const std::string& utf8) const
121 {
122 size_t pos = 0;
123 int x = 0;
124 int y = 0;
125
126 while (pos < utf8.size())
127 {
128 if (utf8[pos] == '\r')
129 {
130 // Ignore carriage return
131 pos++;
132 }
133 else if (utf8[pos] == '\n')
134 {
135 // This is a newline character
136 x = 0;
137 y += static_cast<int>(lineHeight_);
138
139 pos++;
140 }
141 else
142 {
143 uint32_t unicode;
144 size_t length;
145 Orthanc::Toolbox::Utf8ToUnicodeCharacter(unicode, length, utf8, pos);
146
147 Content::const_iterator glyph = content_.find(unicode);
148
149 if (glyph != content_.end())
150 {
151 assert(glyph->second != NULL);
152 const Orthanc::IDynamicObject* payload =
153 (glyph->second->HasPayload() ? &glyph->second->GetPayload() : NULL);
154
155 visitor.Visit(unicode,
156 x + glyph->second->GetOffsetLeft(),
157 y + glyph->second->GetOffsetTop(),
158 glyph->second->GetWidth(),
159 glyph->second->GetHeight(),
160 payload);
161 x += glyph->second->GetAdvanceX();
162 }
163
164 assert(length != 0);
165 pos += length;
166 }
167 }
168 }
169 }