For years, authors of Glulx games have asked for the ability to embed fonts in blorbs and render them in standard Glulx interpreters.
Here’s my proposal, a draft PR to add font support to blorbs.
- There’s a new “font” resource chunk. Only TrueType
.ttffonts are supported.- Each font resource chunk requires an entry in a “font description” resource chunk, which specifies the font weight, style (normal, italic, oblique), and family name for the font. (We’ll turn these into
@font-faceCSS rules in browser-based implementations. Non-browser implementations will use them to select a face, following the same rules as CSS.)- Variable fonts are supported (where one file contains multiple font weights/styles), but interpreters don’t have to honor them; they can just use the default variation and synthesize the font.
It’s intended to be used with the CSS Glk Extension.
There’s also an FAQ section, which I’ll copy and paste here:
Why TTF for fonts?
The same reasoning applies to fonts as to images: we must insist that every interpreter which supports fonts can display every font format in the standard, so we want exactly one format, and it should be the one that is easiest to support everywhere.
Every major operating system and every browser can render TrueType natively. FreeType (the standard open-source font library) has supported it for decades. The patents on TrueType have been expired since at least 2010.
Why not other font formats?
WOFF and WOFF2 are compressed wrappers around TrueType (or OpenType) data, designed for delivery over the web. Web browsers support them natively, but desktop font libraries often do not; WOFF2 in particular requires Brotli decompression, which FreeType only supports when it is built with an optional dependency.
It is easy for an author (or an authoring tool) to convert a WOFF or WOFF2 font into a TrueType file before packaging it.
OpenType fonts (
.otf) with PostScript (CFF or CFF2) outlines are widely supported, but allowing them would mean two required font formats instead of one, with no benefit for screen display. CFF2 (the variable-font form of CFF) is also not supported everywhere; GDI on Windows cannot load it. Fonts with PostScript outlines can be converted to TrueType outlines before packaging.TrueType collections (
.ttc) are not supported in web browsers.Why support variable fonts?
A variable font is still a TrueType font, and every platform that renders TrueType also renders variable TrueType fonts: FreeType, the major operating systems, and every modern browser. Many typefaces are now distributed primarily (or only) as variable fonts, and one variable font is often much smaller than the separate fonts it replaces.
The font description chunk handles variable fonts without any new structure: each entry names one instance, and an author lists only the instances the game needs. Interpreters still select among a small, fixed set of faces, just as they do for separate fonts. They don’t need to synthesize arbitrary designs on demand.
EDIT:
Why do we need the font description chunk? Can’t we just parse the TTF?
We could parse the TTF at runtime, but there are three reasons not to.
First, it’s crucial that authors use the correct family name to refer to their fonts; it’s very common for authors to know the file name of a font but not its embedded family name.
CSS’s approach is to allow authors to override the name of the font, and to use the overridden font name as close as possible to where it’s declared. We’re doing the same thing here, embedding the (possibly overridden) font family name along with the font itself.
Second, it’s surprisingly common for TTF metadata to be incorrect, e.g. you can have a ‘MyFont-Italic.ttf’ contains the italic version of a font, but doesn’t have its ‘italic’ bit set properly. If we need to override those settings, those overrides need to be embedded, too.
Third, IDEs like Inform are in the best position to parse TTFs, to verify that they’re being used correctly, and to generate a report of warnings/errors on incorrect metadata or incorrect usage. If Inform’s going to do all of that work, it might as well embed its computed metadata in the Blorb so interpreters don’t have to reimplement it.
By relying on the Blorb’s font metadata, we eliminate the possibility of font-rendering bugs that affect only some TTF parsers but not others.
How should we generate @font-face declarations?
In browsers, when using a custom font, you need to emit some @font-face blocks, like this:
@font-face {
font-family: "MyFont";
font-weight: normal;
src: url("myfont.ttf") format("truetype");
}
@font-face {
font-family: "MyFont";
font-weight: bold;
src: url("myfont-bold.ttf") format("truetype");
}
(Note that the thingies in a @font-face block are called “descriptors” instead of CSS “properties”, and that there’s no way to use CSS.supports() to test for whether the browser supports a particular descriptor of @font-face, which is a bummer, because there are zillions of them, almost none of which should be expected to have “mandatory” support in Blorb-compatible interpreters.)
Note also that we can’t just autodetect this data from the file’s contents. We have to track the metadata separately, outside the file.
EDIT: We can autodetect the data by parsing the TTF, but it’s probably not wise, for reason described in the FAQ. See “Can’t we just parse the TTF?” above.
My proposed answer is to define a new kind of chunk, a “font description” resource chunk, which includes an array of metadata entries.
Should we use a Glk API instead of storing this metadata in the blorb?
Instead of each font face getting a “font resource” description entry, I considered a Glk API, something like:
void glk_css_font_face_declare(glui32 resnum,
const char *descriptor, glui32 desclen, const char *val, glui32 vallen);
I’ve changed my mind about this like eight times; your input here is welcome.
- It seems worse for the required metadata about the font files to only live in imperative code
- But a Glk API would allow authors to provide any descriptor property that browsers support in
@font-face. (There are zillions of them, almost none of which should be expected to have “mandatory” support.)- Glk APIs can’t accept arrays of strings, I think…? So I guess we’d have to open some sort of transaction?
- Or implement a parser that can parse a string containing semi-colon delimited properties and values?
- Or establish a convention that the
font-familyproperty would always be declared last, and that would conclude the declaration?!
Maybe the “font resource” description should have an array of entries which, themselves, have an array of descriptors…?
Or maybe we should have a Glk API in addition to Blorb metadata, so users could add their own weird descriptors? (And have two conflicting sources of truth…? I guess the Glk API would probably override whatever’s in the Blorb.)
EDIT: See below why I decided we don’t need this.
Should variable-font variation support be mandatory?
EDIT: Yes, it’ll be mandatory.
Spatterlight can support font variations with Core Text. Gargoyle can support it with FreeType. Browsers support it automagically, so Parchment/Lectrote would get it for free.
The only major Glk library that doesn’t support variable-font font-variations is Windows Glk. Windows Glk uses Windows GDI to render text; GDI can open a variable font and render it in its default variation, but it can’t read/render the variations in a variable font.
It’s possible to port Windows Glk to use DirectWrite. DirectWrite supports variable-font variations on Windows 10+. I have a throwaway implementation on my machine, and it works.
EDIT: Windows Glk will upgrade to DirectWrite
Is the proposal too CSS-y?
My proposal is very, very CSS-y. It’s framed entirely in the context of CSS and its requirements/expectations.
- Font weights and widths are on a scale from 1-1000, just like CSS
- Font variations are matched “the way CSS does it.”
- I don’t use the embedded name in the font file, but only the declared name in the metadata, just like CSS
To the best of my knowledge, the only known users of fonts in blorbs would be the Glk CSS extension. But maybe that’s distorting my thinking about the issue…?
Is there a dependency problem?
- Should we avoid finalizing the Glk CSS Extension until this proposal is finalized?
- Should we avoid finalizing this proposal until the Glk CSS Extension is finalized?
I’m a bit worried that this proposal will throw a wrench into Glk CSS, preventing both from making progress, as each proposal waits for the other proposal to advance.
I kinda think that Glk CSS extension should go first. If we decide that we also need a new Glk CSS API for declaring font faces, that can get its own gestalt.