Skip to content

Commit

Permalink
Simplify Table
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanUkhov committed Feb 2, 2024
1 parent 268ea6c commit a68197d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 72 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "opentype"
version = "0.33.0"
version = "0.34.0"
edition = "2021"
license = "Apache-2.0/MIT"
authors = [
Expand Down
22 changes: 17 additions & 5 deletions src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,31 @@ impl Font {

/// Read a table.
#[inline]
pub fn take<'l, T, U>(&self, tape: &mut T) -> Result<Option<U>>
pub fn take<T, U>(&self, tape: &mut T) -> Result<Option<U>>
where
T: crate::tape::Read,
U: Table<'l, Parameter = ()>,
U: Table + crate::value::Read,
{
self.take_given(tape, ())
self.position::<T, U>(tape)?
.map(|_| tape.take::<U>())
.transpose()
}

/// Read a table given a parameter.
pub fn take_given<'l, T, U>(&self, tape: &mut T, parameter: U::Parameter) -> Result<Option<U>>
where
T: crate::tape::Read,
U: Table<'l>,
U: Table + crate::walue::Read<'l>,
{
self.position::<T, U>(tape)?
.map(|_| tape.take_given::<U>(parameter))
.transpose()
}

fn position<T, U>(&self, tape: &mut T) -> Result<Option<()>>
where
T: crate::tape::Read,
U: Table,
{
let tag = U::tag();
for record in &self.offsets.records {
Expand All @@ -43,7 +55,7 @@ impl Font {
raise!("found a malformed font table with {:?}", record.tag);
}
Read::jump(tape, record.offset as u64)?;
return Ok(Some(Table::take(tape, parameter)?));
return Ok(Some(()));
}
}
Ok(None)
Expand Down
89 changes: 23 additions & 66 deletions src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,82 +8,39 @@ use truetype::{self, Tag};
use crate::tables::{
ColorPalettes, FontVariations, GlyphDefinition, GlyphPositioning, GlyphSubstitution,
};
use crate::Result;

/// A type representing a font table.
pub trait Table<'l>: Sized {
#[doc(hidden)]
type Parameter;

pub trait Table {
#[doc(hidden)]
fn tag() -> Tag;

#[doc(hidden)]
fn take<T>(tape: &mut T, parameter: Self::Parameter) -> Result<Self>
where
T: crate::tape::Read;
}

macro_rules! table {
(@one $tag:expr => opentype::$type:ident()) => (
table! { @one $tag => truetype::$type() }
);
(@one $tag:expr => $type:ident()) => (
impl Table<'static> for $type {
type Parameter = ();

#[inline]
fn tag() -> Tag {
Tag(*$tag)
}

#[inline]
fn take<T>(tape: &mut T, _: Self::Parameter) -> Result<Self>
where
T: $crate::tape::Read,
{
$crate::tape::Read::take(tape)
}
}
);
(@one $tag:expr => $type:ident(..)) => (
impl<'l> Table<'l> for $type {
type Parameter = <$type as $crate::walue::Read<'l>>::Parameter;

macro_rules! implement {
($($tag:expr => $type:ident,)+) => {
$(impl Table for $type {
#[inline]
fn tag() -> Tag {
Tag(*$tag)
}

#[inline]
fn take<T>(tape: &mut T, parameter: Self::Parameter) -> Result<Self>
where
T: $crate::tape::Read,
{
$crate::tape::Read::take_given(tape, parameter)
}
}
);
($($tag:expr => $type:ident($($parameter:tt)*),)+) => (
$(table! { @one $tag => $type($($parameter)*) })+
);
})+
};
}

table! {
b"CFF " => FontSet(),
b"CPAL" => ColorPalettes(),
b"GDEF" => GlyphDefinition(),
b"GPOS" => GlyphPositioning(),
b"GSUB" => GlyphSubstitution(),
b"OS/2" => WindowsMetrics(),
b"cmap" => CharacterMapping(),
b"fvar" => FontVariations(),
b"glyf" => GlyphData(..),
b"head" => FontHeader(),
b"hhea" => HorizontalHeader(),
b"hmtx" => HorizontalMetrics(..),
b"loca" => GlyphMapping(..),
b"maxp" => MaximumProfile(),
b"name" => Names(),
b"post" => PostScript(),
implement! {
b"CFF " => FontSet,
b"CPAL" => ColorPalettes,
b"GDEF" => GlyphDefinition,
b"GPOS" => GlyphPositioning,
b"GSUB" => GlyphSubstitution,
b"OS/2" => WindowsMetrics,
b"cmap" => CharacterMapping,
b"fvar" => FontVariations,
b"glyf" => GlyphData,
b"head" => FontHeader,
b"hhea" => HorizontalHeader,
b"hmtx" => HorizontalMetrics,
b"loca" => GlyphMapping,
b"maxp" => MaximumProfile,
b"name" => Names,
b"post" => PostScript,
}

0 comments on commit a68197d

Please sign in to comment.