Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Samples.bundle/cjk-fallback.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 15 additions & 1 deletion SwiftDraw/Sources/Utilities/CGPath+Segment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ extension String {
for idx in 0..<CFArrayGetCount(glyphRuns) {
let val = CFArrayGetValueAtIndex(glyphRuns, idx)
let run = unsafeBitCast(val, to: CTRun.self)
let attributes = CTRunGetAttributes(run) as NSDictionary
let runFont = resolveRunFont(attributes: attributes, fallback: font)

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

return path
}

// Use the font CoreText resolved for this run, else fall back to the requested font.
private func resolveRunFont(attributes: NSDictionary, fallback: CTFont) -> CTFont {
guard let value = attributes[kCTFontAttributeName] else {
return fallback
}
// CoreFoundation type bridging: ensure it's a CTFont before use.
if CFGetTypeID(value as CFTypeRef) == CTFontGetTypeID() {
return unsafeDowncast(value as AnyObject, to: CTFont.self)
}
return fallback
}
}

#endif