mirror of https://github.com/OpenTTD/OpenTTD
Codechange: Decouple glyph map from SpriteFontCache instances.
This makes the map independent from the SpriteFontCache instances.
parent
03f5f7145f
commit
5d94442b87
|
@ -70,16 +70,6 @@ public:
|
||||||
*/
|
*/
|
||||||
virtual int GetFontSize() const { return this->height; }
|
virtual int GetFontSize() const { return this->height; }
|
||||||
|
|
||||||
/**
|
|
||||||
* Map a SpriteID to the key
|
|
||||||
* @param key The key to map to.
|
|
||||||
* @param sprite The sprite that is being mapped.
|
|
||||||
*/
|
|
||||||
virtual void SetUnicodeGlyph(char32_t key, SpriteID sprite) = 0;
|
|
||||||
|
|
||||||
/** Initialize the glyph map */
|
|
||||||
virtual void InitializeUnicodeGlyphMap() = 0;
|
|
||||||
|
|
||||||
/** Clear the font cache. */
|
/** Clear the font cache. */
|
||||||
virtual void ClearFontCache() = 0;
|
virtual void ClearFontCache() = 0;
|
||||||
|
|
||||||
|
@ -153,20 +143,6 @@ public:
|
||||||
virtual bool IsBuiltInFont() = 0;
|
virtual bool IsBuiltInFont() = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Map a SpriteID to the font size and key */
|
|
||||||
inline void SetUnicodeGlyph(FontSize size, char32_t key, SpriteID sprite)
|
|
||||||
{
|
|
||||||
FontCache::Get(size)->SetUnicodeGlyph(key, sprite);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Initialize the glyph map */
|
|
||||||
inline void InitializeUnicodeGlyphMap()
|
|
||||||
{
|
|
||||||
for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
|
|
||||||
FontCache::Get(fs)->InitializeUnicodeGlyphMap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void ClearFontCache(FontSizes fontsizes)
|
inline void ClearFontCache(FontSizes fontsizes)
|
||||||
{
|
{
|
||||||
for (FontSize fs : fontsizes) {
|
for (FontSize fs : fontsizes) {
|
||||||
|
@ -237,4 +213,8 @@ void UninitFontCache();
|
||||||
bool GetFontAAState();
|
bool GetFontAAState();
|
||||||
void SetFont(FontSize fontsize, const std::string &font, uint size);
|
void SetFont(FontSize fontsize, const std::string &font, uint size);
|
||||||
|
|
||||||
|
/* Implemented in spritefontcache.cpp */
|
||||||
|
void InitializeUnicodeGlyphMap();
|
||||||
|
void SetUnicodeGlyph(FontSize size, char32_t key, SpriteID sprite);
|
||||||
|
|
||||||
#endif /* FONTCACHE_H */
|
#endif /* FONTCACHE_H */
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
#include "../stdafx.h"
|
#include "../stdafx.h"
|
||||||
#include "../fontcache.h"
|
#include "../fontcache.h"
|
||||||
#include "../gfx_layout.h"
|
#include "../gfx_layout.h"
|
||||||
|
#include "../string_func.h"
|
||||||
#include "../zoom_func.h"
|
#include "../zoom_func.h"
|
||||||
#include "spritefontcache.h"
|
#include "spritefontcache.h"
|
||||||
|
|
||||||
|
@ -31,41 +32,43 @@ static int ScaleFontTrad(int value)
|
||||||
return UnScaleByZoom(value * ZOOM_BASE, _font_zoom);
|
return UnScaleByZoom(value * ZOOM_BASE, _font_zoom);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
static std::array<std::unordered_map<char32_t, SpriteID>, FS_END> _char_maps{}; ///< Glyph map for each font size.
|
||||||
* Create a new sprite font cache.
|
|
||||||
* @param fs The font size to create the cache for.
|
|
||||||
*/
|
|
||||||
SpriteFontCache::SpriteFontCache(FontSize fs) : FontCache(fs)
|
|
||||||
{
|
|
||||||
this->InitializeUnicodeGlyphMap();
|
|
||||||
this->height = ScaleGUITrad(FontCache::GetDefaultFontHeight(this->fs));
|
|
||||||
this->ascender = (this->height - ScaleFontTrad(FontCache::GetDefaultFontHeight(this->fs))) / 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get SpriteID associated with a character.
|
* Get SpriteID associated with a character.
|
||||||
* @param key Character to find.
|
* @param key Character to find.
|
||||||
* @return SpriteID for character, or 0 if not present.
|
* @return SpriteID for character, or 0 if not present.
|
||||||
*/
|
*/
|
||||||
SpriteID SpriteFontCache::GetUnicodeGlyph(char32_t key)
|
static SpriteID GetUnicodeGlyph(FontSize fs, char32_t key)
|
||||||
{
|
{
|
||||||
const auto found = this->char_map.find(key);
|
auto found = _char_maps[fs].find(key);
|
||||||
if (found == std::end(this->char_map)) return 0;
|
if (found != std::end(_char_maps[fs])) return found->second;
|
||||||
return found->second;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SpriteFontCache::SetUnicodeGlyph(char32_t key, SpriteID sprite)
|
/**
|
||||||
|
* Set the SpriteID for a unicode character.
|
||||||
|
* @param fs Font size to set.
|
||||||
|
* @param key Unicode character to set.
|
||||||
|
* @param sprite SpriteID of character.
|
||||||
|
*/
|
||||||
|
void SetUnicodeGlyph(FontSize fs, char32_t key, SpriteID sprite)
|
||||||
{
|
{
|
||||||
this->char_map[key] = sprite;
|
_char_maps[fs][key] = sprite;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SpriteFontCache::InitializeUnicodeGlyphMap()
|
/**
|
||||||
|
* Initialize the glyph map for a font size.
|
||||||
|
* This populates the glyph map with the baseset font sprites.
|
||||||
|
* @param fs Font size to initialize.
|
||||||
|
*/
|
||||||
|
void InitializeUnicodeGlyphMap(FontSize fs)
|
||||||
{
|
{
|
||||||
/* Clear out existing glyph map if it exists */
|
/* Clear out existing glyph map if it exists */
|
||||||
this->char_map.clear();
|
_char_maps[fs].clear();
|
||||||
|
|
||||||
SpriteID base;
|
SpriteID base;
|
||||||
switch (this->fs) {
|
switch (fs) {
|
||||||
default: NOT_REACHED();
|
default: NOT_REACHED();
|
||||||
case FS_MONO: // Use normal as default for mono spaced font
|
case FS_MONO: // Use normal as default for mono spaced font
|
||||||
case FS_NORMAL: base = SPR_ASCII_SPACE; break;
|
case FS_NORMAL: base = SPR_ASCII_SPACE; break;
|
||||||
|
@ -76,24 +79,45 @@ void SpriteFontCache::InitializeUnicodeGlyphMap()
|
||||||
for (uint i = ASCII_LETTERSTART; i < 256; i++) {
|
for (uint i = ASCII_LETTERSTART; i < 256; i++) {
|
||||||
SpriteID sprite = base + i - ASCII_LETTERSTART;
|
SpriteID sprite = base + i - ASCII_LETTERSTART;
|
||||||
if (!SpriteExists(sprite)) continue;
|
if (!SpriteExists(sprite)) continue;
|
||||||
this->SetUnicodeGlyph(i, sprite);
|
SetUnicodeGlyph(fs, i, sprite);
|
||||||
this->SetUnicodeGlyph(i + SCC_SPRITE_START, sprite);
|
SetUnicodeGlyph(fs, i + SCC_SPRITE_START, sprite);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Modify map to move non-standard glyphs to a better unicode codepoint. */
|
||||||
for (const auto &unicode_map : _default_unicode_map) {
|
for (const auto &unicode_map : _default_unicode_map) {
|
||||||
uint8_t key = unicode_map.key;
|
uint8_t key = unicode_map.key;
|
||||||
if (key == CLRA) {
|
if (key == CLRA) {
|
||||||
/* Clear the glyph. This happens if the glyph at this code point
|
/* Clear the glyph. This happens if the glyph at this code point
|
||||||
* is non-standard and should be accessed by an SCC_xxx enum
|
* is non-standard and should be accessed by an SCC_xxx enum
|
||||||
* entry only. */
|
* entry only. */
|
||||||
this->SetUnicodeGlyph(unicode_map.code, 0);
|
SetUnicodeGlyph(fs, unicode_map.code, 0);
|
||||||
} else {
|
} else {
|
||||||
SpriteID sprite = base + key - ASCII_LETTERSTART;
|
SpriteID sprite = base + key - ASCII_LETTERSTART;
|
||||||
this->SetUnicodeGlyph(unicode_map.code, sprite);
|
SetUnicodeGlyph(fs, unicode_map.code, sprite);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize the glyph map.
|
||||||
|
*/
|
||||||
|
void InitializeUnicodeGlyphMap()
|
||||||
|
{
|
||||||
|
for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
|
||||||
|
InitializeUnicodeGlyphMap(fs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new sprite font cache.
|
||||||
|
* @param fs The font size to create the cache for.
|
||||||
|
*/
|
||||||
|
SpriteFontCache::SpriteFontCache(FontSize fs) : FontCache(fs)
|
||||||
|
{
|
||||||
|
this->height = ScaleGUITrad(FontCache::GetDefaultFontHeight(this->fs));
|
||||||
|
this->ascender = (this->height - ScaleFontTrad(FontCache::GetDefaultFontHeight(this->fs))) / 2;
|
||||||
|
}
|
||||||
|
|
||||||
void SpriteFontCache::ClearFontCache()
|
void SpriteFontCache::ClearFontCache()
|
||||||
{
|
{
|
||||||
Layouter::ResetFontCache(this->fs);
|
Layouter::ResetFontCache(this->fs);
|
||||||
|
@ -104,21 +128,21 @@ void SpriteFontCache::ClearFontCache()
|
||||||
const Sprite *SpriteFontCache::GetGlyph(GlyphID key)
|
const Sprite *SpriteFontCache::GetGlyph(GlyphID key)
|
||||||
{
|
{
|
||||||
SpriteID sprite = static_cast<SpriteID>(key & ~SPRITE_GLYPH);
|
SpriteID sprite = static_cast<SpriteID>(key & ~SPRITE_GLYPH);
|
||||||
if (sprite == 0) sprite = this->GetUnicodeGlyph('?');
|
if (sprite == 0) sprite = GetUnicodeGlyph(this->fs, '?');
|
||||||
return GetSprite(sprite, SpriteType::Font);
|
return GetSprite(sprite, SpriteType::Font);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint SpriteFontCache::GetGlyphWidth(GlyphID key)
|
uint SpriteFontCache::GetGlyphWidth(GlyphID key)
|
||||||
{
|
{
|
||||||
SpriteID sprite = static_cast<SpriteID>(key & ~SPRITE_GLYPH);
|
SpriteID sprite = static_cast<SpriteID>(key & ~SPRITE_GLYPH);
|
||||||
if (sprite == 0) sprite = this->GetUnicodeGlyph('?');
|
if (sprite == 0) sprite = GetUnicodeGlyph(this->fs, '?');
|
||||||
return SpriteExists(sprite) ? GetSprite(sprite, SpriteType::Font)->width + ScaleFontTrad(this->fs != FS_NORMAL ? 1 : 0) : 0;
|
return SpriteExists(sprite) ? GetSprite(sprite, SpriteType::Font)->width + ScaleFontTrad(this->fs != FS_NORMAL ? 1 : 0) : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
GlyphID SpriteFontCache::MapCharToGlyph(char32_t key, [[maybe_unused]] bool allow_fallback)
|
GlyphID SpriteFontCache::MapCharToGlyph(char32_t key, [[maybe_unused]] bool allow_fallback)
|
||||||
{
|
{
|
||||||
assert(IsPrintable(key));
|
assert(IsPrintable(key));
|
||||||
SpriteID sprite = this->GetUnicodeGlyph(key);
|
SpriteID sprite = GetUnicodeGlyph(this->fs, key);
|
||||||
if (sprite == 0) return 0;
|
if (sprite == 0) return 0;
|
||||||
return SPRITE_GLYPH | sprite;
|
return SPRITE_GLYPH | sprite;
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,15 +10,12 @@
|
||||||
#ifndef SPRITEFONTCACHE_H
|
#ifndef SPRITEFONTCACHE_H
|
||||||
#define SPRITEFONTCACHE_H
|
#define SPRITEFONTCACHE_H
|
||||||
|
|
||||||
#include "../string_func.h"
|
|
||||||
#include "../fontcache.h"
|
#include "../fontcache.h"
|
||||||
|
|
||||||
/** Font cache for fonts that are based on a freetype font. */
|
/** Font cache for fonts that are based on a freetype font. */
|
||||||
class SpriteFontCache : public FontCache {
|
class SpriteFontCache : public FontCache {
|
||||||
public:
|
public:
|
||||||
SpriteFontCache(FontSize fs);
|
SpriteFontCache(FontSize fs);
|
||||||
void SetUnicodeGlyph(char32_t key, SpriteID sprite) override;
|
|
||||||
void InitializeUnicodeGlyphMap() override;
|
|
||||||
void ClearFontCache() override;
|
void ClearFontCache() override;
|
||||||
const Sprite *GetGlyph(GlyphID key) override;
|
const Sprite *GetGlyph(GlyphID key) override;
|
||||||
uint GetGlyphWidth(GlyphID key) override;
|
uint GetGlyphWidth(GlyphID key) override;
|
||||||
|
@ -26,10 +23,6 @@ public:
|
||||||
GlyphID MapCharToGlyph(char32_t key, bool allow_fallback = true) override;
|
GlyphID MapCharToGlyph(char32_t key, bool allow_fallback = true) override;
|
||||||
std::string GetFontName() override { return "sprite"; }
|
std::string GetFontName() override { return "sprite"; }
|
||||||
bool IsBuiltInFont() override { return true; }
|
bool IsBuiltInFont() override { return true; }
|
||||||
|
|
||||||
private:
|
|
||||||
std::unordered_map<char32_t, SpriteID> char_map{}; ///< Mapping of characters to sprite IDs.
|
|
||||||
SpriteID GetUnicodeGlyph(char32_t key);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* SPRITEFONTCACHE_H */
|
#endif /* SPRITEFONTCACHE_H */
|
||||||
|
|
|
@ -46,8 +46,6 @@ public:
|
||||||
TrueTypeFontCache(FontSize fs, int pixels);
|
TrueTypeFontCache(FontSize fs, int pixels);
|
||||||
virtual ~TrueTypeFontCache();
|
virtual ~TrueTypeFontCache();
|
||||||
int GetFontSize() const override { return this->used_size; }
|
int GetFontSize() const override { return this->used_size; }
|
||||||
void SetUnicodeGlyph(char32_t key, SpriteID sprite) override { this->parent->SetUnicodeGlyph(key, sprite); }
|
|
||||||
void InitializeUnicodeGlyphMap() override { this->parent->InitializeUnicodeGlyphMap(); }
|
|
||||||
const Sprite *GetGlyph(GlyphID key) override;
|
const Sprite *GetGlyph(GlyphID key) override;
|
||||||
void ClearFontCache() override;
|
void ClearFontCache() override;
|
||||||
uint GetGlyphWidth(GlyphID key) override;
|
uint GetGlyphWidth(GlyphID key) override;
|
||||||
|
|
|
@ -21,8 +21,6 @@ public:
|
||||||
this->height = FontCache::GetDefaultFontHeight(this->fs);
|
this->height = FontCache::GetDefaultFontHeight(this->fs);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SetUnicodeGlyph(char32_t, SpriteID) override {}
|
|
||||||
void InitializeUnicodeGlyphMap() override {}
|
|
||||||
void ClearFontCache() override {}
|
void ClearFontCache() override {}
|
||||||
const Sprite *GetGlyph(GlyphID) override { return nullptr; }
|
const Sprite *GetGlyph(GlyphID) override { return nullptr; }
|
||||||
uint GetGlyphWidth(GlyphID) override { return this->height / 2; }
|
uint GetGlyphWidth(GlyphID) override { return this->height / 2; }
|
||||||
|
|
Loading…
Reference in New Issue