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 #include "fileio_func.h"
23 
24 #include "table/sprites.h"
25 #include "table/control_codes.h"
26 #include "table/unicode.h"
27 
28 #include "safeguards.h"
29 
30 static const int ASCII_LETTERSTART = 32;
31 static const int MAX_FONT_SIZE = 72;
32 
34 static const int _default_font_height[FS_END] = {10, 6, 18, 10};
35 static const int _default_font_ascender[FS_END] = { 8, 5, 15, 8};
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 
65 {
66  return FontCache::Get(size)->GetHeight();
67 }
68 
69 
71 class SpriteFontCache : public FontCache {
72 private:
74 
75  void ClearGlyphToSpriteMap();
76 public:
78  ~SpriteFontCache();
79  virtual SpriteID GetUnicodeGlyph(WChar key);
80  virtual void SetUnicodeGlyph(WChar key, SpriteID sprite);
81  virtual void InitializeUnicodeGlyphMap();
82  virtual void ClearFontCache();
83  virtual const Sprite *GetGlyph(GlyphID key);
84  virtual uint GetGlyphWidth(GlyphID key);
85  virtual int GetHeight() const;
86  virtual bool GetDrawGlyphShadow();
87  virtual GlyphID MapCharToGlyph(WChar key) { assert(IsPrintable(key)); return SPRITE_GLYPH | key; }
88  virtual const void *GetFontTable(uint32 tag, size_t &length) { length = 0; return nullptr; }
89  virtual const char *GetFontName() { return "sprite"; }
90  virtual bool IsBuiltInFont() { return true; }
91 };
92 
97 SpriteFontCache::SpriteFontCache(FontSize fs) : FontCache(fs), glyph_to_spriteid_map(nullptr)
98 {
100 }
101 
106 {
107  this->ClearGlyphToSpriteMap();
108 }
109 
111 {
112  if (this->glyph_to_spriteid_map[GB(key, 8, 8)] == nullptr) return 0;
113  return this->glyph_to_spriteid_map[GB(key, 8, 8)][GB(key, 0, 8)];
114 }
115 
117 {
118  if (this->glyph_to_spriteid_map == nullptr) this->glyph_to_spriteid_map = CallocT<SpriteID*>(256);
119  if (this->glyph_to_spriteid_map[GB(key, 8, 8)] == nullptr) this->glyph_to_spriteid_map[GB(key, 8, 8)] = CallocT<SpriteID>(256);
120  this->glyph_to_spriteid_map[GB(key, 8, 8)][GB(key, 0, 8)] = sprite;
121 }
122 
124 {
125  /* Clear out existing glyph map if it exists */
126  this->ClearGlyphToSpriteMap();
127 
128  SpriteID base;
129  switch (this->fs) {
130  default: NOT_REACHED();
131  case FS_MONO: // Use normal as default for mono spaced font
132  case FS_NORMAL: base = SPR_ASCII_SPACE; break;
133  case FS_SMALL: base = SPR_ASCII_SPACE_SMALL; break;
134  case FS_LARGE: base = SPR_ASCII_SPACE_BIG; break;
135  }
136 
137  for (uint i = ASCII_LETTERSTART; i < 256; i++) {
138  SpriteID sprite = base + i - ASCII_LETTERSTART;
139  if (!SpriteExists(sprite)) continue;
140  this->SetUnicodeGlyph(i, sprite);
141  this->SetUnicodeGlyph(i + SCC_SPRITE_START, sprite);
142  }
143 
144  for (uint i = 0; i < lengthof(_default_unicode_map); i++) {
145  byte key = _default_unicode_map[i].key;
146  if (key == CLRA) {
147  /* Clear the glyph. This happens if the glyph at this code point
148  * is non-standard and should be accessed by an SCC_xxx enum
149  * entry only. */
150  this->SetUnicodeGlyph(_default_unicode_map[i].code, 0);
151  } else {
152  SpriteID sprite = base + key - ASCII_LETTERSTART;
153  this->SetUnicodeGlyph(_default_unicode_map[i].code, sprite);
154  }
155  }
156 }
157 
162 {
163  if (this->glyph_to_spriteid_map == nullptr) return;
164 
165  for (uint i = 0; i < 256; i++) {
166  free(this->glyph_to_spriteid_map[i]);
167  }
169  this->glyph_to_spriteid_map = nullptr;
170 }
171 
173 {
175 }
176 
178 {
179  SpriteID sprite = this->GetUnicodeGlyph(key);
180  if (sprite == 0) sprite = this->GetUnicodeGlyph('?');
181  return GetSprite(sprite, ST_FONT);
182 }
183 
185 {
186  SpriteID sprite = this->GetUnicodeGlyph(key);
187  if (sprite == 0) sprite = this->GetUnicodeGlyph('?');
188  return SpriteExists(sprite) ? GetSprite(sprite, ST_FONT)->width + ScaleFontTrad(this->fs != FS_NORMAL ? 1 : 0) : 0;
189 }
190 
192 {
193  return ScaleFontTrad(this->height);
194 }
195 
197 {
198  return false;
199 }
200 
202 
203 #if defined(WITH_FREETYPE) || defined(_WIN32)
204 
205 FreeTypeSettings _freetype;
206 
207 static const byte FACE_COLOUR = 1;
208 static const byte SHADOW_COLOUR = 2;
209 
211 class TrueTypeFontCache : public FontCache {
212 protected:
213  int req_size;
214  int used_size;
215 
217  FontTable font_tables;
218 
220  struct GlyphEntry {
222  byte width;
223  bool duplicate;
224  };
225 
240 
241  GlyphEntry *GetGlyphPtr(GlyphID key);
242  void SetGlyphPtr(GlyphID key, const GlyphEntry *glyph, bool duplicate = false);
243 
244  virtual const void *InternalGetFontTable(uint32 tag, size_t &length) = 0;
245  virtual const Sprite *InternalGetGlyph(GlyphID key, bool aa) = 0;
246 
247 public:
248  TrueTypeFontCache(FontSize fs, int pixels);
249  virtual ~TrueTypeFontCache();
250  virtual int GetFontSize() const { return this->used_size; }
251  virtual SpriteID GetUnicodeGlyph(WChar key) { return this->parent->GetUnicodeGlyph(key); }
252  virtual void SetUnicodeGlyph(WChar key, SpriteID sprite) { this->parent->SetUnicodeGlyph(key, sprite); }
254  virtual const Sprite *GetGlyph(GlyphID key);
255  virtual const void *GetFontTable(uint32 tag, size_t &length);
256  virtual void ClearFontCache();
257  virtual uint GetGlyphWidth(GlyphID key);
258  virtual bool GetDrawGlyphShadow();
259  virtual bool IsBuiltInFont() { return false; }
260 };
261 
267 TrueTypeFontCache::TrueTypeFontCache(FontSize fs, int pixels) : FontCache(fs), req_size(pixels), glyph_to_sprite(nullptr)
268 {
269 }
270 
275 {
276  this->ClearFontCache();
277 
278  for (auto &iter : this->font_tables) {
279  free(iter.second.second);
280  }
281 }
282 
287 {
288  if (this->glyph_to_sprite == nullptr) return;
289 
290  for (int i = 0; i < 256; i++) {
291  if (this->glyph_to_sprite[i] == nullptr) continue;
292 
293  for (int j = 0; j < 256; j++) {
294  if (this->glyph_to_sprite[i][j].duplicate) continue;
295  free(this->glyph_to_sprite[i][j].sprite);
296  }
297 
298  free(this->glyph_to_sprite[i]);
299  }
300 
301  free(this->glyph_to_sprite);
302  this->glyph_to_sprite = nullptr;
303 
305 }
306 
307 
308 TrueTypeFontCache::GlyphEntry *TrueTypeFontCache::GetGlyphPtr(GlyphID key)
309 {
310  if (this->glyph_to_sprite == nullptr) return nullptr;
311  if (this->glyph_to_sprite[GB(key, 8, 8)] == nullptr) return nullptr;
312  return &this->glyph_to_sprite[GB(key, 8, 8)][GB(key, 0, 8)];
313 }
314 
315 void TrueTypeFontCache::SetGlyphPtr(GlyphID key, const GlyphEntry *glyph, bool duplicate)
316 {
317  if (this->glyph_to_sprite == nullptr) {
318  DEBUG(freetype, 3, "Allocating root glyph cache for size %u", this->fs);
319  this->glyph_to_sprite = CallocT<GlyphEntry*>(256);
320  }
321 
322  if (this->glyph_to_sprite[GB(key, 8, 8)] == nullptr) {
323  DEBUG(freetype, 3, "Allocating glyph cache for range 0x%02X00, size %u", GB(key, 8, 8), this->fs);
324  this->glyph_to_sprite[GB(key, 8, 8)] = CallocT<GlyphEntry>(256);
325  }
326 
327  DEBUG(freetype, 4, "Set glyph for unicode character 0x%04X, size %u", key, this->fs);
328  this->glyph_to_sprite[GB(key, 8, 8)][GB(key, 0, 8)].sprite = glyph->sprite;
329  this->glyph_to_sprite[GB(key, 8, 8)][GB(key, 0, 8)].width = glyph->width;
330  this->glyph_to_sprite[GB(key, 8, 8)][GB(key, 0, 8)].duplicate = duplicate;
331 }
332 
333 static void *AllocateFont(size_t size)
334 {
335  return MallocT<byte>(size);
336 }
337 
338 
339 /* Check if a glyph should be rendered with anti-aliasing. */
340 static bool GetFontAAState(FontSize size)
341 {
342  /* AA is only supported for 32 bpp */
343  if (BlitterFactory::GetCurrentBlitter()->GetScreenDepth() != 32) return false;
344 
345  switch (size) {
346  default: NOT_REACHED();
347  case FS_NORMAL: return _freetype.medium.aa;
348  case FS_SMALL: return _freetype.small.aa;
349  case FS_LARGE: return _freetype.large.aa;
350  case FS_MONO: return _freetype.mono.aa;
351  }
352 }
353 
355 {
356  return this->fs == FS_NORMAL && GetFontAAState(FS_NORMAL);
357 }
358 
360 {
361  if ((key & SPRITE_GLYPH) != 0) return this->parent->GetGlyphWidth(key);
362 
363  GlyphEntry *glyph = this->GetGlyphPtr(key);
364  if (glyph == nullptr || glyph->sprite == nullptr) {
365  this->GetGlyph(key);
366  glyph = this->GetGlyphPtr(key);
367  }
368 
369  return glyph->width;
370 }
371 
373 {
374  if ((key & SPRITE_GLYPH) != 0) return this->parent->GetGlyph(key);
375 
376  /* Check for the glyph in our cache */
377  GlyphEntry *glyph = this->GetGlyphPtr(key);
378  if (glyph != nullptr && glyph->sprite != nullptr) return glyph->sprite;
379 
380  if (key == 0) {
381  GlyphID question_glyph = this->MapCharToGlyph('?');
382  if (question_glyph == 0) {
383  /* The font misses the '?' character. Use built-in sprite.
384  * Note: We cannot use the baseset as this also has to work in the bootstrap GUI. */
385 #define CPSET { 0, 0, 0, 0, 1 }
386 #define CP___ { 0, 0, 0, 0, 0 }
387  static SpriteLoader::CommonPixel builtin_questionmark_data[10 * 8] = {
388  CP___, CP___, CPSET, CPSET, CPSET, CPSET, CP___, CP___,
389  CP___, CPSET, CPSET, CP___, CP___, CPSET, CPSET, CP___,
390  CP___, CP___, CP___, CP___, CP___, CPSET, CPSET, CP___,
391  CP___, CP___, CP___, CP___, CPSET, CPSET, CP___, CP___,
392  CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
393  CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
394  CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
395  CP___, CP___, CP___, CP___, CP___, CP___, CP___, CP___,
396  CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
397  CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
398  };
399 #undef CPSET
400 #undef CP___
401  static const SpriteLoader::Sprite builtin_questionmark = {
402  10, // height
403  8, // width
404  0, // x_offs
405  0, // y_offs
406  ST_FONT,
407  builtin_questionmark_data
408  };
409 
410  Sprite *spr = BlitterFactory::GetCurrentBlitter()->Encode(&builtin_questionmark, AllocateFont);
411  assert(spr != nullptr);
412  GlyphEntry new_glyph;
413  new_glyph.sprite = spr;
414  new_glyph.width = spr->width + (this->fs != FS_NORMAL);
415  this->SetGlyphPtr(key, &new_glyph, false);
416  return new_glyph.sprite;
417  } else {
418  /* Use '?' for missing characters. */
419  this->GetGlyph(question_glyph);
420  glyph = this->GetGlyphPtr(question_glyph);
421  this->SetGlyphPtr(key, glyph, true);
422  return glyph->sprite;
423  }
424  }
425 
426  return this->InternalGetGlyph(key, GetFontAAState(this->fs));
427 }
428 
429 const void *TrueTypeFontCache::GetFontTable(uint32 tag, size_t &length)
430 {
431  const FontTable::iterator iter = this->font_tables.Find(tag);
432  if (iter != this->font_tables.data() + this->font_tables.size()) {
433  length = iter->second.first;
434  return iter->second.second;
435  }
436 
437  const void *result = this->InternalGetFontTable(tag, length);
438 
439  this->font_tables.Insert(tag, SmallPair<size_t, const void *>(length, result));
440  return result;
441 }
442 
443 
444 #ifdef WITH_FREETYPE
445 #include <ft2build.h>
446 #include FT_FREETYPE_H
447 #include FT_GLYPH_H
448 #include FT_TRUETYPE_TABLES_H
449 
452 private:
453  FT_Face face;
454 
455  void SetFontSize(FontSize fs, FT_Face face, int pixels);
456  virtual const void *InternalGetFontTable(uint32 tag, size_t &length);
457  virtual const Sprite *InternalGetGlyph(GlyphID key, bool aa);
458 
459 public:
460  FreeTypeFontCache(FontSize fs, FT_Face face, int pixels);
462  virtual void ClearFontCache();
463  virtual GlyphID MapCharToGlyph(WChar key);
464  virtual const char *GetFontName() { return face->family_name; }
465  virtual bool IsBuiltInFont() { return false; }
466 };
467 
468 FT_Library _library = nullptr;
469 
470 
477 FreeTypeFontCache::FreeTypeFontCache(FontSize fs, FT_Face face, int pixels) : TrueTypeFontCache(fs, pixels), face(face)
478 {
479  assert(face != nullptr);
480 
481  this->SetFontSize(fs, face, pixels);
482 }
483 
484 void FreeTypeFontCache::SetFontSize(FontSize fs, FT_Face face, int pixels)
485 {
486  if (pixels == 0) {
487  /* Try to determine a good height based on the minimal height recommended by the font. */
488  int scaled_height = ScaleFontTrad(_default_font_height[this->fs]);
489  pixels = scaled_height;
490 
491  TT_Header *head = (TT_Header *)FT_Get_Sfnt_Table(this->face, ft_sfnt_head);
492  if (head != nullptr) {
493  /* Font height is minimum height plus the difference between the default
494  * height for this font size and the small size. */
495  int diff = scaled_height - ScaleFontTrad(_default_font_height[FS_SMALL]);
496  pixels = Clamp(min(head->Lowest_Rec_PPEM, 20) + diff, scaled_height, MAX_FONT_SIZE);
497  }
498  } else {
499  pixels = ScaleFontTrad(pixels);
500  }
501  this->used_size = pixels;
502 
503  FT_Error err = FT_Set_Pixel_Sizes(this->face, 0, pixels);
504  if (err != FT_Err_Ok) {
505 
506  /* Find nearest size to that requested */
507  FT_Bitmap_Size *bs = this->face->available_sizes;
508  int i = this->face->num_fixed_sizes;
509  if (i > 0) { // In pathetic cases one might get no fixed sizes at all.
510  int n = bs->height;
511  FT_Int chosen = 0;
512  for (; --i; bs++) {
513  if (abs(pixels - bs->height) >= abs(pixels - n)) continue;
514  n = bs->height;
515  chosen = this->face->num_fixed_sizes - i;
516  }
517 
518  /* Don't use FT_Set_Pixel_Sizes here - it might give us another
519  * error, even though the size is available (FS#5885). */
520  err = FT_Select_Size(this->face, chosen);
521  }
522  }
523 
524  if (err == FT_Err_Ok) {
525  this->units_per_em = this->face->units_per_EM;
526  this->ascender = this->face->size->metrics.ascender >> 6;
527  this->descender = this->face->size->metrics.descender >> 6;
528  this->height = this->ascender - this->descender;
529  } else {
530  /* Both FT_Set_Pixel_Sizes and FT_Select_Size failed. */
531  DEBUG(freetype, 0, "Font size selection failed. Using FontCache defaults.");
532  }
533 }
534 
543 {
544  FreeTypeSubSetting *settings = nullptr;
545  switch (fs) {
546  default: NOT_REACHED();
547  case FS_SMALL: settings = &_freetype.small; break;
548  case FS_NORMAL: settings = &_freetype.medium; break;
549  case FS_LARGE: settings = &_freetype.large; break;
550  case FS_MONO: settings = &_freetype.mono; break;
551  }
552 
553  if (StrEmpty(settings->font)) return;
554 
555  if (_library == nullptr) {
556  if (FT_Init_FreeType(&_library) != FT_Err_Ok) {
557  ShowInfoF("Unable to initialize FreeType, using sprite fonts instead");
558  return;
559  }
560 
561  DEBUG(freetype, 2, "Initialized");
562  }
563 
564  FT_Face face = nullptr;
565  FT_Error error = FT_New_Face(_library, settings->font, 0, &face);
566 
567  if (error != FT_Err_Ok) error = GetFontByFaceName(settings->font, &face);
568 
569  if (error == FT_Err_Ok) {
570  DEBUG(freetype, 2, "Requested '%s', using '%s %s'", settings->font, face->family_name, face->style_name);
571 
572  /* Attempt to select the unicode character map */
573  error = FT_Select_Charmap(face, ft_encoding_unicode);
574  if (error == FT_Err_Ok) goto found_face; // Success
575 
576  if (error == FT_Err_Invalid_CharMap_Handle) {
577  /* Try to pick a different character map instead. We default to
578  * the first map, but platform_id 0 encoding_id 0 should also
579  * be unicode (strange system...) */
580  FT_CharMap found = face->charmaps[0];
581  int i;
582 
583  for (i = 0; i < face->num_charmaps; i++) {
584  FT_CharMap charmap = face->charmaps[i];
585  if (charmap->platform_id == 0 && charmap->encoding_id == 0) {
586  found = charmap;
587  }
588  }
589 
590  if (found != nullptr) {
591  error = FT_Set_Charmap(face, found);
592  if (error == FT_Err_Ok) goto found_face;
593  }
594  }
595  }
596 
597  FT_Done_Face(face);
598 
599  static const char *SIZE_TO_NAME[] = { "medium", "small", "large", "mono" };
600  ShowInfoF("Unable to use '%s' for %s font, FreeType reported error 0x%X, using sprite font instead", settings->font, SIZE_TO_NAME[fs], error);
601  return;
602 
603 found_face:
604  new FreeTypeFontCache(fs, face, settings->size);
605 }
606 
607 
612 {
613  FT_Done_Face(this->face);
614  this->face = nullptr;
615  this->ClearFontCache();
616 }
617 
622 {
623  /* Font scaling might have changed, determine font size anew if it was automatically selected. */
624  if (this->face != nullptr) this->SetFontSize(this->fs, this->face, this->req_size);
625 
627 }
628 
629 
630 const Sprite *FreeTypeFontCache::InternalGetGlyph(GlyphID key, bool aa)
631 {
632  FT_GlyphSlot slot = this->face->glyph;
633 
634  FT_Load_Glyph(this->face, key, aa ? FT_LOAD_TARGET_NORMAL : FT_LOAD_TARGET_MONO);
635  FT_Render_Glyph(this->face->glyph, aa ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO);
636 
637  /* Despite requesting a normal glyph, FreeType may have returned a bitmap */
638  aa = (slot->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY);
639 
640  /* Add 1 pixel for the shadow on the medium font. Our sprite must be at least 1x1 pixel */
641  uint width = max(1U, (uint)slot->bitmap.width + (this->fs == FS_NORMAL));
642  uint height = max(1U, (uint)slot->bitmap.rows + (this->fs == FS_NORMAL));
643 
644  /* Limit glyph size to prevent overflows later on. */
645  if (width > 256 || height > 256) usererror("Font glyph is too large");
646 
647  /* FreeType has rendered the glyph, now we allocate a sprite and copy the image into it */
648  SpriteLoader::Sprite sprite;
649  sprite.AllocateData(ZOOM_LVL_NORMAL, width * height);
650  sprite.type = ST_FONT;
651  sprite.width = width;
652  sprite.height = height;
653  sprite.x_offs = slot->bitmap_left;
654  sprite.y_offs = this->ascender - slot->bitmap_top;
655 
656  /* Draw shadow for medium size */
657  if (this->fs == FS_NORMAL && !aa) {
658  for (uint y = 0; y < (uint)slot->bitmap.rows; y++) {
659  for (uint x = 0; x < (uint)slot->bitmap.width; x++) {
660  if (aa ? (slot->bitmap.buffer[x + y * slot->bitmap.pitch] > 0) : HasBit(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
661  sprite.data[1 + x + (1 + y) * sprite.width].m = SHADOW_COLOUR;
662  sprite.data[1 + x + (1 + y) * sprite.width].a = aa ? slot->bitmap.buffer[x + y * slot->bitmap.pitch] : 0xFF;
663  }
664  }
665  }
666  }
667 
668  for (uint y = 0; y < (uint)slot->bitmap.rows; y++) {
669  for (uint x = 0; x < (uint)slot->bitmap.width; x++) {
670  if (aa ? (slot->bitmap.buffer[x + y * slot->bitmap.pitch] > 0) : HasBit(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
671  sprite.data[x + y * sprite.width].m = FACE_COLOUR;
672  sprite.data[x + y * sprite.width].a = aa ? slot->bitmap.buffer[x + y * slot->bitmap.pitch] : 0xFF;
673  }
674  }
675  }
676 
677  GlyphEntry new_glyph;
678  new_glyph.sprite = BlitterFactory::GetCurrentBlitter()->Encode(&sprite, AllocateFont);
679  new_glyph.width = slot->advance.x >> 6;
680 
681  this->SetGlyphPtr(key, &new_glyph);
682 
683  return new_glyph.sprite;
684 }
685 
686 
688 {
689  assert(IsPrintable(key));
690 
691  if (key >= SCC_SPRITE_START && key <= SCC_SPRITE_END) {
692  return this->parent->MapCharToGlyph(key);
693  }
694 
695  return FT_Get_Char_Index(this->face, key);
696 }
697 
698 const void *FreeTypeFontCache::InternalGetFontTable(uint32 tag, size_t &length)
699 {
700  FT_ULong len = 0;
701  FT_Byte *result = nullptr;
702 
703  FT_Load_Sfnt_Table(this->face, tag, 0, nullptr, &len);
704 
705  if (len > 0) {
706  result = MallocT<FT_Byte>(len);
707  FT_Load_Sfnt_Table(this->face, tag, 0, result, &len);
708  }
709 
710  length = len;
711  return result;
712 }
713 
714 #elif defined(_WIN32)
715 
716 #include "os/windows/win32.h"
717 #ifndef ANTIALIASED_QUALITY
718 #define ANTIALIASED_QUALITY 4
719 #endif
720 
722 class Win32FontCache : public TrueTypeFontCache {
723 private:
724  LOGFONT logfont;
725  HFONT font = nullptr;
726  HDC dc = nullptr;
727  HGDIOBJ old_font;
728  SIZE glyph_size;
729 
730  void SetFontSize(FontSize fs, int pixels);
731  virtual const void *InternalGetFontTable(uint32 tag, size_t &length);
732  virtual const Sprite *InternalGetGlyph(GlyphID key, bool aa);
733 
734 public:
735  Win32FontCache(FontSize fs, const LOGFONT &logfont, int pixels);
736  ~Win32FontCache();
737  virtual void ClearFontCache();
738  virtual GlyphID MapCharToGlyph(WChar key);
739  virtual const char *GetFontName() { return WIDE_TO_MB(this->logfont.lfFaceName); }
740  virtual bool IsBuiltInFont() { return false; }
741  virtual void *GetOSHandle() { return &this->logfont; }
742 };
743 
744 
751 Win32FontCache::Win32FontCache(FontSize fs, const LOGFONT &logfont, int pixels) : TrueTypeFontCache(fs, pixels), logfont(logfont)
752 {
753  this->dc = CreateCompatibleDC(nullptr);
754  this->SetFontSize(fs, pixels);
755 }
756 
757 Win32FontCache::~Win32FontCache()
758 {
759  this->ClearFontCache();
760  DeleteDC(this->dc);
761  DeleteObject(this->font);
762 }
763 
764 void Win32FontCache::SetFontSize(FontSize fs, int pixels)
765 {
766  if (pixels == 0) {
767  /* Try to determine a good height based on the minimal height recommended by the font. */
768  int scaled_height = ScaleFontTrad(_default_font_height[this->fs]);
769  pixels = scaled_height;
770 
771  HFONT temp = CreateFontIndirect(&this->logfont);
772  if (temp != nullptr) {
773  HGDIOBJ old = SelectObject(this->dc, temp);
774 
775  UINT size = GetOutlineTextMetrics(this->dc, 0, nullptr);
776  LPOUTLINETEXTMETRIC otm = (LPOUTLINETEXTMETRIC)AllocaM(BYTE, size);
777  GetOutlineTextMetrics(this->dc, size, otm);
778 
779  /* Font height is minimum height plus the difference between the default
780  * height for this font size and the small size. */
781  int diff = scaled_height - ScaleFontTrad(_default_font_height[FS_SMALL]);
782  pixels = Clamp(min(otm->otmusMinimumPPEM, 20) + diff, scaled_height, MAX_FONT_SIZE);
783 
784  SelectObject(dc, old);
785  DeleteObject(temp);
786  }
787  } else {
788  pixels = ScaleFontTrad(pixels);
789  }
790  this->used_size = pixels;
791 
792  /* Create GDI font handle. */
793  this->logfont.lfHeight = -pixels;
794  this->logfont.lfWidth = 0;
795  this->logfont.lfOutPrecision = ANTIALIASED_QUALITY;
796 
797  if (this->font != nullptr) {
798  SelectObject(dc, this->old_font);
799  DeleteObject(this->font);
800  }
801  this->font = CreateFontIndirect(&this->logfont);
802  this->old_font = SelectObject(this->dc, this->font);
803 
804  /* Query the font metrics we needed. */
805  UINT otmSize = GetOutlineTextMetrics(this->dc, 0, nullptr);
806  POUTLINETEXTMETRIC otm = (POUTLINETEXTMETRIC)AllocaM(BYTE, otmSize);
807  GetOutlineTextMetrics(this->dc, otmSize, otm);
808 
809  this->units_per_em = otm->otmEMSquare;
810  this->ascender = otm->otmTextMetrics.tmAscent;
811  this->descender = otm->otmTextMetrics.tmDescent;
812  this->height = this->ascender + this->descender;
813  this->glyph_size.cx = otm->otmTextMetrics.tmMaxCharWidth;
814  this->glyph_size.cy = otm->otmTextMetrics.tmHeight;
815 
816  DEBUG(freetype, 2, "Loaded font '%s' with size %d", FS2OTTD((LPTSTR)((BYTE *)otm + (ptrdiff_t)otm->otmpFullName)), pixels);
817 }
818 
822 void Win32FontCache::ClearFontCache()
823 {
824  /* GUI scaling might have changed, determine font size anew if it was automatically selected. */
825  if (this->font != nullptr) this->SetFontSize(this->fs, this->req_size);
826 
828 }
829 
830 /* virtual */ const Sprite *Win32FontCache::InternalGetGlyph(GlyphID key, bool aa)
831 {
832  GLYPHMETRICS gm;
833  MAT2 mat = { {0, 1}, {0, 0}, {0, 0}, {0, 1} };
834 
835  /* Make a guess for the needed memory size. */
836  DWORD size = this->glyph_size.cy * Align(aa ? this->glyph_size.cx : max(this->glyph_size.cx / 8l, 1l), 4); // Bitmap data is DWORD-aligned rows.
837  byte *bmp = AllocaM(byte, size);
838  size = GetGlyphOutline(this->dc, key, GGO_GLYPH_INDEX | (aa ? GGO_GRAY8_BITMAP : GGO_BITMAP), &gm, size, bmp, &mat);
839 
840  if (size == GDI_ERROR) {
841  /* No dice with the guess. First query size of needed glyph memory, then allocate the
842  * memory and query again. This dance is necessary as some glyphs will only render with
843  * the exact matching size; e.g. the space glyph has no pixels and must be requested
844  * with size == 0, anything else fails. Unfortunately, a failed call doesn't return any
845  * info about the size and thus the triple GetGlyphOutline()-call. */
846  size = GetGlyphOutline(this->dc, key, GGO_GLYPH_INDEX | (aa ? GGO_GRAY8_BITMAP : GGO_BITMAP), &gm, 0, nullptr, &mat);
847  if (size == GDI_ERROR) usererror("Unable to render font glyph");
848  bmp = AllocaM(byte, size);
849  GetGlyphOutline(this->dc, key, GGO_GLYPH_INDEX | (aa ? GGO_GRAY8_BITMAP : GGO_BITMAP), &gm, size, bmp, &mat);
850  }
851 
852  /* Add 1 pixel for the shadow on the medium font. Our sprite must be at least 1x1 pixel. */
853  uint width = max(1U, (uint)gm.gmBlackBoxX + (this->fs == FS_NORMAL));
854  uint height = max(1U, (uint)gm.gmBlackBoxY + (this->fs == FS_NORMAL));
855 
856  /* Limit glyph size to prevent overflows later on. */
857  if (width > 256 || height > 256) usererror("Font glyph is too large");
858 
859  /* GDI has rendered the glyph, now we allocate a sprite and copy the image into it. */
860  SpriteLoader::Sprite sprite;
861  sprite.AllocateData(ZOOM_LVL_NORMAL, width * height);
862  sprite.type = ST_FONT;
863  sprite.width = width;
864  sprite.height = height;
865  sprite.x_offs = gm.gmptGlyphOrigin.x;
866  sprite.y_offs = this->ascender - gm.gmptGlyphOrigin.y;
867 
868  if (size > 0) {
869  /* All pixel data returned by GDI is in the form of DWORD-aligned rows.
870  * For a non anti-aliased glyph, the returned bitmap has one bit per pixel.
871  * For anti-aliased rendering, GDI uses the strange value range of 0 to 64,
872  * inclusively. To map this to 0 to 255, we shift left by two and then
873  * subtract one. */
874  uint pitch = Align(aa ? gm.gmBlackBoxX : max(gm.gmBlackBoxX / 8u, 1u), 4);
875 
876  /* Draw shadow for medium size. */
877  if (this->fs == FS_NORMAL && !aa) {
878  for (uint y = 0; y < gm.gmBlackBoxY; y++) {
879  for (uint x = 0; x < gm.gmBlackBoxX; x++) {
880  if (aa ? (bmp[x + y * pitch] > 0) : HasBit(bmp[(x / 8) + y * pitch], 7 - (x % 8))) {
881  sprite.data[1 + x + (1 + y) * sprite.width].m = SHADOW_COLOUR;
882  sprite.data[1 + x + (1 + y) * sprite.width].a = aa ? (bmp[x + y * pitch] << 2) - 1 : 0xFF;
883  }
884  }
885  }
886  }
887 
888  for (uint y = 0; y < gm.gmBlackBoxY; y++) {
889  for (uint x = 0; x < gm.gmBlackBoxX; x++) {
890  if (aa ? (bmp[x + y * pitch] > 0) : HasBit(bmp[(x / 8) + y * pitch], 7 - (x % 8))) {
891  sprite.data[x + y * sprite.width].m = FACE_COLOUR;
892  sprite.data[x + y * sprite.width].a = aa ? (bmp[x + y * pitch] << 2) - 1 : 0xFF;
893  }
894  }
895  }
896  }
897 
898  GlyphEntry new_glyph;
899  new_glyph.sprite = BlitterFactory::GetCurrentBlitter()->Encode(&sprite, AllocateFont);
900  new_glyph.width = gm.gmCellIncX;
901 
902  this->SetGlyphPtr(key, &new_glyph);
903 
904  return new_glyph.sprite;
905 }
906 
907 /* virtual */ GlyphID Win32FontCache::MapCharToGlyph(WChar key)
908 {
909  assert(IsPrintable(key));
910 
911  if (key >= SCC_SPRITE_START && key <= SCC_SPRITE_END) {
912  return this->parent->MapCharToGlyph(key);
913  }
914 
915  /* Convert characters outside of the BMP into surrogate pairs. */
916  WCHAR chars[2];
917  if (key >= 0x010000U) {
918  chars[0] = (WCHAR)(((key - 0x010000U) >> 10) + 0xD800);
919  chars[1] = (WCHAR)(((key - 0x010000U) & 0x3FF) + 0xDC00);
920  } else {
921  chars[0] = (WCHAR)(key & 0xFFFF);
922  }
923 
924  WORD glyphs[2] = {0, 0};
925  GetGlyphIndicesW(this->dc, chars, key >= 0x010000U ? 2 : 1, glyphs, GGI_MARK_NONEXISTING_GLYPHS);
926 
927  return glyphs[0] != 0xFFFF ? glyphs[0] : 0;
928 }
929 
930 /* virtual */ const void *Win32FontCache::InternalGetFontTable(uint32 tag, size_t &length)
931 {
932  DWORD len = GetFontData(this->dc, tag, 0, nullptr, 0);
933 
934  void *result = nullptr;
935  if (len != GDI_ERROR && len > 0) {
936  result = MallocT<BYTE>(len);
937  GetFontData(this->dc, tag, 0, result, len);
938  }
939 
940  length = len;
941  return result;
942 }
943 
950 static void LoadWin32Font(FontSize fs)
951 {
952  static const char *SIZE_TO_NAME[] = { "medium", "small", "large", "mono" };
953 
954  FreeTypeSubSetting *settings = nullptr;
955  switch (fs) {
956  default: NOT_REACHED();
957  case FS_SMALL: settings = &_freetype.small; break;
958  case FS_NORMAL: settings = &_freetype.medium; break;
959  case FS_LARGE: settings = &_freetype.large; break;
960  case FS_MONO: settings = &_freetype.mono; break;
961  }
962 
963  if (StrEmpty(settings->font)) return;
964 
965  LOGFONT logfont;
966  MemSetT(&logfont, 0);
967  logfont.lfPitchAndFamily = fs == FS_MONO ? FIXED_PITCH : VARIABLE_PITCH;
968  logfont.lfCharSet = DEFAULT_CHARSET;
969  logfont.lfOutPrecision = OUT_OUTLINE_PRECIS;
970  logfont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
971 
972  if (settings->os_handle != nullptr) {
973  logfont = *(const LOGFONT *)settings->os_handle;
974  } else if (strchr(settings->font, '.') != nullptr && FileExists(settings->font)) {
975  /* Might be a font file name, try load it. */
976  TCHAR fontPath[MAX_PATH];
977  convert_to_fs(settings->font, fontPath, lengthof(fontPath), false);
978 
979  if (AddFontResourceEx(fontPath, FR_PRIVATE, 0) != 0) {
980  /* Try a nice little undocumented function first for getting the internal font name.
981  * Some documentation is found at: http://www.undocprint.org/winspool/getfontresourceinfo */
982  typedef BOOL(WINAPI * PFNGETFONTRESOURCEINFO)(LPCTSTR, LPDWORD, LPVOID, DWORD);
983 #ifdef UNICODE
984  static PFNGETFONTRESOURCEINFO GetFontResourceInfo = (PFNGETFONTRESOURCEINFO)GetProcAddress(GetModuleHandle(_T("Gdi32")), "GetFontResourceInfoW");
985 #else
986  static PFNGETFONTRESOURCEINFO GetFontResourceInfo = (PFNGETFONTRESOURCEINFO)GetProcAddress(GetModuleHandle(_T("Gdi32")), "GetFontResourceInfoA");
987 #endif
988 
989  if (GetFontResourceInfo != nullptr) {
990  /* Try to query an array of LOGFONTs that describe the file. */
991  DWORD len = 0;
992  if (GetFontResourceInfo(fontPath, &len, nullptr, 2) && len >= sizeof(LOGFONT)) {
993  LOGFONT *buf = (LOGFONT *)AllocaM(byte, len);
994  if (GetFontResourceInfo(fontPath, &len, buf, 2)) {
995  logfont = *buf; // Just use first entry.
996  }
997  }
998  }
999 
1000  /* No dice yet. Use the file name as the font face name, hoping it matches. */
1001  if (logfont.lfFaceName[0] == 0) {
1002  TCHAR fname[_MAX_FNAME];
1003  _tsplitpath(fontPath, nullptr, nullptr, fname, nullptr);
1004 
1005  _tcsncpy_s(logfont.lfFaceName, lengthof(logfont.lfFaceName), fname, _TRUNCATE);
1006  logfont.lfWeight = strcasestr(settings->font, " bold") != nullptr || strcasestr(settings->font, "-bold") != nullptr ? FW_BOLD : FW_NORMAL; // Poor man's way to allow selecting bold fonts.
1007  }
1008  } else {
1009  ShowInfoF("Unable to load file '%s' for %s font, using default windows font selection instead", settings->font, SIZE_TO_NAME[fs]);
1010  }
1011  }
1012 
1013  if (logfont.lfFaceName[0] == 0) {
1014  logfont.lfWeight = strcasestr(settings->font, " bold") != nullptr ? FW_BOLD : FW_NORMAL; // Poor man's way to allow selecting bold fonts.
1015  convert_to_fs(settings->font, logfont.lfFaceName, lengthof(logfont.lfFaceName), false);
1016  }
1017 
1018  HFONT font = CreateFontIndirect(&logfont);
1019  if (font == nullptr) {
1020  ShowInfoF("Unable to use '%s' for %s font, Win32 reported error 0x%lX, using sprite font instead", settings->font, SIZE_TO_NAME[fs], GetLastError());
1021  return;
1022  }
1023  DeleteObject(font);
1024 
1025  new Win32FontCache(fs, logfont, settings->size);
1026 }
1027 
1028 #endif /* WITH_FREETYPE */
1029 
1030 #endif /* defined(WITH_FREETYPE) || defined(_WIN32) */
1031 
1036 void InitFreeType(bool monospace)
1037 {
1038  for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
1039  if (monospace != (fs == FS_MONO)) continue;
1040 
1041  FontCache *fc = FontCache::Get(fs);
1042  if (fc->HasParent()) delete fc;
1043 
1044 #ifdef WITH_FREETYPE
1046 #elif defined(_WIN32)
1047  LoadWin32Font(fs);
1048 #endif
1049  }
1050 }
1051 
1056 {
1057  for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
1058  FontCache *fc = FontCache::Get(fs);
1059  if (fc->HasParent()) delete fc;
1060  }
1061 
1062 #ifdef WITH_FREETYPE
1063  FT_Done_FreeType(_library);
1064  _library = nullptr;
1065 #endif /* WITH_FREETYPE */
1066 }
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:123
uint8 a
Alpha-channel.
int height
The height of the font.
Definition: fontcache.h:29
Control codes that are embedded in the translation strings.
TCHAR * convert_to_fs(const char *name, TCHAR *system_buf, size_t buflen, bool console_cp)
Convert from OpenTTD&#39;s encoding to that of the environment in UNICODE.
Definition: win32.cpp:627
const char * FS2OTTD(const TCHAR *name)
Convert to OpenTTD&#39;s encoding from that of the local environment.
Definition: win32.cpp:560
virtual SpriteID GetUnicodeGlyph(WChar key)
Get the SpriteID mapped to the given key.
Definition: fontcache.cpp:110
SpriteFontCache(FontSize fs)
Create a new sprite font cache.
Definition: fontcache.cpp:97
virtual void * GetOSHandle()
Get the native OS font handle, if there is one.
Definition: fontcache.h:132
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
Definition: fontcache.cpp:64
int descender
The descender value of the font.
Definition: fontcache.h:31
Settings for a single freetype font.
Definition: fontcache.h:217
~FreeTypeFontCache()
Free everything that was allocated for this font cache.
Definition: fontcache.cpp:611
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
std::vector< Pair >::const_iterator Find(const T &key) const
Finds given key in this map.
Implementation of simple mapping class.
Functions related to detecting/finding the right font.
virtual ~TrueTypeFontCache()
Free everything that was allocated for this font cache.
Definition: fontcache.cpp:274
void AllocateData(ZoomLevel zoom, size_t size)
Allocate the sprite data of this sprite.
virtual void ClearFontCache()
Clear the font cache.
Definition: fontcache.cpp:172
virtual bool IsBuiltInFont()
Is this a built-in sprite font?
Definition: fontcache.cpp:90
Functions for Standard In/Out file operations.
FreeTypeSubSetting large
The largest font; mostly used for newspapers.
Definition: fontcache.h:229
byte width
The width of the glyph.
Definition: fontcache.cpp:222
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:98
Container for information about a glyph.
Definition: fontcache.cpp:220
virtual const char * GetFontName()
Get the name of this font.
Definition: fontcache.cpp:464
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:87
#define AllocaM(T, num_elements)
alloca() has to be called in the parent function, so define AllocaM() as a macro
Definition: alloc_func.hpp:134
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:148
SmallMap< uint32, SmallPair< size_t, const void * > > FontTable
Table with font table cache.
Definition: fontcache.cpp:216
Sprite * sprite
The loaded sprite.
Definition: fontcache.cpp:221
Settings for the freetype fonts.
Definition: fontcache.h:226
bool HasParent()
Check whether the font cache has a parent.
Definition: fontcache.h:157
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:71
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:1036
void CDECL ShowInfoF(const char *str,...)
Shows some information on the console/a popup box depending on the OS.
Definition: openttd.cpp:136
Functions to read fonts from files and cache them.
virtual void ClearFontCache()
Reset cached glyphs.
Definition: fontcache.cpp:621
bool aa
Whether to do anti aliasing or not.
Definition: fontcache.h:220
A sprite used for fonts.
Definition: gfx_type.h:300
static T Align(const T x, uint n)
Return the smallest multiple of n equal or greater than x.
Definition: math_func.hpp:97
Simple pair of data.
First font.
Definition: gfx_type.h:210
virtual ~FontCache()
Clean everything up.
Definition: fontcache.cpp:51
FreeTypeSubSetting mono
The mono space font used for license/readme viewers.
Definition: fontcache.h:230
FontCache(FontSize fs)
Create a new font cache.
Definition: fontcache.cpp:41
FT_Face face
The font face associated with this font.
Definition: fontcache.cpp:453
virtual uint GetGlyphWidth(GlyphID key)=0
Get the width of the glyph with the given key.
bool FileExists(const char *filename)
Test whether the given filename exists.
Definition: fileio.cpp:326
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:465
int req_size
Requested font size.
Definition: fontcache.cpp:213
Font cache for fonts that are based on a freetype font.
Definition: fontcache.cpp:451
void CDECL usererror(const char *s,...)
Error handling for fatal user errors.
Definition: openttd.cpp:94
virtual bool GetDrawGlyphShadow()
Do we need to draw a glyph shadow?
Definition: fontcache.cpp:354
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:88
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:177
FreeTypeFontCache(FontSize fs, FT_Face face, int pixels)
Create a new FreeTypeFontCache.
Definition: fontcache.cpp:477
~SpriteFontCache()
Free everything we allocated.
Definition: fontcache.cpp:105
SpriteLoader::CommonPixel * data
The sprite itself.
virtual bool GetDrawGlyphShadow()
Do we need to draw a glyph shadow?
Definition: fontcache.cpp:196
virtual SpriteID GetUnicodeGlyph(WChar key)
Get the SpriteID mapped to the given key.
Definition: fontcache.cpp:251
virtual void InitializeUnicodeGlyphMap()
Initialize the glyph map.
Definition: fontcache.cpp:253
Structure for passing information from the sprite loader to the blitter.
FreeTypeSubSetting medium
The normal font size.
Definition: fontcache.h:228
bool Insert(const T &key, const U &data)
Adds new item to this map.
static const int MAX_FONT_SIZE
Maximum font size.
Definition: fontcache.cpp:31
Font cache for fonts that are based on a TrueType font.
Definition: fontcache.cpp:211
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:42
bool duplicate
Whether this glyph entry is a duplicate, i.e. may this be freed?
Definition: fontcache.cpp:223
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:858
virtual int GetFontSize() const
Get the nominal font size of the font.
Definition: fontcache.cpp:250
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:37
FontCache * parent
The parent of this font cache.
Definition: fontcache.h:27
uint size
The (requested) size of the font.
Definition: fontcache.h:219
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:218
uint16 width
Width of the sprite.
Definition: spritecache.h:20
void ClearGlyphToSpriteMap()
Clear the glyph to sprite mapping.
Definition: fontcache.cpp:161
const void * os_handle
Optional native OS font info.
Definition: fontcache.h:222
SpriteID ** glyph_to_spriteid_map
Mapping of glyphs to sprite IDs.
Definition: fontcache.cpp:73
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:184
virtual void SetUnicodeGlyph(WChar key, SpriteID sprite)
Map a SpriteID to the key.
Definition: fontcache.cpp:116
virtual const char * GetFontName()
Get the name of this font.
Definition: fontcache.cpp:89
TrueTypeFontCache(FontSize fs, int pixels)
Create a new TrueTypeFontCache.
Definition: fontcache.cpp:267
virtual bool IsBuiltInFont()
Is this a built-in sprite font?
Definition: fontcache.cpp:259
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:114
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:227
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:542
Index of the normal font in the font tables.
Definition: gfx_type.h:204
The normal zoom level.
Definition: zoom_type.h:24
virtual uint GetGlyphWidth(GlyphID key)
Get the width of the glyph with the given key.
Definition: fontcache.cpp:359
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:191
static const int _default_font_height[FS_END]
Default heights for the different sizes of fonts.
Definition: fontcache.cpp:34
void UninitFreeType()
Free everything allocated w.r.t.
Definition: fontcache.cpp:1055
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
int used_size
Used font size.
Definition: fontcache.cpp:214
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:131
Index of the large font in the font tables.
Definition: gfx_type.h:206
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.
virtual const void * GetFontTable(uint32 tag, size_t &length)
Read a font table from the font.
Definition: fontcache.cpp:429
virtual GlyphID MapCharToGlyph(WChar key)
Map a character into a glyph.
Definition: fontcache.cpp:687
uint32 GlyphID
Glyphs are characters from a font.
Definition: fontcache.h:19
virtual void ClearFontCache()=0
Clear the font cache.
virtual const Sprite * GetGlyph(GlyphID key)
Get the glyph (sprite) of the given key.
Definition: fontcache.cpp:372
virtual void SetUnicodeGlyph(WChar key, SpriteID sprite)
Map a SpriteID to the key.
Definition: fontcache.cpp:252
uint32 WChar
Type for wide characters, i.e.
Definition: string_type.h:37
GlyphEntry ** glyph_to_sprite
The glyph cache.
Definition: fontcache.cpp:239
declarations of functions for MS windows systems
This file contains all sprite-related enums and defines.
Factory to &#39;query&#39; all available blitters.
virtual SpriteID GetUnicodeGlyph(WChar key)=0
Get the SpriteID mapped to the given key.
virtual void ClearFontCache()
Reset cached glyphs.
Definition: fontcache.cpp:286
static const int ASCII_LETTERSTART
First printable ASCII letter.
Definition: fontcache.cpp:30
FontTable font_tables
Cached font tables.
Definition: fontcache.cpp:217
static void MemSetT(T *ptr, byte value, size_t num=1)
Type-safe version of memset().
Definition: mem_func.hpp:51