My Favorite Open Source Project
Tags: octograd2020
Published: 2020-05-21 18:19:09

## Add Support for WOFF2 Fonts to FreeType

Over the Summer of 2019 I worked on [The FreeType Project](https://www.freetype.org/) as a Google Summer of Code student to integrate support for the WOFF2 format into FreeType so that these fonts can be recognized, decompressed, and loaded as any other SFNT font. This included study and comparison of how SFNT fonts are wrapped into WOFF 1.0 and WOFF 2.0 files, exploration of existing libraries for WOFF 2.0 and Brotli compression, and finally, writing code to allow FreeType to handle WOFF 2.0 fonts.

### Introduction to FreeType

FreeType is a freely available software library to render fonts. It is written in C, designed to be small, efficient, highly customizable, and portable while capable of producing high-quality output (glyph images) of most vector and bitmap font formats. Today, more than a _billion_ devices across the planet contain FreeType.

### Introduction to WOFF2

The WOFF 2.0 Web Font compression format ([W3C Recommendation](https://www.w3.org/TR/WOFF2/) 01 March 2018) is a  web font format developed by W3C in concert with Google and Monotype.

Web fonts are fonts that are automatically downloaded and used on demand to render a web page. Web fonts adoption statistics from 2018 showed that over 70% of the top 10,000 websites used web fonts. They have proven to be very useful, especially for non-English markets, where it is convenient to serve fonts over the web. More information on web fonts and their adoption can be found [here].

The WOFF2 format offers a 30% average compression gain over the original WOFF, making it faster to serve over the web, while allowing fast decompression even on mobile devices.

## Final Work Product

Starting version 2.10.2, FreeType now supports recognition, decompression and handling of WOFF2 fonts.

Sources and mirrors to download FreeType can be found [here](https://www.freetype.org/download.html).

## Link to Code

FreeType Git Repository: http://git.savannah.gnu.org/cgit/freetype/freetype2.git
Working branch: http://git.savannah.gnu.org/cgit/freetype/freetype2.git/?h=GSoC-2019-nikhil
Full diff: http://git.savannah.gnu.org/cgit/freetype/freetype2.git/diff/?id=f67786d&id2=47ca5cb

## How I built it

* Study existing SFNT sources and create a roadmap ([Initial obersvations][goal-1]).
* Move the WOFF 1.0 sources and header definitions to separate files ([commit][goal-2]).
* Study the WOFF2 W3C Recommendation in detail ([WOFF2 Recommendation][goal-3]).
* Create `sfwoff2.c`, read and parse the WOFF2 header ([commit][goal-4]).
* Read WOFF2 table and collection directory ([commit][goal-5], [relevant discussion][goal-5.1]).
* Add Brotli support to build system and decompress the compressed WOFF2 stream ([commit 1][goal-6], [commit 2][goal-6.1]).
* In case input font is a TTC, optimize reconstruction using the `face_index` paramater ([commit][goal-7]).
* Reconstruct transformed `glyf` table ([steps][goal-8], [commit][goal-8.1]).
* Reconstruct transformed `loca` and `hmtx`, swap streams and load SFNT ([commit][goal-9]).
* Improve memory handling to avoid too many reallocations ([commit][goal-10]).
* Support hmtx reconstruction when glyf is untransformed ([commit][goal-11], [relevant issue][goal-11.1]).


[goal-1]: https://lists.nongnu.org/archive/html/freetype-devel/2019-03/msg00052.html
[goal-2]: http://git.savannah.gnu.org/cgit/freetype/freetype2.git/commit/?id=7b275a5af161c71a169dcec65f2f6998c9f2d6d5
[goal-3]: http://w3.org/TR/WOFF2
[goal-4]: http://git.savannah.gnu.org/cgit/freetype/freetype2.git/commit/?h=GSoC-2019-nikhil&id=1b7f95bf708b622334521a210f145ddcc7aa5349
[goal-5]: http://git.savannah.gnu.org/cgit/freetype/freetype2.git/commit/?h=GSoC-2019-nikhil&id=e6350530458572e16afff8836c1c1ee71038cb63
[goal-5.1]: https://lists.nongnu.org/archive/html/freetype-devel/2019-06/msg00018.html
[goal-6]: http://git.savannah.gnu.org/cgit/freetype/freetype2.git/commit/?h=GSoC-2019-nikhil&id=8e05ab3e2ebf9d90b6015cb2aa7e789517b293e6
[goal-6.1]: http://git.savannah.gnu.org/cgit/freetype/freetype2.git/commit/?h=GSoC-2019-nikhil&id=f6c4d58ec8340051051311ef4781089a9e56d17c
[goal-7]: http://git.savannah.gnu.org/cgit/freetype/freetype2.git/commit/?h=GSoC-2019-nikhil&id=0d0bd49be5d0b77ccdc873201c98d490b53c2dbe
[goal-8]: https://www.w3.org/TR/WOFF2/#glyf_table_format
[goal-8.1]: http://git.savannah.gnu.org/cgit/freetype/freetype2.git/commit/?h=GSoC-2019-nikhil&id=f5a1a74c8d70105e4741d012231d33a7bb55f2a3
[goal-9]: http://git.savannah.gnu.org/cgit/freetype/freetype2.git/commit/?h=GSoC-2019-nikhil&id=b9414b4dc662202d1bba9097979f640a353788b9
[goal-10]: http://git.savannah.gnu.org/cgit/freetype/freetype2.git/commit/?h=GSoC-2019-nikhil&id=befd05c0603f70bc89fceabf5426532691c32a50
[goal-11]: http://git.savannah.gnu.org/cgit/freetype/freetype2.git/commit/?h=GSoC-2019-nikhil&id=73dc5ef018695a011ce22cacc344d1db1639546d
[goal-11.1]: https://github.com/google/woff2/issues/124


## Additional Resources

**Project Proposal**:
<https://docs.google.com/document/d/1JETYduhFgrmhRgUMFo4HjwmUp0IMyh3UZsQbmwAmjjI/edit?usp=sharing>

**GSoC Project Link**:
<https://summerofcode.withgoogle.com/projects/#5914661667471360>

## Final Note

It has been an extremely fun journey learning, building and hacking over the last few years. Now that we are graduating virtually, let's make this the biggest collective graduation event in history! Congratulations to everyone!