Fonts in blorbs

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 .ttf fonts 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-face CSS 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?

EDIT: I think not

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-family property 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.

5 Likes

Yes!

1 Like

This looks like a solid plan! Not using woff2 is fine, blorbs already should be compressed over HTTP.

I’m not sure what the benefit of specifying an oblique font would be? Isn’t that intended for just tilting the base font? If you have a distinct font file shouldn’t it be an italic?

Maybe there should be a recommendation to trim font files to remove unused glyphs when possible?

The font-family of these should both be just "MyFont".

3 Likes

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.

But why, though? My understanding is that when a browser is resolving a font request, it matches it against a font-database built from metadata extracted from system fonts, and metadata from @font-face rules. The only reason browsers need @font-face rules, rather than extracting metadata from downloaded fonts directly, is that they desperately want to avoid downloading anything they’re not absolutely sure they need to display the page. For the purposes of fonts-in-blorbs, the font-data is already locally available, so it should be possible to extract metadata from them just like system fonts.

2 Likes

I’d expect that many users of this would want to use a custom name to ensure that their custom fonts are actually being used. For example if the interpreter loaded some old version of Font Awesome the player downloaded years ago then it might be missing many icons the author needs. (I don’t know how the various font libraries handle loading fonts the user already has on their computer. This might not actually be a concern.)

And it would also mean a client JS library like AsyncGlk would avoid needing to parse TTF files.

I wrongly thought that certain variable fonts with “slant” variations (and no “italic” variation) required a @font-face { font-style: oblique } declaration. https://fonts.google.com/specimen/Roboto+Flex is an example. But, after you mentioned it, I did some experiments, and it turns out that font-style: italic works just fine. I’ll remove oblique.

Oops! fixed.

This is a good point! Based on further research, I think I definitely overstated the case that we “can’t” extract the metadata from system fonts. A decent TTF parser can be implemented in ~400 lines of code. It’s not out of the question to add that to a Blorb parser, even one intended to be run in client-side JavaScript.

But I still think that Blorb parsers shouldn’t be in the business of parsing TTF files.

Now, I must concede that if we add fonts to blorbs, somebody is going to have to try to compute/generate all the necessary metadata, at the very least the font family name, the font style(s), and the font weight(s) in the TTF file. Generating the metadata based on the TTF’s embedded metadata is the obvious place to start.

Presumably someday we want it to be possible to drag a TTF into the Inform IDE and have it work. When you do that, either Inform is going to have to try to inspect the TTF to generate metadata, or the interpreter will have to do it. Either way, somebody is going to have to embed a TTF parser!

So, I asked myself, who is “best equipped” to deal with metadata issues (including overrides) in fonts?

  • If you’ve got a font that’s italic, but it doesn’t have the italic bit set, who’s going to figure that out and fix it?
  • If you’ve got a file OpenSans.ttf whose embedded font family name is Open Sans, and the author thinks the name is OpenSans, who’s going to figure that out and fix it?

IMO, at least as much as avoiding unnecessary downloads, @font-face ensures that the font metadata is set by the user of the metadata, as close as possible to where the metadata is actually used. If you write @font-face { font-family: "OpenSans" }, well, it doesn’t matter what the TTF says, the @font-face wins, and OpenSans will be the font’s name for that page load.

And if MyFont-Italic.ttf is missing its italic bit, we could tell the author to go download a free font editor and repair the font’s metadata, but I think most authors would have no ability to manage that. Instead, Inform’s build should probably generate a report on the fonts used, logging warnings on potential issues, and allowing authors to set embedded blorb metadata right there in the IDE.

If Inform doesn’t do any of that, then when authors try to refer to Open Sans by the name OpenSans, it’s just not going to work when authors try to test the game in an interpreter, with no error message and no clear next diagnostic steps. That would really suck.

On the other hand, if Inform is doing that job, then clearly the interpreters should free ride on the hard work that Inform did.

Worst of all would be implementing a TTF parser in N Glk libraries, creating the possibility of a bug that impacts the TTF parser in just one of them and not the others. By making the IDE be the source of truth, we ensure that certain kinds of mistakes are simply impossible.

EDIT: I’ve added a new question to the FAQ.

  • 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.

3 Likes

If you’re going to do this, I think you should consider something in the font metadata about the licence for the font.

I say this as we’ve seen quite a few uploads of HTML-based games to the IF-Archive that have included copyrighted fonts that can’t be distributed. When we’ve pointed this out, the common denominator is that the uploader didn’t even think about the font licence - they saw a font they liked, used it, then when packaging up their game, stuck it in there.

You could say that the same issue is true of pictures and sounds. But these are at least reasonably easy to generate yourself. Realistically, hardly anyone is going to generate their own font. They’re going to download or copy one from somewhere. Having something required to be in the metadata about the licence won’t stop anyone determined to ignore licensing, but it might cause authors who hadn’t even considered the issue to think about it.

7 Likes

If you’re going to do this, I think you should consider something in the font metadata about the licence for the font.

I spent some time investigating this.

TrueType fonts carry human-readable copyright and license metadata in their name table, and there’s one machine-readable permission field, but it’s weirdsies, man.

Text fields in the name table

ID Field Example (Crimson Text)
0 Copyright notice “Copyright 2010 The Crimson Text Project Authors (https://github.com/googlefonts/Crimson)”
7 Trademark “Crimson is a trademark of Sebastian Kosch.”
8 Manufacturer “Sebastian Kosch”
9 Designer “Sebastian Kosch”
11 / 12 Vendor URL / designer URL “https://aldusleaf.org/”
13 License description “This Font Software is licensed under the SIL Open Font License, Version 1.1. …”
14 License URL “https://scripts.sil.org/OFL”

I checked ten font files from Google Fonts, in seven families (Cinzel Decorative, Crimson Text, Dancing Script, Recursive, Roboto, Roboto Flex, Source Serif 4). All of them have a copyright notice and license text; all but one have a license URL, and most name the designer and manufacturer.

One machine-readable field: fsType in the OS/2 table

These are the font’s embedding permissions, intended for embedding fonts in documents like PDFs:

fsType Meaning
0 Installable (no restriction)
0x2 Restricted: must not be embedded
0x4 Embed for preview and print only
0x8 Embed for editing
0x100 No subsetting
0x200 Embed bitmaps only

The Google Fonts files I checked all have fsType 0.

I did find a font OCR A Extended used in Skybreak! that has fsType 0x8 (Embed for editing). Its license field says, “Please contact the vendor to learn more about license restrictions.”

(Skybreak! doesn’t actually include OCR A Extended; it just refers to it, hoping that you happen to have “OCR A Extended” already installed in your system.)

fsType = 0 (installable) doesn’t mean “free to redistribute.” So, what does it mean?

Many commercial fonts allow you to “embed” them in a document (e.g. in a PDF) with fsType 0x0, 0x4, or 0x8, but don’t allow you to “redistribute” the font file itself.

The distinction is quite subtle, because it’s possible to extract a font embedded in a PDF. In my opinion, embedding a font in a PDF is redistributing it, embedded. (I don’t think many font authors fully understand this.)

I don’t know whether font authors who say “embedding is allowed, but redistribution isn’t” would be happy with, say, distributing the font file in a ZIP containing HTML+CSS. I can imagine a smartass argument of the shape “I’m not redistributing your font, I’m just embedding it in my ZIP document!” But the argument doesn’t ring true.

I think part of the reason why the argument doesn’t ring true is that you don’t normally consume ZIP files directly. Instead, the first thing end users are expected to do is to extract the ZIP, giving them access to all of the files inside.

But there are plenty of ZIP formats that are expected to be used zipped. Quest .quest files are zips, but you’re meant to just open them in the Quest player.

Strangely, the argument “I’m not redistributing the font, I’m embedding it in a .quest file” feels more true to me than the argument about redistributing them in a ZIP file, even though .quest files are ZIP files.

It feels like these authors just want it to be, ya know, harder to steal their font.

In my opinion, a Blorb is a document embedding a font, and the machine-readable fsType field is what automated tools should use to determine whether distributing a font in a Blorb is permissible.

It is possible to extract fonts from Blorbs, but it’s also possible to extract them from PDFs. Automated tools should assume that a font that allows itself to be embedded in a PDF would also be allowed to be distributed in a Blorb.

The Inform IDE should refuse to embed fonts that restrict embedding

I’ve argued earlier in this thread that Inform would parse the TTF, not just to generate Blorb metadata, but to generate a report that warns of possible font issues.

I was imagining technical warnings, e.g. when a file name says “italic” but the “italic” bit isn’t set, or when two files claim the same family, weight, and style, so one of them will never be used.

But it makes sense to me to include licensing warnings, as well. The report should at least show each font’s copyright and license. Inform should probably fail with an error if fsType is 0x2 Restricted or 0x200 Embed bitmaps only, and maybe warn when the license fields are empty.

Maybe Inform should warn when distributing a game as a website with a font that permits embedding but doesn’t have a license that clearly permits redistribution. (But, if so, it will be by machine parsing the human-readable license fields out of the TTF, which sounds perilous.)

The IF Archive could check blorbs for permissible fsType values, but I doubt that’s worth it

Maybe there could be a CLI tool that would inspect a blorb to ensure all of its fonts had permissible fsType values?

(But, I’m not sure it’s worth running, especially if Inform/inblorb automatically refuse to embed fonts that don’t permit embedding. 99.999% of blorbs will have permissible fsTypes.)

Maybe it would be more useful to have a CLI tool that checks to see if a zip contains fonts that don’t permit redistribution? (But, again, this would require parsing human-readable license fields, which is perilous.)

I don’t think we need the Blorb itself to include the fsType value in the font description chunk

99.999% of the time, Inform/inblorb is going to have validated it already. Interpreters would have no use for it. And, if I’m right, even the IF Archive wouldn’t need to use it.

3 Likes

I agree with this in particular.

I think the one wrinkle in this argument is that in PDFs, fonts are often embedded via subsetting, which is to say, a subset of the glyphs is included (only those used in the PDF), and the mapping from glyph ID to glyph differs from the source font. You can extract it, but it’s pretty useless. So a font author could say, yeah, embed me in a PDF but use subsetting when you do. Nothing’s technically stopping somebody from just embedding the full font, naturally, but PDF at least gives a way to “protect” the fonts to a decent degree.

1 Like

I’ve pushed a new draft of the proposal.

  • oblique is out
  • We recommend that authoring tools refuse to embed TTFs that restrict embedding
  • I’ve added support for condensed variations in variable fonts (accessed via font-stretch: condensed, aka font-width. font-stretch is not required to be implemented in Glk CSS “Basic”, but font-stretch could still be used in browser-based environments, or in any interpreter that implements font-stretch.)

I don’t think we’re going to need a Glk API for @font-face descriptors

The descriptors supported by @font-face really fall into two categories:

  1. Identification/range restrictions

    • font-family
    • font-style
    • font-weight
    • font-stretch / font-width (font-width is the new name, less widely supported)
    • unicode-range
  2. Descriptors aimed at mitigating layout shift / temporary fallbacks while a font is loading

    • font-display
    • size-adjust
    • font-variation-settings
    • font-feature-settings
    • ascent-override
    • descent-override
    • line-gap-override

I say that these descriptors are “aimed at” mitigating layout shift, but only in the sense that they’re setting values that could have been set with ordinary CSS properties, if only you could know for sure in advance which font you’re going to use (i.e. that you aren’t going to fallback to another font).

For example, you can set font-size-adjust as a CSS property, but if you don’t know which font was used, you can’t know what value to use. Associating different @font-faces with different size-adjust values is what fixes that. Here’s a guide walking through how it works, including guidelines on using ascent-override, descent-override, and line-gap-override.

But we don’t really have to worry about any of that in Blorb land. If you use a font from a Blorb, you’re going to get that font, guaranteed, so you can just use CSS properties like font-size-adjust to set what you’re looking for.

Even font-display probably doesn’t mean anything for us. We don’t render anything in a Blorb without first downloading the entire Blorb. A typical browser-based implementation would use the JS FontFace API, which would pass the font as a binary blob, and await its promise before rendering any text in the font.

(We can always add a Glk API later, but, as I write this, I don’t anticipate any need to.)

I guess they could say that as a term of their license, (licensing can have arbitrary restrictions, anything humans can write down), but there’s no technical way to make an embedding tool know that they must subset the font when embedding. (0x100 is the opposite of that, forbidding subsetting.)

So I think the point stands that fsType is all we have; we should assume that 0x2 and 0x200 are the only restrictions we can detect and automatically honor.

1 Like

I wish I could find the citation I’m thinking about, but my understanding is that’s at least part of the reason for the existence of the WOFF font format. If somebody finds a .ttf lying around on their hard-drive, they might legitimately think they’ve been given the right to use it for whatever purpose. If somebody gets a font by extracting it from a PDF, or using a special tool to convert WOFF back into TTF, or rummaging around with a hex-editor, they should know they’re stepping outside the bounds of expectation.

Even though embedding a font is effectively redistributing it, intent is what seems to matter here. I agree that “.ttf in a blorb” amounts to embedding, especially since blorb-extraction tools are less widely available than ZIP extraction tools.

It looks like Windows Glk is going to upgrade to DirectWrite. In that case, all major Glk libraries can support variable fonts.

Since we don’t have to make variable fonts optional, (and since making them optional adds complexity), I’ve updated the proposal to make them mandatory.

I therefore think that all of my open questions about this proposal are resolved, but we should probably still wait to merge it into the Blorb spec until after the Glk CSS extension is finalized.

I predict that will leave plenty of time for comment.