font_map_core/raw/ttf/
glyf.rs

1#![allow(clippy::cast_sign_loss)]
2use crate::error::ParseResult;
3use crate::reader::{BinaryReader, Parse};
4
5mod simple;
6pub use simple::SimpleGlyf;
7
8mod compound;
9pub use compound::CompoundGlyf;
10
11mod svg;
12
13/// The outline features of a glyph
14#[derive(Debug, Clone)]
15pub enum GlyfOutline {
16    /// A simple glyph outline
17    Simple(SimpleGlyf),
18
19    /// A compound glyph outline
20    Compound(CompoundGlyf),
21}
22impl Default for GlyfOutline {
23    fn default() -> Self {
24        GlyfOutline::Simple(SimpleGlyf {
25            contours: vec![],
26            num_contours: 0,
27            x: (0, 0),
28            y: (0, 0),
29        })
30    }
31}
32impl GlyfOutline {
33    /// Returns true if the outline is a simple glyph
34    #[must_use]
35    pub fn is_simple(&self) -> bool {
36        matches!(self, GlyfOutline::Simple(_))
37    }
38
39    /// Returns true if the outline is a compound glyph
40    #[must_use]
41    pub fn is_compound(&self) -> bool {
42        matches!(self, GlyfOutline::Compound(_))
43    }
44}
45impl Parse for GlyfOutline {
46    fn parse(reader: &mut BinaryReader) -> ParseResult<Self> {
47        let num_contours = reader.read_i16()?;
48        let xmin = reader.read_i16()?;
49        let ymin = reader.read_i16()?;
50        let xmax = reader.read_i16()?;
51        let ymax = reader.read_i16()?;
52
53        if num_contours >= 0 {
54            //
55            // Simple glyph
56            let mut glyph = SimpleGlyf {
57                contours: Vec::with_capacity(num_contours as usize),
58                num_contours,
59                x: (xmin, xmax),
60                y: (ymin, ymax),
61            };
62
63            glyph.parse_with(reader)?;
64            Ok(GlyfOutline::Simple(glyph))
65        } else {
66            //
67            // Compound glyf
68            let glyph = CompoundGlyf::parse(reader)?;
69            Ok(GlyfOutline::Compound(glyph))
70        }
71    }
72}