Changeset - r20454:35a010f0c655
[Not reviewed]
master
0 2 0
rubidium - 11 years ago 2013-06-25 20:39:58
rubidium@openttd.org
(svn r25469) -Add: method for getting the font tables from freetype fonts
2 files changed with 36 insertions and 3 deletions:
0 comments (0 inline, 0 general)
src/fontcache.cpp
Show inline comments
 
@@ -11,12 +11,13 @@
 

	
 
#include "stdafx.h"
 
#include "fontcache.h"
 
#include "fontdetection.h"
 
#include "blitter/factory.hpp"
 
#include "core/math_func.hpp"
 
#include "core/smallmap_type.hpp"
 
#include "strings_func.h"
 
#include "zoom_type.h"
 

	
 
#include "table/sprites.h"
 
#include "table/control_codes.h"
 
#include "table/unicode.h"
 
@@ -68,17 +69,18 @@ private:
 
public:
 
	SpriteFontCache(FontSize fs);
 
	~SpriteFontCache();
 
	virtual SpriteID GetUnicodeGlyph(WChar key);
 
	virtual void SetUnicodeGlyph(WChar key, SpriteID sprite);
 
	virtual void InitializeUnicodeGlyphMap();
 
	virtual void ClearFontCache();
 
	virtual void ClearFontCache() {}
 
	virtual const Sprite *GetGlyph(GlyphID key);
 
	virtual uint GetGlyphWidth(GlyphID key);
 
	virtual bool GetDrawGlyphShadow();
 
	virtual GlyphID MapCharToGlyph(WChar key) { return SPRITE_GLYPH | key; }
 
	virtual const void *GetFontTable(uint32 tag) { return NULL; }
 
};
 

	
 
/**
 
 * Create a new sprite font cache.
 
 * @param fs The font size to create the cache for.
 
 */
 
@@ -154,14 +156,12 @@ void SpriteFontCache::ClearGlyphToSprite
 
		free(this->glyph_to_spriteid_map[i]);
 
	}
 
	free(this->glyph_to_spriteid_map);
 
	this->glyph_to_spriteid_map = NULL;
 
}
 

	
 
void SpriteFontCache::ClearFontCache() {}
 

	
 
const Sprite *SpriteFontCache::GetGlyph(GlyphID key)
 
{
 
	SpriteID sprite = this->GetUnicodeGlyph(key);
 
	if (sprite == 0) sprite = this->GetUnicodeGlyph('?');
 
	return GetSprite(sprite, ST_FONT);
 
}
 
@@ -188,12 +188,14 @@ bool SpriteFontCache::GetDrawGlyphShadow
 

	
 
/** Font cache for fonts that are based on a freetype font. */
 
class FreeTypeFontCache : public FontCache {
 
private:
 
	FT_Face face;  ///< The font face associated with this font.
 

	
 
	SmallMap<uint32, const void*> font_tables; ///< Cached font tables.
 

	
 
	/** Container for information about a glyph. */
 
	struct GlyphEntry {
 
		Sprite *sprite; ///< The loaded sprite.
 
		byte width;     ///< The width of the glyph.
 
		bool duplicate; ///< Whether this glyph entry is a duplicate, i.e. may this be freed?
 
	};
 
@@ -224,12 +226,13 @@ public:
 
	virtual void InitializeUnicodeGlyphMap() { this->parent->InitializeUnicodeGlyphMap(); }
 
	virtual void ClearFontCache();
 
	virtual const Sprite *GetGlyph(GlyphID key);
 
	virtual uint GetGlyphWidth(GlyphID key);
 
	virtual bool GetDrawGlyphShadow();
 
	virtual GlyphID MapCharToGlyph(WChar key);
 
	virtual const void *GetFontTable(uint32 tag);
 
};
 

	
 
FT_Library _library = NULL;
 

	
 
FreeTypeSettings _freetype;
 

	
 
@@ -356,12 +359,16 @@ found_face:
 
 * Free everything that was allocated for this font cache.
 
 */
 
FreeTypeFontCache::~FreeTypeFontCache()
 
{
 
	FT_Done_Face(this->face);
 
	this->ClearFontCache();
 

	
 
	for (SmallPair<uint32, const void *> *iter = this->font_tables.Begin(); iter != this->font_tables.End(); iter++) {
 
		free(iter->second);
 
	}
 
}
 

	
 
/**
 
 * Reset cached glyphs.
 
 */
 
void FreeTypeFontCache::ClearFontCache()
 
@@ -542,12 +549,31 @@ GlyphID FreeTypeFontCache::MapCharToGlyp
 
		return this->parent->MapCharToGlyph(key);
 
	}
 

	
 
	return FT_Get_Char_Index(this->face, key);
 
}
 

	
 
const void *FreeTypeFontCache::GetFontTable(uint32 tag)
 
{
 
	const SmallPair<uint32, const void *> *iter = this->font_tables.Find(tag);
 
	if (iter != this->font_tables.End()) return iter->second;
 

	
 
	FT_ULong len = 0;
 
	FT_Byte *result = NULL;
 

	
 
	FT_Load_Sfnt_Table(this->face, tag, 0, NULL, &len);
 

	
 
	if (len > 0) {
 
		result = MallocT<FT_Byte>(len);
 
		FT_Load_Sfnt_Table(this->face, tag, 0, result, &len);
 
	}
 

	
 
	this->font_tables.Insert(tag, result);
 
	return result;
 
}
 

	
 
#endif /* WITH_FREETYPE */
 

	
 
/**
 
 * (Re)initialize the freetype related things, i.e. load the non-sprite fonts.
 
 * @param monospace Whether to initialise the monospace or regular fonts.
 
 */
src/fontcache.h
Show inline comments
 
@@ -109,12 +109,19 @@ public:
 
	 * @param key The character.
 
	 * @return The glyph ID used to draw the character.
 
	 */
 
	virtual GlyphID MapCharToGlyph(WChar key) = 0;
 

	
 
	/**
 
	 * Read a font table from the font.
 
	 * @param tag The of the table to load.
 
	 * @return The loaded table data.
 
	 */
 
	virtual const void *GetFontTable(uint32 tag) = 0;
 

	
 
	/**
 
	 * 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)
 
	{
0 comments (0 inline, 0 general)