escpos_db/
lib.rs

1//! Rust bindings to the [ESC/POS printer database](https://github.com/receipt-print-hq/escpos-printer-db).
2
3#![deny(missing_docs)]
4
5use std::borrow::Cow;
6
7#[rustfmt::skip]
8mod gen;
9mod int_map;
10
11pub use crate::gen::*;
12pub use crate::int_map::*;
13
14impl Encoding {
15    /// This encoding's 7-bit codepage.
16    pub fn data(&self) -> Option<&'static [char; 128]> {
17        gen::encoding_data(*self)
18    }
19}
20
21/// A profile with capability information for an ESC/POS printer.
22#[derive(Debug)]
23#[non_exhaustive]
24pub struct Profile<'a> {
25    /// The name of this printer.
26    pub name: Cow<'a, str>,
27    /// The vendor or manufacturer of this printer.
28    pub vendor: Cow<'a, str>,
29    /// Specific ESC/POS features supported by this printer.
30    pub features: Features,
31    /// Supported code pages.
32    pub code_pages: Cow<'a, IntMap<Encoding>>,
33    /// The ink colors supported by this printer.
34    pub colors: Cow<'a, IntMap<Color>>,
35    /// Information about the character fonts supported by this printer.
36    pub fonts: Cow<'a, IntMap<FontInfo>>,
37    /// Print media information for this printer.
38    pub media: Media,
39}
40
41impl<'a> Profile<'a> {
42    /// Create a new profile.
43    ///
44    /// All fields other than `name` and `vendor` are set to their default values.
45    pub const fn new(name: Cow<'a, str>, vendor: Cow<'a, str>) -> Self {
46        Self {
47            name,
48            vendor,
49            features: Features::new(),
50            code_pages: Cow::Borrowed(IntMap::empty()),
51            colors: Cow::Borrowed(IntMap::empty()),
52            fonts: Cow::Borrowed(IntMap::empty()),
53            media: Media::new(None, None),
54        }
55    }
56
57    /// Set [`Self::features`] to `features`
58    pub const fn with_features(mut self, features: Features) -> Self {
59        self.features = features;
60        self
61    }
62
63    /// Set [`Self::code_pages`] to `code_pages`
64    // TODO: make const once that's possible
65    pub fn with_code_pages(mut self, code_pages: Cow<'a, IntMap<Encoding>>) -> Self {
66        self.code_pages = code_pages;
67        self
68    }
69
70    /// Set [`Self::colors`] to `colors`
71    // TODO: make const once that's possible
72    pub fn with_colors(mut self, colors: Cow<'a, IntMap<Color>>) -> Self {
73        self.colors = colors;
74        self
75    }
76
77    /// Set [`Self::fonts`] to `fonts`
78    // TODO: make const once that's possible
79    pub fn with_fonts(mut self, fonts: Cow<'a, IntMap<FontInfo>>) -> Self {
80        self.fonts = fonts;
81        self
82    }
83
84    /// Set [`Self::media`] to `media`
85    pub const fn with_media(mut self, media: Media) -> Self {
86        self.media = media;
87        self
88    }
89}
90
91/// An ink color supported by a printer profile.
92#[derive(Copy, Clone, Debug)]
93#[allow(missing_docs)]
94pub enum Color {
95    Black,
96    Red,
97    Alternate,
98}
99
100/// Information for a supported ESC/POS font.
101#[derive(Debug)]
102#[non_exhaustive]
103pub struct FontInfo {
104    /// The maximum number of characters that can fit on a line, using this font.
105    pub columns: u8,
106}
107
108/// The specific ESC/POS features that are supported by a printer profile.
109#[derive(Copy, Clone, Debug, Default)]
110pub struct Features(FeaturesInner);
111
112impl Features {
113    const fn _with(mut self, flag: FeaturesInner, on: bool) -> Self {
114        self.0 = if on {
115            self.0.union(flag)
116        } else {
117            self.0.difference(flag)
118        };
119        self
120    }
121}
122
123/// Print media information for a printer profile.
124#[derive(Debug, Default)]
125#[non_exhaustive]
126pub struct Media {
127    /// The pixel density of this printer in dots per inch.
128    pub dpi: Option<u16>,
129    /// The print width of this printer.
130    pub width: Option<Width>,
131}
132
133impl Media {
134    /// Create a new `Media` with the given dpi and width.
135    pub const fn new(dpi: Option<u16>, width: Option<Width>) -> Self {
136        Self { dpi, width }
137    }
138}
139
140/// The supported print width for a printer profile.
141#[derive(Debug)]
142#[non_exhaustive]
143pub struct Width {
144    /// The print width in millimeters.
145    pub mm: f32,
146    /// The print width in pixels.
147    pub px: u16,
148}
149
150impl Width {
151    /// Create a new `Width` with the given millimeter and pixel width values.
152    pub fn new(mm: f32, px: u16) -> Self {
153        Self { mm, px }
154    }
155}