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 virtual const void *InternalGetFontTable(uint32 tag,
size_t &length);
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(this->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.");
127 default: NOT_REACHED();
136 if (_library ==
nullptr) {
137 if (FT_Init_FreeType(&_library) != FT_Err_Ok) {
138 ShowInfoF(
"Unable to initialize FreeType, using sprite fonts instead");
142 Debug(fontcache, 2,
"Initialized");
145 const char *font_name =
settings->font.c_str();
146 FT_Face face =
nullptr;
149 FT_Error
error = FT_New_Face(_library, font_name, 0, &face);
151 #if defined(WITH_COCOA)
156 if (
error != FT_Err_Ok) {
159 if (!full_font.empty()) {
160 error = FT_New_Face(_library, full_font.c_str(), 0, &face);
161 #if defined(WITH_COCOA)
170 if (
error == FT_Err_Ok) {
171 Debug(fontcache, 2,
"Requested '{}', using '{} {}'", font_name, face->family_name, face->style_name);
174 error = FT_Select_Charmap(face, ft_encoding_unicode);
175 if (
error == FT_Err_Ok)
goto found_face;
177 if (
error == FT_Err_Invalid_CharMap_Handle) {
181 FT_CharMap found = face->charmaps[0];
184 for (i = 0; i < face->num_charmaps; i++) {
185 FT_CharMap charmap = face->charmaps[i];
186 if (charmap->platform_id == 0 && charmap->encoding_id == 0) {
191 if (found !=
nullptr) {
192 error = FT_Set_Charmap(face, found);
193 if (
error == FT_Err_Ok)
goto found_face;
200 static const char *SIZE_TO_NAME[] = {
"medium",
"small",
"large",
"mono" };
201 ShowInfoF(
"Unable to use '%s' for %s font, FreeType reported error 0x%X, using sprite font instead", font_name, SIZE_TO_NAME[fs],
error);
214 FT_Done_Face(this->face);
215 this->face =
nullptr;
225 if (this->face !=
nullptr) this->SetFontSize(this->fs, this->face, this->
req_size);
231 const Sprite *FreeTypeFontCache::InternalGetGlyph(
GlyphID key,
bool aa)
233 FT_GlyphSlot slot = this->face->glyph;
235 FT_Load_Glyph(this->face, key, aa ? FT_LOAD_TARGET_NORMAL : FT_LOAD_TARGET_MONO);
236 FT_Render_Glyph(this->face->glyph, aa ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO);
239 aa = (slot->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY);
243 uint width = std::max(1U, (uint)slot->bitmap.width + shadow);
244 uint
height = std::max(1U, (uint)slot->bitmap.rows + shadow);
254 sprite.
width = width;
256 sprite.
x_offs = slot->bitmap_left;
261 for (uint y = 0; y < (uint)slot->bitmap.rows; y++) {
262 for (uint x = 0; x < (uint)slot->bitmap.width; x++) {
263 if (
HasBit(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
264 sprite.
data[shadow + x + (shadow + y) * sprite.
width].
m = SHADOW_COLOUR;
265 sprite.
data[shadow + x + (shadow + y) * sprite.
width].
a = 0xFF;
271 for (uint y = 0; y < (uint)slot->bitmap.rows; y++) {
272 for (uint x = 0; x < (uint)slot->bitmap.width; x++) {
273 if (aa ? (slot->bitmap.buffer[x + y * slot->bitmap.pitch] > 0) :
HasBit(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
274 sprite.
data[x + y * sprite.
width].
m = FACE_COLOUR;
275 sprite.
data[x + y * sprite.
width].
a = aa ? slot->bitmap.buffer[x + y * slot->bitmap.pitch] : 0xFF;
280 GlyphEntry new_glyph;
282 new_glyph.
width = slot->advance.x >> 6;
284 this->SetGlyphPtr(key, &new_glyph);
286 return new_glyph.sprite;
292 assert(IsPrintable(key));
294 if (key >= SCC_SPRITE_START && key <= SCC_SPRITE_END) {
298 return FT_Get_Char_Index(this->face, key);
301 const void *FreeTypeFontCache::InternalGetFontTable(uint32 tag,
size_t &length)
304 FT_Byte *result =
nullptr;
306 FT_Load_Sfnt_Table(this->face, tag, 0,
nullptr, &len);
309 result = MallocT<FT_Byte>(len);
310 FT_Load_Sfnt_Table(this->face, tag, 0, result, &len);
322 FT_Done_FreeType(_library);
326 #if !defined(_WIN32) && !defined(__APPLE__) && !defined(WITH_FONTCONFIG) && !defined(WITH_COCOA)
328 FT_Error
GetFontByFaceName(
const char *font_name, FT_Face *face) {
return FT_Err_Cannot_Open_Resource; }