Compare commits
9 Commits
feat/font_
...
dev/14
| Author | SHA1 | Date | |
|---|---|---|---|
| a3fa504bee | |||
| d962361e36 | |||
|
|
9bb6266be0 | ||
|
|
65dc054414 | ||
|
|
1b398f13d5 | ||
|
|
3a047a01e8 | ||
| 501c88c6bf | |||
| 33a33d6a34 | |||
| 40b429bc3b |
2
.ottdrev
2
.ottdrev
@@ -1 +1 @@
|
||||
14.0 20240413 0 b3c704a6306027de4aad575c8e394a2d8a1878f9 1 1 2024
|
||||
14.1 20240503 0 b3c704a6306027de4aad575c8e394a2d8a1878f9 1 1 2024
|
||||
|
||||
@@ -22,9 +22,6 @@
|
||||
|
||||
#include "safeguards.h"
|
||||
|
||||
static void LoadFontHelper([[maybe_unused]] FontSize fs);
|
||||
static void TryLoadDefaultTrueTypeFont([[maybe_unused]] FontSize fs);
|
||||
|
||||
/** Default heights for the different sizes of fonts. */
|
||||
static const int _default_font_height[FS_END] = {10, 6, 18, 10};
|
||||
static const int _default_font_ascender[FS_END] = { 8, 5, 15, 8};
|
||||
@@ -36,8 +33,7 @@ FontCacheSettings _fcsettings;
|
||||
* @param fs The size of the font.
|
||||
*/
|
||||
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)
|
||||
ascender(_default_font_ascender[fs]), descender(_default_font_ascender[fs] - _default_font_height[fs])
|
||||
{
|
||||
assert(this->parent == nullptr || this->fs == this->parent->fs);
|
||||
FontCache::caches[this->fs] = this;
|
||||
@@ -102,29 +98,6 @@ bool GetFontAAState(FontSize size, bool check_blitter)
|
||||
return _fcsettings.global_aa || GetFontCacheSubSetting(size)->aa;
|
||||
}
|
||||
|
||||
void ResizeFont(FontSize font_size, uint size)
|
||||
{
|
||||
FontCacheSubSetting *setting = GetFontCacheSubSetting(font_size);
|
||||
|
||||
if (setting->size == size) {
|
||||
return;
|
||||
}
|
||||
|
||||
setting->size = size;
|
||||
|
||||
// Default fonts are empty here. We will allow the user to resize the default font:
|
||||
if (setting->font.empty()){
|
||||
TryLoadDefaultTrueTypeFont(font_size);
|
||||
} else {
|
||||
LoadFontHelper(font_size);
|
||||
}
|
||||
|
||||
LoadStringWidthTable();
|
||||
UpdateAllVirtCoords();
|
||||
ReInitAllWindows(true);
|
||||
|
||||
if (_save_config) SaveToConfig();
|
||||
}
|
||||
|
||||
void SetFont(FontSize fontsize, const std::string &font, uint size, bool aa)
|
||||
{
|
||||
@@ -169,64 +142,66 @@ void SetFont(FontSize fontsize, const std::string &font, uint size, bool aa)
|
||||
if (_save_config) SaveToConfig();
|
||||
}
|
||||
|
||||
void ResizeFont(FontSize fontsize, uint size)
|
||||
{
|
||||
FontCacheSubSetting *setting = GetFontCacheSubSetting(fontsize);
|
||||
SetFont(fontsize, setting->font, size, setting->aa);
|
||||
}
|
||||
|
||||
#ifdef WITH_FREETYPE
|
||||
extern void LoadFreeTypeFont(FontSize fs);
|
||||
extern void LoadFreeTypeFont(FontSize fs, const std::string &file_name, uint size);
|
||||
extern void UninitFreeType();
|
||||
#elif defined(_WIN32)
|
||||
extern void LoadWin32Font(FontSize fs);
|
||||
extern void LoadWin32Font(FontSize fs, const std::string &file_name, uint size);
|
||||
#elif defined(WITH_COCOA)
|
||||
extern void LoadCoreTextFont(FontSize fs);
|
||||
extern void LoadCoreTextFont(FontSize fs, const std::string &file_name, uint size);
|
||||
#endif
|
||||
|
||||
static void LoadFontHelper([[maybe_unused]] FontSize fs)
|
||||
{
|
||||
#ifdef WITH_FREETYPE
|
||||
LoadFreeTypeFont(fs);
|
||||
#elif defined(_WIN32)
|
||||
LoadWin32Font(fs);
|
||||
#elif defined(WITH_COCOA)
|
||||
LoadCoreTextFont(fs);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
static void TryLoadDefaultTrueTypeFont([[maybe_unused]] FontSize fs)
|
||||
{
|
||||
#if defined(WITH_FREETYPE) || defined(_WIN32) || defined(WITH_COCOA)
|
||||
std::string font_name{};
|
||||
/**
|
||||
* Get name of default font file for a given font size.
|
||||
* @param fs Font size.
|
||||
* @return Name of default font file.
|
||||
*/
|
||||
static std::string GetDefaultTruetypeFont(FontSize fs)
|
||||
{
|
||||
switch (fs) {
|
||||
case FS_NORMAL:
|
||||
font_name = "OpenTTD-Sans.ttf";
|
||||
break;
|
||||
case FS_SMALL:
|
||||
font_name = "OpenTTD-Small.ttf";
|
||||
break;
|
||||
case FS_LARGE:
|
||||
font_name = "OpenTTD-Serif.ttf";
|
||||
break;
|
||||
case FS_MONO:
|
||||
font_name = "OpenTTD-Mono.ttf";
|
||||
break;
|
||||
|
||||
case FS_NORMAL: return "OpenTTD-Sans.ttf";
|
||||
case FS_SMALL: return "OpenTTD-Small.ttf";
|
||||
case FS_LARGE: return "OpenTTD-Serif.ttf";
|
||||
case FS_MONO: return "OpenTTD-Mono.ttf";
|
||||
default: NOT_REACHED();
|
||||
}
|
||||
|
||||
/* Find font file. */
|
||||
std::string full_font = FioFindFullPath(BASESET_DIR, font_name);
|
||||
if (!full_font.empty()) {
|
||||
int size = FontCache::GetDefaultFontHeight(fs);
|
||||
#ifdef WITH_FREETYPE
|
||||
LoadFreeTypeFont(fs, full_font, size);
|
||||
#elif defined(_WIN32)
|
||||
LoadWin32Font(fs, full_font, size);
|
||||
#elif defined(WITH_COCOA)
|
||||
LoadCoreTextFont(fs, full_font, size);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
#endif /* defined(WITH_FREETYPE) || defined(_WIN32) || defined(WITH_COCOA) */
|
||||
|
||||
/**
|
||||
* Get path of default font file for a given font size.
|
||||
* @param fs Font size.
|
||||
* @return Full path of default font file.
|
||||
*/
|
||||
static std::string GetDefaultTruetypeFontFile([[maybe_unused]] FontSize fs)
|
||||
{
|
||||
#if defined(WITH_FREETYPE) || defined(_WIN32) || defined(WITH_COCOA)
|
||||
/* Find font file. */
|
||||
return FioFindFullPath(BASESET_DIR, GetDefaultTruetypeFont(fs));
|
||||
#else
|
||||
return {};
|
||||
#endif /* defined(WITH_FREETYPE) || defined(_WIN32) || defined(WITH_COCOA) */
|
||||
}
|
||||
|
||||
/**
|
||||
* Get font to use for a given font size.
|
||||
* @param fs Font size.
|
||||
* @return If configured, the font name to use, or the path of the default TrueType font if sprites are not preferred.
|
||||
*/
|
||||
std::string GetFontCacheFontName(FontSize fs)
|
||||
{
|
||||
const FontCacheSubSetting *settings = GetFontCacheSubSetting(fs);
|
||||
if (!settings->font.empty()) return settings->font;
|
||||
if (_fcsettings.prefer_sprite) return {};
|
||||
return GetDefaultTruetypeFontFile(fs);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -243,17 +218,13 @@ void InitFontCache(bool monospace)
|
||||
FontCache *fc = FontCache::Get(fs);
|
||||
if (fc->HasParent()) delete fc;
|
||||
|
||||
if (!_fcsettings.prefer_sprite && GetFontCacheSubSetting(fs)->font.empty()) {
|
||||
TryLoadDefaultTrueTypeFont(fs);
|
||||
} else {
|
||||
#ifdef WITH_FREETYPE
|
||||
LoadFreeTypeFont(fs);
|
||||
LoadFreeTypeFont(fs);
|
||||
#elif defined(_WIN32)
|
||||
LoadWin32Font(fs);
|
||||
LoadWin32Font(fs);
|
||||
#elif defined(WITH_COCOA)
|
||||
LoadCoreTextFont(fs);
|
||||
LoadCoreTextFont(fs);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -26,7 +26,6 @@ protected:
|
||||
int height; ///< The height of the font.
|
||||
int ascender; ///< The ascender value of the font.
|
||||
int descender; ///< The descender value of the font.
|
||||
int units_per_em; ///< The units per EM value of the font.
|
||||
|
||||
public:
|
||||
FontCache(FontSize fs);
|
||||
@@ -60,12 +59,6 @@ public:
|
||||
*/
|
||||
inline int GetDescender() const{ return this->descender; }
|
||||
|
||||
/**
|
||||
* Get the units per EM value of the font.
|
||||
* @return The units per EM value of the font.
|
||||
*/
|
||||
inline int GetUnitsPerEM() const { return this->units_per_em; }
|
||||
|
||||
/**
|
||||
* Get the nominal font size of the font.
|
||||
* @return The nominal font size.
|
||||
@@ -113,14 +106,6 @@ public:
|
||||
*/
|
||||
virtual GlyphID MapCharToGlyph(char32_t key, bool fallback = true) = 0;
|
||||
|
||||
/**
|
||||
* Read a font table from the font.
|
||||
* @param tag The of the table to load.
|
||||
* @param length The length of the read data.
|
||||
* @return The loaded table data.
|
||||
*/
|
||||
virtual const void *GetFontTable(uint32_t tag, size_t &length) = 0;
|
||||
|
||||
/**
|
||||
* Get the native OS font handle, if there is one.
|
||||
* @return Opaque OS font handle.
|
||||
@@ -240,6 +225,7 @@ inline FontCacheSubSetting *GetFontCacheSubSetting(FontSize fs)
|
||||
}
|
||||
}
|
||||
|
||||
std::string GetFontCacheFontName(FontSize fs);
|
||||
void InitFontCache(bool monospace);
|
||||
void UninitFontCache();
|
||||
void ResizeFont(FontSize font_size, uint32_t size);
|
||||
|
||||
@@ -33,8 +33,7 @@ class FreeTypeFontCache : public TrueTypeFontCache {
|
||||
private:
|
||||
FT_Face face; ///< The font face associated with this font.
|
||||
|
||||
void SetFontSize(FontSize fs, FT_Face face, int pixels);
|
||||
const void *InternalGetFontTable(uint32_t tag, size_t &length) override;
|
||||
void SetFontSize(int pixels);
|
||||
const Sprite *InternalGetGlyph(GlyphID key, bool aa) override;
|
||||
|
||||
public:
|
||||
@@ -60,10 +59,10 @@ FreeTypeFontCache::FreeTypeFontCache(FontSize fs, FT_Face face, int pixels) : Tr
|
||||
{
|
||||
assert(face != nullptr);
|
||||
|
||||
this->SetFontSize(fs, face, pixels);
|
||||
this->SetFontSize(pixels);
|
||||
}
|
||||
|
||||
void FreeTypeFontCache::SetFontSize(FontSize, FT_Face, int pixels)
|
||||
void FreeTypeFontCache::SetFontSize(int pixels)
|
||||
{
|
||||
if (pixels == 0) {
|
||||
/* Try to determine a good height based on the minimal height recommended by the font. */
|
||||
@@ -105,7 +104,6 @@ void FreeTypeFontCache::SetFontSize(FontSize, FT_Face, int pixels)
|
||||
}
|
||||
|
||||
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;
|
||||
@@ -162,7 +160,8 @@ void LoadFreeTypeFont(FontSize fs)
|
||||
{
|
||||
FontCacheSubSetting *settings = GetFontCacheSubSetting(fs);
|
||||
|
||||
if (settings->font.empty()) return;
|
||||
std::string font = GetFontCacheFontName(fs);
|
||||
if (font.empty()) return;
|
||||
|
||||
if (_library == nullptr) {
|
||||
if (FT_Init_FreeType(&_library) != FT_Err_Ok) {
|
||||
@@ -173,7 +172,7 @@ void LoadFreeTypeFont(FontSize fs)
|
||||
Debug(fontcache, 2, "Initialized");
|
||||
}
|
||||
|
||||
const char *font_name = settings->font.c_str();
|
||||
const char *font_name = font.c_str();
|
||||
FT_Face face = nullptr;
|
||||
|
||||
/* If font is an absolute path to a ttf, try loading that first. */
|
||||
@@ -202,34 +201,6 @@ void LoadFreeTypeFont(FontSize fs)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a TrueType font from a file.
|
||||
* @param fs The font size to load.
|
||||
* @param file_name Path to the font file.
|
||||
* @param size Requested font size.
|
||||
*/
|
||||
void LoadFreeTypeFont(FontSize fs, const std::string &file_name, uint size)
|
||||
{
|
||||
if (_library == nullptr) {
|
||||
if (FT_Init_FreeType(&_library) != FT_Err_Ok) {
|
||||
ShowInfo("Unable to initialize FreeType, using sprite fonts instead");
|
||||
return;
|
||||
}
|
||||
|
||||
Debug(fontcache, 2, "Initialized");
|
||||
}
|
||||
|
||||
FT_Face face = nullptr;
|
||||
int32_t index = 0;
|
||||
FT_Error error = FT_New_Face(_library, file_name.c_str(), index, &face);
|
||||
if (error == FT_Err_Ok) {
|
||||
LoadFont(fs, face, file_name.c_str(), size);
|
||||
} else {
|
||||
FT_Done_Face(face);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Free everything that was allocated for this font cache.
|
||||
*/
|
||||
@@ -246,7 +217,7 @@ FreeTypeFontCache::~FreeTypeFontCache()
|
||||
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->face != nullptr) this->SetFontSize(this->req_size);
|
||||
|
||||
this->TrueTypeFontCache::ClearFontCache();
|
||||
}
|
||||
@@ -325,22 +296,6 @@ GlyphID FreeTypeFontCache::MapCharToGlyph(char32_t key, bool allow_fallback)
|
||||
return glyph;
|
||||
}
|
||||
|
||||
const void *FreeTypeFontCache::InternalGetFontTable(uint32_t tag, size_t &length)
|
||||
{
|
||||
FT_ULong len = 0;
|
||||
FT_Byte *result = nullptr;
|
||||
|
||||
FT_Load_Sfnt_Table(this->face, tag, 0, nullptr, &len);
|
||||
|
||||
if (len > 0) {
|
||||
result = MallocT<FT_Byte>(len);
|
||||
FT_Load_Sfnt_Table(this->face, tag, 0, result, &len);
|
||||
}
|
||||
|
||||
length = len;
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Free everything allocated w.r.t. freetype.
|
||||
*/
|
||||
|
||||
@@ -30,7 +30,6 @@ public:
|
||||
uint GetGlyphWidth(GlyphID key) override;
|
||||
bool GetDrawGlyphShadow() override;
|
||||
GlyphID MapCharToGlyph(char32_t key, [[maybe_unused]] bool allow_fallback = true) override { assert(IsPrintable(key)); return SPRITE_GLYPH | key; }
|
||||
const void *GetFontTable(uint32_t, size_t &length) override { length = 0; return nullptr; }
|
||||
std::string GetFontName() override { return "sprite"; }
|
||||
bool IsBuiltInFont() override { return true; }
|
||||
};
|
||||
|
||||
@@ -33,10 +33,6 @@ TrueTypeFontCache::~TrueTypeFontCache()
|
||||
{
|
||||
/* Virtual functions get called statically in destructors, so make it explicit to remove any confusion. */
|
||||
this->TrueTypeFontCache::ClearFontCache();
|
||||
|
||||
for (auto &iter : this->font_tables) {
|
||||
free(iter.second.second);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -164,17 +160,3 @@ const Sprite *TrueTypeFontCache::GetGlyph(GlyphID key)
|
||||
|
||||
return this->InternalGetGlyph(key, GetFontAAState(this->fs));
|
||||
}
|
||||
|
||||
const void *TrueTypeFontCache::GetFontTable(uint32_t tag, size_t &length)
|
||||
{
|
||||
const auto iter = this->font_tables.find(tag);
|
||||
if (iter != this->font_tables.end()) {
|
||||
length = iter->second.first;
|
||||
return iter->second.second;
|
||||
}
|
||||
|
||||
const void *result = this->InternalGetFontTable(tag, length);
|
||||
|
||||
this->font_tables[tag] = std::pair<size_t, const void *>(length, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -27,9 +27,6 @@ protected:
|
||||
int req_size; ///< Requested font size.
|
||||
int used_size; ///< Used font size.
|
||||
|
||||
using FontTable = std::map<uint32_t, std::pair<size_t, const void *>>; ///< Table with font table cache
|
||||
FontTable font_tables; ///< Cached font tables.
|
||||
|
||||
/** Container for information about a glyph. */
|
||||
struct GlyphEntry {
|
||||
Sprite *sprite; ///< The loaded sprite.
|
||||
@@ -55,7 +52,6 @@ protected:
|
||||
GlyphEntry *GetGlyphPtr(GlyphID key);
|
||||
void SetGlyphPtr(GlyphID key, const GlyphEntry *glyph, bool duplicate = false);
|
||||
|
||||
virtual const void *InternalGetFontTable(uint32_t tag, size_t &length) = 0;
|
||||
virtual const Sprite *InternalGetGlyph(GlyphID key, bool aa) = 0;
|
||||
|
||||
public:
|
||||
@@ -65,7 +61,6 @@ public:
|
||||
void SetUnicodeGlyph(char32_t key, SpriteID sprite) override { this->parent->SetUnicodeGlyph(key, sprite); }
|
||||
void InitializeUnicodeGlyphMap() override { this->parent->InitializeUnicodeGlyphMap(); }
|
||||
const Sprite *GetGlyph(GlyphID key) override;
|
||||
const void *GetFontTable(uint32_t tag, size_t &length) override;
|
||||
void ClearFontCache() override;
|
||||
uint GetGlyphWidth(GlyphID key) override;
|
||||
bool GetDrawGlyphShadow() override;
|
||||
|
||||
@@ -166,7 +166,6 @@ void CoreTextFontCache::SetFontSize(int pixels)
|
||||
/* Query the font metrics we needed. We generally round all values up to
|
||||
* make sure we don't inadvertently cut off a row or column of pixels,
|
||||
* except when determining glyph to glyph advances. */
|
||||
this->units_per_em = CTFontGetUnitsPerEm(this->font.get());
|
||||
this->ascender = (int)std::ceil(CTFontGetAscent(this->font.get()));
|
||||
this->descender = -(int)std::ceil(CTFontGetDescent(this->font.get()));
|
||||
this->height = this->ascender - this->descender;
|
||||
@@ -205,18 +204,6 @@ GlyphID CoreTextFontCache::MapCharToGlyph(char32_t key, bool allow_fallback)
|
||||
return 0;
|
||||
}
|
||||
|
||||
const void *CoreTextFontCache::InternalGetFontTable(uint32_t tag, size_t &length)
|
||||
{
|
||||
CFAutoRelease<CFDataRef> data(CTFontCopyTable(this->font.get(), (CTFontTableTag)tag, kCTFontTableOptionNoOptions));
|
||||
if (!data) return nullptr;
|
||||
|
||||
length = CFDataGetLength(data.get());
|
||||
auto buf = MallocT<UInt8>(length);
|
||||
|
||||
CFDataGetBytes(data.get(), CFRangeMake(0, (CFIndex)length), buf);
|
||||
return buf;
|
||||
}
|
||||
|
||||
const Sprite *CoreTextFontCache::InternalGetGlyph(GlyphID key, bool use_aa)
|
||||
{
|
||||
/* Get glyph size. */
|
||||
@@ -341,7 +328,8 @@ void LoadCoreTextFont(FontSize fs)
|
||||
{
|
||||
FontCacheSubSetting *settings = GetFontCacheSubSetting(fs);
|
||||
|
||||
if (settings->font.empty()) return;
|
||||
std::string font = GetFontCacheFontName(fs);
|
||||
if (font.empty()) return;
|
||||
|
||||
CFAutoRelease<CTFontDescriptorRef> font_ref;
|
||||
|
||||
@@ -352,12 +340,12 @@ void LoadCoreTextFont(FontSize fs)
|
||||
|
||||
if (!font_ref && MacOSVersionIsAtLeast(10, 6, 0)) {
|
||||
/* Might be a font file name, try load it. */
|
||||
font_ref.reset(LoadFontFromFile(settings->font));
|
||||
if (!font_ref) ShowInfo("Unable to load file '{}' for {} font, using default OS font selection instead", settings->font, FontSizeToName(fs));
|
||||
font_ref.reset(LoadFontFromFile(font));
|
||||
if (!font_ref) ShowInfo("Unable to load file '{}' for {} font, using default OS font selection instead", font, FontSizeToName(fs));
|
||||
}
|
||||
|
||||
if (!font_ref) {
|
||||
CFAutoRelease<CFStringRef> name(CFStringCreateWithCString(kCFAllocatorDefault, settings->font.c_str(), kCFStringEncodingUTF8));
|
||||
CFAutoRelease<CFStringRef> name(CFStringCreateWithCString(kCFAllocatorDefault, font.c_str(), kCFStringEncodingUTF8));
|
||||
|
||||
/* Simply creating the font using CTFontCreateWithNameAndSize will *always* return
|
||||
* something, no matter the name. As such, we can't use it to check for existence.
|
||||
@@ -375,23 +363,9 @@ void LoadCoreTextFont(FontSize fs)
|
||||
}
|
||||
|
||||
if (!font_ref) {
|
||||
ShowInfo("Unable to use '{}' for {} font, using sprite font instead", settings->font, FontSizeToName(fs));
|
||||
ShowInfo("Unable to use '{}' for {} font, using sprite font instead", font, FontSizeToName(fs));
|
||||
return;
|
||||
}
|
||||
|
||||
new CoreTextFontCache(fs, std::move(font_ref), settings->size);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a TrueType font from a file.
|
||||
* @param fs The font size to load.
|
||||
* @param file_name Path to the font file.
|
||||
* @param size Requested font size.
|
||||
*/
|
||||
void LoadCoreTextFont(FontSize fs, const std::string &file_name, uint size)
|
||||
{
|
||||
CFAutoRelease<CTFontDescriptorRef> font_ref{LoadFontFromFile(file_name)};
|
||||
if (font_ref) {
|
||||
new CoreTextFontCache(fs, std::move(font_ref), size);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,6 @@ class CoreTextFontCache : public TrueTypeFontCache {
|
||||
|
||||
void SetFontSize(int pixels);
|
||||
const Sprite *InternalGetGlyph(GlyphID key, bool use_aa) override;
|
||||
const void *InternalGetFontTable(uint32_t tag, size_t &length) override;
|
||||
public:
|
||||
CoreTextFontCache(FontSize fs, CFAutoRelease<CTFontDescriptorRef> &&font, int pixels);
|
||||
~CoreTextFontCache() {}
|
||||
@@ -36,6 +35,5 @@ public:
|
||||
};
|
||||
|
||||
void LoadCoreTextFont(FontSize fs);
|
||||
void LoadCoreTextFont(FontSize fs, const std::string &file_name, uint size);
|
||||
|
||||
#endif /* FONT_OSX_H */
|
||||
|
||||
@@ -182,7 +182,6 @@ void Win32FontCache::SetFontSize(int pixels)
|
||||
POUTLINETEXTMETRIC otm = (POUTLINETEXTMETRIC)new 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;
|
||||
@@ -299,20 +298,6 @@ void Win32FontCache::ClearFontCache()
|
||||
return allow_fallback && key >= SCC_SPRITE_START && key <= SCC_SPRITE_END ? this->parent->MapCharToGlyph(key) : 0;
|
||||
}
|
||||
|
||||
/* virtual */ const void *Win32FontCache::InternalGetFontTable(uint32_t 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;
|
||||
}
|
||||
|
||||
|
||||
static bool TryLoadFontFromFile(const std::string &font_name, LOGFONT &logfont)
|
||||
{
|
||||
@@ -384,9 +369,10 @@ void LoadWin32Font(FontSize fs)
|
||||
{
|
||||
FontCacheSubSetting *settings = GetFontCacheSubSetting(fs);
|
||||
|
||||
if (settings->font.empty()) return;
|
||||
std::string font = GetFontCacheFontName(fs);
|
||||
if (font.empty()) return;
|
||||
|
||||
const char *font_name = settings->font.c_str();
|
||||
const char *font_name = font.c_str();
|
||||
LOGFONT logfont;
|
||||
MemSetT(&logfont, 0);
|
||||
logfont.lfPitchAndFamily = fs == FS_MONO ? FIXED_PITCH : VARIABLE_PITCH;
|
||||
@@ -398,8 +384,8 @@ void LoadWin32Font(FontSize fs)
|
||||
logfont = *(const LOGFONT *)settings->os_handle;
|
||||
} else if (strchr(font_name, '.') != nullptr) {
|
||||
/* Might be a font file name, try load it. */
|
||||
if (!TryLoadFontFromFile(settings->font, logfont)) {
|
||||
ShowInfo("Unable to load file '{}' for {} font, using default windows font selection instead", font_name, FontSizeToName(fs));
|
||||
if (!TryLoadFontFromFile(font, logfont)) {
|
||||
ShowInfo("Unable to load file '{}' for {} font, using default windows font selection instead", font, FontSizeToName(fs));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -410,23 +396,3 @@ void LoadWin32Font(FontSize fs)
|
||||
|
||||
LoadWin32Font(fs, logfont, settings->size, font_name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a TrueType font from a file.
|
||||
* @param fs The font size to load.
|
||||
* @param file_name Path to the font file.
|
||||
* @param size Requested font size.
|
||||
*/
|
||||
void LoadWin32Font(FontSize fs, const std::string &file_name, uint size)
|
||||
{
|
||||
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 (TryLoadFontFromFile(file_name, logfont)) {
|
||||
LoadWin32Font(fs, logfont, size, file_name.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,6 @@ private:
|
||||
void SetFontSize(int pixels);
|
||||
|
||||
protected:
|
||||
const void *InternalGetFontTable(uint32_t tag, size_t &length) override;
|
||||
const Sprite *InternalGetGlyph(GlyphID key, bool aa) override;
|
||||
|
||||
public:
|
||||
@@ -41,6 +40,5 @@ public:
|
||||
};
|
||||
|
||||
void LoadWin32Font(FontSize fs);
|
||||
void LoadWin32Font(FontSize fs, const std::string &file_name, uint size);
|
||||
|
||||
#endif /* FONT_WIN32_H */
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include "currency.h"
|
||||
#include "error.h"
|
||||
#include "settings_gui.h"
|
||||
#include "string_type.h"
|
||||
#include "textbuf_gui.h"
|
||||
#include "command_func.h"
|
||||
#include "network/network.h"
|
||||
@@ -357,18 +358,8 @@ std::unique_ptr<NWidgetBase> MakeNWidgetSocialPlugins()
|
||||
return std::make_unique<NWidgetSocialPlugins>();
|
||||
}
|
||||
|
||||
static inline FontCacheSubSetting *getSafeFontCacheSub(FontSize fs) {
|
||||
FontCache *fc = FontCache::Get(fs);
|
||||
FontCacheSubSetting *setting = GetFontCacheSubSetting(fs);
|
||||
/* Make sure all non sprite fonts are loaded. */
|
||||
if (!setting->font.empty() && !fc->HasParent()) {
|
||||
InitFontCache(fs == FS_MONO);
|
||||
fc = FontCache::Get(fs);
|
||||
}
|
||||
return setting;
|
||||
}
|
||||
|
||||
struct GameOptionsWindow : Window {
|
||||
int query_widget;
|
||||
GameSettings *opt;
|
||||
bool reload;
|
||||
int gui_scale;
|
||||
@@ -383,7 +374,7 @@ struct GameOptionsWindow : Window {
|
||||
this->gui_scale = _gui_scale;
|
||||
this->button_ratio = _button_ratio;
|
||||
for (FontSize fs = FS_BEGIN; fs < FS_END; fs++){
|
||||
FontCacheSubSetting *fc = getSafeFontCacheSub(fs);
|
||||
FontCacheSubSetting *fc = GetFontCacheSubSetting(fs);
|
||||
font_sizes[fs] = fc->size;
|
||||
}
|
||||
|
||||
@@ -833,13 +824,26 @@ struct GameOptionsWindow : Window {
|
||||
case WID_GO_MONO_FONT_L:
|
||||
case WID_GO_MONO_FONT_R: {
|
||||
int index = widget - WID_GO_NORMAL_FONT_L;
|
||||
FontSize fs = static_cast<FontSize>(Clamp((widget - WID_GO_NORMAL_FONT_L) / 2, FS_BEGIN, FS_END));
|
||||
FontSize fs = static_cast<FontSize>(Clamp((widget - WID_GO_NORMAL_FONT_L) / 2, FS_BEGIN, FS_END));
|
||||
|
||||
if (index % 2 == 0) {
|
||||
this->font_sizes[fs] = Clamp(this->font_sizes[fs] - 1, 0, 200);
|
||||
} else {
|
||||
this->font_sizes[fs] = Clamp(this->font_sizes[fs] + 1, 0, 200);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case WID_GO_NORMAL_FONT_VALUE:
|
||||
case WID_GO_SMALL_FONT_VALUE:
|
||||
case WID_GO_LARGE_FONT_VALUE:
|
||||
case WID_GO_MONO_FONT_VALUE: {
|
||||
int index = Clamp(widget - WID_GO_NORMAL_FONT_VALUE, FS_BEGIN, FS_END);
|
||||
this->query_widget = widget;
|
||||
FontSize fs = static_cast<FontSize>(index);
|
||||
SetDParam(0, this->font_sizes[fs]);
|
||||
ShowQueryString(STR_JUST_INT, STR_GAME_OPTIONS_FONT_NORMAL + index, 3, this, CS_NUMERAL, QSF_NONE);
|
||||
break;
|
||||
}
|
||||
|
||||
case WID_GO_MOUSE_CURSOR:
|
||||
@@ -959,6 +963,15 @@ struct GameOptionsWindow : Window {
|
||||
}
|
||||
}
|
||||
|
||||
void OnQueryTextFinished(char *str) override
|
||||
{
|
||||
if (str == nullptr) return;
|
||||
|
||||
int index = Clamp(this->query_widget - WID_GO_NORMAL_FONT_VALUE, FS_BEGIN, FS_END);
|
||||
this->font_sizes[index] = Clamp(atoi(str), 0, 200);
|
||||
}
|
||||
|
||||
|
||||
bool anySizeSettingChanged()
|
||||
{
|
||||
if (this->gui_scale != _gui_scale || this->button_ratio != _button_ratio) {
|
||||
@@ -966,7 +979,7 @@ struct GameOptionsWindow : Window {
|
||||
}
|
||||
|
||||
for (FontSize fs = FS_BEGIN; fs < FS_END; fs++){
|
||||
FontCacheSubSetting *fc = getSafeFontCacheSub(fs);
|
||||
FontCacheSubSetting *fc = GetFontCacheSubSetting(fs);
|
||||
if (this->font_sizes[fs] != fc->size)
|
||||
return true;
|
||||
}
|
||||
@@ -981,7 +994,7 @@ struct GameOptionsWindow : Window {
|
||||
_button_ratio_cfg = this->button_ratio;
|
||||
|
||||
for (FontSize fs = FS_BEGIN; fs < FS_END; fs++){
|
||||
FontCacheSubSetting *fc = getSafeFontCacheSub(fs);
|
||||
FontCacheSubSetting *fc = GetFontCacheSubSetting(fs);
|
||||
if (this->font_sizes[fs] != fc->size) {
|
||||
ResizeFont(fs, this->font_sizes[fs]);
|
||||
return;
|
||||
|
||||
@@ -30,7 +30,6 @@ public:
|
||||
uint GetGlyphWidth(GlyphID) override { return this->height / 2; }
|
||||
bool GetDrawGlyphShadow() override { return false; }
|
||||
GlyphID MapCharToGlyph(char32_t key, [[maybe_unused]] bool allow_fallback = true) override { return key; }
|
||||
const void *GetFontTable(uint32_t, size_t &length) override { length = 0; return nullptr; }
|
||||
std::string GetFontName() override { return "mock"; }
|
||||
bool IsBuiltInFont() override { return true; }
|
||||
|
||||
|
||||
@@ -525,9 +525,7 @@ bool VideoDriver_SDL::PollEvent()
|
||||
}
|
||||
|
||||
if (_cursor.UpdateCursorPosition(x, y)) {
|
||||
#ifndef __ANDROID__ // No mouse warping on Android, mouse strictly follows finger
|
||||
SDL_WarpMouse(_cursor.pos.x, _cursor.pos.y);
|
||||
#endif
|
||||
}
|
||||
HandleMouseEvents();
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user