mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-08-02 14:57:31 +03:00
coretext: improved baseline calculation
This commit is contained in:
@ -30,30 +30,12 @@ pub const Line = opaque {
|
||||
self: *Line,
|
||||
opts: LineBoundsOptions,
|
||||
) graphics.Rect {
|
||||
// return @bitCast(c.CGRect, 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(
|
||||
return @bitCast(c.CTLineGetBoundsWithOptions(
|
||||
@ptrCast(self),
|
||||
opts.cval(),
|
||||
@ptrCast(&result),
|
||||
);
|
||||
|
||||
return result;
|
||||
));
|
||||
}
|
||||
|
||||
// See getBoundsWithOptions
|
||||
extern "c" fn zig_cabi_CTLineGetBoundsWithOptions(
|
||||
c.CTLineRef,
|
||||
c.CTLineBoundsOptions,
|
||||
*c.CGRect,
|
||||
) void;
|
||||
|
||||
pub fn getTypographicBounds(
|
||||
self: *Line,
|
||||
ascent: ?*f64,
|
||||
|
@ -282,7 +282,9 @@ pub fn renderGlyph(
|
||||
|
||||
const face = &self.faces.get(index.style).items[@intCast(index.idx)];
|
||||
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.
|
||||
|
@ -545,16 +545,22 @@ pub const Face = struct {
|
||||
// NOTE(mitchellh): For some reason, CTLineGetBoundsWithOptions
|
||||
// returns garbage and I can't figure out why... so we use the
|
||||
// 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;
|
||||
var descent: f64 = 0;
|
||||
var leading: f64 = 0;
|
||||
_ = line.getTypographicBounds(&ascent, &descent, &leading);
|
||||
// This is an alternate approach to the above to calculate the
|
||||
// baseline by simply using the ascender. Using this approach led
|
||||
// to less accurate results, but I'm leaving it here for reference.
|
||||
// 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 });
|
||||
|
||||
break :metrics .{
|
||||
.height = @floatCast(points[0].y - points[1].y),
|
||||
.ascent = @floatCast(ascent),
|
||||
.ascent = @floatCast(baseline),
|
||||
};
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user