1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use std::{io, str::FromStr};
use printer::PrinterKind;
use thiserror::*;
pub mod editor;
pub mod encoding;
pub mod printer;
#[derive(Copy, Clone)]
pub struct UseTable {
pub chars: [usize; 128],
}
impl UseTable {
pub fn first_last(&self) -> Option<(u8, u8)> {
let mut iter = self.chars.iter();
let first_char = iter.position(|x| *x > 0)? as u8;
let last_char = iter.rposition(|x| *x > 0).map(|x| x + 1).unwrap_or(0) as u8 + first_char;
Some((first_char, last_char))
}
}
impl From<&str> for UseTable {
fn from(text: &str) -> UseTable {
let mut chars = [0; 128];
for c in text.chars() {
chars[c as usize] += 1;
}
Self { chars }
}
}
impl UseTable {
pub const fn new() -> Self {
Self { chars: [0; 128] }
}
}
pub struct UseMatrix {
pub csets: [UseTable; 8],
}
impl UseMatrix {
pub const fn new() -> Self {
Self {
csets: [UseTable::new(); 8],
}
}
}
pub struct UseTableVec {
pub csets: Vec<UseTable>,
}
impl Default for UseTableVec {
fn default() -> Self {
Self::new()
}
}
impl UseTableVec {
pub fn new() -> Self {
UseTableVec {
csets: Vec::with_capacity(8),
}
}
pub fn append(&mut self, chsets: &[Option<usize>; 8], use_matrix: UseMatrix) {
for (cset, use_table) in use_matrix.csets.iter().enumerate() {
if let Some(index) = chsets.get(cset).and_then(|x| *x) {
while self.csets.len() + 1 < index {
self.csets.push(UseTable::new());
}
if self.csets.len() == index {
self.csets.push(*use_table);
} else {
for (left, right) in self.csets[index]
.chars
.iter_mut()
.zip(use_table.chars.iter())
{
*left += *right
}
}
}
}
}
}
#[derive(Debug, Copy, Clone)]
pub enum FontKind {
Editor,
Printer(PrinterKind),
}
impl FontKind {
pub fn scale_y(&self, units: u16) -> u32 {
match self {
Self::Editor => u32::from(units) * 2,
Self::Printer(pk) => pk.scale_y(units),
}
}
pub fn scale_x(&self, units: u16) -> u32 {
match self {
Self::Editor => u32::from(units),
Self::Printer(pk) => pk.scale_x(units),
}
}
pub fn baseline(&self) -> i16 {
match self {
Self::Editor => 18,
Self::Printer(pk) => pk.baseline(),
}
}
pub fn resolution(&self) -> (isize, isize) {
match self {
Self::Editor => (104, 90),
Self::Printer(PrinterKind::Needle9) => (216, 216),
Self::Printer(PrinterKind::Needle24) => (360, 360),
Self::Printer(PrinterKind::Laser30) => (300, 300),
}
}
pub fn scale(&self) -> f32 {
match self {
Self::Printer(pk) => pk.scale(),
Self::Editor => todo!(),
}
}
pub fn extension(&self) -> &'static str {
match self {
Self::Editor => "E24",
Self::Printer(p) => p.extension(),
}
}
}
#[derive(Debug, Error)]
#[error("Unknown print driver!")]
pub struct UnknownFontKind {}
impl FromStr for FontKind {
type Err = UnknownFontKind;
fn from_str(input: &str) -> Result<Self, Self::Err> {
match input {
"P09" => Ok(Self::Printer(PrinterKind::Needle9)),
"E24" => Ok(Self::Editor),
"P24" => Ok(Self::Printer(PrinterKind::Needle24)),
"L30" => Ok(Self::Printer(PrinterKind::Laser30)),
_ => Err(UnknownFontKind {}),
}
}
}
#[derive(Debug, Error)]
pub enum LoadError {
#[error("Failed IO")]
Io(#[from] io::Error),
#[error("Parsing failed: {0}")]
Parse(String),
}