OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

fontcache.cpp
Go to the documentation of this file.
1 /* $Id: fontcache.cpp 27187 2015-03-15 12:19:58Z frosch $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
9
12 #include "stdafx.h"
13 #include "fontcache.h"
14 #include "fontdetection.h"
15 #include "blitter/factory.hpp"
16 #include "core/math_func.hpp"
17 #include "core/smallmap_type.hpp"
18 #include "strings_func.h"
19 #include "zoom_type.h"
20 #include "gfx_layout.h"
21 #include "zoom_func.h"
22
23 #include "table/sprites.h"
24 #include "table/control_codes.h"
25 #include "table/unicode.h"
26
27 #include "safeguards.h"
28
29 static const int ASCII_LETTERSTART = 32;
30 static const int MAX_FONT_SIZE = 72;
31
33 static const int _default_font_height[FS_END] = {10, 6, 18, 10};
34 static const int _default_font_ascender[FS_END] = { 8, 5, 15, 8};
35
40 FontCache::FontCache(FontSize fs) : parent(FontCache::Get(fs)), fs(fs), height(_default_font_height[fs]),
41 ascender(_default_font_ascender[fs]), descender(_default_font_ascender[fs] - _default_font_height[fs]),
42 units_per_em(1)
43 {
44 assert(parent == NULL || this->fs == parent->fs);
45 FontCache::caches[this->fs] = this;
46 Layouter::ResetFontCache(this->fs);
47 }
48
50 FontCache::~FontCache()
51 {
52 assert(this->fs == parent->fs);
53 FontCache::caches[this->fs] = this->parent;
54 Layouter::ResetFontCache(this->fs);
55 }
56
57
63 int GetCharacterHeight(FontSize size)
64 {
65 return FontCache::Get(size)->GetHeight();
66 }
67
68
70 class SpriteFontCache : public FontCache {
71 private:
72 SpriteID **glyph_to_spriteid_map;
73
74 void ClearGlyphToSpriteMap();
75 public:
76 SpriteFontCache(FontSize fs);
77 ~SpriteFontCache();
78 virtual SpriteID GetUnicodeGlyph(WChar key);
79 virtual void SetUnicodeGlyph(WChar key, SpriteID sprite);
80 virtual void InitializeUnicodeGlyphMap();
81 virtual void ClearFontCache();
82 virtual const Sprite *GetGlyph(GlyphID key);
83 virtual uint GetGlyphWidth(GlyphID key);
84 virtual int GetHeight() const;
85 virtual bool GetDrawGlyphShadow();
86 virtual GlyphID MapCharToGlyph(WChar key) { assert(IsPrintable(key)); return SPRITE_GLYPH | key; }
87 virtual const void *GetFontTable(uint32 tag, size_t &length) { length = 0; return NULL; }
88 virtual const char *GetFontName() { return "sprite"; }
89 };
90
95 SpriteFontCache::SpriteFontCache(FontSize fs) : FontCache(fs), glyph_to_spriteid_map(NULL)
96 {
97 this->InitializeUnicodeGlyphMap();
98 }
99
103 SpriteFontCache::~SpriteFontCache()
104 {
105 this->ClearGlyphToSpriteMap();
106 }
107
108 SpriteID SpriteFontCache::GetUnicodeGlyph(GlyphID key)
109 {
110 if (this->glyph_to_spriteid_map[GB(key, 8, 8)] == NULL) return 0;
111 return this->glyph_to_spriteid_map[GB(key, 8, 8)][GB(key, 0, 8)];
112 }
113
114 void SpriteFontCache::SetUnicodeGlyph(GlyphID key, SpriteID sprite)
115 {
116 if (this->glyph_to_spriteid_map == NULL) this->glyph_to_spriteid_map = CallocT<SpriteID*>(256);
117 if (this->glyph_to_spriteid_map[GB(key, 8, 8)] == NULL) this->glyph_to_spriteid_map[GB(key, 8, 8)] = CallocT<SpriteID>(256);
118 this->glyph_to_spriteid_map[GB(key, 8, 8)][GB(key, 0, 8)] = sprite;
119 }
120
121 void SpriteFontCache::InitializeUnicodeGlyphMap()
122 {
123 /* Clear out existing glyph map if it exists */
124 this->ClearGlyphToSpriteMap();
125
126 SpriteID base;
127 switch (this->fs) {
128 default: NOT_REACHED();
129 case FS_MONO: // Use normal as default for mono spaced font, i.e. FALL THROUGH
130 case FS_NORMAL: base = SPR_ASCII_SPACE; break;
131 case FS_SMALL: base = SPR_ASCII_SPACE_SMALL; break;
132 case FS_LARGE: base = SPR_ASCII_SPACE_BIG; break;
133 }
134
135 for (uint i = ASCII_LETTERSTART; i < 256; i++) {
136 SpriteID sprite = base + i - ASCII_LETTERSTART;
137 if (!SpriteExists(sprite)) continue;
138 this->SetUnicodeGlyph(i, sprite);
139 this->SetUnicodeGlyph(i + SCC_SPRITE_START, sprite);
140 }
141
142 for (uint i = 0; i < lengthof(_default_unicode_map); i++) {
143 byte key = _default_unicode_map[i].key;
144 if (key == CLRA) {
145 /* Clear the glyph. This happens if the glyph at this code point
146 * is non-standard and should be accessed by an SCC_xxx enum
147 * entry only. */
148 this->SetUnicodeGlyph(_default_unicode_map[i].code, 0);
149 } else {
150 SpriteID sprite = base + key - ASCII_LETTERSTART;
151 this->SetUnicodeGlyph(_default_unicode_map[i].code, sprite);
152 }
153 }
154 }
155
159 void SpriteFontCache::ClearGlyphToSpriteMap()
160 {
161 if (this->glyph_to_spriteid_map == NULL) return;
162
163 for (uint i = 0; i < 256; i++) {
164 free(this->glyph_to_spriteid_map[i]);
165 }
166 free(this->glyph_to_spriteid_map);
167 this->glyph_to_spriteid_map = NULL;
168 }
169
170 void SpriteFontCache::ClearFontCache()
171 {
172 Layouter::ResetFontCache(this->fs);
173 }
174
175 const Sprite *SpriteFontCache::GetGlyph(GlyphID key)
176 {
177 SpriteID sprite = this->GetUnicodeGlyph(key);
178 if (sprite == 0) sprite = this->GetUnicodeGlyph('?');
179 return GetSprite(sprite, ST_FONT);
180 }
181
182 uint SpriteFontCache::GetGlyphWidth(GlyphID key)
183 {
184 SpriteID sprite = this->GetUnicodeGlyph(key);
185 if (sprite == 0) sprite = this->GetUnicodeGlyph('?');
186 return SpriteExists(sprite) ? GetSprite(sprite, ST_FONT)->width + ScaleGUITrad(this->fs != FS_NORMAL ? 1 : 0) : 0;
187 }
188
189 int SpriteFontCache::GetHeight() const
190 {
191 return ScaleGUITrad(this->height);
192 }
193
194 bool SpriteFontCache::GetDrawGlyphShadow()
195 {
196 return false;
197 }
198
199 /* static */ FontCache *FontCache::caches[FS_END] = { new SpriteFontCache(FS_NORMAL), new SpriteFontCache(FS_SMALL), new SpriteFontCache(FS_LARGE), new SpriteFontCache(FS_MONO) };
200
201 #ifdef WITH_FREETYPE
202 #include <ft2build.h>
203 #include FT_FREETYPE_H
204 #include FT_GLYPH_H
205 #include FT_TRUETYPE_TABLES_H
206
208 class FreeTypeFontCache : public FontCache {
209 private:
210 FT_Face face;
211
212 typedef SmallMap<uint32, SmallPair<size_t, const void*> > FontTable;
213 FontTable font_tables;
214
216 struct GlyphEntry {
217 Sprite *sprite;
218 byte width;
219 bool duplicate;
220 };
221
235 GlyphEntry **glyph_to_sprite;
236
237 GlyphEntry *GetGlyphPtr(GlyphID key);
238 void SetGlyphPtr(GlyphID key, const GlyphEntry *glyph, bool duplicate = false);
239
240 public:
241 FreeTypeFontCache(FontSize fs, FT_Face face, int pixels);
242 ~FreeTypeFontCache();
243 virtual SpriteID GetUnicodeGlyph(WChar key) { return this->parent->GetUnicodeGlyph(key); }
244 virtual void SetUnicodeGlyph(WChar key, SpriteID sprite) { this->parent->SetUnicodeGlyph(key, sprite); }
245 virtual void InitializeUnicodeGlyphMap() { this->parent->InitializeUnicodeGlyphMap(); }
246 virtual void ClearFontCache();
247 virtual const Sprite *GetGlyph(GlyphID key);
248 virtual uint GetGlyphWidth(GlyphID key);
249 virtual bool GetDrawGlyphShadow();
250 virtual GlyphID MapCharToGlyph(WChar key);
251 virtual const void *GetFontTable(uint32 tag, size_t &length);
252 virtual const char *GetFontName() { return face->family_name; }
253 };
254
255 FT_Library _library = NULL;
256
257 FreeTypeSettings _freetype;
258
259 static const byte FACE_COLOUR = 1;
260 static const byte SHADOW_COLOUR = 2;
261
268 FreeTypeFontCache::FreeTypeFontCache(FontSize fs, FT_Face face, int pixels) : FontCache(fs), face(face), glyph_to_sprite(NULL)
269 {
270 assert(face != NULL);
271
272 if (pixels == 0) {
273 /* Try to determine a good height based on the minimal height recommended by the font. */
274 pixels = _default_font_height[this->fs];
275
276 TT_Header *head = (TT_Header *)FT_Get_Sfnt_Table(this->face, ft_sfnt_head);
277 if (head != NULL) {
278 /* Font height is minimum height plus the difference between the default
279 * height for this font size and the small size. */
280 int diff = _default_font_height[this->fs] - _default_font_height[FS_SMALL];
281 pixels = Clamp(min(head->Lowest_Rec_PPEM, 20) + diff, _default_font_height[this->fs], MAX_FONT_SIZE);
282 }
283 }
284
285 FT_Error err = FT_Set_Pixel_Sizes(this->face, 0, pixels);
286 if (err != FT_Err_Ok) {
287
288 /* Find nearest size to that requested */
289 FT_Bitmap_Size *bs = this->face->available_sizes;
290 int i = this->face->num_fixed_sizes;
291 if (i > 0) { // In pathetic cases one might get no fixed sizes at all.
292 int n = bs->height;
293 FT_Int chosen = 0;
294 for (; --i; bs++) {
295 if (abs(pixels - bs->height) >= abs(pixels - n)) continue;
296 n = bs->height;
297 chosen = this->face->num_fixed_sizes - i;
298 }
299
300 /* Don't use FT_Set_Pixel_Sizes here - it might give us another
301 * error, even though the size is available (FS#5885). */
302 err = FT_Select_Size(this->face, chosen);
303 }
304 }
305
306 if (err == FT_Err_Ok) {
307 this->units_per_em = this->face->units_per_EM;
308 this->ascender = this->face->size->metrics.ascender >> 6;
309 this->descender = this->face->size->metrics.descender >> 6;
310 this->height = this->ascender - this->descender;
311 } else {
312 /* Both FT_Set_Pixel_Sizes and FT_Select_Size failed. */
313 DEBUG(freetype, 0, "Font size selection failed. Using FontCache defaults.");
314 }
315 }
316
324 static void LoadFreeTypeFont(FontSize fs)
325 {
326 FreeTypeSubSetting *settings = NULL;
327 switch (fs) {
328 default: NOT_REACHED();
329 case FS_SMALL: settings = &_freetype.small; break;
330 case FS_NORMAL: settings = &_freetype.medium; break;
331 case FS_LARGE: settings = &_freetype.large; break;
332 case FS_MONO: settings = &_freetype.mono; break;
333 }
334
335 if (StrEmpty(settings->font)) return;
336
337 if (_library == NULL) {
338 if (FT_Init_FreeType(&_library) != FT_Err_Ok) {
339 ShowInfoF("Unable to initialize FreeType, using sprite fonts instead");
340 return;
341 }
342
343 DEBUG(freetype, 2, "Initialized");
344 }
345
346 FT_Face face = NULL;
347 FT_Error error = FT_New_Face(_library, settings->font, 0, &face);
348
349 if (error != FT_Err_Ok) error = GetFontByFaceName(settings->font, &face);
350
351 if (error == FT_Err_Ok) {
352 DEBUG(freetype, 2, "Requested '%s', using '%s %s'", settings->font, face->family_name, face->style_name);
353
354 /* Attempt to select the unicode character map */
355 error = FT_Select_Charmap(face, ft_encoding_unicode);
356 if (error == FT_Err_Ok) goto found_face; // Success
357
358 if (error == FT_Err_Invalid_CharMap_Handle) {
359 /* Try to pick a different character map instead. We default to
360 * the first map, but platform_id 0 encoding_id 0 should also
361 * be unicode (strange system...) */
362 FT_CharMap found = face->charmaps[0];
363 int i;
364
365 for (i = 0; i < face->num_charmaps; i++) {
366 FT_CharMap charmap = face->charmaps[i];
367 if (charmap->platform_id == 0 && charmap->encoding_id == 0) {
368 found = charmap;
369 }
370 }
371
372 if (found != NULL) {
373 error = FT_Set_Charmap(face, found);
374 if (error == FT_Err_Ok) goto found_face;
375 }
376 }
377 }
378
379 FT_Done_Face(face);
380
381 static const char *SIZE_TO_NAME[] = { "medium", "small", "large", "mono" };
382 ShowInfoF("Unable to use '%s' for %s font, FreeType reported error 0x%X, using sprite font instead", settings->font, SIZE_TO_NAME[fs], error);
383 return;
384
385 found_face:
386 new FreeTypeFontCache(fs, face, settings->size);
387 }
388
389
393 FreeTypeFontCache::~FreeTypeFontCache()
394 {
395 FT_Done_Face(this->face);
396 this->ClearFontCache();
397
398 for (FontTable::iterator iter = this->font_tables.Begin(); iter != this->font_tables.End(); iter++) {
399 free(iter->second.second);
400 }
401 }
402
406 void FreeTypeFontCache::ClearFontCache()
407 {
408 if (this->glyph_to_sprite == NULL) return;
409
410 for (int i = 0; i < 256; i++) {
411 if (this->glyph_to_sprite[i] == NULL) continue;
412
413 for (int j = 0; j < 256; j++) {
414 if (this->glyph_to_sprite[i][j].duplicate) continue;
415 free(this->glyph_to_sprite[i][j].sprite);
416 }
417
418 free(this->glyph_to_sprite[i]);
419 }
420
421 free(this->glyph_to_sprite);
422 this->glyph_to_sprite = NULL;
423
424 Layouter::ResetFontCache(this->fs);
425 }
426
427 FreeTypeFontCache::GlyphEntry *FreeTypeFontCache::GetGlyphPtr(GlyphID key)
428 {
429 if (this->glyph_to_sprite == NULL) return NULL;
430 if (this->glyph_to_sprite[GB(key, 8, 8)] == NULL) return NULL;
431 return &this->glyph_to_sprite[GB(key, 8, 8)][GB(key, 0, 8)];
432 }
433
434
435 void FreeTypeFontCache::SetGlyphPtr(GlyphID key, const GlyphEntry *glyph, bool duplicate)
436 {
437 if (this->glyph_to_sprite == NULL) {
438 DEBUG(freetype, 3, "Allocating root glyph cache for size %u", this->fs);
439 this->glyph_to_sprite = CallocT<GlyphEntry*>(256);
440 }
441
442 if (this->glyph_to_sprite[GB(key, 8, 8)] == NULL) {
443 DEBUG(freetype, 3, "Allocating glyph cache for range 0x%02X00, size %u", GB(key, 8, 8), this->fs);
444 this->glyph_to_sprite[GB(key, 8, 8)] = CallocT<GlyphEntry>(256);
445 }
446
447 DEBUG(freetype, 4, "Set glyph for unicode character 0x%04X, size %u", key, this->fs);
448 this->glyph_to_sprite[GB(key, 8, 8)][GB(key, 0, 8)].sprite = glyph->sprite;
449 this->glyph_to_sprite[GB(key, 8, 8)][GB(key, 0, 8)].width = glyph->width;
450 this->glyph_to_sprite[GB(key, 8, 8)][GB(key, 0, 8)].duplicate = duplicate;
451 }
452
453 static void *AllocateFont(size_t size)
454 {
455 return MallocT<byte>(size);
456 }
457
458
459 /* Check if a glyph should be rendered with antialiasing */
460 static bool GetFontAAState(FontSize size)
461 {
462 /* AA is only supported for 32 bpp */
463 if (BlitterFactory::GetCurrentBlitter()->GetScreenDepth() != 32) return false;
464
465 switch (size) {
466 default: NOT_REACHED();
467 case FS_NORMAL: return _freetype.medium.aa;
468 case FS_SMALL: return _freetype.small.aa;
469 case FS_LARGE: return _freetype.large.aa;
470 case FS_MONO: return _freetype.mono.aa;
471 }
472 }
473
474
475 const Sprite *FreeTypeFontCache::GetGlyph(GlyphID key)
476 {
477 if ((key & SPRITE_GLYPH) != 0) return parent->GetGlyph(key);
478
479 /* Check for the glyph in our cache */
480 GlyphEntry *glyph = this->GetGlyphPtr(key);
481 if (glyph != NULL && glyph->sprite != NULL) return glyph->sprite;
482
483 FT_GlyphSlot slot = this->face->glyph;
484
485 bool aa = GetFontAAState(this->fs);
486
487 GlyphEntry new_glyph;
488 if (key == 0) {
489 GlyphID question_glyph = this->MapCharToGlyph('?');
490 if (question_glyph == 0) {
491 /* The font misses the '?' character. Use built-in sprite.
492 * Note: We cannot use the baseset as this also has to work in the bootstrap GUI. */
493 #define CPSET { 0, 0, 0, 0, 1 }
494 #define CP___ { 0, 0, 0, 0, 0 }
495 static SpriteLoader::CommonPixel builtin_questionmark_data[10 * 8] = {
496 CP___, CP___, CPSET, CPSET, CPSET, CPSET, CP___, CP___,
497 CP___, CPSET, CPSET, CP___, CP___, CPSET, CPSET, CP___,
498 CP___, CP___, CP___, CP___, CP___, CPSET, CPSET, CP___,
499 CP___, CP___, CP___, CP___, CPSET, CPSET, CP___, CP___,
500 CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
501 CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
502 CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
503 CP___, CP___, CP___, CP___, CP___, CP___, CP___, CP___,
504 CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
505 CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
506 };
507 #undef CPSET
508 #undef CP___
509 static const SpriteLoader::Sprite builtin_questionmark = {
510 10, // height
511 8, // width
512 0, // x_offs
513 0, // y_offs
514 ST_FONT,
515 builtin_questionmark_data
516 };
517
518 Sprite *spr = BlitterFactory::GetCurrentBlitter()->Encode(&builtin_questionmark, AllocateFont);
519 assert(spr != NULL);
520 new_glyph.sprite = spr;
521 new_glyph.width = spr->width + (this->fs != FS_NORMAL);
522 this->SetGlyphPtr(key, &new_glyph, false);
523 return new_glyph.sprite;
524 } else {
525 /* Use '?' for missing characters. */
526 this->GetGlyph(question_glyph);
527 glyph = this->GetGlyphPtr(question_glyph);
528 this->SetGlyphPtr(key, glyph, true);
529 return glyph->sprite;
530 }
531 }
532 FT_Load_Glyph(this->face, key, FT_LOAD_DEFAULT);
533 FT_Render_Glyph(this->face->glyph, aa ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO);
534
535 /* Despite requesting a normal glyph, FreeType may have returned a bitmap */
536 aa = (slot->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY);
537
538 /* Add 1 pixel for the shadow on the medium font. Our sprite must be at least 1x1 pixel */
539 uint width = max(1U, (uint)slot->bitmap.width + (this->fs == FS_NORMAL));
540 uint height = max(1U, (uint)slot->bitmap.rows + (this->fs == FS_NORMAL));
541
542 /* Limit glyph size to prevent overflows later on. */
543 if (width > 256 || height > 256) usererror("Font glyph is too large");
544
545 /* FreeType has rendered the glyph, now we allocate a sprite and copy the image into it */
546 SpriteLoader::Sprite sprite;
547 sprite.AllocateData(ZOOM_LVL_NORMAL, width * height);
548 sprite.type = ST_FONT;
549 sprite.width = width;
550 sprite.height = height;
551 sprite.x_offs = slot->bitmap_left;
552 sprite.y_offs = this->ascender - slot->bitmap_top;
553
554 /* Draw shadow for medium size */
555 if (this->fs == FS_NORMAL && !aa) {
556 for (uint y = 0; y < (uint)slot->bitmap.rows; y++) {
557 for (uint x = 0; x < (uint)slot->bitmap.width; x++) {
558 if (aa ? (slot->bitmap.buffer[x + y * slot->bitmap.pitch] > 0) : HasBit(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
559 sprite.data[1 + x + (1 + y) * sprite.width].m = SHADOW_COLOUR;
560 sprite.data[1 + x + (1 + y) * sprite.width].a = aa ? slot->bitmap.buffer[x + y * slot->bitmap.pitch] : 0xFF;
561 }
562 }
563 }
564 }
565
566 for (uint y = 0; y < (uint)slot->bitmap.rows; y++) {
567 for (uint x = 0; x < (uint)slot->bitmap.width; x++) {
568 if (aa ? (slot->bitmap.buffer[x + y * slot->bitmap.pitch] > 0) : HasBit(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
569 sprite.data[x + y * sprite.width].m = FACE_COLOUR;
570 sprite.data[x + y * sprite.width].a = aa ? slot->bitmap.buffer[x + y * slot->bitmap.pitch] : 0xFF;
571 }
572 }
573 }
574
575 new_glyph.sprite = BlitterFactory::GetCurrentBlitter()->Encode(&sprite, AllocateFont);
576 new_glyph.width = slot->advance.x >> 6;
577
578 this->SetGlyphPtr(key, &new_glyph);
579
580 return new_glyph.sprite;
581 }
582
583
584 bool FreeTypeFontCache::GetDrawGlyphShadow()
585 {
586 return this->fs == FS_NORMAL && GetFontAAState(FS_NORMAL);
587 }
588
589
590 uint FreeTypeFontCache::GetGlyphWidth(GlyphID key)
591 {
592 if ((key & SPRITE_GLYPH) != 0) return this->parent->GetGlyphWidth(key);
593
594 GlyphEntry *glyph = this->GetGlyphPtr(key);
595 if (glyph == NULL || glyph->sprite == NULL) {
596 this->GetGlyph(key);
597 glyph = this->GetGlyphPtr(key);
598 }
599
600 return glyph->width;
601 }
602
603 GlyphID FreeTypeFontCache::MapCharToGlyph(WChar key)
604 {
605 assert(IsPrintable(key));
606
607 if (key >= SCC_SPRITE_START && key <= SCC_SPRITE_END) {
608 return this->parent->MapCharToGlyph(key);
609 }
610
611 return FT_Get_Char_Index(this->face, key);
612 }
613
614 const void *FreeTypeFontCache::GetFontTable(uint32 tag, size_t &length)
615 {
616 const FontTable::iterator iter = this->font_tables.Find(tag);
617 if (iter != this->font_tables.End()) {
618 length = iter->second.first;
619 return iter->second.second;
620 }
621
622 FT_ULong len = 0;
623 FT_Byte *result = NULL;
624
625 FT_Load_Sfnt_Table(this->face, tag, 0, NULL, &len);
626
627 if (len > 0) {
628 result = MallocT<FT_Byte>(len);
629 FT_Load_Sfnt_Table(this->face, tag, 0, result, &len);
630 }
631 length = len;
632
633 this->font_tables.Insert(tag, SmallPair<size_t, const void *>(length, result));
634 return result;
635 }
636
637 #endif /* WITH_FREETYPE */
638
643 void InitFreeType(bool monospace)
644 {
645 for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
646 if (monospace != (fs == FS_MONO)) continue;
647
648 FontCache *fc = FontCache::Get(fs);
649 if (fc->HasParent()) delete fc;
650
651 #ifdef WITH_FREETYPE
652 LoadFreeTypeFont(fs);
653 #endif
654 }
655 }
656
660 void UninitFreeType()
661 {
662 for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
663 FontCache *fc = FontCache::Get(fs);
664 if (fc->HasParent()) delete fc;
665 }
666
667 #ifdef WITH_FREETYPE
668 FT_Done_FreeType(_library);
669 _library = NULL;
670 #endif /* WITH_FREETYPE */
671 }