2006-11-16 22:05:33 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file fontcache.cpp Cache for characters from fonts. */
|
2007-03-01 01:24:44 +00:00
|
|
|
|
2006-11-16 22:05:33 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "fontcache.h"
|
2013-06-23 15:20:23 +00:00
|
|
|
#include "fontdetection.h"
|
2007-06-17 20:30:28 +00:00
|
|
|
#include "blitter/factory.hpp"
|
2007-12-25 11:26:07 +00:00
|
|
|
#include "core/math_func.hpp"
|
2011-11-19 18:43:00 +00:00
|
|
|
#include "strings_func.h"
|
2011-11-24 12:38:48 +00:00
|
|
|
#include "zoom_type.h"
|
2006-11-16 22:05:33 +00:00
|
|
|
|
2008-01-13 01:21:35 +00:00
|
|
|
#include "table/sprites.h"
|
|
|
|
#include "table/control_codes.h"
|
2013-06-23 15:36:29 +00:00
|
|
|
#include "table/unicode.h"
|
2006-11-16 22:05:33 +00:00
|
|
|
|
2009-06-21 08:35:05 +00:00
|
|
|
static const int ASCII_LETTERSTART = 32; ///< First printable ASCII letter.
|
2013-04-06 22:06:44 +00:00
|
|
|
static const int MAX_FONT_SIZE = 72; ///< Maximum font size.
|
2009-06-21 08:35:05 +00:00
|
|
|
|
2013-04-06 22:06:44 +00:00
|
|
|
/** Default heights for the different sizes of fonts. */
|
|
|
|
static const int _default_font_height[FS_END] = {10, 6, 18, 10};
|
2009-03-25 21:35:22 +00:00
|
|
|
|
2013-06-23 15:24:36 +00:00
|
|
|
/**
|
|
|
|
* Create a new font cache.
|
|
|
|
* @param fs The size of the font.
|
|
|
|
*/
|
2013-06-23 15:32:09 +00:00
|
|
|
FontCache::FontCache(FontSize fs) : parent(FontCache::Get(fs)), fs(fs), height(_default_font_height[fs])
|
2013-06-23 15:24:36 +00:00
|
|
|
{
|
|
|
|
assert(parent == NULL || this->fs == parent->fs);
|
|
|
|
FontCache::caches[this->fs] = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Clean everything up. */
|
|
|
|
FontCache::~FontCache()
|
|
|
|
{
|
|
|
|
assert(this->fs == parent->fs);
|
|
|
|
FontCache::caches[this->fs] = this->parent;
|
|
|
|
}
|
|
|
|
|
2013-06-23 15:32:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get height of a character for a given font size.
|
|
|
|
* @param size Font size to get height of
|
|
|
|
* @return Height of characters in the given font (pixels)
|
|
|
|
*/
|
|
|
|
int GetCharacterHeight(FontSize size)
|
|
|
|
{
|
|
|
|
return FontCache::Get(size)->GetHeight();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-23 15:24:36 +00:00
|
|
|
/** Font cache for fonts that are based on a freetype font. */
|
|
|
|
class SpriteFontCache : public FontCache {
|
2013-06-23 15:25:35 +00:00
|
|
|
private:
|
|
|
|
SpriteID **glyph_to_spriteid_map; ///< Mapping of glyphs to sprite IDs.
|
|
|
|
|
|
|
|
void ClearGlyphToSpriteMap();
|
2013-06-23 15:24:36 +00:00
|
|
|
public:
|
2013-06-23 15:25:35 +00:00
|
|
|
SpriteFontCache(FontSize fs);
|
|
|
|
~SpriteFontCache();
|
2013-06-23 15:24:36 +00:00
|
|
|
virtual SpriteID GetUnicodeGlyph(uint32 key);
|
|
|
|
virtual void SetUnicodeGlyph(uint32 key, SpriteID sprite);
|
|
|
|
virtual void InitializeUnicodeGlyphMap();
|
|
|
|
virtual void ClearFontCache();
|
|
|
|
virtual const Sprite *GetGlyph(uint32 key);
|
|
|
|
virtual uint GetGlyphWidth(uint32 key);
|
|
|
|
virtual bool GetDrawGlyphShadow();
|
|
|
|
};
|
|
|
|
|
2013-06-23 15:25:35 +00:00
|
|
|
/**
|
|
|
|
* Create a new sprite font cache.
|
|
|
|
* @param fs The font size to create the cache for.
|
|
|
|
*/
|
|
|
|
SpriteFontCache::SpriteFontCache(FontSize fs) : FontCache(fs), glyph_to_spriteid_map(NULL)
|
|
|
|
{
|
|
|
|
this->InitializeUnicodeGlyphMap();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Free everything we allocated.
|
|
|
|
*/
|
|
|
|
SpriteFontCache::~SpriteFontCache()
|
|
|
|
{
|
|
|
|
this->ClearGlyphToSpriteMap();
|
|
|
|
}
|
|
|
|
|
2013-06-23 15:36:29 +00:00
|
|
|
SpriteID SpriteFontCache::GetUnicodeGlyph(uint32 key)
|
|
|
|
{
|
|
|
|
if (this->glyph_to_spriteid_map[GB(key, 8, 8)] == NULL) return 0;
|
|
|
|
return this->glyph_to_spriteid_map[GB(key, 8, 8)][GB(key, 0, 8)];
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpriteFontCache::SetUnicodeGlyph(uint32 key, SpriteID sprite)
|
|
|
|
{
|
|
|
|
if (this->glyph_to_spriteid_map == NULL) this->glyph_to_spriteid_map = CallocT<SpriteID*>(256);
|
|
|
|
if (this->glyph_to_spriteid_map[GB(key, 8, 8)] == NULL) this->glyph_to_spriteid_map[GB(key, 8, 8)] = CallocT<SpriteID>(256);
|
|
|
|
this->glyph_to_spriteid_map[GB(key, 8, 8)][GB(key, 0, 8)] = sprite;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpriteFontCache::InitializeUnicodeGlyphMap()
|
|
|
|
{
|
|
|
|
/* Clear out existing glyph map if it exists */
|
|
|
|
this->ClearGlyphToSpriteMap();
|
|
|
|
|
|
|
|
SpriteID base;
|
|
|
|
switch (this->fs) {
|
|
|
|
default: NOT_REACHED();
|
|
|
|
case FS_MONO: // Use normal as default for mono spaced font, i.e. FALL THROUGH
|
|
|
|
case FS_NORMAL: base = SPR_ASCII_SPACE; break;
|
|
|
|
case FS_SMALL: base = SPR_ASCII_SPACE_SMALL; break;
|
|
|
|
case FS_LARGE: base = SPR_ASCII_SPACE_BIG; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint i = ASCII_LETTERSTART; i < 256; i++) {
|
|
|
|
SpriteID sprite = base + i - ASCII_LETTERSTART;
|
|
|
|
if (!SpriteExists(sprite)) continue;
|
|
|
|
this->SetUnicodeGlyph(i, sprite);
|
|
|
|
this->SetUnicodeGlyph(i + SCC_SPRITE_START, sprite);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint i = 0; i < lengthof(_default_unicode_map); i++) {
|
|
|
|
byte key = _default_unicode_map[i].key;
|
|
|
|
if (key == CLRA) {
|
|
|
|
/* Clear the glyph. This happens if the glyph at this code point
|
|
|
|
* is non-standard and should be accessed by an SCC_xxx enum
|
|
|
|
* entry only. */
|
|
|
|
this->SetUnicodeGlyph(_default_unicode_map[i].code, 0);
|
|
|
|
} else {
|
|
|
|
SpriteID sprite = base + key - ASCII_LETTERSTART;
|
|
|
|
this->SetUnicodeGlyph(_default_unicode_map[i].code, sprite);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-23 15:25:35 +00:00
|
|
|
/**
|
|
|
|
* Clear the glyph to sprite mapping.
|
|
|
|
*/
|
|
|
|
void SpriteFontCache::ClearGlyphToSpriteMap()
|
|
|
|
{
|
|
|
|
if (this->glyph_to_spriteid_map == NULL) return;
|
|
|
|
|
|
|
|
for (uint i = 0; i < 256; i++) {
|
|
|
|
free(this->glyph_to_spriteid_map[i]);
|
|
|
|
}
|
|
|
|
free(this->glyph_to_spriteid_map);
|
|
|
|
this->glyph_to_spriteid_map = NULL;
|
|
|
|
}
|
|
|
|
|
2013-06-23 15:24:36 +00:00
|
|
|
void SpriteFontCache::ClearFontCache() {}
|
|
|
|
|
|
|
|
const Sprite *SpriteFontCache::GetGlyph(uint32 key)
|
|
|
|
{
|
|
|
|
SpriteID sprite = this->GetUnicodeGlyph(key);
|
|
|
|
if (sprite == 0) sprite = this->GetUnicodeGlyph('?');
|
|
|
|
return GetSprite(sprite, ST_FONT);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint SpriteFontCache::GetGlyphWidth(uint32 key)
|
|
|
|
{
|
|
|
|
SpriteID sprite = this->GetUnicodeGlyph(key);
|
|
|
|
if (sprite == 0) sprite = this->GetUnicodeGlyph('?');
|
|
|
|
return SpriteExists(sprite) ? GetSprite(sprite, ST_FONT)->width + (this->fs != FS_NORMAL) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SpriteFontCache::GetDrawGlyphShadow()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*static */ FontCache *FontCache::caches[FS_END] = { new SpriteFontCache(FS_NORMAL), new SpriteFontCache(FS_SMALL), new SpriteFontCache(FS_LARGE), new SpriteFontCache(FS_MONO) };
|
|
|
|
|
2008-01-13 01:21:35 +00:00
|
|
|
#ifdef WITH_FREETYPE
|
2006-11-16 22:05:33 +00:00
|
|
|
#include <ft2build.h>
|
|
|
|
#include FT_FREETYPE_H
|
|
|
|
#include FT_GLYPH_H
|
2013-04-06 22:06:44 +00:00
|
|
|
#include FT_TRUETYPE_TABLES_H
|
2006-11-16 22:05:33 +00:00
|
|
|
|
2013-06-23 15:24:36 +00:00
|
|
|
/** Font cache for fonts that are based on a freetype font. */
|
|
|
|
class FreeTypeFontCache : public FontCache {
|
2013-06-23 15:29:20 +00:00
|
|
|
private:
|
|
|
|
FT_Face face; ///< The font face associated with this font.
|
2013-06-23 15:32:09 +00:00
|
|
|
int ascender; ///< The ascender value of this font.
|
|
|
|
int descender; ///< The descender value of this font.
|
2013-06-23 15:35:06 +00:00
|
|
|
|
|
|
|
/** 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?
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
GlyphEntry **glyph_to_sprite;
|
|
|
|
|
|
|
|
GlyphEntry *GetGlyphPtr(WChar key);
|
|
|
|
void SetGlyphPtr(WChar key, const GlyphEntry *glyph, bool duplicate = false);
|
|
|
|
|
2013-06-23 15:24:36 +00:00
|
|
|
public:
|
2013-06-23 15:29:20 +00:00
|
|
|
FreeTypeFontCache(FontSize fs, FT_Face face, int pixels);
|
|
|
|
~FreeTypeFontCache();
|
2013-06-23 15:24:36 +00:00
|
|
|
virtual SpriteID GetUnicodeGlyph(uint32 key) { return this->parent->GetUnicodeGlyph(key); }
|
|
|
|
virtual void SetUnicodeGlyph(uint32 key, SpriteID sprite) { this->parent->SetUnicodeGlyph(key, sprite); }
|
|
|
|
virtual void InitializeUnicodeGlyphMap() { this->parent->InitializeUnicodeGlyphMap(); }
|
|
|
|
virtual void ClearFontCache();
|
|
|
|
virtual const Sprite *GetGlyph(uint32 key);
|
|
|
|
virtual uint GetGlyphWidth(uint32 key);
|
|
|
|
virtual bool GetDrawGlyphShadow();
|
|
|
|
};
|
|
|
|
|
2013-06-23 15:20:23 +00:00
|
|
|
FT_Library _library = NULL;
|
2006-11-16 22:05:33 +00:00
|
|
|
|
|
|
|
FreeTypeSettings _freetype;
|
|
|
|
|
2010-05-13 10:14:29 +00:00
|
|
|
static const byte FACE_COLOUR = 1;
|
|
|
|
static const byte SHADOW_COLOUR = 2;
|
2006-11-16 22:05:33 +00:00
|
|
|
|
2013-06-23 15:29:20 +00:00
|
|
|
/**
|
|
|
|
* Create a new FreeTypeFontCache.
|
|
|
|
* @param fs The font size that is going to be cached.
|
|
|
|
* @param face The font that has to be loaded.
|
|
|
|
* @param pixels The number of pixels this font should be high.
|
|
|
|
*/
|
2013-06-23 15:35:06 +00:00
|
|
|
FreeTypeFontCache::FreeTypeFontCache(FontSize fs, FT_Face face, int pixels) : FontCache(fs), face(face), glyph_to_sprite(NULL)
|
2009-11-15 14:39:37 +00:00
|
|
|
{
|
2013-06-23 15:38:51 +00:00
|
|
|
assert(face != NULL);
|
|
|
|
|
2013-04-06 22:06:44 +00:00
|
|
|
if (pixels == 0) {
|
|
|
|
/* Try to determine a good height based on the minimal height recommended by the font. */
|
2013-06-23 15:29:20 +00:00
|
|
|
pixels = _default_font_height[this->fs];
|
2013-04-06 22:06:44 +00:00
|
|
|
|
2013-06-23 15:29:20 +00:00
|
|
|
TT_Header *head = (TT_Header *)FT_Get_Sfnt_Table(this->face, ft_sfnt_head);
|
2013-04-06 22:06:44 +00:00
|
|
|
if (head != NULL) {
|
|
|
|
/* Font height is minimum height plus the difference between the default
|
|
|
|
* height for this font size and the small size. */
|
2013-06-23 15:29:20 +00:00
|
|
|
int diff = _default_font_height[this->fs] - _default_font_height[FS_SMALL];
|
|
|
|
pixels = Clamp(min(head->Lowest_Rec_PPEM, 20) + diff, _default_font_height[this->fs], MAX_FONT_SIZE);
|
2013-04-06 22:06:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-23 15:29:20 +00:00
|
|
|
FT_Error err = FT_Set_Pixel_Sizes(this->face, 0, pixels);
|
2011-10-18 17:57:42 +00:00
|
|
|
if (err == FT_Err_Invalid_Pixel_Size) {
|
|
|
|
|
|
|
|
/* Find nearest size to that requested */
|
2013-06-23 15:29:20 +00:00
|
|
|
FT_Bitmap_Size *bs = this->face->available_sizes;
|
|
|
|
int i = this->face->num_fixed_sizes;
|
2011-10-18 17:57:42 +00:00
|
|
|
int n = bs->height;
|
|
|
|
for (; --i; bs++) {
|
|
|
|
if (abs(pixels - bs->height) < abs(pixels - n)) n = bs->height;
|
|
|
|
}
|
2009-11-15 14:39:37 +00:00
|
|
|
|
2013-06-23 15:29:20 +00:00
|
|
|
FT_Set_Pixel_Sizes(this->face, 0, n);
|
2009-11-15 14:39:37 +00:00
|
|
|
}
|
2011-10-18 17:57:42 +00:00
|
|
|
|
2013-06-23 15:32:09 +00:00
|
|
|
this->ascender = this->face->size->metrics.ascender >> 6;
|
|
|
|
this->descender = this->face->size->metrics.descender >> 6;
|
|
|
|
this->height = this->ascender - this->descender;
|
2009-11-15 14:39:37 +00:00
|
|
|
}
|
|
|
|
|
2006-11-17 22:15:55 +00:00
|
|
|
/**
|
|
|
|
* Loads the freetype font.
|
|
|
|
* First type to load the fontname as if it were a path. If that fails,
|
|
|
|
* try to resolve the filename of the font using fontconfig, where the
|
|
|
|
* format is 'font family name' or 'font family name, font style'.
|
2013-06-23 15:37:18 +00:00
|
|
|
* @param fs The font size to load.
|
2006-11-17 22:15:55 +00:00
|
|
|
*/
|
2013-06-23 15:37:18 +00:00
|
|
|
static void LoadFreeTypeFont(FontSize fs)
|
2006-11-16 22:05:33 +00:00
|
|
|
{
|
2013-06-23 20:08:13 +00:00
|
|
|
FreeTypeSubSetting *settings = NULL;
|
2013-06-23 15:37:18 +00:00
|
|
|
switch (fs) {
|
|
|
|
default: NOT_REACHED();
|
2013-06-23 20:08:13 +00:00
|
|
|
case FS_SMALL: settings = &_freetype.small; break;
|
|
|
|
case FS_NORMAL: settings = &_freetype.medium; break;
|
|
|
|
case FS_LARGE: settings = &_freetype.large; break;
|
|
|
|
case FS_MONO: settings = &_freetype.mono; break;
|
2013-06-23 15:37:18 +00:00
|
|
|
}
|
|
|
|
|
2013-06-23 20:08:13 +00:00
|
|
|
if (StrEmpty(settings->font)) return;
|
2006-11-16 22:05:33 +00:00
|
|
|
|
2013-06-23 15:37:18 +00:00
|
|
|
if (_library == NULL) {
|
|
|
|
if (FT_Init_FreeType(&_library) != FT_Err_Ok) {
|
|
|
|
ShowInfoF("Unable to initialize FreeType, using sprite fonts instead");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG(freetype, 2, "Initialized");
|
|
|
|
}
|
2006-11-16 22:05:33 +00:00
|
|
|
|
2013-06-23 15:37:18 +00:00
|
|
|
FT_Face face = NULL;
|
2013-06-23 20:08:13 +00:00
|
|
|
FT_Error error = FT_New_Face(_library, settings->font, 0, &face);
|
2006-11-17 22:15:55 +00:00
|
|
|
|
2013-06-23 20:08:13 +00:00
|
|
|
if (error != FT_Err_Ok) error = GetFontByFaceName(settings->font, &face);
|
2006-11-17 22:15:55 +00:00
|
|
|
|
2006-11-16 22:05:33 +00:00
|
|
|
if (error == FT_Err_Ok) {
|
2013-06-23 20:08:13 +00:00
|
|
|
DEBUG(freetype, 2, "Requested '%s', using '%s %s'", settings->font, face->family_name, face->style_name);
|
2006-12-03 19:03:38 +00:00
|
|
|
|
2006-11-16 22:05:33 +00:00
|
|
|
/* Attempt to select the unicode character map */
|
2013-06-23 15:37:18 +00:00
|
|
|
error = FT_Select_Charmap(face, ft_encoding_unicode);
|
|
|
|
if (error == FT_Err_Ok) goto found_face; // Success
|
2006-12-03 19:03:38 +00:00
|
|
|
|
|
|
|
if (error == FT_Err_Invalid_CharMap_Handle) {
|
2006-11-16 22:05:33 +00:00
|
|
|
/* Try to pick a different character map instead. We default to
|
|
|
|
* the first map, but platform_id 0 encoding_id 0 should also
|
|
|
|
* be unicode (strange system...) */
|
2013-06-23 15:37:18 +00:00
|
|
|
FT_CharMap found = face->charmaps[0];
|
2006-11-16 22:05:33 +00:00
|
|
|
int i;
|
|
|
|
|
2013-06-23 15:37:18 +00:00
|
|
|
for (i = 0; i < face->num_charmaps; i++) {
|
|
|
|
FT_CharMap charmap = face->charmaps[i];
|
2006-11-16 22:05:33 +00:00
|
|
|
if (charmap->platform_id == 0 && charmap->encoding_id == 0) {
|
|
|
|
found = charmap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found != NULL) {
|
2013-06-23 15:37:18 +00:00
|
|
|
error = FT_Set_Charmap(face, found);
|
|
|
|
if (error == FT_Err_Ok) goto found_face;
|
2006-11-16 22:05:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-23 15:37:18 +00:00
|
|
|
FT_Done_Face(face);
|
2006-12-04 18:57:09 +00:00
|
|
|
|
2013-06-23 15:37:18 +00:00
|
|
|
static const char *SIZE_TO_NAME[] = { "medium", "small", "large", "mono" };
|
2013-06-23 20:08:13 +00:00
|
|
|
ShowInfoF("Unable to use '%s' for %s font, FreeType reported error 0x%X, using sprite font instead", settings->font, SIZE_TO_NAME[fs], error);
|
2013-06-23 15:37:18 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
found_face:
|
2013-06-23 20:08:13 +00:00
|
|
|
new FreeTypeFontCache(fs, face, settings->size);
|
2006-11-16 22:05:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-20 08:08:13 +00:00
|
|
|
/**
|
2013-06-23 15:35:06 +00:00
|
|
|
* Free everything that was allocated for this font cache.
|
2011-11-20 08:08:13 +00:00
|
|
|
*/
|
2013-06-23 15:29:20 +00:00
|
|
|
FreeTypeFontCache::~FreeTypeFontCache()
|
2011-11-20 08:08:13 +00:00
|
|
|
{
|
2013-06-23 15:38:51 +00:00
|
|
|
FT_Done_Face(this->face);
|
2013-06-23 15:35:06 +00:00
|
|
|
this->ClearFontCache();
|
2011-11-20 08:08:13 +00:00
|
|
|
}
|
|
|
|
|
2012-02-25 17:20:02 +00:00
|
|
|
/**
|
|
|
|
* Reset cached glyphs.
|
|
|
|
*/
|
2013-06-23 15:24:36 +00:00
|
|
|
void FreeTypeFontCache::ClearFontCache()
|
2012-02-25 17:20:02 +00:00
|
|
|
{
|
2013-06-23 15:35:06 +00:00
|
|
|
if (this->glyph_to_sprite == NULL) return;
|
2006-11-16 22:05:33 +00:00
|
|
|
|
2013-06-23 15:35:06 +00:00
|
|
|
for (int i = 0; i < 256; i++) {
|
|
|
|
if (this->glyph_to_sprite[i] == NULL) continue;
|
2008-11-24 18:53:17 +00:00
|
|
|
|
|
|
|
for (int j = 0; j < 256; j++) {
|
2013-06-23 15:35:06 +00:00
|
|
|
if (this->glyph_to_sprite[i][j].duplicate) continue;
|
|
|
|
free(this->glyph_to_sprite[i][j].sprite);
|
2008-11-24 18:53:17 +00:00
|
|
|
}
|
|
|
|
|
2013-06-23 15:35:06 +00:00
|
|
|
free(this->glyph_to_sprite[i]);
|
2008-11-24 18:53:17 +00:00
|
|
|
}
|
2013-06-23 15:35:06 +00:00
|
|
|
|
|
|
|
free(this->glyph_to_sprite);
|
|
|
|
this->glyph_to_sprite = NULL;
|
2008-11-24 18:53:17 +00:00
|
|
|
}
|
2006-11-16 22:05:33 +00:00
|
|
|
|
2013-06-23 15:35:06 +00:00
|
|
|
FreeTypeFontCache::GlyphEntry *FreeTypeFontCache::GetGlyphPtr(WChar key)
|
2006-11-16 22:05:33 +00:00
|
|
|
{
|
2013-06-23 15:35:06 +00:00
|
|
|
if (this->glyph_to_sprite == NULL) return NULL;
|
|
|
|
if (this->glyph_to_sprite[GB(key, 8, 8)] == NULL) return NULL;
|
|
|
|
return &this->glyph_to_sprite[GB(key, 8, 8)][GB(key, 0, 8)];
|
2006-11-16 22:05:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-23 15:35:06 +00:00
|
|
|
void FreeTypeFontCache::SetGlyphPtr(WChar key, const GlyphEntry *glyph, bool duplicate)
|
2006-11-16 22:05:33 +00:00
|
|
|
{
|
2013-06-23 15:35:06 +00:00
|
|
|
if (this->glyph_to_sprite == NULL) {
|
|
|
|
DEBUG(freetype, 3, "Allocating root glyph cache for size %u", this->fs);
|
|
|
|
this->glyph_to_sprite = CallocT<GlyphEntry*>(256);
|
2006-11-16 22:05:33 +00:00
|
|
|
}
|
|
|
|
|
2013-06-23 15:35:06 +00:00
|
|
|
if (this->glyph_to_sprite[GB(key, 8, 8)] == NULL) {
|
|
|
|
DEBUG(freetype, 3, "Allocating glyph cache for range 0x%02X00, size %u", GB(key, 8, 8), this->fs);
|
|
|
|
this->glyph_to_sprite[GB(key, 8, 8)] = CallocT<GlyphEntry>(256);
|
2006-11-16 22:05:33 +00:00
|
|
|
}
|
|
|
|
|
2013-06-23 15:35:06 +00:00
|
|
|
DEBUG(freetype, 4, "Set glyph for unicode character 0x%04X, size %u", key, this->fs);
|
|
|
|
this->glyph_to_sprite[GB(key, 8, 8)][GB(key, 0, 8)].sprite = glyph->sprite;
|
|
|
|
this->glyph_to_sprite[GB(key, 8, 8)][GB(key, 0, 8)].width = glyph->width;
|
|
|
|
this->glyph_to_sprite[GB(key, 8, 8)][GB(key, 0, 8)].duplicate = duplicate;
|
2006-11-16 22:05:33 +00:00
|
|
|
}
|
|
|
|
|
2009-11-09 10:40:33 +00:00
|
|
|
static void *AllocateFont(size_t size)
|
2007-06-11 13:38:11 +00:00
|
|
|
{
|
2007-12-08 14:50:41 +00:00
|
|
|
return MallocT<byte>(size);
|
2007-06-11 13:38:11 +00:00
|
|
|
}
|
|
|
|
|
2006-11-16 22:05:33 +00:00
|
|
|
|
2007-06-15 16:21:56 +00:00
|
|
|
/* Check if a glyph should be rendered with antialiasing */
|
|
|
|
static bool GetFontAAState(FontSize size)
|
|
|
|
{
|
|
|
|
/* AA is only supported for 32 bpp */
|
|
|
|
if (BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() != 32) return false;
|
|
|
|
|
|
|
|
switch (size) {
|
|
|
|
default: NOT_REACHED();
|
2013-06-23 15:23:22 +00:00
|
|
|
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;
|
2007-06-15 16:21:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-23 15:24:36 +00:00
|
|
|
const Sprite *FreeTypeFontCache::GetGlyph(WChar key)
|
2006-11-16 22:05:33 +00:00
|
|
|
{
|
|
|
|
assert(IsPrintable(key));
|
|
|
|
|
2013-06-23 15:38:51 +00:00
|
|
|
/* Bail out for our special characters */
|
|
|
|
if (key >= SCC_SPRITE_START && key <= SCC_SPRITE_END) {
|
|
|
|
return parent->GetGlyph(key);
|
2006-11-16 22:05:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for the glyph in our cache */
|
2013-06-23 15:38:51 +00:00
|
|
|
GlyphEntry *glyph = this->GetGlyphPtr(key);
|
2006-11-16 22:05:33 +00:00
|
|
|
if (glyph != NULL && glyph->sprite != NULL) return glyph->sprite;
|
|
|
|
|
2013-06-23 15:38:51 +00:00
|
|
|
FT_GlyphSlot slot = this->face->glyph;
|
2006-11-16 22:05:33 +00:00
|
|
|
|
2013-06-23 15:24:36 +00:00
|
|
|
bool aa = GetFontAAState(this->fs);
|
2007-06-15 16:21:56 +00:00
|
|
|
|
2013-06-23 15:29:20 +00:00
|
|
|
FT_UInt glyph_index = FT_Get_Char_Index(this->face, key);
|
2013-06-23 15:38:51 +00:00
|
|
|
GlyphEntry new_glyph;
|
2010-11-23 17:59:50 +00:00
|
|
|
if (glyph_index == 0) {
|
2011-01-14 16:39:41 +00:00
|
|
|
if (key == '?') {
|
|
|
|
/* The font misses the '?' character. Use sprite font. */
|
2013-06-23 15:24:36 +00:00
|
|
|
SpriteID sprite = this->GetUnicodeGlyph(key);
|
2011-01-14 16:39:41 +00:00
|
|
|
Sprite *spr = (Sprite*)GetRawSprite(sprite, ST_FONT, AllocateFont);
|
|
|
|
assert(spr != NULL);
|
|
|
|
new_glyph.sprite = spr;
|
2013-06-23 15:24:36 +00:00
|
|
|
new_glyph.width = spr->width + (this->fs != FS_NORMAL);
|
2013-06-23 15:35:06 +00:00
|
|
|
this->SetGlyphPtr(key, &new_glyph, false);
|
2011-01-14 16:39:41 +00:00
|
|
|
return new_glyph.sprite;
|
|
|
|
} else {
|
|
|
|
/* Use '?' for missing characters. */
|
2013-06-23 15:24:36 +00:00
|
|
|
this->GetGlyph('?');
|
2013-06-23 15:35:06 +00:00
|
|
|
glyph = this->GetGlyphPtr('?');
|
|
|
|
this->SetGlyphPtr(key, glyph, true);
|
2011-01-14 16:39:41 +00:00
|
|
|
return glyph->sprite;
|
|
|
|
}
|
2010-11-23 17:59:50 +00:00
|
|
|
}
|
2013-06-23 15:29:20 +00:00
|
|
|
FT_Load_Glyph(this->face, glyph_index, FT_LOAD_DEFAULT);
|
|
|
|
FT_Render_Glyph(this->face->glyph, aa ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO);
|
2006-11-16 22:05:33 +00:00
|
|
|
|
2008-07-26 10:21:26 +00:00
|
|
|
/* Despite requesting a normal glyph, FreeType may have returned a bitmap */
|
2009-06-20 09:35:22 +00:00
|
|
|
aa = (slot->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY);
|
2008-07-26 10:21:26 +00:00
|
|
|
|
2006-11-16 22:05:33 +00:00
|
|
|
/* Add 1 pixel for the shadow on the medium font. Our sprite must be at least 1x1 pixel */
|
2013-06-23 15:38:51 +00:00
|
|
|
int width = max(1, slot->bitmap.width + (this->fs == FS_NORMAL));
|
|
|
|
int height = max(1, slot->bitmap.rows + (this->fs == FS_NORMAL));
|
2006-11-16 22:05:33 +00:00
|
|
|
|
2011-09-02 20:16:41 +00:00
|
|
|
/* Limit glyph size to prevent overflows later on. */
|
|
|
|
if (width > 256 || height > 256) usererror("Font glyph is too large");
|
|
|
|
|
2006-11-16 22:05:33 +00:00
|
|
|
/* FreeType has rendered the glyph, now we allocate a sprite and copy the image into it */
|
2013-06-23 15:38:51 +00:00
|
|
|
SpriteLoader::Sprite sprite;
|
2012-02-04 13:29:13 +00:00
|
|
|
sprite.AllocateData(ZOOM_LVL_NORMAL, width * height);
|
2011-11-24 12:26:44 +00:00
|
|
|
sprite.type = ST_FONT;
|
2007-06-11 13:38:11 +00:00
|
|
|
sprite.width = width;
|
|
|
|
sprite.height = height;
|
|
|
|
sprite.x_offs = slot->bitmap_left;
|
2013-06-23 15:32:09 +00:00
|
|
|
sprite.y_offs = this->ascender - slot->bitmap_top;
|
2006-11-16 22:05:33 +00:00
|
|
|
|
|
|
|
/* Draw shadow for medium size */
|
2013-06-23 15:24:36 +00:00
|
|
|
if (this->fs == FS_NORMAL && !aa) {
|
2013-06-23 15:38:51 +00:00
|
|
|
for (int y = 0; y < slot->bitmap.rows; y++) {
|
|
|
|
for (int x = 0; x < slot->bitmap.width; x++) {
|
2007-11-19 21:02:30 +00:00
|
|
|
if (aa ? (slot->bitmap.buffer[x + y * slot->bitmap.pitch] > 0) : HasBit(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
|
2007-06-11 13:38:11 +00:00
|
|
|
sprite.data[1 + x + (1 + y) * sprite.width].m = SHADOW_COLOUR;
|
2007-06-15 16:21:56 +00:00
|
|
|
sprite.data[1 + x + (1 + y) * sprite.width].a = aa ? slot->bitmap.buffer[x + y * slot->bitmap.pitch] : 0xFF;
|
2006-11-16 22:05:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-23 15:38:51 +00:00
|
|
|
for (int y = 0; y < slot->bitmap.rows; y++) {
|
|
|
|
for (int x = 0; x < slot->bitmap.width; x++) {
|
2007-11-19 21:02:30 +00:00
|
|
|
if (aa ? (slot->bitmap.buffer[x + y * slot->bitmap.pitch] > 0) : HasBit(slot->bitmap.buffer[(x / 8) + y * slot->bitmap.pitch], 7 - (x % 8))) {
|
2007-06-11 13:38:11 +00:00
|
|
|
sprite.data[x + y * sprite.width].m = FACE_COLOUR;
|
2007-06-15 16:21:56 +00:00
|
|
|
sprite.data[x + y * sprite.width].a = aa ? slot->bitmap.buffer[x + y * slot->bitmap.pitch] : 0xFF;
|
2006-11-16 22:05:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-11 13:38:11 +00:00
|
|
|
new_glyph.sprite = BlitterFactoryBase::GetCurrentBlitter()->Encode(&sprite, AllocateFont);
|
2010-01-04 16:37:53 +00:00
|
|
|
new_glyph.width = slot->advance.x >> 6;
|
2006-11-16 22:05:33 +00:00
|
|
|
|
2013-06-23 15:35:06 +00:00
|
|
|
this->SetGlyphPtr(key, &new_glyph);
|
2006-11-16 22:05:33 +00:00
|
|
|
|
2007-06-11 13:38:11 +00:00
|
|
|
return new_glyph.sprite;
|
2006-11-16 22:05:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-23 15:24:36 +00:00
|
|
|
bool FreeTypeFontCache::GetDrawGlyphShadow()
|
2011-10-07 07:48:59 +00:00
|
|
|
{
|
2013-06-23 15:24:36 +00:00
|
|
|
return GetFontAAState(FS_NORMAL);
|
2011-10-07 07:48:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-23 15:24:36 +00:00
|
|
|
uint FreeTypeFontCache::GetGlyphWidth(WChar key)
|
2006-11-16 22:05:33 +00:00
|
|
|
{
|
2013-06-23 15:38:51 +00:00
|
|
|
if (key >= SCC_SPRITE_START && key <= SCC_SPRITE_END) {
|
|
|
|
return this->parent->GetGlyphWidth(key);
|
2006-11-16 22:05:33 +00:00
|
|
|
}
|
|
|
|
|
2013-06-23 15:38:51 +00:00
|
|
|
GlyphEntry *glyph = this->GetGlyphPtr(key);
|
2006-11-16 22:05:33 +00:00
|
|
|
if (glyph == NULL || glyph->sprite == NULL) {
|
2013-06-23 15:24:36 +00:00
|
|
|
this->GetGlyph(key);
|
2013-06-23 15:35:06 +00:00
|
|
|
glyph = this->GetGlyphPtr(key);
|
2006-11-16 22:05:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return glyph->width;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* WITH_FREETYPE */
|
2013-06-23 15:37:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* (Re)initialize the freetype related things, i.e. load the non-sprite fonts.
|
|
|
|
* @param monospace Whether to initialise the monospace or regular fonts.
|
|
|
|
*/
|
|
|
|
void InitFreeType(bool monospace)
|
|
|
|
{
|
|
|
|
for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
|
|
|
|
if (monospace != (fs == FS_MONO)) continue;
|
|
|
|
|
|
|
|
FontCache *fc = FontCache::Get(fs);
|
|
|
|
if (fc->HasParent()) delete fc;
|
|
|
|
|
|
|
|
#ifdef WITH_FREETYPE
|
|
|
|
LoadFreeTypeFont(fs);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Free everything allocated w.r.t. fonts.
|
|
|
|
*/
|
|
|
|
void UninitFreeType()
|
|
|
|
{
|
|
|
|
for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
|
|
|
|
FontCache *fc = FontCache::Get(fs);
|
|
|
|
if (fc->HasParent()) delete fc;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef WITH_FREETYPE
|
|
|
|
FT_Done_FreeType(_library);
|
|
|
|
_library = NULL;
|
|
|
|
#endif /* WITH_FREETYPE */
|
|
|
|
}
|