a73x

src/cell_instance.zig

Ref:   Size: 2.2 KiB

//! Shared cell-instance helpers used by both the live renderer (main.zig)
//! and the capture renderer (capture.zig).

const std = @import("std");
const renderer = @import("renderer");
const font = @import("font");
const vt = @import("vt");

/// Appends 0-2 `renderer.Instance` entries for a single terminal cell:
///   - a filled-background quad when the cell's bg differs from the terminal
///     default bg (so transparent cells don't draw a quad at all);
///   - a glyph quad when `glyph_uv` is non-null (i.e. the cell has a
///     printable codepoint that was found in the atlas).
pub fn appendCellInstances(
    alloc: std.mem.Allocator,
    instances: *std.ArrayListUnmanaged(renderer.Instance),
    row_idx: u32,
    col_idx: u32,
    cell_w: u32,
    cell_h: u32,
    baseline: u32,
    glyph_uv: ?font.GlyphUV,
    bg_uv: font.GlyphUV,
    colors: vt.CellColors,
    default_bg: [4]f32,
) !void {
    if (!std.meta.eql(colors.bg, default_bg)) {
        try instances.append(alloc, .{
            .cell_pos = .{ @floatFromInt(col_idx), @floatFromInt(row_idx) },
            .glyph_size = .{ @floatFromInt(cell_w), @floatFromInt(cell_h) },
            .glyph_bearing = .{ 0, 0 },
            .uv_rect = .{ bg_uv.u0, bg_uv.v0, bg_uv.u1, bg_uv.v1 },
            .fg = colors.bg,
            .bg = colors.bg,
        });
    }

    const uv = glyph_uv orelse return;
    try instances.append(alloc, .{
        .cell_pos = .{ @floatFromInt(col_idx), @floatFromInt(row_idx) },
        .glyph_size = .{ @floatFromInt(uv.width), @floatFromInt(uv.height) },
        .glyph_bearing = .{
            @floatFromInt(uv.bearing_x),
            glyphTopOffset(baseline, uv.bearing_y),
        },
        .uv_rect = .{ uv.u0, uv.v0, uv.u1, uv.v1 },
        .fg = colors.fg,
        .bg = colors.bg,
    });
}

/// Returns the number of pixels from the top of the cell to the top of the
/// glyph bitmap, given the cell `baseline` (pixels from cell top to the
/// typographic baseline) and the glyph's `bearing_y` (pixels from baseline
/// to the top of the glyph bitmap, positive = up).
pub fn glyphTopOffset(baseline: u32, bearing_y: i32) f32 {
    return @as(f32, @floatFromInt(baseline)) - @as(f32, @floatFromInt(bearing_y));
}