Files
ghostty/pkg/macos/text/run.zig
Mitchell Hashimoto 8dd810521c Fix Weird Behavior in CoreText Shaper (#9002)
You can pretty simply reproduce a crash on `main` in `Debug` mode by
running `printf "مرحبًا \n"` with your primary font set to one that
supports Arabic such as Cascadia Code/Mono or Kawkab Mono, which will
cause CoreText to output the shaped glyphs non-monotonically which hits
the assert we have in the renderer.

In `ReleaseFast` this assert is skipped and because we already moved
ahead to the space glyph (which belongs at the end but is emitted first)
all of the glyphs up to that point are lost. I believe this is probably
the cause of #8280, I tested and this change seems to fix it at least.

Included in this PR is a little optimization: we were allocating buffers
to copy glyphs etc. from runs to every time, even though CoreText
provides `CTRunGet*Ptr` functions which get *pointers* to the internal
storage of these values- these aren't guaranteed to return a usable
pointer but in that case we can always fall back to allocating again.
Also avoided allocation while processing glyphs by ensuring capacity
beforehand immediately after creating the `CTLine`.

The performance impact of this PR is negligible on my machine and
actually seems to be positive, probably due to avoiding allocations if I
had to guess.
2025-10-06 09:04:48 -07:00

118 lines
3.6 KiB
Zig

const std = @import("std");
const assert = std.debug.assert;
const Allocator = std.mem.Allocator;
const foundation = @import("../foundation.zig");
const graphics = @import("../graphics.zig");
const text = @import("../text.zig");
const c = @import("c.zig").c;
pub const Run = opaque {
pub fn release(self: *Run) void {
foundation.CFRelease(self);
}
pub fn getGlyphCount(self: *Run) usize {
return @intCast(c.CTRunGetGlyphCount(@ptrCast(self)));
}
pub fn getGlyphsPtr(self: *Run) ?[]const graphics.Glyph {
const len = self.getGlyphCount();
if (len == 0) return &.{};
const ptr: [*c]const graphics.Glyph = @ptrCast(
c.CTRunGetGlyphsPtr(@ptrCast(self)),
);
if (ptr == null) return null;
return ptr[0..len];
}
pub fn getGlyphs(self: *Run, alloc: Allocator) ![]const graphics.Glyph {
const len = self.getGlyphCount();
const ptr = try alloc.alloc(graphics.Glyph, len);
errdefer alloc.free(ptr);
c.CTRunGetGlyphs(
@ptrCast(self),
.{ .location = 0, .length = 0 },
@ptrCast(ptr.ptr),
);
return ptr;
}
pub fn getPositionsPtr(self: *Run) ?[]const graphics.Point {
const len = self.getGlyphCount();
if (len == 0) return &.{};
const ptr: [*c]const graphics.Point = @ptrCast(
c.CTRunGetPositionsPtr(@ptrCast(self)),
);
if (ptr == null) return null;
return ptr[0..len];
}
pub fn getPositions(self: *Run, alloc: Allocator) ![]const graphics.Point {
const len = self.getGlyphCount();
const ptr = try alloc.alloc(graphics.Point, len);
errdefer alloc.free(ptr);
c.CTRunGetPositions(
@ptrCast(self),
.{ .location = 0, .length = 0 },
@ptrCast(ptr.ptr),
);
return ptr;
}
pub fn getAdvancesPtr(self: *Run) ?[]const graphics.Size {
const len = self.getGlyphCount();
if (len == 0) return &.{};
const ptr: [*c]const graphics.Size = @ptrCast(
c.CTRunGetAdvancesPtr(@ptrCast(self)),
);
if (ptr == null) return null;
return ptr[0..len];
}
pub fn getAdvances(self: *Run, alloc: Allocator) ![]const graphics.Size {
const len = self.getGlyphCount();
const ptr = try alloc.alloc(graphics.Size, len);
errdefer alloc.free(ptr);
c.CTRunGetAdvances(
@ptrCast(self),
.{ .location = 0, .length = 0 },
@ptrCast(ptr.ptr),
);
return ptr;
}
pub fn getStringIndicesPtr(self: *Run) ?[]const usize {
const len = self.getGlyphCount();
if (len == 0) return &.{};
const ptr: [*c]const usize = @ptrCast(
c.CTRunGetStringIndicesPtr(@ptrCast(self)),
);
if (ptr == null) return null;
return ptr[0..len];
}
pub fn getStringIndices(self: *Run, alloc: Allocator) ![]const usize {
const len = self.getGlyphCount();
const ptr = try alloc.alloc(usize, len);
errdefer alloc.free(ptr);
c.CTRunGetStringIndices(
@ptrCast(self),
.{ .location = 0, .length = 0 },
@ptrCast(ptr.ptr),
);
return ptr;
}
pub fn getStatus(self: *Run) Status {
return @bitCast(c.CTRunGetStatus(@ptrCast(self)));
}
};
/// https://developer.apple.com/documentation/coretext/ctrunstatus?language=objc
pub const Status = packed struct(u32) {
right_to_left: bool,
non_monotonic: bool,
has_non_identity_matrix: bool,
_pad: u29 = 0,
};