Add support for loading fonts from files.

wxFont::AddPrivateFont() can now be used to load a font from a file for the
applications private use. Update the font sample to show this.

Closes #13568.
This commit is contained in:
Arthur Norman
2017-11-07 17:41:11 +01:00
committed by Vadim Zeitlin
parent 760bd1bf4e
commit 547e40b114
19 changed files with 366 additions and 8 deletions

View File

@@ -29,6 +29,7 @@
#include "wx/fontmap.h"
#include "wx/encconv.h"
#include "wx/splitter.h"
#include "wx/stdpaths.h"
#include "wx/textfile.h"
#include "wx/settings.h"
@@ -130,6 +131,7 @@ public:
void OnSetFamily(wxCommandEvent& event);
void OnSetFaceName(wxCommandEvent& event);
void OnSetEncoding(wxCommandEvent& event);
void OnPrivateFont(wxCommandEvent& event);
protected:
bool DoEnumerateFamilies(bool fixedWidthOnly,
@@ -212,6 +214,8 @@ enum
Font_SetFamily,
Font_SetFaceName,
Font_SetEncoding,
Font_Private,
Font_Max
};
@@ -265,6 +269,7 @@ wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(Font_EnumFamilies, MyFrame::OnEnumerateFamilies)
EVT_MENU(Font_EnumFixedFamilies, MyFrame::OnEnumerateFixedFamilies)
EVT_MENU(Font_EnumEncodings, MyFrame::OnEnumerateEncodings)
EVT_MENU(Font_Private, MyFrame::OnPrivateFont)
wxEND_EVENT_TABLE()
// Create a new application object: this macro will allow wxWidgets to create
@@ -287,6 +292,7 @@ bool MyApp::OnInit()
{
if ( !wxApp::OnInit() )
return false;
wxString privfont = argv[0].BeforeLast('/');
// Create the main application window
MyFrame *frame = new MyFrame(wxT("Font wxWidgets demo"),
@@ -381,7 +387,6 @@ MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
wxT("Default font for user interface objects such as menus and dialog boxes. "));
menuSelect->Append(Font_SystemSettings, wxT("System fonts"), menuSettingFonts);
menuSelect->AppendSeparator();
menuSelect->Append(Font_EnumFamilies, wxT("Enumerate font &families\tCtrl-F"));
menuSelect->Append(Font_EnumFixedFamilies,
@@ -392,6 +397,42 @@ MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
wxT("Find font for en&coding...\tCtrl-C"),
wxT("Find font families for given encoding"));
#ifdef wxHAS_PRIVATE_FONTS
// Try to use a private font, under most platforms we just look for it in
// the current directory but under OS X it must be in a specific location
// so look for it there.
//
// For OS X you also need to ensure that you actually do put wxprivate.ttf
// in font.app/Contents/Resources/Fonts and add the following snippet
//
// <plist version="0.9">
// <dict>
// ...
// <key>ATSApplicationFontsPath</key>
// <string>Fonts</string>
// ...
// </dict>
// </plist>
//
// to your font.app/Contents/Info.plist.
wxString privfont;
#ifdef __WXOSX__
privfont << wxStandardPaths::Get().GetResourcesDir() << "/Fonts/";
#endif
privfont << "wxprivate.ttf";
if ( wxFont::AddPrivateFont(privfont) &&
wxFont::ActivatePrivateFonts() )
{
menuSelect->AppendSeparator();
menuSelect->Append(Font_Private,
"Select private font",
"Select a font available only in this application");
}
#endif // wxHAS_PRIVATE_FONTS
// now append the freshly created menu to the menu bar...
wxMenuBar *menuBar = new wxMenuBar;
menuBar->Append(menuFile, wxT("&File"));
@@ -873,6 +914,20 @@ void MyFrame::OnSelectFont(wxCommandEvent& WXUNUSED(event))
}
}
void MyFrame::OnPrivateFont(wxCommandEvent& WXUNUSED(event))
{
wxFont font(GetCanvas()->GetTextFont());
if (font.SetFaceName("wxprivate"))
{
wxASSERT_MSG( font.IsOk(), wxT("The font should now be valid")) ;
DoChangeFont(font);
}
else
{
wxLogError("Failed to use private font.");
}
}
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
// true is to force the frame to close