David PHAM-VAN

fix Compressed XREF

@@ -21,30 +21,35 @@ import 'package:pdf/src/pdf/stream.dart'; @@ -21,30 +21,35 @@ import 'package:pdf/src/pdf/stream.dart';
21 import 'data_types.dart'; 21 import 'data_types.dart';
22 import 'object.dart'; 22 import 'object.dart';
23 23
  24 +enum PdfCrossRefEntryType { free, inUse }
  25 +
24 /// Cross-reference for a Pdf Object 26 /// Cross-reference for a Pdf Object
25 class PdfXref { 27 class PdfXref {
26 /// Creates a cross-reference for a Pdf Object 28 /// Creates a cross-reference for a Pdf Object
27 - PdfXref(this.id, this.offset, {this.generation = 0}); 29 + const PdfXref(
  30 + this.id,
  31 + this.offset, {
  32 + this.generation = 0,
  33 + this.type = PdfCrossRefEntryType.inUse,
  34 + });
28 35
29 /// The id of a Pdf Object 36 /// The id of a Pdf Object
30 - int id; 37 + final int id;
31 38
32 /// The offset within the Pdf file 39 /// The offset within the Pdf file
33 - int offset; 40 + final int offset;
34 41
35 /// The generation of the object, usually 0 42 /// The generation of the object, usually 0
36 - int generation = 0; 43 + final int generation;
  44 +
  45 + final PdfCrossRefEntryType type;
37 46
38 /// The xref in the format of the xref section in the Pdf file 47 /// The xref in the format of the xref section in the Pdf file
39 String ref() { 48 String ref() {
40 - final rs = offset.toString().padLeft(10, '0') + 49 + return offset.toString().padLeft(10, '0') +
41 ' ' + 50 ' ' +
42 - generation.toString().padLeft(5, '0');  
43 -  
44 - if (generation == 65535) {  
45 - return rs + ' f ';  
46 - }  
47 - return rs + ' n '; 51 + generation.toString().padLeft(5, '0') +
  52 + (type == PdfCrossRefEntryType.inUse ? ' n ' : ' f ');
48 } 53 }
49 54
50 /// The xref in the format of the compressed xref section in the Pdf file 55 /// The xref in the format of the compressed xref section in the Pdf file
@@ -58,7 +63,7 @@ class PdfXref { @@ -58,7 +63,7 @@ class PdfXref {
58 } 63 }
59 } 64 }
60 65
61 - setVal(w[0], 1); 66 + setVal(w[0], type == PdfCrossRefEntryType.inUse ? 1 : 0);
62 setVal(w[1], offset); 67 setVal(w[1], offset);
63 setVal(w[2], generation); 68 setVal(w[2], generation);
64 69
@@ -75,7 +80,7 @@ class PdfXref { @@ -75,7 +80,7 @@ class PdfXref {
75 } 80 }
76 81
77 @override 82 @override
78 - String toString() => '$runtimeType $id $generation $offset'; 83 + String toString() => '$runtimeType $id $generation $offset $type';
79 84
80 @override 85 @override
81 int get hashCode => offset; 86 int get hashCode => offset;
@@ -114,7 +119,12 @@ class PdfXrefTable extends PdfDataType { @@ -114,7 +119,12 @@ class PdfXrefTable extends PdfDataType {
114 final block = <PdfXref>[]; // xrefs in this block 119 final block = <PdfXref>[]; // xrefs in this block
115 120
116 // We need block 0 to exist 121 // We need block 0 to exist
117 - block.add(PdfXref(0, 0, generation: 65535)); 122 + block.add(const PdfXref(
  123 + 0,
  124 + 0,
  125 + generation: 65535,
  126 + type: PdfCrossRefEntryType.free,
  127 + ));
118 128
119 for (var x in offsets) { 129 for (var x in offsets) {
120 // check to see if block is in range 130 // check to see if block is in range
@@ -181,7 +191,7 @@ class PdfXrefTable extends PdfDataType { @@ -181,7 +191,7 @@ class PdfXrefTable extends PdfDataType {
181 params['/W'] = PdfArray.fromNum(w); 191 params['/W'] = PdfArray.fromNum(w);
182 final wl = w.reduce((a, b) => a + b); 192 final wl = w.reduce((a, b) => a + b);
183 193
184 - final o = ByteData((offsets.length + 2) * wl); 194 + final o = ByteData((offsets.length + 1) * wl);
185 var ofs = 0; 195 var ofs = 0;
186 // Write offset zero, all zeros 196 // Write offset zero, all zeros
187 ofs += wl; 197 ofs += wl;
@@ -194,7 +204,7 @@ class PdfXrefTable extends PdfDataType { @@ -194,7 +204,7 @@ class PdfXrefTable extends PdfDataType {
194 PdfDictStream( 204 PdfDictStream(
195 object: object, 205 object: object,
196 data: o.buffer.asUint8List(), 206 data: o.buffer.asUint8List(),
197 - isBinary: true, 207 + isBinary: false,
198 encrypt: false, 208 encrypt: false,
199 values: params.values, 209 values: params.values,
200 ).output(s); 210 ).output(s);