1use crate::model::{Instructions, Orientation};
9use serde::Serialize;
10
11pub const PAPER_SIZES: &[(&str, f64, f64)] = &[
14 ("A4", 297.0, 210.0),
15 ("A3", 420.0, 297.0),
16 ("A2", 594.0, 420.0),
17 ("A1", 841.0, 594.0),
18 ("A0", 1189.0, 841.0),
19 ("Letter", 279.4, 215.9),
20 ("Tabloid", 431.8, 279.4),
21];
22
23pub const MARGIN_MM: f64 = 12.0;
24pub const HEADER_MM: f64 = 12.0;
26pub const GUTTER_MM: f64 = 6.0;
27pub const PARTS_ROW_MM: f64 = 12.0;
28pub const TOOLS_ROW_MM: f64 = 16.0;
29
30#[derive(Debug, Clone, Copy, PartialEq, Serialize)]
31pub struct Rect {
32 pub x: f64,
33 pub y: f64,
34 pub w: f64,
35 pub h: f64,
36}
37
38impl Rect {
39 pub fn contains(&self, other: &Rect) -> bool {
40 const E: f64 = 1e-9;
41 other.x >= self.x - E && other.y >= self.y - E && other.x + other.w <= self.x + self.w + E && other.y + other.h <= self.y + self.h + E
42 }
43
44 pub fn overlaps(&self, other: &Rect) -> bool {
45 const E: f64 = 1e-9;
46 self.x + E < other.x + other.w && other.x + E < self.x + self.w && self.y + E < other.y + other.h && other.y + E < self.y + self.h
47 }
48}
49
50#[derive(Debug, Clone, PartialEq, Serialize)]
51#[serde(tag = "kind", rename_all = "camelCase")]
52pub enum PageKind {
53 Cover,
54 PartsList { first: usize, count: usize },
56 ToolsList { first: usize, count: usize },
58 Steps,
59}
60
61#[derive(Debug, Clone, PartialEq, Serialize)]
63#[serde(rename_all = "camelCase")]
64pub struct Slot {
65 pub step_id: String,
66 pub step_number: usize,
67 pub rect: Rect,
68 pub picture: Rect,
70 pub text: Rect,
72}
73
74#[derive(Debug, Clone, PartialEq, Serialize)]
75#[serde(rename_all = "camelCase")]
76pub struct Page {
77 pub number: usize,
79 pub width_mm: f64,
80 pub height_mm: f64,
81 #[serde(flatten)]
82 pub kind: PageKind,
83 pub content: Rect,
85 #[serde(skip_serializing_if = "Vec::is_empty")]
86 pub slots: Vec<Slot>,
87}
88
89pub fn paper_mm(paper: &str, orientation: Orientation) -> Result<(f64, f64), String> {
91 let &(_, w, h) = PAPER_SIZES
92 .iter()
93 .find(|(key, _, _)| key.eq_ignore_ascii_case(paper))
94 .ok_or_else(|| format!("unknown paper size '{paper}'"))?;
95 Ok(match orientation {
96 Orientation::Landscape => (w, h),
97 Orientation::Portrait => (h, w),
98 })
99}
100
101pub fn pages(block: &Instructions) -> Result<Vec<Page>, String> {
103 let template = &block.template;
104 if !crate::model::STEPS_PER_PAGE.contains(&template.steps_per_page) {
105 return Err(format!("steps per page must be 1, 2 or 4, not {}", template.steps_per_page));
106 }
107 let (width, height) = paper_mm(&template.paper, template.orientation)?;
108 let content = Rect {
109 x: MARGIN_MM,
110 y: MARGIN_MM + HEADER_MM,
111 w: width - 2.0 * MARGIN_MM,
112 h: height - 2.0 * MARGIN_MM - HEADER_MM,
113 };
114 let mut pages = Vec::new();
115 let push = |kind: PageKind, slots: Vec<Slot>, pages: &mut Vec<Page>| {
116 let number = pages.len() + 1;
117 pages.push(Page { number, width_mm: width, height_mm: height, kind, content, slots });
118 };
119 push(PageKind::Cover, Vec::new(), &mut pages);
120 if block.front.parts_list {
121 let rows = block.parts_list().len();
122 for (first, count) in chunks(rows, rows_per_page(content.h, PARTS_ROW_MM)) {
123 push(PageKind::PartsList { first, count }, Vec::new(), &mut pages);
124 }
125 }
126 if block.front.tools_list {
127 let rows = block.tools_list().len();
128 if rows > 0 {
129 for (first, count) in chunks(rows, rows_per_page(content.h, TOOLS_ROW_MM)) {
130 push(PageKind::ToolsList { first, count }, Vec::new(), &mut pages);
131 }
132 }
133 }
134 let cells = grid(content, template.steps_per_page, template.orientation);
135 let steps: Vec<&str> = block.steps().map(|(_, s)| s.id.as_str()).collect();
136 for (page_index, chunk) in steps.chunks(cells.len()).enumerate() {
137 let slots = chunk
138 .iter()
139 .zip(&cells)
140 .enumerate()
141 .map(|(i, (id, cell))| {
142 let (picture, text) = split_cell(*cell);
143 Slot {
144 step_id: id.to_string(),
145 step_number: page_index * cells.len() + i + 1,
146 rect: *cell,
147 picture,
148 text,
149 }
150 })
151 .collect();
152 push(PageKind::Steps, slots, &mut pages);
153 }
154 Ok(pages)
155}
156
157fn rows_per_page(content_h: f64, row_mm: f64) -> usize {
160 ((content_h / row_mm).floor() as usize).saturating_sub(1).max(1)
161}
162
163fn chunks(rows: usize, per_page: usize) -> Vec<(usize, usize)> {
166 if rows == 0 {
167 return vec![(0, 0)];
168 }
169 (0..rows).step_by(per_page).map(|first| (first, per_page.min(rows - first))).collect()
170}
171
172fn grid(content: Rect, per_page: u8, orientation: Orientation) -> Vec<Rect> {
176 let (columns, rows) = match (per_page, orientation) {
177 (1, _) => (1, 1),
178 (2, Orientation::Portrait) => (1, 2),
179 (2, Orientation::Landscape) => (2, 1),
180 _ => (2, 2),
181 };
182 let w = (content.w - GUTTER_MM * (columns - 1) as f64) / columns as f64;
183 let h = (content.h - GUTTER_MM * (rows - 1) as f64) / rows as f64;
184 let mut cells = Vec::new();
185 for r in 0..rows {
186 for c in 0..columns {
187 cells.push(Rect { x: content.x + c as f64 * (w + GUTTER_MM), y: content.y + r as f64 * (h + GUTTER_MM), w, h });
188 }
189 }
190 cells
191}
192
193fn split_cell(cell: Rect) -> (Rect, Rect) {
196 if cell.w > 1.3 * cell.h {
197 let text_w = (cell.w * 0.34).max(40.0f64.min(cell.w * 0.5));
198 let picture = Rect { x: cell.x, y: cell.y, w: cell.w - text_w - GUTTER_MM / 2.0, h: cell.h };
199 let text = Rect { x: cell.x + cell.w - text_w, y: cell.y, w: text_w, h: cell.h };
200 (picture, text)
201 } else {
202 let text_h = cell.h * 0.3;
203 let picture = Rect { x: cell.x, y: cell.y, w: cell.w, h: cell.h - text_h - GUTTER_MM / 2.0 };
204 let text = Rect { x: cell.x, y: cell.y + cell.h - text_h, w: cell.w, h: text_h };
205 (picture, text)
206 }
207}
208