JFLib

text/segment.rs#

Source: lib/text/segment.rs

segment -- Unicode text segmentation: grapheme clusters and words (UAX #29), line break
opportunities (UAX #14), Unicode 15.0 (segment_data.rs, tools/gen_segment.py)

  grapheme_next(s, i) -> index     the end of the grapheme cluster that starts at or holds i
  grapheme_prev(s, i) -> index     the start of the cluster before i (0 at the start)
  cluster_end(s, i) -> index       the end of the cluster that starts at boundary i (walks
                                   over a text cluster by cluster)
  is_grapheme_boundary(s, i)
  graphemes(s) -> Vec<usize>       every cluster boundary, 0 and s.len() included
  pictographic_cluster(s, a, b)    the cluster a..b is an emoji sequence (Backspace deletes it
                                   whole; other clusters lose their last code point)
  word_bounds(s) -> Vec<usize>     every word boundary, 0 and s.len() included
  word_at(s, i) -> (start, end)    the word segment that holds i (at s.len(): the last one)
  word_kind(s, a, b) -> WK_*       WK_WORD (a letter or number in it), WK_SPACE (only white
                                   space), WK_PUNCT
  word_next(s, i) / word_prev(s, i)   Ctrl+Right / Ctrl+Left: the start of the next word
                                   after i (spaces skipped) / of the word before i
  line_breaks(s) -> Vec<(usize, bool)>   break opportunities (index where a line may start,
                                   mandatory), s.len() last (mandatory)
  wrap_end(s, pos, pe, fits) -> end   where a line from pos ends inside pos..pe: the last
                                   opportunity whose text (without trailing spaces) fits,
                                   else graphemes while they fit (at least one); a mandatory
                                   break inside ends it
  wrap_end_hyph(s, pos, pe, fits, hyph) -> (end, hyphen)   the same with hyphenation: a line
                                   may end after a soft hyphen (U+00AD) or, with a hook
                                   (hyph(word, &mut offsets): where a word may break, e.g.
                                   spell::Hyphenator::hyphenate), inside the word that does
                                   not fit; hyphen: draw a hyphen after the line.
                                   fits(a, b, hyphen): a..b (and a hyphen) fits
  char_props(c) -> u32             the packed properties (tests, other tables)

Indices are byte offsets into UTF-8 text; an index inside a character is moved to that
character's start.  Rules: UAX #29 GB3..GB13 and WB3..WB16 of Unicode 15.0 (no GB9c: that came
with 15.1); UAX #14 LB2..LB31 with the pair rules of LB25 (numbers), LB30 for East Asian
parentheses and LB30b for emoji modifiers.  Grapheme and word scans around i start at the
nearest place before i where no rule joins or looks across (usually right there; at the
latest the paragraph start, the last '\n' before i) and words end at the next such place,
so the cost does not grow with the length of a line.
/// word_kind: a word (a letter or number in it)
WK_WORD: u32 = 0
/// word_kind: white space only
WK_SPACE: u32 = 1
/// word_kind: anything else (punctuation, symbols)
WK_PUNCT: u32 = 2
  • fn char_props(c: char) -> u32 the packed properties of a character (see segment_data.rs)

  • fn grapheme_next(s: &str, i: usize) -> usize the end of the grapheme cluster that starts at or holds i (s.len() at the end)

  • fn cluster_end(s: &str, i: usize) -> usize the end of the grapheme cluster that starts at i, a cluster boundary (grapheme_next without looking back: walks from boundary to boundary)

  • fn grapheme_prev(s: &str, i: usize) -> usize the start of the grapheme cluster before i (0 at the start)

  • fn is_grapheme_boundary(s: &str, i: usize) -> bool i is a cluster boundary

  • fn graphemes(s: &str) -> Vec<usize> every cluster boundary of s (0 and s.len() included)

  • fn pictographic_cluster(s: &str, a: usize, b: usize) -> bool the cluster a..b is an emoji sequence (pictographs, flags, keycaps, ZWJ sequences)

  • fn word_bounds(s: &str) -> Vec<usize> every word boundary of s (0 and s.len() included)

  • fn word_kind(s: &str, a: usize, b: usize) -> u32 the kind of the segment a..b (WK_WORD, WK_SPACE, WK_PUNCT)

  • fn word_at(s: &str, i: usize) -> (usize, usize) the word segment that holds i (at the end of the text: the last one)

  • fn word_next(s: &str, i: usize) -> usize Ctrl+Right: the start of the next word after i (the rest of this word and the spaces after it skipped)

  • fn word_prev(s: &str, i: usize) -> usize Ctrl+Left: the start of the word before i (spaces before i skipped)

  • fn line_breaks(s: &str) -> Vec<(usize, bool)> the break opportunities of s: (index where a line may start, mandatory); the end of the text is the last entry (mandatory)

  • fn wrap_end(s: &str, pos: usize, pe: usize, fits: &mut dyn FnMut(usize, usize) -> bool) -> usize where a line starting at pos ends inside pos..pe (pos < end <= pe; pe when pos == pe): the whole rest when it fits, else the last break opportunity whose text before it (without trailing spaces) fits, else grapheme clusters while they fit (at least one); a mandatory break inside ends the line there. fits(a, b): the text a..b fits.

  • fn wrap_end_hyph( s: &str, pos: usize, pe: usize, fits: &mut dyn FnMut(usize, usize, bool) -> bool, mut hyph: Option<&mut dyn FnMut(&str, &mut Vec<usize>)>, ) -> (usize, bool) wrap_end with hyphenation: where a line starting at pos ends inside pos..pe, and whether it ends with a hyphen (to be drawn after its text). As wrap_end, and: an opportunity after a soft hyphen (U+00AD) counts with the hyphen's width; when the next word does not fit, hyph(word, &mut offsets) gives the places where that whole word (UAX #29) may break (byte offsets inside it, ascending) and the line ends at the last one that fits with a hyphen -- also inside a single word too long for a line, before graphemes are cut. Words with a digit or a soft hyphen of their own, or longer than about 256 characters, are never hyphenated. fits(a, b, hyphen): the text a..b fits (with a hyphen after it).

  • fn unicode_version() -> &'static str the Unicode version of the tables

Source: docs/API.md in the JFLib repository.