Skip to content

Commit 95bf25b

Browse files
Improve read macros
1 parent d07b70f commit 95bf25b

9 files changed

Lines changed: 64 additions & 200 deletions

File tree

src/font/grammar.rs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
1-
use crate::util::read_bytes::{
2-
U16_BYTES,
3-
U8_BYTES,
4-
};
5-
use anyhow::{
6-
anyhow,
7-
bail,
8-
ensure,
9-
Result,
10-
};
1+
use anyhow::{anyhow, bail, ensure, Result};
112
use std::collections::BTreeMap;
123

134
pub type ShortFrac = i16;
@@ -50,10 +41,10 @@ pub enum ScalarType {
5041
OpenType,
5142
}
5243

53-
impl TryFrom<&[u8; 4]> for ScalarType {
44+
impl TryFrom<&[u8]> for ScalarType {
5445
type Error = anyhow::Error;
5546

56-
fn try_from(value: &[u8; 4]) -> Result<Self, Self::Error> {
47+
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
5748
let scalar_type = match value {
5849
b"true" | b"\x00\x01\x00\x00" => Self::TrueType,
5950
b"typ1" => Self::PostScript,
@@ -94,13 +85,13 @@ pub enum TableTag<'a> {
9485
OS2,
9586
Prep,
9687

97-
Foreign(&'a [u8; 4]),
88+
Foreign(&'a [u8]),
9889
}
9990

100-
impl<'a> TryFrom<&'a [u8; 4]> for TableTag<'a> {
91+
impl<'a> TryFrom<&'a [u8]> for TableTag<'a> {
10192
type Error = anyhow::Error;
10293

103-
fn try_from(value: &'a [u8; 4]) -> Result<Self, Self::Error> {
94+
fn try_from(value: &'a [u8]) -> Result<Self, Self::Error> {
10495
let tag = match value {
10596
b"cmap" => Self::CMap,
10697
b"glyf" => Self::Glyf,
@@ -383,8 +374,8 @@ impl TryFrom<i16> for IndexToLocFormat {
383374
impl IndexToLocFormat {
384375
pub const fn size(&self) -> usize {
385376
match self {
386-
Self::Short => U16_BYTES,
387-
Self::Long => U8_BYTES,
377+
Self::Short => std::mem::size_of::<u16>(),
378+
Self::Long => std::mem::size_of::<u8>(),
388379
}
389380
}
390381
}

src/font/parser.rs

Lines changed: 26 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,16 @@
11
use 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
};
369
use 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};
5614
use 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)]

src/jpeg/decoder.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use crate::{
2+
impl_read_for_datatype,
23
jpeg::grammar::{
34
Component, EncodingProcess, HuffmanTable, Jpeg, Marker, QuantizationTable, StartOfFrame,
45
StartOfScan, JFIF,
56
},
6-
read, read_slice,
7-
util::read_bytes::{U16_BYTES, U8_BYTES},
7+
read_slice,
88
};
9+
910
use anyhow::{anyhow, ensure, Result};
1011
use std::ops::{Range, RangeInclusive};
1112

@@ -79,7 +80,7 @@ impl<'a> JpegDecoder<'a> {
7980
let offset = self.cursor;
8081
let length = self.read_u16()?;
8182

82-
ensure!(self.read_fixed::<5>()? == b"JFIF\0");
83+
ensure!(self.read_slice(5)? == b"JFIF\0");
8384

8485
self.cursor = offset + length as usize;
8586

@@ -188,7 +189,7 @@ impl<'a> JpegDecoder<'a> {
188189
let range = Range {
189190
start: self.cursor,
190191
end: {
191-
while self.data[self.cursor..self.cursor + U16_BYTES] != [0xFF, 0xD9] {
192+
while self.peek_slice(2)? != &[0xFF, 0xD9] {
192193
self.cursor += 1;
193194
}
194195

@@ -199,8 +200,8 @@ impl<'a> JpegDecoder<'a> {
199200
Ok(range)
200201
}
201202

202-
read!(read_u8, u8, U8_BYTES);
203-
read!(read_u16, u16, U16_BYTES);
204-
read!(read_marker, Marker, U16_BYTES);
203+
impl_read_for_datatype!(read_u8, u8);
204+
impl_read_for_datatype!(read_u16, u16);
205+
impl_read_for_datatype!(read_marker, Marker);
205206
read_slice!();
206207
}

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,5 @@ pub mod font;
77
pub mod image;
88
pub mod jpeg;
99
pub mod png;
10-
pub mod qoi;
1110
pub mod renderer;
1211
pub mod util;

src/png/decoder.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
#[cfg(feature = "time")]
22
use crate::util::event_log::{log_event, Event};
33
use crate::{
4-
eof,
54
image::grammar::ColorType,
5+
impl_read_for_datatype,
66
png::{
77
crc32::compute_crc,
88
grammar::{Chunk, ImageHeader, Png},
99
scanline_reader::ScanlineReader,
1010
},
11-
read, read_slice,
12-
util::read_bytes::{U32_BYTES, U8_BYTES},
11+
read_slice,
1312
};
1413
use anyhow::{bail, ensure, Result};
1514
use flate2::read::ZlibDecoder;
@@ -224,15 +223,13 @@ impl<'a> PngDecoder<'a> {
224223
}
225224

226225
fn skip_crc(&mut self) -> Result<()> {
227-
self.eof(4)?;
228-
self.cursor += 4;
226+
let _ = self.read_slice(4)?;
229227

230228
Ok(())
231229
}
232230

233-
eof!();
234-
read!(read_u8, u8, U8_BYTES);
235-
read!(read_u32, u32, U32_BYTES);
231+
impl_read_for_datatype!(read_u8, u8);
232+
impl_read_for_datatype!(read_u32, u32);
236233
read_slice!();
237234

238235
fn read_slice_until(&mut self, stop: u8) -> Result<&'a [u8]> {

src/qoi/decoder.rs

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/qoi/grammar.rs

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/qoi/mod.rs

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)