diff options
author | Michael Lutz <michi@icosahedron.de> | 2018-11-25 02:02:20 +0100 |
---|---|---|
committer | Owen Rudge <owen@owenrudge.net> | 2019-05-14 11:21:36 +0100 |
commit | d2ed4260772e0a2ce8805e9c40525d0abb9345ea (patch) | |
tree | 2daef56dc038e55aa630a4f55f8af0eeeaff9b06 | |
parent | 2675762ae9c58c47dc442b422927206c7bee13a8 (diff) | |
download | openttd-d2ed4260772e0a2ce8805e9c40525d0abb9345ea.tar.xz |
Codechange: [Win32] Pass a native GDI font description around when we have one, instead of repeatedly guessing the font.
-rw-r--r-- | src/fontcache.cpp | 18 | ||||
-rw-r--r-- | src/fontcache.h | 11 | ||||
-rw-r--r-- | src/fontdetection.cpp | 4 | ||||
-rw-r--r-- | src/os/windows/string_uniscribe.cpp | 2 | ||||
-rw-r--r-- | src/strings.cpp | 15 | ||||
-rw-r--r-- | src/strings_func.h | 3 | ||||
-rw-r--r-- | src/textfile_gui.cpp | 6 | ||||
-rw-r--r-- | src/textfile_gui.h | 2 |
8 files changed, 47 insertions, 14 deletions
diff --git a/src/fontcache.cpp b/src/fontcache.cpp index 778af3440..709b8a7d6 100644 --- a/src/fontcache.cpp +++ b/src/fontcache.cpp @@ -737,6 +737,7 @@ public: 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; } }; @@ -960,13 +961,16 @@ static void LoadWin32Font(FontSize fs) LOGFONT logfont; MemSetT(&logfont, 0); - - logfont.lfWeight = strcasestr(settings->font, " bold") != nullptr ? FW_BOLD : FW_NORMAL; // Poor man's way to allow selecting bold fonts. - logfont.lfPitchAndFamily = fs == FS_MONO ? FIXED_PITCH : VARIABLE_PITCH; - logfont.lfCharSet = DEFAULT_CHARSET; - logfont.lfOutPrecision = OUT_OUTLINE_PRECIS; - logfont.lfClipPrecision = CLIP_DEFAULT_PRECIS; - convert_to_fs(settings->font, logfont.lfFaceName, lengthof(logfont.lfFaceName), false); + if (settings->os_handle != nullptr) logfont = *(const LOGFONT *)settings->os_handle; + + 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. + logfont.lfPitchAndFamily = fs == FS_MONO ? FIXED_PITCH : VARIABLE_PITCH; + logfont.lfCharSet = DEFAULT_CHARSET; + logfont.lfOutPrecision = OUT_OUTLINE_PRECIS; + logfont.lfClipPrecision = CLIP_DEFAULT_PRECIS; + convert_to_fs(settings->font, logfont.lfFaceName, lengthof(logfont.lfFaceName), false); + } HFONT font = CreateFontIndirect(&logfont); if (font == nullptr) { diff --git a/src/fontcache.h b/src/fontcache.h index 62ce596f3..741610395 100644 --- a/src/fontcache.h +++ b/src/fontcache.h @@ -126,6 +126,15 @@ public: virtual const void *GetFontTable(uint32 tag, size_t &length) = 0; /** + * Get the native OS font handle, if there is one. + * @return Opaque OS font handle. + */ + virtual void *GetOSHandle() + { + return nullptr; + } + + /** * Get the name of this font. * @return The name of the font. */ @@ -209,6 +218,8 @@ struct FreeTypeSubSetting { char font[MAX_PATH]; ///< The name of the font, or path to the font. uint size; ///< The (requested) size of the font. bool aa; ///< Whether to do anti aliasing or not. + + const void *os_handle = nullptr; ///< Optional native OS font info. }; /** Settings for the freetype fonts. */ diff --git a/src/fontdetection.cpp b/src/fontdetection.cpp index f7465a268..7b2a6756c 100644 --- a/src/fontdetection.cpp +++ b/src/fontdetection.cpp @@ -346,7 +346,9 @@ static int CALLBACK EnumFontCallback(const ENUMLOGFONTEX *logfont, const NEWTEXT const char *english_name = font_name; #endif /* WITH_FREETYPE */ - info->callback->SetFontNames(info->settings, font_name); + PLOGFONT os_data = MallocT<LOGFONT>(1); + *os_data = logfont->elfLogFont; + info->callback->SetFontNames(info->settings, font_name, os_data); if (info->callback->FindMissingGlyphs(nullptr)) return 1; DEBUG(freetype, 1, "Fallback font: %s (%s)", font_name, english_name); return 0; // stop enumerating diff --git a/src/os/windows/string_uniscribe.cpp b/src/os/windows/string_uniscribe.cpp index 8c4e11d3f..01f0e5e3b 100644 --- a/src/os/windows/string_uniscribe.cpp +++ b/src/os/windows/string_uniscribe.cpp @@ -148,6 +148,8 @@ void UniscribeResetScriptCache(FontSize size) /** Load the matching native Windows font. */ static HFONT HFontFromFont(Font *font) { + if (font->fc->GetOSHandle() != nullptr) return CreateFontIndirect((PLOGFONT)font->fc->GetOSHandle()); + LOGFONT logfont; ZeroMemory(&logfont, sizeof(LOGFONT)); logfont.lfHeight = font->fc->GetHeight(); diff --git a/src/strings.cpp b/src/strings.cpp index fda92d8a7..5d186e717 100644 --- a/src/strings.cpp +++ b/src/strings.cpp @@ -2068,13 +2068,18 @@ class LanguagePackGlyphSearcher : public MissingGlyphSearcher { return false; } - void SetFontNames(FreeTypeSettings *settings, const char *font_name) override + void SetFontNames(FreeTypeSettings *settings, const char *font_name, const void *os_data) override { #if defined(WITH_FREETYPE) || defined(_WIN32) strecpy(settings->small.font, font_name, lastof(settings->small.font)); strecpy(settings->medium.font, font_name, lastof(settings->medium.font)); strecpy(settings->large.font, font_name, lastof(settings->large.font)); -#endif /* WITH_FREETYPE */ + + free(settings->medium.os_handle); // Only free one, they are all the same pointer. + settings->small.os_handle = os_data; + settings->medium.os_handle = os_data; + settings->large.os_handle = os_data; +#endif } }; @@ -2103,8 +2108,14 @@ void CheckForMissingGlyphs(bool base_font, MissingGlyphSearcher *searcher) FreeTypeSettings backup; memcpy(&backup, &_freetype, sizeof(backup)); + _freetype.mono.os_handle = nullptr; + _freetype.medium.os_handle = nullptr; + bad_font = !SetFallbackFont(&_freetype, _langpack->isocode, _langpack->winlangid, searcher); + free(_freetype.mono.os_handle); + free(_freetype.medium.os_handle); + memcpy(&_freetype, &backup, sizeof(backup)); if (bad_font && base_font) { diff --git a/src/strings_func.h b/src/strings_func.h index d0fbde495..26ca56fbd 100644 --- a/src/strings_func.h +++ b/src/strings_func.h @@ -275,8 +275,9 @@ public: * Set the right font names. * @param settings The settings to modify. * @param font_name The new font name. + * @param os_data Opaque pointer to OS-specific data. */ - virtual void SetFontNames(struct FreeTypeSettings *settings, const char *font_name) = 0; + virtual void SetFontNames(struct FreeTypeSettings *settings, const char *font_name, const void *os_data = nullptr) = 0; bool FindMissingGlyphs(const char **str); }; diff --git a/src/textfile_gui.cpp b/src/textfile_gui.cpp index 07851e12f..5676a7d10 100644 --- a/src/textfile_gui.cpp +++ b/src/textfile_gui.cpp @@ -194,11 +194,13 @@ void TextfileWindow::SetupScrollbars() return true; } -/* virtual */ void TextfileWindow::SetFontNames(FreeTypeSettings *settings, const char *font_name) +/* virtual */ void TextfileWindow::SetFontNames(FreeTypeSettings *settings, const char *font_name, const void *os_data) { #if defined(WITH_FREETYPE) || defined(_WIN32) strecpy(settings->mono.font, font_name, lastof(settings->mono.font)); -#endif /* WITH_FREETYPE */ + free(settings->mono.os_handle); + settings->mono.os_handle = os_data; +#endif } #if defined(WITH_ZLIB) diff --git a/src/textfile_gui.h b/src/textfile_gui.h index b572fe09c..237b36053 100644 --- a/src/textfile_gui.h +++ b/src/textfile_gui.h @@ -43,7 +43,7 @@ struct TextfileWindow : public Window, MissingGlyphSearcher { FontSize DefaultSize() override; const char *NextString() override; bool Monospace() override; - void SetFontNames(FreeTypeSettings *settings, const char *font_name) override; + void SetFontNames(FreeTypeSettings *settings, const char *font_name, const void *os_data) override; virtual void LoadTextfile(const char *textfile, Subdirectory dir); |