Fix #12973: Don't exclude high score after using sandbox
[openttd-github.git] / src / fontcache / spritefontcache.cpp
blobc9f75153b578cdaefed339622efa24e1f8fdb166
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 */
8 /** @file spritefontcache.cpp Sprite fontcache implementation. */
10 #include "../stdafx.h"
11 #include "../fontcache.h"
12 #include "../gfx_layout.h"
13 #include "../zoom_func.h"
14 #include "spritefontcache.h"
16 #include "../table/sprites.h"
17 #include "../table/control_codes.h"
18 #include "../table/unicode.h"
20 #include "../safeguards.h"
22 static const int ASCII_LETTERSTART = 32; ///< First printable ASCII letter.
24 /**
25 * Scale traditional pixel dimensions to font zoom level, for drawing sprite fonts.
26 * @param value Pixel amount at #ZOOM_BASE (traditional "normal" interface size).
27 * @return Pixel amount at _font_zoom (current interface size).
29 static int ScaleFontTrad(int value)
31 return UnScaleByZoom(value * ZOOM_BASE, _font_zoom);
34 /**
35 * Create a new sprite font cache.
36 * @param fs The font size to create the cache for.
38 SpriteFontCache::SpriteFontCache(FontSize fs) : FontCache(fs)
40 this->InitializeUnicodeGlyphMap();
41 this->height = ScaleGUITrad(FontCache::GetDefaultFontHeight(this->fs));
42 this->ascender = (this->height - ScaleFontTrad(FontCache::GetDefaultFontHeight(this->fs))) / 2;
45 /**
46 * Get SpriteID associated with a GlyphID.
47 * @param key Glyph to find.
48 * @return SpriteID of glyph, or 0 if not present.
50 SpriteID SpriteFontCache::GetUnicodeGlyph(GlyphID key)
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;
57 void SpriteFontCache::SetUnicodeGlyph(char32_t key, SpriteID sprite)
59 this->glyph_to_spriteid_map[key] = sprite;
62 void SpriteFontCache::InitializeUnicodeGlyphMap()
64 /* Clear out existing glyph map if it exists */
65 this->glyph_to_spriteid_map.clear();
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;
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);
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);
97 void SpriteFontCache::ClearFontCache()
99 Layouter::ResetFontCache(this->fs);
100 this->height = ScaleGUITrad(FontCache::GetDefaultFontHeight(this->fs));
101 this->ascender = (this->height - ScaleFontTrad(FontCache::GetDefaultFontHeight(this->fs))) / 2;
104 const Sprite *SpriteFontCache::GetGlyph(GlyphID key)
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);
111 uint SpriteFontCache::GetGlyphWidth(GlyphID key)
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;
118 GlyphID SpriteFontCache::MapCharToGlyph(char32_t key, [[maybe_unused]] bool allow_fallback)
120 assert(IsPrintable(key));
121 SpriteID sprite = this->GetUnicodeGlyph(key);
122 if (sprite == 0) return 0;
123 return SPRITE_GLYPH | key;
126 bool SpriteFontCache::GetDrawGlyphShadow()
128 return false;