#![doc(html_root_url = "https://docs.rs/ttf-parser/0.12.0")]
#![no_std]
#![forbid(unsafe_code)]
#![warn(missing_docs)]
#![warn(missing_copy_implementations)]
#![warn(missing_debug_implementations)]
#[cfg(feature = "std")]
#[macro_use]
extern crate std;
use core::fmt;
use core::num::NonZeroU16;
use core::ops::{Deref, DerefMut};
macro_rules! try_opt_or {
($value:expr, $ret:expr) => {
match $value {
Some(v) => v,
None => return $ret,
}
};
}
pub mod parser;
mod ggg;
mod tables;
#[cfg(feature = "variable-fonts")] mod var_store;
#[cfg(feature = "std")]
mod writer;
use tables::*;
use parser::{Stream, FromData, NumFrom, TryNumFrom, LazyArray16, LazyArrayIter16, Offset32, Offset};
use head::IndexToLocationFormat;
#[cfg(feature = "variable-fonts")] pub use fvar::{VariationAxes, VariationAxis};
pub use gdef::GlyphClass;
pub use ggg::*;
pub use name::*;
pub use os2::*;
pub use tables::{cmap, kern};
#[repr(transparent)]
#[derive(Clone, Copy, Ord, PartialOrd, Eq, PartialEq, Default, Debug)]
pub struct GlyphId(pub u16);
impl FromData for GlyphId {
const SIZE: usize = 2;
#[inline]
fn parse(data: &[u8]) -> Option<Self> {
u16::parse(data).map(GlyphId)
}
}
#[derive(Clone, Copy, PartialEq, Debug)]
enum Magic {
TrueType,
OpenType,
FontCollection,
}
impl FromData for Magic {
const SIZE: usize = 4;
#[inline]
fn parse(data: &[u8]) -> Option<Self> {
match u32::parse(data)? {
0x00010000 | 0x74727565 => Some(Magic::TrueType),
0x4F54544F => Some(Magic::OpenType),
0x74746366 => Some(Magic::FontCollection),
_ => None,
}
}
}
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Default, Debug)]
pub struct NormalizedCoordinate(i16);
impl From<i16> for NormalizedCoordinate {
#[inline]
fn from(n: i16) -> Self {
NormalizedCoordinate(parser::i16_bound(-16384, n, 16384))
}
}
impl From<f32> for NormalizedCoordinate {
#[inline]
fn from(n: f32) -> Self {
NormalizedCoordinate((parser::f32_bound(-1.0, n, 1.0) * 16384.0) as i16)
}
}
impl NormalizedCoordinate {
#[inline]
pub fn get(self) -> i16 {
self.0
}
}
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct Variation {
pub axis: Tag,
pub value: f32,
}
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Tag(pub u32);
impl Tag {
#[inline]
pub const fn from_bytes(bytes: &[u8; 4]) -> Self {
Tag(((bytes[0] as u32) << 24) | ((bytes[1] as u32) << 16) |
((bytes[2] as u32) << 8) | (bytes[3] as u32))
}
#[inline]
pub fn from_bytes_lossy(bytes: &[u8]) -> Self {
if bytes.is_empty() {
return Tag::from_bytes(&[0, 0, 0, 0]);
}
let mut iter = bytes.iter().cloned().chain(core::iter::repeat(b' '));
Tag::from_bytes(&[
iter.next().unwrap(),
iter.next().unwrap(),
iter.next().unwrap(),
iter.next().unwrap(),
])
}
#[inline]
pub const fn to_bytes(self) -> [u8; 4] {
[
(self.0 >> 24 & 0xff) as u8,
(self.0 >> 16 & 0xff) as u8,
(self.0 >> 8 & 0xff) as u8,
(self.0 >> 0 & 0xff) as u8,
]
}
#[inline]
pub const fn to_chars(self) -> [char; 4] {
[
(self.0 >> 24 & 0xff) as u8 as char,
(self.0 >> 16 & 0xff) as u8 as char,
(self.0 >> 8 & 0xff) as u8 as char,
(self.0 >> 0 & 0xff) as u8 as char,
]
}
#[inline]
pub const fn is_null(&self) -> bool {
self.0 == 0
}
#[inline]
pub const fn as_u32(&self) -> u32 {
self.0
}
#[inline]
pub fn to_lowercase(&self) -> Self {
let b = self.to_bytes();
Tag::from_bytes(&[
b[0].to_ascii_lowercase(),
b[1].to_ascii_lowercase(),
b[2].to_ascii_lowercase(),
b[3].to_ascii_lowercase(),
])
}
#[inline]
pub fn to_uppercase(&self) -> Self {
let b = self.to_bytes();
Tag::from_bytes(&[
b[0].to_ascii_uppercase(),
b[1].to_ascii_uppercase(),
b[2].to_ascii_uppercase(),
b[3].to_ascii_uppercase(),
])
}
}
impl core::fmt::Debug for Tag {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "Tag({})", self)
}
}
impl core::fmt::Display for Tag {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let b = self.to_chars();
write!(
f,
"{}{}{}{}",
b.get(0).unwrap_or(&' '),
b.get(1).unwrap_or(&' '),
b.get(2).unwrap_or(&' '),
b.get(3).unwrap_or(&' ')
)
}
}
impl FromData for Tag {
const SIZE: usize = 4;
#[inline]
fn parse(data: &[u8]) -> Option<Self> {
u32::parse(data).map(Tag)
}
}
#[repr(C)]
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct LineMetrics {
pub position: i16,
pub thickness: i16,
}
#[repr(C)]
#[derive(Clone, Copy, PartialEq, Debug)]
#[allow(missing_docs)]
pub struct Rect {
pub x_min: i16,
pub y_min: i16,
pub x_max: i16,
pub y_max: i16,
}
impl Rect {
#[inline]
pub fn width(&self) -> i16 {
self.x_max - self.x_min
}
#[inline]
pub fn height(&self) -> i16 {
self.y_max - self.y_min
}
}
#[derive(Clone, Copy, Debug)]
pub(crate) struct BBox {
x_min: f32,
y_min: f32,
x_max: f32,
y_max: f32,
}
impl BBox {
#[inline]
fn new() -> Self {
BBox {
x_min: core::f32::MAX,
y_min: core::f32::MAX,
x_max: core::f32::MIN,
y_max: core::f32::MIN,
}
}
#[inline]
fn is_default(&self) -> bool {
self.x_min == core::f32::MAX &&
self.y_min == core::f32::MAX &&
self.x_max == core::f32::MIN &&
self.y_max == core::f32::MIN
}
#[inline]
fn extend_by(&mut self, x: f32, y: f32) {
self.x_min = self.x_min.min(x);
self.y_min = self.y_min.min(y);
self.x_max = self.x_max.max(x);
self.y_max = self.y_max.max(y);
}
#[inline]
fn to_rect(&self) -> Option<Rect> {
Some(Rect {
x_min: i16::try_num_from(self.x_min)?,
y_min: i16::try_num_from(self.y_min)?,
x_max: i16::try_num_from(self.x_max)?,
y_max: i16::try_num_from(self.y_max)?,
})
}
}
pub trait OutlineBuilder {
fn move_to(&mut self, x: f32, y: f32);
fn line_to(&mut self, x: f32, y: f32);
fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32);
fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32);
fn close(&mut self);
}
struct DummyOutline;
impl OutlineBuilder for DummyOutline {
fn move_to(&mut self, _: f32, _: f32) {}
fn line_to(&mut self, _: f32, _: f32) {}
fn quad_to(&mut self, _: f32, _: f32, _: f32, _: f32) {}
fn curve_to(&mut self, _: f32, _: f32, _: f32, _: f32, _: f32, _: f32) {}
fn close(&mut self) {}
}
#[allow(missing_docs)]
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum RasterImageFormat {
PNG,
}
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct RasterGlyphImage<'a> {
pub x: i16,
pub y: i16,
pub width: u16,
pub height: u16,
pub pixels_per_em: u16,
pub format: RasterImageFormat,
pub data: &'a [u8],
}
#[repr(C)]
#[derive(Clone, Copy, PartialEq, Debug)]
#[allow(missing_docs)]
pub enum TableName {
AxisVariations = 0,
CharacterToGlyphIndexMapping,
ColorBitmapData,
ColorBitmapLocation,
CompactFontFormat,
CompactFontFormat2,
FontVariations,
GlyphData,
GlyphDefinition,
GlyphVariations,
Header,
HorizontalHeader,
HorizontalMetrics,
HorizontalMetricsVariations,
IndexToLocation,
Kerning,
MaximumProfile,
MetricsVariations,
Naming,
PostScript,
ScalableVectorGraphics,
StandardBitmapGraphics,
VerticalHeader,
VerticalMetrics,
VerticalMetricsVariations,
VerticalOrigin,
WindowsMetrics,
}
#[derive(Clone, Copy)]
struct TableRecord {
table_tag: Tag,
#[allow(dead_code)]
check_sum: u32,
offset: u32,
length: u32,
}
impl FromData for TableRecord {
const SIZE: usize = 16;
#[inline]
fn parse(data: &[u8]) -> Option<Self> {
let mut s = Stream::new(data);
Some(TableRecord {
table_tag: s.read::<Tag>()?,
check_sum: s.read::<u32>()?,
offset: s.read::<u32>()?,
length: s.read::<u32>()?,
})
}
}
#[cfg(feature = "variable-fonts")]
const MAX_VAR_COORDS: usize = 32;
#[cfg(feature = "variable-fonts")]
#[derive(Clone, Default)]
struct VarCoords {
data: [NormalizedCoordinate; MAX_VAR_COORDS],
len: u8,
}
#[cfg(feature = "variable-fonts")]
impl VarCoords {
#[inline]
fn as_slice(&self) -> &[NormalizedCoordinate] {
&self.data[0..usize::from(self.len)]
}
#[inline]
fn as_mut_slice(&mut self) -> &mut [NormalizedCoordinate] {
let end = usize::from(self.len);
&mut self.data[0..end]
}
}
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum FaceParsingError {
MalformedFont,
UnknownMagic,
FaceIndexOutOfBounds,
NoHeadTable,
NoHheaTable,
NoMaxpTable,
}
impl core::fmt::Display for FaceParsingError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
FaceParsingError::MalformedFont => write!(f, "malformed font"),
FaceParsingError::UnknownMagic => write!(f, "unknown magic"),
FaceParsingError::FaceIndexOutOfBounds => write!(f, "face index is out of bounds"),
FaceParsingError::NoHeadTable => write!(f, "the head table is missing or malformed"),
FaceParsingError::NoHheaTable => write!(f, "the hhea table is missing or malformed"),
FaceParsingError::NoMaxpTable => write!(f, "the maxp table is missing or malformed"),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for FaceParsingError {}
#[derive(Clone)]
pub struct Face<'a> {
font_data: &'a [u8],
table_records: LazyArray16<'a, TableRecord>,
internal: FaceTables<'a>,
}
impl<'a> Deref for Face<'a> {
type Target = FaceTables<'a>;
#[inline]
fn deref(&self) -> &Self::Target {
&self.internal
}
}
impl<'a> DerefMut for Face<'a> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.internal
}
}
#[derive(Clone)]
pub struct FaceTables<'a> {
cbdt: Option<&'a [u8]>,
cblc: Option<&'a [u8]>,
cff1: Option<cff1::Metadata<'a>>,
cmap: Option<cmap::Subtables<'a>>,
gdef: Option<gdef::Table<'a>>,
glyf: Option<&'a [u8]>,
head: &'a [u8],
hhea: &'a [u8],
hmtx: Option<hmtx::Table<'a>>,
kern: Option<kern::Subtables<'a>>,
loca: Option<loca::Table<'a>>,
name: Option<name::Names<'a>>,
os_2: Option<os2::Table<'a>>,
post: Option<post::Table<'a>>,
vhea: Option<&'a [u8]>,
vmtx: Option<hmtx::Table<'a>>,
sbix: Option<&'a [u8]>,
svg_: Option<&'a [u8]>,
vorg: Option<vorg::Table<'a>>,
#[cfg(feature = "variable-fonts")] avar: Option<avar::Table<'a>>,
#[cfg(feature = "variable-fonts")] cff2: Option<cff2::Metadata<'a>>,
#[cfg(feature = "variable-fonts")] fvar: Option<fvar::Table<'a>>,
#[cfg(feature = "variable-fonts")] gvar: Option<gvar::Table<'a>>,
#[cfg(feature = "variable-fonts")] hvar: Option<hvar::Table<'a>>,
#[cfg(feature = "variable-fonts")] mvar: Option<mvar::Table<'a>>,
#[cfg(feature = "variable-fonts")] vvar: Option<hvar::Table<'a>>,
number_of_glyphs: NonZeroU16,
#[cfg(feature = "variable-fonts")] coordinates: VarCoords,
}
impl fmt::Debug for FaceTables<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "FaceTables()")
}
}
impl<'a> Face<'a> {
pub fn from_slice(data: &'a [u8], index: u32) -> Result<Self, FaceParsingError> {
let mut s = Stream::new(data);
let magic: Magic = s.read().ok_or(FaceParsingError::UnknownMagic)?;
if magic == Magic::FontCollection {
s.skip::<u32>();
let number_of_faces: u32 = s.read().ok_or(FaceParsingError::MalformedFont)?;
let offsets = s.read_array32::<Offset32>(number_of_faces)
.ok_or(FaceParsingError::MalformedFont)?;
let face_offset = offsets.get(index).ok_or(FaceParsingError::FaceIndexOutOfBounds)?;
let face_offset = face_offset.to_usize().checked_sub(s.offset())
.ok_or(FaceParsingError::MalformedFont)?;
s.advance_checked(face_offset).ok_or(FaceParsingError::MalformedFont)?;
let magic: Magic = s.read().ok_or(FaceParsingError::UnknownMagic)?;
if magic == Magic::FontCollection {
return Err(FaceParsingError::UnknownMagic);
}
}
let num_tables: u16 = s.read().ok_or(FaceParsingError::MalformedFont)?;
s.advance(6);
let tables = s.read_array16::<TableRecord>(num_tables)
.ok_or(FaceParsingError::MalformedFont)?;
let internal = FaceTables::from_table_provider(
DefaultTableProvider {
tables: tables.into_iter(),
data
}
)?;
Ok(Face {
font_data: data,
table_records: tables,
internal,
})
}
pub fn table_data(&self, tag: Tag) -> Option<&'a [u8]> {
let (_, table) = self.table_records.binary_search_by(|record| record.table_tag.cmp(&tag))?;
let offset = usize::num_from(table.offset);
let length = usize::num_from(table.length);
let end = offset.checked_add(length)?;
self.font_data.get(offset..end)
}
}
impl<'a> FaceTables<'a> {
pub fn from_table_provider<T>(provider: T) -> Result<Self, FaceParsingError>
where T: Iterator<Item=Result<(Tag, Option<&'a [u8]>), FaceParsingError>>
{
let mut face = FaceTables {
cbdt: None,
cblc: None,
cff1: None,
cmap: None,
gdef: None,
glyf: None,
head: &[],
hhea: &[],
hmtx: None,
kern: None,
loca: None,
name: None,
os_2: None,
post: None,
vhea: None,
vmtx: None,
sbix: None,
svg_: None,
vorg: None,
#[cfg(feature = "variable-fonts")] avar: None,
#[cfg(feature = "variable-fonts")] cff2: None,
#[cfg(feature = "variable-fonts")] fvar: None,
#[cfg(feature = "variable-fonts")] gvar: None,
#[cfg(feature = "variable-fonts")] hvar: None,
#[cfg(feature = "variable-fonts")] mvar: None,
#[cfg(feature = "variable-fonts")] vvar: None,
number_of_glyphs: NonZeroU16::new(1).unwrap(),
#[cfg(feature = "variable-fonts")] coordinates: VarCoords::default(),
};
let mut number_of_glyphs = None;
let mut hmtx = None;
let mut vmtx = None;
let mut loca = None;
for table_tag_table_data in provider {
let (table_tag, table_data) = table_tag_table_data?;
match &table_tag.to_bytes() {
b"CBDT" => face.cbdt = table_data,
b"CBLC" => face.cblc = table_data,
b"CFF " => face.cff1 = table_data.and_then(|data| cff1::parse_metadata(data)),
#[cfg(feature = "variable-fonts")]
b"CFF2" => face.cff2 = table_data.and_then(|data| cff2::parse_metadata(data)),
b"GDEF" => face.gdef = table_data.and_then(|data| gdef::Table::parse(data)),
#[cfg(feature = "variable-fonts")]
b"HVAR" => face.hvar = table_data.and_then(|data| hvar::Table::parse(data)),
#[cfg(feature = "variable-fonts")]
b"MVAR" => face.mvar = table_data.and_then(|data| mvar::Table::parse(data)),
b"OS/2" => face.os_2 = table_data.and_then(|data| os2::Table::parse(data)),
b"SVG " => face.svg_ = table_data,
b"VORG" => face.vorg = table_data.and_then(|data| vorg::Table::parse(data)),
#[cfg(feature = "variable-fonts")]
b"VVAR" => face.vvar = table_data.and_then(|data| hvar::Table::parse(data)),
#[cfg(feature = "variable-fonts")]
b"avar" => face.avar = table_data.and_then(|data| avar::Table::parse(data)),
b"cmap" => face.cmap = table_data.and_then(|data| cmap::parse(data)),
#[cfg(feature = "variable-fonts")]
b"fvar" => face.fvar = table_data.and_then(|data| fvar::Table::parse(data)),
b"glyf" => face.glyf = table_data,
#[cfg(feature = "variable-fonts")]
b"gvar" => face.gvar = table_data.and_then(|data| gvar::Table::parse(data)),
b"head" => face.head = table_data.and_then(|data| head::parse(data)).unwrap_or_default(),
b"hhea" => face.hhea = table_data.and_then(|data| hhea::parse(data)).unwrap_or_default(),
b"hmtx" => hmtx = table_data,
b"kern" => face.kern = table_data.and_then(|data| kern::parse(data)),
b"loca" => loca = table_data,
b"maxp" => number_of_glyphs = table_data.and_then(|data| maxp::parse(data)),
b"name" => face.name = table_data.and_then(|data| name::parse(data)),
b"post" => face.post = table_data.and_then(|data| post::Table::parse(data)),
b"sbix" => face.sbix = table_data,
b"vhea" => face.vhea = table_data.and_then(|data| vhea::parse(data)),
b"vmtx" => vmtx = table_data,
_ => {}
}
}
if face.head.is_empty() {
return Err(FaceParsingError::NoHeadTable);
}
if face.hhea.is_empty() {
return Err(FaceParsingError::NoHheaTable);
}
face.number_of_glyphs = match number_of_glyphs {
Some(n) => n,
None => return Err(FaceParsingError::NoMaxpTable),
};
#[cfg(feature = "variable-fonts")] {
if let Some(ref fvar) = face.fvar {
face.coordinates.len = fvar.axes().count().min(MAX_VAR_COORDS) as u8;
}
}
if let Some(data) = hmtx {
if let Some(number_of_h_metrics) = hhea::number_of_h_metrics(face.hhea) {
face.hmtx = hmtx::Table::parse(data, number_of_h_metrics, face.number_of_glyphs);
}
}
if let (Some(vhea), Some(data)) = (face.vhea, vmtx) {
if let Some(number_of_v_metrics) = vhea::num_of_long_ver_metrics(vhea) {
face.vmtx = hmtx::Table::parse(data, number_of_v_metrics, face.number_of_glyphs);
}
}
if let Some(data) = loca {
if let Some(format) = head::index_to_loc_format(face.head) {
face.loca = loca::Table::parse(data, face.number_of_glyphs, format);
}
}
Ok(face)
}
#[cfg(feature = "variable-fonts")]
#[inline]
pub fn has_table(&self, name: TableName) -> bool {
match name {
TableName::Header => true,
TableName::HorizontalHeader => true,
TableName::MaximumProfile => true,
TableName::AxisVariations => self.avar.is_some(),
TableName::CharacterToGlyphIndexMapping => self.cmap.is_some(),
TableName::ColorBitmapData => self.cbdt.is_some(),
TableName::ColorBitmapLocation => self.cblc.is_some(),
TableName::CompactFontFormat => self.cff1.is_some(),
TableName::CompactFontFormat2 => self.cff2.is_some(),
TableName::FontVariations => self.fvar.is_some(),
TableName::GlyphData => self.glyf.is_some(),
TableName::GlyphDefinition => self.gdef.is_some(),
TableName::GlyphVariations => self.gvar.is_some(),
TableName::HorizontalMetrics => self.hmtx.is_some(),
TableName::HorizontalMetricsVariations => self.hvar.is_some(),
TableName::IndexToLocation => self.loca.is_some(),
TableName::Kerning => self.kern.is_some(),
TableName::MetricsVariations => self.mvar.is_some(),
TableName::Naming => self.name.is_some(),
TableName::PostScript => self.post.is_some(),
TableName::ScalableVectorGraphics => self.svg_.is_some(),
TableName::StandardBitmapGraphics => self.sbix.is_some(),
TableName::VerticalHeader => self.vhea.is_some(),
TableName::VerticalMetrics => self.vmtx.is_some(),
TableName::VerticalMetricsVariations => self.vvar.is_some(),
TableName::VerticalOrigin => self.vorg.is_some(),
TableName::WindowsMetrics => self.os_2.is_some(),
}
}
#[cfg(not(feature = "variable-fonts"))]
#[inline]
pub fn has_table(&self, name: TableName) -> bool {
match name {
TableName::Header => true,
TableName::HorizontalHeader => true,
TableName::MaximumProfile => true,
TableName::AxisVariations => false,
TableName::CharacterToGlyphIndexMapping => self.cmap.is_some(),
TableName::ColorBitmapData => self.cbdt.is_some(),
TableName::ColorBitmapLocation => self.cblc.is_some(),
TableName::CompactFontFormat => self.cff1.is_some(),
TableName::CompactFontFormat2 => false,
TableName::FontVariations => false,
TableName::GlyphData => self.glyf.is_some(),
TableName::GlyphDefinition => self.gdef.is_some(),
TableName::GlyphVariations => false,
TableName::HorizontalMetrics => self.hmtx.is_some(),
TableName::HorizontalMetricsVariations => false,
TableName::IndexToLocation => self.loca.is_some(),
TableName::Kerning => self.kern.is_some(),
TableName::MetricsVariations => false,
TableName::Naming => self.name.is_some(),
TableName::PostScript => self.post.is_some(),
TableName::ScalableVectorGraphics => self.svg_.is_some(),
TableName::StandardBitmapGraphics => self.sbix.is_some(),
TableName::VerticalHeader => self.vhea.is_some(),
TableName::VerticalMetrics => self.vmtx.is_some(),
TableName::VerticalMetricsVariations => false,
TableName::VerticalOrigin => self.vorg.is_some(),
TableName::WindowsMetrics => self.os_2.is_some(),
}
}
#[inline]
pub fn names(&self) -> Names {
self.name.unwrap_or_default()
}
#[inline]
pub fn is_regular(&self) -> bool {
try_opt_or!(self.os_2, false).is_regular()
}
#[inline]
pub fn is_italic(&self) -> bool {
try_opt_or!(self.os_2, false).is_italic()
}
#[inline]
pub fn is_bold(&self) -> bool {
try_opt_or!(self.os_2, false).is_bold()
}
#[inline]
pub fn is_oblique(&self) -> bool {
try_opt_or!(self.os_2, false).is_oblique()
}
#[inline]
pub fn is_monospaced(&self) -> bool {
try_opt_or!(self.post, false).is_monospaced()
}
#[inline]
pub fn is_variable(&self) -> bool {
#[cfg(feature = "variable-fonts")] {
self.fvar.is_some()
}
#[cfg(not(feature = "variable-fonts"))] {
false
}
}
#[inline]
pub fn weight(&self) -> Weight {
try_opt_or!(self.os_2, Weight::default()).weight()
}
#[inline]
pub fn width(&self) -> Width {
try_opt_or!(self.os_2, Width::default()).width()
}
#[inline]
pub fn italic_angle(&self) -> Option<f32> {
self.post.map(|table| table.italic_angle())
}
#[inline]
pub fn ascender(&self) -> i16 {
if let Some(os_2) = self.os_2 {
if os_2.is_use_typo_metrics() {
let v = os_2.typo_ascender();
self.apply_metrics_variation(Tag::from_bytes(b"hasc"), v)
} else {
let v = os_2.windows_ascender();
self.apply_metrics_variation(Tag::from_bytes(b"hcla"), v)
}
} else {
hhea::ascender(self.hhea)
}
}
#[inline]
pub fn descender(&self) -> i16 {
if let Some(os_2) = self.os_2 {
if os_2.is_use_typo_metrics() {
let v = os_2.typo_descender();
self.apply_metrics_variation(Tag::from_bytes(b"hdsc"), v)
} else {
let v = os_2.windows_descender();
self.apply_metrics_variation(Tag::from_bytes(b"hcld"), v)
}
} else {
hhea::descender(self.hhea)
}
}
#[inline]
pub fn height(&self) -> i16 {
self.ascender() - self.descender()
}
#[inline]
pub fn line_gap(&self) -> i16 {
if let Some(os_2) = self.os_2 {
if os_2.is_use_typo_metrics() {
let v = os_2.typo_line_gap();
self.apply_metrics_variation(Tag::from_bytes(b"hlgp"), v)
} else {
hhea::line_gap(self.hhea)
}
} else {
hhea::line_gap(self.hhea)
}
}
#[inline]
pub fn typographic_ascender(&self) -> Option<i16> {
self.os_2.map(|table| {
let v = table.typo_ascender();
self.apply_metrics_variation(Tag::from_bytes(b"hasc"), v)
})
}
#[inline]
pub fn typographic_descender(&self) -> Option<i16> {
self.os_2.map(|table| {
let v = table.typo_descender();
self.apply_metrics_variation(Tag::from_bytes(b"hdsc"), v)
})
}
#[inline]
pub fn typographic_line_gap(&self) -> Option<i16> {
self.os_2.map(|table| {
let v = table.typo_line_gap();
self.apply_metrics_variation(Tag::from_bytes(b"hlgp"), v)
})
}
#[inline]
pub fn vertical_ascender(&self) -> Option<i16> {
self.vhea.map(vhea::ascender)
.map(|v| self.apply_metrics_variation(Tag::from_bytes(b"vasc"), v))
}
#[inline]
pub fn vertical_descender(&self) -> Option<i16> {
self.vhea.map(vhea::descender)
.map(|v| self.apply_metrics_variation(Tag::from_bytes(b"vdsc"), v))
}
#[inline]
pub fn vertical_height(&self) -> Option<i16> {
Some(self.vertical_ascender()? - self.vertical_descender()?)
}
#[inline]
pub fn vertical_line_gap(&self) -> Option<i16> {
self.vhea.map(vhea::line_gap)
.map(|v| self.apply_metrics_variation(Tag::from_bytes(b"vlgp"), v))
}
#[inline]
pub fn units_per_em(&self) -> Option<u16> {
head::units_per_em(self.head)
}
#[inline]
pub fn x_height(&self) -> Option<i16> {
self.os_2.and_then(|os_2| os_2.x_height())
.map(|v| self.apply_metrics_variation(Tag::from_bytes(b"xhgt"), v))
}
#[inline]
pub fn capital_height(&self) -> Option<i16> {
self.os_2.and_then(|os_2| os_2.cap_height())
.map(|v| self.apply_metrics_variation(Tag::from_bytes(b"cpht"), v))
}
#[inline]
pub fn underline_metrics(&self) -> Option<LineMetrics> {
let mut metrics = self.post?.underline_metrics();
if self.is_variable() {
self.apply_metrics_variation_to(Tag::from_bytes(b"undo"), &mut metrics.position);
self.apply_metrics_variation_to(Tag::from_bytes(b"unds"), &mut metrics.thickness);
}
Some(metrics)
}
#[inline]
pub fn strikeout_metrics(&self) -> Option<LineMetrics> {
let mut metrics = self.os_2?.strikeout_metrics();
if self.is_variable() {
self.apply_metrics_variation_to(Tag::from_bytes(b"stro"), &mut metrics.position);
self.apply_metrics_variation_to(Tag::from_bytes(b"strs"), &mut metrics.thickness);
}
Some(metrics)
}
#[inline]
pub fn subscript_metrics(&self) -> Option<ScriptMetrics> {
let mut metrics = self.os_2?.subscript_metrics();
if self.is_variable() {
self.apply_metrics_variation_to(Tag::from_bytes(b"sbxs"), &mut metrics.x_size);
self.apply_metrics_variation_to(Tag::from_bytes(b"sbys"), &mut metrics.y_size);
self.apply_metrics_variation_to(Tag::from_bytes(b"sbxo"), &mut metrics.x_offset);
self.apply_metrics_variation_to(Tag::from_bytes(b"sbyo"), &mut metrics.y_offset);
}
Some(metrics)
}
#[inline]
pub fn superscript_metrics(&self) -> Option<ScriptMetrics> {
let mut metrics = self.os_2?.superscript_metrics();
if self.is_variable() {
self.apply_metrics_variation_to(Tag::from_bytes(b"spxs"), &mut metrics.x_size);
self.apply_metrics_variation_to(Tag::from_bytes(b"spys"), &mut metrics.y_size);
self.apply_metrics_variation_to(Tag::from_bytes(b"spxo"), &mut metrics.x_offset);
self.apply_metrics_variation_to(Tag::from_bytes(b"spyo"), &mut metrics.y_offset);
}
Some(metrics)
}
#[inline]
pub fn number_of_glyphs(&self) -> u16 {
self.number_of_glyphs.get()
}
#[inline]
pub fn character_mapping_subtables(&self) -> cmap::Subtables {
self.cmap.unwrap_or_default()
}
#[inline]
pub fn glyph_index(&self, c: char) -> Option<GlyphId> {
for encoding in self.character_mapping_subtables() {
if !encoding.is_unicode() {
continue;
}
if let Some(id) = encoding.glyph_index(u32::from(c)) {
return Some(id);
}
}
None
}
#[inline]
pub fn glyph_variation_index(&self, c: char, variation: char) -> Option<GlyphId> {
let res = self.character_mapping_subtables()
.find(|e| e.format() == cmap::Format::UnicodeVariationSequences)
.and_then(|e| e.glyph_variation_index(c, variation))?;
match res {
cmap::GlyphVariationResult::Found(v) => Some(v),
cmap::GlyphVariationResult::UseDefault => self.glyph_index(c),
}
}
#[inline]
pub fn glyph_hor_advance(&self, glyph_id: GlyphId) -> Option<u16> {
#[cfg(feature = "variable-fonts")] {
let mut advance = self.hmtx?.advance(glyph_id)? as f32;
if self.is_variable() {
if let Some(hvar_data) = self.hvar {
advance += hvar::glyph_advance_offset(hvar_data, glyph_id, self.coords())? + 0.5;
}
}
u16::try_num_from(advance)
}
#[cfg(not(feature = "variable-fonts"))] {
self.hmtx?.advance(glyph_id)
}
}
#[inline]
pub fn glyph_ver_advance(&self, glyph_id: GlyphId) -> Option<u16> {
#[cfg(feature = "variable-fonts")] {
let mut advance = self.vmtx?.advance(glyph_id)? as f32;
if self.is_variable() {
if let Some(vvar_data) = self.vvar {
advance += hvar::glyph_advance_offset(vvar_data, glyph_id, self.coords())? + 0.5;
}
}
u16::try_num_from(advance)
}
#[cfg(not(feature = "variable-fonts"))] {
self.vmtx?.advance(glyph_id)
}
}
#[inline]
pub fn glyph_hor_side_bearing(&self, glyph_id: GlyphId) -> Option<i16> {
#[cfg(feature = "variable-fonts")] {
let mut bearing = self.hmtx?.side_bearing(glyph_id)? as f32;
if self.is_variable() {
if let Some(hvar_data) = self.hvar {
bearing += hvar::glyph_side_bearing_offset(hvar_data, glyph_id, self.coords())? + 0.5;
}
}
i16::try_num_from(bearing)
}
#[cfg(not(feature = "variable-fonts"))] {
self.hmtx?.side_bearing(glyph_id)
}
}
#[inline]
pub fn glyph_ver_side_bearing(&self, glyph_id: GlyphId) -> Option<i16> {
#[cfg(feature = "variable-fonts")] {
let mut bearing = self.vmtx?.side_bearing(glyph_id)? as f32;
if self.is_variable() {
if let Some(vvar_data) = self.vvar {
bearing += hvar::glyph_side_bearing_offset(vvar_data, glyph_id, self.coords())? + 0.5;
}
}
i16::try_num_from(bearing)
}
#[cfg(not(feature = "variable-fonts"))] {
self.vmtx?.side_bearing(glyph_id)
}
}
pub fn glyph_y_origin(&self, glyph_id: GlyphId) -> Option<i16> {
self.vorg.map(|vorg| vorg.glyph_y_origin(glyph_id))
}
#[inline]
pub fn glyph_name(&self, glyph_id: GlyphId) -> Option<&str> {
if let Some(name) = self.post.and_then(|post| post.glyph_name(glyph_id)) {
return Some(name);
}
if let Some(name) = self.cff1.as_ref().and_then(|cff1| cff1::glyph_name(cff1, glyph_id)) {
return Some(name);
}
None
}
pub fn has_glyph_classes(&self) -> bool {
try_opt_or!(self.gdef, false).has_glyph_classes()
}
pub fn glyph_class(&self, glyph_id: GlyphId) -> Option<GlyphClass> {
self.gdef.and_then(|gdef| gdef.glyph_class(glyph_id))
}
pub fn glyph_mark_attachment_class(&self, glyph_id: GlyphId) -> Class {
try_opt_or!(self.gdef, Class(0)).glyph_mark_attachment_class(glyph_id)
}
#[inline]
pub fn is_mark_glyph(&self, glyph_id: GlyphId, set_index: Option<u16>) -> bool {
try_opt_or!(self.gdef, false).is_mark_glyph(glyph_id, set_index)
}
#[cfg(feature = "variable-fonts")]
#[inline]
pub fn glyph_variation_delta(&self, outer_index: u16, inner_index: u16) -> Option<f32> {
self.gdef.and_then(|gdef|
gdef.variation_delta(outer_index, inner_index, self.coordinates.as_slice()))
}
pub fn kerning_subtables(&self) -> kern::Subtables {
self.kern.unwrap_or_default()
}
#[inline]
pub fn outline_glyph(
&self,
glyph_id: GlyphId,
builder: &mut dyn OutlineBuilder,
) -> Option<Rect> {
#[cfg(feature = "variable-fonts")] {
if let Some(ref gvar_table) = self.gvar {
return gvar::outline(self.loca?, self.glyf?, gvar_table, self.coords(), glyph_id, builder);
}
}
if let Some(glyf_table) = self.glyf {
return glyf::outline(self.loca?, glyf_table, glyph_id, builder);
}
if let Some(ref metadata) = self.cff1 {
return cff1::outline(metadata, glyph_id, builder);
}
#[cfg(feature = "variable-fonts")] {
if let Some(ref metadata) = self.cff2 {
return cff2::outline(metadata, self.coords(), glyph_id, builder);
}
}
None
}
#[inline]
pub fn glyph_bounding_box(&self, glyph_id: GlyphId) -> Option<Rect> {
#[cfg(feature = "variable-fonts")]
{
if !self.is_variable() {
if let Some(glyf_table) = self.glyf {
return glyf::glyph_bbox(self.loca?, glyf_table, glyph_id);
}
}
}
#[cfg(not(feature = "variable-fonts"))]
{
if let Some(glyf_table) = self.glyf {
return glyf::glyph_bbox(self.loca?, glyf_table, glyph_id);
}
}
self.outline_glyph(glyph_id, &mut DummyOutline)
}
#[inline]
pub fn global_bounding_box(&self) -> Rect {
head::global_bbox(self.head).unwrap()
}
#[inline]
pub fn glyph_raster_image(&self, glyph_id: GlyphId, pixels_per_em: u16) -> Option<RasterGlyphImage> {
if let Some(sbix_data) = self.sbix {
return sbix::parse(sbix_data, self.number_of_glyphs, glyph_id, pixels_per_em, 0);
}
if let (Some(cblc_data), Some(cbdt_data)) = (self.cblc, self.cbdt) {
let location = cblc::find_location(cblc_data, glyph_id, pixels_per_em)?;
return cbdt::parse(cbdt_data, location);
}
None
}
#[inline]
pub fn glyph_svg_image(&self, glyph_id: GlyphId) -> Option<&'a [u8]> {
self.svg_.and_then(|svg_data| svg::parse(svg_data, glyph_id))
}
#[cfg(feature = "variable-fonts")]
#[inline]
pub fn variation_axes(&self) -> VariationAxes {
self.fvar.map(|fvar| fvar.axes()).unwrap_or_default()
}
#[cfg(feature = "variable-fonts")]
pub fn set_variation(&mut self, axis: Tag, value: f32) -> Option<()> {
if !self.is_variable() {
return None;
}
let v = self.variation_axes().enumerate().find(|(_, a)| a.tag == axis);
if let Some((idx, a)) = v {
if idx >= MAX_VAR_COORDS {
return None;
}
self.coordinates.data[idx] = a.normalized_value(value);
} else {
return None;
}
if let Some(avar) = self.avar {
let _ = avar.map_coordinates(self.coordinates.as_mut_slice());
}
Some(())
}
#[cfg(feature = "variable-fonts")]
#[inline]
pub fn variation_coordinates(&self) -> &[NormalizedCoordinate] {
self.coordinates.as_slice()
}
#[cfg(feature = "variable-fonts")]
#[inline]
pub fn has_non_default_variation_coordinates(&self) -> bool {
self.coordinates.as_slice().iter().any(|c| c.0 != 0)
}
#[cfg(feature = "variable-fonts")]
#[inline]
fn metrics_var_offset(&self, tag: Tag) -> f32 {
self.mvar.and_then(|table| table.metrics_offset(tag, self.coords())).unwrap_or(0.0)
}
#[inline]
fn apply_metrics_variation(&self, tag: Tag, mut value: i16) -> i16 {
self.apply_metrics_variation_to(tag, &mut value);
value
}
#[cfg(feature = "variable-fonts")]
#[inline]
fn apply_metrics_variation_to(&self, tag: Tag, value: &mut i16) {
if self.is_variable() {
let v = f32::from(*value) + self.metrics_var_offset(tag);
if let Some(v) = i16::try_num_from(v) {
*value = v;
}
}
}
#[cfg(not(feature = "variable-fonts"))]
#[inline]
fn apply_metrics_variation_to(&self, _: Tag, _: &mut i16) {
}
#[cfg(feature = "variable-fonts")]
#[inline]
fn coords(&self) -> &[NormalizedCoordinate] {
self.coordinates.as_slice()
}
}
struct DefaultTableProvider<'a> {
data: &'a [u8],
tables: LazyArrayIter16<'a, TableRecord>,
}
impl<'a> Iterator for DefaultTableProvider<'a> {
type Item = Result<(Tag, Option<&'a [u8]>), FaceParsingError>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.tables.next().map(|table| {
Ok((table.table_tag, {
let offset = usize::num_from(table.offset);
let length = usize::num_from(table.length);
let end = offset.checked_add(length).ok_or(FaceParsingError::MalformedFont)?;
let range = offset..end;
self.data.get(range)
}))
})
}
}
impl fmt::Debug for Face<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Face()")
}
}
#[inline]
pub fn fonts_in_collection(data: &[u8]) -> Option<u32> {
let mut s = Stream::new(data);
if s.read::<Magic>()? != Magic::FontCollection {
return None;
}
s.skip::<u32>();
s.read::<u32>()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn empty_font() {
assert_eq!(Face::from_slice(&[], 0).unwrap_err(),
FaceParsingError::UnknownMagic);
}
#[test]
fn zero_tables() {
let data = &[
0x00, 0x01, 0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
];
assert_eq!(Face::from_slice(data, 0).unwrap_err(),
FaceParsingError::NoHeadTable);
}
#[test]
fn tables_count_overflow() {
let data = &[
0x00, 0x01, 0x00, 0x00,
0xFF, 0xFF,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
];
assert_eq!(Face::from_slice(data, 0).unwrap_err(),
FaceParsingError::MalformedFont);
}
#[test]
fn empty_font_collection() {
let data = &[
0x74, 0x74, 0x63, 0x66,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
];
assert_eq!(fonts_in_collection(data), Some(0));
assert_eq!(Face::from_slice(data, 0).unwrap_err(),
FaceParsingError::FaceIndexOutOfBounds);
}
#[test]
fn font_collection_num_fonts_overflow() {
let data = &[
0x74, 0x74, 0x63, 0x66,
0x00, 0x00,
0x00, 0x00,
0xFF, 0xFF, 0xFF, 0xFF,
];
assert_eq!(fonts_in_collection(data), Some(std::u32::MAX));
assert_eq!(Face::from_slice(data, 0).unwrap_err(),
FaceParsingError::MalformedFont);
}
#[test]
fn font_index_overflow() {
let data = &[
0x74, 0x74, 0x63, 0x66,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x0C,
];
assert_eq!(fonts_in_collection(data), Some(1));
assert_eq!(Face::from_slice(data, std::u32::MAX).unwrap_err(),
FaceParsingError::FaceIndexOutOfBounds);
}
}