Merge pull request #355 from mitchellh/box-cras

font/sprite: fix horizontal dash drawing math for cells too small
This commit is contained in:
Mitchell Hashimoto
2023-08-29 11:32:56 -07:00
committed by GitHub

View File

@ -2414,7 +2414,7 @@ fn draw_dash_horizontal(
canvas: *font.sprite.Canvas, canvas: *font.sprite.Canvas,
count: u8, count: u8,
thick_px: u32, thick_px: u32,
gap: u32, desired_gap: u32,
) void { ) void {
assert(count >= 2 and count <= 4); assert(count >= 2 and count <= 4);
@ -2422,23 +2422,35 @@ fn draw_dash_horizontal(
// "- - -" => 2 gaps // "- - -" => 2 gaps
const gap_count = count - 1; const gap_count = count - 1;
// Determine the width of our dashes // Determine the width of each dash and the gap between them. We try
const dash_width = dash_width: { // to have gap match desired_gap but if our cell is too small then we
var gap_i = gap; // have to bring it down.
var dash_width = (self.width - (gap_count * gap_i)) / count; const adjusted: struct {
while (dash_width <= 0 and gap_i > 1) { dash_width: u32,
gap_i -= 1; gap: u32,
dash_width = (self.width - (gap_count * gap_i)) / count; } = adjusted: {
for (0..desired_gap) |i| {
const gap_width: u32 = desired_gap - @as(u32, @intCast(i));
const total_gap_width: u32 = gap_count * gap_width;
// This would make a negative and overflow our u32. A negative
// dash width is not allowed so we keep trying to fit it.
if (total_gap_width >= self.width) continue;
break :adjusted .{
.dash_width = (self.width - total_gap_width) / count,
.gap = gap_width,
};
} }
// If we can't fit any dashes then we just render a horizontal line. // In this case, there is no combination of gap width and dash
if (dash_width <= 0) { // width that would fit our desired number of dashes, so we just
// draw a horizontal line.
self.hline_middle(canvas, .light); self.hline_middle(canvas, .light);
return; return;
}
break :dash_width dash_width;
}; };
const dash_width = adjusted.dash_width;
const gap = adjusted.gap;
// Our total width should be less than our real width // Our total width should be less than our real width
assert(count * dash_width + gap_count * gap <= self.width); assert(count * dash_width + gap_count * gap <= self.width);