coretext: improved baseline calculation

This commit is contained in:
Mitchell Hashimoto
2023-06-30 21:59:52 -07:00
parent 69396b0853
commit d39e3f5428
3 changed files with 16 additions and 26 deletions

View File

@ -30,30 +30,12 @@ pub const Line = opaque {
self: *Line, self: *Line,
opts: LineBoundsOptions, opts: LineBoundsOptions,
) graphics.Rect { ) graphics.Rect {
// return @bitCast(c.CGRect, c.CTLineGetBoundsWithOptions( return @bitCast(c.CTLineGetBoundsWithOptions(
// @ptrCast(c.CTLineRef, self),
// opts.cval(),
// ));
// We have to use a custom C wrapper here because there is some
// C ABI issue happening.
var result: graphics.Rect = undefined;
zig_cabi_CTLineGetBoundsWithOptions(
@ptrCast(self), @ptrCast(self),
opts.cval(), opts.cval(),
@ptrCast(&result), ));
);
return result;
} }
// See getBoundsWithOptions
extern "c" fn zig_cabi_CTLineGetBoundsWithOptions(
c.CTLineRef,
c.CTLineBoundsOptions,
*c.CGRect,
) void;
pub fn getTypographicBounds( pub fn getTypographicBounds(
self: *Line, self: *Line,
ascent: ?*f64, ascent: ?*f64,

View File

@ -282,7 +282,9 @@ pub fn renderGlyph(
const face = &self.faces.get(index.style).items[@intCast(index.idx)]; const face = &self.faces.get(index.style).items[@intCast(index.idx)];
try face.load(self.lib, self.size); try face.load(self.lib, self.size);
return try face.face.?.renderGlyph(alloc, atlas, glyph_index, max_height); const glyph = try face.face.?.renderGlyph(alloc, atlas, glyph_index, max_height);
// log.warn("GLYPH={}", .{glyph});
return glyph;
} }
/// The wasm-compatible API. /// The wasm-compatible API.

View File

@ -545,16 +545,22 @@ pub const Face = struct {
// NOTE(mitchellh): For some reason, CTLineGetBoundsWithOptions // NOTE(mitchellh): For some reason, CTLineGetBoundsWithOptions
// returns garbage and I can't figure out why... so we use the // returns garbage and I can't figure out why... so we use the
// raw ascender. // raw ascender.
const bounds = line.getBoundsWithOptions(.{ .exclude_leading = true });
const bounds_ascent = bounds.size.height + bounds.origin.y;
const baseline = @floor(bounds_ascent + 0.5);
var ascent: f64 = 0; // This is an alternate approach to the above to calculate the
var descent: f64 = 0; // baseline by simply using the ascender. Using this approach led
var leading: f64 = 0; // to less accurate results, but I'm leaving it here for reference.
_ = line.getTypographicBounds(&ascent, &descent, &leading); // var ascent: f64 = 0;
// var descent: f64 = 0;
// var leading: f64 = 0;
// _ = line.getTypographicBounds(&ascent, &descent, &leading);
//std.log.warn("ascent={} descent={} leading={}", .{ ascent, descent, leading }); //std.log.warn("ascent={} descent={} leading={}", .{ ascent, descent, leading });
break :metrics .{ break :metrics .{
.height = @floatCast(points[0].y - points[1].y), .height = @floatCast(points[0].y - points[1].y),
.ascent = @floatCast(ascent), .ascent = @floatCast(baseline),
}; };
}; };