diff --git a/src/buf.rs b/src/buf.rs index 9662278..67a91cb 100644 --- a/src/buf.rs +++ b/src/buf.rs @@ -1,5 +1,5 @@ use super::Primitive; -use std::ops::{Deref, DerefMut}; +use std::ops::Deref; /// Track the limits of data types used in a buffer. #[derive(Clone, PartialEq, Debug, Default)] @@ -87,7 +87,7 @@ impl Limits { /// A buffer of arbitrary PDF content. #[derive(Clone, PartialEq, Debug)] pub struct Buf { - buf: Vec, + pub(crate) inner: Vec, pub(crate) limits: Limits, } @@ -95,31 +95,25 @@ impl Deref for Buf { type Target = Vec; fn deref(&self) -> &Self::Target { - &self.buf - } -} - -impl DerefMut for Buf { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.buf + &self.inner } } impl Buf { pub(crate) fn new() -> Self { - Self { buf: Vec::new(), limits: Limits::new() } + Self { inner: Vec::new(), limits: Limits::new() } } pub(crate) fn with_capacity(capacity: usize) -> Self { Self { - buf: Vec::with_capacity(capacity), + inner: Vec::with_capacity(capacity), limits: Limits::new(), } } /// Get the underlying bytes of the buffer. pub fn to_bytes(self) -> Vec { - self.buf + self.inner } /// Return the limits of the buffer. @@ -160,7 +154,7 @@ impl Buf { #[inline(never)] fn write_extreme(buf: &mut Buf, value: f32) { use std::io::Write; - write!(buf, "{}", value).unwrap(); + write!(buf.inner, "{}", value).unwrap(); } write_extreme(self, value); @@ -169,18 +163,18 @@ impl Buf { #[inline] pub(crate) fn extend_slice(&mut self, other: &[u8]) { - self.buf.extend(other); + self.inner.extend(other); } #[inline] pub(crate) fn extend(&mut self, other: &Buf) { self.limits.merge(&other.limits); - self.buf.extend(&other.buf); + self.inner.extend(&other.inner); } #[inline] pub(crate) fn push(&mut self, b: u8) { - self.buf.push(b); + self.inner.push(b); } #[inline] @@ -213,4 +207,9 @@ impl Buf { self.push(octal((value >> 3) & 7)); self.push(octal(value & 7)); } + + #[inline] + pub(crate) fn reserve(&mut self, additional: usize) { + self.inner.reserve(additional) + } } diff --git a/src/content.rs b/src/content.rs index e19ebff..2d34e4a 100644 --- a/src/content.rs +++ b/src/content.rs @@ -30,7 +30,7 @@ impl Content { /// Return the raw constructed byte stream. pub fn finish(mut self) -> Buf { if self.buf.last() == Some(&b'\n') { - self.buf.pop(); + self.buf.inner.pop(); } self.buf } diff --git a/src/font.rs b/src/font.rs index d65e48d..286a806 100644 --- a/src/font.rs +++ b/src/font.rs @@ -991,7 +991,7 @@ where } self.count = 0; - self.mappings.clear(); + self.mappings.inner.clear(); } } diff --git a/src/functions.rs b/src/functions.rs index 8ce08f1..72296e2 100644 --- a/src/functions.rs +++ b/src/functions.rs @@ -351,7 +351,7 @@ impl<'a> PostScriptOp<'a> { buf.push(b' '); } if ops.len() == 1 { - buf.pop(); + buf.inner.pop(); } buf.push(b'}'); } diff --git a/src/lib.rs b/src/lib.rs index 0cc5a1b..4c4a85f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -241,7 +241,7 @@ impl Pdf { /// /// _Default value_: \x80\x80\x80\x80 pub fn set_binary_marker(&mut self, marker: &[u8; 4]) { - self.chunk.buf[10..14].copy_from_slice(marker); + self.chunk.buf.inner[10..14].copy_from_slice(marker); } /// Set the PDF version. @@ -252,10 +252,10 @@ impl Pdf { /// _Default value_: 1.7. pub fn set_version(&mut self, major: u8, minor: u8) { if major < 10 { - self.chunk.buf[5] = b'0' + major; + self.chunk.buf.inner[5] = b'0' + major; } if minor < 10 { - self.chunk.buf[7] = b'0' + minor; + self.chunk.buf.inner[7] = b'0' + minor; } } @@ -305,7 +305,7 @@ impl Pdf { buf.push(b'\n'); if offsets.is_empty() { - write!(buf, "0000000000 65535 f\r\n").unwrap(); + write!(buf.inner, "0000000000 65535 f\r\n").unwrap(); } let mut written = 0; @@ -330,11 +330,11 @@ impl Pdf { } let gen = if free_id == 0 { "65535" } else { "00000" }; - write!(buf, "{:010} {} f\r\n", next % xref_len, gen).unwrap(); + write!(buf.inner, "{:010} {} f\r\n", next % xref_len, gen).unwrap(); written += 1; } - write!(buf, "{:010} 00000 n\r\n", offset).unwrap(); + write!(buf.inner, "{:010} 00000 n\r\n", offset).unwrap(); written += 1; } @@ -362,7 +362,7 @@ impl Pdf { // Write where the cross-reference table starts. buf.extend_slice(b"\nstartxref\n"); - write!(buf, "{}", xref_offset).unwrap(); + write!(buf.inner, "{}", xref_offset).unwrap(); // Write the end of file marker. buf.extend_slice(b"\n%%EOF"); diff --git a/src/object.rs b/src/object.rs index b305217..6bcf98c 100644 --- a/src/object.rs +++ b/src/object.rs @@ -79,7 +79,7 @@ impl Primitive for Str<'_> { // - Hex strings for anything non-ASCII. if self.0.iter().all(|b| b.is_ascii()) { buf.reserve(self.0.len()); - buf.push(b'('); + buf.inner.push(b'('); let mut balanced = None; for &byte in self.0 { @@ -403,17 +403,17 @@ impl Primitive for Date { buf.extend_slice(b"(D:"); (|| { - write!(buf, "{:04}", self.year).unwrap(); - write!(buf, "{:02}", self.month?).unwrap(); - write!(buf, "{:02}", self.day?).unwrap(); - write!(buf, "{:02}", self.hour?).unwrap(); - write!(buf, "{:02}", self.minute?).unwrap(); - write!(buf, "{:02}", self.second?).unwrap(); + write!(buf.inner, "{:04}", self.year).unwrap(); + write!(buf.inner, "{:02}", self.month?).unwrap(); + write!(buf.inner, "{:02}", self.day?).unwrap(); + write!(buf.inner, "{:02}", self.hour?).unwrap(); + write!(buf.inner, "{:02}", self.minute?).unwrap(); + write!(buf.inner, "{:02}", self.second?).unwrap(); let utc_offset_hour = self.utc_offset_hour?; if utc_offset_hour == 0 && self.utc_offset_minute == 0 { buf.push(b'Z'); } else { - write!(buf, "{:+03}'{:02}", utc_offset_hour, self.utc_offset_minute) + write!(buf.inner, "{:+03}'{:02}", utc_offset_hour, self.utc_offset_minute) .unwrap(); } Some(())