OpenTTD Source 20250905-master-g122023be8d
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 "../string_func.h"
14#include "../zoom_func.h"
15#include "spritefontcache.h"
16
17#include "../table/sprites.h"
18#include "../table/control_codes.h"
19#include "../table/unicode.h"
20
21#include "../safeguards.h"
22
23static const int ASCII_LETTERSTART = 32;
24
30static int ScaleFontTrad(int value)
31{
32 return UnScaleByZoom(value * ZOOM_BASE, _font_zoom);
33}
34
35static std::array<std::unordered_map<char32_t, SpriteID>, FS_END> _char_maps{};
36
42static SpriteID GetUnicodeGlyph(FontSize fs, char32_t key)
43{
44 auto found = _char_maps[fs].find(key);
45 if (found != std::end(_char_maps[fs])) return found->second;
46 return 0;
47}
48
55void SetUnicodeGlyph(FontSize fs, char32_t key, SpriteID sprite)
56{
57 _char_maps[fs][key] = sprite;
58}
59
66{
67 /* Clear out existing glyph map if it exists */
68 _char_maps[fs].clear();
69
70 SpriteID base;
71 switch (fs) {
72 default: NOT_REACHED();
73 case FS_MONO: // Use normal as default for mono spaced font
74 case FS_NORMAL: base = SPR_ASCII_SPACE; break;
75 case FS_SMALL: base = SPR_ASCII_SPACE_SMALL; break;
76 case FS_LARGE: base = SPR_ASCII_SPACE_BIG; break;
77 }
78
79 for (uint i = ASCII_LETTERSTART; i < 256; i++) {
80 SpriteID sprite = base + i - ASCII_LETTERSTART;
81 if (!SpriteExists(sprite)) continue;
82 SetUnicodeGlyph(fs, i, sprite);
83 SetUnicodeGlyph(fs, i + SCC_SPRITE_START, sprite);
84 }
85
86 /* Modify map to move non-standard glyphs to a better unicode codepoint. */
87 for (const auto &unicode_map : _default_unicode_map) {
88 uint8_t key = unicode_map.key;
89 if (key == CLRA) {
90 /* Clear the glyph. This happens if the glyph at this code point
91 * is non-standard and should be accessed by an SCC_xxx enum
92 * entry only. */
93 SetUnicodeGlyph(fs, unicode_map.code, 0);
94 } else {
95 SpriteID sprite = base + key - ASCII_LETTERSTART;
96 SetUnicodeGlyph(fs, unicode_map.code, sprite);
97 }
98 }
99}
100
105{
106 for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
108 }
109}
110
116{
117 this->height = ScaleGUITrad(FontCache::GetDefaultFontHeight(this->fs));
118 this->ascender = (this->height - ScaleFontTrad(FontCache::GetDefaultFontHeight(this->fs))) / 2;
119}
120
122{
124 this->height = ScaleGUITrad(FontCache::GetDefaultFontHeight(this->fs));
125 this->ascender = (this->height - ScaleFontTrad(FontCache::GetDefaultFontHeight(this->fs))) / 2;
126}
127
129{
130 SpriteID sprite = static_cast<SpriteID>(key & ~SPRITE_GLYPH);
131 if (sprite == 0) sprite = GetUnicodeGlyph(this->fs, '?');
132 return GetSprite(sprite, SpriteType::Font);
133}
134
136{
137 SpriteID sprite = static_cast<SpriteID>(key & ~SPRITE_GLYPH);
138 if (sprite == 0) sprite = GetUnicodeGlyph(this->fs, '?');
139 return SpriteExists(sprite) ? GetSprite(sprite, SpriteType::Font)->width + ScaleFontTrad(this->fs != FS_NORMAL ? 1 : 0) : 0;
140}
141
142GlyphID SpriteFontCache::MapCharToGlyph(char32_t key, [[maybe_unused]] bool allow_fallback)
143{
144 assert(IsPrintable(key));
145 SpriteID sprite = GetUnicodeGlyph(this->fs, key);
146 if (sprite == 0) return 0;
147 return SPRITE_GLYPH | sprite;
148}
149
151{
152 return false;
153}
154
156public:
157 SpriteFontCacheFactory() : FontCacheFactory("sprite", "Sprite font provider") {}
158
159 std::unique_ptr<FontCache> LoadFont(FontSize fs, FontType fonttype) override
160 {
161 if (fonttype != FontType::Sprite) return nullptr;
162
163 return std::make_unique<SpriteFontCache>(fs);
164 }
165
166 bool FindFallbackFont(struct FontCacheSettings *, const std::string &, class MissingGlyphSearcher *) override
167 {
168 return false;
169 }
170
171private:
172 static SpriteFontCacheFactory instance;
173};
174
175/* static */ SpriteFontCacheFactory SpriteFontCacheFactory::instance;
Factory for FontCaches.
Definition fontcache.h:220
Font cache for basic fonts.
Definition fontcache.h:22
int height
The height of the font.
Definition fontcache.h:27
const FontSize fs
The size of the font.
Definition fontcache.h:26
int ascender
The ascender value of the font.
Definition fontcache.h:28
static void ResetFontCache(FontSize size)
Reset cached font information.
A searcher for missing glyphs.
SpriteFontCache(FontSize fs)
Create a new sprite font cache.
uint GetGlyphWidth(GlyphID key) override
Get the width of the glyph with the given key.
GlyphID MapCharToGlyph(char32_t key, bool allow_fallback=true) override
Map a character into a glyph.
bool GetDrawGlyphShadow() override
Do we need to draw a glyph shadow?
void ClearFontCache() override
Clear the font cache.
const Sprite * GetGlyph(GlyphID key) override
Get the glyph (sprite) of the given key.
FontType
Different types of font that can be loaded.
Definition fontcache.h:214
@ Sprite
Bitmap sprites from GRF files.
uint32_t GlyphID
Glyphs are characters from a font.
Definition fontcache.h:18
ZoomLevel _font_zoom
Sprite font Zoom level (not clamped)
Definition gfx.cpp:63
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
@ Font
A sprite used for fonts.
FontSize
Available font sizes.
Definition gfx_type.h:248
@ FS_MONO
Index of the monospaced font in the font tables.
Definition gfx_type.h:252
@ FS_SMALL
Index of the small font in the font tables.
Definition gfx_type.h:250
@ FS_BEGIN
First font.
Definition gfx_type.h:255
@ FS_NORMAL
Index of the normal font in the font tables.
Definition gfx_type.h:249
@ FS_LARGE
Index of the large font in the font tables.
Definition gfx_type.h:251
static int ScaleFontTrad(int value)
Scale traditional pixel dimensions to font zoom level, for drawing sprite fonts.
void SetUnicodeGlyph(FontSize fs, char32_t key, SpriteID sprite)
Set the SpriteID for a unicode character.
static const int ASCII_LETTERSTART
First printable ASCII letter.
static std::array< std::unordered_map< char32_t, SpriteID >, FS_END > _char_maps
Glyph map for each font size.
void InitializeUnicodeGlyphMap()
Initialize the glyph map.
static SpriteID GetUnicodeGlyph(FontSize fs, char32_t key)
Get SpriteID associated with a character.
Sprite font cache implementation definition.
Settings for the four different fonts.
Definition fontcache.h:180
Data structure describing a sprite.
uint16_t width
Width of the sprite.
static const uint8_t CLRA
Identifier to clear all glyphs at this codepoint.
Definition unicode.h:15
static RectPadding ScaleGUITrad(const RectPadding &r)
Scale a RectPadding to GUI zoom level.
Definition widget.cpp:49
int UnScaleByZoom(int value, ZoomLevel zoom)
Scale by zoom level, usually shift right (when zoom > ZoomLevel::Min) When shifting right,...
Definition zoom_func.h:34