#bidi #text-layout #rtl #unicode #unicode-text #text

no-std unicode-bidi

Implementation of the Unicode Bidirectional Algorithm

31 releases

0.3.18 Dec 16, 2024
0.3.17 Oct 2, 2024
0.3.15 Jan 17, 2024
0.3.14 Dec 6, 2023
0.2.0 Jul 23, 2015

#21 in Text processing

Download history 1634091/week @ 2025-01-28 1941707/week @ 2025-02-04 1690753/week @ 2025-02-11 1928370/week @ 2025-02-18 1765181/week @ 2025-02-25 1789099/week @ 2025-03-04 1797680/week @ 2025-03-11 1793734/week @ 2025-03-18 1732974/week @ 2025-03-25 1766043/week @ 2025-04-01 1796698/week @ 2025-04-08 1626829/week @ 2025-04-15 1699548/week @ 2025-04-22 1581387/week @ 2025-04-29 1672320/week @ 2025-05-06 1426289/week @ 2025-05-13

6,611,017 downloads per month
Used in 7,559 crates (30 directly)

MIT/Apache

250KB
4K SLoC

unicode-bidi

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.

Documentation

CI AppVeyor


lib.rs:

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.

Dependencies

~0–570KB