34 static const int _default_font_ascender[FS_END] = { 8, 5, 15, 8};
41 ascender(_default_font_ascender[fs]), descender(_default_font_ascender[fs] -
_default_font_height[fs]),
74 void ClearGlyphToSpriteMap();
87 virtual const void *
GetFontTable(uint32 tag,
size_t &length) { length = 0;
return NULL; }
129 default: NOT_REACHED();
131 case FS_NORMAL: base = SPR_ASCII_SPACE;
break;
132 case FS_SMALL: base = SPR_ASCII_SPACE_SMALL;
break;
133 case FS_LARGE: base = SPR_ASCII_SPACE_BIG;
break;
138 if (!SpriteExists(sprite))
continue;
143 for (uint i = 0; i <
lengthof(_default_unicode_map); i++) {
144 byte key = _default_unicode_map[i].key;
164 for (uint i = 0; i < 256; i++) {
180 return GetSprite(sprite,
ST_FONT);
203 #include <ft2build.h> 204 #include FT_FREETYPE_H 206 #include FT_TRUETYPE_TABLES_H 242 void SetFontSize(
FontSize fs, FT_Face face,
int pixels);
256 virtual const void *
GetFontTable(uint32 tag,
size_t &length);
261 FT_Library _library = NULL;
265 static const byte FACE_COLOUR = 1;
266 static const byte SHADOW_COLOUR = 2;
276 assert(face != NULL);
278 this->SetFontSize(fs, face, pixels);
281 void FreeTypeFontCache::SetFontSize(
FontSize fs, FT_Face
face,
int pixels)
286 pixels = scaled_height;
288 TT_Header *head = (TT_Header *)FT_Get_Sfnt_Table(this->
face, ft_sfnt_head);
298 FT_Error err = FT_Set_Pixel_Sizes(this->
face, 0, pixels);
299 if (err != FT_Err_Ok) {
302 FT_Bitmap_Size *bs = this->
face->available_sizes;
303 int i = this->
face->num_fixed_sizes;
308 if (
abs(pixels - bs->height) >=
abs(pixels - n))
continue;
310 chosen = this->
face->num_fixed_sizes - i;
315 err = FT_Select_Size(this->
face, chosen);
319 if (err == FT_Err_Ok) {
321 this->
ascender = this->
face->size->metrics.ascender >> 6;
326 DEBUG(freetype, 0,
"Font size selection failed. Using FontCache defaults.");
341 default: NOT_REACHED();
350 if (_library == NULL) {
351 if (FT_Init_FreeType(&_library) != FT_Err_Ok) {
352 ShowInfoF(
"Unable to initialize FreeType, using sprite fonts instead");
356 DEBUG(freetype, 2,
"Initialized");
360 FT_Error
error = FT_New_Face(_library, settings->
font, 0, &face);
364 if (error == FT_Err_Ok) {
365 DEBUG(freetype, 2,
"Requested '%s', using '%s %s'", settings->
font, face->family_name, face->style_name);
368 error = FT_Select_Charmap(face, ft_encoding_unicode);
369 if (error == FT_Err_Ok)
goto found_face;
371 if (error == FT_Err_Invalid_CharMap_Handle) {
375 FT_CharMap found = face->charmaps[0];
378 for (i = 0; i < face->num_charmaps; i++) {
379 FT_CharMap charmap = face->charmaps[i];
380 if (charmap->platform_id == 0 && charmap->encoding_id == 0) {
386 error = FT_Set_Charmap(face, found);
387 if (error == FT_Err_Ok)
goto found_face;
394 static const char *SIZE_TO_NAME[] = {
"medium",
"small",
"large",
"mono" };
395 ShowInfoF(
"Unable to use '%s' for %s font, FreeType reported error 0x%X, using sprite font instead", settings->
font, SIZE_TO_NAME[fs], error);
408 FT_Done_Face(this->
face);
413 free(iter->second.second);
424 for (
int i = 0; i < 256; i++) {
427 for (
int j = 0; j < 256; j++) {
452 void FreeTypeFontCache::SetGlyphPtr(
GlyphID key,
const GlyphEntry *glyph,
bool duplicate)
455 DEBUG(freetype, 3,
"Allocating root glyph cache for size %u", this->
fs);
460 DEBUG(freetype, 3,
"Allocating glyph cache for range 0x%02X00, size %u",
GB(key, 8, 8), this->
fs);
464 DEBUG(freetype, 4,
"Set glyph for unicode character 0x%04X, size %u", key, this->
fs);
470 static void *AllocateFont(
size_t size)
472 return MallocT<byte>(size);
477 static bool GetFontAAState(
FontSize size)
483 default: NOT_REACHED();
494 if ((key & SPRITE_GLYPH) != 0)
return this->
parent->
GetGlyph(key);
498 if (glyph != NULL && glyph->
sprite != NULL)
return glyph->
sprite;
500 FT_GlyphSlot slot = this->
face->glyph;
502 bool aa = GetFontAAState(this->
fs);
507 if (question_glyph == 0) {
510 #define CPSET { 0, 0, 0, 0, 1 } 511 #define CP___ { 0, 0, 0, 0, 0 } 513 CP___, CP___, CPSET, CPSET, CPSET, CPSET, CP___, CP___,
514 CP___, CPSET, CPSET, CP___, CP___, CPSET, CPSET, CP___,
515 CP___, CP___, CP___, CP___, CP___, CPSET, CPSET, CP___,
516 CP___, CP___, CP___, CP___, CPSET, CPSET, CP___, CP___,
517 CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
518 CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
519 CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
520 CP___, CP___, CP___, CP___, CP___, CP___, CP___, CP___,
521 CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
522 CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
532 builtin_questionmark_data
539 this->SetGlyphPtr(key, &new_glyph,
false);
544 glyph = this->GetGlyphPtr(question_glyph);
545 this->SetGlyphPtr(key, glyph,
true);
549 FT_Load_Glyph(this->
face, key, aa ? FT_LOAD_TARGET_NORMAL : FT_LOAD_TARGET_MONO);
550 FT_Render_Glyph(this->
face->glyph, aa ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO);
553 aa = (slot->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY);
556 uint width =
max(1U, (uint)slot->bitmap.width + (this->fs ==
FS_NORMAL));
560 if (width > 256 || height > 256)
usererror(
"Font glyph is too large");
566 sprite.
width = width;
568 sprite.
x_offs = slot->bitmap_left;
573 for (uint y = 0; y < (uint)slot->bitmap.rows; y++) {
574 for (uint x = 0; x < (uint)slot->bitmap.width; x++) {
575 if (aa ? (slot->bitmap.buffer[x + y * slot->bitmap.pitch] > 0) :
HasBit(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
576 sprite.
data[1 + x + (1 + y) * sprite.
width].
m = SHADOW_COLOUR;
577 sprite.
data[1 + x + (1 + y) * sprite.
width].
a = aa ? slot->bitmap.buffer[x + y * slot->bitmap.pitch] : 0xFF;
583 for (uint y = 0; y < (uint)slot->bitmap.rows; y++) {
584 for (uint x = 0; x < (uint)slot->bitmap.width; x++) {
585 if (aa ? (slot->bitmap.buffer[x + y * slot->bitmap.pitch] > 0) :
HasBit(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
586 sprite.
data[x + y * sprite.
width].
m = FACE_COLOUR;
587 sprite.
data[x + y * sprite.
width].
a = aa ? slot->bitmap.buffer[x + y * slot->bitmap.pitch] : 0xFF;
593 new_glyph.
width = slot->advance.x >> 6;
595 this->SetGlyphPtr(key, &new_glyph);
612 if (glyph == NULL || glyph->
sprite == NULL) {
614 glyph = this->GetGlyphPtr(key);
622 assert(IsPrintable(key));
624 if (key >= SCC_SPRITE_START && key <= SCC_SPRITE_END) {
628 return FT_Get_Char_Index(this->
face, key);
635 length = iter->second.first;
636 return iter->second.second;
640 FT_Byte *result = NULL;
642 FT_Load_Sfnt_Table(this->
face, tag, 0, NULL, &len);
645 result = MallocT<FT_Byte>(len);
646 FT_Load_Sfnt_Table(this->
face, tag, 0, result, &len);
663 if (monospace != (
fs ==
FS_MONO))
continue;
685 FT_Done_FreeType(_library);
Functions related to OTTD's strings.
Character mapping for using Unicode characters in OTTD.
virtual void InitializeUnicodeGlyphMap()
Initialize the glyph map.
virtual uint GetGlyphWidth(GlyphID key)
Get the width of the glyph with the given key.
int height
The height of the font.
Control codes that are embedded in the translation strings.
virtual SpriteID GetUnicodeGlyph(WChar key)
Get the SpriteID mapped to the given key.
SpriteFontCache(FontSize fs)
Create a new sprite font cache.
const Pair * Find(const T &key) const
Finds given key in this map.
virtual bool GetDrawGlyphShadow()
Do we need to draw a glyph shadow?
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
int descender
The descender value of the font.
Settings for a single freetype font.
~FreeTypeFontCache()
Free everything that was allocated for this font cache.
Index of the monospaced font in the font tables.
Data structure describing a sprite.
static int ScaleGUITrad(int value)
Scale traditional pixel dimensions to GUI zoom level.
fluid_settings_t * settings
FluidSynth settings handle.
Sprite * sprite
The loaded sprite.
FontTable font_tables
Cached font tables.
Implementation of simple mapping class.
SmallMap< uint32, SmallPair< size_t, const void * > > FontTable
Table with font table cache.
Functions related to detecting/finding the right font.
const T * Begin() const
Get the pointer to the first item (const)
bool Insert(const T &key, const U &data)
Adds new item to this map.
void AllocateData(ZoomLevel zoom, size_t size)
Allocate the sprite data of this sprite.
virtual void ClearFontCache()
Clear the font cache.
virtual bool IsBuiltInFont()
Is this a built-in sprite font?
FreeTypeSubSetting large
The largest font; mostly used for newspapers.
int units_per_em
The units per EM value of the font.
virtual void SetUnicodeGlyph(WChar key, SpriteID sprite)=0
Map a SpriteID to the key.
virtual const char * GetFontName()
Get the name of this font.
virtual Sprite * Encode(const SpriteLoader::Sprite *sprite, AllocatorProc *allocator)=0
Convert a sprite from the loader to our own format.
virtual GlyphID MapCharToGlyph(WChar key)
Map a character into a glyph.
byte width
The width of the glyph.
static T max(const T a, const T b)
Returns the maximum of two values.
Functions related to laying out the texts.
static FontCache * Get(FontSize fs)
Get the font cache of a given font size.
const T * End() const
Get the pointer behind the last valid item (const)
virtual int GetFontSize() const
Get the nominal font size of the font.
int used_size
Used font size.
Settings for the freetype fonts.
bool HasParent()
Check whether the font cache has a parent.
Definition of a common pixel in OpenTTD's realm.
SpriteType type
The sprite type.
Font cache for fonts that are based on a freetype font.
Types related to zooming in and out.
Simple mapping class targeted for small sets of data.
void InitFreeType(bool monospace)
(Re)initialize the freetype related things, i.e.
void CDECL ShowInfoF(const char *str,...)
Shows some information on the console/a popup box depending on the OS.
Functions to read fonts from files and cache them.
virtual void ClearFontCache()
Reset cached glyphs.
bool aa
Whether to do anti aliasing or not.
virtual ~FontCache()
Clean everything up.
FreeTypeSubSetting mono
The mono space font used for license/readme viewers.
FontCache(FontSize fs)
Create a new font cache.
FT_Face face
The font face associated with this font.
virtual void InitializeUnicodeGlyphMap()
Initialize the glyph map.
virtual uint GetGlyphWidth(GlyphID key)=0
Get the width of the glyph with the given key.
GlyphEntry ** glyph_to_sprite
The glyph cache.
Definition of base types and functions in a cross-platform compatible way.
virtual bool IsBuiltInFont()
Is this a built-in sprite font?
Font cache for fonts that are based on a freetype font.
void CDECL usererror(const char *s,...)
Error handling for fatal user errors.
A number of safeguards to prevent using unsafe methods.
FT_Error GetFontByFaceName(const char *font_name, FT_Face *face)
Get the font loaded into a Freetype face by using a font-name.
virtual const void * GetFontTable(uint32 tag, size_t &length)
Read a font table from the font.
int16 x_offs
The x-offset of where the sprite will be drawn.
virtual int GetHeight() const
Get the height of the font.
virtual const Sprite * GetGlyph(GlyphID key)
Get the glyph (sprite) of the given key.
FreeTypeFontCache(FontSize fs, FT_Face face, int pixels)
Create a new FreeTypeFontCache.
~SpriteFontCache()
Free everything we allocated.
SpriteLoader::CommonPixel * data
The sprite itself.
virtual bool GetDrawGlyphShadow()
Do we need to draw a glyph shadow?
Structure for passing information from the sprite loader to the blitter.
FreeTypeSubSetting medium
The normal font size.
static const int MAX_FONT_SIZE
Maximum font size.
#define lengthof(x)
Return the length of an fixed size array.
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
static T min(const T a, const T b)
Returns the minimum of two values.
virtual GlyphID MapCharToGlyph(WChar key)=0
Map a character into a glyph.
static void ResetFontCache(FontSize size)
Reset cached font information.
Font cache for basic fonts.
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
#define DEBUG(name, level,...)
Output a line of debugging information.
FontCache * parent
The parent of this font cache.
uint size
The (requested) size of the font.
uint16 width
Width of the sprite.
char font[MAX_PATH]
The name of the font, or path to the font.
uint16 width
Width of the sprite.
void ClearGlyphToSpriteMap()
Clear the glyph to sprite mapping.
SpriteID ** glyph_to_spriteid_map
Mapping of glyphs to sprite IDs.
uint32 SpriteID
The number of a sprite, without mapping bits and colourtables.
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
static FontCache * caches[FS_END]
All the font caches.
virtual uint GetGlyphWidth(GlyphID key)
Get the width of the glyph with the given key.
virtual void SetUnicodeGlyph(WChar key, SpriteID sprite)
Map a SpriteID to the key.
virtual const char * GetFontName()
Get the name of this font.
static const byte CLRA
Identifier to clear all glyphs at this codepoint.
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
static T abs(const T a)
Returns the absolute value of (scalar) variable.
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
FreeTypeSubSetting small
The smallest font; mostly used for zoomed out view.
virtual const Sprite * GetGlyph(GlyphID key)=0
Get the glyph (sprite) of the given key.
Functions related to zooming.
FontSize
Available font sizes.
uint16 height
Height of the sprite.
static void LoadFreeTypeFont(FontSize fs)
Loads the freetype font.
Index of the normal font in the font tables.
virtual const Sprite * GetGlyph(GlyphID key)
Get the glyph (sprite) of the given key.
virtual SpriteID GetUnicodeGlyph(WChar key)
Get the SpriteID mapped to the given key.
virtual bool GetDrawGlyphShadow()=0
Do we need to draw a glyph shadow?
virtual int GetHeight() const
Get the height of the font.
static const int _default_font_height[FS_END]
Default heights for the different sizes of fonts.
void UninitFreeType()
Free everything allocated w.r.t.
int16 y_offs
The y-offset of where the sprite will be drawn.
Index of the small font in the font tables.
virtual void InitializeUnicodeGlyphMap()=0
Initialize the glyph map.
int ascender
The ascender value of the font.
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Index of the large font in the font tables.
Container for information about a glyph.
const FontSize fs
The size of the font.
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
int req_size
Requested font size.
virtual void SetUnicodeGlyph(WChar key, SpriteID sprite)
Map a SpriteID to the key.
virtual const void * GetFontTable(uint32 tag, size_t &length)
Read a font table from the font.
virtual GlyphID MapCharToGlyph(WChar key)
Map a character into a glyph.
uint32 GlyphID
Glyphs are characters from a font.
virtual void ClearFontCache()=0
Clear the font cache.
uint32 WChar
Type for wide characters, i.e.
bool duplicate
Whether this glyph entry is a duplicate, i.e. may this be freed?
This file contains all sprite-related enums and defines.
Factory to 'query' all available blitters.
virtual SpriteID GetUnicodeGlyph(WChar key)=0
Get the SpriteID mapped to the given key.
static const int ASCII_LETTERSTART
First printable ASCII letter.