use oxitext_core::{LayoutConstraints, ShapedGlyph, ShapedRun, TextAlignment};
use oxitext_layout::LayoutEngine;
use std::sync::Arc;
fn shaped_run_from_text(text: &str, advance: f32) -> ShapedRun {
let glyphs: Vec<ShapedGlyph> = text
.char_indices()
.enumerate()
.map(|(i, (byte_idx, ch))| ShapedGlyph {
gid: (i + 1) as u16,
x_advance: advance,
cluster: byte_idx as u32,
is_whitespace: ch.is_whitespace(),
..Default::default()
})
.collect();
ShapedRun {
glyphs: glyphs.into(),
font_data: Arc::from(&[][..]),
}
}
fn main() {
let text = "The quick brown fox jumps";
let run = shaped_run_from_text(text, 12.0);
let constraints = LayoutConstraints {
max_width: 120.0,
font_size: 16.0,
};
let mut engine = LayoutEngine::new();
let result = engine
.layout(text, &[run], &constraints, TextAlignment::Left, None)
.expect("layout is currently infallible for well-formed input");
println!(
"laid out {} glyph(s) into {} line(s); paragraph size = {:.1} x {:.1}px",
result.glyphs.len(),
result.lines.len(),
result.metrics.total_width,
result.metrics.total_height,
);
assert!(
result.lines.len() > 1,
"narrow max_width should force wraps"
);
for (i, line) in result.lines.iter().enumerate() {
let glyphs = &result.glyphs[line.glyph_start..line.glyph_end];
let first_x = glyphs.first().map(|g| g.pos.0).unwrap_or(0.0);
println!(
" line {i}: {} glyph(s), starts at x={first_x:.1}, width={:.1}px",
line.len(),
line.metrics.width,
);
assert!((first_x - 0.0).abs() < 1e-3);
}
let glyph_set = result.unique_glyphs_for_atlas();
println!(
"{} unique glyph(s) needed for rasterization",
glyph_set.len()
);
assert!(!glyph_set.is_empty());
}