Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for the STAT table. #166

Merged
merged 17 commits into from
Aug 22, 2024
Merged
90 changes: 90 additions & 0 deletions examples/font-info.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use ttf_parser::stat::{AxisValue, AxisValueTable};

fn main() {
let args: Vec<_> = std::env::args().collect();
if args.len() != 2 {
Expand Down Expand Up @@ -84,6 +86,94 @@ fn main() {
}
}

if let Some(stat) = face.tables().stat {
inferiorhumanorgans marked this conversation as resolved.
Show resolved Hide resolved
let axis_names = stat
.axes
.into_iter()
.map(|axis| axis.tag)
.collect::<Vec<_>>();

println!("Style attributes:");

println!(" Axes:");
for axis in axis_names.iter() {
println!(" {}", axis);
}

println!(" Axis Values:");
for table in stat.tables() {
match table {
AxisValueTable::Format1(table) => {
let value_name = face
.names()
.into_iter()
.filter(|name| name.name_id == table.value_name_id)
.map(|name| name.to_string().unwrap())
.collect::<Vec<_>>()
.join(", ");

let axis_name = &axis_names[table.axis_index as usize];
let value = table.value;
let flags = table.flags;

println!(" {axis_name} {value:?}={value_name:?} flags={flags:?}");
}
AxisValueTable::Format2(table) => {
let value_name = face
.names()
.into_iter()
.filter(|name| name.name_id == table.value_name_id)
.map(|name| name.to_string().unwrap())
.collect::<Vec<_>>()
.join(", ");

let axis_name = &axis_names[table.axis_index as usize];
let nominal_value = table.nominal_value;
let min_value = table.range_min_value;
let max_value = table.range_max_value;
let flags = table.flags;

println!(" {axis_name} {min_value:?}..{max_value:?}={value_name:?} nominal={nominal_value:?} flags={flags:?}");
}
AxisValueTable::Format3(table) => {
let value_name = face
.names()
.into_iter()
.filter(|name| name.name_id == table.value_name_id)
.map(|name| name.to_string().unwrap())
.collect::<Vec<_>>()
.join(", ");

let axis_name = &axis_names[table.axis_index as usize];
let value = table.value;
let linked_value = table.linked_value;
let flags = table.flags;

println!(
" {axis_name} {value:?}<=>{linked_value:?} = {value_name:?} flags={flags:?}",
);
}
AxisValueTable::Format4(table) => {
let value_name = face
.names()
.into_iter()
.filter(|name| name.name_id == table.value_name_id)
.map(|name| name.to_string().unwrap())
.collect::<Vec<_>>()
.join(", ");

let flags = table.flags;

println!(" {value_name:?} flags={flags:?}");
for pair in table.values {
let AxisValue { axis_index, value } = pair;
println!(" {axis_index} = {value:?}")
}
}
}
}
}

println!("Elapsed: {}us", now.elapsed().as_micros());
}

Expand Down
7 changes: 6 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ pub use tables::{ankr, feat, kerx, morx, trak};
pub use tables::{avar, cff2, fvar, gvar, hvar, mvar, vvar};
pub use tables::{cbdt, cblc, cff1 as cff, vhea};
pub use tables::{
cmap, colr, cpal, glyf, head, hhea, hmtx, kern, loca, maxp, name, os2, post, sbix, svg, vorg,
cmap, colr, cpal, glyf, head, hhea, hmtx, kern, loca, maxp, name, os2, post, sbix, stat, svg,
vorg,
};
#[cfg(feature = "opentype-layout")]
pub use tables::{gdef, gpos, gsub, math};
Expand Down Expand Up @@ -938,6 +939,7 @@ pub struct RawFaceTables<'a> {
pub os2: Option<&'a [u8]>,
pub post: Option<&'a [u8]>,
pub sbix: Option<&'a [u8]>,
pub stat: Option<&'a [u8]>,
pub svg: Option<&'a [u8]>,
pub vhea: Option<&'a [u8]>,
pub vmtx: Option<&'a [u8]>,
Expand Down Expand Up @@ -1008,6 +1010,7 @@ pub struct FaceTables<'a> {
pub os2: Option<os2::Table<'a>>,
pub post: Option<post::Table<'a>>,
pub sbix: Option<sbix::Table<'a>>,
pub stat: Option<stat::Table<'a>>,
pub svg: Option<svg::Table<'a>>,
pub vhea: Option<vhea::Table>,
pub vmtx: Option<hmtx::Table<'a>>,
Expand Down Expand Up @@ -1189,6 +1192,7 @@ impl<'a> Face<'a> {
b"name" => tables.name = table_data,
b"post" => tables.post = table_data,
b"sbix" => tables.sbix = table_data,
b"STAT" => tables.stat = table_data,
#[cfg(feature = "apple-layout")]
b"trak" => tables.trak = table_data,
b"vhea" => tables.vhea = table_data,
Expand Down Expand Up @@ -1305,6 +1309,7 @@ impl<'a> Face<'a> {
sbix: raw_tables
.sbix
.and_then(|data| sbix::Table::parse(maxp.number_of_glyphs, data)),
stat: raw_tables.stat.and_then(stat::Table::parse),
svg: raw_tables.svg.and_then(svg::Table::parse),
vhea: raw_tables.vhea.and_then(vhea::Table::parse),
vmtx,
Expand Down
1 change: 1 addition & 0 deletions src/tables/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub mod name;
pub mod os2;
pub mod post;
pub mod sbix;
pub mod stat;
pub mod svg;
pub mod vhea;
pub mod vorg;
Expand Down
Loading
Loading