Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
gpui: Fix web fallback for text-default emoji (#64319)
Max Brunsfeld87f65de697bd
diff --git a/crates/gpui_web/examples/hello_web/font_fallback_test.rs b/crates/gpui_web/examples/hello_web/font_fallback_test.rsindex 2d62b88be8b..039da244a56 100644--- a/crates/gpui_web/examples/hello_web/font_fallback_test.rs+++ b/crates/gpui_web/examples/hello_web/font_fallback_test.rs@@ -56,6 +56,59 @@ pub fn test_missing_glyph_notifications() -> Result<(), JsValue> {.all(|report| report.font_class() == FallbackFontClass::Monospace));+ for graphemes in [+ vec!["🕸", "🕸", "🕸"],+ vec!["🕸", "🕸\u{fe0f}", "🕸\u{fe0e}", "🕸"],+ ] {+ reports.0.lock().expect("report lock").clear();+ let text = graphemes.concat();+ let layout = text_system.layout_line(+ &text,+ px(24.),+ &[FontRun {+ len: text.len(),+ font_id,+ }],+ );+ let glyphs = layout+ .runs+ .iter()+ .flat_map(|run| run.glyphs.iter().map(move |glyph| (run.font_id, glyph)))+ .collect::<Vec<_>>();+ assert_eq!(glyphs.len(), graphemes.len());+ let mut source_index = 0;+ for ((resolved_font, glyph), grapheme) in glyphs.iter().zip(&graphemes) {+ assert_ne!(+ *resolved_font, font_id,+ "Missing Lilex glyph must fall back"+ );+ assert_eq!(glyph.index, source_index, "Preserve each source grapheme");+ assert_eq!(glyph.is_emoji, grapheme.ends_with('\u{fe0f}'));+ source_index += grapheme.len();+ }+ assert_eq!(source_index, text.len());+ assert!(+ reports.0.lock().expect("report lock").is_empty(),+ "Every spider-web glyph must resolve, not just the first cached raster"+ );+ }++ reports.0.lock().expect("report lock").clear();+ let bundled = "Hello ©";+ let layout = text_system.layout_line(+ bundled,+ px(24.),+ &[FontRun {+ len: bundled.len(),+ font_id,+ }],+ );+ for run in &layout.runs {+ assert_eq!(run.font_id, font_id, "Retain existing bundled text glyphs");+ assert!(run.glyphs.iter().all(|glyph| !glyph.is_emoji));+ }+ assert!(reports.0.lock().expect("report lock").is_empty());+reports.0.lock().expect("report lock").clear();text_system.set_missing_glyph_sink(None);text_system.layout_line(text, px(24.), &runs);diff --git a/crates/gpui_web/examples/hello_web/test_canvas_fallback.mjs b/crates/gpui_web/examples/hello_web/test_canvas_fallback.mjsindex 1568c4a7564..8e8ba607bf4 100644--- a/crates/gpui_web/examples/hello_web/test_canvas_fallback.mjs+++ b/crates/gpui_web/examples/hello_web/test_canvas_fallback.mjs@@ -158,12 +158,22 @@ try {await delay(200);assert.equal(await rasterCount("😀"), 1, "Caret movement should not rerasterize emoji");- const text = "A中文 が か\u3099 각 각 😀 ❤️ 👍🏽 🇯🇵 1️⃣ 👩💻 👨👩👧👦Z";+ await key("a", true);+ await insert("🕸🕸🕸");+ await delay(200);+ assert.equal(await rasterCount("🕸"), 1, "Repeated text-default emoji should share one atlas image");+ assert.equal(await readText(), "🕸🕸🕸");+ await key("ArrowLeft");+ await key("ArrowRight");+ await delay(200);+ assert.equal(await rasterCount("🕸"), 1, "Caret movement should not rerasterize text-default emoji");++ const text = "A中文 が か\u3099 각 각 🕸 🕸\uFE0F 🕸\uFE0E 🕸 😀 ❤️ 👍🏽 🇯🇵 1️⃣ 👩💻 👨👩👧👦Z";await key("a", true);await insert(text);await delay(200);assert.equal(await readText(), text);- for (const grapheme of ["❤️", "👍🏽", "🇯🇵", "1️⃣", "👩💻", "👨👩👧👦"]) {+ for (const grapheme of ["🕸", "🕸\uFE0F", "🕸\uFE0E", "❤️", "👍🏽", "🇯🇵", "1️⃣", "👩💻", "👨👩👧👦"]) {assert.ok(await rasterCount(grapheme) > 0, `Missing whole-grapheme raster: ${grapheme}`);}for (const grapheme of ["中", "文", "が", "か\u3099", "각", "각"]) {diff --git a/crates/gpui_web/src/canvas_fallback.rs b/crates/gpui_web/src/canvas_fallback.rsindex 5aec7e74b74..c852cea2727 100644--- a/crates/gpui_web/src/canvas_fallback.rs+++ b/crates/gpui_web/src/canvas_fallback.rs@@ -19,7 +19,7 @@ use unicode_segmentation::UnicodeSegmentation;pub enum CanvasFontFallback {/// Use only fonts loaded into GPUI.Disabled,- /// Use Canvas only for eligible graphemes requesting emoji presentation.+ /// Use Canvas for eligible emoji, including missing text-presentation symbols.#[default]Emoji,/// Also allow approximate independent rendering of eligible horizontal CJK text.@@ -28,10 +28,10 @@ pub enum CanvasFontFallback {impl CanvasFontFallback {#[cfg(any(target_family = "wasm", test))]- pub(crate) fn allows(self, emoji_presentation: bool) -> bool {+ pub(crate) fn allows(self, is_emoji: bool) -> bool {match self {Self::Disabled => false,- Self::Emoji => emoji_presentation,+ Self::Emoji => is_emoji,Self::EmojiAndCjk => true,}}@@ -40,6 +40,8 @@ impl CanvasFontFallback {/// A supported grapheme assumed to be independently renderable.#[derive(Clone, Copy, Debug, Eq, PartialEq)]pub struct CanvasFallback {+ /// Whether this is an emoji-capable grapheme, independent of its presentation.+ pub is_emoji: bool,/// Whether to request emoji presentation instead of ordinary text presentation.pub emoji_presentation: bool,}@@ -57,10 +59,14 @@ pub fn classify_canvas_fallback(grapheme: &str) -> Option<CanvasFallback> {if is_cjk(grapheme) {Some(CanvasFallback {+ is_emoji: false,emoji_presentation: false,})} else {- classify_emoji(grapheme).map(|emoji_presentation| CanvasFallback { emoji_presentation })+ classify_emoji(grapheme).map(|emoji_presentation| CanvasFallback {+ is_emoji: true,+ emoji_presentation,+ })}}@@ -227,15 +233,28 @@ mod tests {#[test]fn canvas_font_fallback_policy() {assert_eq!(CanvasFontFallback::default(), CanvasFontFallback::Emoji);- for grapheme in ["😀", "❤️", "1️⃣", "👨👩👧👦", "中", "か\u{3099}", "각", "©", "❤︎"]- {+ for (grapheme, allowed) in [+ ("😀", true),+ ("❤️", true),+ ("1️⃣", true),+ ("👨👩👧👦", true),+ ("🕸", true),+ ("🕸\u{fe0e}", true),+ ("🕸\u{fe0f}", true),+ ("©", true),+ ("❤︎", true),+ ("中", false),+ ("か\u{3099}", false),+ ("각", false),+ ] {let fallback = classify_canvas_fallback(grapheme).expect("eligible grapheme");- assert!(!CanvasFontFallback::Disabled.allows(fallback.emoji_presentation));+ assert!(!CanvasFontFallback::Disabled.allows(fallback.is_emoji));assert_eq!(- CanvasFontFallback::Emoji.allows(fallback.emoji_presentation),- ["😀", "❤️", "1️⃣", "👨👩👧👦"].contains(&grapheme),+ CanvasFontFallback::Emoji.allows(fallback.is_emoji),+ allowed,+ "{grapheme:?}",);- assert!(CanvasFontFallback::EmojiAndCjk.allows(fallback.emoji_presentation));+ assert!(CanvasFontFallback::EmojiAndCjk.allows(fallback.is_emoji));}}@@ -252,6 +271,7 @@ mod tests {assert_eq!(classify_canvas_fallback(&text),Some(CanvasFallback {+ is_emoji: true,emoji_presentation: true,}),"{text:?}"@@ -291,6 +311,7 @@ mod tests {assert_eq!(classify_canvas_fallback(text),Some(CanvasFallback {+ is_emoji: false,emoji_presentation: false,}),"{text:?}"@@ -302,6 +323,9 @@ mod tests {fn emoji_presentation_is_preserved() {for (text, emoji_presentation) in [("😀", true),+ ("🕸", false),+ ("🕸\u{fe0e}", false),+ ("🕸\u{fe0f}", true),("©", false),("©\u{fe0e}", false),("©\u{fe0f}", true),@@ -335,7 +359,10 @@ mod tests {] {assert_eq!(classify_canvas_fallback(text),- Some(CanvasFallback { emoji_presentation }),+ Some(CanvasFallback {+ is_emoji: true,+ emoji_presentation,+ }),"{text:?}");}diff --git a/crates/gpui_web/src/run_replacements.rs b/crates/gpui_web/src/run_replacements.rsindex cac43ebf41b..2a190a24896 100644--- a/crates/gpui_web/src/run_replacements.rs+++ b/crates/gpui_web/src/run_replacements.rs@@ -8,6 +8,7 @@ use unicode_segmentation::UnicodeSegmentation;pub(crate) struct Candidate {pub(crate) source: Range<usize>,pub(crate) font_id: FontId,+ pub(crate) is_emoji: bool,pub(crate) color: bool,pub(crate) glyphs: Option<GlyphSpan>,}@@ -52,6 +53,7 @@ pub(crate) fn collect_candidates(candidates.push(Candidate {source: start..end,font_id: run.font_id,+ is_emoji: fallback.is_emoji,color: fallback.emoji_presentation,glyphs: None,});diff --git a/crates/gpui_web/src/text_system.rs b/crates/gpui_web/src/text_system.rsindex b2851511be5..e8ce869a802 100644--- a/crates/gpui_web/src/text_system.rs+++ b/crates/gpui_web/src/text_system.rs@@ -195,7 +195,7 @@ impl WebTextSystem {}let mut replacements = Vec::new();for candidate in collect_candidates(text, font_runs, layout) {- if !self.canvas_font_fallback.allows(candidate.color) {+ if !self.canvas_font_fallback.allows(candidate.is_emoji) {continue;}let Some(glyphs) = candidate.glyphs else {