mod parser;
mod index;
pub mod charset;
mod charstring;
mod dict;
mod argstack;
mod std_names;
mod encoding;
mod cff;
pub use cff::{Table, string_by_id};
use parser::{FromData, Stream, TryNumFrom};
pub use encoding::{Encoding, EncodingKind, Format1Range, STANDARD_ENCODING};
#[derive(Clone, Copy, PartialEq, PartialOrd, Debug)]
pub struct StringId(pub u16);
impl FromData for StringId {
const SIZE: usize = 2;
#[inline]
fn parse(data: &[u8]) -> Option<Self> {
u16::parse(data).map(StringId)
}
}
trait IsEven {
fn is_odd(&self) -> bool;
}
impl IsEven for usize {
fn is_odd(&self) -> bool {
self & 1 == 1
}
}
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum CFFError {
NoGlyph,
ReadOutOfBounds,
ZeroBBox,
InvalidOperator,
UnsupportedOperator,
MissingEndChar,
DataAfterEndChar,
NestingLimitReached,
ArgumentsStackLimitReached,
InvalidArgumentsStackLength,
BboxOverflow,
MissingMoveTo,
InvalidSubroutineIndex,
NoLocalSubroutines,
InvalidSeacCode,
}
#[inline]
pub fn f32_abs(n: f32) -> f32 {
n.abs()
}
#[inline]
pub fn conv_subroutine_index(index: f32, bias: u16) -> Result<u32, CFFError> {
conv_subroutine_index_impl(index, bias).ok_or(CFFError::InvalidSubroutineIndex)
}
#[inline]
fn conv_subroutine_index_impl(index: f32, bias: u16) -> Option<u32> {
let index = i32::try_num_from(index)?;
let bias = i32::from(bias);
let index = index.checked_add(bias)?;
u32::try_from(index).ok()
}
#[inline]
pub fn calc_subroutine_bias(len: u32) -> u16 {
if len < 1240 {
107
} else if len < 33900 {
1131
} else {
32768
}
}
pub trait OutlineBuilder {
fn move_to(&mut self, x: f32, y: f32);
fn line_to(&mut self, x: f32, y: f32);
fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32);
fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32);
fn close(&mut self);
}
struct DummyOutline;
impl OutlineBuilder for DummyOutline {
fn move_to(&mut self, _: f32, _: f32) {}
fn line_to(&mut self, _: f32, _: f32) {}
fn quad_to(&mut self, _: f32, _: f32, _: f32, _: f32) {}
fn curve_to(&mut self, _: f32, _: f32, _: f32, _: f32, _: f32, _: f32) {}
fn close(&mut self) {}
}
pub(crate) struct Builder<'a> {
builder: &'a mut dyn OutlineBuilder,
bbox: RectF,
}
impl<'a> Builder<'a> {
#[inline]
fn move_to(&mut self, x: f32, y: f32) {
self.bbox.extend_by(x, y);
self.builder.move_to(x, y);
}
#[inline]
fn line_to(&mut self, x: f32, y: f32) {
self.bbox.extend_by(x, y);
self.builder.line_to(x, y);
}
#[inline]
fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32) {
self.bbox.extend_by(x1, y1);
self.bbox.extend_by(x2, y2);
self.bbox.extend_by(x, y);
self.builder.curve_to(x1, y1, x2, y2, x, y);
}
#[inline]
fn close(&mut self) {
self.builder.close();
}
}
#[repr(C)]
#[allow(missing_docs)]
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct Rect {
pub x_min: i16,
pub y_min: i16,
pub x_max: i16,
pub y_max: i16,
}
impl Rect {
#[inline]
fn zero() -> Self {
Self {
x_min: 0,
y_min: 0,
x_max: 0,
y_max: 0,
}
}
#[inline]
pub fn width(&self) -> i16 {
self.x_max - self.x_min
}
#[inline]
pub fn height(&self) -> i16 {
self.y_max - self.y_min
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct RectF {
pub x_min: f32,
pub y_min: f32,
pub x_max: f32,
pub y_max: f32,
}
impl RectF {
#[inline]
fn new() -> Self {
RectF {
x_min: f32::MAX,
y_min: f32::MAX,
x_max: f32::MIN,
y_max: f32::MIN,
}
}
#[inline]
fn is_default(&self) -> bool {
self.x_min == f32::MAX
&& self.y_min == f32::MAX
&& self.x_max == f32::MIN
&& self.y_max == f32::MIN
}
#[inline]
fn extend_by(&mut self, x: f32, y: f32) {
self.x_min = self.x_min.min(x);
self.y_min = self.y_min.min(y);
self.x_max = self.x_max.max(x);
self.y_max = self.y_max.max(y);
}
#[inline]
fn to_rect(self) -> Option<Rect> {
Some(Rect {
x_min: i16::try_num_from(self.x_min)?,
y_min: i16::try_num_from(self.y_min)?,
x_max: i16::try_num_from(self.x_max)?,
y_max: i16::try_num_from(self.y_max)?,
})
}
}
#[repr(transparent)]
#[derive(Clone, Copy, Ord, PartialOrd, Eq, PartialEq, Default, Debug)]
pub struct GlyphId(pub u16);
impl FromData for GlyphId {
const SIZE: usize = 2;
#[inline]
fn parse(data: &[u8]) -> Option<Self> {
u16::parse(data).map(GlyphId)
}
}