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"
20 #include "../table/control_codes.h"
22 #include "../safeguards.h"
26 #include FT_FREETYPE_H
28 #include FT_TRUETYPE_TABLES_H
36 const void *InternalGetFontTable(uint32 tag,
size_t &length)
override;
37 const Sprite *InternalGetGlyph(
GlyphID key,
bool aa)
override;
44 std::string
GetFontName()
override {
return fmt::format(
"{}, {}",
face->family_name,
face->style_name); }
48 FT_Library _library =
nullptr;
59 assert(
face !=
nullptr);
61 this->SetFontSize(
fs,
face, pixels);
64 void FreeTypeFontCache::SetFontSize(
FontSize fs, FT_Face face,
int pixels)
68 int scaled_height =
ScaleGUITrad(FontCache::GetDefaultFontHeight(this->fs));
69 pixels = scaled_height;
71 TT_Header *head = (TT_Header *)FT_Get_Sfnt_Table(this->face, ft_sfnt_head);
72 if (head !=
nullptr) {
84 FT_Error err = FT_Set_Pixel_Sizes(this->face, 0, pixels);
85 if (err != FT_Err_Ok) {
88 FT_Bitmap_Size *bs = this->face->available_sizes;
89 int i = this->face->num_fixed_sizes;
94 if (
abs(pixels - bs->height) >=
abs(pixels - n))
continue;
96 chosen = this->face->num_fixed_sizes - i;
101 err = FT_Select_Size(this->face, chosen);
105 if (err == FT_Err_Ok) {
107 this->
ascender = this->face->size->metrics.ascender >> 6;
108 this->
descender = this->face->size->metrics.descender >> 6;
112 Debug(fontcache, 0,
"Font size selection failed. Using FontCache defaults.");
129 if (_library ==
nullptr) {
130 if (FT_Init_FreeType(&_library) != FT_Err_Ok) {
131 ShowInfoF(
"Unable to initialize FreeType, using sprite fonts instead");
135 Debug(fontcache, 2,
"Initialized");
138 const char *font_name =
settings->font.c_str();
139 FT_Face face =
nullptr;
142 FT_Error
error = FT_New_Face(_library, font_name, 0, &face);
144 #if defined(WITH_COCOA)
149 if (
error != FT_Err_Ok) {
152 if (!full_font.empty()) {
153 error = FT_New_Face(_library, full_font.c_str(), 0, &face);
154 #if defined(WITH_COCOA)
163 if (
error == FT_Err_Ok) {
164 Debug(fontcache, 2,
"Requested '{}', using '{} {}'", font_name, face->family_name, face->style_name);
167 error = FT_Select_Charmap(face, ft_encoding_unicode);
168 if (
error == FT_Err_Ok)
goto found_face;
170 if (
error == FT_Err_Invalid_CharMap_Handle) {
174 FT_CharMap found = face->charmaps[0];
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) {
184 if (found !=
nullptr) {
185 error = FT_Set_Charmap(face, found);
186 if (
error == FT_Err_Ok)
goto found_face;
193 ShowInfoF(
"Unable to use '%s' for %s font, FreeType reported error 0x%X, using sprite font instead", font_name, FontSizeToName(fs),
error);
206 FT_Done_Face(this->face);
207 this->face =
nullptr;
217 if (this->face !=
nullptr) this->SetFontSize(this->fs, this->face, this->
req_size);
223 const Sprite *FreeTypeFontCache::InternalGetGlyph(
GlyphID key,
bool aa)
225 FT_GlyphSlot slot = this->face->glyph;
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);
231 aa = (slot->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY);
235 uint width = std::max(1U, (uint)slot->bitmap.width + shadow);
236 uint
height = std::max(1U, (uint)slot->bitmap.rows + shadow);
246 sprite.
width = width;
248 sprite.
x_offs = slot->bitmap_left;
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;
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;
272 GlyphEntry new_glyph;
274 new_glyph.
width = slot->advance.x >> 6;
276 this->SetGlyphPtr(key, &new_glyph);
278 return new_glyph.sprite;
284 assert(IsPrintable(key));
286 if (key >= SCC_SPRITE_START && key <= SCC_SPRITE_END) {
290 return FT_Get_Char_Index(this->face, key);
293 const void *FreeTypeFontCache::InternalGetFontTable(uint32 tag,
size_t &length)
296 FT_Byte *result =
nullptr;
298 FT_Load_Sfnt_Table(this->face, tag, 0,
nullptr, &len);
301 result = MallocT<FT_Byte>(len);
302 FT_Load_Sfnt_Table(this->face, tag, 0, result, &len);
314 FT_Done_FreeType(_library);
318 #if !defined(_WIN32) && !defined(__APPLE__) && !defined(WITH_FONTCONFIG) && !defined(WITH_COCOA)
320 FT_Error
GetFontByFaceName(
const char *font_name, FT_Face *face) {
return FT_Err_Cannot_Open_Resource; }