Relax the point data validation.

This commit is contained in:
Tal Leming 2018-06-11 21:44:50 -04:00
parent 62dba9b6fa
commit 4c75212ee6

View File

@ -1110,7 +1110,7 @@ def _buildOutlineContourFormat1(pen, contour, validate):
raise GlifLibError("Unknown attributes in contour element.") raise GlifLibError("Unknown attributes in contour element.")
pen.beginPath() pen.beginPath()
if len(contour): if len(contour):
_validateAndMassagePointStructures(contour, pointAttributesFormat1, openContourOffCurveLeniency=True) _validateAndMassagePointStructures(contour, pointAttributesFormat1, openContourOffCurveLeniency=True, validate=validate)
_buildOutlinePointsFormat1(pen, contour) _buildOutlinePointsFormat1(pen, contour)
pen.endPath() pen.endPath()
@ -1170,7 +1170,7 @@ def _buildOutlineContourFormat2(pen, contour, identifiers, validate):
pen.beginPath() pen.beginPath()
warn("The beginPath method needs an identifier kwarg. The contour's identifier value has been discarded.", DeprecationWarning) warn("The beginPath method needs an identifier kwarg. The contour's identifier value has been discarded.", DeprecationWarning)
if len(contour): if len(contour):
_validateAndMassagePointStructures(contour, pointAttributesFormat2) _validateAndMassagePointStructures(contour, pointAttributesFormat2, validate)
_buildOutlinePointsFormat2(pen, contour, identifiers, validate) _buildOutlinePointsFormat2(pen, contour, identifiers, validate)
pen.endPath() pen.endPath()
@ -1226,7 +1226,7 @@ def _buildOutlineComponentFormat2(pen, component, identifiers, validate):
# all formats # all formats
def _validateAndMassagePointStructures(contour, pointAttributes, openContourOffCurveLeniency=False): def _validateAndMassagePointStructures(contour, pointAttributes, openContourOffCurveLeniency=False, validate=False):
if not len(contour): if not len(contour):
return return
# store some data for later validation # store some data for later validation
@ -1237,22 +1237,23 @@ def _validateAndMassagePointStructures(contour, pointAttributes, openContourOffC
# not <point> # not <point>
if element.tag != "point": if element.tag != "point":
raise GlifLibError("Unknown child element (%s) of contour element." % element.tag) raise GlifLibError("Unknown child element (%s) of contour element." % element.tag)
# unknown attributes if validate:
for attr in element.attrib.keys(): # unknown attributes
if attr not in pointAttributes: for attr in element.attrib.keys():
raise GlifLibError("Unknown attribute in point element: %s" % attr) if attr not in pointAttributes:
# search for unknown children raise GlifLibError("Unknown attribute in point element: %s" % attr)
if len(element): # search for unknown children
raise GlifLibError("Unknown child elements in point element.") if len(element):
raise GlifLibError("Unknown child elements in point element.")
# x and y are required # x and y are required
for attr in ("x", "y"): for attr in ("x", "y"):
value = element.get(attr) value = element.get(attr)
if value is None: if validate and value is None:
raise GlifLibError("Required %s attribute is missing in point element." % attr) raise GlifLibError("Required %s attribute is missing in point element." % attr)
element.attrib[attr] = _number(value) element.attrib[attr] = _number(value)
# segment type # segment type
pointType = element.attrib.pop("type", "offcurve") pointType = element.attrib.pop("type", "offcurve")
if pointType not in pointTypeOptions: if validate and pointType not in pointTypeOptions:
raise GlifLibError("Unknown point type: %s" % pointType) raise GlifLibError("Unknown point type: %s" % pointType)
if pointType == "offcurve": if pointType == "offcurve":
pointType = None pointType = None
@ -1262,17 +1263,17 @@ def _validateAndMassagePointStructures(contour, pointAttributes, openContourOffC
else: else:
lastOnCurvePoint = index lastOnCurvePoint = index
# move can only occur as the first point # move can only occur as the first point
if pointType == "move" and index != 0: if validate and pointType == "move" and index != 0:
raise GlifLibError("A move point occurs after the first point in the contour.") raise GlifLibError("A move point occurs after the first point in the contour.")
# smooth is optional # smooth is optional
smooth = element.get("smooth", "no") smooth = element.get("smooth", "no")
if smooth is not None: if validate and smooth is not None:
if smooth not in pointSmoothOptions: if smooth not in pointSmoothOptions:
raise GlifLibError("Unknown point smooth value: %s" % smooth) raise GlifLibError("Unknown point smooth value: %s" % smooth)
smooth = smooth == "yes" smooth = smooth == "yes"
element.attrib["smooth"] = smooth element.attrib["smooth"] = smooth
# smooth can only be applied to curve and qcurve # smooth can only be applied to curve and qcurve
if smooth and pointType is None: if validate and smooth and pointType is None:
raise GlifLibError("smooth attribute set in an offcurve point.") raise GlifLibError("smooth attribute set in an offcurve point.")
# name is optional # name is optional
if "name" not in element.attrib: if "name" not in element.attrib:
@ -1287,7 +1288,7 @@ def _validateAndMassagePointStructures(contour, pointAttributes, openContourOffC
else: else:
break break
# validate the off-curves in the segments # validate the off-curves in the segments
if haveOffCurvePoint and lastOnCurvePoint is not None: if validate and haveOffCurvePoint and lastOnCurvePoint is not None:
# we only care about how many offCurves there are before an onCurve # we only care about how many offCurves there are before an onCurve
# filter out the trailing offCurves # filter out the trailing offCurves
offCurvesCount = len(contour) - 1 - lastOnCurvePoint offCurvesCount = len(contour) - 1 - lastOnCurvePoint