Skip to main content

hardless_render/
lib.rs

1#![doc = include_str!("../README.md")]
2#![forbid(unsafe_code)]
3#![deny(missing_docs)]
4
5mod codes;
6mod html;
7mod layout;
8#[cfg(feature = "png")]
9mod png;
10mod svg;
11mod text;
12
13use serde::{Deserialize, Serialize};
14
15pub use html::{render_html, render_html_document, STYLESHEET};
16#[cfg(feature = "png")]
17pub use png::{render_png, PngError, MAX_PNG_HEIGHT};
18pub use svg::render_svg;
19pub use text::{to_text, to_text_with};
20
21/// Licence of the bundled Go Mono font (BSD-3-Clause), which the PNG output embeds. Ship it
22/// with any binary built with the `png` feature.
23pub const FONT_LICENSE: &str = include_str!("../fonts/LICENSE");
24
25/// Paper roll width.
26///
27/// ```
28/// use hardless_render::PaperWidth;
29/// assert_eq!(PaperWidth::Mm80.columns(), (48, 64));
30/// assert_eq!(serde_json::to_string(&PaperWidth::Mm58).unwrap(), "58");
31/// ```
32#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
33#[serde(try_from = "u16", into = "u16")]
34pub enum PaperWidth {
35    /// 58 mm: 384 dots, 32 columns in Font A, 42 in Font B.
36    Mm58,
37    /// 80 mm: 576 dots, 48 columns in Font A, 64 in Font B (the default).
38    #[default]
39    Mm80,
40}
41
42impl PaperWidth {
43    /// Columns per line in Font A and Font B.
44    pub fn columns(self) -> (u32, u32) {
45        match self {
46            PaperWidth::Mm58 => (32, 42),
47            PaperWidth::Mm80 => (48, 64),
48        }
49    }
50
51    /// The width in millimetres.
52    pub fn millimetres(self) -> u16 {
53        match self {
54            PaperWidth::Mm58 => 58,
55            PaperWidth::Mm80 => 80,
56        }
57    }
58
59    /// The printable width in dots: 576 for 80 mm, 384 for 58 mm (12 dots per Font A column).
60    pub fn dots(self) -> u32 {
61        self.columns().0 * layout::DOTS_PER_COLUMN
62    }
63}
64
65impl TryFrom<u16> for PaperWidth {
66    type Error = String;
67
68    fn try_from(mm: u16) -> Result<Self, String> {
69        match mm {
70            58 => Ok(PaperWidth::Mm58),
71            80 => Ok(PaperWidth::Mm80),
72            other => Err(format!("paper width must be 58 or 80, not {other}")),
73        }
74    }
75}
76
77impl From<PaperWidth> for u16 {
78    fn from(paper: PaperWidth) -> u16 {
79        paper.millimetres()
80    }
81}
82
83/// Rendering options.
84#[derive(Clone, Copy, Debug, PartialEq, Eq)]
85pub struct RenderOptions {
86    /// Paper width (default 80 mm).
87    pub paper: PaperWidth,
88    /// Show unknown commands as small badges in HTML (default on; FR-12).
89    pub show_unknown: bool,
90}
91
92impl Default for RenderOptions {
93    fn default() -> Self {
94        Self {
95            paper: PaperWidth::Mm80,
96            show_unknown: true,
97        }
98    }
99}