[GX] Leave 'gvar' coordinates unchanged when round-tripping through TTX

Before this change, a rounding issue in fixedToFloat() would sometimes
change 'gvar' coordinate values when round-tripping Skia.ttf through TTX.
This change works around https://github.com/behdad/fonttools/issues/286.
This commit is contained in:
Sascha Brawer 2015-06-08 16:49:28 +02:00
parent 9dd0a7e4cd
commit cd8af14991
2 changed files with 13 additions and 2 deletions

View File

@ -2,7 +2,7 @@ from __future__ import print_function, division, absolute_import
from fontTools.misc.py23 import * from fontTools.misc.py23 import *
from fontTools import ttLib from fontTools import ttLib
from fontTools.misc import sstruct from fontTools.misc import sstruct
from fontTools.misc.fixedTools import fixedToFloat, floatToFixed from fontTools.misc.fixedTools import floatToFixed
from fontTools.misc.textTools import safeEval from fontTools.misc.textTools import safeEval
from fontTools.ttLib import TTLibError from fontTools.ttLib import TTLibError
from fontTools.ttLib.tables._g_l_y_f import GlyphCoordinates from fontTools.ttLib.tables._g_l_y_f import GlyphCoordinates
@ -478,7 +478,10 @@ class GlyphVariation(object):
coord = {} coord = {}
pos = offset pos = offset
for axis in axisTags: for axis in axisTags:
coord[axis] = fixedToFloat(struct.unpack(">h", data[pos:pos+2])[0], 14) # Work around https://github.com/behdad/fonttools/issues/286
# coord[axis] = fixedToFloat(struct.unpack(">h", data[pos:pos+2])[0], 14)
fixedValue = struct.unpack(">h", data[pos:pos+2])[0]
coord[axis] = float(fixedValue) / (1 << 14)
pos += 2 pos += 2
return coord, pos return coord, pos

View File

@ -352,6 +352,14 @@ class GlyphVariationTest(unittest.TestCase):
data = deHexStr("DE AD C0 00 20 00 DE AD") data = deHexStr("DE AD C0 00 20 00 DE AD")
self.assertEqual(({"wght": -1.0, "wdth": 0.5}, 6), decompileCoord(["wght", "wdth"], data, 2)) self.assertEqual(({"wght": -1.0, "wdth": 0.5}, 6), decompileCoord(["wght", "wdth"], data, 2))
def test_decompileCoord_roundTrip(self):
# Make sure we are not affected by https://github.com/behdad/fonttools/issues/286
data = deHexStr("7F B9 80 35")
values, _ = GlyphVariation.decompileCoord_(["wght", "wdth"], data, 0)
axisValues = dict([(axis, (val, val, val)) for axis, val in values.items()])
gvar = GlyphVariation(axisValues, GlyphCoordinates.zeros(4))
self.assertEqual("7F B9 80 35", hexencode(gvar.compileCoord(["wght", "wdth"])))
def test_decompileCoords(self): def test_decompileCoords(self):
decompileCoords = GlyphVariation.decompileCoords_ decompileCoords = GlyphVariation.decompileCoords_
axes = ["wght", "wdth", "opsz"] axes = ["wght", "wdth", "opsz"]