Skip to main content

ClusterMap

Struct ClusterMap 

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

Bidirectional mapping between source byte offsets and visual cell columns.

Built from text (optionally with shaped glyph data) and provides O(log n) lookups in both directions via binary search over the sorted cluster array.

§Construction

  • from_text — build from plain text (uses grapheme cluster widths, suitable for terminal/monospace rendering).
  • from_shaped_run — build from a shaped glyph run (uses glyph cluster byte offsets and advances, suitable for proportional/shaped rendering).

Implementations§

Source§

impl ClusterMap

Source

pub fn from_text(text: &str) -> Self

Build a cluster map from plain text using grapheme cluster boundaries.

Each grapheme cluster maps to 1 or 2 cells based on display_width. This is the appropriate constructor for terminal/monospace rendering.

Source

pub fn from_shaped_run(text: &str, run: &ShapedRun) -> Self

Build a cluster map from a shaped glyph run.

Uses glyph cluster byte offsets from the ShapedRun to determine cluster boundaries, with advances determining cell widths.

For terminal rendering (NoopShaper), each glyph maps to one grapheme cluster. For proportional rendering, multiple glyphs may share a cluster (ligatures) or one glyph may span multiple characters.

Source

pub fn byte_to_cell(&self, byte_offset: usize) -> usize

Map a byte offset to its visual cell column.

If the byte offset falls mid-cluster, it snaps to the cluster’s start cell. Returns total_cells for offsets at or past the end.

Source

pub fn byte_to_entry(&self, byte_offset: usize) -> Option<&ClusterEntry>

Map a byte offset to the containing ClusterEntry.

Returns None for empty maps or offsets past the end.

Source

pub fn byte_range_to_cell_range( &self, byte_start: usize, byte_end: usize, ) -> (usize, usize)

Map a byte range to a visual cell range.

Returns (cell_start, cell_end) covering all clusters that overlap the given byte range.

Source

pub fn cell_to_byte(&self, cell_col: usize) -> usize

Map a visual cell column to a source byte offset.

Continuation cells (cells within a wide character) map back to the cluster’s start byte. Returns total_bytes for cells at or past the total width.

Source

pub fn cell_to_entry(&self, cell_col: usize) -> Option<&ClusterEntry>

Map a visual cell column to the containing ClusterEntry.

Returns None for empty maps or cells past the total width.

Source

pub fn cell_range_to_byte_range( &self, cell_start: usize, cell_end: usize, ) -> (usize, usize)

Map a visual cell range to a source byte range.

Returns (byte_start, byte_end) covering all clusters that overlap the given cell range. Continuation cells are resolved to their owning cluster.

Source

pub fn grapheme_to_cell(&self, grapheme_index: usize) -> usize

Map a grapheme index to a visual cell column.

Source

pub fn cell_to_grapheme(&self, cell_col: usize) -> usize

Map a visual cell column to a grapheme index.

Source

pub fn grapheme_to_byte(&self, grapheme_index: usize) -> usize

Map a grapheme index to a byte offset.

Source

pub fn byte_to_grapheme(&self, byte_offset: usize) -> usize

Map a byte offset to a grapheme index.

Source

pub fn total_cells(&self) -> usize

Total visual width in cells.

Source

pub fn total_bytes(&self) -> usize

Total byte length of the source text.

Source

pub fn cluster_count(&self) -> usize

Number of grapheme clusters.

Source

pub fn is_empty(&self) -> bool

Whether the map is empty.

Source

pub fn entries(&self) -> &[ClusterEntry]

Iterate over all cluster entries.

Source

pub fn get(&self, grapheme_index: usize) -> Option<&ClusterEntry>

Get the cluster entry at a grapheme index.

Source

pub fn extract_text_for_cells<'a>( &self, source: &'a str, cell_start: usize, cell_end: usize, ) -> &'a str

Extract text from the source string for a cell range.

Returns the substring covering all clusters that overlap the given visual cell range.

Trait Implementations§

Source§

impl Clone for ClusterMap

Source§

fn clone(&self) -> ClusterMap

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ClusterMap

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more