On-Chain Font

On-Chain Font

686 views

This post is about condensing font files to store them on-chain. Click here to skip to the guide.

I enjoy fully on-chain projects for many reasons:

  • strongest guarantees of transparency, permanence, and immutability;
  • native composition with anything else on-chain and dynamicism;
  • constraint-driven creativity and innovation.

Every implementation detail requires more thought because everything is costlier and harder to do on-chain, and every design choice is made more deliberately because it's supposed to live forever.

I've experienced this while building all of my fully on-chain projects (1, 2, 3, 4, 5, 6, 7, 8, 9, 10), and one of the most common tasks was to include font files fully on-chain for accurate and consistent rendering of text across all clients.

If font files aren't included on-chain, clients have to select fallback fonts, and the guarantee of accurate rendering is destroyed. Similarly, if font files are loaded in via external dependencies (e.g. Google Fonts), the transparency/permanence/immutability guarantees are greatly weakened.

The challenge is that font files can be quite large (>100 KB), and storing them on-chain can be expensive. Luckily, depending on your usecase, you can usually reduce this by 95+% to just 2-4 KB with a few optimizations.

Guide

If you're looking to store entire font files on-chain (i.e. all glyphs), this guide is probably useless.

Storing ~100 KB on-chain isn't prohibitively expensive—it's been done before, even on Ethereum mainnet. You could probably even get by with the most naive route: divide up the font file into many smaller parts, store them all to storage, then read them from storage. More modern approaches of storing large files use the SSTORE2 pattern, which "stores" and reads data as nonsensical contract bytecode because after some point, deploying and reading data as bytecode becomes much cheaper than the corresponding number of SSTORE/SLOADs. At 24 KB per contract, you only need a few shards, and even if you need more, tools like EthFS make it manageable.

If you don't need the entire font file, which is true most of the time, this is not worth doing. It'll cost more money and time, have worse DX of deploying/reading from multiple contracts, and bloat your final output.

The key realization of this guide is that font files implement paths for many glyphs, and most of the time, a project only requires a very small fraction of them. Thus, by condensing the font down to just the glyphs you need, you can greatly reduce the necessary font file's size.

Selecting the glyphs

Once you have a font you want to use, the first step is to determine the domain of characters that will be displayed with that font. For example, suppose we want to make an NFT that displays "fiveoutofnine" and the first 8 digits of the minter's checksummed address in Fira Code, something like:

fiveoutofnine0xA85572Cd

Even though there's a dynamic text component, we only need the 29 characters present in "fiveoutofnine", "0x", and lowercase/uppercase hexadecimal digits (with duplicates removed):

set("fiveoutofnine" + "0x0123456789abcdef" + "ABCDEF")

Let's write their unicode codes to a file named glyphs.txt with the following script:

PythonPython's logo.
write_glyphs.py
1
CHARACTERS = sorted(set("fiveoutofnine0x0123456789abcdefABCDEF"))
2
3
with open("glyphs.txt", "w") as file:
4
file.write("\n".join([f"U+{str(hex(ord(char))[2:]).zfill(4).upper()}" for char in CHARACTERS]))

Even on the web, it's common for font registries like Google Fonts to require subsets (e.g. latin) to be specified to only import necessary ranges of characters.

Condensing the font

Next, install fonttools and run the following command to generate the condensed font file:

condense_font.sh
pyftsubset ${FONT}.ttf --output-file=${FONT}-Subset.ttf --unicodes-file=glyphs.txt

Make sure to run the command on a font file with a specified weight, rather than the variable weight version.

You can also optionally convert the font file into the WOFF2 format. It has good browser support and usually yields a further ~65% size reduction. I recommend using an online converter such as this one.

With this, we go from 184.1 KB to 3.4 KB—a 98.2% reduction!

Using the font

The easiest way to use the font on-chain is to base-64 encode it, so it can be used inline (e.g. inside some SVG source):

PythonPython's logo.
base64_encode_font.py
1
import base64
2
3
FONT_NAME = "FiraCode-Regular-Subset"
4
5
with open(f"{FONT_NAME}.txt", "w") as output_file:
6
with open(f"{FONT_NAME}.woff2", "rb") as input_file:
7
output_file.write(
8
f"data:font/woff2;utf-8;base64,{base64.b64encode(input_file.read()).decode('utf-8')}"
9
)

This encoding increases the size by ~34% (between [43,437876][\frac{4}{3},\frac{4}{3}\cdot\frac{78}{76}]).

Then, you can use the font inside some SVG as follows:

fiveoutofnine0xA85572Cd
1
<svg width="256" height="256" viewBox="0 0 256 256" xmlns="http://www.w3.org/2000/svg">
2
<style>
3
@font-face {
4
font-family: FiraCode;
5
src: url(data:font/woff2;utf-8;base64,d09GMgABAAAAAA2MABAAAAAAF4AAAA0wAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGiIbIByESAZgP1NUQVQuAGQRCAqfDJlAC3AAATYCJANwBCAFg1wHIAwHGzATo6KUsfaR/eUBT8ZrvAwnZECdEJ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4migiHLCgEAbI0bphRfSRcABe5A0pPkaJBkCWzRiEh6tYhop4Yfx4rAvY+FoW2So7D2MAE8CUgS+3jOdSifRsJZ1Wog+trDIcCV9xVzZcyTr6Q29OwaKd4xV7DzmLGY2rWcsBkh6QW0ZNMXC4kIcp2uab8Au6H84S03ofZL/2TtaCwAA)
6
}
7
</style>
8
<path d="M1 9v238q0 8 8 8h238q8 0 8-8V9q0-8-8-8H9Q1 1 1 9" fill="#111" stroke="#3a3a3a" stroke-width="2"/>
9
<text style="font-family: FiraCode" x="50%" y="125" fill="#eee" font-size="16" text-anchor="middle" dominant-baseline="bottom">
10
fiveoutofnine
11
</text>
12
<text style="font-family: FiraCode" x="50%" y="131" fill="#b4b4b4" font-size="14" text-anchor="middle" dominant-baseline="hanging">
13
{ADDRESS}
14
</text>
15
</svg>