Truly independent web browser
LibWeb+LibGfx: Draw color emoji in SVG text
sideshowbarkerd94ca37eb006
diff --git a/Libraries/LibGfx/Rust/src/font.rs b/Libraries/LibGfx/Rust/src/font.rsindex 37593d71912..b163dd114fb 100644--- a/Libraries/LibGfx/Rust/src/font.rs+++ b/Libraries/LibGfx/Rust/src/font.rs@@ -257,6 +257,26 @@ pub struct EmojiPresentation {pub forced: bool,}+/// Whether `code_point` only ever extends the emoji sequence it follows: a zero width joiner, a variation selector, an+/// emoji modifier, a tag character, or the combining enclosing keycap. The font that draws what it follows draws it+/// too, where that font has a glyph for it.+/// https://www.unicode.org/reports/tr51/#Emoji_Sequences+pub fn code_point_extends_emoji_sequence(code_point: u32) -> bool {+ matches!(+ code_point,+ 0x200d | 0xfe0e | 0xfe0f | 0x20e3 | 0x1f3fb..=0x1f3ff | 0xe0020..=0xe007f+ )+}++/// Whether a zero width joiner right after `code_point` joins what follows it to an emoji sequence: `code_point` is an+/// emoji by default, or it ends the emoji sequence it's in. A joiner after any other character, as in Indic or Arabic+/// text, joins no emoji.+/// 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+}+pub fn emoji_presentation_for_code_point(code_point: u32, next_code_point: Option<u32>) -> EmojiPresentation {// SAFETY: The lookup reads only immutable Unicode tables.let encoded = unsafe {diff --git a/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs b/Libraries/LibWeb/Rust/src/layout/fragment_tree.rsindex 8bf7016f750..1130556d857 100644--- a/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs+++ b/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs@@ -130,6 +130,26 @@ pub(crate) struct SvgTextSegment {/// The child element the part follows, or the invalid slot for the part that comes before every child element.pub(crate) after_child: crate::layout::node_data::NodeSlotId,pub(crate) path: libgfx_rust::path::Path,+ /// The glyphs of the part that a color font draws. They have no outlines for the path to hold, so they paint as+ /// glyphs.+ pub(crate) color_glyph_runs: Vec<SvgColorGlyphRun>,+}++/// A run of glyphs of a color font in SVG text, in user units.+#[derive(Clone, PartialEq)]+pub(crate) struct SvgColorGlyphRun {+ pub(crate) font: libgfx_rust::font::FontHandle,+ /// The glyphs, each positioned by the top of the font's ascent.+ pub(crate) glyphs: Vec<SvgColorGlyph>,+ /// The cells the glyphs occupy.+ pub(crate) bounds: libgfx_rust::FloatRect,+}++#[derive(Clone, Copy, Debug, PartialEq)]+pub(crate) struct SvgColorGlyph {+ pub(crate) x: f32,+ pub(crate) y: f32,+ pub(crate) glyph_id: u32,}#[derive(Clone, Default)]diff --git a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rsindex 978dad38a2d..0d6b0ab60d7 100644--- a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs@@ -619,6 +619,8 @@ impl SvgTextDecorationLines {/// What a text content element paints in one go: glyph outlines, or the rects of text decoration lines.enum SvgTextPaintPart {Glyphs(Vec<libgfx_rust::path::GlyphRun>),+ /// Glyphs of a color font, which have no outlines.+ ColorGlyphs(Vec<fragment_tree::SvgColorGlyphRun>),DecorationLines(Vec<FloatRect>),}@@ -642,6 +644,19 @@ impl SvgTextPaintParts {self.parts.push((after_child, SvgTextPaintPart::Glyphs(runs)));}+ fn add_color_glyphs(&mut self, after_child: Node, run: fragment_tree::SvgColorGlyphRun) {+ if run.glyphs.is_empty() {+ return;+ }+ if let Some((last_after_child, SvgTextPaintPart::ColorGlyphs(last_runs))) = self.parts.last_mut()+ && *last_after_child == after_child+ {+ last_runs.push(run);+ return;+ }+ self.parts.push((after_child, SvgTextPaintPart::ColorGlyphs(vec![run])));+ }+fn add_decoration_lines(&mut self, after_child: Node, rects: Vec<FloatRect>) {if rects.is_empty() {return;@@ -1839,26 +1854,49 @@ impl<'pass> SvgFormattingContext<'pass> {if text.is_empty() {return;}+ const ZERO_WIDTH_JOINER: u32 = 0x200d;let frozen_font_list = style.frozen_font_list();- let font_for = |offset: usize| {- frozen_font_list.font_for_code_point(- code_point_at(text, offset),- libgfx_rust::font::EmojiPresentation {- is_emoji: false,- forced: false,- },- )+ // https://drafts.csswg.org/css-fonts-4/#font-variant-emoji-prop+ // FIXME: Take 'font-variant-emoji' into account.+ let presentation_at = |offset: usize| {+ 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 font_for =+ |offset: usize| frozen_font_list.font_for_code_point(code_point_at(text, offset), presentation_at(offset));let mut last_font = font_for(0);let mut run_start = 0;+ let mut previous_code_point = code_point_at(text, 0);+ let mut joiner_joins_emoji = false;let mut offset = code_point_length_at(text, 0);while offset < text.len() {- let font = font_for(offset);- if font != last_font {- run(&last_font, &text[run_start..offset]);- last_font = font;- run_start = offset;+ let code_point = code_point_at(text, offset);+ // 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 = libgfx_rust::font::code_point_ends_emoji_zwj_element(previous_code_point);+ }+ let presentation = presentation_at(offset);+ 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)+ || ((libgfx_rust::font::code_point_extends_emoji_sequence(code_point) || is_joined)+ && last_font.contains_glyph(code_point));+ if !stays_in_run {+ let font = font_for(offset);+ if font != last_font {+ run(&last_font, &text[run_start..offset]);+ last_font = font;+ run_start = offset;+ }}+ previous_code_point = code_point;offset += code_point_length_at(text, offset);}run(&last_font, &text[run_start..]);@@ -2186,7 +2224,39 @@ impl<'pass> SvgFormattingContext<'pass> {}spans.extend(run_spans);paint_parts.add_decoration_lines(after_child, lines_under);- paint_parts.add_glyphs(after_child, shaped.runs);+ for run in shaped.runs {+ if !run.font.is_emoji_font() {+ paint_parts.add_glyphs(after_child, vec![run]);+ continue;+ }+ // NB: The glyphs of a color font have no outlines to fill or stroke, so they're kept as glyphs.+ // Where 'textLength' scales the glyphs, they move to where they'd land, but aren't stretched.+ let facts = run.font.facts();+ let mut bounds: Option<FloatRect> = None;+ let glyphs = run+ .glyphs+ .placed()+ .filter(|placed| placed.glyph.should_paint())+ .map(|placed| {+ let x = placed.x * glyph_scale;+ let cell = FloatRect::new(x, placed.y, placed.glyph.advance, facts.ascent + facts.descent);+ bounds = Some(united_even_if_empty(bounds, cell));+ fragment_tree::SvgColorGlyph {+ x,+ y: placed.y,+ glyph_id: u32::from(placed.glyph.glyph_id),+ }+ })+ .collect();+ paint_parts.add_color_glyphs(+ after_child,+ fragment_tree::SvgColorGlyphRun {+ font: run.font,+ glyphs,+ bounds: bounds.unwrap_or_default(),+ },+ );+ }paint_parts.add_decoration_lines(after_child, lines_over);contents.glyph_cells.unite(shaped.glyph_cells);@@ -2223,8 +2293,10 @@ impl<'pass> SvgFormattingContext<'pass> {// has to paint between two parts of it, the path paints as it is.let paints_as_one_path = paint_parts.are_one_path();let mut glyph_outlines = libgfx_rust::path::Path::new();+ let mut color_glyph_cells = libgfx_rust::path::Path::new();let mut segments = Vec::new();for (after_child, part) in paint_parts.parts {+ let mut color_glyph_runs = Vec::new();let path = match part {SvgTextPaintPart::Glyphs(runs) => {let path = libgfx_rust::path::Path::from_glyph_runs(&runs);@@ -2235,14 +2307,25 @@ impl<'pass> SvgFormattingContext<'pass> {glyph_outlines.append_path(&path);path}+ SvgTextPaintPart::ColorGlyphs(runs) => {+ // NB: A glyph without an outline takes hits anywhere in its cell.+ for run in &runs {+ color_glyph_cells.append_path(&path_of_rects(&[run.bounds]));+ }+ color_glyph_runs = runs;+ libgfx_rust::path::Path::new()+ }SvgTextPaintPart::DecorationLines(rects) => path_of_rects(&rects),};segments.push(fragment_tree::SvgTextSegment {after_child,path: scale_glyphs(path),+ color_glyph_runs,});}contents.path = scale_glyphs(glyph_outlines);+ // NB: The cells of the color glyphs are where 'textLength' moves the glyphs to already.+ contents.path.append_path(&color_glyph_cells);if !paints_as_one_path {contents.segments = Some(segments);}diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/svg.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/svg.rsindex 5586f2677fb..56f281e6a38 100644--- a/Libraries/LibWeb/Rust/src/painting/record/paint/svg.rs+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/svg.rs@@ -510,8 +510,14 @@ fn paint_path_parts<O: Observer>(let device_scale = recorder.inputs.device_pixels_per_css_pixel as f32;let paint_transform = [device_scale, 0.0, 0.0, device_scale, 0.0, 0.0];let segments = crate::painting::paintable_geometry::committed_svg_text_segments(recorder.source, paintable);- // Each part of the path to paint, with its bounding box in user units.- let parts: Vec<(libgfx_rust::path::Path, FloatRect)> = match &segments {+ // Each part of the path to paint, with its bounding box in user units, and the glyphs of color fonts that paint+ // along with it.+ type Part<'a> = (+ libgfx_rust::path::Path,+ FloatRect,+ &'a [crate::layout::fragment_tree::SvgColorGlyphRun],+ );+ let parts: Vec<Part<'_>> = match &segments {Some(segments) => {let range = match path_parts {PathParts::BeforeChildren => {@@ -528,6 +534,7 @@ fn paint_path_parts<O: Observer>((segment.path.copy_transformed(paint_transform),FloatRect::from_array(segment.path.bounding_box()),+ segment.color_glyph_runs.as_slice(),)}).collect()@@ -535,6 +542,7 @@ fn paint_path_parts<O: Observer>(None => vec![(computed_path.copy_transformed(paint_transform),FloatRect::from_array(computed_path.bounding_box()),+ &[][..],)],};let anti_alias = should_anti_alias(facts.should_anti_alias);@@ -542,7 +550,8 @@ fn paint_path_parts<O: Observer>(if recorder.draws_clip_path_geometry() {// https://drafts.fxtf.org/css-masking/#ClipPathElement: the raw geometry of each child// element, exclusive of rendering properties, defines a 1-bit mask.- for (path, _) in &parts {+ // AD-HOC: The glyphs of a color font have no outlines, so they're left out of the clipping path.+ for (path, _, _) in &parts {recorder.recorder.fill_path(FillPathParams {force_dark_role: ForceDarkRole::Svg,path,@@ -557,7 +566,7 @@ fn paint_path_parts<O: Observer>(let geometry =crate::painting::paintable_geometry::committed_svg_geometry(recorder.source, paintable).unwrap_or_default();- for (path, path_bounding_box) in &parts {+ for (path, path_bounding_box, color_glyph_runs) in &parts {// NB: A paint server is placed from the bounding box of the path it paints, so each part is painted with its// own.let paint_context = SvgPaintContext {@@ -572,6 +581,19 @@ fn paint_path_parts<O: Observer>(paint_order::FILL => {let fill_opacity = facts.fill_opacity;let fill_winding = WindingRule::from_raw(facts.fill_winding);+ // NB: The glyphs of a color font paint with their own colors, wherever the text has a fill.+ if !color_glyph_runs.is_empty() {+ let opacity = if paint_server_style(recorder, paintable, false, &paint_context).is_some() {+ Some(fill_opacity)+ } else {+ facts+ .fill_color+ .map(|fill_color| fill_opacity * f32::from(Color(fill_color).alpha()) / 255.0)+ };+ if let Some(opacity) = opacity {+ paint_color_glyph_runs(recorder, paintable, color_glyph_runs, opacity, device_scale);+ }+ }if let Some(paint_style) = paint_server_style(recorder, paintable, false, &paint_context) {recorder.recorder.fill_path(FillPathParams {force_dark_role: ForceDarkRole::Svg,@@ -611,6 +633,20 @@ fn paint_path_parts<O: Observer>(let stroke_dashoffset = facts.stroke_dashoffset * stroke_scale;let stroke_opacity = facts.stroke_opacity;+ // NB: A glyph of a color font has no outline to stroke, so a stroke paints the glyph itself.+ if !color_glyph_runs.is_empty() && stroke_thickness > 0.0 {+ let opacity = if paint_server_style(recorder, paintable, true, &paint_context).is_some() {+ Some(stroke_opacity)+ } else {+ facts+ .stroke_color+ .map(|stroke_color| stroke_opacity * f32::from(Color(stroke_color).alpha()) / 255.0)+ };+ if let Some(opacity) = opacity {+ paint_color_glyph_runs(recorder, paintable, color_glyph_runs, opacity, device_scale);+ }+ }+if let Some(paint_style) = paint_server_style(recorder, paintable, true, &paint_context) {recorder.recorder.stroke_path(StrokePathParams {force_dark_role: ForceDarkRole::Svg,@@ -650,6 +686,51 @@ fn paint_path_parts<O: Observer>(}}+fn paint_color_glyph_runs<O: Observer>(+ recorder: &mut PaintRecorder<'_, O>,+ paintable: NodeSlotId,+ runs: &[crate::layout::fragment_tree::SvgColorGlyphRun],+ opacity: f32,+ device_scale: f32,+) {+ use crate::painting::display_list::commands::{DisplayListGlyph, FontResourceId};+ use crate::painting::display_list::recorder::GlyphRunForRecording;++ let Some(style) = recorder.source.node_style_if_live(paintable) else {+ return;+ };+ let font_smoothing = style.inherited_text().font_smoothing;+ for run in runs {+ let font_id = recorder.register_font(&run.font);+ let left = (run.bounds.x * device_scale).floor();+ let top = (run.bounds.y * device_scale).floor();+ let device_rect = libgfx_rust::IntRect::new(+ left as i32,+ top as i32,+ ((run.bounds.right() * device_scale).ceil() - left) as i32,+ ((run.bounds.bottom() * device_scale).ceil() - top) as i32,+ );+ recorder.recorder.draw_glyph_run(+ libgfx_rust::FloatPoint { x: 0.0, y: 0.0 },+ GlyphRunForRecording {+ font_smoothing,+ font_id: FontResourceId(font_id),+ glyphs: run.glyphs.iter().map(|glyph| DisplayListGlyph {+ position: libgfx_rust::FloatPoint { x: glyph.x, y: glyph.y },+ glyph_id: glyph.glyph_id,+ }),+ },+ Color::from_rgb(0, 0, 0).with_opacity(opacity),+ 0.0,+ device_rect,+ f64::from(device_scale),+ libgfx_rust::Orientation::Horizontal,+ device_rect,+ ForceDarkRole::Svg,+ );+ }+}+pub(crate) fn svg_image_unquantized_device_rect(layout_arena: &impl PaintRead,paintable: NodeSlotId,diff --git a/Tests/LibWeb/Ref/expected/svg-text-color-emoji-ref.html b/Tests/LibWeb/Ref/expected/svg-text-color-emoji-ref.htmlnew file mode 100644index 00000000000..2da4a917d74--- /dev/null+++ b/Tests/LibWeb/Ref/expected/svg-text-color-emoji-ref.html@@ -0,0 +1,33 @@+<!DOCTYPE html>+<style>+ @font-face {+ font-family: Ahem;+ src: url("../data/fonts/Ahem.ttf");+ }+ @font-face {+ font-family: ColorEmojiFont;+ src: url("../data/fonts/NotoColorEmoji-Regular_subset.ttf");+ }+ @font-face {+ font-family: MonoEmojiFont;+ src: url("../data/fonts/NotoEmoji-Regular_subset.ttf");+ }+ body {+ margin: 0;+ }+ div {+ position: absolute;+ left: 20px;+ font: 50px/100px Ahem;+ white-space: nowrap;+ }+ span {+ line-height: 0;+ }+</style>+<!-- The baseline of each line is 65px below the top of its block: Ahem's ascent is 40px here, and half of the leading 25px. -->+<div style="top: 15px"><span style="font: 48px/0 ColorEmojiFont">🫨</span></div>+<div style="top: 115px">XX<span style="font: 50px/0 Ahem, ColorEmojiFont">🫨</span></div>+<div style="top: 215px"><span style="font: 48px/0 ColorEmojiFont">🫨</span></div>+<div style="top: 315px"><span style="font: 48px/0 ColorEmojiFont">🫨</span></div>+<div style="top: 315px; left: 120px"><span style="font: 48px/0 ColorEmojiFont">🫨</span></div>diff --git a/Tests/LibWeb/Ref/input/svg-text-color-emoji.html b/Tests/LibWeb/Ref/input/svg-text-color-emoji.htmlnew file mode 100644index 00000000000..5c3548f3ae1--- /dev/null+++ b/Tests/LibWeb/Ref/input/svg-text-color-emoji.html@@ -0,0 +1,36 @@+<!DOCTYPE html>+<link rel="match" href="../expected/svg-text-color-emoji-ref.html" />+<style>+ @font-face {+ font-family: Ahem;+ src: url("../data/fonts/Ahem.ttf");+ }+ @font-face {+ font-family: ColorEmojiFont;+ src: url("../data/fonts/NotoColorEmoji-Regular_subset.ttf");+ }+ @font-face {+ font-family: MonoEmojiFont;+ src: url("../data/fonts/NotoEmoji-Regular_subset.ttf");+ }+ body {+ margin: 0;+ }+ svg {+ display: block;+ }+</style>+<svg width="600" height="500">+ <!-- A glyph of a color font has no outline to fill, so it's drawn as it is. -->+ <text x="20" y="80" style="font: 48px ColorEmojiFont">🫨</text>+ <!-- Text and emoji, each in its own font. -->+ <text x="20" y="180" style="font: 50px Ahem, ColorEmojiFont">XX🫨</text>+ <!-- With the emoji variation selector, an emoji takes the color font, even after a font with a text glyph for it. -->+ <text x="20" y="280" style="font: 48px MonoEmojiFont, ColorEmojiFont">🫨️</text>+ <!-- An emoji keeps its own colors, whatever the fill is. It has no outline, so a stroke paints the emoji too. -->+ <text x="20" y="380" fill="red" style="font: 48px ColorEmojiFont">🫨</text>+ <text x="120" y="380" fill="none" stroke="blue" stroke-width="4" style="font: 48px ColorEmojiFont">🫨</text>+ <!-- Without a fill, or with a transparent one, it isn't drawn. -->+ <text x="20" y="480" fill="none" style="font: 48px ColorEmojiFont">🫨</text>+ <text x="120" y="480" fill="transparent" style="font: 48px ColorEmojiFont">🫨</text>+</svg>diff --git a/Tests/LibWeb/Text/expected/SVG/text-color-emoji-hit-testing.txt b/Tests/LibWeb/Text/expected/SVG/text-color-emoji-hit-testing.txtnew file mode 100644index 00000000000..a099c1ed26d--- /dev/null+++ b/Tests/LibWeb/Text/expected/SVG/text-color-emoji-hit-testing.txt@@ -0,0 +1,4 @@+In the middle of the emoji: link+Past the emoji: svg+In the second emoji of text scaled to twice its length: scaled-link+Past the text scaled to twice its length: svgdiff --git a/Tests/LibWeb/Text/expected/SVG/text-emoji-sequence.txt b/Tests/LibWeb/Text/expected/SVG/text-emoji-sequence.txtnew file mode 100644index 00000000000..f4f945e98d3--- /dev/null+++ b/Tests/LibWeb/Text/expected/SVG/text-emoji-sequence.txt@@ -0,0 +1,5 @@+An emoji has a length: true+Three emoji that joiners make one are as long as one emoji: true+Apart, they're as long as three: true+Ahem doesn't draw a "B" by itself: true+Nor a "B" after an "A" and a joiner: truediff --git a/Tests/LibWeb/Text/input/SVG/text-color-emoji-hit-testing.html b/Tests/LibWeb/Text/input/SVG/text-color-emoji-hit-testing.htmlnew file mode 100644index 00000000000..dea609b879a--- /dev/null+++ b/Tests/LibWeb/Text/input/SVG/text-color-emoji-hit-testing.html@@ -0,0 +1,40 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<style>+ @font-face {+ font-family: ColorEmoji;+ src: url("../../../Ref/data/fonts/NotoColorEmoji-Regular_subset.ttf");+ }+ body {+ margin: 0;+ }+ svg {+ display: block;+ font: 48px ColorEmoji;+ }+</style>+<svg width="400" height="200">+ <text id="text" x="20" y="60"><a id="link" href="#">🫨</a></text>+ <text id="scaled-text" x="20" y="160" lengthAdjust="spacingAndGlyphs"><a id="scaled-link" href="#">🫨🫨</a></text>+</svg>+<script>+ asyncTest(async done => {+ await document.fonts.load("48px ColorEmoji", "\u{1FAE8}");+ // A color glyph has no outline, so it's hit anywhere in its cell.+ const cell = document.getElementById("text").getExtentOfChar(0);+ const hit = document.elementFromPoint(cell.x + cell.width / 2, cell.y + cell.height / 2);+ println(`In the middle of the emoji: ${hit.id || hit.localName}`);+ const miss = document.elementFromPoint(cell.x + cell.width + 20, cell.y + cell.height / 2);+ println(`Past the emoji: ${miss.id || miss.localName}`);++ // The cell of an emoji is where textLength moves the emoji to.+ const scaledText = document.getElementById("scaled-text");+ scaledText.setAttribute("textLength", 2 * scaledText.getComputedTextLength());+ const scaledCell = scaledText.getExtentOfChar(2);+ const scaledHit = document.elementFromPoint(scaledCell.x + scaledCell.width / 4, scaledCell.y + scaledCell.height / 2);+ println(`In the second emoji of text scaled to twice its length: ${scaledHit.id || scaledHit.localName}`);+ const scaledMiss = document.elementFromPoint(scaledCell.x + scaledCell.width + 30, scaledCell.y + scaledCell.height / 2);+ println(`Past the text scaled to twice its length: ${scaledMiss.id || scaledMiss.localName}`);+ done();+ });+</script>diff --git a/Tests/LibWeb/Text/input/SVG/text-emoji-sequence.html b/Tests/LibWeb/Text/input/SVG/text-emoji-sequence.htmlnew file mode 100644index 00000000000..f9f4d46ef38--- /dev/null+++ b/Tests/LibWeb/Text/input/SVG/text-emoji-sequence.html@@ -0,0 +1,44 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<style>+ @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;+ }+ svg {+ font: 20px Ahem;+ }+ .letters {+ font-family: OnlyA, serif;+ }+</style>+<svg width="400" height="200">+ <text id="one" y="30">👨</text>+ <text id="joined" y="60">👨‍👩‍👧</text>+ <text id="apart" y="90">👨​👩​👧</text>+ <text id="letter" class="letters" y="120">B</text>+ <text id="letter-and-joiner" class="letters" y="150">A‍</text>+ <text id="letters-joined" class="letters" y="180">A‍B</text>+</svg>+<script>+ // Ahem has a glyph for the zero width joiner, and none for an emoji, which a fallback font draws.+ asyncTest(async done => {+ await document.fonts.load("20px Ahem");+ await document.fonts.load("20px OnlyA", "A");+ const length = id => document.getElementById(id).getComputedTextLength();+ println(`An emoji has a length: ${length("one") > 0}`);+ println(`Three emoji that joiners make one are as long as one emoji: ${length("joined") === length("one")}`);+ println(`Apart, they're as long as three: ${length("apart") === 3 * length("one")}`);+ // 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.+ println(`Ahem doesn't draw a "B" by itself: ${length("letter") !== 20}`);+ const afterJoiner = length("letters-joined") - length("letter-and-joiner");+ println(`Nor a "B" after an "A" and a joiner: ${Math.abs(afterJoiner - length("letter")) < 0.01}`);+ done();+ });+</script>