Skip to content

Add a way for applications to supply custom fonts#711

Merged
jwiggins merged 6 commits into
masterfrom
feature/app-specific-fonts
Mar 15, 2021
Merged

Add a way for applications to supply custom fonts#711
jwiggins merged 6 commits into
masterfrom
feature/app-specific-fonts

Conversation

@jwiggins
Copy link
Copy Markdown
Member

Closes #555

This works well with the QPainter and AGG backends. It does not work with the Quartz backend. Also, I was not able to test this properly with Wx because the feature isn't supported in the wheel that I installed (perhaps it's a macOS thing?).

@jwiggins
Copy link
Copy Markdown
Member Author

Perhaps this: QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-runner' has something to do with the failure?

@jwiggins
Copy link
Copy Markdown
Member Author

Works just fine on Windows 🎉

@jwiggins
Copy link
Copy Markdown
Member Author

From the Qt docs:

Adding application fonts on Unix/X11 platforms without fontconfig is currently not supported.

Also, QFontDatabase::addApplicationFont is a static method.

@jwiggins
Copy link
Copy Markdown
Member Author

macOS + pyqt appears to have crashed when running the OpenGL drawing tests... Sort of an improvement. The tests are passing for the new feature.

@rahulporuri
Copy link
Copy Markdown
Contributor

rahulporuri commented Mar 12, 2021

I've been trying to experiment with the new add_application_font api but i'm having trouble finding an actual font file i can download and make use of - i'm not sure what the font name in the test font files is which are used in the tests.

Update : I came across the Noto fonts (https://web.archive.org/web/20150411172453if_/http://www.google.com/get/noto/) while browsing the Microsoft Font Typography Vendor page (https://docs.microsoft.com/en-gb/typography/font-list/). I was able to successfully add it in the kiva_explorer using

fontfilepath = "C:\\Users\\rporuri\\Downloads\\NotoSans-hinted\\NotoSans-Regular.ttf"
add_application_font(fontfilepath)
fontfilepath = "C:\\Users\\rporuri\\Downloads\\NotoSerif-hinted\\NotoSerif-Regular.ttf"
add_application_font(fontfilepath)

but then when I tried using the font via gc.set_font(Font('Noto', size=42)), I get nothing. I don't see errors specific to the Noto font and I just see the default font used in the kiva explorer application.

Edit : Noto Sans and Noto Serif are what need to be passed to Font - and the code works now like I expect it to. 🎉

Copy link
Copy Markdown
Contributor

@rahulporuri rahulporuri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with a few questions/comments. I tested this in the kiva explorer with a font I downloaded and things worked as expected.

There's one minor user improvement I request. When I downloaded the Font, I downloaded a zip archive which contained 4 font files - one for each style i.e. regular, bold, bold italic and italic. I needed to add each font file separately to be able to use style kwarg to Font correctly. Do you think it makes sense to support passing directories to add_application_font?

We should also update the user documentation - but we can also do that in a separate PR if you prefer that.

Comment thread kiva/api.py Outdated
if ext in fontexts:
fontfiles.add(path)

return sorted(fontfiles)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know that sorting a set will return a list - in case anyone was wondering if it'll be a problem that we're returning an empty list above but a sorted set here.

Comment thread kiva/fonttools/_scan_sys.py
Comment thread kiva/fonttools/font_manager.py Outdated
Comment thread kiva/fonttools/font_manager.py Outdated
@rahulporuri
Copy link
Copy Markdown
Contributor

I tried using the kiva explorer on wx (because i wanted to try this PR on wx) and I ran into a few issues across ETS - enthought/pyface#901, enthought/traitsui#1535 and #713.

@rahulporuri
Copy link
Copy Markdown
Contributor

After fixing issues with the ETS libraries (traitsui and pyface) underneath, I was actually able to get this working with the wx toolkit - and it works perfectly 🎉

@jwiggins
Copy link
Copy Markdown
Member Author

The macOS test is calling abort() from some PyObjC code...

Fatal Python error: Aborted

Current thread 0x0000000109bbddc0 (most recent call first):
  File "/Users/runner/work/enable/enable/.edm/envs/enable-test-3.6-pyqt/lib/python3.6/site-packages/pyglet/libs/darwin/cocoapy/runtime.py", line 775 in __call__
  File "/Users/runner/work/enable/enable/.edm/envs/enable-test-3.6-pyqt/lib/python3.6/site-packages/pyglet/libs/darwin/cocoapy/runtime.py", line 805 in __call__
  File "/Users/runner/work/enable/enable/.edm/envs/enable-test-3.6-pyqt/lib/python3.6/site-packages/pyglet/window/cocoa/__init__.py", line 314 in dispatch_events
  File "/Users/runner/work/enable/enable/.edm/envs/enable-test-3.6-pyqt/lib/python3.6/site-packages/kiva/tests/test_gl_drawing.py", line 76 in draw_and_check
...

@jwiggins
Copy link
Copy Markdown
Member Author

I've disabled the GL drawing test on everything but Linux now. CI is finally happy (and I still hate the GL backend).

@jwiggins jwiggins merged commit ba6f246 into master Mar 15, 2021
@jwiggins jwiggins deleted the feature/app-specific-fonts branch March 15, 2021 11:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

API for adding application-specific fonts

2 participants