use std::mem::size_of;
use raw::tables::glyf::{PointCoord, ToPathStyle};
use read_fonts::{
tables::glyf::{to_path, Glyph, PointFlags, ToPathError},
types::{F26Dot6, Fixed, GlyphId, Pen, Point},
};
use super::super::Hinting;
#[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
}
}
#[derive(Debug)]
pub struct ScaledOutline<'a, C>
where
C: PointCoord,
{
pub points: &'a mut [Point<C>],
pub flags: &'a mut [PointFlags],
pub contours: &'a mut [u16],
pub phantom_points: [Point<C>; 4],
}
impl<'a, C> ScaledOutline<'a, C>
where
C: PointCoord,
{
pub(crate) fn new(
points: &'a mut [Point<C>],
phantom_points: [Point<C>; 4],
flags: &'a mut [PointFlags],
contours: &'a mut [u16],
) -> Self {
let x_shift = phantom_points[0].x;
if x_shift != C::zeroed() {
for point in points.iter_mut() {
point.x = point.x - x_shift;
}
}
Self {
points,
flags,
contours,
phantom_points,
}
}
pub fn adjusted_lsb(&self) -> C {
self.phantom_points[0].x
}
pub fn adjusted_advance_width(&self) -> C {
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)
}
}