我想遍历给定字体的所有字体表并打印/获取表内容属性和值,但我不知道该怎么做。
[dependencies]
write-fonts = { version = "*", features = ["read"] }
use write_fonts::read::FontRef;
fn main() {
let path_to_my_font_file = std::path::Path::new("/Users/ollimeier/Documents/Vary-Black.otf");
let font_bytes = std::fs::read(path_to_my_font_file).unwrap();
let font = FontRef::new(&font_bytes).expect("failed to read font data");
for table in font.table_directory.table_records() {
let tag = table.tag();
println!(" table {tag} ...");
println!(" {tag}: {:?}", table);
//for table_field in table {
// println!(" table field {table_field} ...");
//}
}
}
要从
table_directory
遍历表格,您必须调用方法 traverse
来获取 RecordResolver
,然后实现 SomeTable
为您提供一个可以迭代的 iter
:
use write_fonts::read::traversal::{SomeRecord, SomeTable};
use write_fonts::read::FontRef;
fn main() {
let font_bytes =
include_bytes!("/usr/share/fonts/adobe-source-code-pro/SourceCodePro-Black.otf");
let font = FontRef::new(font_bytes).expect("failed to read font data");
for table in font.table_directory.table_records() {
let tag = table.tag();
println!(" table {tag} ...");
println!(" {tag}: {:?}", table);
let table = table.clone().traverse(font.table_data(tag).unwrap());
for table_field in (&table as &dyn SomeTable).iter() {
println!("{}: {:?}", table_field.name, table_field.value);
}
}
}
库 API 确实很重要......