f-office-pdf 0.2.4

Writes a display list as PDF.
Documentation
//! Grouping of display-list glyphs for PDF text positioning.

use f_office_paint::GlyphRun;

/// The glyphs of a run, as ranges of its glyphs, that stay together as a single text cluster:
/// a glyph with the glyphs after it that show no text or a part of its text, such as the glyphs
/// of one character with its combining marks.
pub(crate) fn positioned_groups(run: &GlyphRun) -> Vec<std::ops::Range<usize>> {
    let mut groups = Vec::new();
    let mut start = 0;
    let mut text: Option<(u32, u32)> = None;
    for (index, glyph) in run.glyphs.iter().enumerate() {
        let (from, to) = (glyph.text_start, glyph.text_end);
        let joins = match text {
            None => true,
            Some((low, high)) => from == to || (from < high && low < to),
        };
        if joins {
            text = Some(text.map_or((from, to), |(low, high)| (low.min(from), high.max(to))));
        } else {
            groups.push(start..index);
            start = index;
            text = Some((from, to));
        }
    }
    if start < run.glyphs.len() {
        groups.push(start..run.glyphs.len());
    }
    groups
}