BlueGit

timfa posprint

Files

posprint / src / text_wrap.rs

text_wrap.rs

fixes
timfa Tim Falken · pushed by timfa · 4 months ago
4ab2186f History
text_wrap.rs 3.3 KiB Download Raw
1use crate::io::PosMetadata;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4pub enum TextWrapStyle {
5 Normal,
6 Heading1,
7 Heading2,
8}
9
10/// Character columns available for a text style on the target printer.
11pub 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/// Word-wrap `text` to at most `max_cols` characters per line.
20/// Explicit newlines in `text` start a new wrapped paragraph.
21pub fn wrap_text(text: &str, max_cols: usize) -> Vec<String> {
22 if max_cols == 0 {
23 return vec![text.to_string()];
24 }
25
26 let mut lines = Vec::new();
27 for paragraph in text.split('\n') {
28 lines.extend(wrap_line(paragraph, max_cols));
29 }
30 if lines.is_empty() {
31 lines.push(String::new());
32 }
33 lines
34}
35
36fn wrap_line(line: &str, max_cols: usize) -> Vec<String> {
37 if line.is_empty() {
38 return vec![String::new()];
39 }
40
41 let mut lines = Vec::new();
42 let mut current = String::new();
43
44 for word in line.split_whitespace() {
45 let word_len = word.chars().count();
46 if current.is_empty() {
47 if word_len <= max_cols {
48 current.push_str(word);
49 } else {
50 lines.extend(split_long_word(word, max_cols));
51 }
52 } else {
53 let current_len = current.chars().count();
54 if current_len + 1 + word_len <= max_cols {
55 current.push(' ');
56 current.push_str(word);
57 } else {
58 lines.push(std::mem::take(&mut current));
59 if word_len <= max_cols {
60 current.push_str(word);
61 } else {
62 lines.extend(split_long_word(word, max_cols));
63 }
64 }
65 }
66 }
67
68 if !current.is_empty() {
69 lines.push(current);
70 }
71
72 if lines.is_empty() {
73 lines.push(String::new());
74 }
75 lines
76}
77
78fn split_long_word(word: &str, max_cols: usize) -> Vec<String> {
79 let mut chunks = Vec::new();
80 let mut chunk = String::new();
81 for ch in word.chars() {
82 if chunk.chars().count() >= max_cols {
83 chunks.push(std::mem::take(&mut chunk));
84 }
85 chunk.push(ch);
86 }
87 if !chunk.is_empty() {
88 chunks.push(chunk);
89 }
90 chunks
91}
92
93#[cfg(test)]
94mod tests {
95 use super::*;
96
97 fn meta_53mm() -> PosMetadata {
98 PosMetadata {
99 page_width_mm: 53.0,
100 ..Default::default()
101 }
102 }
103
104 #[test]
105 fn heading1_wraps_at_half_line_width() {
106 let cols = wrap_cols(&meta_53mm(), TextWrapStyle::Heading1);
107 assert_eq!(cols, 16);
108 let lines = wrap_text("Proof of Transfer", cols);
109 assert_eq!(lines, vec!["Proof of", "Transfer"]);
110 }
111
112 #[test]
113 fn preserves_explicit_newlines() {
114 let lines = wrap_text("Line one\nLine two", 32);
115 assert_eq!(lines, vec!["Line one", "Line two"]);
116 }
117
118 #[test]
119 fn normal_text_wraps_at_full_width() {
120 let cols = wrap_cols(&meta_53mm(), TextWrapStyle::Normal);
121 assert_eq!(cols, 32);
122 }
123}