OpenTTD Source: TrueTypeFontCache Class Reference

OpenTTD Source 12.2

Data Structures | Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | Static Protected Attributes
TrueTypeFontCache Class Referenceabstract

Font cache for fonts that are based on a TrueType font. More...

`#include <fontcache_internal.h>`

Inheritance diagram for TrueTypeFontCache:

## Data Structures
struct | GlyphEntry
| Container for information about a glyph. More...

## Public Member Functions
| TrueTypeFontCache (FontSize fs, int pixels)
| Create a new TrueTypeFontCache. More...
virtual | ~TrueTypeFontCache ()
| Free everything that was allocated for this font cache.
int | GetFontSize () const override
| Get the nominal font size of the font. More...
SpriteID | GetUnicodeGlyph (WChar key) override
| Get the SpriteID mapped to the given key. More...
void | SetUnicodeGlyph (WChar key, SpriteID sprite) override
| Map a SpriteID to the key. More...
void | InitializeUnicodeGlyphMap () override
| Initialize the glyph map.
const Sprite * | GetGlyph (GlyphID key) override
| Get the glyph (sprite) of the given key. More...
const void * | GetFontTable (uint32 tag, size_t &length) override
| Read a font table from the font. More...
void | ClearFontCache () override
| Reset cached glyphs.
uint | GetGlyphWidth (GlyphID key) override
| Get the width of the glyph with the given key. More...
bool | GetDrawGlyphShadow () override
| Do we need to draw a glyph shadow? More...
bool | IsBuiltInFont () override
| Is this a built-in sprite font?
Public Member Functions inherited from FontCache
| FontCache (FontSize fs)
| Create a new font cache. More...
virtual | ~FontCache ()
| Clean everything up.
FontSize | GetSize () const
| Get the FontSize of the font. More...
int | GetHeight () const
| Get the height of the font. More...
int | GetAscender () const
| Get the ascender value of the font. More...
int | GetDescender () const
| Get the descender value of the font. More...
int | GetUnitsPerEM () const
| Get the units per EM value of the font. More...
virtual GlyphID | MapCharToGlyph (WChar key)=0
| Map a character into a glyph. More...
virtual const void * | GetOSHandle ()
| Get the native OS font handle, if there is one. More...
virtual const char * | GetFontName ()=0
| Get the name of this font. More...
bool | HasParent ()
| Check whether the font cache has a parent.

## Protected Types
typedef SmallMap< uint32, std::pair< size_t, const void * > > | FontTable
| Table with font table cache.

## Protected Member Functions
GlyphEntry * | GetGlyphPtr (GlyphID key)
void | SetGlyphPtr (GlyphID key, const GlyphEntry *glyph, bool duplicate=false)
virtual const void * | InternalGetFontTable (uint32 tag, size_t &length)=0
virtual const Sprite * | InternalGetGlyph (GlyphID key, bool aa)=0

## Protected Attributes
int | req_size
| Requested font size.
int | used_size
| Used font size.
FontTable | font_tables
| Cached font tables.
GlyphEntry ** | glyph_to_sprite
| The glyph cache. More...
Protected Attributes inherited from FontCache
FontCache * | parent
| The parent of this font cache.
const FontSize | fs
| The size of the font.
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.

## Static Protected Attributes
static constexpr int | MAX_GLYPH_DIM = 256
| Maximum glyph dimensions.
static constexpr uint | MAX_FONT_MIN_REC_SIZE = 20u
| Upper limit for the recommended font size in case a font file contains nonsensical values.

## Additional Inherited Members
Static Public Member Functions inherited from FontCache
static FontCache * | Get (FontSize fs)
| Get the font cache of a given font size. More...
Static Protected Member Functions inherited from FontCache
static int | GetDefaultFontHeight (FontSize fs)

## Detailed Description

Font cache for fonts that are based on a TrueType font.

Definition at line 23 of file fontcache_internal.h.

## Constructor & Destructor Documentation

## ◆ TrueTypeFontCache()

TrueTypeFontCache::TrueTypeFontCache | ( | FontSize | fs,
|  | int | pixels
| ) |  |

Create a new TrueTypeFontCache.

Parameters

  fs | The font size that is going to be cached.
  pixels | The number of pixels this font should be high.

Definition at line 210 of file fontcache.cpp.

## Member Function Documentation

## ◆ GetDrawGlyphShadow()

bool TrueTypeFontCache::GetDrawGlyphShadow | ( |  | ) |
| overridevirtual

Do we need to draw a glyph shadow?

Returns
  True if it has to be done, otherwise false.

Implements FontCache.

Definition at line 293 of file fontcache.cpp.

References FontCache::fs, and FS_NORMAL.

## ◆ GetFontSize()

int TrueTypeFontCache::GetFontSize | ( |  | ) | const
| inlineoverridevirtual

Get the nominal font size of the font.

Returns
  The nominal font size.

Reimplemented from FontCache.

Definition at line 65 of file fontcache_internal.h.

References used_size.

## ◆ GetFontTable()

const void * TrueTypeFontCache::GetFontTable | ( | uint32 | tag,
|  | size_t & | length
| ) |  |
| overridevirtual

Read a font table from the font.

Parameters

  tag | The of the table to load.
  length | The length of the read data.

Returns
  The loaded table data.

Implements FontCache.

Definition at line 369 of file fontcache.cpp.

References SmallMap< T, U >::Find(), font_tables, and SmallMap< T, U >::Insert().

## ◆ GetGlyph()

const Sprite * TrueTypeFontCache::GetGlyph | ( | GlyphID | key | ) |
| overridevirtual

Get the glyph (sprite) of the given key.

Parameters

  key | The key to look up.

Returns
  The sprite.

Implements FontCache.

Definition at line 311 of file fontcache.cpp.

## ◆ GetGlyphWidth()

uint TrueTypeFontCache::GetGlyphWidth | ( | GlyphID | key | ) |
| overridevirtual

Get the width of the glyph with the given key.

Parameters

  key | The key to look up.

Returns
  The width.

Implements FontCache.

Definition at line 298 of file fontcache.cpp.

## ◆ GetUnicodeGlyph()

SpriteID TrueTypeFontCache::GetUnicodeGlyph | ( | WChar | key | ) |
| inlineoverridevirtual

Get the SpriteID mapped to the given key.

Parameters

  key | The key to get the sprite for.

Returns
  The sprite.

Implements FontCache.

Definition at line 66 of file fontcache_internal.h.

References FontCache::GetUnicodeGlyph(), and FontCache::parent.

## ◆ SetUnicodeGlyph()

void TrueTypeFontCache::SetUnicodeGlyph | ( | WChar | key,
|  | SpriteID | sprite
| ) |  |
| inlineoverridevirtual

Map a SpriteID to the key.

Parameters

  key | The key to map to.
  sprite | The sprite that is being mapped.

Implements FontCache.

Definition at line 67 of file fontcache_internal.h.

References FontCache::parent, and FontCache::SetUnicodeGlyph().

## Field Documentation

## ◆ glyph_to_sprite

GlyphEntry** TrueTypeFontCache::glyph_to_sprite
| protected

The glyph cache.

This is structured to reduce memory consumption. 1) There is a 'segment' table for each font size. 2) Each segment table is a discrete block of characters. 3) Each block contains 256 (aligned) characters sequential characters.

The cache is accessed in the following way: For character 0x0041 ('A'): glyph_to_sprite[0x00][0x41] For character 0x20AC (Euro): glyph_to_sprite[0x20][0xAC]

Currently only 256 segments are allocated, "limiting" us to 65536 characters. This can be simply changed in the two functions Get & SetGlyphPtr.

Definition at line 54 of file fontcache_internal.h.

Referenced by ClearFontCache().

The documentation for this class was generated from the following files:

- src/fontcache_internal.h
- src/fontcache.cpp