In cosmic_text the information you need (how many visual lines were produced and where each line begins / ends in the original text) is stored in the layout, not in the LayoutRunIter itself.
There are two practical ways to get it:
use cosmic_text::{Alignment, Attrs, Buffer, FontSystem, Layout, Metrics, Wrap};
let mut font_system = FontSystem::new();
let metrics = Metrics::new(14.0, 18.0);
let mut buffer = Buffer::new(&mut font_system, metrics);
buffer.set_text("Hello cosmic_text –\nthis is line two", Attrs::new());
buffer.set_size(400.0, f32::INFINITY); // wrap width, height
let mut layout = Layout::new(Wrap::Word, Alignment::Start, Direction::LeftToRight);
layout.reset(&mut buffer); // run the layout engine
//--------------------------------------------------------------
// The interesting bit ↓
let lines = layout.lines(); // &[LayoutLine]
let has_more_than_one = lines.len() > 1;
if has_more_than_one {
// Each LayoutLine contains the UTF-8 byte range it covers.
let first_line_range = lines[0].text(); // std::ops::Range<usize>
println!("First line ends at byte {}", first_line_range.end);
}
LayoutLine::text() (or range() in older versions) tells you where the line ends in the original buffer, so first_line_range.end is the “cut-off point” of the first line. lines.len() immediately tells you whether more than one line was produced.
Derive it from LayoutRunIter
If, for some reason, you only have a LayoutRunIter, you can still figure it out because every LayoutRun knows which visual line it belongs to:
rust
let mut first_line_end = None;
for run in layout.layout_runs() { // LayoutRunIter
if run.line_i() > 0 { // we just hit the 2nd line
first_line_end = Some(run.start());
break;
}
}
// `first_line_end` is `Some(usize)` ⇢ index of the first char on line 2
let multi_line = first_line_end.is_some();
Explanation of the fields/methods you see above (public in current cosmic_text):
• run.line_i() — visual line number (0-based) that this run is on
• run.start() / run.end() — byte indices into the buffer for that run
So the first time line_i() becomes 1, the corresponding start() index is exactly where the second line begins, i.e. one byte after the end of the first line.
Pick whichever approach fits best. In normal code paths the first solution (layout.lines()) is shorter and clearer, but if you are already streaming over runs (e.g. during rendering) the second snippet lets you discover the cut-off on the fly.