#[cfg(feature = "nostd")]
use alloc::{
string::{String, ToString},
vec::Vec,
};
#[cfg(not(feature = "nostd"))]
use std::{string::String, vec::Vec};
use super::{Alignment, LayoutContext};
pub struct MultiLineLayout {
pub lines: Vec<LineLayout>,
pub total_width: f32,
pub total_height: f32,
}
pub struct LineLayout {
pub text: String,
pub x: f32,
pub y: f32,
pub width: f32,
pub height: f32,
pub baseline: f32,
}
impl MultiLineLayout {
pub fn new(
text: &str,
_context: &LayoutContext,
alignment: Alignment,
line_spacing: f32,
) -> Self {
let lines: Vec<&str> = text.split('\n').collect();
let mut line_layouts = Vec::new();
let mut total_height = 0.0;
let mut max_width = 0.0;
let line_height = 50.0;
for (i, line) in lines.iter().enumerate() {
let line_width = line.len() as f32 * 20.0; max_width = f32::max(max_width, line_width);
let y_offset = i as f32 * (line_height + line_spacing);
line_layouts.push(LineLayout {
text: line.to_string(),
x: 0.0, y: y_offset,
width: line_width,
height: line_height,
baseline: line_height * 0.8,
});
total_height = y_offset + line_height;
}
for line in &mut line_layouts {
line.x = match alignment {
Alignment::BottomLeft | Alignment::MiddleLeft | Alignment::TopLeft => 0.0,
Alignment::BottomCenter | Alignment::Center | Alignment::TopCenter => {
(max_width - line.width) / 2.0
}
Alignment::BottomRight | Alignment::MiddleRight | Alignment::TopRight => {
max_width - line.width
}
};
}
Self {
lines: line_layouts,
total_width: max_width,
total_height,
}
}
}