Script to convert TTF or OTF fonts to WOFF2
Summary
Convert TrueType Font (TTF) and OpenType Font (OTF) files to Web Open Font Format 2 (WOFF2) using a simple script. This guide explains the process and provides installation and usage instructions for the script, which works for single or multiple font files.
Differences between TTF, OTF, and WOFF2 fonts #
TrueType Font (TTF) and OpenType Font (OTF) are common font formats used for desktop applications and operating systems. However, for web development, Web Open Font Format 2 (WOFF2) is preferred because it provides better compression, reducing font file sizes and improving website performance.
Unlike TTF and OTF fonts, which need to be installed on the user’s computer to be used in applications, WOFF2 fonts are embedded directly in web pages. This means that users do not need to have the font installed locally, as their web browser loads and renders the font automatically from the website, ensuring consistent typography across different devices and platforms. This gives web designers a lot more freedom in creating great user experiences.
Installing the woff2
dependency #
Before running the script, you need to install the woff2
tool, which provides the necessary compression and decompression utilities.
You can install woff2
by following these steps:
Manual installation #
Clone and build from Google’s repository.
Linux package managers #
Alternatively, use the packages provided by some distributions:
- Debian/Ubuntu:
sudo apt install woff2
- Fedora:
sudo dnf install woff2
- Arch Linux:
sudo pacman -S woff2
Convert single TTF and OTF files to WOFF2 #
Once woff2
is installed, you can use the following commands to convert single files:
If you have installed woff2
manually, ensure the binaries from the build process are in your $PATH
.
- To compress a single TTF or OTF file to WOFF:
woff2_compress myfont.ttf # or woff2_compress myfont.otf
- To decompress a WOFF2 file back to TTF:
woff2_decompress myfont.woff2
Script to convert all TTF and OTF files to WOFF2 #
The following shell script iterates over all TrueType Font (TTF) and OpenType Font (OTF) files in the current directory and converts them to WOFF2 format:
#!/bin/sh
# Iterate over existing .otf and .ttf font files in ./
for file in ./*.ttf ./*.otf ; do
# Get file name and store it in $font
font=`basename "$file"`
# Print name of currently processed file to standard output (stdout)
echo "$font"
# Compress $font with woff2_compress - the result will be stored in ./
woff2_compress "$font"
done
exit
Explanation of the script #
- The script starts by iterating over all
.ttf
and.otf
files in the current directory (./
). - It extracts the filename using the
basename
command and stores it in thefont
variable. - The script prints the name of each file being processed to the terminal.
- The
woff2_compress
command is run on each file to generate a WOFF2 font file. - The script repeats this process for all
.ttf
and.otf
files before exiting.
How to use the script #
- Copy and paste the script into a new file, for example,
convert_fonts.sh
. - Give the script execution permissions:
chmod +x convert_fonts.sh
- Run the script in the directory containing your
.ttf
and.otf
font files:./convert_fonts.sh
- The newly created WOFF2 files will appear in the same directory.
Happy hacking!
Further readings #
Sources and recommended, further resources on the topic:
- W3C: WOFF File Format 2.0
- Google WOFF2 compression reference code
- Ubuntu Linux WOFF2 package
- Arch Linux WOFF2 package
- Fedora Linux WOFF2 package
License
Script to convert TTF or OTF fonts to WOFF2 by Jonas Jared Jacek is licensed under CC BY-SA 4.0.
This license requires that reusers give credit to the creator. It allows reusers to distribute, remix, adapt, and build upon the material in any medium or format, for noncommercial purposes only. To give credit, provide a link back to the original source, the author, and the license e.g. like this:
<p xmlns:cc="http://creativecommons.org/ns#" xmlns:dct="http://purl.org/dc/terms/"><a property="dct:title" rel="cc:attributionURL" href="https://www.ditig.com/script-to-convert-ttf-otf-fonts-to-woff2">Script to convert TTF or OTF fonts to WOFF2</a> by <a rel="cc:attributionURL dct:creator" property="cc:attributionName" href="https://www.j15k.com/">Jonas Jared Jacek</a> is licensed under <a href="https://creativecommons.org/licenses/by-sa/4.0/" target="_blank" rel="license noopener noreferrer">CC BY-SA 4.0</a>.</p>
For more information see the Ditig legal page.