Skip to content

Truly independent web browser

LibGfx+LibUnicode: Draw emoji modifier and ZWJ sequences as one emoji

sideshowbarker2094a004476e
diff --git a/Libraries/LibGfx/FontCascadeList.cpp b/Libraries/LibGfx/FontCascadeList.cpp
index f83d5800e1e..6feebf7a0bf 100644
--- a/Libraries/LibGfx/FontCascadeList.cpp
+++ b/Libraries/LibGfx/FontCascadeList.cpp
@@ -84,7 +84,7 @@ FontCascadeList::~FontCascadeList()
ladybird_gfx_frozen_font_list_release(m_frozen_list);
}
-EmojiPresentationResult emoji_presentation_for_code_point(u32 code_point, Optional<u32> next_code_point)
+EmojiPresentationResult emoji_presentation_for_code_point(u32 code_point, Optional<u32> next_code_point, Optional<u32> code_point_after_next)
{
// VARIATION SELECTOR-16 (emoji)
if (next_code_point == 0xFE0Fu)
@@ -93,6 +93,22 @@ EmojiPresentationResult emoji_presentation_for_code_point(u32 code_point, Option
if (next_code_point == 0xFE0Eu)
return { EmojiPresentation::Text, ForcedPresentation::Yes };
+ // https://www.unicode.org/reports/tr51/#Diversity
+ // "Emoji presentation selectors are neither needed nor recommended for emoji characters when they are followed by
+ // emoji modifiers, and should not be used in newly generated emoji modifier sequences; the emoji modifier
+ // automatically implies the emoji presentation style."
+ if (next_code_point.has_value() && *next_code_point >= 0x1F3FBu && *next_code_point <= 0x1F3FFu && Unicode::code_point_has_emoji_modifier_base_property(code_point))
+ return { EmojiPresentation::Emoji, ForcedPresentation::Yes };
+
+ // https://www.unicode.org/reports/tr51/#def_emoji_zwj_sequence
+ // NB: A ZERO WIDTH JOINER between two pictographic characters joins them into an emoji ZWJ sequence, which is one
+ // emoji, so it takes emoji presentation even when the first character's own default is text, as in U+1F441
+ // U+200D U+1F5E8. A joiner that no pictographic character follows joins nothing.
+ if (next_code_point == 0x200Du && code_point_after_next.has_value()
+ && Unicode::code_point_has_extended_pictographic_property(code_point)
+ && Unicode::code_point_has_extended_pictographic_property(*code_point_after_next))
+ return { EmojiPresentation::Emoji, ForcedPresentation::Yes };
+
if (Unicode::code_point_has_emoji_presentation_property(code_point))
return { EmojiPresentation::Emoji, ForcedPresentation::No };
return { EmojiPresentation::Text, ForcedPresentation::No };
@@ -451,7 +467,7 @@ void const* ladybird_gfx_font_cascade_list_first(void const*);
void ladybird_gfx_font_cascade_list_ref(void const*);
void ladybird_gfx_font_cascade_list_unref(void const*);
bool ladybird_gfx_font_cascade_list_equals(void const*, void const*);
-u8 ladybird_gfx_emoji_presentation_for_code_point(u32, u32, bool);
+u8 ladybird_gfx_emoji_presentation_for_code_point(u32, u32, bool, u32, bool);
void const* ladybird_gfx_font_cascade_list_frozen(void const*);
void const* ladybird_gfx_cascade_snapshot_begin(void const*);
void ladybird_gfx_cascade_snapshot_header(void const*, Gfx::FFI::FfiCascadeSnapshotHeader*);
@@ -496,10 +512,12 @@ extern "C" bool ladybird_gfx_font_cascade_list_equals(void const* list, void con
return static_cast<Gfx::FontCascadeList const*>(list)->equals(*static_cast<Gfx::FontCascadeList const*>(other));
}
-extern "C" u8 ladybird_gfx_emoji_presentation_for_code_point(u32 code_point, u32 next_code_point, bool has_next_code_point)
+extern "C" u8 ladybird_gfx_emoji_presentation_for_code_point(u32 code_point, u32 next_code_point, bool has_next_code_point, u32 code_point_after_next, bool has_code_point_after_next)
{
auto result = Gfx::emoji_presentation_for_code_point(
- code_point, has_next_code_point ? Optional<u32> { next_code_point } : Optional<u32> {});
+ code_point,
+ has_next_code_point ? Optional<u32> { next_code_point } : Optional<u32> {},
+ has_code_point_after_next ? Optional<u32> { code_point_after_next } : Optional<u32> {});
u8 encoded = 0;
if (result.presentation == Gfx::EmojiPresentation::Emoji)
encoded |= 1;
diff --git a/Libraries/LibGfx/FontCascadeList.h b/Libraries/LibGfx/FontCascadeList.h
index 2fc036909ab..183b2525c30 100644
--- a/Libraries/LibGfx/FontCascadeList.h
+++ b/Libraries/LibGfx/FontCascadeList.h
@@ -31,7 +31,7 @@ struct EmojiPresentationResult {
ForcedPresentation forced { ForcedPresentation::No };
};
-EmojiPresentationResult emoji_presentation_for_code_point(u32 code_point, Optional<u32> next_code_point);
+EmojiPresentationResult emoji_presentation_for_code_point(u32 code_point, Optional<u32> next_code_point, Optional<u32> code_point_after_next = {});
enum class PendingFontState : u8 {
Invisible,
diff --git a/Libraries/LibGfx/Rust/src/font.rs b/Libraries/LibGfx/Rust/src/font.rs
index dad3fde82ef..fe44da74dd4 100644
--- a/Libraries/LibGfx/Rust/src/font.rs
+++ b/Libraries/LibGfx/Rust/src/font.rs
@@ -36,6 +36,8 @@ unsafe extern "C" {
code_point: u32,
next_code_point: u32,
has_next_code_point: bool,
+ code_point_after_next: u32,
+ has_code_point_after_next: bool,
) -> u8;
}
@@ -293,16 +295,24 @@ pub fn code_point_extends_emoji_sequence(code_point: u32) -> bool {
/// https://www.unicode.org/reports/tr51/#def_emoji_zwj_element
pub fn code_point_ends_emoji_zwj_element(code_point: u32) -> bool {
matches!(code_point, 0xfe0f | 0x20e3 | 0x1f3fb..=0x1f3ff | 0xe007f)
- || emoji_presentation_for_code_point(code_point, None).is_emoji
+ || emoji_presentation_for_code_point(code_point, None, None).is_emoji
}
-pub fn emoji_presentation_for_code_point(code_point: u32, next_code_point: Option<u32>) -> EmojiPresentation {
+/// The presentation `code_point` takes, given the code point after it and the one after that, which can make it the
+/// start of an emoji sequence.
+pub fn emoji_presentation_for_code_point(
+ code_point: u32,
+ next_code_point: Option<u32>,
+ code_point_after_next: Option<u32>,
+) -> EmojiPresentation {
// SAFETY: The lookup reads only immutable Unicode tables.
let encoded = unsafe {
ladybird_gfx_emoji_presentation_for_code_point(
code_point,
next_code_point.unwrap_or(0),
next_code_point.is_some(),
+ code_point_after_next.unwrap_or(0),
+ code_point_after_next.is_some(),
)
};
EmojiPresentation {
diff --git a/Libraries/LibUnicode/CharacterTypes.cpp b/Libraries/LibUnicode/CharacterTypes.cpp
index bac5e1a3c0a..e8927292545 100644
--- a/Libraries/LibUnicode/CharacterTypes.cpp
+++ b/Libraries/LibUnicode/CharacterTypes.cpp
@@ -275,6 +275,16 @@ bool code_point_has_emoji_presentation_property(u32 code_point)
return code_point_has_property(code_point, UCHAR_EMOJI_PRESENTATION);
}
+bool code_point_has_emoji_modifier_base_property(u32 code_point)
+{
+ return code_point_has_property(code_point, UCHAR_EMOJI_MODIFIER_BASE);
+}
+
+bool code_point_has_extended_pictographic_property(u32 code_point)
+{
+ return code_point_has_property(code_point, UCHAR_EXTENDED_PICTOGRAPHIC);
+}
+
bool code_point_has_default_ignorable_code_point_property(u32 code_point)
{
return code_point_has_property(code_point, UCHAR_DEFAULT_IGNORABLE_CODE_POINT);
diff --git a/Libraries/LibUnicode/CharacterTypes.h b/Libraries/LibUnicode/CharacterTypes.h
index a9c4180678a..4b08c25725b 100644
--- a/Libraries/LibUnicode/CharacterTypes.h
+++ b/Libraries/LibUnicode/CharacterTypes.h
@@ -34,6 +34,8 @@ bool code_point_has_property(u32 code_point, Property property, CaseSensitivity
bool code_point_has_emoji_property(u32 code_point);
bool code_point_has_emoji_presentation_property(u32 code_point);
+bool code_point_has_emoji_modifier_base_property(u32 code_point);
+bool code_point_has_extended_pictographic_property(u32 code_point);
bool code_point_has_default_ignorable_code_point_property(u32 code_point);
bool code_point_has_identifier_start_property(u32 code_point);
bool code_point_has_identifier_continue_property(u32 code_point);
diff --git a/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/text.rs b/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/text.rs
index 536ed013e80..d3de410eb81 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/text.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/text.rs
@@ -53,8 +53,16 @@ pub(crate) fn shape_text(
// The presentation the code point at `index` has: Its default one, or the one a variation selector after it asks
// for.
let presentation_at = |index: usize, code_point: u32, length: usize| -> EmojiPresentation {
- let next_code_point = (index + length < text.len()).then(|| code_point_at(text, index + length).0);
- emoji_presentation_for_code_point(code_point, next_code_point)
+ let next = (index + length < text.len()).then(|| code_point_at(text, index + length));
+ let code_point_after_next = next.and_then(|(_, next_length)| {
+ let index_after_next = index + length + next_length;
+ (index_after_next < text.len()).then(|| code_point_at(text, index_after_next).0)
+ });
+ emoji_presentation_for_code_point(
+ code_point,
+ next.map(|(next_code_point, _)| next_code_point),
+ code_point_after_next,
+ )
};
let mut runs = Vec::new();
@@ -99,7 +107,11 @@ pub(crate) fn shape_text(
// does one before a character with a variation selector that asks for its text presentation: Those are no
// part of an emoji sequence.
if code_point == ZERO_WIDTH_JOINER {
- joiner_joins_emoji = code_point_ends_emoji_zwj_element(previous_code_point);
+ // NB: A pictograph that defaults to text presentation ends an emoji ZWJ element too, when the joiner
+ // joins it to another pictograph.
+ let joined_code_point = (index + length < text.len()).then(|| code_point_at(text, index + length).0);
+ joiner_joins_emoji = code_point_ends_emoji_zwj_element(previous_code_point)
+ || emoji_presentation_for_code_point(previous_code_point, Some(code_point), joined_code_point).is_emoji;
}
let presentation = presentation_at(index, code_point, length);
let is_joined = previous_code_point == ZERO_WIDTH_JOINER
diff --git a/Libraries/LibWeb/Rust/src/gfx_test_stubs.rs b/Libraries/LibWeb/Rust/src/gfx_test_stubs.rs
index c929416807e..60c03b73d95 100644
--- a/Libraries/LibWeb/Rust/src/gfx_test_stubs.rs
+++ b/Libraries/LibWeb/Rust/src/gfx_test_stubs.rs
@@ -25,6 +25,8 @@ extern "C" fn ladybird_gfx_emoji_presentation_for_code_point(
_code_point: u32,
_next_code_point: u32,
_has_next_code_point: bool,
+ _code_point_after_next: u32,
+ _has_code_point_after_next: bool,
) -> u8 {
unreachable!("no unit test reads a font");
}
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
index 1922e970585..7faaf1ce82b 100644
--- a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
@@ -79,7 +79,7 @@ impl TextOverflowEllipsis {
.font()
.first_available_font()
.expect("layout read a font group that never received a frozen font list");
- let presentation = libgfx_rust::font::emoji_presentation_for_code_point(ELLIPSIS_CODE_POINT, None);
+ let presentation = libgfx_rust::font::emoji_presentation_for_code_point(ELLIPSIS_CODE_POINT, None, None);
// NB: Where the block's first available font has no glyph for U+2026 HORIZONTAL ELLIPSIS, three periods stand
// for it, rather than an ellipsis from whatever font the cascade falls back to.
const ELLIPSIS: [u16; 1] = [ELLIPSIS_CODE_POINT as u16];
@@ -406,12 +406,14 @@ fn apply_block_ellipsis(
ellipsis_text: &[u16],
) {
let (style_source, style) = (context.containing_block, context.style(context.containing_block));
- let first_code_point = char::decode_utf16(ellipsis_text.iter().copied())
- .next()
- .map_or(char::REPLACEMENT_CHARACTER, |result| {
- result.unwrap_or(char::REPLACEMENT_CHARACTER)
- }) as u32;
- let presentation = libgfx_rust::font::emoji_presentation_for_code_point(first_code_point, None);
+ // NB: The code points after the first can make it the start of an emoji sequence.
+ let mut code_points = char::decode_utf16(ellipsis_text.iter().copied())
+ .map(|result| result.unwrap_or(char::REPLACEMENT_CHARACTER) as u32);
+ let first_code_point = code_points.next().unwrap_or(char::REPLACEMENT_CHARACTER as u32);
+ let next_code_point = code_points.next();
+ let code_point_after_next = code_points.next();
+ let presentation =
+ libgfx_rust::font::emoji_presentation_for_code_point(first_code_point, next_code_point, code_point_after_next);
let font = style
.frozen_font_list()
.font_for_code_point(first_code_point, presentation);
diff --git a/Libraries/LibWeb/Rust/src/layout/math_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/math_formatting_context.rs
index 20026f5dd2f..6a0868ae051 100644
--- a/Libraries/LibWeb/Rust/src/layout/math_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/math_formatting_context.rs
@@ -3187,7 +3187,7 @@ impl RadicalMetrics {
const SQUARE_ROOT: u32 = 0x221A;
let font = style.frozen_font_list().font_for_code_point(
SQUARE_ROOT,
- libgfx_rust::font::emoji_presentation_for_code_point(SQUARE_ROOT, None),
+ libgfx_rust::font::emoji_presentation_for_code_point(SQUARE_ROOT, None, None),
);
let glyph_id = font.glyph_id_for_code_point(SQUARE_ROOT);
// The radical gap is given by RadicalVerticalGap if the math-style is compact and
diff --git a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
index f4ac96fe7a0..e7d731672bd 100644
--- a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
@@ -2033,7 +2033,11 @@ impl<'pass> SvgFormattingContext<'pass> {
let code_point = code_point_at(text, offset);
let next_offset = offset + code_point_length_at(text, offset);
let next_code_point = (next_offset < text.len()).then(|| code_point_at(text, next_offset));
- libgfx_rust::font::emoji_presentation_for_code_point(code_point, next_code_point)
+ let offset_after_next =
+ next_offset + next_code_point.map_or(0, |_| code_point_length_at(text, next_offset));
+ let code_point_after_next = (next_code_point.is_some() && offset_after_next < text.len())
+ .then(|| code_point_at(text, offset_after_next));
+ libgfx_rust::font::emoji_presentation_for_code_point(code_point, next_code_point, code_point_after_next)
};
let font_for =
|offset: usize| frozen_font_list.font_for_code_point(code_point_at(text, offset), presentation_at(offset));
@@ -2050,7 +2054,17 @@ impl<'pass> SvgFormattingContext<'pass> {
// and neither does one before a character with a variation selector that asks for its text
// presentation: Those are no part of an emoji sequence.
if code_point == ZERO_WIDTH_JOINER {
- joiner_joins_emoji = libgfx_rust::font::code_point_ends_emoji_zwj_element(previous_code_point);
+ // NB: A pictograph that defaults to text presentation ends an emoji ZWJ element too, when the joiner
+ // joins it to another pictograph.
+ let joined_offset = offset + code_point_length_at(text, offset);
+ let joined_code_point = (joined_offset < text.len()).then(|| code_point_at(text, joined_offset));
+ joiner_joins_emoji = libgfx_rust::font::code_point_ends_emoji_zwj_element(previous_code_point)
+ || libgfx_rust::font::emoji_presentation_for_code_point(
+ previous_code_point,
+ Some(code_point),
+ joined_code_point,
+ )
+ .is_emoji;
}
let presentation = presentation_at(offset);
let is_joined = previous_code_point == ZERO_WIDTH_JOINER
diff --git a/Libraries/LibWeb/Rust/src/layout/text_chunker.rs b/Libraries/LibWeb/Rust/src/layout/text_chunker.rs
index 4ac9b7edd5a..274512ddd01 100644
--- a/Libraries/LibWeb/Rust/src/layout/text_chunker.rs
+++ b/Libraries/LibWeb/Rust/src/layout/text_chunker.rs
@@ -1223,15 +1223,19 @@ impl<'text> TextChunker<'text> {
fn emoji_presentation_at(&self, code_unit_offset: usize, code_point: u32) -> EmojiPresentation {
let next_offset = code_unit_offset + code_unit_length_for_code_point(code_point);
let next_code_point = (next_offset < self.text.len()).then(|| code_point_at(self.text, next_offset));
+ let code_point_after_next = next_code_point.and_then(|next_code_point| {
+ let offset_after_next = next_offset + code_unit_length_for_code_point(next_code_point);
+ (offset_after_next < self.text.len()).then(|| code_point_at(self.text, offset_after_next))
+ });
// OPTIMIZATION: No code point below U+0300 has the Emoji_Presentation property, so only a variation selector
- // can give one of them anything but the default text presentation.
- let default_presentation = if code_point < 0x300 && !matches!(next_code_point, Some(0xfe0e | 0xfe0f)) {
+ // or a zero width joiner can give one of them anything but the default text presentation.
+ let default_presentation = if code_point < 0x300 && !matches!(next_code_point, Some(0xfe0e | 0xfe0f | 0x200d)) {
EmojiPresentation {
is_emoji: false,
forced: false,
}
} else {
- emoji_presentation_for_code_point(code_point, next_code_point)
+ emoji_presentation_for_code_point(code_point, next_code_point, code_point_after_next)
};
// OPTIMIZATION: Below U+0300, only '#', '*', the ASCII digits, U+00A9 and U+00AE have the Emoji property.
let has_emoji_property = if code_point < 0x300 {
@@ -1241,7 +1245,8 @@ impl<'text> TextChunker<'text> {
};
debug_assert!(
code_point >= 0x300 || {
- let presentation = emoji_presentation_for_code_point(code_point, next_code_point);
+ let presentation =
+ emoji_presentation_for_code_point(code_point, next_code_point, code_point_after_next);
let has_property = unicode_layout_code_point_has_emoji_property(code_point);
presentation.is_emoji == default_presentation.is_emoji
&& presentation.forced == default_presentation.forced
diff --git a/Tests/LibWeb/Text/expected/css/emoji-sequences-take-emoji-presentation.txt b/Tests/LibWeb/Text/expected/css/emoji-sequences-take-emoji-presentation.txt
new file mode 100644
index 00000000000..7dd05be0194
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/emoji-sequences-take-emoji-presentation.txt
@@ -0,0 +1,7 @@
+U+261D U+1F3FB: one emoji: true
+U+270C U+1F3FD: one emoji: true
+U+1F575 U+1F3FF: one emoji: true
+U+1F441 U+200D U+1F5E8: one emoji: true
+U+1F575 U+200D U+2640: one emoji: true
+U+261D U+200D x: as wide as without the joiner: true
+U+00A9 U+200D x: as wide as without the joiner: true
diff --git a/Tests/LibWeb/Text/input/css/emoji-sequences-take-emoji-presentation.html b/Tests/LibWeb/Text/input/css/emoji-sequences-take-emoji-presentation.html
new file mode 100644
index 00000000000..441412e063b
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/emoji-sequences-take-emoji-presentation.html
@@ -0,0 +1,38 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<style>
+ span { font-size: 32px; }
+</style>
+<div id="root"></div>
+<script src="../include.js"></script>
+<script>
+ test(() => {
+ const width = text => {
+ const span = document.createElement("span");
+ span.textContent = text;
+ root.appendChild(span);
+ const result = span.getBoundingClientRect().width;
+ span.remove();
+ return result;
+ };
+ // Each base below defaults to text presentation. A skin-tone modifier after it makes the sequence one emoji, as
+ // wide as the base with VARIATION SELECTOR-16. And a zero width joiner after it makes the ZWJ sequence one
+ // emoji, as wide as the sequence written with VARIATION SELECTOR-16 after each pictograph.
+ const cases = [
+ ["U+261D U+1F3FB", "\u261D\u{1F3FB}", "\u261D\uFE0F"],
+ ["U+270C U+1F3FD", "\u270C\u{1F3FD}", "\u270C\uFE0F"],
+ ["U+1F575 U+1F3FF", "\u{1F575}\u{1F3FF}", "\u{1F575}\uFE0F"],
+ ["U+1F441 U+200D U+1F5E8", "\u{1F441}\u200D\u{1F5E8}", "\u{1F441}\uFE0F\u200D\u{1F5E8}\uFE0F"],
+ ["U+1F575 U+200D U+2640", "\u{1F575}\u200D\u2640", "\u{1F575}\uFE0F\u200D\u2640\uFE0F"],
+ ];
+ for (const [name, sequence, with_selectors] of cases)
+ println(`${name}: one emoji: ${width(sequence) === width(with_selectors)}`);
+ // A joiner that no pictograph follows joins nothing, so the base before it keeps its own presentation.
+ const incomplete = [
+ ["U+261D U+200D x", "\u261D\u200D\u0078", "\u261D\u0078"],
+ ["U+00A9 U+200D x", "\u00A9\u200D\u0078", "\u00A9\u0078"],
+ ];
+ for (const [name, sequence, without_joiner] of incomplete)
+ println(`${name}: as wide as without the joiner: ${width(sequence) === width(without_joiner)}`);
+ });
+</script>