Merge pull request #1079 from mitchellh/unadjusted-box

font/sprite: manually determine which box codepoints are unadjusted
This commit is contained in:
Mitchell Hashimoto
2023-12-13 11:07:15 -08:00
committed by GitHub
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 { fn draw(self: Box, alloc: Allocator, canvas: *font.sprite.Canvas, cp: u32) !void {
switch (cp) { switch (cp) {
0x2500 => self.draw_light_horizontal(canvas), 0x2500 => self.draw_light_horizontal(canvas),

View File

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