[morx] Preliminary code for writing AATLookups

Needs more work.
This commit is contained in:
Sascha Brawer 2017-06-07 18:12:29 +02:00
parent f188187b08
commit 57f01b27cb
3 changed files with 58 additions and 1 deletions

View File

@ -37,6 +37,7 @@ def parseXML(xmlSnippet):
class FakeFont:
def __init__(self, glyphs):
self.glyphOrder_ = glyphs
self.reverseGlyphOrderDict_ = {g:i for i,g in enumerate(glyphs)}
self.lazy = False
self.tables = {}
@ -50,7 +51,7 @@ class FakeFont:
return self.tables.get(tag, default)
def getGlyphID(self, name):
return self.glyphOrder_.index(name)
return self.reverseGlyphOrderDict_[name]
def getGlyphName(self, glyphID):
if glyphID < len(self.glyphOrder_):
@ -61,6 +62,9 @@ class FakeFont:
def getGlyphOrder(self):
return self.glyphOrder_
def getReverseGlyphMap(self):
return self.reverseGlyphOrderDict_
class TestXMLReader_(object):
def __init__(self):

View File

@ -566,6 +566,36 @@ class AATLookup(BaseConverter):
return {font.getGlyphName(k):font.getGlyphName(v)
for k, v in mapping.items() if k != v}
def write(self, writer, font, tableDict, value, repeatIndex=None):
glyphMap = font.getReverseGlyphMap()
binSrchHeaderSize = 10
formatSizes = {6: binSrchHeaderSize + len(value) * 4}
if glyphMap.keys() == value.keys():
formatSizes[0] = len(value) * 2
# TODO: Also implement format 2, 4, and 8.
bestFormat = min(formatSizes, key=formatSizes.get)
writeMethod = getattr(self, 'writeFormat%d' % bestFormat)
writer.writeUShort(bestFormat)
writeMethod(writer, font, value)
def writeFormat0(self, writer, font, value):
for glyph in font.getGlyphOrder():
writer.writeUShort(font.getGlyphID(value[glyph]))
def writeFormat6(self, writer, font, value):
writer.writeUShort(2) # unit size
writer.writeUShort(len(value)) # nUnits
# TODO: The following three are not correct yet.
writer.writeUShort(int.bit_length(len(value))) # searchRange
writer.writeUShort(int.bit_length(len(value))) # entrySelector
writer.writeUShort(int.bit_length(len(value))) # rangeShift
table = [(font.getGlyphID(key), font.getGlyphID(value))
for key, value in value.items()]
table.sort()
for key, value in table:
writer.writeUShort(key)
writer.writeUShort(value)
def readFormat0(self, reader, numGlyphs):
data = reader.readUShortArray(numGlyphs)
return {k:v for (k,v) in enumerate(data)}

View File

@ -250,6 +250,29 @@ class AATLookupTest(unittest.TestCase):
"unsupported lookup format: 9",
self.converter.read, reader, self.font, None)
def test_writeFormat0(self):
writer = OTTableWriter()
font = FakeFont(".notdef A B C".split())
self.converter.write(writer, font, {}, {
".notdef": ".notdef",
"A": "C",
"B": "C",
"C": "A"
})
self.assertEqual(writer.getData(), deHexStr("0000 0000 0003 0003 0001"))
def test_writeFormat6(self):
writer = OTTableWriter()
font = FakeFont(".notdef A B C".split())
self.converter.write(writer, font, {}, {
"A": "C",
"C": "B"
})
# TODO: The binSrchHeader entries have the wrong values.
self.assertEqual(writer.getData(),
deHexStr("0006 0002 0002 0002 0002 0002 "
"0001 0003 0003 0002"))
def test_xmlRead(self):
value = self.converter.xmlRead({}, [
("Substitution", {"in": "A", "out": "A.alt"}, []),