OpenTTD Source 20260206-master-g4d4e37dbf1
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
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"
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
44static SpriteID GetUnicodeGlyph(FontSize fs, char32_t key)
45{
46 auto found = _char_maps[fs].find(key);
47 if (found != std::end(_char_maps[fs])) return found->second;
48 return 0;
49}
50
57void SetUnicodeGlyph(FontSize fs, char32_t key, SpriteID sprite)
58{
59 _char_maps[fs][key] = sprite;
60}
61
68{
69 /* Clear out existing glyph map if it exists */
70 _char_maps[fs].clear();
71
72 SpriteID base;
73 switch (fs) {
74 default: NOT_REACHED();
75 case FS_MONO: // Use normal as default for mono spaced font
76 case FS_NORMAL: base = SPR_ASCII_SPACE; break;
77 case FS_SMALL: base = SPR_ASCII_SPACE_SMALL; break;
78 case FS_LARGE: base = SPR_ASCII_SPACE_BIG; break;
79 }
80
81 for (uint i = ASCII_LETTERSTART; i < 256; i++) {
82 SpriteID sprite = base + i - ASCII_LETTERSTART;
83 if (!SpriteExists(sprite)) continue;
84 SetUnicodeGlyph(fs, i, sprite);
85 SetUnicodeGlyph(fs, i + SCC_SPRITE_START, sprite);
86 }
87
88 /* Modify map to move non-standard glyphs to a better unicode codepoint. */
89 for (const auto &unicode_map : _default_unicode_map) {
90 uint8_t key = unicode_map.key;
91 if (key == CLRA) {
92 /* Clear the glyph. This happens if the glyph at this code point
93 * is non-standard and should be accessed by an SCC_xxx enum
94 * entry only. */
95 SetUnicodeGlyph(fs, unicode_map.code, 0);
96 } else {
97 SpriteID sprite = base + key - ASCII_LETTERSTART;
98 if (!SpriteExists(sprite)) continue;
99 SetUnicodeGlyph(fs, unicode_map.code, sprite);
100 }
101 }
102}
103
108{
109 for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
111 }
112}
113
119{
120 this->height = ScaleGUITrad(FontCache::GetDefaultFontHeight(this->fs));
121 this->ascender = (this->height - ScaleFontTrad(FontCache::GetDefaultFontHeight(this->fs))) / 2;
122}
123
125{
127 this->height = ScaleGUITrad(FontCache::GetDefaultFontHeight(this->fs));
128 this->ascender = (this->height - ScaleFontTrad(FontCache::GetDefaultFontHeight(this->fs))) / 2;
129}
130
132{
133 SpriteID sprite = static_cast<SpriteID>(key & ~SPRITE_GLYPH);
134 if (sprite == 0) sprite = GetUnicodeGlyph(this->fs, '?');
135 return GetSprite(sprite, SpriteType::Font);
136}
137
139{
140 SpriteID sprite = static_cast<SpriteID>(key & ~SPRITE_GLYPH);
141 if (sprite == 0) sprite = GetUnicodeGlyph(this->fs, '?');
142 return SpriteExists(sprite) ? GetSprite(sprite, SpriteType::Font)->width + ScaleFontTrad(this->fs != FS_NORMAL ? 1 : 0) : 0;
143}
144
145GlyphID SpriteFontCache::MapCharToGlyph(char32_t key, [[maybe_unused]] bool allow_fallback)
146{
147 assert(IsPrintable(key));
148 SpriteID sprite = GetUnicodeGlyph(this->fs, key);
149 if (sprite == 0) return 0;
150 return SPRITE_GLYPH | sprite;
151}
152
154{
155 return false;
156}
157
158class SpriteFontCacheFactory : public FontCacheFactory {
159public:
160 SpriteFontCacheFactory() : FontCacheFactory("sprite", "Sprite font provider") {}
161
162 std::unique_ptr<FontCache> LoadFont(FontSize fs, FontType fonttype) const override
163 {
164 if (fonttype != FontType::Sprite) return nullptr;
165
166 return std::make_unique<SpriteFontCache>(fs);
167 }
168
169 bool FindFallbackFont(struct FontCacheSettings *, const std::string &, class MissingGlyphSearcher *) const override
170 {
171 return false;
172 }
173
174private:
175 static SpriteFontCacheFactory instance;
176};
177
178/* static */ SpriteFontCacheFactory SpriteFontCacheFactory::instance;
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.
Control codes that are embedded in the translation strings.
Functions to read fonts from files and cache them.
FontType
Different types of font that can be loaded.
Definition fontcache.h:214
@ Sprite
Bitmap sprites from GRF files.
Definition fontcache.h:215
ZoomLevel _font_zoom
Sprite font Zoom level (not clamped).
Definition gfx.cpp:63
Functions related to laying out the texts.
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
@ Font
A sprite used for fonts.
Definition gfx_type.h:360
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
A number of safeguards to prevent using unsafe methods.
Functions to cache sprites in memory.
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.
This file contains all sprite-related enums and defines.
Definition of base types and functions in a cross-platform compatible way.
Functions related to low-level strings.
Settings for the four different fonts.
Definition fontcache.h:180
Data structure describing a sprite.
Character mapping for using Unicode characters in OTTD.
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
Functions related to zooming.
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