use super::{preludes::*, ImageItem, PathStyle};
use crate::vector::vm::{GroupContext, TransformContext};
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
#[cfg_attr(feature = "rkyv", derive(Archive, rDeser, rSer))]
#[cfg_attr(feature = "rkyv-validation", archive(check_bytes))]
pub enum FlatGlyphItem {
None,
Image(Arc<ImageGlyphItem>),
Outline(Arc<OutlineGlyphItem>),
}
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
#[cfg_attr(feature = "rkyv", derive(Archive, rDeser, rSer))]
#[cfg_attr(feature = "rkyv-validation", archive(check_bytes))]
pub struct ImageGlyphItem {
pub ts: Transform,
pub image: ImageItem,
pub ligature_len: u8,
}
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
#[cfg_attr(feature = "rkyv", derive(Archive, rDeser, rSer))]
#[cfg_attr(feature = "rkyv-validation", archive(check_bytes))]
pub struct OutlineGlyphItem {
pub ts: Option<Box<Transform>>,
pub d: ImmutStr,
pub ligature_len: u8,
}
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
#[cfg_attr(feature = "rkyv", derive(Archive, rDeser, rSer))]
#[cfg_attr(feature = "rkyv-validation", archive(check_bytes))]
pub struct FontItem {
pub fingerprint: Fingerprint,
pub family: ImmutStr,
pub hash: u32,
pub cap_height: Abs,
pub ascender: Abs,
pub descender: Abs,
pub units_per_em: Abs,
pub vertical: bool,
pub glyphs: Vec<Arc<FlatGlyphItem>>,
#[cfg_attr(feature = "rkyv", with(rkyv::with::Skip))]
pub glyph_cov: bitvec::vec::BitVec<u32>,
}
impl FontItem {
pub fn get_glyph(&self, glyph_id: u32) -> Option<&Arc<FlatGlyphItem>> {
self.glyphs.get(glyph_id as usize)
}
}
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
#[cfg_attr(feature = "rkyv", derive(Archive, rDeser, rSer))]
#[cfg_attr(feature = "rkyv-validation", archive(check_bytes))]
pub struct TextShape {
pub font: FontRef,
pub dir: ImmutStr,
pub size: Scalar,
pub styles: Vec<PathStyle>,
}
impl TextShape {
pub fn ppem(&self, upem: f32) -> Scalar {
Scalar(self.size.0 / upem)
}
pub fn inv_ppem(&self, upem: f32) -> Scalar {
Scalar(upem / self.size.0)
}
pub fn add_transform<C, T: GroupContext<C> + TransformContext<C>>(
&self,
ctx: &mut C,
group_ctx: T,
upem: Scalar,
) -> T {
let ppem = self.ppem(upem.0);
group_ctx.transform_scale(ctx, ppem, -ppem)
}
#[inline]
pub(crate) fn render_glyphs<'a, 'b: 'a>(
&self,
upem: Abs,
glyph_iter: impl Iterator<Item = &'a (Axes<Abs>, Axes<Abs>, u32)> + 'a,
size: &'b mut Axes<f32>,
) -> impl Iterator<Item = (Axes<Scalar>, u32)> + 'a {
*size = Axes { x: 0f32, y: 0f32 };
let inv_ppem = self.inv_ppem(upem.0).0;
glyph_iter.into_iter().map(move |(offset, advance, glyph)| {
let offset = Axes {
x: size.x + offset.x.0,
y: size.y + offset.y.0,
};
let ts = Axes {
x: Scalar(offset.x * inv_ppem),
y: Scalar(offset.y * inv_ppem),
};
size.x += advance.x.0;
size.y += advance.y.0;
(ts, *glyph)
})
}
}
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
#[cfg_attr(feature = "rkyv", derive(Archive, rDeser, rSer))]
#[cfg_attr(feature = "rkyv-validation", archive(check_bytes))]
pub struct TextItem {
pub shape: Arc<TextShape>,
pub content: Arc<TextItemContent>,
}
impl TextItem {
pub fn width(&self) -> Abs {
Scalar(
self.content
.glyphs
.iter()
.map(|(_, advance, _)| advance.x.0)
.sum(),
)
}
pub fn render_glyphs<'a, 'b: 'a>(
&'a self,
upem: Abs,
size: &'b mut Axes<f32>,
) -> impl Iterator<Item = (Axes<Scalar>, u32)> + 'a {
self.shape
.render_glyphs(upem, self.content.glyphs.iter(), size)
}
}
type GlyphData = (Axes<Abs>, Axes<Abs>, u32);
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
#[cfg_attr(feature = "rkyv", derive(Archive, rDeser, rSer))]
#[cfg_attr(feature = "rkyv-validation", archive(check_bytes))]
pub struct TextItemContent {
pub content: ImmutStr,
pub glyphs: Arc<[GlyphData]>,
}