OpenTTD Source: src/fontcache/freetypefontcache.cpp Source File

OpenTTD Source 13.1

freetypefontcache.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10 #include "../stdafx.h"
11 #include "../debug.h"
12 #include "../fontcache.h"
13 #include "../fontdetection.h"
14 #include "../blitter/factory.hpp"
15 #include "../core/math_func.hpp"
16 #include "../zoom_func.h"
17 #include "../fileio_func.h"
18 #include "truetypefontcache.h"
19
20 #include "../table/control_codes.h"
21
22 #include "../safeguards.h"
23
24 #ifdef WITH_FREETYPE
25 #include <ft2build.h>
26 #include FT_FREETYPE_H
27 #include FT_GLYPH_H
28 #include FT_TRUETYPE_TABLES_H
29
31 class FreeTypeFontCache : public TrueTypeFontCache {
32 private:
33 FT_Face face;
34
35 void SetFontSize(FontSize fs, FT_Face face, int pixels);
36 virtual const void *InternalGetFontTable(uint32 tag, size_t &length);
37 virtual const Sprite *InternalGetGlyph(GlyphID key, bool aa);
38
39 public:
40 FreeTypeFontCache(FontSize fs, FT_Face face, int pixels);
41 ~FreeTypeFontCache();
42 virtual void ClearFontCache();
43 virtual GlyphID MapCharToGlyph(WChar key);
44 virtual const char *GetFontName() { return face->family_name; }
45 virtual bool IsBuiltInFont() { return false; }
46 };
47
48 FT_Library _library = nullptr;
49
50
57 FreeTypeFontCache::FreeTypeFontCache(FontSize fs, FT_Face face, int pixels) : TrueTypeFontCache(fs, pixels), face(face)
58 {
59 assert(face != nullptr);
60
61 this->SetFontSize(fs, face, pixels);
62 }
63
64 void FreeTypeFontCache::SetFontSize(FontSize fs, FT_Face face, int pixels)
65 {
66 if (pixels == 0) {
67 /* Try to determine a good height based on the minimal height recommended by the font. */
68 int scaled_height = ScaleGUITrad(FontCache::GetDefaultFontHeight(this->fs));
69 pixels = scaled_height;
70
71 TT_Header *head = (TT_Header *)FT_Get_Sfnt_Table(this->face, ft_sfnt_head);
72 if (head != nullptr) {
73 /* Font height is minimum height plus the difference between the default
74 * height for this font size and the small size. */
75 int diff = scaled_height - ScaleGUITrad(FontCache::GetDefaultFontHeight(FS_SMALL));
76 /* Clamp() is not used as scaled_height could be greater than MAX_FONT_SIZE, which is not permitted in Clamp(). */
77 pixels = std::min(std::max(std::min<int>(head->Lowest_Rec_PPEM, MAX_FONT_MIN_REC_SIZE) + diff, scaled_height), MAX_FONT_SIZE);
78 }
79 } else {
80 pixels = ScaleGUITrad(pixels);
81 }
82 this->used_size = pixels;
83
84 FT_Error err = FT_Set_Pixel_Sizes(this->face, 0, pixels);
85 if (err != FT_Err_Ok) {
86
87 /* Find nearest size to that requested */
88 FT_Bitmap_Size *bs = this->face->available_sizes;
89 int i = this->face->num_fixed_sizes;
90 if (i > 0) { // In pathetic cases one might get no fixed sizes at all.
91 int n = bs->height;
92 FT_Int chosen = 0;
93 for (; --i; bs++) {
94 if (abs(pixels - bs->height) >= abs(pixels - n)) continue;
95 n = bs->height;
96 chosen = this->face->num_fixed_sizes - i;
97 }
98
99 /* Don't use FT_Set_Pixel_Sizes here - it might give us another
100 * error, even though the size is available (FS#5885). */
101 err = FT_Select_Size(this->face, chosen);
102 }
103 }
104
105 if (err == FT_Err_Ok) {
106 this->units_per_em = this->face->units_per_EM;
107 this->ascender = this->face->size->metrics.ascender >> 6;
108 this->descender = this->face->size->metrics.descender >> 6;
109 this->height = this->ascender - this->descender;
110 } else {
111 /* Both FT_Set_Pixel_Sizes and FT_Select_Size failed. */
112 Debug(fontcache, 0, "Font size selection failed. Using FontCache defaults.");
113 }
114 }
115
123 void LoadFreeTypeFont(FontSize fs)
124 {
125 FontCacheSubSetting *settings = GetFontCacheSubSetting(fs);
126
127 if (settings->font.empty()) return;
128
129 if (_library == nullptr) {
130 if (FT_Init_FreeType(&_library) != FT_Err_Ok) {
131 ShowInfoF("Unable to initialize FreeType, using sprite fonts instead");
132 return;
133 }
134
135 Debug(fontcache, 2, "Initialized");
136 }
137
138 const char *font_name = settings->font.c_str();
139 FT_Face face = nullptr;
140
141 /* If font is an absolute path to a ttf, try loading that first. */
142 FT_Error error = FT_New_Face(_library, font_name, 0, &face);
143
144 #if defined(WITH_COCOA)
145 extern void MacOSRegisterExternalFont(const char *file_path);
146 if (error == FT_Err_Ok) MacOSRegisterExternalFont(font_name);
147 #endif
148
149 if (error != FT_Err_Ok) {
150 /* Check if font is a relative filename in one of our search-paths. */
151 std::string full_font = FioFindFullPath(BASE_DIR, font_name);
152 if (!full_font.empty()) {
153 error = FT_New_Face(_library, full_font.c_str(), 0, &face);
154 #if defined(WITH_COCOA)
155 if (error == FT_Err_Ok) MacOSRegisterExternalFont(full_font.c_str());
156 #endif
157 }
158 }
159
160 /* Try loading based on font face name (OS-wide fonts). */
161 if (error != FT_Err_Ok) error = GetFontByFaceName(font_name, &face);
162
163 if (error == FT_Err_Ok) {
164 Debug(fontcache, 2, "Requested '{}', using '{} {}'", font_name, face->family_name, face->style_name);
165
166 /* Attempt to select the unicode character map */
167 error = FT_Select_Charmap(face, ft_encoding_unicode);
168 if (error == FT_Err_Ok) goto found_face; // Success
169
170 if (error == FT_Err_Invalid_CharMap_Handle) {
171 /* Try to pick a different character map instead. We default to
172 * the first map, but platform_id 0 encoding_id 0 should also
173 * be unicode (strange system...) */
174 FT_CharMap found = face->charmaps[0];
175 int i;
176
177 for (i = 0; i < face->num_charmaps; i++) {
178 FT_CharMap charmap = face->charmaps[i];
179 if (charmap->platform_id == 0 && charmap->encoding_id == 0) {
180 found = charmap;
181 }
182 }
183
184 if (found != nullptr) {
185 error = FT_Set_Charmap(face, found);
186 if (error == FT_Err_Ok) goto found_face;
187 }
188 }
189 }
190
191 FT_Done_Face(face);
192
193 ShowInfoF("Unable to use '%s' for %s font, FreeType reported error 0x%X, using sprite font instead", font_name, FontSizeToName(fs), error);
194 return;
195
196 found_face:
197 new FreeTypeFontCache(fs, face, settings->size);
198 }
199
200
204 FreeTypeFontCache::~FreeTypeFontCache()
205 {
206 FT_Done_Face(this->face);
207 this->face = nullptr;
208 this->ClearFontCache();
209 }
210
214 void FreeTypeFontCache::ClearFontCache()
215 {
216 /* Font scaling might have changed, determine font size anew if it was automatically selected. */
217 if (this->face != nullptr) this->SetFontSize(this->fs, this->face, this->req_size);
218
219 this->TrueTypeFontCache::ClearFontCache();
220 }
221
222
223 const Sprite *FreeTypeFontCache::InternalGetGlyph(GlyphID key, bool aa)
224 {
225 FT_GlyphSlot slot = this->face->glyph;
226
227 FT_Load_Glyph(this->face, key, aa ? FT_LOAD_TARGET_NORMAL : FT_LOAD_TARGET_MONO);
228 FT_Render_Glyph(this->face->glyph, aa ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO);
229
230 /* Despite requesting a normal glyph, FreeType may have returned a bitmap */
231 aa = (slot->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY);
232
233 /* Add 1 scaled pixel for the shadow on the medium font. Our sprite must be at least 1x1 pixel */
234 uint shadow = (this->fs == FS_NORMAL) ? ScaleGUITrad(1) : 0;
235 uint width = std::max(1U, (uint)slot->bitmap.width + shadow);
236 uint height = std::max(1U, (uint)slot->bitmap.rows + shadow);
237
238 /* Limit glyph size to prevent overflows later on. */
239 if (width > MAX_GLYPH_DIM || height > MAX_GLYPH_DIM) usererror("Font glyph is too large");
240
241 /* FreeType has rendered the glyph, now we allocate a sprite and copy the image into it */
242 SpriteLoader::Sprite sprite;
243 sprite.AllocateData(ZOOM_LVL_NORMAL, width * height);
244 sprite.type = ST_FONT;
245 sprite.colours = (aa ? SCC_PAL | SCC_ALPHA : SCC_PAL);
246 sprite.width = width;
247 sprite.height = height;
248 sprite.x_offs = slot->bitmap_left;
249 sprite.y_offs = this->ascender - slot->bitmap_top;
250
251 /* Draw shadow for medium size */
252 if (this->fs == FS_NORMAL && !aa) {
253 for (uint y = 0; y < (uint)slot->bitmap.rows; y++) {
254 for (uint x = 0; x < (uint)slot->bitmap.width; x++) {
255 if (HasBit(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
256 sprite.data[shadow + x + (shadow + y) * sprite.width].m = SHADOW_COLOUR;
257 sprite.data[shadow + x + (shadow + y) * sprite.width].a = 0xFF;
258 }
259 }
260 }
261 }
262
263 for (uint y = 0; y < (uint)slot->bitmap.rows; y++) {
264 for (uint x = 0; x < (uint)slot->bitmap.width; x++) {
265 if (aa ? (slot->bitmap.buffer[x + y * slot->bitmap.pitch] > 0) : HasBit(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
266 sprite.data[x + y * sprite.width].m = FACE_COLOUR;
267 sprite.data[x + y * sprite.width].a = aa ? slot->bitmap.buffer[x + y * slot->bitmap.pitch] : 0xFF;
268 }
269 }
270 }
271
272 GlyphEntry new_glyph;
273 new_glyph.sprite = BlitterFactory::GetCurrentBlitter()->Encode(&sprite, SimpleSpriteAlloc);
274 new_glyph.width = slot->advance.x >> 6;
275
276 this->SetGlyphPtr(key, &new_glyph);
277
278 return new_glyph.sprite;
279 }
280
281
282 GlyphID FreeTypeFontCache::MapCharToGlyph(WChar key)
283 {
284 assert(IsPrintable(key));
285
286 if (key >= SCC_SPRITE_START && key <= SCC_SPRITE_END) {
287 return this->parent->MapCharToGlyph(key);
288 }
289
290 return FT_Get_Char_Index(this->face, key);
291 }
292
293 const void *FreeTypeFontCache::InternalGetFontTable(uint32 tag, size_t &length)
294 {
295 FT_ULong len = 0;
296 FT_Byte *result = nullptr;
297
298 FT_Load_Sfnt_Table(this->face, tag, 0, nullptr, &len);
299
300 if (len > 0) {
301 result = MallocT<FT_Byte>(len);
302 FT_Load_Sfnt_Table(this->face, tag, 0, result, &len);
303 }
304
305 length = len;
306 return result;
307 }
308
312 void UninitFreeType()
313 {
314 FT_Done_FreeType(_library);
315 _library = nullptr;
316 }
317
318 #if !defined(_WIN32) && !defined(__APPLE__) && !defined(WITH_FONTCONFIG) && !defined(WITH_COCOA)
319
320 FT_Error GetFontByFaceName(const char *font_name, FT_Face *face) { return FT_Err_Cannot_Open_Resource; }
321
322 #endif /* !defined(_WIN32) && !defined(__APPLE__) && !defined(WITH_FONTCONFIG) && !defined(WITH_COCOA) */
323
324 #endif /* WITH_FREETYPE */
SpriteLoader::CommonPixel::m
uint8 m
Remap-channel.
Definition: spriteloader.hpp:39
SCC_PAL
@ SCC_PAL
Sprite has palette data.
Definition: spriteloader.hpp:25
GlyphID
uint32 GlyphID
Glyphs are characters from a font.
Definition: fontcache.h:17
TrueTypeFontCache
Font cache for fonts that are based on a TrueType font.
Definition: truetypefontcache.h:23
WChar
char32_t WChar
Type for wide characters, i.e.
Definition: string_type.h:36
usererror
void CDECL usererror(const char *s,...)
Error handling for fatal user errors.
Definition: openttd.cpp:105
ST_FONT
@ ST_FONT
A sprite used for fonts.
Definition: gfx_type.h:311
FontCacheSubSetting
Settings for a single font.
Definition: fontcache.h:203
SpriteLoader::Sprite::AllocateData
void AllocateData(ZoomLevel zoom, size_t size)
Allocate the sprite data of this sprite.
Definition: spriteloader.hpp:62
FontCache::height
int height
The height of the font.
Definition: fontcache.h:27
FreeTypeFontCache
Font cache for fonts that are based on a freetype font.
Definition: freetypefontcache.cpp:31
FreeTypeFontCache::~FreeTypeFontCache
~FreeTypeFontCache()
Free everything that was allocated for this font cache.
Definition: freetypefontcache.cpp:204
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103
FreeTypeFontCache::ClearFontCache
virtual void ClearFontCache()
Reset cached glyphs.
Definition: freetypefontcache.cpp:214
SpriteEncoder::Encode
virtual Sprite * Encode(const SpriteLoader::Sprite *sprite, AllocatorProc *allocator)=0
Convert a sprite from the loader to our own format.
LoadFreeTypeFont
void LoadFreeTypeFont(FontSize fs)
Loads the freetype font.
Definition: freetypefontcache.cpp:123
SpriteLoader::Sprite::data
SpriteLoader::CommonPixel * data
The sprite itself.
Definition: spriteloader.hpp:55
UninitFreeType
void UninitFreeType()
Free everything allocated w.r.t.
Definition: freetypefontcache.cpp:312
FontCache::units_per_em
int units_per_em
The units per EM value of the font.
Definition: fontcache.h:30
BASE_DIR
@ BASE_DIR
Base directory for all subdirectories.
Definition: fileio_type.h:109
FreeTypeFontCache::face
FT_Face face
The font face associated with this font.
Definition: freetypefontcache.cpp:33
TrueTypeFontCache::MAX_GLYPH_DIM
static constexpr int MAX_GLYPH_DIM
Maximum glyph dimensions.
Definition: truetypefontcache.h:25
FS_NORMAL
@ FS_NORMAL
Index of the normal font in the font tables.
Definition: gfx_type.h:207
SpriteLoader::Sprite::type
SpriteType type
The sprite type.
Definition: spriteloader.hpp:53
FreeTypeFontCache::GetFontName
virtual const char * GetFontName()
Get the name of this font.
Definition: freetypefontcache.cpp:44
FreeTypeFontCache::IsBuiltInFont
virtual bool IsBuiltInFont()
Is this a built-in sprite font?
Definition: freetypefontcache.cpp:45
FS_SMALL
@ FS_SMALL
Index of the small font in the font tables.
Definition: gfx_type.h:208
TrueTypeFontCache::MAX_FONT_MIN_REC_SIZE
static constexpr uint MAX_FONT_MIN_REC_SIZE
Upper limit for the recommended font size in case a font file contains nonsensical values.
Definition: truetypefontcache.h:26
BlitterFactory::GetCurrentBlitter
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition: factory.hpp:141
GetFontCacheSubSetting
static FontCacheSubSetting * GetFontCacheSubSetting(FontSize fs)
Get the settings of a given font size.
Definition: fontcache.h:226
truetypefontcache.h
Sprite::width
uint16 width
Width of the sprite.
Definition: spritecache.h:19
SpriteLoader::Sprite::x_offs
int16 x_offs
The x-offset of where the sprite will be drawn.
Definition: spriteloader.hpp:51
settings
fluid_settings_t * settings
FluidSynth settings handle.
Definition: fluidsynth.cpp:21
GetFontByFaceName
FT_Error GetFontByFaceName(const char *font_name, FT_Face *face)
Load a freetype font face with the given font name.
Definition: font_osx.cpp:35
FontCache::fs
const FontSize fs
The size of the font.
Definition: fontcache.h:26
SpriteLoader::Sprite::colours
SpriteColourComponent colours
The colour components of the sprite with useful information.
Definition: spriteloader.hpp:54
MacOSRegisterExternalFont
void MacOSRegisterExternalFont(const char *file_path)
Register an external font file with the CoreText system.
Definition: string_osx.cpp:299
TrueTypeFontCache::req_size
int req_size
Requested font size.
Definition: truetypefontcache.h:28
FontCache::descender
int descender
The descender value of the font.
Definition: fontcache.h:29
SpriteLoader::Sprite::width
uint16 width
Width of the sprite.
Definition: spriteloader.hpp:50
FreeTypeFontCache::FreeTypeFontCache
FreeTypeFontCache(FontSize fs, FT_Face face, int pixels)
Create a new FreeTypeFontCache.
Definition: freetypefontcache.cpp:57
FontCache::parent
FontCache * parent
The parent of this font cache.
Definition: fontcache.h:25
SCC_ALPHA
@ SCC_ALPHA
Sprite has alpha.
Definition: spriteloader.hpp:24
ShowInfoF
void CDECL ShowInfoF(const char *str,...)
Shows some information on the console/a popup box depending on the OS.
Definition: openttd.cpp:156
FontCache::ascender
int ascender
The ascender value of the font.
Definition: fontcache.h:28
TrueTypeFontCache::ClearFontCache
void ClearFontCache() override
Reset cached glyphs.
Definition: truetypefontcache.cpp:45
SpriteLoader::Sprite
Structure for passing information from the sprite loader to the blitter.
Definition: spriteloader.hpp:48
abs
static T abs(const T a)
Returns the absolute value of (scalar) variable.
Definition: math_func.hpp:21
TrueTypeFontCache::used_size
int used_size
Used font size.
Definition: truetypefontcache.h:29
error
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:134
Debug
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
SpriteLoader::CommonPixel::a
uint8 a
Alpha-channel.
Definition: spriteloader.hpp:38
FreeTypeFontCache::MapCharToGlyph
virtual GlyphID MapCharToGlyph(WChar key)
Map a character into a glyph.
Definition: freetypefontcache.cpp:282
ZOOM_LVL_NORMAL
@ ZOOM_LVL_NORMAL
The normal zoom level.
Definition: zoom_type.h:22
FontSize
FontSize
Available font sizes.
Definition: gfx_type.h:206
SpriteLoader::Sprite::y_offs
int16 y_offs
The y-offset of where the sprite will be drawn.
Definition: spriteloader.hpp:52
SpriteLoader::Sprite::height
uint16 height
Height of the sprite.
Definition: spriteloader.hpp:49
FioFindFullPath
std::string FioFindFullPath(Subdirectory subdir, const char *filename)
Find a path to the filename in one of the search directories.
Definition: fileio.cpp:141
SimpleSpriteAlloc
void * SimpleSpriteAlloc(size_t size)
Sprite allocator simply using malloc.
Definition: spritecache.cpp:882
FontCache::MapCharToGlyph
virtual GlyphID MapCharToGlyph(WChar key)=0
Map a character into a glyph.
Sprite
Data structure describing a sprite.
Definition: spritecache.h:17
MAX_FONT_SIZE
static const int MAX_FONT_SIZE
Maximum font size.
Definition: truetypefontcache.h:17
ScaleGUITrad
static RectPadding ScaleGUITrad(const RectPadding &r)
Scale a RectPadding to GUI zoom level.
Definition: widget.cpp:168