Converting TTF to WOFF
Asked Answered
T

5

27

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.

Tautomer answered 20/12, 2016 at 1:19 Comment(0)
G
12

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.

Gaberones answered 14/10, 2018 at 6:5 Comment(3)
This doesn't answer the question. The question is what is font-squirrel doing differentTautomer
@UmangGupta True, but you also get here, when just looking for "ttf to woff" and this answer shows how to do it.Allsopp
Install woff-tools on Ubuntu to get sfnt2woff.Allsopp
T
11

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.

Tancred answered 20/12, 2016 at 11:32 Comment(1)
I have used TTX/fonttools to convert ttf->ttx->woff, do we have any solution to convert directly ttf to woff by using ttx command?Paulpaula
I
4

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.

Invertase answered 25/2, 2021 at 19:59 Comment(0)
J
0

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.

Jahn answered 19/8, 2023 at 16:7 Comment(0)
F
0

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.

Fribble answered 9/5 at 8:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.