font/sprite: manually determine which box codepoints are unadjusted

Some box codepoints (the cursor) want to use the original font metrics
height but others (corners) want to use the full grid height. I can't
see a better way to do this than manually maintaining a switch here. We
can add codepoints as needed.
This commit is contained in:
Mitchell Hashimoto
2023-12-13 10:32:16 -08:00
parent 20dbe9792e
commit 85b40874cf
2 changed files with 39 additions and 18 deletions

View File

@ -79,6 +79,19 @@ pub fn renderGlyph(
};
}
/// Returns true if this codepoint should be rendered with the
/// width/height set to unadjusted values.
pub fn unadjustedCodepoint(cp: u32) bool {
return switch (cp) {
@intFromEnum(Sprite.cursor_rect),
@intFromEnum(Sprite.cursor_hollow_rect),
@intFromEnum(Sprite.cursor_bar),
=> true,
else => false,
};
}
fn draw(self: Box, alloc: Allocator, canvas: *font.sprite.Canvas, cp: u32) !void {
switch (cp) {
0x2500 => self.draw_light_horizontal(canvas),

View File

@ -68,30 +68,38 @@ pub fn renderGlyph(
// Safe to ".?" because of the above assertion.
return switch (Kind.init(cp).?) {
.box => box: {
// For box fonts, we want to adjust the height of the box
// based on the original cell height, not the adjusted height.
// This is because adjusted cell height doesn't change font size.
const height, const offset = metrics: {
const metrics = opts.grid_metrics orelse break :metrics .{ self.height, 0 };
const height = metrics.original_cell_height orelse break :metrics .{ self.height, 0 };
// If our height shrunk, then we use the original adjusted
// height because we don't want to overflow the cell.
if (height >= self.height) break :metrics .{ self.height, 0 };
// The offset is divided by two because it is vertically
// centered.
break :metrics .{ height, (self.height - height) / 2 };
};
const f: Box = .{
const f: Box, const y_offset: u32 = face: {
// Expected, usual values.
var f: Box = .{
.width = width,
.height = height,
.height = self.height,
.thickness = self.thickness,
};
// If the codepoint is unadjusted then we want to adjust
// (heh) the width/height to the proper size and also record
// an offset to apply to our final glyph so it renders in the
// correct place because renderGlyph assumes full size.
var y_offset: u32 = 0;
if (Box.unadjustedCodepoint(cp)) unadjust: {
const metrics = opts.grid_metrics orelse break :unadjust;
const height = metrics.original_cell_height orelse break :unadjust;
// If our height shrunk, then we use the original adjusted
// height because we don't want to overflow the cell.
if (height >= self.height) break :unadjust;
// The offset is divided by two because it is vertically
// centered.
y_offset = (self.height - height) / 2;
f.height = height;
}
break :face .{ f, y_offset };
};
var g = try f.renderGlyph(alloc, atlas, cp);
g.offset_y += @intCast(offset);
g.offset_y += @intCast(y_offset);
break :box g;
},