font: center text when adjust-cell-width is used

Fixes #1086
This commit is contained in:
Mitchell Hashimoto
2023-12-16 20:56:57 -08:00
parent b622df64b2
commit 481529393b
2 changed files with 33 additions and 3 deletions

View File

@ -384,11 +384,11 @@ pub const Face = struct {
};
atlas.set(region, buf);
const metrics = opts.grid_metrics orelse self.metrics;
const offset_y: i32 = offset_y: {
// Our Y coordinate in 3D is (0, 0) bottom left, +y is UP.
// We need to calculate our baseline from the bottom of a cell.
//const baseline_from_bottom: f64 = @floatFromInt(self.metrics.cell_baseline);
const metrics = opts.grid_metrics orelse self.metrics;
const baseline_from_bottom: f64 = @floatFromInt(metrics.cell_baseline);
// Next we offset our baseline by the bearing in the font. We
@ -398,6 +398,21 @@ pub const Face = struct {
break :offset_y @intFromFloat(@ceil(baseline_with_offset));
};
const offset_x: i32 = offset_x: {
var result: i32 = @intFromFloat(render_x);
// If our cell was resized to be wider then we center our
// glyph in the cell.
if (metrics.original_cell_width) |original_width| {
if (original_width < metrics.cell_width) {
const diff = (metrics.cell_width - original_width) / 2;
result += @intCast(diff);
}
}
break :offset_x result;
};
// Get our advance
var advances: [glyphs.len]macos.graphics.Size = undefined;
_ = self.font.getAdvancesForGlyphs(.horizontal, &glyphs, &advances);
@ -417,7 +432,7 @@ pub const Face = struct {
return .{
.width = width,
.height = height,
.offset_x = @intFromFloat(render_x),
.offset_x = offset_x,
.offset_y = offset_y,
.atlas_x = region.x,
.atlas_y = region.y,

View File

@ -430,6 +430,21 @@ pub const Face = struct {
break :offset_y glyph_metrics.bitmap_top + @as(c_int, @intCast(metrics.cell_baseline));
};
const offset_x: i32 = offset_x: {
var result: i32 = glyph_metrics.bitmap_left;
// If our cell was resized to be wider then we center our
// glyph in the cell.
if (metrics.original_cell_width) |original_width| {
if (original_width < metrics.cell_width) {
const diff = (metrics.cell_width - original_width) / 2;
result += @intCast(diff);
}
}
break :offset_x result;
};
// log.warn("renderGlyph width={} height={} offset_x={} offset_y={} glyph_metrics={}", .{
// tgt_w,
// tgt_h,
@ -442,7 +457,7 @@ pub const Face = struct {
return Glyph{
.width = tgt_w,
.height = tgt_h,
.offset_x = glyph_metrics.bitmap_left,
.offset_x = offset_x,
.offset_y = offset_y,
.atlas_x = region.x,
.atlas_y = region.y,