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