From 3c407a275a99c2ec2999f476b7e2ed2b24b83915 Mon Sep 17 00:00:00 2001 From: "G. Endignoux" Date: Thu, 21 Dec 2023 14:11:31 +0100 Subject: [PATCH] Add a function to list tags to the FontTableProvider trait. --- src/font_data.rs | 4 ++++ src/tables.rs | 18 ++++++++++++++++++ src/woff.rs | 7 +++++++ src/woff2.rs | 4 ++++ 4 files changed, 33 insertions(+) diff --git a/src/font_data.rs b/src/font_data.rs index d827ded..0050440 100644 --- a/src/font_data.rs +++ b/src/font_data.rs @@ -41,6 +41,10 @@ impl<'b> ReadBinary for FontData<'b> { } impl<'a> FontTableProvider for DynamicFontTableProvider<'a> { + fn table_tags(&self) -> Vec { + self.provider.table_tags() + } + fn table_data(&self, tag: u32) -> Result>, ParseError> { self.provider.table_data(tag) } diff --git a/src/tables.rs b/src/tables.rs index a56dd2f..c437af1 100644 --- a/src/tables.rs +++ b/src/tables.rs @@ -47,6 +47,9 @@ pub struct Fixed(i32); type LongDateTime = i64; pub trait FontTableProvider { + /// Returns the tags of all tables. + fn table_tags(&self) -> Vec; + /// Return data for the specified table if present fn table_data(&self, tag: u32) -> Result>, ParseError>; @@ -378,6 +381,10 @@ impl<'b> ReadBinary for OffsetTable<'b> { } impl<'a> FontTableProvider for OffsetTableFontProvider<'a> { + fn table_tags(&self) -> Vec { + self.offset_table.tags() + } + fn table_data(&self, tag: u32) -> Result>, ParseError> { self.offset_table .read_table(&self.scope, tag) @@ -421,6 +428,13 @@ impl WriteBinary<&Self> for TableRecord { } impl<'a> OffsetTable<'a> { + pub fn tags(&self) -> Vec { + self.table_records + .iter() + .map(|table_record| table_record.table_tag) + .collect() + } + pub fn find_table_record(&self, tag: u32) -> Option { for table_record in &self.table_records { if table_record.table_tag == tag { @@ -1305,6 +1319,10 @@ impl From for f32 { } impl FontTableProvider for Box { + fn table_tags(&self) -> Vec { + self.as_ref().table_tags() + } + fn table_data(&self, tag: u32) -> Result>, ParseError> { self.as_ref().table_data(tag) } diff --git a/src/woff.rs b/src/woff.rs index cbf322f..563d464 100644 --- a/src/woff.rs +++ b/src/woff.rs @@ -93,6 +93,13 @@ impl<'b> ReadBinary for WoffFont<'b> { } impl<'a> FontTableProvider for WoffFont<'a> { + fn table_tags(&self) -> Vec { + self.table_directory + .iter() + .map(|table_entry| table_entry.tag) + .collect() + } + fn table_data(&self, tag: u32) -> Result>, ParseError> { self.find_table_directory_entry(tag) .map(|table_entry| { diff --git a/src/woff2.rs b/src/woff2.rs index df5adfd..b07e574 100644 --- a/src/woff2.rs +++ b/src/woff2.rs @@ -234,6 +234,10 @@ impl<'b> ReadBinary for Woff2Font<'b> { } impl FontTableProvider for Woff2TableProvider { + fn table_tags(&self) -> Vec { + self.tables.keys().copied().collect() + } + fn table_data(&self, tag: u32) -> Result>, ParseError> { Ok(self.tables.get(&tag).map(|table| Cow::from(table.as_ref()))) }