OpenTTD Source  13.2.1
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 
28 SpriteFontCache::SpriteFontCache(FontSize fs) : FontCache(fs), glyph_to_spriteid_map(nullptr)
29 {
31  this->height = ScaleGUITrad(FontCache::GetDefaultFontHeight(this->fs));
32  this->ascender = (this->height - ScaleSpriteTrad(FontCache::GetDefaultFontHeight(this->fs))) / 2;
33 }
34 
39 {
40  this->ClearGlyphToSpriteMap();
41 }
42 
43 SpriteID SpriteFontCache::GetUnicodeGlyph(WChar key)
44 {
45  if (this->glyph_to_spriteid_map[GB(key, 8, 8)] == nullptr) return 0;
46  return this->glyph_to_spriteid_map[GB(key, 8, 8)][GB(key, 0, 8)];
47 }
48 
50 {
51  if (this->glyph_to_spriteid_map == nullptr) this->glyph_to_spriteid_map = CallocT<SpriteID*>(256);
52  if (this->glyph_to_spriteid_map[GB(key, 8, 8)] == nullptr) this->glyph_to_spriteid_map[GB(key, 8, 8)] = CallocT<SpriteID>(256);
53  this->glyph_to_spriteid_map[GB(key, 8, 8)][GB(key, 0, 8)] = sprite;
54 }
55 
57 {
58  /* Clear out existing glyph map if it exists */
59  this->ClearGlyphToSpriteMap();
60 
61  SpriteID base;
62  switch (this->fs) {
63  default: NOT_REACHED();
64  case FS_MONO: // Use normal as default for mono spaced font
65  case FS_NORMAL: base = SPR_ASCII_SPACE; break;
66  case FS_SMALL: base = SPR_ASCII_SPACE_SMALL; break;
67  case FS_LARGE: base = SPR_ASCII_SPACE_BIG; break;
68  }
69 
70  for (uint i = ASCII_LETTERSTART; i < 256; i++) {
71  SpriteID sprite = base + i - ASCII_LETTERSTART;
72  if (!SpriteExists(sprite)) continue;
73  this->SetUnicodeGlyph(i, sprite);
74  this->SetUnicodeGlyph(i + SCC_SPRITE_START, sprite);
75  }
76 
77  for (uint i = 0; i < lengthof(_default_unicode_map); i++) {
78  byte key = _default_unicode_map[i].key;
79  if (key == CLRA) {
80  /* Clear the glyph. This happens if the glyph at this code point
81  * is non-standard and should be accessed by an SCC_xxx enum
82  * entry only. */
83  this->SetUnicodeGlyph(_default_unicode_map[i].code, 0);
84  } else {
85  SpriteID sprite = base + key - ASCII_LETTERSTART;
86  this->SetUnicodeGlyph(_default_unicode_map[i].code, sprite);
87  }
88  }
89 }
90 
95 {
96  if (this->glyph_to_spriteid_map == nullptr) return;
97 
98  for (uint i = 0; i < 256; i++) {
99  free(this->glyph_to_spriteid_map[i]);
100  }
102  this->glyph_to_spriteid_map = nullptr;
103 }
104 
106 {
108  this->height = ScaleGUITrad(FontCache::GetDefaultFontHeight(this->fs));
109  this->ascender = (this->height - ScaleSpriteTrad(FontCache::GetDefaultFontHeight(this->fs))) / 2;
110 }
111 
113 {
114  SpriteID sprite = this->GetUnicodeGlyph(key);
115  if (sprite == 0) sprite = this->GetUnicodeGlyph('?');
116  return GetSprite(sprite, ST_FONT);
117 }
118 
120 {
121  SpriteID sprite = this->GetUnicodeGlyph(key);
122  if (sprite == 0) sprite = this->GetUnicodeGlyph('?');
123  return SpriteExists(sprite) ? GetSprite(sprite, ST_FONT)->width + ScaleSpriteTrad(this->fs != FS_NORMAL ? 1 : 0) : 0;
124 }
125 
127 {
128  return false;
129 }
GlyphID
uint32 GlyphID
Glyphs are characters from a font.
Definition: fontcache.h:17
SpriteFontCache::InitializeUnicodeGlyphMap
virtual void InitializeUnicodeGlyphMap()
Initialize the glyph map.
Definition: spritefontcache.cpp:56
WChar
char32_t WChar
Type for wide characters, i.e.
Definition: string_type.h:36
GB
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
ST_FONT
@ ST_FONT
A sprite used for fonts.
Definition: gfx_type.h:310
SpriteFontCache::GetGlyphWidth
virtual uint GetGlyphWidth(GlyphID key)
Get the width of the glyph with the given key.
Definition: spritefontcache.cpp:119
spritefontcache.h
FontCache::height
int height
The height of the font.
Definition: fontcache.h:27
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:864
SpriteID
uint32 SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:17
FS_NORMAL
@ FS_NORMAL
Index of the normal font in the font tables.
Definition: gfx_type.h:203
FS_SMALL
@ FS_SMALL
Index of the small font in the font tables.
Definition: gfx_type.h:204
ScaleSpriteTrad
static int ScaleSpriteTrad(int value)
Scale traditional pixel dimensions to GUI zoom level, for drawing sprites.
Definition: zoom_func.h:107
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:26
SpriteFontCache::GetDrawGlyphShadow
virtual bool GetDrawGlyphShadow()
Do we need to draw a glyph shadow?
Definition: spritefontcache.cpp:126
CLRA
static const byte CLRA
Identifier to clear all glyphs at this codepoint.
Definition: unicode.h:15
FontCache
Font cache for basic fonts.
Definition: fontcache.h:21
SpriteFontCache::SetUnicodeGlyph
virtual void SetUnicodeGlyph(WChar key, SpriteID sprite)
Map a SpriteID to the key.
Definition: spritefontcache.cpp:49
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:28
SpriteFontCache::ClearFontCache
virtual void ClearFontCache()
Clear the font cache.
Definition: spritefontcache.cpp:105
SpriteFontCache::~SpriteFontCache
~SpriteFontCache()
Free everything we allocated.
Definition: spritefontcache.cpp:38
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:386
FS_MONO
@ FS_MONO
Index of the monospaced font in the font tables.
Definition: gfx_type.h:206
SpriteFontCache::ClearGlyphToSpriteMap
void ClearGlyphToSpriteMap()
Clear the glyph to sprite mapping.
Definition: spritefontcache.cpp:94
FontSize
FontSize
Available font sizes.
Definition: gfx_type.h:202
SpriteFontCache::GetGlyph
virtual const Sprite * GetGlyph(GlyphID key)
Get the glyph (sprite) of the given key.
Definition: spritefontcache.cpp:112
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:470
SpriteFontCache::SpriteFontCache
SpriteFontCache(FontSize fs)
Create a new sprite font cache.
Definition: spritefontcache.cpp:28
Sprite
Data structure describing a sprite.
Definition: spritecache.h:17
ScaleGUITrad
static RectPadding ScaleGUITrad(const RectPadding &r)
Scale a RectPadding to GUI zoom level.
Definition: widget.cpp:168