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
use std::{
    borrow::Cow,
    io::{BufRead, BufReader, Read},
    str::{from_utf8, FromStr},
};

use crate::{
    error::{Error, ParseError},
    result::Result,
    utils::read_line,
};

use bitflags::bitflags;

const MAGIC_NUMBER: &'static [u8] = b"flf2a";

bitflags! {
    /// The FIGfont's layout informations.
    pub struct Layout: u32 {
        /// Equals smushing.
        const HORIZONTAL_EQUAL = 1;
        /// Underscore smushing.
        const HORIZONTAL_LOWLINE = 2;
        /// Hierarchy smushing.
        const HORIZONTAL_HIERARCHY = 4;
        /// Pair brackets smushing.
        const HORIZONTAL_PAIR = 8;
        /// Big X smushing.
        const HORIZONTAL_BIGX = 16;
        /// Hard blank smushing.
        const HORIZONTAL_HARDBLANK = 32;
        /// Apply kerning.
        const HORIZONTAL_KERNING = 64;
        /// Apply smushing.
        const HORIZONTAL_SMUSH = 128;

        const VERTICAL_EQUAL = 256;
        const VERTICAL_LOWLINE = 512;
        const VERTICAL_HIERARCHY = 1024;
        const VERTICAL_PAIR = 2048;
        const VERTICAL_BIGX = 4096;
        const VERTICAL_KERNING = 8192;
        const VERTICAL_SMUSH = 16392;
    }
}

impl FromStr for Layout {
    type Err = Error;

    fn from_str(raw: &str) -> std::result::Result<Self, <Self as FromStr>::Err> {
        let raw: u32 = raw
            .parse()
            .ok()
            .ok_or::<Error>(ParseError::InvalidHeader.into())?;
        Layout::from_bits(raw).ok_or(ParseError::InvalidHeader.into())
    }
}

/// FIGfont's header.
#[derive(Debug)]
pub struct Header {
    hard_blank_char: Vec<u8>,
    height: usize,
    baseline: usize,
    max_length: usize,
    layout: Layout,
    comment: String,
    print_direction: PrintDirection,
    codetag_count: Option<u32>,
}

impl Header {
    pub(crate) fn parse<R: Read>(bread: &mut BufReader<R>) -> Result<Header> {
        parse_header(bread)
    }

    /// Get the hard blank character.
    pub fn hard_blank_char<'a>(&'a self) -> &'a [u8] {
        &self.hard_blank_char[..]
    }

    /// Get the font's height (lines).
    pub fn height(&self) -> usize {
        self.height
    }

    /// Get the font's basline. Unused.
    pub fn baseline(&self) -> usize {
        self.baseline
    }

    /// Get the font's max length. Unused.
    pub fn max_length(&self) -> usize {
        self.max_length
    }

    /// Get the font's layout.
    pub fn layout(&self) -> Layout {
        self.layout
    }

    /// Get the font's comment.
    pub fn comment<'a>(&'a self) -> Cow<'a, str> {
        Cow::Borrowed(&self.comment)
    }

    /// Get the print direction.
    pub fn print_direction(&self) -> PrintDirection {
        self.print_direction
    }

    /// Get the number of codetagged characters.
    pub fn codetag_count(&self) -> Option<u32> {
        self.codetag_count
    }
}

/// Print direction enum.
#[derive(Debug, Copy, Clone)]
pub enum PrintDirection {
    LeftToRight,
    RightToLeft,
}

impl FromStr for PrintDirection {
    type Err = ParseError;

    fn from_str(text: &str) -> std::result::Result<Self, <Self as FromStr>::Err> {
        match text.parse::<u8>() {
            Ok(n) => match n {
                0 => Ok(Self::LeftToRight),
                1 => Ok(Self::RightToLeft),
                _ => Err(ParseError::InvalidHeader),
            },
            Err(_) => Err(ParseError::InvalidHeader),
        }
    }
}

fn read_string_lines<R: Read>(bread: &mut BufReader<R>, num: usize) -> Result<String> {
    let mut lines = String::new();

    for _ in 0..num {
        bread.read_line(&mut lines)?;
    }

    if lines.ends_with("\r\n") {
        lines.truncate(lines.len() - 2);
    } else if lines.ends_with("\n") {
        lines.truncate(lines.len() - 1);
    } else {
        return Err(ParseError::NotEnoughData.into());
    }

    Ok(lines)
}

macro_rules! parse {
    ($arg:expr) => {
        parse!($arg, _)
    };

    ($arg:expr, $t:ty) => {
        match from_utf8($arg)
            .map_err(|_| ParseError::InvalidHeader)?
            .parse::<$t>()
        {
            Ok(res) => Some(res),
            Err(_) => {
                return Err(ParseError::InvalidHeader.into());
            }
        }
    };
}

fn parse_header<R: Read>(bread: &mut BufReader<R>) -> Result<Header> {
    let header = read_line(bread)?;
    let header: Vec<u8> = if header.starts_with(MAGIC_NUMBER) {
        header.into_iter().skip(MAGIC_NUMBER.len()).collect()
    } else {
        return Err(ParseError::InvalidHeader.into());
    };

    let arguments: Vec<&[u8]> = header
        .split(|c| c == &b' ')
        .enumerate()
        .filter(|(i, x)| *i == 0 || !x.is_empty())
        .map(|(_, x)| x)
        .collect();

    if arguments.len() < 6 || arguments.len() > 9 {
        return Err(ParseError::InvalidHeader.into());
    }

    if arguments[0].is_empty() {
        return Err(ParseError::InvalidHeader.into());
    }

    let mut hard_blank_char: Vec<u8> = Vec::with_capacity(arguments[0].len());
    hard_blank_char.extend_from_slice(arguments[0]);
    let height: usize = parse!(arguments[1]).ok_or(ParseError::InvalidHeader)?;
    let baseline: usize = parse!(arguments[2]).ok_or(ParseError::InvalidHeader)?;
    let max_length: usize = parse!(arguments[3]).ok_or(ParseError::InvalidHeader)?;
    let old_layout: i32 = parse!(arguments[4]).ok_or(ParseError::InvalidHeader)?;

    let print_direction: PrintDirection = if arguments.len() > 6 {
        parse!(arguments[6]).ok_or(ParseError::InvalidHeader)?
    } else {
        PrintDirection::LeftToRight
    };

    let layout: Layout = if arguments.len() > 7 {
        parse!(arguments[7]).ok_or(ParseError::InvalidHeader)?
    } else {
        full_layout_from_old_layout(old_layout)
    };

    let codetag_count: Option<u32> = if arguments.len() > 8 {
        Some(parse!(arguments[8]).ok_or(ParseError::InvalidHeader)?)
    } else {
        None
    };

    let comment: String = {
        let comment_lines: usize = parse!(arguments[5]).ok_or(ParseError::InvalidHeader)?;
        read_string_lines(bread, comment_lines)?
    };

    Ok(Header {
        hard_blank_char,
        height,
        baseline,
        max_length,
        comment,
        print_direction,
        layout,
        codetag_count,
    })
}

fn full_layout_from_old_layout(old_layout: i32) -> Layout {
    let raw = if old_layout == 0 {
        64
    } else if old_layout < 0 {
        0
    } else {
        (old_layout as u32 & 31) | 128
    };

    Layout::from_bits_truncate(raw)
}