TupleVariation: use floatToFixedToStr and strToFixedToFloat in toXML/fromXML methods

This commit is contained in:
Cosimo Lupo 2019-10-07 13:52:35 +01:00
parent 8500dfca71
commit 79d654eaf2
No known key found for this signature in database
GPG Key ID: 20D4A261E4A0E642

View File

@ -1,5 +1,11 @@
from fontTools.misc.py23 import *
from fontTools.misc.fixedTools import fixedToFloat, floatToFixed, otRound
from fontTools.misc.fixedTools import (
fixedToFloat as fi2fl,
floatToFixed as fl2fi,
floatToFixedToStr as fl2str,
strToFixedToFloat as str2fl,
otRound,
)
from fontTools.misc.textTools import safeEval
import array
import io
@ -62,17 +68,17 @@ class TupleVariation(object):
for axis in axisTags:
value = self.axes.get(axis)
if value is not None:
minValue, value, maxValue = (float(v) for v in value)
minValue, value, maxValue = value
defaultMinValue = min(value, 0.0) # -0.3 --> -0.3; 0.7 --> 0.0
defaultMaxValue = max(value, 0.0) # -0.3 --> 0.0; 0.7 --> 0.7
if minValue == defaultMinValue and maxValue == defaultMaxValue:
writer.simpletag("coord", axis=axis, value=value)
writer.simpletag("coord", axis=axis, value=fl2str(value, 14))
else:
attrs = [
("axis", axis),
("min", minValue),
("value", value),
("max", maxValue),
("min", fl2str(minValue, 14)),
("value", fl2str(value, 14)),
("max", fl2str(maxValue, 14)),
]
writer.simpletag("coord", attrs)
writer.newline()
@ -100,11 +106,11 @@ class TupleVariation(object):
def fromXML(self, name, attrs, _content):
if name == "coord":
axis = attrs["axis"]
value = float(attrs["value"])
value = str2fl(attrs["value"], 14)
defaultMinValue = min(value, 0.0) # -0.3 --> -0.3; 0.7 --> 0.0
defaultMaxValue = max(value, 0.0) # -0.3 --> 0.0; 0.7 --> 0.7
minValue = float(attrs.get("min", defaultMinValue))
maxValue = float(attrs.get("max", defaultMaxValue))
minValue = str2fl(attrs.get("min", defaultMinValue), 14)
maxValue = str2fl(attrs.get("max", defaultMaxValue), 14)
self.axes[axis] = (minValue, value, maxValue)
elif name == "delta":
if "pt" in attrs:
@ -155,7 +161,7 @@ class TupleVariation(object):
result = []
for axis in axisTags:
_minValue, value, _maxValue = self.axes.get(axis, (0.0, 0.0, 0.0))
result.append(struct.pack(">h", floatToFixed(value, 14)))
result.append(struct.pack(">h", fl2fi(value, 14)))
return bytesjoin(result)
def compileIntermediateCoord(self, axisTags):
@ -173,8 +179,8 @@ class TupleVariation(object):
maxCoords = []
for axis in axisTags:
minValue, value, maxValue = self.axes.get(axis, (0.0, 0.0, 0.0))
minCoords.append(struct.pack(">h", floatToFixed(minValue, 14)))
maxCoords.append(struct.pack(">h", floatToFixed(maxValue, 14)))
minCoords.append(struct.pack(">h", fl2fi(minValue, 14)))
maxCoords.append(struct.pack(">h", fl2fi(maxValue, 14)))
return bytesjoin(minCoords + maxCoords)
@staticmethod
@ -182,7 +188,7 @@ class TupleVariation(object):
coord = {}
pos = offset
for axis in axisTags:
coord[axis] = fixedToFloat(struct.unpack(">h", data[pos:pos+2])[0], 14)
coord[axis] = fi2fl(struct.unpack(">h", data[pos:pos+2])[0], 14)
pos += 2
return coord, pos