core, renderer: handle wide preedit chars

Fixes #855
This commit is contained in:
Mitchell Hashimoto
2023-11-10 17:26:36 -08:00
parent a8614815d6
commit ce4541dd61
3 changed files with 34 additions and 10 deletions

View File

@@ -1203,7 +1203,7 @@ fn rebuildCells(
// a cursor cell then we invert the colors on that and add it in so
// that we can always see it.
if (cursor_style_) |cursor_style| {
const real_cursor_cell = self.addCursor(screen, cursor_style);
const real_cursor_cell = self.addCursor(screen, cursor_style, preedit);
// If we have a preedit, we try to render the preedit text on top
// of the cursor.
@@ -1427,10 +1427,18 @@ fn addCursor(
self: *Metal,
screen: *terminal.Screen,
cursor_style: renderer.CursorStyle,
preedit: ?renderer.State.Preedit,
) ?*const mtl_shaders.Cell {
// Add the cursor. We render the cursor over the wide character if
// we're on the wide characer tail.
const cell, const x = cell: {
const wide, const x = cell: {
// If we have preedit text, our width is based on that.
if (preedit) |p| {
if (p.codepoint > 0) {
break :cell .{ p.wide, screen.cursor.x };
}
}
// The cursor goes over the screen cursor position.
const cell = screen.getCell(
.active,
@@ -1438,7 +1446,7 @@ fn addCursor(
screen.cursor.x,
);
if (!cell.attrs.wide_spacer_tail or screen.cursor.x == 0)
break :cell .{ cell, screen.cursor.x };
break :cell .{ cell.attrs.wide, screen.cursor.x };
// If we're part of a wide character, we move the cursor back to
// the actual character.
@@ -1446,7 +1454,7 @@ fn addCursor(
.active,
screen.cursor.y,
screen.cursor.x - 1,
), screen.cursor.x - 1 };
).attrs.wide, screen.cursor.x - 1 };
};
const color = self.cursor_color orelse self.foreground_color;
@@ -1466,7 +1474,7 @@ fn addCursor(
self.alloc,
font.sprite_index,
@intFromEnum(sprite),
.{ .cell_width = if (cell.attrs.wide) 2 else 1 },
.{ .cell_width = if (wide) 2 else 1 },
) catch |err| {
log.warn("error rendering cursor glyph err={}", .{err});
return null;
@@ -1478,7 +1486,7 @@ fn addCursor(
@as(f32, @floatFromInt(x)),
@as(f32, @floatFromInt(screen.cursor.y)),
},
.cell_width = if (cell.attrs.wide) 2 else 1,
.cell_width = if (wide) 2 else 1,
.color = .{ color.r, color.g, color.b, alpha },
.glyph_pos = .{ glyph.atlas_x, glyph.atlas_y },
.glyph_size = .{ glyph.width, glyph.height },

View File

@@ -34,4 +34,7 @@ pub const Preedit = struct {
/// This can also be "0" in which case we can know we're in a preedit
/// mode but we don't have any preedit text to render.
codepoint: u21 = 0,
/// True if the preedit text should be rendered "wide" (two cells)
wide: bool = false,
};