diff --git a/NvimView/Sources/NvimView/Typesetter.swift b/NvimView/Sources/NvimView/Typesetter.swift index 95abcbae..2f8dbd7b 100644 --- a/NvimView/Sources/NvimView/Typesetter.swift +++ b/NvimView/Sources/NvimView/Typesetter.swift @@ -20,59 +20,56 @@ final class Typesetter { let ctRuns = self.ctRuns(from: utf16Chars, font: font) return ctRuns.withUnsafeBufferPointer { pointer -> [FontGlyphRun] in - // Tried to use Array(unsafeUninitializedCapacity, initializingWith:) for result, - // but I get EXC_BAD_ACCESS, I don't know why. - var result: [FontGlyphRun] = [] - result.reserveCapacity(pointer.count) + [FontGlyphRun](unsafeUninitializedCapacity: pointer.count) { resultBuffer, endCount in + for k in 0...fromOpaque( + CFDictionaryGetValue(attrs, Unmanaged.passUnretained(kCTFontAttributeName).toOpaque()) + ).takeUnretainedValue() + + resultBuffer[k] = FontGlyphRun(font: font, glyphs: glyphs, positions: positions) } - let attrs = CTRunGetAttributes(run) - let font = Unmanaged.fromOpaque( - CFDictionaryGetValue(attrs, Unmanaged.passUnretained(kCTFontAttributeName).toOpaque()) - ).takeUnretainedValue() - - result.append(FontGlyphRun(font: font, glyphs: glyphs, positions: positions)) + endCount = pointer.count } - - return result } } @@ -149,8 +146,8 @@ final class Typesetter { attributes: [.font: font, .ligature: ligatureOption] ) - let ctLine = CTLineCreateWithAttributedString(attrStr) - guard let ctRuns = CTLineGetGlyphRuns(ctLine) as? [CTRun] else { return [] } + let ctLine = CTLineCreateWithAttributedString(consume attrStr) + guard let ctRuns = CTLineGetGlyphRuns(consume ctLine) as? [CTRun] else { return [] } self.ctRunsCache.set(CtRunsAndFont(ctRuns: ctRuns, font: font), forKey: utf16Chars)