[otlLib] Return None for empty argument to buildAttachList()

This simplifies call sites when building GDEF tables.
Also, publicly expose the buildAttachPoint() function.

https://github.com/behdad/fonttools/issues/468#issuecomment-173006650
This commit is contained in:
Sascha Brawer 2016-01-19 23:25:47 +01:00
parent d1fd788af9
commit 1bb757ee3c
3 changed files with 18 additions and 5 deletions

View File

@ -162,9 +162,8 @@ class Builder(object):
def buildGDEF(self):
gdef = otTables.GDEF()
gdef.GlyphClassDef = self.buildGDEFGlyphClassDef_()
gdef.AttachList = (
gdef.AttachList = \
otl.buildAttachList(self.attachPoints_, self.glyphMap)
if self.attachPoints_ else None)
gdef.LigCaretList = self.buildGDEFLigCaretList_()
gdef.MarkAttachClassDef = self.buildGDEFMarkAttachClassDef_()
gdef.MarkGlyphSetsDef = self.buildGDEFMarkGlyphSetsDef_()

View File

@ -225,16 +225,18 @@ def buildValue(value):
# GDEF
def buildAttachList(attachPoints, glyphMap):
"""{"glyphName": [4, 23]} --> otTables.AttachList"""
"""{"glyphName": [4, 23]} --> otTables.AttachList, or None"""
if not attachPoints:
return None
self = ot.AttachList()
self.Coverage = buildCoverage(attachPoints.keys(), glyphMap)
self.AttachPoint = [_buildAttachPoint(attachPoints[g])
self.AttachPoint = [buildAttachPoint(attachPoints[g])
for g in self.Coverage.glyphs]
self.GlyphCount = len(self.AttachPoint)
return self
def _buildAttachPoint(points):
def buildAttachPoint(points):
"""[4, 23, 41] --> otTables.AttachPoint"""
self = ot.AttachPoint()
self.PointIndex = sorted(points)

View File

@ -75,6 +75,18 @@ class BuilderTest(unittest.TestCase):
' </AttachPoint>'
'</AttachList>')
def test_buildAttachList_empty(self):
self.assertIsNone(builder.buildAttachList({}, self.GLYPHMAP))
def test_buildAttachPoint(self):
attachPoint = builder.buildAttachPoint([7, 3])
self.assertEqual(getXML(attachPoint.toXML),
'<AttachPoint>'
' <!-- PointCount=2 -->'
' <PointIndex index="0" value="3"/>'
' <PointIndex index="1" value="7"/>'
'</AttachPoint>')
def test_buildCoverage(self):
cov = builder.buildCoverage({"two", "four"}, {"two": 2, "four": 4})
self.assertEqual(getXML(cov.toXML),