Truly independent web browser
LibWeb: Give a space the first font in the list that covers it
sideshowbarkerc75b8ed87fe1
diff --git a/Libraries/LibWeb/Rust/src/layout/text_chunker.rs b/Libraries/LibWeb/Rust/src/layout/text_chunker.rsindex 20731a952c0..4ac9b7edd5a 100644--- a/Libraries/LibWeb/Rust/src/layout/text_chunker.rs+++ b/Libraries/LibWeb/Rust/src/layout/text_chunker.rs@@ -1134,11 +1134,8 @@ struct TextChunker<'text> {current_index: usize,/// The fonts of the chunks, each once, which the chunks name by index.fonts: Vec<FontHandle>,- last_non_whitespace_font: Option<u16>,first_font_and_ascii_code_points_it_resolves: Option<(&'text FontHandle, u128)>,font_for_every_space: Option<&'text FontHandle>,- /// Where the look-ahead from a run of white space to the text after it last started, and where it ended.- space_look_ahead: std::cell::Cell<(usize, usize)>,}impl<'text> TextChunker<'text> {@@ -1181,10 +1178,8 @@ impl<'text> TextChunker<'text> {splits_at_orientation_changes: inputs.splits_at_orientation_changes,current_index: 0,fonts: Vec::new(),- last_non_whitespace_font: None,first_font_and_ascii_code_points_it_resolves,font_for_every_space,- space_look_ahead: std::cell::Cell::new((usize::MAX, usize::MAX)),}}@@ -1286,83 +1281,52 @@ impl<'text> TextChunker<'text> {.font_for_code_point_borrowed(code_point, self.emoji_presentation_at(code_unit_offset, code_point))}- fn font_for_space(&self, at_index: usize, space_code_point: u32) -> ChunkFont<'text> {+ fn font_for_space(&self, space_code_point: u32) -> Cow<'text, FontHandle> {if let Some(font) = self.font_for_every_space {- return ChunkFont::FromCascade(Cow::Borrowed(font));- }-- let has_glyph = |font: &FontHandle| font.contains_glyph(space_code_point);-- // 1. Prefer the last non-whitespace font in this node/run.- if let Some(last_font_index) = self.last_non_whitespace_font {- let last_font = &self.fonts[usize::from(last_font_index)];- if !last_font.is_emoji_font() && has_glyph(last_font) {- return ChunkFont::InTable(last_font_index);- }- }-- // 2. Look ahead to the next non-space to infer the base font of this run.- // NB: Each space or tab of a run looks ahead to the same text, so start from where the look-ahead from an- // earlier one in the run ended.- let (scanned_from, scanned_to) = self.space_look_ahead.get();- let mut i = if (scanned_from..=scanned_to).contains(&at_index) {- scanned_to- } else {- at_index- };- let scan_start = if i == at_index { at_index } else { scanned_from };- while i < self.text.len() {- let code_point = code_point_at(self.text, i);- if !is_interword_space(code_point) && code_point != '\t' as u32 && code_point != '\n' as u32 {- self.space_look_ahead.set((scan_start, i));- let font = self.font_for_code_point_at(i, code_point);- if !font.is_emoji_font() && has_glyph(&font) {- return ChunkFont::FromCascade(font);- }- // Text is coming from an emoji face; we'll fall back to (3).- break;- }- i = self- .grapheme_segmenter- .next_boundary(i, false)- .unwrap_or(self.text.len());+ return Cow::Borrowed(font);}- self.space_look_ahead.set((scan_start, i));- // 3. No text around (leading/trailing/all spaces) — pick a font with the glyph from the cascade.- ChunkFont::FromCascade(self.frozen_font_list.font_for_code_point_borrowed(+ // https://drafts.csswg.org/css-fonts-4/#font-matching-algorithm+ // NB: A space is matched like any other character: It takes the first font in the list whose unicode-range+ // covers it and that has a glyph for it, rather than the font of the text next to it.+ let font = self.frozen_font_list.font_for_code_point_borrowed(space_code_point,EmojiPresentation {is_emoji: false,forced: false,},- ))+ );+ if !font.is_emoji_font() {+ return font;+ }+ // AD-HOC: An emoji font listed ahead of a text font would give spaces its width, so a space skips it for the+ // first text font with a glyph for it.+ self.frozen_font_list.font_for_code_point_borrowed(+ space_code_point,+ EmojiPresentation {+ is_emoji: false,+ forced: true,+ },+ )}- fn expected_font_for(&self, code_point: u32) -> ChunkFont<'text> {+ fn expected_font_for(&self, code_point: u32) -> Cow<'text, FontHandle> {if is_interword_space(code_point) {- self.font_for_space(self.current_index, code_point)+ self.font_for_space(code_point)} else if code_point == '\t' as u32 {// NB: A tab is drawn as a shift to a tab stop rather than as a glyph, so it takes the font that a space in// its place would take, not a fallback font that happens to have a glyph for it.- self.font_for_space(self.current_index, ' ' as u32)+ self.font_for_space(' ' as u32)} else {- ChunkFont::FromCascade(self.font_for_code_point_at(self.current_index, code_point))+ self.font_for_code_point_at(self.current_index, code_point)}}- fn is_font_at_index(&self, font: &ChunkFont<'_>, font_index: u16) -> bool {- match font {- ChunkFont::InTable(index) => *index == font_index,- ChunkFont::FromCascade(font) => **font == self.fonts[usize::from(font_index)],- }+ fn is_font_at_index(&self, font: &FontHandle, font_index: u16) -> bool {+ *font == self.fonts[usize::from(font_index)]}- fn index_of_font(&mut self, font: ChunkFont<'text>) -> u16 {- let font = match font {- ChunkFont::InTable(index) => return index,- ChunkFont::FromCascade(font) => font,- };+ fn index_of_font(&mut self, font: Cow<'text, FontHandle>) -> u16 {let index = match self.fonts.iter().rposition(|kept| *kept == *font) {Some(index) => index,None => {@@ -1388,9 +1352,6 @@ impl<'text> TextChunker<'text> {let is_all_whitespace = self.text[start..end].iter().all(|unit| *unit <= 0x7f && code_point_is_ascii_space(u32::from(*unit)));- if !is_all_whitespace {- self.last_non_whitespace_font = Some(font_index);- }Some(TextChunk {start,length: length_in_code_units,@@ -1514,7 +1475,7 @@ impl<'text> TextChunker<'text> {// Otherwise, commit the whitespace!self.current_index = self.next_chunk_boundary();can_break_at_current_position = self.is_at_line_break_opportunity();- let space_font = self.font_for_space(self.current_index, code_point);+ let space_font = self.font_for_space(code_point);let space_font_index = self.index_of_font(space_font);if let Some(chunk) = self.try_commit_chunk(pending.start,@@ -1583,12 +1544,6 @@ impl<'text> TextChunker<'text> {}}-/// A font the chunker picked: one of the fonts of its chunks, or one the cascade answered with.-enum ChunkFont<'text> {- InTable(u16),- FromCascade(Cow<'text, FontHandle>),-}-pub(crate) fn text_chunks(pass: &LayoutPass<'_>,node: Node,diff --git a/Tests/LibWeb/Layout/expected/css-pseudo-element-display-list-item-without-marker.txt b/Tests/LibWeb/Layout/expected/css-pseudo-element-display-list-item-without-marker.txtindex bfc77939c71..e5996a4ed56 100644--- a/Tests/LibWeb/Layout/expected/css-pseudo-element-display-list-item-without-marker.txt+++ b/Tests/LibWeb/Layout/expected/css-pseudo-element-display-list-item-without-marker.txt@@ -37,9 +37,11 @@ Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] childrenListItemBox <(anonymous)> at [8,136] [0+0+0 784 0+0+0] [0+0+0 19 0+0+0] children: inlinefrag 0 from GeneratedTextNode start: 0, length: 5, rect: [8,138 49.828125x16] baseline: 13"shown"- ListItemMarkerBox <(anonymous)> at [-33,136] [0+0+0 40.625 0+0+0] [0+0+0 19 0+0+0] [BFC] children: inline- frag 0 from GeneratedTextNode start: 0, length: 2, rect: [-33,138 40.625x16] baseline: 13- "▪ "+ ListItemMarkerBox <(anonymous)> at [-20,136] [0+0+0 28.3125 0+0+0] [0+0+0 19 0+0+0] [BFC] children: inline+ frag 0 from GeneratedTextNode start: 0, length: 1, rect: [-20,138 20.3125x16] baseline: 13+ "▪"+ frag 1 from GeneratedTextNode start: 1, length: 1, rect: [0.3125,138 8x16] baseline: 13+ " "GeneratedTextNode <(anonymous)> (not painted)GeneratedTextNode <(anonymous)> (not painted)BlockContainer <(anonymous)> at [8,155] [0+0+0 784 0+0+0] [0+0+0 16 0+0+0] children: inline@@ -59,9 +61,11 @@ Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] childrenListItemBox <(anonymous)> at [8,203] [0+0+0 784 0+0+0] [0+0+0 19 0+0+0] children: inlinefrag 0 from GeneratedTextNode start: 0, length: 7, rect: [8,205 53.15625x16] baseline: 13"toggled"- ListItemMarkerBox <(anonymous)> at [-33,203] [0+0+0 40.625 0+0+0] [0+0+0 19 0+0+0] [BFC] children: inline- frag 0 from GeneratedTextNode start: 0, length: 2, rect: [-33,205 40.625x16] baseline: 13- "▪ "+ ListItemMarkerBox <(anonymous)> at [-20,203] [0+0+0 28.3125 0+0+0] [0+0+0 19 0+0+0] [BFC] children: inline+ frag 0 from GeneratedTextNode start: 0, length: 1, rect: [-20,205 20.3125x16] baseline: 13+ "▪"+ frag 1 from GeneratedTextNode start: 1, length: 1, rect: [0.3125,205 8x16] baseline: 13+ " "GeneratedTextNode <(anonymous)> (not painted)GeneratedTextNode <(anonymous)> (not painted)BlockContainer <(anonymous)> at [8,222] [0+0+0 784 0+0+0] [0+0+0 16 0+0+0] children: inlinediff --git a/Tests/LibWeb/Layout/expected/line-height-normal-uses-font-metrics.txt b/Tests/LibWeb/Layout/expected/line-height-normal-uses-font-metrics.txtindex f398cbec5c3..5d2515feb37 100644--- a/Tests/LibWeb/Layout/expected/line-height-normal-uses-font-metrics.txt+++ b/Tests/LibWeb/Layout/expected/line-height-normal-uses-font-metrics.txt@@ -2,8 +2,12 @@ Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] childrenBlockContainer <html> at [0,0] [0+0+0 800 0+0+0] [0+0+0 22 0+0+0] [BFC] children: not-inlineBlockContainer <body> at [0,0] [0+0+0 800 0+0+0] [0+0+0 22 0+0+0] children: not-inlineBlockContainer <div#tall> at [0,0] [0+0+0 800 0+0+0] [0+0+0 22 0+0+0] children: inline- frag 0 from TextNode start: 0, length: 12, rect: [0,0 97.515625x22] baseline: 17- "Tall metrics"+ frag 0 from TextNode start: 0, length: 4, rect: [0,0 30.109375x22] baseline: 17+ "Tall"+ frag 1 from TextNode start: 4, length: 1, rect: [30.109375,0 4.15625x22] baseline: 17+ " "+ frag 2 from TextNode start: 5, length: 7, rect: [34.265625,0 59.40625x22] baseline: 17+ "metrics"TextNode <#text> (not painted)SC for Viewport<#document> [0,0 800x600] (z-index: auto)diff --git a/Tests/LibWeb/Layout/expected/pseudo-element-content-replacement-list-item.txt b/Tests/LibWeb/Layout/expected/pseudo-element-content-replacement-list-item.txtindex 201948a5400..31da1472995 100644--- a/Tests/LibWeb/Layout/expected/pseudo-element-content-replacement-list-item.txt+++ b/Tests/LibWeb/Layout/expected/pseudo-element-content-replacement-list-item.txt@@ -3,21 +3,25 @@ Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] childrenBlockContainer <body> at [8,8] [8+0+0 784 0+0+8] [8+0+0 40 0+0+8] children: not-inlineBlockContainer <div> at [8,8] [0+0+0 784 0+0+0] [0+0+0 40 0+0+0] children: not-inlineListItemBox <(anonymous)> at [8,8] [0+0+0 784 0+0+0] [0+0+0 20 0+0+0] children: inline- frag 0 from ListItemMarkerBox start: 0, length: 0, rect: [8,9 40.625x19] baseline: 15- frag 1 from ImageBox start: 0, length: 0, rect: [48.625,8 16x16] baseline: 16- ListItemMarkerBox <(anonymous)> at [8,9] [0+0+0 40.625 0+0+0] [0+0+0 19 0+0+0] [BFC] children: inline- frag 0 from GeneratedTextNode start: 0, length: 2, rect: [8,11 40.625x16] baseline: 13- "▪ "+ frag 0 from ListItemMarkerBox start: 0, length: 0, rect: [8,9 28.3125x19] baseline: 15+ frag 1 from ImageBox start: 0, length: 0, rect: [36.3125,8 16x16] baseline: 16+ ListItemMarkerBox <(anonymous)> at [8,9] [0+0+0 28.3125 0+0+0] [0+0+0 19 0+0+0] [BFC] children: inline+ frag 0 from GeneratedTextNode start: 0, length: 1, rect: [8,11 20.3125x16] baseline: 13+ "▪"+ frag 1 from GeneratedTextNode start: 1, length: 1, rect: [28.3125,11 8x16] baseline: 13+ " "GeneratedTextNode <(anonymous)> (not painted)- ImageBox <(anonymous)> at [48.625,8] [0+0+0 16 0+0+0] [0+0+0 16 0+0+0] children: not-inline+ ImageBox <(anonymous)> at [36.3125,8] [0+0+0 16 0+0+0] [0+0+0 16 0+0+0] children: not-inlineListItemBox <(anonymous)> at [8,28] [0+0+0 784 0+0+0] [0+0+0 20 0+0+0] children: inline- frag 0 from ListItemMarkerBox start: 0, length: 0, rect: [8,29 40.625x19] baseline: 15- frag 1 from ImageBox start: 0, length: 0, rect: [48.625,28 16x16] baseline: 16- ListItemMarkerBox <(anonymous)> at [8,29] [0+0+0 40.625 0+0+0] [0+0+0 19 0+0+0] [BFC] children: inline- frag 0 from GeneratedTextNode start: 0, length: 2, rect: [8,31 40.625x16] baseline: 13- "▪ "+ frag 0 from ListItemMarkerBox start: 0, length: 0, rect: [8,29 28.3125x19] baseline: 15+ frag 1 from ImageBox start: 0, length: 0, rect: [36.3125,28 16x16] baseline: 16+ ListItemMarkerBox <(anonymous)> at [8,29] [0+0+0 28.3125 0+0+0] [0+0+0 19 0+0+0] [BFC] children: inline+ frag 0 from GeneratedTextNode start: 0, length: 1, rect: [8,31 20.3125x16] baseline: 13+ "▪"+ frag 1 from GeneratedTextNode start: 1, length: 1, rect: [28.3125,31 8x16] baseline: 13+ " "GeneratedTextNode <(anonymous)> (not painted)- ImageBox <(anonymous)> at [48.625,28] [0+0+0 16 0+0+0] [0+0+0 16 0+0+0] children: not-inline+ ImageBox <(anonymous)> at [36.3125,28] [0+0+0 16 0+0+0] [0+0+0 16 0+0+0] children: not-inlineSC for Viewport<#document> [0,0 800x600] (z-index: auto)SC for BlockContainer<HTML> [0,0 800x56] (z-index: auto)diff --git a/Tests/LibWeb/Text/expected/Editing/contenteditable-arrow-navigation.txt b/Tests/LibWeb/Text/expected/Editing/contenteditable-arrow-navigation.txtindex ff4da4fa229..0a9e8b66a82 100644--- a/Tests/LibWeb/Text/expected/Editing/contenteditable-arrow-navigation.txt+++ b/Tests/LibWeb/Text/expected/Editing/contenteditable-arrow-navigation.txt@@ -4,16 +4,16 @@ Right: position=2 character="l"Right: position=3 character="l"Right: position=4 character="o"Right: position=5 character=" "-Down: position=23 character="e"-Left: position=22 character="r"-Up: position=3 character="l"+Down: position=28 character="👩🏼❤️👨🏻"+Left: position=27 character=" "+Up: position=2 character="l"+Right: position=3 character="l"Right: position=4 character="o"Right: position=5 character=" "Right: position=6 character="👩🏼❤️👨🏻"-Right: position=18 character=" "-Down: position=26 character="y"-Up: position=18 character=" "-Down: position=26 character="y"-Left: position=25 character="m"-Left: position=24 character=" "+Down: position=40 character=" "Up: position=6 character="👩🏼❤️👨🏻"+Down: position=40 character=" "+Left: position=28 character="👩🏼❤️👨🏻"+Left: position=27 character=" "+Up: position=2 character="l"diff --git a/Tests/LibWeb/Text/expected/css/space-font-matches-like-any-character.txt b/Tests/LibWeb/Text/expected/css/space-font-matches-like-any-character.txtnew file mode 100644index 00000000000..82f2f6b037c--- /dev/null+++ b/Tests/LibWeb/Text/expected/css/space-font-matches-like-any-character.txt@@ -0,0 +1,4 @@+digits: space is narrower than a digit: true+digits, no-break space: narrower than a digit: true+words: four spaces are 80px wide: true+words, no-break space: 20px wide: truediff --git a/Tests/LibWeb/Text/input/css/space-font-matches-like-any-character.html b/Tests/LibWeb/Text/input/css/space-font-matches-like-any-character.htmlnew file mode 100644index 00000000000..5cec1f11569--- /dev/null+++ b/Tests/LibWeb/Text/input/css/space-font-matches-like-any-character.html@@ -0,0 +1,26 @@+<!DOCTYPE html>+<style>+ @font-face { font-family: AhemDigits; src: url(../wpt-import/fonts/Ahem.ttf); unicode-range: U+30-39; }+ @font-face { font-family: AhemSpaces; src: url(../wpt-import/fonts/Ahem.ttf); unicode-range: U+20, U+A0; }+ div { font-size: 20px; white-space: pre; }+</style>+<!-- A space between digits doesn't take the digits' font, since its unicode-range leaves the space out. -->+<div style="font-family: AhemDigits, serif"><span id="digits">0 1</span></div>+<div style="font-family: AhemDigits, serif"><span id="digits-nbsp">0 1</span></div>+<!-- A space between words from a later font takes the first font's space, since the first font covers it. -->+<div style="font-family: AhemSpaces, serif"><span id="words">ab cd</span></div>+<div style="font-family: AhemSpaces, serif"><span id="words-nbsp">ab cd</span></div>+<div style="font-family: AhemSpaces, serif"><span id="letters">abcd</span></div>+<script src="../include.js"></script>+<script>+ asyncTest(async done => {+ await document.fonts.load("20px AhemDigits", "0");+ await document.fonts.load("20px AhemSpaces", " ");+ const width = id => document.getElementById(id).getBoundingClientRect().width;+ println(`digits: space is narrower than a digit: ${width("digits") - 40 < 20}`);+ println(`digits, no-break space: narrower than a digit: ${width("digits-nbsp") - 40 < 20}`);+ println(`words: four spaces are 80px wide: ${width("words") - width("letters") === 80}`);+ println(`words, no-break space: 20px wide: ${width("words-nbsp") - width("letters") === 20}`);+ done();+ });+</script>