31#define WIN32_LEAN_AND_MEAN
51 return wxString::Format(
"%d.%d.%d", FC_MAJOR, FC_MINOR, FC_REVISION );
69#if defined( _MSC_VER )
75#if defined( _MSC_VER )
77 __except( GetExceptionCode() == STATUS_IN_PAGE_ERROR ? EXCEPTION_EXECUTE_HANDLER
78 : EXCEPTION_CONTINUE_SEARCH )
104 if( aSearchLang.Lower() == aSupportedLang.Lower() )
107 if( aSupportedLang.empty() )
110 if( aSearchLang.empty() )
113 wxArrayString supportedLangBits;
114 wxStringSplit( aSupportedLang.Lower(), supportedLangBits, wxS(
'-' ) );
116 wxArrayString searhcLangBits;
117 wxStringSplit( aSearchLang.Lower(), searhcLangBits, wxS(
'-' ) );
121 if( searhcLangBits.size() == 1 || supportedLangBits.size() == 1 )
123 return searhcLangBits[0] == supportedLangBits[0];
137 if( FcPatternGetString( aPat.
pat, aObj, aIdx, &str ) == FcResultMatch )
138 res = std::string(
reinterpret_cast<char*
>( str ) );
145 std::unordered_map<std::string, std::string>& aFamStringMap )
153 famLang =
getFcString( aPat, FC_FAMILYLANG, langIdx );
155 if( famLang.empty() && langIdx != 0 )
160 aFamStringMap.insert_or_assign( famLang, fam );
162 }
while( langIdx++ < std::numeric_limits<
169 std::unordered_map<std::string, std::string> famStrings;
172 if( famStrings.empty() )
175 for(
auto const& [key, val] : famStrings )
186 return famStrings.begin()->second;
191 int& aFaceIndex,
bool aBold,
bool aItalic )
198 wxString qualifiedFontName = aFontName;
200 wxScopedCharBuffer
const fcBuffer = qualifiedFontName.ToUTF8();
202 FcPattern* pat = FcPatternCreate();
205 FcPatternAddString( pat, FC_STYLE, (
const FcChar8*)
"Bold" );
208 FcPatternAddString( pat, FC_STYLE, (
const FcChar8*)
"Italic" );
210 FcPatternAddString( pat, FC_FAMILY, (FcChar8*) fcBuffer.data() );
212 FcConfigSubstitute(
nullptr, pat, FcMatchPattern );
213 FcDefaultSubstitute( pat );
215 FcResult r = FcResultNoMatch;
216 FcPattern* font = FcFontMatch(
nullptr, pat, &r );
222 FcChar8* file =
nullptr;
224 if( FcPatternGetString( font, FC_FILE, 0, &file ) == FcResultMatch )
226 aFontFile = wxString::FromUTF8( (
char*) file );
230 FcChar8* family =
nullptr;
231 FcChar8* style =
nullptr;
235 std::unordered_map<std::string, std::string> famStrings;
240 if( FcPatternGetString( font, FC_FAMILY, 0, &family ) == FcResultMatch )
242 FcPatternGetInteger( font, FC_INDEX, 0, &aFaceIndex );
244 fontName = wxString::FromUTF8( (
char*) family );
246 if( FcPatternGetString( font, FC_STYLE, 0, &style ) == FcResultMatch )
248 styleStr = wxString::FromUTF8( (
char*) style );
250 if( !styleStr.IsEmpty() )
252 styleStr.Replace(
' ',
':' );
253 fontName +=
':' + styleStr;
257 bool has_bold =
false;
258 bool has_ital =
false;
259 wxString lower_style = styleStr.Lower();
261 if( lower_style.Contains( wxS(
"thin" ) )
262 || lower_style.Contains( wxS(
"light" ) )
263 || lower_style.Contains( wxS(
"regular" ) )
264 || lower_style.Contains( wxS(
"roman" ) )
265 || lower_style.Contains( wxS(
"book" ) ) )
269 else if( lower_style.Contains( wxS(
"medium" ) )
270 || lower_style.Contains( wxS(
"semibold" ) )
271 || lower_style.Contains( wxS(
"demibold" ) ) )
275 else if( lower_style.Contains( wxS(
"bold" ) )
276 || lower_style.Contains( wxS(
"heavy" ) )
277 || lower_style.Contains( wxS(
"black" ) )
278 || lower_style.Contains( wxS(
"thick" ) )
279 || lower_style.Contains( wxS(
"dark" ) ) )
284 if( lower_style.Contains( wxS(
"italic" ) )
285 || lower_style.Contains( wxS(
"oblique" ) )
286 || lower_style.Contains( wxS(
"slant" ) ) )
291 for(
auto const& [key, val] : famStrings )
294 searchFont = wxString::FromUTF8( (
char*) val.data() );
296 if( searchFont.Lower().StartsWith( aFontName.Lower() ) )
298 if( ( aBold && !has_bold ) && ( aItalic && !has_ital ) )
300 else if( aBold && !has_bold )
302 else if( aItalic && !has_ital )
304 else if( ( aBold != has_bold ) || ( aItalic != has_ital ) )
315 FcPatternDestroy( font );
320 wxLogWarning(
_(
"Error loading font '%s'." ), qualifiedFontName );
324 fontName.Replace(
':',
' ' );
325 wxLogWarning(
_(
"Font '%s' not found; substituting '%s'." ), qualifiedFontName, fontName );
328 FcPatternDestroy( pat );
341 FcPattern* pat = FcPatternCreate();
342 FcObjectSet* os = FcObjectSetBuild( FC_FAMILY, FC_FAMILYLANG, FC_STYLE, FC_LANG, FC_FILE,
343 FC_OUTLINE,
nullptr );
344 FcFontSet* fs = FcFontList(
nullptr, pat, os );
346 for(
int i = 0; fs && i < fs->nfont; ++i )
348 FcPattern* font = fs->fonts[i];
354 if( FcPatternGetString( font, FC_FILE, 0, &file ) == FcResultMatch
355 && FcPatternGetString( font, FC_STYLE, 0, &style ) == FcResultMatch
356 && FcPatternGetLangSet( font, FC_LANG, 0, &langSet ) == FcResultMatch
357 && FcPatternGetBool( font, FC_OUTLINE, 0, &outline ) == FcResultMatch )
363 std::string theFamily =
374 static std::map<wxString, bool> availableLanguages;
376 FcStrSet* langStrSet = FcLangSetGetLangs( langSet );
377 FcStrList* langStrList = FcStrListCreate( langStrSet );
378 FcChar8* langStr = FcStrListNext( langStrList );
379 bool langSupported =
false;
384 langSupported =
true;
386 else while( langStr )
388 wxString langWxStr(
reinterpret_cast<char *
>( langStr ) );
390 if( availableLanguages.find( langWxStr ) == availableLanguages.end() )
392 const wxLanguageInfo* langInfo = wxLocale::FindLanguageInfo( langWxStr );
393 bool available = langInfo && wxLocale::IsAvailable( langInfo->Language );
395 availableLanguages[ langWxStr ] = available;
398 if( availableLanguages[ langWxStr ] )
400 langSupported =
true;
405 wxLogTrace(
traceFonts, wxS(
"Font '%s' language '%s' not supported by OS." ),
406 theFamily, langWxStr );
409 langStr = FcStrListNext( langStrList );
412 FcStrListDone( langStrList );
413 FcStrSetDestroy( langStrSet );
419 std::string theFile(
reinterpret_cast<char *
>( file ) );
420 std::string theStyle(
reinterpret_cast<char *
>( style ) );
421 FONTINFO fontInfo( std::move( theFile ), std::move( theStyle ), theFamily );
423 if( theFamily.length() > 0 && theFamily.front() ==
'.' )
426 std::map<std::string, FONTINFO>::iterator it =
m_fontInfoCache.find( theFamily );
431 it->second.Children().push_back( fontInfo );
436 FcFontSetDestroy( fs );
441 for(
const std::pair<const std::string, FONTINFO>& entry :
m_fontInfoCache )
442 aFonts.push_back( entry.second.Family() );
void getAllFamilyStrings(FONTCONFIG_PAT &aPat, std::unordered_map< std::string, std::string > &aFamStringMap)
Gets a list of all family name strings maped to lang.
static wxString Version()
std::map< std::string, FONTINFO > m_fontInfoCache
FF_RESULT FindFont(const wxString &aFontName, wxString &aFontFile, int &aFaceIndex, bool aBold, bool aItalic)
Given a fully-qualified font name ("Times:Bold:Italic") find the closest matching font and return its...
void ListFonts(std::vector< std::string > &aFonts, const std::string &aDesiredLang)
List the current available font families.
std::string getFcString(FONTCONFIG_PAT &aPat, const char *aObj, int aIdx)
Wrapper of FcPatternGetString to return a std::string.
std::string getFamilyStringByLang(FONTCONFIG_PAT &APat, const wxString &aDesiredLang)
Gets a family name based on desired language.
bool isLanguageMatch(const wxString &aSearchLang, const wxString &aSupportedLang)
Matches the two rfc 3306 language entries, used for when searching for matching family names.
wxString m_fontCacheLastLang
static FONTCONFIG * g_config
FONTCONFIG * Fontconfig()
static void bootstrapFc()
This is simply a wrapper to call FcInit() with SEH for Windows SEH on Windows can only be used in fun...
static bool g_fcInitSuccess
const wxChar *const traceFonts
Flag to enable locale debug output.
This file contains miscellaneous commonly used macros and functions.
wxString From_UTF8(const char *cstring)
void wxStringSplit(const wxString &aText, wxArrayString &aStrings, wxChar aSplitter)
Split aString to a string list separated at aSplitter.
A simple wrapper to avoid exporing fontconfig in the header.
wxLogTrace helper definitions.