use crate::error::{ParseError, RawTag};
use crate::reader::Reader;
use alloc::vec::Vec;
use font_types::{F2Dot14, GlyphId};
pub(crate) const TAG: RawTag = *b"glyf";
pub(crate) const FLAG_ON_CURVE: u8 = 0x01;
pub(crate) const FLAG_X_SHORT: u8 = 0x02;
pub(crate) const FLAG_Y_SHORT: u8 = 0x04;
pub(crate) const FLAG_REPEAT: u8 = 0x08;
pub(crate) const FLAG_X_SAME_OR_POS: u8 = 0x10;
pub(crate) const FLAG_Y_SAME_OR_POS: u8 = 0x20;
#[cfg(feature = "woff2")]
pub(crate) const FLAG_OVERLAP_SIMPLE: u8 = 0x40;
pub(crate) const ARG_1_AND_2_ARE_WORDS: u16 = 0x0001;
pub(crate) const ARGS_ARE_XY_VALUES: u16 = 0x0002;
pub(crate) const MORE_COMPONENTS: u16 = 0x0020;
pub(crate) const WE_HAVE_A_SCALE: u16 = 0x0008;
pub(crate) const WE_HAVE_AN_X_AND_Y_SCALE: u16 = 0x0040;
pub(crate) const WE_HAVE_A_TWO_BY_TWO: u16 = 0x0080;
#[cfg(feature = "woff2")]
pub(crate) const WE_HAVE_INSTRUCTIONS: u16 = 0x0100;
pub(crate) const MAX_COMPONENT_DEPTH: u8 = 16;
pub(crate) const MAX_OUTLINE_POINTS: usize = 1 << 20;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct OutlinePoint {
pub x: f32,
pub y: f32,
pub on_curve: bool,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Contour {
pub points: Vec<OutlinePoint>,
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct GlyphOutline {
pub contours: Vec<Contour>,
}
impl GlyphOutline {
#[must_use]
pub fn point_count(&self) -> usize {
self.contours.iter().map(|c| c.points.len()).sum()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.contours.is_empty()
}
}
#[derive(Debug, Clone, Copy)]
pub enum GlyphRef<'a> {
Simple(SimpleGlyph<'a>),
Composite(CompositeGlyph<'a>),
}
impl<'a> GlyphRef<'a> {
pub fn parse(data: &'a [u8]) -> Result<Option<Self>, ParseError> {
if data.is_empty() {
return Ok(None);
}
if data.len() < 10 {
return Err(ParseError::TruncatedTable {
tag: TAG,
expected: 10,
got: data.len(),
});
}
let n_contours = crate::reader::be_i16_at(data, 0, TAG)?;
if n_contours >= 0 {
Ok(Some(Self::Simple(SimpleGlyph::parse(data)?)))
} else if n_contours == -1 {
Ok(Some(Self::Composite(CompositeGlyph::parse(data)?)))
} else {
Err(ParseError::InvalidGlyphFormat)
}
}
pub fn bounds(&self) -> Result<[i16; 4], ParseError> {
match self {
Self::Simple(g) => g.bounds(),
Self::Composite(g) => g.bounds(),
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct SimpleGlyph<'a> {
data: &'a [u8],
n_contours: u16,
}
impl<'a> SimpleGlyph<'a> {
fn parse(data: &'a [u8]) -> Result<Self, ParseError> {
let n = crate::reader::be_u16_at(data, 0, TAG)?;
Ok(Self {
data,
n_contours: n,
})
}
#[must_use]
pub fn as_bytes(&self) -> &'a [u8] {
self.data
}
#[must_use]
pub const fn num_contours(&self) -> u16 {
self.n_contours
}
pub fn bounds(&self) -> Result<[i16; 4], ParseError> {
Ok([
crate::reader::be_i16_at(self.data, 2, TAG)?,
crate::reader::be_i16_at(self.data, 4, TAG)?,
crate::reader::be_i16_at(self.data, 6, TAG)?,
crate::reader::be_i16_at(self.data, 8, TAG)?,
])
}
pub fn end_points(&self) -> Result<Vec<u16>, ParseError> {
let n = usize::from(self.n_contours);
let at = 10usize;
let end = at
.checked_add(n * 2)
.ok_or(ParseError::InvalidGlyphFormat)?;
if self.data.len() < end {
return Err(ParseError::TruncatedTable {
tag: TAG,
expected: end,
got: self.data.len(),
});
}
let mut out = Vec::with_capacity(n);
for i in 0..n {
out.push(crate::reader::be_u16_at(self.data, at + i * 2, TAG)?);
}
Ok(out)
}
pub fn points(&self) -> Result<Vec<OutlinePoint>, ParseError> {
let mut r = Reader::new(self.data, TAG);
r.skip(10)?;
let ends = self.end_points()?;
let n_points = ends.last().map_or(0, |&e| usize::from(e) + 1);
r.skip(ends.len() * 2)?;
let instr_len = usize::from(r.u16()?);
r.skip(instr_len)?;
let mut flags = Vec::with_capacity(n_points);
while flags.len() < n_points {
let flag = r.u8()?;
flags.push(flag);
if flag & FLAG_REPEAT != 0 {
let repeat = usize::from(r.u8()?);
for _ in 0..repeat {
if flags.len() >= n_points {
return Err(ParseError::InvalidGlyphFormat);
}
flags.push(flag);
}
}
}
let mut xs = Vec::with_capacity(n_points);
let mut x: i32 = 0;
for &flag in &flags {
if flag & FLAG_X_SHORT != 0 {
let d = i32::from(r.u8()?);
x += if flag & FLAG_X_SAME_OR_POS != 0 {
d
} else {
-d
};
} else if flag & FLAG_X_SAME_OR_POS == 0 {
x += i32::from(r.i16()?);
}
xs.push(x);
}
let mut pts = Vec::with_capacity(n_points);
let mut y: i32 = 0;
for (i, &flag) in flags.iter().enumerate() {
if flag & FLAG_Y_SHORT != 0 {
let d = i32::from(r.u8()?);
y += if flag & FLAG_Y_SAME_OR_POS != 0 {
d
} else {
-d
};
} else if flag & FLAG_Y_SAME_OR_POS == 0 {
y += i32::from(r.i16()?);
}
if i >= MAX_OUTLINE_POINTS {
return Err(ParseError::InvalidGlyphFormat);
}
let x = *xs.get(i).unwrap_or(&0);
pts.push(OutlinePoint {
x: x as f32,
y: y as f32,
on_curve: flag & FLAG_ON_CURVE != 0,
});
}
Ok(pts)
}
pub fn outline(&self) -> Result<GlyphOutline, ParseError> {
let pts = self.points()?;
let ends = self.end_points()?;
let mut contours = Vec::with_capacity(ends.len());
let mut start = 0usize;
for &e in &ends {
let end = usize::from(e) + 1;
if end > pts.len() || start > end {
return Err(ParseError::InvalidGlyphFormat);
}
let slice = pts.get(start..end).ok_or(ParseError::InvalidGlyphFormat)?;
contours.push(Contour {
points: slice.to_vec(),
});
start = end;
}
Ok(GlyphOutline { contours })
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Component {
pub glyph: GlyphId,
pub flags: u16,
pub args: (i32, i32),
pub transform: [f32; 4],
}
impl Component {
#[must_use]
pub const fn args_are_offsets(&self) -> bool {
self.flags & ARGS_ARE_XY_VALUES != 0
}
}
#[derive(Debug, Clone, Copy)]
pub struct CompositeGlyph<'a> {
data: &'a [u8],
}
impl<'a> CompositeGlyph<'a> {
fn parse(data: &'a [u8]) -> Result<Self, ParseError> {
Ok(Self { data })
}
#[must_use]
pub fn as_bytes(&self) -> &'a [u8] {
self.data
}
pub fn bounds(&self) -> Result<[i16; 4], ParseError> {
Ok([
crate::reader::be_i16_at(self.data, 2, TAG)?,
crate::reader::be_i16_at(self.data, 4, TAG)?,
crate::reader::be_i16_at(self.data, 6, TAG)?,
crate::reader::be_i16_at(self.data, 8, TAG)?,
])
}
pub fn components(&self) -> Result<Vec<Component>, ParseError> {
let mut r = Reader::new(self.data, TAG);
r.skip(10)?;
let mut out = Vec::new();
loop {
if out.len() > MAX_OUTLINE_POINTS {
return Err(ParseError::InvalidGlyphFormat);
}
let flags = r.u16()?;
let glyph = GlyphId::new(u32::from(r.u16()?));
let (a1, a2) = if flags & ARG_1_AND_2_ARE_WORDS != 0 {
(i32::from(r.i16()?), i32::from(r.i16()?))
} else {
(i32::from(r.i8()?), i32::from(r.i8()?))
};
let scale = |r: &mut Reader<'a>| -> Result<F2Dot14, ParseError> { r.f2dot14() };
let transform = if flags & WE_HAVE_A_SCALE != 0 {
let a = scale(&mut r)?;
[a.to_f32(), 0.0, 0.0, a.to_f32()]
} else if flags & WE_HAVE_AN_X_AND_Y_SCALE != 0 {
let a = scale(&mut r)?;
let d = scale(&mut r)?;
[a.to_f32(), 0.0, 0.0, d.to_f32()]
} else if flags & WE_HAVE_A_TWO_BY_TWO != 0 {
let a = scale(&mut r)?;
let b = scale(&mut r)?;
let c = scale(&mut r)?;
let d = scale(&mut r)?;
[a.to_f32(), b.to_f32(), c.to_f32(), d.to_f32()]
} else {
[1.0, 0.0, 0.0, 1.0]
};
out.push(Component {
glyph,
flags,
args: (a1, a2),
transform,
});
if flags & MORE_COMPONENTS == 0 {
break;
}
}
Ok(out)
}
}
#[cfg(test)]
mod tests {
#![allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::indexing_slicing,
clippy::panic
)]
use super::{Component, GlyphOutline, GlyphRef, OutlinePoint, SimpleGlyph};
use crate::error::ParseError;
use alloc::vec;
use alloc::vec::Vec;
use font_types::GlyphId;
fn triangle() -> Vec<u8> {
let mut d = Vec::new();
d.extend_from_slice(&1i16.to_be_bytes()); d.extend_from_slice(&0i16.to_be_bytes());
d.extend_from_slice(&100i16.to_be_bytes());
d.extend_from_slice(&300i16.to_be_bytes());
d.extend_from_slice(&200i16.to_be_bytes());
d.extend_from_slice(&2u16.to_be_bytes()); d.extend_from_slice(&0u16.to_be_bytes()); d.extend_from_slice(&[0x37, 0x37, 0x17]);
d.extend_from_slice(&[100, 0, 100]);
d.extend_from_slice(&[0, 200, 200]);
d
}
#[test]
fn simple_glyph_points_and_contours() {
let d = triangle();
let g = GlyphRef::parse(&d).unwrap().unwrap();
let super::GlyphRef::Simple(simple) = g else {
panic!("expected simple glyph");
};
assert_eq!(simple.num_contours(), 1);
assert_eq!(simple.bounds().unwrap(), [0, 100, 300, 200]);
assert_eq!(simple.end_points().unwrap(), vec![2]);
let pts = simple.points().unwrap();
assert_eq!(
pts,
vec![
OutlinePoint {
x: 100.0,
y: 0.0,
on_curve: true
},
OutlinePoint {
x: 100.0,
y: 200.0,
on_curve: true
},
OutlinePoint {
x: 200.0,
y: 0.0,
on_curve: true
},
]
);
let outline: GlyphOutline = simple.outline().unwrap();
assert_eq!(outline.contours.len(), 1);
assert_eq!(outline.point_count(), 3);
}
#[test]
fn repeat_flags_and_off_curve() {
let mut g = Vec::new();
g.extend_from_slice(&1i16.to_be_bytes());
g.extend_from_slice(&0i16.to_be_bytes());
g.extend_from_slice(&0i16.to_be_bytes());
g.extend_from_slice(&200i16.to_be_bytes());
g.extend_from_slice(&100i16.to_be_bytes());
g.extend_from_slice(&1u16.to_be_bytes()); g.extend_from_slice(&0u16.to_be_bytes());
g.extend_from_slice(&[0x3F, 1]);
g.extend_from_slice(&[100, 100]); g.extend_from_slice(&[200, 200]); let glyph = GlyphRef::parse(&g).unwrap().unwrap();
let super::GlyphRef::Simple(simple) = glyph else {
panic!("expected simple");
};
let pts = simple.points().unwrap();
assert_eq!(
pts,
vec![
OutlinePoint {
x: 100.0,
y: 200.0,
on_curve: true
},
OutlinePoint {
x: 200.0,
y: 400.0,
on_curve: true
},
]
);
}
#[test]
fn composite_components_and_flags() {
let mut d = Vec::new();
d.extend_from_slice(&(-1i16).to_be_bytes());
d.extend_from_slice(&0i16.to_be_bytes());
d.extend_from_slice(&0i16.to_be_bytes());
d.extend_from_slice(&400i16.to_be_bytes());
d.extend_from_slice(&400i16.to_be_bytes());
let flags: u16 = 0x0001 | 0x0080;
d.extend_from_slice(&(flags | 0x0100u16).to_be_bytes()); d.extend_from_slice(&7u16.to_be_bytes());
d.extend_from_slice(&3i16.to_be_bytes()); d.extend_from_slice(&9i16.to_be_bytes());
d.extend_from_slice(&0x4000i16.to_be_bytes());
d.extend_from_slice(&0i16.to_be_bytes());
d.extend_from_slice(&0i16.to_be_bytes());
d.extend_from_slice(&0x2000i16.to_be_bytes());
d.extend_from_slice(&2u16.to_be_bytes()); d.extend_from_slice(&[0xAA, 0xBB]);
let glyph = GlyphRef::parse(&d).unwrap().unwrap();
let super::GlyphRef::Composite(composite) = glyph else {
panic!("expected composite");
};
let comps: Vec<Component> = composite.components().unwrap();
assert_eq!(comps.len(), 1);
let c = comps[0];
assert_eq!(c.glyph, GlyphId::new(7));
assert!(!c.args_are_offsets());
assert_eq!(c.args, (3, 9));
assert!((c.transform[0] - 1.0).abs() < 1e-6);
assert!((c.transform[3] - 0.5).abs() < 1e-6);
assert_eq!(composite.bounds().unwrap(), [0, 0, 400, 400]);
}
#[test]
fn malformed_records() {
let mut d = Vec::new();
d.extend_from_slice(&(-3i16).to_be_bytes());
d.extend_from_slice(&[0u8; 9]);
assert!(matches!(
GlyphRef::parse(&d),
Err(ParseError::InvalidGlyphFormat)
));
assert!(matches!(
GlyphRef::parse(&[0, 1, 0]),
Err(ParseError::TruncatedTable { .. })
));
assert!(GlyphRef::parse(&[]).unwrap().is_none());
let mut g = triangle();
g[10..12].copy_from_slice(&50u16.to_be_bytes()); let glyph = GlyphRef::parse(&g).unwrap().unwrap();
let super::GlyphRef::Simple(simple) = glyph else {
panic!("expected simple");
};
assert!(simple.points().is_err()); let _ = SimpleGlyph::parse(&g).unwrap().num_contours();
}
}