diff --git a/common/src/app/common/fonts.cljs b/common/src/app/common/fonts.cljs index ee96b6c9df..927b72be0d 100644 --- a/common/src/app/common/fonts.cljs +++ b/common/src/app/common/fonts.cljs @@ -158,8 +158,12 @@ ;; ;; Which scripts/emoji a text uses and which (google) fallback fonts cover them. +;; Color-emoji load: SMP emoji, U+FE0F, and BMP Emoji_Presentation=Yes. +;; Text-default symbols (♥ ☀ ✔ …) use :symbols / :symbols-2 instead. +;; SMP arms follow historical emoji blocks used for Color Emoji upload. +;; BMP arm is Unicode Emoji_Presentation=Yes only (no text-default ❤ U+2764). (def ^:private emoji-pattern - #"(?:\uD83C[\uDDE6-\uDDFF]\uD83C[\uDDE6-\uDDFF])|(?:\uD83C[\uDC00-\uDFFF]|\uD83D[\uDC00-\uDEFF])|(?:\uD83E[\uDD00-\uDDFF])|(?:\uD83D[\uDE80-\uDEFF]|\uD83E[\uDC00-\uDCFF])|(?:\uD83E[\uDE70-\uDFFF])|[\u2600-\u26FF\u2700-\u27BF\u2300-\u23FF\u2B00-\u2BFF]") + #"(?:\uD83C[\uDDE6-\uDDFF]\uD83C[\uDDE6-\uDDFF])|(?:\uD83C[\uDC00-\uDFFF]|\uD83D[\uDC00-\uDEFF])|(?:\uD83E[\uDD00-\uDDFF])|(?:\uD83D[\uDE80-\uDEFF]|\uD83E[\uDC00-\uDCFF])|(?:\uD83E[\uDE70-\uDFFF])|\uFE0F|[\u231A\u231B\u23E9-\u23EC\u23F0\u23F3\u25FD\u25FE\u2614\u2615\u2648-\u2653\u267F\u2693\u26A1\u26AA\u26AB\u26BD\u26BE\u26C4\u26C5\u26CE\u26D4\u26EA\u26F2\u26F3\u26F5\u26FA\u26FD\u2705\u270A\u270B\u2728\u274C\u274E\u2753-\u2755\u2757\u2795-\u2797\u27B0\u27BF\u2B1B\u2B1C\u2B50\u2B55]") (def ^:private unicode-ranges {:japanese #"[\u3040-\u30FF\u31F0-\u31FF\uFF66-\uFF9F]" @@ -220,9 +224,11 @@ ;; Letterlike Symbols (ℝ, ℕ, ℃, ™, ...) :letterlike #"[\u2100-\u214F]"}) -(defn contains-emoji? [text] - (let [result (re-find emoji-pattern text)] - (boolean result))) +(defn contains-emoji? + "True when `text` needs Noto Color Emoji. Text-default symbols without + U+FE0F do not count; those use :symbols language fallbacks." + [text] + (boolean (re-find emoji-pattern text))) (defn collect-used-languages [used text] diff --git a/common/test/common_tests/fonts_test.cljs b/common/test/common_tests/fonts_test.cljs index 36786ccb7c..5ec652014d 100644 --- a/common/test/common_tests/fonts_test.cljs +++ b/common/test/common_tests/fonts_test.cljs @@ -35,3 +35,15 @@ (t/deftest plain-text-does-not-use-noto-sans-math (t/is (not (contains? (fallback-font-ids "Hello world") "gfont-noto-sans-math"))) (t/is (not (contains? (fallback-font-ids "★ ☀") "gfont-noto-sans-math")))) + +(t/deftest text-default-symbols-do-not-count-as-emoji + (doseq [text ["♥" "☀" "✔" "★ ☀" "Hello" "❤"]] + (t/is (not (cfnt/contains-emoji? text))))) + +(t/deftest emoji-presentation-counts-as-emoji + (doseq [text ["❤️" "♥️" "✅" "⚡️" "😀"]] + (t/is (cfnt/contains-emoji? text)))) + +(t/deftest text-default-symbols-still-use-symbol-fallbacks + (t/is (contains? (fallback-font-ids "♥") "gfont-noto-sans-symbols")) + (t/is (contains? (fallback-font-ids "☀") "gfont-noto-sans-symbols"))) diff --git a/render-wasm/src/render/text.rs b/render-wasm/src/render/text.rs index 65cf0f4366..eaa7ba11f5 100644 --- a/render-wasm/src/render/text.rs +++ b/render-wasm/src/render/text.rs @@ -3,11 +3,11 @@ use crate::{ error::Result, math::{Point, Rect}, shapes::{ - add_text_with_tabs, add_trailing_space_anchor, calculate_text_layout_data, set_paint_fill, - vertical_align_offset, ListMarker, ListStyleType, Paragraph as TextParagraph, - ParagraphBuilderGroup, ParagraphLayout, Stroke, StrokeKind, TextContent, - TextDecorationSegment, TextDirection, LIST_MARKER_CENTER_FACTOR, LIST_MARKER_GAP_FACTOR, - LIST_MARKER_MIN_RADIUS, LIST_MARKER_RADIUS_FACTOR, + add_trailing_space_anchor, calculate_text_layout_data, push_text_with_presentation_styles, + set_paint_fill, vertical_align_offset, ListMarker, ListStyleType, + Paragraph as TextParagraph, ParagraphBuilderGroup, ParagraphLayout, Stroke, StrokeKind, + TextContent, TextDecorationSegment, TextDirection, LIST_MARKER_CENTER_FACTOR, + LIST_MARKER_GAP_FACTOR, LIST_MARKER_MIN_RADIUS, LIST_MARKER_RADIUS_FACTOR, }, utils::{get_fallback_fonts, get_font_collection}, }; @@ -52,14 +52,10 @@ pub fn stroke_paragraph_builder_group_from_text( }); let stroke_paint = stroke_paint.clone(); let remove_alpha = use_shadow.unwrap_or(false) && !span.is_transparent(); - let stroke_style = span.to_stroke_style( - &stroke_paint, - fallback_fonts, - remove_alpha, - paragraph.line_height(), - ); - builder.push_style(&stroke_style); - add_text_with_tabs(builder, &text, span.font_size); + let line_height = paragraph.line_height(); + push_text_with_presentation_styles(builder, &text, span, fallback_fonts, || { + span.to_stroke_style(&stroke_paint, fallback_fonts, remove_alpha, line_height) + }); } } diff --git a/render-wasm/src/shapes.rs b/render-wasm/src/shapes.rs index f4776dad95..3c8fb4101f 100644 --- a/render-wasm/src/shapes.rs +++ b/render-wasm/src/shapes.rs @@ -28,6 +28,7 @@ mod svg_attrs; mod svgraw; mod text; pub mod text_paths; +mod text_presentation; mod transform; pub use blend::*; diff --git a/render-wasm/src/shapes/text.rs b/render-wasm/src/shapes/text.rs index 65dbfb63f9..a5cb69e645 100644 --- a/render-wasm/src/shapes/text.rs +++ b/render-wasm/src/shapes/text.rs @@ -26,6 +26,7 @@ use std::collections::HashSet; use std::rc::Rc; use unicode_segmentation::UnicodeSegmentation; +use super::text_presentation::{font_families_for, split_presentation_runs, Presentation}; use super::FontFamily; use crate::math::Point; use crate::shapes::{self, merge_fills, Shape, Type, VerticalAlign}; @@ -1002,69 +1003,75 @@ impl TextContent { let mut builder = ParagraphBuilder::new(¶graph_style, fonts); let mut has_text = false; for span in paragraph.children() { - let text_style = if let Some((layer, image_fill)) = opaque_image_layer { - let mut style = span.to_style( - &self.bounds(), - fallback_fonts, - false, - paragraph.line_height(), - ); - let mut paint = paint::Paint::default(); - match span.fills_from_bottom(layer) { - Some(shapes::Fill::Image(img)) if img == image_fill => { - paint.set_color(skia::Color::BLACK); - paint.set_alpha(255); - } - _ => { - paint.set_color(skia::Color::TRANSPARENT); - } - } - style.set_foreground_paint(&paint); - style - } else if let (Some(layer), Some(skip)) = (fill_layer, skip_image_ids) { - let skip_span = matches!( - span.fills_from_bottom(layer), - Some(shapes::Fill::Image(img)) if skip.contains(&img.id()) - ); - if skip_span { - let mut style = span.to_style( - &self.bounds(), - fallback_fonts, - false, - paragraph.line_height(), - ); - let mut paint = paint::Paint::default(); - paint.set_color(skia::Color::TRANSPARENT); - style.set_foreground_paint(&paint); - style - } else { - let remove_alpha = - opaque || (use_shadow.unwrap_or(false) && !span.is_transparent()); - span.to_style_with_paint( - &self.bounds(), - fallback_fonts, - remove_alpha, - paragraph.line_height(), - fill_layer, - ) - } - } else { - let remove_alpha = - opaque || (use_shadow.unwrap_or(false) && !span.is_transparent()); - span.to_style_with_paint( - &self.bounds(), - fallback_fonts, - remove_alpha, - paragraph.line_height(), - fill_layer, - ) - }; let text: String = span.apply_text_transform(); if !text.is_empty() { has_text = true; } - builder.push_style(&text_style); - add_text_with_tabs(&mut builder, &text, span.font_size); + push_text_with_presentation_styles( + &mut builder, + &text, + span, + fallback_fonts, + || { + if let Some((layer, image_fill)) = opaque_image_layer { + let mut style = span.to_style( + &self.bounds(), + fallback_fonts, + false, + paragraph.line_height(), + ); + let mut paint = paint::Paint::default(); + match span.fills_from_bottom(layer) { + Some(shapes::Fill::Image(img)) if img == image_fill => { + paint.set_color(skia::Color::BLACK); + paint.set_alpha(255); + } + _ => { + paint.set_color(skia::Color::TRANSPARENT); + } + } + style.set_foreground_paint(&paint); + style + } else if let (Some(layer), Some(skip)) = (fill_layer, skip_image_ids) { + let skip_span = matches!( + span.fills_from_bottom(layer), + Some(shapes::Fill::Image(img)) if skip.contains(&img.id()) + ); + if skip_span { + let mut style = span.to_style( + &self.bounds(), + fallback_fonts, + false, + paragraph.line_height(), + ); + let mut paint = paint::Paint::default(); + paint.set_color(skia::Color::TRANSPARENT); + style.set_foreground_paint(&paint); + style + } else { + let remove_alpha = opaque + || (use_shadow.unwrap_or(false) && !span.is_transparent()); + span.to_style_with_paint( + &self.bounds(), + fallback_fonts, + remove_alpha, + paragraph.line_height(), + fill_layer, + ) + } + } else { + let remove_alpha = + opaque || (use_shadow.unwrap_or(false) && !span.is_transparent()); + span.to_style_with_paint( + &self.bounds(), + fallback_fonts, + remove_alpha, + paragraph.line_height(), + fill_layer, + ) + } + }, + ); } if !has_text { builder.add_text(" "); @@ -1810,6 +1817,34 @@ pub fn add_text_with_tabs(builder: &mut ParagraphBuilder, text: &str, font_size: } } +/// Push `text` as presentation runs. `make_style` supplies paint/size; this +/// overrides `font_families` per run (text vs emoji). +pub fn push_text_with_presentation_styles( + builder: &mut ParagraphBuilder, + text: &str, + span: &TextSpan, + fallback_fonts: &HashSet, + mut make_style: F, +) where + F: FnMut() -> skia::textlayout::TextStyle, +{ + let primary = format!("{}", span.font_family); + let default = default_font(); + for (kind, run) in split_presentation_runs(text) { + let mut style = make_style(); + let families = font_families_for( + kind, + primary.clone(), + default.clone(), + DEFAULT_EMOJI_FONT, + fallback_fonts, + ); + style.set_font_families(&families); + builder.push_style(&style); + add_text_with_tabs(builder, &run, span.font_size); + } +} + /// Filter control characters below U+0020, preserving tabs and line breaks. /// Browser-dependent: Firefox drops them, others replace with space. fn process_ignored_chars(text: &str, browser: u8) -> String { @@ -1953,13 +1988,15 @@ impl TextSpan { // Trick to avoid showing the text decoration style.set_decoration_thickness_multiplier(0.0); - let mut font_families = vec![ + // Text presentation stack; emoji runs override this in + // push_text_with_presentation_styles. + let font_families = font_families_for( + Presentation::Text, self.serialized_font_family(), default_font(), - DEFAULT_EMOJI_FONT.to_string(), - ]; - - font_families.extend(fallback_fonts.iter().cloned()); + DEFAULT_EMOJI_FONT, + fallback_fonts, + ); style.set_font_families(&font_families); style.set_font_size(self.font_size); style.set_letter_spacing(self.letter_spacing); diff --git a/render-wasm/src/shapes/text_presentation.rs b/render-wasm/src/shapes/text_presentation.rs new file mode 100644 index 0000000000..647d4539ac --- /dev/null +++ b/render-wasm/src/shapes/text_presentation.rs @@ -0,0 +1,260 @@ +//! Text vs emoji presentation for Skia font fallbacks. +//! +//! Unicode chooses monochrome vs color emoji via `Emoji_Presentation` and +//! variation selectors U+FE0E (text) / U+FE0F (emoji). Each run gets a matching +//! `font_families` stack. + +use std::collections::HashSet; + +use unicode_segmentation::UnicodeSegmentation; + +const VS15_TEXT: char = '\u{FE0E}'; +const VS16_EMOJI: char = '\u{FE0F}'; + +/// Unicode `Emoji_Presentation=Yes` ranges (emoji-data). Characters outside +/// these defaults to text presentation unless a variation selector says +/// otherwise — e.g. 🌡 U+1F321, 🅰 U+1F170, 📽 U+1F4FD stay text. +const EMOJI_PRESENTATION_RANGES: &[(u32, u32)] = &[ + (0x231A, 0x231B), + (0x23E9, 0x23EC), + (0x23F0, 0x23F0), + (0x23F3, 0x23F3), + (0x25FD, 0x25FE), + (0x2614, 0x2615), + (0x2648, 0x2653), + (0x267F, 0x267F), + (0x2693, 0x2693), + (0x26A1, 0x26A1), + (0x26AA, 0x26AB), + (0x26BD, 0x26BE), + (0x26C4, 0x26C5), + (0x26CE, 0x26CE), + (0x26D4, 0x26D4), + (0x26EA, 0x26EA), + (0x26F2, 0x26F3), + (0x26F5, 0x26F5), + (0x26FA, 0x26FA), + (0x26FD, 0x26FD), + (0x2705, 0x2705), + (0x270A, 0x270B), + (0x2728, 0x2728), + (0x274C, 0x274C), + (0x274E, 0x274E), + (0x2753, 0x2755), + (0x2757, 0x2757), + (0x2795, 0x2797), + (0x27B0, 0x27B0), + (0x27BF, 0x27BF), + (0x2B1B, 0x2B1C), + (0x2B50, 0x2B50), + (0x2B55, 0x2B55), + (0x1F004, 0x1F004), + (0x1F0CF, 0x1F0CF), + (0x1F18E, 0x1F18E), + (0x1F191, 0x1F19A), + (0x1F1E6, 0x1F1FF), + (0x1F201, 0x1F201), + (0x1F21A, 0x1F21A), + (0x1F22F, 0x1F22F), + (0x1F232, 0x1F236), + (0x1F238, 0x1F23A), + (0x1F250, 0x1F251), + (0x1F300, 0x1F320), + (0x1F32D, 0x1F335), + (0x1F337, 0x1F37C), + (0x1F37E, 0x1F393), + (0x1F3A0, 0x1F3CA), + (0x1F3CF, 0x1F3D3), + (0x1F3E0, 0x1F3F0), + (0x1F3F4, 0x1F3F4), + (0x1F3F8, 0x1F43E), + (0x1F440, 0x1F440), + (0x1F442, 0x1F4FC), + (0x1F4FF, 0x1F53D), + (0x1F54B, 0x1F54E), + (0x1F550, 0x1F567), + (0x1F57A, 0x1F57A), + (0x1F595, 0x1F596), + (0x1F5A4, 0x1F5A4), + (0x1F5FB, 0x1F64F), + (0x1F680, 0x1F6C5), + (0x1F6CC, 0x1F6CC), + (0x1F6D0, 0x1F6D2), + (0x1F6D5, 0x1F6D9), + (0x1F6DC, 0x1F6DF), + (0x1F6EB, 0x1F6EC), + (0x1F6F4, 0x1F6FC), + (0x1F7E0, 0x1F7EB), + (0x1F7F0, 0x1F7F0), + (0x1F90C, 0x1F93A), + (0x1F93C, 0x1F945), + (0x1F947, 0x1F9FF), + (0x1FA70, 0x1FA7C), + (0x1FA80, 0x1FAC6), + (0x1FAC8, 0x1FAC8), + (0x1FACC, 0x1FADD), + (0x1FADF, 0x1FAEB), + (0x1FAEF, 0x1FAFA), +]; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Presentation { + Text, + Emoji, +} + +/// Build the Skia family list for a presentation run. +/// +/// - Text: primary → default → symbol/script fallbacks → color emoji last +/// (so Symbols can win, but emoji-only glyphs like `😀︎` still resolve). +/// - Emoji: color emoji first so a primary that maps the base codepoint cannot +/// shadow FE0F / default-emoji presentation. +pub fn font_families_for( + kind: Presentation, + primary: String, + default: String, + emoji: &str, + fallbacks: &HashSet, +) -> Vec { + match kind { + Presentation::Text => { + let mut v = vec![primary, default]; + v.extend(fallbacks.iter().cloned()); + v.push(emoji.to_string()); + v + } + Presentation::Emoji => { + let mut v = vec![emoji.to_string(), primary, default]; + v.extend(fallbacks.iter().cloned()); + v + } + } +} + +pub fn presentation_of_cluster(cluster: &str) -> Presentation { + if cluster.contains(VS15_TEXT) { + return Presentation::Text; + } + if cluster.contains(VS16_EMOJI) { + return Presentation::Emoji; + } + for c in cluster.chars() { + if c == '\u{200D}' { + continue; + } + if is_default_emoji_presentation(c) { + return Presentation::Emoji; + } + } + Presentation::Text +} + +/// Merge consecutive grapheme clusters that share the same presentation. +/// An empty string yields one empty text run so callers still push a style +/// (needed for blank lines / empty spans). +pub fn split_presentation_runs(text: &str) -> Vec<(Presentation, String)> { + if text.is_empty() { + return vec![(Presentation::Text, String::new())]; + } + let mut runs: Vec<(Presentation, String)> = Vec::new(); + for cluster in text.graphemes(true) { + let kind = presentation_of_cluster(cluster); + match runs.last_mut() { + Some((prev_kind, buf)) if *prev_kind == kind => buf.push_str(cluster), + _ => runs.push((kind, cluster.to_string())), + } + } + runs +} + +fn is_default_emoji_presentation(c: char) -> bool { + let cp = c as u32; + EMOJI_PRESENTATION_RANGES + .binary_search_by(|&(start, end)| { + if cp < start { + std::cmp::Ordering::Greater + } else if cp > end { + std::cmp::Ordering::Less + } else { + std::cmp::Ordering::Equal + } + }) + .is_ok() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn classifies_text_default_symbols() { + for s in ["♥", "☀", "✔", "★", "A", " ", ",", "🌡", "🅰", "📽", "❤"] { + assert_eq!(presentation_of_cluster(s), Presentation::Text, "{s}"); + } + } + + #[test] + fn classifies_variation_selectors() { + assert_eq!(presentation_of_cluster("⚡\u{FE0E}"), Presentation::Text); + assert_eq!(presentation_of_cluster("❤\u{FE0E}"), Presentation::Text); + assert_eq!(presentation_of_cluster("😀\u{FE0E}"), Presentation::Text); + assert_eq!(presentation_of_cluster("♥\u{FE0F}"), Presentation::Emoji); + assert_eq!(presentation_of_cluster("❤\u{FE0F}"), Presentation::Emoji); + assert_eq!(presentation_of_cluster("⚡\u{FE0F}"), Presentation::Emoji); + } + + #[test] + fn classifies_default_emoji_presentation() { + for s in ["✅", "⚡", "😀", "🎉"] { + assert_eq!(presentation_of_cluster(s), Presentation::Emoji, "{s}"); + } + } + + #[test] + fn split_empty_text_keeps_a_text_run() { + assert_eq!( + split_presentation_runs(""), + vec![(Presentation::Text, String::new())] + ); + } + + #[test] + fn split_merges_adjacent_same_presentation() { + let runs = split_presentation_runs("A♥B❤️C"); + assert_eq!( + runs, + vec![ + (Presentation::Text, "A♥B".to_string()), + (Presentation::Emoji, "❤️".to_string()), + (Presentation::Text, "C".to_string()), + ] + ); + } + + #[test] + fn font_stacks_match_presentation() { + let mut fallbacks = HashSet::new(); + fallbacks.insert("symbols".to_string()); + + assert_eq!( + font_families_for( + Presentation::Text, + "jost".into(), + "sourcesanspro".into(), + "noto-color-emoji", + &fallbacks, + ), + vec!["jost", "sourcesanspro", "symbols", "noto-color-emoji"] + ); + + let emoji = font_families_for( + Presentation::Emoji, + "jost".into(), + "sourcesanspro".into(), + "noto-color-emoji", + &fallbacks, + ); + assert_eq!(emoji[0], "noto-color-emoji"); + assert_eq!(emoji[1], "jost"); + } +}