slint/internal/core/textlayout/linebreak_unicode.rs
Simon Hausmann f1fb451791 Make the line break iterators implement Clone
This is needed in the future to be able to roll back state within the
line breaking when we can't fit even a single word into the line and
have to go back to fitting glyph by glyph.

For the unicode line break iterator the clone is a little more complicated.
The iterator is clone, but the type is anonymous. I first had an
implementation to moved the iterator into a closure, which was cloned
via another control closure. But that's complicated and still involves
allocating the various bits & pieces the closure captures.

So instead the simpler solution used here is to allocate the breaks
into a shared vector.
2022-05-13 17:33:01 +02:00

45 lines
1.3 KiB
Rust

// Copyright © SixtyFPS GmbH <info@slint-ui.com>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-commercial
use core::marker::PhantomData;
pub use unicode_linebreak::BreakOpportunity;
use crate::SharedVector;
#[derive(Clone)]
pub struct LineBreakIterator<'a> {
breaks: SharedVector<(usize, unicode_linebreak::BreakOpportunity)>,
pos: usize,
phantom: PhantomData<&'a str>,
}
impl<'a> LineBreakIterator<'a> {
pub fn new(text: &str) -> Self {
let iterator = unicode_linebreak::linebreaks(text).filter(|(offset, opportunity)| {
// unicode-linebreaks emits a mandatory break at the end of the text. We're not interested
// in that.
if *offset == text.len() && matches!(opportunity, BreakOpportunity::Mandatory) {
false
} else {
true
}
});
Self { breaks: iterator.collect(), pos: 0, phantom: Default::default() }
}
}
impl<'a> Iterator for LineBreakIterator<'a> {
type Item = (usize, unicode_linebreak::BreakOpportunity);
fn next(&mut self) -> Option<Self::Item> {
if self.pos < self.breaks.len() {
let i = self.pos;
self.pos += 1;
Some(self.breaks[i])
} else {
None
}
}
}