-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a way for applications to supply custom fonts (#711)
- Loading branch information
Showing
10 changed files
with
231 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# (C) Copyright 2005-2021 Enthought, Inc., Austin, TX | ||
# All rights reserved. | ||
# | ||
# This software is provided without warranty under the terms of the BSD | ||
# license included in LICENSE.txt and may be redistributed only under | ||
# the conditions described in the aforementioned license. The license | ||
# is also available online at http://www.enthought.com/licenses/BSD.txt | ||
# | ||
# Thanks for using Enthought open source! | ||
import warnings | ||
|
||
from traits.etsconfig.api import ETSConfig | ||
|
||
from kiva.fonttools.font_manager import default_font_manager | ||
|
||
|
||
def add_application_fonts(filenames): | ||
""" Add a TrueType font to the system in a way that makes it available to | ||
both the GUI toolkit and Kiva. | ||
Parameters | ||
---------- | ||
filenames : list of str | ||
Filesystem paths of TrueType or OpenType font files. | ||
""" | ||
if isinstance(filenames, str): | ||
filenames = [filenames] | ||
|
||
# Handle Kiva | ||
fm = default_font_manager() | ||
fm.update_fonts(filenames) | ||
|
||
# Handle the GUI toolkit | ||
if ETSConfig.toolkit.startswith("qt"): | ||
_qt_impl(filenames) | ||
elif ETSConfig.toolkit == "wx": | ||
_wx_impl(filenames) | ||
|
||
|
||
def _qt_impl(filenames): | ||
from pyface.qt import QtGui | ||
|
||
for fname in filenames: | ||
QtGui.QFontDatabase.addApplicationFont(fname) | ||
|
||
|
||
def _wx_impl(filenames): | ||
import wx | ||
|
||
if hasattr(wx.Font, "CanUsePrivateFont") and wx.Font.CanUsePrivateFont(): | ||
for fname in filenames: | ||
wx.Font.AddPrivateFont(fname) | ||
else: | ||
warnings.warn("Wx does not support private fonts! Failed to add.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# (C) Copyright 2005-2021 Enthought, Inc., Austin, TX | ||
# All rights reserved. | ||
# | ||
# This software is provided without warranty under the terms of the BSD | ||
# license included in LICENSE.txt and may be redistributed only under | ||
# the conditions described in the aforementioned license. The license | ||
# is also available online at http://www.enthought.com/licenses/BSD.txt | ||
# | ||
# Thanks for using Enthought open source! | ||
import os | ||
import unittest | ||
|
||
import pkg_resources | ||
|
||
from traits.etsconfig.api import ETSConfig | ||
|
||
from kiva.api import add_application_fonts, Font | ||
|
||
is_null = (ETSConfig.toolkit in ("", "null")) | ||
is_qt = ETSConfig.toolkit.startswith("qt") | ||
is_wx = (ETSConfig.toolkit == "wx") | ||
data_dir = pkg_resources.resource_filename("kiva.fonttools.tests", "data") | ||
|
||
|
||
@unittest.skipIf(not is_null, "Test only for null toolkit") | ||
class TestNullApplicationFonts(unittest.TestCase): | ||
def test_add_application_font(self): | ||
path = os.path.join(data_dir, "TestTTF.ttf") | ||
family = "Test TTF" | ||
kivafont = Font(family) | ||
|
||
# Before adding the font | ||
with self.assertWarns(UserWarning): | ||
self.assertNotEqual(kivafont.findfont().filename, path) | ||
|
||
add_application_fonts([path]) | ||
|
||
# After adding the font | ||
self.assertEqual(kivafont.findfont().filename, path) | ||
|
||
|
||
@unittest.skipIf(not is_qt, "Test only for qt") | ||
class TestQtApplicationFonts(unittest.TestCase): | ||
def setUp(self): | ||
from pyface.qt import QtGui | ||
|
||
application = QtGui.QApplication.instance() | ||
if application is None: | ||
self.application = QtGui.QApplication([]) | ||
else: | ||
self.application = application | ||
unittest.TestCase.setUp(self) | ||
|
||
def test_add_application_font(self): | ||
from pyface.qt import QtGui | ||
|
||
path = os.path.join(data_dir, "TestTTF.ttf") | ||
family = "Test TTF" | ||
font_db = QtGui.QFontDatabase() | ||
|
||
# Before adding the font | ||
self.assertNotIn(family, font_db.families()) | ||
|
||
add_application_fonts([path]) | ||
|
||
# After adding the font | ||
self.assertIn(family, font_db.families()) | ||
|
||
|
||
@unittest.skipIf(not is_wx, "Test only for wx") | ||
class TestWxApplicationFonts(unittest.TestCase): | ||
def setUp(self): | ||
import wx | ||
|
||
application = wx.App.Get() | ||
if application is None: | ||
self.application = wx.App() | ||
else: | ||
self.application = application | ||
unittest.TestCase.setUp(self) | ||
|
||
# XXX: How do we check to see if Wx loaded our font? | ||
@unittest.expectedFailure | ||
def test_add_application_font(self): | ||
import wx | ||
|
||
path = os.path.join(data_dir, "TestTTF.ttf") | ||
family = "Test TTF" | ||
|
||
fontinfo = wx.FontInfo() | ||
fontinfo.FaceName(family) | ||
wxfont = wx.Font(fontinfo) | ||
|
||
# Before adding the font | ||
self.assertFalse(wxfont.IsOk()) | ||
|
||
add_application_fonts([path]) | ||
|
||
# After adding the font | ||
self.assertTrue(wxfont.IsOk()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters