mod config;
mod raster;
mod session;
#[cfg(test)]
mod tests;
pub use config::{
FontBudget, FontBudgetResource, FontError, TextDirection, TextLayoutBudget, TextStyle,
};
use config::{check, reserve};
pub use raster::RasterizedGlyph;
pub use session::{ShapedLineError, TextShapingSession};
#[derive(Debug, Clone)]
pub struct FontFace {
font: std::sync::Arc<ab_glyph::FontVec>,
source_capacity: usize,
glyph_count: usize,
}
impl FontFace {
pub fn from_bytes(bytes: Vec<u8>, budget: FontBudget) -> Result<Self, FontError> {
check(
FontBudgetResource::FontBytes,
bytes.capacity(),
budget.max_font_bytes(),
)?;
let face =
rustybuzz::ttf_parser::Face::parse(&bytes, 0).map_err(|_| FontError::InvalidFont)?;
let tables = face.tables();
if tables.glyf.is_none() && tables.cff.is_none() && tables.cff2.is_none() {
return Err(FontError::InvalidFont);
}
let glyph_count = usize::from(face.number_of_glyphs());
check(
FontBudgetResource::FontGlyphs,
glyph_count,
budget.max_font_glyphs(),
)?;
if face.units_per_em() == 0 || face.ascender() <= face.descender() {
return Err(FontError::InvalidFont);
}
let source_capacity = bytes.capacity();
let font = ab_glyph::FontVec::try_from_vec(bytes).map_err(|_| FontError::InvalidFont)?;
Ok(Self {
font: std::sync::Arc::new(font),
source_capacity,
glyph_count,
})
}
pub fn font_data(&self) -> &[u8] {
self.font.as_slice()
}
pub const fn allocation_bytes(&self) -> usize {
self.source_capacity
}
pub const fn glyph_count(&self) -> usize {
self.glyph_count
}
pub fn shape_line(
&self,
text: &str,
style: &TextStyle,
budget: &TextLayoutBudget,
) -> Result<ShapedLine, FontError> {
TextShapingSession::new(self, *style, *budget)?.shape_line(text)
}
pub fn rasterize_glyph(
&self,
glyph_id: u16,
style: &TextStyle,
budget: &TextLayoutBudget,
) -> Result<RasterizedGlyph, FontError> {
raster::rasterize(self, glyph_id, style, budget)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ShapedGlyph {
glyph_id: u16,
logical_x: f32,
logical_y: f32,
cluster: usize,
}
impl ShapedGlyph {
pub const fn glyph_id(self) -> u16 {
self.glyph_id
}
pub const fn logical_x(self) -> f32 {
self.logical_x
}
pub const fn logical_y(self) -> f32 {
self.logical_y
}
pub const fn cluster(self) -> usize {
self.cluster
}
}
#[derive(Debug)]
pub struct ShapedLine {
font: FontFace,
style: TextStyle,
text: String,
glyphs: Vec<ShapedGlyph>,
advance: f32,
ascent: f32,
descent: f32,
line_height: f32,
}
impl ShapedLine {
pub fn text(&self) -> &str {
&self.text
}
pub const fn font(&self) -> &FontFace {
&self.font
}
pub const fn style(&self) -> &TextStyle {
&self.style
}
pub fn validate_for(
&self,
font: &FontFace,
style: &TextStyle,
budget: &TextLayoutBudget,
) -> Result<(), ShapedLineError> {
self.validate_provenance(font, style)?;
check(
FontBudgetResource::TextBytes,
self.text.len(),
budget.max_text_bytes(),
)?;
check(
FontBudgetResource::ShapedGlyphs,
self.glyphs.len(),
budget.max_shaped_glyphs(),
)?;
Ok(())
}
pub(crate) fn validate_provenance(
&self,
font: &FontFace,
style: &TextStyle,
) -> Result<(), ShapedLineError> {
if !std::sync::Arc::ptr_eq(&self.font.font, &font.font) {
return Err(ShapedLineError::FontMismatch);
}
if self.style != *style {
return Err(ShapedLineError::StyleMismatch);
}
Ok(())
}
pub fn glyphs(&self) -> &[ShapedGlyph] {
&self.glyphs
}
pub const fn advance(&self) -> f32 {
self.advance
}
pub const fn ascent(&self) -> f32 {
self.ascent
}
pub const fn descent(&self) -> f32 {
self.descent
}
pub const fn line_height(&self) -> f32 {
self.line_height
}
pub fn allocation_bytes(&self) -> usize {
self.glyphs
.capacity()
.saturating_mul(std::mem::size_of::<ShapedGlyph>())
.saturating_add(self.text.capacity())
}
}
fn represent(value: f64) -> Result<f32, FontError> {
let result = value as f32;
if result.is_finite() {
Ok(result)
} else {
Err(FontError::InvalidGeometry)
}
}