KiCad PCB EDA Suite
Loading...
Searching...
No Matches
fontconfig.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2021 Ola Rinta-Koski
5 * Copyright (C) 2023 CERN (www.cern.ch)
6 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software: you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation, either version 3 of the License, or (at your
11 * option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include <mutex>
23#include <font/fontconfig.h>
24#include <wx/log.h>
25#include <trace_helpers.h>
26#include <string_utils.h>
27#include <macros.h>
28#include <cstdint>
29#include <reporter.h>
30#include <embedded_files.h>
31
32#ifdef __WIN32__
33#define WIN32_LEAN_AND_MEAN
34#include <windows.h>
35#endif
36
37using namespace fontconfig;
38
39static FONTCONFIG* g_config = nullptr;
40static bool g_fcInitSuccess = false;
41
43static std::mutex g_fontConfigMutex;
44
49{
50 FcPattern* pat;
51};
52
53
55{
56 return wxString::Format( "%d.%d.%d", FC_MAJOR, FC_MINOR, FC_REVISION );
57}
58
59
61{
62};
63
64
66{
67 std::lock_guard lock( g_fontConfigMutex );
68 s_reporter = aReporter;
69}
70
71
80static void bootstrapFc()
81{
82#if defined( _MSC_VER )
83 __try
84 {
85#endif
86 FcInit();
87 g_fcInitSuccess = true;
88#if defined( _MSC_VER )
89 }
90 __except( GetExceptionCode() == STATUS_IN_PAGE_ERROR ? EXCEPTION_EXECUTE_HANDLER
91 : EXCEPTION_CONTINUE_SEARCH )
92 {
93 g_fcInitSuccess = false;
94
95 // We have documented cases that fontconfig while trying to cache fonts
96 // ends up using freetype to try and get font info
97 // freetype itself reads fonts through memory mapping instead of normal file APIs
98 // there are crashes reading fonts sometimes as a result that return STATUS_IN_PAGE_ERROR
99 }
100#endif
101}
102
103
105{
106 if( !g_config )
107 {
108 bootstrapFc();
109 g_config = new FONTCONFIG();
110 }
111
112 return g_config;
113}
114
115
116bool FONTCONFIG::isLanguageMatch( const wxString& aSearchLang, const wxString& aSupportedLang )
117{
118 if( aSearchLang.Lower() == aSupportedLang.Lower() )
119 return true;
120
121 if( aSupportedLang.empty() )
122 return false;
123
124 if( aSearchLang.empty() )
125 return false;
126
127 wxArrayString supportedLangBits;
128 wxStringSplit( aSupportedLang.Lower(), supportedLangBits, wxS( '-' ) );
129
130 wxArrayString searhcLangBits;
131 wxStringSplit( aSearchLang.Lower(), searhcLangBits, wxS( '-' ) );
132
133 // if either side of the comparison have only one section, then its a broad match but fine
134 // i.e. the haystack is declaring broad support or the search language is broad
135 if( searhcLangBits.size() == 1 || supportedLangBits.size() == 1 )
136 {
137 return searhcLangBits[0] == supportedLangBits[0];
138 }
139
140 // the full two part comparison should have passed the initial shortcut
141
142 return false;
143}
144
145
146std::string FONTCONFIG::getFcString( FONTCONFIG_PAT& aPat, const char* aObj, int aIdx )
147{
148 FcChar8* str;
149 std::string res;
150
151 if( FcPatternGetString( aPat.pat, aObj, aIdx, &str ) == FcResultMatch )
152 res = std::string( reinterpret_cast<char*>( str ) );
153
154 return res;
155}
156
157
159 std::unordered_map<std::string, std::string>& aFamStringMap )
160{
161 std::string famLang;
162 std::string fam;
163
164 int langIdx = 0;
165
166 do
167 {
168 famLang = getFcString( aPat, FC_FAMILYLANG, langIdx );
169
170 if( famLang.empty() && langIdx != 0 )
171 {
172 break;
173 }
174 else
175 {
176 fam = getFcString( aPat, FC_FAMILY, langIdx );
177 aFamStringMap.insert_or_assign( famLang, fam );
178 }
179 } while( langIdx++ < std::numeric_limits<
180 int8_t>::max() ); //arbitrary to avoid getting stuck for any reason
181}
182
183
184std::string FONTCONFIG::getFamilyStringByLang( FONTCONFIG_PAT& aPat, const wxString& aDesiredLang )
185{
186 std::unordered_map<std::string, std::string> famStrings;
187 getAllFamilyStrings( aPat, famStrings );
188
189 if( famStrings.empty() )
190 return "";
191
192 for( auto const& [key, val] : famStrings )
193 {
194 if( isLanguageMatch( aDesiredLang, From_UTF8( key.c_str() ) ) )
195 {
196 return val;
197 }
198 }
199
200 // fall back to the first and maybe only available name
201 // most fonts by review don't even bother declaring more than one font family name
202 // and they don't even bother declare the language tag either, they just leave it blank
203 return famStrings.begin()->second;
204}
205
206
207FONTCONFIG::FF_RESULT FONTCONFIG::FindFont( const wxString& aFontName, wxString& aFontFile,
208 int& aFaceIndex, bool aBold, bool aItalic,
209 const std::vector<wxString>* aEmbeddedFiles )
210{
212
213 if( !g_fcInitSuccess )
214 return retval;
215
216 // If the original font name contains any of these, then it is bold, regardless
217 // of whether we are looking for bold or not
218 if( aFontName.Lower().Contains( wxS( "bold" ) ) // also catches ultrabold
219 || aFontName.Lower().Contains( wxS( "heavy" ) )
220 || aFontName.Lower().Contains( wxS( "black" ) ) // also catches extrablack
221 || aFontName.Lower().Contains( wxS( "thick" ) )
222 || aFontName.Lower().Contains( wxS( "dark" ) ) )
223 {
224 aBold = true;
225 }
226
227 FcConfig* config = FcConfigGetCurrent();
228
229 if( aEmbeddedFiles )
230 {
231 for( const auto& file : *aEmbeddedFiles )
232 {
233 FcConfigAppFontAddFile( config, (const FcChar8*) file.c_str().AsChar() );
234 }
235 }
236
237 wxString qualifiedFontName = aFontName;
238
239 wxScopedCharBuffer const fcBuffer = qualifiedFontName.ToUTF8();
240
241 FcPattern* pat = FcPatternCreate();
242
243 if( aBold )
244 FcPatternAddString( pat, FC_STYLE, (const FcChar8*) "Bold" );
245
246 if( aItalic )
247 FcPatternAddString( pat, FC_STYLE, (const FcChar8*) "Italic" );
248
249 FcPatternAddString( pat, FC_FAMILY, (FcChar8*) fcBuffer.data() );
250
251 FcConfigSubstitute( config, pat, FcMatchPattern );
252 FcDefaultSubstitute( pat );
253
254 FcResult r = FcResultNoMatch;
255 FcPattern* font = FcFontMatch( config, pat, &r );
256
257 wxString fontName;
258
259 if( font )
260 {
261 FcChar8* file = nullptr;
262
263 if( FcPatternGetString( font, FC_FILE, 0, &file ) == FcResultMatch )
264 {
265 aFontFile = wxString::FromUTF8( (char*) file );
266 aFaceIndex = 0;
267
268 wxString styleStr;
269 FcChar8* family = nullptr;
270 FcChar8* style = nullptr;
271
273
274 std::unordered_map<std::string, std::string> famStrings;
275 FONTCONFIG_PAT patHolder{ font };
276
277 getAllFamilyStrings( patHolder, famStrings );
278
279 if( FcPatternGetString( font, FC_FAMILY, 0, &family ) == FcResultMatch )
280 {
281 FcPatternGetInteger( font, FC_INDEX, 0, &aFaceIndex );
282
283 fontName = wxString::FromUTF8( (char*) family );
284
285 if( FcPatternGetString( font, FC_STYLE, 0, &style ) == FcResultMatch )
286 {
287 styleStr = wxString::FromUTF8( (char*) style );
288
289 if( !styleStr.IsEmpty() )
290 {
291 styleStr.Replace( ' ', ':' );
292 fontName += ':' + styleStr;
293 }
294 }
295
296 bool has_bold = false;
297 bool has_ital = false;
298 wxString lower_style = styleStr.Lower();
299
300 if( lower_style.Contains( wxS( "thin" ) )
301 || lower_style.Contains( wxS( "light" ) ) // catches ultra & extra light
302 || lower_style.Contains( wxS( "regular" ) )
303 || lower_style.Contains( wxS( "roman" ) )
304 || lower_style.Contains( wxS( "book" ) ) )
305 {
306 has_bold = false;
307 }
308 else if( lower_style.Contains( wxS( "medium" ) )
309 || lower_style.Contains( wxS( "semibold" ) )
310 || lower_style.Contains( wxS( "demibold" ) ) )
311 {
312 has_bold = aBold;
313 }
314 else if( lower_style.Contains( wxS( "bold" ) ) // also catches ultrabold
315 || lower_style.Contains( wxS( "heavy" ) )
316 || lower_style.Contains( wxS( "black" ) ) // also catches extrablack
317 || lower_style.Contains( wxS( "thick" ) )
318 || lower_style.Contains( wxS( "dark" ) ) )
319 {
320 has_bold = true;
321 }
322
323 if( lower_style.Contains( wxS( "italic" ) )
324 || lower_style.Contains( wxS( "oblique" ) )
325 || lower_style.Contains( wxS( "slant" ) ) )
326 {
327 has_ital = true;
328 }
329
330 for( auto const& [key, val] : famStrings )
331 {
332 wxString searchFont;
333 searchFont = wxString::FromUTF8( (char*) val.data() );
334
335 if( searchFont.Lower().StartsWith( aFontName.Lower() ) )
336 {
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 ) )
345 else
346 retval = FF_RESULT::FF_OK;
347
348 break;
349 }
350 }
351 }
352 }
353
354 FcPatternDestroy( font );
355 }
356
357 if( retval == FF_RESULT::FF_ERROR )
358 {
359 if( s_reporter )
360 s_reporter->Report( wxString::Format( _( "Error loading font '%s'." ),
361 qualifiedFontName ) );
362 }
363 else if( retval == FF_RESULT::FF_SUBSTITUTE )
364 {
365 fontName.Replace( ':', ' ' );
366
367 // If we missed a case but the matching found the original font name, then we are
368 // not substituting
369 if( fontName.CmpNoCase( qualifiedFontName ) == 0 )
370 retval = FF_RESULT::FF_OK;
371 else if( s_reporter )
372 s_reporter->Report( wxString::Format( _( "Font '%s' not found; substituting '%s'." ),
373 qualifiedFontName, fontName ) );
374 }
375
376 FcPatternDestroy( pat );
377 return retval;
378}
379
380
381void FONTCONFIG::ListFonts( std::vector<std::string>& aFonts, const std::string& aDesiredLang,
382 const std::vector<wxString>* aEmbeddedFiles, bool aForce )
383{
384 if( !g_fcInitSuccess )
385 return;
386
387 // be sure to cache bust if the language changed
388 if( m_fontInfoCache.empty() || m_fontCacheLastLang != aDesiredLang || aForce )
389 {
390 FcConfig* config = FcConfigGetCurrent();
391
392 if( aEmbeddedFiles )
393 {
394 for( const auto& file : *aEmbeddedFiles )
395 {
396 FcConfigAppFontAddFile( config, (const FcChar8*) file.c_str().AsChar() );
397 }
398 }
399
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 );
404
405 for( int i = 0; fs && i < fs->nfont; ++i )
406 {
407 FcPattern* font = fs->fonts[i];
408 FcChar8* file;
409 FcChar8* style;
410 FcLangSet* langSet;
411 FcBool outline;
412
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 )
417 {
418 if( !outline )
419 continue;
420
421 FONTCONFIG_PAT patHolder{ font };
422 std::string theFamily =
423 getFamilyStringByLang( patHolder, From_UTF8( aDesiredLang.c_str() ) );
424
425#ifdef __WXMAC__
426 // On Mac (at least) some of the font names are in their own language. If
427 // the OS doesn't support this language then we get a bunch of garbage names
428 // in the font menu.
429 //
430 // GTK, on the other hand, doesn't appear to support wxLocale::IsAvailable(),
431 // so we can't run these checks.
432
433 static std::map<wxString, bool> availableLanguages;
434
435 FcStrSet* langStrSet = FcLangSetGetLangs( langSet );
436 FcStrList* langStrList = FcStrListCreate( langStrSet );
437 FcChar8* langStr = FcStrListNext( langStrList );
438 bool langSupported = false;
439
440 if( !langStr )
441 {
442 // Symbol fonts (Wingdings, etc.) have no language
443 langSupported = true;
444 }
445 else while( langStr )
446 {
447 wxString langWxStr( reinterpret_cast<char *>( langStr ) );
448
449 if( availableLanguages.find( langWxStr ) == availableLanguages.end() )
450 {
451 const wxLanguageInfo* langInfo = wxLocale::FindLanguageInfo( langWxStr );
452 bool available = langInfo && wxLocale::IsAvailable( langInfo->Language );
453
454 availableLanguages[ langWxStr ] = available;
455 }
456
457 if( availableLanguages[ langWxStr ] )
458 {
459 langSupported = true;
460 break;
461 }
462 else
463 {
464 wxLogTrace( traceFonts,
465 wxS( "Font '%s' language '%s' not supported by OS." ),
466 theFamily, langWxStr );
467 }
468
469 langStr = FcStrListNext( langStrList );
470 }
471
472 FcStrListDone( langStrList );
473 FcStrSetDestroy( langStrSet );
474
475 if( !langSupported )
476 continue;
477#endif
478
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 );
482
483 if( theFamily.length() > 0 && theFamily.front() == '.' )
484 continue;
485
486 std::map<std::string, FONTINFO>::iterator it = m_fontInfoCache.find( theFamily );
487
488 if( it == m_fontInfoCache.end() )
489 m_fontInfoCache.emplace( theFamily, fontInfo );
490 else
491 it->second.Children().push_back( fontInfo );
492 }
493 }
494
495 if( fs )
496 FcFontSetDestroy( fs );
497
498 m_fontCacheLastLang = aDesiredLang;
499 }
500
501 for( const std::pair<const std::string, FONTINFO>& entry : m_fontInfoCache )
502 aFonts.push_back( entry.second.Family() );
503}
504
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:72
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)=0
Report a string with a given severity.
static REPORTER * s_reporter
Definition: fontconfig.h:87
void getAllFamilyStrings(FONTCONFIG_PAT &aPat, std::unordered_map< std::string, std::string > &aFamStringMap)
Get a list of all family name strings mapped to lang.
Definition: fontconfig.cpp:158
static wxString Version()
Definition: fontconfig.cpp:54
static void SetReporter(REPORTER *aReporter)
Set the reporter to use for reporting font substitution warnings.
Definition: fontconfig.cpp:65
std::map< std::string, FONTINFO > m_fontInfoCache
Definition: fontconfig.h:85
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...
Definition: fontconfig.cpp:207
std::string getFcString(FONTCONFIG_PAT &aPat, const char *aObj, int aIdx)
Wrapper of FcPatternGetString to return a std::string.
Definition: fontconfig.cpp:146
std::string getFamilyStringByLang(FONTCONFIG_PAT &APat, const wxString &aDesiredLang)
Get a family name based on desired language.
Definition: fontconfig.cpp:184
bool isLanguageMatch(const wxString &aSearchLang, const wxString &aSupportedLang)
Match two rfc 3306 language entries, used for when searching for matching family names.
Definition: fontconfig.cpp:116
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.
Definition: fontconfig.cpp:381
wxString m_fontCacheLastLang
Definition: fontconfig.h:86
#define _(s)
static FONTCONFIG * g_config
Definition: fontconfig.cpp:39
FONTCONFIG * Fontconfig()
Definition: fontconfig.cpp:104
static void bootstrapFc()
This is simply a wrapper to call FcInit() with SEH for Windows.
Definition: fontconfig.cpp:80
static bool g_fcInitSuccess
Definition: fontconfig.cpp:40
static std::mutex g_fontConfigMutex
Definition: fontconfig.cpp:43
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.
Definition: fontconfig.cpp:49
VECTOR3I res
wxLogTrace helper definitions.