Merge pull request #1111 from mitchellh/adjust-width

font: center text when adjust-cell-width is used
This commit is contained in:
Mitchell Hashimoto
2023-12-16 20:58:22 -08:00
committed by GitHub
2 changed files with 33 additions and 3 deletions

View File

@ -384,11 +384,11 @@ pub const Face = struct {
}; };
atlas.set(region, buf); atlas.set(region, buf);
const metrics = opts.grid_metrics orelse self.metrics;
const offset_y: i32 = offset_y: { const offset_y: i32 = offset_y: {
// Our Y coordinate in 3D is (0, 0) bottom left, +y is UP. // 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. // We need to calculate our baseline from the bottom of a cell.
//const baseline_from_bottom: f64 = @floatFromInt(self.metrics.cell_baseline); //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); const baseline_from_bottom: f64 = @floatFromInt(metrics.cell_baseline);
// Next we offset our baseline by the bearing in the font. We // 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)); 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 // Get our advance
var advances: [glyphs.len]macos.graphics.Size = undefined; var advances: [glyphs.len]macos.graphics.Size = undefined;
_ = self.font.getAdvancesForGlyphs(.horizontal, &glyphs, &advances); _ = self.font.getAdvancesForGlyphs(.horizontal, &glyphs, &advances);
@ -417,7 +432,7 @@ pub const Face = struct {
return .{ return .{
.width = width, .width = width,
.height = height, .height = height,
.offset_x = @intFromFloat(render_x), .offset_x = offset_x,
.offset_y = offset_y, .offset_y = offset_y,
.atlas_x = region.x, .atlas_x = region.x,
.atlas_y = region.y, .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)); 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={}", .{ // log.warn("renderGlyph width={} height={} offset_x={} offset_y={} glyph_metrics={}", .{
// tgt_w, // tgt_w,
// tgt_h, // tgt_h,
@ -442,7 +457,7 @@ pub const Face = struct {
return Glyph{ return Glyph{
.width = tgt_w, .width = tgt_w,
.height = tgt_h, .height = tgt_h,
.offset_x = glyph_metrics.bitmap_left, .offset_x = offset_x,
.offset_y = offset_y, .offset_y = offset_y,
.atlas_x = region.x, .atlas_x = region.x,
.atlas_y = region.y, .atlas_y = region.y,