10 #include "../stdafx.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 "../error_func.h"
21 #include "../table/control_codes.h"
23 #include "../safeguards.h"
27 #include FT_FREETYPE_H
29 #include FT_TRUETYPE_TABLES_H
37 const void *InternalGetFontTable(uint32_t tag,
size_t &length)
override;
38 const Sprite *InternalGetGlyph(
GlyphID key,
bool aa)
override;
45 std::string
GetFontName()
override {
return fmt::format(
"{}, {}",
face->family_name,
face->style_name); }
50 FT_Library _library =
nullptr;
61 assert(
face !=
nullptr);
63 this->SetFontSize(
fs,
face, pixels);
66 void FreeTypeFontCache::SetFontSize(
FontSize, FT_Face,
int pixels)
70 int scaled_height =
ScaleGUITrad(FontCache::GetDefaultFontHeight(this->
fs));
71 pixels = scaled_height;
73 TT_Header *head = (TT_Header *)FT_Get_Sfnt_Table(this->
face, ft_sfnt_head);
74 if (head !=
nullptr) {
86 FT_Error err = FT_Set_Pixel_Sizes(this->
face, 0, pixels);
87 if (err != FT_Err_Ok) {
90 FT_Bitmap_Size *bs = this->
face->available_sizes;
91 int i = this->
face->num_fixed_sizes;
96 if (
abs(pixels - bs->height) >=
abs(pixels - n))
continue;
98 chosen = this->
face->num_fixed_sizes - i;
103 err = FT_Select_Size(this->
face, chosen);
107 if (err == FT_Err_Ok) {
109 this->
ascender = this->
face->size->metrics.ascender >> 6;
114 Debug(fontcache, 0,
"Font size selection failed. Using FontCache defaults.");
118 static FT_Error LoadFont(
FontSize fs, FT_Face face,
const char *font_name, uint size)
120 Debug(fontcache, 2,
"Requested '{}', using '{} {}'", font_name, face->family_name, face->style_name);
123 FT_Error error = FT_Select_Charmap(face, ft_encoding_unicode);
124 if (error == FT_Err_Ok)
goto found_face;
126 if (error == FT_Err_Invalid_CharMap_Handle) {
130 FT_CharMap found = face->charmaps[0];
133 for (i = 0; i < face->num_charmaps; i++) {
134 FT_CharMap charmap = face->charmaps[i];
135 if (charmap->platform_id == 0 && charmap->encoding_id == 0) {
140 if (found !=
nullptr) {
141 error = FT_Set_Charmap(face, found);
142 if (error == FT_Err_Ok)
goto found_face;
167 if (_library ==
nullptr) {
168 if (FT_Init_FreeType(&_library) != FT_Err_Ok) {
169 ShowInfo(
"Unable to initialize FreeType, using sprite fonts instead");
173 Debug(fontcache, 2,
"Initialized");
176 const char *font_name =
settings->font.c_str();
177 FT_Face face =
nullptr;
181 if (
settings->os_handle !=
nullptr) index = *
static_cast<const int32_t *
>(
settings->os_handle);
182 FT_Error error = FT_New_Face(_library, font_name, index, &face);
184 if (error != FT_Err_Ok) {
187 if (!full_font.empty()) {
188 error = FT_New_Face(_library, full_font.c_str(), 0, &face);
195 if (error == FT_Err_Ok) {
196 error = LoadFont(fs, face, font_name,
settings->size);
197 if (error != FT_Err_Ok) {
198 ShowInfo(
"Unable to use '{}' for {} font, FreeType reported error 0x{:X}, using sprite font instead", font_name, FontSizeToName(fs), error);
213 if (_library ==
nullptr) {
214 if (FT_Init_FreeType(&_library) != FT_Err_Ok) {
215 ShowInfo(
"Unable to initialize FreeType, using sprite fonts instead");
219 Debug(fontcache, 2,
"Initialized");
222 FT_Face face =
nullptr;
224 FT_Error error = FT_New_Face(_library, file_name.c_str(), index, &face);
225 if (error == FT_Err_Ok) {
226 LoadFont(fs, face, file_name.c_str(), size);
238 FT_Done_Face(this->face);
239 this->face =
nullptr;
249 if (this->face !=
nullptr) this->SetFontSize(this->fs, this->face, this->
req_size);
255 const Sprite *FreeTypeFontCache::InternalGetGlyph(
GlyphID key,
bool aa)
257 FT_GlyphSlot slot = this->face->glyph;
259 FT_Load_Glyph(this->face, key, aa ? FT_LOAD_TARGET_NORMAL : FT_LOAD_TARGET_MONO);
260 FT_Render_Glyph(this->face->glyph, aa ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO);
263 aa = (slot->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY);
267 uint width = std::max(1U, (uint)slot->bitmap.width + shadow);
268 uint
height = std::max(1U, (uint)slot->bitmap.rows + shadow);
279 sprite.
width = width;
281 sprite.
x_offs = slot->bitmap_left;
286 for (uint y = 0; y < (uint)slot->bitmap.rows; y++) {
287 for (uint x = 0; x < (uint)slot->bitmap.width; x++) {
288 if (
HasBit(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
289 sprite.
data[shadow + x + (shadow + y) * sprite.
width].
m = SHADOW_COLOUR;
290 sprite.
data[shadow + x + (shadow + y) * sprite.
width].
a = 0xFF;
296 for (uint y = 0; y < (uint)slot->bitmap.rows; y++) {
297 for (uint x = 0; x < (uint)slot->bitmap.width; x++) {
298 if (aa ? (slot->bitmap.buffer[x + y * slot->bitmap.pitch] > 0) :
HasBit(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
299 sprite.
data[x + y * sprite.
width].
m = FACE_COLOUR;
300 sprite.
data[x + y * sprite.
width].
a = aa ? slot->bitmap.buffer[x + y * slot->bitmap.pitch] : 0xFF;
305 GlyphEntry new_glyph;
307 new_glyph.
width = slot->advance.x >> 6;
309 this->SetGlyphPtr(key, &new_glyph);
311 return new_glyph.sprite;
317 assert(IsPrintable(key));
319 FT_UInt glyph = FT_Get_Char_Index(this->face, key);
321 if (glyph == 0 && allow_fallback && key >= SCC_SPRITE_START && key <= SCC_SPRITE_END) {
328 const void *FreeTypeFontCache::InternalGetFontTable(uint32_t tag,
size_t &length)
331 FT_Byte *result =
nullptr;
333 FT_Load_Sfnt_Table(this->face, tag, 0,
nullptr, &len);
336 result = MallocT<FT_Byte>(len);
337 FT_Load_Sfnt_Table(this->face, tag, 0, result, &len);
349 FT_Done_FreeType(_library);
353 #if !defined(WITH_FONTCONFIG)
355 FT_Error
GetFontByFaceName(
const char *font_name, FT_Face *face) {
return FT_Err_Cannot_Open_Resource; }