googlefonts / fontdiffenator

Font comparison tool
Apache License 2.0
80 stars 13 forks source link

Compare OTFs #20

Open davelab6 opened 6 years ago

davelab6 commented 6 years ago

We only compare TTFs for now, not OTFs.

Example error when passing in OTFs:

$ diffenator ~/Downloads/NotoSansGrantha-Regular.otf ~/Downloads/mNotoSansGrantha-Regular.otf 

'diff_kerning'  8.96 ms

Traceback (most recent call last):

  File "/usr/local/bin/diffenator", line 11, in <module>

    load_entry_point('fontdiffenator==0.3.1', 'console_scripts', 'diffenator')()

  File "/usr/local/lib/python2.7/site-packages/diffenator/__main__.py", line 54, in main

    font_b,

  File "/usr/local/lib/python2.7/site-packages/diffenator/diff.py", line 126, in diff_fonts

    ('metrics', diff_metrics(font_a, font_b)),

  File "/usr/local/lib/python2.7/site-packages/diffenator/diff.py", line 101, in timed

    result = method(*args, **kw)

  File "/usr/local/lib/python2.7/site-packages/diffenator/diff.py", line 264, in diff_metrics

    metrics_a = dump_glyph_metrics(font_a)

  File "/usr/local/lib/python2.7/site-packages/diffenator/metrics.py", line 23, in dump_glyph_metrics

    return [glyph_metrics(font, g) for g in glyphs]

  File "/usr/local/lib/python2.7/site-packages/diffenator/metrics.py", line 8, in glyph_metrics

    lsb = _glyph_lsb(font, glyph)

  File "/usr/local/lib/python2.7/site-packages/diffenator/metrics.py", line 35, in _glyph_lsb

    return font['glyf'][glyph].xMin

  File "/usr/local/lib/python2.7/site-packages/fontTools/ttLib/ttFont.py", line 382, in __getitem__

    data = self.reader[tag]

  File "/usr/local/lib/python2.7/site-packages/fontTools/ttLib/sfnt.py", line 104, in __getitem__

    entry = self.tables[Tag(tag)]

KeyError: 'glyf'
marekjez86 commented 6 years ago

@davelab6 , @m4rc1e : I got the same error. Now I know why

anthrotype commented 6 years ago

the hmtx table has advance widths and left side bearings, works for both TTF and CFF-flavored OpenType. The _TTGlyph objects that you get with TTFont.getGlyphSet() already expose width and lsb attributes (from htmx) for both the OT flavors. https://github.com/fonttools/fonttools/blob/7d59b729ab996af6fe08411267eaad0bec1a2615/Lib/fontTools/ttLib/ttFont.py#L732

The right side bearings can be inferred from the advance width and the bounding box xMax coordinate (from glyf table). For CFF you need to compute the bounds, e.g. see https://github.com/fonttools/fonttools/blob/7d59b729ab996af6fe08411267eaad0bec1a2615/Lib/fontTools/ttLib/tables/_h_h_e_a.py#L66-L70

m4rc1e commented 6 years ago

Thanks for the pointers @anthrotype!

rsheeter commented 5 years ago

What needs to be done to support comparing OTF? Are we able to enumerate the tasks or do we currently have no idea?

m4rc1e commented 5 years ago

Off the top of my head, I can only think of:

I originally wrote this tool only with ttfs in mind. If we want otf support, I'll need to refactor some of the logic so it's more flexible. Luckily, most of it is kept inside the diffenator.font module.

I also don't know how well hb, freetype and fonttools.varLib support otf fonts. I need to check this.

When will otf support arrive in Google Fonts?

rsheeter commented 5 years ago

We support and ship OTFs today.

m4rc1e commented 5 years ago

We can now diff and dump otfs.

Still not sure how well it will work for CFF VFs so I'm going to leave this open.

marekjez86 commented 5 years ago

great news. @m4rc1e : thanks