David PHAM-VAN

Improve debugging information

1 # Changelog 1 # Changelog
2 2
  3 +## 3.7.2
  4 +
  5 +- Improve debugging information
  6 +
3 ## 3.7.1 7 ## 3.7.1
4 8
5 - Fix missing chars with pdfjs 9 - Fix missing chars with pdfjs
  1 +import 'dart:math' as math;
  2 +
  3 +import 'package:meta/meta.dart';
  4 +
  5 +import '../stream.dart';
  6 +
  7 +mixin PdfDiagnostic {
  8 + static const _maxSize = 300;
  9 +
  10 + final _properties = <String>[];
  11 +
  12 + int? _offset;
  13 +
  14 + Stopwatch? _stopwatch;
  15 +
  16 + int get elapsedStopwatch => _stopwatch?.elapsedMicroseconds ?? 0;
  17 +
  18 + @protected
  19 + @mustCallSuper
  20 + void debugFill(String value) {
  21 + assert(() {
  22 + if (_properties.isEmpty) {
  23 + _properties.add('');
  24 + _properties.add('-' * 78);
  25 + _properties.add('$runtimeType');
  26 + }
  27 + _properties.add(value);
  28 + return true;
  29 + }());
  30 + }
  31 +
  32 + void setInsertion(PdfStream os) {
  33 + assert(() {
  34 + _offset = os.offset;
  35 + os.putComment(' ' * _maxSize);
  36 + return true;
  37 + }());
  38 + }
  39 +
  40 + void writeDebug(PdfStream os) {
  41 + assert(() {
  42 + if (_offset != null) {
  43 + final o = PdfStream();
  44 + _properties.forEach(o.putComment);
  45 + _properties.forEach(print);
  46 + final b = o.output();
  47 + os.setBytes(
  48 + _offset!,
  49 + b.sublist(0, math.min(_maxSize + 2, b.lengthInBytes - 1)),
  50 + );
  51 + }
  52 + return true;
  53 + }());
  54 + }
  55 +
  56 + void startStopwatch() {
  57 + _stopwatch ??= Stopwatch();
  58 + _stopwatch!.start();
  59 + }
  60 +
  61 + void stopStopwatch() {
  62 + _stopwatch?.stop();
  63 + }
  64 +}
@@ -69,6 +69,12 @@ class PdfImage extends PdfXObject { @@ -69,6 +69,12 @@ class PdfImage extends PdfXObject {
69 orientation, 69 orientation,
70 ); 70 );
71 71
  72 + assert(() {
  73 + im.startStopwatch();
  74 + im.debugFill('RAW RGB${alpha ? 'A' : ''} Image ${width}x$height');
  75 + return true;
  76 + }());
  77 +
72 im.params['/BitsPerComponent'] = const PdfNum(8); 78 im.params['/BitsPerComponent'] = const PdfNum(8);
73 im.params['/Name'] = PdfName(im.name); 79 im.params['/Name'] = PdfName(im.name);
74 im.params['/ColorSpace'] = const PdfName('/DeviceRGB'); 80 im.params['/ColorSpace'] = const PdfName('/DeviceRGB');
@@ -95,6 +101,10 @@ class PdfImage extends PdfXObject { @@ -95,6 +101,10 @@ class PdfImage extends PdfXObject {
95 } 101 }
96 102
97 im.buf.putBytes(out); 103 im.buf.putBytes(out);
  104 + assert(() {
  105 + im.stopStopwatch();
  106 + return true;
  107 + }());
98 return im; 108 return im;
99 } 109 }
100 110
@@ -112,6 +122,11 @@ class PdfImage extends PdfXObject { @@ -112,6 +122,11 @@ class PdfImage extends PdfXObject {
112 orientation ?? info.orientation, 122 orientation ?? info.orientation,
113 ); 123 );
114 124
  125 + assert(() {
  126 + im.startStopwatch();
  127 + im.debugFill('Jpeg Image ${info.width}x${info.height}');
  128 + return true;
  129 + }());
115 im.params['/BitsPerComponent'] = const PdfNum(8); 130 im.params['/BitsPerComponent'] = const PdfNum(8);
116 im.params['/Name'] = PdfName(im.name); 131 im.params['/Name'] = PdfName(im.name);
117 im.params['/Intent'] = const PdfName('/RelativeColorimetric'); 132 im.params['/Intent'] = const PdfName('/RelativeColorimetric');
@@ -124,6 +139,10 @@ class PdfImage extends PdfXObject { @@ -124,6 +139,10 @@ class PdfImage extends PdfXObject {
124 } 139 }
125 140
126 im.buf.putBytes(image); 141 im.buf.putBytes(image);
  142 + assert(() {
  143 + im.stopStopwatch();
  144 + return true;
  145 + }());
127 return im; 146 return im;
128 } 147 }
129 148
@@ -178,6 +197,11 @@ class PdfImage extends PdfXObject { @@ -178,6 +197,11 @@ class PdfImage extends PdfXObject {
178 orientation, 197 orientation,
179 ); 198 );
180 199
  200 + assert(() {
  201 + im.startStopwatch();
  202 + im.debugFill('Image alpha channel ${width}x$height');
  203 + return true;
  204 + }());
181 im.params['/BitsPerComponent'] = const PdfNum(8); 205 im.params['/BitsPerComponent'] = const PdfNum(8);
182 im.params['/Name'] = PdfName(im.name); 206 im.params['/Name'] = PdfName(im.name);
183 im.params['/ColorSpace'] = const PdfName('/DeviceGray'); 207 im.params['/ColorSpace'] = const PdfName('/DeviceGray');
@@ -193,6 +217,10 @@ class PdfImage extends PdfXObject { @@ -193,6 +217,10 @@ class PdfImage extends PdfXObject {
193 } 217 }
194 218
195 im.buf.putBytes(out); 219 im.buf.putBytes(out);
  220 + assert(() {
  221 + im.stopStopwatch();
  222 + return true;
  223 + }());
196 return im; 224 return im;
197 } 225 }
198 226
@@ -204,6 +232,10 @@ class PdfImage extends PdfXObject { @@ -204,6 +232,10 @@ class PdfImage extends PdfXObject {
204 ) : super(pdfDocument, '/Image', isBinary: true) { 232 ) : super(pdfDocument, '/Image', isBinary: true) {
205 params['/Width'] = PdfNum(_width); 233 params['/Width'] = PdfNum(_width);
206 params['/Height'] = PdfNum(_height); 234 params['/Height'] = PdfNum(_height);
  235 + assert(() {
  236 + debugFill('Orientation: $orientation');
  237 + return true;
  238 + }());
207 } 239 }
208 240
209 final int _width; 241 final int _width;
@@ -19,9 +19,10 @@ import 'package:meta/meta.dart'; @@ -19,9 +19,10 @@ import 'package:meta/meta.dart';
19 import '../data_types.dart'; 19 import '../data_types.dart';
20 import '../document.dart'; 20 import '../document.dart';
21 import '../stream.dart'; 21 import '../stream.dart';
  22 +import 'diagnostic.dart';
22 23
23 /// Base Object used in the PDF file 24 /// Base Object used in the PDF file
24 -abstract class PdfObject<T extends PdfDataType> { 25 +abstract class PdfObject<T extends PdfDataType> with PdfDiagnostic {
25 /// This is usually called by extensors to this class, and sets the 26 /// This is usually called by extensors to this class, and sets the
26 /// Pdf Object Type 27 /// Pdf Object Type
27 PdfObject( 28 PdfObject(
@@ -60,15 +61,6 @@ abstract class PdfObject<T extends PdfDataType> { @@ -60,15 +61,6 @@ abstract class PdfObject<T extends PdfDataType> {
60 /// The write method should call this before writing anything to the 61 /// The write method should call this before writing anything to the
61 /// OutputStream. This will send the standard header for each object. 62 /// OutputStream. This will send the standard header for each object.
62 void _writeStart(PdfStream os) { 63 void _writeStart(PdfStream os) {
63 - assert(() {  
64 - if (pdfDocument.verbose) {  
65 - os.putComment('');  
66 - os.putComment('-' * 78);  
67 - os.putComment('$runtimeType');  
68 - }  
69 - return true;  
70 - }());  
71 -  
72 os.putString('$objser $objgen obj\n'); 64 os.putString('$objser $objgen obj\n');
73 } 65 }
74 66
@@ -17,6 +17,7 @@ @@ -17,6 +17,7 @@
17 import 'data_types.dart'; 17 import 'data_types.dart';
18 import 'document.dart'; 18 import 'document.dart';
19 import 'obj/catalog.dart'; 19 import 'obj/catalog.dart';
  20 +import 'obj/diagnostic.dart';
20 import 'obj/encryption.dart'; 21 import 'obj/encryption.dart';
21 import 'obj/info.dart'; 22 import 'obj/info.dart';
22 import 'obj/object.dart'; 23 import 'obj/object.dart';
@@ -25,7 +26,7 @@ import 'stream.dart'; @@ -25,7 +26,7 @@ import 'stream.dart';
25 import 'xref.dart'; 26 import 'xref.dart';
26 27
27 /// PDF document writer 28 /// PDF document writer
28 -class PdfOutput { 29 +class PdfOutput with PdfDiagnostic {
29 /// This creates a Pdf [PdfStream] 30 /// This creates a Pdf [PdfStream]
30 PdfOutput(this.os, this.version, this.verbose) { 31 PdfOutput(this.os, this.version, this.verbose) {
31 String v; 32 String v;
@@ -42,20 +43,16 @@ class PdfOutput { @@ -42,20 +43,16 @@ class PdfOutput {
42 os.putBytes(const <int>[0x25, 0xC2, 0xA5, 0xC2, 0xB1, 0xC3, 0xAB, 0x0A]); 43 os.putBytes(const <int>[0x25, 0xC2, 0xA5, 0xC2, 0xB1, 0xC3, 0xAB, 0x0A]);
43 assert(() { 44 assert(() {
44 if (verbose) { 45 if (verbose) {
45 - _stopwatch = Stopwatch()..start();  
46 - os.putComment('');  
47 - os.putComment('Verbose dart_pdf');  
48 - os.putComment('Creation date: ${DateTime.now()}');  
49 - _comment = os.offset;  
50 - os.putBytes(List<int>.filled(120, 0x20)); 46 + setInsertion(os);
  47 + startStopwatch();
  48 + debugFill('Verbose dart_pdf');
  49 + debugFill('Producer https://github.com/DavBfr/dart_pdf');
  50 + debugFill('Creation date: ${DateTime.now()}');
51 } 51 }
52 return true; 52 return true;
53 }()); 53 }());
54 } 54 }
55 55
56 - late final Stopwatch _stopwatch;  
57 - var _comment = 0;  
58 -  
59 /// Pdf version to output 56 /// Pdf version to output
60 final PdfVersion version; 57 final PdfVersion version;
61 58
@@ -98,7 +95,23 @@ class PdfOutput { @@ -98,7 +95,23 @@ class PdfOutput {
98 } 95 }
99 96
100 xref.add(PdfXref(ob.objser, os.offset)); 97 xref.add(PdfXref(ob.objser, os.offset));
  98 + assert(() {
  99 + if (verbose) {
  100 + ob.setInsertion(os);
  101 + ob.startStopwatch();
  102 + }
  103 + return true;
  104 + }());
101 ob.write(os); 105 ob.write(os);
  106 + assert(() {
  107 + if (verbose) {
  108 + ob.stopStopwatch();
  109 + ob.debugFill(
  110 + 'Creation time: ${ob.elapsedStopwatch / Duration.microsecondsPerSecond} seconds');
  111 + ob.writeDebug(os);
  112 + }
  113 + return true;
  114 + }());
102 } 115 }
103 116
104 /// This closes the Stream, writing the xref table 117 /// This closes the Stream, writing the xref table
@@ -171,15 +184,13 @@ class PdfOutput { @@ -171,15 +184,13 @@ class PdfOutput {
171 184
172 assert(() { 185 assert(() {
173 if (verbose) { 186 if (verbose) {
174 - _stopwatch.stop();  
175 - final h = PdfStream();  
176 - h.putComment(  
177 - 'Creation time: ${_stopwatch.elapsed.inMicroseconds / Duration.microsecondsPerSecond} seconds');  
178 - h.putComment('File size: ${os.offset} bytes');  
179 - h.putComment('Pages: ${rootID!.pdfDocument.pdfPageList.pages.length}');  
180 - h.putComment('Objects: ${xref.offsets.length}');  
181 -  
182 - os.setBytes(_comment, h.output()); 187 + stopStopwatch();
  188 + debugFill(
  189 + 'Creation time: ${elapsedStopwatch / Duration.microsecondsPerSecond} seconds');
  190 + debugFill('File size: ${os.offset} bytes');
  191 + debugFill('Pages: ${rootID!.pdfDocument.pdfPageList.pages.length}');
  192 + debugFill('Objects: ${xref.offsets.length}');
  193 + writeDebug(os);
183 } 194 }
184 return true; 195 return true;
185 }()); 196 }());
@@ -3,7 +3,7 @@ description: A pdf producer for Dart. It can create pdf files for both web or fl @@ -3,7 +3,7 @@ description: A pdf producer for Dart. It can create pdf files for both web or fl
3 homepage: https://github.com/DavBfr/dart_pdf/tree/master/pdf 3 homepage: https://github.com/DavBfr/dart_pdf/tree/master/pdf
4 repository: https://github.com/DavBfr/dart_pdf 4 repository: https://github.com/DavBfr/dart_pdf
5 issue_tracker: https://github.com/DavBfr/dart_pdf/issues 5 issue_tracker: https://github.com/DavBfr/dart_pdf/issues
6 -version: 3.7.1 6 +version: 3.7.2
7 7
8 environment: 8 environment:
9 sdk: ">=2.12.0 <3.0.0" 9 sdk: ">=2.12.0 <3.0.0"