Umang Gupta
Umang Gupta

Reputation: 16500

Converting TTF to WOFF

I have been trying to convert TTF to WOFF using various command line and online tools. I have tried following :

Command line :

Online :

I have ttf file of size ~220KB. All these tools generate woff file of size around 100KB except for font-squirrel which generates a size optimized file of ~20KB.

I am curious as to what font-squirrel does which no other command-line (read open source) tool is able to do. And if possible how can I do the same via command-line, even if it involves writing or hacking some code.

Upvotes: 27

Views: 29891

Answers (5)

Carson
Carson

Reputation: 8138

TL;DR: font-squirrel is not lossless!

and as RoelN says:

Simply doing WOFF compression should yield very similar file sizes, regardless of tool used.


If you already know how to parse TTF files, then converting them to WOFF is not too difficult.

It mainly compresses the tables from the original ttf. If compression is needed for a table, zlib is used for compression.

Below is the structure of woff.

type Woff struct {
    Header    struct {
        Signature [4]byte
        Flavor [4]byte
        Length uint32
        NumTables uint16
        Reserved uint16
        TotalSFNTSize uint32
        MajorVersion uint16
        MinorVersion uint16
        MetaOffset     uint32
        MetaLength     uint32
        MetaOrigLength uint32
        PrivateOffset uint32
        PrivateLength uint32
    }
    TableDirectoryEntry []struct{
        Tag          [4]byte
        Offset       uint32
        CompLength   uint32
        OrigLength   uint32
        OrigChecksum uint32
    }
    TableRawData []byte
    MetaData []byte
    PrivateData []byte
}

For details, please refer to the documentation

https://www.w3.org/TR/WOFF/

Therefore, the content is limited. small differences, such as whether cmap.format4 should use idDelta or rangeOffset... (of course, there are many other reasons), so the results from different parsing tools may differ, but not significantly. (that is why WOFF compression should yield very similar file sizes, regardless of tool used.)

The main difference is that WOFF includes privateData and metaData.

If there is a lot of data in these areas of the woff file, the resulting data will be relatively large.

So, you can check if these two optional areas have data. If there is no data in both areas but the resulting files are significantly different, then it cannot be lossless.


I indeed tested the results from font-squirrel with test data and compared them with the results I parsed myself.

The glyf table of the font occupies a large space because the point information of each outline is recorded here.

So, you can start from here.

However, glyf corresponds to loca.

Therefore, you can check if the number of loca is correct.

You will find that it writes much fewer loca,

so the content of glyf can also be written much less, resulting in a much smaller file size.

Upvotes: 1

Michael Rafailyk
Michael Rafailyk

Reputation: 462

The answer can be in some tables that the compressor may ignore/remove. For example, the font may contain two different tables for kerning – the main one and the legacy one. So the legacy kerning table may be removed during the compression. It save a lot of space.

Upvotes: 0

Marcos Buarque
Marcos Buarque

Reputation: 3418

You can use the Fontdrop website to compare the original font set against the new one generated by FontSquirrel. In my case, the converted font set had support for 64 languages in comparison to 99 languages on the original file. Also, the converted version had fewer features than the original.

The information provided by Fontdrop will probably help you make a decision if the converted set from FontSquirrel is enough for you or if you should try another conversion tool.

Upvotes: 4

Reinhard Kotucha
Reinhard Kotucha

Reputation: 131

I'm using Jonathan Kew's sfnt2woff program and am amazed to see how small the .woff files are.

I couldn't believe that compression is lossless. Thus I tried:

sfnt2woff CharisSIL-R.ttf
woff2sfnt CharisSIL-R.woff > CharisSIL-R_converted.ttf 
cmp CharisSIL-R.ttf CharisSIL-R_converted.ttf
echo $?

according to cmp(1) there is no difference between the font converted to woff and back to ttf and the original ttf file.

Upvotes: 12

RoelN
RoelN

Reputation: 2321

Simply doing WOFF compression should yield very similar file sizes, regardless of tool used.

I expect Font Squirrel is doing more to account for that extra 80kB savings, like stripping hints, dropping OpenType features like small caps, or subsetting to support only western languages.

You could use TTX/FontTools to inspect the files before and after conversion, and see what's changed.

Upvotes: 11

Related Questions