Mercurial > hg > orthanc-stone
annotate OrthancStone/Sources/Fonts/FontRenderer.cpp @ 1985:bb307007f8e2
improved interaction with rectangle probes
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 31 Oct 2022 15:01:09 +0100 |
parents | 7053b8a0aaec |
children | 07964689cb0b |
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 | |
1871
7053b8a0aaec
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1870
diff
changeset
|
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium |
7053b8a0aaec
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1870
diff
changeset
|
6 * Copyright (C) 2021-2022 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 "FontRenderer.h" | |
25 | |
26 #include "../Toolbox/DynamicBitmap.h" | |
27 | |
1455
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1298
diff
changeset
|
28 #include <OrthancException.h> |
576 | 29 |
30 | |
31 #include <ft2build.h> | |
32 #include FT_FREETYPE_H | |
33 #include FT_GLYPH_H | |
34 | |
35 | |
36 // https://stackoverflow.com/questions/31161284/how-can-i-get-the-corresponding-error-string-from-an-ft-error-code | |
37 static std::string GetErrorMessage(FT_Error err) | |
38 { | |
39 #undef __FTERRORS_H__ | |
40 #define FT_ERRORDEF( e, v, s ) case e: return s; | |
41 #define FT_ERROR_START_LIST switch (err) { | |
42 #define FT_ERROR_END_LIST } | |
43 #include FT_ERRORS_H | |
44 return "(Unknown error)"; | |
45 } | |
46 | |
47 | |
48 static void CheckError(FT_Error err) | |
49 { | |
50 if (err != 0) | |
51 { | |
52 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, | |
53 "Error in FreeType: " + GetErrorMessage(err)); | |
54 } | |
55 } | |
56 | |
57 | |
58 namespace OrthancStone | |
59 { | |
60 class FontRenderer::PImpl : public boost::noncopyable | |
61 { | |
62 private: | |
63 std::string fontContent_; | |
64 FT_Library library_; | |
65 FT_Face face_; | |
66 | |
67 void Clear() | |
68 { | |
69 if (face_ != NULL) | |
70 { | |
71 FT_Done_Face(face_); | |
72 face_ = NULL; | |
73 } | |
74 | |
75 fontContent_.clear(); | |
76 } | |
77 | |
78 public: | |
79 PImpl() : | |
80 library_(NULL), | |
81 face_(NULL) | |
82 { | |
83 CheckError(FT_Init_FreeType(&library_)); | |
84 } | |
85 | |
86 | |
87 ~PImpl() | |
88 { | |
89 Clear(); | |
90 FT_Done_FreeType(library_); | |
91 } | |
92 | |
93 | |
94 void LoadFont(const std::string& fontContent, | |
95 unsigned int fontSize) | |
96 { | |
97 Clear(); | |
98 | |
99 // It is necessary to make a private copy of the font, as | |
100 // Freetype makes the assumption that the buffer containing the | |
101 // font is never deleted | |
102 fontContent_.assign(fontContent); | |
103 | |
104 const FT_Byte* data = reinterpret_cast<const FT_Byte*>(fontContent_.c_str()); | |
105 | |
693
9a474e90e832
Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents:
576
diff
changeset
|
106 CheckError(FT_New_Memory_Face( |
9a474e90e832
Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents:
576
diff
changeset
|
107 library_, data, static_cast<FT_Long>(fontContent_.size()), 0, &face_)); |
9a474e90e832
Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents:
576
diff
changeset
|
108 |
576 | 109 CheckError(FT_Set_Char_Size(face_, // handle to face object |
110 0, // char_width in 1/64th of points | |
111 fontSize * 64, // char_height in 1/64th of points | |
112 72, // horizontal device resolution | |
113 72)); // vertical device resolution | |
114 | |
115 CheckError(FT_Select_Charmap(face_, FT_ENCODING_UNICODE)); | |
116 } | |
117 | |
118 | |
119 Glyph* Render(uint32_t unicode) | |
120 { | |
121 if (face_ == NULL) | |
122 { | |
123 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls, | |
124 "First call LoadFont()"); | |
125 } | |
126 else if (FT_Load_Char(face_, unicode, FT_LOAD_RENDER) != 0) | |
127 { | |
128 // This character is not available | |
129 return NULL; | |
130 } | |
131 else | |
132 { | |
133 if (face_->glyph->format != FT_GLYPH_FORMAT_BITMAP) | |
134 { | |
135 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
136 //CheckError(FT_Glyph_To_Bitmap(&glyph, FT_RENDER_MODE_NORMAL, 0, 1)); | |
137 } | |
138 | |
139 Orthanc::ImageAccessor bitmap; | |
140 bitmap.AssignReadOnly(Orthanc::PixelFormat_Grayscale8, | |
141 face_->glyph->bitmap.width, | |
142 face_->glyph->bitmap.rows, | |
143 face_->glyph->bitmap.pitch, | |
144 face_->glyph->bitmap.buffer); | |
145 | |
1298
8a0a62189f46
replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1270
diff
changeset
|
146 std::unique_ptr<Glyph> glyph( |
576 | 147 new Glyph(bitmap.GetWidth(), |
148 bitmap.GetHeight(), | |
149 face_->glyph->bitmap_left, | |
150 -face_->glyph->bitmap_top, // Positive for an upwards vertical distance | |
151 face_->glyph->advance.x >> 6, | |
152 face_->glyph->metrics.vertAdvance >> 6)); | |
153 | |
154 glyph->SetPayload(new DynamicBitmap(bitmap)); | |
155 | |
156 return glyph.release(); | |
157 } | |
158 } | |
159 }; | |
160 | |
161 | |
162 | |
163 FontRenderer::FontRenderer() : | |
164 pimpl_(new PImpl) | |
165 { | |
166 } | |
167 | |
168 | |
169 void FontRenderer::LoadFont(const std::string& fontContent, | |
170 unsigned int fontSize) | |
171 { | |
172 pimpl_->LoadFont(fontContent, fontSize); | |
173 } | |
174 | |
175 | |
176 Glyph* FontRenderer::Render(uint32_t unicode) | |
177 { | |
178 return pimpl_->Render(unicode); | |
179 } | |
180 } |