OpenTTD Source 20251019-master-g9f7f314f81
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 "../spritecache.h"
14#include "../string_func.h"
15#include "../zoom_func.h"
16#include "spritefontcache.h"
17
18#include "../table/sprites.h"
19#include "../table/control_codes.h"
20#include "../table/unicode.h"
21
22#include "../safeguards.h"
23
24static const int ASCII_LETTERSTART = 32;
25
31static int ScaleFontTrad(int value)
32{
33 return UnScaleByZoom(value * ZOOM_BASE, _font_zoom);
34}
35
36static std::array<std::unordered_map<char32_t, SpriteID>, FS_END> _char_maps{};
37
43static SpriteID GetUnicodeGlyph(FontSize fs, char32_t key)
44{
45 auto found = _char_maps[fs].find(key);
46 if (found != std::end(_char_maps[fs])) return found->second;
47 return 0;
48}
49
56void SetUnicodeGlyph(FontSize fs, char32_t key, SpriteID sprite)
57{
58 _char_maps[fs][key] = sprite;
59}
60
67{
68 /* Clear out existing glyph map if it exists */
69 _char_maps[fs].clear();
70
71 SpriteID base;
72 switch (fs) {
73 default: NOT_REACHED();
74 case FS_MONO: // Use normal as default for mono spaced font
75 case FS_NORMAL: base = SPR_ASCII_SPACE; break;
76 case FS_SMALL: base = SPR_ASCII_SPACE_SMALL; break;
77 case FS_LARGE: base = SPR_ASCII_SPACE_BIG; break;
78 }
79
80 for (uint i = ASCII_LETTERSTART; i < 256; i++) {
81 SpriteID sprite = base + i - ASCII_LETTERSTART;
82 if (!SpriteExists(sprite)) continue;
83 SetUnicodeGlyph(fs, i, sprite);
84 SetUnicodeGlyph(fs, i + SCC_SPRITE_START, sprite);
85 }
86
87 /* Modify map to move non-standard glyphs to a better unicode codepoint. */
88 for (const auto &unicode_map : _default_unicode_map) {
89 uint8_t key = unicode_map.key;
90 if (key == CLRA) {
91 /* Clear the glyph. This happens if the glyph at this code point
92 * is non-standard and should be accessed by an SCC_xxx enum
93 * entry only. */
94 SetUnicodeGlyph(fs, unicode_map.code, 0);
95 } else {
96 SpriteID sprite = base + key - ASCII_LETTERSTART;
97 if (!SpriteExists(sprite)) continue;
98 SetUnicodeGlyph(fs, unicode_map.code, sprite);
99 }
100 }
101}
102
107{
108 for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
110 }
111}
112
118{
119 this->height = ScaleGUITrad(FontCache::GetDefaultFontHeight(this->fs));
120 this->ascender = (this->height - ScaleFontTrad(FontCache::GetDefaultFontHeight(this->fs))) / 2;
121}
122
124{
126 this->height = ScaleGUITrad(FontCache::GetDefaultFontHeight(this->fs));
127 this->ascender = (this->height - ScaleFontTrad(FontCache::GetDefaultFontHeight(this->fs))) / 2;
128}
129
131{
132 SpriteID sprite = static_cast<SpriteID>(key & ~SPRITE_GLYPH);
133 if (sprite == 0) sprite = GetUnicodeGlyph(this->fs, '?');
134 return GetSprite(sprite, SpriteType::Font);
135}
136
138{
139 SpriteID sprite = static_cast<SpriteID>(key & ~SPRITE_GLYPH);
140 if (sprite == 0) sprite = GetUnicodeGlyph(this->fs, '?');
141 return SpriteExists(sprite) ? GetSprite(sprite, SpriteType::Font)->width + ScaleFontTrad(this->fs != FS_NORMAL ? 1 : 0) : 0;
142}
143
144GlyphID SpriteFontCache::MapCharToGlyph(char32_t key, [[maybe_unused]] bool allow_fallback)
145{
146 assert(IsPrintable(key));
147 SpriteID sprite = GetUnicodeGlyph(this->fs, key);
148 if (sprite == 0) return 0;
149 return SPRITE_GLYPH | sprite;
150}
151
153{
154 return false;
155}
156
158public:
159 SpriteFontCacheFactory() : FontCacheFactory("sprite", "Sprite font provider") {}
160
161 std::unique_ptr<FontCache> LoadFont(FontSize fs, FontType fonttype) const override
162 {
163 if (fonttype != FontType::Sprite) return nullptr;
164
165 return std::make_unique<SpriteFontCache>(fs);
166 }
167
168 bool FindFallbackFont(struct FontCacheSettings *, const std::string &, class MissingGlyphSearcher *) const override
169 {
170 return false;
171 }
172
173private:
174 static SpriteFontCacheFactory instance;
175};
176
177/* 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