2

I've created a CGPath from a glyph using CoreText's CTFontCreatePathForGlyph. Thanks to this SO post, I also have access to all the CGPoint in my CGPath as an array, which I can .filter() and .map() to my needs (I seek to translate certain points which match a position criteria). On the other hand, I am unable to convert said array back to a CGPath, which I need to do in order to graphically render the changes.

How could I go about this issue? Is there a better way of achieving this?

Thank you.

PS: Because I'm eventually going to convert said CGPaths to UIBezierPaths, feel free to use a UIBezierMethod if applicable.

Community
  • 1
  • 1
Egoscio
  • 97
  • 10

1 Answers1

4

I've come up with a solution, as it seems like there's no better way (from what I've researched): Reverse the CGPath to CGPoint function. Here's a proof of concept:

let newPath = UIBezierPath()

myPath.forEach(body: { element -> Void in
    let point = element.points.pointee
    switch element.type {
    case .moveToPoint:
        newPath.move(to: point)
    case .addLineToPoint:
        newPath.addLine(to: point)
    case .addQuadCurveToPoint:
        newPath.addQuadCurve(to: point, controlPoint: point)
    case .addCurveToPoint:
        newPath.addCurve(to: point, controlPoint1: point, controlPoint2: point)
    case .closeSubpath:
        newPath.close()
    }
})

The glyph set I'm currently working with has no curves (8-Bit), so for the time being, this will do. When tried on a glyph set with curves, the final path is somewhat sharp-cornered (FIXME: Create curve point parsing algorithm)

Community
  • 1
  • 1
Egoscio
  • 97
  • 10