Truly independent web browser
LibGfx+LibWeb: Keep shaped glyphs in GlyphBuffer everywhere
Aliaksandr Kalenikf25ea3fec040
diff --git a/Libraries/LibGfx/Rust/src/path.rs b/Libraries/LibGfx/Rust/src/path.rsindex 172cc37ed19..b6743652871 100644--- a/Libraries/LibGfx/Rust/src/path.rs+++ b/Libraries/LibGfx/Rust/src/path.rs@@ -37,7 +37,7 @@ unsafe extern "C" {/// A shaped run of glyphs in one font, as the path operations below take one.pub struct GlyphRun {pub font: crate::font::FontHandle,- pub glyphs: Vec<crate::text_layout::DrawGlyph>,+ pub glyphs: crate::text_layout::GlyphBuffer,}struct GlyphOutlineSink<'a> {@@ -295,7 +295,7 @@ impl Path {continue;}let mut transforms = Vec::with_capacity(run.glyphs.len());- for glyph in &run.glyphs {+ for glyph in run.glyphs.iter() {let glyph_distance = offset + glyph.x;if glyph_distance + (glyph.glyph_width / 2.0) > path_length {break;diff --git a/Libraries/LibGfx/Rust/src/text_layout.rs b/Libraries/LibGfx/Rust/src/text_layout.rsindex cb38a849058..445a9bbf175 100644--- a/Libraries/LibGfx/Rust/src/text_layout.rs+++ b/Libraries/LibGfx/Rust/src/text_layout.rs@@ -173,6 +173,15 @@ pub enum GlyphBuffer {}impl GlyphBuffer {+ /// The same glyphs in the form that clones without copying them.+ #[must_use]+ pub fn into_shared(self) -> Self {+ match self {+ Self::Owned(glyphs) => Self::Shared(Arc::from(glyphs)),+ shared @ Self::Shared(_) => shared,+ }+ }+pub fn to_mut(&mut self) -> &mut Vec<DrawGlyph> {if let Self::Shared(shared) = self {*self = Self::Owned(shared.to_vec());@@ -223,7 +232,7 @@ pub struct ShapedText {impl ShapedText {#[inline]- pub fn glyphs(&self) -> &[DrawGlyph] {+ pub fn glyphs(&self) -> &GlyphBuffer {&self.glyphs}@@ -271,7 +280,7 @@ impl ShapeParams {#[derive(Debug)]struct CachedShape {- glyphs: Arc<[DrawGlyph]>,+ glyphs: GlyphBuffer,width: f32,trailing_whitespace_length_in_code_units: usize,trailing_whitespace_advance: f32,@@ -293,13 +302,13 @@ pub struct SharedShape(Arc<CachedShape>);impl SharedShape {pub fn from_glyphs(- glyphs: Vec<DrawGlyph>,+ glyphs: GlyphBuffer,width: f32,trailing_whitespace_length_in_code_units: usize,trailing_whitespace_advance: f32,) -> Self {Self(Arc::new(CachedShape {- glyphs: Arc::from(glyphs),+ glyphs: glyphs.into_shared(),width,trailing_whitespace_length_in_code_units,trailing_whitespace_advance,@@ -307,7 +316,7 @@ impl SharedShape {}#[inline]- pub fn glyphs(&self) -> &Arc<[DrawGlyph]> {+ pub fn glyphs(&self) -> &GlyphBuffer {&self.0.glyphs}@@ -478,13 +487,13 @@ fn shape_text_uncached(// glyph pointer stays valid for this synchronous callback.let shape = unsafe { &mut *sink.cast::<Option<CachedShape>>() };assert!(glyph_count == 0 || !glyphs.is_null());- let glyphs = if glyph_count == 0 {+ let glyphs = GlyphBuffer::Shared(if glyph_count == 0 {Arc::from([])} else {// SAFETY: The C++ side hands a pointer to glyph_count glyphs that// outlive the callback.Arc::from(unsafe { ak::slice_from_raw_parts(glyphs, glyph_count) })- };+ });*shape = Some(CachedShape {glyphs,width,@@ -514,7 +523,7 @@ fn shape_text_uncached(fn shaped_text_with_baseline_start(shape: &CachedShape, baseline_start_x: f32) -> ShapedText {let glyphs = if baseline_start_x == 0.0 {- GlyphBuffer::Shared(shape.glyphs.clone())+ shape.glyphs.clone()} else {GlyphBuffer::Owned(shape@@ -596,7 +605,7 @@ mod shaping_cache_tests {fn shape_with_glyph_count(glyph_count: usize) -> CachedShape {CachedShape {- glyphs: Arc::from(vec![DrawGlyph::default(); glyph_count]),+ glyphs: GlyphBuffer::Shared(Arc::from(vec![DrawGlyph::default(); glyph_count])),width: glyph_count as f32,trailing_whitespace_length_in_code_units: 0,trailing_whitespace_advance: 0.0,diff --git a/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/text.rs b/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/text.rsindex d03543e0a0f..083da04110d 100644--- a/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/text.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/text.rs@@ -9,7 +9,7 @@use libgfx_rust::font::{EmojiPresentation, FontCascadeListHandle, FontHandle};use libgfx_rust::path::{GlyphRun, Path};-use libgfx_rust::text_layout::{DrawGlyph, TextType, shape_text as shape_text_in_font};+use libgfx_rust::text_layout::{GlyphBuffer, TextType, shape_text as shape_text_in_font};use libgfx_rust::{AffineTransform, FloatPoint};use super::context::multiply_transforms;@@ -19,7 +19,7 @@ use crate::html::canvas_rendering_context2d::{CanvasTextAlign, CanvasTextBaselin/// A run of the text that one font of the cascade shapes.pub(crate) struct ShapedRun {pub(crate) font: FontHandle,- pub(crate) glyphs: Vec<DrawGlyph>,+ pub(crate) glyphs: GlyphBuffer,pub(crate) width: f32,}@@ -59,18 +59,19 @@ pub(crate) fn shape_text(let mut last_position = baseline_start;let mut add_run = |text: &[u16], font: &FontHandle| {let shaped = shape_text_in_font(font, text, TextType::Common, 0.0, letter_spacing, word_spacing);- let mut glyphs = shaped.glyphs().to_vec();+ let width = shaped.width();+ let mut glyphs = shaped.into_glyphs();if last_position.x != 0.0 || last_position.y != 0.0 {- for glyph in &mut glyphs {+ for glyph in glyphs.to_mut() {glyph.x += last_position.x;glyph.y += last_position.y;}}- last_position.x += shaped.width();+ last_position.x += width;runs.push(ShapedRun {font: font.clone(),glyphs,- width: shaped.width(),+ width,});};diff --git a/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs b/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rsindex b592d6405b8..ab117b6f6c2 100644--- a/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs+++ b/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs@@ -62,7 +62,7 @@ const _: () = assert!(size_of::<Item>() == 96);#[derive(Clone, Debug)]struct OwnedGlyphRun {- glyphs: Vec<libgfx_rust::text_layout::DrawGlyph>,+ glyphs: libgfx_rust::text_layout::GlyphBuffer,width: f32,is_upright: bool,}@@ -79,11 +79,8 @@ struct AtomicItemMetrics {fn shaped_glyphs_from(shape: &libgfx_rust::text_layout::SharedShape,baseline_start_x: f32,-) -> Vec<libgfx_rust::text_layout::DrawGlyph> {- match shape.with_baseline_start(baseline_start_x).into_glyphs() {- libgfx_rust::text_layout::GlyphBuffer::Owned(glyphs) => glyphs,- libgfx_rust::text_layout::GlyphBuffer::Shared(glyphs) => glyphs.to_vec(),- }+) -> libgfx_rust::text_layout::GlyphBuffer {+ shape.with_baseline_start(baseline_start_x).into_glyphs()}fn reshaped_glyph_run(@@ -101,7 +98,7 @@ fn reshaped_glyph_run(word_spacing,);OwnedGlyphRun {- glyphs: shape.glyphs().to_vec(),+ glyphs: shape.glyphs().clone(),width: shape.width(),is_upright: false,}@@ -153,8 +150,8 @@ fn shape_set_upright(shape: &libgfx_rust::text_layout::SharedShape,advance: f32,) -> libgfx_rust::text_layout::SharedShape {- let mut glyphs = shape.glyphs().to_vec();- let width = set_glyphs_upright(&mut glyphs, 0.0, advance);+ let mut glyphs = shape.glyphs().clone();+ let width = set_glyphs_upright(glyphs.to_mut(), 0.0, advance);libgfx_rust::text_layout::SharedShape::from_glyphs(glyphs,width,@@ -257,14 +254,14 @@ impl Item {}}- /// The glyph run of a text item as one it owns, copying the glyphs of its prepared item's shape if it has none.+ /// The glyph run of a text item as one it owns, sharing the glyphs of its prepared item's shape if it has none.fn take_owned_glyph_run(&mut self, prepared: &PreparedInlineContent) -> OwnedGlyphRun {if let Some(owned) = self.owned_glyph_run.take() {return *owned;}let prepared_item = self.prepared_item(prepared);OwnedGlyphRun {- glyphs: prepared_item.shape().glyphs().to_vec(),+ glyphs: prepared_item.shape().glyphs().clone(),width: prepared_item.shape().width(),is_upright: prepared_item.has(PreparedItem::UPRIGHT),}@@ -404,7 +401,8 @@ impl Item {cluster_start: glyph.cluster_start.saturating_sub(split_cluster_start),..*glyph})- .collect();+ .collect::<Vec<_>>()+ .into();Some(OwnedGlyphRun {glyphs,width: run.width - split_x,@@ -485,13 +483,13 @@ impl Item {let mut prefix = self.take_owned_glyph_run(prepared);let split_x = prefix.glyphs[split_glyph_index].x;let split_cluster_start = prefix.glyphs[split_glyph_index].cluster_start;- let mut remainder_glyphs = prefix.glyphs.split_off(split_glyph_index);+ let mut remainder_glyphs = prefix.glyphs.to_mut().split_off(split_glyph_index);for glyph in &mut remainder_glyphs {glyph.x -= split_x;glyph.cluster_start = glyph.cluster_start.saturating_sub(split_cluster_start);}let remainder = OwnedGlyphRun {- glyphs: remainder_glyphs,+ glyphs: remainder_glyphs.into(),width: prefix.width - split_x,is_upright: prefix.is_upright,};@@ -1594,7 +1592,7 @@ impl<'context> RunItemResolver<'_, 'context> {if inline_offset != 0.0 || upright_advance_after_tabs.is_some() {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);+ width = set_glyphs_upright(glyphs.to_mut(), inline_offset, advance);}owned_glyph_run = Some(Box::new(OwnedGlyphRun {glyphs,diff --git a/Libraries/LibWeb/Rust/src/layout/line_box_fragment.rs b/Libraries/LibWeb/Rust/src/layout/line_box_fragment.rsindex 29518c4b9b9..d31388bd7b7 100644--- a/Libraries/LibWeb/Rust/src/layout/line_box_fragment.rs+++ b/Libraries/LibWeb/Rust/src/layout/line_box_fragment.rs@@ -29,7 +29,7 @@ pub(crate) struct GlyphData {/// 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) glyphs: std::borrow::Cow<'run, libgfx_rust::text_layout::GlyphBuffer>,pub(crate) font: &'run libgfx_rust::font::FontHandle,pub(crate) text_type: u8,pub(crate) width: f32,@@ -37,10 +37,11 @@ pub(crate) struct ItemGlyphRun<'run> {}impl ItemGlyphRun<'_> {- /// The glyph run of a fragment made of these glyphs alone. It owns its glyphs, which merging more into it changes.+ /// The glyph run of a fragment made of these glyphs alone, which shares them with the shape they came from until+ /// merging more into it changes them.pub(crate) fn into_glyph_data(self) -> GlyphData {GlyphData {- glyphs: self.glyphs.into_owned().into(),+ glyphs: self.glyphs.into_owned(),font: self.font.clone(),text_type: self.text_type,width: self.width,diff --git a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rsindex 1ef7718f93c..5ab8025b865 100644--- a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs@@ -1468,16 +1468,15 @@ impl<'pass> SvgFormattingContext<'pass> {cell_height,);shaped_text.advance += shaped.width();- let mut glyph_buffer = shaped.into_glyphs();- let glyphs = glyph_buffer.to_mut();+ let mut glyphs = shaped.into_glyphs();if baseline_start.y != 0.0 {- for glyph in glyphs.iter_mut() {+ for glyph in glyphs.to_mut().iter_mut() {glyph.y += baseline_start.y;}}shaped_text.runs.push(libgfx_rust::path::GlyphRun {font: font.clone(),- glyphs: std::mem::take(glyphs),+ glyphs,});});shaped_text