diff options
Diffstat (limited to 'src/surface.zig')
| -rw-r--r-- | src/surface.zig | 193 |
1 files changed, 76 insertions, 117 deletions
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; |
