Skip to content

Commit d510174

Browse files
committed
Don't write space before operator if not necessary
1 parent 159986b commit d510174

2 files changed

Lines changed: 12 additions & 10 deletions

File tree

src/content.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::*;
22
use crate::chunk::WriteSettings;
3-
use crate::object::TextStrLike;
3+
use crate::object::{is_delimiter_character, TextStrLike};
44

55
/// A builder for a content stream.
66
pub struct Content {
@@ -121,7 +121,11 @@ impl Drop for Operation<'_> {
121121
#[inline]
122122
fn drop(&mut self) {
123123
if !self.first {
124-
self.buf.push(b' ');
124+
if self.write_settings.pretty
125+
|| self.buf.last().is_some_and(|b| !is_delimiter_character(*b))
126+
{
127+
self.buf.push(b' ');
128+
}
125129
}
126130
self.buf.extend(self.op.as_bytes());
127131
self.buf.push(b'\n');
@@ -1755,12 +1759,9 @@ mod tests {
17551759

17561760
assert_eq!(
17571761
content.finish().into_vec(),
1758-
b"/F1 12 Tf\nBT\n[] TJ\n[(AB)2(CD)4(EF)] TJ\nET"
1762+
b"/F1 12 Tf\nBT\n[]TJ\n[(AB)2(CD)4(EF)]TJ\nET"
17591763
);
17601764
}
1761-
1762-
// TODO: Dont' write newlines between operations if not necessary?
1763-
17641765
#[test]
17651766
fn test_content_dict_no_pretty() {
17661767
let mut content = Content::new_with(WriteSettings { pretty: false });
@@ -1773,7 +1774,7 @@ mod tests {
17731774

17741775
assert_eq!(
17751776
content.finish().into_vec(),
1776-
b"/Test<</ActualText(Actual)/MCID 1/Type/Background>> BDC"
1777+
b"/Test<</ActualText(Actual)/MCID 1/Type/Background>>BDC"
17771778
);
17781779
}
17791780
}

src/object.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ fn is_regular_character(byte: u8) -> bool {
357357
}
358358

359359
#[inline]
360-
fn is_delimiter_character(byte: u8) -> bool {
360+
pub(crate) fn is_delimiter_character(byte: u8) -> bool {
361361
matches!(byte, b'(' | b')' | b'<' | b'>' | b'[' | b']' | b'/' | b'%')
362362
}
363363

@@ -651,8 +651,9 @@ impl<'a> Obj<'a> {
651651
/// Write a primitive object.
652652
#[inline]
653653
pub fn primitive<T: Primitive>(self, value: T) {
654-
let ends_with_delimiter = self.buf.last().copied().is_some_and(is_delimiter_character);
655-
654+
let ends_with_delimiter =
655+
self.buf.last().copied().is_some_and(is_delimiter_character);
656+
656657
if self.needs_padding && !T::HAS_DELIMITER && !ends_with_delimiter {
657658
self.buf.extend(b" ");
658659
}

0 commit comments

Comments
 (0)