Skip to content

Truly independent web browser

LibWeb: Merge text items into line box fragments from borrowed glyphs

Aliaksandr Kalenik973e3fb137d7
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs b/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs
index 8d3e7d6eec6..af4628e7eff 100644
--- a/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs
+++ b/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs
@@ -206,16 +206,19 @@ impl Item {
return None;
}
let prepared_item = self.prepared_item(prepared);
+ let font = prepared.font_of(prepared_item);
let text_type = prepared_item.text_type();
Some(match &self.owned_glyph_run {
Some(owned) => line_box_fragment::ItemGlyphRun {
glyphs: std::borrow::Cow::Borrowed(&owned.glyphs),
+ font,
text_type,
width: owned.width,
is_upright: owned.is_upright,
},
None => line_box_fragment::ItemGlyphRun {
glyphs: std::borrow::Cow::Borrowed(prepared_item.shape().glyphs()),
+ font,
text_type,
width: prepared_item.shape().width(),
is_upright: prepared_item.has(PreparedItem::UPRIGHT),
@@ -223,21 +226,25 @@ impl Item {
})
}
- /// The glyph run of a text item, which the line it goes on takes from it.
- pub(crate) fn take_glyph_data(&mut self, prepared: &PreparedInlineContent) -> line_box_fragment::GlyphData {
+ /// The glyph run of a text item, which the line it goes on takes from it: the item's own glyphs, moved out of it,
+ /// or the shape of its prepared item, borrowed.
+ pub(crate) fn take_glyph_run<'prepared>(
+ &mut self,
+ prepared: &'prepared PreparedInlineContent,
+ ) -> line_box_fragment::ItemGlyphRun<'prepared> {
let prepared_item = self.prepared_item(prepared);
- let font = prepared.font_of(prepared_item).clone();
+ let font = prepared.font_of(prepared_item);
let text_type = prepared_item.text_type();
match self.owned_glyph_run.take() {
- Some(owned) => line_box_fragment::GlyphData {
- glyphs: owned.glyphs.into(),
+ Some(owned) => line_box_fragment::ItemGlyphRun {
+ glyphs: std::borrow::Cow::Owned(owned.glyphs),
font,
text_type,
width: owned.width,
is_upright: owned.is_upright,
},
- None => line_box_fragment::GlyphData {
- glyphs: libgfx_rust::text_layout::GlyphBuffer::Shared(prepared_item.shape().glyphs().clone()),
+ None => line_box_fragment::ItemGlyphRun {
+ glyphs: std::borrow::Cow::Borrowed(prepared_item.shape().glyphs()),
font,
text_type,
width: prepared_item.shape().width(),
diff --git a/Libraries/LibWeb/Rust/src/layout/line_box.rs b/Libraries/LibWeb/Rust/src/layout/line_box.rs
index bf4971e523b..4e1c85a3625 100644
--- a/Libraries/LibWeb/Rust/src/layout/line_box.rs
+++ b/Libraries/LibWeb/Rust/src/layout/line_box.rs
@@ -134,7 +134,7 @@ impl LineBoxData {
content_block_size: CssPixels,
border_box_block_start: CssPixels,
border_box_block_end: CssPixels,
- glyphs: Option<line_box_fragment::GlyphData>,
+ glyphs: Option<line_box_fragment::ItemGlyphRun<'_>>,
facts: line_box_fragment::FragmentBuildFacts,
text_align_is_justify: bool,
trailing_whitespace: line_box_fragment::TrailingWhitespace,
@@ -145,7 +145,7 @@ impl LineBoxData {
!last.has_soft_wrap_opportunity_after
&& last.layout_node == layout_node
&& last.glyphs.as_ref().is_some_and(|last_glyphs| {
- last_glyphs.font == glyphs.font && last_glyphs.is_upright == glyphs.is_upright
+ last_glyphs.font == *glyphs.font && last_glyphs.is_upright == glyphs.is_upright
})
&& last.start + last.length_in_code_units == start
})
@@ -153,7 +153,7 @@ impl LineBoxData {
if can_merge {
let last = self.fragments.last_mut().unwrap();
last.length_in_code_units += length;
- last.append_glyph_run(glyphs.unwrap(), content_inline_size, start - last.start);
+ last.append_glyph_run(&glyphs.unwrap(), content_inline_size, start - last.start);
if trailing_whitespace.length_in_code_units == length {
last.trailing_whitespace.length_in_code_units += trailing_whitespace.length_in_code_units;
last.trailing_whitespace.inline_size += trailing_whitespace.inline_size;
@@ -173,7 +173,7 @@ impl LineBoxData {
border_box_block_start,
self.direction,
self.writing_mode,
- glyphs,
+ glyphs.map(line_box_fragment::ItemGlyphRun::into_glyph_data),
facts,
);
fragment.trailing_whitespace = trailing_whitespace;
diff --git a/Libraries/LibWeb/Rust/src/layout/line_box_fragment.rs b/Libraries/LibWeb/Rust/src/layout/line_box_fragment.rs
index f44146fe2bb..29518c4b9b9 100644
--- a/Libraries/LibWeb/Rust/src/layout/line_box_fragment.rs
+++ b/Libraries/LibWeb/Rust/src/layout/line_box_fragment.rs
@@ -27,14 +27,28 @@ pub(crate) struct GlyphData {
pub(crate) is_upright: bool,
}
-/// The glyphs of a text item.
+/// The glyphs of a text item and the font they are set in, which a line merges into a fragment or makes one of.
pub(crate) struct ItemGlyphRun<'run> {
pub(crate) glyphs: std::borrow::Cow<'run, [libgfx_rust::text_layout::DrawGlyph]>,
+ pub(crate) font: &'run libgfx_rust::font::FontHandle,
pub(crate) text_type: u8,
pub(crate) width: f32,
pub(crate) is_upright: bool,
}
+impl ItemGlyphRun<'_> {
+ /// The glyph run of a fragment made of these glyphs alone. It owns its glyphs, which merging more into it changes.
+ pub(crate) fn into_glyph_data(self) -> GlyphData {
+ GlyphData {
+ glyphs: self.glyphs.into_owned().into(),
+ font: self.font.clone(),
+ text_type: self.text_type,
+ width: self.width,
+ is_upright: self.is_upright,
+ }
+ }
+}
+
// The advance of a run's trailing whitespace, recorded at shaping time so that trimming it subtracts exactly what
// shaping added.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
@@ -189,7 +203,7 @@ impl LineBoxFragmentData {
}
}
- pub(crate) fn append_glyph_run(&mut self, run: GlyphData, run_inline_size: CssPixels, text_offset: usize) {
+ pub(crate) fn append_glyph_run(&mut self, run: &ItemGlyphRun<'_>, run_inline_size: CssPixels, text_offset: usize) {
let text_offset = u32::try_from(text_offset).expect("text offset fits a shaping cluster index");
match self.direction {
direction::LTR => self.append_glyph_run_ltr(run, run_inline_size, text_offset),
@@ -198,7 +212,7 @@ impl LineBoxFragmentData {
}
}
- fn append_glyph_run_ltr(&mut self, run: GlyphData, run_inline_size: CssPixels, text_offset: u32) {
+ fn append_glyph_run_ltr(&mut self, run: &ItemGlyphRun<'_>, run_inline_size: CssPixels, text_offset: u32) {
let run_direction = self.resolve_glyph_run_direction(run.text_type);
let inline_offset = self.inline_length.to_double() as f32;
@@ -242,7 +256,7 @@ impl LineBoxFragmentData {
self.inline_length += run_inline_size;
}
- fn append_glyph_run_rtl(&mut self, run: GlyphData, run_inline_size: CssPixels, text_offset: u32) {
+ fn append_glyph_run_rtl(&mut self, run: &ItemGlyphRun<'_>, run_inline_size: CssPixels, text_offset: u32) {
let run_direction = self.resolve_glyph_run_direction(run.text_type);
let run_offset = run_inline_size.to_double() as f32;
diff --git a/Libraries/LibWeb/Rust/src/layout/line_builder.rs b/Libraries/LibWeb/Rust/src/layout/line_builder.rs
index 49de08a6a5a..1bff91ea90a 100644
--- a/Libraries/LibWeb/Rust/src/layout/line_builder.rs
+++ b/Libraries/LibWeb/Rust/src/layout/line_builder.rs
@@ -489,8 +489,8 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {
prepared: &prepared_inline_content::PreparedInlineContent,
style: StyleValues,
) {
- let glyphs = item.take_glyph_data(prepared);
- let content_block_size = TextLayoutBounds::new(style, Some(&glyphs.font)).block_size();
+ let glyphs = item.take_glyph_run(prepared);
+ let content_block_size = TextLayoutBounds::new(style, Some(glyphs.font)).block_size();
self.append_text_chunk(
item.node,
item.offset_in_node,
@@ -518,7 +518,7 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {
trailing_margin: CssPixels,
content_inline_size: CssPixels,
content_block_size: CssPixels,
- glyphs: line_box_fragment::GlyphData,
+ glyphs: line_box_fragment::ItemGlyphRun<'_>,
trailing_whitespace: line_box_fragment::TrailingWhitespace,
) {
self.prepare_to_append_inline_content();