2015-06-20 00:13:01 +02:00
|
|
|
from fontTools.misc import sstruct
|
2019-10-07 15:41:06 +01:00
|
|
|
from fontTools.misc.fixedTools import (
|
|
|
|
fixedToFloat as fi2fl,
|
|
|
|
floatToFixed as fl2fi,
|
|
|
|
floatToFixedToStr as fl2str,
|
|
|
|
strToFixedToFloat as str2fl,
|
|
|
|
)
|
2021-08-20 00:45:43 +02:00
|
|
|
from fontTools.misc.textTools import bytesjoin
|
2015-06-20 00:13:01 +02:00
|
|
|
from fontTools.ttLib import TTLibError
|
|
|
|
from . import DefaultTable
|
|
|
|
import struct
|
2016-01-24 14:35:34 +00:00
|
|
|
import logging
|
2015-06-20 00:13:01 +02:00
|
|
|
|
|
|
|
|
2016-01-24 14:35:34 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2015-06-20 00:13:01 +02:00
|
|
|
# Apple's documentation of 'avar':
|
|
|
|
# https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6avar.html
|
|
|
|
|
|
|
|
AVAR_HEADER_FORMAT = """
|
|
|
|
> # big endian
|
2017-01-13 15:46:20 +01:00
|
|
|
majorVersion: H
|
|
|
|
minorVersion: H
|
|
|
|
reserved: H
|
|
|
|
axisCount: H
|
2015-06-20 00:13:01 +02:00
|
|
|
"""
|
2017-01-13 15:46:20 +01:00
|
|
|
assert sstruct.calcsize(AVAR_HEADER_FORMAT) == 8, sstruct.calcsize(AVAR_HEADER_FORMAT)
|
2015-06-20 00:13:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
class table__a_v_a_r(DefaultTable.DefaultTable):
|
2021-11-18 09:01:27 +00:00
|
|
|
"""Axis Variations Table
|
|
|
|
|
|
|
|
This class represents the ``avar`` table of a variable font. The object has one
|
|
|
|
substantive attribute, ``segments``, which maps axis tags to a segments dictionary::
|
|
|
|
|
|
|
|
>>> font["avar"].segments # doctest: +SKIP
|
|
|
|
{'wght': {-1.0: -1.0,
|
|
|
|
0.0: 0.0,
|
|
|
|
0.125: 0.11444091796875,
|
|
|
|
0.25: 0.23492431640625,
|
|
|
|
0.5: 0.35540771484375,
|
|
|
|
0.625: 0.5,
|
|
|
|
0.75: 0.6566162109375,
|
|
|
|
0.875: 0.81927490234375,
|
|
|
|
1.0: 1.0},
|
|
|
|
'ital': {-1.0: -1.0, 0.0: 0.0, 1.0: 1.0}}
|
|
|
|
|
|
|
|
Notice that the segments dictionary is made up of normalized values. A valid
|
|
|
|
``avar`` segment mapping must contain the entries ``-1.0: -1.0, 0.0: 0.0, 1.0: 1.0``.
|
|
|
|
fontTools does not enforce this, so it is your responsibility to ensure that
|
|
|
|
mappings are valid.
|
|
|
|
"""
|
2016-01-24 14:35:34 +00:00
|
|
|
|
2015-06-20 00:13:01 +02:00
|
|
|
dependencies = ["fvar"]
|
|
|
|
|
|
|
|
def __init__(self, tag=None):
|
|
|
|
DefaultTable.DefaultTable.__init__(self, tag)
|
|
|
|
self.segments = {}
|
|
|
|
|
|
|
|
def compile(self, ttFont):
|
2015-06-23 22:04:41 +02:00
|
|
|
axisTags = [axis.axisTag for axis in ttFont["fvar"].axes]
|
2017-01-13 15:46:20 +01:00
|
|
|
header = {
|
|
|
|
"majorVersion": 1,
|
|
|
|
"minorVersion": 0,
|
|
|
|
"reserved": 0,
|
|
|
|
"axisCount": len(axisTags),
|
|
|
|
}
|
2015-06-20 00:13:01 +02:00
|
|
|
result = [sstruct.pack(AVAR_HEADER_FORMAT, header)]
|
|
|
|
for axis in axisTags:
|
|
|
|
mappings = sorted(self.segments[axis].items())
|
|
|
|
result.append(struct.pack(">H", len(mappings)))
|
|
|
|
for key, value in mappings:
|
2019-10-07 15:41:06 +01:00
|
|
|
fixedKey = fl2fi(key, 14)
|
|
|
|
fixedValue = fl2fi(value, 14)
|
2015-06-20 00:13:01 +02:00
|
|
|
result.append(struct.pack(">hh", fixedKey, fixedValue))
|
|
|
|
return bytesjoin(result)
|
|
|
|
|
|
|
|
def decompile(self, data, ttFont):
|
2015-06-23 22:04:41 +02:00
|
|
|
axisTags = [axis.axisTag for axis in ttFont["fvar"].axes]
|
2015-06-20 00:13:01 +02:00
|
|
|
header = {}
|
|
|
|
headerSize = sstruct.calcsize(AVAR_HEADER_FORMAT)
|
|
|
|
header = sstruct.unpack(AVAR_HEADER_FORMAT, data[0:headerSize])
|
2017-01-13 15:46:20 +01:00
|
|
|
majorVersion = header["majorVersion"]
|
|
|
|
if majorVersion != 1:
|
|
|
|
raise TTLibError("unsupported 'avar' version %d" % majorVersion)
|
2015-06-20 00:13:01 +02:00
|
|
|
pos = headerSize
|
|
|
|
for axis in axisTags:
|
|
|
|
segments = self.segments[axis] = {}
|
|
|
|
numPairs = struct.unpack(">H", data[pos : pos + 2])[0]
|
|
|
|
pos = pos + 2
|
|
|
|
for _ in range(numPairs):
|
|
|
|
fromValue, toValue = struct.unpack(">hh", data[pos : pos + 4])
|
2019-10-07 15:41:06 +01:00
|
|
|
segments[fi2fl(fromValue, 14)] = fi2fl(toValue, 14)
|
2015-06-20 00:13:01 +02:00
|
|
|
pos = pos + 4
|
|
|
|
|
2018-01-25 17:30:23 -08:00
|
|
|
def toXML(self, writer, ttFont):
|
2015-06-23 22:04:41 +02:00
|
|
|
axisTags = [axis.axisTag for axis in ttFont["fvar"].axes]
|
2015-06-20 00:13:01 +02:00
|
|
|
for axis in axisTags:
|
|
|
|
writer.begintag("segment", axis=axis)
|
|
|
|
writer.newline()
|
|
|
|
for key, value in sorted(self.segments[axis].items()):
|
2019-10-07 15:41:06 +01:00
|
|
|
key = fl2str(key, 14)
|
|
|
|
value = fl2str(value, 14)
|
2015-06-20 00:13:01 +02:00
|
|
|
writer.simpletag("mapping", **{"from": key, "to": value})
|
|
|
|
writer.newline()
|
|
|
|
writer.endtag("segment")
|
|
|
|
writer.newline()
|
|
|
|
|
|
|
|
def fromXML(self, name, attrs, content, ttFont):
|
|
|
|
if name == "segment":
|
|
|
|
axis = attrs["axis"]
|
|
|
|
segment = self.segments[axis] = {}
|
|
|
|
for element in content:
|
|
|
|
if isinstance(element, tuple):
|
2015-06-21 12:13:51 +02:00
|
|
|
elementName, elementAttrs, _ = element
|
2015-06-20 00:13:01 +02:00
|
|
|
if elementName == "mapping":
|
2019-10-07 15:41:06 +01:00
|
|
|
fromValue = str2fl(elementAttrs["from"], 14)
|
|
|
|
toValue = str2fl(elementAttrs["to"], 14)
|
2015-06-20 00:13:01 +02:00
|
|
|
if fromValue in segment:
|
2016-01-24 14:35:34 +00:00
|
|
|
log.warning(
|
|
|
|
"duplicate entry for %s in axis '%s'", fromValue, axis
|
|
|
|
)
|
2015-06-20 00:13:01 +02:00
|
|
|
segment[fromValue] = toValue
|