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