Fix underline/strikeout position with larger fonts

This commit is contained in:
Mitchell Hashimoto
2022-10-06 15:40:43 -07:00
parent b18309187e
commit a44d4ea33b

View File

@ -346,13 +346,18 @@ fn calcMetrics(face: freetype.Face) Metrics {
// The underline position. This is a value from the top where the // The underline position. This is a value from the top where the
// underline should go. // underline should go.
const underline_position = underline_pos: { const underline_position = underline_pos: {
// The ascender is already scaled for scalable fonts, but the
// underline position is not.
const ascender_px = @intCast(i32, size_metrics.ascender) >> 6;
const declared_px = freetype.mulFix(
face.handle.*.underline_position,
@intCast(i32, face.handle.*.size.*.metrics.y_scale),
) >> 6;
// We use the declared underline position if its available // We use the declared underline position if its available
const declared = fontUnitsToPxY( const declared = ascender_px - declared_px;
face,
@intCast(i32, size_metrics.ascender) - face.handle.*.underline_position,
);
if (declared > 0) if (declared > 0)
break :underline_pos declared; break :underline_pos @intToFloat(f32, declared);
// If we have no declared underline position, we go slightly under the // If we have no declared underline position, we go slightly under the
// cell height (mainly: non-scalable fonts, i.e. emoji) // cell height (mainly: non-scalable fonts, i.e. emoji)
@ -369,10 +374,16 @@ fn calcMetrics(face: freetype.Face) Metrics {
pos: f32, pos: f32,
thickness: f32, thickness: f32,
} = if (face.getSfntTable(.os2)) |os2| .{ } = if (face.getSfntTable(.os2)) |os2| .{
.pos = fontUnitsToPxY(face, @maximum( .pos = pos: {
0, // Ascender is scaled, strikeout pos is not
@intCast(i32, size_metrics.ascender) - os2.yStrikeoutPosition, const ascender_px = @intCast(i32, size_metrics.ascender) >> 6;
)), const declared_px = freetype.mulFix(
os2.yStrikeoutPosition,
@intCast(i32, face.handle.*.size.*.metrics.y_scale),
) >> 6;
break :pos @intToFloat(f32, ascender_px - declared_px);
},
.thickness = @maximum(1, fontUnitsToPxY(face, os2.yStrikeoutSize)), .thickness = @maximum(1, fontUnitsToPxY(face, os2.yStrikeoutSize)),
} else .{ } else .{
.pos = cell_baseline * 0.6, .pos = cell_baseline * 0.6,