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"
|
2021-02-13 16:34:22 +00:00
|
|
|
#include "fontcache_internal.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"
|
2013-06-25 20:39:58 +00:00
|
|
|
#include "core/smallmap_type.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"
|
2013-07-06 18:56:23 +00:00
|
|
|
#include "gfx_layout.h"
|
2014-10-12 20:43:25 +00:00
|
|
|
#include "zoom_func.h"
|
2019-05-05 00:47:32 +00:00
|
|
|
#include "fileio_func.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
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
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
|
|
|
/** Default heights for the different sizes of fonts. */
|
2013-06-25 20:21:21 +00:00
|
|
|
static const int _default_font_height[FS_END] = {10, 6, 18, 10};
|
|
|
|
static const int _default_font_ascender[FS_END] = { 8, 5, 15, 8};
|
2009-03-25 21:35:22 +00:00
|
|
|
|
2021-02-13 16:34:22 +00:00
|
|
|
FreeTypeSettings _freetype;
|
|
|
|
|
2013-06-23 15:24:36 +00:00
|
|
|
/**
|
|
|
|
* Create a new font cache.
|
|
|
|
* @param fs The size of the font.
|
|
|
|
*/
|
2013-06-25 20:21:21 +00:00
|
|
|
FontCache::FontCache(FontSize fs) : parent(FontCache::Get(fs)), fs(fs), height(_default_font_height[fs]),
|
|
|
|
ascender(_default_font_ascender[fs]), descender(_default_font_ascender[fs] - _default_font_height[fs]),
|
|
|
|
units_per_em(1)
|
2013-06-23 15:24:36 +00:00
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
assert(this->parent == nullptr || this->fs == this->parent->fs);
|
2013-06-23 15:24:36 +00:00
|
|
|
FontCache::caches[this->fs] = this;
|
2013-07-06 18:56:23 +00:00
|
|
|
Layouter::ResetFontCache(this->fs);
|
2013-06-23 15:24:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Clean everything up. */
|
|
|
|
FontCache::~FontCache()
|
|
|
|
{
|
2017-07-11 19:34:41 +00:00
|
|
|
assert(this->fs == this->parent->fs);
|
2013-06-23 15:24:36 +00:00
|
|
|
FontCache::caches[this->fs] = this->parent;
|
2013-07-06 18:56:23 +00:00
|
|
|
Layouter::ResetFontCache(this->fs);
|
2013-06-23 15:24:36 +00:00
|
|
|
}
|
|
|
|
|
2021-02-13 16:34:22 +00:00
|
|
|
int FontCache::GetDefaultFontHeight(FontSize fs)
|
|
|
|
{
|
|
|
|
return _default_font_height[fs];
|
|
|
|
}
|
|
|
|
|
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-25 20:20:15 +00:00
|
|
|
virtual SpriteID GetUnicodeGlyph(WChar key);
|
|
|
|
virtual void SetUnicodeGlyph(WChar key, SpriteID sprite);
|
2013-06-23 15:24:36 +00:00
|
|
|
virtual void InitializeUnicodeGlyphMap();
|
2013-10-13 13:28:06 +00:00
|
|
|
virtual void ClearFontCache();
|
2013-06-25 20:20:15 +00:00
|
|
|
virtual const Sprite *GetGlyph(GlyphID key);
|
|
|
|
virtual uint GetGlyphWidth(GlyphID key);
|
2013-06-23 15:24:36 +00:00
|
|
|
virtual bool GetDrawGlyphShadow();
|
2013-06-29 12:03:44 +00:00
|
|
|
virtual GlyphID MapCharToGlyph(WChar key) { assert(IsPrintable(key)); return SPRITE_GLYPH | key; }
|
2019-04-10 21:07:06 +00:00
|
|
|
virtual const void *GetFontTable(uint32 tag, size_t &length) { length = 0; return nullptr; }
|
2013-12-22 17:46:27 +00:00
|
|
|
virtual const char *GetFontName() { return "sprite"; }
|
2018-05-26 14:13:12 +00:00
|
|
|
virtual bool IsBuiltInFont() { return true; }
|
2013-06-23 15:24:36 +00:00
|
|
|
};
|
|
|
|
|
2013-06-23 15:25:35 +00:00
|
|
|
/**
|
|
|
|
* Create a new sprite font cache.
|
|
|
|
* @param fs The font size to create the cache for.
|
|
|
|
*/
|
2019-04-10 21:07:06 +00:00
|
|
|
SpriteFontCache::SpriteFontCache(FontSize fs) : FontCache(fs), glyph_to_spriteid_map(nullptr)
|
2013-06-23 15:25:35 +00:00
|
|
|
{
|
|
|
|
this->InitializeUnicodeGlyphMap();
|
2021-05-01 07:54:05 +00:00
|
|
|
this->height = ScaleFontTrad(this->GetDefaultFontHeight(this->fs));
|
2013-06-23 15:25:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Free everything we allocated.
|
|
|
|
*/
|
|
|
|
SpriteFontCache::~SpriteFontCache()
|
|
|
|
{
|
|
|
|
this->ClearGlyphToSpriteMap();
|
|
|
|
}
|
|
|
|
|
2020-05-17 21:31:49 +00:00
|
|
|
SpriteID SpriteFontCache::GetUnicodeGlyph(WChar key)
|
2013-06-23 15:36:29 +00:00
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
if (this->glyph_to_spriteid_map[GB(key, 8, 8)] == nullptr) return 0;
|
2013-06-23 15:36:29 +00:00
|
|
|
return this->glyph_to_spriteid_map[GB(key, 8, 8)][GB(key, 0, 8)];
|
|
|
|
}
|
|
|
|
|
2020-05-17 21:31:49 +00:00
|
|
|
void SpriteFontCache::SetUnicodeGlyph(WChar key, SpriteID sprite)
|
2013-06-23 15:36:29 +00:00
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
if (this->glyph_to_spriteid_map == nullptr) this->glyph_to_spriteid_map = CallocT<SpriteID*>(256);
|
|
|
|
if (this->glyph_to_spriteid_map[GB(key, 8, 8)] == nullptr) this->glyph_to_spriteid_map[GB(key, 8, 8)] = CallocT<SpriteID>(256);
|
2013-06-23 15:36:29 +00:00
|
|
|
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();
|
2017-08-13 18:38:42 +00:00
|
|
|
case FS_MONO: // Use normal as default for mono spaced font
|
2013-06-23 15:36:29 +00:00
|
|
|
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
|
2014-10-12 18:41:53 +00:00
|
|
|
* is non-standard and should be accessed by an SCC_xxx enum
|
|
|
|
* entry only. */
|
2013-06-23 15:36:29 +00:00
|
|
|
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()
|
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
if (this->glyph_to_spriteid_map == nullptr) return;
|
2013-06-23 15:25:35 +00:00
|
|
|
|
|
|
|
for (uint i = 0; i < 256; i++) {
|
|
|
|
free(this->glyph_to_spriteid_map[i]);
|
|
|
|
}
|
|
|
|
free(this->glyph_to_spriteid_map);
|
2019-04-10 21:07:06 +00:00
|
|
|
this->glyph_to_spriteid_map = nullptr;
|
2013-06-23 15:25:35 +00:00
|
|
|
}
|
|
|
|
|
2013-10-13 13:28:06 +00:00
|
|
|
void SpriteFontCache::ClearFontCache()
|
|
|
|
{
|
|
|
|
Layouter::ResetFontCache(this->fs);
|
2021-05-01 07:54:05 +00:00
|
|
|
this->height = ScaleFontTrad(this->GetDefaultFontHeight(this->fs));
|
2013-10-13 13:28:06 +00:00
|
|
|
}
|
|
|
|
|
2013-06-25 20:20:15 +00:00
|
|
|
const Sprite *SpriteFontCache::GetGlyph(GlyphID key)
|
2013-06-23 15:24:36 +00:00
|
|
|
{
|
|
|
|
SpriteID sprite = this->GetUnicodeGlyph(key);
|
|
|
|
if (sprite == 0) sprite = this->GetUnicodeGlyph('?');
|
|
|
|
return GetSprite(sprite, ST_FONT);
|
|
|
|
}
|
|
|
|
|
2013-06-25 20:20:15 +00:00
|
|
|
uint SpriteFontCache::GetGlyphWidth(GlyphID key)
|
2013-06-23 15:24:36 +00:00
|
|
|
{
|
|
|
|
SpriteID sprite = this->GetUnicodeGlyph(key);
|
|
|
|
if (sprite == 0) sprite = this->GetUnicodeGlyph('?');
|
2019-02-23 16:07:25 +00:00
|
|
|
return SpriteExists(sprite) ? GetSprite(sprite, ST_FONT)->width + ScaleFontTrad(this->fs != FS_NORMAL ? 1 : 0) : 0;
|
2013-06-23 15:24:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SpriteFontCache::GetDrawGlyphShadow()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-15 18:31:37 +00:00
|
|
|
/* static */ FontCache *FontCache::caches[FS_END] = { new SpriteFontCache(FS_NORMAL), new SpriteFontCache(FS_SMALL), new SpriteFontCache(FS_LARGE), new SpriteFontCache(FS_MONO) };
|
2013-06-23 15:24:36 +00:00
|
|
|
|
2006-11-16 22:05:33 +00:00
|
|
|
|
2018-11-25 00:32:48 +00:00
|
|
|
/**
|
|
|
|
* Create a new TrueTypeFontCache.
|
|
|
|
* @param fs The font size that is going to be cached.
|
|
|
|
* @param pixels The number of pixels this font should be high.
|
|
|
|
*/
|
|
|
|
TrueTypeFontCache::TrueTypeFontCache(FontSize fs, int pixels) : FontCache(fs), req_size(pixels), glyph_to_sprite(nullptr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Free everything that was allocated for this font cache.
|
|
|
|
*/
|
|
|
|
TrueTypeFontCache::~TrueTypeFontCache()
|
|
|
|
{
|
2021-05-12 17:44:00 +00:00
|
|
|
/* Virtual functions get called statically in destructors, so make it explicit to remove any confusion. */
|
|
|
|
this->TrueTypeFontCache::ClearFontCache();
|
2018-11-25 00:32:48 +00:00
|
|
|
|
|
|
|
for (auto &iter : this->font_tables) {
|
|
|
|
free(iter.second.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset cached glyphs.
|
|
|
|
*/
|
|
|
|
void TrueTypeFontCache::ClearFontCache()
|
|
|
|
{
|
|
|
|
if (this->glyph_to_sprite == nullptr) return;
|
|
|
|
|
|
|
|
for (int i = 0; i < 256; i++) {
|
|
|
|
if (this->glyph_to_sprite[i] == nullptr) continue;
|
|
|
|
|
|
|
|
for (int j = 0; j < 256; j++) {
|
|
|
|
if (this->glyph_to_sprite[i][j].duplicate) continue;
|
|
|
|
free(this->glyph_to_sprite[i][j].sprite);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(this->glyph_to_sprite[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(this->glyph_to_sprite);
|
|
|
|
this->glyph_to_sprite = nullptr;
|
|
|
|
|
|
|
|
Layouter::ResetFontCache(this->fs);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TrueTypeFontCache::GlyphEntry *TrueTypeFontCache::GetGlyphPtr(GlyphID key)
|
|
|
|
{
|
|
|
|
if (this->glyph_to_sprite == nullptr) return nullptr;
|
|
|
|
if (this->glyph_to_sprite[GB(key, 8, 8)] == nullptr) return nullptr;
|
|
|
|
return &this->glyph_to_sprite[GB(key, 8, 8)][GB(key, 0, 8)];
|
|
|
|
}
|
|
|
|
|
|
|
|
void TrueTypeFontCache::SetGlyphPtr(GlyphID key, const GlyphEntry *glyph, bool duplicate)
|
|
|
|
{
|
|
|
|
if (this->glyph_to_sprite == nullptr) {
|
2021-06-12 07:10:17 +00:00
|
|
|
Debug(freetype, 3, "Allocating root glyph cache for size {}", this->fs);
|
2018-11-25 00:32:48 +00:00
|
|
|
this->glyph_to_sprite = CallocT<GlyphEntry*>(256);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->glyph_to_sprite[GB(key, 8, 8)] == nullptr) {
|
2021-06-12 07:10:17 +00:00
|
|
|
Debug(freetype, 3, "Allocating glyph cache for range 0x{:02X}00, size {}", GB(key, 8, 8), this->fs);
|
2018-11-25 00:32:48 +00:00
|
|
|
this->glyph_to_sprite[GB(key, 8, 8)] = CallocT<GlyphEntry>(256);
|
|
|
|
}
|
|
|
|
|
2021-06-12 07:10:17 +00:00
|
|
|
Debug(freetype, 4, "Set glyph for unicode character 0x{:04X}, size {}", key, this->fs);
|
2018-11-25 00:32:48 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Check if a glyph should be rendered with anti-aliasing. */
|
2021-02-23 19:54:50 +00:00
|
|
|
static bool GetFontAAState(FontSize size, bool check_blitter = true)
|
2018-11-25 00:32:48 +00:00
|
|
|
{
|
|
|
|
/* AA is only supported for 32 bpp */
|
2021-02-23 19:54:50 +00:00
|
|
|
if (check_blitter && BlitterFactory::GetCurrentBlitter()->GetScreenDepth() != 32) return false;
|
2018-11-25 00:32:48 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TrueTypeFontCache::GetDrawGlyphShadow()
|
|
|
|
{
|
|
|
|
return this->fs == FS_NORMAL && GetFontAAState(FS_NORMAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint TrueTypeFontCache::GetGlyphWidth(GlyphID key)
|
|
|
|
{
|
|
|
|
if ((key & SPRITE_GLYPH) != 0) return this->parent->GetGlyphWidth(key);
|
|
|
|
|
|
|
|
GlyphEntry *glyph = this->GetGlyphPtr(key);
|
|
|
|
if (glyph == nullptr || glyph->sprite == nullptr) {
|
|
|
|
this->GetGlyph(key);
|
|
|
|
glyph = this->GetGlyphPtr(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
return glyph->width;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Sprite *TrueTypeFontCache::GetGlyph(GlyphID key)
|
|
|
|
{
|
|
|
|
if ((key & SPRITE_GLYPH) != 0) return this->parent->GetGlyph(key);
|
|
|
|
|
|
|
|
/* Check for the glyph in our cache */
|
|
|
|
GlyphEntry *glyph = this->GetGlyphPtr(key);
|
|
|
|
if (glyph != nullptr && glyph->sprite != nullptr) return glyph->sprite;
|
|
|
|
|
|
|
|
if (key == 0) {
|
|
|
|
GlyphID question_glyph = this->MapCharToGlyph('?');
|
|
|
|
if (question_glyph == 0) {
|
|
|
|
/* The font misses the '?' character. Use built-in sprite.
|
|
|
|
* Note: We cannot use the baseset as this also has to work in the bootstrap GUI. */
|
|
|
|
#define CPSET { 0, 0, 0, 0, 1 }
|
|
|
|
#define CP___ { 0, 0, 0, 0, 0 }
|
|
|
|
static SpriteLoader::CommonPixel builtin_questionmark_data[10 * 8] = {
|
|
|
|
CP___, CP___, CPSET, CPSET, CPSET, CPSET, CP___, CP___,
|
|
|
|
CP___, CPSET, CPSET, CP___, CP___, CPSET, CPSET, CP___,
|
|
|
|
CP___, CP___, CP___, CP___, CP___, CPSET, CPSET, CP___,
|
|
|
|
CP___, CP___, CP___, CP___, CPSET, CPSET, CP___, CP___,
|
|
|
|
CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
|
|
|
|
CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
|
|
|
|
CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
|
|
|
|
CP___, CP___, CP___, CP___, CP___, CP___, CP___, CP___,
|
|
|
|
CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
|
|
|
|
CP___, CP___, CP___, CPSET, CPSET, CP___, CP___, CP___,
|
|
|
|
};
|
|
|
|
#undef CPSET
|
|
|
|
#undef CP___
|
|
|
|
static const SpriteLoader::Sprite builtin_questionmark = {
|
|
|
|
10, // height
|
|
|
|
8, // width
|
|
|
|
0, // x_offs
|
|
|
|
0, // y_offs
|
|
|
|
ST_FONT,
|
2021-01-16 15:43:30 +00:00
|
|
|
SCC_PAL,
|
2018-11-25 00:32:48 +00:00
|
|
|
builtin_questionmark_data
|
|
|
|
};
|
|
|
|
|
2021-01-16 15:43:31 +00:00
|
|
|
Sprite *spr = BlitterFactory::GetCurrentBlitter()->Encode(&builtin_questionmark, SimpleSpriteAlloc);
|
2018-11-25 00:32:48 +00:00
|
|
|
assert(spr != nullptr);
|
|
|
|
GlyphEntry new_glyph;
|
|
|
|
new_glyph.sprite = spr;
|
|
|
|
new_glyph.width = spr->width + (this->fs != FS_NORMAL);
|
|
|
|
this->SetGlyphPtr(key, &new_glyph, false);
|
|
|
|
return new_glyph.sprite;
|
|
|
|
} else {
|
|
|
|
/* Use '?' for missing characters. */
|
|
|
|
this->GetGlyph(question_glyph);
|
|
|
|
glyph = this->GetGlyphPtr(question_glyph);
|
|
|
|
this->SetGlyphPtr(key, glyph, true);
|
|
|
|
return glyph->sprite;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this->InternalGetGlyph(key, GetFontAAState(this->fs));
|
|
|
|
}
|
|
|
|
|
|
|
|
const void *TrueTypeFontCache::GetFontTable(uint32 tag, size_t &length)
|
|
|
|
{
|
|
|
|
const FontTable::iterator iter = this->font_tables.Find(tag);
|
|
|
|
if (iter != this->font_tables.data() + this->font_tables.size()) {
|
|
|
|
length = iter->second.first;
|
|
|
|
return iter->second.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
const void *result = this->InternalGetFontTable(tag, length);
|
|
|
|
|
2020-05-17 21:31:44 +00:00
|
|
|
this->font_tables.Insert(tag, std::pair<size_t, const void *>(length, result));
|
2018-11-25 00:32:48 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef WITH_FREETYPE
|
|
|
|
#include <ft2build.h>
|
|
|
|
#include FT_FREETYPE_H
|
|
|
|
#include FT_GLYPH_H
|
|
|
|
#include FT_TRUETYPE_TABLES_H
|
|
|
|
|
|
|
|
/** Font cache for fonts that are based on a freetype font. */
|
|
|
|
class FreeTypeFontCache : public TrueTypeFontCache {
|
|
|
|
private:
|
|
|
|
FT_Face face; ///< The font face associated with this font.
|
|
|
|
|
|
|
|
void SetFontSize(FontSize fs, FT_Face face, int pixels);
|
|
|
|
virtual const void *InternalGetFontTable(uint32 tag, size_t &length);
|
|
|
|
virtual const Sprite *InternalGetGlyph(GlyphID key, bool aa);
|
|
|
|
|
|
|
|
public:
|
|
|
|
FreeTypeFontCache(FontSize fs, FT_Face face, int pixels);
|
|
|
|
~FreeTypeFontCache();
|
|
|
|
virtual void ClearFontCache();
|
2013-06-25 20:20:15 +00:00
|
|
|
virtual GlyphID MapCharToGlyph(WChar key);
|
2013-12-22 17:46:27 +00:00
|
|
|
virtual const char *GetFontName() { return face->family_name; }
|
2018-05-26 14:13:12 +00:00
|
|
|
virtual bool IsBuiltInFont() { return false; }
|
2013-06-23 15:24:36 +00:00
|
|
|
};
|
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
FT_Library _library = nullptr;
|
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.
|
|
|
|
*/
|
2018-11-25 00:32:48 +00:00
|
|
|
FreeTypeFontCache::FreeTypeFontCache(FontSize fs, FT_Face face, int pixels) : TrueTypeFontCache(fs, pixels), face(face)
|
2009-11-15 14:39:37 +00:00
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
assert(face != nullptr);
|
2013-06-23 15:38:51 +00:00
|
|
|
|
2018-06-16 23:37:38 +00:00
|
|
|
this->SetFontSize(fs, face, pixels);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FreeTypeFontCache::SetFontSize(FontSize fs, FT_Face face, 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. */
|
2021-05-01 07:48:29 +00:00
|
|
|
int scaled_height = ScaleFontTrad(this->GetDefaultFontHeight(this->fs));
|
2018-06-16 23:37:38 +00:00
|
|
|
pixels = scaled_height;
|
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);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (head != nullptr) {
|
2013-04-06 22:06:44 +00:00
|
|
|
/* Font height is minimum height plus the difference between the default
|
|
|
|
* height for this font size and the small size. */
|
2021-05-01 07:48:29 +00:00
|
|
|
int diff = scaled_height - ScaleFontTrad(this->GetDefaultFontHeight(FS_SMALL));
|
2021-02-13 21:44:45 +00:00
|
|
|
pixels = Clamp(std::min<uint>(head->Lowest_Rec_PPEM, MAX_FONT_MIN_REC_SIZE) + diff, scaled_height, MAX_FONT_SIZE);
|
2013-04-06 22:06:44 +00:00
|
|
|
}
|
2019-02-23 16:07:25 +00:00
|
|
|
} else {
|
|
|
|
pixels = ScaleFontTrad(pixels);
|
2013-04-06 22:06:44 +00:00
|
|
|
}
|
2018-10-28 22:30:49 +00:00
|
|
|
this->used_size = pixels;
|
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);
|
2014-03-03 21:34:36 +00:00
|
|
|
if (err != FT_Err_Ok) {
|
2011-10-18 17:57:42 +00:00
|
|
|
|
|
|
|
/* 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;
|
2014-03-03 21:34:36 +00:00
|
|
|
if (i > 0) { // In pathetic cases one might get no fixed sizes at all.
|
|
|
|
int n = bs->height;
|
|
|
|
FT_Int chosen = 0;
|
|
|
|
for (; --i; bs++) {
|
|
|
|
if (abs(pixels - bs->height) >= abs(pixels - n)) continue;
|
|
|
|
n = bs->height;
|
|
|
|
chosen = this->face->num_fixed_sizes - i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Don't use FT_Set_Pixel_Sizes here - it might give us another
|
|
|
|
* error, even though the size is available (FS#5885). */
|
|
|
|
err = FT_Select_Size(this->face, chosen);
|
2011-10-18 17:57:42 +00:00
|
|
|
}
|
2009-11-15 14:39:37 +00:00
|
|
|
}
|
2011-10-18 17:57:42 +00:00
|
|
|
|
2014-03-03 21:34:36 +00:00
|
|
|
if (err == FT_Err_Ok) {
|
|
|
|
this->units_per_em = this->face->units_per_EM;
|
|
|
|
this->ascender = this->face->size->metrics.ascender >> 6;
|
|
|
|
this->descender = this->face->size->metrics.descender >> 6;
|
|
|
|
this->height = this->ascender - this->descender;
|
|
|
|
} else {
|
|
|
|
/* Both FT_Set_Pixel_Sizes and FT_Select_Size failed. */
|
2021-06-12 07:10:17 +00:00
|
|
|
Debug(freetype, 0, "Font size selection failed. Using FontCache defaults.");
|
2014-03-03 21:34:36 +00:00
|
|
|
}
|
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
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
FreeTypeSubSetting *settings = nullptr;
|
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
|
|
|
}
|
|
|
|
|
2021-04-28 15:10:15 +00:00
|
|
|
if (settings->font.empty()) return;
|
2006-11-16 22:05:33 +00:00
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
if (_library == nullptr) {
|
2013-06-23 15:37:18 +00:00
|
|
|
if (FT_Init_FreeType(&_library) != FT_Err_Ok) {
|
|
|
|
ShowInfoF("Unable to initialize FreeType, using sprite fonts instead");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-12 07:10:17 +00:00
|
|
|
Debug(freetype, 2, "Initialized");
|
2013-06-23 15:37:18 +00:00
|
|
|
}
|
2006-11-16 22:05:33 +00:00
|
|
|
|
2021-04-28 15:10:15 +00:00
|
|
|
const char *font_name = settings->font.c_str();
|
2019-04-10 21:07:06 +00:00
|
|
|
FT_Face face = nullptr;
|
2021-01-02 20:53:50 +00:00
|
|
|
|
|
|
|
/* If font is an absolute path to a ttf, try loading that first. */
|
2021-04-28 15:10:15 +00:00
|
|
|
FT_Error error = FT_New_Face(_library, font_name, 0, &face);
|
2006-11-17 22:15:55 +00:00
|
|
|
|
2021-01-02 22:28:45 +00:00
|
|
|
#if defined(WITH_COCOA)
|
|
|
|
extern void MacOSRegisterExternalFont(const char *file_path);
|
2021-04-28 15:10:15 +00:00
|
|
|
if (error == FT_Err_Ok) MacOSRegisterExternalFont(font_name);
|
2021-01-02 22:28:45 +00:00
|
|
|
#endif
|
|
|
|
|
2021-01-02 20:53:50 +00:00
|
|
|
if (error != FT_Err_Ok) {
|
|
|
|
/* Check if font is a relative filename in one of our search-paths. */
|
2021-04-28 15:10:15 +00:00
|
|
|
std::string full_font = FioFindFullPath(BASE_DIR, font_name);
|
2021-01-02 20:53:50 +00:00
|
|
|
if (!full_font.empty()) {
|
|
|
|
error = FT_New_Face(_library, full_font.c_str(), 0, &face);
|
2021-01-02 22:28:45 +00:00
|
|
|
#if defined(WITH_COCOA)
|
|
|
|
if (error == FT_Err_Ok) MacOSRegisterExternalFont(full_font.c_str());
|
|
|
|
#endif
|
2021-01-02 20:53:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try loading based on font face name (OS-wide fonts). */
|
2021-04-28 15:10:15 +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) {
|
2021-06-12 07:10:17 +00:00
|
|
|
Debug(freetype, 2, "Requested '{}', using '{} {}'", 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 */
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
if (found != nullptr) {
|
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" };
|
2021-04-28 15:10:15 +00:00
|
|
|
ShowInfoF("Unable to use '%s' for %s font, FreeType reported error 0x%X, using sprite font instead", font_name, 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);
|
2019-04-10 21:07:06 +00:00
|
|
|
this->face = nullptr;
|
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
|
|
|
{
|
2019-02-24 16:27:44 +00:00
|
|
|
/* Font scaling might have changed, determine font size anew if it was automatically selected. */
|
2019-04-10 21:07:06 +00:00
|
|
|
if (this->face != nullptr) this->SetFontSize(this->fs, this->face, this->req_size);
|
2019-02-24 16:27:44 +00:00
|
|
|
|
2018-11-25 00:32:48 +00:00
|
|
|
this->TrueTypeFontCache::ClearFontCache();
|
2007-06-11 13:38:11 +00:00
|
|
|
}
|
|
|
|
|
2006-11-16 22:05:33 +00:00
|
|
|
|
2018-11-25 00:32:48 +00:00
|
|
|
const Sprite *FreeTypeFontCache::InternalGetGlyph(GlyphID key, bool aa)
|
2007-06-15 16:21:56 +00:00
|
|
|
{
|
2013-06-23 15:38:51 +00:00
|
|
|
FT_GlyphSlot slot = this->face->glyph;
|
2006-11-16 22:05:33 +00:00
|
|
|
|
2019-01-17 16:50:54 +00:00
|
|
|
FT_Load_Glyph(this->face, key, aa ? FT_LOAD_TARGET_NORMAL : FT_LOAD_TARGET_MONO);
|
2013-06-23 15:29:20 +00:00
|
|
|
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 */
|
2021-01-08 10:16:18 +00:00
|
|
|
uint width = std::max(1U, (uint)slot->bitmap.width + (this->fs == FS_NORMAL));
|
|
|
|
uint height = std::max(1U, (uint)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. */
|
2021-02-13 21:44:45 +00:00
|
|
|
if (width > MAX_GLYPH_DIM || height > MAX_GLYPH_DIM) usererror("Font glyph is too large");
|
2011-09-02 20:16:41 +00:00
|
|
|
|
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;
|
2021-01-16 15:43:30 +00:00
|
|
|
sprite.colours = (aa ? SCC_PAL | SCC_ALPHA : SCC_PAL);
|
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) {
|
2015-03-15 12:19:58 +00:00
|
|
|
for (uint y = 0; y < (uint)slot->bitmap.rows; y++) {
|
|
|
|
for (uint x = 0; x < (uint)slot->bitmap.width; x++) {
|
2021-07-10 08:37:46 +00:00
|
|
|
if (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;
|
2021-07-10 08:37:46 +00:00
|
|
|
sprite.data[1 + x + (1 + y) * sprite.width].a = 0xFF;
|
2006-11-16 22:05:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-15 12:19:58 +00:00
|
|
|
for (uint y = 0; y < (uint)slot->bitmap.rows; y++) {
|
|
|
|
for (uint x = 0; x < (uint)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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-25 00:32:48 +00:00
|
|
|
GlyphEntry new_glyph;
|
2021-01-16 15:43:31 +00:00
|
|
|
new_glyph.sprite = BlitterFactory::GetCurrentBlitter()->Encode(&sprite, SimpleSpriteAlloc);
|
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-25 20:20:15 +00:00
|
|
|
GlyphID FreeTypeFontCache::MapCharToGlyph(WChar key)
|
|
|
|
{
|
|
|
|
assert(IsPrintable(key));
|
|
|
|
|
|
|
|
if (key >= SCC_SPRITE_START && key <= SCC_SPRITE_END) {
|
|
|
|
return this->parent->MapCharToGlyph(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
return FT_Get_Char_Index(this->face, key);
|
|
|
|
}
|
|
|
|
|
2018-11-25 00:32:48 +00:00
|
|
|
const void *FreeTypeFontCache::InternalGetFontTable(uint32 tag, size_t &length)
|
2013-06-25 20:39:58 +00:00
|
|
|
{
|
|
|
|
FT_ULong len = 0;
|
2019-04-10 21:07:06 +00:00
|
|
|
FT_Byte *result = nullptr;
|
2013-06-25 20:39:58 +00:00
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
FT_Load_Sfnt_Table(this->face, tag, 0, nullptr, &len);
|
2013-06-25 20:39:58 +00:00
|
|
|
|
|
|
|
if (len > 0) {
|
|
|
|
result = MallocT<FT_Byte>(len);
|
|
|
|
FT_Load_Sfnt_Table(this->face, tag, 0, result, &len);
|
|
|
|
}
|
|
|
|
|
2018-11-25 00:32:48 +00:00
|
|
|
length = len;
|
2013-06-25 20:39:58 +00:00
|
|
|
return result;
|
|
|
|
}
|
2006-11-16 22:05:33 +00:00
|
|
|
#endif /* WITH_FREETYPE */
|
2013-06-23 15:37:18 +00:00
|
|
|
|
2018-11-25 00:32:48 +00:00
|
|
|
|
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);
|
2018-11-25 01:00:42 +00:00
|
|
|
#elif defined(_WIN32)
|
2021-02-13 16:34:22 +00:00
|
|
|
extern void LoadWin32Font(FontSize fs);
|
2018-11-25 01:00:42 +00:00
|
|
|
LoadWin32Font(fs);
|
2021-02-13 21:51:18 +00:00
|
|
|
#elif defined(WITH_COCOA)
|
|
|
|
extern void LoadCoreTextFont(FontSize fs);
|
|
|
|
LoadCoreTextFont(fs);
|
2013-06-23 15:37:18 +00:00
|
|
|
#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);
|
2019-04-10 21:07:06 +00:00
|
|
|
_library = nullptr;
|
2013-06-23 15:37:18 +00:00
|
|
|
#endif /* WITH_FREETYPE */
|
|
|
|
}
|
2021-02-13 16:53:41 +00:00
|
|
|
|
2021-02-23 19:54:50 +00:00
|
|
|
/**
|
|
|
|
* Should any of the active fonts be anti-aliased?
|
|
|
|
* @return True if any of the loaded fonts want anti-aliased drawing.
|
|
|
|
*/
|
|
|
|
bool HasAntialiasedFonts()
|
|
|
|
{
|
|
|
|
for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
|
|
|
|
if (!FontCache::Get(fs)->IsBuiltInFont() && GetFontAAState(fs, false)) return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-02-13 21:51:18 +00:00
|
|
|
#if !defined(_WIN32) && !defined(__APPLE__) && !defined(WITH_FONTCONFIG) && !defined(WITH_COCOA)
|
2021-02-13 16:53:41 +00:00
|
|
|
|
|
|
|
#ifdef WITH_FREETYPE
|
|
|
|
FT_Error GetFontByFaceName(const char *font_name, FT_Face *face) { return FT_Err_Cannot_Open_Resource; }
|
|
|
|
#endif /* WITH_FREETYPE */
|
|
|
|
|
|
|
|
bool SetFallbackFont(FreeTypeSettings *settings, const char *language_isocode, int winlangid, MissingGlyphSearcher *callback) { return false; }
|
2021-02-13 21:51:18 +00:00
|
|
|
#endif /* !defined(_WIN32) && !defined(__APPLE__) && !defined(WITH_FONTCONFIG) && !defined(WITH_COCOA) */
|