From 283f5a4d5678f7f936144b8dc365904e53704b60 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 13 Nov 2017 22:09:15 +0100 Subject: [PATCH] Remove useless wxFont::ActivatePrivateFonts() Just "activate" the font immediately when adding it using AddPrivateFont(), nothing seems to be gained from having two functions and it just makes things more complicated both when implementing and when using the API. --- docs/doxygen/overviews/font.h | 11 +++--- include/wx/font.h | 9 ++--- interface/wx/font.h | 25 ++++++-------- samples/font/font.cpp | 4 --- src/common/fontcmn.cpp | 5 --- src/gtk/font.cpp | 63 +++++++++++++++++------------------ src/msw/font.cpp | 22 ++---------- src/osx/fontutil.cpp | 6 ---- 8 files changed, 53 insertions(+), 92 deletions(-) diff --git a/docs/doxygen/overviews/font.h b/docs/doxygen/overviews/font.h index 49635f37ec..76103fdfa4 100644 --- a/docs/doxygen/overviews/font.h +++ b/docs/doxygen/overviews/font.h @@ -82,12 +82,9 @@ system. On Macintosh/OSX this can be arranged by placing the desired fonts within the Application Bundle in Contents/Resources/Fonts and using the ATSApplicationFontsPath key to point there. The full details of the procedure there can be found as OSX developer resources. For the GTK+ and -Windows ports it is possible to add TrueType fonts at run-time using -a sequence of calls to wxFont::AddPrivateFont() to give the names of files -containing font data, followed by a call to wxFont::ActivatePrivateFonts() -to complete the process of making the fonts available. These functions -return false if they fail. They should be called just once before any -graphics contexts have been created or other activity liable to use fonts -has happened. +Windows ports it is possible to add TrueType fonts from arbitrary locations at +run-time using wxFont::AddPrivateFont(). Notice that under MSW this function +should be called before creating the first wxGraphicsContext object if you want +the private font to be usable from it. */ diff --git a/include/wx/font.h b/include/wx/font.h index f3731179f8..8236558201 100644 --- a/include/wx/font.h +++ b/include/wx/font.h @@ -328,11 +328,12 @@ public: // from the string representation of wxNativeFontInfo static wxFont *New(const wxString& strNativeFontDesc); - // These functions can be used to load private fonts if supported by this - // platform (wxHAS_PRIVATE_FONTS is defined): first add one or more files - // and then activate all of them at once. + // Load the font from the given file and return true on success or false on + // error (an error message will be logged in this case, unless there is no + // support for private fonts at all in the current port, in which case + // wxHAS_PRIVATE_FONTS will not be defined allowing to check for this at + // compile-time). static bool AddPrivateFont(const wxString& filename); - static bool ActivatePrivateFonts(); // comparison bool operator==(const wxFont& font) const; diff --git a/interface/wx/font.h b/interface/wx/font.h index 7bc5c14cec..9f7c37708f 100644 --- a/interface/wx/font.h +++ b/interface/wx/font.h @@ -678,30 +678,25 @@ public: @c ATSApplicationFontsPath to @c Fonts value in your @c Info.plist file. See also wxStandardPaths::GetResourcesDir(). - Notice that this method must be called before any graphics contexts - have been created. + Under MSW this method must be called before any wxGraphicsContext + objects have been created, otherwise the private font won't be usable + from them. + + Under Unix this method requires Pango 1.38 or later and will return @a + false and log an error message explaining the problem if this + requirement is not satisfied either at compile- or run-time. Currently this method is implemented for all major platforms but you may also test for @c wxHAS_PRIVATE_FONTS preprocessor symbol: if it is - not defined, this function and ActivatePrivatefonts() are not - implemented at all and always simply return false. + not defined, this function is not implemented at all and simply always + returns false. - @return @true if the font was added and ActivatePrivatefonts() should - be called next or @false if adding it failed. + @return @true if the font was added and can now be used. @since 3.1.1 */ static bool AddPrivateFont(const wxString& filename); - /** - Make all fonts registered by AddPrivateFont() actually available. - - @return @true if the private fonts can now be used or @false on error. - - @since 3.1.1 - */ - static bool ActivatePrivatefonts(); - /** Gets the point size. diff --git a/samples/font/font.cpp b/samples/font/font.cpp index baa7f0b054..6b5110064f 100644 --- a/samples/font/font.cpp +++ b/samples/font/font.cpp @@ -426,10 +426,6 @@ MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) { wxLogWarning("Failed to add private font from \"%s\"", privfont); } - else if ( !wxFont::ActivatePrivateFonts() ) - { - wxLogWarning("Failed to activate the private fonts"); - } else { menuSelect->AppendSeparator(); diff --git a/src/common/fontcmn.cpp b/src/common/fontcmn.cpp index f17d045167..dcb899a9ae 100644 --- a/src/common/fontcmn.cpp +++ b/src/common/fontcmn.cpp @@ -1148,9 +1148,4 @@ bool wxFontBase::AddPrivateFont(const wxString& WXUNUSED(filename)) return false; } -bool wxFontBase::ActivatePrivateFonts() -{ - return false; -} - #endif // !wxHAS_PRIVATE_FONTS diff --git a/src/gtk/font.cpp b/src/gtk/font.cpp index 2887537728..f45680a1b3 100644 --- a/src/gtk/font.cpp +++ b/src/gtk/font.cpp @@ -601,42 +601,41 @@ wxIMPLEMENT_DYNAMIC_CLASS(wxFcConfigDestroyModule, wxModule); bool wxFontBase::AddPrivateFont(const wxString& filename) { - if ( !gs_fcConfig ) - { - gs_fcConfig = FcInitLoadConfigAndFonts(); - if ( !gs_fcConfig ) - { - wxLogError(_("Failed to create font configuration object.")); - return false; - } - } - - if ( !FcConfigAppFontAddFile(gs_fcConfig, - reinterpret_cast( - static_cast(filename.utf8_str()) - )) ) - { - wxLogError(_("Failed to add custom font \"%s\"."), filename); - return false; - } - - return true; -} - -bool wxFontBase::ActivatePrivateFonts() -{ - wxGtkObject context(wxGetPangoContext()); - PangoFontMap* const fmap = pango_context_get_font_map(context); - if ( !fmap || !PANGO_IS_FC_FONT_MAP(fmap) ) - { - wxLogError(_("Failed to register font configuration using private fonts.")); - return false; - } - wxString why; + + // All this code only works if we have pango_context_get_font_map() which + // is new in 1.38, so don't bother compiling -- or running -- it if this is + // not the case. #if PANGO_VERSION_CHECK(1,38,0) if ( wx_pango_version_check(1,38,0) == NULL ) { + if ( !gs_fcConfig ) + { + gs_fcConfig = FcInitLoadConfigAndFonts(); + if ( !gs_fcConfig ) + { + wxLogError(_("Failed to create font configuration object.")); + return false; + } + } + + if ( !FcConfigAppFontAddFile(gs_fcConfig, + reinterpret_cast( + static_cast(filename.utf8_str()) + )) ) + { + wxLogError(_("Failed to add custom font \"%s\"."), filename); + return false; + } + + wxGtkObject context(wxGetPangoContext()); + PangoFontMap* const fmap = pango_context_get_font_map(context); + if ( !fmap || !PANGO_IS_FC_FONT_MAP(fmap) ) + { + wxLogError(_("Failed to register font configuration using private fonts.")); + return false; + } + PangoFcFontMap* const fcfmap = PANGO_FC_FONT_MAP(fmap); pango_fc_font_map_set_config(fcfmap, gs_fcConfig); diff --git a/src/msw/font.cpp b/src/msw/font.cpp index ea6920cfff..4ef7642035 100644 --- a/src/msw/font.cpp +++ b/src/msw/font.cpp @@ -35,7 +35,6 @@ #endif // WX_PRECOMP #include "wx/encinfo.h" -#include "wx/filename.h" #include "wx/fontutil.h" #include "wx/fontmap.h" @@ -1122,28 +1121,13 @@ wxIMPLEMENT_DYNAMIC_CLASS(wxPrivateFontsListModule, wxModule); bool wxFontBase::AddPrivateFont(const wxString& filename) { - if ( !wxFileName::FileExists(filename) ) + if ( !AddFontResourceEx(filename.t_str(), FR_PRIVATE, 0) ) { - wxLogError(_("Font file \"%s\" doesn't exist."), filename); + wxLogSysError(_("Font file \"%s\" couldn't be loaded"), filename); return false; } + // Remember it for use in wxGDIPlusRenderer::Load(). gs_privateFontFileNames.Add(filename); return true; } - -bool wxFontBase::ActivatePrivateFonts() -{ - const int n = gs_privateFontFileNames.size(); - for ( int i = 0 ; i < n; i++ ) - { - const wxString& fname = gs_privateFontFileNames[i]; - if ( !AddFontResourceEx(fname.t_str(), FR_PRIVATE, 0) ) - { - wxLogSysError(_("Font file \"%s\" couldn't be loaded"), - fname); - } - } - - return true; -} diff --git a/src/osx/fontutil.cpp b/src/osx/fontutil.cpp index dd67c6322f..5cf015bcda 100644 --- a/src/osx/fontutil.cpp +++ b/src/osx/fontutil.cpp @@ -95,12 +95,6 @@ bool wxFontBase::AddPrivateFont(const wxString& filename) return true; } -bool wxFontBase::ActivatePrivateFonts() -{ - // Nothing to do here. - return true; -} - // ---------------------------------------------------------------------------- // helper functions // ----------------------------------------------------------------------------