From 895c0b254e0ac9911ccf2a1d954bb00a76a4d961 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Sat, 15 Jun 2024 21:11:16 +0100 Subject: [PATCH] Add: FontSearcher interface. --- src/fontcache.cpp | 16 ++++++++++++++++ src/fontdetection.h | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/src/fontcache.cpp b/src/fontcache.cpp index 93d56416ae..954f38d5ed 100644 --- a/src/fontcache.cpp +++ b/src/fontcache.cpp @@ -230,6 +230,22 @@ void UninitFontCache() #endif /* WITH_FREETYPE */ } +/** + * Register the FontSearcher instance. There can be only one font searcher, which depends on platform. + */ +FontSearcher::FontSearcher() +{ + FontSearcher::instance = this; +} + +/** + * Deregister this FontSearcher. + */ +FontSearcher::~FontSearcher() +{ + FontSearcher::instance = nullptr; +} + #if !defined(_WIN32) && !defined(__APPLE__) && !defined(WITH_FONTCONFIG) && !defined(WITH_COCOA) bool SetFallbackFont(FontCacheSettings *, const std::string &, int, MissingGlyphSearcher *) { return false; } diff --git a/src/fontdetection.h b/src/fontdetection.h index 2a316da505..43a5f4731d 100644 --- a/src/fontdetection.h +++ b/src/fontdetection.h @@ -39,4 +39,45 @@ FT_Error GetFontByFaceName(const char *font_name, FT_Face *face); */ bool SetFallbackFont(struct FontCacheSettings *settings, const std::string &language_isocode, int winlangid, class MissingGlyphSearcher *callback); +struct FontFamily { + std::string family; + std::string style; + int32_t slant; + int32_t weight; + + FontFamily(std::string_view family, std::string_view style, int32_t slant, int32_t weight) : family(family), style(style), slant(slant), weight(weight) {} +}; + +class FontSearcher { +public: + FontSearcher(); + virtual ~FontSearcher(); + + /** + * Get the active FontSearcher instance. + * @return FontSearcher instance, or nullptr if not present. + */ + static inline FontSearcher *GetFontSearcher() { return FontSearcher::instance; } + + /** + * List available fonts. + * @param language_isocode the language, e.g. en_GB. + * @param winlangid the language ID windows style. + * @return vector containing font family names. + */ + virtual std::vector ListFamilies(const std::string &language_isocode, int winlangid) = 0; + + /** + * List available styles for a font family. + * @param language_isocode the language, e.g. en_GB. + * @param winlangid the language ID windows style. + * @param font_family The font family to list. + * @return vector containing style information for the family. + */ + virtual std::vector ListStyles(const std::string &language_isocode, int winlangid, std::string_view font_family) = 0; + +private: + static inline FontSearcher *instance = nullptr; +}; + #endif