2009-08-21 21:21:05 +01:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 16:11:33 +01:00
|
|
|
/** @file fontcache.h Functions to read fonts from files and cache them. */
|
|
|
|
|
2006-11-16 22:05:33 +00:00
|
|
|
#ifndef FONTCACHE_H
|
|
|
|
#define FONTCACHE_H
|
|
|
|
|
2013-06-25 21:20:15 +01:00
|
|
|
#include "string_type.h"
|
2009-10-04 19:13:56 +01:00
|
|
|
#include "spritecache.h"
|
2007-12-22 23:30:28 +00:00
|
|
|
|
2013-06-25 21:20:15 +01:00
|
|
|
/** Glyphs are characters from a font. */
|
2023-05-08 18:01:06 +01:00
|
|
|
typedef uint32_t GlyphID;
|
2013-06-25 21:20:15 +01:00
|
|
|
static const GlyphID SPRITE_GLYPH = 1U << 30;
|
|
|
|
|
2013-06-23 16:24:36 +01:00
|
|
|
/** Font cache for basic fonts. */
|
|
|
|
class FontCache {
|
|
|
|
protected:
|
2023-11-02 11:15:41 +00:00
|
|
|
static FontCache *caches[FS_END]; ///< All the font caches.
|
2013-06-23 16:24:36 +01:00
|
|
|
FontCache *parent; ///< The parent of this font cache.
|
|
|
|
const FontSize fs; ///< The size of the font.
|
2013-06-25 21:21:21 +01:00
|
|
|
int height; ///< The height of the font.
|
|
|
|
int ascender; ///< The ascender value of the font.
|
|
|
|
int descender; ///< The descender value of the font.
|
2021-02-13 16:34:22 +00:00
|
|
|
|
2013-06-23 16:24:36 +01:00
|
|
|
public:
|
|
|
|
FontCache(FontSize fs);
|
|
|
|
virtual ~FontCache();
|
|
|
|
|
2023-05-17 14:54:14 +01:00
|
|
|
static void InitializeFontCaches();
|
|
|
|
|
2022-12-17 23:34:19 +00:00
|
|
|
static int GetDefaultFontHeight(FontSize fs);
|
|
|
|
|
2013-06-25 21:21:21 +01:00
|
|
|
/**
|
|
|
|
* Get the FontSize of the font.
|
|
|
|
* @return The FontSize.
|
|
|
|
*/
|
|
|
|
inline FontSize GetSize() const { return this->fs; }
|
|
|
|
|
2013-06-23 16:32:09 +01:00
|
|
|
/**
|
|
|
|
* Get the height of the font.
|
|
|
|
* @return The height of the font.
|
|
|
|
*/
|
2021-05-01 08:54:05 +01:00
|
|
|
inline int GetHeight() const { return this->height; }
|
2013-06-25 21:21:21 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the ascender value of the font.
|
|
|
|
* @return The ascender value of the font.
|
|
|
|
*/
|
|
|
|
inline int GetAscender() const { return this->ascender; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the descender value of the font.
|
|
|
|
* @return The descender value of the font.
|
|
|
|
*/
|
|
|
|
inline int GetDescender() const{ return this->descender; }
|
|
|
|
|
2018-10-28 22:30:49 +00:00
|
|
|
/**
|
|
|
|
* Get the nominal font size of the font.
|
|
|
|
* @return The nominal font size.
|
|
|
|
*/
|
|
|
|
virtual int GetFontSize() const { return this->height; }
|
|
|
|
|
2013-06-23 16:24:36 +01:00
|
|
|
/**
|
|
|
|
* Map a SpriteID to the key
|
|
|
|
* @param key The key to map to.
|
|
|
|
* @param sprite The sprite that is being mapped.
|
|
|
|
*/
|
2023-05-08 18:01:06 +01:00
|
|
|
virtual void SetUnicodeGlyph(char32_t key, SpriteID sprite) = 0;
|
2013-06-23 16:24:36 +01:00
|
|
|
|
|
|
|
/** Initialize the glyph map */
|
|
|
|
virtual void InitializeUnicodeGlyphMap() = 0;
|
|
|
|
|
|
|
|
/** Clear the font cache. */
|
|
|
|
virtual void ClearFontCache() = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the glyph (sprite) of the given key.
|
|
|
|
* @param key The key to look up.
|
|
|
|
* @return The sprite.
|
|
|
|
*/
|
2013-06-25 21:20:15 +01:00
|
|
|
virtual const Sprite *GetGlyph(GlyphID key) = 0;
|
2013-06-23 16:24:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the width of the glyph with the given key.
|
|
|
|
* @param key The key to look up.
|
|
|
|
* @return The width.
|
|
|
|
*/
|
2013-06-25 21:20:15 +01:00
|
|
|
virtual uint GetGlyphWidth(GlyphID key) = 0;
|
2013-06-23 16:24:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Do we need to draw a glyph shadow?
|
|
|
|
* @return True if it has to be done, otherwise false.
|
|
|
|
*/
|
|
|
|
virtual bool GetDrawGlyphShadow() = 0;
|
|
|
|
|
2013-06-25 21:20:15 +01:00
|
|
|
/**
|
|
|
|
* Map a character into a glyph.
|
|
|
|
* @param key The character.
|
2023-12-15 23:06:19 +00:00
|
|
|
* @param fallback Allow fallback to the parent font.
|
2013-06-25 21:20:15 +01:00
|
|
|
* @return The glyph ID used to draw the character.
|
|
|
|
*/
|
2023-12-15 23:06:19 +00:00
|
|
|
virtual GlyphID MapCharToGlyph(char32_t key, bool fallback = true) = 0;
|
2013-06-25 21:20:15 +01:00
|
|
|
|
2018-11-25 01:02:20 +00:00
|
|
|
/**
|
|
|
|
* Get the native OS font handle, if there is one.
|
|
|
|
* @return Opaque OS font handle.
|
|
|
|
*/
|
2021-01-06 21:56:44 +00:00
|
|
|
virtual const void *GetOSHandle()
|
2018-11-25 01:02:20 +00:00
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-12-22 17:46:27 +00:00
|
|
|
/**
|
|
|
|
* Get the name of this font.
|
|
|
|
* @return The name of the font.
|
|
|
|
*/
|
2023-05-26 19:32:41 +01:00
|
|
|
virtual std::string GetFontName() = 0;
|
2013-12-22 17:46:27 +00:00
|
|
|
|
2013-06-23 16:24:36 +01:00
|
|
|
/**
|
|
|
|
* Get the font cache of a given font size.
|
|
|
|
* @param fs The font size to look up.
|
|
|
|
* @return The font cache.
|
|
|
|
*/
|
|
|
|
static inline FontCache *Get(FontSize fs)
|
|
|
|
{
|
|
|
|
assert(fs < FS_END);
|
|
|
|
return FontCache::caches[fs];
|
|
|
|
}
|
|
|
|
|
2023-06-17 17:56:27 +01:00
|
|
|
static std::string GetName(FontSize fs);
|
|
|
|
|
2013-06-23 16:24:36 +01:00
|
|
|
/**
|
|
|
|
* Check whether the font cache has a parent.
|
|
|
|
*/
|
|
|
|
inline bool HasParent()
|
|
|
|
{
|
2019-04-10 22:07:06 +01:00
|
|
|
return this->parent != nullptr;
|
2013-06-23 16:24:36 +01:00
|
|
|
}
|
2018-05-26 15:13:12 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Is this a built-in sprite font?
|
|
|
|
*/
|
|
|
|
virtual bool IsBuiltInFont() = 0;
|
2013-06-23 16:24:36 +01:00
|
|
|
};
|
|
|
|
|
2006-11-16 22:05:33 +00:00
|
|
|
/** Map a SpriteID to the font size and key */
|
2024-01-06 11:19:27 +00:00
|
|
|
inline void SetUnicodeGlyph(FontSize size, char32_t key, SpriteID sprite)
|
2013-06-23 16:24:36 +01:00
|
|
|
{
|
|
|
|
FontCache::Get(size)->SetUnicodeGlyph(key, sprite);
|
|
|
|
}
|
2006-11-16 22:05:33 +00:00
|
|
|
|
|
|
|
/** Initialize the glyph map */
|
2024-01-06 11:19:27 +00:00
|
|
|
inline void InitializeUnicodeGlyphMap()
|
2013-06-23 16:24:36 +01:00
|
|
|
{
|
|
|
|
for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
|
|
|
|
FontCache::Get(fs)->InitializeUnicodeGlyphMap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-06 11:19:27 +00:00
|
|
|
inline void ClearFontCache()
|
2013-10-13 14:28:06 +01:00
|
|
|
{
|
2013-06-23 16:24:36 +01:00
|
|
|
for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
|
|
|
|
FontCache::Get(fs)->ClearFontCache();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Get the Sprite for a glyph */
|
2024-01-06 11:19:27 +00:00
|
|
|
inline const Sprite *GetGlyph(FontSize size, char32_t key)
|
2013-06-23 16:24:36 +01:00
|
|
|
{
|
2013-06-25 21:20:15 +01:00
|
|
|
FontCache *fc = FontCache::Get(size);
|
|
|
|
return fc->GetGlyph(fc->MapCharToGlyph(key));
|
2013-06-23 16:24:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Get the width of a glyph */
|
2024-01-06 11:19:27 +00:00
|
|
|
inline uint GetGlyphWidth(FontSize size, char32_t key)
|
2013-06-23 16:24:36 +01:00
|
|
|
{
|
2013-06-25 21:20:15 +01:00
|
|
|
FontCache *fc = FontCache::Get(size);
|
|
|
|
return fc->GetGlyphWidth(fc->MapCharToGlyph(key));
|
2013-06-23 16:24:36 +01:00
|
|
|
}
|
|
|
|
|
2024-01-06 11:19:27 +00:00
|
|
|
inline bool GetDrawGlyphShadow(FontSize size)
|
2013-06-23 16:24:36 +01:00
|
|
|
{
|
|
|
|
return FontCache::Get(size)->GetDrawGlyphShadow();
|
|
|
|
}
|
2006-11-16 22:05:33 +00:00
|
|
|
|
2022-09-15 18:21:27 +01:00
|
|
|
/** Settings for a single font. */
|
|
|
|
struct FontCacheSubSetting {
|
2021-04-28 16:10:15 +01:00
|
|
|
std::string font; ///< The name of the font, or path to the font.
|
|
|
|
uint size; ///< The (requested) size of the font.
|
2018-11-25 01:02:20 +00:00
|
|
|
|
2021-01-04 14:20:34 +00:00
|
|
|
const void *os_handle = nullptr; ///< Optional native OS font info. Only valid during font search.
|
2013-06-23 16:23:22 +01:00
|
|
|
};
|
|
|
|
|
2022-09-15 18:21:27 +01:00
|
|
|
/** Settings for the four different fonts. */
|
|
|
|
struct FontCacheSettings {
|
|
|
|
FontCacheSubSetting small; ///< The smallest font; mostly used for zoomed out view.
|
|
|
|
FontCacheSubSetting medium; ///< The normal font size.
|
|
|
|
FontCacheSubSetting large; ///< The largest font; mostly used for newspapers.
|
|
|
|
FontCacheSubSetting mono; ///< The mono space font used for license/readme viewers.
|
2023-12-16 17:28:49 +00:00
|
|
|
bool prefer_sprite; ///< Whether to prefer the built-in sprite font over resizable fonts.
|
|
|
|
bool global_aa; ///< Whether to anti alias all font sizes.
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2006-11-16 22:05:33 +00:00
|
|
|
|
2022-09-15 18:21:27 +01:00
|
|
|
extern FontCacheSettings _fcsettings;
|
2006-11-16 22:05:33 +00:00
|
|
|
|
2022-12-22 18:41:58 +00:00
|
|
|
/**
|
|
|
|
* Get the settings of a given font size.
|
|
|
|
* @param fs The font size to look up.
|
|
|
|
* @return The settings.
|
|
|
|
*/
|
2024-01-06 11:19:27 +00:00
|
|
|
inline FontCacheSubSetting *GetFontCacheSubSetting(FontSize fs)
|
2022-12-22 18:41:58 +00:00
|
|
|
{
|
|
|
|
switch (fs) {
|
|
|
|
default: NOT_REACHED();
|
|
|
|
case FS_SMALL: return &_fcsettings.small;
|
|
|
|
case FS_NORMAL: return &_fcsettings.medium;
|
|
|
|
case FS_LARGE: return &_fcsettings.large;
|
|
|
|
case FS_MONO: return &_fcsettings.mono;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-26 12:45:39 +01:00
|
|
|
uint GetFontCacheFontSize(FontSize fs);
|
2024-05-26 15:59:25 +01:00
|
|
|
std::string GetFontCacheFontName(FontSize fs);
|
2022-09-15 18:21:27 +01:00
|
|
|
void InitFontCache(bool monospace);
|
|
|
|
void UninitFontCache();
|
2006-11-16 22:05:33 +00:00
|
|
|
|
2024-04-15 20:44:33 +01:00
|
|
|
bool GetFontAAState();
|
|
|
|
void SetFont(FontSize fontsize, const std::string &font, uint size);
|
2022-09-15 19:57:10 +01:00
|
|
|
|
2006-11-16 22:05:33 +00:00
|
|
|
#endif /* FONTCACHE_H */
|