@@ -101,20 +101,18 @@ impl<'a> Operation<'a> {
101101 /// Start writing an arbitrary object operand.
102102 #[ inline]
103103 pub fn obj ( & mut self ) -> Obj < ' _ > {
104- let needs_padding = if !self . first {
105- if self . write_settings . pretty {
106- self . buf . push ( b' ' ) ;
107- false
108- } else {
109- true
110- }
104+ // In case we are writing the first object, we want a newline to separate it from
105+ // previous operations. Otherwise, a space is sufficient.
106+ let pad_byte = if self . first { b'\n' } else { b' ' } ;
107+
108+ // Similarly to how chunks are handled, we always add padding when pretty-writing
109+ // is enabled, and only lazily add padding depending on whether it's really necessary
110+ // if not.
111+ let needs_padding = if self . write_settings . pretty {
112+ self . buf . push ( pad_byte) ;
113+ false
111114 } else {
112- if self . write_settings . pretty {
113- self . buf . push ( b'\n' ) ;
114- false
115- } else {
116- true
117- }
115+ true
118116 } ;
119117
120118 self . first = false ;
@@ -125,19 +123,16 @@ impl<'a> Operation<'a> {
125123impl Drop for Operation < ' _ > {
126124 #[ inline]
127125 fn drop ( & mut self ) {
128- if !self . first {
129- if self . write_settings . pretty
130- || self . buf . last ( ) . is_some_and ( |b| !is_delimiter_character ( * b) )
131- {
132- self . buf . push ( b' ' ) ;
133- }
134- } else {
135- if self . write_settings . pretty
136- || self . buf . last ( ) . is_some_and ( |b| !is_delimiter_character ( * b) )
137- {
138- self . buf . push ( b'\n' ) ;
139- }
126+ let pad_byte = if self . first { b'\n' } else { b' ' } ;
127+
128+ // For example, in case we previously wrote a BT operator and then a [] operand in the
129+ // next operation, we don't need to pad them.
130+ if self . write_settings . pretty
131+ || self . buf . last ( ) . is_some_and ( |b| !is_delimiter_character ( * b) )
132+ {
133+ self . buf . push ( pad_byte) ;
140134 }
135+
141136 self . buf . extend ( self . op . as_bytes ( ) ) ;
142137 }
143138}
@@ -1773,7 +1768,7 @@ mod tests {
17731768 b"/F1 12 Tf/F2 15 Tf\n BT[]TJ[(AB)2(CD)4(EF)]TJ\n ET"
17741769 ) ;
17751770 }
1776-
1771+
17771772 #[ test]
17781773 fn test_content_dict_no_pretty ( ) {
17791774 let mut content = Content :: new_with ( WriteSettings { pretty : false } ) ;
0 commit comments