33#define WIN32_LEAN_AND_MEAN
56 return wxString::Format(
"%d.%d.%d", FC_MAJOR, FC_MINOR, FC_REVISION );
82#if defined( _MSC_VER )
88#if defined( _MSC_VER )
90 __except( GetExceptionCode() == STATUS_IN_PAGE_ERROR ? EXCEPTION_EXECUTE_HANDLER
91 : EXCEPTION_CONTINUE_SEARCH )
118 if( aSearchLang.Lower() == aSupportedLang.Lower() )
121 if( aSupportedLang.empty() )
124 if( aSearchLang.empty() )
127 wxArrayString supportedLangBits;
128 wxStringSplit( aSupportedLang.Lower(), supportedLangBits, wxS(
'-' ) );
130 wxArrayString searhcLangBits;
131 wxStringSplit( aSearchLang.Lower(), searhcLangBits, wxS(
'-' ) );
135 if( searhcLangBits.size() == 1 || supportedLangBits.size() == 1 )
137 return searhcLangBits[0] == supportedLangBits[0];
151 if( FcPatternGetString( aPat.
pat, aObj, aIdx, &str ) == FcResultMatch )
152 res = std::string(
reinterpret_cast<char*
>( str ) );
159 std::unordered_map<std::string, std::string>& aFamStringMap )
168 famLang =
getFcString( aPat, FC_FAMILYLANG, langIdx );
170 if( famLang.empty() && langIdx != 0 )
177 aFamStringMap.insert_or_assign( famLang, fam );
179 }
while( langIdx++ < std::numeric_limits<
186 std::unordered_map<std::string, std::string> famStrings;
189 if( famStrings.empty() )
192 for(
auto const& [key, val] : famStrings )
203 return famStrings.begin()->second;
208 int& aFaceIndex,
bool aBold,
bool aItalic,
209 const std::vector<wxString>* aEmbeddedFiles )
218 if( aFontName.Lower().Contains( wxS(
"bold" ) )
219 || aFontName.Lower().Contains( wxS(
"heavy" ) )
220 || aFontName.Lower().Contains( wxS(
"black" ) )
221 || aFontName.Lower().Contains( wxS(
"thick" ) )
222 || aFontName.Lower().Contains( wxS(
"dark" ) ) )
227 FcConfig*
config = FcConfigGetCurrent();
231 for(
const auto& file : *aEmbeddedFiles )
233 FcConfigAppFontAddFile(
config, (
const FcChar8*) file.c_str().AsChar() );
237 wxString qualifiedFontName = aFontName;
239 wxScopedCharBuffer
const fcBuffer = qualifiedFontName.ToUTF8();
241 FcPattern* pat = FcPatternCreate();
244 FcPatternAddString( pat, FC_STYLE, (
const FcChar8*)
"Bold" );
247 FcPatternAddString( pat, FC_STYLE, (
const FcChar8*)
"Italic" );
249 FcPatternAddString( pat, FC_FAMILY, (FcChar8*) fcBuffer.data() );
251 FcConfigSubstitute(
config, pat, FcMatchPattern );
252 FcDefaultSubstitute( pat );
254 FcResult r = FcResultNoMatch;
255 FcPattern* font = FcFontMatch(
config, pat, &r );
261 FcChar8* file =
nullptr;
263 if( FcPatternGetString( font, FC_FILE, 0, &file ) == FcResultMatch )
265 aFontFile = wxString::FromUTF8( (
char*) file );
269 FcChar8* family =
nullptr;
270 FcChar8* style =
nullptr;
274 std::unordered_map<std::string, std::string> famStrings;
279 if( FcPatternGetString( font, FC_FAMILY, 0, &family ) == FcResultMatch )
281 FcPatternGetInteger( font, FC_INDEX, 0, &aFaceIndex );
283 fontName = wxString::FromUTF8( (
char*) family );
285 if( FcPatternGetString( font, FC_STYLE, 0, &style ) == FcResultMatch )
287 styleStr = wxString::FromUTF8( (
char*) style );
289 if( !styleStr.IsEmpty() )
291 styleStr.Replace(
' ',
':' );
292 fontName +=
':' + styleStr;
296 bool has_bold =
false;
297 bool has_ital =
false;
298 wxString lower_style = styleStr.Lower();
300 if( lower_style.Contains( wxS(
"thin" ) )
301 || lower_style.Contains( wxS(
"light" ) )
302 || lower_style.Contains( wxS(
"regular" ) )
303 || lower_style.Contains( wxS(
"roman" ) )
304 || lower_style.Contains( wxS(
"book" ) ) )
308 else if( lower_style.Contains( wxS(
"medium" ) )
309 || lower_style.Contains( wxS(
"semibold" ) )
310 || lower_style.Contains( wxS(
"demibold" ) ) )
314 else if( lower_style.Contains( wxS(
"bold" ) )
315 || lower_style.Contains( wxS(
"heavy" ) )
316 || lower_style.Contains( wxS(
"black" ) )
317 || lower_style.Contains( wxS(
"thick" ) )
318 || lower_style.Contains( wxS(
"dark" ) ) )
323 if( lower_style.Contains( wxS(
"italic" ) )
324 || lower_style.Contains( wxS(
"oblique" ) )
325 || lower_style.Contains( wxS(
"slant" ) ) )
330 for(
auto const& [key, val] : famStrings )
333 searchFont = wxString::FromUTF8( (
char*) val.data() );
335 if( searchFont.Lower().StartsWith( aFontName.Lower() ) )
337 if( ( aBold && !has_bold ) && ( aItalic && !has_ital ) )
339 else if( aBold && !has_bold )
341 else if( aItalic && !has_ital )
343 else if( ( aBold != has_bold ) || ( aItalic != has_ital ) )
354 FcPatternDestroy( font );
361 qualifiedFontName ) );
365 fontName.Replace(
':',
' ' );
369 if( fontName.CmpNoCase( qualifiedFontName ) == 0 )
372 s_reporter->
Report( wxString::Format(
_(
"Font '%s' not found; substituting '%s'." ),
373 qualifiedFontName, fontName ) );
376 FcPatternDestroy( pat );
382 const std::vector<wxString>* aEmbeddedFiles,
bool aForce )
390 FcConfig*
config = FcConfigGetCurrent();
394 for(
const auto& file : *aEmbeddedFiles )
396 FcConfigAppFontAddFile(
config, (
const FcChar8*) file.c_str().AsChar() );
400 FcPattern* pat = FcPatternCreate();
401 FcObjectSet* os = FcObjectSetBuild( FC_FAMILY, FC_FAMILYLANG, FC_STYLE, FC_LANG, FC_FILE,
402 FC_OUTLINE,
nullptr );
403 FcFontSet* fs = FcFontList(
config, pat, os );
405 for(
int i = 0; fs && i < fs->nfont; ++i )
407 FcPattern* font = fs->fonts[i];
413 if( FcPatternGetString( font, FC_FILE, 0, &file ) == FcResultMatch
414 && FcPatternGetString( font, FC_STYLE, 0, &style ) == FcResultMatch
415 && FcPatternGetLangSet( font, FC_LANG, 0, &langSet ) == FcResultMatch
416 && FcPatternGetBool( font, FC_OUTLINE, 0, &outline ) == FcResultMatch )
422 std::string theFamily =
433 static std::map<wxString, bool> availableLanguages;
435 FcStrSet* langStrSet = FcLangSetGetLangs( langSet );
436 FcStrList* langStrList = FcStrListCreate( langStrSet );
437 FcChar8* langStr = FcStrListNext( langStrList );
438 bool langSupported =
false;
443 langSupported =
true;
445 else while( langStr )
447 wxString langWxStr(
reinterpret_cast<char *
>( langStr ) );
449 if( availableLanguages.find( langWxStr ) == availableLanguages.end() )
451 const wxLanguageInfo* langInfo = wxLocale::FindLanguageInfo( langWxStr );
452 bool available = langInfo && wxLocale::IsAvailable( langInfo->Language );
454 availableLanguages[ langWxStr ] = available;
457 if( availableLanguages[ langWxStr ] )
459 langSupported =
true;
465 wxS(
"Font '%s' language '%s' not supported by OS." ),
466 theFamily, langWxStr );
469 langStr = FcStrListNext( langStrList );
472 FcStrListDone( langStrList );
473 FcStrSetDestroy( langStrSet );
479 std::string theFile(
reinterpret_cast<char *
>( file ) );
480 std::string theStyle(
reinterpret_cast<char *
>( style ) );
481 FONTINFO fontInfo( std::move( theFile ), std::move( theStyle ), theFamily );
483 if( theFamily.length() > 0 && theFamily.front() ==
'.' )
486 std::map<std::string, FONTINFO>::iterator it =
m_fontInfoCache.find( theFamily );
491 it->second.Children().push_back( fontInfo );
496 FcFontSetDestroy( fs );
501 for(
const std::pair<const std::string, FONTINFO>& entry :
m_fontInfoCache )
502 aFonts.push_back( entry.second.Family() );
A pure virtual class used to derive REPORTER objects from.
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)=0
Report a string with a given severity.
static REPORTER * s_reporter
void getAllFamilyStrings(FONTCONFIG_PAT &aPat, std::unordered_map< std::string, std::string > &aFamStringMap)
Get a list of all family name strings mapped to lang.
static wxString Version()
static void SetReporter(REPORTER *aReporter)
Set the reporter to use for reporting font substitution warnings.
std::map< std::string, FONTINFO > m_fontInfoCache
FF_RESULT FindFont(const wxString &aFontName, wxString &aFontFile, int &aFaceIndex, bool aBold, bool aItalic, const std::vector< wxString > *aEmbeddedFiles=nullptr)
Given a fully-qualified font name ("Times:Bold:Italic") find the closest matching font and return its...
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)
Get a family name based on desired language.
bool isLanguageMatch(const wxString &aSearchLang, const wxString &aSupportedLang)
Match two rfc 3306 language entries, used for when searching for matching family names.
void ListFonts(std::vector< std::string > &aFonts, const std::string &aDesiredLang, const std::vector< wxString > *aEmbeddedFiles=nullptr, bool aForce=false)
List the current available font families.
wxString m_fontCacheLastLang
static FONTCONFIG * g_config
FONTCONFIG * Fontconfig()
static void bootstrapFc()
This is simply a wrapper to call FcInit() with SEH for Windows.
static bool g_fcInitSuccess
static std::mutex g_fontConfigMutex
const wxChar *const traceFonts
Flag to enable locale debug output.
This file contains miscellaneous commonly used macros and functions.
static WX_STRING_REPORTER s_reporter
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 exporting fontconfig in the header.
wxLogTrace helper definitions.