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
// Copyright 2018 Kyle Mayes
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! A zero-allocation CFF parser.

#![deny(missing_copy_implementations, missing_debug_implementations, missing_docs)]

#![no_std]

#[macro_use]
extern crate tarrasque;

pub mod dict;
pub mod error;
pub mod glyphs;
pub mod index;

use tarrasque::{Extract, ExtractError, ExtractResult, Stream};

use dict::{Top};
use error::{OFFSET_SIZE};
use glyphs::{Glyphs, parse_glyphs};
use index::{Index, Strings};
pub use error::{CffError};

/// A number which indicates the byte length of offsets.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct OffsetSize(pub u8);

impl<'a> Extract<'a, ()> for OffsetSize {
    #[inline]
    fn extract(stream: &mut Stream<'a>, _: ()) -> ExtractResult<'a, Self> {
        let size: u8 = stream.extract(())?;
        if size >= 1 && size <= 4 {
            Ok(OffsetSize(size))
        } else {
            Err(ExtractError::Code(OFFSET_SIZE))
        }
    }
}

extract! {
    /// A CFF file header.
    #[derive(Copy, Clone, Debug, PartialEq, Eq)]
    pub Header[4] {
        /// The major CFF file version number.
        major_version: u8 = ([extract]),
        /// The minor CFF file version number.
        minor_version: u8 = ([extract]),
        /// The byte length of this header.
        header_size: u8 = ([extract]),
        /// The byte length of the absolute offsets in the file.
        offset_size: OffsetSize = ([extract]),
    }
}

/// A CFF file.
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Cff<'a> {
    /// The bytes in this file.
    pub bytes: &'a [u8],
    /// The header of this file.
    pub header: Header,
    /// The names of the fonts in this file.
    pub names: Index<'a, &'a str>,
    /// The top level `DICT`s for the fonts in this file.
    pub tops: Index<'a, Top<'a>>,
    /// The shared strings for the fonts in this file.
    pub strings: Strings<'a>,
    /// The shared subroutines for the fonts in this file.
    pub subroutines: Index<'a, &'a [u8]>,
}

impl<'a> Cff<'a> {
    /// Returns the glyphs for the font at the supplied index in this file.
    #[inline]
    pub fn glyphs(&self, index: usize) -> Option<Result<Glyphs<'a>, CffError<'a>>> {
        self.tops.extract(index).map(|t| parse_glyphs(&self.bytes, t?, self.subroutines))
    }
}

/// Parses the supplied CFF file.
#[inline]
pub fn parse_cff(bytes: &[u8]) -> Result<Cff, CffError> {
    let mut stream = Stream(bytes);
    let header: Header = stream.extract(())?;
    let names: Index<&str> = stream.extract(())?;
    let tops: Index<Top> = stream.extract(())?;
    let strings: Strings = stream.extract(())?;
    let subroutines: Index<&[u8]> = stream.extract(())?;
    Ok(Cff { bytes, header, names, tops, strings, subroutines })
}