use raw::tables::glyf::ToPathStyle;
use super::{
cff,
glyf::{self, FreeTypeScaler},
AdjustedMetrics, DrawError, Hinting, LocationRef, NormalizedCoord, OutlineCollectionKind,
OutlineGlyph, OutlineGlyphCollection, OutlineKind, OutlinePen, Size,
};
use crate::alloc::{boxed::Box, vec::Vec};
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum HintingMode {
Strong,
Smooth {
lcd_subpixel: Option<LcdLayout>,
preserve_linear_metrics: bool,
},
}
impl Default for HintingMode {
fn default() -> Self {
Self::Smooth {
lcd_subpixel: None,
preserve_linear_metrics: false,
}
}
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum LcdLayout {
Horizontal,
Vertical,
}
#[derive(Clone)]
pub struct HintingInstance {
size: Size,
coords: Vec<NormalizedCoord>,
mode: HintingMode,
kind: HinterKind,
}
impl HintingInstance {
pub fn new<'a>(
outline_glyphs: &OutlineGlyphCollection,
size: Size,
location: impl Into<LocationRef<'a>>,
mode: HintingMode,
) -> Result<Self, DrawError> {
let mut hinter = Self {
size: Size::unscaled(),
coords: vec![],
mode,
kind: HinterKind::None,
};
hinter.reconfigure(outline_glyphs, size, location, mode)?;
Ok(hinter)
}
pub fn size(&self) -> Size {
self.size
}
pub fn location(&self) -> LocationRef {
LocationRef::new(&self.coords)
}
pub fn mode(&self) -> HintingMode {
self.mode
}
pub fn reconfigure<'a>(
&mut self,
outlines: &OutlineGlyphCollection,
size: Size,
location: impl Into<LocationRef<'a>>,
mode: HintingMode,
) -> Result<(), DrawError> {
self.size = size;
self.coords.clear();
self.coords.extend_from_slice(location.into().coords());
self.mode = mode;
let current_kind = core::mem::replace(&mut self.kind, HinterKind::None);
match &outlines.kind {
OutlineCollectionKind::Glyf(glyf) => {
let mut hint_instance = match current_kind {
HinterKind::Glyf(instance) => instance,
_ => Box::<glyf::HintInstance>::default(),
};
let ppem = size.ppem();
let scale = glyf.compute_scale(ppem).1.to_bits();
hint_instance.reconfigure(
glyf,
scale,
ppem.unwrap_or_default() as i32,
mode,
&self.coords,
)?;
self.kind = HinterKind::Glyf(hint_instance);
}
OutlineCollectionKind::Cff(cff) => {
let mut subfonts = match current_kind {
HinterKind::Cff(subfonts) => subfonts,
_ => vec![],
};
subfonts.clear();
let ppem = size.ppem();
for i in 0..cff.subfont_count() {
subfonts.push(cff.subfont(i, ppem, &self.coords)?);
}
self.kind = HinterKind::Cff(subfonts);
}
OutlineCollectionKind::None => {}
}
Ok(())
}
pub fn is_enabled(&self) -> bool {
match &self.kind {
HinterKind::Glyf(instance) => instance.is_enabled(),
HinterKind::Cff(_) => true,
_ => false,
}
}
pub(super) fn draw(
&self,
glyph: &OutlineGlyph,
memory: Option<&mut [u8]>,
path_style: ToPathStyle,
pen: &mut impl OutlinePen,
is_pedantic: bool,
) -> Result<AdjustedMetrics, DrawError> {
let ppem = self.size.ppem();
let coords = self.coords.as_slice();
match (&self.kind, &glyph.kind) {
(HinterKind::Glyf(instance), OutlineKind::Glyf(glyf, outline)) => {
if matches!(path_style, ToPathStyle::HarfBuzz) {
return Err(DrawError::HarfBuzzHintingUnsupported);
}
super::with_glyf_memory(outline, Hinting::Embedded, memory, |buf| {
let scaled_outline = FreeTypeScaler::hinted(
glyf.clone(),
outline,
buf,
ppem,
coords,
instance,
is_pedantic,
)?
.scale(&outline.glyph, outline.glyph_id)?;
scaled_outline.to_path(path_style, pen)?;
Ok(AdjustedMetrics {
has_overlaps: outline.has_overlaps,
lsb: Some(scaled_outline.adjusted_lsb().to_f32()),
advance_width: Some(scaled_outline.adjusted_advance_width().to_f32()),
})
})
}
(HinterKind::Cff(subfonts), OutlineKind::Cff(cff, glyph_id, subfont_ix)) => {
let Some(subfont) = subfonts.get(*subfont_ix as usize) else {
return Err(DrawError::NoSources);
};
cff.draw(subfont, *glyph_id, &self.coords, true, pen)?;
Ok(AdjustedMetrics::default())
}
_ => Err(DrawError::NoSources),
}
}
}
#[derive(Clone)]
enum HinterKind {
None,
Glyf(Box<glyf::HintInstance>),
Cff(Vec<cff::Subfont>),
}