use std::mem::size_of;
use raw::tables::glyf::ToPathStyle;
use read_fonts::{
tables::glyf::{to_path, Glyph, PointFlags, ToPathError},
types::{F26Dot6, Fixed, GlyphId, Pen, Point},
};
use super::{super::Hinting, OutlineMemory};
#[derive(Clone, 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 max_stack: usize,
pub cvt_count: usize,
pub storage_count: usize,
pub max_twilight_points: 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::Embedded;
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 hinting {
size += self.max_stack * size_of::<i32>();
size += (self.cvt_count + self.storage_count) * size_of::<i32>();
size += self.max_twilight_points
* (size_of::<Point<F26Dot6>>() * 2 + size_of::<PointFlags>());
}
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],
pub phantom_points: [Point<F26Dot6>; 4],
}
impl<'a> ScaledOutline<'a> {
pub fn adjusted_lsb(&self) -> F26Dot6 {
self.phantom_points[0].x
}
pub fn adjusted_advance_width(&self) -> F26Dot6 {
self.phantom_points[1].x - self.phantom_points[0].x
}
pub fn to_path(&self, path_style: ToPathStyle, pen: &mut impl Pen) -> Result<(), ToPathError> {
to_path(self.points, self.flags, self.contours, path_style, pen)
}
}