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