From 6b2ca86ed83800ec68ea0af15f427d7ac159a9df Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 24 Nov 2022 09:12:34 -0800 Subject: [PATCH] fix offset for box glyphs --- src/font/BoxFont.zig | 22 ++++++++++++++++++---- src/font/Group.zig | 1 + src/renderer/OpenGL.zig | 8 +++++--- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/font/BoxFont.zig b/src/font/BoxFont.zig index 9084a7841..4dfb4eb2e 100644 --- a/src/font/BoxFont.zig +++ b/src/font/BoxFont.zig @@ -11,6 +11,8 @@ const pixman = @import("pixman"); const font = @import("main.zig"); const Atlas = @import("../Atlas.zig"); +const log = std.log.scoped(.box_font); + /// The cell width and height because the boxes are fit perfectly /// into a cell so that they all properly connect with zero spacing. width: u32, @@ -49,11 +51,13 @@ pub fn renderGlyph( alloc: Allocator, atlas: *Atlas, cp: u32, + font_size: font.face.DesiredSize, ) !font.Glyph { assert(atlas.format == .greyscale); // TODO: render depending on cp _ = cp; + _ = font_size; // Determine the config for our image buffer. The images we draw // for boxes are always 8bpp @@ -114,11 +118,16 @@ pub fn renderGlyph( atlas.set(region, buffer); } + // Our coordinates start at the BOTTOM for our renderers so we have to + // specify an offset of the full height because we rendered a full size + // cell. + const offset_y = @intCast(i32, self.height); + return font.Glyph{ .width = self.width, .height = self.height, .offset_x = 0, - .offset_y = 0, + .offset_y = offset_y, .atlas_x = region.x, .atlas_y = region.y, .advance_x = @intToFloat(f32, self.width), @@ -130,8 +139,8 @@ fn draw_box_drawings_light_horizontal(self: BoxFont, img: *pixman.Image) void { } fn hline_middle(self: BoxFont, img: *pixman.Image, thickness: Thickness) void { - const height = thickness.height(self.thickness); - self.hline(img, 0, self.width, (self.height - height) / 2, height); + const thick_px = thickness.height(self.thickness); + self.hline(img, 0, self.width, (self.height - thick_px) / 2, thick_px); } fn hline( @@ -162,7 +171,12 @@ test "all" { defer atlas_greyscale.deinit(alloc); const face: BoxFont = .{ .width = 18, .height = 36, .thickness = 2 }; - const glyph = try face.renderGlyph(alloc, &atlas_greyscale, 0x2500); + const glyph = try face.renderGlyph( + alloc, + &atlas_greyscale, + 0x2500, + .{ .points = 12 }, + ); try testing.expectEqual(@as(u32, face.width), glyph.width); try testing.expectEqual(@as(u32, face.height), glyph.height); } diff --git a/src/font/Group.zig b/src/font/Group.zig index b2cd242ea..8003f1015 100644 --- a/src/font/Group.zig +++ b/src/font/Group.zig @@ -282,6 +282,7 @@ pub fn renderGlyph( alloc, atlas, glyph_index, + self.size, ), }; diff --git a/src/renderer/OpenGL.zig b/src/renderer/OpenGL.zig index 9336c1de7..d8ee2fa89 100644 --- a/src/renderer/OpenGL.zig +++ b/src/renderer/OpenGL.zig @@ -958,7 +958,6 @@ pub fn updateCell( // If the cell has a character, draw it if (cell.char > 0) { // Render - const face = try self.font_group.group.faceFromIndex(shaper_run.font_index); const glyph = try self.font_group.renderGlyph( self.alloc, shaper_run.font_index, @@ -967,8 +966,11 @@ pub fn updateCell( ); // If we're rendering a color font, we use the color atlas - var mode: GPUCellMode = .fg; - if (face.presentation == .emoji) mode = .fg_color; + const presentation = try self.font_group.group.presentationFromIndex(shaper_run.font_index); + var mode: GPUCellMode = switch (presentation) { + .text => .fg, + .emoji => .fg_color, + }; self.cells.appendAssumeCapacity(.{ .mode = mode,