[gvar] Rename gvar.decompileSharedCoords() to decompileSharedTuples()

The new name is consistent with the terminology of the OpenType 1.8
specification, which calls the decoded structure the `SharedTuples`
array.
This commit is contained in:
Sascha Brawer 2017-01-05 14:18:52 +01:00
parent 039cb48927
commit 980ee57b0e
2 changed files with 30 additions and 22 deletions

View File

@ -174,7 +174,8 @@ class table__g_v_a_r(DefaultTable.DefaultTable):
assert len(glyphs) == self.glyphCount assert len(glyphs) == self.glyphCount
assert len(axisTags) == self.axisCount assert len(axisTags) == self.axisCount
offsets = self.decompileOffsets_(data[GVAR_HEADER_SIZE:], tableFormat=(self.flags & 1), glyphCount=self.glyphCount) offsets = self.decompileOffsets_(data[GVAR_HEADER_SIZE:], tableFormat=(self.flags & 1), glyphCount=self.glyphCount)
sharedCoords = self.decompileSharedCoords_(axisTags, data) sharedCoords = decompileSharedTuples_(
axisTags, self.sharedTupleCount, data, self.offsetToSharedTuples)
self.variations = {} self.variations = {}
offsetToData = self.offsetToGlyphVariationData offsetToData = self.offsetToGlyphVariationData
for i in range(self.glyphCount): for i in range(self.glyphCount):
@ -185,10 +186,6 @@ class table__g_v_a_r(DefaultTable.DefaultTable):
self.variations[glyphName] = \ self.variations[glyphName] = \
self.decompileGlyph_(numPointsInGlyph, sharedCoords, axisTags, gvarData) self.decompileGlyph_(numPointsInGlyph, sharedCoords, axisTags, gvarData)
def decompileSharedCoords_(self, axisTags, data):
result, _pos = TupleVariation.decompileCoords_(axisTags, self.sharedTupleCount, data, self.offsetToSharedTuples)
return result
@staticmethod @staticmethod
def decompileOffsets_(data, tableFormat, glyphCount): def decompileOffsets_(data, tableFormat, glyphCount):
if tableFormat == 0: if tableFormat == 0:
@ -338,3 +335,11 @@ class table__g_v_a_r(DefaultTable.DefaultTable):
else: else:
# Empty glyphs (eg. space, nonmarkingreturn) have no "coordinates" attribute. # Empty glyphs (eg. space, nonmarkingreturn) have no "coordinates" attribute.
return len(getattr(glyph, "coordinates", [])) + NUM_PHANTOM_POINTS return len(getattr(glyph, "coordinates", [])) + NUM_PHANTOM_POINTS
def decompileSharedTuples_(axisTags, sharedTupleCount, data, offset):
result = []
for _ in range(sharedTupleCount):
t, offset = TupleVariation.decompileCoord_(axisTags, data, offset)
result.append(t)
return result

View File

@ -1,15 +1,20 @@
from __future__ import print_function, division, absolute_import, unicode_literals from __future__ import print_function, division, absolute_import, unicode_literals
from fontTools.misc.py23 import * from fontTools.misc.py23 import *
from fontTools.misc.textTools import deHexStr, hexStr from fontTools.misc.textTools import deHexStr, hexStr
from fontTools.ttLib import TTLibError from fontTools.ttLib import TTLibError, getTableModule
from fontTools.ttLib.tables.TupleVariation import TupleVariation from fontTools.ttLib.tables.TupleVariation import TupleVariation
from fontTools.ttLib.tables._g_v_a_r import table__g_v_a_r from fontTools.ttLib.tables._g_v_a_r import table__g_v_a_r
import unittest import unittest
gvar = getTableModule("gvar")
def hexencode(s): def hexencode(s):
h = hexStr(s).upper() h = hexStr(s).upper()
return ' '.join([h[i:i+2] for i in range(0, len(h), 2)]) return ' '.join([h[i:i+2] for i in range(0, len(h), 2)])
# Glyph variation table of uppercase I in the Skia font, as printed in Apple's # Glyph variation table of uppercase I in the Skia font, as printed in Apple's
# TrueType spec. The actual Skia font uses a different table for uppercase I. # TrueType spec. The actual Skia font uses a different table for uppercase I.
# https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6gvar.html # https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6gvar.html
@ -32,8 +37,8 @@ SKIA_GVAR_I = deHexStr(
"01 84 91 00 80 06 07 08 08 08 08 0A 07 80 03 FE " "01 84 91 00 80 06 07 08 08 08 08 0A 07 80 03 FE "
"FF FF FF 81 00 08 81 82 02 EE EE EE 8B 6D 00") "FF FF FF 81 00 08 81 82 02 EE EE EE 8B 6D 00")
# Shared coordinates in the Skia font, as printed in Apple's TrueType spec. # Shared tuples in the Skia font, as printed in Apple's TrueType spec.
SKIA_SHARED_COORDS = deHexStr( SKIA_SHARED_TUPLES = deHexStr(
"40 00 00 00 C0 00 00 00 00 00 40 00 00 00 C0 00 " "40 00 00 00 C0 00 00 00 00 00 40 00 00 00 C0 00 "
"C0 00 C0 00 40 00 C0 00 40 00 40 00 C0 00 40 00") "C0 00 C0 00 40 00 C0 00 40 00 40 00 C0 00 40 00")
@ -111,12 +116,11 @@ class GVARTableTest(unittest.TestCase):
result = table.compileSharedCoords_(["wght", "wdth"]) result = table.compileSharedCoords_(["wght", "wdth"])
self.assertEqual(["40 00 2C CD", "40 00 33 33"], [hexencode(c) for c in result]) self.assertEqual(["40 00 2C CD", "40 00 33 33"], [hexencode(c) for c in result])
def test_decompileSharedCoords_Skia(self): def test_decompileSharedTuples_Skia(self):
table = table__g_v_a_r() sharedCoords = gvar.decompileSharedTuples_(
table.offsetToSharedTuples = 0 axisTags=["wght", "wdth"], sharedTupleCount=8,
table.sharedTupleCount = 8 data=SKIA_SHARED_TUPLES, offset=0)
sharedCoords = table.decompileSharedCoords_(["wght", "wdth"], SKIA_SHARED_COORDS) self.assertEqual(sharedCoords, [
self.assertEqual([
{"wght": 1.0, "wdth": 0.0}, {"wght": 1.0, "wdth": 0.0},
{"wght": -1.0, "wdth": 0.0}, {"wght": -1.0, "wdth": 0.0},
{"wght": 0.0, "wdth": 1.0}, {"wght": 0.0, "wdth": 1.0},
@ -125,13 +129,10 @@ class GVARTableTest(unittest.TestCase):
{"wght": 1.0, "wdth": -1.0}, {"wght": 1.0, "wdth": -1.0},
{"wght": 1.0, "wdth": 1.0}, {"wght": 1.0, "wdth": 1.0},
{"wght": -1.0, "wdth": 1.0} {"wght": -1.0, "wdth": 1.0}
], sharedCoords) ])
def test_decompileSharedCoords_empty(self): def test_decompileSharedTuples_empty(self):
table = table__g_v_a_r() self.assertEqual(gvar.decompileSharedTuples_(["wght"], 0, b"", 0), [])
table.offsetToSharedTuples = 0
table.sharedTupleCount = 0
self.assertEqual([], table.decompileSharedCoords_(["wght"], b""))
def test_decompileGlyph_Skia_I(self): def test_decompileGlyph_Skia_I(self):
axes = ["wght", "wdth"] axes = ["wght", "wdth"]
@ -139,8 +140,10 @@ class GVARTableTest(unittest.TestCase):
table.offsetToSharedTuples = 0 table.offsetToSharedTuples = 0
table.sharedTupleCount = 8 table.sharedTupleCount = 8
table.axisCount = len(axes) table.axisCount = len(axes)
sharedCoords = table.decompileSharedCoords_(axes, SKIA_SHARED_COORDS) sharedTuples = gvar.decompileSharedTuples_(
tuples = table.decompileGlyph_(18, sharedCoords, axes, SKIA_GVAR_I) axisTags=axes, sharedTupleCount=8,
data=SKIA_SHARED_TUPLES, offset=0)
tuples = table.decompileGlyph_(18, sharedTuples, axes, SKIA_GVAR_I)
self.assertEqual(8, len(tuples)) self.assertEqual(8, len(tuples))
self.assertEqual({"wght": (0.0, 1.0, 1.0)}, tuples[0].axes) self.assertEqual({"wght": (0.0, 1.0, 1.0)}, tuples[0].axes)
self.assertEqual("257,0 -127,0 -128,58 -130,90 -130,62 -130,67 -130,32 -127,0 257,0 " self.assertEqual("257,0 -127,0 -128,58 -130,90 -130,62 -130,67 -130,32 -127,0 257,0 "