Merge pull request #1666 from mitchellh/ct-runs

font/coretext: shaper may return multiple runs and that's okay
This commit is contained in:
Mitchell Hashimoto
2024-04-08 08:05:29 -07:00
committed by GitHub
3 changed files with 105 additions and 45 deletions

Binary file not shown.

View File

@ -265,9 +265,23 @@ pub const Shaper = struct {
// We should always have one run because we do our own run splitting.
const line = try macos.text.Line.createWithAttributedString(attr_str);
defer line.release();
// This keeps track of the current offsets within a single cell.
var cell_offset: struct {
cluster: u32 = 0,
x: f64 = 0,
y: f64 = 0,
} = .{};
self.cell_buf.clearRetainingCapacity();
// CoreText may generate multiple runs even though our input to
// CoreText is already split into runs by our own run iterator.
// The runs as far as I can tell are always sequential to each
// other so we can iterate over them and just append to our
// cell buffer.
const runs = line.getGlyphRuns();
assert(runs.getCount() == 1);
const ctrun = runs.getValueAtIndex(macos.text.Run, 0);
for (0..runs.getCount()) |i| {
const ctrun = runs.getValueAtIndex(macos.text.Run, i);
// Get our glyphs and positions
const glyphs = try ctrun.getGlyphs(alloc);
@ -278,16 +292,17 @@ pub const Shaper = struct {
assert(glyphs.len == advances.len);
assert(glyphs.len == indices.len);
// This keeps track of the current offsets within a single cell.
var cell_offset: struct {
cluster: u32 = 0,
x: f64 = 0,
y: f64 = 0,
} = .{};
for (
glyphs,
positions,
advances,
indices,
) |glyph, pos, advance, index| {
try self.cell_buf.ensureUnusedCapacity(
self.alloc,
glyphs.len,
);
self.cell_buf.clearRetainingCapacity();
try self.cell_buf.ensureTotalCapacity(self.alloc, glyphs.len);
for (glyphs, positions, advances, indices) |glyph, pos, advance, index| {
// Our cluster is also our cell X position. If the cluster changes
// then we need to reset our current cell offsets.
const cluster = state.codepoints.items[index].cluster;
@ -319,6 +334,7 @@ pub const Shaper = struct {
// );
}
//log.warn("-------------------------------", .{});
}
return self.cell_buf.items;
}
@ -329,6 +345,7 @@ pub const Shaper = struct {
pub fn prepare(self: *RunIteratorHook) !void {
try self.shaper.run_state.reset();
// log.warn("----------- run reset -------------", .{});
}
pub fn addCodepoint(self: RunIteratorHook, cp: u32, cluster: u32) !void {
@ -347,14 +364,18 @@ pub const Shaper = struct {
.codepoint = cp,
.cluster = cluster,
});
// log.warn("run cp={X}", .{cp});
// If the UTF-16 codepoint is a pair then we need to insert
// a dummy entry so that the CTRunGetStringIndices() function
// maps correctly.
if (pair) try state.codepoints.append(self.shaper.alloc, .{
if (pair) {
try state.codepoints.append(self.shaper.alloc, .{
.codepoint = 0,
.cluster = cluster,
});
log.warn("run pair cp=0", .{});
}
}
pub fn finalize(self: RunIteratorHook) !void {
@ -643,6 +664,40 @@ test "shape monaspace ligs" {
}
}
// https://github.com/mitchellh/ghostty/issues/1664
test "shape U+3C9 with JB Mono" {
const testing = std.testing;
const alloc = testing.allocator;
var testdata = try testShaperWithFont(alloc, .jetbrains_mono);
defer testdata.deinit();
{
var screen = try terminal.Screen.init(alloc, 10, 3, 0);
defer screen.deinit();
try screen.testWriteString("\u{03C9} foo");
var shaper = &testdata.shaper;
var it = shaper.runIterator(
testdata.cache,
&screen,
screen.pages.pin(.{ .screen = .{ .y = 0 } }).?,
null,
null,
);
var run_count: usize = 0;
var cell_count: usize = 0;
while (try it.next(alloc)) |run| {
run_count += 1;
const cells = try shaper.shape(run);
cell_count += cells.len;
}
try testing.expectEqual(@as(usize, 1), run_count);
try testing.expectEqual(@as(usize, 5), cell_count);
}
}
test "shape emoji width" {
const testing = std.testing;
const alloc = testing.allocator;
@ -1334,6 +1389,7 @@ const TestShaper = struct {
const TestFont = enum {
inconsolata,
jetbrains_mono,
monaspace_neon,
nerd_font,
};
@ -1348,6 +1404,7 @@ fn testShaperWithFont(alloc: Allocator, font_req: TestFont) !TestShaper {
const testEmojiText = @import("../test.zig").fontEmojiText;
const testFont = switch (font_req) {
.inconsolata => @import("../test.zig").fontRegular,
.jetbrains_mono => @import("../test.zig").fontJetBrainsMono,
.monaspace_neon => @import("../test.zig").fontMonaspaceNeon,
.nerd_font => @import("../test.zig").fontNerdFont,
};

View File

@ -15,6 +15,9 @@ pub const fontVariable = @embedFile("res/Lilex-VF.ttf");
/// Font with nerd fonts embedded.
pub const fontNerdFont = @embedFile("res/JetBrainsMonoNerdFont-Regular.ttf");
/// Specific font families below:
pub const fontJetBrainsMono = @embedFile("res/JetBrainsMonoNoNF-Regular.ttf");
/// Cozette is a unique font because it embeds some emoji characters
/// but has a text presentation.
pub const fontCozette = @embedFile("res/CozetteVector.ttf");