diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/File.zig | 23 | ||||
| -rw-r--r-- | src/Messages.zig | 10 | ||||
| -rw-r--r-- | src/Presentation.zig | 72 | ||||
| -rw-r--r-- | src/animation.zig | 16 | ||||
| -rw-r--r-- | src/body_layer.zig | 64 | ||||
| -rw-r--r-- | src/colors.zig | 123 | ||||
| -rw-r--r-- | src/draw.zig | 25 | ||||
| -rw-r--r-- | src/gui/gui.zig | 38 | ||||
| -rw-r--r-- | src/surface.zig | 193 |
9 files changed, 172 insertions, 392 deletions
diff --git a/src/File.zig b/src/File.zig index 7e05ac9f..e92efaba 100644 --- a/src/File.zig +++ b/src/File.zig @@ -1929,16 +1929,6 @@ pub const DiffTints = struct { } }; -/// A diff row's tint: its ground from `x` to the pane's edge. -fn tintRow(s: *Surface, x: u16, y: u16, end: u16, bg: [3]u8) void { - var col = x; - while (col < end) : (col += 1) { - const cell = s.at(col, y); - cell.default = false; - cell.style.bg = .{ .rgb = bg }; - } -} - fn synStyle(p: *Pardes, sy: syntax.Syn) ?SynStyle { return switch (sy) { .none => null, @@ -2010,7 +2000,7 @@ pub fn recolorSyntax(p: *Pardes, s: *Surface, pane: *Pane, f: *State, r: pardes. if (tint) |t| if (prefix_width < tw) { // A +Diff line added or removed whole: faintly (hunks.zig). const whole = highlights[base - highlight_start] & syntax.diff_whole != 0; - if (row_tint or whole) tintRow(s, tx + @as(u16, @intCast(prefix_width)), body_y + vr, tx + tw, if (row_tint) t.bg else t.line_bg); + if (row_tint or whole) s.tint(tx + @as(u16, @intCast(prefix_width)), body_y + vr, tw -| @as(u16, @intCast(prefix_width)), if (row_tint) t.bg else t.line_bg, null); }; // A +Pager's own colours (pager.zig) on every row but one the diff // look tints: an added or removed line is drawn as pardes draws a @@ -2228,7 +2218,7 @@ pub fn recolorDiffOutput(p: *Pardes, s: *Surface, pane: *Pane, r: pardes.Rect, t const view = maybe orelse continue; const base = @intFromPtr(rows[view.row].ptr) - flat_start - lo; const tint = if (base < styles.len) tints.forStyle(styles[base]) else null; - if (tint) |t| tintRow(s, tx, body_y + @as(u16, @intCast(vr)), tx + tw, t.bg); + if (tint) |t| s.tint(tx, body_y + @as(u16, @intCast(vr)), tw, t.bg, null); var c: usize = 0; var screen_c: usize = 0; while (c < line.len and screen_c < tw) { @@ -2310,12 +2300,9 @@ pub fn paintWordSelection( if (hi <= lo) continue; const c0 = @as(i32, prefix_width) + here.pad + displayOffset(pane, row, here.at, lo); const c1 = @as(i32, prefix_width) + here.pad + displayEndOffset(pane, row, here.at, hi - 1); - var col = @max(@as(i32, prefix_width), c0); - while (col <= c1 and col < @as(i32, tw)) : (col += 1) { - const cell = s.at(tx + @as(u16, @intCast(col)), body_y + @as(u16, @intCast(vr))); - cell.default = false; - cell.style.bg = .{ .rgb = bg }; - } + const col = @max(@as(i32, prefix_width), c0); + const end = @min(c1 + 1, @as(i32, tw)); + if (col < end) s.tint(tx + @as(u16, @intCast(col)), body_y + @as(u16, @intCast(vr)), @intCast(end - col), bg, null); } } diff --git a/src/Messages.zig b/src/Messages.zig index 6d064c93..6785bd8a 100644 --- a/src/Messages.zig +++ b/src/Messages.zig @@ -398,16 +398,6 @@ pub fn noticeLife(pane: *const Pane, i: usize) ?Pane.MessageLife { }; } -pub fn blendRgb(from: [3]u8, to: [3]u8, amount: f32) [3]u8 { - var out: [3]u8 = undefined; - for (&out, from, to) |*o, a, b| { - const af: f32 = @floatFromInt(a); - const bf: f32 = @floatFromInt(b); - o.* = @intFromFloat(@round(bf + (af - bf) * std.math.clamp(amount, 0, 1))); - } - return out; -} - fn logMessage(p: *Pardes, id: usize, text: []const u8) void { if (text.len == 0) return; const serial: u32 = if (p.fs.session_write) 0 else if (id < MAX_PANES) if (p.panes[id]) |pane| pane.serial else 0 else 0; diff --git a/src/Presentation.zig b/src/Presentation.zig index 2a75b66d..3fc9be44 100644 --- a/src/Presentation.zig +++ b/src/Presentation.zig @@ -347,52 +347,36 @@ pub fn acknowledge(self: *Presentation, p: *Pardes, tracks: []const Track) void pub fn capturePrevious(self: *Presentation, gpa: std.mem.Allocator, surface: *const pardes.Surface) void { self.diff_state = .none; const cells = surface.cells; - if (cells.len == 0) { - self.previous_valid = false; - self.previous_layout = @splat(null); - return; - } - if (self.previous_cells.len != cells.len) { - const next = gpa.alloc(pardes.Cell, cells.len) catch { - self.previous_valid = false; - self.previous_layout = @splat(null); - return; - }; - if (self.previous_cells.len > 0) gpa.free(self.previous_cells); - self.previous_cells = next; - } - for (&self.previous_body_layers, surface.bodyLayers()) |*old, *current| { - const count = if (current.rows > 0) current.cells.len else 0; - const storage = if (old.cells.len == count) old.cells else gpa.realloc(old.cells, count) catch { - self.previous_valid = false; - self.previous_layout = @splat(null); - return; - }; - if (count > 0) @memcpy(storage, current.cells); - old.* = current.*; - old.cells = storage; - } - for (&self.previous_tag_layers, surface.tagLayers()) |*old, *current| { - const count = if (current.rows > 0) current.cells.len else 0; - const storage = if (old.cells.len == count) old.cells else gpa.realloc(old.cells, count) catch { - self.previous_valid = false; - self.previous_layout = @splat(null); - return; + copy: { + if (cells.len == 0) break :copy; + if (self.previous_cells.len != cells.len) { + const next = gpa.alloc(pardes.Cell, cells.len) catch break :copy; + if (self.previous_cells.len > 0) gpa.free(self.previous_cells); + self.previous_cells = next; + } + const olds = [_][]pardes.Layer{ &self.previous_body_layers, &self.previous_tag_layers }; + const currents = [_][]const pardes.Layer{ surface.bodyLayers(), surface.tagLayers() }; + for (olds, currents) |old_layers, current_layers| for (old_layers, current_layers) |*old, *current| { + const count = if (current.rows > 0) current.cells.len else 0; + const storage = if (old.cells.len == count) old.cells else gpa.realloc(old.cells, count) catch break :copy; + if (count > 0) @memcpy(storage, current.cells); + old.* = current.*; + old.cells = storage; }; - if (count > 0) @memcpy(storage, current.cells); - old.* = current.*; - old.cells = storage; + @memcpy(self.previous_cells, cells); + @memcpy(self.previous_regions[0..surface.nregions], surface.regionList()); + self.previous_nregions = surface.nregions; + self.previous_cols = surface.cols; + self.previous_rows = surface.rows; + self.previous_valid = true; + self.previous_layout = if (self.submitted_ready) + self.submitted + else + @splat(null); + return; } - @memcpy(self.previous_cells, cells); - @memcpy(self.previous_regions[0..surface.nregions], surface.regionList()); - self.previous_nregions = surface.nregions; - self.previous_cols = surface.cols; - self.previous_rows = surface.rows; - self.previous_valid = true; - self.previous_layout = if (self.submitted_ready) - self.submitted - else - @splat(null); + self.previous_valid = false; + self.previous_layout = @splat(null); } pub fn cellDiff(self: *const Presentation, cols: u16, rows: u16, col: u16, row: u16) pardes.PanelCellDiff { diff --git a/src/animation.zig b/src/animation.zig index 547b9201..a4887148 100644 --- a/src/animation.zig +++ b/src/animation.zig @@ -1226,22 +1226,14 @@ pub fn Immediate(comptime Value: type) type { }; } -pub fn interpolateRgb(from: [3]u8, to: [3]u8, step: u16, steps: u16) [3]u8 { - if (step == 0) return from; - if (step >= steps) return to; - var out: [3]u8 = undefined; - for (&out, from, to) |*dst, a, b| { - const numerator = @as(u32, a) * (steps - step) + @as(u32, b) * step; - dst.* = @intCast((numerator + steps / 2) / steps); - } - return out; -} - +/// A Fade's value for its tests: a colour stepped channel by channel. const TestColor = struct { rgb: [3]u8, pub fn interpolate(from: TestColor, to: TestColor, step: u16, steps: u16) TestColor { - return .{ .rgb = interpolateRgb(from.rgb, to.rgb, step, steps) }; + var out = from; + for (&out.rgb, to.rgb) |*c, b| c.* = @intCast((@as(u32, c.*) * (steps - step) + @as(u32, b) * step + steps / 2) / steps); + return out; } }; diff --git a/src/body_layer.zig b/src/body_layer.zig index 2aa8e5f8..0ddda41d 100644 --- a/src/body_layer.zig +++ b/src/body_layer.zig @@ -6,7 +6,6 @@ const pardes = @import("pardes.zig"); const normal = @import("normal.zig"); const edit = @import("edit.zig"); const config = @import("config.zig"); -const limits = @import("memory.zig").limits; const layout = @import("layout.zig"); const modal = @import("modal.zig"); const tracy = @import("tracy.zig"); @@ -23,19 +22,7 @@ const TAG_TEXT_INSET = pardes.TAG_TEXT_INSET; const Rect = layout.Rect; const Pardes = pardes.Pardes; const tagline = @import("tagline.zig"); -const Messages = @import("Messages.zig"); const Text = panes.Text; -const MAX_PANES = pardes.MAX_PANES; -const TagKind = @import("Layer.zig").Kind; -const MAX_TAG_ROWS = pardes.MAX_TAG_ROWS; -const NOTICE_LAYER_BASE = pardes.NOTICE_LAYER_BASE; -const TAG_LINE_LAYER_BASE = pardes.TAG_LINE_LAYER_BASE; -const HEADER_LINE_LAYER_BASE = pardes.HEADER_LINE_LAYER_BASE; -const MAX_COLS = pardes.MAX_COLS; -const TOPBAR_H = pardes.TOPBAR_H; -const COLBAR_H = pardes.COLBAR_H; -const TAG_GAP = pardes.TAG_GAP; -const wordBoundsAtCol = pardes.wordBoundsAtCol; const Layer = @import("Layer.zig"); @@ -71,14 +58,9 @@ fn paintTerminalSelection(p: *Pardes, s: *Surface, pane: *Pane, r: Rect, rows: [ lo = modal.graphemeStart(line, @min(lo, line.len)); hi = @min(hi, line.len); if (lo >= hi) continue; - var col = panes.File.rawDisplayCol(line, lo); - const end = if (row.to_edge and hi == line.len) width else panes.File.rawDisplayCol(line, hi); - while (col < end and col < width) : (col += 1) { - const cell = s.at(tx + @as(u16, @intCast(col)), body_y + @as(u16, @intCast(visible))); - cell.default = false; - cell.style.bg = .{ .rgb = bg }; - if (fg) |ink| cell.style.fg = .{ .rgb = ink }; - } + const col = panes.File.rawDisplayCol(line, lo); + const end = @min(width, if (row.to_edge and hi == line.len) width else panes.File.rawDisplayCol(line, hi)); + if (col < end) s.tint(tx + @as(u16, @intCast(col)), body_y + @as(u16, @intCast(visible)), @intCast(end - col), bg, fg); } } } @@ -113,14 +95,9 @@ fn paintSourceSelection(p: *Pardes, s: *Surface, pane: *Pane, r: Rect, rows: []c const lo = @max(start, row.lo); const hi = @min(end, row.hi); if (lo >= hi) continue; - var col = prefix + source.pad + @as(i32, @intCast(panes.File.rawDisplayCol(line[start..], lo - start))); - const last = if (row.to_edge and hi == end) @as(i32, width) else prefix + source.pad + @as(i32, @intCast(panes.File.rawDisplayCol(line[start..], hi - start))); - while (col < last and col < width) : (col += 1) { - const cell = s.at(tx + @as(u16, @intCast(col)), body_y + @as(u16, @intCast(visible))); - cell.default = false; - cell.style.bg = .{ .rgb = bg }; - if (fg) |ink| cell.style.fg = .{ .rgb = ink }; - } + const col = prefix + source.pad + @as(i32, @intCast(panes.File.rawDisplayCol(line[start..], lo - start))); + const last = @min(width, if (row.to_edge and hi == end) @as(i32, width) else prefix + source.pad + @as(i32, @intCast(panes.File.rawDisplayCol(line[start..], hi - start)))); + if (col < last) s.tint(tx + @as(u16, @intCast(col)), body_y + @as(u16, @intCast(visible)), @intCast(last - col), bg, fg); } } } @@ -151,15 +128,11 @@ pub fn paintPointerSelection( // File line numbers occupy a gutter only in the body. The tag is // row zero in Sel space and starts at its real first text cell. const scroll: i32 = if (row < pane.tag_rows) pane.tag_scroll else 0; - var col: i32 = if (pane.file != null and row >= pane.tag_rows) @max(c0, @as(i32, panes.File.gutterWidth(pane))) else @max(0, c0 - scroll); + const col: i32 = if (pane.file != null and row >= pane.tag_rows) @max(c0, @as(i32, panes.File.gutterWidth(pane))) else @max(0, c0 - scroll); const row_x = if (row < pane.tag_rows) r.x + @min(TAG_TEXT_INSET, r.w) else tx; const row_width = if (row < pane.tag_rows) r.w -| TAG_TEXT_INSET else tw; - while (col <= c1 - scroll and col < row_width) : (col += 1) { - const cell = s.at(row_x + @as(u16, @intCast(col)), sy); - cell.default = false; - cell.style.bg = .{ .rgb = bg }; - if (fg) |ink| cell.style.fg = .{ .rgb = ink }; - } + const end = @min(c1 - scroll + 1, row_width); + if (col < end) s.tint(row_x + @as(u16, @intCast(col)), sy, @intCast(end - col), bg, fg); } } @@ -345,13 +318,8 @@ pub fn renderBody(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane while (row < body_bottom) : (row += 1) { const ar = pane.wrapAt(@as(i32, row) - @as(i32, pane.tag_rows)).line; if (ar < lo or ar > hi) continue; - var col: u16 = if (pane.file != null) panes.File.gutterWidth(pane) else 0; - while (col < tw) : (col += 1) { - const cell = s.at(tx + col, body_y + row - pane.tag_rows); - cell.default = false; - cell.style.bg = .{ .rgb = modal_bg }; - if (!hover_only) cell.style.fg = .{ .rgb = th.sel_fg }; - } + const col: u16 = if (pane.file != null) panes.File.gutterWidth(pane) else 0; + if (col < tw) s.tint(tx + col, body_y + row - pane.tag_rows, tw - col, modal_bg, if (hover_only) null else th.sel_fg); } } const preview = normal.selRegexArmed(pane) != null; @@ -409,13 +377,9 @@ pub fn renderBody(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane panes.File.lineDisplayEndOffset(visible_line, @intCast(@max(0, w.at)), @intCast(@max(0, bnd.hi_col)))) + vpfx + w.pad else @as(i32, tw) - 1; - var col: i32 = @max(cstart, vpfx); - while (col <= cend and col < tw) : (col += 1) { - const cell = s.at(tx + @as(u16, @intCast(col)), body_y + @as(u16, @intCast(vr))); - cell.default = false; - cell.style.bg = .{ .rgb = bg }; - if (!hover_only) cell.style.fg = .{ .rgb = th.sel_fg }; - } + const col: i32 = @max(cstart, vpfx); + const end = @min(cend + 1, tw); + if (col < end) s.tint(tx + @as(u16, @intCast(col)), body_y + @as(u16, @intCast(vr)), @intCast(end - col), bg, if (hover_only) null else th.sel_fg); } if (primary and !preview and !hover_only) continue; // the hardware cursor IS the primary's if (hover_only) continue; // quiet preview preserves the source ink diff --git a/src/colors.zig b/src/colors.zig index 28136975..c016434b 100644 --- a/src/colors.zig +++ b/src/colors.zig @@ -4,12 +4,10 @@ const pardes = @import("pardes.zig"); const tagline = @import("tagline.zig"); const panes = @import("panes.zig"); const std = @import("std"); -const layout = @import("layout.zig"); const animation = @import("animation.zig"); const Color = @import("surface.zig").Color; const FontRole = @import("surface.zig").FontRole; const config = @import("config.zig"); -const limits = @import("memory.zig").limits; const pdf_enabled = panes.Pdf.enabled; const platform = pardes.platform; const theme_animation = @import("pardes_config").theme_animation; @@ -34,7 +32,7 @@ pub fn contrast(a: [3]u8, b: [3]u8) f32 { return (@max(x, y) + 0.05) / (@min(x, y) + 0.05); } -fn lerp(a: [3]u8, b: [3]u8, t: f32) [3]u8 { +pub fn lerp(a: [3]u8, b: [3]u8, t: f32) [3]u8 { var out: [3]u8 = undefined; for (&out, a, b) |*o, x, y| o.* = @intFromFloat(std.math.clamp(@round(@as(f32, @floatFromInt(x)) + (@as(f32, @floatFromInt(y)) - @as(f32, @floatFromInt(x))) * t), 0, 255)); return out; @@ -498,22 +496,7 @@ fn fold(comptime C: type) [@typeInfo(C).@"struct".decl_names.len]Theme { } } -/// The imported themes (tools/gen_themes.zig), less any name one of ours -/// takes: a faithful port (`dracula`) keeps the plain name, and helix's -/// looser copy of it is still there as `dracula_helix` (the zed ones already -/// carry `_zed`). -fn foldImports(comptime ours: anytype, comptime C: type) [@typeInfo(C).@"struct".decl_names.len]Theme { - comptime { - @setEvalBranchQuota(400000); - var out = fold(C); - for (&out) |*g| for (ours) |c| if (std.mem.eql(u8, c.name, g.name)) { - g.name = g.name ++ "_helix"; - }; - return out; - } -} - -pub const themes = fold(curated) ++ foldImports(fold(curated), @import("generated_themes")); +pub const themes = fold(curated) ++ fold(@import("generated_themes")); /// The first `native_count` of `themes` are pardes's own; the rest imported. pub const native_count = @typeInfo(curated).@"struct".decl_names.len; @@ -589,18 +572,6 @@ test "lapis_plain is lapis on flat grounds, framed and shadowed as lapis" { /// Original and classic-inspired Pardes palettes precede retained imports. pub const native_theme_count = 15; -fn themeContrast(a: [3]u8, b: [3]u8) f64 { - var luminance: [2]f64 = .{ 0, 0 }; - for ([2][3]u8{ a, b }, 0..) |rgb, i| { - for (rgb, [3]f64{ 0.2126, 0.7152, 0.0722 }) |channel, weight| { - const srgb = @as(f64, @floatFromInt(channel)) / 255; - const linear = if (srgb <= 0.04045) srgb / 12.92 else std.math.pow(f64, (srgb + 0.055) / 1.055, 2.4); - luminance[i] += weight * linear; - } - } - return (@max(luminance[0], luminance[1]) + 0.05) / (@min(luminance[0], luminance[1]) + 0.05); -} - comptime { if (themes.len > std.math.maxInt(u16) + 1) @compileError("runtime config theme index no longer fits u16"); @@ -714,21 +685,11 @@ pub fn requestThemeFile(p: *Pardes, id: usize, argument: []const u8) void { var joined: [4095]u8 = undefined; const resolved = if (absolute) input else path: { const dir = p.opts.config_dir.?; - const separator = std.Io.Dir.path.sep; - const needs_separator = dir.len > 0 and dir[dir.len - 1] != separator; - const len = dir.len + @intFromBool(needs_separator) + input.len; - if (len > joined.len) { + const separator = if (dir.len > 0 and dir[dir.len - 1] != std.Io.Dir.path.sep) std.Io.Dir.path.sep_str else ""; + break :path std.fmt.bufPrint(&joined, "{s}{s}{s}", .{ dir, separator, input }) catch { p.reportError(id, "theme file", error.PathTooLong); return; - } - @memcpy(joined[0..dir.len], dir); - var at = dir.len; - if (needs_separator) { - joined[at] = separator; - at += 1; - } - @memcpy(joined[at..][0..input.len], input); - break :path joined[0..len]; + }; }; if (!p.theme_file_path.set(resolved)) { p.reportError(id, "theme file", error.PathTooLong); @@ -787,9 +748,7 @@ fn themeFileFailure(p: *Pardes, request: ThemeFileRequest, why: []const u8) void fn revertThemeFile(p: *Pardes) void { const loaded = p.theme_file_loaded.get(); if (std.mem.eql(u8, loaded, p.theme_file_path.get())) return; - var keep: [limits.host_path_cap]u8 = undefined; - @memcpy(keep[0..loaded.len], loaded); - if (loaded.len == 0) p.theme_file_path.clear() else _ = p.theme_file_path.set(keep[0..loaded.len]); + if (loaded.len == 0) p.theme_file_path.clear() else _ = p.theme_file_path.set(loaded); const generation = nextThemeFileGeneration(p); p.emit(.{ .theme_file = .{ .generation = generation, .on = loaded.len > 0 } }); } @@ -836,9 +795,7 @@ pub fn loadThemeFile(p: *Pardes, generation: u32, bytes: []const u8, animate: bo const old = p.custom_theme; p.custom_theme = parsed; if (old) |theme_value| p.gpa.free(theme_value.name); - var settled: [limits.host_path_cap]u8 = undefined; - @memcpy(settled[0..request.path.len], request.path); - _ = p.theme_file_loaded.set(settled[0..request.path.len]); + _ = p.theme_file_loaded.set(request.path); invalidateThemeDependentRasters(p); _ = p.scratch.reset(.{ .retain_with_limit = pardes.memory.limits.scratch_kept }); p.sync(); @@ -1115,58 +1072,38 @@ test "native themes keep text and UI roles readable on their own surfaces" { for (themes[0..names.len], names) |th, name| { try std.testing.expectEqualStrings(name, th.name); const chrome = ChromeTheme.fromTheme(&th); - const minimum: f64 = if (std.mem.eql(u8, name, "ink") or std.mem.eql(u8, name, "daybreak")) 7 else 4.5; + const minimum: f32 = if (std.mem.eql(u8, name, "ink") or std.mem.eql(u8, name, "daybreak")) 7 else 4.5; const text_colors = [_][3]u8{ th.fg.?, th.kw, th.str, th.num, th.comment, chrome.diagnostic_error, chrome.diagnostic_warning, chrome.diagnostic_info, chrome.diagnostic_hint, }; - for (text_colors) |fg| try std.testing.expect(themeContrast(fg, th.bg.?) >= minimum); + for (text_colors) |fg| try std.testing.expect(contrast(fg, th.bg.?) >= minimum); // Gutters are orientation aids, not source text: stay visible without // competing with comments or pulling attention away from the body. - try std.testing.expect(themeContrast(th.lineno, th.bg.?) >= 2.5); - try std.testing.expect(themeContrast(th.lineno, th.bg.?) <= 4); - try std.testing.expect(themeContrast(th.lineno_active.?, th.bg.?) >= themeContrast(th.lineno, th.bg.?)); - try std.testing.expect(themeContrast(th.lineno_active.?, th.bg.?) <= 4.5); - try std.testing.expect(themeContrast(th.comment, th.bg.?) >= 5); - try std.testing.expect(themeContrast(th.comment, th.bg.?) >= themeContrast(th.lineno, th.bg.?) + 2); - try std.testing.expect(themeContrast(th.tag_fg, th.tag_bg) >= minimum); - try std.testing.expect(themeContrast(chrome.tag_active_fg, chrome.tag_active_bg) >= minimum); + try std.testing.expect(contrast(th.lineno, th.bg.?) >= 2.5); + try std.testing.expect(contrast(th.lineno, th.bg.?) <= 4); + try std.testing.expect(contrast(th.lineno_active.?, th.bg.?) >= contrast(th.lineno, th.bg.?)); + try std.testing.expect(contrast(th.lineno_active.?, th.bg.?) <= 4.5); + try std.testing.expect(contrast(th.comment, th.bg.?) >= 5); + try std.testing.expect(contrast(th.comment, th.bg.?) >= contrast(th.lineno, th.bg.?) + 2); + try std.testing.expect(contrast(th.tag_fg, th.tag_bg) >= minimum); + try std.testing.expect(contrast(chrome.tag_active_fg, chrome.tag_active_bg) >= minimum); try std.testing.expect(th.tag_name_fg != null); - try std.testing.expect(themeContrast(chrome.tag_name_fg, th.tag_bg) >= minimum); - try std.testing.expect(themeContrast(chrome.tag_active_name_fg, chrome.tag_active_bg) >= minimum); + try std.testing.expect(contrast(chrome.tag_name_fg, th.tag_bg) >= minimum); + try std.testing.expect(contrast(chrome.tag_active_name_fg, chrome.tag_active_bg) >= minimum); // Focus stays on the same tonal surface, rather than inverting a // whole command strip. Text remains readable in both states. - try std.testing.expect(themeContrast(chrome.tag_active_bg, th.tag_bg) <= 1.6); - try std.testing.expect(themeContrast(chrome.tag_active_fg, th.tag_fg) <= 1.6); - try std.testing.expect(themeContrast(th.sel_fg, th.sel_bg) >= minimum); - try std.testing.expect(themeContrast(chrome.search_fg, chrome.search_bg) >= minimum); + try std.testing.expect(contrast(chrome.tag_active_bg, th.tag_bg) <= 1.6); + try std.testing.expect(contrast(chrome.tag_active_fg, th.tag_fg) <= 1.6); + try std.testing.expect(contrast(th.sel_fg, th.sel_bg) >= minimum); + try std.testing.expect(contrast(chrome.search_fg, chrome.search_bg) >= minimum); // ANSI black can intentionally match the page (for backgrounds). All // other ANSI foregrounds, including bright black, remain legible. - for (th.palette.?[1..]) |fg| try std.testing.expect(themeContrast(fg, th.bg.?) >= minimum); + for (th.palette.?[1..]) |fg| try std.testing.expect(contrast(fg, th.bg.?) >= minimum); } } test "native filename tints change hue without changing perceived lightness" { - const Perceptual = struct { - fn lab(rgb: [3]u8) [3]f64 { - var linear: [3]f64 = undefined; - for (rgb, 0..) |channel, i| { - const value = @as(f64, @floatFromInt(channel)) / 255; - linear[i] = if (value <= 0.04045) value / 12.92 else std.math.pow(f64, (value + 0.055) / 1.055, 2.4); - } - const r = linear[0]; - const g = linear[1]; - const b = linear[2]; - const l = std.math.cbrt(0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b); - const m = std.math.cbrt(0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b); - const n = std.math.cbrt(0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b); - return .{ - 0.2104542553 * l + 0.7936177850 * m - 0.0040720468 * n, - 1.9779984951 * l - 2.4285922050 * m + 0.4505937099 * n, - 0.0259040371 * l + 0.7827717662 * m - 0.8086757660 * n, - }; - } - }; for (themes[0..native_theme_count]) |th| { try std.testing.expect(th.tag_name_fg != null and th.tag_active_name_fg != null); const chrome = ChromeTheme.fromTheme(&th); @@ -1175,8 +1112,8 @@ test "native filename tints change hue without changing perceived lightness" { .{ chrome.tag_active_fg, chrome.tag_active_name_fg }, }; for (pairs) |pair| { - const base = Perceptual.lab(pair[0]); - const name = Perceptual.lab(pair[1]); + const base = toOklab(pair[0]); + const name = toOklab(pair[1]); // Half a lightness point allows 8-bit quantization without a // basename becoming visibly brighter or darker than its tag. try std.testing.expectApproxEqAbs(base[0], name[0], 0.005); @@ -1193,15 +1130,15 @@ test "native filename tints change hue without changing perceived lightness" { test "native dark palettes choose near-black or deliberately soft contrast" { for (themes[0..native_theme_count]) |th| { - if (themeContrast(th.bg.?, .{ 0, 0, 0 }) > 10) continue; // light paper + if (contrast(th.bg.?, .{ 0, 0, 0 }) > 10) continue; // light paper const soft = std.mem.eql(u8, th.name, "dusk") or std.mem.eql(u8, th.name, "lagoon") or std.mem.eql(u8, th.name, "solarium") or std.mem.eql(u8, th.name, "forge_soft"); if (soft) { - try std.testing.expect(themeContrast(th.fg.?, th.bg.?) >= 4.5); - try std.testing.expect(themeContrast(th.fg.?, th.bg.?) <= 7); + try std.testing.expect(contrast(th.fg.?, th.bg.?) >= 4.5); + try std.testing.expect(contrast(th.fg.?, th.bg.?) <= 7); } else { for (th.bg.?) |channel| try std.testing.expect(channel <= 0x14); - try std.testing.expect(themeContrast(th.fg.?, th.bg.?) >= 14); + try std.testing.expect(contrast(th.fg.?, th.bg.?) >= 14); } } const forge = curated.forge.theme; diff --git a/src/draw.zig b/src/draw.zig index 3d0d4d3c..93a78090 100644 --- a/src/draw.zig +++ b/src/draw.zig @@ -35,8 +35,6 @@ const wordBoundsAtCol = pardes.wordBoundsAtCol; const BOX_H = pardes.BOX_H; const TAG_GAP = pardes.TAG_GAP; const TAG_TEXT_INSET = pardes.TAG_TEXT_INSET; -const TOPBAR_H = pardes.TOPBAR_H; -const COLBAR_H = pardes.COLBAR_H; const MAX_PANES = pardes.MAX_PANES; const MAX_COLS = pardes.MAX_COLS; const MAX_TAG_ROWS = pardes.MAX_TAG_ROWS; @@ -1213,12 +1211,7 @@ fn joinPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r // Window mode (layout.windowKey): the outlined pane's tag in the // selection's colours. if (p.window_pick == id) { - const th = p.theme(); - for (storage) |*cell| { - cell.style.bg = .{ .rgb = th.sel_bg }; - cell.style.fg = .{ .rgb = th.sel_fg }; - cell.default = false; - } + for (0..rows) |row| temporary.tint(0, @intCast(row), temporary.cols, p.theme().sel_bg, p.theme().sel_fg); } const tag_fg: Color = .{ .rgb = if (active and p.settings.focus_tint) chrome.tag_active_fg else chrome.tag_fg }; // One layer, a row to each line the tag shows. @@ -1268,11 +1261,11 @@ fn joinNotice(p: *Pardes, s: *Surface, arena: std.mem.Allocator, region: Region) // cannot move it by less than a row, a pixel shell slides it too. const motion: Messages.MessageMotion = if (Messages.noticeLife(pane, i)) |life| Messages.messageMotion(p, life) else .{}; const page = th.bg orelse chrome.tag_bg; - const bg = Messages.blendRgb(chrome.tag_bg, page, motion.alpha); + const bg = colors.lerp(page, chrome.tag_bg, motion.alpha); // Where an exec was sent to a REPL reads in the tag's name tint, set // apart from what the editor says. const ink = if (kind == .message and pane.msg_sent) chrome.tag_name_fg else chrome.tag_fg; - const fg = Messages.blendRgb(ink, page, motion.alpha); + const fg = colors.lerp(page, ink, motion.alpha); // Nothing to say, or too narrow for a layer: a blank chip on the grid. if (text.len == 0 or r.w <= TAG_TEXT_INSET) { s.fill(region.rect.x, region.rect.y, chip, 1, .{ .bg = .{ .rgb = bg }, .font_role = .tagline }); @@ -1428,15 +1421,9 @@ pub fn paintPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pa const line = lines[row - top]; const from = if (row == first.row) first.col else 0; const to = @min(line.len, if (row == last.row) last.col else line.len); - var col = panes.File.rawDisplayCol(line, from) -| pane.tag_scroll; - const end = panes.File.rawDisplayCol(line, to); - const y = tag_y + @as(u16, @intCast(row - top)); - while (col + pane.tag_scroll < end and col < tw) : (col += 1) { - const cell = s.at(tx + @as(u16, @intCast(col)), y); - cell.default = false; - cell.style.bg = .{ .rgb = th.tag_sel_bg orelse th.sel_bg }; - cell.style.fg = .{ .rgb = th.sel_fg }; - } + const col = panes.File.rawDisplayCol(line, from) -| pane.tag_scroll; + const end = @min(panes.File.rawDisplayCol(line, to) -| pane.tag_scroll, tw); + if (col < end) s.tint(tx + @as(u16, @intCast(col)), tag_y + @as(u16, @intCast(row - top)), @intCast(end - col), th.tag_sel_bg orelse th.sel_bg, th.sel_fg); } } } diff --git a/src/gui/gui.zig b/src/gui/gui.zig index d3734fac..2d01db33 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -8794,37 +8794,25 @@ fn clipToBodies(g: *Gui, gpa: std.mem.Allocator, first: usize, bodies: []const [ /// the decor pipeline blends a colour that is not black): the largest `a` /// that keeps text and page apart by min(their contrast, 4.5). fn lightenCeiling(fg: [3]u8, bg: [3]u8, rgb: [3]u8) f32 { - const contrast = struct { - fn of(a: [3]u8, b: [3]u8) f32 { - const la = luminance(a); - const lb = luminance(b); - return (@max(la, lb) + 0.05) / (@min(la, lb) + 0.05); - } - fn blend(under: [3]u8, over: [3]u8, a: f32) [3]u8 { + const contrast = pardes.colors.contrast; + const blend = struct { + fn of(under: [3]u8, over: [3]u8, a: f32) [3]u8 { var out: [3]u8 = undefined; for (&out, under, over) |*o, x, y| o.* = @intFromFloat(@round(@as(f32, @floatFromInt(x)) * (1 - a) + @as(f32, @floatFromInt(y)) * a)); return out; } - }; - const target = @min(contrast.of(fg, bg), 4.5); + }.of; + const target = @min(contrast(fg, bg), 4.5); var lo: f32 = 0; var hi: f32 = 1; for (0..24) |_| { const mid = (lo + hi) / 2; - if (contrast.of(contrast.blend(fg, rgb, mid), contrast.blend(bg, rgb, mid)) >= target - 1e-3) lo = mid else hi = mid; + if (contrast(blend(fg, rgb, mid), blend(bg, rgb, mid)) >= target - 1e-3) lo = mid else hi = mid; } return lo; } -/// sRGB's relative luminance (WCAG). -fn luminance(rgb: [3]u8) f32 { - var sum: f32 = 0; - for (rgb, [3]f32{ 0.2126, 0.7152, 0.0722 }) |channel, weight| { - const v = @as(f32, @floatFromInt(channel)) / 255; - sum += weight * (if (v <= 0.04045) v / 12.92 else std.math.pow(f32, (v + 0.055) / 1.055, 2.4)); - } - return sum; -} +const luminance = pardes.colors.relativeLuminance; /// How dark a shadow may make the page and its text, both together, before /// the pair drops below its own contrast or 4.5, whichever is lower (§8.2): @@ -8853,11 +8841,7 @@ test "a rim never lightens a native theme's text below its contrast or 4.5" { return out; } }.of; - const ratio = struct { - fn of(x: [3]u8, y: [3]u8) f32 { - return (@max(luminance(x), luminance(y)) + 0.05) / (@min(luminance(x), luminance(y)) + 0.05); - } - }.of; + const ratio = pardes.colors.contrast; try std.testing.expect(ratio(blend(fg, light, a), blend(bg, light, a)) >= @min(ratio(fg, bg), 4.5) - 2e-3); } } @@ -9206,11 +9190,7 @@ fn paneDecor(g: *Gui, gpa: std.mem.Allocator, regions: []const pardes.Region, gr } /// Halfway between two colours. -fn mixRgb(a: [3]u8, b: [3]u8) [3]u8 { - var out: [3]u8 = undefined; - for (&out, a, b) |*o, x, y| o.* = @intCast((@as(u16, x) + y) / 2); - return out; -} +const mixRgb = pardes.colors.mix; /// A rule's width: the theme's own, or one pixel. fn rulePx(chrome: *const pardes.Chrome) f32 { diff --git a/src/surface.zig b/src/surface.zig index d6ec016d..839e8f33 100644 --- a/src/surface.zig +++ b/src/surface.zig @@ -221,10 +221,6 @@ pub const ImageCacheKey = if (pdf_enabled) struct { } }; -const PdfFitMode = panes.Pdf.FitMode; - -const PdfTintMode = panes.Pdf.TintMode; - /// Dynamic placement exists only for native PDF pages. Static image panes need /// only their pane identity, so feature-off builds carry a zero-bit payload. pub const NativePlacement = if (pdf_enabled) struct { @@ -562,67 +558,73 @@ pub const Surface = struct { continue; } } - if (col >= end) break; - // n == 0: not a start byte at all. A short tail or a bad - // continuation decodes to null the same way — one U+FFFD, one byte. - const n = std.unicode.utf8ByteSequenceLength(text[i]) catch 0; - const decoded: ?u21 = if (n > 0 and i + n <= text.len) - (std.unicode.utf8Decode(text[i .. i + n]) catch null) - else - null; - var cp_slice: []const u8 = "\u{FFFD}"; - var consumed: usize = 1; - if (decoded != null) { - var git = uucode.grapheme.utf8Iterator(text[i..]); - if (git.nextGrapheme()) |g| { - const candidate = text[i .. i + g.end]; - if (std.unicode.utf8ValidateSlice(candidate)) { - cp_slice = candidate; - consumed = candidate.len; - } else { - cp_slice = text[i .. i + n]; - consumed = n; - } - } - } - i += consumed; - var cp = decoded orelse 0xFFFD; - if (cp == '\r') continue; - if (cp == '\t') { - const spaces = @min(config.tab_now, end - col); - s.fill(col, y, spaces, 1, style); - col += spaces; - continue; - } - // No other C0/C1 control or DEL reaches a font fallback either. - if (cp < ' ' or cp == 0x7f or (cp >= 0x80 and cp <= 0x9f)) { - cp = 0xFFFD; - cp_slice = "\u{FFFD}"; - } - const width: u16 = if (decoded == null or (cp < 0x80 and cp_slice.len == 1)) - 1 - else - @max(1, vaxis.gwidth.gwidth(cp_slice, .unicode)); - if (width == 2 and col + 1 >= end) break; - var shown = cp_slice; - if (shown.len > @typeInfo(@FieldType(Cell, "text")).array.len) { - const cap = @typeInfo(@FieldType(Cell, "text")).array.len; - var prefix: usize = 0; - while (prefix < shown.len) { - const cp_len = std.unicode.utf8ByteSequenceLength(shown[prefix]) catch break; - if (prefix + cp_len > cap) break; - prefix += cp_len; + col = s.printGeneral(col, end, y, text, &i, style) orelse break; + } + return col; + } + + /// `print`'s general arm: the grapheme at `i.*`, painted at `col`. + /// Advances `i.*` past it and returns the column after it, or null + /// when a wide glyph does not fit before `end`. + fn printGeneral(s: *Surface, col: u16, end: u16, y: u16, text: []const u8, i: *usize, style: CellStyle) ?u16 { + const start = i.*; + // n == 0: not a start byte at all. A short tail or a bad + // continuation decodes to null the same way — one U+FFFD, one byte. + const n = std.unicode.utf8ByteSequenceLength(text[start]) catch 0; + const decoded: ?u21 = if (n > 0 and start + n <= text.len) + (std.unicode.utf8Decode(text[start .. start + n]) catch null) + else + null; + var cp_slice: []const u8 = "\u{FFFD}"; + var consumed: usize = 1; + if (decoded != null) { + var git = uucode.grapheme.utf8Iterator(text[start..]); + if (git.nextGrapheme()) |g| { + const candidate = text[start .. start + g.end]; + if (std.unicode.utf8ValidateSlice(candidate)) { + cp_slice = candidate; + consumed = candidate.len; + } else { + cp_slice = text[start .. start + n]; + consumed = n; } - shown = if (prefix > 0) shown[0..prefix] else "\u{FFFD}"; } - s.set(col, y, shown, style); - if (width == 2 and col + 1 < end) { - // spacer: empty cell under the wide glyph's tail - s.set(col + 1, y, "", style); + } + i.* += consumed; + var cp = decoded orelse 0xFFFD; + if (cp == '\r') return col; + if (cp == '\t') { + const spaces = @min(config.tab_now, end - col); + s.fill(col, y, spaces, 1, style); + return col + spaces; + } + // No other C0/C1 control or DEL reaches a font fallback either. + if (cp < ' ' or cp == 0x7f or (cp >= 0x80 and cp <= 0x9f)) { + cp = 0xFFFD; + cp_slice = "\u{FFFD}"; + } + const width: u16 = if (decoded == null or (cp < 0x80 and cp_slice.len == 1)) + 1 + else + @max(1, vaxis.gwidth.gwidth(cp_slice, .unicode)); + if (width == 2 and col + 1 >= end) return null; + var shown = cp_slice; + if (shown.len > @typeInfo(@FieldType(Cell, "text")).array.len) { + const cap = @typeInfo(@FieldType(Cell, "text")).array.len; + var prefix: usize = 0; + while (prefix < shown.len) { + const cp_len = std.unicode.utf8ByteSequenceLength(shown[prefix]) catch break; + if (prefix + cp_len > cap) break; + prefix += cp_len; } - col += width; + shown = if (prefix > 0) shown[0..prefix] else "\u{FFFD}"; } - return col; + s.set(col, y, shown, style); + if (width == 2 and col + 1 < end) { + // spacer: empty cell under the wide glyph's tail + s.set(col + 1, y, "", style); + } + return col + width; } pub fn fill(s: *Surface, x: u16, y: u16, w: u16, h: u16, style: CellStyle) void { @@ -633,6 +635,17 @@ pub const Surface = struct { } } + /// Recolour `n` cells from (x, y), keeping their glyphs: a selection's + /// ground, and its ink when `fg` is given. + pub fn tint(s: *Surface, x: u16, y: u16, n: u16, bg: [3]u8, fg: ?[3]u8) void { + for (x..x + n) |col| { + const cell = s.at(@intCast(col), y); + cell.default = false; + cell.style.bg = .{ .rgb = bg }; + if (fg) |ink| cell.style.fg = .{ .rgb = ink }; + } + } + pub fn clearRect(s: *Surface, x: u16, y: u16, w: u16, h: u16) void { var yy = y; while (yy < y + h) : (yy += 1) { @@ -683,66 +696,12 @@ test "surface print keeps combining and wide graphemes in their display cells" { } test "the ASCII fast path in surface print paints what the general arm paints" { + // The general arm alone, every grapheme through it. const ref = struct { fn print(s: *Surface, x: u16, y: u16, w: u16, text: []const u8, style: CellStyle) u16 { var col = x; - const end = x + w; var i: usize = 0; - while (i < text.len) { - if (col >= end) break; - const n = std.unicode.utf8ByteSequenceLength(text[i]) catch 0; - const decoded: ?u21 = if (n > 0 and i + n <= text.len) - (std.unicode.utf8Decode(text[i .. i + n]) catch null) - else - null; - var cp_slice: []const u8 = "\u{FFFD}"; - var consumed: usize = 1; - if (decoded != null) { - var git = uucode.grapheme.utf8Iterator(text[i..]); - if (git.nextGrapheme()) |g| { - const candidate = text[i .. i + g.end]; - if (std.unicode.utf8ValidateSlice(candidate)) { - cp_slice = candidate; - consumed = candidate.len; - } else { - cp_slice = text[i .. i + n]; - consumed = n; - } - } - } - i += consumed; - var cp = decoded orelse 0xFFFD; - if (cp == '\r') continue; - if (cp == '\t') { - const spaces = @min(config.tab_now, end - col); - s.fill(col, y, spaces, 1, style); - col += spaces; - continue; - } - if (cp < ' ' or cp == 0x7f or (cp >= 0x80 and cp <= 0x9f)) { - cp = 0xFFFD; - cp_slice = "\u{FFFD}"; - } - const width: u16 = if (decoded == null or (cp < 0x80 and cp_slice.len == 1)) - 1 - else - @max(1, vaxis.gwidth.gwidth(cp_slice, .unicode)); - if (width == 2 and col + 1 >= end) break; - var shown = cp_slice; - if (shown.len > @typeInfo(@FieldType(Cell, "text")).array.len) { - const cap = @typeInfo(@FieldType(Cell, "text")).array.len; - var prefix: usize = 0; - while (prefix < shown.len) { - const cp_len = std.unicode.utf8ByteSequenceLength(shown[prefix]) catch break; - if (prefix + cp_len > cap) break; - prefix += cp_len; - } - shown = if (prefix > 0) shown[0..prefix] else "\u{FFFD}"; - } - s.set(col, y, shown, style); - if (width == 2 and col + 1 < end) s.set(col + 1, y, "", style); - col += width; - } + while (i < text.len and col < x + w) col = s.printGeneral(col, x + w, y, text, &i, style) orelse break; return col; } }.print; |
