-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
301 lines (274 loc) · 7.76 KB
/
lib.rs
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
//! Parsers for the TeX font metric (.tfm) and property list (.pl) file formats
#[cfg(test)]
mod examples;
pub mod format;
pub mod ligkern;
pub mod pl;
/// The TFM header, which contains metadata about the file.
///
/// This is defined in TFtoPL.2014.10.
#[derive(Debug, Default, PartialEq, Eq)]
pub struct Header {
/// In TeX82, this is stored in the `font_check` array (TeX82.2021.549).
pub checksum: u32,
/// In TeX82, this is stored in the `font_dsize` array (TeX82.2021.549).
pub design_size: Number,
pub character_coding_scheme: String,
pub font_family: String,
pub seven_bit_safe: Option<bool>,
pub face: Option<Face>,
/// The TFM format allows the header to contain arbitrary additional data.
pub additional_data: Vec<u32>,
}
impl Header {
/// Returns the default header when parsing property list files.
///
/// This is defined in PLtoTF.2014.70.
pub fn property_list_default() -> Header {
Header {
checksum: 0,
design_size: Number::UNITY * 10,
character_coding_scheme: "UNSPECIFIED".into(),
font_family: "UNSPECIFIED".into(),
seven_bit_safe: Some(false),
face: Some(0.into()),
additional_data: vec![],
}
}
}
/// A character in a TFM file.
///
/// TFM and PL files only support 1-byte characters.
#[derive(Debug, Default, PartialEq, Eq, Hash, Clone, Copy, PartialOrd, Ord)]
pub struct Char(pub u8);
impl From<u8> for Char {
fn from(value: u8) -> Self {
Char(value)
}
}
impl TryFrom<char> for Char {
type Error = std::char::TryFromCharError;
fn try_from(value: char) -> Result<Self, Self::Error> {
let u: u8 = value.try_into()?;
Ok(Char(u))
}
}
/// Fixed-width numeric type used in TFM files.
///
/// This numeric type has 11 bits for the integer part,
/// 20 bits for the fractional part, and a single signed bit.
/// The inner value is the number multiplied by 2^20.
/// It is called a `fix_word` in TFtoPL.
///
/// In property list files, this type is represented as a decimal number
/// with up to 6 digits after the decimal point.
/// This is a non-lossy representation
/// because 10^(-6) is larger than 2^(-20).
#[derive(Default, PartialEq, Eq, Debug, Copy, Clone, PartialOrd, Ord, Hash)]
pub struct Number(pub i32);
impl Number {
/// Representation of the number 0 as a [Number].
pub const ZERO: Number = Number(0);
/// Representation of the number 1 as a [Number].
pub const UNITY: Number = Number(1 << 20);
}
impl std::ops::Add<Number> for Number {
type Output = Number;
fn add(self, rhs: Number) -> Self::Output {
Number(self.0 + rhs.0)
}
}
impl std::ops::Sub<Number> for Number {
type Output = Number;
fn sub(self, rhs: Number) -> Self::Output {
Number(self.0 - rhs.0)
}
}
impl std::ops::Mul<i32> for Number {
type Output = Number;
fn mul(self, rhs: i32) -> Self::Output {
Number(self.0 * rhs)
}
}
impl std::ops::Div<i32> for Number {
type Output = Number;
fn div(self, rhs: i32) -> Self::Output {
Number(self.0 / rhs)
}
}
impl std::fmt::Display for Number {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// TFtoPL.2014.40-43
if self.0 < 0 {
write!(f, "-")?;
}
let integer_part = self.0 / Number::UNITY.0;
write!(f, "{integer_part}.")?;
let mut fp = (self.0 % Number::UNITY.0).abs();
fp = 10 * fp + 5;
let mut delta = 10;
loop {
if delta > 0o4_000_000 {
fp = fp + 0o2_000_000 - delta / 2;
}
write!(f, "{}", fp / 0o4_000_000)?;
fp = 10 * (fp % 0o4_000_000);
delta *= 10;
if fp <= delta {
break;
}
}
Ok(())
}
}
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum FaceWeight {
Light,
Medium,
Bold,
}
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum FaceSlope {
Roman,
Italic,
}
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum FaceExpansion {
Regular,
Condensed,
Extended,
}
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum Face {
Valid(FaceWeight, FaceSlope, FaceExpansion),
Other(u8),
}
impl From<u8> for Face {
fn from(value: u8) -> Self {
if value >= 18 {
return Face::Other(value);
}
let a = match (value % 6) / 2 {
0 => FaceWeight::Medium,
1 => FaceWeight::Bold,
2 => FaceWeight::Light,
_ => unreachable!(),
};
let b = match value % 2 {
0 => FaceSlope::Roman,
1 => FaceSlope::Italic,
_ => unreachable!(),
};
let c = match value / 6 {
0 => FaceExpansion::Regular,
1 => FaceExpansion::Condensed,
2 => FaceExpansion::Extended,
_ => unreachable!(),
};
Face::Valid(a, b, c)
}
}
impl From<Face> for u8 {
fn from(value: Face) -> Self {
match value {
Face::Valid(w, s, c) => {
let a: u8 = match w {
FaceWeight::Medium => 0,
FaceWeight::Bold => 1,
FaceWeight::Light => 2,
};
let b: u8 = match s {
FaceSlope::Roman => 0,
FaceSlope::Italic => 1,
};
let c: u8 = match c {
FaceExpansion::Regular => 0,
FaceExpansion::Condensed => 1,
FaceExpansion::Extended => 2,
};
c * 6 + a * 2 + b
}
Face::Other(b) => b,
}
}
}
/// TeX font metric parameters
#[derive(Debug, Default, PartialEq, Eq)]
pub struct Params(pub Vec<Number>);
impl Params {
pub fn set(&mut self, number: usize, value: Number) {
if self.0.len() <= number {
self.0.resize(number, Default::default());
}
self.0[number - 1] = value;
}
pub fn set_named(&mut self, named_param: NamedParam, value: Number) {
self.set(named_param.number() as usize, value)
}
}
/// A named TeX font metric parameter.
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
pub enum NamedParam {
Slant,
Space,
Stretch,
Shrink,
XHeight,
Quad,
ExtraSpace,
Num1,
Num2,
Num3,
Denom1,
Denom2,
Sup1,
Sup2,
Sup3,
Sub1,
Sub2,
SupDrop,
SubDrop,
Delim1,
Delim2,
AxisHeight,
DefaultRuleThickness,
BigOpSpacing1,
BigOpSpacing2,
BigOpSpacing3,
BigOpSpacing4,
BigOpSpacing5,
}
impl NamedParam {
pub fn number(&self) -> u8 {
match self {
NamedParam::Slant => 1,
NamedParam::Space => 2,
NamedParam::Stretch => 3,
NamedParam::Shrink => 4,
NamedParam::XHeight => 5,
NamedParam::Quad => 6,
NamedParam::ExtraSpace => 7,
NamedParam::Num1 => 8,
NamedParam::Num2 => 9,
NamedParam::Num3 => 10,
NamedParam::Denom1 => 11,
NamedParam::Denom2 => 12,
NamedParam::Sup1 => 13,
NamedParam::Sup2 => 14,
NamedParam::Sup3 => 15,
NamedParam::Sub1 => 16,
NamedParam::Sub2 => 17,
NamedParam::SupDrop => 18,
NamedParam::SubDrop => 19,
NamedParam::Delim1 => 20,
NamedParam::Delim2 => 21,
NamedParam::AxisHeight => 22,
NamedParam::DefaultRuleThickness => 8,
NamedParam::BigOpSpacing1 => 9,
NamedParam::BigOpSpacing2 => 10,
NamedParam::BigOpSpacing3 => 11,
NamedParam::BigOpSpacing4 => 12,
NamedParam::BigOpSpacing5 => 13,
}
}
}