| 1 | use crate::io::PosMetadata;
|
| 2 |
|
| 3 | #[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
| 4 | pub enum TextWrapStyle {
|
| 5 | Normal,
|
| 6 | Heading1,
|
| 7 | Heading2,
|
| 8 | }
|
| 9 |
|
| 10 | /// Character columns available for a text style on the target printer.
|
| 11 | pub fn wrap_cols(metadata: &PosMetadata, style: TextWrapStyle) -> usize {
|
| 12 | let base = metadata.chars_per_line().max(1) as usize;
|
| 13 | match style {
|
| 14 | TextWrapStyle::Heading1 => (base / 2).max(1),
|
| 15 | TextWrapStyle::Normal | TextWrapStyle::Heading2 => base,
|
| 16 | }
|
| 17 | }
|
| 18 |
|
| 19 | /// ASCII bullet prefix — universally supported on ESC/POS printers.
|
| 20 | pub const BULLET_FIRST_PREFIX: &str = "* ";
|
| 21 | /// Indent for wrapped continuation lines (same width as [`BULLET_FIRST_PREFIX`]).
|
| 22 | pub const BULLET_NEXT_PREFIX: &str = " ";
|
| 23 |
|
| 24 | /// Word-wrap a bullet list item with hanging indentation.
|
| 25 | pub fn wrap_bullet_item(text: &str, max_cols: usize) -> Vec<String> {
|
| 26 | wrap_hanging(text, max_cols, BULLET_FIRST_PREFIX, BULLET_NEXT_PREFIX)
|
| 27 | }
|
| 28 |
|
| 29 | /// Word-wrap `text` with different budgets for the first and subsequent lines.
|
| 30 | pub fn wrap_hanging(
|
| 31 | text: &str,
|
| 32 | max_cols: usize,
|
| 33 | first_prefix: &str,
|
| 34 | next_prefix: &str,
|
| 35 | ) -> Vec<String> {
|
| 36 | let first_budget = max_cols.saturating_sub(first_prefix.chars().count());
|
| 37 | let next_budget = max_cols.saturating_sub(next_prefix.chars().count());
|
| 38 | let content_lines = wrap_content_var_budget(text, first_budget, next_budget);
|
| 39 |
|
| 40 | content_lines
|
| 41 | .into_iter()
|
| 42 | .enumerate()
|
| 43 | .map(|(idx, line)| {
|
| 44 | if idx == 0 {
|
| 45 | format!("{first_prefix}{line}")
|
| 46 | } else {
|
| 47 | format!("{next_prefix}{line}")
|
| 48 | }
|
| 49 | })
|
| 50 | .collect()
|
| 51 | }
|
| 52 |
|
| 53 | fn wrap_content_var_budget(text: &str, first_budget: usize, next_budget: usize) -> Vec<String> {
|
| 54 | if first_budget == 0 && next_budget == 0 {
|
| 55 | return vec![text.to_string()];
|
| 56 | }
|
| 57 |
|
| 58 | let words: Vec<&str> = text.split_whitespace().collect();
|
| 59 | if words.is_empty() {
|
| 60 | return vec![String::new()];
|
| 61 | }
|
| 62 |
|
| 63 | let mut lines = Vec::new();
|
| 64 | let mut current = String::new();
|
| 65 | let mut budget = first_budget.max(1);
|
| 66 | let mut word_idx = 0usize;
|
| 67 |
|
| 68 | while word_idx < words.len() {
|
| 69 | let word = words[word_idx];
|
| 70 | let word_len = word.chars().count();
|
| 71 |
|
| 72 | if current.is_empty() {
|
| 73 | if word_len <= budget {
|
| 74 | current.push_str(word);
|
| 75 | word_idx += 1;
|
| 76 | } else {
|
| 77 | let mut remaining = word;
|
| 78 | while !remaining.is_empty() {
|
| 79 | let chunk = take_char_prefix(remaining, budget);
|
| 80 | let consumed = chunk.chars().count();
|
| 81 | if chunk.is_empty() {
|
| 82 | break;
|
| 83 | }
|
| 84 | lines.push(chunk);
|
| 85 | remaining = &remaining[remaining
|
| 86 | .char_indices()
|
| 87 | .nth(consumed)
|
| 88 | .map(|(idx, _)| idx)
|
| 89 | .unwrap_or(remaining.len())..];
|
| 90 | budget = next_budget.max(1);
|
| 91 | }
|
| 92 | }
|
| 93 | } else {
|
| 94 | let current_len = current.chars().count();
|
| 95 | if current_len + 1 + word_len <= budget {
|
| 96 | current.push(' ');
|
| 97 | current.push_str(word);
|
| 98 | word_idx += 1;
|
| 99 | } else {
|
| 100 | lines.push(std::mem::take(&mut current));
|
| 101 | budget = next_budget.max(1);
|
| 102 | }
|
| 103 | }
|
| 104 | }
|
| 105 |
|
| 106 | if !current.is_empty() {
|
| 107 | lines.push(current);
|
| 108 | }
|
| 109 | if lines.is_empty() {
|
| 110 | lines.push(String::new());
|
| 111 | }
|
| 112 | lines
|
| 113 | }
|
| 114 |
|
| 115 | fn take_char_prefix(text: &str, max_chars: usize) -> String {
|
| 116 | text.chars().take(max_chars).collect()
|
| 117 | }
|
| 118 |
|
| 119 | /// Word-wrap `text` to at most `max_cols` characters per line.
|
| 120 | /// Explicit newlines in `text` start a new wrapped paragraph.
|
| 121 | pub fn wrap_text(text: &str, max_cols: usize) -> Vec<String> {
|
| 122 | if max_cols == 0 {
|
| 123 | return vec![text.to_string()];
|
| 124 | }
|
| 125 |
|
| 126 | let mut lines = Vec::new();
|
| 127 | for paragraph in text.split('\n') {
|
| 128 | lines.extend(wrap_line(paragraph, max_cols));
|
| 129 | }
|
| 130 | if lines.is_empty() {
|
| 131 | lines.push(String::new());
|
| 132 | }
|
| 133 | lines
|
| 134 | }
|
| 135 |
|
| 136 | fn wrap_line(line: &str, max_cols: usize) -> Vec<String> {
|
| 137 | if line.is_empty() {
|
| 138 | return vec![String::new()];
|
| 139 | }
|
| 140 |
|
| 141 | let mut lines = Vec::new();
|
| 142 | let mut current = String::new();
|
| 143 |
|
| 144 | for word in line.split_whitespace() {
|
| 145 | let word_len = word.chars().count();
|
| 146 | if current.is_empty() {
|
| 147 | if word_len <= max_cols {
|
| 148 | current.push_str(word);
|
| 149 | } else {
|
| 150 | lines.extend(split_long_word(word, max_cols));
|
| 151 | }
|
| 152 | } else {
|
| 153 | let current_len = current.chars().count();
|
| 154 | if current_len + 1 + word_len <= max_cols {
|
| 155 | current.push(' ');
|
| 156 | current.push_str(word);
|
| 157 | } else {
|
| 158 | lines.push(std::mem::take(&mut current));
|
| 159 | if word_len <= max_cols {
|
| 160 | current.push_str(word);
|
| 161 | } else {
|
| 162 | lines.extend(split_long_word(word, max_cols));
|
| 163 | }
|
| 164 | }
|
| 165 | }
|
| 166 | }
|
| 167 |
|
| 168 | if !current.is_empty() {
|
| 169 | lines.push(current);
|
| 170 | }
|
| 171 |
|
| 172 | if lines.is_empty() {
|
| 173 | lines.push(String::new());
|
| 174 | }
|
| 175 | lines
|
| 176 | }
|
| 177 |
|
| 178 | fn split_long_word(word: &str, max_cols: usize) -> Vec<String> {
|
| 179 | let mut chunks = Vec::new();
|
| 180 | let mut chunk = String::new();
|
| 181 | for ch in word.chars() {
|
| 182 | if chunk.chars().count() >= max_cols {
|
| 183 | chunks.push(std::mem::take(&mut chunk));
|
| 184 | }
|
| 185 | chunk.push(ch);
|
| 186 | }
|
| 187 | if !chunk.is_empty() {
|
| 188 | chunks.push(chunk);
|
| 189 | }
|
| 190 | chunks
|
| 191 | }
|
| 192 |
|
| 193 | #[cfg(test)]
|
| 194 | mod tests {
|
| 195 | use super::*;
|
| 196 |
|
| 197 | fn meta_53mm() -> PosMetadata {
|
| 198 | PosMetadata {
|
| 199 | page_width_mm: 53.0,
|
| 200 | ..Default::default()
|
| 201 | }
|
| 202 | }
|
| 203 |
|
| 204 | #[test]
|
| 205 | fn heading1_wraps_at_half_line_width() {
|
| 206 | let cols = wrap_cols(&meta_53mm(), TextWrapStyle::Heading1);
|
| 207 | assert_eq!(cols, 16);
|
| 208 | let lines = wrap_text("Proof of Transfer", cols);
|
| 209 | assert_eq!(lines, vec!["Proof of", "Transfer"]);
|
| 210 | }
|
| 211 |
|
| 212 | #[test]
|
| 213 | fn preserves_explicit_newlines() {
|
| 214 | let lines = wrap_text("Line one\nLine two", 32);
|
| 215 | assert_eq!(lines, vec!["Line one", "Line two"]);
|
| 216 | }
|
| 217 |
|
| 218 | #[test]
|
| 219 | fn normal_text_wraps_at_full_width() {
|
| 220 | let cols = wrap_cols(&meta_53mm(), TextWrapStyle::Normal);
|
| 221 | assert_eq!(cols, 32);
|
| 222 | }
|
| 223 |
|
| 224 | #[test]
|
| 225 | fn bullet_wrap_uses_hanging_indent() {
|
| 226 | let lines = wrap_bullet_item("this is a long line", 15);
|
| 227 | assert_eq!(lines, vec!["* this is a", " long line"]);
|
| 228 | }
|
| 229 | }
|