/// Break up text into chunks guaranteed to be no wider than max_width when rendered with
/// face at font_size.
Break up text into chunks guaranteed to be no wider than max_width when rendered with
face at font_size.
Break up text into chunks guaranteed to be no wider than max_width when rendered with face at font_size .
pub fn linebreak_text(face: &FontFace, font_size: f32, text: &str, max_width: f32) -> Vec<String> {
    let chunks: Vec<&str> = text.split_whitespace().collect();
    let space_width = measure_text_width(face, font_size, " ");
    let breaks: Vec<usize> = chunks
        .iter()
        .enumerate()
        .scan(0.0, |width, (index, word)| {
            let word_width = measure_text_width(face, font_size, word);
            let resulting_line_len = *width + word_width + space_width;
            if resulting_line_len > max_width {
                *width = word_width + space_width;
                Some(Some(index))
            } else {
                *width += word_width;
                *width += space_width;
                Some(None)
            }
        })
        .flatten()
        .chain(std::iter::once(chunks.len()))
        .collect();
    let lines: Vec<String> = breaks
        .iter()
        .scan(0, |first_word_index, last_word_index| {
            let first = *first_word_index;
            *first_word_index = *last_word_index;
            let line = &chunks[first..*last_word_index];
            let line_str = String::from(line.join(" "));
            Some(line_str)
        })
        .collect();
    lines
}
pub fn linebreak_text ( face : & FontFace , font_size : f32 , text : & str , max_width : f32 ) -> Vec < String > { let chunks : Vec < & str > = text . split_whitespace ( ) . collect ( ) ; let space_width = measure_text_width ( face , font_size , " " ) ; let breaks : Vec < usize > = chunks . iter ( ) . enumerate ( ) . scan ( 0.0 , | width , ( index , word ) | { let word_width = measure_text_width ( face , font_size , word ) ; let resulting_line_len = * width + word_width + space_width ; if resulting_line_len > max_width { * width = word_width + space_width ; Some ( Some ( index ) ) } else { * width += word_width ; * width += space_width ; Some ( None ) } } ) . flatten ( ) . chain ( std :: iter :: once ( chunks . len ( ) ) ) . collect ( ) ; let lines : Vec < String > = breaks . iter ( ) . scan ( 0 , | first_word_index , last_word_index | { let first = * first_word_index ; * first_word_index = * last_word_index ; let line = & chunks [ first .. * last_word_index ] ; let line_str = String :: from ( line . join ( " " ) ) ; Some ( line_str ) } ) . collect ( ) ; lines }
Break up text into chunks guaranteed to be no wider than max_width when rendered with
face at font_size.
Break up text into chunks guaranteed to be no wider than max_width when rendered with face at font_size .
face
&FontFace
font_size
f32
text
&str
max_width
f32