Skip to content

Commit

Permalink
adjustable max depth
Browse files Browse the repository at this point in the history
  • Loading branch information
yukiny0811 committed Aug 3, 2024
1 parent c062760 commit 799872e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import SimpleSimdSwift

public extension GlyphUtil {
enum MainFunctions {
private static let DEPTH: Int = 8
static func adaptiveQubicBezierCurve2(
a: f2,
b: f2,
Expand All @@ -47,9 +46,10 @@ public extension GlyphUtil {
cVel: f2,
angleLimit: Float,
depth: Int,
line: inout [f2]
line: inout [f2],
maxDepth: Int
) {
if depth > Self.DEPTH { return }
if depth > maxDepth { return }
let startMiddleAngle: Float = acos(simd_dot(aVel, bVel))
let middleEndAngle: Float = acos(simd_dot(bVel, cVel))
if startMiddleAngle + middleEndAngle > angleLimit {
Expand All @@ -60,10 +60,10 @@ public extension GlyphUtil {
let bcd = (bc + cd) * 0.5
let abcd = (abc + bcd) * 0.5
let sVel = simd_normalize(HelperFunctions.cubicBezierVelocity2(a, ab, abc, abcd, 0.5))
Self.adaptiveQubicBezierCurve2(a: a, b: ab, c: abc, d: abcd, aVel: aVel, bVel: sVel, cVel: bVel, angleLimit: angleLimit, depth: depth+1, line: &line)
Self.adaptiveQubicBezierCurve2(a: a, b: ab, c: abc, d: abcd, aVel: aVel, bVel: sVel, cVel: bVel, angleLimit: angleLimit, depth: depth+1, line: &line, maxDepth: maxDepth)
line.append(abcd)
let eVel = simd_normalize(HelperFunctions.cubicBezierVelocity2(abcd, bcd, cd, d, 0.5))
Self.adaptiveQubicBezierCurve2(a: abcd, b: bcd, c: cd, d: d, aVel: bVel, bVel: eVel, cVel: cVel, angleLimit: angleLimit, depth: depth+1, line: &line)
Self.adaptiveQubicBezierCurve2(a: abcd, b: bcd, c: cd, d: d, aVel: bVel, bVel: eVel, cVel: cVel, angleLimit: angleLimit, depth: depth+1, line: &line, maxDepth: maxDepth)
}
}
static func adaptiveQuadraticBezierCurve2(
Expand Down Expand Up @@ -91,7 +91,7 @@ public extension GlyphUtil {
adaptiveQuadraticBezierCurve2(a: abc, b: bc, c: c, aVel: bVel, bVel: eVel, cVel: cVel, angleLimit: angleLimit, depth: depth+1, line: &line)
}
}
static func getGlyphLines(_ glyphPath: CGPath, _ angleLimit: Float, _ distanceLimit: Float) -> [GlyphLine] {
static func getGlyphLines(_ glyphPath: CGPath, _ angleLimit: Float, _ distanceLimit: Float, maxDepth: Int) -> [GlyphLine] {
var myPath = GlyphLine()
var myGlyphPaths = [GlyphLine]()
glyphPath.applyWithBlock { (elementPtr: UnsafePointer<CGPathElement>) in
Expand Down Expand Up @@ -148,7 +148,7 @@ public extension GlyphUtil {
let cVel = simd_normalize(GlyphUtil.HelperFunctions.cubicBezierVelocity2(myA, myB, myC, myD, 1.0))
var data: [simd_float2] = []
data.append(myA)
GlyphUtil.MainFunctions.adaptiveQubicBezierCurve2(a: myA, b: myB, c: myC, d: myD, aVel: aVel, bVel: bVel, cVel: cVel, angleLimit: angleLimit, depth: 0, line: &data)
GlyphUtil.MainFunctions.adaptiveQubicBezierCurve2(a: myA, b: myB, c: myC, d: myD, aVel: aVel, bVel: bVel, cVel: cVel, angleLimit: angleLimit, depth: 0, line: &data, maxDepth: maxDepth)
data.append(myD)
data.removeFirst()
myPath += data
Expand Down
18 changes: 10 additions & 8 deletions Sources/FontVertexBuilder/PathText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ open class PathText {
verticalAlignment: VerticalAlignment = .center,
kern: Float = 0.0,
lineSpacing: Float = 0.0,
isClockwiseFont: Bool = false
isClockwiseFont: Bool = false,
maxDepth: Int = 8
) {
self.text = text
textBounds = bounds
Expand All @@ -172,7 +173,7 @@ open class PathText {
self.lineSpacing = lineSpacing
self.isClockwiseFont = isClockwiseFont
ctFont = SwiftyCTFont(name: fontName, size: CGFloat(fontSize), matrix: nil, options: nil)
setupData()
setupData(maxDepth: maxDepth)
}

public init(
Expand All @@ -184,7 +185,8 @@ open class PathText {
textAlignment: CTTextAlignment = .natural,
verticalAlignment: VerticalAlignment = .center,
kern: Float = 0.0,
lineSpacing: Float = 0.0
lineSpacing: Float = 0.0,
maxDepth: Int = 8
) {
self.text = text
textBounds = bounds
Expand All @@ -194,10 +196,10 @@ open class PathText {
self.kern = kern
self.lineSpacing = lineSpacing
ctFont = SwiftyCTFont(name: fontName, size: CGFloat(fontSize), matrix: nil, options: nil)
setupData()
setupData(maxDepth: maxDepth)
}

func setupData() {
func setupData(maxDepth: Int) {
var charOffset = 0
for (lineIndex, line) in lines.enumerated() {
let origin = origins[lineIndex]
Expand All @@ -211,18 +213,18 @@ open class PathText {
for glyphIndex in 0 ..< glyphCount {
let glyph = glyphs[glyphIndex]
let glyphPosition = glyphPositions[glyphIndex]
addGlyphGeometryData(glyph, glyphPosition, origin)
addGlyphGeometryData(glyph, glyphPosition, origin, maxDepth: maxDepth)
charOffset += 1
}
glyphPositions.deallocate()
glyphs.deallocate()
}
}
}
func addGlyphGeometryData(_ glyph: CGGlyph, _ glyphPosition: CGPoint, _ origin: CGPoint) {
func addGlyphGeometryData(_ glyph: CGGlyph, _ glyphPosition: CGPoint, _ origin: CGPoint, maxDepth: Int) {
guard let framePivot = framePivot, let verticalOffset = verticalOffset else { return }
if let glyphPath = ctFont.createPathForGlyph(glyph: glyph, matrix: nil) {
let glyphPaths = GlyphUtil.MainFunctions.getGlyphLines(glyphPath, angleLimit, fontSize*10)
let glyphPaths = GlyphUtil.MainFunctions.getGlyphLines(glyphPath, angleLimit, fontSize*10, maxDepth: maxDepth)
let glyphOffset = f2(Float(glyphPosition.x + origin.x - framePivot.x), Float(glyphPosition.y + origin.y - framePivot.y - verticalOffset))
calculatedPaths.append(LetterPath(glyphs: glyphPaths, offset: glyphOffset))
}
Expand Down
8 changes: 5 additions & 3 deletions Sources/SVGVertexBuilder/SVG.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ open class SVG: NSObject, XMLParserDelegate {

var cgPaths: [CGPath] = []
var parsingFinished = false

var maxDepth: Int

public var triangulatedPaths: [SVGGlyphLine] = []

public init? (url: URL) async {
public init? (url: URL, maxDepth: Int = 8) async {
guard let parser = XMLParser(contentsOf: url) else {
return nil
}
self.maxDepth = maxDepth
super.init()
parser.delegate = self
parser.parse()
Expand Down Expand Up @@ -52,7 +54,7 @@ open class SVG: NSObject, XMLParserDelegate {
public func parserDidEndDocument(_ parser: XMLParser) {
var parsed: [SVGPathObject] = []
for cgPath in cgPaths {
let glyphLines = SVGUtil.MainFunctions.getGlyphLines(cgPath)
let glyphLines = SVGUtil.MainFunctions.getGlyphLines(cgPath, maxDepth: maxDepth)
parsed.append(.init(glyphs: glyphLines, offset: .zero))
}

Expand Down
14 changes: 7 additions & 7 deletions Sources/SVGVertexBuilder/SVGUtils/SVGUtil+MainFunctions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import SimpleSimdSwift

public extension SVGUtil {
enum MainFunctions {
private static let DEPTH: Int = 8
static func adaptiveQubicBezierCurve2(
a: f2,
b: f2,
Expand All @@ -46,9 +45,10 @@ public extension SVGUtil {
cVel: f2,
angleLimit: Float,
depth: Int,
line: inout [f2]
line: inout [f2],
maxDepth: Int
) {
if depth > Self.DEPTH { return }
if depth > maxDepth { return }
let startMiddleAngle: Float = acos(simd_dot(aVel, bVel))
let middleEndAngle: Float = acos(simd_dot(bVel, cVel))
if startMiddleAngle + middleEndAngle > angleLimit {
Expand All @@ -59,10 +59,10 @@ public extension SVGUtil {
let bcd = (bc + cd) * 0.5
let abcd = (abc + bcd) * 0.5
let sVel = simd_normalize(HelperFunctions.cubicBezierVelocity2(a, ab, abc, abcd, 0.5))
Self.adaptiveQubicBezierCurve2(a: a, b: ab, c: abc, d: abcd, aVel: aVel, bVel: sVel, cVel: bVel, angleLimit: angleLimit, depth: depth+1, line: &line)
Self.adaptiveQubicBezierCurve2(a: a, b: ab, c: abc, d: abcd, aVel: aVel, bVel: sVel, cVel: bVel, angleLimit: angleLimit, depth: depth+1, line: &line, maxDepth: maxDepth)
line.append(abcd)
let eVel = simd_normalize(HelperFunctions.cubicBezierVelocity2(abcd, bcd, cd, d, 0.5))
Self.adaptiveQubicBezierCurve2(a: abcd, b: bcd, c: cd, d: d, aVel: bVel, bVel: eVel, cVel: cVel, angleLimit: angleLimit, depth: depth+1, line: &line)
Self.adaptiveQubicBezierCurve2(a: abcd, b: bcd, c: cd, d: d, aVel: bVel, bVel: eVel, cVel: cVel, angleLimit: angleLimit, depth: depth+1, line: &line, maxDepth: maxDepth)
}
}
static func adaptiveQuadraticBezierCurve2(
Expand Down Expand Up @@ -90,7 +90,7 @@ public extension SVGUtil {
adaptiveQuadraticBezierCurve2(a: abc, b: bc, c: c, aVel: bVel, bVel: eVel, cVel: cVel, angleLimit: angleLimit, depth: depth+1, line: &line)
}
}
public static func getGlyphLines(_ glyphPath: CGPath, _ angleLimit: Float = 7.5 * Float.pi / 180.0, _ distanceLimit: Float = 10000) -> [SVGGlyphLine] {
public static func getGlyphLines(_ glyphPath: CGPath, _ angleLimit: Float = 7.5 * Float.pi / 180.0, _ distanceLimit: Float = 10000, maxDepth: Int) -> [SVGGlyphLine] {

var myPath = SVGGlyphLine()
var myGlyphPaths = [SVGGlyphLine]()
Expand Down Expand Up @@ -149,7 +149,7 @@ public extension SVGUtil {
let cVel = simd_normalize(SVGUtil.HelperFunctions.cubicBezierVelocity2(myA, myB, myC, myD, 1.0))
var data: [simd_float2] = []
data.append(myA)
SVGUtil.MainFunctions.adaptiveQubicBezierCurve2(a: myA, b: myB, c: myC, d: myD, aVel: aVel, bVel: bVel, cVel: cVel, angleLimit: angleLimit, depth: 0, line: &data)
SVGUtil.MainFunctions.adaptiveQubicBezierCurve2(a: myA, b: myB, c: myC, d: myD, aVel: aVel, bVel: bVel, cVel: cVel, angleLimit: angleLimit, depth: 0, line: &data, maxDepth: maxDepth)
data.append(myD)
data.removeFirst()
myPath += data
Expand Down

0 comments on commit 799872e

Please sign in to comment.