From a4349ae638c2e6e9f8d494d485eabf82cdbf7697 Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Sun, 9 Jan 2022 00:44:52 +0000 Subject: [PATCH] Cleanup and error reporting for fonts. --- common/font/font.cpp | 30 ++--------------------- common/font/fontconfig.cpp | 30 +++++++++++++++++++---- common/font/outline_font.cpp | 47 ++++++------------------------------ include/font/font.h | 3 --- include/font/fontconfig.h | 1 - include/font/outline_font.h | 2 -- 6 files changed, 35 insertions(+), 78 deletions(-) diff --git a/common/font/font.cpp b/common/font/font.cpp index d82b59c9d2..0e3b5bd380 100644 --- a/common/font/font.cpp +++ b/common/font/font.cpp @@ -57,9 +57,6 @@ const wxString& FONT::Name() const FONT* FONT::getDefaultFont() { - // FONT TODO: default font should be user-selectable in Eeschema but the KiCad stroke - // font in Pcbnew - if( !s_defaultFont ) s_defaultFont = STROKE_FONT::LoadFont( wxEmptyString ); @@ -90,17 +87,9 @@ FONT* FONT::GetFont( const wxString& aFontName, bool aBold, bool aItalic ) bool FONT::IsStroke( const wxString& aFontName ) { -#if 0 // FONT TODO - // Stroke fonts will be loaded under all four bold/italic combinations, so we only have - // to check for one. - std::tuple key = { aFontName, false, false }; - - FONT* font = s_fontMap[key]; - - return font && font->IsStroke(); -#else + // This would need a more complex implementation if we ever support more stroke fonts + // than the KiCad Font. return aFontName == _( "Default Font" ) || aFontName == wxT( "KiCad Font" ); -#endif } @@ -159,21 +148,6 @@ void FONT::getLinePositions( const UTF8& aText, const VECTOR2I& aPosition, } -void FONT::DrawText( KIGFX::GAL* aGal, const UTF8& aText, const VECTOR2I& aPosition, - const TEXT_ATTRIBUTES& aAttributes ) const -{ - // FONT TODO: do we need to set the attributes to the gal at all? - aGal->SetHorizontalJustify( aAttributes.m_Halign ); - aGal->SetVerticalJustify( aAttributes.m_Valign ); - aGal->SetGlyphSize( aAttributes.m_Size ); - aGal->SetFontItalic( aAttributes.m_Italic ); - aGal->SetFontBold( aAttributes.m_Bold ); - aGal->SetTextMirrored( aAttributes.m_Mirrored ); - - Draw( aGal, aText, aPosition, aAttributes ); -} - - /** * Draw a string. * diff --git a/common/font/fontconfig.cpp b/common/font/fontconfig.cpp index a92ddb23f6..58425e4e5b 100644 --- a/common/font/fontconfig.cpp +++ b/common/font/fontconfig.cpp @@ -22,12 +22,13 @@ #include #include #include -#include +#include using namespace fontconfig; static FONTCONFIG* g_config = nullptr; + inline static FcChar8* wxStringToFcChar8( const wxString& str ) { wxScopedCharBuffer const fcBuffer = str.ToUTF8(); @@ -37,10 +38,7 @@ inline static FcChar8* wxStringToFcChar8( const wxString& str ) FONTCONFIG::FONTCONFIG() { - m_config = FcInitLoadConfigAndFonts(); - - wxString configDirPath( Pgm().GetSettingsManager().GetUserSettingsPath() + wxT( "/fonts" ) ); - FcConfigAppFontAddDir( nullptr, wxStringToFcChar8( configDirPath ) ); + (void) FcInitLoadConfigAndFonts(); }; @@ -74,12 +72,34 @@ bool FONTCONFIG::FindFont( const wxString& aFontName, wxString& aFontFile ) if( FcPatternGetString( font, FC_FILE, 0, &file ) == FcResultMatch ) { aFontFile = wxString::FromUTF8( (char*) file ); + + FcChar8* family = nullptr; + FcChar8* style = nullptr; + + FcPatternGetString( font, FC_FAMILY, 0, &family ); + FcPatternGetString( font, FC_STYLE, 0, &style ); + + wxString fontName( family ); + wxString styleStr( style ); + + if( !styleStr.IsEmpty() ) + { + styleStr.Replace( " ", ":" ); + fontName += ":" + styleStr; + } + + if( aFontName.CmpNoCase( fontName ) != 0 ) + wxLogWarning( _( "Font '%s' not found; substituting '%s'." ), aFontName, fontName ); + ok = true; } FcPatternDestroy( font ); } + if( !ok ) + wxLogWarning( _( "Error loading font '%s'." ), aFontName ); + FcPatternDestroy( pat ); return ok; } diff --git a/common/font/outline_font.cpp b/common/font/outline_font.cpp index 8b51f99b48..d8c4183bce 100644 --- a/common/font/outline_font.cpp +++ b/common/font/outline_font.cpp @@ -62,11 +62,7 @@ OUTLINE_FONT::OUTLINE_FONT() : m_faceSize( 16 ) { if( !m_freeType ) - { - //FT_Error ft_error = FT_Init_FreeType( &m_freeType ); - // TODO: handle ft_error FT_Init_FreeType( &m_freeType ); - } } @@ -77,49 +73,25 @@ OUTLINE_FONT* OUTLINE_FONT::LoadFont( const wxString& aFontName, bool aBold, boo wxString fontFile; wxString qualifiedFontName = aFontName; + if( !aBold && !aItalic ) + qualifiedFontName << ":Regular"; + if( aBold ) - qualifiedFontName << ":bold"; + qualifiedFontName << ":Bold"; if( aItalic ) - qualifiedFontName << ":italic"; + qualifiedFontName << ":Italic"; if( Fontconfig().FindFont( qualifiedFontName, fontFile ) ) (void) font->loadFace( fontFile ); - else - (void) font->loadFontSimple( aFontName ); + + font->m_fontName = aFontName; // Keep asked-for name, even if we substituted. + font->m_fontFileName = fontFile; return font; } -bool OUTLINE_FONT::loadFontSimple( const wxString& aFontFileName ) -{ - wxFileName fontFile( aFontFileName ); - wxString fileName = fontFile.GetFullPath(); - // TODO: handle ft_error properly (now we just return false if load does not succeed) - FT_Error ft_error = loadFace( fileName ); - - if( ft_error == FT_Err_Unknown_File_Format ) - { - wxLogWarning( _( "The font file %s could be opened and read, " - "but it appears that its font format is unsupported." ), - fileName ); - } - else if( ft_error ) - { - wxLogWarning( _( "Unknown font error (%d) " ), ft_error ); - return false; - } - else - { - m_fontName = aFontFileName; - m_fontFileName = fileName; - } - - return true; -} - - FT_Error OUTLINE_FONT::loadFace( const wxString& aFontFileName ) { m_faceScaler = m_faceSize * 64; @@ -140,9 +112,6 @@ FT_Error OUTLINE_FONT::loadFace( const wxString& aFontFileName ) { FT_Select_Charmap( m_subscriptFace, FT_Encoding::FT_ENCODING_UNICODE ); FT_Set_Char_Size( m_subscriptFace, 0, m_subscriptFaceScaler, 0, 0 ); - - m_fontName = wxString( m_face->family_name ); - m_fontFileName = aFontFileName; } } diff --git a/include/font/font.h b/include/font/font.h index efcfe804de..548881fc31 100644 --- a/include/font/font.h +++ b/include/font/font.h @@ -132,9 +132,6 @@ public: Draw( aGal, aText, aPosition, VECTOR2I( 0, 0 ), aAttributes ); } - virtual void DrawText( KIGFX::GAL* aGal, const UTF8& aText, const VECTOR2I& aPosition, - const TEXT_ATTRIBUTES& aAttributes ) const; - /** * Compute the boundary limits of aText (the bounding box of all shapes). * diff --git a/include/font/fontconfig.h b/include/font/fontconfig.h index 49e35df68b..a8500b0e35 100644 --- a/include/font/fontconfig.h +++ b/include/font/fontconfig.h @@ -40,7 +40,6 @@ public: void ListFonts( std::vector& aFonts ); private: - FcConfig* m_config; std::map m_fonts; }; diff --git a/include/font/outline_font.h b/include/font/outline_font.h index c532683565..2e97f538ed 100644 --- a/include/font/outline_font.h +++ b/include/font/outline_font.h @@ -127,8 +127,6 @@ public: protected: FT_Error loadFace( const wxString& aFontFileName ); - bool loadFontSimple( const wxString& aFontFileName ); - BOX2I getBoundingBox( const std::vector>& aGlyphs ) const; private: