Skip to main content

SwashShaper

Struct SwashShaper 

Source
pub struct SwashShaper { /* private fields */ }
Expand description

Text shaper backed by swash.

Keep a single SwashShaper alive across multiple layout passes to amortise the cost of the internal LRU caches that swash maintains in ShapeContext.

Optionally attach a ShapeCache via Self::with_cache to skip swash entirely on repeated requests for the same (font, text, size) tuple.

Implementations§

Source§

impl SwashShaper

Source

pub fn shape_batch( &mut self, font_data: &[u8], segments: &[&str], px_size: f32, ) -> Vec<Result<ShapeResult, OxiTextError>>

Shape multiple independent text segments with the same font and size.

Returns one ShapeResult per input segment. More efficient than calling SwashShaper::shape_full N times for the same (font_data, px_size) pair because the ShapeContext is reused across all calls without reallocation.

§Errors

Each element of the returned Vec is Ok when the font can be parsed and Err when shaping fails for that segment.

Source

pub fn shape_batch_directed( &mut self, font_data: &[u8], segments: &[(&str, ShapeDirection)], px_size: f32, ) -> Vec<Result<ShapeResult, OxiTextError>>

Shape a batch with per-segment directions.

Each element of segments is a (text, direction) pair. The appropriate shaping direction is applied to each segment independently. Vertical directions (Ttb, Btt) automatically inject vert/vrt2 features, matching the behaviour of SwashShaper::shape_request.

Returns one ShapeResult per input pair.

Source

pub fn shape_batch_with_features( &mut self, font_data: &[u8], segments: &[&str], px_size: f32, features: &[ShapeFeature], ) -> Vec<Result<ShapeResult, OxiTextError>>

Shape a batch with a shared feature list applied to every segment.

All segments share the same (font_data, px_size, features) context.

Returns one ShapeResult per input segment.

Source§

impl SwashShaper

Source

pub fn shape_with_variations( &mut self, font_data: &[u8], text: &str, px_size: f32, variations: &[([u8; 4], f32)], ) -> Result<ShapeResult, OxiTextError>

Shape text with specific OpenType variation axis values.

variations is a list of (axis_tag, value) pairs, e.g. ([b'w', b'g', b'h', b't'], 700.0) for Bold weight.

§Note on swash 0.2.x API

swash’s ShapeContext does not expose a public variation-axis API in version 0.2.x — the font metrics are applied internally. This method provides the correct API surface for future enhancement when swash adds explicit variation support; for now it delegates to regular shaping and returns the result unchanged.

§Errors

Returns OxiTextError::Shaping if the font bytes cannot be parsed.

Source§

impl SwashShaper

Source

pub fn new() -> Self

Creates a new shaper with default cache settings and no shape cache.

Source

pub fn with_cache(capacity: usize) -> Self

Creates a new shaper with an attached ShapeCache of capacity entries.

Repeated calls to Self::shape with the same (font_data, text, size) tuple will be served from the cache after the first miss.

§Arguments
  • capacity: maximum number of ShapedRuns to keep in the cache. Passing 0 uses a minimum capacity of 1.
Source

pub fn shape_cache(&self) -> Option<&Arc<ShapeCache>>

Returns a reference to the attached shape cache, if any.

Source

pub fn shape( &mut self, text: &str, font_data: Arc<[u8]>, size: f32, ) -> Result<ShapedRun, OxiTextError>

Shapes text using the font in font_data at size pixels-per-em.

Returns a ShapedRun containing one ShapedGlyph per output glyph. The x_advance of each glyph is in pixels (already scaled by size).

When an attached ShapeCache is present the result is looked up before invoking swash. Cache keys incorporate font_data pointer identity, the exact text, and size.

§Errors

Returns OxiTextError::Shaping if the font bytes cannot be parsed by swash.

Source

pub fn shape_with_direction( &mut self, text: &str, font_data: Arc<[u8]>, size: f32, rtl: bool, ) -> Result<ShapedRun, OxiTextError>

Shapes text with explicit direction control.

When rtl is false this is identical to Self::shape.

When rtl is true the shaper signals Direction::RightToLeft to swash (enabling correct Arabic/Hebrew form selection via OpenType GSUB), then sorts the resulting glyphs by ascending cluster byte offset so the output is always in logical source order regardless of what swash emits. The caller (bidi engine) is responsible for visual reordering.

§Errors

Returns OxiTextError::Shaping if the font bytes cannot be parsed.

Source

pub fn shape_request( &mut self, req: &ShapeRequest<'_>, ) -> Result<Vec<ShapedGlyph>, OxiTextError>

Shapes text using all parameters in a ShapeRequest.

When direction is ShapeDirection::Ttb or ShapeDirection::Btt, the vert and vrt2 OpenType features are automatically appended to the feature list (if not already present) so that fonts with a vertical substitution table produce the correct glyph variants.

Script and language tags, if provided, are forwarded to swash’s ShaperBuilder for language-specific GSUB/GPOS rule selection.

§Errors

Returns OxiTextError::Shaping if the font bytes cannot be parsed.

Source

pub fn shape_with_features( &mut self, font_data: &[u8], text: &str, px_size: f32, rtl: bool, features: &[ShapeFeature], ) -> Result<Vec<ShapedGlyph>, OxiTextError>

Shapes text with an explicit list of OpenType feature overrides.

Unlike Self::shape_request this entry point does not inject vertical features automatically; callers are responsible for supplying the full feature list.

§Errors

Returns OxiTextError::Shaping if the font bytes cannot be parsed.

Source

pub fn shape_full( &mut self, font_data: &[u8], text: &str, px_size: f32, ) -> Result<ShapeResult, OxiTextError>

Shapes text and returns a rich ShapeResult with metadata.

The result includes the glyph list, the direction used, and any codepoints that could not be mapped (glyph ID 0 / .notdef).

§Errors

Returns OxiTextError::Shaping if the font bytes cannot be parsed.

Source

pub fn shape_slice( &mut self, font_data: &[u8], text: &str, px_size: f32, ) -> Result<Vec<ShapedGlyph>, OxiTextError>

Shapes text using raw font bytes supplied as &[u8] (LTR).

A convenience wrapper over Self::shape_with_features_internal for callers that already hold raw font bytes and do not need the Arc wrapping or cache infrastructure of Self::shape.

§Errors

Returns OxiTextError::Shaping if the font bytes cannot be parsed.

Source

pub fn shape_slice_rtl( &mut self, font_data: &[u8], text: &str, px_size: f32, ) -> Result<Vec<ShapedGlyph>, OxiTextError>

Shapes text using raw font bytes supplied as &[u8] (RTL).

Like Self::shape_slice but shapes in right-to-left direction and returns glyphs in ascending cluster (logical source) order.

§Errors

Returns OxiTextError::Shaping if the font bytes cannot be parsed.

Source

pub fn shape_with_fallback( &mut self, fonts: &[&[u8]], text: &str, px_size: f32, ) -> Result<Vec<ShapedGlyph>, OxiTextError>

Shapes text with a font fallback chain.

For each codepoint that produces glyph_id == 0 (.notdef), the corresponding text run is re-shaped with each successive fallback font in fonts[1..]. If a fallback produces a non-zero glyph ID the fallback glyphs replace the .notdef glyphs in the result; otherwise the .notdef glyphs are preserved (best-effort).

fonts[0] is the primary font; fonts[1..] are tried in order.

§Note on cluster offsets

When a sub-string is re-shaped with a fallback font, swash emits cluster byte offsets relative to that sub-string (starting at 0). This function adds the original start offset back before merging so all returned glyphs carry absolute offsets into text.

§Errors

Returns OxiTextError::Shaping if the primary font cannot be parsed.

Source

pub fn font_has_aat(font_data: &[u8]) -> bool

Returns true if the given font data contains AAT layout tables.

Checks for the presence of morx (extended glyph metamorphosis rules), kerx (extended kerning data), or ankr (anchor point) tables — the three primary tables that distinguish Apple Advanced Typography (AAT) fonts from pure OpenType fonts.

Swash’s ShapeContext already applies AAT tables transparently when present, so this function is informational only; it does not change the shaping path.

Source

pub fn shape_with_aat_fallback( &mut self, font_data: &[u8], text: &str, px_size: f32, ) -> Result<ShapeResult, OxiTextError>

Shape using AAT if the font has Morx/Kerx tables, otherwise fall back to standard OpenType shaping.

Swash handles both AAT and OpenType tables transparently via its ShapeContext; this method is informational. It delegates directly to Self::shape_with_features_internal regardless of table presence.

§Errors

Returns OxiTextError::Shaping if the font bytes cannot be parsed.

Trait Implementations§

Source§

impl Default for SwashShaper

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.