Crate unicode_bidi

Source
Expand description

This crate implements the Unicode Bidirectional Algorithm for display of mixed right-to-left and left-to-right text. It is written in safe Rust, compatible with the current stable release.

§Example

use unicode_bidi::BidiInfo;

// This example text is defined using `concat!` because some browsers
// and text editors have trouble displaying bidi strings.
let text = concat![
  "א",
  "ב",
  "ג",
  "a",
  "b",
  "c",
];

// Resolve embedding levels within the text.  Pass `None` to detect the
// paragraph level automatically.
let bidi_info = BidiInfo::new(&text, None);

// This paragraph has embedding level 1 because its first strong character is RTL.
assert_eq!(bidi_info.paragraphs.len(), 1);
let para = &bidi_info.paragraphs[0];
assert_eq!(para.level.number(), 1);
assert_eq!(para.level.is_rtl(), true);

// Re-ordering is done after wrapping each paragraph into a sequence of
// lines. For this example, I'll just use a single line that spans the
// entire paragraph.
let line = para.range.clone();

let display = bidi_info.reorder_line(para, line);
assert_eq!(display, concat![
  "a",
  "b",
  "c",
  "ג",
  "ב",
  "א",
]);

§Features

  • std: Enabled by default, but can be disabled to make unicode_bidi #![no_std] + alloc compatible.
  • hardcoded-data: Enabled by default. Includes hardcoded Unicode bidi data and more convenient APIs.
  • serde: Adds [serde::Serialize] and [serde::Deserialize] implementations to relevant types.

Re-exports§

pub use crate::data_source::BidiDataSource;
pub use crate::level::Level;
pub use crate::level::LTR_LEVEL;
pub use crate::level::RTL_LEVEL;

Modules§

data_source
deprecated
This module holds deprecated assets only.
format_chars
Directional Formatting Characters
level
Bidi Embedding Level
utf16

Structs§

BidiInfo
Bidi information of the text.
HardcodedBidiData
Hardcoded Bidi data that ships with the unicode-bidi crate.
InitialInfo
Initial bidi information of the text.
Paragraph
Contains a reference of BidiInfo and one of its paragraphs. And it supports all operation in the Paragraph that needs also its BidiInfo such as direction.
ParagraphBidiInfo
Bidi information of text treated as a single paragraph.
ParagraphInfo
Bidi information about a single paragraph
Utf8IndexLenIter
Iterator over (UTF-8) string slices returning (index, char_len) tuple.

Enums§

BidiClass
Represents values of the Unicode character property Bidi_Class, also known as the bidirectional character type.
Direction

Constants§

UNICODE_VERSION
The Unicode version of data

Traits§

TextSource
Trait that abstracts over a text source for use by the bidi algorithms. We implement this for str (UTF-8) and for u16 (UTF-16, native-endian). (For internal unicode-bidi use; API may be unstable.) This trait is sealed and cannot be implemented for types outside this crate.

Functions§

bidi_class
Find the BidiClass of a single char.
get_base_direction
Get the base direction of the text provided according to the Unicode Bidirectional Algorithm.
get_base_direction_full
Get the base direction of the text provided according to the Unicode Bidirectional Algorithm, considering the full text if the first paragraph is all-neutral.
get_base_direction_full_with_data_source
get_base_direction_with_data_source

Type Aliases§

LevelRun
A maximal substring of characters with the same embedding level.
LevelRunVec