summaryrefslogtreecommitdiff
path: root/src/gui
diff options
context:
space:
mode:
Diffstat (limited to 'src/gui')
-rw-r--r--src/gui/gui.zig72
1 files changed, 70 insertions, 2 deletions
diff --git a/src/gui/gui.zig b/src/gui/gui.zig
index 49a98f5b..d71f7047 100644
--- a/src/gui/gui.zig
+++ b/src/gui/gui.zig
@@ -105,9 +105,52 @@ const GlyphKey = packed struct(u64) {
centered: bool = false,
lead: u4 = 0,
span: u4 = 1,
- _: u2 = 0,
+ /// A light box-drawing rule (`ruleArms`), drawn edge to edge rather
+ /// than from the font; `glyph` is its codepoint.
+ rule: bool = false,
+ _: u1 = 0,
};
+/// The light box-drawing rules drawn by hand, as Ghostty and kitty draw
+/// them: a logical pixel thick through the cell's middle, out to its edges,
+/// so a markdown table's `|` (Tables.zig) joins the rows above and below it
+/// and its delimiter row is one line across, at any line height a font's
+/// own glyph would leave gaps in. The arms each one has: left, right, up, down.
+fn ruleArms(cp: u32) ?[4]bool {
+ return switch (cp) {
+ 0x2500 => .{ true, true, false, false }, // ─
+ 0x2502 => .{ false, false, true, true }, // │
+ 0x250c => .{ false, true, false, true }, // ┌
+ 0x2510 => .{ true, false, false, true }, // ┐
+ 0x2514 => .{ false, true, true, false }, // └
+ 0x2518 => .{ true, false, true, false }, // ┘
+ 0x251c => .{ false, true, true, true }, // ├
+ 0x2524 => .{ true, false, true, true }, // ┤
+ 0x252c => .{ true, true, false, true }, // ┬
+ 0x2534 => .{ true, true, true, false }, // ┴
+ 0x253c => .{ true, true, true, true }, // ┼
+ else => null,
+ };
+}
+
+fn drawRule(out: [*]u8, stride: usize, width: usize, height: usize, arms: [4]bool) void {
+ if (width == 0 or height == 0) return;
+ const t: usize = @min(@max(1, @as(usize, @intFromFloat(dp(1)))), @min(width, height));
+ const mx = (width - t) / 2;
+ const my = (height - t) / 2;
+ for (0..height) |y| @memset(out[y * stride ..][0..width], 0);
+ if (arms[0] or arms[1]) for (0..t) |k| {
+ const from: usize = if (arms[0]) 0 else mx;
+ const to: usize = if (arms[1]) width else mx + t;
+ @memset(out[(my + k) * stride + from ..][0 .. to - from], 255);
+ };
+ if (arms[2] or arms[3]) {
+ const from: usize = if (arms[2]) 0 else my;
+ const to: usize = if (arms[3]) height else my + t;
+ for (from..to) |y| @memset(out[y * stride + mx ..][0..t], 255);
+ }
+}
+
const GlyphDecoration = packed struct(u5) {
bold: bool = false,
underline: @FieldType(pardes.CellStyle, "ul") = .off,
@@ -7314,6 +7357,8 @@ fn wordCell(cell: *const pardes.Cell) WordCell {
/// cell tints its own slice.
fn wordBreak(a: WordCell, b: WordCell, i: usize, cursor: ?usize) bool {
if (a.cp == ' ' or b.cp == ' ') return true;
+ // A rule is drawn by hand (ruleArms), never a slice of a shaped word.
+ if (ruleArms(a.cp) != null or ruleArms(b.cp) != null) return true;
if (cursor) |at| if (at == i or at == i + 1) return true;
if (a.role != b.role or !std.meta.eql(a.decoration, b.decoration)) return true;
return (a.cp == 'f' and (b.cp == 'i' or b.cp == 'l')) or (a.cp == 's' and b.cp == 't');
@@ -7334,6 +7379,8 @@ fn cellSlot(g: *Gui, line: []const pardes.Cell, at: usize, cursor: ?usize, role:
var here = wordCell(&line[at]);
// An underlined or struck-out space is a glyph of its own: it draws its line.
if (here.cp == ' ' and !here.decoration.drawsBlank()) return g.space_slot;
+ if (ruleArms(here.cp) != null)
+ return cachedGlyph(g, .{ .face = 0, .glyph = here.cp, .role = role, .rule = true }) orelse g.space_slot;
const gpa = g.glyphs.allocator;
const cell_key: Words.CellKey = .{ .cp = @intCast(here.cp), .role = role, .decoration = here.decoration };
const known = words.cells.getOrPut(gpa, cell_key) catch return g.space_slot;
@@ -7589,7 +7636,9 @@ fn cachedGlyph(g: *Gui, key: GlyphKey) ?Slot {
const raster_w = if (key.centered) slot_w else cell_w * key.span;
const raster_h = if (key.role == .tagline) g.tagline_height else g.cell_h;
const bold = @intFromBool(key.decoration.bold);
- if (key.centered) {
+ if (key.rule) {
+ drawRule(out, atlas_w, raster_w, raster_h, ruleArms(key.glyph).?);
+ } else if (key.centered) {
_ = c.ui_font_raster_centered(face, px, key.glyph, bold, out, @intCast(atlas_w), @intCast(raster_w), @intCast(raster_h));
} else {
_ = c.ui_font_raster(face, px, key.glyph, bold, out, @intCast(atlas_w), @intCast(raster_w), @intCast(raster_h), @intCast(cell_w * key.lead), @intCast(cell_w), baseline);
@@ -7764,6 +7813,25 @@ fn decorateLine(out: [*]u8, stride: usize, width: usize, height: usize, baseline
}
}
+test "a box-drawing rule reaches its cell's edges, and only its arms do" {
+ const stride = 12;
+ const width = 9;
+ const height = 15;
+ var mask: [stride * height]u8 = @splat(0xa5);
+ drawRule(&mask, stride, width, height, ruleArms(0x253c).?);
+ // ┼: ink on all four edges, none in the corners, nothing past the slot.
+ try std.testing.expectEqual(@as(u8, 255), mask[0 * stride + width / 2]);
+ try std.testing.expectEqual(@as(u8, 255), mask[(height - 1) * stride + width / 2]);
+ try std.testing.expectEqual(@as(u8, 255), mask[(height / 2) * stride]);
+ try std.testing.expectEqual(@as(u8, 255), mask[(height / 2) * stride + width - 1]);
+ try std.testing.expectEqual(@as(u8, 0), mask[0]);
+ for (0..height) |y| for (mask[y * stride + width ..][0 .. stride - width]) |guard| try std.testing.expectEqual(@as(u8, 0xa5), guard);
+ drawRule(&mask, stride, width, height, ruleArms(0x2502).?);
+ try std.testing.expectEqual(@as(u8, 0), mask[(height / 2) * stride]);
+ try std.testing.expectEqual(@as(u8, 255), mask[0 * stride + width / 2]);
+ try std.testing.expect(ruleArms('|') == null);
+}
+
test "glyph decorations render spaces and stay inside their atlas slot" {
const stride = 9;
const width = 5;