Skip to content

Commit

Permalink
Merge pull request #124 from tigregalis/iter-bidi-para
Browse files Browse the repository at this point in the history
Replace use of `str::Lines` with `unicode-bidi`-based iterator
  • Loading branch information
jackpot51 authored Jun 9, 2023
2 parents b6b0358 + 5ab509e commit b5f45f8
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
6 changes: 3 additions & 3 deletions examples/editor-test/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// SPDX-License-Identifier: MIT OR Apache-2.0

use cosmic_text::{
Action, BorrowedWithFontSystem, Buffer, Color, Edit, Editor, FontSystem, Metrics, SwashCache,
Action, BidiParagraphs, BorrowedWithFontSystem, Buffer, Color, Edit, Editor, FontSystem,
Metrics, SwashCache,
};
use orbclient::{EventOption, Renderer, Window, WindowFlag};
use std::{env, fs, process, time::Instant};
Expand Down Expand Up @@ -83,8 +84,7 @@ fn main() {

let test_start = Instant::now();

//TODO: support bidi
for line in text.lines() {
for line in BidiParagraphs::new(&text) {
log::debug!("Line {:?}", line);

for grapheme in line.graphemes(true) {
Expand Down
39 changes: 39 additions & 0 deletions src/bidi_para.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// SPDX-License-Identifier: MIT OR Apache-2.0

use unicode_bidi::{bidi_class, BidiClass, BidiInfo, ParagraphInfo};

/// An iterator over the paragraphs in the input text.
/// It is equivalent to [`core::str::Lines`] but follows `unicode-bidi` behaviour.
pub struct BidiParagraphs<'text> {
text: &'text str,
info: alloc::vec::IntoIter<ParagraphInfo>,
}

impl<'text> BidiParagraphs<'text> {
/// Create an iterator to split the input text into paragraphs
/// in accordance with `unicode-bidi` behaviour.
pub fn new(text: &'text str) -> Self {
let info = BidiInfo::new(text, None);
let info = info.paragraphs.into_iter();
Self { text, info }
}
}

impl<'text> Iterator for BidiParagraphs<'text> {
type Item = &'text str;

fn next(&mut self) -> Option<Self::Item> {
let para = self.info.next()?;
let paragraph = &self.text[para.range];
// `para.range` includes the newline that splits the line, so remove it if present
let mut char_indices = paragraph.char_indices();
if let Some(i) = char_indices.next_back().and_then(|(i, c)| {
// `BidiClass::B` is a Paragraph_Separator (various newline characters)
(bidi_class(c) == BidiClass::B).then_some(i)
}) {
Some(&paragraph[0..i])
} else {
Some(paragraph)
}
}
}
6 changes: 3 additions & 3 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use core::{cmp, fmt};
use unicode_segmentation::UnicodeSegmentation;

use crate::{
Attrs, AttrsList, BorrowedWithFontSystem, BufferLine, Color, FontSystem, LayoutGlyph,
LayoutLine, ShapeLine, Shaping, Wrap,
Attrs, AttrsList, BidiParagraphs, BorrowedWithFontSystem, BufferLine, Color, FontSystem,
LayoutGlyph, LayoutLine, ShapeLine, Shaping, Wrap,
};

/// Current cursor location
Expand Down Expand Up @@ -594,7 +594,7 @@ impl Buffer {
shaping: Shaping,
) {
self.lines.clear();
for line in text.lines() {
for line in BidiParagraphs::new(text) {
self.lines.push(BufferLine::new(
line.to_string(),
AttrsList::new(attrs),
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ extern crate alloc;
pub use self::attrs::*;
mod attrs;

pub use self::bidi_para::*;
mod bidi_para;

pub use self::buffer::*;
mod buffer;

Expand Down

0 comments on commit b5f45f8

Please sign in to comment.