Truly independent web browser
LibWeb: Keep characters after a soft hyphen in place in text rects
sideshowbarker733a4004785f
diff --git a/Libraries/LibWeb/Rust/src/painting/text_fragment.rs b/Libraries/LibWeb/Rust/src/painting/text_fragment.rsindex 3b3af435452..087d89729cd 100644--- a/Libraries/LibWeb/Rust/src/painting/text_fragment.rs+++ b/Libraries/LibWeb/Rust/src/painting/text_fragment.rs@@ -240,25 +240,45 @@ pub(crate) fn selection_offsets_for_dom_range(})}+/// Calls `callback` with each cluster of a fragment's glyph run, in glyph order: the code units it covers+/// (`cluster_start..cluster_end`) and its advance. Each glyph that covers code units starts a cluster, even one with no+/// advance (e.g. a soft hyphen's), and a glyph that covers none (another glyph of the same cluster) adds its advance to+/// the cluster before it. Stops early when `callback` returns false.fn for_each_cluster_in_glyph_run(glyphs: &[libgfx_rust::text_layout::DrawGlyph],fragment_length_in_code_units: usize,mut callback: impl FnMut(usize, usize, f32) -> bool,) {let mut cursor = 0usize;+ // The cluster whose glyphs are still being added up, and the advance of glyphs that came before any code unit.+ let mut pending: Option<(usize, usize, f32)> = None;+ let mut leading_advance = 0.0f32;for glyph in glyphs {- if glyph.advance == 0.0 {+ if glyph.length_in_code_units() == 0 {+ match &mut pending {+ Some((_, _, width)) => *width += glyph.advance,+ None => leading_advance += glyph.advance,+ }continue;}let cluster_start = cursor;let cluster_end = (cursor + glyph.length_in_code_units()).min(fragment_length_in_code_units);cursor = cluster_end;- if cluster_end <= cluster_start {- continue;- }- if !callback(cluster_start, cluster_end, glyph.advance) {+ if let Some((start, end, width)) = pending.take()+ && !callback(start, end, width)+ {return;}+ if cluster_end > cluster_start {+ pending = Some((+ cluster_start,+ cluster_end,+ glyph.advance + std::mem::take(&mut leading_advance),+ ));+ }+ }+ if let Some((start, end, width)) = pending {+ callback(start, end, width);}}diff --git a/Tests/LibWeb/Text/expected/range-rects-after-soft-hyphen.txt b/Tests/LibWeb/Text/expected/range-rects-after-soft-hyphen.txtnew file mode 100644index 00000000000..464aa48b74f--- /dev/null+++ b/Tests/LibWeb/Text/expected/range-rects-after-soft-hyphen.txt@@ -0,0 +1,2 @@+c starts where b ends: true+d starts where c ends: truediff --git a/Tests/LibWeb/Text/input/range-rects-after-soft-hyphen.html b/Tests/LibWeb/Text/input/range-rects-after-soft-hyphen.htmlnew file mode 100644index 00000000000..b676683011a--- /dev/null+++ b/Tests/LibWeb/Text/input/range-rects-after-soft-hyphen.html@@ -0,0 +1,26 @@+<!DOCTYPE html>+<script src="include.js"></script>+<style>+ #text {+ font: 16px SerenitySans;+ }+</style>+<!-- A soft hyphen that doesn't end a line renders nothing, so the characters after it are where they'd be without it:+ A range around a character starts where the one before it ends. -->+<div id="text">ab­cd</div>+<script>+ test(() => {+ const text = document.getElementById("text").firstChild;+ const range = document.createRange();+ const rectOf = index => {+ range.setStart(text, index);+ range.setEnd(text, index + 1);+ return range.getBoundingClientRect();+ };+ const b = rectOf(1);+ const c = rectOf(3);+ const d = rectOf(4);+ println(`c starts where b ends: ${Math.abs(c.x - b.right) < 0.5}`);+ println(`d starts where c ends: ${Math.abs(d.x - c.right) < 0.5}`);+ });+</script>