Skip to content

Instantly share code, notes, and snippets.

@fangel
Created September 13, 2018 14:46
Show Gist options
  • Save fangel/c38f72010c086108f8bcd5ee63a461f8 to your computer and use it in GitHub Desktop.
Save fangel/c38f72010c086108f8bcd5ee63a461f8 to your computer and use it in GitHub Desktop.
Font subsetting using pyftsubset + sfnt2woff-zopfli

Font subsetting using pyftsubset + sfnt2woff-zopfli

A Makefile for automatically building a font subset in WOFF2 and WOFF-with-Zopfli.

Getting started

Dependencies

You must install Fonttools and SFNT2WOFF-Zopfli to use the functionality in this repo. You will also need to ensure that the brotli library is available for Fonttools.

Fonttools can be easily installed using Homebrew, by doing brew install fonttools. If you installed via Brew, you can install the brotli extension using

/usr/local/Cellar/fonttools/3.5.0/libexec/bin/pip install brotli

SFNT2WOFF-Zopfli is a bit harder to install. You can do it by downloading the source off of GitHub and then running make inside the downloaded directory.

Setup

First create a directory to hold this script and it's work-dir. Inside the dir, copy over the Makefile and create the directories input and output. Place your original font-files in the input-directory

You also need to create a file called subset.txt that contains all of the characters you want the subsetted font to contain.

If you starting font-files are not in .woff, you need to open up the Makefile and modify the variable INPUT_EXT to e.g. woff2 or otf.

Running the optimize scripts

Simply run make in the directory.

If Fonttools isn't in your path, use FONTTOOLS_DIR to change where to search for the tools. Similarly for SFNT2WOFF-Zopfli where you can use SFNT2WOFF_DIR to modify the search-path.

If you wish to change the number of iterations that Zopfli runs, then you can use the variable ITERATIONS

If you want to utilize more than one CPU, you can use the -j-flag, which enables parallel processing in Make.

Example

If you have Fonttools installed via Brews (and thus in your path), but have SFNT2WOFF-Zopfli in your user-directory (Users/xyz/sfnt2woff-zopfli), and you want to run 50 Zopfli-iterations and utilize 4 CPU-cores, you can use

SFNT2WOFF_DIR=/Users/xyz/sfnt2woff-zopfli ITERATIONS=50 make -j4
# The following variables determine which directories to look for input fonts,
# and which directory to place the output fonts in.
INDIR = input
INPUT_EXT = woff
OUTDIR = output
SUBSET_FILE = subset.txt
SUBSET_FLAGS = --layout-features='' --obfuscate-names --glyphs="space"
ZOPFLI_ITERATIONS = 15
ifneq ($(shell echo $$ITERATIONS),)
ZOPFLI_ITERATIONS = $(shell echo $$ITERATIONS)
endif
# SUBSET will point to pyftsubset, but we allow changing the search-dir using
# the variable `FONTTOOLS_DIR`
SUBSET := pyftsubset
ifneq ($(shell echo $$FONTTOOLS_DIR),)
SUBSET = $(shell echo $$FONTTOOLS_DIR)/bin/pyftsubset
endif
# WOFF2SFNT & SFNT2WOFF will point to sfnt2woff-zopfli, but again, we allow
# chaing the search-dir. This time using the variable SFNT2WOFF_DIR
WOFF2SFNT = woff2sfnt-zopfli
SFNT2WOFF = sfnt2woff-zopfli
ifneq ($(shell echo $$SFNT2WOFF_DIR),)
WOFF2SFNT = $(shell echo $$SFNT2WOFF_DIR)/woff2sfnt-zopfli
SFNT2WOFF = $(shell echo $$SFNT2WOFF_DIR)/sfnt2woff-zopfli
endif
# Fonts will contain a list of all of our input files. Not used, besides to
# build the next two lists
FONTS := $(shell find $(INDIR) -name "*.$(INPUT_EXT)")
# A list of all of our woff and woff2 output files. They are based on the
# available input-fonts, but placed in the ourdir.
WOFFS := $(patsubst $(INDIR)/%.$(INPUT_EXT),$(OUTDIR)/%.woff,$(FONTS))
WOFF2S := $(patsubst $(INDIR)/%.$(INPUT_EXT),$(OUTDIR)/%.woff2,$(FONTS))
# A build-rule for all of our woff-output
$(OUTDIR)/%.woff : $(SUBSET_FILE) $(INDIR)/%.$(INPUT_EXT)
@# First we subset the font to a normal zlib compressed woff-file
$(SUBSET) $(patsubst $(OUTDIR)/%.woff,$(INDIR)/%.$(INPUT_EXT),$@) --text-file=$(SUBSET_FILE) $(SUBSET_FLAGS) --flavor=woff --output-file=$(@:.woff=.tmp.woff)
@# And then we want to run it through Zopfli - but to do that we must first
@# convert it to OTF, and then back to WOFF with Zopfli compression
$(WOFF2SFNT) $(patsubst %.woff,%.tmp.woff,$@) > $(@:.woff=.otf)
$(SFNT2WOFF) -n $(ZOPFLI_ITERATIONS) $(@:.woff=.otf)
@# We want to output a little debug-info about how much we saved with Zopfli
@echo \# $(patsubst ${OUTDIR}/%.woff,%,$@): Pre zopfli: `wc -c $(@:.woff=.tmp.woff) | sed -e 's/^[[:space:]]*//' | cut -d' ' -f 1`, With Zopfli: `wc -c $@ | sed -e 's/^[[:space:]]*//' | cut -d' ' -f 1`
@# And then we can clean up our tmp files
$(RM) $(@:.woff=.otf) $(@:.woff=.tmp.woff)
# A build-rule for all of our woff-output
$(OUTDIR)/%.woff2 : $(SUBSET_FILE) $(INDIR)/%.$(INPUT_EXT)
$(SUBSET) $(patsubst $(OUTDIR)/%.woff2,$(INDIR)/%.$(INPUT_EXT),$@) --text-file=$(SUBSET_FILE) $(SUBSET_FLAGS) --flavor=woff2 --output-file=$@
# The all rule, which has all of our woff and woff2 fonts as dependencies.
all : $(WOFFS) $(WOFF2S)
# And a clean-task to remove the built files again
clean :
$(RM) $(OUTDIR)/*.woff $(OUTDIR)/*.woff2 $(OUTDIR)/*.otf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment