Skip to content

Truly independent web browser

LibGfx+LibWeb: Read and write glyph positions through GlyphBuffer

Aliaksandr Kalenik65256f71c0de
diff --git a/Libraries/LibCompositing/Rust/src/display_list/commands.rs b/Libraries/LibCompositing/Rust/src/display_list/commands.rs
index 68033b420b5..b68ebd72681 100644
--- a/Libraries/LibCompositing/Rust/src/display_list/commands.rs
+++ b/Libraries/LibCompositing/Rust/src/display_list/commands.rs
@@ -567,11 +567,14 @@ pub struct DisplayListGlyph {
}
ffi_bytes_fields!(DisplayListGlyph { position, glyph_id });
-impl From<&text_layout::DrawGlyph> for DisplayListGlyph {
- fn from(glyph: &text_layout::DrawGlyph) -> Self {
+impl From<text_layout::PlacedGlyph> for DisplayListGlyph {
+ fn from(placed: text_layout::PlacedGlyph) -> Self {
Self {
- position: FloatPoint { x: glyph.x, y: glyph.y },
- glyph_id: glyph.glyph_id,
+ position: FloatPoint {
+ x: placed.x,
+ y: placed.y,
+ },
+ glyph_id: placed.glyph.glyph_id,
}
}
}
diff --git a/Libraries/LibGfx/Rust/src/path.rs b/Libraries/LibGfx/Rust/src/path.rs
index b6743652871..45385c3a085 100644
--- a/Libraries/LibGfx/Rust/src/path.rs
+++ b/Libraries/LibGfx/Rust/src/path.rs
@@ -270,7 +270,7 @@ impl Path {
let ascent = run.font.facts().ascent;
let transforms: Vec<_> = run
.glyphs
- .iter()
+ .placed()
.map(|glyph| AffineTransform {
values: [1.0, 0.0, 0.0, 1.0, glyph.x, glyph.y + ascent],
})
@@ -295,9 +295,9 @@ impl Path {
continue;
}
let mut transforms = Vec::with_capacity(run.glyphs.len());
- for glyph in run.glyphs.iter() {
+ for glyph in run.glyphs.placed() {
let glyph_distance = offset + glyph.x;
- if glyph_distance + (glyph.glyph_width / 2.0) > path_length {
+ if glyph_distance + (glyph.glyph.advance / 2.0) > path_length {
break;
}
let (point, tangent) = contour.position_and_tangent(glyph_distance);
diff --git a/Libraries/LibGfx/Rust/src/text_layout.rs b/Libraries/LibGfx/Rust/src/text_layout.rs
index 445a9bbf175..66f6f6f17c6 100644
--- a/Libraries/LibGfx/Rust/src/text_layout.rs
+++ b/Libraries/LibGfx/Rust/src/text_layout.rs
@@ -37,22 +37,50 @@ impl TryFrom<u8> for TextType {
#[derive(Clone, Copy, Debug, Default, PartialEq)]
#[repr(C)]
pub struct DrawGlyph {
- pub x: f32,
- pub y: f32,
- pub length_in_code_units: usize,
- pub glyph_width: f32,
+ x: f32,
+ y: f32,
+ length_in_code_units: usize,
+ pub advance: f32,
pub glyph_id: u32,
- pub cluster_start: u32,
- pub should_paint: bool,
- pub is_rtl: bool,
+ cluster_start: u32,
+ should_paint: bool,
+ is_rtl: bool,
}
impl DrawGlyph {
+ #[must_use]
+ pub fn new(advance: f32, glyph_id: u32, length_in_code_units: usize, should_paint: bool) -> Self {
+ Self {
+ x: 0.0,
+ y: 0.0,
+ length_in_code_units,
+ advance,
+ glyph_id,
+ cluster_start: 0,
+ should_paint,
+ is_rtl: false,
+ }
+ }
+
+ /// How many code units of the text the glyph stands for: none for a glyph that continues the cluster of the glyph
+ /// before it.
+ #[inline]
+ #[must_use]
+ pub fn length_in_code_units(&self) -> usize {
+ self.length_in_code_units
+ }
+
+ #[inline]
+ #[must_use]
+ pub fn should_paint(&self) -> bool {
+ self.should_paint
+ }
+
fn is_bitwise_equal_to(&self, other: &DrawGlyph) -> bool {
self.x.to_bits() == other.x.to_bits()
&& self.y.to_bits() == other.y.to_bits()
&& self.length_in_code_units == other.length_in_code_units
- && self.glyph_width.to_bits() == other.glyph_width.to_bits()
+ && self.advance.to_bits() == other.advance.to_bits()
&& self.glyph_id == other.glyph_id
&& self.cluster_start == other.cluster_start
&& self.should_paint == other.should_paint
@@ -166,6 +194,44 @@ pub fn glyph_run_glyph_intercepts(
intercepts
}
+/// A glyph of a run, with where the run places it and what it stands for in the text.
+#[derive(Clone, Copy, Debug, PartialEq)]
+pub struct PlacedGlyph {
+ pub glyph: DrawGlyph,
+ pub x: f32,
+ pub y: f32,
+ pub cluster_start: u32,
+ pub is_rtl: bool,
+}
+
+/// The glyphs of a run in order, each with its placement.
+pub struct PlacedGlyphs<'a> {
+ glyphs: std::slice::Iter<'a, DrawGlyph>,
+}
+
+impl Iterator for PlacedGlyphs<'_> {
+ type Item = PlacedGlyph;
+
+ #[inline]
+ fn next(&mut self) -> Option<PlacedGlyph> {
+ let glyph = *self.glyphs.next()?;
+ Some(PlacedGlyph {
+ glyph,
+ x: glyph.x,
+ y: glyph.y,
+ cluster_start: glyph.cluster_start,
+ is_rtl: glyph.is_rtl,
+ })
+ }
+
+ #[inline]
+ fn size_hint(&self) -> (usize, Option<usize>) {
+ self.glyphs.size_hint()
+ }
+}
+
+impl ExactSizeIterator for PlacedGlyphs<'_> {}
+
#[derive(Clone, Debug)]
pub enum GlyphBuffer {
Shared(Arc<[DrawGlyph]>),
@@ -182,7 +248,7 @@ impl GlyphBuffer {
}
}
- pub fn to_mut(&mut self) -> &mut Vec<DrawGlyph> {
+ fn glyphs_mut(&mut self) -> &mut Vec<DrawGlyph> {
if let Self::Shared(shared) = self {
*self = Self::Owned(shared.to_vec());
}
@@ -191,6 +257,69 @@ impl GlyphBuffer {
Self::Shared(_) => unreachable!(),
}
}
+
+ #[inline]
+ #[must_use]
+ pub fn placed(&self) -> PlacedGlyphs<'_> {
+ PlacedGlyphs { glyphs: self.iter() }
+ }
+
+ pub fn reserve(&mut self, additional_glyphs: usize) {
+ self.glyphs_mut().reserve(additional_glyphs);
+ }
+
+ /// Has `update` move each glyph, given its x and y.
+ pub fn update_positions(&mut self, mut update: impl FnMut(&DrawGlyph, &mut f32, &mut f32)) {
+ for glyph in self.glyphs_mut() {
+ let record = *glyph;
+ update(&record, &mut glyph.x, &mut glyph.y);
+ }
+ }
+
+ /// Moves all glyphs along the block axis.
+ pub fn shift_baseline(&mut self, offset: f32) {
+ self.update_positions(|_, _, y| *y += offset);
+ }
+
+ pub fn push_placed(&mut self, placed: PlacedGlyph) {
+ self.glyphs_mut().push(DrawGlyph {
+ x: placed.x,
+ y: placed.y,
+ cluster_start: placed.cluster_start,
+ is_rtl: placed.is_rtl,
+ ..placed.glyph
+ });
+ }
+
+ pub fn truncate(&mut self, glyph_count: usize) {
+ self.glyphs_mut().truncate(glyph_count);
+ }
+
+ pub fn pop(&mut self) {
+ self.glyphs_mut().pop();
+ }
+
+ /// Drops all glyphs outside of `range`, leaving the placement of the others as it is.
+ pub fn keep_range(&mut self, range: std::ops::Range<usize>) {
+ let glyphs = self.glyphs_mut();
+ glyphs.drain(range.end..);
+ glyphs.drain(..range.start);
+ }
+
+ /// 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 {
+ let first = self[index];
+ self[index..]
+ .iter()
+ .map(|glyph| DrawGlyph {
+ x: glyph.x - first.x,
+ cluster_start: glyph.cluster_start.saturating_sub(first.cluster_start),
+ ..*glyph
+ })
+ .collect::<Vec<_>>()
+ .into()
+ }
}
impl Default for GlyphBuffer {
@@ -522,20 +651,10 @@ 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 {
- shape.glyphs.clone()
- } else {
- GlyphBuffer::Owned(
- shape
- .glyphs
- .iter()
- .map(|glyph| DrawGlyph {
- x: glyph.x + baseline_start_x,
- ..*glyph
- })
- .collect(),
- )
- };
+ let mut glyphs = shape.glyphs.clone();
+ if baseline_start_x != 0.0 {
+ glyphs.update_positions(|_, x, _| *x += baseline_start_x);
+ }
ShapedText {
glyphs,
width: shape.width,
diff --git a/Libraries/LibGfx/TextLayout.cpp b/Libraries/LibGfx/TextLayout.cpp
index 2882c23dfd7..a8c2fe9fa4b 100644
--- a/Libraries/LibGfx/TextLayout.cpp
+++ b/Libraries/LibGfx/TextLayout.cpp
@@ -568,7 +568,7 @@ static_assert(sizeof(Gfx::FloatPoint) == 2 * sizeof(float));
static_assert(offsetof(Gfx::FFI::DrawGlyph, x) == offsetof(Gfx::DrawGlyph, position));
static_assert(offsetof(Gfx::FFI::DrawGlyph, y) == offsetof(Gfx::DrawGlyph, position) + sizeof(float));
static_assert(offsetof(Gfx::FFI::DrawGlyph, length_in_code_units) == offsetof(Gfx::DrawGlyph, length_in_code_units));
-static_assert(offsetof(Gfx::FFI::DrawGlyph, glyph_width) == offsetof(Gfx::DrawGlyph, glyph_width));
+static_assert(offsetof(Gfx::FFI::DrawGlyph, advance) == offsetof(Gfx::DrawGlyph, glyph_width));
static_assert(offsetof(Gfx::FFI::DrawGlyph, glyph_id) == offsetof(Gfx::DrawGlyph, glyph_id));
static_assert(offsetof(Gfx::FFI::DrawGlyph, should_paint) == offsetof(Gfx::DrawGlyph, should_paint));
diff --git a/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/text.rs b/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/text.rs
index 083da04110d..109d3461872 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/text.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/text.rs
@@ -62,10 +62,10 @@ pub(crate) fn shape_text(
let width = shaped.width();
let mut glyphs = shaped.into_glyphs();
if last_position.x != 0.0 || last_position.y != 0.0 {
- for glyph in glyphs.to_mut() {
- glyph.x += last_position.x;
- glyph.y += last_position.y;
- }
+ glyphs.update_positions(|_, x, y| {
+ *x += last_position.x;
+ *y += last_position.y;
+ });
}
last_position.x += width;
runs.push(ShapedRun {
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/canvas_text.rs b/Libraries/LibWeb/Bindings/Rust/src/html/canvas_text.rs
index 848ba698136..74967a99328 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/html/canvas_text.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/canvas_text.rs
@@ -118,12 +118,9 @@ impl<K: CanvasKind> RenderingContext2D<K> {
}
let glyphs: Vec<DisplayListGlyph> = run
.glyphs
- .iter()
- .filter(|glyph| glyph.should_paint)
- .map(|glyph| DisplayListGlyph {
- position: FloatPoint { x: glyph.x, y: glyph.y },
- glyph_id: glyph.glyph_id,
- })
+ .placed()
+ .filter(|placed| placed.glyph.should_paint())
+ .map(DisplayListGlyph::from)
.collect();
let first_font = font.cascade.first();
let state = self.drawing_state();
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_content.rs b/Libraries/LibWeb/Rust/src/layout/inline_content.rs
index d779cab01b5..c236aacd270 100644
--- a/Libraries/LibWeb/Rust/src/layout/inline_content.rs
+++ b/Libraries/LibWeb/Rust/src/layout/inline_content.rs
@@ -492,11 +492,11 @@ impl InlineContent {
let ascent = fragment.baseline.to_double() as f32;
let line_width = rect.width.to_double() as f32;
let mut glyphs = run.glyphs.clone();
- for glyph in glyphs.to_mut().iter_mut() {
- let inline_position = glyph.x;
- glyph.x = (line_width - glyph.glyph_width) / 2.0;
- glyph.y += inline_position;
- }
+ glyphs.update_positions(|glyph, x, y| {
+ let inline_position = *x;
+ *x = (line_width - glyph.advance) / 2.0;
+ *y += inline_position;
+ });
let bounding_box = libgfx_rust::FloatRect::from_array(
libgfx_rust::text_layout::glyph_run_bounding_box(&run.font, &glyphs, 1.0),
);
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
index dbd24e3b794..145e9fe314e 100644
--- a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
@@ -23,44 +23,43 @@ fn truncate_line_at_glyph_boundary(
if index == 0 {
return Some((index, fragment.inline_offset));
}
- fragment.glyphs.as_mut().unwrap().glyphs.to_mut().pop();
+ fragment.glyphs.as_mut().unwrap().glyphs.pop();
fragment.has_text_overflow_ellipsis = false;
}
let available = available_inline_size - fragment.inline_offset;
let Some(glyphs) = fragment.glyphs.as_mut().filter(|_| available > CssPixels::default()) else {
return (index > 0).then_some((index, fragment.inline_offset));
};
- let glyph_end =
- |glyph: &libgfx_rust::text_layout::DrawGlyph| CssPixels::nearest_value_for_f32(glyph.x + glyph.glyph_width);
+ let placed: Vec<_> = glyphs.glyphs.placed().collect();
+ let glyph_end = |placed: &libgfx_rust::text_layout::PlacedGlyph| {
+ CssPixels::nearest_value_for_f32(placed.x + placed.glyph.advance)
+ };
let keep_from_end = fragment.current_insert_direction == direction::RTL && line_direction == direction::RTL;
let (mut start, mut end) = if keep_from_end {
(
- glyphs.glyphs.partition_point(|glyph| {
- fragment.record.inline_length - CssPixels::nearest_value_for_f32(glyph.x) > available
+ placed.partition_point(|placed| {
+ fragment.record.inline_length - CssPixels::nearest_value_for_f32(placed.x) > available
}),
- glyphs.glyphs.len(),
+ placed.len(),
)
} else {
- (0, glyphs.glyphs.partition_point(|glyph| glyph_end(glyph) <= available))
+ (0, placed.partition_point(|placed| glyph_end(placed) <= available))
};
- while end > start && end < glyphs.glyphs.len() && glyphs.glyphs[end].length_in_code_units == 0 {
+ while end > start && end < placed.len() && placed[end].glyph.length_in_code_units() == 0 {
end -= 1;
}
- while start < end && glyphs.glyphs[start].length_in_code_units == 0 {
+ while start < end && placed[start].glyph.length_in_code_units() == 0 {
start += 1;
}
if start == end {
return (index > 0).then_some((index, fragment.inline_offset));
}
- let origin = CssPixels::nearest_value_for_f32(glyphs.glyphs[start].x);
- let inline_size = glyph_end(&glyphs.glyphs[end - 1]) - origin;
- let kept_glyphs = glyphs.glyphs.to_mut();
- kept_glyphs.drain(end..);
- kept_glyphs.drain(..start);
- for glyph in kept_glyphs.iter_mut() {
- glyph.x -= origin.to_double() as f32;
- }
- let length = glyphs.glyphs.iter().map(|glyph| glyph.length_in_code_units).sum();
+ let origin = CssPixels::nearest_value_for_f32(placed[start].x);
+ let inline_size = glyph_end(&placed[end - 1]) - origin;
+ glyphs.glyphs.keep_range(start..end);
+ let origin_x = origin.to_double() as f32;
+ glyphs.glyphs.update_positions(|_, x, _| *x -= origin_x);
+ let length = glyphs.glyphs.iter().map(|glyph| glyph.length_in_code_units()).sum();
glyphs.width = inline_size.to_double() as f32;
if fragment.current_insert_direction == direction::RTL && !keep_from_end {
fragment.start += fragment.length_in_code_units - length;
@@ -99,27 +98,28 @@ fn apply_text_overflow_to_line(line: &mut line_box::LineBoxData) {
let mut keep_count = 0usize;
let mut last_kept_end = 0.0f32;
let mut glyph_block_offset = 0.0f32;
- for glyph in glyphs.iter() {
- let glyph_end = glyph.x + glyph.glyph_width;
+ for placed in glyphs.placed() {
+ let glyph_end = placed.x + placed.glyph.advance;
if glyph_end > max_text_inline_size && (keep_count > 0 || line_has_visible_content) {
break;
}
keep_count += 1;
last_kept_end = glyph_end;
- glyph_block_offset = glyph.y;
+ glyph_block_offset = placed.y;
}
let glyph_data = line.fragments[index].glyphs.as_mut().unwrap();
- let kept_glyphs = glyph_data.glyphs.to_mut();
- kept_glyphs.truncate(keep_count);
- kept_glyphs.push(libgfx_rust::text_layout::DrawGlyph {
+ glyph_data.glyphs.truncate(keep_count);
+ glyph_data.glyphs.push_placed(libgfx_rust::text_layout::PlacedGlyph {
+ glyph: libgfx_rust::text_layout::DrawGlyph::new(
+ ellipsis_inline_size,
+ font.glyph_id_for_code_point(ELLIPSIS_CODE_POINT),
+ 1,
+ true,
+ ),
x: last_kept_end,
y: glyph_block_offset,
- length_in_code_units: 1,
- glyph_width: ellipsis_inline_size,
- glyph_id: font.glyph_id_for_code_point(ELLIPSIS_CODE_POINT),
cluster_start: 0,
- should_paint: true,
is_rtl: false,
});
let ellipsis_fragment_inline_length = line.fragments[index].inline_length;
@@ -198,10 +198,15 @@ fn apply_block_ellipsis(
if last_retained_fragment.length_in_code_units > length || last_retained_fragment.has_text_overflow_ellipsis {
let mut retained_code_units = 0usize;
if let Some(glyphs) = &mut last_retained_fragment.glyphs {
- glyphs.glyphs.to_mut().retain(|glyph| {
- retained_code_units += glyph.length_in_code_units;
- retained_code_units <= length
- });
+ let retained_glyph_count = glyphs
+ .glyphs
+ .iter()
+ .take_while(|glyph| {
+ retained_code_units += glyph.length_in_code_units();
+ retained_code_units <= length
+ })
+ .count();
+ glyphs.glyphs.truncate(retained_glyph_count);
glyphs.width = inline_size.to_double() as f32;
}
last_retained_fragment.length_in_code_units = length;
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs b/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs
index ab117b6f6c2..6a5372ce7ee 100644
--- a/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs
+++ b/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs
@@ -131,18 +131,18 @@ pub(crate) fn text_is_set_upright(writing_mode: u8, text_orientation: u8, code_p
/// which runs downwards in the frame of a vertical writing mode, while keeping its own horizontal width, which painting
/// centers it within. Invisible characters such as soft hyphens take no space.
// FIXME: Use the vertical metrics of the font, where it has them.
-fn set_glyphs_upright(glyphs: &mut [libgfx_rust::text_layout::DrawGlyph], inline_offset: f32, advance: f32) -> f32 {
+fn set_glyphs_upright(glyphs: &mut libgfx_rust::text_layout::GlyphBuffer, inline_offset: f32, advance: f32) -> f32 {
let mut pen = inline_offset;
let mut count = 0usize;
- for glyph in glyphs.iter_mut() {
- if glyph.length_in_code_units != 0 && glyph.should_paint {
+ glyphs.update_positions(|glyph, x, _| {
+ if glyph.length_in_code_units() != 0 && glyph.should_paint() {
if count > 0 {
pen += advance;
}
count += 1;
}
- glyph.x = pen;
- }
+ *x = pen;
+ });
advance * count as f32
}
@@ -151,7 +151,7 @@ fn shape_set_upright(
advance: f32,
) -> libgfx_rust::text_layout::SharedShape {
let mut glyphs = shape.glyphs().clone();
- let width = set_glyphs_upright(glyphs.to_mut(), 0.0, advance);
+ let width = set_glyphs_upright(&mut glyphs, 0.0, advance);
libgfx_rust::text_layout::SharedShape::from_glyphs(
glyphs,
width,
@@ -382,29 +382,24 @@ impl Item {
fn shaped_glyphs_after(&self, prepared: &PreparedInlineContent, code_units: usize) -> Option<OwnedGlyphRun> {
let run = self.glyph_run(prepared)?;
if run.text_type == line_box_fragment::GLYPH_TEXT_TYPE_RTL
- || run.glyphs.iter().map(|glyph| glyph.length_in_code_units).sum::<usize>() != self.length_in_node
+ || run
+ .glyphs
+ .iter()
+ .map(|glyph| glyph.length_in_code_units())
+ .sum::<usize>()
+ != self.length_in_node
{
return None;
}
let mut consumed = 0;
let split_glyph_index = run.glyphs.iter().position(|glyph| {
- let at_split = consumed == code_units && glyph.length_in_code_units != 0;
- consumed += glyph.length_in_code_units;
+ let at_split = consumed == code_units && glyph.length_in_code_units() != 0;
+ consumed += glyph.length_in_code_units();
at_split
})?;
- let split_x = run.glyphs[split_glyph_index].x;
- let split_cluster_start = run.glyphs[split_glyph_index].cluster_start;
- let glyphs = run.glyphs[split_glyph_index..]
- .iter()
- .map(|glyph| libgfx_rust::text_layout::DrawGlyph {
- x: glyph.x - split_x,
- cluster_start: glyph.cluster_start.saturating_sub(split_cluster_start),
- ..*glyph
- })
- .collect::<Vec<_>>()
- .into();
+ let split_x = run.glyphs.placed().nth(split_glyph_index)?.x;
Some(OwnedGlyphRun {
- glyphs,
+ glyphs: run.glyphs.remainder_from(split_glyph_index),
width: run.width - split_x,
is_upright: run.is_upright,
})
@@ -453,21 +448,21 @@ impl Item {
if run.text_type == line_box_fragment::GLYPH_TEXT_TYPE_RTL || run.glyphs.len() < 2 {
return None;
}
- let shaped_length_in_code_units: usize = run.glyphs.iter().map(|glyph| glyph.length_in_code_units).sum();
+ let shaped_length_in_code_units: usize = run.glyphs.iter().map(|glyph| glyph.length_in_code_units()).sum();
if shaped_length_in_code_units != self.length_in_node {
return None;
}
let leading_edge_inline_size = self.margin_start + self.border_start + self.padding_start;
let mut split: Option<(usize, usize)> = None;
- let mut code_units = run.glyphs[0].length_in_code_units;
- for (glyph_index, glyph) in run.glyphs.iter().enumerate().skip(1) {
- if glyph.length_in_code_units == 0 {
+ let mut code_units = run.glyphs[0].length_in_code_units();
+ for (glyph_index, placed) in run.glyphs.placed().enumerate().skip(1) {
+ if placed.glyph.length_in_code_units() == 0 {
continue;
}
let offset = self.offset_in_node + code_units;
if segmenter.next_boundary(offset, true) == Some(offset) {
- let prefix_inline_size = CssPixels::nearest_value_for_f32(glyph.x);
+ let prefix_inline_size = CssPixels::nearest_value_for_f32(placed.x);
if leading_edge_inline_size + prefix_inline_size > max_border_box_inline_size {
if split.is_none() && take_first_grapheme_when_none_fits {
split = Some((glyph_index, code_units));
@@ -476,20 +471,16 @@ impl Item {
}
split = Some((glyph_index, code_units));
}
- code_units += glyph.length_in_code_units;
+ code_units += placed.glyph.length_in_code_units();
}
let (split_glyph_index, split_code_units) = split?;
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.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 split_x = prefix.glyphs.placed().nth(split_glyph_index)?.x;
+ let remainder_glyphs = prefix.glyphs.remainder_from(split_glyph_index);
+ prefix.glyphs.truncate(split_glyph_index);
let remainder = OwnedGlyphRun {
- glyphs: remainder_glyphs.into(),
+ glyphs: remainder_glyphs,
width: prefix.width - split_x,
is_upright: prefix.is_upright,
};
@@ -574,9 +565,9 @@ impl Item {
if run.text_type != line_box_fragment::GLYPH_TEXT_TYPE_RTL
&& let Some(first_glyph) = run.glyphs.first()
{
- let mut code_units = first_glyph.length_in_code_units;
- for glyph in run.glyphs.iter().skip(1) {
- if glyph.length_in_code_units == 0 {
+ let mut code_units = first_glyph.length_in_code_units();
+ for placed in run.glyphs.placed().skip(1) {
+ if placed.glyph.length_in_code_units() == 0 {
continue;
}
let offset = start + code_units;
@@ -584,9 +575,9 @@ impl Item {
break;
}
if segmenter.next_boundary(offset, true) == Some(offset) {
- return Some(CssPixels::nearest_value_for_f32(glyph.x));
+ return Some(CssPixels::nearest_value_for_f32(placed.x));
}
- code_units += glyph.length_in_code_units;
+ code_units += placed.glyph.length_in_code_units();
}
}
let boundary = segmenter
@@ -1592,7 +1583,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(glyphs.to_mut(), inline_offset, advance);
+ width = set_glyphs_upright(&mut glyphs, 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.rs
index d31388bd7b7..87693037ca4 100644
--- a/Libraries/LibWeb/Rust/src/layout/line_box_fragment.rs
+++ b/Libraries/LibWeb/Rust/src/layout/line_box_fragment.rs
@@ -224,32 +224,36 @@ impl LineBoxFragmentData {
self.current_insert_direction = run_direction;
}
- let existing = self
+ let insert_position = self.insert_position;
+ let existing = &mut self
.glyphs
.as_mut()
.expect("merged fragment must have a glyph run")
- .glyphs
- .to_mut();
+ .glyphs;
existing.reserve(run.glyphs.len());
match run_direction {
direction::LTR => {
- for mut glyph in run.glyphs.iter().copied() {
- glyph.x += inline_offset;
- glyph.cluster_start += text_offset;
- existing.push(glyph);
+ for placed in run.glyphs.placed() {
+ existing.push_placed(libgfx_rust::text_layout::PlacedGlyph {
+ x: placed.x + inline_offset,
+ cluster_start: placed.cluster_start + text_offset,
+ ..placed
+ });
}
}
direction::RTL => {
let run_offset = run_inline_size.to_double() as f32;
- for glyph in existing.iter_mut() {
- if glyph.x >= self.insert_position {
- glyph.x += run_offset;
+ existing.update_positions(|_, x, _| {
+ if *x >= insert_position {
+ *x += run_offset;
}
- }
- for mut glyph in run.glyphs.iter().copied() {
- glyph.x += self.insert_position;
- glyph.cluster_start += text_offset;
- existing.push(glyph);
+ });
+ for placed in run.glyphs.placed() {
+ existing.push_placed(libgfx_rust::text_layout::PlacedGlyph {
+ x: placed.x + insert_position,
+ cluster_start: placed.cluster_start + text_offset,
+ ..placed
+ });
}
}
_ => unreachable!(),
@@ -268,35 +272,37 @@ impl LineBoxFragmentData {
self.current_insert_direction = run_direction;
}
- let existing = self
+ let insert_position = self.insert_position;
+ let existing = &mut self
.glyphs
.as_mut()
.expect("merged fragment must have a glyph run")
- .glyphs
- .to_mut();
+ .glyphs;
existing.reserve(run.glyphs.len());
match run_direction {
direction::LTR => {
- for glyph in existing.iter_mut() {
- if glyph.x >= self.insert_position {
- glyph.x += run_offset;
+ existing.update_positions(|_, x, _| {
+ if *x >= insert_position {
+ *x += run_offset;
}
- }
- for mut glyph in run.glyphs.iter().copied() {
- glyph.x += self.insert_position;
- glyph.cluster_start += text_offset;
- existing.push(glyph);
+ });
+ for placed in run.glyphs.placed() {
+ existing.push_placed(libgfx_rust::text_layout::PlacedGlyph {
+ x: placed.x + insert_position,
+ cluster_start: placed.cluster_start + text_offset,
+ ..placed
+ });
}
}
direction::RTL => {
if run.text_type != GLYPH_TEXT_TYPE_END_PADDING {
- for glyph in existing.iter_mut() {
- glyph.x += run_offset;
- }
+ existing.update_positions(|_, x, _| *x += run_offset);
}
- for mut glyph in run.glyphs.iter().copied() {
- glyph.cluster_start += text_offset;
- existing.push(glyph);
+ for placed in run.glyphs.placed() {
+ existing.push_placed(libgfx_rust::text_layout::PlacedGlyph {
+ cluster_start: placed.cluster_start + text_offset,
+ ..placed
+ });
}
}
_ => unreachable!(),
diff --git a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
index 5ab8025b865..520245dd4ce 100644
--- a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
@@ -61,26 +61,27 @@ struct ShapedSvgText {
/// this layout shapes.
fn append_svg_text_character_cells(
cells: &mut Vec<FfiSvgTextCharacterCell>,
- glyphs: &[libgfx_rust::text_layout::DrawGlyph],
+ glyphs: &libgfx_rust::text_layout::GlyphBuffer,
length_in_code_units: usize,
run_end_x: f32,
top: f32,
height: f32,
) {
+ let placed: Vec<_> = glyphs.placed().collect();
let mut covered = 0usize;
let mut index = 0usize;
- while index < glyphs.len() && covered < length_in_code_units {
- let first = glyphs[index];
+ while index < placed.len() && covered < length_in_code_units {
+ let first = placed[index];
// A glyph that maps to no code unit of its own (a combining mark's, e.g.) belongs to the
// typographic character before it, so it extends that character's cell. One at the start
// of the run has no character before it, so it joins the character after it instead.
let mut next = index + 1;
- let mut code_units = first.length_in_code_units;
- while next < glyphs.len() && (glyphs[next].length_in_code_units == 0 || code_units == 0) {
- code_units += glyphs[next].length_in_code_units;
+ let mut code_units = first.glyph.length_in_code_units();
+ while next < placed.len() && (placed[next].glyph.length_in_code_units() == 0 || code_units == 0) {
+ code_units += placed[next].glyph.length_in_code_units();
next += 1;
}
- let end_x = glyphs.get(next).map_or(run_end_x, |glyph| glyph.x);
+ let end_x = placed.get(next).map_or(run_end_x, |glyph| glyph.x);
let code_units = code_units.max(1).min(length_in_code_units - covered);
let cell = FfiSvgTextCharacterCell {
x: first.x,
@@ -1470,9 +1471,7 @@ impl<'pass> SvgFormattingContext<'pass> {
shaped_text.advance += shaped.width();
let mut glyphs = shaped.into_glyphs();
if baseline_start.y != 0.0 {
- for glyph in glyphs.to_mut().iter_mut() {
- glyph.y += baseline_start.y;
- }
+ glyphs.shift_baseline(baseline_start.y);
}
shaped_text.runs.push(libgfx_rust::path::GlyphRun {
font: font.clone(),
diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/background.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/background.rs
index e0ad500af43..dfc08edfbf3 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/paint/background.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/background.rs
@@ -1104,7 +1104,7 @@ fn append_text_clip_paths<O: Observer>(recorder: &mut PaintRecorder<'_, O>, pain
font_id: crate::painting::display_list::commands::FontResourceId(font_id),
glyphs: run
.glyphs
- .iter()
+ .placed()
.map(crate::painting::display_list::commands::DisplayListGlyph::from),
},
libgfx_rust::Color::from_rgb(0, 0, 0),
diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/inspector_overlay.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/inspector_overlay.rs
index 137db897c61..2703229a0f7 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/paint/inspector_overlay.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/inspector_overlay.rs
@@ -530,7 +530,7 @@ fn draw_label<O: Observer>(recorder: &mut PaintRecorder<'_, O>, label: &OverlayL
GlyphRunForRecording {
font_smoothing: crate::css::css_enums::font_smoothing::AUTO,
font_id: FontResourceId(label.font_id),
- glyphs: label.glyphs.iter().map(DisplayListGlyph::from),
+ glyphs: label.glyphs.placed().map(DisplayListGlyph::from),
},
color,
rect,
diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/text.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/text.rs
index 3f6975096b5..48ac09b199a 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/paint/text.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/text.rs
@@ -17,7 +17,6 @@ use crate::painting::record::PaintRecorder;
use crate::painting::record::inputs::CaretTarget;
use crate::painting::selection::{HighlightPseudoElement, SelectionRange};
use crate::painting::text_fragment::{self, SelectionOffsets};
-use libgfx_rust::text_layout::DrawGlyph;
use libgfx_rust::{Color, FloatPoint, IntPoint, IntRect, Orientation};
use std::sync::Arc;
@@ -542,7 +541,7 @@ fn paint_text_shadow<O: Observer>(
} = glyph_run_emission(fragment, run, fragment_absolute_rect, fragment_device_rect, scale);
// If this is a partial span, slice the glyph run to only include the relevant glyphs.
- let mut span_glyphs: &[DrawGlyph] = &run.glyphs;
+ let mut span_glyphs = 0..run.glyphs.len();
if span.start_code_unit != 0 || span.end_code_unit != fragment.length_in_code_units {
let mut start_glyph = 0usize;
let mut glyph_count = 0usize;
@@ -551,14 +550,14 @@ fn paint_text_shadow<O: Observer>(
if code_unit_offset == span.start_code_unit {
start_glyph = i;
}
- code_unit_offset += glyph.length_in_code_units;
+ code_unit_offset += glyph.length_in_code_units();
if code_unit_offset == span.end_code_unit {
glyph_count = i - start_glyph + 1;
break;
}
}
if glyph_count > 0 {
- span_glyphs = &run.glyphs[start_glyph..start_glyph + glyph_count];
+ span_glyphs = start_glyph..start_glyph + glyph_count;
}
}
@@ -610,7 +609,12 @@ fn paint_text_shadow<O: Observer>(
.inherited_text()
.font_smoothing,
font_id: FontResourceId(font_id),
- glyphs: span_glyphs.iter().map(DisplayListGlyph::from),
+ glyphs: run
+ .glyphs
+ .placed()
+ .skip(span_glyphs.start)
+ .take(span_glyphs.len())
+ .map(DisplayListGlyph::from),
},
scale,
orientation,
@@ -680,7 +684,7 @@ fn paint_text_fragment<O: Observer>(
.inherited_text()
.font_smoothing,
font_id: FontResourceId(font_id),
- glyphs: run.glyphs.iter().map(DisplayListGlyph::from),
+ glyphs: run.glyphs.placed().map(DisplayListGlyph::from),
};
// Paint text, clipped to span range if not full fragment.
diff --git a/Libraries/LibWeb/Rust/src/painting/text_fragment.rs b/Libraries/LibWeb/Rust/src/painting/text_fragment.rs
index 8508389c53c..3b3af435452 100644
--- a/Libraries/LibWeb/Rust/src/painting/text_fragment.rs
+++ b/Libraries/LibWeb/Rust/src/painting/text_fragment.rs
@@ -247,16 +247,16 @@ fn for_each_cluster_in_glyph_run(
) {
let mut cursor = 0usize;
for glyph in glyphs {
- if glyph.glyph_width == 0.0 {
+ if glyph.advance == 0.0 {
continue;
}
let cluster_start = cursor;
- let cluster_end = (cursor + glyph.length_in_code_units).min(fragment_length_in_code_units);
+ 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.glyph_width) {
+ if !callback(cluster_start, cluster_end, glyph.advance) {
return;
}
}
@@ -432,20 +432,20 @@ pub(crate) fn physical_edge_offset(
left: bool,
) -> Option<(usize, bool)> {
let run = fragment.glyph_run.as_ref()?;
- let glyph = run
+ let placed = run
.glyphs
- .iter()
- .filter(|glyph| glyph.length_in_code_units > 0 && glyph.glyph_width > 0.0)
+ .placed()
+ .filter(|placed| placed.glyph.length_in_code_units() > 0 && placed.glyph.advance > 0.0)
.min_by(|a, b| {
if left {
a.x.total_cmp(&b.x)
} else {
- (b.x + b.glyph_width).total_cmp(&(a.x + a.glyph_width))
+ (b.x + b.glyph.advance).total_cmp(&(a.x + a.glyph.advance))
}
})?;
- let boundary = glyph.cluster_start as usize
- + if left == glyph.is_rtl {
- glyph.length_in_code_units
+ let boundary = placed.cluster_start as usize
+ + if left == placed.is_rtl {
+ placed.glyph.length_in_code_units()
} else {
0
};
@@ -455,21 +455,26 @@ pub(crate) fn physical_edge_offset(
fragment.start + boundary,
crate::layout::RenderedTextBoundary::End,
),
- left == glyph.is_rtl,
+ left == placed.is_rtl,
))
}
pub(crate) fn caret_inline_offset(fragment: &FragmentRecord, offset: usize, downstream: bool) -> Option<f32> {
let run = fragment.glyph_run.as_ref()?;
let mut fallback = None;
- for glyph in run.glyphs.iter().filter(|glyph| glyph.length_in_code_units > 0) {
- let start = glyph.cluster_start as usize;
- let end = start + glyph.length_in_code_units;
+ for placed in run
+ .glyphs
+ .placed()
+ .filter(|placed| placed.glyph.length_in_code_units() > 0)
+ {
+ let start = placed.cluster_start as usize;
+ let length = placed.glyph.length_in_code_units();
+ let end = start + length;
if offset < start || offset > end {
continue;
}
- let fraction = (offset - start) as f32 / glyph.length_in_code_units as f32;
- let coordinate = glyph.x + glyph.glyph_width * if glyph.is_rtl { 1.0 - fraction } else { fraction };
+ let fraction = (offset - start) as f32 / length as f32;
+ let coordinate = placed.x + placed.glyph.advance * if placed.is_rtl { 1.0 - fraction } else { fraction };
if (offset > start && offset < end) || (downstream && offset == start) || (!downstream && offset == end) {
return Some(coordinate);
}