OpenTTD Source: src/fontcache/spritefontcache.cpp Source File

OpenTTD Source 15.3

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
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
24 static const int ASCII_LETTERSTART = 32;
25
31 static int ScaleFontTrad(int value)
32{
33 return UnScaleByZoom(value * ZOOM_BASE, _font_zoom);
34}
35
36 static std::array<std::unordered_map<char32_t, SpriteID>, FS_END> _char_maps{};
37
43 static 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
56 void SetUnicodeGlyph(FontSize fs, char32_t key, SpriteID sprite)
57{
58 _char_maps[fs][key] = sprite;
59}
60
66 void InitializeUnicodeGlyphMap(FontSize fs)
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
106 void InitializeUnicodeGlyphMap()
107{
108 for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
109 InitializeUnicodeGlyphMap(fs);
110 }
111}
112
117SpriteFontCache::SpriteFontCache(FontSize fs) : FontCache(fs)
118{
119 this->height = ScaleGUITrad(FontCache::GetDefaultFontHeight(this->fs));
120 this->ascender = (this->height - ScaleFontTrad(FontCache::GetDefaultFontHeight(this->fs))) / 2;
121}
122
123 void SpriteFontCache::ClearFontCache()
124{
125 Layouter::ResetFontCache(this->fs);
126 this->height = ScaleGUITrad(FontCache::GetDefaultFontHeight(this->fs));
127 this->ascender = (this->height - ScaleFontTrad(FontCache::GetDefaultFontHeight(this->fs))) / 2;
128}
129
130 const Sprite *SpriteFontCache::GetGlyph(GlyphID key)
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
137uint SpriteFontCache::GetGlyphWidth(GlyphID key)
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
144 GlyphID 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
152 bool SpriteFontCache::GetDrawGlyphShadow()
153{
154 return false;
155}
156
157 class SpriteFontCacheFactory : public FontCacheFactory {
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;
FontCacheFactory
Factory for FontCaches.
Definition fontcache.h:220
FontCache
Font cache for basic fonts.
Definition fontcache.h:22
FontCache::height
int height
The height of the font.
Definition fontcache.h:27
FontCache::fs
const FontSize fs
The size of the font.
Definition fontcache.h:26
FontCache::ascender
int ascender
The ascender value of the font.
Definition fontcache.h:28
Layouter::ResetFontCache
static void ResetFontCache(FontSize size)
Reset cached font information.
Definition gfx_layout.cpp:367
MissingGlyphSearcher
A searcher for missing glyphs.
Definition strings_func.h:156
SpriteFontCacheFactory
Definition spritefontcache.cpp:157
SpriteFontCache::SpriteFontCache
SpriteFontCache(FontSize fs)
Create a new sprite font cache.
Definition spritefontcache.cpp:117
SpriteFontCache::GetGlyphWidth
uint GetGlyphWidth(GlyphID key) override
Get the width of the glyph with the given key.
Definition spritefontcache.cpp:137
SpriteFontCache::MapCharToGlyph
GlyphID MapCharToGlyph(char32_t key, bool allow_fallback=true) override
Map a character into a glyph.
Definition spritefontcache.cpp:144
SpriteFontCache::GetDrawGlyphShadow
bool GetDrawGlyphShadow() override
Do we need to draw a glyph shadow?
Definition spritefontcache.cpp:152
SpriteFontCache::ClearFontCache
void ClearFontCache() override
Clear the font cache.
Definition spritefontcache.cpp:123
SpriteFontCache::GetGlyph
const Sprite * GetGlyph(GlyphID key) override
Get the glyph (sprite) of the given key.
Definition spritefontcache.cpp:130
FontType
FontType
Different types of font that can be loaded.
Definition fontcache.h:214
FontType::Sprite
@ Sprite
Bitmap sprites from GRF files.
GlyphID
uint32_t GlyphID
Glyphs are characters from a font.
Definition fontcache.h:18
_font_zoom
ZoomLevel _font_zoom
Sprite font Zoom level (not clamped)
Definition gfx.cpp:62
SpriteID
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
SpriteType::Font
@ Font
A sprite used for fonts.
FontSize
FontSize
Available font sizes.
Definition gfx_type.h:248
FS_MONO
@ FS_MONO
Index of the monospaced font in the font tables.
Definition gfx_type.h:252
FS_SMALL
@ FS_SMALL
Index of the small font in the font tables.
Definition gfx_type.h:250
FS_BEGIN
@ FS_BEGIN
First font.
Definition gfx_type.h:255
FS_NORMAL
@ FS_NORMAL
Index of the normal font in the font tables.
Definition gfx_type.h:249
FS_LARGE
@ FS_LARGE
Index of the large font in the font tables.
Definition gfx_type.h:251
ScaleFontTrad
static int ScaleFontTrad(int value)
Scale traditional pixel dimensions to font zoom level, for drawing sprite fonts.
Definition spritefontcache.cpp:31
SetUnicodeGlyph
void SetUnicodeGlyph(FontSize fs, char32_t key, SpriteID sprite)
Set the SpriteID for a unicode character.
Definition spritefontcache.cpp:56
ASCII_LETTERSTART
static const int ASCII_LETTERSTART
First printable ASCII letter.
Definition spritefontcache.cpp:24
_char_maps
static std::array< std::unordered_map< char32_t, SpriteID >, FS_END > _char_maps
Glyph map for each font size.
Definition spritefontcache.cpp:36
InitializeUnicodeGlyphMap
void InitializeUnicodeGlyphMap()
Initialize the glyph map.
Definition spritefontcache.cpp:106
GetUnicodeGlyph
static SpriteID GetUnicodeGlyph(FontSize fs, char32_t key)
Get SpriteID associated with a character.
Definition spritefontcache.cpp:43
spritefontcache.h
Sprite font cache implementation definition.
FontCacheSettings
Settings for the four different fonts.
Definition fontcache.h:180
Sprite
Data structure describing a sprite.
Definition spritecache_type.h:16
Sprite::width
uint16_t width
Width of the sprite.
Definition spritecache_type.h:18
CLRA
static const uint8_t CLRA
Identifier to clear all glyphs at this codepoint.
Definition unicode.h:15
ScaleGUITrad
static RectPadding ScaleGUITrad(const RectPadding &r)
Scale a RectPadding to GUI zoom level.
Definition widget.cpp:49
UnScaleByZoom
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