OpenTTD Source  1.11.0-beta2
fontcache.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
10 #include "stdafx.h"
11 #include "fontcache.h"
12 #include "fontcache_internal.h"
13 #include "fontdetection.h"
14 #include "blitter/factory.hpp"
15 #include "core/math_func.hpp"
16 #include "core/smallmap_type.hpp"
17 #include "strings_func.h"
18 #include "zoom_type.h"
19 #include "gfx_layout.h"
20 #include "zoom_func.h"
21 #include "fileio_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 
32 static const int _default_font_height[FS_END] = {10, 6, 18, 10};
33 static const int _default_font_ascender[FS_END] = { 8, 5, 15, 8};
34 
35 FreeTypeSettings _freetype;
36 
41 FontCache::FontCache(FontSize fs) : parent(FontCache::Get(fs)), fs(fs), height(_default_font_height[fs]),
42  ascender(_default_font_ascender[fs]), descender(_default_font_ascender[fs] - _default_font_height[fs]),
43  units_per_em(1)
44 {
45  assert(this->parent == nullptr || this->fs == this->parent->fs);
46  FontCache::caches[this->fs] = this;
47  Layouter::ResetFontCache(this->fs);
48 }
49 
52 {
53  assert(this->fs == this->parent->fs);
54  FontCache::caches[this->fs] = this->parent;
56 }
57 
58 int FontCache::GetDefaultFontHeight(FontSize fs)
59 {
60  return _default_font_height[fs];
61 }
62 
63 
70 {
71  return FontCache::Get(size)->GetHeight();
72 }
73 
74 
76 class SpriteFontCache : public FontCache {
77 private:
79 
80  void ClearGlyphToSpriteMap();
81 public:
84  virtual SpriteID GetUnicodeGlyph(WChar key);
85  virtual void SetUnicodeGlyph(WChar key, SpriteID sprite);
86  virtual void InitializeUnicodeGlyphMap();
87  virtual void ClearFontCache();
88  virtual const Sprite *GetGlyph(GlyphID key);
89  virtual uint GetGlyphWidth(GlyphID key);
90  virtual int GetHeight() const;
91  virtual bool GetDrawGlyphShadow();
92  virtual GlyphID MapCharToGlyph(WChar key) { assert(IsPrintable(key)); return SPRITE_GLYPH | key; }
93  virtual const void *GetFontTable(uint32 tag, size_t &length) { length = 0; return nullptr; }
94  virtual const char *GetFontName() { return "sprite"; }
95  virtual bool IsBuiltInFont() { return true; }
96 };
97 
102 SpriteFontCache::SpriteFontCache(FontSize fs) : FontCache(fs), glyph_to_spriteid_map(nullptr)
103 {
105 }
106 
111 {
112  this->ClearGlyphToSpriteMap();
113 }
114 
116 {
117  if (this->glyph_to_spriteid_map[GB(key, 8, 8)] == nullptr) return 0;
118  return this->glyph_to_spriteid_map[GB(key, 8, 8)][GB(key, 0, 8)];
119 }
120 
122 {
123  if (this->glyph_to_spriteid_map == nullptr) this->glyph_to_spriteid_map = CallocT<SpriteID*>(256);
124  if (this->glyph_to_spriteid_map[GB(key, 8, 8)] == nullptr) this->glyph_to_spriteid_map[GB(key, 8, 8)] = CallocT<SpriteID>(256);
125  this->glyph_to_spriteid_map[GB(key, 8, 8)][GB(key, 0, 8)] = sprite;
126 }
127 
129 {
130  /* Clear out existing glyph map if it exists */
131  this->ClearGlyphToSpriteMap();
132 
133  SpriteID base;
134  switch (this->fs) {
135  default: NOT_REACHED();
136  case FS_MONO: // Use normal as default for mono spaced font
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;
140  }
141 
142  for (uint i = ASCII_LETTERSTART; i < 256; i++) {
143  SpriteID sprite = base + i - ASCII_LETTERSTART;
144  if (!SpriteExists(sprite)) continue;
145  this->SetUnicodeGlyph(i, sprite);
146  this->SetUnicodeGlyph(i + SCC_SPRITE_START, sprite);
147  }
148 
149  for (uint i = 0; i < lengthof(_default_unicode_map); i++) {
150  byte key = _default_unicode_map[i].key;
151  if (key == CLRA) {
152  /* Clear the glyph. This happens if the glyph at this code point
153  * is non-standard and should be accessed by an SCC_xxx enum
154  * entry only. */
155  this->SetUnicodeGlyph(_default_unicode_map[i].code, 0);
156  } else {
157  SpriteID sprite = base + key - ASCII_LETTERSTART;
158  this->SetUnicodeGlyph(_default_unicode_map[i].code, sprite);
159  }
160  }
161 }
162 
167 {
168  if (this->glyph_to_spriteid_map == nullptr) return;
169 
170  for (uint i = 0; i < 256; i++) {
171  free(this->glyph_to_spriteid_map[i]);
172  }
174  this->glyph_to_spriteid_map = nullptr;
175 }
176 
178 {
180 }
181 
183 {
184  SpriteID sprite = this->GetUnicodeGlyph(key);
185  if (sprite == 0) sprite = this->GetUnicodeGlyph('?');
186  return GetSprite(sprite, ST_FONT);
187 }
188 
190 {
191  SpriteID sprite = this->GetUnicodeGlyph(key);
192  if (sprite == 0) sprite = this->GetUnicodeGlyph('?');
193  return SpriteExists(sprite) ? GetSprite(sprite, ST_FONT)->width + ScaleFontTrad(this->fs != FS_NORMAL ? 1 : 0) : 0;
194 }
195 
197 {
198  return ScaleFontTrad(this->height);
199 }
200 
202 {
203  return false;
204 }
205 
207 
208 
214 TrueTypeFontCache::TrueTypeFontCache(FontSize fs, int pixels) : FontCache(fs), req_size(pixels), glyph_to_sprite(nullptr)
215 {
216 }
217 
222 {
223  this->ClearFontCache();
224 
225  for (auto &iter : this->font_tables) {
226  free(iter.second.second);
227  }
228 }
229 
234 {
235  if (this->glyph_to_sprite == nullptr) return;
236 
237  for (int i = 0; i < 256; i++) {
238  if (this->glyph_to_sprite[i] == nullptr) continue;
239 
240  for (int j = 0; j < 256; j++) {
241  if (this->glyph_to_sprite[i][j].duplicate) continue;
242  free(this->glyph_to_sprite[i][j].sprite);
243  }
244 
245  free(this->glyph_to_sprite[i]);
246  }
247 
248  free(this->glyph_to_sprite);
249  this->glyph_to_sprite = nullptr;
250 
252 }
253 
254 
255 TrueTypeFontCache::GlyphEntry *TrueTypeFontCache::GetGlyphPtr(GlyphID key)
256 {
257  if (this->glyph_to_sprite == nullptr) return nullptr;
258  if (this->glyph_to_sprite[GB(key, 8, 8)] == nullptr) return nullptr;
259  return &this->glyph_to_sprite[GB(key, 8, 8)][GB(key, 0, 8)];
260 }
261 
262 void TrueTypeFontCache::SetGlyphPtr(GlyphID key, const GlyphEntry *glyph, bool duplicate)
263 {
264  if (this->glyph_to_sprite == nullptr) {
265  DEBUG(freetype, 3, "Allocating root glyph cache for size %u", this->fs);
266  this->glyph_to_sprite = CallocT<GlyphEntry*>(256);
267  }
268 
269  if (this->glyph_to_sprite[GB(key, 8, 8)] == nullptr) {
270  DEBUG(freetype, 3, "Allocating glyph cache for range 0x%02X00, size %u", GB(key, 8, 8), this->fs);
271  this->glyph_to_sprite[GB(key, 8, 8)] = CallocT<GlyphEntry>(256);
272  }
273 
274  DEBUG(freetype, 4, "Set glyph for unicode character 0x%04X, size %u", key, this->fs);
275  this->glyph_to_sprite[GB(key, 8, 8)][GB(key, 0, 8)].sprite = glyph->sprite;
276  this->glyph_to_sprite[GB(key, 8, 8)][GB(key, 0, 8)].width = glyph->width;
277  this->glyph_to_sprite[GB(key, 8, 8)][GB(key, 0, 8)].duplicate = duplicate;
278 }
279 
280 
281 /* Check if a glyph should be rendered with anti-aliasing. */
282 static bool GetFontAAState(FontSize size, bool check_blitter = true)
283 {
284  /* AA is only supported for 32 bpp */
285  if (check_blitter && BlitterFactory::GetCurrentBlitter()->GetScreenDepth() != 32) return false;
286 
287  switch (size) {
288  default: NOT_REACHED();
289  case FS_NORMAL: return _freetype.medium.aa;
290  case FS_SMALL: return _freetype.small.aa;
291  case FS_LARGE: return _freetype.large.aa;
292  case FS_MONO: return _freetype.mono.aa;
293  }
294 }
295 
297 {
298  return this->fs == FS_NORMAL && GetFontAAState(FS_NORMAL);
299 }
300 
302 {
303  if ((key & SPRITE_GLYPH) != 0) return this->parent->GetGlyphWidth(key);
304 
305  GlyphEntry *glyph = this->GetGlyphPtr(key);
306  if (glyph == nullptr || glyph->sprite == nullptr) {
307  this->GetGlyph(key);
308  glyph = this->GetGlyphPtr(key);
309  }
310 
311  return glyph->width;
312 }
313 
315 {
316  if ((key & SPRITE_GLYPH) != 0) return this->parent->GetGlyph(key);
317 
318  /* Check for the glyph in our cache */
319  GlyphEntry *glyph = this->GetGlyphPtr(key);
320  if (glyph != nullptr && glyph->sprite != nullptr) return glyph->sprite;
321 
322  if (key == 0) {
323  GlyphID question_glyph = this->MapCharToGlyph('?');
324  if (question_glyph == 0) {
325  /* The font misses the '?' character. Use built-in sprite.
326  * Note: We cannot use the baseset as this also has to work in the bootstrap GUI. */
327 #define CPSET { 0, 0, 0, 0, 1 }
328 #define CP___ { 0, 0, 0, 0, 0 }
329  static SpriteLoader::CommonPixel builtin_questionmark_data[10 * 8] = {
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___,
340  };
341 #undef CPSET
342 #undef CP___
343  static const SpriteLoader::Sprite builtin_questionmark = {
344  10, // height
345  8, // width
346  0, // x_offs
347  0, // y_offs
348  ST_FONT,
349  SCC_PAL,
350  builtin_questionmark_data
351  };
352 
353  Sprite *spr = BlitterFactory::GetCurrentBlitter()->Encode(&builtin_questionmark, SimpleSpriteAlloc);
354  assert(spr != nullptr);
355  GlyphEntry new_glyph;
356  new_glyph.sprite = spr;
357  new_glyph.width = spr->width + (this->fs != FS_NORMAL);
358  this->SetGlyphPtr(key, &new_glyph, false);
359  return new_glyph.sprite;
360  } else {
361  /* Use '?' for missing characters. */
362  this->GetGlyph(question_glyph);
363  glyph = this->GetGlyphPtr(question_glyph);
364  this->SetGlyphPtr(key, glyph, true);
365  return glyph->sprite;
366  }
367  }
368 
369  return this->InternalGetGlyph(key, GetFontAAState(this->fs));
370 }
371 
372 const void *TrueTypeFontCache::GetFontTable(uint32 tag, size_t &length)
373 {
374  const FontTable::iterator iter = this->font_tables.Find(tag);
375  if (iter != this->font_tables.data() + this->font_tables.size()) {
376  length = iter->second.first;
377  return iter->second.second;
378  }
379 
380  const void *result = this->InternalGetFontTable(tag, length);
381 
382  this->font_tables.Insert(tag, std::pair<size_t, const void *>(length, result));
383  return result;
384 }
385 
386 
387 #ifdef WITH_FREETYPE
388 #include <ft2build.h>
389 #include FT_FREETYPE_H
390 #include FT_GLYPH_H
391 #include FT_TRUETYPE_TABLES_H
392 
395 private:
396  FT_Face face;
397 
398  void SetFontSize(FontSize fs, FT_Face face, int pixels);
399  virtual const void *InternalGetFontTable(uint32 tag, size_t &length);
400  virtual const Sprite *InternalGetGlyph(GlyphID key, bool aa);
401 
402 public:
403  FreeTypeFontCache(FontSize fs, FT_Face face, int pixels);
405  virtual void ClearFontCache();
406  virtual GlyphID MapCharToGlyph(WChar key);
407  virtual const char *GetFontName() { return face->family_name; }
408  virtual bool IsBuiltInFont() { return false; }
409 };
410 
411 FT_Library _library = nullptr;
412 
413 
420 FreeTypeFontCache::FreeTypeFontCache(FontSize fs, FT_Face face, int pixels) : TrueTypeFontCache(fs, pixels), face(face)
421 {
422  assert(face != nullptr);
423 
424  this->SetFontSize(fs, face, pixels);
425 }
426 
427 void FreeTypeFontCache::SetFontSize(FontSize fs, FT_Face face, int pixels)
428 {
429  if (pixels == 0) {
430  /* Try to determine a good height based on the minimal height recommended by the font. */
431  int scaled_height = ScaleFontTrad(_default_font_height[this->fs]);
432  pixels = scaled_height;
433 
434  TT_Header *head = (TT_Header *)FT_Get_Sfnt_Table(this->face, ft_sfnt_head);
435  if (head != nullptr) {
436  /* Font height is minimum height plus the difference between the default
437  * height for this font size and the small size. */
438  int diff = scaled_height - ScaleFontTrad(_default_font_height[FS_SMALL]);
439  pixels = Clamp(std::min<uint>(head->Lowest_Rec_PPEM, MAX_FONT_MIN_REC_SIZE) + diff, scaled_height, MAX_FONT_SIZE);
440  }
441  } else {
442  pixels = ScaleFontTrad(pixels);
443  }
444  this->used_size = pixels;
445 
446  FT_Error err = FT_Set_Pixel_Sizes(this->face, 0, pixels);
447  if (err != FT_Err_Ok) {
448 
449  /* Find nearest size to that requested */
450  FT_Bitmap_Size *bs = this->face->available_sizes;
451  int i = this->face->num_fixed_sizes;
452  if (i > 0) { // In pathetic cases one might get no fixed sizes at all.
453  int n = bs->height;
454  FT_Int chosen = 0;
455  for (; --i; bs++) {
456  if (abs(pixels - bs->height) >= abs(pixels - n)) continue;
457  n = bs->height;
458  chosen = this->face->num_fixed_sizes - i;
459  }
460 
461  /* Don't use FT_Set_Pixel_Sizes here - it might give us another
462  * error, even though the size is available (FS#5885). */
463  err = FT_Select_Size(this->face, chosen);
464  }
465  }
466 
467  if (err == FT_Err_Ok) {
468  this->units_per_em = this->face->units_per_EM;
469  this->ascender = this->face->size->metrics.ascender >> 6;
470  this->descender = this->face->size->metrics.descender >> 6;
471  this->height = this->ascender - this->descender;
472  } else {
473  /* Both FT_Set_Pixel_Sizes and FT_Select_Size failed. */
474  DEBUG(freetype, 0, "Font size selection failed. Using FontCache defaults.");
475  }
476 }
477 
485 static void LoadFreeTypeFont(FontSize fs)
486 {
487  FreeTypeSubSetting *settings = nullptr;
488  switch (fs) {
489  default: NOT_REACHED();
490  case FS_SMALL: settings = &_freetype.small; break;
491  case FS_NORMAL: settings = &_freetype.medium; break;
492  case FS_LARGE: settings = &_freetype.large; break;
493  case FS_MONO: settings = &_freetype.mono; break;
494  }
495 
496  if (StrEmpty(settings->font)) return;
497 
498  if (_library == nullptr) {
499  if (FT_Init_FreeType(&_library) != FT_Err_Ok) {
500  ShowInfoF("Unable to initialize FreeType, using sprite fonts instead");
501  return;
502  }
503 
504  DEBUG(freetype, 2, "Initialized");
505  }
506 
507  FT_Face face = nullptr;
508 
509  /* If font is an absolute path to a ttf, try loading that first. */
510  FT_Error error = FT_New_Face(_library, settings->font, 0, &face);
511 
512 #if defined(WITH_COCOA)
513  extern void MacOSRegisterExternalFont(const char *file_path);
514  if (error == FT_Err_Ok) MacOSRegisterExternalFont(settings->font);
515 #endif
516 
517  if (error != FT_Err_Ok) {
518  /* Check if font is a relative filename in one of our search-paths. */
519  std::string full_font = FioFindFullPath(BASE_DIR, settings->font);
520  if (!full_font.empty()) {
521  error = FT_New_Face(_library, full_font.c_str(), 0, &face);
522 #if defined(WITH_COCOA)
523  if (error == FT_Err_Ok) MacOSRegisterExternalFont(full_font.c_str());
524 #endif
525  }
526  }
527 
528  /* Try loading based on font face name (OS-wide fonts). */
529  if (error != FT_Err_Ok) error = GetFontByFaceName(settings->font, &face);
530 
531  if (error == FT_Err_Ok) {
532  DEBUG(freetype, 2, "Requested '%s', using '%s %s'", settings->font, face->family_name, face->style_name);
533 
534  /* Attempt to select the unicode character map */
535  error = FT_Select_Charmap(face, ft_encoding_unicode);
536  if (error == FT_Err_Ok) goto found_face; // Success
537 
538  if (error == FT_Err_Invalid_CharMap_Handle) {
539  /* Try to pick a different character map instead. We default to
540  * the first map, but platform_id 0 encoding_id 0 should also
541  * be unicode (strange system...) */
542  FT_CharMap found = face->charmaps[0];
543  int i;
544 
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) {
548  found = charmap;
549  }
550  }
551 
552  if (found != nullptr) {
553  error = FT_Set_Charmap(face, found);
554  if (error == FT_Err_Ok) goto found_face;
555  }
556  }
557  }
558 
559  FT_Done_Face(face);
560 
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);
563  return;
564 
565 found_face:
566  new FreeTypeFontCache(fs, face, settings->size);
567 }
568 
569 
574 {
575  FT_Done_Face(this->face);
576  this->face = nullptr;
577  this->ClearFontCache();
578 }
579 
584 {
585  /* Font scaling might have changed, determine font size anew if it was automatically selected. */
586  if (this->face != nullptr) this->SetFontSize(this->fs, this->face, this->req_size);
587 
589 }
590 
591 
592 const Sprite *FreeTypeFontCache::InternalGetGlyph(GlyphID key, bool aa)
593 {
594  FT_GlyphSlot slot = this->face->glyph;
595 
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);
598 
599  /* Despite requesting a normal glyph, FreeType may have returned a bitmap */
600  aa = (slot->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY);
601 
602  /* Add 1 pixel for the shadow on the medium font. Our sprite must be at least 1x1 pixel */
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));
605 
606  /* Limit glyph size to prevent overflows later on. */
607  if (width > MAX_GLYPH_DIM || height > MAX_GLYPH_DIM) usererror("Font glyph is too large");
608 
609  /* FreeType has rendered the glyph, now we allocate a sprite and copy the image into it */
610  SpriteLoader::Sprite sprite;
611  sprite.AllocateData(ZOOM_LVL_NORMAL, width * height);
612  sprite.type = ST_FONT;
613  sprite.colours = (aa ? SCC_PAL | SCC_ALPHA : SCC_PAL);
614  sprite.width = width;
615  sprite.height = height;
616  sprite.x_offs = slot->bitmap_left;
617  sprite.y_offs = this->ascender - slot->bitmap_top;
618 
619  /* Draw shadow for medium size */
620  if (this->fs == FS_NORMAL && !aa) {
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;
626  }
627  }
628  }
629  }
630 
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;
636  }
637  }
638  }
639 
640  GlyphEntry new_glyph;
641  new_glyph.sprite = BlitterFactory::GetCurrentBlitter()->Encode(&sprite, SimpleSpriteAlloc);
642  new_glyph.width = slot->advance.x >> 6;
643 
644  this->SetGlyphPtr(key, &new_glyph);
645 
646  return new_glyph.sprite;
647 }
648 
649 
651 {
652  assert(IsPrintable(key));
653 
654  if (key >= SCC_SPRITE_START && key <= SCC_SPRITE_END) {
655  return this->parent->MapCharToGlyph(key);
656  }
657 
658  return FT_Get_Char_Index(this->face, key);
659 }
660 
661 const void *FreeTypeFontCache::InternalGetFontTable(uint32 tag, size_t &length)
662 {
663  FT_ULong len = 0;
664  FT_Byte *result = nullptr;
665 
666  FT_Load_Sfnt_Table(this->face, tag, 0, nullptr, &len);
667 
668  if (len > 0) {
669  result = MallocT<FT_Byte>(len);
670  FT_Load_Sfnt_Table(this->face, tag, 0, result, &len);
671  }
672 
673  length = len;
674  return result;
675 }
676 #endif /* WITH_FREETYPE */
677 
678 
683 void InitFreeType(bool monospace)
684 {
685  for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
686  if (monospace != (fs == FS_MONO)) continue;
687 
688  FontCache *fc = FontCache::Get(fs);
689  if (fc->HasParent()) delete fc;
690 
691 #ifdef WITH_FREETYPE
692  LoadFreeTypeFont(fs);
693 #elif defined(_WIN32)
694  extern void LoadWin32Font(FontSize fs);
695  LoadWin32Font(fs);
696 #elif defined(WITH_COCOA)
697  extern void LoadCoreTextFont(FontSize fs);
698  LoadCoreTextFont(fs);
699 #endif
700  }
701 }
702 
707 {
708  for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
709  FontCache *fc = FontCache::Get(fs);
710  if (fc->HasParent()) delete fc;
711  }
712 
713 #ifdef WITH_FREETYPE
714  FT_Done_FreeType(_library);
715  _library = nullptr;
716 #endif /* WITH_FREETYPE */
717 }
718 
724 {
725  for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
726  if (!FontCache::Get(fs)->IsBuiltInFont() && GetFontAAState(fs, false)) return true;
727  }
728 
729  return false;
730 }
731 
732 #if !defined(_WIN32) && !defined(__APPLE__) && !defined(WITH_FONTCONFIG) && !defined(WITH_COCOA)
733 
734 #ifdef WITH_FREETYPE
735 FT_Error GetFontByFaceName(const char *font_name, FT_Face *face) { return FT_Err_Cannot_Open_Resource; }
736 #endif /* WITH_FREETYPE */
737 
738 bool SetFallbackFont(FreeTypeSettings *settings, const char *language_isocode, int winlangid, MissingGlyphSearcher *callback) { return false; }
739 #endif /* !defined(_WIN32) && !defined(__APPLE__) && !defined(WITH_FONTCONFIG) && !defined(WITH_COCOA) */
SpriteLoader::CommonPixel::m
uint8 m
Remap-channel.
Definition: spriteloader.hpp:38
SCC_PAL
@ SCC_PAL
Sprite has palette data.
Definition: spriteloader.hpp:24
GlyphID
uint32 GlyphID
Glyphs are characters from a font.
Definition: fontcache.h:17
TrueTypeFontCache::GlyphEntry::sprite
Sprite * sprite
The loaded sprite.
Definition: fontcache_internal.h:36
factory.hpp
SpriteFontCache::InitializeUnicodeGlyphMap
virtual void InitializeUnicodeGlyphMap()
Initialize the glyph map.
Definition: fontcache.cpp:128
TrueTypeFontCache::font_tables
FontTable font_tables
Cached font tables.
Definition: fontcache_internal.h:32
TrueTypeFontCache::GetFontTable
const void * GetFontTable(uint32 tag, size_t &length) override
Read a font table from the font.
Definition: fontcache.cpp:372
TrueTypeFontCache::~TrueTypeFontCache
virtual ~TrueTypeFontCache()
Free everything that was allocated for this font cache.
Definition: fontcache.cpp:221
TrueTypeFontCache
Font cache for fonts that are based on a TrueType font.
Definition: fontcache_internal.h:23
TrueTypeFontCache::GlyphEntry::duplicate
bool duplicate
Whether this glyph entry is a duplicate, i.e. may this be freed?
Definition: fontcache_internal.h:38
WChar
char32_t WChar
Type for wide characters, i.e.
Definition: string_type.h:35
_default_font_height
static const int _default_font_height[FS_END]
Default heights for the different sizes of fonts.
Definition: fontcache.cpp:32
usererror
void CDECL usererror(const char *s,...)
Error handling for fatal user errors.
Definition: openttd.cpp:100
GB
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
MissingGlyphSearcher
A searcher for missing glyphs.
Definition: strings_func.h:244
ScaleFontTrad
static int ScaleFontTrad(int value)
Scale traditional pixel dimensions to Font zoom level.
Definition: zoom_func.h:96
ST_FONT
@ ST_FONT
A sprite used for fonts.
Definition: gfx_type.h:304
SpriteFontCache::GetGlyphWidth
virtual uint GetGlyphWidth(GlyphID key)
Get the width of the glyph with the given key.
Definition: fontcache.cpp:189
FreeTypeSettings::large
FreeTypeSubSetting large
The largest font; mostly used for newspapers.
Definition: fontcache.h:228
TrueTypeFontCache::glyph_to_sprite
GlyphEntry ** glyph_to_sprite
The glyph cache.
Definition: fontcache_internal.h:54
SpriteLoader::Sprite::AllocateData
void AllocateData(ZoomLevel zoom, size_t size)
Allocate the sprite data of this sprite.
Definition: spriteloader.hpp:61
FS_BEGIN
@ FS_BEGIN
First font.
Definition: gfx_type.h:213
FontCache::height
int height
The height of the font.
Definition: fontcache.h:27
math_func.hpp
FreeTypeFontCache
Font cache for fonts that are based on a freetype font.
Definition: fontcache.cpp:394
FreeTypeSettings::mono
FreeTypeSubSetting mono
The mono space font used for license/readme viewers.
Definition: fontcache.h:229
SpriteFontCache::GetHeight
virtual int GetHeight() const
Get the height of the font.
Definition: fontcache.cpp:196
FreeTypeFontCache::~FreeTypeFontCache
~FreeTypeFontCache()
Free everything that was allocated for this font cache.
Definition: fontcache.cpp:573
FreeTypeSubSetting::aa
bool aa
Whether to do anti aliasing or not.
Definition: fontcache.h:219
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103
FS_LARGE
@ FS_LARGE
Index of the large font in the font tables.
Definition: gfx_type.h:209
FreeTypeFontCache::ClearFontCache
virtual void ClearFontCache()
Reset cached glyphs.
Definition: fontcache.cpp:583
TrueTypeFontCache::GlyphEntry
Container for information about a glyph.
Definition: fontcache_internal.h:35
zoom_func.h
fileio_func.h
Layouter::ResetFontCache
static void ResetFontCache(FontSize size)
Reset cached font information.
Definition: gfx_layout.cpp:858
ASCII_LETTERSTART
static const int ASCII_LETTERSTART
First printable ASCII letter.
Definition: fontcache.cpp:29
smallmap_type.hpp
SpriteEncoder::Encode
virtual Sprite * Encode(const SpriteLoader::Sprite *sprite, AllocatorProc *allocator)=0
Convert a sprite from the loader to our own format.
TrueTypeFontCache::GetGlyphWidth
uint GetGlyphWidth(GlyphID key) override
Get the width of the glyph with the given key.
Definition: fontcache.cpp:301
SpriteFontCache::GetUnicodeGlyph
virtual SpriteID GetUnicodeGlyph(WChar key)
Get the SpriteID mapped to the given key.
Definition: fontcache.cpp:115
SpriteFontCache
Font cache for fonts that are based on a freetype font.
Definition: fontcache.cpp:76
SmallMap::Insert
bool Insert(const T &key, const U &data)
Adds new item to this map.
Definition: smallmap_type.hpp:127
SpriteFontCache::MapCharToGlyph
virtual GlyphID MapCharToGlyph(WChar key)
Map a character into a glyph.
Definition: fontcache.cpp:92
TrueTypeFontCache::GetDrawGlyphShadow
bool GetDrawGlyphShadow() override
Do we need to draw a glyph shadow?
Definition: fontcache.cpp:296
control_codes.h
SpriteID
uint32 SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:17
LoadFreeTypeFont
static void LoadFreeTypeFont(FontSize fs)
Loads the freetype font.
Definition: fontcache.cpp:485
SpriteLoader::Sprite::data
SpriteLoader::CommonPixel * data
The sprite itself.
Definition: spriteloader.hpp:54
FontCache::units_per_em
int units_per_em
The units per EM value of the font.
Definition: fontcache.h:30
BASE_DIR
@ BASE_DIR
Base directory for all subdirectories.
Definition: fileio_type.h:109
FreeTypeFontCache::face
FT_Face face
The font face associated with this font.
Definition: fontcache.cpp:396
FontCache::HasParent
bool HasParent()
Check whether the font cache has a parent.
Definition: fontcache.h:158
TrueTypeFontCache::MAX_GLYPH_DIM
static constexpr int MAX_GLYPH_DIM
Maximum glyph dimensions.
Definition: fontcache_internal.h:25
FS_NORMAL
@ FS_NORMAL
Index of the normal font in the font tables.
Definition: gfx_type.h:207
LoadWin32Font
void LoadWin32Font(FontSize fs)
Loads the GDI font.
Definition: font_win32.cpp:577
SpriteLoader::Sprite::type
SpriteType type
The sprite type.
Definition: spriteloader.hpp:52
FreeTypeFontCache::GetFontName
virtual const char * GetFontName()
Get the name of this font.
Definition: fontcache.cpp:407
FreeTypeFontCache::IsBuiltInFont
virtual bool IsBuiltInFont()
Is this a built-in sprite font?
Definition: fontcache.cpp:408
DEBUG
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
FS_SMALL
@ FS_SMALL
Index of the small font in the font tables.
Definition: gfx_type.h:208
HasAntialiasedFonts
bool HasAntialiasedFonts()
Should any of the active fonts be anti-aliased?
Definition: fontcache.cpp:723
SpriteLoader::CommonPixel
Definition of a common pixel in OpenTTD's realm.
Definition: spriteloader.hpp:33
TrueTypeFontCache::MAX_FONT_MIN_REC_SIZE
static constexpr uint MAX_FONT_MIN_REC_SIZE
Upper limit for the recommended font size in case a font file contains nonsensical values.
Definition: fontcache_internal.h:26
BlitterFactory::GetCurrentBlitter
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition: factory.hpp:140
fontcache_internal.h
FontCache::~FontCache
virtual ~FontCache()
Clean everything up.
Definition: fontcache.cpp:51
safeguards.h
Sprite::width
uint16 width
Width of the sprite.
Definition: spritecache.h:19
UninitFreeType
void UninitFreeType()
Free everything allocated w.r.t.
Definition: fontcache.cpp:706
SpriteLoader::Sprite::x_offs
int16 x_offs
The x-offset of where the sprite will be drawn.
Definition: spriteloader.hpp:50
StrEmpty
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:60
settings
fluid_settings_t * settings
FluidSynth settings handle.
Definition: fluidsynth.cpp:21
SpriteFontCache::GetFontTable
virtual const void * GetFontTable(uint32 tag, size_t &length)
Read a font table from the font.
Definition: fontcache.cpp:93
fontdetection.h
SpriteFontCache::GetFontName
virtual const char * GetFontName()
Get the name of this font.
Definition: fontcache.cpp:94
gfx_layout.h
GetFontByFaceName
FT_Error GetFontByFaceName(const char *font_name, FT_Face *face)
Load a freetype font face with the given font name.
Definition: font_osx.cpp:35
SpriteFontCache::glyph_to_spriteid_map
SpriteID ** glyph_to_spriteid_map
Mapping of glyphs to sprite IDs.
Definition: fontcache.cpp:78
sprites.h
FontCache::fs
const FontSize fs
The size of the font.
Definition: fontcache.h:26
stdafx.h
FontCache::GetGlyphWidth
virtual uint GetGlyphWidth(GlyphID key)=0
Get the width of the glyph with the given key.
SpriteFontCache::GetDrawGlyphShadow
virtual bool GetDrawGlyphShadow()
Do we need to draw a glyph shadow?
Definition: fontcache.cpp:201
SpriteLoader::Sprite::colours
SpriteColourComponent colours
The colour components of the sprite with useful information.
Definition: spriteloader.hpp:53
MacOSRegisterExternalFont
void MacOSRegisterExternalFont(const char *file_path)
Register an external font file with the CoreText system.
Definition: string_osx.cpp:297
CLRA
static const byte CLRA
Identifier to clear all glyphs at this codepoint.
Definition: unicode.h:15
TrueTypeFontCache::req_size
int req_size
Requested font size.
Definition: fontcache_internal.h:28
FontCache::descender
int descender
The descender value of the font.
Definition: fontcache.h:29
FontCache::caches
static FontCache * caches[FS_END]
All the font caches.
Definition: fontcache.h:23
Clamp
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:77
SpriteLoader::Sprite::width
uint16 width
Width of the sprite.
Definition: spriteloader.hpp:49
strings_func.h
FontCache
Font cache for basic fonts.
Definition: fontcache.h:21
FreeTypeFontCache::FreeTypeFontCache
FreeTypeFontCache(FontSize fs, FT_Face face, int pixels)
Create a new FreeTypeFontCache.
Definition: fontcache.cpp:420
SpriteFontCache::SetUnicodeGlyph
virtual void SetUnicodeGlyph(WChar key, SpriteID sprite)
Map a SpriteID to the key.
Definition: fontcache.cpp:121
FreeTypeSubSetting
Settings for a single freetype font.
Definition: fontcache.h:216
FontCache::GetGlyph
virtual const Sprite * GetGlyph(GlyphID key)=0
Get the glyph (sprite) of the given key.
FreeTypeSettings::medium
FreeTypeSubSetting medium
The normal font size.
Definition: fontcache.h:227
MAX_FONT_SIZE
static const int MAX_FONT_SIZE
Maximum font size.
Definition: fontcache_internal.h:17
FontCache::parent
FontCache * parent
The parent of this font cache.
Definition: fontcache.h:25
SCC_ALPHA
@ SCC_ALPHA
Sprite has alpha.
Definition: spriteloader.hpp:23
ShowInfoF
void CDECL ShowInfoF(const char *str,...)
Shows some information on the console/a popup box depending on the OS.
Definition: openttd.cpp:151
FontCache::ascender
int ascender
The ascender value of the font.
Definition: fontcache.h:28
SpriteFontCache::ClearFontCache
virtual void ClearFontCache()
Clear the font cache.
Definition: fontcache.cpp:177
SpriteFontCache::~SpriteFontCache
~SpriteFontCache()
Free everything we allocated.
Definition: fontcache.cpp:110
TrueTypeFontCache::ClearFontCache
void ClearFontCache() override
Reset cached glyphs.
Definition: fontcache.cpp:233
InitFreeType
void InitFreeType(bool monospace)
(Re)initialize the freetype related things, i.e.
Definition: fontcache.cpp:683
SpriteLoader::Sprite
Structure for passing information from the sprite loader to the blitter.
Definition: spriteloader.hpp:47
abs
static T abs(const T a)
Returns the absolute value of (scalar) variable.
Definition: math_func.hpp:21
TrueTypeFontCache::used_size
int used_size
Used font size.
Definition: fontcache_internal.h:29
error
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:129
TrueTypeFontCache::TrueTypeFontCache
TrueTypeFontCache(FontSize fs, int pixels)
Create a new TrueTypeFontCache.
Definition: fontcache.cpp:214
SmallMap::Find
std::vector< Pair >::const_iterator Find(const T &key) const
Finds given key in this map.
Definition: smallmap_type.hpp:41
GetCharacterHeight
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
Definition: fontcache.cpp:69
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:367
unicode.h
TrueTypeFontCache::GlyphEntry::width
byte width
The width of the glyph.
Definition: fontcache_internal.h:37
FS_MONO
@ FS_MONO
Index of the monospaced font in the font tables.
Definition: gfx_type.h:210
SpriteLoader::CommonPixel::a
uint8 a
Alpha-channel.
Definition: spriteloader.hpp:37
fontcache.h
FreeTypeFontCache::MapCharToGlyph
virtual GlyphID MapCharToGlyph(WChar key)
Map a character into a glyph.
Definition: fontcache.cpp:650
SpriteFontCache::ClearGlyphToSpriteMap
void ClearGlyphToSpriteMap()
Clear the glyph to sprite mapping.
Definition: fontcache.cpp:166
ZOOM_LVL_NORMAL
@ ZOOM_LVL_NORMAL
The normal zoom level.
Definition: zoom_type.h:24
FontSize
FontSize
Available font sizes.
Definition: gfx_type.h:206
zoom_type.h
SpriteFontCache::IsBuiltInFont
virtual bool IsBuiltInFont()
Is this a built-in sprite font?
Definition: fontcache.cpp:95
SpriteLoader::Sprite::y_offs
int16 y_offs
The y-offset of where the sprite will be drawn.
Definition: spriteloader.hpp:51
SpriteFontCache::GetGlyph
virtual const Sprite * GetGlyph(GlyphID key)
Get the glyph (sprite) of the given key.
Definition: fontcache.cpp:182
LoadCoreTextFont
void LoadCoreTextFont(FontSize fs)
Loads the TrueType font.
Definition: font_osx.cpp:349
SpriteLoader::Sprite::height
uint16 height
Height of the sprite.
Definition: spriteloader.hpp:48
TrueTypeFontCache::GetGlyph
const Sprite * GetGlyph(GlyphID key) override
Get the glyph (sprite) of the given key.
Definition: fontcache.cpp:314
FioFindFullPath
std::string FioFindFullPath(Subdirectory subdir, const char *filename)
Find a path to the filename in one of the search directories.
Definition: fileio.cpp:299
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:454
SimpleSpriteAlloc
void * SimpleSpriteAlloc(size_t size)
Sprite allocator simply using malloc.
Definition: spritecache.cpp:815
FontCache::MapCharToGlyph
virtual GlyphID MapCharToGlyph(WChar key)=0
Map a character into a glyph.
SpriteFontCache::SpriteFontCache
SpriteFontCache(FontSize fs)
Create a new sprite font cache.
Definition: fontcache.cpp:102
FreeTypeSettings::small
FreeTypeSubSetting small
The smallest font; mostly used for zoomed out view.
Definition: fontcache.h:226
Sprite
Data structure describing a sprite.
Definition: spritecache.h:17
FontCache::FontCache
FontCache(FontSize fs)
Create a new font cache.
Definition: fontcache.cpp:41
FreeTypeSettings
Settings for the freetype fonts.
Definition: fontcache.h:225
FontCache::Get
static FontCache * Get(FontSize fs)
Get the font cache of a given font size.
Definition: fontcache.h:149
SetFallbackFont
bool SetFallbackFont(struct FreeTypeSettings *settings, const char *language_isocode, int winlangid, class MissingGlyphSearcher *callback)
We would like to have a fallback font as the current one doesn't contain all characters we need.
Definition: font_osx.cpp:70
FontCache::GetHeight
virtual int GetHeight() const
Get the height of the font.
Definition: fontcache.h:48