use super::{glyf, Context, Error, NormalizedCoord, Pen, Result, VariationSetting};
use crate::{Size, UniqueId};
#[cfg(feature = "hinting")]
use super::Hinting;
use core::{borrow::Borrow, str::FromStr};
use read_fonts::{
types::{Fixed, GlyphId, Tag},
TableProvider,
};
pub struct ScalerBuilder<'a> {
context: &'a mut Context,
cache_key: Option<UniqueId>,
size: Size,
#[cfg(feature = "hinting")]
hint: Option<Hinting>,
}
impl<'a> ScalerBuilder<'a> {
pub fn new(context: &'a mut Context) -> Self {
context.coords.clear();
context.variations.clear();
Self {
context,
cache_key: None,
size: Size::unscaled(),
#[cfg(feature = "hinting")]
hint: None,
}
}
pub fn cache_key(mut self, key: Option<UniqueId>) -> Self {
self.cache_key = key;
self
}
pub fn size(mut self, size: Size) -> Self {
self.size = size;
self
}
#[cfg(feature = "hinting")]
pub fn hint(mut self, hint: Option<Hinting>) -> Self {
self.hint = hint;
self
}
pub fn normalized_coords<I>(self, coords: I) -> Self
where
I: IntoIterator,
I::Item: Borrow<NormalizedCoord>,
{
self.context.variations.clear();
self.context.coords.clear();
self.context
.coords
.extend(coords.into_iter().map(|v| *v.borrow()));
self
}
pub fn variation_settings<I>(self, settings: I) -> Self
where
I: IntoIterator,
I::Item: Into<VariationSetting>,
{
self.context.coords.clear();
self.context
.variations
.extend(settings.into_iter().map(|v| v.into()));
self
}
pub fn build(mut self, font: &impl TableProvider<'a>) -> Scaler<'a> {
self.resolve_variations(font);
let coords = &self.context.coords[..];
let glyf = if let Ok(glyf) = glyf::Scaler::new(
&mut self.context.glyf,
font,
self.cache_key,
self.size.ppem().unwrap_or_default(),
#[cfg(feature = "hinting")]
self.hint,
coords,
) {
Some((glyf, &mut self.context.glyf_outline))
} else {
None
};
Scaler {
coords,
outlines: Outlines { glyf },
}
}
fn resolve_variations(&mut self, font: &impl TableProvider<'a>) {
if self.context.variations.is_empty() {
return; }
let Ok(fvar) = font.fvar() else {
return; };
let Ok(axes) = fvar.axes() else {
return; };
let avar_mappings = font.avar().ok().map(|avar| avar.axis_segment_maps());
let axis_count = fvar.axis_count() as usize;
self.context.coords.clear();
self.context
.coords
.resize(axis_count, NormalizedCoord::default());
for variation in &self.context.variations {
for (i, axis) in axes
.iter()
.enumerate()
.filter(|(_, axis)| axis.axis_tag() == variation.selector)
{
let coord = axis.normalize(Fixed::from_f64(variation.value as f64));
let coord = avar_mappings
.as_ref()
.and_then(|mappings| mappings.get(i).transpose().ok())
.flatten()
.map(|mapping| mapping.apply(coord))
.unwrap_or(coord);
self.context.coords[i] = coord.to_f2dot14();
}
}
}
}
pub struct Scaler<'a> {
coords: &'a [NormalizedCoord],
outlines: Outlines<'a>,
}
impl<'a> Scaler<'a> {
pub fn normalized_coords(&self) -> &'a [NormalizedCoord] {
self.coords
}
pub fn has_outlines(&self) -> bool {
self.outlines.has_outlines()
}
pub fn outline(&mut self, glyph_id: GlyphId, sink: &mut impl Pen) -> Result<()> {
self.outlines.outline(glyph_id, sink)
}
}
struct Outlines<'a> {
glyf: Option<(glyf::Scaler<'a>, &'a mut glyf::Outline)>,
}
impl<'a> Outlines<'a> {
fn has_outlines(&self) -> bool {
self.glyf.is_some()
}
fn outline(&mut self, glyph_id: GlyphId, sink: &mut impl Pen) -> Result<()> {
if let Some((scaler, glyf_outline)) = &mut self.glyf {
scaler.load(glyph_id, glyf_outline)?;
Ok(glyf_outline.to_path(sink)?)
} else {
Err(Error::NoSources)
}
}
}