//! Space-pad multi-align text onto one Font A column row. //! //! ESC/POS `ESC a` justifies whole lines only; same-line `:left:` + `:right:` //! (and optional `:center:`) must be simulated with spaces to `chars_per_line`. use crate::dsl::Align; #[derive(Debug, Clone, Default, PartialEq, Eq)] pub struct AlignedColumns { pub left: Option, pub center: Option, pub right: Option, } /// Map ordered `(Align, text)` parts into Left / Center / Right columns. /// Later segments for the same align win (silent replace). pub fn columns_from_parts(parts: &[(Align, String)]) -> AlignedColumns { let mut cols = AlignedColumns::default(); for (align, text) in parts { let text = text.clone(); match align { Align::Left => cols.left = Some(text), Align::Center => cols.center = Some(text), Align::Right => cols.right = Some(text), } } cols } /// Build one fixed-width line, or `None` when content cannot fit without overlap. pub fn layout_aligned_row( left: Option<&str>, center: Option<&str>, right: Option<&str>, cols: usize, ) -> Option { if cols == 0 { return None; } let left = left.unwrap_or(""); let center = center.unwrap_or(""); let right = right.unwrap_or(""); let left_len = left.chars().count(); let center_len = center.chars().count(); let right_len = right.chars().count(); if left_len + center_len + right_len > cols { return None; } // Fast path: classic label … amount (no center). if center_len == 0 { let gap = cols - left_len - right_len; return Some(format!("{left}{}{right}", " ".repeat(gap))); } let mut buf = vec![' '; cols]; for (i, ch) in left.chars().enumerate() { buf[i] = ch; } if right_len > 0 { let start = cols - right_len; for (i, ch) in right.chars().enumerate() { buf[start + i] = ch; } } let center_start = (cols - center_len) / 2; for (i, ch) in center.chars().enumerate() { let idx = center_start + i; // Overlaps left or right region → cannot pad safely. if idx < left_len || (right_len > 0 && idx >= cols - right_len) { return None; } if buf[idx] != ' ' { return None; } buf[idx] = ch; } Some(buf.into_iter().collect()) } /// Prefer a single padded line; on overflow return Left → Center → Right lines. pub fn layout_aligned_row_or_lines(columns: &AlignedColumns, cols: usize) -> Vec<(Align, String)> { let left = columns.left.as_deref(); let center = columns.center.as_deref(); let right = columns.right.as_deref(); if let Some(line) = layout_aligned_row(left, center, right, cols) { return vec![(Align::Left, line)]; } let mut lines = Vec::new(); if let Some(t) = columns.left.as_ref().filter(|s| !s.is_empty()) { lines.push((Align::Left, t.clone())); } if let Some(t) = columns.center.as_ref().filter(|s| !s.is_empty()) { lines.push((Align::Center, t.clone())); } if let Some(t) = columns.right.as_ref().filter(|s| !s.is_empty()) { lines.push((Align::Right, t.clone())); } lines } #[cfg(test)] mod tests { use super::*; #[test] fn left_right_gap_matches_cols() { let line = layout_aligned_row(Some("appel"), None, Some("10"), 32).unwrap(); assert_eq!(line.chars().count(), 32); assert!(line.starts_with("appel")); assert!(line.ends_with("10")); assert_eq!(&line[5..30], &" ".repeat(25)); } #[test] fn right_then_left_parts_normalize_order() { let cols = columns_from_parts(&[ (Align::Right, "10".into()), (Align::Left, "appel".into()), ]); assert_eq!(cols.left.as_deref(), Some("appel")); assert_eq!(cols.right.as_deref(), Some("10")); let line = layout_aligned_row_or_lines(&cols, 32); assert_eq!(line.len(), 1); assert_eq!(line[0].0, Align::Left); assert!(line[0].1.starts_with("appel")); assert!(line[0].1.ends_with("10")); } #[test] fn longer_right_shortens_gap() { let short = layout_aligned_row(Some("A"), None, Some("1"), 10).unwrap(); let long = layout_aligned_row(Some("A"), None, Some("12345"), 10).unwrap(); assert_eq!(short.chars().count(), 10); assert_eq!(long.chars().count(), 10); assert!(short.contains(" ")); // 8 spaces assert_eq!(&long[1..5], " "); // 4 spaces } #[test] fn overflow_falls_back_to_separate_lines() { let cols = AlignedColumns { left: Some("AAAAAAAAAA".into()), center: None, right: Some("BBBBBBBBBBBB".into()), }; let lines = layout_aligned_row_or_lines(&cols, 10); assert_eq!(lines.len(), 2); assert_eq!(lines[0], (Align::Left, "AAAAAAAAAA".into())); assert_eq!(lines[1], (Align::Right, "BBBBBBBBBBBB".into())); } #[test] fn center_between_left_and_right() { let line = layout_aligned_row(Some("L"), Some("MID"), Some("R"), 11).unwrap(); assert_eq!(line, "L MID R"); } #[test] fn center_overlap_with_left_returns_none() { assert!(layout_aligned_row(Some("LEFTSIDE"), Some("CENTER"), None, 12).is_none()); } }