2006-11-16 22:05:33 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00: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 15:11:33 +00: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 20:20:15 +00:00
|
|
|
#include "string_type.h"
|
2009-10-04 18:13:56 +00:00
|
|
|
#include "spritecache.h"
|
2007-12-22 23:30:28 +00:00
|
|
|
|
2013-06-25 20:20:15 +00:00
|
|
|
/** Glyphs are characters from a font. */
|
|
|
|
typedef uint32 GlyphID;
|
|
|
|
static const GlyphID SPRITE_GLYPH = 1U << 30;
|
|
|
|
|
2016-01-01 21:47:34 +00:00
|
|
|
extern int font_height_cache[FS_END]; ///< Cache of font heights
|
|
|
|
|
2016-01-03 14:52:30 +00:00
|
|
|
void UpdateFontHeightCache();
|
|
|
|
|
2013-06-23 15:24:36 +00:00
|
|
|
/** Font cache for basic fonts. */
|
|
|
|
class FontCache {
|
|
|
|
private:
|
|
|
|
static FontCache *caches[FS_END]; ///< All the font caches.
|
|
|
|
protected:
|
|
|
|
FontCache *parent; ///< The parent of this font cache.
|
|
|
|
const FontSize fs; ///< The size of the font.
|
2013-06-25 20:21:21 +00:00
|
|
|
int height; ///< The height of the font.
|
|
|
|
int ascender; ///< The ascender value of the font.
|
|
|
|
int descender; ///< The descender value of the font.
|
|
|
|
int units_per_em; ///< The units per EM value of the font.
|
2013-06-23 15:24:36 +00:00
|
|
|
public:
|
|
|
|
FontCache(FontSize fs);
|
|
|
|
virtual ~FontCache();
|
|
|
|
|
2013-06-25 20:21:21 +00:00
|
|
|
/**
|
|
|
|
* Get the FontSize of the font.
|
|
|
|
* @return The FontSize.
|
|
|
|
*/
|
|
|
|
inline FontSize GetSize() const { return this->fs; }
|
|
|
|
|
2013-06-23 15:32:09 +00:00
|
|
|
/**
|
|
|
|
* Get the height of the font.
|
|
|
|
* @return The height of the font.
|
|
|
|
*/
|
2014-10-12 20:43:25 +00:00
|
|
|
virtual int GetHeight() const { return this->height; }
|
2013-06-25 20:21:21 +00: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; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the units per EM value of the font.
|
|
|
|
* @return The units per EM value of the font.
|
|
|
|
*/
|
|
|
|
inline int GetUnitsPerEM() const { return this->units_per_em; }
|
2013-06-23 15:32:09 +00:00
|
|
|
|
2013-06-23 15:24:36 +00:00
|
|
|
/**
|
|
|
|
* Get the SpriteID mapped to the given key
|
|
|
|
* @param key The key to get the sprite for.
|
|
|
|
* @return The sprite.
|
|
|
|
*/
|
2013-06-25 20:20:15 +00:00
|
|
|
virtual SpriteID GetUnicodeGlyph(WChar key) = 0;
|
2013-06-23 15:24:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Map a SpriteID to the key
|
|
|
|
* @param key The key to map to.
|
|
|
|
* @param sprite The sprite that is being mapped.
|
|
|
|
*/
|
2013-06-25 20:20:15 +00:00
|
|
|
virtual void SetUnicodeGlyph(WChar key, SpriteID sprite) = 0;
|
2013-06-23 15:24:36 +00: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 20:20:15 +00:00
|
|
|
virtual const Sprite *GetGlyph(GlyphID key) = 0;
|
2013-06-23 15:24:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the width of the glyph with the given key.
|
|
|
|
* @param key The key to look up.
|
|
|
|
* @return The width.
|
|
|
|
*/
|
2013-06-25 20:20:15 +00:00
|
|
|
virtual uint GetGlyphWidth(GlyphID key) = 0;
|
2013-06-23 15:24:36 +00: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 20:20:15 +00:00
|
|
|
/**
|
|
|
|
* Map a character into a glyph.
|
|
|
|
* @param key The character.
|
|
|
|
* @return The glyph ID used to draw the character.
|
|
|
|
*/
|
|
|
|
virtual GlyphID MapCharToGlyph(WChar key) = 0;
|
|
|
|
|
2013-06-25 20:39:58 +00:00
|
|
|
/**
|
|
|
|
* Read a font table from the font.
|
|
|
|
* @param tag The of the table to load.
|
2013-06-27 21:21:47 +00:00
|
|
|
* @param length The length of the read data.
|
2013-06-25 20:39:58 +00:00
|
|
|
* @return The loaded table data.
|
|
|
|
*/
|
2013-06-27 21:21:47 +00:00
|
|
|
virtual const void *GetFontTable(uint32 tag, size_t &length) = 0;
|
2013-06-25 20:39:58 +00:00
|
|
|
|
2013-12-22 17:46:27 +00:00
|
|
|
/**
|
|
|
|
* Get the name of this font.
|
|
|
|
* @return The name of the font.
|
|
|
|
*/
|
|
|
|
virtual const char *GetFontName() = 0;
|
|
|
|
|
2013-06-23 15:24:36 +00: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];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether the font cache has a parent.
|
|
|
|
*/
|
|
|
|
inline bool HasParent()
|
|
|
|
{
|
|
|
|
return this->parent != NULL;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2006-11-16 22:05:33 +00:00
|
|
|
/** Get the SpriteID mapped to the given font size and key */
|
2013-06-25 20:20:15 +00:00
|
|
|
static inline SpriteID GetUnicodeGlyph(FontSize size, WChar key)
|
2013-06-23 15:24:36 +00:00
|
|
|
{
|
|
|
|
return FontCache::Get(size)->GetUnicodeGlyph(key);
|
|
|
|
}
|
2006-11-16 22:05:33 +00:00
|
|
|
|
|
|
|
/** Map a SpriteID to the font size and key */
|
2013-06-25 20:20:15 +00:00
|
|
|
static inline void SetUnicodeGlyph(FontSize size, WChar key, SpriteID sprite)
|
2013-06-23 15:24:36 +00:00
|
|
|
{
|
|
|
|
FontCache::Get(size)->SetUnicodeGlyph(key, sprite);
|
|
|
|
}
|
2006-11-16 22:05:33 +00:00
|
|
|
|
|
|
|
/** Initialize the glyph map */
|
2013-06-23 15:24:36 +00:00
|
|
|
static inline void InitializeUnicodeGlyphMap()
|
|
|
|
{
|
|
|
|
for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
|
|
|
|
FontCache::Get(fs)->InitializeUnicodeGlyphMap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-13 13:28:06 +00:00
|
|
|
static inline void ClearFontCache()
|
|
|
|
{
|
2013-06-23 15:24:36 +00:00
|
|
|
for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
|
|
|
|
FontCache::Get(fs)->ClearFontCache();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Get the Sprite for a glyph */
|
2013-06-25 20:20:15 +00:00
|
|
|
static inline const Sprite *GetGlyph(FontSize size, WChar key)
|
2013-06-23 15:24:36 +00:00
|
|
|
{
|
2013-06-25 20:20:15 +00:00
|
|
|
FontCache *fc = FontCache::Get(size);
|
|
|
|
return fc->GetGlyph(fc->MapCharToGlyph(key));
|
2013-06-23 15:24:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Get the width of a glyph */
|
2013-06-25 20:20:15 +00:00
|
|
|
static inline uint GetGlyphWidth(FontSize size, WChar key)
|
2013-06-23 15:24:36 +00:00
|
|
|
{
|
2013-06-25 20:20:15 +00:00
|
|
|
FontCache *fc = FontCache::Get(size);
|
|
|
|
return fc->GetGlyphWidth(fc->MapCharToGlyph(key));
|
2013-06-23 15:24:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool GetDrawGlyphShadow(FontSize size)
|
|
|
|
{
|
|
|
|
return FontCache::Get(size)->GetDrawGlyphShadow();
|
|
|
|
}
|
2006-11-16 22:05:33 +00:00
|
|
|
|
|
|
|
#ifdef WITH_FREETYPE
|
|
|
|
|
2013-06-23 15:23:22 +00:00
|
|
|
/** Settings for a single freetype font. */
|
|
|
|
struct FreeTypeSubSetting {
|
|
|
|
char font[MAX_PATH]; ///< The name of the font, or path to the font.
|
|
|
|
uint size; ///< The (requested) size of the font.
|
|
|
|
bool aa; ///< Whether to do anti aliasing or not.
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Settings for the freetype fonts. */
|
2007-03-07 12:11:48 +00:00
|
|
|
struct FreeTypeSettings {
|
2013-06-23 15:23:22 +00:00
|
|
|
FreeTypeSubSetting small; ///< The smallest font; mostly used for zoomed out view.
|
|
|
|
FreeTypeSubSetting medium; ///< The normal font size.
|
|
|
|
FreeTypeSubSetting large; ///< The largest font; mostly used for newspapers.
|
|
|
|
FreeTypeSubSetting mono; ///< The mono space font used for license/readme viewers.
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2006-11-16 22:05:33 +00:00
|
|
|
|
|
|
|
extern FreeTypeSettings _freetype;
|
|
|
|
|
2013-06-23 15:37:18 +00:00
|
|
|
#endif /* WITH_FREETYPE */
|
|
|
|
|
2011-11-20 11:59:36 +00:00
|
|
|
void InitFreeType(bool monospace);
|
2008-11-24 18:53:17 +00:00
|
|
|
void UninitFreeType();
|
2006-11-16 22:05:33 +00:00
|
|
|
|
|
|
|
#endif /* FONTCACHE_H */
|