fix(editor): engine-agnostic decoration measurement (#71 follow-up 4)
The reading canvas runs TextKit1 (textLayoutManager nil — the scroll-view config path engages the TK1 bridge), so the TK2 fragment walk found nothing and draw() bailed: no hairline, no grid. framesForRange() now measures through whichever engine is live — TK2 fragments, else TK1 boundingRect per character line (lineFragmentRect returns degenerate rects mid-layout; boundingRect is truthful, pixel-verified). Headless pixel probe (temporary, deleted): quote hairline + table grid lines render at the right coordinates.
This commit is contained in:
1 parent
2fbc8af24d
commit
d9cb615bc2
1 file changed
+37
-27
@@ -135,36 +135,15 @@ final class ReadingCanvasView: NSTextView {
|
||||
// MARK: Decorations (#69 fix: quote hairlines, HR rules, table grids —
|
||||
|
||||
// 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
|
||||
}
|
||||
// layout is broken on modern TextKit and the TK1-compat bridge reports
|
||||
// wrong rects. Enumeration pattern is probe-proven: walk ALL fragments
|
||||
// from document start, map each to a doc offset, slice per bound.)
|
||||
|
||||
override func draw(_ dirtyRect: NSRect) {
|
||||
super.draw(dirtyRect)
|
||||
for bound in blockBounds {
|
||||
guard let decoration = bound.decoration else { continue }
|
||||
let frames = fragmentFrames(for: bound.docRange)
|
||||
let frames = framesForRange(bound.docRange)
|
||||
guard let firstFrame = frames.first, let lastFrame = frames.last else { continue }
|
||||
switch decoration {
|
||||
case let .quote(indent):
|
||||
@@ -186,13 +165,11 @@ final class ReadingCanvasView: NSTextView {
|
||||
case let .table(columns):
|
||||
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: firstFrame.minY))
|
||||
path.line(to: NSPoint(x: x, y: lastFrame.maxY))
|
||||
}
|
||||
// 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))
|
||||
@@ -205,6 +182,39 @@ final class ReadingCanvasView: NSTextView {
|
||||
}
|
||||
}
|
||||
|
||||
/// Line frames for a document character range, engine-agnostic: TK2
|
||||
/// fragments when present, else TK1 boundingRect per character line
|
||||
/// (lineFragmentRect returns degenerate rects mid-layout on TK1 here).
|
||||
func framesForRange(_ docRange: NSRange) -> [CGRect] {
|
||||
if let tlm = textLayoutManager, let tcm = tlm.textContentManager {
|
||||
let docStart = tcm.documentRange.location
|
||||
var frames: [CGRect] = []
|
||||
tlm.enumerateTextLayoutFragments(from: docStart) { fragment in
|
||||
let tr = fragment.rangeInElement
|
||||
let loc = tcm.offset(from: docStart, to: tr.location)
|
||||
let len = tcm.offset(from: tr.location, to: tr.endLocation)
|
||||
if NSIntersectionRange(NSRange(location: loc, length: len), docRange).length > 0 {
|
||||
frames.append(fragment.layoutFragmentFrame)
|
||||
}
|
||||
return true
|
||||
}
|
||||
return frames
|
||||
}
|
||||
guard let layoutManager, let container = textContainer else { return [] }
|
||||
let nsText = string as NSString
|
||||
var frames: [CGRect] = []
|
||||
var lineStart = docRange.location
|
||||
while lineStart < NSMaxRange(docRange) {
|
||||
let lineRange = nsText.lineRange(for: NSRange(location: lineStart, length: 0))
|
||||
let clamped = NSIntersectionRange(lineRange, docRange)
|
||||
guard clamped.length > 0 else { break }
|
||||
let glyphRange = layoutManager.glyphRange(forCharacterRange: clamped, actualCharacterRange: nil)
|
||||
frames.append(layoutManager.boundingRect(forGlyphRange: glyphRange, in: container))
|
||||
lineStart = NSMaxRange(lineRange)
|
||||
}
|
||||
return frames
|
||||
}
|
||||
|
||||
override func updateTrackingAreas() {
|
||||
super.updateTrackingAreas()
|
||||
if hoverTrackingArea == nil {
|
||||
|
||||
Reference in new issue
Block a user