Skip to main content

verso/reader/
linebreak.rs

1use hyphenation::{Language, Load, Standard};
2use textwrap::Options;
3
4pub fn wrap(text: &str, width: u16) -> Vec<String> {
5    let dict = Standard::from_embedded(Language::EnglishUS).ok();
6    let mut opts = Options::new(width as usize).break_words(false);
7    if let Some(d) = dict.as_ref() {
8        opts = opts.word_splitter(textwrap::WordSplitter::Hyphenation(d.clone()));
9    }
10
11    let mut out = Vec::new();
12    for (i, para) in text.split("\n\n").enumerate() {
13        if i > 0 {
14            out.push(String::new());
15        }
16        let wrapped = textwrap::wrap(para, &opts);
17        for line in wrapped {
18            out.push(line.into_owned());
19        }
20    }
21    out
22}