copper-bdf-parser 0.1.1

BDF font parser
Documentation
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
//! BDF parser.

#![warn(missing_docs)]
#![deny(unsafe_code)]
#![deny(missing_debug_implementations)]

mod glyph;
mod metadata;
mod parser;
mod properties;

pub use glyph::{Encoding, Glyph, Glyphs};
pub use metadata::{Metadata, MetricsSet};
pub use parser::ParserError;
pub use properties::{Properties, Property, PropertyType};

use crate::parser::{Line, Lines};

/// BDF Font.
#[derive(Debug, Clone, PartialEq)]
pub struct Font {
    /// Font metadata.
    pub metadata: Metadata,

    /// Glyphs.
    pub glyphs: Glyphs,

    /// Metrics.
    pub metrics: Metrics,
}

impl Font {
    /// Parses a BDF file.
    pub fn parse(input: &str) -> Result<Self, ParserError> {
        let mut lines = Lines::new(input);

        let first_line = lines
            .next()
            .ok_or_else(|| ParserError::new("empty input"))?;

        if first_line.keyword != "STARTFONT" || first_line.parameters != "2.1" {
            return Err(ParserError::with_line(
                "expected \"STARTFONT 2.1\"",
                &first_line,
            ));
        }

        let metadata = Metadata::parse(&mut lines)?;
        let glyphs = Glyphs::parse(&mut lines, &metadata)?;
        let metrics = Metrics::new(&metadata, &glyphs)?;

        Ok(Font {
            metadata,
            glyphs,
            metrics,
        })
    }
}

/// Bounding box.
#[derive(Debug, Default, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct BoundingBox {
    /// Offset to the lower left corner of the bounding box.
    pub offset: Coord,

    /// Size of the bounding box.
    pub size: Coord,
}

impl BoundingBox {
    pub(crate) fn parse(line: &Line<'_>) -> Option<Self> {
        let [size_x, size_y, offset_x, offset_y] = line.parse_integer_parameters()?;

        Some(Self {
            offset: Coord::new(offset_x, offset_y),
            size: Coord::new(size_x, size_y),
        })
    }

    fn upper_right(&self) -> Coord {
        Coord::new(
            self.offset.x + self.size.x - 1,
            self.offset.y + self.size.y - 1,
        )
    }

    /// Calculates the smallest bounding box that surrounds two bounding boxes.
    ///
    /// # Panics
    ///
    /// Panics if any bounding box has a negative size.
    pub fn union(&self, other: &Self) -> Self {
        assert!(self.size.x >= 0);
        assert!(self.size.y >= 0);
        assert!(other.size.x >= 0);
        assert!(other.size.y >= 0);

        if other.size.x == 0 || other.size.y == 0 {
            *self
        } else if self.size.x == 0 || self.size.y == 0 {
            *other
        } else {
            let self_ur = self.upper_right();
            let other_ur = other.upper_right();

            let x_min = self.offset.x.min(other.offset.x);
            let y_min = self.offset.y.min(other.offset.y);
            let x_max = self_ur.x.max(other_ur.x);
            let y_max = self_ur.y.max(other_ur.y);

            Self {
                offset: Coord::new(x_min, y_min),
                size: Coord::new(x_max - x_min + 1, y_max - y_min + 1),
            }
        }
    }
}

/// Coordinate.
///
/// BDF files use a cartesian coordinate system, where the positive half-axis points upwards.
#[derive(Debug, Default, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct Coord {
    /// X coordinate.
    pub x: i32,

    /// Y coordinate.
    pub y: i32,
}

impl Coord {
    /// Creates a new coord.
    pub const fn new(x: i32, y: i32) -> Self {
        Self { x, y }
    }

    pub(crate) fn parse(line: &Line<'_>) -> Option<Self> {
        let [x, y] = line.parse_integer_parameters()?;

        Some(Self { x, y })
    }
}

/// Metrics.
#[derive(Debug, Default, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct Metrics {
    /// Ascent above the baseline in pixels.
    pub ascent: u32,

    /// Descent above the baseline in pixels.
    pub descent: u32,
}

impl Metrics {
    fn new(metadata: &Metadata, glyphs: &Glyphs) -> Result<Self, ParserError> {
        let ascent = metadata
            .properties
            .try_get::<u32>(Property::FontAscent)
            .map_err(|_| ParserError::new("invalid value for FONT_ASCENT property"))?
            .unwrap_or_else(|| glyphs.approximate_ascent());

        let descent = metadata
            .properties
            .try_get::<u32>(Property::FontDescent)
            .map_err(|_| ParserError::new("invalid value for FONT_DESCENT property"))?
            .unwrap_or_else(|| glyphs.approximate_descent());

        Ok(Self { ascent, descent })
    }

    /// Gets the line height in pixels.
    pub const fn line_height(&self) -> u32 {
        self.ascent + self.descent
    }
}

#[cfg(test)]
mod tests {
    use crate::{glyph::GlyphWidth, properties::PropertyValue};

    use super::*;
    use indoc::indoc;

    #[track_caller]
    pub(crate) fn assert_parser_error(input: &str, message: &str, line_number: Option<usize>) {
        assert_eq!(
            Font::parse(input),
            Err(ParserError {
                message: message.to_string(),
                line_number,
            })
        );
    }
    const FONT: &str = indoc! {r#"
        STARTFONT 2.1
        FONT "test font"
        SIZE 16 75 75
        FONTBOUNDINGBOX 16 24 0 0
        STARTPROPERTIES 3
        COPYRIGHT "Copyright123"
        FONT_ASCENT 1
        COMMENT comment
        FONT_DESCENT 2
        ENDPROPERTIES
        CHARS 2
        STARTCHAR Char 0
        ENCODING 64
        SWIDTH 480 0
        DWIDTH 8 0
        BBX 8 8 0 0
        BITMAP
        1f
        01
        ENDCHAR
        STARTCHAR Char 1
        ENCODING 65
        SWIDTH 480 0
        DWIDTH 8 0
        BBX 8 8 0 0
        BITMAP
        2f
        02
        ENDCHAR
        ENDFONT
    "#};

    fn test_font(font: &Font) {
        assert_eq!(
            font.metadata,
            Metadata {
                name: String::from("\"test font\""),
                point_size: 16,
                resolution: Coord::new(75, 75),
                bounding_box: BoundingBox {
                    size: Coord::new(16, 24),
                    offset: Coord::new(0, 0),
                },
                metrics_set: MetricsSet::Horizontal,
                properties: Properties::new(
                    [
                        (
                            "COPYRIGHT".to_string(),
                            PropertyValue::Text("Copyright123".to_string()),
                        ),
                        ("FONT_ASCENT".to_string(), PropertyValue::Int(1)),
                        ("FONT_DESCENT".to_string(), PropertyValue::Int(2)),
                    ]
                    .into_iter()
                    .collect(),
                )
            }
        );

        assert_eq!(
            font.glyphs.iter().cloned().collect::<Vec<_>>(),
            vec![
                Glyph {
                    bitmap: vec![0x1f, 0x01],
                    bounding_box: BoundingBox {
                        size: Coord::new(8, 8),
                        offset: Coord::new(0, 0),
                    },
                    encoding: Encoding::Standard(64), // '@'
                    name: "Char 0".to_string(),
                    width_horizontal: Some(GlyphWidth {
                        device: Coord::new(8, 0),
                        scalable: Coord::new(480, 0),
                    }),
                    width_vertical: None,
                    origin_offset: None,
                },
                Glyph {
                    bitmap: vec![0x2f, 0x02],
                    bounding_box: BoundingBox {
                        size: Coord::new(8, 8),
                        offset: Coord::new(0, 0),
                    },
                    encoding: Encoding::Standard(65), // 'A'
                    name: "Char 1".to_string(),
                    width_horizontal: Some(GlyphWidth {
                        device: Coord::new(8, 0),
                        scalable: Coord::new(480, 0),
                    }),
                    width_vertical: None,
                    origin_offset: None,
                },
            ],
        );
    }

    #[test]
    fn parse_font() {
        test_font(&Font::parse(FONT).unwrap())
    }

    #[test]
    fn parse_font_without_endfont() {
        let lines: Vec<_> = FONT
            .lines()
            .filter(|line| !line.contains("ENDFONT"))
            .collect();
        let input = lines.join("\n");

        test_font(&Font::parse(&input).unwrap());
    }

    #[test]
    fn parse_font_without_chars() {
        let lines: Vec<_> = FONT
            .lines()
            .filter(|line| !line.contains("CHARS"))
            .collect();
        let input = lines.join("\n");

        test_font(&Font::parse(&input).unwrap());
    }

    #[test]
    fn parse_font_missing_swidth() {
        let lines: Vec<_> = FONT
            .lines()
            .filter(|line| !line.contains("SWIDTH"))
            .collect();
        let input = lines.join("\n");

        test_font(&Font::parse(&input).unwrap());
    }

    #[test]
    fn parse_font_with_windows_line_endings() {
        let lines: Vec<_> = FONT.lines().collect();
        let input = lines.join("\r\n");

        test_font(&Font::parse(&input).unwrap());
    }

    #[test]
    fn parse_empty_font() {
        assert_parser_error("", "empty input", None);
    }

    // TODO: Should it be OK to have garbage after ENDFONT?
    #[test]
    #[ignore]
    fn parse_font_with_garbage_after_endfont() {
        let lines: Vec<_> = FONT.lines().chain(std::iter::once("Invalid")).collect();
        let input = lines.join("\n");

        assert_parser_error(&input, "expected end of input", Some(28));
    }

    #[test]
    fn parse_with_leading_whitespace() {
        let lines: Vec<_> = std::iter::once("").chain(FONT.lines()).collect();
        let input = lines.join("\n");

        test_font(&Font::parse(&input).unwrap());
    }

    #[test]
    fn invalid_first_line() {
        let input = "\nSOMETHING 2.1";
        assert_parser_error(input, "expected \"STARTFONT 2.1\"", Some(2));
    }

    #[test]
    fn missing_font_name() {
        let input = "STARTFONT 2.1\n";
        assert_parser_error(input, "missing \"FONT\"", None);
    }

    const fn bb(offset_x: i32, offset_y: i32, size_x: i32, size_y: i32) -> BoundingBox {
        BoundingBox {
            offset: Coord::new(offset_x, offset_y),
            size: Coord::new(size_x, size_y),
        }
    }

    #[test]
    fn union() {
        for ((bb1, bb2), expected_union) in [
            // Non overlapping
            ((bb(0, 0, 4, 5), bb(4, 0, 4, 5)), bb(0, 0, 8, 5)),
            ((bb(0, 0, 4, 5), bb(5, 0, 4, 5)), bb(0, 0, 9, 5)),
            ((bb(0, 0, 4, 5), bb(-4, 0, 4, 5)), bb(-4, 0, 8, 5)),
            ((bb(0, 0, 4, 5), bb(-6, 0, 4, 5)), bb(-6, 0, 10, 5)),
            ((bb(0, 0, 4, 5), bb(0, 5, 4, 5)), bb(0, 0, 4, 10)),
            ((bb(0, 0, 4, 5), bb(0, 6, 4, 5)), bb(0, 0, 4, 11)),
            ((bb(0, 0, 4, 5), bb(0, -5, 4, 5)), bb(0, -5, 4, 10)),
            ((bb(0, 0, 4, 5), bb(0, -10, 4, 5)), bb(0, -10, 4, 15)),
            ((bb(1, 2, 3, 4), bb(5, 6, 7, 8)), bb(1, 2, 11, 12)),
            // Overlapping
            ((bb(0, 0, 4, 5), bb(2, 0, 4, 5)), bb(0, 0, 6, 5)),
            ((bb(0, 0, 4, 5), bb(-3, 0, 4, 5)), bb(-3, 0, 7, 5)),
            ((bb(0, 0, 4, 5), bb(0, 3, 4, 5)), bb(0, 0, 4, 8)),
            ((bb(0, 0, 4, 5), bb(0, -2, 4, 5)), bb(0, -2, 4, 7)),
            ((bb(1, 2, 5, 7), bb(5, 6, 3, 4)), bb(1, 2, 7, 8)),
            // Inside
            ((bb(-1, -2, 3, 5), bb(0, 0, 1, 2)), bb(-1, -2, 3, 5)),
            // Zero sized
            ((bb(0, 0, 0, 0), bb(0, 0, 0, 0)), bb(0, 0, 0, 0)),
            ((bb(1, 2, 3, 4), bb(0, 0, 0, 0)), bb(1, 2, 3, 4)),
            ((bb(1, 2, 3, 4), bb(0, 0, 1, 0)), bb(1, 2, 3, 4)),
            ((bb(1, 2, 3, 4), bb(0, 0, 0, 1)), bb(1, 2, 3, 4)),
            ((bb(0, 0, 0, 0), bb(1, 2, 3, 4)), bb(1, 2, 3, 4)),
            ((bb(0, 0, 1, 0), bb(1, 2, 3, 4)), bb(1, 2, 3, 4)),
            ((bb(0, 0, 0, 1), bb(1, 2, 3, 4)), bb(1, 2, 3, 4)),
        ]
        .into_iter()
        {
            assert_eq!(bb1.union(&bb2), expected_union, "{bb1:?}, {bb2:?}");
        }
    }
}