@@ -8,6 +8,9 @@ use super::*;
88
99/// A primitive PDF object.
1010pub trait Primitive {
11+ /// Whether the primitive object starts with one of the delimiters.
12+ const HAS_DELIMITER : bool ;
13+
1114 /// Write the object into a buffer.
1215 fn write ( self , buf : & mut Buf ) ;
1316}
@@ -16,13 +19,17 @@ impl<T: Primitive> Primitive for &T
1619where
1720 T : Copy ,
1821{
22+ const HAS_DELIMITER : bool = T :: HAS_DELIMITER ;
23+
1924 #[ inline]
2025 fn write ( self , buf : & mut Buf ) {
2126 ( * self ) . write ( buf) ;
2227 }
2328}
2429
2530impl Primitive for bool {
31+ const HAS_DELIMITER : bool = false ;
32+
2633 #[ inline]
2734 fn write ( self , buf : & mut Buf ) {
2835 if self {
@@ -34,13 +41,17 @@ impl Primitive for bool {
3441}
3542
3643impl Primitive for i32 {
44+ const HAS_DELIMITER : bool = false ;
45+
3746 #[ inline]
3847 fn write ( self , buf : & mut Buf ) {
3948 buf. push_int ( self ) ;
4049 }
4150}
4251
4352impl Primitive for f32 {
53+ const HAS_DELIMITER : bool = false ;
54+
4455 #[ inline]
4556 fn write ( self , buf : & mut Buf ) {
4657 buf. push_float ( self ) ;
@@ -70,6 +81,8 @@ impl Str<'_> {
7081}
7182
7283impl Primitive for Str < ' _ > {
84+ const HAS_DELIMITER : bool = true ;
85+
7386 fn write ( self , buf : & mut Buf ) {
7487 buf. limits . register_str_len ( self . 0 . len ( ) ) ;
7588
@@ -134,6 +147,8 @@ impl Primitive for Str<'_> {
134147pub struct TextStr < ' a > ( pub & ' a str ) ;
135148
136149impl Primitive for TextStr < ' _ > {
150+ const HAS_DELIMITER : bool = true ;
151+
137152 fn write ( self , buf : & mut Buf ) {
138153 buf. limits . register_str_len ( self . 0 . len ( ) ) ;
139154
@@ -219,6 +234,8 @@ impl LanguageIdentifier {
219234pub struct TextStrWithLang < ' a , ' b > ( pub & ' b [ ( LanguageIdentifier , & ' a str ) ] ) ;
220235
221236impl < ' a , ' b > Primitive for TextStrWithLang < ' a , ' b > {
237+ const HAS_DELIMITER : bool = true ;
238+
222239 fn write ( self , buf : & mut Buf ) {
223240 let mut len = 0 ;
224241 let mut buf_len = 6 ;
@@ -295,6 +312,8 @@ impl<'a, 'b> TextStrLike for TextStrWithLang<'a, 'b> {}
295312pub struct Name < ' a > ( pub & ' a [ u8 ] ) ;
296313
297314impl Primitive for Name < ' _ > {
315+ const HAS_DELIMITER : bool = true ;
316+
298317 fn write ( self , buf : & mut Buf ) {
299318 buf. limits . register_name_len ( self . 0 . len ( ) ) ;
300319
@@ -342,6 +361,8 @@ fn is_regular_character(byte: u8) -> bool {
342361pub struct Null ;
343362
344363impl Primitive for Null {
364+ const HAS_DELIMITER : bool = false ;
365+
345366 #[ inline]
346367 fn write ( self , buf : & mut Buf ) {
347368 buf. extend ( b"null" ) ;
@@ -391,6 +412,8 @@ impl Ref {
391412}
392413
393414impl Primitive for Ref {
415+ const HAS_DELIMITER : bool = false ;
416+
394417 #[ inline]
395418 fn write ( self , buf : & mut Buf ) {
396419 buf. push_int ( self . 0 . get ( ) ) ;
@@ -427,6 +450,8 @@ impl Rect {
427450}
428451
429452impl Primitive for Rect {
453+ const HAS_DELIMITER : bool = true ;
454+
430455 #[ inline]
431456 fn write ( self , buf : & mut Buf ) {
432457 buf. push ( b'[' ) ;
@@ -542,6 +567,8 @@ impl Date {
542567}
543568
544569impl Primitive for Date {
570+ const HAS_DELIMITER : bool = true ;
571+
545572 fn write ( self , buf : & mut Buf ) {
546573 buf. extend ( b"(D:" ) ;
547574
@@ -577,26 +604,48 @@ pub struct Obj<'a> {
577604 indirect : bool ,
578605 indent : u8 ,
579606 settings : WriteSettings ,
607+ needs_padding : bool ,
580608}
581609
582610impl < ' a > Obj < ' a > {
583611 /// Start a new direct object.
584612 #[ inline]
585- pub ( crate ) fn direct ( buf : & ' a mut Buf , indent : u8 , settings : WriteSettings ) -> Self {
586- Self { buf, indirect : false , indent, settings }
613+ pub ( crate ) fn direct (
614+ buf : & ' a mut Buf ,
615+ indent : u8 ,
616+ settings : WriteSettings ,
617+ needs_padding : bool ,
618+ ) -> Self {
619+ Self {
620+ buf,
621+ indirect : false ,
622+ indent,
623+ settings,
624+ needs_padding,
625+ }
587626 }
588627
589628 /// Start a new indirect object.
590629 #[ inline]
591630 pub ( crate ) fn indirect ( buf : & ' a mut Buf , id : Ref , settings : WriteSettings ) -> Self {
592631 buf. push_int ( id. get ( ) ) ;
593632 buf. extend ( b" 0 obj\n " ) ;
594- Self { buf, indirect : true , indent : 0 , settings }
633+ Self {
634+ buf,
635+ indirect : true ,
636+ indent : 0 ,
637+ settings,
638+ needs_padding : false ,
639+ }
595640 }
596641
597642 /// Write a primitive object.
598643 #[ inline]
599644 pub fn primitive < T : Primitive > ( self , value : T ) {
645+ if self . needs_padding && !T :: HAS_DELIMITER {
646+ self . buf . extend ( b" " ) ;
647+ }
648+
600649 value. write ( self . buf ) ;
601650 if self . indirect {
602651 self . buf . extend ( b"\n endobj\n " ) ;
@@ -692,11 +741,18 @@ impl<'a> Array<'a> {
692741 /// Start writing an arbitrary item.
693742 #[ inline]
694743 pub fn push ( & mut self ) -> Obj < ' _ > {
695- if self . len != 0 {
696- self . buf . push ( b' ' ) ;
697- }
744+ let needs_padding = if self . len != 0 {
745+ if self . settings . pretty {
746+ self . buf . push ( b' ' ) ;
747+ false
748+ } else {
749+ true
750+ }
751+ } else {
752+ false
753+ } ;
698754 self . len += 1 ;
699- Obj :: direct ( self . buf , self . indent , self . settings )
755+ Obj :: direct ( self . buf , self . indent , self . settings , needs_padding )
700756 }
701757
702758 /// Write an item with a primitive value.
@@ -856,9 +912,14 @@ impl<'a> Dict<'a> {
856912 }
857913
858914 self . buf . push_val ( key) ;
859- self . buf . push ( b' ' ) ;
915+ let needs_padding = if self . settings . pretty {
916+ self . buf . push ( b' ' ) ;
917+ false
918+ } else {
919+ true
920+ } ;
860921
861- Obj :: direct ( self . buf , self . indent , self . settings )
922+ Obj :: direct ( self . buf , self . indent , self . settings , needs_padding )
862923 }
863924
864925 /// Write a pair with a primitive value.
0 commit comments