• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1from __future__ import \
2    print_function, division, absolute_import, unicode_literals
3from fontTools.misc.py23 import *
4from . import DefaultTable
5from fontTools.misc import sstruct
6from fontTools.ttLib.tables.TupleVariation import \
7    compileTupleVariationStore, decompileTupleVariationStore, TupleVariation
8
9
10# https://www.microsoft.com/typography/otspec/cvar.htm
11# https://www.microsoft.com/typography/otspec/otvarcommonformats.htm
12# https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6cvar.html
13
14CVAR_HEADER_FORMAT = """
15    > # big endian
16    majorVersion:        H
17    minorVersion:        H
18    tupleVariationCount: H
19    offsetToData:        H
20"""
21
22CVAR_HEADER_SIZE = sstruct.calcsize(CVAR_HEADER_FORMAT)
23
24
25class table__c_v_a_r(DefaultTable.DefaultTable):
26    dependencies = ["cvt ", "fvar"]
27
28    def __init__(self, tag=None):
29        DefaultTable.DefaultTable.__init__(self, tag)
30        self.majorVersion, self.minorVersion = 1, 0
31        self.variations = []
32
33    def compile(self, ttFont, useSharedPoints=False):
34        tupleVariationCount, tuples, data = compileTupleVariationStore(
35            variations=[v for v in self.variations if v.hasImpact()],
36            pointCount=len(ttFont["cvt "].values),
37            axisTags=[axis.axisTag for axis in ttFont["fvar"].axes],
38            sharedTupleIndices={},
39            useSharedPoints=useSharedPoints)
40        header = {
41            "majorVersion": self.majorVersion,
42            "minorVersion": self.minorVersion,
43            "tupleVariationCount": tupleVariationCount,
44            "offsetToData": CVAR_HEADER_SIZE + len(tuples),
45        }
46        return bytesjoin([
47            sstruct.pack(CVAR_HEADER_FORMAT, header),
48            tuples,
49            data
50        ])
51
52    def decompile(self, data, ttFont):
53        axisTags = [axis.axisTag for axis in ttFont["fvar"].axes]
54        header = {}
55        sstruct.unpack(CVAR_HEADER_FORMAT, data[0:CVAR_HEADER_SIZE], header)
56        self.majorVersion = header["majorVersion"]
57        self.minorVersion = header["minorVersion"]
58        assert self.majorVersion == 1, self.majorVersion
59        self.variations = decompileTupleVariationStore(
60            tableTag=self.tableTag, axisTags=axisTags,
61            tupleVariationCount=header["tupleVariationCount"],
62            pointCount=len(ttFont["cvt "].values), sharedTuples=None,
63            data=data, pos=CVAR_HEADER_SIZE, dataPos=header["offsetToData"])
64
65    def fromXML(self, name, attrs, content, ttFont):
66        if name == "version":
67            self.majorVersion = int(attrs.get("major", "1"))
68            self.minorVersion = int(attrs.get("minor", "0"))
69        elif name == "tuple":
70            valueCount = len(ttFont["cvt "].values)
71            var = TupleVariation({}, [None] * valueCount)
72            self.variations.append(var)
73            for tupleElement in content:
74                if isinstance(tupleElement, tuple):
75                    tupleName, tupleAttrs, tupleContent = tupleElement
76                    var.fromXML(tupleName, tupleAttrs, tupleContent)
77
78    def toXML(self, writer, ttFont):
79        axisTags = [axis.axisTag for axis in ttFont["fvar"].axes]
80        writer.simpletag("version",
81                         major=self.majorVersion, minor=self.minorVersion)
82        writer.newline()
83        for var in self.variations:
84            var.toXML(writer, axisTags)
85