Skip to content

Truly independent web browser

LibWeb: Keep an emoji sequence in one font in canvas text

sideshowbarkerc61f8866b52a
diff --git a/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/text.rs b/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/text.rs
index 4e8158c40ed..536ed013e80 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/text.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/text.rs
@@ -7,7 +7,10 @@
//! The text of a 2D context: shaping it across the fonts of the cascade of the font of the context, laying it out
//! as the text preparation algorithm does, and its outlines.
-use libgfx_rust::font::{EmojiPresentation, FontCascadeListHandle, FontHandle};
+use libgfx_rust::font::{
+ EmojiPresentation, FontCascadeListHandle, FontHandle, code_point_ends_emoji_zwj_element,
+ code_point_extends_emoji_sequence, emoji_presentation_for_code_point,
+};
use libgfx_rust::path::{GlyphRun, Path};
use libgfx_rust::text_layout::{GlyphBuffer, TextType, shape_text as shape_text_in_font};
use libgfx_rust::{AffineTransform, FloatPoint};
@@ -46,9 +49,12 @@ pub(crate) fn shape_text(
letter_spacing: f32,
word_spacing: f32,
) -> Vec<ShapedRun> {
- const TEXT_PRESENTATION: EmojiPresentation = EmojiPresentation {
- is_emoji: false,
- forced: false,
+ const ZERO_WIDTH_JOINER: u32 = 0x200d;
+ // 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 mut runs = Vec::new();
@@ -75,18 +81,41 @@ pub(crate) fn shape_text(
});
};
- let (first_code_point, _) = code_point_at(text, 0);
- let mut last_font = cascade.font_for_code_point(first_code_point, TEXT_PRESENTATION, None);
+ let (first_code_point, first_length) = code_point_at(text, 0);
+ let mut last_font = cascade.font_for_code_point(
+ first_code_point,
+ presentation_at(0, first_code_point, first_length),
+ None,
+ );
let mut run_start = 0;
- let mut index = 0;
+ let mut previous_code_point = first_code_point;
+ let mut joiner_joins_emoji = false;
+ let mut index = first_length;
while index < text.len() {
let (code_point, length) = code_point_at(text, index);
- let font = cascade.font_for_code_point(code_point, TEXT_PRESENTATION, Some(&last_font));
- if font.as_raw() != last_font.as_raw() {
- add_run(&text[run_start..index], &last_font);
- last_font = font;
- run_start = index;
+ // NB: A variation selector and a joiner go with the character they follow. And the font that draws that
+ // character draws the rest of its sequence too, as far as it has the glyphs: What extends the sequence,
+ // and what a joiner joins to it. A joiner after a character that's no emoji joins nothing, 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 = code_point_ends_emoji_zwj_element(previous_code_point);
}
+ let presentation = presentation_at(index, code_point, length);
+ let is_joined = previous_code_point == ZERO_WIDTH_JOINER
+ && joiner_joins_emoji
+ && (presentation.is_emoji || !presentation.forced);
+ let stays_in_run = matches!(code_point, 0xfe0e | 0xfe0f | ZERO_WIDTH_JOINER)
+ || ((code_point_extends_emoji_sequence(code_point) || is_joined) && last_font.contains_glyph(code_point));
+ if !stays_in_run {
+ let font = cascade.font_for_code_point(code_point, presentation, Some(&last_font));
+ if font.as_raw() != last_font.as_raw() {
+ add_run(&text[run_start..index], &last_font);
+ last_font = font;
+ run_start = index;
+ }
+ }
+ previous_code_point = code_point;
index += length;
}
if run_start < text.len() {
diff --git a/Tests/LibWeb/Text/expected/canvas/fill-text-emoji-font-choice.txt b/Tests/LibWeb/Text/expected/canvas/fill-text-emoji-font-choice.txt
new file mode 100644
index 00000000000..48446f5960e
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/canvas/fill-text-emoji-font-choice.txt
@@ -0,0 +1,8 @@
+With the emoji variation selector, after the text font: in color
+With the text variation selector, after the color font: not in color
+Without a variation selector, after the text font: not in color
+Without a variation selector, after the color font: in color
+A letter after an emoji and a joiner is as wide as an Ahem glyph: true
+Three emoji that joiners make one are as wide as one emoji: true
+Ahem doesn't draw a "B" by itself: true
+Nor a "B" after an "A" and a joiner: true
diff --git a/Tests/LibWeb/Text/input/canvas/fill-text-emoji-font-choice.html b/Tests/LibWeb/Text/input/canvas/fill-text-emoji-font-choice.html
new file mode 100644
index 00000000000..55c810ef928
--- /dev/null
+++ b/Tests/LibWeb/Text/input/canvas/fill-text-emoji-font-choice.html
@@ -0,0 +1,79 @@
+<!DOCTYPE html>
+<style>
+ @font-face {
+ font-family: ColorEmoji;
+ src: url("../../../Ref/data/fonts/NotoColorEmoji-Regular_subset.ttf");
+ }
+ @font-face {
+ font-family: MonoEmoji;
+ src: url("../../../Ref/data/fonts/NotoEmoji-Regular_subset.ttf");
+ }
+ @font-face {
+ font-family: Ahem;
+ src: url("../../../Ref/data/fonts/Ahem.ttf");
+ }
+ @font-face {
+ font-family: OnlyA;
+ src: url("../../../Ref/data/fonts/Ahem.ttf");
+ unicode-range: U+0041;
+ }
+</style>
+<script src="../include.js"></script>
+<script>
+ // Both fonts have a glyph for the emoji: MonoEmoji a black outline, and ColorEmoji a color image.
+ const EMOJI = "\u{1FAE8}";
+
+ function draw(font, text) {
+ const canvas = document.createElement("canvas");
+ canvas.width = 200;
+ canvas.height = 100;
+ const context = canvas.getContext("2d");
+ context.font = font;
+ context.fillText(text, 20, 70);
+ return context.getImageData(0, 0, canvas.width, canvas.height).data;
+ }
+
+ function width(font, text) {
+ const context = document.createElement("canvas").getContext("2d");
+ context.font = font;
+ return context.measureText(text).width;
+ }
+
+ function hasColorInk(font, text) {
+ const data = draw(font, text);
+ for (let i = 0; i < data.length; i += 4) {
+ if (data[i + 3] > 128 && data[i] - data[i + 2] > 64)
+ return "in color";
+ }
+ return "not in color";
+ }
+
+ asyncTest(async done => {
+ await Promise.all([
+ document.fonts.load("40px ColorEmoji", EMOJI),
+ document.fonts.load("40px MonoEmoji", EMOJI),
+ document.fonts.load("40px Ahem"),
+ document.fonts.load("40px OnlyA", "A"),
+ ]);
+ // A variation selector picks the font that has that presentation, wherever it is in the list.
+ println(`With the emoji variation selector, after the text font: ${hasColorInk("40px MonoEmoji, ColorEmoji", EMOJI + "\uFE0F")}`);
+ println(`With the text variation selector, after the color font: ${hasColorInk("40px ColorEmoji, MonoEmoji", EMOJI + "\uFE0E")}`);
+ // Without one, the first font with a glyph draws it.
+ println(`Without a variation selector, after the text font: ${hasColorInk("40px MonoEmoji, ColorEmoji", EMOJI)}`);
+ println(`Without a variation selector, after the color font: ${hasColorInk("40px ColorEmoji, MonoEmoji", EMOJI)}`);
+ // What follows a zero width joiner is still drawn by a font that has a glyph for it.
+ const font = "40px ColorEmoji, Ahem";
+ const letterWidth = width(font, EMOJI + "\u200DX") - width(font, EMOJI + "\u200D");
+ println(`A letter after an emoji and a joiner is as wide as an Ahem glyph: ${letterWidth === 40}`);
+ // Ahem has a glyph for the joiner too, but the joiner stays in the font of the emoji it follows.
+ const joined = width("40px Ahem", "\u{1F468}\u200D\u{1F469}\u200D\u{1F467}");
+ println(`Three emoji that joiners make one are as wide as one emoji: ${joined === width("40px Ahem", "\u{1F468}")}`);
+ // OnlyA is Ahem, limited to "A" by its unicode-range. So, another font draws "B", though Ahem has a glyph for
+ // it. That holds after a joiner too, where the joiner follows a letter rather than an emoji.
+ const letters = "40px OnlyA, serif";
+ println(`Ahem doesn't draw a "B" by itself: ${width(letters, "B") !== 40}`);
+ const afterJoiner = width(letters, "A\u200DB") - width(letters, "A\u200D");
+ println(`Nor a "B" after an "A" and a joiner: ${Math.abs(afterJoiner - width(letters, "B")) < 0.01}`);
+ done();
+ });
+</script>