Merge branch 'master' into jgrpp-nrt
# Conflicts: # src/fontcache.cpp # src/game/game_text.cpp # src/lang/korean.txt # src/os/macosx/crashlog_osx.cpp # src/os/windows/crashlog_win.cpp # src/station_cmd.cpp # src/viewport.cpp
This commit is contained in:
@@ -19,6 +19,7 @@
|
||||
#include "zoom_type.h"
|
||||
#include "gfx_layout.h"
|
||||
#include "zoom_func.h"
|
||||
#include "fileio_func.h"
|
||||
|
||||
#include "table/sprites.h"
|
||||
#include "table/control_codes.h"
|
||||
@@ -197,16 +198,16 @@ void UpdateFontHeightCache()
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef WITH_FREETYPE
|
||||
#include <ft2build.h>
|
||||
#include FT_FREETYPE_H
|
||||
#include FT_GLYPH_H
|
||||
#include FT_TRUETYPE_TABLES_H
|
||||
#if defined(WITH_FREETYPE) || defined(_WIN32)
|
||||
|
||||
/** Font cache for fonts that are based on a freetype font. */
|
||||
class FreeTypeFontCache : public FontCache {
|
||||
private:
|
||||
FT_Face face; ///< The font face associated with this font.
|
||||
FreeTypeSettings _freetype;
|
||||
|
||||
static const byte FACE_COLOUR = 1;
|
||||
static const byte SHADOW_COLOUR = 2;
|
||||
|
||||
/** Font cache for fonts that are based on a TrueType font. */
|
||||
class TrueTypeFontCache : public FontCache {
|
||||
protected:
|
||||
int req_size; ///< Requested font size.
|
||||
int used_size; ///< Used font size.
|
||||
|
||||
@@ -237,11 +238,13 @@ private:
|
||||
|
||||
GlyphEntry *GetGlyphPtr(GlyphID key);
|
||||
void SetGlyphPtr(GlyphID key, const GlyphEntry *glyph, bool duplicate = false);
|
||||
void SetFontSize(FontSize fs, FT_Face face, int pixels);
|
||||
|
||||
virtual const void *InternalGetFontTable(uint32 tag, size_t &length) = 0;
|
||||
virtual const Sprite *InternalGetGlyph(GlyphID key, bool aa) = 0;
|
||||
|
||||
public:
|
||||
FreeTypeFontCache(FontSize fs, FT_Face face, int pixels);
|
||||
~FreeTypeFontCache();
|
||||
TrueTypeFontCache(FontSize fs, int pixels);
|
||||
virtual ~TrueTypeFontCache();
|
||||
virtual int GetFontSize() const { return this->used_size; }
|
||||
virtual SpriteID GetUnicodeGlyph(WChar key) { return this->parent->GetUnicodeGlyph(key); }
|
||||
virtual void SetUnicodeGlyph(WChar key, SpriteID sprite) { this->parent->SetUnicodeGlyph(key, sprite); }
|
||||
@@ -252,22 +255,222 @@ public:
|
||||
font_height_cache[this->fs] = this->GetHeight();
|
||||
}
|
||||
|
||||
virtual void ClearFontCache();
|
||||
virtual const Sprite *GetGlyph(GlyphID key);
|
||||
virtual const void *GetFontTable(uint32 tag, size_t &length);
|
||||
virtual void ClearFontCache();
|
||||
virtual uint GetGlyphWidth(GlyphID key);
|
||||
virtual bool GetDrawGlyphShadow();
|
||||
virtual bool IsBuiltInFont() { return false; }
|
||||
};
|
||||
|
||||
/**
|
||||
* 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()
|
||||
{
|
||||
this->ClearFontCache();
|
||||
|
||||
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) {
|
||||
DEBUG(freetype, 3, "Allocating root glyph cache for size %u", this->fs);
|
||||
this->glyph_to_sprite = CallocT<GlyphEntry*>(256);
|
||||
}
|
||||
|
||||
if (this->glyph_to_sprite[GB(key, 8, 8)] == nullptr) {
|
||||
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);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
static void *AllocateFont(size_t size)
|
||||
{
|
||||
return MallocT<byte>(size);
|
||||
}
|
||||
|
||||
|
||||
/* Check if a glyph should be rendered with anti-aliasing. */
|
||||
static bool GetFontAAState(FontSize size)
|
||||
{
|
||||
/* AA is only supported for 32 bpp */
|
||||
if (BlitterFactory::GetCurrentBlitter()->GetScreenDepth() != 32) return false;
|
||||
|
||||
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,
|
||||
builtin_questionmark_data
|
||||
};
|
||||
|
||||
Sprite *spr = BlitterFactory::GetCurrentBlitter()->Encode(&builtin_questionmark, AllocateFont);
|
||||
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);
|
||||
|
||||
this->font_tables.Insert(tag, SmallPair<size_t, const void *>(length, result));
|
||||
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();
|
||||
virtual GlyphID MapCharToGlyph(WChar key);
|
||||
virtual const void *GetFontTable(uint32 tag, size_t &length);
|
||||
virtual const char *GetFontName() { return face->family_name; }
|
||||
virtual bool IsBuiltInFont() { return false; }
|
||||
};
|
||||
|
||||
FT_Library _library = nullptr;
|
||||
|
||||
FreeTypeSettings _freetype;
|
||||
|
||||
static const byte FACE_COLOUR = 1;
|
||||
static const byte SHADOW_COLOUR = 2;
|
||||
|
||||
/**
|
||||
* Create a new FreeTypeFontCache.
|
||||
@@ -275,7 +478,7 @@ static const byte SHADOW_COLOUR = 2;
|
||||
* @param face The font that has to be loaded.
|
||||
* @param pixels The number of pixels this font should be high.
|
||||
*/
|
||||
FreeTypeFontCache::FreeTypeFontCache(FontSize fs, FT_Face face, int pixels) : FontCache(fs), face(face), req_size(pixels), glyph_to_sprite(nullptr)
|
||||
FreeTypeFontCache::FreeTypeFontCache(FontSize fs, FT_Face face, int pixels) : TrueTypeFontCache(fs, pixels), face(face)
|
||||
{
|
||||
assert(face != nullptr);
|
||||
|
||||
@@ -416,10 +619,6 @@ FreeTypeFontCache::~FreeTypeFontCache()
|
||||
FT_Done_Face(this->face);
|
||||
this->face = nullptr;
|
||||
this->ClearFontCache();
|
||||
|
||||
for (auto &iter : this->font_tables) {
|
||||
free(iter.second.second);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -430,130 +629,14 @@ void FreeTypeFontCache::ClearFontCache()
|
||||
/* Font scaling might have changed, determine font size anew if it was automatically selected. */
|
||||
if (this->face != nullptr) this->SetFontSize(this->fs, this->face, this->req_size);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
FreeTypeFontCache::GlyphEntry *FreeTypeFontCache::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)];
|
||||
this->TrueTypeFontCache::ClearFontCache();
|
||||
}
|
||||
|
||||
|
||||
void FreeTypeFontCache::SetGlyphPtr(GlyphID key, const GlyphEntry *glyph, bool duplicate)
|
||||
const Sprite *FreeTypeFontCache::InternalGetGlyph(GlyphID key, bool aa)
|
||||
{
|
||||
if (this->glyph_to_sprite == nullptr) {
|
||||
DEBUG(freetype, 3, "Allocating root glyph cache for size %u", this->fs);
|
||||
this->glyph_to_sprite = CallocT<GlyphEntry*>(256);
|
||||
}
|
||||
|
||||
if (this->glyph_to_sprite[GB(key, 8, 8)] == nullptr) {
|
||||
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);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
static void *AllocateFont(size_t size)
|
||||
{
|
||||
return MallocT<byte>(size);
|
||||
}
|
||||
|
||||
|
||||
/* Check if a glyph should be rendered with antialiasing */
|
||||
static bool GetFontAAState(FontSize size)
|
||||
{
|
||||
/* AA is only supported for 32 bpp */
|
||||
if (BlitterFactory::GetCurrentBlitter()->GetScreenDepth() != 32) return false;
|
||||
|
||||
switch (size) {
|
||||
default: NOT_REACHED();
|
||||
case FS_NORMAL: return _freetype.medium.aa;
|
||||
case FS_SMALL: return _freetype.small.aa;
|
||||
case FS_LARGE: return _freetype.large.aa;
|
||||
case FS_MONO: return _freetype.mono.aa;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const Sprite *FreeTypeFontCache::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;
|
||||
|
||||
FT_GlyphSlot slot = this->face->glyph;
|
||||
|
||||
bool aa = GetFontAAState(this->fs);
|
||||
|
||||
GlyphEntry new_glyph;
|
||||
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,
|
||||
builtin_questionmark_data
|
||||
};
|
||||
|
||||
Sprite *spr = BlitterFactory::GetCurrentBlitter()->Encode(&builtin_questionmark, AllocateFont);
|
||||
assert(spr != nullptr);
|
||||
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;
|
||||
}
|
||||
}
|
||||
FT_Load_Glyph(this->face, key, aa ? FT_LOAD_TARGET_NORMAL : FT_LOAD_TARGET_MONO);
|
||||
FT_Render_Glyph(this->face->glyph, aa ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO);
|
||||
|
||||
@@ -597,6 +680,7 @@ const Sprite *FreeTypeFontCache::GetGlyph(GlyphID key)
|
||||
}
|
||||
}
|
||||
|
||||
GlyphEntry new_glyph;
|
||||
new_glyph.sprite = BlitterFactory::GetCurrentBlitter()->Encode(&sprite, AllocateFont);
|
||||
new_glyph.width = slot->advance.x >> 6;
|
||||
|
||||
@@ -606,25 +690,6 @@ const Sprite *FreeTypeFontCache::GetGlyph(GlyphID key)
|
||||
}
|
||||
|
||||
|
||||
bool FreeTypeFontCache::GetDrawGlyphShadow()
|
||||
{
|
||||
return this->fs == FS_NORMAL && GetFontAAState(FS_NORMAL);
|
||||
}
|
||||
|
||||
|
||||
uint FreeTypeFontCache::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;
|
||||
}
|
||||
|
||||
GlyphID FreeTypeFontCache::MapCharToGlyph(WChar key)
|
||||
{
|
||||
assert(IsPrintable(key));
|
||||
@@ -636,14 +701,8 @@ GlyphID FreeTypeFontCache::MapCharToGlyph(WChar key)
|
||||
return FT_Get_Char_Index(this->face, key);
|
||||
}
|
||||
|
||||
const void *FreeTypeFontCache::GetFontTable(uint32 tag, size_t &length)
|
||||
const void *FreeTypeFontCache::InternalGetFontTable(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;
|
||||
}
|
||||
|
||||
FT_ULong len = 0;
|
||||
FT_Byte *result = nullptr;
|
||||
|
||||
@@ -653,14 +712,329 @@ const void *FreeTypeFontCache::GetFontTable(uint32 tag, size_t &length)
|
||||
result = MallocT<FT_Byte>(len);
|
||||
FT_Load_Sfnt_Table(this->face, tag, 0, result, &len);
|
||||
}
|
||||
length = len;
|
||||
|
||||
this->font_tables.Insert(tag, SmallPair<size_t, const void *>(length, result));
|
||||
length = len;
|
||||
return result;
|
||||
}
|
||||
|
||||
#elif defined(_WIN32)
|
||||
|
||||
#include "os/windows/win32.h"
|
||||
#ifndef ANTIALIASED_QUALITY
|
||||
#define ANTIALIASED_QUALITY 4
|
||||
#endif
|
||||
|
||||
/** Font cache for fonts that are based on a Win32 font. */
|
||||
class Win32FontCache : public TrueTypeFontCache {
|
||||
private:
|
||||
LOGFONT logfont; ///< Logical font information for selecting the font face.
|
||||
HFONT font = nullptr; ///< The font face associated with this font.
|
||||
HDC dc = nullptr; ///< Cached GDI device context.
|
||||
HGDIOBJ old_font; ///< Old font selected into the GDI context.
|
||||
SIZE glyph_size; ///< Maximum size of regular glyphs.
|
||||
|
||||
void SetFontSize(FontSize fs, int pixels);
|
||||
virtual const void *InternalGetFontTable(uint32 tag, size_t &length);
|
||||
virtual const Sprite *InternalGetGlyph(GlyphID key, bool aa);
|
||||
|
||||
public:
|
||||
Win32FontCache(FontSize fs, const LOGFONT &logfont, int pixels);
|
||||
~Win32FontCache();
|
||||
virtual void ClearFontCache();
|
||||
virtual GlyphID MapCharToGlyph(WChar key);
|
||||
virtual const char *GetFontName() { return WIDE_TO_MB(this->logfont.lfFaceName); }
|
||||
virtual bool IsBuiltInFont() { return false; }
|
||||
virtual void *GetOSHandle() { return &this->logfont; }
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Create a new Win32FontCache.
|
||||
* @param fs The font size that is going to be cached.
|
||||
* @param logfont The font that has to be loaded.
|
||||
* @param pixels The number of pixels this font should be high.
|
||||
*/
|
||||
Win32FontCache::Win32FontCache(FontSize fs, const LOGFONT &logfont, int pixels) : TrueTypeFontCache(fs, pixels), logfont(logfont)
|
||||
{
|
||||
this->dc = CreateCompatibleDC(nullptr);
|
||||
this->SetFontSize(fs, pixels);
|
||||
}
|
||||
|
||||
Win32FontCache::~Win32FontCache()
|
||||
{
|
||||
this->ClearFontCache();
|
||||
DeleteDC(this->dc);
|
||||
DeleteObject(this->font);
|
||||
}
|
||||
|
||||
void Win32FontCache::SetFontSize(FontSize fs, int pixels)
|
||||
{
|
||||
if (pixels == 0) {
|
||||
/* Try to determine a good height based on the minimal height recommended by the font. */
|
||||
int scaled_height = ScaleFontTrad(_default_font_height[this->fs]);
|
||||
pixels = scaled_height;
|
||||
|
||||
HFONT temp = CreateFontIndirect(&this->logfont);
|
||||
if (temp != nullptr) {
|
||||
HGDIOBJ old = SelectObject(this->dc, temp);
|
||||
|
||||
UINT size = GetOutlineTextMetrics(this->dc, 0, nullptr);
|
||||
LPOUTLINETEXTMETRIC otm = (LPOUTLINETEXTMETRIC)AllocaM(BYTE, size);
|
||||
GetOutlineTextMetrics(this->dc, size, otm);
|
||||
|
||||
/* Font height is minimum height plus the difference between the default
|
||||
* height for this font size and the small size. */
|
||||
int diff = scaled_height - ScaleFontTrad(_default_font_height[FS_SMALL]);
|
||||
pixels = Clamp(min(otm->otmusMinimumPPEM, 20) + diff, scaled_height, MAX_FONT_SIZE);
|
||||
|
||||
SelectObject(dc, old);
|
||||
DeleteObject(temp);
|
||||
}
|
||||
} else {
|
||||
pixels = ScaleFontTrad(pixels);
|
||||
}
|
||||
this->used_size = pixels;
|
||||
|
||||
/* Create GDI font handle. */
|
||||
this->logfont.lfHeight = -pixels;
|
||||
this->logfont.lfWidth = 0;
|
||||
this->logfont.lfOutPrecision = ANTIALIASED_QUALITY;
|
||||
|
||||
if (this->font != nullptr) {
|
||||
SelectObject(dc, this->old_font);
|
||||
DeleteObject(this->font);
|
||||
}
|
||||
this->font = CreateFontIndirect(&this->logfont);
|
||||
this->old_font = SelectObject(this->dc, this->font);
|
||||
|
||||
/* Query the font metrics we needed. */
|
||||
UINT otmSize = GetOutlineTextMetrics(this->dc, 0, nullptr);
|
||||
POUTLINETEXTMETRIC otm = (POUTLINETEXTMETRIC)AllocaM(BYTE, otmSize);
|
||||
GetOutlineTextMetrics(this->dc, otmSize, otm);
|
||||
|
||||
this->units_per_em = otm->otmEMSquare;
|
||||
this->ascender = otm->otmTextMetrics.tmAscent;
|
||||
this->descender = otm->otmTextMetrics.tmDescent;
|
||||
this->height = this->ascender + this->descender;
|
||||
this->glyph_size.cx = otm->otmTextMetrics.tmMaxCharWidth;
|
||||
this->glyph_size.cy = otm->otmTextMetrics.tmHeight;
|
||||
|
||||
DEBUG(freetype, 2, "Loaded font '%s' with size %d", FS2OTTD((LPTSTR)((BYTE *)otm + (ptrdiff_t)otm->otmpFullName)), pixels);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset cached glyphs.
|
||||
*/
|
||||
void Win32FontCache::ClearFontCache()
|
||||
{
|
||||
/* GUI scaling might have changed, determine font size anew if it was automatically selected. */
|
||||
if (this->font != nullptr) this->SetFontSize(this->fs, this->req_size);
|
||||
|
||||
this->TrueTypeFontCache::ClearFontCache();
|
||||
}
|
||||
|
||||
/* virtual */ const Sprite *Win32FontCache::InternalGetGlyph(GlyphID key, bool aa)
|
||||
{
|
||||
GLYPHMETRICS gm;
|
||||
MAT2 mat = { {0, 1}, {0, 0}, {0, 0}, {0, 1} };
|
||||
|
||||
/* Make a guess for the needed memory size. */
|
||||
DWORD size = this->glyph_size.cy * Align(aa ? this->glyph_size.cx : max(this->glyph_size.cx / 8l, 1l), 4); // Bitmap data is DWORD-aligned rows.
|
||||
byte *bmp = AllocaM(byte, size);
|
||||
size = GetGlyphOutline(this->dc, key, GGO_GLYPH_INDEX | (aa ? GGO_GRAY8_BITMAP : GGO_BITMAP), &gm, size, bmp, &mat);
|
||||
|
||||
if (size == GDI_ERROR) {
|
||||
/* No dice with the guess. First query size of needed glyph memory, then allocate the
|
||||
* memory and query again. This dance is necessary as some glyphs will only render with
|
||||
* the exact matching size; e.g. the space glyph has no pixels and must be requested
|
||||
* with size == 0, anything else fails. Unfortunately, a failed call doesn't return any
|
||||
* info about the size and thus the triple GetGlyphOutline()-call. */
|
||||
size = GetGlyphOutline(this->dc, key, GGO_GLYPH_INDEX | (aa ? GGO_GRAY8_BITMAP : GGO_BITMAP), &gm, 0, nullptr, &mat);
|
||||
if (size == GDI_ERROR) usererror("Unable to render font glyph");
|
||||
bmp = AllocaM(byte, size);
|
||||
GetGlyphOutline(this->dc, key, GGO_GLYPH_INDEX | (aa ? GGO_GRAY8_BITMAP : GGO_BITMAP), &gm, size, bmp, &mat);
|
||||
}
|
||||
|
||||
/* Add 1 pixel for the shadow on the medium font. Our sprite must be at least 1x1 pixel. */
|
||||
uint width = max(1U, (uint)gm.gmBlackBoxX + (this->fs == FS_NORMAL));
|
||||
uint height = max(1U, (uint)gm.gmBlackBoxY + (this->fs == FS_NORMAL));
|
||||
|
||||
/* Limit glyph size to prevent overflows later on. */
|
||||
if (width > 256 || height > 256) usererror("Font glyph is too large");
|
||||
|
||||
/* GDI has rendered the glyph, now we allocate a sprite and copy the image into it. */
|
||||
SpriteLoader::Sprite sprite;
|
||||
sprite.AllocateData(ZOOM_LVL_NORMAL, width * height);
|
||||
sprite.type = ST_FONT;
|
||||
sprite.width = width;
|
||||
sprite.height = height;
|
||||
sprite.x_offs = gm.gmptGlyphOrigin.x;
|
||||
sprite.y_offs = this->ascender - gm.gmptGlyphOrigin.y;
|
||||
|
||||
if (size > 0) {
|
||||
/* All pixel data returned by GDI is in the form of DWORD-aligned rows.
|
||||
* For a non anti-aliased glyph, the returned bitmap has one bit per pixel.
|
||||
* For anti-aliased rendering, GDI uses the strange value range of 0 to 64,
|
||||
* inclusively. To map this to 0 to 255, we shift left by two and then
|
||||
* subtract one. */
|
||||
uint pitch = Align(aa ? gm.gmBlackBoxX : max(gm.gmBlackBoxX / 8u, 1u), 4);
|
||||
|
||||
/* Draw shadow for medium size. */
|
||||
if (this->fs == FS_NORMAL && !aa) {
|
||||
for (uint y = 0; y < gm.gmBlackBoxY; y++) {
|
||||
for (uint x = 0; x < gm.gmBlackBoxX; x++) {
|
||||
if (aa ? (bmp[x + y * pitch] > 0) : HasBit(bmp[(x / 8) + y * pitch], 7 - (x % 8))) {
|
||||
sprite.data[1 + x + (1 + y) * sprite.width].m = SHADOW_COLOUR;
|
||||
sprite.data[1 + x + (1 + y) * sprite.width].a = aa ? (bmp[x + y * pitch] << 2) - 1 : 0xFF;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (uint y = 0; y < gm.gmBlackBoxY; y++) {
|
||||
for (uint x = 0; x < gm.gmBlackBoxX; x++) {
|
||||
if (aa ? (bmp[x + y * pitch] > 0) : HasBit(bmp[(x / 8) + y * pitch], 7 - (x % 8))) {
|
||||
sprite.data[x + y * sprite.width].m = FACE_COLOUR;
|
||||
sprite.data[x + y * sprite.width].a = aa ? (bmp[x + y * pitch] << 2) - 1 : 0xFF;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
GlyphEntry new_glyph;
|
||||
new_glyph.sprite = BlitterFactory::GetCurrentBlitter()->Encode(&sprite, AllocateFont);
|
||||
new_glyph.width = gm.gmCellIncX;
|
||||
|
||||
this->SetGlyphPtr(key, &new_glyph);
|
||||
|
||||
return new_glyph.sprite;
|
||||
}
|
||||
|
||||
/* virtual */ GlyphID Win32FontCache::MapCharToGlyph(WChar key)
|
||||
{
|
||||
assert(IsPrintable(key));
|
||||
|
||||
if (key >= SCC_SPRITE_START && key <= SCC_SPRITE_END) {
|
||||
return this->parent->MapCharToGlyph(key);
|
||||
}
|
||||
|
||||
/* Convert characters outside of the BMP into surrogate pairs. */
|
||||
WCHAR chars[2];
|
||||
if (key >= 0x010000U) {
|
||||
chars[0] = (WCHAR)(((key - 0x010000U) >> 10) + 0xD800);
|
||||
chars[1] = (WCHAR)(((key - 0x010000U) & 0x3FF) + 0xDC00);
|
||||
} else {
|
||||
chars[0] = (WCHAR)(key & 0xFFFF);
|
||||
}
|
||||
|
||||
WORD glyphs[2] = {0, 0};
|
||||
GetGlyphIndicesW(this->dc, chars, key >= 0x010000U ? 2 : 1, glyphs, GGI_MARK_NONEXISTING_GLYPHS);
|
||||
|
||||
return glyphs[0] != 0xFFFF ? glyphs[0] : 0;
|
||||
}
|
||||
|
||||
/* virtual */ const void *Win32FontCache::InternalGetFontTable(uint32 tag, size_t &length)
|
||||
{
|
||||
DWORD len = GetFontData(this->dc, tag, 0, nullptr, 0);
|
||||
|
||||
void *result = nullptr;
|
||||
if (len != GDI_ERROR && len > 0) {
|
||||
result = MallocT<BYTE>(len);
|
||||
GetFontData(this->dc, tag, 0, result, len);
|
||||
}
|
||||
|
||||
length = len;
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the GDI font.
|
||||
* If a GDI font description is present, e.g. from the automatic font
|
||||
* fallback search, use it. Otherwise, try to resolve it by font name.
|
||||
* @param fs The font size to load.
|
||||
*/
|
||||
static void LoadWin32Font(FontSize fs)
|
||||
{
|
||||
static const char *SIZE_TO_NAME[] = { "medium", "small", "large", "mono" };
|
||||
|
||||
FreeTypeSubSetting *settings = nullptr;
|
||||
switch (fs) {
|
||||
default: NOT_REACHED();
|
||||
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;
|
||||
}
|
||||
|
||||
if (StrEmpty(settings->font)) return;
|
||||
|
||||
LOGFONT logfont;
|
||||
MemSetT(&logfont, 0);
|
||||
logfont.lfPitchAndFamily = fs == FS_MONO ? FIXED_PITCH : VARIABLE_PITCH;
|
||||
logfont.lfCharSet = DEFAULT_CHARSET;
|
||||
logfont.lfOutPrecision = OUT_OUTLINE_PRECIS;
|
||||
logfont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
|
||||
|
||||
if (settings->os_handle != nullptr) {
|
||||
logfont = *(const LOGFONT *)settings->os_handle;
|
||||
} else if (strchr(settings->font, '.') != nullptr && FileExists(settings->font)) {
|
||||
/* Might be a font file name, try load it. */
|
||||
TCHAR fontPath[MAX_PATH];
|
||||
convert_to_fs(settings->font, fontPath, lengthof(fontPath), false);
|
||||
|
||||
if (AddFontResourceEx(fontPath, FR_PRIVATE, 0) != 0) {
|
||||
/* Try a nice little undocumented function first for getting the internal font name.
|
||||
* Some documentation is fount at: http://www.undocprint.org/winspool/getfontresourceinfo */
|
||||
typedef BOOL(WINAPI * PFNGETFONTRESOURCEINFO)(LPCTSTR, LPDWORD, LPVOID, DWORD);
|
||||
#ifdef UNICODE
|
||||
static PFNGETFONTRESOURCEINFO GetFontResourceInfo = (PFNGETFONTRESOURCEINFO)GetProcAddress(GetModuleHandle(_T("Gdi32")), "GetFontResourceInfoW");
|
||||
#else
|
||||
static PFNGETFONTRESOURCEINFO GetFontResourceInfo = (PFNGETFONTRESOURCEINFO)GetProcAddress(GetModuleHandle(_T("Gdi32")), "GetFontResourceInfoA");
|
||||
#endif
|
||||
|
||||
if (GetFontResourceInfo != nullptr) {
|
||||
/* Try to query an array of LOGFONTs that describe the file. */
|
||||
DWORD len = 0;
|
||||
if (GetFontResourceInfo(fontPath, &len, nullptr, 2) && len >= sizeof(LOGFONT)) {
|
||||
LOGFONT *buf = (LOGFONT *)AllocaM(byte, len);
|
||||
if (GetFontResourceInfo(fontPath, &len, buf, 2)) {
|
||||
logfont = *buf; // Just use first entry.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* No dice yet. Use the file name as the font face name, hoping it matches. */
|
||||
if (logfont.lfFaceName[0] == 0) {
|
||||
TCHAR fname[_MAX_FNAME];
|
||||
_tsplitpath(fontPath, nullptr, nullptr, fname, nullptr);
|
||||
|
||||
_tcsncpy_s(logfont.lfFaceName, lengthof(logfont.lfFaceName), fname, _TRUNCATE);
|
||||
logfont.lfWeight = strcasestr(settings->font, " bold") != nullptr || strcasestr(settings->font, "-bold") != nullptr ? FW_BOLD : FW_NORMAL; // Poor man's way to allow selecting bold fonts.
|
||||
}
|
||||
} else {
|
||||
ShowInfoF("Unable to load file '%s' for %s font, using default windows font selection instead", settings->font, SIZE_TO_NAME[fs]);
|
||||
}
|
||||
}
|
||||
|
||||
if (logfont.lfFaceName[0] == 0) {
|
||||
logfont.lfWeight = strcasestr(settings->font, " bold") != nullptr ? FW_BOLD : FW_NORMAL; // Poor man's way to allow selecting bold fonts.
|
||||
convert_to_fs(settings->font, logfont.lfFaceName, lengthof(logfont.lfFaceName), false);
|
||||
}
|
||||
|
||||
HFONT font = CreateFontIndirect(&logfont);
|
||||
if (font == nullptr) {
|
||||
ShowInfoF("Unable to use '%s' for %s font, Win32 reported error 0x%lX, using sprite font instead", settings->font, SIZE_TO_NAME[fs], GetLastError());
|
||||
return;
|
||||
}
|
||||
DeleteObject(font);
|
||||
|
||||
new Win32FontCache(fs, logfont, settings->size);
|
||||
}
|
||||
|
||||
#endif /* WITH_FREETYPE */
|
||||
|
||||
#endif /* defined(WITH_FREETYPE) || defined(_WIN32) */
|
||||
|
||||
/**
|
||||
* (Re)initialize the freetype related things, i.e. load the non-sprite fonts.
|
||||
* @param monospace Whether to initialise the monospace or regular fonts.
|
||||
@@ -675,6 +1049,8 @@ void InitFreeType(bool monospace)
|
||||
|
||||
#ifdef WITH_FREETYPE
|
||||
LoadFreeTypeFont(fs);
|
||||
#elif defined(_WIN32)
|
||||
LoadWin32Font(fs);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user