OpenTTD Source 20250810-master-g3587db6a23
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
12#include "../debug.h"
13#include "../fontcache.h"
14#include "../blitter/factory.hpp"
15#include "../zoom_func.h"
16#include "../fileio_func.h"
17#include "../error_func.h"
18#include "../../os/unix/font_unix.h"
19#include "truetypefontcache.h"
20
21#include "../table/control_codes.h"
22
23#include "../safeguards.h"
24
25#ifdef WITH_FREETYPE
26#include <ft2build.h>
27#include FT_FREETYPE_H
28#include FT_GLYPH_H
29#include FT_TRUETYPE_TABLES_H
30
33private:
34 FT_Face face;
35
36 void SetFontSize(int pixels);
37 const Sprite *InternalGetGlyph(GlyphID key, bool aa) override;
38
39public:
40 FreeTypeFontCache(FontSize fs, FT_Face face, int pixels);
42 void ClearFontCache() override;
43 GlyphID MapCharToGlyph(char32_t key, bool allow_fallback = true) override;
44 std::string GetFontName() override { return fmt::format("{}, {}", face->family_name, face->style_name); }
45 bool IsBuiltInFont() override { return false; }
46 const void *GetOSHandle() override { return &face; }
47};
48
55FreeTypeFontCache::FreeTypeFontCache(FontSize fs, FT_Face face, int pixels) : TrueTypeFontCache(fs, pixels), face(face)
56{
57 assert(face != nullptr);
58
59 this->SetFontSize(pixels);
60}
61
62void FreeTypeFontCache::SetFontSize(int pixels)
63{
64 if (pixels == 0) {
65 /* Try to determine a good height based on the minimal height recommended by the font. */
66 int scaled_height = ScaleGUITrad(FontCache::GetDefaultFontHeight(this->fs));
67 pixels = scaled_height;
68
69 TT_Header *head = (TT_Header *)FT_Get_Sfnt_Table(this->face, ft_sfnt_head);
70 if (head != nullptr) {
71 /* Font height is minimum height plus the difference between the default
72 * height for this font size and the small size. */
73 int diff = scaled_height - ScaleGUITrad(FontCache::GetDefaultFontHeight(FS_SMALL));
74 /* Clamp() is not used as scaled_height could be greater than MAX_FONT_SIZE, which is not permitted in Clamp(). */
75 pixels = std::min(std::max(std::min<int>(head->Lowest_Rec_PPEM, MAX_FONT_MIN_REC_SIZE) + diff, scaled_height), MAX_FONT_SIZE);
76 }
77 } else {
78 pixels = ScaleGUITrad(pixels);
79 }
80 this->used_size = pixels;
81
82 FT_Error err = FT_Set_Pixel_Sizes(this->face, 0, pixels);
83 if (err != FT_Err_Ok) {
84
85 /* Find nearest size to that requested */
86 FT_Bitmap_Size *bs = this->face->available_sizes;
87 int i = this->face->num_fixed_sizes;
88 if (i > 0) { // In pathetic cases one might get no fixed sizes at all.
89 int n = bs->height;
90 FT_Int chosen = 0;
91 for (; --i; bs++) {
92 if (abs(pixels - bs->height) >= abs(pixels - n)) continue;
93 n = bs->height;
94 chosen = this->face->num_fixed_sizes - i;
95 }
96
97 /* Don't use FT_Set_Pixel_Sizes here - it might give us another
98 * error, even though the size is available (FS#5885). */
99 err = FT_Select_Size(this->face, chosen);
100 }
101 }
102
103 if (err == FT_Err_Ok) {
104 this->ascender = this->face->size->metrics.ascender >> 6;
105 this->descender = this->face->size->metrics.descender >> 6;
106 this->height = this->ascender - this->descender;
107 } else {
108 /* Both FT_Set_Pixel_Sizes and FT_Select_Size failed. */
109 Debug(fontcache, 0, "Font size selection failed. Using FontCache defaults.");
110 }
111}
112
117{
118 FT_Done_Face(this->face);
119 this->face = nullptr;
120 this->ClearFontCache();
121}
122
127{
128 /* Font scaling might have changed, determine font size anew if it was automatically selected. */
129 if (this->face != nullptr) this->SetFontSize(this->req_size);
130
132}
133
134
135const Sprite *FreeTypeFontCache::InternalGetGlyph(GlyphID key, bool aa)
136{
137 FT_GlyphSlot slot = this->face->glyph;
138
139 FT_Load_Glyph(this->face, key, aa ? FT_LOAD_TARGET_NORMAL : FT_LOAD_TARGET_MONO);
140 FT_Render_Glyph(this->face->glyph, aa ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO);
141
142 /* Despite requesting a normal glyph, FreeType may have returned a bitmap */
143 aa = (slot->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY);
144
145 /* Add 1 scaled pixel for the shadow on the medium font. Our sprite must be at least 1x1 pixel */
146 uint shadow = (this->fs == FS_NORMAL) ? ScaleGUITrad(1) : 0;
147 uint width = std::max(1U, (uint)slot->bitmap.width + shadow);
148 uint height = std::max(1U, (uint)slot->bitmap.rows + shadow);
149
150 /* Limit glyph size to prevent overflows later on. */
151 if (width > MAX_GLYPH_DIM || height > MAX_GLYPH_DIM) UserError("Font glyph is too large");
152
153 /* FreeType has rendered the glyph, now we allocate a sprite and copy the image into it */
154 SpriteLoader::SpriteCollection spritecollection;
155 SpriteLoader::Sprite &sprite = spritecollection[ZoomLevel::Min];
156 sprite.AllocateData(ZoomLevel::Min, static_cast<size_t>(width) * height);
158 if (aa) sprite.colours.Set(SpriteComponent::Alpha);
159 sprite.width = width;
160 sprite.height = height;
161 sprite.x_offs = slot->bitmap_left;
162 sprite.y_offs = this->ascender - slot->bitmap_top;
163
164 /* Draw shadow for medium size */
165 if (this->fs == FS_NORMAL && !aa) {
166 for (uint y = 0; y < (uint)slot->bitmap.rows; y++) {
167 for (uint x = 0; x < (uint)slot->bitmap.width; x++) {
168 if (HasBit(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
169 sprite.data[shadow + x + (shadow + y) * sprite.width].m = SHADOW_COLOUR;
170 sprite.data[shadow + x + (shadow + y) * sprite.width].a = 0xFF;
171 }
172 }
173 }
174 }
175
176 for (uint y = 0; y < (uint)slot->bitmap.rows; y++) {
177 for (uint x = 0; x < (uint)slot->bitmap.width; x++) {
178 if (aa ? (slot->bitmap.buffer[x + y * slot->bitmap.pitch] > 0) : HasBit(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
179 sprite.data[x + y * sprite.width].m = FACE_COLOUR;
180 sprite.data[x + y * sprite.width].a = aa ? slot->bitmap.buffer[x + y * slot->bitmap.pitch] : 0xFF;
181 }
182 }
183 }
184
185 UniquePtrSpriteAllocator allocator;
186 BlitterFactory::GetCurrentBlitter()->Encode(SpriteType::Font, spritecollection, allocator);
187
188 GlyphEntry new_glyph;
189 new_glyph.data = std::move(allocator.data);
190 new_glyph.width = slot->advance.x >> 6;
191
192 return this->SetGlyphPtr(key, std::move(new_glyph)).GetSprite();
193}
194
195
196GlyphID FreeTypeFontCache::MapCharToGlyph(char32_t key, bool allow_fallback)
197{
198 assert(IsPrintable(key));
199
200 FT_UInt glyph = FT_Get_Char_Index(this->face, key);
201
202 if (glyph == 0 && allow_fallback && key >= SCC_SPRITE_START && key <= SCC_SPRITE_END) {
203 return this->parent->MapCharToGlyph(key);
204 }
205
206 return glyph;
207}
208
209FT_Library _ft_library = nullptr;
210
212public:
213 FreeTypeFontCacheFactory() : FontCacheFactory("freetype", "FreeType font provider") {}
214
216 {
217 FT_Done_FreeType(_ft_library);
218 _ft_library = nullptr;
219 }
220
228 std::unique_ptr<FontCache> LoadFont(FontSize fs, FontType fonttype) override
229 {
230 if (fonttype != FontType::TrueType) return nullptr;
231
233
234 std::string font = GetFontCacheFontName(fs);
235 if (font.empty()) return nullptr;
236
237 if (_ft_library == nullptr) {
238 if (FT_Init_FreeType(&_ft_library) != FT_Err_Ok) {
239 ShowInfo("Unable to initialize FreeType, using sprite fonts instead");
240 return nullptr;
241 }
242
243 Debug(fontcache, 2, "Initialized");
244 }
245
246 FT_Face face = nullptr;
247
248 /* If font is an absolute path to a ttf, try loading that first. */
249 int32_t index = 0;
250 if (settings->os_handle != nullptr) index = *static_cast<const int32_t *>(settings->os_handle);
251 FT_Error error = FT_New_Face(_ft_library, font.c_str(), index, &face);
252
253 if (error != FT_Err_Ok) {
254 /* Check if font is a relative filename in one of our search-paths. */
255 std::string full_font = FioFindFullPath(BASE_DIR, font);
256 if (!full_font.empty()) {
257 error = FT_New_Face(_ft_library, full_font.c_str(), 0, &face);
258 }
259 }
260
261#ifdef WITH_FONTCONFIG
262 /* Try loading based on font face name (OS-wide fonts). */
263 if (error != FT_Err_Ok) error = GetFontByFaceName(font, &face);
264#endif /* WITH_FONTCONFIG */
265
266 if (error != FT_Err_Ok) {
267 FT_Done_Face(face);
268 return nullptr;
269 }
270
271 return LoadFont(fs, face, font, GetFontCacheFontSize(fs));
272 }
273
274 bool FindFallbackFont(struct FontCacheSettings *settings, const std::string &language_isocode, class MissingGlyphSearcher *callback) override
275 {
276#ifdef WITH_FONTCONFIG
277 if (FontConfigFindFallbackFont(settings, language_isocode, callback)) return true;
278#endif /* WITH_FONTCONFIG */
279
280 return false;
281 }
282
283private:
284 static std::unique_ptr<FontCache> LoadFont(FontSize fs, FT_Face face, std::string_view font_name, uint size)
285 {
286 Debug(fontcache, 2, "Requested '{}', using '{} {}'", font_name, face->family_name, face->style_name);
287
288 /* Attempt to select the unicode character map */
289 FT_Error error = FT_Select_Charmap(face, ft_encoding_unicode);
290 if (error == FT_Err_Invalid_CharMap_Handle) {
291 /* Try to pick a different character map instead. We default to
292 * the first map, but platform_id 0 encoding_id 0 should also
293 * be unicode (strange system...) */
294 FT_CharMap found = face->charmaps[0];
295
296 for (int i = 0; i < face->num_charmaps; ++i) {
297 FT_CharMap charmap = face->charmaps[i];
298 if (charmap->platform_id == 0 && charmap->encoding_id == 0) {
299 found = charmap;
300 }
301 }
302
303 if (found != nullptr) {
304 error = FT_Set_Charmap(face, found);
305 }
306 }
307
308 if (error != FT_Err_Ok) {
309 FT_Done_Face(face);
310
311 ShowInfo("Unable to use '{}' for {} font, FreeType reported error 0x{:X}, using sprite font instead", font_name, FontSizeToName(fs), error);
312 return nullptr;
313 }
314
315 return std::make_unique<FreeTypeFontCache>(fs, face, size);
316 }
317
318private:
319 static FreeTypeFontCacheFactory instance;
320};
321
322/* static */ FreeTypeFontCacheFactory FreeTypeFontCacheFactory::instance;
323
324#endif /* WITH_FREETYPE */
debug_inline constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
constexpr Timpl & Set()
Set all bits.
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition factory.hpp:136
Factory for FontCaches.
Definition fontcache.h:220
int height
The height of the font.
Definition fontcache.h:27
std::unique_ptr< FontCache > parent
The parent of this font cache.
Definition fontcache.h:25
const FontSize fs
The size of the font.
Definition fontcache.h:26
int descender
The descender value of the font.
Definition fontcache.h:29
int ascender
The ascender value of the font.
Definition fontcache.h:28
std::unique_ptr< FontCache > LoadFont(FontSize fs, FontType fonttype) override
Loads the freetype font.
Font cache for fonts that are based on a freetype font.
const void * GetOSHandle() override
Get the native OS font handle, if there is one.
FreeTypeFontCache(FontSize fs, FT_Face face, int pixels)
Create a new FreeTypeFontCache.
~FreeTypeFontCache()
Free everything that was allocated for this font cache.
void ClearFontCache() override
Reset cached glyphs.
GlyphID MapCharToGlyph(char32_t key, bool allow_fallback=true) override
Map a character into a glyph.
FT_Face face
The font face associated with this font.
std::string GetFontName() override
Get the name of this font.
bool IsBuiltInFont() override
Is this a built-in sprite font?
A searcher for missing glyphs.
Map zoom level to data.
virtual Sprite * Encode(SpriteType sprite_type, const SpriteLoader::SpriteCollection &sprite, SpriteAllocator &allocator)=0
Convert a sprite from the loader to our own format.
Font cache for fonts that are based on a TrueType font.
static constexpr int MAX_GLYPH_DIM
Maximum glyph dimensions.
int used_size
Used font size.
int req_size
Requested font size.
void ClearFontCache() override
Reset cached glyphs.
static constexpr uint MAX_FONT_MIN_REC_SIZE
Upper limit for the recommended font size in case a font file contains nonsensical values.
SpriteAllocator that allocates memory via a unique_ptr array.
Definition spritecache.h:20
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
std::string FioFindFullPath(Subdirectory subdir, std::string_view filename)
Find a path to the filename in one of the search directories.
Definition fileio.cpp:144
@ BASE_DIR
Base directory for all subdirectories.
Definition fileio_type.h:89
fluid_settings_t * settings
FluidSynth settings handle.
FT_Error GetFontByFaceName(std::string_view font_name, FT_Face *face)
Load a freetype font face with the given font name.
Definition font_unix.cpp:70
uint GetFontCacheFontSize(FontSize fs)
Get the scalable font size to use for a FontSize.
std::string GetFontCacheFontName(FontSize fs)
Get font to use for a given font size.
FontType
Different types of font that can be loaded.
Definition fontcache.h:214
@ TrueType
Scalable TrueType fonts.
FontCacheSubSetting * GetFontCacheSubSetting(FontSize fs)
Get the settings of a given font size.
Definition fontcache.h:196
uint32_t GlyphID
Glyphs are characters from a font.
Definition fontcache.h:18
@ Font
A sprite used for fonts.
FontSize
Available font sizes.
Definition gfx_type.h:249
@ FS_SMALL
Index of the small font in the font tables.
Definition gfx_type.h:251
@ FS_NORMAL
Index of the normal font in the font tables.
Definition gfx_type.h:250
constexpr T abs(const T a)
Returns the absolute value of (scalar) variable.
Definition math_func.hpp:23
@ Palette
Sprite has palette data.
@ Alpha
Sprite has alpha.
Settings for the four different fonts.
Definition fontcache.h:180
Settings for a single font.
Definition fontcache.h:172
uint8_t m
Remap-channel.
uint8_t a
Alpha-channel.
Structure for passing information from the sprite loader to the blitter.
SpriteComponents colours
The colour components of the sprite with useful information.
void AllocateData(ZoomLevel zoom, size_t size)
Allocate the sprite data of this sprite.
uint16_t width
Width of the sprite.
int16_t x_offs
The x-offset of where the sprite will be drawn.
SpriteLoader::CommonPixel * data
The sprite itself.
uint16_t height
Height of the sprite.
int16_t y_offs
The y-offset of where the sprite will be drawn.
Data structure describing a sprite.
std::byte data[]
Sprite data.
Common base definition for font file based font caches.
static const int MAX_FONT_SIZE
Maximum font size.
static RectPadding ScaleGUITrad(const RectPadding &r)
Scale a RectPadding to GUI zoom level.
Definition widget.cpp:49
@ Min
Minimum zoom level.