_a_v_a_r: use floatToFixedToStr and strToFixedToFloat in toXML/fromXML methods

This commit is contained in:
Cosimo Lupo 2019-10-07 15:41:06 +01:00
parent d66c92fbe0
commit 8791caf3b8
No known key found for this signature in database
GPG Key ID: 20D4A261E4A0E642
2 changed files with 31 additions and 15 deletions

View File

@ -1,7 +1,12 @@
from fontTools.misc.py23 import *
from fontTools import ttLib
from fontTools.misc import sstruct
from fontTools.misc.fixedTools import fixedToFloat, floatToFixed
from fontTools.misc.fixedTools import (
fixedToFloat as fi2fl,
floatToFixed as fl2fi,
floatToFixedToStr as fl2str,
strToFixedToFloat as str2fl,
)
from fontTools.misc.textTools import safeEval
from fontTools.ttLib import TTLibError
from . import DefaultTable
@ -46,8 +51,8 @@ class table__a_v_a_r(DefaultTable.DefaultTable):
mappings = sorted(self.segments[axis].items())
result.append(struct.pack(">H", len(mappings)))
for key, value in mappings:
fixedKey = floatToFixed(key, 14)
fixedValue = floatToFixed(value, 14)
fixedKey = fl2fi(key, 14)
fixedValue = fl2fi(value, 14)
result.append(struct.pack(">hh", fixedKey, fixedValue))
return bytesjoin(result)
@ -66,7 +71,7 @@ class table__a_v_a_r(DefaultTable.DefaultTable):
pos = pos + 2
for _ in range(numPairs):
fromValue, toValue = struct.unpack(">hh", data[pos:pos+4])
segments[fixedToFloat(fromValue, 14)] = fixedToFloat(toValue, 14)
segments[fi2fl(fromValue, 14)] = fi2fl(toValue, 14)
pos = pos + 4
def toXML(self, writer, ttFont):
@ -75,10 +80,8 @@ class table__a_v_a_r(DefaultTable.DefaultTable):
writer.begintag("segment", axis=axis)
writer.newline()
for key, value in sorted(self.segments[axis].items()):
# roundtrip float -> fixed -> float to normalize TTX output
# as dumped after decompiling or straight from varLib
key = fixedToFloat(floatToFixed(key, 14), 14)
value = fixedToFloat(floatToFixed(value, 14), 14)
key = fl2str(key, 14)
value = fl2str(value, 14)
writer.simpletag("mapping", **{"from": key, "to": value})
writer.newline()
writer.endtag("segment")
@ -92,8 +95,8 @@ class table__a_v_a_r(DefaultTable.DefaultTable):
if isinstance(element, tuple):
elementName, elementAttrs, _ = element
if elementName == "mapping":
fromValue = safeEval(elementAttrs["from"])
toValue = safeEval(elementAttrs["to"])
fromValue = str2fl(elementAttrs["from"], 14)
toValue = str2fl(elementAttrs["to"], 14)
if fromValue in segment:
log.warning("duplicate entry for %s in axis '%s'",
fromValue, axis)

View File

@ -17,6 +17,17 @@ TEST_DATA = deHexStr(
class AxisVariationTableTest(unittest.TestCase):
def assertAvarAlmostEqual(self, segments1, segments2):
self.assertSetEqual(set(segments1.keys()), set(segments2.keys()))
for axisTag, mapping1 in segments1.items():
mapping2 = segments2[axisTag]
self.assertEqual(len(mapping1), len(mapping2))
for (k1, v1), (k2, v2) in zip(
sorted(mapping1.items()), sorted(mapping2.items())
):
self.assertAlmostEqual(k1, k2)
self.assertAlmostEqual(v1, v2)
def test_compile(self):
avar = table__a_v_a_r()
avar.segments["wdth"] = {-1.0: -1.0, 0.0: 0.0, 0.3: 0.8, 1.0: 1.0}
@ -26,8 +37,8 @@ class AxisVariationTableTest(unittest.TestCase):
def test_decompile(self):
avar = table__a_v_a_r()
avar.decompile(TEST_DATA, self.makeFont(["wdth", "wght"]))
self.assertEqual({
"wdth": {-1.0: -1.0, 0.0: 0.0, 0.3: 0.8, 1.0: 1.0},
self.assertAvarAlmostEqual({
"wdth": {-1.0: -1.0, 0.0: 0.0, 0.2999878: 0.7999878, 1.0: 1.0},
"wght": {-1.0: -1.0, 0.0: 0.0, 1.0: 1.0}
}, avar.segments)
@ -38,7 +49,7 @@ class AxisVariationTableTest(unittest.TestCase):
def test_toXML(self):
avar = table__a_v_a_r()
avar.segments["opsz"] = {-1.0: -1.0, 0.0: 0.0, 0.3: 0.8, 1.0: 1.0}
avar.segments["opsz"] = {-1.0: -1.0, 0.0: 0.0, 0.2999878: 0.7999878, 1.0: 1.0}
writer = XMLWriter(BytesIO())
avar.toXML(writer, self.makeFont(["opsz"]))
self.assertEqual([
@ -60,8 +71,10 @@ class AxisVariationTableTest(unittest.TestCase):
' <mapping from="1.0" to="1.0"/>'
'</segment>'):
avar.fromXML(name, attrs, content, ttFont=None)
self.assertEqual({"wdth": {-1: -1, 0: 0, 0.7: 0.2, 1.0: 1.0}},
avar.segments)
self.assertAvarAlmostEqual(
{"wdth": {-1: -1, 0: 0, 0.7000122: 0.2000122, 1.0: 1.0}},
avar.segments
)
@staticmethod
def makeFont(axisTags):