OpenTTD
fontcache.cpp
Go to the documentation of this file.
1 /* $Id$ */
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(this->parent == NULL || this->fs == this->parent->fs);
45  FontCache::caches[this->fs] = this;
46  Layouter::ResetFontCache(this->fs);
47 }
48 
51 {
52  assert(this->fs == this->parent->fs);
53  FontCache::caches[this->fs] = this->parent;
55 }
56 
57 
64 {
65  return FontCache::Get(size)->GetHeight();
66 }
67 
68 
70 class SpriteFontCache : public FontCache {
71 private:
73 
74  void ClearGlyphToSpriteMap();
75 public:
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  virtual bool IsBuiltInFont() { return true; }
90 };
91 
96 SpriteFontCache::SpriteFontCache(FontSize fs) : FontCache(fs), glyph_to_spriteid_map(NULL)
97 {
99 }
100 
105 {
106  this->ClearGlyphToSpriteMap();
107 }
108 
110 {
111  if (this->glyph_to_spriteid_map[GB(key, 8, 8)] == NULL) return 0;
112  return this->glyph_to_spriteid_map[GB(key, 8, 8)][GB(key, 0, 8)];
113 }
114 
116 {
117  if (this->glyph_to_spriteid_map == NULL) this->glyph_to_spriteid_map = CallocT<SpriteID*>(256);
118  if (this->glyph_to_spriteid_map[GB(key, 8, 8)] == NULL) this->glyph_to_spriteid_map[GB(key, 8, 8)] = CallocT<SpriteID>(256);
119  this->glyph_to_spriteid_map[GB(key, 8, 8)][GB(key, 0, 8)] = sprite;
120 }
121 
123 {
124  /* Clear out existing glyph map if it exists */
125  this->ClearGlyphToSpriteMap();
126 
127  SpriteID base;
128  switch (this->fs) {
129  default: NOT_REACHED();
130  case FS_MONO: // Use normal as default for mono spaced font
131  case FS_NORMAL: base = SPR_ASCII_SPACE; break;
132  case FS_SMALL: base = SPR_ASCII_SPACE_SMALL; break;
133  case FS_LARGE: base = SPR_ASCII_SPACE_BIG; break;
134  }
135 
136  for (uint i = ASCII_LETTERSTART; i < 256; i++) {
137  SpriteID sprite = base + i - ASCII_LETTERSTART;
138  if (!SpriteExists(sprite)) continue;
139  this->SetUnicodeGlyph(i, sprite);
140  this->SetUnicodeGlyph(i + SCC_SPRITE_START, sprite);
141  }
142 
143  for (uint i = 0; i < lengthof(_default_unicode_map); i++) {
144  byte key = _default_unicode_map[i].key;
145  if (key == CLRA) {
146  /* Clear the glyph. This happens if the glyph at this code point
147  * is non-standard and should be accessed by an SCC_xxx enum
148  * entry only. */
149  this->SetUnicodeGlyph(_default_unicode_map[i].code, 0);
150  } else {
151  SpriteID sprite = base + key - ASCII_LETTERSTART;
152  this->SetUnicodeGlyph(_default_unicode_map[i].code, sprite);
153  }
154  }
155 }
156 
161 {
162  if (this->glyph_to_spriteid_map == NULL) return;
163 
164  for (uint i = 0; i < 256; i++) {
165  free(this->glyph_to_spriteid_map[i]);
166  }
168  this->glyph_to_spriteid_map = NULL;
169 }
170 
172 {
174 }
175 
177 {
178  SpriteID sprite = this->GetUnicodeGlyph(key);
179  if (sprite == 0) sprite = this->GetUnicodeGlyph('?');
180  return GetSprite(sprite, ST_FONT);
181 }
182 
184 {
185  SpriteID sprite = this->GetUnicodeGlyph(key);
186  if (sprite == 0) sprite = this->GetUnicodeGlyph('?');
187  return SpriteExists(sprite) ? GetSprite(sprite, ST_FONT)->width + ScaleFontTrad(this->fs != FS_NORMAL ? 1 : 0) : 0;
188 }
189 
191 {
192  return ScaleFontTrad(this->height);
193 }
194 
196 {
197  return false;
198 }
199 
201 
202 #ifdef WITH_FREETYPE
203 #include <ft2build.h>
204 #include FT_FREETYPE_H
205 #include FT_GLYPH_H
206 #include FT_TRUETYPE_TABLES_H
207 
209 class FreeTypeFontCache : public FontCache {
210 private:
211  FT_Face face;
212  int req_size;
213  int used_size;
214 
216  FontTable font_tables;
217 
219  struct GlyphEntry {
221  byte width;
222  bool duplicate;
223  };
224 
239 
240  GlyphEntry *GetGlyphPtr(GlyphID key);
241  void SetGlyphPtr(GlyphID key, const GlyphEntry *glyph, bool duplicate = false);
242  void SetFontSize(FontSize fs, FT_Face face, int pixels);
243 
244 public:
245  FreeTypeFontCache(FontSize fs, FT_Face face, int pixels);
247  virtual int GetFontSize() const { return this->used_size; }
248  virtual SpriteID GetUnicodeGlyph(WChar key) { return this->parent->GetUnicodeGlyph(key); }
249  virtual void SetUnicodeGlyph(WChar key, SpriteID sprite) { this->parent->SetUnicodeGlyph(key, sprite); }
251  virtual void ClearFontCache();
252  virtual const Sprite *GetGlyph(GlyphID key);
253  virtual uint GetGlyphWidth(GlyphID key);
254  virtual bool GetDrawGlyphShadow();
255  virtual GlyphID MapCharToGlyph(WChar key);
256  virtual const void *GetFontTable(uint32 tag, size_t &length);
257  virtual const char *GetFontName() { return face->family_name; }
258  virtual bool IsBuiltInFont() { return false; }
259 };
260 
261 FT_Library _library = NULL;
262 
263 FreeTypeSettings _freetype;
264 
265 static const byte FACE_COLOUR = 1;
266 static const byte SHADOW_COLOUR = 2;
267 
274 FreeTypeFontCache::FreeTypeFontCache(FontSize fs, FT_Face face, int pixels) : FontCache(fs), face(face), req_size(pixels), glyph_to_sprite(NULL)
275 {
276  assert(face != NULL);
277 
278  this->SetFontSize(fs, face, pixels);
279 }
280 
281 void FreeTypeFontCache::SetFontSize(FontSize fs, FT_Face face, int pixels)
282 {
283  if (pixels == 0) {
284  /* Try to determine a good height based on the minimal height recommended by the font. */
285  int scaled_height = ScaleFontTrad(_default_font_height[this->fs]);
286  pixels = scaled_height;
287 
288  TT_Header *head = (TT_Header *)FT_Get_Sfnt_Table(this->face, ft_sfnt_head);
289  if (head != NULL) {
290  /* Font height is minimum height plus the difference between the default
291  * height for this font size and the small size. */
292  int diff = scaled_height - ScaleFontTrad(_default_font_height[FS_SMALL]);
293  pixels = Clamp(min(head->Lowest_Rec_PPEM, 20) + diff, scaled_height, MAX_FONT_SIZE);
294  }
295  } else {
296  pixels = ScaleFontTrad(pixels);
297  }
298  this->used_size = pixels;
299 
300  FT_Error err = FT_Set_Pixel_Sizes(this->face, 0, pixels);
301  if (err != FT_Err_Ok) {
302 
303  /* Find nearest size to that requested */
304  FT_Bitmap_Size *bs = this->face->available_sizes;
305  int i = this->face->num_fixed_sizes;
306  if (i > 0) { // In pathetic cases one might get no fixed sizes at all.
307  int n = bs->height;
308  FT_Int chosen = 0;
309  for (; --i; bs++) {
310  if (abs(pixels - bs->height) >= abs(pixels - n)) continue;
311  n = bs->height;
312  chosen = this->face->num_fixed_sizes - i;
313  }
314 
315  /* Don't use FT_Set_Pixel_Sizes here - it might give us another
316  * error, even though the size is available (FS#5885). */
317  err = FT_Select_Size(this->face, chosen);
318  }
319  }
320 
321  if (err == FT_Err_Ok) {
322  this->units_per_em = this->face->units_per_EM;
323  this->ascender = this->face->size->metrics.ascender >> 6;
324  this->descender = this->face->size->metrics.descender >> 6;
325  this->height = this->ascender - this->descender;
326  } else {
327  /* Both FT_Set_Pixel_Sizes and FT_Select_Size failed. */
328  DEBUG(freetype, 0, "Font size selection failed. Using FontCache defaults.");
329  }
330 }
331 
340 {
342  switch (fs) {
343  default: NOT_REACHED();
344  case FS_SMALL: settings = &_freetype.small; break;
345  case FS_NORMAL: settings = &_freetype.medium; break;
346  case FS_LARGE: settings = &_freetype.large; break;
347  case FS_MONO: settings = &_freetype.mono; break;
348  }
349 
350  if (StrEmpty(settings->font)) return;
351 
352  if (_library == NULL) {
353  if (FT_Init_FreeType(&_library) != FT_Err_Ok) {
354  ShowInfoF("Unable to initialize FreeType, using sprite fonts instead");
355  return;
356  }
357 
358  DEBUG(freetype, 2, "Initialized");
359  }
360 
361  FT_Face face = NULL;
362  FT_Error error = FT_New_Face(_library, settings->font, 0, &face);
363 
364  if (error != FT_Err_Ok) error = GetFontByFaceName(settings->font, &face);
365 
366  if (error == FT_Err_Ok) {
367  DEBUG(freetype, 2, "Requested '%s', using '%s %s'", settings->font, face->family_name, face->style_name);
368 
369  /* Attempt to select the unicode character map */
370  error = FT_Select_Charmap(face, ft_encoding_unicode);
371  if (error == FT_Err_Ok) goto found_face; // Success
372 
373  if (error == FT_Err_Invalid_CharMap_Handle) {
374  /* Try to pick a different character map instead. We default to
375  * the first map, but platform_id 0 encoding_id 0 should also
376  * be unicode (strange system...) */
377  FT_CharMap found = face->charmaps[0];
378  int i;
379 
380  for (i = 0; i < face->num_charmaps; i++) {
381  FT_CharMap charmap = face->charmaps[i];
382  if (charmap->platform_id == 0 && charmap->encoding_id == 0) {
383  found = charmap;
384  }
385  }
386 
387  if (found != NULL) {
388  error = FT_Set_Charmap(face, found);
389  if (error == FT_Err_Ok) goto found_face;
390  }
391  }
392  }
393 
394  FT_Done_Face(face);
395 
396  static const char *SIZE_TO_NAME[] = { "medium", "small", "large", "mono" };
397  ShowInfoF("Unable to use '%s' for %s font, FreeType reported error 0x%X, using sprite font instead", settings->font, SIZE_TO_NAME[fs], error);
398  return;
399 
400 found_face:
401  new FreeTypeFontCache(fs, face, settings->size);
402 }
403 
404 
409 {
410  FT_Done_Face(this->face);
411  this->face = NULL;
412  this->ClearFontCache();
413 
414  for (FontTable::iterator iter = this->font_tables.Begin(); iter != this->font_tables.End(); iter++) {
415  free(iter->second.second);
416  }
417 }
418 
423 {
424  /* Font scaling might have changed, determine font size anew if it was automatically selected. */
425  if (this->face != NULL) this->SetFontSize(this->fs, this->face, this->req_size);
426 
427  if (this->glyph_to_sprite == NULL) return;
428 
429  for (int i = 0; i < 256; i++) {
430  if (this->glyph_to_sprite[i] == NULL) continue;
431 
432  for (int j = 0; j < 256; j++) {
433  if (this->glyph_to_sprite[i][j].duplicate) continue;
434  free(this->glyph_to_sprite[i][j].sprite);
435  }
436 
437  free(this->glyph_to_sprite[i]);
438  }
439 
440  free(this->glyph_to_sprite);
441  this->glyph_to_sprite = NULL;
442 
444 }
445 
446 FreeTypeFontCache::GlyphEntry *FreeTypeFontCache::GetGlyphPtr(GlyphID key)
447 {
448  if (this->glyph_to_sprite == NULL) return NULL;
449  if (this->glyph_to_sprite[GB(key, 8, 8)] == NULL) return NULL;
450  return &this->glyph_to_sprite[GB(key, 8, 8)][GB(key, 0, 8)];
451 }
452 
453 
454 void FreeTypeFontCache::SetGlyphPtr(GlyphID key, const GlyphEntry *glyph, bool duplicate)
455 {
456  if (this->glyph_to_sprite == NULL) {
457  DEBUG(freetype, 3, "Allocating root glyph cache for size %u", this->fs);
458  this->glyph_to_sprite = CallocT<GlyphEntry*>(256);
459  }
460 
461  if (this->glyph_to_sprite[GB(key, 8, 8)] == NULL) {
462  DEBUG(freetype, 3, "Allocating glyph cache for range 0x%02X00, size %u", GB(key, 8, 8), this->fs);
463  this->glyph_to_sprite[GB(key, 8, 8)] = CallocT<GlyphEntry>(256);
464  }
465 
466  DEBUG(freetype, 4, "Set glyph for unicode character 0x%04X, size %u", key, this->fs);
467  this->glyph_to_sprite[GB(key, 8, 8)][GB(key, 0, 8)].sprite = glyph->sprite;
468  this->glyph_to_sprite[GB(key, 8, 8)][GB(key, 0, 8)].width = glyph->width;
469  this->glyph_to_sprite[GB(key, 8, 8)][GB(key, 0, 8)].duplicate = duplicate;
470 }
471 
472 static void *AllocateFont(size_t size)
473 {
474  return MallocT<byte>(size);
475 }
476 
477 
478 /* Check if a glyph should be rendered with antialiasing */
479 static bool GetFontAAState(FontSize size)
480 {
481  /* AA is only supported for 32 bpp */
482  if (BlitterFactory::GetCurrentBlitter()->GetScreenDepth() != 32) return false;
483 
484  switch (size) {
485  default: NOT_REACHED();
486  case FS_NORMAL: return _freetype.medium.aa;
487  case FS_SMALL: return _freetype.small.aa;
488  case FS_LARGE: return _freetype.large.aa;
489  case FS_MONO: return _freetype.mono.aa;
490  }
491 }
492 
493 
495 {
496  if ((key & SPRITE_GLYPH) != 0) return this->parent->GetGlyph(key);
497 
498  /* Check for the glyph in our cache */
499  GlyphEntry *glyph = this->GetGlyphPtr(key);
500  if (glyph != NULL && glyph->sprite != NULL) return glyph->sprite;
501 
502  FT_GlyphSlot slot = this->face->glyph;
503 
504  bool aa = GetFontAAState(this->fs);
505 
506  GlyphEntry new_glyph;
507  if (key == 0) {
508  GlyphID question_glyph = this->MapCharToGlyph('?');
509  if (question_glyph == 0) {
510  /* The font misses the '?' character. Use built-in sprite.
511  * Note: We cannot use the baseset as this also has to work in the bootstrap GUI. */
512 #define CPSET { 0, 0, 0, 0, 1 }
513 #define CP___ { 0, 0, 0, 0, 0 }
514  static SpriteLoader::CommonPixel builtin_questionmark_data[10 * 8] = {
515  CP___, CP___, CPSET, CPSET, CPSET, CPSET, CP___, CP___,
516  CP___, CPSET, CPSET, CP___, CP___, CPSET, CPSET, CP___,
517  CP___, CP___, CP___, CP___, CP___, CPSET, CPSET, CP___,
518  CP___, CP___, CP___, CP___, CPSET, CPSET, CP___, CP___,
519  CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
520  CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
521  CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
522  CP___, CP___, CP___, CP___, CP___, CP___, CP___, CP___,
523  CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
524  CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
525  };
526 #undef CPSET
527 #undef CP___
528  static const SpriteLoader::Sprite builtin_questionmark = {
529  10, // height
530  8, // width
531  0, // x_offs
532  0, // y_offs
533  ST_FONT,
534  builtin_questionmark_data
535  };
536 
537  Sprite *spr = BlitterFactory::GetCurrentBlitter()->Encode(&builtin_questionmark, AllocateFont);
538  assert(spr != NULL);
539  new_glyph.sprite = spr;
540  new_glyph.width = spr->width + (this->fs != FS_NORMAL);
541  this->SetGlyphPtr(key, &new_glyph, false);
542  return new_glyph.sprite;
543  } else {
544  /* Use '?' for missing characters. */
545  this->GetGlyph(question_glyph);
546  glyph = this->GetGlyphPtr(question_glyph);
547  this->SetGlyphPtr(key, glyph, true);
548  return glyph->sprite;
549  }
550  }
551  FT_Load_Glyph(this->face, key, aa ? FT_LOAD_TARGET_NORMAL : FT_LOAD_TARGET_MONO);
552  FT_Render_Glyph(this->face->glyph, aa ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO);
553 
554  /* Despite requesting a normal glyph, FreeType may have returned a bitmap */
555  aa = (slot->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY);
556 
557  /* Add 1 pixel for the shadow on the medium font. Our sprite must be at least 1x1 pixel */
558  uint width = max(1U, (uint)slot->bitmap.width + (this->fs == FS_NORMAL));
559  uint height = max(1U, (uint)slot->bitmap.rows + (this->fs == FS_NORMAL));
560 
561  /* Limit glyph size to prevent overflows later on. */
562  if (width > 256 || height > 256) usererror("Font glyph is too large");
563 
564  /* FreeType has rendered the glyph, now we allocate a sprite and copy the image into it */
565  SpriteLoader::Sprite sprite;
566  sprite.AllocateData(ZOOM_LVL_NORMAL, width * height);
567  sprite.type = ST_FONT;
568  sprite.width = width;
569  sprite.height = height;
570  sprite.x_offs = slot->bitmap_left;
571  sprite.y_offs = this->ascender - slot->bitmap_top;
572 
573  /* Draw shadow for medium size */
574  if (this->fs == FS_NORMAL && !aa) {
575  for (uint y = 0; y < (uint)slot->bitmap.rows; y++) {
576  for (uint x = 0; x < (uint)slot->bitmap.width; x++) {
577  if (aa ? (slot->bitmap.buffer[x + y * slot->bitmap.pitch] > 0) : HasBit(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
578  sprite.data[1 + x + (1 + y) * sprite.width].m = SHADOW_COLOUR;
579  sprite.data[1 + x + (1 + y) * sprite.width].a = aa ? slot->bitmap.buffer[x + y * slot->bitmap.pitch] : 0xFF;
580  }
581  }
582  }
583  }
584 
585  for (uint y = 0; y < (uint)slot->bitmap.rows; y++) {
586  for (uint x = 0; x < (uint)slot->bitmap.width; x++) {
587  if (aa ? (slot->bitmap.buffer[x + y * slot->bitmap.pitch] > 0) : HasBit(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
588  sprite.data[x + y * sprite.width].m = FACE_COLOUR;
589  sprite.data[x + y * sprite.width].a = aa ? slot->bitmap.buffer[x + y * slot->bitmap.pitch] : 0xFF;
590  }
591  }
592  }
593 
594  new_glyph.sprite = BlitterFactory::GetCurrentBlitter()->Encode(&sprite, AllocateFont);
595  new_glyph.width = slot->advance.x >> 6;
596 
597  this->SetGlyphPtr(key, &new_glyph);
598 
599  return new_glyph.sprite;
600 }
601 
602 
604 {
605  return this->fs == FS_NORMAL && GetFontAAState(FS_NORMAL);
606 }
607 
608 
610 {
611  if ((key & SPRITE_GLYPH) != 0) return this->parent->GetGlyphWidth(key);
612 
613  GlyphEntry *glyph = this->GetGlyphPtr(key);
614  if (glyph == NULL || glyph->sprite == NULL) {
615  this->GetGlyph(key);
616  glyph = this->GetGlyphPtr(key);
617  }
618 
619  return glyph->width;
620 }
621 
623 {
624  assert(IsPrintable(key));
625 
626  if (key >= SCC_SPRITE_START && key <= SCC_SPRITE_END) {
627  return this->parent->MapCharToGlyph(key);
628  }
629 
630  return FT_Get_Char_Index(this->face, key);
631 }
632 
633 const void *FreeTypeFontCache::GetFontTable(uint32 tag, size_t &length)
634 {
635  const FontTable::iterator iter = this->font_tables.Find(tag);
636  if (iter != this->font_tables.End()) {
637  length = iter->second.first;
638  return iter->second.second;
639  }
640 
641  FT_ULong len = 0;
642  FT_Byte *result = NULL;
643 
644  FT_Load_Sfnt_Table(this->face, tag, 0, NULL, &len);
645 
646  if (len > 0) {
647  result = MallocT<FT_Byte>(len);
648  FT_Load_Sfnt_Table(this->face, tag, 0, result, &len);
649  }
650  length = len;
651 
652  this->font_tables.Insert(tag, SmallPair<size_t, const void *>(length, result));
653  return result;
654 }
655 
656 #endif /* WITH_FREETYPE */
657 
662 void InitFreeType(bool monospace)
663 {
664  for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
665  if (monospace != (fs == FS_MONO)) continue;
666 
667  FontCache *fc = FontCache::Get(fs);
668  if (fc->HasParent()) delete fc;
669 
670 #ifdef WITH_FREETYPE
672 #endif
673  }
674 }
675 
680 {
681  for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
682  FontCache *fc = FontCache::Get(fs);
683  if (fc->HasParent()) delete fc;
684  }
685 
686 #ifdef WITH_FREETYPE
687  FT_Done_FreeType(_library);
688  _library = NULL;
689 #endif /* WITH_FREETYPE */
690 }
Functions related to OTTD&#39;s strings.
Character mapping for using Unicode characters in OTTD.
virtual void InitializeUnicodeGlyphMap()
Initialize the glyph map.
Definition: fontcache.cpp:122
virtual uint GetGlyphWidth(GlyphID key)
Get the width of the glyph with the given key.
Definition: fontcache.cpp:609
uint8 a
Alpha-channel.
int height
The height of the font.
Definition: fontcache.h:29
Control codes that are embedded in the translation strings.
virtual SpriteID GetUnicodeGlyph(WChar key)
Get the SpriteID mapped to the given key.
Definition: fontcache.cpp:109
SpriteFontCache(FontSize fs)
Create a new sprite font cache.
Definition: fontcache.cpp:96
const Pair * Find(const T &key) const
Finds given key in this map.
virtual bool GetDrawGlyphShadow()
Do we need to draw a glyph shadow?
Definition: fontcache.cpp:603
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
Definition: fontcache.cpp:63
int descender
The descender value of the font.
Definition: fontcache.h:31
Settings for a single freetype font.
Definition: fontcache.h:208
~FreeTypeFontCache()
Free everything that was allocated for this font cache.
Definition: fontcache.cpp:408
Index of the monospaced font in the font tables.
Definition: gfx_type.h:207
Data structure describing a sprite.
Definition: spritecache.h:18
fluid_settings_t * settings
FluidSynth settings handle.
Definition: fluidsynth.cpp:22
Sprite * sprite
The loaded sprite.
Definition: fontcache.cpp:220
FontTable font_tables
Cached font tables.
Definition: fontcache.cpp:216
Implementation of simple mapping class.
SmallMap< uint32, SmallPair< size_t, const void * > > FontTable
Table with font table cache.
Definition: fontcache.cpp:215
Functions related to detecting/finding the right font.
const T * Begin() const
Get the pointer to the first item (const)
bool Insert(const T &key, const U &data)
Adds new item to this map.
void AllocateData(ZoomLevel zoom, size_t size)
Allocate the sprite data of this sprite.
virtual void ClearFontCache()
Clear the font cache.
Definition: fontcache.cpp:171
virtual bool IsBuiltInFont()
Is this a built-in sprite font?
Definition: fontcache.cpp:89
FreeTypeSubSetting large
The largest font; mostly used for newspapers.
Definition: fontcache.h:218
int units_per_em
The units per EM value of the font.
Definition: fontcache.h:32
virtual void SetUnicodeGlyph(WChar key, SpriteID sprite)=0
Map a SpriteID to the key.
static int ScaleFontTrad(int value)
Scale traditional pixel dimensions to Font zoom level.
Definition: zoom_func.h:102
virtual const char * GetFontName()
Get the name of this font.
Definition: fontcache.cpp:257
virtual Sprite * Encode(const SpriteLoader::Sprite *sprite, AllocatorProc *allocator)=0
Convert a sprite from the loader to our own format.
virtual GlyphID MapCharToGlyph(WChar key)
Map a character into a glyph.
Definition: fontcache.cpp:86
byte width
The width of the glyph.
Definition: fontcache.cpp:221
static T max(const T a, const T b)
Returns the maximum of two values.
Definition: math_func.hpp:26
Functions related to laying out the texts.
static FontCache * Get(FontSize fs)
Get the font cache of a given font size.
Definition: fontcache.h:139
const T * End() const
Get the pointer behind the last valid item (const)
virtual int GetFontSize() const
Get the nominal font size of the font.
Definition: fontcache.cpp:247
int used_size
Used font size.
Definition: fontcache.cpp:213
Settings for the freetype fonts.
Definition: fontcache.h:215
bool HasParent()
Check whether the font cache has a parent.
Definition: fontcache.h:148
Definition of a common pixel in OpenTTD&#39;s realm.
SpriteType type
The sprite type.
Font cache for fonts that are based on a freetype font.
Definition: fontcache.cpp:70
Types related to zooming in and out.
Simple mapping class targeted for small sets of data.
void InitFreeType(bool monospace)
(Re)initialize the freetype related things, i.e.
Definition: fontcache.cpp:662
void CDECL ShowInfoF(const char *str,...)
Shows some information on the console/a popup box depending on the OS.
Definition: openttd.cpp:134
Functions to read fonts from files and cache them.
virtual void ClearFontCache()
Reset cached glyphs.
Definition: fontcache.cpp:422
bool aa
Whether to do anti aliasing or not.
Definition: fontcache.h:211
Simple pair of data.
First font.
Definition: gfx_type.h:210
virtual ~FontCache()
Clean everything up.
Definition: fontcache.cpp:50
FreeTypeSubSetting mono
The mono space font used for license/readme viewers.
Definition: fontcache.h:219
FontCache(FontSize fs)
Create a new font cache.
Definition: fontcache.cpp:40
FT_Face face
The font face associated with this font.
Definition: fontcache.cpp:211
virtual void InitializeUnicodeGlyphMap()
Initialize the glyph map.
Definition: fontcache.cpp:250
virtual uint GetGlyphWidth(GlyphID key)=0
Get the width of the glyph with the given key.
GlyphEntry ** glyph_to_sprite
The glyph cache.
Definition: fontcache.cpp:238
Definition of base types and functions in a cross-platform compatible way.
virtual bool IsBuiltInFont()
Is this a built-in sprite font?
Definition: fontcache.cpp:258
Font cache for fonts that are based on a freetype font.
Definition: fontcache.cpp:209
void CDECL usererror(const char *s,...)
Error handling for fatal user errors.
Definition: openttd.cpp:92
A number of safeguards to prevent using unsafe methods.
FT_Error GetFontByFaceName(const char *font_name, FT_Face *face)
Get the font loaded into a Freetype face by using a font-name.
virtual const void * GetFontTable(uint32 tag, size_t &length)
Read a font table from the font.
Definition: fontcache.cpp:87
int16 x_offs
The x-offset of where the sprite will be drawn.
virtual int GetHeight() const
Get the height of the font.
Definition: fontcache.h:47
virtual const Sprite * GetGlyph(GlyphID key)
Get the glyph (sprite) of the given key.
Definition: fontcache.cpp:176
FreeTypeFontCache(FontSize fs, FT_Face face, int pixels)
Create a new FreeTypeFontCache.
Definition: fontcache.cpp:274
~SpriteFontCache()
Free everything we allocated.
Definition: fontcache.cpp:104
SpriteLoader::CommonPixel * data
The sprite itself.
virtual bool GetDrawGlyphShadow()
Do we need to draw a glyph shadow?
Definition: fontcache.cpp:195
Structure for passing information from the sprite loader to the blitter.
FreeTypeSubSetting medium
The normal font size.
Definition: fontcache.h:217
static const int MAX_FONT_SIZE
Maximum font size.
Definition: fontcache.cpp:30
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:42
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition: factory.hpp:147
static T min(const T a, const T b)
Returns the minimum of two values.
Definition: math_func.hpp:42
virtual GlyphID MapCharToGlyph(WChar key)=0
Map a character into a glyph.
static void ResetFontCache(FontSize size)
Reset cached font information.
Definition: gfx_layout.cpp:845
Font cache for basic fonts.
Definition: fontcache.h:23
Integer math functions.
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:139
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:36
FontCache * parent
The parent of this font cache.
Definition: fontcache.h:27
uint size
The (requested) size of the font.
Definition: fontcache.h:210
uint8 m
Remap-channel.
uint16 width
Width of the sprite.
char font[MAX_PATH]
The name of the font, or path to the font.
Definition: fontcache.h:209
uint16 width
Width of the sprite.
Definition: spritecache.h:20
void ClearGlyphToSpriteMap()
Clear the glyph to sprite mapping.
Definition: fontcache.cpp:160
SpriteID ** glyph_to_spriteid_map
Mapping of glyphs to sprite IDs.
Definition: fontcache.cpp:72
uint32 SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:19
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:59
static FontCache * caches[FS_END]
All the font caches.
Definition: fontcache.h:25
virtual uint GetGlyphWidth(GlyphID key)
Get the width of the glyph with the given key.
Definition: fontcache.cpp:183
virtual void SetUnicodeGlyph(WChar key, SpriteID sprite)
Map a SpriteID to the key.
Definition: fontcache.cpp:115
virtual const char * GetFontName()
Get the name of this font.
Definition: fontcache.cpp:88
static const byte CLRA
Identifier to clear all glyphs at this codepoint.
Definition: unicode.h:17
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:112
static T abs(const T a)
Returns the absolute value of (scalar) variable.
Definition: math_func.hpp:83
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
FreeTypeSubSetting small
The smallest font; mostly used for zoomed out view.
Definition: fontcache.h:216
virtual const Sprite * GetGlyph(GlyphID key)=0
Get the glyph (sprite) of the given key.
Functions related to zooming.
FontSize
Available font sizes.
Definition: gfx_type.h:203
uint16 height
Height of the sprite.
static void LoadFreeTypeFont(FontSize fs)
Loads the freetype font.
Definition: fontcache.cpp:339
Index of the normal font in the font tables.
Definition: gfx_type.h:204
virtual const Sprite * GetGlyph(GlyphID key)
Get the glyph (sprite) of the given key.
Definition: fontcache.cpp:494
virtual SpriteID GetUnicodeGlyph(WChar key)
Get the SpriteID mapped to the given key.
Definition: fontcache.cpp:248
virtual bool GetDrawGlyphShadow()=0
Do we need to draw a glyph shadow?
virtual int GetHeight() const
Get the height of the font.
Definition: fontcache.cpp:190
static const int _default_font_height[FS_END]
Default heights for the different sizes of fonts.
Definition: fontcache.cpp:33
void UninitFreeType()
Free everything allocated w.r.t.
Definition: fontcache.cpp:679
int16 y_offs
The y-offset of where the sprite will be drawn.
Index of the small font in the font tables.
Definition: gfx_type.h:205
virtual void InitializeUnicodeGlyphMap()=0
Initialize the glyph map.
int ascender
The ascender value of the font.
Definition: fontcache.h:30
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:114
Index of the large font in the font tables.
Definition: gfx_type.h:206
Container for information about a glyph.
Definition: fontcache.cpp:219
const FontSize fs
The size of the font.
Definition: fontcache.h:28
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
int req_size
Requested font size.
Definition: fontcache.cpp:212
The normal zoom level.
Definition: zoom_type.h:24
virtual void SetUnicodeGlyph(WChar key, SpriteID sprite)
Map a SpriteID to the key.
Definition: fontcache.cpp:249
virtual const void * GetFontTable(uint32 tag, size_t &length)
Read a font table from the font.
Definition: fontcache.cpp:633
virtual GlyphID MapCharToGlyph(WChar key)
Map a character into a glyph.
Definition: fontcache.cpp:622
uint32 GlyphID
Glyphs are characters from a font.
Definition: fontcache.h:19
virtual void ClearFontCache()=0
Clear the font cache.
uint32 WChar
Type for wide characters, i.e.
Definition: string_type.h:35
bool duplicate
Whether this glyph entry is a duplicate, i.e. may this be freed?
Definition: fontcache.cpp:222
This file contains all sprite-related enums and defines.
Factory to &#39;query&#39; all available blitters.
A sprite used for fonts.
Definition: gfx_type.h:300
virtual SpriteID GetUnicodeGlyph(WChar key)=0
Get the SpriteID mapped to the given key.
static const int ASCII_LETTERSTART
First printable ASCII letter.
Definition: fontcache.cpp:29