From 481529393baee295b929f99c88fb83e58346102c Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 16 Dec 2023 20:56:57 -0800 Subject: [PATCH] font: center text when adjust-cell-width is used Fixes #1086 --- src/font/face/coretext.zig | 19 +++++++++++++++++-- src/font/face/freetype.zig | 17 ++++++++++++++++- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/font/face/coretext.zig b/src/font/face/coretext.zig index 2f6bd6d16..274557821 100644 --- a/src/font/face/coretext.zig +++ b/src/font/face/coretext.zig @@ -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, diff --git a/src/font/face/freetype.zig b/src/font/face/freetype.zig index 362588e38..75ec3e4af 100644 --- a/src/font/face/freetype.zig +++ b/src/font/face/freetype.zig @@ -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,