use crate::small_array::SmallArray;
pub type NormalizedCoord = read_fonts::types::F2Dot14;
#[derive(Copy, Clone, PartialEq, PartialOrd, Debug)]
pub struct Size(f32);
impl Size {
pub fn new(ppem: f32) -> Self {
Self(ppem)
}
pub fn unscaled() -> Self {
Self(0.0)
}
pub fn ppem(self) -> Option<f32> {
(self.0 > 0.0).then_some(self.0)
}
pub fn linear_scale(self, units_per_em: u16) -> f32 {
if self.0 > 0.0 && units_per_em != 0 {
self.0 / units_per_em as f32
} else {
1.0
}
}
}
#[derive(Copy, Clone, Default, Debug)]
pub struct LocationRef<'a>(&'a [NormalizedCoord]);
impl<'a> LocationRef<'a> {
pub fn new(coords: &'a [NormalizedCoord]) -> Self {
Self(coords)
}
pub fn coords(&self) -> &'a [NormalizedCoord] {
self.0
}
}
impl<'a> From<&'a [NormalizedCoord]> for LocationRef<'a> {
fn from(value: &'a [NormalizedCoord]) -> Self {
Self(value)
}
}
impl<'a> IntoIterator for LocationRef<'a> {
type IntoIter = core::slice::Iter<'a, NormalizedCoord>;
type Item = &'a NormalizedCoord;
fn into_iter(self) -> Self::IntoIter {
self.0.iter()
}
}
impl<'a> IntoIterator for &'_ LocationRef<'a> {
type IntoIter = core::slice::Iter<'a, NormalizedCoord>;
type Item = &'a NormalizedCoord;
fn into_iter(self) -> Self::IntoIter {
self.0.iter()
}
}
const MAX_INLINE_COORDS: usize = 8;
#[derive(Clone, Debug)]
pub struct Location {
coords: SmallArray<NormalizedCoord, MAX_INLINE_COORDS>,
}
impl Location {
pub fn new(len: usize) -> Self {
Self {
coords: SmallArray::new(NormalizedCoord::default(), len),
}
}
pub fn coords(&self) -> &[NormalizedCoord] {
self.coords.as_slice()
}
pub fn coords_mut(&mut self) -> &mut [NormalizedCoord] {
self.coords.as_mut_slice()
}
}
impl Default for Location {
fn default() -> Self {
Self {
coords: SmallArray::new(NormalizedCoord::default(), 0),
}
}
}
impl<'a> From<&'a Location> for LocationRef<'a> {
fn from(value: &'a Location) -> Self {
LocationRef(value.coords())
}
}
impl<'a> IntoIterator for &'a Location {
type IntoIter = core::slice::Iter<'a, NormalizedCoord>;
type Item = &'a NormalizedCoord;
fn into_iter(self) -> Self::IntoIter {
self.coords().iter()
}
}
impl<'a> IntoIterator for &'a mut Location {
type IntoIter = core::slice::IterMut<'a, NormalizedCoord>;
type Item = &'a mut NormalizedCoord;
fn into_iter(self) -> Self::IntoIter {
self.coords_mut().iter_mut()
}
}