David PHAM-VAN

Generalize PdfObject

@@ -17,24 +17,15 @@ @@ -17,24 +17,15 @@
17 import 'data_types.dart'; 17 import 'data_types.dart';
18 import 'document.dart'; 18 import 'document.dart';
19 import 'object.dart'; 19 import 'object.dart';
20 -import 'stream.dart';  
21 20
22 /// An array object 21 /// An array object
23 -class PdfArrayObject extends PdfObject { 22 +class PdfArrayObject extends PdfObject<PdfArray> {
24 /// Creates an array object 23 /// Creates an array object
25 PdfArrayObject( 24 PdfArrayObject(
26 PdfDocument pdfDocument, 25 PdfDocument pdfDocument,
27 - this.array,  
28 - ) : super(pdfDocument); 26 + PdfArray array,
  27 + ) : super(pdfDocument, params: array);
29 28
30 /// The array 29 /// The array
31 - final PdfArray array;  
32 -  
33 - @override  
34 - void writeContent(PdfStream os) {  
35 - super.writeContent(os);  
36 -  
37 - array.output(os);  
38 - os.putBytes(<int>[0x0a]);  
39 - } 30 + PdfArray get array => params;
40 } 31 }
@@ -17,7 +17,7 @@ @@ -17,7 +17,7 @@
17 import 'annotation.dart'; 17 import 'annotation.dart';
18 import 'data_types.dart'; 18 import 'data_types.dart';
19 import 'document.dart'; 19 import 'document.dart';
20 -import 'object.dart'; 20 +import 'object_dict.dart';
21 21
22 /// Border style 22 /// Border style
23 enum PdfBorderStyle { 23 enum PdfBorderStyle {
@@ -40,7 +40,7 @@ enum PdfBorderStyle { @@ -40,7 +40,7 @@ enum PdfBorderStyle {
40 } 40 }
41 41
42 /// Defines a border object 42 /// Defines a border object
43 -class PdfBorder extends PdfObject { 43 +class PdfBorder extends PdfObjectDict {
44 /// Creates a border using the predefined styles in [PdfAnnot]. 44 /// Creates a border using the predefined styles in [PdfAnnot].
45 PdfBorder( 45 PdfBorder(
46 PdfDocument pdfDocument, 46 PdfDocument pdfDocument,
@@ -18,7 +18,6 @@ import 'annotation.dart'; @@ -18,7 +18,6 @@ import 'annotation.dart';
18 import 'data_types.dart'; 18 import 'data_types.dart';
19 import 'document.dart'; 19 import 'document.dart';
20 import 'names.dart'; 20 import 'names.dart';
21 -import 'object.dart';  
22 import 'object_dict.dart'; 21 import 'object_dict.dart';
23 import 'outline.dart'; 22 import 'outline.dart';
24 import 'page_list.dart'; 23 import 'page_list.dart';
@@ -18,9 +18,10 @@ import 'dart:typed_data'; @@ -18,9 +18,10 @@ import 'dart:typed_data';
18 18
19 import 'document.dart'; 19 import 'document.dart';
20 import 'object.dart'; 20 import 'object.dart';
  21 +import 'object_dict.dart';
21 22
22 /// Encryption object 23 /// Encryption object
23 -abstract class PdfEncryption extends PdfObject { 24 +abstract class PdfEncryption extends PdfObjectDict {
24 /// Creates an encryption object 25 /// Creates an encryption object
25 PdfEncryption(PdfDocument pdfDocument) : super(pdfDocument); 26 PdfEncryption(PdfDocument pdfDocument) : super(pdfDocument);
26 27
@@ -15,7 +15,6 @@ @@ -15,7 +15,6 @@
15 */ 15 */
16 16
17 import 'data_types.dart'; 17 import 'data_types.dart';
18 -import 'object.dart';  
19 import 'object_dict.dart'; 18 import 'object_dict.dart';
20 import 'object_stream.dart'; 19 import 'object_stream.dart';
21 import 'ttffont.dart'; 20 import 'ttffont.dart';
@@ -17,10 +17,10 @@ @@ -17,10 +17,10 @@
17 import 'color.dart'; 17 import 'color.dart';
18 import 'data_types.dart'; 18 import 'data_types.dart';
19 import 'document.dart'; 19 import 'document.dart';
20 -import 'object.dart'; 20 +import 'object_dict.dart';
21 import 'object_stream.dart'; 21 import 'object_stream.dart';
22 22
23 -abstract class PdfBaseFunction extends PdfObject { 23 +abstract class PdfBaseFunction extends PdfObjectDict {
24 PdfBaseFunction(PdfDocument pdfDocument) : super(pdfDocument); 24 PdfBaseFunction(PdfDocument pdfDocument) : super(pdfDocument);
25 25
26 factory PdfBaseFunction.colorsAndStops( 26 factory PdfBaseFunction.colorsAndStops(
@@ -21,7 +21,7 @@ import 'package:meta/meta.dart'; @@ -21,7 +21,7 @@ import 'package:meta/meta.dart';
21 import 'data_types.dart'; 21 import 'data_types.dart';
22 import 'document.dart'; 22 import 'document.dart';
23 import 'function.dart'; 23 import 'function.dart';
24 -import 'object.dart'; 24 +import 'object_dict.dart';
25 import 'smask.dart'; 25 import 'smask.dart';
26 26
27 enum PdfBlendMode { 27 enum PdfBlendMode {
@@ -159,7 +159,7 @@ class PdfGraphicState { @@ -159,7 +159,7 @@ class PdfGraphicState {
159 } 159 }
160 160
161 /// Stores all the graphic states used in the document 161 /// Stores all the graphic states used in the document
162 -class PdfGraphicStates extends PdfObject { 162 +class PdfGraphicStates extends PdfObjectDict {
163 /// Create a new Graphic States object 163 /// Create a new Graphic States object
164 PdfGraphicStates(PdfDocument pdfDocument) : super(pdfDocument); 164 PdfGraphicStates(PdfDocument pdfDocument) : super(pdfDocument);
165 165
@@ -18,13 +18,13 @@ import 'data_types.dart'; @@ -18,13 +18,13 @@ import 'data_types.dart';
18 import 'document.dart'; 18 import 'document.dart';
19 import 'font.dart'; 19 import 'font.dart';
20 import 'graphic_state.dart'; 20 import 'graphic_state.dart';
21 -import 'object.dart'; 21 +import 'object_dict.dart';
22 import 'pattern.dart'; 22 import 'pattern.dart';
23 import 'shading.dart'; 23 import 'shading.dart';
24 import 'xobject.dart'; 24 import 'xobject.dart';
25 25
26 /// Helper functions for graphic objects 26 /// Helper functions for graphic objects
27 -mixin PdfGraphicStream on PdfObject { 27 +mixin PdfGraphicStream on PdfObjectDict {
28 /// Isolated transparency: If this flag is true, objects within the group 28 /// Isolated transparency: If this flag is true, objects within the group
29 /// shall be composited against a fully transparent initial backdrop; 29 /// shall be composited against a fully transparent initial backdrop;
30 /// if false, they shall be composited against the group’s backdrop 30 /// if false, they shall be composited against the group’s backdrop
@@ -16,10 +16,10 @@ @@ -16,10 +16,10 @@
16 16
17 import 'data_types.dart'; 17 import 'data_types.dart';
18 import 'document.dart'; 18 import 'document.dart';
19 -import 'object.dart'; 19 +import 'object_dict.dart';
20 20
21 /// Information object 21 /// Information object
22 -class PdfInfo extends PdfObject { 22 +class PdfInfo extends PdfObjectDict {
23 /// Create an information object 23 /// Create an information object
24 PdfInfo(PdfDocument pdfDocument, 24 PdfInfo(PdfDocument pdfDocument,
25 {this.title, 25 {this.title,
@@ -16,11 +16,11 @@ @@ -16,11 +16,11 @@
16 16
17 import 'data_types.dart'; 17 import 'data_types.dart';
18 import 'document.dart'; 18 import 'document.dart';
19 -import 'object.dart'; 19 +import 'object_dict.dart';
20 import 'page.dart'; 20 import 'page.dart';
21 21
22 /// Pdf Name object 22 /// Pdf Name object
23 -class PdfNames extends PdfObject { 23 +class PdfNames extends PdfObjectDict {
24 /// This constructs a Pdf Name object 24 /// This constructs a Pdf Name object
25 PdfNames(PdfDocument pdfDocument) : super(pdfDocument); 25 PdfNames(PdfDocument pdfDocument) : super(pdfDocument);
26 26
@@ -21,12 +21,12 @@ import 'document.dart'; @@ -21,12 +21,12 @@ import 'document.dart';
21 import 'stream.dart'; 21 import 'stream.dart';
22 22
23 /// Base Object used in the PDF file 23 /// Base Object used in the PDF file
24 -class PdfObject { 24 +abstract class PdfObject<T extends PdfDataType> {
25 /// This is usually called by extensors to this class, and sets the 25 /// This is usually called by extensors to this class, and sets the
26 /// Pdf Object Type 26 /// Pdf Object Type
27 PdfObject( 27 PdfObject(
28 this.pdfDocument, { 28 this.pdfDocument, {
29 - // String? type, 29 + required this.params,
30 this.objgen = 0, 30 this.objgen = 0,
31 int? objser, 31 int? objser,
32 }) : objser = objser ?? pdfDocument.genSerial() { 32 }) : objser = objser ?? pdfDocument.genSerial() {
@@ -34,7 +34,7 @@ class PdfObject { @@ -34,7 +34,7 @@ class PdfObject {
34 } 34 }
35 35
36 /// This is the object parameters. 36 /// This is the object parameters.
37 - final PdfDict params = PdfDict(); 37 + final T params;
38 38
39 /// This is the unique serial number for this object. 39 /// This is the unique serial number for this object.
40 final int objser; 40 final int objser;
@@ -64,10 +64,8 @@ class PdfObject { @@ -64,10 +64,8 @@ class PdfObject {
64 } 64 }
65 65
66 void writeContent(PdfStream os) { 66 void writeContent(PdfStream os) {
67 - if (params.isNotEmpty) {  
68 - params.output(os);  
69 - os.putString('\n');  
70 - } 67 + params.output(os);
  68 + os.putByte(0x0a);
71 } 69 }
72 70
73 /// The write method should call this after writing anything to the 71 /// The write method should call this after writing anything to the
@@ -17,9 +17,10 @@ @@ -17,9 +17,10 @@
17 import 'data_types.dart'; 17 import 'data_types.dart';
18 import 'document.dart'; 18 import 'document.dart';
19 import 'object.dart'; 19 import 'object.dart';
  20 +import 'stream.dart';
20 21
21 /// Object with a PdfDict used in the PDF file 22 /// Object with a PdfDict used in the PDF file
22 -class PdfObjectDict extends PdfObject { 23 +class PdfObjectDict extends PdfObject<PdfDict> {
23 /// This is usually called by extensors to this class, and sets the 24 /// This is usually called by extensors to this class, and sets the
24 /// Pdf Object Type 25 /// Pdf Object Type
25 PdfObjectDict( 26 PdfObjectDict(
@@ -27,15 +28,17 @@ class PdfObjectDict extends PdfObject { @@ -27,15 +28,17 @@ class PdfObjectDict extends PdfObject {
27 String? type, 28 String? type,
28 int objgen = 0, 29 int objgen = 0,
29 int? objser, 30 int? objser,
30 - }) : super(pdfDocument, objgen: objgen, objser: objser) { 31 + }) : super(pdfDocument, params: PdfDict(), objgen: objgen, objser: objser) {
31 if (type != null) { 32 if (type != null) {
32 params['/Type'] = PdfName(type); 33 params['/Type'] = PdfName(type);
33 } 34 }
34 } 35 }
35 36
36 - /// This is the object parameters.  
37 - final PdfDict params = PdfDict();  
38 -  
39 @override 37 @override
40 - String toString() => '$runtimeType $params'; 38 + void writeContent(PdfStream os) {
  39 + if (params.isNotEmpty) {
  40 + params.output(os);
  41 + os.putByte(0x0a);
  42 + }
  43 + }
41 } 44 }
@@ -14,9 +14,6 @@ @@ -14,9 +14,6 @@
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 16
17 -import 'dart:typed_data';  
18 -  
19 -import 'ascii85.dart';  
20 import 'data_types.dart'; 17 import 'data_types.dart';
21 import 'document.dart'; 18 import 'document.dart';
22 import 'object_dict.dart'; 19 import 'object_dict.dart';
@@ -37,47 +34,13 @@ class PdfObjectStream extends PdfObjectDict { @@ -37,47 +34,13 @@ class PdfObjectStream extends PdfObjectDict {
37 /// defines if the stream needs to be converted to ascii85 34 /// defines if the stream needs to be converted to ascii85
38 final bool isBinary; 35 final bool isBinary;
39 36
40 - Uint8List? _data;  
41 -  
42 - @override  
43 - void prepare() {  
44 - super.prepare();  
45 -  
46 - if (params.containsKey('/Filter') && _data == null) {  
47 - // The data is already in the right format  
48 - _data = buf.output();  
49 - } else if (pdfDocument.deflate != null) {  
50 - final original = buf.output();  
51 - final newData = Uint8List.fromList(pdfDocument.deflate!(original));  
52 - if (newData.lengthInBytes < original.lengthInBytes) {  
53 - params['/Filter'] = const PdfName('/FlateDecode');  
54 - _data = newData;  
55 - }  
56 - }  
57 -  
58 - if (_data == null) {  
59 - if (isBinary) {  
60 - // This is an Ascii85 stream  
61 - final e = Ascii85Encoder();  
62 - _data = e.convert(buf.output());  
63 - params['/Filter'] = const PdfName('/ASCII85Decode');  
64 - } else {  
65 - // This is a non-deflated stream  
66 - _data = buf.output();  
67 - }  
68 - }  
69 - if (pdfDocument.encryption != null) {  
70 - _data = pdfDocument.encryption!.encrypt(_data!, this);  
71 - }  
72 - params['/Length'] = PdfNum(_data!.length);  
73 - }  
74 -  
75 @override 37 @override
76 void writeContent(PdfStream os) { 38 void writeContent(PdfStream os) {
77 - super.writeContent(os);  
78 -  
79 - os.putString('stream\n');  
80 - os.putBytes(_data!);  
81 - os.putString('\nendstream\n'); 39 + PdfDictStream(
  40 + object: this,
  41 + isBinary: isBinary,
  42 + values: params.values,
  43 + data: buf.output(),
  44 + ).output(os);
82 } 45 }
83 } 46 }
@@ -17,7 +17,7 @@ @@ -17,7 +17,7 @@
17 import 'color.dart'; 17 import 'color.dart';
18 import 'data_types.dart'; 18 import 'data_types.dart';
19 import 'document.dart'; 19 import 'document.dart';
20 -import 'object.dart'; 20 +import 'object_dict.dart';
21 import 'page.dart'; 21 import 'page.dart';
22 import 'rect.dart'; 22 import 'rect.dart';
23 23
@@ -46,7 +46,7 @@ enum PdfOutlineStyle { @@ -46,7 +46,7 @@ enum PdfOutlineStyle {
46 } 46 }
47 47
48 /// Pdf Outline object 48 /// Pdf Outline object
49 -class PdfOutline extends PdfObject { 49 +class PdfOutline extends PdfObjectDict {
50 /// Constructs a Pdf Outline object. 50 /// Constructs a Pdf Outline object.
51 /// When selected, the specified region is displayed. 51 /// When selected, the specified region is displayed.
52 PdfOutline( 52 PdfOutline(
@@ -19,10 +19,10 @@ import 'package:vector_math/vector_math_64.dart'; @@ -19,10 +19,10 @@ import 'package:vector_math/vector_math_64.dart';
19 import 'data_types.dart'; 19 import 'data_types.dart';
20 import 'document.dart'; 20 import 'document.dart';
21 import 'graphic_state.dart'; 21 import 'graphic_state.dart';
22 -import 'object.dart'; 22 +import 'object_dict.dart';
23 import 'shading.dart'; 23 import 'shading.dart';
24 24
25 -abstract class PdfPattern extends PdfObject { 25 +abstract class PdfPattern extends PdfObjectDict {
26 PdfPattern(PdfDocument pdfDocument, this.patternType, this.matrix) 26 PdfPattern(PdfDocument pdfDocument, this.patternType, this.matrix)
27 : super(pdfDocument); 27 : super(pdfDocument);
28 28
@@ -17,13 +17,13 @@ @@ -17,13 +17,13 @@
17 import 'data_types.dart'; 17 import 'data_types.dart';
18 import 'document.dart'; 18 import 'document.dart';
19 import 'function.dart'; 19 import 'function.dart';
20 -import 'object.dart'; 20 +import 'object_dict.dart';
21 import 'point.dart'; 21 import 'point.dart';
22 import 'rect.dart'; 22 import 'rect.dart';
23 23
24 enum PdfShadingType { axial, radial } 24 enum PdfShadingType { axial, radial }
25 25
26 -class PdfShading extends PdfObject { 26 +class PdfShading extends PdfObjectDict {
27 PdfShading( 27 PdfShading(
28 PdfDocument pdfDocument, { 28 PdfDocument pdfDocument, {
29 required this.shadingType, 29 required this.shadingType,