From ba71d52624230a9f945b73144c511b3bf2182385 Mon Sep 17 00:00:00 2001 From: Justin Walsh Date: Sun, 11 Oct 2026 09:10:09 -0400 Subject: [PATCH 1/2] refactor(shaper): deduplicate engine helpers --- .../rust/shaper/src/engine/cluster_state.rs | 21 ++----------------- .../shaper/src/engine/flow_composition.rs | 12 +++-------- .../rust/shaper/src/engine/identity_index.rs | 11 ++++++++++ packages/glyph/rust/shaper/src/engine/mod.rs | 11 ++++++++++ .../rust/shaper/src/engine/positioning.rs | 21 ++----------------- .../rust/shaper/src/engine/shaping_state.rs | 18 ++-------------- 6 files changed, 31 insertions(+), 63 deletions(-) diff --git a/packages/glyph/rust/shaper/src/engine/cluster_state.rs b/packages/glyph/rust/shaper/src/engine/cluster_state.rs index ef943b5fb..aa251ca12 100644 --- a/packages/glyph/rust/shaper/src/engine/cluster_state.rs +++ b/packages/glyph/rust/shaper/src/engine/cluster_state.rs @@ -9,8 +9,9 @@ use crate::{FontGlyphExtents, FontMetrics, unicode::UnicodeAnalysis}; use super::{ EngineError, FrameFault, frame::{WRAP_CHARACTER, WRAP_NONE, WRAP_WORD}, - identity_index::{IdentityIndex, IdentityIndexError}, + identity_index::{IdentityIndex, identity_index_error}, line_composition::Correction, + reserve_vec as reserve, run_local::{NumericBlockSpan, RunLocalArena}, shaping_state::{ShapeArena, ShapingRun}, style_state::{ResolvedStyle, StyleArena, StyleSegment}, @@ -1975,15 +1976,6 @@ fn is_hard_break(text: &[u16], start: u32) -> Result { )) } -fn reserve(values: &mut Vec, capacity: usize) -> Result<(), EngineError> { - if values.capacity() < capacity { - values - .try_reserve_exact(capacity.saturating_sub(values.len())) - .map_err(|_| EngineError::ResultTooLarge)?; - } - Ok(()) -} - /// True exactly when the build scatter's destination equals every glyph's own /// index: the shaped runs tile the glyph array in array order and the source /// cluster ids never decrease across it, so per-cluster ordinals assign @@ -2398,15 +2390,6 @@ fn glyph_slice(values: &[T], range: core::ops::Range) -> Result<&[T], values.get(range).ok_or(EngineError::InvalidRequest) } -fn identity_index_error(error: IdentityIndexError) -> EngineError { - match error { - IdentityIndexError::AllocationFailed | IdentityIndexError::ArithmeticOverflow => { - EngineError::ResultTooLarge - } - IdentityIndexError::DuplicateIdentity => EngineError::InvalidRequest, - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/packages/glyph/rust/shaper/src/engine/flow_composition.rs b/packages/glyph/rust/shaper/src/engine/flow_composition.rs index 83f75c51b..2659b445b 100644 --- a/packages/glyph/rust/shaper/src/engine/flow_composition.rs +++ b/packages/glyph/rust/shaper/src/engine/flow_composition.rs @@ -921,7 +921,7 @@ impl FlowLayoutArena { self.clear(); return Ok(false); } - let first_fragments = line_fragments(previous, line_index)?; + let first_fragments = previous.line_fragments(line_index)?; let Some(first_fragment) = first_fragments.first() else { self.clear(); return Ok(false); @@ -1281,7 +1281,8 @@ impl FlowLayoutArena { .iter() .rposition(|line| line.flow_thread_id == constraint.flow_thread_id); let cursor_start = if let Some(line_index) = previous_thread_line { - line_fragments(previous, line_index)? + previous + .line_fragments(line_index)? .last() .and_then(|fragment| usize::try_from(fragment.line.cluster_end).ok()) .ok_or(EngineError::InvalidRequest)? @@ -2005,13 +2006,6 @@ fn shrink_for_flow_thread( .ok_or(EngineError::InvalidRequest) } -fn line_fragments( - flow: &FlowLayoutArena, - line_index: usize, -) -> Result, EngineError> { - flow.line_fragments(line_index) -} - fn same_flow_line_records( left_line: FlowLine, left_fragments: &RopeRange<'_, FlowFragment>, diff --git a/packages/glyph/rust/shaper/src/engine/identity_index.rs b/packages/glyph/rust/shaper/src/engine/identity_index.rs index 542fa37f6..bb9728311 100644 --- a/packages/glyph/rust/shaper/src/engine/identity_index.rs +++ b/packages/glyph/rust/shaper/src/engine/identity_index.rs @@ -2,6 +2,8 @@ use alloc::vec::Vec; +use super::EngineError; + #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(crate) enum IdentityIndexError { AllocationFailed, @@ -9,6 +11,15 @@ pub(crate) enum IdentityIndexError { DuplicateIdentity, } +pub(super) fn identity_index_error(error: IdentityIndexError) -> EngineError { + match error { + IdentityIndexError::AllocationFailed | IdentityIndexError::ArithmeticOverflow => { + EngineError::ResultTooLarge + } + IdentityIndexError::DuplicateIdentity => EngineError::InvalidRequest, + } +} + #[derive(Default)] pub(crate) struct IdentityIndex { keys: Vec, diff --git a/packages/glyph/rust/shaper/src/engine/mod.rs b/packages/glyph/rust/shaper/src/engine/mod.rs index 77c626b05..1d60b0803 100644 --- a/packages/glyph/rust/shaper/src/engine/mod.rs +++ b/packages/glyph/rust/shaper/src/engine/mod.rs @@ -3,6 +3,8 @@ //! The public types in this module are available to native consumers. Wasm memory ownership and //! pointer validation stay in the target-gated transport module. +use alloc::vec::Vec; + mod cluster_state; #[cfg_attr(not(test), allow(dead_code))] mod flow_composition; @@ -60,3 +62,12 @@ mod work_attribution; pub(crate) use positioning::SemanticGlyph; pub use state::{EngineError, FrameFault, TextEngine}; + +fn reserve_vec(values: &mut Vec, capacity: usize) -> Result<(), EngineError> { + if values.capacity() < capacity { + values + .try_reserve_exact(capacity.saturating_sub(values.len())) + .map_err(|_| EngineError::ResultTooLarge)?; + } + Ok(()) +} diff --git a/packages/glyph/rust/shaper/src/engine/positioning.rs b/packages/glyph/rust/shaper/src/engine/positioning.rs index 5aa596550..c49bf6443 100644 --- a/packages/glyph/rust/shaper/src/engine/positioning.rs +++ b/packages/glyph/rust/shaper/src/engine/positioning.rs @@ -18,9 +18,10 @@ use super::{ codec_gather::{LayoutGlyph, PAINT_LAYER_GLYPH}, flow_composition::{FlowDropCap, FlowFragment, FlowLayoutArena, FlowLine}, frame::{ALIGN_CENTER, ALIGN_END, ALIGN_JUSTIFY, ALIGN_START}, - identity_index::{IdentityIndex, IdentityIndexError}, + identity_index::{IdentityIndex, identity_index_error}, line_composition::ComposedLine, placement_slot_arena::PlacementHandle, + reserve_vec as reserve, retained_rope::{RopeCursor, RopeCursorRange}, run_local::{ ClusterFinish, RunLocalArena, RunLocalBuildError, RunLocalGlyph, RunLocalGlyphInput, @@ -4541,24 +4542,6 @@ fn nonnegative_f32(value: f64) -> Result { .ok_or(EngineError::InvalidRequest) } -fn identity_index_error(error: IdentityIndexError) -> EngineError { - match error { - IdentityIndexError::AllocationFailed | IdentityIndexError::ArithmeticOverflow => { - EngineError::ResultTooLarge - } - IdentityIndexError::DuplicateIdentity => EngineError::InvalidRequest, - } -} - -fn reserve(values: &mut Vec, capacity: usize) -> Result<(), EngineError> { - if values.capacity() < capacity { - values - .try_reserve_exact(capacity.saturating_sub(values.len())) - .map_err(|_| EngineError::ResultTooLarge)?; - } - Ok(()) -} - #[cfg(test)] mod tests { use super::super::{ diff --git a/packages/glyph/rust/shaper/src/engine/shaping_state.rs b/packages/glyph/rust/shaper/src/engine/shaping_state.rs index 7109132de..b4723f760 100644 --- a/packages/glyph/rust/shaper/src/engine/shaping_state.rs +++ b/packages/glyph/rust/shaper/src/engine/shaping_state.rs @@ -6,7 +6,7 @@ use crate::{ }; use super::{ - EngineError, + EngineError, reserve_vec, style_state::{ResolvedStyle, StyleArena, StyleSegment}, }; @@ -92,12 +92,7 @@ pub(crate) struct BoundaryShapeArena { impl ShapingRunArena { pub(crate) fn reserve(&mut self, capacity: usize) -> Result<(), EngineError> { - if self.runs.capacity() < capacity { - self.runs - .try_reserve_exact(capacity.saturating_sub(self.runs.len())) - .map_err(|_| EngineError::ResultTooLarge)?; - } - Ok(()) + reserve_vec(&mut self.runs, capacity) } pub(crate) fn build( @@ -580,15 +575,6 @@ impl BoundaryShapeArena { } } -fn reserve_vec(values: &mut Vec, capacity: usize) -> Result<(), EngineError> { - if values.capacity() < capacity { - values - .try_reserve_exact(capacity.saturating_sub(values.len())) - .map_err(|_| EngineError::ResultTooLarge)?; - } - Ok(()) -} - fn direction(style: ResolvedStyle, level: u8) -> u8 { if style.bidi_override { u8::from(style.direction == 2) From eedd6957e8bcdfde924613c4112fe4840beeac55 Mon Sep 17 00:00:00 2001 From: Justin Walsh Date: Sun, 11 Oct 2026 09:14:55 -0400 Subject: [PATCH 2/2] docs(glyph): attest engine helper cleanup --- .../docs/attestations/2026-10-11-glyph-975230cb.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 .agents/docs/attestations/2026-10-11-glyph-975230cb.md diff --git a/.agents/docs/attestations/2026-10-11-glyph-975230cb.md b/.agents/docs/attestations/2026-10-11-glyph-975230cb.md new file mode 100644 index 000000000..a59d39c38 --- /dev/null +++ b/.agents/docs/attestations/2026-10-11-glyph-975230cb.md @@ -0,0 +1,13 @@ +--- +type: Attestation +title: '@pmndrs/glyph at 975230cb' +description: 'Contributor intent for @pmndrs/glyph, awaiting verification.' +package: '@pmndrs/glyph' +concept: 'packages/glyph.md' +source: 'sha256:975230cbce6f6c097c308539c4bae1de27700a8b80da0adfb1089a06a49463e9' +generated: + by: process:docs-attest + at: '2026-10-11T13:14:42Z' +--- + +Deduplicated three engine helpers; 92 focused tests, formatting, Clippy, and locked SIMD Wasm build passed. Wave 2 source reduced by 32 lines; package-size comparison is recorded separately.