calculate the cell width/height using the font size scale factor

This commit is contained in:
Mitchell Hashimoto
2022-04-19 20:05:24 -07:00
parent d250bcda65
commit df9fcf636f
2 changed files with 14 additions and 8 deletions

View File

@ -179,6 +179,12 @@ pub fn addGlyph(self: *FontAtlas, alloc: Allocator, v: anytype) !*Glyph {
return gop.value_ptr; return gop.value_ptr;
} }
/// Convert 16.6 pixel format to pixels based on the scale factor of the
/// current font size.
pub fn unitsToPxY(self: FontAtlas, units: i32) i32 {
return @intCast(i32, ftc.FT_MulFix(units, self.ft_face.*.size.*.metrics.y_scale) >> 6);
}
/// Convert 26.6 pixel format to f32 /// Convert 26.6 pixel format to f32
fn f26dot6ToFloat(v: ftc.FT_F26Dot6) f32 { fn f26dot6ToFloat(v: ftc.FT_F26Dot6) f32 {
return @intToFloat(f32, v >> 6); return @intToFloat(f32, v >> 6);

View File

@ -75,7 +75,7 @@ pub fn init(alloc: Allocator) !Grid {
errdefer atlas.deinit(alloc); errdefer atlas.deinit(alloc);
var font = try FontAtlas.init(atlas); var font = try FontAtlas.init(atlas);
errdefer font.deinit(alloc); errdefer font.deinit(alloc);
try font.loadFaceFromMemory(face_ttf, 30); try font.loadFaceFromMemory(face_ttf, 40);
// Load all visible ASCII characters and build our cell width based on // Load all visible ASCII characters and build our cell width based on
// the widest character that we see. // the widest character that we see.
@ -96,21 +96,23 @@ pub fn init(alloc: Allocator) !Grid {
// '_' which should live at the bottom of a cell. // '_' which should live at the bottom of a cell.
const cell_height: f32 = cell_height: { const cell_height: f32 = cell_height: {
// This is the height reported by the font face // This is the height reported by the font face
const face_height: i32 = font.ft_face.*.height >> 6; const face_height: i32 = font.unitsToPxY(font.ft_face.*.height);
// Determine the height of the underscore char // Determine the height of the underscore char
assert(font.ft_face != null); assert(font.ft_face != null);
const glyph = font.getGlyph('_').?; const glyph = font.getGlyph('_').?;
var res: i32 = font.ft_face.*.ascender >> 6; var res: i32 = font.unitsToPxY(font.ft_face.*.ascender);
res -= glyph.offset_y; res -= glyph.offset_y;
res += @intCast(i32, glyph.height); res += @intCast(i32, glyph.height);
// We take whatever is larger to account for some fonts that // We take whatever is larger to account for some fonts that
// put the underscore outside f the rectangle. // put the underscore outside f the rectangle.
if (res < face_height) res = face_height; if (res < face_height) res = face_height;
break :cell_height @intToFloat(f32, res); break :cell_height @intToFloat(f32, res);
}; };
log.debug("cell dimensions w={d} h={d}", .{ cell_width, cell_height }); const cell_baseline = cell_height - @intToFloat(f32, font.unitsToPxY(font.ft_face.*.ascender));
log.debug("cell dimensions w={d} h={d} baseline={d}", .{ cell_width, cell_height, cell_baseline });
// Create our shader // Create our shader
const program = try gl.Program.createVF( const program = try gl.Program.createVF(
@ -122,10 +124,7 @@ pub fn init(alloc: Allocator) !Grid {
const pbind = try program.use(); const pbind = try program.use();
defer pbind.unbind(); defer pbind.unbind();
try program.setUniform("cell_size", @Vector(2, f32){ cell_width, cell_height }); try program.setUniform("cell_size", @Vector(2, f32){ cell_width, cell_height });
try program.setUniform( try program.setUniform("glyph_baseline", cell_baseline);
"glyph_baseline",
cell_height - @intToFloat(f32, font.ft_face.*.ascender >> 6),
);
// Setup our VAO // Setup our VAO
const vao = try gl.VertexArray.create(); const vao = try gl.VertexArray.create();
@ -430,3 +429,4 @@ test "GridSize update rounding" {
} }
const face_ttf = @embedFile("../fonts/FiraCode-Regular.ttf"); const face_ttf = @embedFile("../fonts/FiraCode-Regular.ttf");
//const face_ttf = @embedFile("../fonts/Inconsolata-Regular.ttf");