fix(editor): measure decorations via TK2 fragment geometry (#71 follow-up 3)

The TK1-compat layoutManager reports wrong rects under TextKit2 — the
table grid drew compressed above its text, the quote hairline landed
mid-heading. fragmentFrames(for:) walks NSTextLayoutManager fragments
(the engine's own frames, document order): quote hairlines span the
block, HRs center on their line, table verticals span all rows and
horizontals underline every grid line.
This commit is contained in:
AlexKucera committed 2026-10-06 11:36:29 +02:00
1 parent 4138bf22dd
commit 2fbc8af24d
1 file changed
+39 -32
+39 -32
View File
@@ -134,60 +134,67 @@ final class ReadingCanvasView: NSTextView {
// MARK: Decorations (#69 fix: quote hairlines, HR rules, table grids —
// drawn from measured geometry; NSTextBlock/NSTextTable layout is
// broken on modern TextKit)
// drawn from measured TK2 fragment geometry; NSTextBlock/NSTextTable
// layout is broken on modern TextKit, and the TK1-compat layoutManager
// reports wrong rects, so measure via NSTextLayoutManager directly.)
/// Union of layout-fragment frames for a document character range
/// (engine-native measurement; fragments arrive in document order).
private func fragmentFrames(for docRange: NSRange) -> [CGRect] {
guard let tlm = textLayoutManager, let tcm = tlm.textContentManager else { return [] }
let docStart = tcm.documentRange.location
guard
let startLoc = tcm.location(docStart, offsetBy: docRange.location),
let endLoc = tcm.location(docStart, offsetBy: NSMaxRange(docRange))
else { return [] }
let endOffset = docRange.location + docRange.length
var frames: [CGRect] = []
tlm.enumerateTextLayoutFragments(from: startLoc) { fragment in
let fragStart = tcm.offset(from: docStart, to: fragment.rangeInElement.location)
if fragStart >= endOffset {
return false
}
frames.append(fragment.layoutFragmentFrame)
return true
}
return frames
}
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
guard let layoutManager, let container = textContainer else { return }
for bound in blockBounds {
guard let decoration = bound.decoration else { continue }
let glyphRange = layoutManager.glyphRange(
forCharacterRange: bound.docRange, actualCharacterRange: nil,
)
let frames = fragmentFrames(for: bound.docRange)
guard let firstFrame = frames.first, let lastFrame = frames.last else { continue }
switch decoration {
case let .quote(indent):
let rect = layoutManager.boundingRect(forGlyphRange: glyphRange, in: container)
let x = textContainerOrigin.x + container.lineFragmentPadding + indent - 6
let x = textContainerOrigin.x + (textContainer?.lineFragmentPadding ?? 0) + indent - 6
let path = NSBezierPath()
path.move(to: NSPoint(x: x, y: rect.minY))
path.line(to: NSPoint(x: x, y: rect.maxY))
path.move(to: NSPoint(x: x, y: firstFrame.minY))
path.line(to: NSPoint(x: x, y: lastFrame.maxY))
path.lineWidth = 2
ProseTypography.quoteRuleColor.setStroke()
path.stroke()
case .rule:
let rect = layoutManager.boundingRect(forGlyphRange: glyphRange, in: container)
let y = rect.midY
let y = (firstFrame.minY + firstFrame.maxY) / 2
let path = NSBezierPath()
path.move(to: NSPoint(x: textContainerOrigin.x, y: y))
path.line(to: NSPoint(x: textContainerOrigin.x + container.size.width, y: y))
path.line(to: NSPoint(x: textContainerOrigin.x + (textContainer?.size.width ?? 0), y: y))
path.lineWidth = 1
ProseTypography.tableBorderColor.setStroke()
path.stroke()
case let .table(columns):
let rect = layoutManager.boundingRect(forGlyphRange: glyphRange, in: container)
let originX = textContainerOrigin.x + container.lineFragmentPadding
let originX = textContainerOrigin.x + (textContainer?.lineFragmentPadding ?? 0)
let path = NSBezierPath()
// verticals: column separators + closing edge, spanning all rows
for offset in columns {
let x = originX + offset
path.move(to: NSPoint(x: x, y: rect.minY))
path.line(to: NSPoint(x: x, y: rect.maxY))
path.move(to: NSPoint(x: x, y: firstFrame.minY))
path.line(to: NSPoint(x: x, y: lastFrame.maxY))
}
var lineStart = glyphRange.location
var rowLines: [CGFloat] = []
while lineStart < NSMaxRange(glyphRange) {
var effective = NSRange(location: 0, length: 0)
let lineRect = layoutManager.lineFragmentRect(
forGlyphAt: lineStart, effectiveRange: &effective,
withoutAdditionalLayout: true,
)
if effective.length == 0 {
break
}
rowLines.append(lineRect.maxY)
lineStart = NSMaxRange(effective)
}
for y in rowLines {
// horizontals: top border + a line under every text line
let ys = [firstFrame.minY] + frames.map(\.maxY)
for y in ys {
path.move(to: NSPoint(x: originX, y: y))
path.line(to: NSPoint(x: originX + (columns.last ?? 0), y: y))
}