use std::mem::size_of;
use read_fonts::{
tables::glyf::{to_path, Glyph, PointFlags, ToPathError},
types::{F26Dot6, Fixed, GlyphId, Pen, Point},
};
use super::{Hinting, OutlineMemory};
#[derive(Default)]
pub struct Outline<'a> {
pub glyph_id: GlyphId,
pub glyph: Option<Glyph<'a>>,
pub points: usize,
pub contours: usize,
pub max_simple_points: usize,
pub max_other_points: usize,
pub max_component_delta_stack: usize,
pub has_hinting: bool,
pub has_variations: bool,
pub has_overlaps: bool,
}
impl<'a> Outline<'a> {
pub fn required_buffer_size(&self, hinting: Hinting) -> usize {
let mut size = 0;
let hinting = self.has_hinting && hinting != Hinting::None;
size += self.points * size_of::<Point<F26Dot6>>();
size += self.max_other_points * size_of::<Point<i32>>() * if hinting { 2 } else { 1 };
size += self.contours * size_of::<u16>();
size += self.points * size_of::<PointFlags>();
if self.has_variations {
size += self.max_simple_points * size_of::<Point<Fixed>>();
size += self.max_simple_points * size_of::<Point<Fixed>>();
size += self.max_component_delta_stack * size_of::<Point<Fixed>>();
}
if size != 0 {
size += std::mem::align_of::<i32>();
}
size
}
pub fn memory_from_buffer(
&self,
buf: &'a mut [u8],
hinting: Hinting,
) -> Option<OutlineMemory<'a>> {
OutlineMemory::new(self, buf, hinting)
}
}
#[derive(Debug)]
pub struct ScaledOutline<'a> {
pub points: &'a mut [Point<F26Dot6>],
pub flags: &'a mut [PointFlags],
pub contours: &'a mut [u16],
}
impl<'a> ScaledOutline<'a> {
pub fn to_path(&self, pen: &mut impl Pen) -> Result<(), ToPathError> {
to_path(self.points, self.flags, self.contours, pen)
}
}