OpenTTD Source  12.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 bool GetDrawGlyphShadow();
91  virtual GlyphID MapCharToGlyph(WChar key) { assert(IsPrintable(key)); return SPRITE_GLYPH | key; }
92  virtual const void *GetFontTable(uint32 tag, size_t &length) { length = 0; return nullptr; }
93  virtual const char *GetFontName() { return "sprite"; }
94  virtual bool IsBuiltInFont() { return true; }
95 };
96 
101 SpriteFontCache::SpriteFontCache(FontSize fs) : FontCache(fs), glyph_to_spriteid_map(nullptr)
102 {
104  this->height = ScaleFontTrad(this->GetDefaultFontHeight(this->fs));
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  this->height = ScaleFontTrad(this->GetDefaultFontHeight(this->fs));
181 }
182 
184 {
185  SpriteID sprite = this->GetUnicodeGlyph(key);
186  if (sprite == 0) sprite = this->GetUnicodeGlyph('?');
187  return GetSprite(sprite, ST_FONT);
188 }
189 
191 {
192  SpriteID sprite = this->GetUnicodeGlyph(key);
193  if (sprite == 0) sprite = this->GetUnicodeGlyph('?');
194  return SpriteExists(sprite) ? GetSprite(sprite, ST_FONT)->width + ScaleFontTrad(this->fs != FS_NORMAL ? 1 : 0) : 0;
195 }
196 
198 {
199  return false;
200 }
201 
203 
204 
210 TrueTypeFontCache::TrueTypeFontCache(FontSize fs, int pixels) : FontCache(fs), req_size(pixels), glyph_to_sprite(nullptr)
211 {
212 }
213 
218 {
219  /* Virtual functions get called statically in destructors, so make it explicit to remove any confusion. */
221 
222  for (auto &iter : this->font_tables) {
223  free(iter.second.second);
224  }
225 }
226 
231 {
232  if (this->glyph_to_sprite == nullptr) return;
233 
234  for (int i = 0; i < 256; i++) {
235  if (this->glyph_to_sprite[i] == nullptr) continue;
236 
237  for (int j = 0; j < 256; j++) {
238  if (this->glyph_to_sprite[i][j].duplicate) continue;
239  free(this->glyph_to_sprite[i][j].sprite);
240  }
241 
242  free(this->glyph_to_sprite[i]);
243  }
244 
245  free(this->glyph_to_sprite);
246  this->glyph_to_sprite = nullptr;
247 
249 }
250 
251 
252 TrueTypeFontCache::GlyphEntry *TrueTypeFontCache::GetGlyphPtr(GlyphID key)
253 {
254  if (this->glyph_to_sprite == nullptr) return nullptr;
255  if (this->glyph_to_sprite[GB(key, 8, 8)] == nullptr) return nullptr;
256  return &this->glyph_to_sprite[GB(key, 8, 8)][GB(key, 0, 8)];
257 }
258 
259 void TrueTypeFontCache::SetGlyphPtr(GlyphID key, const GlyphEntry *glyph, bool duplicate)
260 {
261  if (this->glyph_to_sprite == nullptr) {
262  Debug(freetype, 3, "Allocating root glyph cache for size {}", this->fs);
263  this->glyph_to_sprite = CallocT<GlyphEntry*>(256);
264  }
265 
266  if (this->glyph_to_sprite[GB(key, 8, 8)] == nullptr) {
267  Debug(freetype, 3, "Allocating glyph cache for range 0x{:02X}00, size {}", GB(key, 8, 8), this->fs);
268  this->glyph_to_sprite[GB(key, 8, 8)] = CallocT<GlyphEntry>(256);
269  }
270 
271  Debug(freetype, 4, "Set glyph for unicode character 0x{:04X}, size {}", key, this->fs);
272  this->glyph_to_sprite[GB(key, 8, 8)][GB(key, 0, 8)].sprite = glyph->sprite;
273  this->glyph_to_sprite[GB(key, 8, 8)][GB(key, 0, 8)].width = glyph->width;
274  this->glyph_to_sprite[GB(key, 8, 8)][GB(key, 0, 8)].duplicate = duplicate;
275 }
276 
277 
278 /* Check if a glyph should be rendered with anti-aliasing. */
279 static bool GetFontAAState(FontSize size, bool check_blitter = true)
280 {
281  /* AA is only supported for 32 bpp */
282  if (check_blitter && BlitterFactory::GetCurrentBlitter()->GetScreenDepth() != 32) return false;
283 
284  switch (size) {
285  default: NOT_REACHED();
286  case FS_NORMAL: return _freetype.medium.aa;
287  case FS_SMALL: return _freetype.small.aa;
288  case FS_LARGE: return _freetype.large.aa;
289  case FS_MONO: return _freetype.mono.aa;
290  }
291 }
292 
294 {
295  return this->fs == FS_NORMAL && GetFontAAState(FS_NORMAL);
296 }
297 
299 {
300  if ((key & SPRITE_GLYPH) != 0) return this->parent->GetGlyphWidth(key);
301 
302  GlyphEntry *glyph = this->GetGlyphPtr(key);
303  if (glyph == nullptr || glyph->sprite == nullptr) {
304  this->GetGlyph(key);
305  glyph = this->GetGlyphPtr(key);
306  }
307 
308  return glyph->width;
309 }
310 
312 {
313  if ((key & SPRITE_GLYPH) != 0) return this->parent->GetGlyph(key);
314 
315  /* Check for the glyph in our cache */
316  GlyphEntry *glyph = this->GetGlyphPtr(key);
317  if (glyph != nullptr && glyph->sprite != nullptr) return glyph->sprite;
318 
319  if (key == 0) {
320  GlyphID question_glyph = this->MapCharToGlyph('?');
321  if (question_glyph == 0) {
322  /* The font misses the '?' character. Use built-in sprite.
323  * Note: We cannot use the baseset as this also has to work in the bootstrap GUI. */
324 #define CPSET { 0, 0, 0, 0, 1 }
325 #define CP___ { 0, 0, 0, 0, 0 }
326  static SpriteLoader::CommonPixel builtin_questionmark_data[10 * 8] = {
327  CP___, CP___, CPSET, CPSET, CPSET, CPSET, CP___, CP___,
328  CP___, CPSET, CPSET, CP___, CP___, CPSET, CPSET, CP___,
329  CP___, CP___, CP___, CP___, CP___, CPSET, CPSET, CP___,
330  CP___, CP___, CP___, CP___, CPSET, CPSET, CP___, CP___,
331  CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
332  CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
333  CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
334  CP___, CP___, CP___, CP___, CP___, CP___, CP___, CP___,
335  CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
336  CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
337  };
338 #undef CPSET
339 #undef CP___
340  static const SpriteLoader::Sprite builtin_questionmark = {
341  10, // height
342  8, // width
343  0, // x_offs
344  0, // y_offs
345  ST_FONT,
346  SCC_PAL,
347  builtin_questionmark_data
348  };
349 
350  Sprite *spr = BlitterFactory::GetCurrentBlitter()->Encode(&builtin_questionmark, SimpleSpriteAlloc);
351  assert(spr != nullptr);
352  GlyphEntry new_glyph;
353  new_glyph.sprite = spr;
354  new_glyph.width = spr->width + (this->fs != FS_NORMAL);
355  this->SetGlyphPtr(key, &new_glyph, false);
356  return new_glyph.sprite;
357  } else {
358  /* Use '?' for missing characters. */
359  this->GetGlyph(question_glyph);
360  glyph = this->GetGlyphPtr(question_glyph);
361  this->SetGlyphPtr(key, glyph, true);
362  return glyph->sprite;
363  }
364  }
365 
366  return this->InternalGetGlyph(key, GetFontAAState(this->fs));
367 }
368 
369 const void *TrueTypeFontCache::GetFontTable(uint32 tag, size_t &length)
370 {
371  const FontTable::iterator iter = this->font_tables.Find(tag);
372  if (iter != this->font_tables.data() + this->font_tables.size()) {
373  length = iter->second.first;
374  return iter->second.second;
375  }
376 
377  const void *result = this->InternalGetFontTable(tag, length);
378 
379  this->font_tables.Insert(tag, std::pair<size_t, const void *>(length, result));
380  return result;
381 }
382 
383 
384 #ifdef WITH_FREETYPE
385 #include <ft2build.h>
386 #include FT_FREETYPE_H
387 #include FT_GLYPH_H
388 #include FT_TRUETYPE_TABLES_H
389 
392 private:
393  FT_Face face;
394 
395  void SetFontSize(FontSize fs, FT_Face face, int pixels);
396  virtual const void *InternalGetFontTable(uint32 tag, size_t &length);
397  virtual const Sprite *InternalGetGlyph(GlyphID key, bool aa);
398 
399 public:
400  FreeTypeFontCache(FontSize fs, FT_Face face, int pixels);
402  virtual void ClearFontCache();
403  virtual GlyphID MapCharToGlyph(WChar key);
404  virtual const char *GetFontName() { return face->family_name; }
405  virtual bool IsBuiltInFont() { return false; }
406 };
407 
408 FT_Library _library = nullptr;
409 
410 
417 FreeTypeFontCache::FreeTypeFontCache(FontSize fs, FT_Face face, int pixels) : TrueTypeFontCache(fs, pixels), face(face)
418 {
419  assert(face != nullptr);
420 
421  this->SetFontSize(fs, face, pixels);
422 }
423 
424 void FreeTypeFontCache::SetFontSize(FontSize fs, FT_Face face, int pixels)
425 {
426  if (pixels == 0) {
427  /* Try to determine a good height based on the minimal height recommended by the font. */
428  int scaled_height = ScaleFontTrad(this->GetDefaultFontHeight(this->fs));
429  pixels = scaled_height;
430 
431  TT_Header *head = (TT_Header *)FT_Get_Sfnt_Table(this->face, ft_sfnt_head);
432  if (head != nullptr) {
433  /* Font height is minimum height plus the difference between the default
434  * height for this font size and the small size. */
435  int diff = scaled_height - ScaleFontTrad(this->GetDefaultFontHeight(FS_SMALL));
436  pixels = Clamp(std::min<uint>(head->Lowest_Rec_PPEM, MAX_FONT_MIN_REC_SIZE) + diff, scaled_height, MAX_FONT_SIZE);
437  }
438  } else {
439  pixels = ScaleFontTrad(pixels);
440  }
441  this->used_size = pixels;
442 
443  FT_Error err = FT_Set_Pixel_Sizes(this->face, 0, pixels);
444  if (err != FT_Err_Ok) {
445 
446  /* Find nearest size to that requested */
447  FT_Bitmap_Size *bs = this->face->available_sizes;
448  int i = this->face->num_fixed_sizes;
449  if (i > 0) { // In pathetic cases one might get no fixed sizes at all.
450  int n = bs->height;
451  FT_Int chosen = 0;
452  for (; --i; bs++) {
453  if (abs(pixels - bs->height) >= abs(pixels - n)) continue;
454  n = bs->height;
455  chosen = this->face->num_fixed_sizes - i;
456  }
457 
458  /* Don't use FT_Set_Pixel_Sizes here - it might give us another
459  * error, even though the size is available (FS#5885). */
460  err = FT_Select_Size(this->face, chosen);
461  }
462  }
463 
464  if (err == FT_Err_Ok) {
465  this->units_per_em = this->face->units_per_EM;
466  this->ascender = this->face->size->metrics.ascender >> 6;
467  this->descender = this->face->size->metrics.descender >> 6;
468  this->height = this->ascender - this->descender;
469  } else {
470  /* Both FT_Set_Pixel_Sizes and FT_Select_Size failed. */
471  Debug(freetype, 0, "Font size selection failed. Using FontCache defaults.");
472  }
473 }
474 
482 static void LoadFreeTypeFont(FontSize fs)
483 {
484  FreeTypeSubSetting *settings = nullptr;
485  switch (fs) {
486  default: NOT_REACHED();
487  case FS_SMALL: settings = &_freetype.small; break;
488  case FS_NORMAL: settings = &_freetype.medium; break;
489  case FS_LARGE: settings = &_freetype.large; break;
490  case FS_MONO: settings = &_freetype.mono; break;
491  }
492 
493  if (settings->font.empty()) return;
494 
495  if (_library == nullptr) {
496  if (FT_Init_FreeType(&_library) != FT_Err_Ok) {
497  ShowInfoF("Unable to initialize FreeType, using sprite fonts instead");
498  return;
499  }
500 
501  Debug(freetype, 2, "Initialized");
502  }
503 
504  const char *font_name = settings->font.c_str();
505  FT_Face face = nullptr;
506 
507  /* If font is an absolute path to a ttf, try loading that first. */
508  FT_Error error = FT_New_Face(_library, font_name, 0, &face);
509 
510 #if defined(WITH_COCOA)
511  extern void MacOSRegisterExternalFont(const char *file_path);
512  if (error == FT_Err_Ok) MacOSRegisterExternalFont(font_name);
513 #endif
514 
515  if (error != FT_Err_Ok) {
516  /* Check if font is a relative filename in one of our search-paths. */
517  std::string full_font = FioFindFullPath(BASE_DIR, font_name);
518  if (!full_font.empty()) {
519  error = FT_New_Face(_library, full_font.c_str(), 0, &face);
520 #if defined(WITH_COCOA)
521  if (error == FT_Err_Ok) MacOSRegisterExternalFont(full_font.c_str());
522 #endif
523  }
524  }
525 
526  /* Try loading based on font face name (OS-wide fonts). */
527  if (error != FT_Err_Ok) error = GetFontByFaceName(font_name, &face);
528 
529  if (error == FT_Err_Ok) {
530  Debug(freetype, 2, "Requested '{}', using '{} {}'", font_name, face->family_name, face->style_name);
531 
532  /* Attempt to select the unicode character map */
533  error = FT_Select_Charmap(face, ft_encoding_unicode);
534  if (error == FT_Err_Ok) goto found_face; // Success
535 
536  if (error == FT_Err_Invalid_CharMap_Handle) {
537  /* Try to pick a different character map instead. We default to
538  * the first map, but platform_id 0 encoding_id 0 should also
539  * be unicode (strange system...) */
540  FT_CharMap found = face->charmaps[0];
541  int i;
542 
543  for (i = 0; i < face->num_charmaps; i++) {
544  FT_CharMap charmap = face->charmaps[i];
545  if (charmap->platform_id == 0 && charmap->encoding_id == 0) {
546  found = charmap;
547  }
548  }
549 
550  if (found != nullptr) {
551  error = FT_Set_Charmap(face, found);
552  if (error == FT_Err_Ok) goto found_face;
553  }
554  }
555  }
556 
557  FT_Done_Face(face);
558 
559  static const char *SIZE_TO_NAME[] = { "medium", "small", "large", "mono" };
560  ShowInfoF("Unable to use '%s' for %s font, FreeType reported error 0x%X, using sprite font instead", font_name, SIZE_TO_NAME[fs], error);
561  return;
562 
563 found_face:
564  new FreeTypeFontCache(fs, face, settings->size);
565 }
566 
567 
572 {
573  FT_Done_Face(this->face);
574  this->face = nullptr;
575  this->ClearFontCache();
576 }
577 
582 {
583  /* Font scaling might have changed, determine font size anew if it was automatically selected. */
584  if (this->face != nullptr) this->SetFontSize(this->fs, this->face, this->req_size);
585 
587 }
588 
589 
590 const Sprite *FreeTypeFontCache::InternalGetGlyph(GlyphID key, bool aa)
591 {
592  FT_GlyphSlot slot = this->face->glyph;
593 
594  FT_Load_Glyph(this->face, key, aa ? FT_LOAD_TARGET_NORMAL : FT_LOAD_TARGET_MONO);
595  FT_Render_Glyph(this->face->glyph, aa ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO);
596 
597  /* Despite requesting a normal glyph, FreeType may have returned a bitmap */
598  aa = (slot->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY);
599 
600  /* Add 1 pixel for the shadow on the medium font. Our sprite must be at least 1x1 pixel */
601  uint width = std::max(1U, (uint)slot->bitmap.width + (this->fs == FS_NORMAL));
602  uint height = std::max(1U, (uint)slot->bitmap.rows + (this->fs == FS_NORMAL));
603 
604  /* Limit glyph size to prevent overflows later on. */
605  if (width > MAX_GLYPH_DIM || height > MAX_GLYPH_DIM) usererror("Font glyph is too large");
606 
607  /* FreeType has rendered the glyph, now we allocate a sprite and copy the image into it */
608  SpriteLoader::Sprite sprite;
609  sprite.AllocateData(ZOOM_LVL_NORMAL, width * height);
610  sprite.type = ST_FONT;
611  sprite.colours = (aa ? SCC_PAL | SCC_ALPHA : SCC_PAL);
612  sprite.width = width;
613  sprite.height = height;
614  sprite.x_offs = slot->bitmap_left;
615  sprite.y_offs = this->ascender - slot->bitmap_top;
616 
617  /* Draw shadow for medium size */
618  if (this->fs == FS_NORMAL && !aa) {
619  for (uint y = 0; y < (uint)slot->bitmap.rows; y++) {
620  for (uint x = 0; x < (uint)slot->bitmap.width; x++) {
621  if (HasBit(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
622  sprite.data[1 + x + (1 + y) * sprite.width].m = SHADOW_COLOUR;
623  sprite.data[1 + x + (1 + y) * sprite.width].a = 0xFF;
624  }
625  }
626  }
627  }
628 
629  for (uint y = 0; y < (uint)slot->bitmap.rows; y++) {
630  for (uint x = 0; x < (uint)slot->bitmap.width; x++) {
631  if (aa ? (slot->bitmap.buffer[x + y * slot->bitmap.pitch] > 0) : HasBit(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
632  sprite.data[x + y * sprite.width].m = FACE_COLOUR;
633  sprite.data[x + y * sprite.width].a = aa ? slot->bitmap.buffer[x + y * slot->bitmap.pitch] : 0xFF;
634  }
635  }
636  }
637 
638  GlyphEntry new_glyph;
639  new_glyph.sprite = BlitterFactory::GetCurrentBlitter()->Encode(&sprite, SimpleSpriteAlloc);
640  new_glyph.width = slot->advance.x >> 6;
641 
642  this->SetGlyphPtr(key, &new_glyph);
643 
644  return new_glyph.sprite;
645 }
646 
647 
649 {
650  assert(IsPrintable(key));
651 
652  if (key >= SCC_SPRITE_START && key <= SCC_SPRITE_END) {
653  return this->parent->MapCharToGlyph(key);
654  }
655 
656  return FT_Get_Char_Index(this->face, key);
657 }
658 
659 const void *FreeTypeFontCache::InternalGetFontTable(uint32 tag, size_t &length)
660 {
661  FT_ULong len = 0;
662  FT_Byte *result = nullptr;
663 
664  FT_Load_Sfnt_Table(this->face, tag, 0, nullptr, &len);
665 
666  if (len > 0) {
667  result = MallocT<FT_Byte>(len);
668  FT_Load_Sfnt_Table(this->face, tag, 0, result, &len);
669  }
670 
671  length = len;
672  return result;
673 }
674 #endif /* WITH_FREETYPE */
675 
676 
681 void InitFreeType(bool monospace)
682 {
683  for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
684  if (monospace != (fs == FS_MONO)) continue;
685 
686  FontCache *fc = FontCache::Get(fs);
687  if (fc->HasParent()) delete fc;
688 
689 #ifdef WITH_FREETYPE
690  LoadFreeTypeFont(fs);
691 #elif defined(_WIN32)
692  extern void LoadWin32Font(FontSize fs);
693  LoadWin32Font(fs);
694 #elif defined(WITH_COCOA)
695  extern void LoadCoreTextFont(FontSize fs);
696  LoadCoreTextFont(fs);
697 #endif
698  }
699 }
700 
705 {
706  for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
707  FontCache *fc = FontCache::Get(fs);
708  if (fc->HasParent()) delete fc;
709  }
710 
711 #ifdef WITH_FREETYPE
712  FT_Done_FreeType(_library);
713  _library = nullptr;
714 #endif /* WITH_FREETYPE */
715 }
716 
722 {
723  for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
724  if (!FontCache::Get(fs)->IsBuiltInFont() && GetFontAAState(fs, false)) return true;
725  }
726 
727  return false;
728 }
729 
730 #if !defined(_WIN32) && !defined(__APPLE__) && !defined(WITH_FONTCONFIG) && !defined(WITH_COCOA)
731 
732 #ifdef WITH_FREETYPE
733 FT_Error GetFontByFaceName(const char *font_name, FT_Face *face) { return FT_Err_Cannot_Open_Resource; }
734 #endif /* WITH_FREETYPE */
735 
736 bool SetFallbackFont(FreeTypeSettings *settings, const char *language_isocode, int winlangid, MissingGlyphSearcher *callback) { return false; }
737 #endif /* !defined(_WIN32) && !defined(__APPLE__) && !defined(WITH_FONTCONFIG) && !defined(WITH_COCOA) */
SpriteLoader::CommonPixel::m
uint8 m
Remap-channel.
Definition: spriteloader.hpp:39
SCC_PAL
@ SCC_PAL
Sprite has palette data.
Definition: spriteloader.hpp:25
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:369
TrueTypeFontCache::~TrueTypeFontCache
virtual ~TrueTypeFontCache()
Free everything that was allocated for this font cache.
Definition: fontcache.cpp:217
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:103
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:242
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:190
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
FontCache::GetHeight
int GetHeight() const
Get the height of the font.
Definition: fontcache.h:48
SpriteLoader::Sprite::AllocateData
void AllocateData(ZoomLevel zoom, size_t size)
Allocate the sprite data of this sprite.
Definition: spriteloader.hpp:62
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:391
FreeTypeSettings::mono
FreeTypeSubSetting mono
The mono space font used for license/readme viewers.
Definition: fontcache.h:229
FreeTypeFontCache::~FreeTypeFontCache
~FreeTypeFontCache()
Free everything that was allocated for this font cache.
Definition: fontcache.cpp:571
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:581
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:298
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:91
TrueTypeFontCache::GetDrawGlyphShadow
bool GetDrawGlyphShadow() override
Do we need to draw a glyph shadow?
Definition: fontcache.cpp:293
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:482
SpriteLoader::Sprite::data
SpriteLoader::CommonPixel * data
The sprite itself.
Definition: spriteloader.hpp:55
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:393
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:578
SpriteLoader::Sprite::type
SpriteType type
The sprite type.
Definition: spriteloader.hpp:53
FreeTypeFontCache::GetFontName
virtual const char * GetFontName()
Get the name of this font.
Definition: fontcache.cpp:404
FreeTypeFontCache::IsBuiltInFont
virtual bool IsBuiltInFont()
Is this a built-in sprite font?
Definition: fontcache.cpp:405
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:721
SpriteLoader::CommonPixel
Definition of a common pixel in OpenTTD's realm.
Definition: spriteloader.hpp:34
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:141
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:704
SpriteLoader::Sprite::x_offs
int16 x_offs
The x-offset of where the sprite will be drawn.
Definition: spriteloader.hpp:51
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:92
fontdetection.h
SpriteFontCache::GetFontName
virtual const char * GetFontName()
Get the name of this font.
Definition: fontcache.cpp:93
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:197
SpriteLoader::Sprite::colours
SpriteColourComponent colours
The colour components of the sprite with useful information.
Definition: spriteloader.hpp:54
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:50
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:417
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:24
ShowInfoF
void CDECL ShowInfoF(const char *str,...)
Shows some information on the console/a popup box depending on the OS.
Definition: openttd.cpp:154
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:230
InitFreeType
void InitFreeType(bool monospace)
(Re)initialize the freetype related things, i.e.
Definition: fontcache.cpp:681
SpriteLoader::Sprite
Structure for passing information from the sprite loader to the blitter.
Definition: spriteloader.hpp:48
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:132
TrueTypeFontCache::TrueTypeFontCache
TrueTypeFontCache(FontSize fs, int pixels)
Create a new TrueTypeFontCache.
Definition: fontcache.cpp:210
SmallMap::Find
std::vector< Pair >::const_iterator Find(const T &key) const
Finds given key in this map.
Definition: smallmap_type.hpp:41
Debug
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
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:378
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:38
fontcache.h
FreeTypeFontCache::MapCharToGlyph
virtual GlyphID MapCharToGlyph(WChar key)
Map a character into a glyph.
Definition: fontcache.cpp:648
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:94
SpriteLoader::Sprite::y_offs
int16 y_offs
The y-offset of where the sprite will be drawn.
Definition: spriteloader.hpp:52
SpriteFontCache::GetGlyph
virtual const Sprite * GetGlyph(GlyphID key)
Get the glyph (sprite) of the given key.
Definition: fontcache.cpp:183
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:49
TrueTypeFontCache::GetGlyph
const Sprite * GetGlyph(GlyphID key) override
Get the glyph (sprite) of the given key.
Definition: fontcache.cpp:311
FioFindFullPath
std::string FioFindFullPath(Subdirectory subdir, const char *filename)
Find a path to the filename in one of the search directories.
Definition: fileio.cpp:141
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:460
SimpleSpriteAlloc
void * SimpleSpriteAlloc(size_t size)
Sprite allocator simply using malloc.
Definition: spritecache.cpp:846
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:101
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