33 static const int _default_font_ascender[FS_END] = { 8, 5, 15, 8};
42 ascender(_default_font_ascender[fs]), descender(_default_font_ascender[fs] -
_default_font_height[fs]),
58 int FontCache::GetDefaultFontHeight(
FontSize fs)
93 virtual const void *
GetFontTable(uint32 tag,
size_t &length) { length = 0;
return nullptr; }
135 default: NOT_REACHED();
137 case FS_NORMAL: base = SPR_ASCII_SPACE;
break;
138 case FS_SMALL: base = SPR_ASCII_SPACE_SMALL;
break;
139 case FS_LARGE: base = SPR_ASCII_SPACE_BIG;
break;
144 if (!SpriteExists(sprite))
continue;
149 for (uint i = 0; i <
lengthof(_default_unicode_map); i++) {
150 byte key = _default_unicode_map[i].key;
170 for (uint i = 0; i < 256; i++) {
186 return GetSprite(sprite,
ST_FONT);
226 free(iter.second.second);
237 for (
int i = 0; i < 256; i++) {
240 for (
int j = 0; j < 256; j++) {
262 void TrueTypeFontCache::SetGlyphPtr(
GlyphID key,
const GlyphEntry *glyph,
bool duplicate)
265 DEBUG(freetype, 3,
"Allocating root glyph cache for size %u", this->
fs);
270 DEBUG(freetype, 3,
"Allocating glyph cache for range 0x%02X00, size %u",
GB(key, 8, 8), this->
fs);
274 DEBUG(freetype, 4,
"Set glyph for unicode character 0x%04X, size %u", key, this->
fs);
282 static bool GetFontAAState(
FontSize size,
bool check_blitter =
true)
288 default: NOT_REACHED();
306 if (glyph ==
nullptr || glyph->
sprite ==
nullptr) {
308 glyph = this->GetGlyphPtr(key);
316 if ((key & SPRITE_GLYPH) != 0)
return this->
parent->
GetGlyph(key);
320 if (glyph !=
nullptr && glyph->
sprite !=
nullptr)
return glyph->
sprite;
324 if (question_glyph == 0) {
327 #define CPSET { 0, 0, 0, 0, 1 }
328 #define CP___ { 0, 0, 0, 0, 0 }
330 CP___, CP___, CPSET, CPSET, CPSET, CPSET, CP___, CP___,
331 CP___, CPSET, CPSET, CP___, CP___, CPSET, CPSET, CP___,
332 CP___, CP___, CP___, CP___, CP___, CPSET, CPSET, CP___,
333 CP___, CP___, CP___, CP___, CPSET, CPSET, CP___, CP___,
334 CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
335 CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
336 CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
337 CP___, CP___, CP___, CP___, CP___, CP___, CP___, CP___,
338 CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
339 CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
350 builtin_questionmark_data
354 assert(spr !=
nullptr);
358 this->SetGlyphPtr(key, &new_glyph,
false);
363 glyph = this->GetGlyphPtr(question_glyph);
364 this->SetGlyphPtr(key, glyph,
true);
369 return this->InternalGetGlyph(key, GetFontAAState(this->
fs));
375 if (iter != this->
font_tables.data() + this->font_tables.size()) {
376 length = iter->second.first;
377 return iter->second.second;
380 const void *result = this->InternalGetFontTable(tag, length);
388 #include <ft2build.h>
389 #include FT_FREETYPE_H
391 #include FT_TRUETYPE_TABLES_H
399 virtual const void *InternalGetFontTable(uint32 tag,
size_t &length);
400 virtual const Sprite *InternalGetGlyph(
GlyphID key,
bool aa);
411 FT_Library _library =
nullptr;
422 assert(
face !=
nullptr);
424 this->SetFontSize(
fs,
face, pixels);
427 void FreeTypeFontCache::SetFontSize(
FontSize fs, FT_Face face,
int pixels)
432 pixels = scaled_height;
434 TT_Header *head = (TT_Header *)FT_Get_Sfnt_Table(this->face, ft_sfnt_head);
435 if (head !=
nullptr) {
446 FT_Error err = FT_Set_Pixel_Sizes(this->face, 0, pixels);
447 if (err != FT_Err_Ok) {
450 FT_Bitmap_Size *bs = this->face->available_sizes;
451 int i = this->face->num_fixed_sizes;
456 if (
abs(pixels - bs->height) >=
abs(pixels - n))
continue;
458 chosen = this->face->num_fixed_sizes - i;
463 err = FT_Select_Size(this->face, chosen);
467 if (err == FT_Err_Ok) {
469 this->
ascender = this->face->size->metrics.ascender >> 6;
470 this->
descender = this->face->size->metrics.descender >> 6;
474 DEBUG(freetype, 0,
"Font size selection failed. Using FontCache defaults.");
489 default: NOT_REACHED();
498 if (_library ==
nullptr) {
499 if (FT_Init_FreeType(&_library) != FT_Err_Ok) {
500 ShowInfoF(
"Unable to initialize FreeType, using sprite fonts instead");
504 DEBUG(freetype, 2,
"Initialized");
507 FT_Face face =
nullptr;
510 FT_Error
error = FT_New_Face(_library,
settings->font, 0, &face);
512 #if defined(WITH_COCOA)
517 if (
error != FT_Err_Ok) {
520 if (!full_font.empty()) {
521 error = FT_New_Face(_library, full_font.c_str(), 0, &face);
522 #if defined(WITH_COCOA)
531 if (
error == FT_Err_Ok) {
532 DEBUG(freetype, 2,
"Requested '%s', using '%s %s'",
settings->font, face->family_name, face->style_name);
535 error = FT_Select_Charmap(face, ft_encoding_unicode);
536 if (
error == FT_Err_Ok)
goto found_face;
538 if (
error == FT_Err_Invalid_CharMap_Handle) {
542 FT_CharMap found = face->charmaps[0];
545 for (i = 0; i < face->num_charmaps; i++) {
546 FT_CharMap charmap = face->charmaps[i];
547 if (charmap->platform_id == 0 && charmap->encoding_id == 0) {
552 if (found !=
nullptr) {
553 error = FT_Set_Charmap(face, found);
554 if (
error == FT_Err_Ok)
goto found_face;
561 static const char *SIZE_TO_NAME[] = {
"medium",
"small",
"large",
"mono" };
562 ShowInfoF(
"Unable to use '%s' for %s font, FreeType reported error 0x%X, using sprite font instead",
settings->font, SIZE_TO_NAME[fs],
error);
575 FT_Done_Face(this->face);
576 this->face =
nullptr;
586 if (this->face !=
nullptr) this->SetFontSize(this->fs, this->face, this->
req_size);
592 const Sprite *FreeTypeFontCache::InternalGetGlyph(
GlyphID key,
bool aa)
594 FT_GlyphSlot slot = this->face->glyph;
596 FT_Load_Glyph(this->face, key, aa ? FT_LOAD_TARGET_NORMAL : FT_LOAD_TARGET_MONO);
597 FT_Render_Glyph(this->face->glyph, aa ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO);
600 aa = (slot->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY);
603 uint width = std::max(1U, (uint)slot->bitmap.width + (this->fs ==
FS_NORMAL));
604 uint
height = std::max(1U, (uint)slot->bitmap.rows + (this->fs ==
FS_NORMAL));
614 sprite.
width = width;
616 sprite.
x_offs = slot->bitmap_left;
621 for (uint y = 0; y < (uint)slot->bitmap.rows; y++) {
622 for (uint x = 0; x < (uint)slot->bitmap.width; x++) {
623 if (aa ? (slot->bitmap.buffer[x + y * slot->bitmap.pitch] > 0) :
HasBit(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
624 sprite.
data[1 + x + (1 + y) * sprite.
width].
m = SHADOW_COLOUR;
625 sprite.
data[1 + x + (1 + y) * sprite.
width].
a = aa ? slot->bitmap.buffer[x + y * slot->bitmap.pitch] : 0xFF;
631 for (uint y = 0; y < (uint)slot->bitmap.rows; y++) {
632 for (uint x = 0; x < (uint)slot->bitmap.width; x++) {
633 if (aa ? (slot->bitmap.buffer[x + y * slot->bitmap.pitch] > 0) :
HasBit(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
634 sprite.
data[x + y * sprite.
width].
m = FACE_COLOUR;
635 sprite.
data[x + y * sprite.
width].
a = aa ? slot->bitmap.buffer[x + y * slot->bitmap.pitch] : 0xFF;
640 GlyphEntry new_glyph;
642 new_glyph.
width = slot->advance.x >> 6;
644 this->SetGlyphPtr(key, &new_glyph);
646 return new_glyph.sprite;
652 assert(IsPrintable(key));
654 if (key >= SCC_SPRITE_START && key <= SCC_SPRITE_END) {
658 return FT_Get_Char_Index(this->face, key);
661 const void *FreeTypeFontCache::InternalGetFontTable(uint32 tag,
size_t &length)
664 FT_Byte *result =
nullptr;
666 FT_Load_Sfnt_Table(this->face, tag, 0,
nullptr, &len);
669 result = MallocT<FT_Byte>(len);
670 FT_Load_Sfnt_Table(this->face, tag, 0, result, &len);
686 if (monospace != (fs ==
FS_MONO))
continue;
693 #elif defined(_WIN32)
696 #elif defined(WITH_COCOA)
714 FT_Done_FreeType(_library);
726 if (!
FontCache::Get(fs)->IsBuiltInFont() && GetFontAAState(fs,
false))
return true;
732 #if !defined(_WIN32) && !defined(__APPLE__) && !defined(WITH_FONTCONFIG) && !defined(WITH_COCOA)
735 FT_Error
GetFontByFaceName(
const char *font_name, FT_Face *face) {
return FT_Err_Cannot_Open_Resource; }