Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compress/Deflate @font-face fonts

I am using the @font-face CSS property to load a special font for headings on a website, and I want Apache to send the font compressed to reduce loading time.

However, the "AddOutputFilterByType" declaration in Apache 2's deflate module only accepts mime types and, as AFAIK, neither opentype nor truetype have registered mime types.

So, how do I configure Apache to deflate/compress font.otf or font.ttf files?

like image 373
Torrance Avatar asked Oct 14 '09 01:10

Torrance


People also ask

Can fonts be compressed?

Fonts in general should be compressed, but not always. TrueType fonts ( ttf ) and OpenType fonts ( otf ) should be compressed, but Web Open Font Format ( woff ) fonts should not be compressed (they're already compressed).

Can WOFF2 files be compressed?

Both WOFF and WOFF 2.0 have built-in compression.

How do I compress a WOFF font?

You can compress . woff font files with https://github.com/hn/woff-compress. It uses zlib's Z_BEST_COMPRESSION flag or Zopfli to losslessly shrink files down to a minimum. If you want to lossy reduce files even more, you can remove unneeded glyphs from the font as discussed in Way to reduce size of .


1 Answers

Came up with a solution:

Use Apache's AddType declaration to add a custom mime type and then use that mime type in the AddOutputFilterByType declaration.

For example, for opentype and truetype fonts:

Addtype font/opentype .otf
Addtype font/truetype .ttf
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css font/opentype font/truetype

Both these declarations require their appropriate modules to be active: mod_mime and mod_deflate. And for best practice, the AddType declaration should be in the mime.conf file, and the AddOutputFilterByType declaration should be in the deflate.conf file. See your particular distribution's help files for enabling and configuring Apache modules.

like image 153
Torrance Avatar answered Oct 17 '22 23:10

Torrance