native table border rendering

This commit is contained in:
jess 2026-04-04 22:44:23 -07:00
parent d1efbed9a8
commit 39feb208ab
1 changed files with 23 additions and 11 deletions

View File

@ -118,11 +118,22 @@ class MarkdownLayoutManager: NSLayoutManager {
charOffset += lineLen + 1 charOffset += lineLen + 1
} }
// Draw vertical column separators from first line pipe positions // Draw vertical column separators using pipe character glyph positions
if let firstLine = lines.first { if let firstLine = lines.first {
let colWidth = rect.size.width / CGFloat(max(columns, 1)) let nsFirstLine = firstLine as NSString
for col in 1..<columns { var pipeOffsets: [Int] = []
let x = rect.origin.x + colWidth * CGFloat(col) for i in 0..<nsFirstLine.length {
if nsFirstLine.character(at: i) == UInt16(UnicodeScalar("|").value) {
pipeOffsets.append(i)
}
}
// Skip first and last pipe (outer borders), draw inner separators
if pipeOffsets.count > 2 {
for pi in 1..<(pipeOffsets.count - 1) {
let charPos = charRange.location + pipeOffsets[pi]
let pipeGlyph = self.glyphRange(forCharacterRange: NSRange(location: charPos, length: 1), actualCharacterRange: nil)
let pipeRect = boundingRect(forGlyphRange: pipeGlyph, in: container)
let x = pipeRect.origin.x + origin.x + pipeRect.size.width / 2
let colLine = NSBezierPath() let colLine = NSBezierPath()
colLine.move(to: NSPoint(x: x, y: rect.origin.y)) colLine.move(to: NSPoint(x: x, y: rect.origin.y))
colLine.line(to: NSPoint(x: x, y: rect.origin.y + rect.size.height)) colLine.line(to: NSPoint(x: x, y: rect.origin.y + rect.size.height))
@ -130,10 +141,11 @@ class MarkdownLayoutManager: NSLayoutManager {
Theme.current.surface2.setStroke() Theme.current.surface2.setStroke()
colLine.stroke() colLine.stroke()
} }
}
// Header background // Header background
if lines.count > 1 { if lines.count > 1 {
let firstLineRange = NSRange(location: charRange.location, length: (firstLine as NSString).length) let firstLineRange = NSRange(location: charRange.location, length: nsFirstLine.length)
let headerGlyphRange = self.glyphRange(forCharacterRange: firstLineRange, actualCharacterRange: nil) let headerGlyphRange = self.glyphRange(forCharacterRange: firstLineRange, actualCharacterRange: nil)
var headerRect = boundingRect(forGlyphRange: headerGlyphRange, in: container) var headerRect = boundingRect(forGlyphRange: headerGlyphRange, in: container)
headerRect.origin.x = rect.origin.x headerRect.origin.x = rect.origin.x