Skip to content

Truly independent web browser

LibWeb: Append left-to-right runs to fragments without placements

Aliaksandr Kalenik7d9a5f2d3a9f
diff --git a/Libraries/LibGfx/Rust/src/text_layout.rs b/Libraries/LibGfx/Rust/src/text_layout.rs
index 665e56ccbf0..27582ea5d35 100644
--- a/Libraries/LibGfx/Rust/src/text_layout.rs
+++ b/Libraries/LibGfx/Rust/src/text_layout.rs
@@ -402,6 +402,15 @@ fn placements_follow_from_glyphs(shaped: &[ShapedGlyph], glyphs: &[DrawGlyph]) -
})
}
+/// Where the glyphs of a run that stores no placements end, as of the run having `glyph_count` glyphs. A run holds no
+/// such thing itself: only runs that grow need it, and they need not add up their advances for every piece they take.
+#[derive(Clone, Copy, Debug, PartialEq)]
+pub struct DerivedPlacementsEnd {
+ glyph_count: usize,
+ pen: f32,
+ code_units: u32,
+}
+
/// The glyphs of a run, and where the run places them. A run stores no placements where they follow from the glyphs,
/// see `placements_follow_from_glyphs()`, which is what plain left to right text does.
#[derive(Clone, Debug, Default)]
@@ -540,6 +549,48 @@ impl GlyphBuffer {
glyphs.drain(..range.start);
}
+ /// Appends the glyphs of `run` at `x_offset` and at `cluster_offset` code units into the text, if their placements
+ /// then still follow from the glyphs, and answers whether it did. `end` is what the last call that appended to this
+ /// run left there, if any.
+ pub fn append_keeping_placements_derived(
+ &mut self,
+ end: &mut Option<DerivedPlacementsEnd>,
+ run: &GlyphBuffer,
+ x_offset: f32,
+ cluster_offset: u32,
+ ) -> bool {
+ if self.placements.is_some()
+ || run.placements.is_some()
+ || self.baseline_y.to_bits() != run.baseline_y.to_bits()
+ {
+ return false;
+ }
+ let current_end = (*end)
+ .filter(|end| end.glyph_count == self.len())
+ .unwrap_or_else(|| DerivedPlacementsEnd {
+ glyph_count: self.len(),
+ pen: self
+ .iter()
+ .map(|glyph| glyph.advance)
+ .fold(0.0, |pen, advance| pen + advance),
+ code_units: self.iter().map(|glyph| glyph.length_in_code_units() as u32).sum(),
+ });
+ if current_end.pen.to_bits() != x_offset.to_bits() || current_end.code_units != cluster_offset {
+ return false;
+ }
+ let Some(pen) = exact_pen_position_past(current_end.pen, run) else {
+ return false;
+ };
+ self.glyphs.to_mut().extend_from_slice(run);
+ *end = Some(DerivedPlacementsEnd {
+ glyph_count: self.len(),
+ pen,
+ code_units: current_end.code_units
+ + run.iter().map(|glyph| glyph.length_in_code_units() as u32).sum::<u32>(),
+ });
+ true
+ }
+
/// The glyphs from the one at `index` on, with that glyph at the start of the run and of the text.
#[must_use]
pub fn remainder_from(&self, index: usize) -> Self {
@@ -1125,6 +1176,78 @@ mod glyph_buffer_tests {
assert_places_glyphs_as_shaped(&stored, &expected);
}
+ #[test]
+ fn appending_at_the_end_of_a_run_keeps_placements_derived() {
+ let first = plain_run();
+ let second = plain_run();
+ let mut buffer = GlyphBuffer::from_shaped(&first);
+ let appended = GlyphBuffer::from_shaped(&second);
+ let mut end = None;
+
+ assert!(buffer.append_keeping_placements_derived(&mut end, &appended, 20.75, 3));
+ assert!(buffer.append_keeping_placements_derived(&mut end, &appended, 41.5, 6));
+ assert!(buffer.placements.is_none());
+
+ let mut by_placing_glyphs = GlyphBuffer::from_shaped(&first);
+ for (x_offset, cluster_offset) in [(20.75, 3), (41.5, 6)] {
+ for placed in appended.placed() {
+ by_placing_glyphs.push_placed(PlacedGlyph {
+ x: placed.x + x_offset,
+ cluster_start: placed.cluster_start + cluster_offset,
+ ..placed
+ });
+ }
+ }
+ assert!(by_placing_glyphs.placements.is_some());
+ let shaped: Vec<ShapedGlyph> = by_placing_glyphs.placed().map(ShapedGlyph::from).collect();
+ assert_places_glyphs_as_shaped(&buffer, &shaped);
+ }
+
+ #[test]
+ fn appending_anywhere_else_changes_nothing() {
+ let appended = GlyphBuffer::from_shaped(&plain_run());
+ let original = GlyphBuffer::from_shaped(&plain_run());
+ let mut buffer = original.clone();
+ let mut end = None;
+
+ assert!(!buffer.append_keeping_placements_derived(&mut end, &appended, 21.0, 3));
+ assert!(!buffer.append_keeping_placements_derived(&mut end, &appended, 20.75, 4));
+
+ let mut other_baseline = appended.clone();
+ other_baseline.shift_baseline(1.0);
+ assert!(!buffer.append_keeping_placements_derived(&mut end, &other_baseline, 20.75, 3));
+
+ let mut stored = appended.clone();
+ stored.update_positions(|_, _, _| {});
+ assert!(!buffer.append_keeping_placements_derived(&mut end, &stored, 20.75, 3));
+
+ assert!(buffer == original);
+ assert!(buffer.placements.is_none());
+
+ let mut far_along = buffer.clone();
+ far_along.truncate(0);
+ let mut huge = plain_run();
+ huge[0].glyph_width = MAX_EXACT_PEN_POSITION - 4.0;
+ huge[1].x = MAX_EXACT_PEN_POSITION - 4.0;
+ huge[2].x = MAX_EXACT_PEN_POSITION + 0.25;
+ huge.truncate(1);
+ let huge = GlyphBuffer::from_shaped(&huge);
+ assert!(huge.placements.is_none());
+ assert!(far_along.append_keeping_placements_derived(&mut end, &huge, 0.0, 0));
+ assert!(!far_along.append_keeping_placements_derived(&mut end, &appended, MAX_EXACT_PEN_POSITION - 4.0, 1));
+ }
+
+ #[test]
+ fn appending_after_the_run_shrank_adds_up_where_it_ends_now() {
+ let appended = GlyphBuffer::from_shaped(&plain_run());
+ let mut buffer = GlyphBuffer::from_shaped(&plain_run());
+ let mut end = None;
+ assert!(buffer.append_keeping_placements_derived(&mut end, &appended, 20.75, 3));
+ buffer.truncate(2);
+ assert!(buffer.append_keeping_placements_derived(&mut end, &appended, 12.75, 2));
+ assert_eq!(buffer.len(), 5);
+ }
+
#[test]
fn runs_compare_by_where_they_place_glyphs_however_they_store_it() {
let shaped = plain_run();
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
index 145e9fe314e..915f7ba1fe4 100644
--- a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
@@ -308,6 +308,7 @@ fn apply_block_ellipsis(
text_type: line_box_fragment::GLYPH_TEXT_TYPE_COMMON,
width: ellipsis_width,
is_upright: false,
+ derived_placements_end: None,
}),
line_box_fragment::FragmentBuildFacts {
style_source,
diff --git a/Libraries/LibWeb/Rust/src/layout/line_box_fragment.rs b/Libraries/LibWeb/Rust/src/layout/line_box_fragment.rs
index 87693037ca4..aec1e3f49db 100644
--- a/Libraries/LibWeb/Rust/src/layout/line_box_fragment.rs
+++ b/Libraries/LibWeb/Rust/src/layout/line_box_fragment.rs
@@ -25,6 +25,8 @@ pub(crate) struct GlyphData {
pub(crate) width: f32,
/// Whether the glyphs are set upright in vertical text, one em apart along the inline axis.
pub(crate) is_upright: bool,
+ /// Where merging more glyphs into the run last left it.
+ pub(crate) derived_placements_end: Option<libgfx_rust::text_layout::DerivedPlacementsEnd>,
}
/// The glyphs of a text item and the font they are set in, which a line merges into a fragment or makes one of.
@@ -46,6 +48,7 @@ impl ItemGlyphRun<'_> {
text_type: self.text_type,
width: self.width,
is_upright: self.is_upright,
+ derived_placements_end: None,
}
}
}
@@ -225,11 +228,19 @@ impl LineBoxFragmentData {
}
let insert_position = self.insert_position;
- let existing = &mut self
- .glyphs
- .as_mut()
- .expect("merged fragment must have a glyph run")
- .glyphs;
+ let glyph_data = self.glyphs.as_mut().expect("merged fragment must have a glyph run");
+ if run_direction == direction::LTR
+ && glyph_data.glyphs.append_keeping_placements_derived(
+ &mut glyph_data.derived_placements_end,
+ &run.glyphs,
+ inline_offset,
+ text_offset,
+ )
+ {
+ self.inline_length += run_inline_size;
+ return;
+ }
+ let existing = &mut glyph_data.glyphs;
existing.reserve(run.glyphs.len());
match run_direction {
direction::LTR => {