Skip to content

Truly independent web browser

LibWeb: Remember cached text chunks that need no shaping context

sideshowbarker6f0e2c2e58e0
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs b/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs
index 7bb979e8451..ab377181463 100644
--- a/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs
+++ b/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs
@@ -945,6 +945,7 @@ struct TextNodeContext<'pass> {
text: &'pass [u16],
next_chunk_index: usize,
independent_chunks_until: usize,
+ can_memoize_independent_chunks: bool,
style: StyleValues<'pass>,
letter_spacing: f32,
word_spacing: f32,
@@ -1354,14 +1355,26 @@ impl<'pass> InlineContentPreparer<'pass> {
self.is_unidirectional_left_to_right,
);
let facts = self.facts(text_node);
+ let letter_spacing = style.letter_spacing().to_double() as f32;
+ let word_spacing = style.word_spacing().to_double() as f32;
+ let can_memoize_independent_chunks =
+ self.is_unidirectional_left_to_right && style.writing_mode() == writing_mode::HORIZONTAL_TB;
+ let independent_chunks_until = if can_memoize_independent_chunks
+ && chunks.independent_chunks_for_spacing.get() == Some(&(letter_spacing.to_bits(), word_spacing.to_bits()))
+ {
+ usize::MAX
+ } else {
+ 0
+ };
self.text_node_context = Some(TextNodeContext {
chunks,
text: &self.pass.text_content(text_node).text,
next_chunk_index: 0,
- independent_chunks_until: 0,
+ independent_chunks_until,
+ can_memoize_independent_chunks,
style,
- letter_spacing: style.letter_spacing().to_double() as f32,
- word_spacing: style.word_spacing().to_double() as f32,
+ letter_spacing,
+ word_spacing,
writing_mode: style.writing_mode(),
text_orientation: style.text_orientation(),
should_wrap_lines,
@@ -1470,6 +1483,12 @@ impl<'pass> InlineContentPreparer<'pass> {
.expect("layout read a font group that never received a frozen font list");
(chunk, first_available_font)
} else {
+ if text_context.can_memoize_independent_chunks {
+ let _ = chunks.independent_chunks_for_spacing.set((
+ text_context.letter_spacing.to_bits(),
+ text_context.word_spacing.to_bits(),
+ ));
+ }
if !text_context.text.is_empty() {
let regional_indicators = text_chunker::trailing_regional_indicator_count(text_context.text);
// NB: Each regional indicator is a surrogate pair.
@@ -1615,6 +1634,12 @@ impl<'pass> InlineContentPreparer<'pass> {
text_context.word_spacing,
text_chunker::joining_context(full_text, shaped_range),
);
+ // Remember the decision only if every combined run is restored to its original chunks. Text, font, and break-rule
+ // changes replace the chunk memo, while spacing is checked separately.
+ if text_context.next_chunk_index != next_chunk_index && (!has_soft_wraps || shape.requires_context_for_breaks())
+ {
+ text_context.can_memoize_independent_chunks = false;
+ }
if has_soft_wraps && !shape.requires_context_for_breaks() {
// Independent glyphs can keep their small, separately measured chunks. Remember the end of this run so
// that preparing each following chunk doesn't shape the same suffix again.
diff --git a/Libraries/LibWeb/Rust/src/layout/rendered_text.rs b/Libraries/LibWeb/Rust/src/layout/rendered_text.rs
index c87cf7e9ac1..74ba846f28d 100644
--- a/Libraries/LibWeb/Rust/src/layout/rendered_text.rs
+++ b/Libraries/LibWeb/Rust/src/layout/rendered_text.rs
@@ -172,6 +172,8 @@ pub(crate) struct TextChunkCacheKey {
pub(crate) struct CachedTextChunks {
key: TextChunkCacheKey,
chunks: super::text_chunker::ChunkedText,
+ /// Spacing for which horizontal LTR preparation kept every original chunk separate.
+ pub(super) independent_chunks_for_spacing: std::sync::OnceLock<(u32, u32)>,
}
impl CachedTextChunks {
@@ -282,6 +284,7 @@ impl TextMemos {
let entry = Arc::new(CachedTextChunks {
key: key.clone(),
chunks: compute(),
+ independent_chunks_for_spacing: Default::default(),
});
self.chunks.with_row_mut(index, |memo| {
*memo = Some(ChunksOfText {
diff --git a/Tests/LibWeb/Ref/expected/ligatures-across-soft-breaks-after-reflow-ref.html b/Tests/LibWeb/Ref/expected/ligatures-across-soft-breaks-after-reflow-ref.html
new file mode 100644
index 00000000000..4b51dfbe9a9
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/ligatures-across-soft-breaks-after-reflow-ref.html
@@ -0,0 +1,20 @@
+<!DOCTYPE html>
+<html class="reftest-wait">
+<style>
+ @font-face {
+ font-family: "Programming";
+ src: url("../../Assets/FiraCode-Latin.woff2");
+ }
+ @font-face {
+ font-family: "Ligatures";
+ src: url("../../Assets/Lato-Bold.ttf");
+ }
+ p { font: 16px "Programming"; white-space: nowrap; }
+ #spacing { font-family: "Ligatures"; }
+</style>
+<p>LYoWAT - ff fi fl ffl -&gt; --&gt; ---&gt; &lt;- &lt;-&gt; &lt;=&gt; =&gt; ==&gt; &lt;==</p>
+<p>LYoWAT - ff fi fl ffl -&gt; --&gt; ---&gt; &lt;- &lt;-&gt; &lt;=&gt; =&gt; ==&gt; &lt;==</p>
+<p id="spacing">fi ffi fifi offi fiffi</p>
+<script>
+ document.fonts.ready.then(() => document.documentElement.classList.remove("reftest-wait"));
+</script>
diff --git a/Tests/LibWeb/Ref/input/ligatures-across-soft-breaks-after-reflow.html b/Tests/LibWeb/Ref/input/ligatures-across-soft-breaks-after-reflow.html
new file mode 100644
index 00000000000..6c7a12fc246
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/ligatures-across-soft-breaks-after-reflow.html
@@ -0,0 +1,41 @@
+<!DOCTYPE html>
+<html class="reftest-wait">
+<link rel="match" href="../expected/ligatures-across-soft-breaks-after-reflow-ref.html">
+<style>
+ @font-face {
+ font-family: "Programming";
+ src: url("../../Assets/FiraCode-Latin.woff2");
+ }
+ @font-face {
+ font-family: "Ligatures";
+ src: url("../../Assets/Lato-Bold.ttf");
+ }
+ p { font: 16px "Programming"; word-break: break-all; width: 600px; }
+ .no-context { font-feature-settings: "calt" 0; }
+ #spacing { font-family: "Ligatures"; }
+ .spaced { letter-spacing: 2px; }
+</style>
+<p id="features" class="no-context">LYoWAT - ff fi fl ffl -&gt; --&gt; ---&gt; &lt;- &lt;-&gt; &lt;=&gt; =&gt; ==&gt; &lt;==</p>
+<p id="text">abcdefghij abcdefghij abcdefghij</p>
+<p id="spacing" class="spaced">fi ffi fifi offi fiffi</p>
+<script>
+ document.fonts.ready.then(() => {
+ // Lay each paragraph out several times while its glyphs don't need context across its break opportunities,
+ // then change what decides that, and lay it out several times again.
+ const paragraphs = document.querySelectorAll("p");
+ function relayout() {
+ for (let i = 0; i < 4; i++) {
+ for (const p of paragraphs) {
+ p.style.width = i % 2 ? "600px" : "601px";
+ }
+ document.body.offsetHeight;
+ }
+ }
+ relayout();
+ document.getElementById("features").classList.remove("no-context");
+ document.getElementById("text").firstChild.data = "LYoWAT - ff fi fl ffl -> --> ---> <- <-> <=> => ==> <==";
+ document.getElementById("spacing").classList.remove("spaced");
+ relayout();
+ document.documentElement.classList.remove("reftest-wait");
+ });
+</script>