slint/internal/core/textlayout/linebreak_simple.rs
Olivier Goffart ad80e3e5e2 Fix software renderer line breaker without the unicode feature
This change makes the test pass without the unicode feature.

Note that it is not possible to run the test without the unicode feature
unless one changes the Cargo.toml, so I did that locally to run the
tests
2022-09-07 13:49:02 +02:00

39 lines
1.2 KiB
Rust

// Copyright © SixtyFPS GmbH <info@slint-ui.com>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-commercial
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum BreakOpportunity {
Allowed,
Mandatory,
}
#[derive(Clone)]
pub struct LineBreakIterator<'a> {
it: core::str::CharIndices<'a>,
}
impl<'a> LineBreakIterator<'a> {
pub fn new(text: &'a str) -> Self {
Self { it: text.char_indices() }
}
}
impl<'a> Iterator for LineBreakIterator<'a> {
type Item = (usize, BreakOpportunity);
fn next(&mut self) -> Option<Self::Item> {
while let Some((byte_offset, char)) = self.it.next() {
let maybe_opportunity = match char {
'\u{2028}' | '\u{2029}' => Some(BreakOpportunity::Mandatory), // unicode line- and paragraph separators
'\n' => Some(BreakOpportunity::Mandatory), // ascii line break
_ if char.is_ascii_whitespace() => Some(BreakOpportunity::Allowed),
_ => None,
};
if let Some(opportunity) = maybe_opportunity {
return Some((byte_offset + 1, opportunity));
}
}
None
}
}