OpenTTD Source  13.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 
28 SpriteFontCache::SpriteFontCache(FontSize fs) : FontCache(fs), glyph_to_spriteid_map(nullptr)
29 {
31  this->height = ScaleFontTrad(this->GetDefaultFontHeight(this->fs));
32 }
33 
38 {
39  this->ClearGlyphToSpriteMap();
40 }
41 
42 SpriteID SpriteFontCache::GetUnicodeGlyph(WChar key)
43 {
44  if (this->glyph_to_spriteid_map[GB(key, 8, 8)] == nullptr) return 0;
45  return this->glyph_to_spriteid_map[GB(key, 8, 8)][GB(key, 0, 8)];
46 }
47 
49 {
50  if (this->glyph_to_spriteid_map == nullptr) this->glyph_to_spriteid_map = CallocT<SpriteID*>(256);
51  if (this->glyph_to_spriteid_map[GB(key, 8, 8)] == nullptr) this->glyph_to_spriteid_map[GB(key, 8, 8)] = CallocT<SpriteID>(256);
52  this->glyph_to_spriteid_map[GB(key, 8, 8)][GB(key, 0, 8)] = sprite;
53 }
54 
56 {
57  /* Clear out existing glyph map if it exists */
58  this->ClearGlyphToSpriteMap();
59 
60  SpriteID base;
61  switch (this->fs) {
62  default: NOT_REACHED();
63  case FS_MONO: // Use normal as default for mono spaced font
64  case FS_NORMAL: base = SPR_ASCII_SPACE; break;
65  case FS_SMALL: base = SPR_ASCII_SPACE_SMALL; break;
66  case FS_LARGE: base = SPR_ASCII_SPACE_BIG; break;
67  }
68 
69  for (uint i = ASCII_LETTERSTART; i < 256; i++) {
70  SpriteID sprite = base + i - ASCII_LETTERSTART;
71  if (!SpriteExists(sprite)) continue;
72  this->SetUnicodeGlyph(i, sprite);
73  this->SetUnicodeGlyph(i + SCC_SPRITE_START, sprite);
74  }
75 
76  for (uint i = 0; i < lengthof(_default_unicode_map); i++) {
77  byte key = _default_unicode_map[i].key;
78  if (key == CLRA) {
79  /* Clear the glyph. This happens if the glyph at this code point
80  * is non-standard and should be accessed by an SCC_xxx enum
81  * entry only. */
82  this->SetUnicodeGlyph(_default_unicode_map[i].code, 0);
83  } else {
84  SpriteID sprite = base + key - ASCII_LETTERSTART;
85  this->SetUnicodeGlyph(_default_unicode_map[i].code, sprite);
86  }
87  }
88 }
89 
94 {
95  if (this->glyph_to_spriteid_map == nullptr) return;
96 
97  for (uint i = 0; i < 256; i++) {
98  free(this->glyph_to_spriteid_map[i]);
99  }
101  this->glyph_to_spriteid_map = nullptr;
102 }
103 
105 {
107  this->height = ScaleFontTrad(this->GetDefaultFontHeight(this->fs));
108 }
109 
111 {
112  SpriteID sprite = this->GetUnicodeGlyph(key);
113  if (sprite == 0) sprite = this->GetUnicodeGlyph('?');
114  return GetSprite(sprite, ST_FONT);
115 }
116 
118 {
119  SpriteID sprite = this->GetUnicodeGlyph(key);
120  if (sprite == 0) sprite = this->GetUnicodeGlyph('?');
121  return SpriteExists(sprite) ? GetSprite(sprite, ST_FONT)->width + ScaleFontTrad(this->fs != FS_NORMAL ? 1 : 0) : 0;
122 }
123 
125 {
126  return false;
127 }
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:55
WChar
char32_t WChar
Type for wide characters, i.e.
Definition: string_type.h:35
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
ScaleFontTrad
static int ScaleFontTrad(int value)
Scale traditional pixel dimensions to Font zoom level.
Definition: zoom_func.h:127
ST_FONT
@ ST_FONT
A sprite used for fonts.
Definition: gfx_type.h:304
SpriteFontCache::GetGlyphWidth
virtual uint GetGlyphWidth(GlyphID key)
Get the width of the glyph with the given key.
Definition: spritefontcache.cpp:117
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:209
Layouter::ResetFontCache
static void ResetFontCache(FontSize size)
Reset cached font information.
Definition: gfx_layout.cpp:858
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:207
FS_SMALL
@ FS_SMALL
Index of the small font in the font tables.
Definition: gfx_type.h:208
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:124
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:48
ASCII_LETTERSTART
static const int ASCII_LETTERSTART
First printable ASCII letter.
Definition: spritefontcache.cpp:22
SpriteFontCache::ClearFontCache
virtual void ClearFontCache()
Clear the font cache.
Definition: spritefontcache.cpp:104
SpriteFontCache::~SpriteFontCache
~SpriteFontCache()
Free everything we allocated.
Definition: spritefontcache.cpp:37
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:210
SpriteFontCache::ClearGlyphToSpriteMap
void ClearGlyphToSpriteMap()
Clear the glyph to sprite mapping.
Definition: spritefontcache.cpp:93
FontSize
FontSize
Available font sizes.
Definition: gfx_type.h:206
SpriteFontCache::GetGlyph
virtual const Sprite * GetGlyph(GlyphID key)
Get the glyph (sprite) of the given key.
Definition: spritefontcache.cpp:110
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