2022-01-01 01:21:03 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2021 Ola Rinta-Koski
|
|
|
|
* Copyright (C) 2021-2022 KiCad Developers, see AUTHORS.txt for contributors.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2022-02-06 19:31:02 +00:00
|
|
|
|
2022-01-01 01:21:03 +00:00
|
|
|
#include <font/fontconfig.h>
|
|
|
|
#include <pgm_base.h>
|
2022-01-09 00:44:52 +00:00
|
|
|
#include <wx/log.h>
|
2022-01-18 12:43:31 +00:00
|
|
|
#include <trace_helpers.h>
|
2023-02-22 01:26:30 +00:00
|
|
|
#include <string_utils.h>
|
|
|
|
#include <macros.h>
|
2022-01-01 01:21:03 +00:00
|
|
|
|
|
|
|
using namespace fontconfig;
|
|
|
|
|
|
|
|
static FONTCONFIG* g_config = nullptr;
|
|
|
|
|
2023-02-22 01:26:30 +00:00
|
|
|
/**
|
|
|
|
* A simple wrapper to avoid exporing fontconfig in the header
|
|
|
|
*/
|
|
|
|
struct fontconfig::FONTCONFIG_PAT
|
2022-01-01 01:21:03 +00:00
|
|
|
{
|
2023-02-22 01:26:30 +00:00
|
|
|
FcPattern* pat;
|
|
|
|
};
|
2022-01-01 01:21:03 +00:00
|
|
|
|
|
|
|
|
2022-03-07 14:01:37 +00:00
|
|
|
wxString FONTCONFIG::Version()
|
|
|
|
{
|
|
|
|
return wxString::Format( "%d.%d.%d", FC_MAJOR, FC_MINOR, FC_REVISION );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-01 01:21:03 +00:00
|
|
|
FONTCONFIG::FONTCONFIG()
|
|
|
|
{
|
2022-01-09 00:44:52 +00:00
|
|
|
(void) FcInitLoadConfigAndFonts();
|
2022-01-01 01:21:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2022-01-09 11:32:37 +00:00
|
|
|
FONTCONFIG* Fontconfig()
|
2022-01-01 01:21:03 +00:00
|
|
|
{
|
|
|
|
if( !g_config )
|
|
|
|
{
|
|
|
|
FcInit();
|
|
|
|
g_config = new FONTCONFIG();
|
|
|
|
}
|
|
|
|
|
2022-01-09 11:32:37 +00:00
|
|
|
return g_config;
|
2022-01-01 01:21:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-22 01:26:30 +00:00
|
|
|
bool FONTCONFIG::isLanguageMatch( const wxString& aSearchLang, const wxString& aSupportedLang )
|
|
|
|
{
|
|
|
|
if( aSearchLang.Lower() == aSupportedLang.Lower() )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if( aSupportedLang.empty() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( aSearchLang.empty() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
wxArrayString supportedLangBits;
|
|
|
|
wxStringSplit( aSupportedLang.Lower(), supportedLangBits, wxS( '-' ) );
|
|
|
|
|
|
|
|
wxArrayString searhcLangBits;
|
|
|
|
wxStringSplit( aSearchLang.Lower(), searhcLangBits, wxS( '-' ) );
|
|
|
|
|
|
|
|
// if either side of the comparison have only one section, then its a broad match but fine
|
|
|
|
// i.e. the haystack is declaring broad support or the search language is broad
|
|
|
|
if( searhcLangBits.size() == 1 || supportedLangBits.size() == 1 )
|
|
|
|
{
|
|
|
|
return searhcLangBits[0] == supportedLangBits[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
// the full two part comparison should have passed the initial shortcut
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string FONTCONFIG::getFcString( FONTCONFIG_PAT& aPat, const char* aObj, int aIdx )
|
|
|
|
{
|
|
|
|
FcChar8* str;
|
|
|
|
std::string res;
|
|
|
|
|
|
|
|
if( FcPatternGetString( aPat.pat, aObj, aIdx, &str ) == FcResultMatch )
|
|
|
|
res = std::string( reinterpret_cast<char*>( str ) );
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FONTCONFIG::getAllFamilyStrings( FONTCONFIG_PAT& aPat,
|
|
|
|
std::unordered_map<std::string, std::string>& aFamStringMap )
|
|
|
|
{
|
|
|
|
std::string famLang;
|
|
|
|
std::string fam;
|
|
|
|
|
|
|
|
int langIdx = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
famLang = getFcString( aPat, FC_FAMILYLANG, langIdx );
|
|
|
|
|
|
|
|
if( famLang.empty() && langIdx != 0 )
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fam = getFcString( aPat, FC_FAMILY, langIdx );
|
|
|
|
aFamStringMap.insert_or_assign( famLang, fam );
|
|
|
|
}
|
|
|
|
} while( langIdx++ < std::numeric_limits<
|
|
|
|
int8_t>::max() ); //arbitrary to avoid getting stuck for any reason
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string FONTCONFIG::getFamilyStringByLang( FONTCONFIG_PAT& aPat, const wxString& aDesiredLang )
|
|
|
|
{
|
|
|
|
std::unordered_map<std::string, std::string> famStrings;
|
|
|
|
getAllFamilyStrings( aPat, famStrings );
|
|
|
|
|
|
|
|
if( famStrings.empty() )
|
|
|
|
return "";
|
|
|
|
|
|
|
|
for( auto const& [key, val] : famStrings )
|
|
|
|
{
|
|
|
|
if( isLanguageMatch( aDesiredLang, FROM_UTF8( key.c_str() ) ) )
|
|
|
|
{
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// fall back to the first and maybe only available name
|
|
|
|
// most fonts by review don't even bother declaring more than one font family name
|
|
|
|
// and they don't even bother declare the language tag either, they just leave it blank
|
|
|
|
return famStrings.begin()->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-25 21:10:31 +00:00
|
|
|
FONTCONFIG::FF_RESULT FONTCONFIG::FindFont( const wxString &aFontName, wxString &aFontFile,
|
|
|
|
bool aBold, bool aItalic )
|
2022-01-01 01:21:03 +00:00
|
|
|
{
|
2023-01-26 00:44:35 +00:00
|
|
|
FF_RESULT retval = FF_RESULT::FF_ERROR;
|
2023-01-25 21:10:31 +00:00
|
|
|
wxString qualifiedFontName = aFontName;
|
|
|
|
|
2023-02-22 01:26:30 +00:00
|
|
|
wxScopedCharBuffer const fcBuffer = qualifiedFontName.ToUTF8();
|
|
|
|
|
|
|
|
FcPattern* pat = FcPatternCreate();
|
|
|
|
|
2023-01-25 21:10:31 +00:00
|
|
|
if( aBold )
|
2023-02-22 01:26:30 +00:00
|
|
|
FcPatternAddString( pat, FC_STYLE, (const FcChar8*) "Bold" );
|
2023-01-25 21:10:31 +00:00
|
|
|
|
|
|
|
if( aItalic )
|
2023-02-22 01:26:30 +00:00
|
|
|
FcPatternAddString( pat, FC_STYLE, (const FcChar8*) "Italic" );
|
|
|
|
|
|
|
|
FcPatternAddString( pat, FC_FAMILY, (FcChar8*) fcBuffer.data() );
|
2023-01-25 21:10:31 +00:00
|
|
|
|
2022-01-01 01:21:03 +00:00
|
|
|
FcConfigSubstitute( nullptr, pat, FcMatchPattern );
|
|
|
|
FcDefaultSubstitute( pat );
|
|
|
|
|
|
|
|
FcResult r = FcResultNoMatch;
|
|
|
|
FcPattern* font = FcFontMatch( nullptr, pat, &r );
|
|
|
|
|
2022-01-10 01:53:01 +00:00
|
|
|
wxString fontName;
|
2022-01-01 01:21:03 +00:00
|
|
|
|
|
|
|
if( font )
|
|
|
|
{
|
|
|
|
FcChar8* file = nullptr;
|
|
|
|
|
|
|
|
if( FcPatternGetString( font, FC_FILE, 0, &file ) == FcResultMatch )
|
|
|
|
{
|
|
|
|
aFontFile = wxString::FromUTF8( (char*) file );
|
2023-01-25 21:10:31 +00:00
|
|
|
wxString styleStr;
|
2022-01-09 00:44:52 +00:00
|
|
|
FcChar8* family = nullptr;
|
|
|
|
FcChar8* style = nullptr;
|
|
|
|
|
2023-01-26 00:44:35 +00:00
|
|
|
retval = FF_RESULT::FF_SUBSTITUTE;
|
2023-01-25 21:10:31 +00:00
|
|
|
|
2023-02-22 01:26:30 +00:00
|
|
|
std::unordered_map<std::string, std::string> famStrings;
|
|
|
|
FONTCONFIG_PAT patHolder{ font };
|
|
|
|
getAllFamilyStrings( patHolder, famStrings );
|
2022-01-10 01:53:01 +00:00
|
|
|
if( FcPatternGetString( font, FC_FAMILY, 0, &family ) == FcResultMatch )
|
|
|
|
{
|
|
|
|
fontName = wxString::FromUTF8( (char*) family );
|
2022-01-09 00:44:52 +00:00
|
|
|
|
2022-01-10 01:53:01 +00:00
|
|
|
if( FcPatternGetString( font, FC_STYLE, 0, &style ) == FcResultMatch )
|
|
|
|
{
|
2023-01-25 21:10:31 +00:00
|
|
|
styleStr = wxString::FromUTF8( (char*) style );
|
2022-01-09 00:44:52 +00:00
|
|
|
|
2022-01-10 01:53:01 +00:00
|
|
|
if( !styleStr.IsEmpty() )
|
|
|
|
{
|
2023-01-17 00:07:41 +00:00
|
|
|
styleStr.Replace( wxS( " " ), wxS( ":" ) );
|
2022-01-10 01:53:01 +00:00
|
|
|
fontName += ":" + styleStr;
|
|
|
|
}
|
|
|
|
}
|
2022-01-09 00:44:52 +00:00
|
|
|
|
2023-01-25 21:10:31 +00:00
|
|
|
bool has_bold = false;
|
|
|
|
bool has_ital = false;
|
|
|
|
wxString lower_style = styleStr.Lower();
|
2022-01-09 11:09:59 +00:00
|
|
|
|
2023-01-25 21:10:31 +00:00
|
|
|
if( lower_style.Contains( wxS( "bold" ) )
|
|
|
|
|| lower_style.Contains( wxS( "black" ) )
|
|
|
|
|| lower_style.Contains( wxS( "thick" ) )
|
|
|
|
|| lower_style.Contains( wxS( "dark" ) ) )
|
|
|
|
{
|
|
|
|
has_bold = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( lower_style.Contains( wxS( "italic" ) )
|
|
|
|
|| lower_style.Contains( wxS( "oblique" ) )
|
|
|
|
|| lower_style.Contains( wxS( "slant" ) ) )
|
|
|
|
{
|
|
|
|
has_ital = true;
|
|
|
|
}
|
2022-01-09 00:44:52 +00:00
|
|
|
|
2023-02-22 01:26:30 +00:00
|
|
|
for( auto const& [key, val] : famStrings )
|
2023-01-25 21:10:31 +00:00
|
|
|
{
|
2023-02-22 01:26:30 +00:00
|
|
|
wxString searchFont;
|
|
|
|
searchFont = wxString::FromUTF8( (char*) val.data() );
|
|
|
|
|
|
|
|
if( searchFont.Lower().StartsWith( aFontName.Lower() ) )
|
|
|
|
{
|
|
|
|
if( ( aBold && !has_bold ) && ( aItalic && !has_ital ) )
|
|
|
|
retval = FF_RESULT::FF_MISSING_BOLD_ITAL;
|
|
|
|
else if( aBold && !has_bold )
|
|
|
|
retval = FF_RESULT::FF_MISSING_BOLD;
|
|
|
|
else if( aItalic && !has_ital )
|
|
|
|
retval = FF_RESULT::FF_MISSING_ITAL;
|
|
|
|
else if( ( aBold != has_bold ) || ( aItalic != has_ital ) )
|
|
|
|
retval = FF_RESULT::FF_SUBSTITUTE;
|
|
|
|
else
|
|
|
|
retval = FF_RESULT::FF_OK;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2023-01-25 21:10:31 +00:00
|
|
|
}
|
2023-02-22 01:26:30 +00:00
|
|
|
|
|
|
|
|
2023-01-25 21:10:31 +00:00
|
|
|
}
|
2022-01-01 01:21:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FcPatternDestroy( font );
|
|
|
|
}
|
|
|
|
|
2023-01-26 00:44:35 +00:00
|
|
|
if( retval == FF_RESULT::FF_ERROR )
|
2023-01-25 21:10:31 +00:00
|
|
|
wxLogWarning( _( "Error loading font '%s'." ), qualifiedFontName );
|
2023-01-26 00:44:35 +00:00
|
|
|
else if( retval == FF_RESULT::FF_SUBSTITUTE )
|
2023-01-25 21:10:31 +00:00
|
|
|
wxLogWarning( _( "Font '%s' not found; substituting '%s'." ), qualifiedFontName, fontName );
|
2022-01-09 00:44:52 +00:00
|
|
|
|
2022-01-01 01:21:03 +00:00
|
|
|
FcPatternDestroy( pat );
|
2023-01-25 21:10:31 +00:00
|
|
|
return retval;
|
2022-01-01 01:21:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-22 01:26:30 +00:00
|
|
|
void FONTCONFIG::ListFonts( std::vector<std::string>& aFonts, const std::string& aDesiredLang )
|
2022-01-01 01:21:03 +00:00
|
|
|
{
|
2023-02-22 01:26:30 +00:00
|
|
|
// be sure to cache bust if the language changed
|
|
|
|
if( m_fontInfoCache.empty() || m_fontCacheLastLang != aDesiredLang )
|
2022-01-01 01:21:03 +00:00
|
|
|
{
|
|
|
|
FcPattern* pat = FcPatternCreate();
|
2023-02-22 01:26:30 +00:00
|
|
|
FcObjectSet* os = FcObjectSetBuild( FC_FAMILY, FC_FAMILYLANG, FC_STYLE, FC_LANG, FC_FILE,
|
|
|
|
FC_OUTLINE, nullptr );
|
2022-01-01 01:21:03 +00:00
|
|
|
FcFontSet* fs = FcFontList( nullptr, pat, os );
|
|
|
|
|
|
|
|
for( int i = 0; fs && i < fs->nfont; ++i )
|
|
|
|
{
|
|
|
|
FcPattern* font = fs->fonts[i];
|
|
|
|
FcChar8* file;
|
|
|
|
FcChar8* style;
|
2022-01-08 22:02:17 +00:00
|
|
|
FcLangSet* langSet;
|
|
|
|
FcBool outline;
|
2022-01-01 01:21:03 +00:00
|
|
|
|
|
|
|
if( FcPatternGetString( font, FC_FILE, 0, &file ) == FcResultMatch
|
2022-01-08 22:02:17 +00:00
|
|
|
&& FcPatternGetString( font, FC_STYLE, 0, &style ) == FcResultMatch
|
|
|
|
&& FcPatternGetLangSet( font, FC_LANG, 0, &langSet ) == FcResultMatch
|
|
|
|
&& FcPatternGetBool( font, FC_OUTLINE, 0, &outline ) == FcResultMatch )
|
2022-01-01 01:21:03 +00:00
|
|
|
{
|
2022-01-08 22:02:17 +00:00
|
|
|
if( !outline )
|
|
|
|
continue;
|
|
|
|
|
2023-02-22 01:26:30 +00:00
|
|
|
FONTCONFIG_PAT patHolder{ font };
|
|
|
|
std::string theFamily =
|
|
|
|
getFamilyStringByLang( patHolder, FROM_UTF8( aDesiredLang.c_str() ) );
|
2022-01-18 12:43:31 +00:00
|
|
|
|
2022-01-18 14:19:57 +00:00
|
|
|
#ifdef __WXMAC__
|
|
|
|
// On Mac (at least) some of the font names are in their own language. If
|
|
|
|
// the OS doesn't support this language then we get a bunch of garbage names
|
|
|
|
// in the font menu.
|
|
|
|
//
|
|
|
|
// GTK, on the other hand, doesn't appear to support wxLocale::IsAvailable(),
|
|
|
|
// so we can't run these checks.
|
|
|
|
|
2022-02-07 16:32:19 +00:00
|
|
|
FcStrSet* langStrSet = FcLangSetGetLangs( langSet );
|
|
|
|
FcStrList* langStrList = FcStrListCreate( langStrSet );
|
|
|
|
FcChar8* langStr = FcStrListNext( langStrList );
|
2022-01-18 14:19:57 +00:00
|
|
|
bool langSupported = false;
|
|
|
|
|
2022-01-09 11:32:37 +00:00
|
|
|
if( !langStr )
|
|
|
|
{
|
|
|
|
// Symbol fonts (Wingdings, etc.) have no language
|
|
|
|
langSupported = true;
|
|
|
|
}
|
|
|
|
else while( langStr )
|
2022-01-08 22:02:17 +00:00
|
|
|
{
|
|
|
|
wxString langWxStr( reinterpret_cast<char *>( langStr ) );
|
|
|
|
const wxLanguageInfo* langInfo = wxLocale::FindLanguageInfo( langWxStr );
|
|
|
|
|
|
|
|
if( langInfo && wxLocale::IsAvailable( langInfo->Language ) )
|
|
|
|
{
|
|
|
|
langSupported = true;
|
|
|
|
break;
|
|
|
|
}
|
2022-01-18 12:43:31 +00:00
|
|
|
else
|
|
|
|
{
|
2023-01-22 14:41:33 +00:00
|
|
|
wxLogTrace( traceFonts, wxS( "Font '%s' language '%s' not supported by OS." ),
|
2022-01-18 12:43:31 +00:00
|
|
|
theFamily, langWxStr );
|
|
|
|
}
|
2022-01-09 11:32:37 +00:00
|
|
|
|
|
|
|
langStr = FcStrListNext( langStrList );
|
2022-01-08 22:02:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FcStrListDone( langStrList );
|
|
|
|
FcStrSetDestroy( langStrSet );
|
|
|
|
|
|
|
|
if( !langSupported )
|
|
|
|
continue;
|
2022-01-18 14:19:57 +00:00
|
|
|
#endif
|
2022-01-08 22:02:17 +00:00
|
|
|
|
2022-01-04 18:59:10 +00:00
|
|
|
std::string theFile( reinterpret_cast<char *>( file ) );
|
|
|
|
std::string theStyle( reinterpret_cast<char *>( style ) );
|
2022-01-01 01:21:03 +00:00
|
|
|
FONTINFO fontInfo( theFile, theStyle, theFamily );
|
|
|
|
|
|
|
|
if( theFamily.length() > 0 && theFamily.front() == '.' )
|
|
|
|
continue;
|
|
|
|
|
2023-02-22 01:26:30 +00:00
|
|
|
std::map<std::string, FONTINFO>::iterator it = m_fontInfoCache.find( theFamily );
|
2022-01-01 01:21:03 +00:00
|
|
|
|
2023-02-22 01:26:30 +00:00
|
|
|
if( it == m_fontInfoCache.end() )
|
|
|
|
m_fontInfoCache.emplace( theFamily, fontInfo );
|
2022-01-01 01:21:03 +00:00
|
|
|
else
|
|
|
|
it->second.Children().push_back( fontInfo );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( fs )
|
|
|
|
FcFontSetDestroy( fs );
|
2023-02-22 01:26:30 +00:00
|
|
|
|
|
|
|
m_fontCacheLastLang = aDesiredLang;
|
2022-01-01 01:21:03 +00:00
|
|
|
}
|
|
|
|
|
2023-02-22 01:26:30 +00:00
|
|
|
for( const std::pair<const std::string, FONTINFO>& entry : m_fontInfoCache )
|
2022-01-01 01:21:03 +00:00
|
|
|
aFonts.push_back( entry.second.Family() );
|
|
|
|
}
|