From 54ffe01f0dceb718fbdf6bc561e45538ed6c6012 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Dziedzic?= Date: Wed, 6 Jul 2022 17:07:24 +0200 Subject: [PATCH] ClosePath method to close contours --- Sources/UFOKit/GLIFPointPen.swift | 23 +++++++++++++++++++++++ Sources/UFOKit/UFOKit.swift | 1 + 2 files changed, 24 insertions(+) diff --git a/Sources/UFOKit/GLIFPointPen.swift b/Sources/UFOKit/GLIFPointPen.swift index 62757b4..c2f2fb6 100644 --- a/Sources/UFOKit/GLIFPointPen.swift +++ b/Sources/UFOKit/GLIFPointPen.swift @@ -25,6 +25,29 @@ public class GLIFPointPen: PointPen { prevOffCurveCount = 0 } + public func closePath() throws { + print ("Close Path") + if let currentContour = currentContour { + if firstSegmentType == .move && lastSegmentType == .offCurve { + throw UFOError.openContourEndsOffCurve + } + let length = currentContour.childCount + guard length != 0, + let first = currentContour.children?[0] as? XMLElement, + let last = currentContour.children? [length - 1] as? XMLElement, + first.name == "point", + last.name == "point", + let connectionType = last.attribute(forName: "type" ) + else { throw UFOError.pathTooShort } + first.attribute(forName: "type")?.stringValue = connectionType.stringValue + currentContour.removeChild(at: length - 1) + outlineElement.addChild(currentContour) + } else { + throw UFOError.pathNotBegun + } + currentContour = nil + } + public func endPath() throws { if let currentContour = currentContour { if firstSegmentType == .move && lastSegmentType == .offCurve { diff --git a/Sources/UFOKit/UFOKit.swift b/Sources/UFOKit/UFOKit.swift index 3bd92ef..b009056 100644 --- a/Sources/UFOKit/UFOKit.swift +++ b/Sources/UFOKit/UFOKit.swift @@ -55,6 +55,7 @@ public enum UFOError: Error { case offCurveNotFollowedByCurve case tooManyOffCurvesBeforeCurve case offCurveCannotBeSmooth + case pathTooShort } public enum UFOFormatVersion: Int, Codable {