11use super :: grammar:: {
2- CMapFormat0 ,
3- CMapFormat12 ,
4- CMapFormat4 ,
5- CMapIndividualGroup ,
6- CMapSubtable ,
7- CMapTable ,
8- ComponentGlyph ,
9- ComponentGlyphArgument ,
10- ComponentGlyphFlag ,
11- ComponentGlyphTransformation ,
12- CompoundGlyph ,
13- F2Dot14 ,
14- FWord ,
15- Fixed ,
16- FontDirectory ,
17- Glyph ,
18- GlyphData ,
19- GlyphDescription ,
20- GlyphTable ,
21- HHeaTable ,
22- HMtxTable ,
23- HeadTable ,
24- LongDateTime ,
25- LongHorizontalMetric ,
26- MaxPTable ,
27- OffsetSubTable ,
28- ScalarType ,
29- SimpleGlyph ,
30- SimpleGlyphFlag ,
31- TableRecord ,
32- TableTag ,
33- TrueTypeFontFile ,
34- UnsignedFWord ,
2+ CMapFormat0 , CMapFormat12 , CMapFormat4 , CMapIndividualGroup , CMapSubtable , CMapTable ,
3+ ComponentGlyph , ComponentGlyphArgument , ComponentGlyphFlag , ComponentGlyphTransformation ,
4+ CompoundGlyph , F2Dot14 , FWord , Fixed , FontDirectory , Glyph , GlyphData , GlyphDescription ,
5+ GlyphTable , HHeaTable , HMtxTable , HeadTable , LongDateTime , LongHorizontalMetric , MaxPTable ,
6+ OffsetSubTable , ScalarType , SimpleGlyph , SimpleGlyphFlag , TableRecord , TableTag ,
7+ TrueTypeFontFile , UnsignedFWord ,
358} ;
369use crate :: {
37- eof,
38- font:: grammar:: {
39- IndexToLocFormat ,
40- Platform ,
41- PlatformDouble ,
42- } ,
43- read,
44- util:: read_bytes:: {
45- U16_BYTES ,
46- U32_BYTES ,
47- U64_BYTES ,
48- U8_BYTES ,
49- } ,
50- } ;
51- use anyhow:: {
52- bail,
53- ensure,
54- Result ,
10+ font:: grammar:: { IndexToLocFormat , Platform , PlatformDouble } ,
11+ impl_read_for_datatype, read_slice,
5512} ;
13+ use anyhow:: { bail, ensure, Result } ;
5614use std:: collections:: BTreeMap ;
5715
5816#[ derive( Debug ) ]
@@ -157,7 +115,7 @@ impl<'a> TrueTypeFontParser<'a> {
157115
158116 fn parse_font_directory ( & mut self ) -> Result < FontDirectory < ' a > > {
159117 let offset_sub_table = OffsetSubTable {
160- scalar_type : ScalarType :: try_from ( self . read_slice :: < U32_BYTES > ( ) ?) ?,
118+ scalar_type : ScalarType :: try_from ( self . read_slice ( 4 ) ?) ?,
161119 num_tables : self . read_u16 ( ) ?,
162120 search_range : self . read_u16 ( ) ?,
163121 entry_selector : self . read_u16 ( ) ?,
@@ -168,7 +126,7 @@ impl<'a> TrueTypeFontParser<'a> {
168126
169127 for _ in 0 ..offset_sub_table. num_tables as usize {
170128 // todo: what does a checksum validation look like?
171- let table_tag = TableTag :: try_from ( self . read_slice :: < U32_BYTES > ( ) ?) ?;
129+ let table_tag = TableTag :: try_from ( self . read_slice ( 4 ) ?) ?;
172130
173131 ensure ! (
174132 !table_directory. contains_key( & table_tag) ,
@@ -653,55 +611,32 @@ impl<'a> TrueTypeFontParser<'a> {
653611 Ok ( CompoundGlyph { components } )
654612 }
655613
656- eof ! ( ) ;
657-
658614 fn jump_to_table_record ( & mut self , table_record : & TableRecord ) -> Result < ( ) > {
659615 let ( offset, length) = ( table_record. offset as usize , table_record. length as usize ) ;
660616 self . jump ( offset, length)
661617 }
662618
663619 fn jump ( & mut self , offset : usize , length : usize ) -> Result < ( ) > {
664- ensure ! ( offset < self . data. len( ) , "Offset is out of bounds." ) ;
665620 self . cursor = offset;
666- self . eof ( length) ? ;
621+ ensure ! ( self . cursor + length < self . data . len ( ) , "oob" ) ;
667622
668623 Ok ( ( ) )
669624 }
670625
671- fn read_vec < T > (
672- & mut self ,
673- capacity : usize ,
674- read_fn : impl Fn ( & mut Self ) -> Result < T > ,
675- ) -> Result < Vec < T > > {
676- let mut list = Vec :: with_capacity ( capacity) ;
677-
678- for _ in 0 ..capacity {
679- list. push ( read_fn ( self ) ?)
680- }
681-
682- Ok ( list)
683- }
684-
685- fn read_slice < const N : usize > ( & mut self ) -> Result < & ' a [ u8 ; N ] > {
686- self . eof ( N ) ?;
687- let bs = & self . data [ self . cursor ..self . cursor + N ] ;
688- self . cursor += N ;
689-
690- Ok ( bs. try_into ( ) ?)
691- }
692-
693- // read!(read_short_frac, ShortFrac, U16_BYTES);
694- read ! ( read_fixed, Fixed , U32_BYTES ) ;
695- read ! ( read_fword, FWord , U16_BYTES ) ;
696- read ! ( read_unsigned_fword, UnsignedFWord , U16_BYTES ) ;
697- read ! ( read_f2dot14, F2Dot14 , U16_BYTES ) ;
698- read ! ( read_long_date_time, LongDateTime , U64_BYTES ) ;
699- read ! ( read_i8, i8 , U8_BYTES ) ;
700- read ! ( read_u8, u8 , U8_BYTES ) ;
701- read ! ( read_u16, u16 , U16_BYTES ) ;
702- read ! ( read_u32, u32 , U32_BYTES ) ;
703- read ! ( read_i16, i16 , U16_BYTES ) ;
704- read ! ( read_i64, i64 , U64_BYTES ) ;
626+ // impl_read_for_datatype!(read_short_frac, ShortFrac, U16_BYTES);
627+
628+ read_slice ! ( ) ;
629+ impl_read_for_datatype ! ( read_fixed, Fixed ) ;
630+ impl_read_for_datatype ! ( read_fword, FWord ) ;
631+ impl_read_for_datatype ! ( read_unsigned_fword, UnsignedFWord ) ;
632+ impl_read_for_datatype ! ( read_f2dot14, F2Dot14 ) ;
633+ impl_read_for_datatype ! ( read_long_date_time, LongDateTime ) ;
634+ impl_read_for_datatype ! ( read_i8, i8 ) ;
635+ impl_read_for_datatype ! ( read_u8, u8 ) ;
636+ impl_read_for_datatype ! ( read_u16, u16 ) ;
637+ impl_read_for_datatype ! ( read_u32, u32 ) ;
638+ impl_read_for_datatype ! ( read_i16, i16 ) ;
639+ impl_read_for_datatype ! ( read_i64, i64 ) ;
705640}
706641
707642#[ cfg( test) ]
0 commit comments