mod hint;
mod read;
const MAX_COORD: i32 = 1 << 24;
fn bounded(value: i64) -> Result<i32> {
if value.unsigned_abs() > MAX_COORD as u64 {
Err(invalid("TrueType numeric limit exceeded"))
} else {
Ok(value as i32)
}
}
pub use read::Font;
use std::{error, fmt};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Error(pub String);
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl error::Error for Error {}
type Result<T> = std::result::Result<T, Error>;
fn invalid(message: &str) -> Error {
Error(message.into())
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Size {
pub x: u16,
pub y: u16,
}
impl Size {
pub fn new(x: u16, y: u16) -> Result<Self> {
if x == 0 || y == 0 || x > 4096 || y > 4096 {
return Err(invalid("TrueType size must be in 1..=4096 dots per em"));
}
Ok(Self { x, y })
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Point {
pub x: i32,
pub y: i32,
pub on_curve: bool,
}
#[derive(Clone, Debug)]
pub struct Outline {
pub contours: Vec<Vec<Point>>,
pub advance: i32,
pub linear_advance: i32,
pub scan_control: u16,
pub scan_type: u16,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Hinting {
None,
Native,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Environment {
#[default]
Standard,
Zebra203 { quarter_turns: u8 },
Zd621V93 { quarter_turns: u8 },
}
impl Environment {
pub(super) fn cvt_axis(self, size: Size) -> u16 {
if matches!(self, Self::Zd621V93 { .. }) {
size.x.max(size.y)
} else {
size.y
}
}
pub(super) fn cvt_ratio(self, dots: u16, base: u16) -> f64 {
let ratio = self.ppem(dots, false) / self.ppem(base, false);
if matches!(self, Self::Zd621V93 { .. }) {
(ratio * 65536.).round() / 65536.
} else {
ratio
}
}
pub(super) fn ppem(self, dots: u16, layout: bool) -> f64 {
if matches!(self, Self::Zd621V93 { .. }) {
if layout {
return f64::from(dots.max(10)) * 204. / 203.;
}
let numerator = u32::from(dots.max(10)) * 1152;
let points = numerator / 203;
f64::from(points) * 184958. / (16. * 65536.)
} else {
f64::from(dots)
}
}
pub(super) fn scale(self, value: i32, dots: u16, units: u16) -> Result<i32> {
if matches!(self, Self::Zd621V93 { .. }) {
let points = i64::from(dots.max(10)) * 1152 / 203;
let binary_scale = (points / 45) as u64;
if points % 45 == 0
&& binary_scale.is_power_of_two()
&& (u64::from(units) * 32) % binary_scale == 0
{
let denominator = (u64::from(units) * 32 / binary_scale) as i32;
let result = if (denominator as u32).is_power_of_two() {
value.wrapping_mul(16_256).wrapping_add(denominator / 2)
>> denominator.trailing_zeros()
} else {
value
.wrapping_abs()
.wrapping_mul(16_256)
.wrapping_add(denominator / 2)
/ denominator
* value.signum()
};
return bounded(i64::from(result));
}
let ppem = (points * 508 * 65_536 + 1440) / 2880;
let coefficient = (ppem * 64 + i64::from(units) / 2) / i64::from(units);
let result = (i64::from(value).abs() * coefficient + 32_768) / 65_536;
return bounded(result * i64::from(value.signum()));
}
bounded((f64::from(value) * self.ppem(dots, false) * 64. / f64::from(units)).round() as i64)
}
}
pub struct Instance<'a> {
font: &'a Font<'a>,
size: Size,
hinting: Hinting,
environment: Environment,
vm: hint::Vm,
}
impl<'a> Instance<'a> {
pub fn layout_advance(&self, glyph: u16) -> Result<u32> {
let (advance, _) = self.font.metric(glyph)?;
let dots = (f64::from(advance) * self.environment.ppem(self.size.x, true)
/ f64::from(self.font.units))
.round();
if !(0. ..=4096.).contains(&dots) {
return Err(invalid("TrueType layout advance limit exceeded"));
}
Ok(dots as u32)
}
pub fn outline(&self, glyph: u16) -> Result<Outline> {
let (mut outline, origin) =
self.font
.outline(self, glyph, &mut Vec::new(), &mut read::Budget::default())?;
for point in outline.contours.iter_mut().flatten() {
point.x = bounded(point.x as i64 - origin as i64)?;
}
Ok(outline)
}
pub fn glyph(&self, character: char) -> Result<Outline> {
let glyph = self
.font
.glyph_index(character)
.ok_or_else(|| invalid("character absent from TrueType cmap"))?;
self.outline(glyph)
}
}