Truly independent web browser
LibWeb: Line up the text offsets of glyphs after a leading tab
sideshowbarkere85a2e7ab8e8
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs b/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rsindex cc9fd6c91c8..0ff0c67d8bd 100644--- a/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs+++ b/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs@@ -83,6 +83,33 @@ fn shaped_glyphs_from(shape.with_baseline_start(baseline_start_x).into_glyphs()}+/// `glyphs`, the glyphs of a text item past its leading tabs, after a glyph that paints nothing standing for the+/// `tab_count` tabs and advancing to the tab stop at `tab_advance`, so that the code units of the item's glyphs line up+/// with those of its text.+fn glyphs_after_tab_spacer(+ glyphs: &libgfx_rust::text_layout::GlyphBuffer,+ tab_count: usize,+ tab_advance: f32,+) -> libgfx_rust::text_layout::GlyphBuffer {+ use libgfx_rust::text_layout::{DrawGlyph, GlyphBuffer, PlacedGlyph};+ let tab_count_u32 = u32::try_from(tab_count).expect("tab counts fit in 32 bits");+ let mut with_spacer = GlyphBuffer::default();+ with_spacer.push_placed(PlacedGlyph {+ glyph: DrawGlyph::new(tab_advance, 0, tab_count, false),+ x: 0.0,+ y: glyphs.placed().next().map_or(0.0, |placed| placed.y),+ cluster_start: 0,+ is_rtl: false,+ });+ for placed in glyphs.placed() {+ with_spacer.push_placed(PlacedGlyph {+ cluster_start: placed.cluster_start + tab_count_u32,+ ..placed+ });+ }+ with_spacer+}+fn reshaped_glyph_run(text: &[u16],font: &libgfx_rust::font::FontHandle,@@ -1642,6 +1669,7 @@ impl<'context> RunItemResolver<'_, 'context> {let prepared = self.prepared;let mut inline_offset = 0.0f32;let mut upright_advance_after_tabs = None;+ let mut tab_count = 0;if prepared_item.has(PreparedItem::TAB_STOP) {let tab_stop = prepared.tab_stops[self.next_tab_stop];self.next_tab_stop += 1;@@ -1658,6 +1686,7 @@ impl<'context> RunItemResolver<'_, 'context> {tab_stop_distance = tab_stop_distance * tab_stop.tab_count as usize;inline_offset = tab_stop_distance.to_double() as f32;upright_advance_after_tabs = tab_stop.upright_advance;+ tab_count = tab_stop.tab_count as usize;}let shape = prepared_item.shape();let mut width = shape.width();@@ -1666,6 +1695,8 @@ impl<'context> RunItemResolver<'_, 'context> {let mut glyphs = shaped_glyphs_from(shape, inline_offset);if let Some(advance) = upright_advance_after_tabs {width = set_glyphs_upright(&mut glyphs, inline_offset, advance);+ } else if tab_count > 0 {+ glyphs = glyphs_after_tab_spacer(&glyphs, tab_count, inline_offset);}owned_glyph_run = Some(Box::new(OwnedGlyphRun {glyphs,diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/background.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/background.rsindex 27432ef334a..b914c6399a1 100644--- a/Libraries/LibWeb/Rust/src/painting/record/paint/background.rs+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/background.rs@@ -1140,6 +1140,7 @@ fn append_text_clip_paths<O: Observer>(recorder: &mut PaintRecorder<'_, O>, fragglyphs: run.glyphs.placed()+ .filter(|placed| placed.glyph.should_paint()).map(crate::painting::display_list::commands::DisplayListGlyph::from),},libgfx_rust::Color::from_rgb(0, 0, 0),diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/text.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/text.rsindex e108a168870..0f44676cdb4 100644--- a/Libraries/LibWeb/Rust/src/painting/record/paint/text.rs+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/text.rs@@ -704,6 +704,7 @@ fn paint_text_shadow<O: Observer>(.placed().skip(span_glyphs.start).take(span_glyphs.len())+ .filter(|placed| placed.glyph.should_paint()).map(DisplayListGlyph::from),},scale,@@ -774,7 +775,11 @@ fn paint_text_fragment<O: Observer>(.inherited_text().font_smoothing,font_id: FontResourceId(font_id),- glyphs: run.glyphs.placed().map(DisplayListGlyph::from),+ glyphs: run+ .glyphs+ .placed()+ .filter(|placed| placed.glyph.should_paint())+ .map(DisplayListGlyph::from),};// Paint text, clipped to span range if not full fragment.diff --git a/Tests/LibWeb/Text/expected/range-rects-after-tab.txt b/Tests/LibWeb/Text/expected/range-rects-after-tab.txtnew file mode 100644index 00000000000..b884c6fff77--- /dev/null+++ b/Tests/LibWeb/Text/expected/range-rects-after-tab.txt@@ -0,0 +1,2 @@+word after a leading tab: true+text after a tab mid-line: truediff --git a/Tests/LibWeb/Text/input/range-rects-after-tab.html b/Tests/LibWeb/Text/input/range-rects-after-tab.htmlnew file mode 100644index 00000000000..30fe3f6c031--- /dev/null+++ b/Tests/LibWeb/Text/input/range-rects-after-tab.html@@ -0,0 +1,26 @@+<!DOCTYPE html>+<script src="include.js"></script>+<style>+pre {+ font: 13px monospace;+ margin: 0;+}+</style>+<pre id="text"> pkgname =</pre>+<pre> <span id="pkgname">pkgname</span> <span id="equals">=</span></pre>+<script>+ // The text after a tab is where the tab stop puts it, both for the text itself and for ranges in it.+ test(() => {+ const text = document.getElementById("text").firstChild;+ const rangeRect = (start, end) => {+ const range = document.createRange();+ range.setStart(text, start);+ range.setEnd(text, end);+ return range.getBoundingClientRect();+ };+ const pkgname = document.getElementById("pkgname").getBoundingClientRect();+ const equals = document.getElementById("equals").getBoundingClientRect();+ println(`word after a leading tab: ${rangeRect(1, 8).left === pkgname.left && rangeRect(1, 8).width === pkgname.width}`);+ println(`text after a tab mid-line: ${rangeRect(9, 10).left === equals.left}`);+ });+</script>