Skip to content

Commit bdf90b3

Browse files
authored
Merge pull request #110 from Weixi779/main
Use CoreText run font fallback for glyph paths
2 parents 17d55c1 + 14535c5 commit bdf90b3

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

Samples.bundle/cjk-fallback.svg

Lines changed: 6 additions & 0 deletions
Loading

SwiftDraw/Sources/Utilities/CGPath+Segment.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ extension String {
134134
for idx in 0..<CFArrayGetCount(glyphRuns) {
135135
let val = CFArrayGetValueAtIndex(glyphRuns, idx)
136136
let run = unsafeBitCast(val, to: CTRun.self)
137+
let attributes = CTRunGetAttributes(run) as NSDictionary
138+
let runFont = resolveRunFont(attributes: attributes, fallback: font)
137139

138140
for idx in 0..<CTRunGetGlyphCount(run) {
139141
let glyphRange = CFRange(location: idx, length: 1)
@@ -142,7 +144,7 @@ extension String {
142144
CTRunGetGlyphs(run, glyphRange, &glyph)
143145
CTRunGetPositions(run, glyphRange, &position)
144146
var t = CGAffineTransform.identity
145-
if let glyphPath = CTFontCreatePathForGlyph(font, glyph, &t) {
147+
if let glyphPath = CTFontCreatePathForGlyph(runFont, glyph, &t) {
146148
let t = CGAffineTransform(a: 1, b: 0, c: 0, d: -1, tx: position.x, ty: baseline)
147149
let t1 = t.translatedBy(x: 0, y: baseline)
148150
path.addPath(glyphPath, transform: t1)
@@ -152,6 +154,18 @@ extension String {
152154

153155
return path
154156
}
157+
158+
// Use the font CoreText resolved for this run, else fall back to the requested font.
159+
private func resolveRunFont(attributes: NSDictionary, fallback: CTFont) -> CTFont {
160+
guard let value = attributes[kCTFontAttributeName] else {
161+
return fallback
162+
}
163+
// CoreFoundation type bridging: ensure it's a CTFont before use.
164+
if CFGetTypeID(value as CFTypeRef) == CTFontGetTypeID() {
165+
return unsafeDowncast(value as AnyObject, to: CTFont.self)
166+
}
167+
return fallback
168+
}
155169
}
156170

157171
#endif

0 commit comments

Comments
 (0)