New Univ Studies

NewUnivStudies.org

Converting Desktop Fonts
to Web Fonts

Desktop fonts for operating systems like Microsoft Windows use filename extensions .ttf and .otf.

Web fonts, displayed in browsers, use file extension .woff2.

This page provides examples of how to use Python to convert desktop fonts to web fonts.

Python uses the fonttools and brotli python libraries to create .woff2 files. To install those packages, if not already installed, first enter this command in the OS terminal (e.g., PowerShell) to install fonttools:

pip install fonttools

After fonttools finishes installing, enter this command to install brotli:

pip install brotli

After fonttools and brotli are installed, we are ready to convert desktop fonts to web fonts.

The first font we will convert is Chantelli Antiqua, which has a .ttf filename extension.  The converted (woff2) version of this font, if supported by your browser, rendered the first line at the top of this web page (“New Univ Studies”).

Assuming the font file Chantelli_Antiqua.ttf is stored in a folder called tmp on the C: drive (use any other appropriate folder as needed), enter these commands into the Python command line interface to create a web font with .woff2 filename extension:

>>> from fontTools.ttLib import TTFont
>>> f = TTFont('c:/tmp/Chantelli_Antiqua.ttf')
>>> f.flavor='woff2'
>>> f.save('c:/tmp/Chantelli_Antiqua.woff2')

(Specify correct folder if different than in this example).

Chantelli Antiqua was a conventional font, not a variable font. Variable fonts are also converted, such as Public Sans which renders the main body text of this web page:

>>> from fontTools.ttLib import TTFont
>>> f = TTFont('C:/tmp/PublicSans[wght].ttf')
>>> f.flavor='woff2'
>>> f.save('C:/tmp/PublicSans[wght].woff2')

Fonts with .otf extension can also be converted, including the Cantarell variable font which renders the second line at the top of this web page (“NewUnivStudies.org”):

>>> from fontTools.ttLib import TTFont
>>> f = TTFont('C:/tmp/Cantarell-VF.otf')
>>> f.flavor='woff2'
>>> f.save('C:/tmp/Cantarell-VF.woff2')


Return to: Computer Graphics
New Univ Studies