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"
|
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
|
|
|
|
2009-03-25 21:35:22 +00:00
|
|
|
/** Semi-constant for the height of the different sizes of fonts. */
|
|
|
|
int _font_height[FS_END];
|
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
|
|
|
|
2011-11-20 11:56:51 +00:00
|
|
|
/**
|
|
|
|
* Reset the font sizes to the defaults of the sprite based fonts.
|
2011-11-20 11:59:36 +00:00
|
|
|
* @param monospace Whether to reset the monospace or regular fonts.
|
2011-11-20 11:56:51 +00:00
|
|
|
*/
|
2011-11-20 11:59:36 +00:00
|
|
|
void ResetFontSizes(bool monospace)
|
2011-11-20 08:08:13 +00:00
|
|
|
{
|
2011-11-20 11:59:36 +00:00
|
|
|
if (monospace) {
|
2013-04-06 22:06:44 +00:00
|
|
|
_font_height[FS_MONO] = _default_font_height[FS_MONO];
|
2011-11-20 11:59:36 +00:00
|
|
|
} else {
|
2013-04-06 22:06:44 +00:00
|
|
|
_font_height[FS_SMALL] = _default_font_height[FS_SMALL];
|
|
|
|
_font_height[FS_NORMAL] = _default_font_height[FS_NORMAL];
|
|
|
|
_font_height[FS_LARGE] = _default_font_height[FS_LARGE];
|
2011-11-20 11:59:36 +00:00
|
|
|
}
|
2011-11-20 08:08:13 +00:00
|
|
|
}
|
|
|
|
|
2013-06-23 15:24:36 +00:00
|
|
|
/**
|
|
|
|
* Create a new font cache.
|
|
|
|
* @param fs The size of the font.
|
|
|
|
*/
|
|
|
|
FontCache::FontCache(FontSize fs) : parent(FontCache::Get(fs)), fs(fs)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Font cache for fonts that are based on a freetype font. */
|
|
|
|
class SpriteFontCache : public FontCache {
|
|
|
|
public:
|
|
|
|
SpriteFontCache(FontSize fs) : FontCache(fs) {}
|
|
|
|
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();
|
|
|
|
};
|
|
|
|
|
|
|
|
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 {
|
|
|
|
public:
|
|
|
|
FreeTypeFontCache(FontSize fs) : FontCache(fs) {}
|
|
|
|
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
|
|
|
static FT_Face _face_small = NULL;
|
|
|
|
static FT_Face _face_medium = NULL;
|
|
|
|
static FT_Face _face_large = NULL;
|
2011-11-20 11:56:51 +00:00
|
|
|
static FT_Face _face_mono = NULL;
|
2009-11-15 14:39:37 +00:00
|
|
|
static int _ascender[FS_END];
|
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
|
|
|
|
2009-11-15 14:39:37 +00:00
|
|
|
static void SetFontGeometry(FT_Face face, FontSize size, int pixels)
|
|
|
|
{
|
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. */
|
|
|
|
pixels = _default_font_height[size];
|
|
|
|
|
|
|
|
TT_Header *head = (TT_Header *)FT_Get_Sfnt_Table(face, ft_sfnt_head);
|
|
|
|
if (head != NULL) {
|
|
|
|
/* Font height is minimum height plus the difference between the default
|
|
|
|
* height for this font size and the small size. */
|
|
|
|
int diff = _default_font_height[size] - _default_font_height[FS_SMALL];
|
|
|
|
pixels = Clamp(min(head->Lowest_Rec_PPEM, 20) + diff, _default_font_height[size], MAX_FONT_SIZE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-18 17:57:42 +00:00
|
|
|
FT_Error err = FT_Set_Pixel_Sizes(face, 0, pixels);
|
|
|
|
if (err == FT_Err_Invalid_Pixel_Size) {
|
|
|
|
|
|
|
|
/* Find nearest size to that requested */
|
|
|
|
FT_Bitmap_Size *bs = face->available_sizes;
|
|
|
|
int i = face->num_fixed_sizes;
|
|
|
|
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
|
|
|
|
2011-10-18 17:57:42 +00:00
|
|
|
FT_Set_Pixel_Sizes(face, 0, n);
|
2009-11-15 14:39:37 +00:00
|
|
|
}
|
2011-10-18 17:57:42 +00:00
|
|
|
|
|
|
|
int asc = face->size->metrics.ascender >> 6;
|
|
|
|
int dec = face->size->metrics.descender >> 6;
|
|
|
|
|
|
|
|
_ascender[size] = asc;
|
|
|
|
_font_height[size] = asc - dec;
|
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'.
|
|
|
|
*/
|
2006-11-16 22:05:33 +00:00
|
|
|
static void LoadFreeTypeFont(const char *font_name, FT_Face *face, const char *type)
|
|
|
|
{
|
|
|
|
FT_Error error;
|
|
|
|
|
2007-01-13 15:00:16 +00:00
|
|
|
if (StrEmpty(font_name)) return;
|
2006-11-16 22:05:33 +00:00
|
|
|
|
|
|
|
error = FT_New_Face(_library, font_name, 0, face);
|
2006-11-17 22:15:55 +00:00
|
|
|
|
2006-12-03 19:03:38 +00:00
|
|
|
if (error != FT_Err_Ok) error = GetFontByFaceName(font_name, face);
|
2006-11-17 22:15:55 +00:00
|
|
|
|
2006-11-16 22:05:33 +00:00
|
|
|
if (error == FT_Err_Ok) {
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(freetype, 2, "Requested '%s', using '%s %s'", font_name, (*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 */
|
|
|
|
error = FT_Select_Charmap(*face, ft_encoding_unicode);
|
2006-12-03 19:03:38 +00:00
|
|
|
if (error == FT_Err_Ok) return; // Success
|
|
|
|
|
|
|
|
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...) */
|
|
|
|
FT_CharMap found = (*face)->charmaps[0];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < (*face)->num_charmaps; i++) {
|
|
|
|
FT_CharMap charmap = (*face)->charmaps[i];
|
|
|
|
if (charmap->platform_id == 0 && charmap->encoding_id == 0) {
|
|
|
|
found = charmap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found != NULL) {
|
|
|
|
error = FT_Set_Charmap(*face, found);
|
|
|
|
if (error == FT_Err_Ok) return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-04 18:57:09 +00:00
|
|
|
FT_Done_Face(*face);
|
|
|
|
*face = NULL;
|
|
|
|
|
2006-11-16 22:05:33 +00:00
|
|
|
ShowInfoF("Unable to use '%s' for %s font, FreeType reported error 0x%X, using sprite font instead", font_name, type, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-20 11:59:36 +00:00
|
|
|
static void ResetGlyphCache(bool monospace);
|
2011-11-20 08:08:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Unload a face and set it to NULL.
|
|
|
|
* @param face the face to unload
|
|
|
|
*/
|
|
|
|
static void UnloadFace(FT_Face *face)
|
|
|
|
{
|
|
|
|
if (*face == NULL) return;
|
|
|
|
|
|
|
|
FT_Done_Face(*face);
|
|
|
|
*face = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* (Re)initialize the freetype related things, i.e. load the non-sprite fonts.
|
2011-11-20 11:59:36 +00:00
|
|
|
* @param monospace Whether to initialise the monospace or regular fonts.
|
2011-11-20 08:08:13 +00:00
|
|
|
*/
|
2011-11-20 11:59:36 +00:00
|
|
|
void InitFreeType(bool monospace)
|
2006-11-16 22:05:33 +00:00
|
|
|
{
|
2011-11-20 11:59:36 +00:00
|
|
|
ResetFontSizes(monospace);
|
|
|
|
ResetGlyphCache(monospace);
|
2011-11-20 08:08:13 +00:00
|
|
|
|
2011-11-20 11:59:36 +00:00
|
|
|
if (monospace) {
|
|
|
|
UnloadFace(&_face_mono);
|
|
|
|
} else {
|
|
|
|
UnloadFace(&_face_small);
|
|
|
|
UnloadFace(&_face_medium);
|
|
|
|
UnloadFace(&_face_large);
|
|
|
|
}
|
2009-03-25 20:01:34 +00:00
|
|
|
|
2013-06-23 15:23:22 +00:00
|
|
|
if (StrEmpty(_freetype.small.font) && StrEmpty(_freetype.medium.font) && StrEmpty(_freetype.large.font) && StrEmpty(_freetype.mono.font)) {
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(freetype, 1, "No font faces specified, using sprite fonts instead");
|
2006-11-16 22:05:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-11-20 08:08:13 +00:00
|
|
|
if (_library == NULL) {
|
|
|
|
if (FT_Init_FreeType(&_library) != FT_Err_Ok) {
|
|
|
|
ShowInfoF("Unable to initialize FreeType, using sprite fonts instead");
|
|
|
|
return;
|
|
|
|
}
|
2006-11-16 22:05:33 +00:00
|
|
|
|
2011-11-20 08:08:13 +00:00
|
|
|
DEBUG(freetype, 2, "Initialized");
|
|
|
|
}
|
2006-11-16 22:05:33 +00:00
|
|
|
|
|
|
|
/* Load each font */
|
2011-11-20 11:59:36 +00:00
|
|
|
if (monospace) {
|
2013-06-23 15:23:22 +00:00
|
|
|
LoadFreeTypeFont(_freetype.mono.font , &_face_mono, "mono");
|
2011-11-20 11:59:36 +00:00
|
|
|
|
|
|
|
if (_face_mono != NULL) {
|
2013-06-23 15:24:36 +00:00
|
|
|
new FreeTypeFontCache(FS_MONO);
|
2013-06-23 15:23:22 +00:00
|
|
|
SetFontGeometry(_face_mono, FS_MONO, _freetype.mono.size);
|
2011-11-20 11:59:36 +00:00
|
|
|
}
|
|
|
|
} else {
|
2013-06-23 15:23:22 +00:00
|
|
|
LoadFreeTypeFont(_freetype.small.font, &_face_small, "small");
|
|
|
|
LoadFreeTypeFont(_freetype.medium.font, &_face_medium, "medium");
|
|
|
|
LoadFreeTypeFont(_freetype.large.font, &_face_large, "large");
|
2011-11-20 11:59:36 +00:00
|
|
|
|
|
|
|
/* Set each font size */
|
|
|
|
if (_face_small != NULL) {
|
2013-06-23 15:24:36 +00:00
|
|
|
new FreeTypeFontCache(FS_SMALL);
|
2013-06-23 15:23:22 +00:00
|
|
|
SetFontGeometry(_face_small, FS_SMALL, _freetype.small.size);
|
2011-11-20 11:59:36 +00:00
|
|
|
}
|
|
|
|
if (_face_medium != NULL) {
|
2013-06-23 15:24:36 +00:00
|
|
|
new FreeTypeFontCache(FS_NORMAL);
|
2013-06-23 15:23:22 +00:00
|
|
|
SetFontGeometry(_face_medium, FS_NORMAL, _freetype.medium.size);
|
2011-11-20 11:59:36 +00:00
|
|
|
}
|
|
|
|
if (_face_large != NULL) {
|
2013-06-23 15:24:36 +00:00
|
|
|
new FreeTypeFontCache(FS_LARGE);
|
2013-06-23 15:23:22 +00:00
|
|
|
SetFontGeometry(_face_large, FS_LARGE, _freetype.large.size);
|
2011-11-20 11:59:36 +00:00
|
|
|
}
|
2009-03-25 20:01:34 +00:00
|
|
|
}
|
2006-11-16 22:05:33 +00:00
|
|
|
}
|
|
|
|
|
2008-11-24 18:53:17 +00:00
|
|
|
/**
|
|
|
|
* Free everything allocated w.r.t. fonts.
|
|
|
|
*/
|
|
|
|
void UninitFreeType()
|
|
|
|
{
|
2011-11-20 11:59:36 +00:00
|
|
|
ResetGlyphCache(true);
|
|
|
|
ResetGlyphCache(false);
|
2008-11-24 18:53:17 +00:00
|
|
|
|
|
|
|
UnloadFace(&_face_small);
|
|
|
|
UnloadFace(&_face_medium);
|
|
|
|
UnloadFace(&_face_large);
|
2011-11-20 11:56:51 +00:00
|
|
|
UnloadFace(&_face_mono);
|
2008-11-24 18:53:17 +00:00
|
|
|
|
2013-06-23 15:24:36 +00:00
|
|
|
for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
|
|
|
|
FontCache *fc = FontCache::Get(fs);
|
|
|
|
if (fc->HasParent()) delete fc;
|
|
|
|
}
|
|
|
|
|
2008-11-24 18:53:17 +00:00
|
|
|
FT_Done_FreeType(_library);
|
|
|
|
_library = NULL;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
ResetGlyphCache(true);
|
|
|
|
ResetGlyphCache(false);
|
|
|
|
}
|
2006-11-16 22:05:33 +00:00
|
|
|
|
|
|
|
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;
|
2011-11-20 11:56:51 +00:00
|
|
|
case FS_MONO: return _face_mono;
|
2006-11-16 22:05:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-07 12:11:48 +00:00
|
|
|
struct GlyphEntry {
|
2006-11-16 22:05:33 +00:00
|
|
|
Sprite *sprite;
|
|
|
|
byte width;
|
2010-11-23 17:59:50 +00:00
|
|
|
bool duplicate;
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2006-11-16 22:05:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* 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_ptr[FS_NORMAL][0x00][0x41]
|
|
|
|
* For character 0x20AC (Euro): _glyph_ptr[FS_NORMAL][0x20][0xAC]
|
|
|
|
*
|
|
|
|
* Currently only 256 segments are allocated, "limiting" us to 65536 characters.
|
|
|
|
* This can be simply changed in the two functions Get & SetGlyphPtr.
|
|
|
|
*/
|
|
|
|
static GlyphEntry **_glyph_ptr[FS_END];
|
|
|
|
|
2011-11-20 11:59:36 +00:00
|
|
|
/**
|
|
|
|
* Clear the complete cache
|
|
|
|
* @param monospace Whether to reset the monospace or regular font.
|
|
|
|
*/
|
|
|
|
static void ResetGlyphCache(bool monospace)
|
2008-11-24 18:53:17 +00:00
|
|
|
{
|
2009-06-20 15:05:59 +00:00
|
|
|
for (FontSize i = FS_BEGIN; i < FS_END; i++) {
|
2011-11-20 11:59:36 +00:00
|
|
|
if (monospace != (i == FS_MONO)) continue;
|
2008-11-24 18:53:17 +00:00
|
|
|
if (_glyph_ptr[i] == NULL) continue;
|
|
|
|
|
|
|
|
for (int j = 0; j < 256; j++) {
|
|
|
|
if (_glyph_ptr[i][j] == NULL) continue;
|
|
|
|
|
|
|
|
for (int k = 0; k < 256; k++) {
|
2010-11-23 17:59:50 +00:00
|
|
|
if (_glyph_ptr[i][j][k].duplicate) continue;
|
2008-11-24 18:53:17 +00:00
|
|
|
free(_glyph_ptr[i][j][k].sprite);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(_glyph_ptr[i][j]);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(_glyph_ptr[i]);
|
|
|
|
_glyph_ptr[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
2006-11-16 22:05:33 +00:00
|
|
|
|
|
|
|
static GlyphEntry *GetGlyphPtr(FontSize size, WChar key)
|
|
|
|
{
|
|
|
|
if (_glyph_ptr[size] == NULL) return NULL;
|
|
|
|
if (_glyph_ptr[size][GB(key, 8, 8)] == NULL) return NULL;
|
|
|
|
return &_glyph_ptr[size][GB(key, 8, 8)][GB(key, 0, 8)];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-23 17:59:50 +00:00
|
|
|
static void SetGlyphPtr(FontSize size, WChar key, const GlyphEntry *glyph, bool duplicate = false)
|
2006-11-16 22:05:33 +00:00
|
|
|
{
|
|
|
|
if (_glyph_ptr[size] == NULL) {
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(freetype, 3, "Allocating root glyph cache for size %u", size);
|
2007-01-11 17:29:39 +00:00
|
|
|
_glyph_ptr[size] = CallocT<GlyphEntry*>(256);
|
2006-11-16 22:05:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_glyph_ptr[size][GB(key, 8, 8)] == NULL) {
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(freetype, 3, "Allocating glyph cache for range 0x%02X00, size %u", GB(key, 8, 8), size);
|
2007-01-11 17:29:39 +00:00
|
|
|
_glyph_ptr[size][GB(key, 8, 8)] = CallocT<GlyphEntry>(256);
|
2006-11-16 22:05:33 +00:00
|
|
|
}
|
|
|
|
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(freetype, 4, "Set glyph for unicode character 0x%04X, size %u", key, size);
|
2010-11-23 17:59:50 +00:00
|
|
|
_glyph_ptr[size][GB(key, 8, 8)][GB(key, 0, 8)].sprite = glyph->sprite;
|
|
|
|
_glyph_ptr[size][GB(key, 8, 8)][GB(key, 0, 8)].width = glyph->width;
|
|
|
|
_glyph_ptr[size][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
|
|
|
{
|
2013-06-23 15:24:36 +00:00
|
|
|
FT_Face face = GetFontFace(this->fs);
|
2006-11-16 22:05:33 +00:00
|
|
|
FT_GlyphSlot slot;
|
|
|
|
GlyphEntry new_glyph;
|
|
|
|
GlyphEntry *glyph;
|
2007-06-11 13:38:11 +00:00
|
|
|
SpriteLoader::Sprite sprite;
|
2006-11-16 22:05:33 +00:00
|
|
|
int width;
|
|
|
|
int height;
|
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
|
|
|
|
assert(IsPrintable(key));
|
|
|
|
|
|
|
|
/* Bail out if no face loaded, or for our special characters */
|
|
|
|
if (face == NULL || (key >= SCC_SPRITE_START && key <= SCC_SPRITE_END)) {
|
2013-06-23 15:24:36 +00:00
|
|
|
SpriteID sprite = this->GetUnicodeGlyph(key);
|
|
|
|
if (sprite == 0) sprite = this->GetUnicodeGlyph('?');
|
2011-11-17 21:13:05 +00:00
|
|
|
|
|
|
|
/* Load the sprite if it's known. */
|
|
|
|
if (sprite != 0) return GetSprite(sprite, ST_FONT);
|
|
|
|
|
|
|
|
/* For the 'rare' case there is no font available at all. */
|
|
|
|
if (face == NULL) error("No sprite font and no real font either... bailing!");
|
|
|
|
|
|
|
|
/* Use the '?' from the freetype font. */
|
|
|
|
key = '?';
|
2006-11-16 22:05:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for the glyph in our cache */
|
2013-06-23 15:24:36 +00:00
|
|
|
glyph = GetGlyphPtr(this->fs, key);
|
2006-11-16 22:05:33 +00:00
|
|
|
if (glyph != NULL && glyph->sprite != NULL) return glyph->sprite;
|
|
|
|
|
|
|
|
slot = face->glyph;
|
|
|
|
|
2013-06-23 15:24:36 +00:00
|
|
|
bool aa = GetFontAAState(this->fs);
|
2007-06-15 16:21:56 +00:00
|
|
|
|
2010-11-23 17:59:50 +00:00
|
|
|
FT_UInt glyph_index = FT_Get_Char_Index(face, key);
|
|
|
|
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);
|
|
|
|
SetGlyphPtr(this->fs, 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('?');
|
|
|
|
glyph = GetGlyphPtr(this->fs, '?');
|
|
|
|
SetGlyphPtr(this->fs, key, glyph, true);
|
2011-01-14 16:39:41 +00:00
|
|
|
return glyph->sprite;
|
|
|
|
}
|
2010-11-23 17:59:50 +00:00
|
|
|
}
|
|
|
|
FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT);
|
2007-06-15 16:21:56 +00:00
|
|
|
FT_Render_Glyph(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:24:36 +00:00
|
|
|
width = max(1, slot->bitmap.width + (this->fs == FS_NORMAL));
|
|
|
|
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 */
|
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:24:36 +00:00
|
|
|
sprite.y_offs = _ascender[this->fs] - 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) {
|
2006-11-16 22:05:33 +00:00
|
|
|
for (y = 0; y < slot->bitmap.rows; y++) {
|
|
|
|
for (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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (y = 0; y < slot->bitmap.rows; y++) {
|
|
|
|
for (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:24:36 +00:00
|
|
|
SetGlyphPtr(this->fs, 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:24:36 +00:00
|
|
|
FT_Face face = GetFontFace(this->fs);
|
2006-11-16 22:05:33 +00:00
|
|
|
GlyphEntry *glyph;
|
|
|
|
|
|
|
|
if (face == NULL || (key >= SCC_SPRITE_START && key <= SCC_SPRITE_END)) {
|
2013-06-23 15:24:36 +00:00
|
|
|
SpriteID sprite = this->GetUnicodeGlyph(key);
|
|
|
|
if (sprite == 0) sprite = this->GetUnicodeGlyph('?');
|
|
|
|
return SpriteExists(sprite) ? GetSprite(sprite, ST_FONT)->width + (this->fs != FS_NORMAL && this->fs != FS_MONO) : 0;
|
2006-11-16 22:05:33 +00:00
|
|
|
}
|
|
|
|
|
2013-06-23 15:24:36 +00:00
|
|
|
glyph = GetGlyphPtr(this->fs, 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);
|
|
|
|
glyph = GetGlyphPtr(this->fs, key);
|
2006-11-16 22:05:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return glyph->width;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* WITH_FREETYPE */
|
|
|
|
|
|
|
|
/* Sprite based glyph mapping */
|
|
|
|
|
|
|
|
#include "table/unicode.h"
|
|
|
|
|
|
|
|
static SpriteID **_unicode_glyph_map[FS_END];
|
|
|
|
|
|
|
|
|
|
|
|
/** Get the SpriteID of the first glyph for the given font size */
|
|
|
|
static SpriteID GetFontBase(FontSize size)
|
|
|
|
{
|
|
|
|
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;
|
2011-11-20 11:56:51 +00:00
|
|
|
case FS_MONO: return SPR_ASCII_SPACE;
|
2006-11-16 22:05:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-23 15:24:36 +00:00
|
|
|
SpriteID SpriteFontCache::GetUnicodeGlyph(uint32 key)
|
2006-11-16 22:05:33 +00:00
|
|
|
{
|
2013-06-23 15:24:36 +00:00
|
|
|
if (_unicode_glyph_map[this->fs][GB(key, 8, 8)] == NULL) return 0;
|
|
|
|
return _unicode_glyph_map[this->fs][GB(key, 8, 8)][GB(key, 0, 8)];
|
2006-11-16 22:05:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-23 15:24:36 +00:00
|
|
|
void SpriteFontCache::SetUnicodeGlyph(uint32 key, SpriteID sprite)
|
2006-11-16 22:05:33 +00:00
|
|
|
{
|
2013-06-23 15:24:36 +00:00
|
|
|
if (_unicode_glyph_map[this->fs] == NULL) _unicode_glyph_map[this->fs] = CallocT<SpriteID*>(256);
|
|
|
|
if (_unicode_glyph_map[this->fs][GB(key, 8, 8)] == NULL) _unicode_glyph_map[this->fs][GB(key, 8, 8)] = CallocT<SpriteID>(256);
|
|
|
|
_unicode_glyph_map[this->fs][GB(key, 8, 8)][GB(key, 0, 8)] = sprite;
|
2006-11-16 22:05:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-23 15:24:36 +00:00
|
|
|
void SpriteFontCache::InitializeUnicodeGlyphMap()
|
2006-11-16 22:05:33 +00:00
|
|
|
{
|
2013-06-23 15:24:36 +00:00
|
|
|
/* Clear out existing glyph map if it exists */
|
|
|
|
if (_unicode_glyph_map[this->fs] != NULL) {
|
|
|
|
for (uint i = 0; i < 256; i++) {
|
|
|
|
free(_unicode_glyph_map[this->fs][i]);
|
2006-11-24 18:35:59 +00:00
|
|
|
}
|
2013-06-23 15:24:36 +00:00
|
|
|
free(_unicode_glyph_map[this->fs]);
|
|
|
|
_unicode_glyph_map[this->fs] = NULL;
|
|
|
|
}
|
2006-11-24 18:35:59 +00:00
|
|
|
|
2013-06-23 15:24:36 +00:00
|
|
|
SpriteID base = GetFontBase(this->fs);
|
2008-01-25 13:48:39 +00:00
|
|
|
|
2013-06-23 15:24:36 +00:00
|
|
|
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);
|
|
|
|
}
|
2008-01-25 13:48:39 +00:00
|
|
|
|
2013-06-23 15:24:36 +00:00
|
|
|
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);
|
2006-11-16 22:05:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|