Special handling of anchors in GLIF 1. This was never part of the spec, but all glifLib uses that I know of did things this way.

git-svn-id: http://svn.robofab.com/branches/ufo3k@406 b5fa9d6c-a76f-4ffd-b3cb-f825fc41095c
This commit is contained in:
Tal Leming 2011-10-17 13:29:07 +00:00
parent a2895df22f
commit 08732962b4
2 changed files with 49 additions and 0 deletions

View File

@ -946,17 +946,34 @@ pointSmoothOptions = set(("no", "yes"))
pointTypeOptions = set(["move", "line", "offcurve", "curve", "qcurve"]) pointTypeOptions = set(["move", "line", "offcurve", "curve", "qcurve"])
def buildOutline(pen, xmlNodes, formatVersion, identifiers): def buildOutline(pen, xmlNodes, formatVersion, identifiers):
anchors = []
for node in xmlNodes: for node in xmlNodes:
if len(node) != 3: if len(node) != 3:
raise GlifLibError("The outline element is not properly structured.") raise GlifLibError("The outline element is not properly structured.")
element, attrs, children = node element, attrs, children = node
if element == "contour": if element == "contour":
# special handling of implied anchors in GLIF 1
if formatVersion == 1 and len(children) == 1:
child = children[0]
if len(child) != 3:
raise GlifLibError("The outline element is not properly structured.")
if child[0] == "point":
anchor = _buildAnchorFormat1(child[1])
if anchor is not None:
anchors.append(anchor)
continue
_buildOutlineContour(pen, (attrs, children), formatVersion, identifiers) _buildOutlineContour(pen, (attrs, children), formatVersion, identifiers)
elif element == "component": elif element == "component":
_buildOutlineComponent(pen, (attrs, children), formatVersion, identifiers) _buildOutlineComponent(pen, (attrs, children), formatVersion, identifiers)
else: else:
raise GlifLibError("Unknown element in outline element: %s" % element) raise GlifLibError("Unknown element in outline element: %s" % element)
def _buildAnchorFormat1(point):
if point.get("type") != "move":
return None
anchor = dict(x=point.get("x"), y=point.get("y"), name=point.get("name"))
return anchor
def _buildOutlineContour(pen, (attrs, children), formatVersion, identifiers): def _buildOutlineContour(pen, (attrs, children), formatVersion, identifiers):
# search for unknown attributes # search for unknown attributes
if set(attrs.keys()) - contourAttributes: if set(attrs.keys()) - contourAttributes:

View File

@ -473,6 +473,7 @@ class TestGLIF1(unittest.TestCase):
<outline> <outline>
<contour> <contour>
<point x="1" y="2" type="move"/> <point x="1" y="2" type="move"/>
<point x="10" y="20" type="line"/>
</contour> </contour>
<contour> <contour>
<point x="1" y="2" type="move"/> <point x="1" y="2" type="move"/>
@ -485,6 +486,7 @@ class TestGLIF1(unittest.TestCase):
glyph.name = "a" glyph.name = "a"
pointPen.beginPath() pointPen.beginPath()
pointPen.addPoint(*[(1, 2)], **{"segmentType" : "move", "smooth" : False}) pointPen.addPoint(*[(1, 2)], **{"segmentType" : "move", "smooth" : False})
pointPen.addPoint(*[(10, 20)], **{"segmentType" : "line", "smooth" : False})
pointPen.endPath() pointPen.endPath()
pointPen.beginPath() pointPen.beginPath()
pointPen.addPoint(*[(1, 2)], **{"segmentType" : "move", "smooth" : False}) pointPen.addPoint(*[(1, 2)], **{"segmentType" : "move", "smooth" : False})
@ -514,6 +516,7 @@ class TestGLIF1(unittest.TestCase):
<outline> <outline>
<contour> <contour>
<point x="1" y="-2" type="move"/> <point x="1" y="-2" type="move"/>
<point x="0" y="0" type="line" name="this is here so that the contour isn't seen as an anchor"/>
</contour> </contour>
</outline> </outline>
</glyph> </glyph>
@ -522,6 +525,7 @@ class TestGLIF1(unittest.TestCase):
glyph.name = "a" glyph.name = "a"
pointPen.beginPath() pointPen.beginPath()
pointPen.addPoint(*[(1, -2)], **{"segmentType" : "move", "smooth" : False}) pointPen.addPoint(*[(1, -2)], **{"segmentType" : "move", "smooth" : False})
pointPen.addPoint(*[(0, 0)], **{"name" : "this is here so that the contour isn't seen as an anchor", "segmentType" : "line", "smooth" : False})
pointPen.endPath() pointPen.endPath()
""" """
resultGlif = self.pyToGLIF(py) resultGlif = self.pyToGLIF(py)
@ -534,6 +538,7 @@ class TestGLIF1(unittest.TestCase):
<outline> <outline>
<contour> <contour>
<point x="1.1" y="-2.2" type="move"/> <point x="1.1" y="-2.2" type="move"/>
<point x="0" y="0" type="line" name="this is here so that the contour isn't seen as an anchor"/>
</contour> </contour>
</outline> </outline>
</glyph> </glyph>
@ -542,6 +547,7 @@ class TestGLIF1(unittest.TestCase):
glyph.name = "a" glyph.name = "a"
pointPen.beginPath() pointPen.beginPath()
pointPen.addPoint(*[(1.1, -2.2)], **{"segmentType" : "move", "smooth" : False}) pointPen.addPoint(*[(1.1, -2.2)], **{"segmentType" : "move", "smooth" : False})
pointPen.addPoint(*[(0, 0)], **{"name" : "this is here so that the contour isn't seen as an anchor", "segmentType" : "line", "smooth" : False})
pointPen.endPath() pointPen.endPath()
""" """
resultGlif = self.pyToGLIF(py) resultGlif = self.pyToGLIF(py)
@ -554,6 +560,7 @@ class TestGLIF1(unittest.TestCase):
<outline> <outline>
<contour> <contour>
<point x="a" y="2" type="move"/> <point x="a" y="2" type="move"/>
<point x="0" y="0" type="line" name="this is here so that the contour isn't seen as an anchor"/>
</contour> </contour>
</outline> </outline>
</glyph> </glyph>
@ -562,6 +569,7 @@ class TestGLIF1(unittest.TestCase):
glyph.name = "a" glyph.name = "a"
pointPen.beginPath() pointPen.beginPath()
pointPen.addPoint(*[("a", 2)], **{"segmentType" : "move", "smooth" : False}) pointPen.addPoint(*[("a", 2)], **{"segmentType" : "move", "smooth" : False})
pointPen.addPoint(*[(0, 0)], **{"name" : "this is here so that the contour isn't seen as an anchor", "segmentType" : "line", "smooth" : False})
pointPen.endPath() pointPen.endPath()
""" """
self.assertRaises(GlifLibError, self.pyToGLIF, py) self.assertRaises(GlifLibError, self.pyToGLIF, py)
@ -572,6 +580,7 @@ class TestGLIF1(unittest.TestCase):
<outline> <outline>
<contour> <contour>
<point x="1" y="a" type="move"/> <point x="1" y="a" type="move"/>
<point x="0" y="0" type="line" name="this is here so that the contour isn't seen as an anchor"/>
</contour> </contour>
</outline> </outline>
</glyph> </glyph>
@ -580,6 +589,7 @@ class TestGLIF1(unittest.TestCase):
glyph.name = "a" glyph.name = "a"
pointPen.beginPath() pointPen.beginPath()
pointPen.addPoint(*[(1, "a")], **{"segmentType" : "move", "smooth" : False}) pointPen.addPoint(*[(1, "a")], **{"segmentType" : "move", "smooth" : False})
pointPen.addPoint(*[(0, 0)], **{"name" : "this is here so that the contour isn't seen as an anchor", "segmentType" : "line", "smooth" : False})
pointPen.endPath() pointPen.endPath()
""" """
self.assertRaises(GlifLibError, self.pyToGLIF, py) self.assertRaises(GlifLibError, self.pyToGLIF, py)
@ -1157,6 +1167,28 @@ class TestGLIF1(unittest.TestCase):
self.assertRaises(GlifLibError, self.pyToGLIF, py) self.assertRaises(GlifLibError, self.pyToGLIF, py)
self.assertRaises(GlifLibError, self.glifToPy, glif) self.assertRaises(GlifLibError, self.glifToPy, glif)
def testAnchor(self):
# legal
glif = """
<glyph name="a" format="1">
<outline>
<contour>
<point x="1" y="-2" type="move"/>
</contour>
</outline>
</glyph>
"""
#py = """
#glyph.name = "a"
#pointPen.beginPath()
#pointPen.anchors = [{"x" : 1, "y" : 2}]
#pointPen.endPath()
#"""
#resultGlif = self.pyToGLIF(py)
resultPy = self.glifToPy(glif)
#self.assertEqual(glif, resultGlif)
#self.assertEqual(py, resultPy)
if __name__ == "__main__": if __name__ == "__main__":
from robofab.test.testSupport import runTests from robofab.test.testSupport import runTests