OpenTTD Source  20241108-master-g80f628063a
spritefontcache.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 "../gfx_layout.h"
13 #include "../zoom_func.h"
14 #include "spritefontcache.h"
15 
16 #include "../table/sprites.h"
17 #include "../table/control_codes.h"
18 #include "../table/unicode.h"
19 
20 #include "../safeguards.h"
21 
22 static const int ASCII_LETTERSTART = 32;
23 
29 static int ScaleFontTrad(int value)
30 {
31  return UnScaleByZoom(value * ZOOM_BASE, _font_zoom);
32 }
33 
39 {
41  this->height = ScaleGUITrad(FontCache::GetDefaultFontHeight(this->fs));
42  this->ascender = (this->height - ScaleFontTrad(FontCache::GetDefaultFontHeight(this->fs))) / 2;
43 }
44 
51 {
52  const auto found = this->glyph_to_spriteid_map.find(key & ~SPRITE_GLYPH);
53  if (found == std::end(this->glyph_to_spriteid_map)) return 0;
54  return found->second;
55 }
56 
57 void SpriteFontCache::SetUnicodeGlyph(char32_t key, SpriteID sprite)
58 {
59  this->glyph_to_spriteid_map[key] = sprite;
60 }
61 
63 {
64  /* Clear out existing glyph map if it exists */
65  this->glyph_to_spriteid_map.clear();
66 
67  SpriteID base;
68  switch (this->fs) {
69  default: NOT_REACHED();
70  case FS_MONO: // Use normal as default for mono spaced font
71  case FS_NORMAL: base = SPR_ASCII_SPACE; break;
72  case FS_SMALL: base = SPR_ASCII_SPACE_SMALL; break;
73  case FS_LARGE: base = SPR_ASCII_SPACE_BIG; break;
74  }
75 
76  for (uint i = ASCII_LETTERSTART; i < 256; i++) {
77  SpriteID sprite = base + i - ASCII_LETTERSTART;
78  if (!SpriteExists(sprite)) continue;
79  this->SetUnicodeGlyph(i, sprite);
80  this->SetUnicodeGlyph(i + SCC_SPRITE_START, sprite);
81  }
82 
83  for (const auto &unicode_map : _default_unicode_map) {
84  uint8_t key = unicode_map.key;
85  if (key == CLRA) {
86  /* Clear the glyph. This happens if the glyph at this code point
87  * is non-standard and should be accessed by an SCC_xxx enum
88  * entry only. */
89  this->SetUnicodeGlyph(unicode_map.code, 0);
90  } else {
91  SpriteID sprite = base + key - ASCII_LETTERSTART;
92  this->SetUnicodeGlyph(unicode_map.code, sprite);
93  }
94  }
95 }
96 
98 {
100  this->height = ScaleGUITrad(FontCache::GetDefaultFontHeight(this->fs));
101  this->ascender = (this->height - ScaleFontTrad(FontCache::GetDefaultFontHeight(this->fs))) / 2;
102 }
103 
105 {
106  SpriteID sprite = this->GetUnicodeGlyph(static_cast<char32_t>(key & ~SPRITE_GLYPH));
107  if (sprite == 0) sprite = this->GetUnicodeGlyph('?');
108  return GetSprite(sprite, SpriteType::Font);
109 }
110 
112 {
113  SpriteID sprite = this->GetUnicodeGlyph(static_cast<char32_t>(key & ~SPRITE_GLYPH));
114  if (sprite == 0) sprite = this->GetUnicodeGlyph('?');
115  return SpriteExists(sprite) ? GetSprite(sprite, SpriteType::Font)->width + ScaleFontTrad(this->fs != FS_NORMAL ? 1 : 0) : 0;
116 }
117 
118 GlyphID SpriteFontCache::MapCharToGlyph(char32_t key, [[maybe_unused]] bool allow_fallback)
119 {
120  assert(IsPrintable(key));
121  SpriteID sprite = this->GetUnicodeGlyph(key);
122  if (sprite == 0) return 0;
123  return SPRITE_GLYPH | key;
124 }
125 
127 {
128  return false;
129 }
Font cache for basic fonts.
Definition: fontcache.h:21
int height
The height of the font.
Definition: fontcache.h:26
const FontSize fs
The size of the font.
Definition: fontcache.h:25
int ascender
The ascender value of the font.
Definition: fontcache.h:27
static void ResetFontCache(FontSize size)
Reset cached font information.
Definition: gfx_layout.cpp:353
SpriteFontCache(FontSize fs)
Create a new sprite font cache.
uint GetGlyphWidth(GlyphID key) override
Get the width of the glyph with the given key.
void InitializeUnicodeGlyphMap() override
Initialize the glyph map.
GlyphID MapCharToGlyph(char32_t key, bool allow_fallback=true) override
Map a character into a glyph.
SpriteID GetUnicodeGlyph(GlyphID key)
Get SpriteID associated with a GlyphID.
std::unordered_map< GlyphID, SpriteID > glyph_to_spriteid_map
Mapping of glyphs to sprite IDs.
bool GetDrawGlyphShadow() override
Do we need to draw a glyph shadow?
void SetUnicodeGlyph(char32_t key, SpriteID sprite) override
Map a SpriteID to the key.
void ClearFontCache() override
Clear the font cache.
const Sprite * GetGlyph(GlyphID key) override
Get the glyph (sprite) of the given key.
uint32_t GlyphID
Glyphs are characters from a font.
Definition: fontcache.h:17
ZoomLevel _font_zoom
Sprite font Zoom level (not clamped)
Definition: gfx.cpp:62
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:18
@ Font
A sprite used for fonts.
FontSize
Available font sizes.
Definition: gfx_type.h:208
@ FS_MONO
Index of the monospaced font in the font tables.
Definition: gfx_type.h:212
@ FS_SMALL
Index of the small font in the font tables.
Definition: gfx_type.h:210
@ FS_NORMAL
Index of the normal font in the font tables.
Definition: gfx_type.h:209
@ FS_LARGE
Index of the large font in the font tables.
Definition: gfx_type.h:211
static int ScaleFontTrad(int value)
Scale traditional pixel dimensions to font zoom level, for drawing sprite fonts.
static const int ASCII_LETTERSTART
First printable ASCII letter.
Sprite font cache implementation definition.
Data structure describing a sprite.
Definition: spritecache.h:17
uint16_t width
Width of the sprite.
Definition: spritecache.h:19
static const uint8_t CLRA
Identifier to clear all glyphs at this codepoint.
Definition: unicode.h:15
int ScaleGUITrad(int value)
Scale traditional pixel dimensions to GUI zoom level.
Definition: zoom_func.h:117
int UnScaleByZoom(int value, ZoomLevel zoom)
Scale by zoom level, usually shift right (when zoom > ZOOM_LVL_MIN) When shifting right,...
Definition: zoom_func.h:34