DEV Community

Nikhil Ramakrishnan
Nikhil Ramakrishnan

Posted on

My Favorite Open Source Project

Add Support for WOFF2 Fonts to FreeType

Over the Summer of 2019 I worked on The FreeType Project 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 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.

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).
  • Move the WOFF 1.0 sources and header definitions to separate files (commit).
  • Study the WOFF2 W3C Recommendation in detail (WOFF2 Recommendation).
  • Create sfwoff2.c, read and parse the WOFF2 header (commit).
  • Read WOFF2 table and collection directory (commit, relevant discussion).
  • Add Brotli support to build system and decompress the compressed WOFF2 stream (commit 1, commit 2).
  • In case input font is a TTC, optimize reconstruction using the face_index paramater (commit).
  • Reconstruct transformed glyf table (steps, commit).
  • Reconstruct transformed loca and hmtx, swap streams and load SFNT (commit).
  • Improve memory handling to avoid too many reallocations (commit).
  • Support hmtx reconstruction when glyf is untransformed (commit, relevant issue).

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!

Top comments (0)