Mercurial > hg > orthanc-stone
annotate OrthancStone/Sources/Fonts/GlyphAlphabet.cpp @ 2089:63d77859edee dicom-sr
cont
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 07 Nov 2023 19:30:44 +0100 |
parents | 07964689cb0b |
children | c23eef785569 |
rev | line source |
---|---|
576 | 1 /** |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
2077
07964689cb0b
upgrade to year 2023
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1871
diff
changeset
|
5 * Copyright (C) 2017-2023 Osimis S.A., Belgium |
07964689cb0b
upgrade to year 2023
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1871
diff
changeset
|
6 * Copyright (C) 2021-2023 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
576 | 7 * |
8 * 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:
1596
diff
changeset
|
9 * modify it under the terms of the GNU Lesser General Public License |
576 | 10 * as published by the Free Software Foundation, either version 3 of |
11 * the License, or (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, but | |
14 * 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:
1596
diff
changeset
|
15 * 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:
1596
diff
changeset
|
16 * Lesser General Public License for more details. |
1596
4fb8fdf03314
removed annoying whitespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1512
diff
changeset
|
17 * |
1598
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
18 * 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:
1596
diff
changeset
|
19 * 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:
1596
diff
changeset
|
20 * <http://www.gnu.org/licenses/>. |
576 | 21 **/ |
22 | |
23 | |
24 #include "GlyphAlphabet.h" | |
25 | |
1455
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1298
diff
changeset
|
26 #include <OrthancException.h> |
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1298
diff
changeset
|
27 #include <Toolbox.h> |
576 | 28 |
29 | |
30 namespace OrthancStone | |
31 { | |
32 void GlyphAlphabet::Clear() | |
33 { | |
34 for (Content::const_iterator it = content_.begin(); it != content_.end(); ++it) | |
35 { | |
36 assert(it->second != NULL); | |
37 delete it->second; | |
38 } | |
39 content_.clear(); | |
40 lineHeight_ = 0; | |
41 } | |
42 | |
43 | |
44 void GlyphAlphabet::Register(uint32_t unicode, | |
45 const Glyph& glyph, | |
46 Orthanc::IDynamicObject* payload) | |
47 { | |
1298
8a0a62189f46
replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1270
diff
changeset
|
48 std::unique_ptr<Orthanc::IDynamicObject> protection(payload); |
576 | 49 |
50 // Don't add twice the same character | |
51 if (content_.find(unicode) == content_.end()) | |
52 { | |
1298
8a0a62189f46
replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1270
diff
changeset
|
53 std::unique_ptr<Glyph> raii(new Glyph(glyph)); |
576 | 54 |
55 if (payload != NULL) | |
56 { | |
57 raii->SetPayload(protection.release()); | |
58 } | |
59 | |
60 content_[unicode] = raii.release(); | |
61 | |
62 lineHeight_ = std::max(lineHeight_, glyph.GetLineHeight()); | |
63 } | |
64 } | |
65 | |
66 | |
67 void GlyphAlphabet::Register(FontRenderer& renderer, | |
68 uint32_t unicode) | |
69 { | |
1298
8a0a62189f46
replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1270
diff
changeset
|
70 std::unique_ptr<Glyph> glyph(renderer.Render(unicode)); |
576 | 71 |
72 if (glyph.get() != NULL) | |
73 { | |
74 Register(unicode, *glyph, glyph->ReleasePayload()); | |
75 } | |
76 } | |
77 | |
78 | |
79 #if ORTHANC_ENABLE_LOCALE == 1 | |
80 bool GlyphAlphabet::GetUnicodeFromCodepage(uint32_t& unicode, | |
81 unsigned int index, | |
82 Orthanc::Encoding encoding) | |
83 { | |
84 if (index > 255) | |
85 { | |
86 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
87 } | |
88 | |
89 std::string character; | |
90 character.resize(1); | |
91 character[0] = static_cast<unsigned char>(index); | |
92 | |
93 std::string utf8 = Orthanc::Toolbox::ConvertToUtf8(character, encoding, false /* no code extensions */); | |
94 | |
95 if (utf8.empty()) | |
96 { | |
97 // This character is not available in this codepage | |
98 return false; | |
99 } | |
100 else | |
101 { | |
102 size_t length; | |
103 Orthanc::Toolbox::Utf8ToUnicodeCharacter(unicode, length, utf8, 0); | |
104 assert(length != 0); | |
105 return true; | |
106 } | |
107 } | |
108 #endif | |
109 | |
110 | |
111 void GlyphAlphabet::Apply(IGlyphVisitor& visitor) const | |
112 { | |
113 for (Content::const_iterator it = content_.begin(); it != content_.end(); ++it) | |
114 { | |
115 assert(it->second != NULL); | |
116 visitor.Visit(it->first, *it->second); | |
117 } | |
118 } | |
119 | |
120 | |
121 void GlyphAlphabet::Apply(ITextVisitor& visitor, | |
122 const std::string& utf8) const | |
123 { | |
124 size_t pos = 0; | |
125 int x = 0; | |
126 int y = 0; | |
127 | |
128 while (pos < utf8.size()) | |
129 { | |
130 if (utf8[pos] == '\r') | |
131 { | |
132 // Ignore carriage return | |
133 pos++; | |
134 } | |
135 else if (utf8[pos] == '\n') | |
136 { | |
137 // This is a newline character | |
138 x = 0; | |
139 y += static_cast<int>(lineHeight_); | |
140 | |
141 pos++; | |
142 } | |
143 else | |
144 { | |
145 uint32_t unicode; | |
146 size_t length; | |
147 Orthanc::Toolbox::Utf8ToUnicodeCharacter(unicode, length, utf8, pos); | |
148 | |
149 Content::const_iterator glyph = content_.find(unicode); | |
150 | |
151 if (glyph != content_.end()) | |
152 { | |
153 assert(glyph->second != NULL); | |
154 const Orthanc::IDynamicObject* payload = | |
155 (glyph->second->HasPayload() ? &glyph->second->GetPayload() : NULL); | |
156 | |
157 visitor.Visit(unicode, | |
158 x + glyph->second->GetOffsetLeft(), | |
159 y + glyph->second->GetOffsetTop(), | |
160 glyph->second->GetWidth(), | |
161 glyph->second->GetHeight(), | |
162 payload); | |
163 x += glyph->second->GetAdvanceX(); | |
164 } | |
165 | |
166 assert(length != 0); | |
167 pos += length; | |
168 } | |
169 } | |
170 } | |
171 } |