escpos_md/command/
mod.rs

1mod char_magnification;
2mod charset;
3mod code_table;
4mod font;
5
6pub use char_magnification::CharMagnification;
7pub use charset::Charset;
8pub use code_table::CodeTable;
9pub use font::Font;
10
11#[derive(Debug, Clone, Copy, Eq, PartialEq)]
12#[repr(u8)]
13pub enum UnderlineThickness {
14    Off = 0,
15    OneDot = 1,
16    TwoDot = 2,
17}
18
19impl Default for UnderlineThickness {
20    fn default() -> Self {
21        Self::Off
22    }
23}
24
25#[derive(Debug, Clone, Copy, Eq, PartialEq)]
26#[repr(u8)]
27pub enum Justification {
28    Left = 0,
29    Center = 1,
30    Right = 2,
31}
32
33impl Default for Justification {
34    fn default() -> Self {
35        Self::Left
36    }
37}
38
39/// Common commands usefull for the printer
40#[derive(Clone, Debug, Copy)]
41pub enum Command {
42    /// Cuts the paper after 0x96 vertical spaces
43    Cut,
44    /// Equivalent to ESC @
45    Init,
46    /// Print mode selected to init the fonts. Equivalent to ESC ! 0
47    PrintModeDefault,
48    /// Set an international character set, Equivalent to ESC R
49    Charset(Charset),
50    /// Selects a different code table, Equivalent to ESC t
51    CodeTable(CodeTable),
52    /// Sets up a font. Equivalent to ESC M
53    Font(Font),
54    Underline(UnderlineThickness),
55    Bold(bool),
56    DoubleStrike(bool),
57    WhiteBlackReverse(bool),
58    /// Equivalent to ESC * m = 0
59    Bitmap,
60    /// Change line size
61    FeedPaper(u8),
62    FeedLines(u8),
63    LineSpacing(u8),
64    DefaultLineSpacing,
65    CharSpacing(u8),
66    CharSize(CharMagnification),
67    SplitWords(bool),
68    LeftMargin(u16),
69    Justification(Justification),
70}
71
72impl Command {
73    /// Returns the byte-array representation of each command
74    pub fn as_bytes(&self) -> Vec<u8> {
75        match self {
76            Command::Cut => vec![0x1d, 0x56, 0x41, 0x96],
77            Command::Init => vec![0x1d, 0x40],
78            Command::PrintModeDefault => vec![0x01b, 0x21, 0x00],
79            Command::Charset(charset) => {
80                let mut res = vec![0x1b, 0x52];
81                res.append(&mut charset.as_bytes());
82                res
83            }
84            Command::CodeTable(code_table) => {
85                let mut res = vec![0x1b, 0x74];
86                res.append(&mut code_table.as_bytes());
87                res
88            }
89            Command::Font(font) => {
90                let mut res = vec![0x1b, 0x4d];
91                res.append(&mut font.as_bytes());
92                res
93            }
94            Command::Underline(thickness) => vec![0x1b, 0x2d, *thickness as u8],
95            Command::Bold(bold) => vec![0x1b, 0x45, *bold as u8],
96            Command::DoubleStrike(double_strike) => vec![0x1b, 0x47, *double_strike as u8],
97            Command::WhiteBlackReverse(reverse) => vec![0x1d, 0x42, *reverse as u8],
98            Command::Bitmap => vec![0x1b, 0x2a],
99            Command::FeedPaper(units) => vec![0x1b, 0x4a, *units],
100            Command::FeedLines(lines) => vec![0x1b, 0x64, *lines],
101            Command::LineSpacing(units) => vec![0x1b, 0x33, *units],
102            Command::DefaultLineSpacing => vec![0x1b, 0x32],
103            Command::CharSpacing(units) => vec![0x1b, 0x20, *units],
104            Command::CharSize(magnification) => vec![0x1d, 0x21, magnification.to_byte()],
105            Command::SplitWords(_) => vec![],
106            Command::LeftMargin(margin) => {
107                let mut res = vec![0x1d, 0x4c];
108                res.append(&mut margin.to_le_bytes().to_vec());
109                res
110            }
111            Command::Justification(justification) => vec![0x1b, 0x61, *justification as u8],
112        }
113    }
114}