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; }
128 default: NOT_REACHED();
130 case FS_NORMAL: base = SPR_ASCII_SPACE;
break;
131 case FS_SMALL: base = SPR_ASCII_SPACE_SMALL;
break;
132 case FS_LARGE: base = SPR_ASCII_SPACE_BIG;
break;
137 if (!SpriteExists(sprite))
continue;
142 for (uint i = 0; i <
lengthof(_default_unicode_map); i++) {
143 byte key = _default_unicode_map[i].key;
163 for (uint i = 0; i < 256; i++) {
179 return GetSprite(sprite,
ST_FONT);
202 #include <ft2build.h> 203 #include FT_FREETYPE_H 205 #include FT_TRUETYPE_TABLES_H 251 virtual const void *
GetFontTable(uint32 tag,
size_t &length);
255 FT_Library _library = NULL;
259 static const byte FACE_COLOUR = 1;
260 static const byte SHADOW_COLOUR = 2;
270 assert(face != NULL);
276 TT_Header *head = (TT_Header *)FT_Get_Sfnt_Table(this->face, ft_sfnt_head);
281 pixels =
Clamp(
min(head->Lowest_Rec_PPEM, 20) + diff, _default_font_height[this->fs],
MAX_FONT_SIZE);
285 FT_Error err = FT_Set_Pixel_Sizes(this->face, 0, pixels);
286 if (err != FT_Err_Ok) {
289 FT_Bitmap_Size *bs = this->face->available_sizes;
290 int i = this->face->num_fixed_sizes;
295 if (
abs(pixels - bs->height) >=
abs(pixels - n))
continue;
297 chosen = this->face->num_fixed_sizes - i;
302 err = FT_Select_Size(this->face, chosen);
306 if (err == FT_Err_Ok) {
308 this->
ascender = this->face->size->metrics.ascender >> 6;
309 this->
descender = this->face->size->metrics.descender >> 6;
313 DEBUG(freetype, 0,
"Font size selection failed. Using FontCache defaults.");
328 default: NOT_REACHED();
337 if (_library == NULL) {
338 if (FT_Init_FreeType(&_library) != FT_Err_Ok) {
339 ShowInfoF(
"Unable to initialize FreeType, using sprite fonts instead");
343 DEBUG(freetype, 2,
"Initialized");
347 FT_Error
error = FT_New_Face(_library, settings->
font, 0, &face);
351 if (error == FT_Err_Ok) {
352 DEBUG(freetype, 2,
"Requested '%s', using '%s %s'", settings->
font, face->family_name, face->style_name);
355 error = FT_Select_Charmap(face, ft_encoding_unicode);
356 if (error == FT_Err_Ok)
goto found_face;
358 if (error == FT_Err_Invalid_CharMap_Handle) {
362 FT_CharMap found = face->charmaps[0];
365 for (i = 0; i < face->num_charmaps; i++) {
366 FT_CharMap charmap = face->charmaps[i];
367 if (charmap->platform_id == 0 && charmap->encoding_id == 0) {
373 error = FT_Set_Charmap(face, found);
374 if (error == FT_Err_Ok)
goto found_face;
381 static const char *SIZE_TO_NAME[] = {
"medium",
"small",
"large",
"mono" };
382 ShowInfoF(
"Unable to use '%s' for %s font, FreeType reported error 0x%X, using sprite font instead", settings->
font, SIZE_TO_NAME[fs], error);
395 FT_Done_Face(this->
face);
399 free(iter->second.second);
410 for (
int i = 0; i < 256; i++) {
413 for (
int j = 0; j < 256; j++) {
435 void FreeTypeFontCache::SetGlyphPtr(
GlyphID key,
const GlyphEntry *glyph,
bool duplicate)
438 DEBUG(freetype, 3,
"Allocating root glyph cache for size %u", this->
fs);
443 DEBUG(freetype, 3,
"Allocating glyph cache for range 0x%02X00, size %u",
GB(key, 8, 8), this->
fs);
447 DEBUG(freetype, 4,
"Set glyph for unicode character 0x%04X, size %u", key, this->
fs);
453 static void *AllocateFont(
size_t size)
455 return MallocT<byte>(size);
460 static bool GetFontAAState(
FontSize size)
466 default: NOT_REACHED();
477 if ((key & SPRITE_GLYPH) != 0)
return this->
parent->
GetGlyph(key);
481 if (glyph != NULL && glyph->
sprite != NULL)
return glyph->
sprite;
483 FT_GlyphSlot slot = this->
face->glyph;
485 bool aa = GetFontAAState(this->
fs);
490 if (question_glyph == 0) {
493 #define CPSET { 0, 0, 0, 0, 1 } 494 #define CP___ { 0, 0, 0, 0, 0 } 496 CP___, CP___, CPSET, CPSET, CPSET, CPSET, CP___, CP___,
497 CP___, CPSET, CPSET, CP___, CP___, CPSET, CPSET, CP___,
498 CP___, CP___, CP___, CP___, CP___, CPSET, CPSET, CP___,
499 CP___, CP___, CP___, CP___, CPSET, CPSET, CP___, CP___,
500 CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
501 CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
502 CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
503 CP___, CP___, CP___, CP___, CP___, CP___, CP___, CP___,
504 CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
505 CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
515 builtin_questionmark_data
522 this->SetGlyphPtr(key, &new_glyph,
false);
527 glyph = this->GetGlyphPtr(question_glyph);
528 this->SetGlyphPtr(key, glyph,
true);
532 FT_Load_Glyph(this->
face, key, FT_LOAD_DEFAULT);
533 FT_Render_Glyph(this->
face->glyph, aa ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO);
536 aa = (slot->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY);
539 uint width =
max(1U, (uint)slot->bitmap.width + (this->fs ==
FS_NORMAL));
543 if (width > 256 || height > 256)
usererror(
"Font glyph is too large");
549 sprite.
width = width;
551 sprite.
x_offs = slot->bitmap_left;
556 for (uint y = 0; y < (uint)slot->bitmap.rows; y++) {
557 for (uint x = 0; x < (uint)slot->bitmap.width; x++) {
558 if (aa ? (slot->bitmap.buffer[x + y * slot->bitmap.pitch] > 0) :
HasBit(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
559 sprite.
data[1 + x + (1 + y) * sprite.
width].
m = SHADOW_COLOUR;
560 sprite.
data[1 + x + (1 + y) * sprite.
width].
a = aa ? slot->bitmap.buffer[x + y * slot->bitmap.pitch] : 0xFF;
566 for (uint y = 0; y < (uint)slot->bitmap.rows; y++) {
567 for (uint x = 0; x < (uint)slot->bitmap.width; x++) {
568 if (aa ? (slot->bitmap.buffer[x + y * slot->bitmap.pitch] > 0) :
HasBit(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
569 sprite.
data[x + y * sprite.
width].
m = FACE_COLOUR;
570 sprite.
data[x + y * sprite.
width].
a = aa ? slot->bitmap.buffer[x + y * slot->bitmap.pitch] : 0xFF;
576 new_glyph.
width = slot->advance.x >> 6;
578 this->SetGlyphPtr(key, &new_glyph);
595 if (glyph == NULL || glyph->
sprite == NULL) {
597 glyph = this->GetGlyphPtr(key);
605 assert(IsPrintable(key));
607 if (key >= SCC_SPRITE_START && key <= SCC_SPRITE_END) {
611 return FT_Get_Char_Index(this->
face, key);
618 length = iter->second.first;
619 return iter->second.second;
623 FT_Byte *result = NULL;
625 FT_Load_Sfnt_Table(this->
face, tag, 0, NULL, &len);
628 result = MallocT<FT_Byte>(len);
629 FT_Load_Sfnt_Table(this->
face, tag, 0, result, &len);
646 if (monospace != (
fs ==
FS_MONO))
continue;
668 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.
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.
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)
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.
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.
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.