David PHAM-VAN

Draw page content only if not empty

@@ -9,6 +9,7 @@ @@ -9,6 +9,7 @@
9 - Improve verbose output 9 - Improve verbose output
10 - Import already defined form 10 - Import already defined form
11 - Add support for deleted objects 11 - Add support for deleted objects
  12 +- Draw page content only if not empty
12 13
13 ## 3.9.0 14 ## 3.9.0
14 15
@@ -123,6 +123,9 @@ class PdfGraphics { @@ -123,6 +123,9 @@ class PdfGraphics {
123 /// Default font if none selected 123 /// Default font if none selected
124 PdfFont? get defaultFont => _page.getDefaultFont(); 124 PdfFont? get defaultFont => _page.getDefaultFont();
125 125
  126 + bool _altered = false;
  127 + bool get altered => _altered;
  128 +
126 /// Draw a surface on the previously defined shape 129 /// Draw a surface on the previously defined shape
127 /// set evenOdd to false to use the nonzero winding number rule to determine the region to fill and to true to use the even-odd rule to determine the region to fill 130 /// set evenOdd to false to use the nonzero winding number rule to determine the region to fill and to true to use the even-odd rule to determine the region to fill
128 void fillPath({bool evenOdd = false}) { 131 void fillPath({bool evenOdd = false}) {
@@ -136,6 +139,7 @@ class PdfGraphics { @@ -136,6 +139,7 @@ class PdfGraphics {
136 }()); 139 }());
137 140
138 _buf.putString('f${evenOdd ? '*' : ''} '); 141 _buf.putString('f${evenOdd ? '*' : ''} ');
  142 + _altered = true;
139 143
140 assert(() { 144 assert(() {
141 if (_page.pdfDocument.verbose) { 145 if (_page.pdfDocument.verbose) {
@@ -158,6 +162,7 @@ class PdfGraphics { @@ -158,6 +162,7 @@ class PdfGraphics {
158 }()); 162 }());
159 163
160 _buf.putString('${close ? 's' : 'S'} '); 164 _buf.putString('${close ? 's' : 'S'} ');
  165 + _altered = true;
161 166
162 assert(() { 167 assert(() {
163 if (_page.pdfDocument.verbose) { 168 if (_page.pdfDocument.verbose) {
@@ -178,6 +183,7 @@ class PdfGraphics { @@ -178,6 +183,7 @@ class PdfGraphics {
178 }()); 183 }());
179 184
180 _buf.putString('h '); 185 _buf.putString('h ');
  186 + _altered = true;
181 187
182 assert(() { 188 assert(() {
183 if (_page.pdfDocument.verbose) { 189 if (_page.pdfDocument.verbose) {
@@ -224,6 +230,7 @@ class PdfGraphics { @@ -224,6 +230,7 @@ class PdfGraphics {
224 }()); 230 }());
225 231
226 _buf.putString('${close ? 'b' : 'B'}${evenOdd ? '*' : ''} '); 232 _buf.putString('${close ? 'b' : 'B'}${evenOdd ? '*' : ''} ');
  233 + _altered = true;
227 234
228 assert(() { 235 assert(() {
229 if (_page.pdfDocument.verbose) { 236 if (_page.pdfDocument.verbose) {
@@ -248,6 +255,7 @@ class PdfGraphics { @@ -248,6 +255,7 @@ class PdfGraphics {
248 // The shader needs to be registered in the page resources 255 // The shader needs to be registered in the page resources
249 _page.addShader(shader); 256 _page.addShader(shader);
250 _buf.putString('${shader.name} sh '); 257 _buf.putString('${shader.name} sh ');
  258 + _altered = true;
251 259
252 assert(() { 260 assert(() {
253 if (_page.pdfDocument.verbose) { 261 if (_page.pdfDocument.verbose) {
@@ -354,7 +362,7 @@ class PdfGraphics { @@ -354,7 +362,7 @@ class PdfGraphics {
354 } 362 }
355 363
356 _buf.putString(' cm ${img.name} Do Q '); 364 _buf.putString(' cm ${img.name} Do Q ');
357 - } 365 + _altered = true;
358 366
359 assert(() { 367 assert(() {
360 if (_page.pdfDocument.verbose) { 368 if (_page.pdfDocument.verbose) {
@@ -571,6 +579,7 @@ class PdfGraphics { @@ -571,6 +579,7 @@ class PdfGraphics {
571 return true; 579 return true;
572 }()); 580 }());
573 581
  582 + _altered = true;
574 } 583 }
575 584
576 void reset() { 585 void reset() {
@@ -20,6 +20,7 @@ import '../graphics.dart'; @@ -20,6 +20,7 @@ import '../graphics.dart';
20 import '../page_format.dart'; 20 import '../page_format.dart';
21 import 'annotation.dart'; 21 import 'annotation.dart';
22 import 'graphic_stream.dart'; 22 import 'graphic_stream.dart';
  23 +import 'object.dart';
23 import 'object_dict.dart'; 24 import 'object_dict.dart';
24 import 'object_stream.dart'; 25 import 'object_stream.dart';
25 26
@@ -64,11 +65,13 @@ class PdfPage extends PdfObjectDict with PdfGraphicStream { @@ -64,11 +65,13 @@ class PdfPage extends PdfObjectDict with PdfGraphicStream {
64 PdfPageRotation rotate; 65 PdfPageRotation rotate;
65 66
66 /// This holds the contents of the page. 67 /// This holds the contents of the page.
67 - final contents = <PdfObjectStream>[]; 68 + final contents = <PdfObject>[];
68 69
69 /// This holds any Annotations contained within this page. 70 /// This holds any Annotations contained within this page.
70 final annotations = <PdfAnnot>[]; 71 final annotations = <PdfAnnot>[];
71 72
  73 + final _contentGraphics = <PdfObject, PdfGraphics>{};
  74 +
72 /// This returns a [PdfGraphics] object, which can then be used to render 75 /// This returns a [PdfGraphics] object, which can then be used to render
73 /// on to this page. If a previous [PdfGraphics] object was used, this object 76 /// on to this page. If a previous [PdfGraphics] object was used, this object
74 /// is appended to the page, and will be drawn over the top of any previous 77 /// is appended to the page, and will be drawn over the top of any previous
@@ -76,6 +79,7 @@ class PdfPage extends PdfObjectDict with PdfGraphicStream { @@ -76,6 +79,7 @@ class PdfPage extends PdfObjectDict with PdfGraphicStream {
76 PdfGraphics getGraphics() { 79 PdfGraphics getGraphics() {
77 final stream = PdfObjectStream(pdfDocument); 80 final stream = PdfObjectStream(pdfDocument);
78 final g = PdfGraphics(this, stream.buf); 81 final g = PdfGraphics(this, stream.buf);
  82 + _contentGraphics[stream] = g;
79 contents.add(stream); 83 contents.add(stream);
80 return g; 84 return g;
81 } 85 }
@@ -100,8 +104,15 @@ class PdfPage extends PdfObjectDict with PdfGraphicStream { @@ -100,8 +104,15 @@ class PdfPage extends PdfObjectDict with PdfGraphicStream {
100 params['/MediaBox'] = 104 params['/MediaBox'] =
101 PdfArray.fromNum(<double>[0, 0, pageFormat.width, pageFormat.height]); 105 PdfArray.fromNum(<double>[0, 0, pageFormat.width, pageFormat.height]);
102 106
  107 + for (final content in contents) {
  108 + if (!_contentGraphics[content]!.altered) {
  109 + content.inUse = false;
  110 + }
  111 + }
  112 +
103 // The graphic operations to draw the page 113 // The graphic operations to draw the page
104 - final contentList = PdfArray.fromObjects(contents); 114 + final contentList =
  115 + PdfArray.fromObjects(contents.where((e) => e.inUse).toList());
105 116
106 if (params.containsKey('/Contents')) { 117 if (params.containsKey('/Contents')) {
107 final prevContent = params['/Contents']!; 118 final prevContent = params['/Contents']!;
@@ -124,9 +135,10 @@ class PdfPage extends PdfObjectDict with PdfGraphicStream { @@ -124,9 +135,10 @@ class PdfPage extends PdfObjectDict with PdfGraphicStream {
124 // The /Annots object 135 // The /Annots object
125 if (annotations.isNotEmpty) { 136 if (annotations.isNotEmpty) {
126 if (params.containsKey('/Annots')) { 137 if (params.containsKey('/Annots')) {
127 - final annotsList = params['/Annots'];  
128 - if (annotsList is PdfArray) {  
129 - annotsList.values.addAll(PdfArray.fromObjects(annotations).values); 138 + final annotationList = params['/Annots'];
  139 + if (annotationList is PdfArray) {
  140 + annotationList.values
  141 + .addAll(PdfArray.fromObjects(annotations).values);
130 } 142 }
131 } else { 143 } else {
132 params['/Annots'] = PdfArray.fromObjects(annotations); 144 params['/Annots'] = PdfArray.fromObjects(annotations);