OpenTTD Source  14.0-beta3
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_LVL_BASE, _font_zoom);
32 }
33 
38 SpriteFontCache::SpriteFontCache(FontSize fs) : FontCache(fs), glyph_to_spriteid_map(nullptr)
39 {
41  this->height = ScaleGUITrad(FontCache::GetDefaultFontHeight(this->fs));
42  this->ascender = (this->height - ScaleFontTrad(FontCache::GetDefaultFontHeight(this->fs))) / 2;
43 }
44 
49 {
50  this->ClearGlyphToSpriteMap();
51 }
52 
53 SpriteID SpriteFontCache::GetUnicodeGlyph(char32_t key)
54 {
55  if (this->glyph_to_spriteid_map[GB(key, 8, 8)] == nullptr) return 0;
56  return this->glyph_to_spriteid_map[GB(key, 8, 8)][GB(key, 0, 8)];
57 }
58 
59 void SpriteFontCache::SetUnicodeGlyph(char32_t key, SpriteID sprite)
60 {
61  if (this->glyph_to_spriteid_map == nullptr) this->glyph_to_spriteid_map = CallocT<SpriteID*>(256);
62  if (this->glyph_to_spriteid_map[GB(key, 8, 8)] == nullptr) this->glyph_to_spriteid_map[GB(key, 8, 8)] = CallocT<SpriteID>(256);
63  this->glyph_to_spriteid_map[GB(key, 8, 8)][GB(key, 0, 8)] = sprite;
64 }
65 
67 {
68  /* Clear out existing glyph map if it exists */
69  this->ClearGlyphToSpriteMap();
70 
71  SpriteID base;
72  switch (this->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  this->SetUnicodeGlyph(i, sprite);
84  this->SetUnicodeGlyph(i + SCC_SPRITE_START, sprite);
85  }
86 
87  for (uint i = 0; i < lengthof(_default_unicode_map); i++) {
88  byte key = _default_unicode_map[i].key;
89  if (key == CLRA) {
90  /* Clear the glyph. This happens if the glyph at this code point
91  * is non-standard and should be accessed by an SCC_xxx enum
92  * entry only. */
93  this->SetUnicodeGlyph(_default_unicode_map[i].code, 0);
94  } else {
95  SpriteID sprite = base + key - ASCII_LETTERSTART;
96  this->SetUnicodeGlyph(_default_unicode_map[i].code, sprite);
97  }
98  }
99 }
100 
105 {
106  if (this->glyph_to_spriteid_map == nullptr) return;
107 
108  for (uint i = 0; i < 256; i++) {
109  free(this->glyph_to_spriteid_map[i]);
110  }
112  this->glyph_to_spriteid_map = nullptr;
113 }
114 
116 {
118  this->height = ScaleGUITrad(FontCache::GetDefaultFontHeight(this->fs));
119  this->ascender = (this->height - ScaleFontTrad(FontCache::GetDefaultFontHeight(this->fs))) / 2;
120 }
121 
123 {
124  SpriteID sprite = this->GetUnicodeGlyph(key);
125  if (sprite == 0) sprite = this->GetUnicodeGlyph('?');
126  return GetSprite(sprite, SpriteType::Font);
127 }
128 
130 {
131  SpriteID sprite = this->GetUnicodeGlyph(key);
132  if (sprite == 0) sprite = this->GetUnicodeGlyph('?');
133  return SpriteExists(sprite) ? GetSprite(sprite, SpriteType::Font)->width + ScaleFontTrad(this->fs != FS_NORMAL ? 1 : 0) : 0;
134 }
135 
137 {
138  return false;
139 }
SpriteFontCache::GetGlyph
const Sprite * GetGlyph(GlyphID key) override
Get the glyph (sprite) of the given key.
Definition: spritefontcache.cpp:122
spritefontcache.h
FontCache::height
int height
The height of the font.
Definition: fontcache.h:26
SpriteFontCache::GetGlyphWidth
uint GetGlyphWidth(GlyphID key) override
Get the width of the glyph with the given key.
Definition: spritefontcache.cpp:129
GB
constexpr static debug_inline uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
SpriteFontCache::InitializeUnicodeGlyphMap
void InitializeUnicodeGlyphMap() override
Initialize the glyph map.
Definition: spritefontcache.cpp:66
FS_LARGE
@ FS_LARGE
Index of the large font in the font tables.
Definition: gfx_type.h:205
Layouter::ResetFontCache
static void ResetFontCache(FontSize size)
Reset cached font information.
Definition: gfx_layout.cpp:351
SpriteType::Font
@ Font
A sprite used for fonts.
ScaleGUITrad
int ScaleGUITrad(int value)
Scale traditional pixel dimensions to GUI zoom level.
Definition: zoom_func.h:117
SpriteFontCache::SetUnicodeGlyph
void SetUnicodeGlyph(char32_t key, SpriteID sprite) override
Map a SpriteID to the key.
Definition: spritefontcache.cpp:59
FS_NORMAL
@ FS_NORMAL
Index of the normal font in the font tables.
Definition: gfx_type.h:203
free
void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:379
FS_SMALL
@ FS_SMALL
Index of the small font in the font tables.
Definition: gfx_type.h:204
Sprite::width
uint16_t width
Width of the sprite.
Definition: spritecache.h:19
SpriteFontCache::GetDrawGlyphShadow
bool GetDrawGlyphShadow() override
Do we need to draw a glyph shadow?
Definition: spritefontcache.cpp:136
GlyphID
uint32_t GlyphID
Glyphs are characters from a font.
Definition: fontcache.h:17
SpriteFontCache::glyph_to_spriteid_map
SpriteID ** glyph_to_spriteid_map
Mapping of glyphs to sprite IDs.
Definition: spritefontcache.h:19
FontCache::fs
const FontSize fs
The size of the font.
Definition: fontcache.h:25
SpriteID
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:17
CLRA
static const byte CLRA
Identifier to clear all glyphs at this codepoint.
Definition: unicode.h:15
_font_zoom
ZoomLevel _font_zoom
Sprite font Zoom level (not clamped)
Definition: gfx.cpp:61
SpriteFontCache::ClearFontCache
void ClearFontCache() override
Clear the font cache.
Definition: spritefontcache.cpp:115
UnScaleByZoom
int UnScaleByZoom(int value, ZoomLevel zoom)
Scale by zoom level, usually shift right (when zoom > ZOOM_LVL_NORMAL) When shifting right,...
Definition: zoom_func.h:34
FontCache
Font cache for basic fonts.
Definition: fontcache.h:21
ASCII_LETTERSTART
static const int ASCII_LETTERSTART
First printable ASCII letter.
Definition: spritefontcache.cpp:22
FontCache::ascender
int ascender
The ascender value of the font.
Definition: fontcache.h:27
SpriteFontCache::~SpriteFontCache
~SpriteFontCache()
Free everything we allocated.
Definition: spritefontcache.cpp:48
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:300
FS_MONO
@ FS_MONO
Index of the monospaced font in the font tables.
Definition: gfx_type.h:206
ScaleFontTrad
static int ScaleFontTrad(int value)
Scale traditional pixel dimensions to font zoom level, for drawing sprite fonts.
Definition: spritefontcache.cpp:29
SpriteFontCache::ClearGlyphToSpriteMap
void ClearGlyphToSpriteMap()
Clear the glyph to sprite mapping.
Definition: spritefontcache.cpp:104
FontSize
FontSize
Available font sizes.
Definition: gfx_type.h:202
SpriteFontCache::SpriteFontCache
SpriteFontCache(FontSize fs)
Create a new sprite font cache.
Definition: spritefontcache.cpp:38
Sprite
Data structure describing a sprite.
Definition: spritecache.h:17