racket/draw: how to extract the path from a font% object?
60 views
Skip to first unread message
Matthew Butterick
unread,
Mar 31, 2020, 7:52:44 PM3/31/20
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Racket Users
IIUC every `font%` object must correspond to a particular font file on disk.
If so, given a `font%` object, how do I extract the path to that file?
Matthew Butterick
unread,
Apr 1, 2020, 2:48:25 PM4/1/20
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Racket Users
The answer is you can't, through pure Racket, because Racket delegates the nitty-gritty of font-file resolution to external font-handling libraries.
It is possible, however, to call into the `fontconfig` library via the FFI and make it do the heavy lifting of 1) scanning font directories to amass a list of possible fonts and then 2) finding a match for a certain query pattern, say a family name.
David Storrs
unread,
Apr 1, 2020, 11:14:15 PM4/1/20
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Matthew Butterick, Racket Users
I knocked together a very minimal pure-Racket library for this so you
don't have to FFI. It won't give you all the bells and whistles, but
it should be a good starting point.
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to David Storrs, Racket Users
The thorny issue that `fontconfig` handles is mapping from a high-level specification of a font (e.g., family, weight, slant) — that is, how a user would typically specify a font — to an actual font file on disk.
The shortcoming with font filenames is that they don't reliably tell you anything about the font inside. So `fontconfig` digs out metadata from the file itself (IIUC) and uses this to guess which font file best suits your wishes. (Also, Racket ships with `fontconfig`, so it’s guaranteed to be available.)
Most of `fontland` is esoteric nonsense, but for the benefit of future list spelunkers, here's how I used `fontconfig` (badly, perhaps) to solve this problem: