From e9e5bbcc854ca83ea75f9c7ef34b566aa43e8569 Mon Sep 17 00:00:00 2001 From: Tim Culverhouse Date: Thu, 11 Apr 2024 08:31:57 -0500 Subject: DisplayWidth: explicitly set width to 2 when VS16 is found Explicitly set the width of an emoji to two when the next codepoint is a VS16 selector. Add unit test for this case. This is essentially the same PR as https://codeberg.org/dude_the_builder/ziglyph/pulls/11 --- src/DisplayWidth.zig | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/DisplayWidth.zig') diff --git a/src/DisplayWidth.zig b/src/DisplayWidth.zig index e547adf..621b8c1 100644 --- a/src/DisplayWidth.zig +++ b/src/DisplayWidth.zig @@ -39,6 +39,7 @@ pub fn strWidth(self: Self, str: []const u8) usize { if (cp_iter.next()) |ncp| { // emoji text sequence. if (ncp.code == 0xFE0E) w = 1; + if (ncp.code == 0xFE0F) w = 2; } // Only adding width of first non-zero-width code point. @@ -70,6 +71,9 @@ test "strWidth" { try testing.expectEqual(@as(usize, 2), self.strWidth("\u{26A1}")); // Lone emoji try testing.expectEqual(@as(usize, 1), self.strWidth("\u{26A1}\u{FE0E}")); // Text sequence try testing.expectEqual(@as(usize, 2), self.strWidth("\u{26A1}\u{FE0F}")); // Presentation sequence + try testing.expectEqual(@as(usize, 1), self.strWidth("\u{2764}")); // Default text presentation + try testing.expectEqual(@as(usize, 1), self.strWidth("\u{2764}\u{FE0E}")); // Default text presentation with VS15 selector + try testing.expectEqual(@as(usize, 2), self.strWidth("\u{2764}\u{FE0F}")); // Default text presentation with VS16 selector try testing.expectEqual(@as(usize, 0), self.strWidth("A\x08")); // Backspace try testing.expectEqual(@as(usize, 0), self.strWidth("\x7FA")); // DEL try testing.expectEqual(@as(usize, 0), self.strWidth("\x7FA\x08\x08")); // never less than o -- cgit v1.2.3