David PHAM-VAN

Improve debugging information

# Changelog
## 3.7.2
- Improve debugging information
## 3.7.1
- Fix missing chars with pdfjs
... ...
import 'dart:math' as math;
import 'package:meta/meta.dart';
import '../stream.dart';
mixin PdfDiagnostic {
static const _maxSize = 300;
final _properties = <String>[];
int? _offset;
Stopwatch? _stopwatch;
int get elapsedStopwatch => _stopwatch?.elapsedMicroseconds ?? 0;
@protected
@mustCallSuper
void debugFill(String value) {
assert(() {
if (_properties.isEmpty) {
_properties.add('');
_properties.add('-' * 78);
_properties.add('$runtimeType');
}
_properties.add(value);
return true;
}());
}
void setInsertion(PdfStream os) {
assert(() {
_offset = os.offset;
os.putComment(' ' * _maxSize);
return true;
}());
}
void writeDebug(PdfStream os) {
assert(() {
if (_offset != null) {
final o = PdfStream();
_properties.forEach(o.putComment);
_properties.forEach(print);
final b = o.output();
os.setBytes(
_offset!,
b.sublist(0, math.min(_maxSize + 2, b.lengthInBytes - 1)),
);
}
return true;
}());
}
void startStopwatch() {
_stopwatch ??= Stopwatch();
_stopwatch!.start();
}
void stopStopwatch() {
_stopwatch?.stop();
}
}
... ...
... ... @@ -69,6 +69,12 @@ class PdfImage extends PdfXObject {
orientation,
);
assert(() {
im.startStopwatch();
im.debugFill('RAW RGB${alpha ? 'A' : ''} Image ${width}x$height');
return true;
}());
im.params['/BitsPerComponent'] = const PdfNum(8);
im.params['/Name'] = PdfName(im.name);
im.params['/ColorSpace'] = const PdfName('/DeviceRGB');
... ... @@ -95,6 +101,10 @@ class PdfImage extends PdfXObject {
}
im.buf.putBytes(out);
assert(() {
im.stopStopwatch();
return true;
}());
return im;
}
... ... @@ -112,6 +122,11 @@ class PdfImage extends PdfXObject {
orientation ?? info.orientation,
);
assert(() {
im.startStopwatch();
im.debugFill('Jpeg Image ${info.width}x${info.height}');
return true;
}());
im.params['/BitsPerComponent'] = const PdfNum(8);
im.params['/Name'] = PdfName(im.name);
im.params['/Intent'] = const PdfName('/RelativeColorimetric');
... ... @@ -124,6 +139,10 @@ class PdfImage extends PdfXObject {
}
im.buf.putBytes(image);
assert(() {
im.stopStopwatch();
return true;
}());
return im;
}
... ... @@ -178,6 +197,11 @@ class PdfImage extends PdfXObject {
orientation,
);
assert(() {
im.startStopwatch();
im.debugFill('Image alpha channel ${width}x$height');
return true;
}());
im.params['/BitsPerComponent'] = const PdfNum(8);
im.params['/Name'] = PdfName(im.name);
im.params['/ColorSpace'] = const PdfName('/DeviceGray');
... ... @@ -193,6 +217,10 @@ class PdfImage extends PdfXObject {
}
im.buf.putBytes(out);
assert(() {
im.stopStopwatch();
return true;
}());
return im;
}
... ... @@ -204,6 +232,10 @@ class PdfImage extends PdfXObject {
) : super(pdfDocument, '/Image', isBinary: true) {
params['/Width'] = PdfNum(_width);
params['/Height'] = PdfNum(_height);
assert(() {
debugFill('Orientation: $orientation');
return true;
}());
}
final int _width;
... ...
... ... @@ -19,9 +19,10 @@ import 'package:meta/meta.dart';
import '../data_types.dart';
import '../document.dart';
import '../stream.dart';
import 'diagnostic.dart';
/// Base Object used in the PDF file
abstract class PdfObject<T extends PdfDataType> {
abstract class PdfObject<T extends PdfDataType> with PdfDiagnostic {
/// This is usually called by extensors to this class, and sets the
/// Pdf Object Type
PdfObject(
... ... @@ -60,15 +61,6 @@ abstract class PdfObject<T extends PdfDataType> {
/// The write method should call this before writing anything to the
/// OutputStream. This will send the standard header for each object.
void _writeStart(PdfStream os) {
assert(() {
if (pdfDocument.verbose) {
os.putComment('');
os.putComment('-' * 78);
os.putComment('$runtimeType');
}
return true;
}());
os.putString('$objser $objgen obj\n');
}
... ...
... ... @@ -17,6 +17,7 @@
import 'data_types.dart';
import 'document.dart';
import 'obj/catalog.dart';
import 'obj/diagnostic.dart';
import 'obj/encryption.dart';
import 'obj/info.dart';
import 'obj/object.dart';
... ... @@ -25,7 +26,7 @@ import 'stream.dart';
import 'xref.dart';
/// PDF document writer
class PdfOutput {
class PdfOutput with PdfDiagnostic {
/// This creates a Pdf [PdfStream]
PdfOutput(this.os, this.version, this.verbose) {
String v;
... ... @@ -42,20 +43,16 @@ class PdfOutput {
os.putBytes(const <int>[0x25, 0xC2, 0xA5, 0xC2, 0xB1, 0xC3, 0xAB, 0x0A]);
assert(() {
if (verbose) {
_stopwatch = Stopwatch()..start();
os.putComment('');
os.putComment('Verbose dart_pdf');
os.putComment('Creation date: ${DateTime.now()}');
_comment = os.offset;
os.putBytes(List<int>.filled(120, 0x20));
setInsertion(os);
startStopwatch();
debugFill('Verbose dart_pdf');
debugFill('Producer https://github.com/DavBfr/dart_pdf');
debugFill('Creation date: ${DateTime.now()}');
}
return true;
}());
}
late final Stopwatch _stopwatch;
var _comment = 0;
/// Pdf version to output
final PdfVersion version;
... ... @@ -98,7 +95,23 @@ class PdfOutput {
}
xref.add(PdfXref(ob.objser, os.offset));
assert(() {
if (verbose) {
ob.setInsertion(os);
ob.startStopwatch();
}
return true;
}());
ob.write(os);
assert(() {
if (verbose) {
ob.stopStopwatch();
ob.debugFill(
'Creation time: ${ob.elapsedStopwatch / Duration.microsecondsPerSecond} seconds');
ob.writeDebug(os);
}
return true;
}());
}
/// This closes the Stream, writing the xref table
... ... @@ -171,15 +184,13 @@ class PdfOutput {
assert(() {
if (verbose) {
_stopwatch.stop();
final h = PdfStream();
h.putComment(
'Creation time: ${_stopwatch.elapsed.inMicroseconds / Duration.microsecondsPerSecond} seconds');
h.putComment('File size: ${os.offset} bytes');
h.putComment('Pages: ${rootID!.pdfDocument.pdfPageList.pages.length}');
h.putComment('Objects: ${xref.offsets.length}');
os.setBytes(_comment, h.output());
stopStopwatch();
debugFill(
'Creation time: ${elapsedStopwatch / Duration.microsecondsPerSecond} seconds');
debugFill('File size: ${os.offset} bytes');
debugFill('Pages: ${rootID!.pdfDocument.pdfPageList.pages.length}');
debugFill('Objects: ${xref.offsets.length}');
writeDebug(os);
}
return true;
}());
... ...
... ... @@ -3,7 +3,7 @@ description: A pdf producer for Dart. It can create pdf files for both web or fl
homepage: https://github.com/DavBfr/dart_pdf/tree/master/pdf
repository: https://github.com/DavBfr/dart_pdf
issue_tracker: https://github.com/DavBfr/dart_pdf/issues
version: 3.7.1
version: 3.7.2
environment:
sdk: ">=2.12.0 <3.0.0"
... ...