1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-11-23 19:21:53 +03:00

Use uninitialized array initializer

This commit is contained in:
Tae Won Ha 2023-12-24 19:20:17 +01:00
parent 4dc97452c2
commit 60ed0f7aa6
No known key found for this signature in database
GPG Key ID: E40743465B5B8B44

View File

@ -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..<pointer.count {
let run = pointer[k]
for k in 0..<pointer.count {
let run = pointer[k]
let glyphCount = CTRunGetGlyphCount(run)
let glyphCount = CTRunGetGlyphCount(run)
// swiftlint:disable force_unwrapping
let glyphs = [CGGlyph](unsafeUninitializedCapacity: glyphCount) { buffer, count in
CTRunGetGlyphs(run, .zero, buffer.baseAddress!)
count = glyphCount
}
var positions = [CGPoint](unsafeUninitializedCapacity: glyphCount) { buffer, count in
CTRunGetPositions(run, .zero, buffer.baseAddress!)
count = glyphCount
}
let indices = [CFIndex](unsafeUninitializedCapacity: glyphCount) { buffer, count in
CTRunGetStringIndices(run, .zero, buffer.baseAddress!)
count = glyphCount
}
// swiftlint:enable force_unwrapping
var column = -1
var columnPosition = 0.0
var deltaX = 0.0
positions.withUnsafeMutableBufferPointer { positionsPtr in
for i in 0..<positionsPtr.count {
let newColumn = cellIndices[indices[i]] + startColumn
if newColumn != column {
columnPosition = offset.x + newColumn.cgf * cellWidth
deltaX = columnPosition - positionsPtr[i].x
column = newColumn
}
positionsPtr[i].x += deltaX
positionsPtr[i].y += offset.y
// swiftlint:disable force_unwrapping
let glyphs = [CGGlyph](unsafeUninitializedCapacity: glyphCount) { buffer, count in
CTRunGetGlyphs(run, .zero, buffer.baseAddress!)
count = glyphCount
}
var positions = [CGPoint](unsafeUninitializedCapacity: glyphCount) { buffer, count in
CTRunGetPositions(run, .zero, buffer.baseAddress!)
count = glyphCount
}
let indices = [CFIndex](unsafeUninitializedCapacity: glyphCount) { buffer, count in
CTRunGetStringIndices(run, .zero, buffer.baseAddress!)
count = glyphCount
}
// swiftlint:enable force_unwrapping
var column = -1
var columnPosition = 0.0
var deltaX = 0.0
positions.withUnsafeMutableBufferPointer { positionsPtr in
for i in 0..<positionsPtr.count {
let newColumn = cellIndices[indices[i]] + startColumn
if newColumn != column {
columnPosition = offset.x + newColumn.cgf * cellWidth
deltaX = columnPosition - positionsPtr[i].x
column = newColumn
}
positionsPtr[i].x += deltaX
positionsPtr[i].y += offset.y
}
}
let attrs = CTRunGetAttributes(run)
let font = Unmanaged<NSFont>.fromOpaque(
CFDictionaryGetValue(attrs, Unmanaged.passUnretained(kCTFontAttributeName).toOpaque())
).takeUnretainedValue()
resultBuffer[k] = FontGlyphRun(font: font, glyphs: glyphs, positions: positions)
}
let attrs = CTRunGetAttributes(run)
let font = Unmanaged<NSFont>.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)