33#define WIN32_LEAN_AND_MEAN
55 return wxString::Format(
"%d.%d.%d", FC_MAJOR, FC_MINOR, FC_REVISION );
80#if defined( _MSC_VER )
86#if defined( _MSC_VER )
88 __except( GetExceptionCode() == STATUS_IN_PAGE_ERROR ? EXCEPTION_EXECUTE_HANDLER
89 : EXCEPTION_CONTINUE_SEARCH )
116 if( aSearchLang.Lower() == aSupportedLang.Lower() )
119 if( aSupportedLang.empty() )
122 if( aSearchLang.empty() )
125 wxArrayString supportedLangBits;
126 wxStringSplit( aSupportedLang.Lower(), supportedLangBits, wxS(
'-' ) );
128 wxArrayString searhcLangBits;
129 wxStringSplit( aSearchLang.Lower(), searhcLangBits, wxS(
'-' ) );
133 if( searhcLangBits.size() == 1 || supportedLangBits.size() == 1 )
135 return searhcLangBits[0] == supportedLangBits[0];
149 if( FcPatternGetString( aPat.
pat, aObj, aIdx, &str ) == FcResultMatch )
150 res = std::string(
reinterpret_cast<char*
>( str ) );
157 std::unordered_map<std::string, std::string>& aFamStringMap )
166 famLang =
getFcString( aPat, FC_FAMILYLANG, langIdx );
168 if( famLang.empty() && langIdx != 0 )
175 aFamStringMap.insert_or_assign( famLang, fam );
177 }
while( langIdx++ < std::numeric_limits<
184 std::unordered_map<std::string, std::string> famStrings;
187 if( famStrings.empty() )
190 for(
auto const& [key, val] : famStrings )
201 return famStrings.begin()->second;
206 int& aFaceIndex,
bool aBold,
bool aItalic,
207 const std::vector<wxString>* aEmbeddedFiles )
216 if( aFontName.Lower().Contains( wxS(
"bold" ) )
217 || aFontName.Lower().Contains( wxS(
"heavy" ) )
218 || aFontName.Lower().Contains( wxS(
"black" ) )
219 || aFontName.Lower().Contains( wxS(
"thick" ) )
220 || aFontName.Lower().Contains( wxS(
"dark" ) ) )
225 FcConfig*
config = FcConfigGetCurrent();
229 for(
const auto& file : *aEmbeddedFiles )
231 FcConfigAppFontAddFile(
config, (
const FcChar8*) file.c_str().AsChar() );
235 wxString qualifiedFontName = aFontName;
237 wxScopedCharBuffer
const fcBuffer = qualifiedFontName.ToUTF8();
239 FcPattern* pat = FcPatternCreate();
242 FcPatternAddString( pat, FC_STYLE, (
const FcChar8*)
"Bold" );
245 FcPatternAddString( pat, FC_STYLE, (
const FcChar8*)
"Italic" );
247 FcPatternAddString( pat, FC_FAMILY, (FcChar8*) fcBuffer.data() );
249 FcConfigSubstitute(
config, pat, FcMatchPattern );
250 FcDefaultSubstitute( pat );
252 FcResult r = FcResultNoMatch;
253 FcPattern* font = FcFontMatch(
config, pat, &r );
259 FcChar8* file =
nullptr;
261 if( FcPatternGetString( font, FC_FILE, 0, &file ) == FcResultMatch )
263 aFontFile = wxString::FromUTF8( (
char*) file );
267 FcChar8* family =
nullptr;
268 FcChar8* style =
nullptr;
272 std::unordered_map<std::string, std::string> famStrings;
277 if( FcPatternGetString( font, FC_FAMILY, 0, &family ) == FcResultMatch )
279 FcPatternGetInteger( font, FC_INDEX, 0, &aFaceIndex );
281 fontName = wxString::FromUTF8( (
char*) family );
283 if( FcPatternGetString( font, FC_STYLE, 0, &style ) == FcResultMatch )
285 styleStr = wxString::FromUTF8( (
char*) style );
287 if( !styleStr.IsEmpty() )
289 styleStr.Replace(
' ',
':' );
290 fontName +=
':' + styleStr;
294 bool has_bold =
false;
295 bool has_ital =
false;
296 wxString lower_style = styleStr.Lower();
298 if( lower_style.Contains( wxS(
"thin" ) )
299 || lower_style.Contains( wxS(
"light" ) )
300 || lower_style.Contains( wxS(
"regular" ) )
301 || lower_style.Contains( wxS(
"roman" ) )
302 || lower_style.Contains( wxS(
"book" ) ) )
306 else if( lower_style.Contains( wxS(
"medium" ) )
307 || lower_style.Contains( wxS(
"semibold" ) )
308 || lower_style.Contains( wxS(
"demibold" ) ) )
312 else if( lower_style.Contains( wxS(
"bold" ) )
313 || lower_style.Contains( wxS(
"heavy" ) )
314 || lower_style.Contains( wxS(
"black" ) )
315 || lower_style.Contains( wxS(
"thick" ) )
316 || lower_style.Contains( wxS(
"dark" ) ) )
321 if( lower_style.Contains( wxS(
"italic" ) )
322 || lower_style.Contains( wxS(
"oblique" ) )
323 || lower_style.Contains( wxS(
"slant" ) ) )
328 for(
auto const& [key, val] : famStrings )
331 searchFont = wxString::FromUTF8( (
char*) val.data() );
333 if( searchFont.Lower().StartsWith( aFontName.Lower() ) )
335 if( ( aBold && !has_bold ) && ( aItalic && !has_ital ) )
337 else if( aBold && !has_bold )
339 else if( aItalic && !has_ital )
341 else if( ( aBold != has_bold ) || ( aItalic != has_ital ) )
352 FcPatternDestroy( font );
359 qualifiedFontName ) );
363 fontName.Replace(
':',
' ' );
367 if( fontName.CmpNoCase( qualifiedFontName ) == 0 )
370 s_reporter->
Report( wxString::Format(
_(
"Font '%s' not found; substituting '%s'." ),
371 qualifiedFontName, fontName ) );
374 FcPatternDestroy( pat );
380 const std::vector<wxString>* aEmbeddedFiles,
bool aForce )
388 FcConfig*
config = FcConfigGetCurrent();
392 for(
const auto& file : *aEmbeddedFiles )
394 FcConfigAppFontAddFile(
config, (
const FcChar8*) file.c_str().AsChar() );
398 FcPattern* pat = FcPatternCreate();
399 FcObjectSet* os = FcObjectSetBuild( FC_FAMILY, FC_FAMILYLANG, FC_STYLE, FC_LANG, FC_FILE,
400 FC_OUTLINE,
nullptr );
401 FcFontSet* fs = FcFontList(
config, pat, os );
403 for(
int i = 0; fs && i < fs->nfont; ++i )
405 FcPattern* font = fs->fonts[i];
411 if( FcPatternGetString( font, FC_FILE, 0, &file ) == FcResultMatch
412 && FcPatternGetString( font, FC_STYLE, 0, &style ) == FcResultMatch
413 && FcPatternGetLangSet( font, FC_LANG, 0, &langSet ) == FcResultMatch
414 && FcPatternGetBool( font, FC_OUTLINE, 0, &outline ) == FcResultMatch )
420 std::string theFamily =
431 static std::map<wxString, bool> availableLanguages;
433 FcStrSet* langStrSet = FcLangSetGetLangs( langSet );
434 FcStrList* langStrList = FcStrListCreate( langStrSet );
435 FcChar8* langStr = FcStrListNext( langStrList );
436 bool langSupported =
false;
441 langSupported =
true;
443 else while( langStr )
445 wxString langWxStr(
reinterpret_cast<char *
>( langStr ) );
447 if( availableLanguages.find( langWxStr ) == availableLanguages.end() )
449 const wxLanguageInfo* langInfo = wxLocale::FindLanguageInfo( langWxStr );
450 bool available = langInfo && wxLocale::IsAvailable( langInfo->Language );
452 availableLanguages[ langWxStr ] = available;
455 if( availableLanguages[ langWxStr ] )
457 langSupported =
true;
463 wxS(
"Font '%s' language '%s' not supported by OS." ),
464 theFamily, langWxStr );
467 langStr = FcStrListNext( langStrList );
470 FcStrListDone( langStrList );
471 FcStrSetDestroy( langStrSet );
477 std::string theFile(
reinterpret_cast<char *
>( file ) );
478 std::string theStyle(
reinterpret_cast<char *
>( style ) );
479 FONTINFO fontInfo( std::move( theFile ), std::move( theStyle ), theFamily );
481 if( theFamily.length() > 0 && theFamily.front() ==
'.' )
484 std::map<std::string, FONTINFO>::iterator it =
m_fontInfoCache.find( theFamily );
489 it->second.Children().push_back( fontInfo );
494 FcFontSetDestroy( fs );
499 for(
const std::pair<const std::string, FONTINFO>& entry :
m_fontInfoCache )
500 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
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.