Changeset - r18422:ab780a7de508
[Not reviewed]
master
0 5 0
rubidium - 13 years ago 2011-11-20 11:56:51
rubidium@openttd.org
(svn r23274) -Add: internal support for a monospaced sprite font
5 files changed with 45 insertions and 3 deletions:
0 comments (0 inline, 0 general)
src/fontcache.cpp
Show inline comments
 
@@ -20,15 +20,18 @@
 

	
 
static const int ASCII_LETTERSTART = 32; ///< First printable ASCII letter.
 

	
 
/** Semi-constant for the height of the different sizes of fonts. */
 
int _font_height[FS_END];
 

	
 
/** Reset the font sizes to the defaults of the sprite based fonts. */
 
/**
 
 * Reset the font sizes to the defaults of the sprite based fonts.
 
 */
 
void ResetFontSizes()
 
{
 
	_font_height[FS_MONO]   = 10;
 
	_font_height[FS_SMALL]  =  6;
 
	_font_height[FS_NORMAL] = 10;
 
	_font_height[FS_LARGE]  = 18;
 
}
 

	
 
#ifdef WITH_FREETYPE
 
@@ -41,12 +44,13 @@ void ResetFontSizes()
 
#endif
 

	
 
static FT_Library _library = NULL;
 
static FT_Face _face_small = NULL;
 
static FT_Face _face_medium = NULL;
 
static FT_Face _face_large = NULL;
 
static FT_Face _face_mono = NULL;
 
static int _ascender[FS_END];
 

	
 
FreeTypeSettings _freetype;
 

	
 
static const byte FACE_COLOUR   = 1;
 
static const byte SHADOW_COLOUR = 2;
 
@@ -836,17 +840,18 @@ static void UnloadFace(FT_Face *face)
 
 */
 
void InitFreeType()
 
{
 
	ResetFontSizes();
 
	ResetGlyphCache();
 

	
 
	UnloadFace(&_face_mono);
 
	UnloadFace(&_face_small);
 
	UnloadFace(&_face_medium);
 
	UnloadFace(&_face_large);
 

	
 
	if (StrEmpty(_freetype.small_font) && StrEmpty(_freetype.medium_font) && StrEmpty(_freetype.large_font)) {
 
	if (StrEmpty(_freetype.small_font) && StrEmpty(_freetype.medium_font) && StrEmpty(_freetype.large_font) && StrEmpty(_freetype.mono_font)) {
 
		DEBUG(freetype, 1, "No font faces specified, using sprite fonts instead");
 
		return;
 
	}
 

	
 
	if (_library == NULL) {
 
		if (FT_Init_FreeType(&_library) != FT_Err_Ok) {
 
@@ -855,17 +860,21 @@ void InitFreeType()
 
		}
 

	
 
		DEBUG(freetype, 2, "Initialized");
 
	}
 

	
 
	/* Load each font */
 
	LoadFreeTypeFont(_freetype.mono_font ,  &_face_mono,   "mono");
 
	LoadFreeTypeFont(_freetype.small_font,  &_face_small,  "small");
 
	LoadFreeTypeFont(_freetype.medium_font, &_face_medium, "medium");
 
	LoadFreeTypeFont(_freetype.large_font,  &_face_large,  "large");
 

	
 
	/* Set each font size */
 
	if (_face_mono != NULL) {
 
		SetFontGeometry(_face_mono, FS_MONO, _freetype.mono_size);
 
	}
 
	if (_face_small != NULL) {
 
		SetFontGeometry(_face_small, FS_SMALL, _freetype.small_size);
 
	}
 
	if (_face_medium != NULL) {
 
		SetFontGeometry(_face_medium, FS_NORMAL, _freetype.medium_size);
 
	}
 
@@ -881,12 +890,13 @@ void UninitFreeType()
 
{
 
	ResetGlyphCache();
 

	
 
	UnloadFace(&_face_small);
 
	UnloadFace(&_face_medium);
 
	UnloadFace(&_face_large);
 
	UnloadFace(&_face_mono);
 

	
 
	FT_Done_FreeType(_library);
 
	_library = NULL;
 
}
 

	
 

	
 
@@ -894,12 +904,13 @@ static FT_Face GetFontFace(FontSize size
 
{
 
	switch (size) {
 
		default: NOT_REACHED();
 
		case FS_NORMAL: return _face_medium;
 
		case FS_SMALL:  return _face_small;
 
		case FS_LARGE:  return _face_large;
 
		case FS_MONO:   return _face_mono;
 
	}
 
}
 

	
 

	
 
struct GlyphEntry {
 
	Sprite *sprite;
 
@@ -984,12 +995,13 @@ static bool GetFontAAState(FontSize size
 

	
 
	switch (size) {
 
		default: NOT_REACHED();
 
		case FS_NORMAL: return _freetype.medium_aa;
 
		case FS_SMALL:  return _freetype.small_aa;
 
		case FS_LARGE:  return _freetype.large_aa;
 
		case FS_MONO:   return _freetype.mono_aa;
 
	}
 
}
 

	
 

	
 
const Sprite *GetGlyph(FontSize size, WChar key)
 
{
 
@@ -1108,13 +1120,13 @@ uint GetGlyphWidth(FontSize size, WChar 
 
	FT_Face face = GetFontFace(size);
 
	GlyphEntry *glyph;
 

	
 
	if (face == NULL || (key >= SCC_SPRITE_START && key <= SCC_SPRITE_END)) {
 
		SpriteID sprite = GetUnicodeGlyph(size, key);
 
		if (sprite == 0) sprite = GetUnicodeGlyph(size, '?');
 
		return SpriteExists(sprite) ? GetSprite(sprite, ST_FONT)->width + (size != FS_NORMAL) : 0;
 
		return SpriteExists(sprite) ? GetSprite(sprite, ST_FONT)->width + (size != FS_NORMAL && size != FS_MONO) : 0;
 
	}
 

	
 
	glyph = GetGlyphPtr(size, key);
 
	if (glyph == NULL || glyph->sprite == NULL) {
 
		GetGlyph(size, key);
 
		glyph = GetGlyphPtr(size, key);
 
@@ -1138,12 +1150,13 @@ static SpriteID GetFontBase(FontSize siz
 
{
 
	switch (size) {
 
		default: NOT_REACHED();
 
		case FS_NORMAL: return SPR_ASCII_SPACE;
 
		case FS_SMALL:  return SPR_ASCII_SPACE_SMALL;
 
		case FS_LARGE:  return SPR_ASCII_SPACE_BIG;
 
		case FS_MONO:   return SPR_ASCII_SPACE;
 
	}
 
}
 

	
 

	
 
SpriteID GetUnicodeGlyph(FontSize size, uint32 key)
 
{
src/fontcache.h
Show inline comments
 
@@ -26,18 +26,21 @@ void InitializeUnicodeGlyphMap();
 
#ifdef WITH_FREETYPE
 

	
 
struct FreeTypeSettings {
 
	char small_font[MAX_PATH];
 
	char medium_font[MAX_PATH];
 
	char large_font[MAX_PATH];
 
	char mono_font[MAX_PATH];
 
	uint small_size;
 
	uint medium_size;
 
	uint large_size;
 
	uint mono_size;
 
	bool small_aa;
 
	bool medium_aa;
 
	bool large_aa;
 
	bool mono_aa;
 
};
 

	
 
extern FreeTypeSettings _freetype;
 

	
 
void InitFreeType();
 
void UninitFreeType();
src/gfx_func.h
Show inline comments
 
@@ -169,12 +169,15 @@ static inline byte GetCharacterHeight(Fo
 
/** Height of characters in the normal (#FS_NORMAL) font. */
 
#define FONT_HEIGHT_NORMAL (GetCharacterHeight(FS_NORMAL))
 

	
 
/** Height of characters in the large (#FS_LARGE) font. */
 
#define FONT_HEIGHT_LARGE  (GetCharacterHeight(FS_LARGE))
 

	
 
/** Height of characters in the large (#FS_MONO) font. */
 
#define FONT_HEIGHT_MONO  (GetCharacterHeight(FS_MONO))
 

	
 
extern DrawPixelInfo *_cur_dpi;
 

	
 
/**
 
 * All 16 colour gradients
 
 * 8 colours per gradient from darkest (0) to lightest (7)
 
 */
src/gfx_type.h
Show inline comments
 
@@ -162,12 +162,13 @@ union Colour {
 

	
 
/** Available font sizes */
 
enum FontSize {
 
	FS_NORMAL, ///< Index of the normal font in the font tables.
 
	FS_SMALL,  ///< Index of the small font in the font tables.
 
	FS_LARGE,  ///< Index of the large font in the font tables.
 
	FS_MONO,   ///< Index of the monospaced font in the font tables.
 
	FS_END,
 

	
 
	FS_BEGIN = FS_NORMAL, ///< First font.
 
};
 
DECLARE_POSTFIX_INCREMENT(FontSize)
 

	
src/table/misc_settings.ini
Show inline comments
 
@@ -141,12 +141,19 @@ def      = NULL
 
ifdef    = WITH_FREETYPE
 
name     = ""large_font""
 
type     = SLE_STRB
 
var      = _freetype.large_font
 
def      = NULL
 

	
 
[SDTG_STR]
 
ifdef    = WITH_FREETYPE
 
name     = ""mono_font""
 
type     = SLE_STRB
 
var      = _freetype.mono_font
 
def      = NULL
 

	
 
[SDTG_VAR]
 
ifdef    = WITH_FREETYPE
 
name     = ""small_size""
 
type     = SLE_UINT
 
var      = _freetype.small_size
 
def      = 8
 
@@ -168,12 +175,21 @@ name     = ""large_size""
 
type     = SLE_UINT
 
var      = _freetype.large_size
 
def      = 16
 
min      = 0
 
max      = 72
 

	
 
[SDTG_VAR]
 
ifdef    = WITH_FREETYPE
 
name     = ""large_mono""
 
type     = SLE_UINT
 
var      = _freetype.mono_size
 
def      = 10
 
min      = 0
 
max      = 72
 

	
 
[SDTG_BOOL]
 
ifdef    = WITH_FREETYPE
 
name     = ""small_aa""
 
var      = _freetype.small_aa
 
def      = false
 

	
 
@@ -186,12 +202,18 @@ def      = false
 
[SDTG_BOOL]
 
ifdef    = WITH_FREETYPE
 
name     = ""large_aa""
 
var      = _freetype.large_aa
 
def      = false
 

	
 
[SDTG_BOOL]
 
ifdef    = WITH_FREETYPE
 
name     = ""mono_aa""
 
var      = _freetype.mono_aa
 
def      = false
 

	
 
[SDTG_VAR]
 
name     = ""sprite_cache_size""
 
type     = SLE_UINT
 
var      = _sprite_cache_size
 
def      = 4
 
min      = 1
0 comments (0 inline, 0 general)