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

OpenTTD Source 15.0-beta1

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
38SpriteFontCache::SpriteFontCache(FontSize fs) : FontCache(fs)
39{
40 this->InitializeUnicodeGlyphMap();
41 this->height = ScaleGUITrad(FontCache::GetDefaultFontHeight(this->fs));
42 this->ascender = (this->height - ScaleFontTrad(FontCache::GetDefaultFontHeight(this->fs))) / 2;
43}
44
50 SpriteID SpriteFontCache::GetUnicodeGlyph(GlyphID key)
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
62 void SpriteFontCache::InitializeUnicodeGlyphMap()
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
97 void SpriteFontCache::ClearFontCache()
98{
99 Layouter::ResetFontCache(this->fs);
100 this->height = ScaleGUITrad(FontCache::GetDefaultFontHeight(this->fs));
101 this->ascender = (this->height - ScaleFontTrad(FontCache::GetDefaultFontHeight(this->fs))) / 2;
102}
103
104 const Sprite *SpriteFontCache::GetGlyph(GlyphID key)
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
111uint SpriteFontCache::GetGlyphWidth(GlyphID key)
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
126 bool SpriteFontCache::GetDrawGlyphShadow()
127{
128 return false;
129}
FontCache
Font cache for basic fonts.
Definition fontcache.h:21
FontCache::height
int height
The height of the font.
Definition fontcache.h:26
FontCache::fs
const FontSize fs
The size of the font.
Definition fontcache.h:25
FontCache::ascender
int ascender
The ascender value of the font.
Definition fontcache.h:27
Layouter::ResetFontCache
static void ResetFontCache(FontSize size)
Reset cached font information.
Definition gfx_layout.cpp:353
SpriteFontCache::SpriteFontCache
SpriteFontCache(FontSize fs)
Create a new sprite font cache.
Definition spritefontcache.cpp:38
SpriteFontCache::GetGlyphWidth
uint GetGlyphWidth(GlyphID key) override
Get the width of the glyph with the given key.
Definition spritefontcache.cpp:111
SpriteFontCache::InitializeUnicodeGlyphMap
void InitializeUnicodeGlyphMap() override
Initialize the glyph map.
Definition spritefontcache.cpp:62
SpriteFontCache::MapCharToGlyph
GlyphID MapCharToGlyph(char32_t key, bool allow_fallback=true) override
Map a character into a glyph.
Definition spritefontcache.cpp:118
SpriteFontCache::GetUnicodeGlyph
SpriteID GetUnicodeGlyph(GlyphID key)
Get SpriteID associated with a GlyphID.
Definition spritefontcache.cpp:50
SpriteFontCache::glyph_to_spriteid_map
std::unordered_map< GlyphID, SpriteID > glyph_to_spriteid_map
Mapping of glyphs to sprite IDs.
Definition spritefontcache.h:31
SpriteFontCache::GetDrawGlyphShadow
bool GetDrawGlyphShadow() override
Do we need to draw a glyph shadow?
Definition spritefontcache.cpp:126
SpriteFontCache::SetUnicodeGlyph
void SetUnicodeGlyph(char32_t key, SpriteID sprite) override
Map a SpriteID to the key.
Definition spritefontcache.cpp:57
SpriteFontCache::ClearFontCache
void ClearFontCache() override
Clear the font cache.
Definition spritefontcache.cpp:97
SpriteFontCache::GetGlyph
const Sprite * GetGlyph(GlyphID key) override
Get the glyph (sprite) of the given key.
Definition spritefontcache.cpp:104
GlyphID
uint32_t GlyphID
Glyphs are characters from a font.
Definition fontcache.h:17
_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:18
SpriteType::Font
@ Font
A sprite used for fonts.
FontSize
FontSize
Available font sizes.
Definition gfx_type.h:208
FS_MONO
@ FS_MONO
Index of the monospaced font in the font tables.
Definition gfx_type.h:212
FS_SMALL
@ FS_SMALL
Index of the small font in the font tables.
Definition gfx_type.h:210
FS_NORMAL
@ FS_NORMAL
Index of the normal font in the font tables.
Definition gfx_type.h:209
FS_LARGE
@ FS_LARGE
Index of the large font in the font tables.
Definition gfx_type.h:211
ScaleFontTrad
static int ScaleFontTrad(int value)
Scale traditional pixel dimensions to font zoom level, for drawing sprite fonts.
Definition spritefontcache.cpp:29
ASCII_LETTERSTART
static const int ASCII_LETTERSTART
First printable ASCII letter.
Definition spritefontcache.cpp:22
spritefontcache.h
Sprite font cache implementation definition.
Sprite
Data structure describing a sprite.
Definition spritecache.h:17
Sprite::width
uint16_t width
Width of the sprite.
Definition spritecache.h:19
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:35
UnScaleByZoom
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