diff --git a/src/cff/dict.rs b/src/cff/dict.rs index c3222888..17ce9f14 100644 --- a/src/cff/dict.rs +++ b/src/cff/dict.rs @@ -5,14 +5,14 @@ use crate::{Error, Reader, Result, Structure, Writer}; /// A DICT data structure. #[derive(Clone)] -pub struct Dict(Vec); +pub struct Dict<'a>(Vec>); -impl Dict { +impl<'a> Dict<'a> { pub fn new() -> Self { Self(vec![]) } - pub fn get(&self, op: Op) -> Option<&[Operand]> { + pub fn get(&self, op: Op) -> Option<&[Operand<'a>]> { self.0 .iter() .find(|pair| pair.op == op) @@ -43,7 +43,7 @@ impl Dict { } } - pub fn set(&mut self, op: Op, operands: Vec) { + pub fn set(&mut self, op: Op, operands: Vec>) { if let Some(pair) = self.0.iter_mut().find(|pair| pair.op == op) { pair.operands = operands; } else { @@ -63,8 +63,8 @@ impl Dict { } } -impl Structure for Dict { - fn read(r: &mut Reader) -> Result { +impl<'a> Structure<'a> for Dict<'a> { + fn read(r: &mut Reader<'a>) -> Result { let mut pairs = vec![]; while !r.eof() { pairs.push(r.read::()?); @@ -79,7 +79,7 @@ impl Structure for Dict { } } -impl Debug for Dict { +impl Debug for Dict<'_> { fn fmt(&self, f: &mut Formatter) -> fmt::Result { f.debug_list().entries(self.0.iter()).finish() } @@ -87,13 +87,13 @@ impl Debug for Dict { /// An operand-operator pair in a DICT. #[derive(Clone)] -struct Pair { - operands: Vec, +struct Pair<'a> { + operands: Vec>, op: Op, } -impl Structure for Pair { - fn read(r: &mut Reader) -> Result { +impl<'a> Structure<'a> for Pair<'a> { + fn read(r: &mut Reader<'a>) -> Result { let mut operands = vec![]; loop { match r.data().first().ok_or(Error::MissingData)? { @@ -113,7 +113,7 @@ impl Structure for Pair { } } -impl Debug for Pair { +impl Debug for Pair<'_> { fn fmt(&self, f: &mut Formatter) -> fmt::Result { write!(f, "{:?}: {:?}", self.op, self.operands) } @@ -123,7 +123,7 @@ impl Debug for Pair { #[derive(Debug, Copy, Clone, Eq, PartialEq)] pub struct Op(u8, u8); -impl Structure for Op { +impl Structure<'_> for Op { fn read(r: &mut Reader) -> Result { let b0 = r.read::()?; match b0 { @@ -143,14 +143,14 @@ impl Structure for Op { /// An operand in a DICT. #[derive(Debug, Clone, Eq, PartialEq)] -pub enum Operand { +pub enum Operand<'a> { Int(i32), Offset(usize), - Real(Vec), + Real(&'a [u8]), } -impl Structure for Operand { - fn read(r: &mut Reader) -> Result { +impl<'a> Structure<'a> for Operand<'a> { + fn read(r: &mut Reader<'a>) -> Result { let b0 = i32::from(r.read::()?); Ok(match b0 { 28 => Self::Int(i32::from(r.read::()?)), @@ -163,7 +163,7 @@ impl Structure for Operand { break; } } - Self::Real(r.take(len)?.to_vec()) + Self::Real(r.take(len)?) } 32 ..= 246 => Self::Int(b0 - 139), 247 ..= 250 => { diff --git a/src/cff/index.rs b/src/cff/index.rs index 9e5e6ae9..c8f068ee 100644 --- a/src/cff/index.rs +++ b/src/cff/index.rs @@ -23,11 +23,11 @@ impl Index { } } -impl Structure for Index +impl<'a, T> Structure<'a> for Index where - T: Structure, + T: Structure<'a>, { - fn read(r: &mut Reader) -> Result { + fn read(r: &mut Reader<'a>) -> Result { let data = r.data(); let count = r.read::()? as usize; if count == 0 { @@ -116,7 +116,7 @@ impl Offsize { } } -impl Structure for Offsize { +impl Structure<'_> for Offsize { fn read(r: &mut Reader) -> Result { match r.read::()? { 1 => Ok(Self::One), diff --git a/src/cff/mod.rs b/src/cff/mod.rs index e111f0ef..e15518c3 100644 --- a/src/cff/mod.rs +++ b/src/cff/mod.rs @@ -10,15 +10,15 @@ use self::index::*; use super::*; /// A CFF table. -struct Table { - name: Index, - top_dict: Dict, - strings: Index, - global_subrs: Index, - char_strings: Index, - charset: Option, - private_dict: Option, - local_subrs: Option>, +struct Table<'a> { + name: Index>, + top_dict: Dict<'a>, + strings: Index>, + global_subrs: Index>, + char_strings: Index>, + charset: Option>, + private_dict: Option>, + local_subrs: Option>>, } /// Recorded offsets that will be written into DICTs. @@ -209,7 +209,7 @@ fn set_offets(table: &mut Table, offsets: &Offsets) { /// Subset a Top DICT. /// /// Keeps only relevant non-offset entries. Offset entries are inserted later. -fn subset_top(top_dict: &Dict) -> Dict { +fn subset_top<'a>(top_dict: &Dict<'a>) -> Dict<'a> { let mut sub = Dict::new(); sub.copy(&top_dict, top::ROS); sub.copy(&top_dict, top::CID_FONT_VERSION); @@ -241,7 +241,7 @@ fn subset_top(top_dict: &Dict) -> Dict { /// Subset a Private DICT. /// /// Keeps only relevant non-offset entries. Offset entries are inserted later. -fn subset_private(private_dict: &Dict) -> Dict { +fn subset_private<'a>(private_dict: &Dict<'a>) -> Dict<'a> { let mut sub = Dict::new(); sub.copy(&private_dict, private::BLUE_VALUES); sub.copy(&private_dict, private::OTHER_BLUES); @@ -264,7 +264,7 @@ fn subset_private(private_dict: &Dict) -> Dict { } /// Extract the charset bytes. -fn read_charset(data: &[u8], num_glyphs: u16) -> Result { +fn read_charset(data: &[u8], num_glyphs: u16) -> Result> { let mut r = Reader::new(data); let mut len = 1; @@ -294,16 +294,14 @@ fn read_charset(data: &[u8], num_glyphs: u16) -> Result { _ => return Err(Error::InvalidData), } - Ok(Opaque( - data.get(.. len).ok_or(Error::InvalidOffset)?.to_vec(), - )) + Ok(Opaque(data.get(.. len).ok_or(Error::InvalidOffset)?)) } /// Subset the glyph descriptions. -fn subset_char_strings( +fn subset_char_strings<'a>( ctx: &Context, - mut strings: Index, -) -> Result> { + mut strings: Index>, +) -> Result>> { // The set of all glyphs we will include in the subset. let subset: HashSet = ctx.profile.glyphs.iter().copied().collect(); @@ -311,8 +309,7 @@ fn subset_char_strings( if !subset.contains(&glyph) { // The byte sequence [14] is the minimal valid charstring consisting // of just a single `endchar` operator. - *strings.get_mut(glyph as usize).ok_or(Error::InvalidOffset)? = - Opaque(vec![14]); + *strings.get_mut(glyph as usize).ok_or(Error::InvalidOffset)? = Opaque(&[14]); } } @@ -320,11 +317,11 @@ fn subset_char_strings( } /// An opaque binary data structure. -struct Opaque(Vec); +struct Opaque<'a>(&'a [u8]); -impl Structure for Opaque { - fn read(r: &mut Reader) -> Result { - let data = r.data().to_vec(); +impl<'a> Structure<'a> for Opaque<'a> { + fn read(r: &mut Reader<'a>) -> Result { + let data = r.data(); r.skip(data.len())?; Ok(Self(data)) } @@ -334,7 +331,7 @@ impl Structure for Opaque { } } -impl Debug for Opaque { +impl Debug for Opaque<'_> { fn fmt(&self, f: &mut Formatter) -> fmt::Result { f.pad("Opaque { .. }") } diff --git a/src/glyf.rs b/src/glyf.rs index a69d5341..73719886 100644 --- a/src/glyf.rs +++ b/src/glyf.rs @@ -14,9 +14,9 @@ pub(crate) fn subset(ctx: &mut Context) -> Result<()> { } } -fn subset_impl(ctx: &mut Context) -> Result<()> +fn subset_impl<'a, T>(ctx: &'a mut Context) -> Result<()> where - T: LocaOffset, + T: LocaOffset<'a>, { let loca = ctx.expect_table(Tag::LOCA)?; let glyf = ctx.expect_table(Tag::GLYF)?; @@ -121,12 +121,12 @@ fn component_glyphs(mut r: Reader) -> impl Iterator + '_ { } /// A loca offset, either 16-bit or 32-bit. -trait LocaOffset: Structure { +trait LocaOffset<'a>: Structure<'a> { fn loca_to_usize(self) -> usize; fn usize_to_loca(offset: usize) -> Self; } -impl LocaOffset for u16 { +impl LocaOffset<'_> for u16 { fn loca_to_usize(self) -> usize { 2 * usize::from(self) } @@ -138,7 +138,7 @@ impl LocaOffset for u16 { } } -impl LocaOffset for u32 { +impl LocaOffset<'_> for u32 { fn loca_to_usize(self) -> usize { self as usize } diff --git a/src/lib.rs b/src/lib.rs index c4063882..a181b4a9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -111,7 +111,7 @@ impl Tag { const SVG: Self = Self(*b"SVG "); } -impl Structure for Tag { +impl Structure<'_> for Tag { fn read(r: &mut Reader) -> Result { r.read::<[u8; 4]>().map(Self) } @@ -346,7 +346,7 @@ enum FontKind { Collection, } -impl Structure for FontKind { +impl Structure<'_> for FontKind { fn read(r: &mut Reader) -> Result { match r.read::()? { 0x00010000 | 0x74727565 => Ok(FontKind::TrueType), @@ -368,7 +368,7 @@ impl Structure for FontKind { /// A signed 16-bit fixed-point number. struct F2Dot14(u16); -impl Structure for F2Dot14 { +impl Structure<'_> for F2Dot14 { fn read(r: &mut Reader) -> Result { r.read::().map(Self) } @@ -387,7 +387,7 @@ struct TableRecord { length: u32, } -impl Structure for TableRecord { +impl Structure<'_> for TableRecord { fn read(r: &mut Reader) -> Result { Ok(TableRecord { tag: r.read::()?, diff --git a/src/stream.rs b/src/stream.rs index 3f47198c..219fdd21 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -20,12 +20,12 @@ impl<'a> Reader<'a> { } /// Try to read `T` from the data. - pub fn read(&mut self) -> Result { + pub fn read>(&mut self) -> Result { T::read(self) } /// Take the first `n` bytes from the stream. - pub fn take(&mut self, n: usize) -> Result<&[u8]> { + pub fn take(&mut self, n: usize) -> Result<&'a [u8]> { if n <= self.0.len() { let head = &self.0[.. n]; self.0 = &self.0[n ..]; @@ -56,12 +56,12 @@ impl Writer { } /// Write `T` into the data. - pub fn write(&mut self, data: T) { + pub fn write<'a, T: Structure<'a>>(&mut self, data: T) { data.write(self); } /// Write `T` into the data, passing it by reference. - pub fn write_ref(&mut self, data: &T) { + pub fn write_ref<'a, T: Structure<'a>>(&mut self, data: &T) { data.write(self); } @@ -89,15 +89,15 @@ impl Writer { } /// Decode structures from a stream of binary data. -pub trait Structure: Sized { +pub trait Structure<'a>: Sized { /// Try to read `Self` from the reader. - fn read(r: &mut Reader) -> Result; + fn read(r: &mut Reader<'a>) -> Result; /// Write `Self` into the writer. fn write(&self, w: &mut Writer); /// Read self at the given offset in the binary data. - fn read_at(data: &[u8], offset: usize) -> Result { + fn read_at(data: &'a [u8], offset: usize) -> Result { if let Some(sub) = data.get(offset ..) { Self::read(&mut Reader::new(sub)) } else { @@ -106,7 +106,7 @@ pub trait Structure: Sized { } } -impl Structure for [u8; N] { +impl Structure<'_> for [u8; N] { fn read(r: &mut Reader) -> Result { Ok(r.take(N)?.try_into().unwrap_or([0; N])) } @@ -116,7 +116,7 @@ impl Structure for [u8; N] { } } -impl Structure for u8 { +impl Structure<'_> for u8 { fn read(r: &mut Reader) -> Result { r.read::<[u8; 1]>().map(Self::from_be_bytes) } @@ -126,7 +126,7 @@ impl Structure for u8 { } } -impl Structure for u16 { +impl Structure<'_> for u16 { fn read(r: &mut Reader) -> Result { r.read::<[u8; 2]>().map(Self::from_be_bytes) } @@ -136,7 +136,7 @@ impl Structure for u16 { } } -impl Structure for i16 { +impl Structure<'_> for i16 { fn read(r: &mut Reader) -> Result { r.read::<[u8; 2]>().map(Self::from_be_bytes) } @@ -146,7 +146,7 @@ impl Structure for i16 { } } -impl Structure for u32 { +impl Structure<'_> for u32 { fn read(r: &mut Reader) -> Result { r.read::<[u8; 4]>().map(Self::from_be_bytes) } @@ -156,7 +156,7 @@ impl Structure for u32 { } } -impl Structure for i32 { +impl Structure<'_> for i32 { fn read(r: &mut Reader) -> Result { r.read::<[u8; 4]>().map(Self::from_be_bytes) }