use std::mem::size_of;
use super::super::{
path::{to_path, ToPathError},
pen::PathStyle,
DrawError, Hinting, OutlinePen,
};
use raw::tables::glyf::PointCoord;
use read_fonts::{
tables::glyf::{Glyph, PointFlags},
types::{F26Dot6, Fixed, GlyphId, Point},
};
const MAX_POINTS: usize = u16::MAX as usize;
#[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 Outline<'_> {
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(super) fn ensure_point_count_limit(&self) -> Result<(), DrawError> {
if self.points > MAX_POINTS {
Err(DrawError::TooManyPoints(self.glyph_id))
} else {
Ok(())
}
}
}
#[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],
pub hdmx_width: Option<u8>,
}
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],
hdmx_width: Option<u8>,
) -> 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,
hdmx_width,
}
}
pub fn adjusted_lsb(&self) -> C {
self.phantom_points[0].x
}
pub fn adjusted_advance_width(&self) -> C {
if let Some(hdmx_width) = self.hdmx_width {
C::from_i32(hdmx_width as i32)
} else {
self.phantom_points[1].x - self.phantom_points[0].x
}
}
pub fn to_path(
&self,
path_style: PathStyle,
pen: &mut impl OutlinePen,
) -> Result<(), ToPathError> {
to_path(self.points, self.flags, self.contours, path_style, pen)
}
}