@@ -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