| 1 | //! Minimal 8×8 ASCII font for rasterizing lines that contain checkbox glyphs.
|
| 2 | //! Glyphs are public-domain style bitmaps (MSB = leftmost pixel).
|
| 3 |
|
| 4 | use image::{GrayImage, Luma};
|
| 5 |
|
| 6 | /// Columns reserved for one checkbox when wrapping text.
|
| 7 | pub const CHECKBOX_WRAP_COLS: usize = 3;
|
| 8 |
|
| 9 | /// Render height of a normal text line (matches Font A row height on ESC/POS).
|
| 10 | pub const LINE_HEIGHT: u32 = 24;
|
| 11 | /// Rendered character cell width after nearest-neighbor scale from 8×8.
|
| 12 | pub const CHAR_WIDTH: u32 = 12;
|
| 13 | const CHAR_SRC: u32 = 8;
|
| 14 | /// Checkbox box size (~20% larger than the original 14px).
|
| 15 | const CHECKBOX_SIDE: u32 = 17;
|
| 16 |
|
| 17 | #[derive(Debug, Clone, PartialEq, Eq)]
|
| 18 | pub enum MixedSeg {
|
| 19 | Text(String),
|
| 20 | Checkbox { checked: bool },
|
| 21 | }
|
| 22 |
|
| 23 | pub fn segments_from_inlines(inlines: &[crate::dsl::Inline]) -> Vec<MixedSeg> {
|
| 24 | let mut out = Vec::new();
|
| 25 | for inline in inlines {
|
| 26 | match inline {
|
| 27 | crate::dsl::Inline::Text(t) | crate::dsl::Inline::Param(t) => {
|
| 28 | if t.is_empty() {
|
| 29 | continue;
|
| 30 | }
|
| 31 | if let Some(MixedSeg::Text(existing)) = out.last_mut() {
|
| 32 | existing.push_str(t);
|
| 33 | } else {
|
| 34 | out.push(MixedSeg::Text(t.clone()));
|
| 35 | }
|
| 36 | }
|
| 37 | crate::dsl::Inline::Checkbox { checked } => {
|
| 38 | out.push(MixedSeg::Checkbox {
|
| 39 | checked: *checked,
|
| 40 | });
|
| 41 | }
|
| 42 | }
|
| 43 | }
|
| 44 | out
|
| 45 | }
|
| 46 |
|
| 47 | pub fn inlines_have_checkbox(inlines: &[crate::dsl::Inline]) -> bool {
|
| 48 | inlines
|
| 49 | .iter()
|
| 50 | .any(|i| matches!(i, crate::dsl::Inline::Checkbox { .. }))
|
| 51 | }
|
| 52 |
|
| 53 | /// Word-wrap mixed segments to `max_cols` character columns.
|
| 54 | /// Explicit `\n` inside text segments start a new line (same as [`crate::text_wrap::wrap_text`]).
|
| 55 | pub fn wrap_segments(segs: &[MixedSeg], max_cols: usize) -> Vec<Vec<MixedSeg>> {
|
| 56 | wrap_segments_hanging(segs, max_cols, "", "")
|
| 57 | }
|
| 58 |
|
| 59 | /// Split segments into hard lines on `\n` inside text (checkboxes never contain newlines).
|
| 60 | fn split_on_newlines(segs: &[MixedSeg]) -> Vec<Vec<MixedSeg>> {
|
| 61 | let mut lines: Vec<Vec<MixedSeg>> = vec![Vec::new()];
|
| 62 | for seg in segs {
|
| 63 | match seg {
|
| 64 | MixedSeg::Checkbox { checked } => {
|
| 65 | lines.last_mut().unwrap().push(MixedSeg::Checkbox {
|
| 66 | checked: *checked,
|
| 67 | });
|
| 68 | }
|
| 69 | MixedSeg::Text(t) => {
|
| 70 | let parts: Vec<&str> = t.split('\n').collect();
|
| 71 | for (i, part) in parts.iter().enumerate() {
|
| 72 | if i > 0 {
|
| 73 | lines.push(Vec::new());
|
| 74 | }
|
| 75 | if part.is_empty() {
|
| 76 | continue;
|
| 77 | }
|
| 78 | let line = lines.last_mut().unwrap();
|
| 79 | if let Some(MixedSeg::Text(existing)) = line.last_mut() {
|
| 80 | existing.push_str(part);
|
| 81 | } else {
|
| 82 | line.push(MixedSeg::Text((*part).to_string()));
|
| 83 | }
|
| 84 | }
|
| 85 | }
|
| 86 | }
|
| 87 | }
|
| 88 | lines
|
| 89 | }
|
| 90 |
|
| 91 | /// Word-wrap with hanging prefixes (e.g. bullet `* ` / ` `).
|
| 92 | /// Explicit `\n` inside text start a new hanging-wrapped group.
|
| 93 | pub fn wrap_segments_hanging(
|
| 94 | segs: &[MixedSeg],
|
| 95 | max_cols: usize,
|
| 96 | first_prefix: &str,
|
| 97 | next_prefix: &str,
|
| 98 | ) -> Vec<Vec<MixedSeg>> {
|
| 99 | let mut out = Vec::new();
|
| 100 | for paragraph in split_on_newlines(segs) {
|
| 101 | out.extend(wrap_segments_hanging_one(
|
| 102 | ¶graph,
|
| 103 | max_cols,
|
| 104 | first_prefix,
|
| 105 | next_prefix,
|
| 106 | ));
|
| 107 | }
|
| 108 | if out.is_empty() {
|
| 109 | out.push(prefix_line(first_prefix, Vec::new()));
|
| 110 | }
|
| 111 | out
|
| 112 | }
|
| 113 |
|
| 114 | fn wrap_segments_hanging_one(
|
| 115 | segs: &[MixedSeg],
|
| 116 | max_cols: usize,
|
| 117 | first_prefix: &str,
|
| 118 | next_prefix: &str,
|
| 119 | ) -> Vec<Vec<MixedSeg>> {
|
| 120 | let first_budget = max_cols.saturating_sub(first_prefix.chars().count()).max(1);
|
| 121 | let next_budget = max_cols.saturating_sub(next_prefix.chars().count()).max(1);
|
| 122 |
|
| 123 | let atoms = flatten_atoms(segs);
|
| 124 | if atoms.is_empty() {
|
| 125 | return vec![prefix_line(first_prefix, Vec::new())];
|
| 126 | }
|
| 127 |
|
| 128 | let mut lines: Vec<Vec<MixedSeg>> = Vec::new();
|
| 129 | let mut current: Vec<Atom> = Vec::new();
|
| 130 | let mut budget = first_budget;
|
| 131 | let mut is_first = true;
|
| 132 |
|
| 133 | for atom in atoms {
|
| 134 | let atom_w = atom.width();
|
| 135 | let cur_w = atoms_width(¤t);
|
| 136 | let need = if current.is_empty() {
|
| 137 | atom_w
|
| 138 | } else {
|
| 139 | cur_w + 1 + atom_w // space between atoms
|
| 140 | };
|
| 141 |
|
| 142 | if !current.is_empty() && need > budget {
|
| 143 | let prefix = if is_first { first_prefix } else { next_prefix };
|
| 144 | lines.push(prefix_line(prefix, atoms_to_segs(¤t)));
|
| 145 | current.clear();
|
| 146 | budget = next_budget;
|
| 147 | is_first = false;
|
| 148 | current.push(atom);
|
| 149 | } else if current.is_empty() && atom_w > budget {
|
| 150 | // Split overlong text atoms.
|
| 151 | match atom {
|
| 152 | Atom::Word(word) => {
|
| 153 | let mut rest = word;
|
| 154 | while !rest.is_empty() {
|
| 155 | let chunk: String = rest.chars().take(budget).collect();
|
| 156 | let n = chunk.chars().count();
|
| 157 | let prefix = if is_first { first_prefix } else { next_prefix };
|
| 158 | lines.push(prefix_line(prefix, vec![MixedSeg::Text(chunk)]));
|
| 159 | rest = rest.chars().skip(n).collect();
|
| 160 | budget = next_budget;
|
| 161 | is_first = false;
|
| 162 | }
|
| 163 | }
|
| 164 | Atom::Checkbox { checked } => {
|
| 165 | let prefix = if is_first { first_prefix } else { next_prefix };
|
| 166 | lines.push(prefix_line(
|
| 167 | prefix,
|
| 168 | vec![MixedSeg::Checkbox { checked }],
|
| 169 | ));
|
| 170 | budget = next_budget;
|
| 171 | is_first = false;
|
| 172 | }
|
| 173 | }
|
| 174 | } else {
|
| 175 | current.push(atom);
|
| 176 | }
|
| 177 | }
|
| 178 |
|
| 179 | if !current.is_empty() || lines.is_empty() {
|
| 180 | let prefix = if is_first { first_prefix } else { next_prefix };
|
| 181 | lines.push(prefix_line(prefix, atoms_to_segs(¤t)));
|
| 182 | }
|
| 183 | lines
|
| 184 | }
|
| 185 |
|
| 186 | fn prefix_line(prefix: &str, mut segs: Vec<MixedSeg>) -> Vec<MixedSeg> {
|
| 187 | if prefix.is_empty() {
|
| 188 | return segs;
|
| 189 | }
|
| 190 | if let Some(MixedSeg::Text(t)) = segs.first_mut() {
|
| 191 | t.insert_str(0, prefix);
|
| 192 | segs
|
| 193 | } else {
|
| 194 | let mut out = vec![MixedSeg::Text(prefix.to_string())];
|
| 195 | out.append(&mut segs);
|
| 196 | out
|
| 197 | }
|
| 198 | }
|
| 199 |
|
| 200 | #[derive(Clone)]
|
| 201 | enum Atom {
|
| 202 | Word(String),
|
| 203 | Checkbox { checked: bool },
|
| 204 | }
|
| 205 |
|
| 206 | impl Atom {
|
| 207 | fn width(&self) -> usize {
|
| 208 | match self {
|
| 209 | Atom::Word(w) => w.chars().count(),
|
| 210 | Atom::Checkbox { .. } => CHECKBOX_WRAP_COLS,
|
| 211 | }
|
| 212 | }
|
| 213 | }
|
| 214 |
|
| 215 | fn flatten_atoms(segs: &[MixedSeg]) -> Vec<Atom> {
|
| 216 | let mut atoms = Vec::new();
|
| 217 | for seg in segs {
|
| 218 | match seg {
|
| 219 | MixedSeg::Checkbox { checked } => {
|
| 220 | atoms.push(Atom::Checkbox {
|
| 221 | checked: *checked,
|
| 222 | });
|
| 223 | }
|
| 224 | MixedSeg::Text(t) => {
|
| 225 | for word in t.split_whitespace() {
|
| 226 | atoms.push(Atom::Word(word.to_string()));
|
| 227 | }
|
| 228 | }
|
| 229 | }
|
| 230 | }
|
| 231 | atoms
|
| 232 | }
|
| 233 |
|
| 234 | fn atoms_width(atoms: &[Atom]) -> usize {
|
| 235 | if atoms.is_empty() {
|
| 236 | return 0;
|
| 237 | }
|
| 238 | let words: usize = atoms.iter().map(Atom::width).sum();
|
| 239 | words + atoms.len().saturating_sub(1)
|
| 240 | }
|
| 241 |
|
| 242 | fn atoms_to_segs(atoms: &[Atom]) -> Vec<MixedSeg> {
|
| 243 | let mut segs = Vec::new();
|
| 244 | for (i, atom) in atoms.iter().enumerate() {
|
| 245 | if i > 0 {
|
| 246 | if let Some(MixedSeg::Text(t)) = segs.last_mut() {
|
| 247 | t.push(' ');
|
| 248 | } else {
|
| 249 | segs.push(MixedSeg::Text(" ".into()));
|
| 250 | }
|
| 251 | }
|
| 252 | match atom {
|
| 253 | Atom::Word(w) => {
|
| 254 | if let Some(MixedSeg::Text(t)) = segs.last_mut() {
|
| 255 | t.push_str(w);
|
| 256 | } else {
|
| 257 | segs.push(MixedSeg::Text(w.clone()));
|
| 258 | }
|
| 259 | }
|
| 260 | Atom::Checkbox { checked } => {
|
| 261 | segs.push(MixedSeg::Checkbox {
|
| 262 | checked: *checked,
|
| 263 | });
|
| 264 | }
|
| 265 | }
|
| 266 | }
|
| 267 | segs
|
| 268 | }
|
| 269 |
|
| 270 | /// Rasterize a mixed text/checkbox line to a gray bitmap (black ink on white).
|
| 271 | pub fn render_mixed_line(segs: &[MixedSeg]) -> GrayImage {
|
| 272 | let width = line_width_dots(segs).max(1);
|
| 273 | let mut img = GrayImage::from_pixel(width, LINE_HEIGHT, Luma([255]));
|
| 274 | let mut x = 0u32;
|
| 275 | for seg in segs {
|
| 276 | match seg {
|
| 277 | MixedSeg::Text(t) => {
|
| 278 | for ch in t.chars() {
|
| 279 | draw_char(&mut img, x, 0, ch);
|
| 280 | x += CHAR_WIDTH;
|
| 281 | }
|
| 282 | }
|
| 283 | MixedSeg::Checkbox { checked } => {
|
| 284 | let box_img = render_checkbox(*checked);
|
| 285 | blit(&mut img, x, 0, &box_img);
|
| 286 | x += checkbox_width_dots();
|
| 287 | }
|
| 288 | }
|
| 289 | }
|
| 290 | img
|
| 291 | }
|
| 292 |
|
| 293 | pub fn checkbox_width_dots() -> u32 {
|
| 294 | // Side + padding; ~20% larger visual than the original 24-dot cell.
|
| 295 | CHECKBOX_SIDE + 10
|
| 296 | }
|
| 297 |
|
| 298 | fn line_width_dots(segs: &[MixedSeg]) -> u32 {
|
| 299 | let mut w = 0u32;
|
| 300 | for seg in segs {
|
| 301 | match seg {
|
| 302 | MixedSeg::Text(t) => w += t.chars().count() as u32 * CHAR_WIDTH,
|
| 303 | MixedSeg::Checkbox { .. } => w += checkbox_width_dots(),
|
| 304 | }
|
| 305 | }
|
| 306 | w
|
| 307 | }
|
| 308 |
|
| 309 | /// Empty or checked checkbox glyph (box + optional checkmark), vertically centered.
|
| 310 | pub fn render_checkbox(checked: bool) -> GrayImage {
|
| 311 | let w = checkbox_width_dots();
|
| 312 | let mut img = GrayImage::from_pixel(w, LINE_HEIGHT, Luma([255]));
|
| 313 | let side = CHECKBOX_SIDE;
|
| 314 | let x0 = (w.saturating_sub(side)) / 2;
|
| 315 | let y0 = (LINE_HEIGHT.saturating_sub(side)) / 2;
|
| 316 |
|
| 317 | for i in 0..side {
|
| 318 | put_black(&mut img, x0 + i, y0);
|
| 319 | put_black(&mut img, x0 + i, y0 + side - 1);
|
| 320 | put_black(&mut img, x0, y0 + i);
|
| 321 | put_black(&mut img, x0 + side - 1, y0 + i);
|
| 322 | // 2px border
|
| 323 | if i + 1 < side {
|
| 324 | put_black(&mut img, x0 + i, y0 + 1);
|
| 325 | put_black(&mut img, x0 + i, y0 + side - 2);
|
| 326 | put_black(&mut img, x0 + 1, y0 + i);
|
| 327 | put_black(&mut img, x0 + side - 2, y0 + i);
|
| 328 | }
|
| 329 | }
|
| 330 |
|
| 331 | if checked {
|
| 332 | // Checkmark proportions scaled with side (was 14px: 3,7 → 6,10 → 11,3).
|
| 333 | let x1 = x0 + side * 3 / 14;
|
| 334 | let y1 = y0 + side * 7 / 14;
|
| 335 | let x2 = x0 + side * 6 / 14;
|
| 336 | let y2 = y0 + side * 10 / 14;
|
| 337 | let x3 = x0 + side * 11 / 14;
|
| 338 | let y3 = y0 + side * 3 / 14;
|
| 339 | draw_thick_line(&mut img, x1, y1, x2, y2);
|
| 340 | draw_thick_line(&mut img, x2, y2, x3, y3);
|
| 341 | }
|
| 342 | img
|
| 343 | }
|
| 344 |
|
| 345 | fn put_black(img: &mut GrayImage, x: u32, y: u32) {
|
| 346 | if x < img.width() && y < img.height() {
|
| 347 | img.put_pixel(x, y, Luma([0]));
|
| 348 | }
|
| 349 | }
|
| 350 |
|
| 351 | fn draw_thick_line(img: &mut GrayImage, x0: u32, y0: u32, x1: u32, y1: u32) {
|
| 352 | for dx in 0..=1u32 {
|
| 353 | for dy in 0..=1u32 {
|
| 354 | draw_line(img, x0 + dx, y0 + dy, x1 + dx, y1 + dy);
|
| 355 | }
|
| 356 | }
|
| 357 | }
|
| 358 |
|
| 359 | fn draw_line(img: &mut GrayImage, x0: u32, y0: u32, x1: u32, y1: u32) {
|
| 360 | let x0i = x0 as i32;
|
| 361 | let y0i = y0 as i32;
|
| 362 | let x1i = x1 as i32;
|
| 363 | let y1i = y1 as i32;
|
| 364 | let dx = (x1i - x0i).abs();
|
| 365 | let sx = if x0i < x1i { 1 } else { -1 };
|
| 366 | let dy = -(y1i - y0i).abs();
|
| 367 | let sy = if y0i < y1i { 1 } else { -1 };
|
| 368 | let mut err = dx + dy;
|
| 369 | let mut x = x0i;
|
| 370 | let mut y = y0i;
|
| 371 | loop {
|
| 372 | if x >= 0 && y >= 0 {
|
| 373 | put_black(img, x as u32, y as u32);
|
| 374 | }
|
| 375 | if x == x1i && y == y1i {
|
| 376 | break;
|
| 377 | }
|
| 378 | let e2 = 2 * err;
|
| 379 | if e2 >= dy {
|
| 380 | err += dy;
|
| 381 | x += sx;
|
| 382 | }
|
| 383 | if e2 <= dx {
|
| 384 | err += dx;
|
| 385 | y += sy;
|
| 386 | }
|
| 387 | }
|
| 388 | }
|
| 389 |
|
| 390 | fn blit(dst: &mut GrayImage, x0: u32, y0: u32, src: &GrayImage) {
|
| 391 | for y in 0..src.height() {
|
| 392 | for x in 0..src.width() {
|
| 393 | let p = src.get_pixel(x, y).0[0];
|
| 394 | if p < 128 {
|
| 395 | put_black(dst, x0 + x, y0 + y);
|
| 396 | }
|
| 397 | }
|
| 398 | }
|
| 399 | }
|
| 400 |
|
| 401 | fn draw_char(img: &mut GrayImage, x0: u32, y0: u32, ch: char) {
|
| 402 | let glyph = glyph_bitmap(ch);
|
| 403 | // Scale 8×8 → 12×24 with nearest neighbor.
|
| 404 | for sy in 0..LINE_HEIGHT {
|
| 405 | for sx in 0..CHAR_WIDTH {
|
| 406 | let src_x = sx * CHAR_SRC / CHAR_WIDTH;
|
| 407 | let src_y = sy * CHAR_SRC / LINE_HEIGHT;
|
| 408 | let row = glyph[src_y as usize];
|
| 409 | // font8x8_basic: least significant bit is the leftmost pixel.
|
| 410 | let on = (row >> src_x) & 1 == 1;
|
| 411 | if on {
|
| 412 | put_black(img, x0 + sx, y0 + sy);
|
| 413 | }
|
| 414 | }
|
| 415 | }
|
| 416 | }
|
| 417 |
|
| 418 | fn glyph_bitmap(ch: char) -> [u8; 8] {
|
| 419 | let idx = if ch.is_ascii() && (ch as u8) >= 32 {
|
| 420 | (ch as u8 - 32) as usize
|
| 421 | } else {
|
| 422 | // '?' for unsupported
|
| 423 | (b'?' - 32) as usize
|
| 424 | };
|
| 425 | FONT8X8_BASIC.get(idx).copied().unwrap_or(FONT8X8_BASIC[b'?' as usize - 32])
|
| 426 | }
|
| 427 |
|
| 428 | // 8×8 font for ASCII 32..126 (95 glyphs). Row-major, MSB = left.
|
| 429 | // Derived from the common public-domain font8x8_basic set.
|
| 430 | const FONT8X8_BASIC: [[u8; 8]; 95] = [
|
| 431 | [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], //
|
| 432 | [0x18, 0x3C, 0x3C, 0x18, 0x18, 0x00, 0x18, 0x00], // !
|
| 433 | [0x36, 0x36, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], // "
|
| 434 | [0x36, 0x36, 0x7F, 0x36, 0x7F, 0x36, 0x36, 0x00], // #
|
| 435 | [0x0C, 0x3E, 0x03, 0x1E, 0x30, 0x1F, 0x0C, 0x00], // $
|
| 436 | [0x00, 0x63, 0x33, 0x18, 0x0C, 0x66, 0x63, 0x00], // %
|
| 437 | [0x1C, 0x36, 0x1C, 0x6E, 0x3B, 0x33, 0x6E, 0x00], // &
|
| 438 | [0x06, 0x06, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00], // '
|
| 439 | [0x18, 0x0C, 0x06, 0x06, 0x06, 0x0C, 0x18, 0x00], // (
|
| 440 | [0x06, 0x0C, 0x18, 0x18, 0x18, 0x0C, 0x06, 0x00], // )
|
| 441 | [0x00, 0x66, 0x3C, 0xFF, 0x3C, 0x66, 0x00, 0x00], // *
|
| 442 | [0x00, 0x0C, 0x0C, 0x3F, 0x0C, 0x0C, 0x00, 0x00], // +
|
| 443 | [0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x0C, 0x06], // ,
|
| 444 | [0x00, 0x00, 0x00, 0x3F, 0x00, 0x00, 0x00, 0x00], // -
|
| 445 | [0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x0C, 0x00], // .
|
| 446 | [0x60, 0x30, 0x18, 0x0C, 0x06, 0x03, 0x01, 0x00], // /
|
| 447 | [0x3E, 0x63, 0x73, 0x7B, 0x6F, 0x67, 0x3E, 0x00], // 0
|
| 448 | [0x0C, 0x0E, 0x0C, 0x0C, 0x0C, 0x0C, 0x3F, 0x00], // 1
|
| 449 | [0x1E, 0x33, 0x30, 0x1C, 0x06, 0x33, 0x3F, 0x00], // 2
|
| 450 | [0x1E, 0x33, 0x30, 0x1C, 0x30, 0x33, 0x1E, 0x00], // 3
|
| 451 | [0x38, 0x3C, 0x36, 0x33, 0x7F, 0x30, 0x78, 0x00], // 4
|
| 452 | [0x3F, 0x03, 0x1F, 0x30, 0x30, 0x33, 0x1E, 0x00], // 5
|
| 453 | [0x1C, 0x06, 0x03, 0x1F, 0x33, 0x33, 0x1E, 0x00], // 6
|
| 454 | [0x3F, 0x33, 0x30, 0x18, 0x0C, 0x0C, 0x0C, 0x00], // 7
|
| 455 | [0x1E, 0x33, 0x33, 0x1E, 0x33, 0x33, 0x1E, 0x00], // 8
|
| 456 | [0x1E, 0x33, 0x33, 0x3E, 0x30, 0x18, 0x0E, 0x00], // 9
|
| 457 | [0x00, 0x0C, 0x0C, 0x00, 0x00, 0x0C, 0x0C, 0x00], // :
|
| 458 | [0x00, 0x0C, 0x0C, 0x00, 0x00, 0x0C, 0x0C, 0x06], // ;
|
| 459 | [0x18, 0x0C, 0x06, 0x03, 0x06, 0x0C, 0x18, 0x00], // <
|
| 460 | [0x00, 0x00, 0x3F, 0x00, 0x00, 0x3F, 0x00, 0x00], // =
|
| 461 | [0x06, 0x0C, 0x18, 0x30, 0x18, 0x0C, 0x06, 0x00], // >
|
| 462 | [0x1E, 0x33, 0x30, 0x18, 0x0C, 0x00, 0x0C, 0x00], // ?
|
| 463 | [0x3E, 0x63, 0x7B, 0x7B, 0x7B, 0x03, 0x1E, 0x00], // @
|
| 464 | [0x0C, 0x1E, 0x33, 0x33, 0x3F, 0x33, 0x33, 0x00], // A
|
| 465 | [0x3F, 0x66, 0x66, 0x3E, 0x66, 0x66, 0x3F, 0x00], // B
|
| 466 | [0x3C, 0x66, 0x03, 0x03, 0x03, 0x66, 0x3C, 0x00], // C
|
| 467 | [0x1F, 0x36, 0x66, 0x66, 0x66, 0x36, 0x1F, 0x00], // D
|
| 468 | [0x7F, 0x46, 0x16, 0x1E, 0x16, 0x46, 0x7F, 0x00], // E
|
| 469 | [0x7F, 0x46, 0x16, 0x1E, 0x16, 0x06, 0x0F, 0x00], // F
|
| 470 | [0x3C, 0x66, 0x03, 0x03, 0x73, 0x66, 0x7C, 0x00], // G
|
| 471 | [0x33, 0x33, 0x33, 0x3F, 0x33, 0x33, 0x33, 0x00], // H
|
| 472 | [0x1E, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x1E, 0x00], // I
|
| 473 | [0x78, 0x30, 0x30, 0x30, 0x33, 0x33, 0x1E, 0x00], // J
|
| 474 | [0x67, 0x66, 0x36, 0x1E, 0x36, 0x66, 0x67, 0x00], // K
|
| 475 | [0x0F, 0x06, 0x06, 0x06, 0x46, 0x66, 0x7F, 0x00], // L
|
| 476 | [0x63, 0x77, 0x7F, 0x7F, 0x6B, 0x63, 0x63, 0x00], // M
|
| 477 | [0x63, 0x67, 0x6F, 0x7B, 0x73, 0x63, 0x63, 0x00], // N
|
| 478 | [0x1C, 0x36, 0x63, 0x63, 0x63, 0x36, 0x1C, 0x00], // O
|
| 479 | [0x3F, 0x66, 0x66, 0x3E, 0x06, 0x06, 0x0F, 0x00], // P
|
| 480 | [0x1E, 0x33, 0x33, 0x33, 0x3B, 0x1E, 0x38, 0x00], // Q
|
| 481 | [0x3F, 0x66, 0x66, 0x3E, 0x36, 0x66, 0x67, 0x00], // R
|
| 482 | [0x1E, 0x33, 0x07, 0x0E, 0x38, 0x33, 0x1E, 0x00], // S
|
| 483 | [0x3F, 0x2D, 0x0C, 0x0C, 0x0C, 0x0C, 0x1E, 0x00], // T
|
| 484 | [0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x3F, 0x00], // U
|
| 485 | [0x33, 0x33, 0x33, 0x33, 0x33, 0x1E, 0x0C, 0x00], // V
|
| 486 | [0x63, 0x63, 0x63, 0x6B, 0x7F, 0x77, 0x63, 0x00], // W
|
| 487 | [0x63, 0x63, 0x36, 0x1C, 0x1C, 0x36, 0x63, 0x00], // X
|
| 488 | [0x33, 0x33, 0x33, 0x1E, 0x0C, 0x0C, 0x1E, 0x00], // Y
|
| 489 | [0x7F, 0x63, 0x31, 0x18, 0x4C, 0x66, 0x7F, 0x00], // Z
|
| 490 | [0x1E, 0x06, 0x06, 0x06, 0x06, 0x06, 0x1E, 0x00], // [
|
| 491 | [0x03, 0x06, 0x0C, 0x18, 0x30, 0x60, 0x40, 0x00], // \
|
| 492 | [0x1E, 0x18, 0x18, 0x18, 0x18, 0x18, 0x1E, 0x00], // ]
|
| 493 | [0x08, 0x1C, 0x36, 0x63, 0x00, 0x00, 0x00, 0x00], // ^
|
| 494 | [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF], // _
|
| 495 | [0x0C, 0x0C, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00], // `
|
| 496 | [0x00, 0x00, 0x1E, 0x30, 0x3E, 0x33, 0x6E, 0x00], // a
|
| 497 | [0x07, 0x06, 0x06, 0x3E, 0x66, 0x66, 0x3B, 0x00], // b
|
| 498 | [0x00, 0x00, 0x1E, 0x33, 0x03, 0x33, 0x1E, 0x00], // c
|
| 499 | [0x38, 0x30, 0x30, 0x3e, 0x33, 0x33, 0x6E, 0x00], // d
|
| 500 | [0x00, 0x00, 0x1E, 0x33, 0x3f, 0x03, 0x1E, 0x00], // e
|
| 501 | [0x1C, 0x36, 0x06, 0x0f, 0x06, 0x06, 0x0F, 0x00], // f
|
| 502 | [0x00, 0x00, 0x6E, 0x33, 0x33, 0x3E, 0x30, 0x1F], // g
|
| 503 | [0x07, 0x06, 0x36, 0x6E, 0x66, 0x66, 0x67, 0x00], // h
|
| 504 | [0x0C, 0x00, 0x0E, 0x0C, 0x0C, 0x0C, 0x1E, 0x00], // i
|
| 505 | [0x30, 0x00, 0x30, 0x30, 0x30, 0x33, 0x33, 0x1E], // j
|
| 506 | [0x07, 0x06, 0x66, 0x36, 0x1E, 0x36, 0x67, 0x00], // k
|
| 507 | [0x0E, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x1E, 0x00], // l
|
| 508 | [0x00, 0x00, 0x33, 0x7F, 0x7F, 0x6B, 0x63, 0x00], // m
|
| 509 | [0x00, 0x00, 0x1F, 0x33, 0x33, 0x33, 0x33, 0x00], // n
|
| 510 | [0x00, 0x00, 0x1E, 0x33, 0x33, 0x33, 0x1E, 0x00], // o
|
| 511 | [0x00, 0x00, 0x3B, 0x66, 0x66, 0x3E, 0x06, 0x0F], // p
|
| 512 | [0x00, 0x00, 0x6E, 0x33, 0x33, 0x3E, 0x30, 0x78], // q
|
| 513 | [0x00, 0x00, 0x3B, 0x6E, 0x66, 0x06, 0x0F, 0x00], // r
|
| 514 | [0x00, 0x00, 0x3E, 0x03, 0x1E, 0x30, 0x1F, 0x00], // s
|
| 515 | [0x08, 0x0C, 0x3E, 0x0C, 0x0C, 0x2C, 0x18, 0x00], // t
|
| 516 | [0x00, 0x00, 0x33, 0x33, 0x33, 0x33, 0x6E, 0x00], // u
|
| 517 | [0x00, 0x00, 0x33, 0x33, 0x33, 0x1E, 0x0C, 0x00], // v
|
| 518 | [0x00, 0x00, 0x63, 0x6B, 0x7F, 0x7F, 0x36, 0x00], // w
|
| 519 | [0x00, 0x00, 0x63, 0x36, 0x1C, 0x36, 0x63, 0x00], // x
|
| 520 | [0x00, 0x00, 0x33, 0x33, 0x33, 0x3E, 0x30, 0x1F], // y
|
| 521 | [0x00, 0x00, 0x3F, 0x19, 0x0C, 0x26, 0x3F, 0x00], // z
|
| 522 | [0x38, 0x0C, 0x0C, 0x07, 0x0C, 0x0C, 0x38, 0x00], // {
|
| 523 | [0x18, 0x18, 0x18, 0x00, 0x18, 0x18, 0x18, 0x00], // |
|
| 524 | [0x07, 0x0C, 0x0C, 0x38, 0x0C, 0x0C, 0x07, 0x00], // }
|
| 525 | [0x6E, 0x3B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], // ~
|
| 526 | ];
|
| 527 |
|
| 528 | #[cfg(test)]
|
| 529 | mod tests {
|
| 530 | use super::*;
|
| 531 | use crate::dsl::parse;
|
| 532 |
|
| 533 | #[test]
|
| 534 | fn checkbox_has_ink() {
|
| 535 | let empty = render_checkbox(false);
|
| 536 | let checked = render_checkbox(true);
|
| 537 | assert_eq!(empty.dimensions(), checked.dimensions());
|
| 538 | let empty_ink: usize = empty.pixels().filter(|p| p.0[0] < 128).count();
|
| 539 | let checked_ink: usize = checked.pixels().filter(|p| p.0[0] < 128).count();
|
| 540 | assert!(empty_ink > 40, "empty box should draw a border");
|
| 541 | assert!(
|
| 542 | checked_ink > empty_ink,
|
| 543 | "checked box should have more ink than empty"
|
| 544 | );
|
| 545 | }
|
| 546 |
|
| 547 | #[test]
|
| 548 | fn wraps_checkbox_segments() {
|
| 549 | let segs = vec![
|
| 550 | MixedSeg::Checkbox { checked: false },
|
| 551 | MixedSeg::Text("hello world".into()),
|
| 552 | ];
|
| 553 | let lines = wrap_segments(&segs, 10);
|
| 554 | assert!(!lines.is_empty());
|
| 555 | assert!(matches!(lines[0][0], MixedSeg::Checkbox { checked: false }));
|
| 556 | }
|
| 557 |
|
| 558 | #[test]
|
| 559 | fn hard_newlines_keep_checkboxes_on_separate_lines() {
|
| 560 | let doc = parse("[] empty\n[x] done").unwrap();
|
| 561 | let crate::dsl::Block::Paragraph { inlines, .. } = &doc.blocks[0] else {
|
| 562 | panic!("paragraph");
|
| 563 | };
|
| 564 | let segs = segments_from_inlines(inlines);
|
| 565 | let lines = wrap_segments(&segs, 32);
|
| 566 | assert_eq!(lines.len(), 2, "expected 2 hard lines, got {lines:?}");
|
| 567 | assert!(matches!(lines[0][0], MixedSeg::Checkbox { checked: false }));
|
| 568 | assert!(matches!(lines[1][0], MixedSeg::Checkbox { checked: true }));
|
| 569 | }
|
| 570 |
|
| 571 | #[test]
|
| 572 | fn segments_from_parsed_checkboxes() {
|
| 573 | let doc = parse("[] hi [x]").unwrap();
|
| 574 | let crate::dsl::Block::Paragraph { inlines, .. } = &doc.blocks[0] else {
|
| 575 | panic!("paragraph");
|
| 576 | };
|
| 577 | assert!(inlines_have_checkbox(inlines));
|
| 578 | let segs = segments_from_inlines(inlines);
|
| 579 | assert_eq!(
|
| 580 | segs.iter()
|
| 581 | .filter(|s| matches!(s, MixedSeg::Checkbox { .. }))
|
| 582 | .count(),
|
| 583 | 2
|
| 584 | );
|
| 585 | }
|
| 586 |
|
| 587 | #[test]
|
| 588 | fn render_mixed_line_wider_than_checkbox() {
|
| 589 | let segs = vec![
|
| 590 | MixedSeg::Checkbox { checked: true },
|
| 591 | MixedSeg::Text(" Done".into()),
|
| 592 | ];
|
| 593 | let img = render_mixed_line(&segs);
|
| 594 | assert!(img.width() > checkbox_width_dots());
|
| 595 | assert_eq!(img.height(), LINE_HEIGHT);
|
| 596 | }
|
| 597 | }
|