Showing
6 changed files
with
64 additions
and
50 deletions
@@ -16,8 +16,6 @@ | @@ -16,8 +16,6 @@ | ||
16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
17 | */ | 17 | */ |
18 | 18 | ||
19 | -library pdf; | ||
20 | - | ||
21 | /// http://www.mactech.com/articles/mactech/Vol.15/15.09/PDFIntro/index.html | 19 | /// http://www.mactech.com/articles/mactech/Vol.15/15.09/PDFIntro/index.html |
22 | /// https://brendanzagaeski.appspot.com/0004.html | 20 | /// https://brendanzagaeski.appspot.com/0004.html |
23 | /// http://blog.idrsolutions.com/?s=%22Make+your+own+PDF+file%22 | 21 | /// http://blog.idrsolutions.com/?s=%22Make+your+own+PDF+file%22 |
@@ -29,15 +27,19 @@ library pdf; | @@ -29,15 +27,19 @@ library pdf; | ||
29 | /// https://www.w3.org/TR/SVG/paths.html#PathDataGeneralInformation | 27 | /// https://www.w3.org/TR/SVG/paths.html#PathDataGeneralInformation |
30 | /// https://www.pdf-online.com/osa/validate.aspx | 28 | /// https://www.pdf-online.com/osa/validate.aspx |
31 | 29 | ||
30 | +library pdf; | ||
31 | + | ||
32 | import 'dart:convert'; | 32 | import 'dart:convert'; |
33 | import 'dart:io'; | 33 | import 'dart:io'; |
34 | import 'dart:typed_data'; | 34 | import 'dart:typed_data'; |
35 | -import 'package:vector_math/vector_math_64.dart'; | ||
36 | -import 'package:image/image.dart'; | 35 | + |
36 | +import 'package:meta/meta.dart'; | ||
37 | import 'package:ttf_parser/ttf_parser.dart'; | 37 | import 'package:ttf_parser/ttf_parser.dart'; |
38 | +import 'package:vector_math/vector_math_64.dart'; | ||
38 | 39 | ||
39 | -part 'src/ascii85.dart'; | 40 | +part 'src/annotation.dart'; |
40 | part 'src/array.dart'; | 41 | part 'src/array.dart'; |
42 | +part 'src/ascii85.dart'; | ||
41 | part 'src/border.dart'; | 43 | part 'src/border.dart'; |
42 | part 'src/catalog.dart'; | 44 | part 'src/catalog.dart'; |
43 | part 'src/color.dart'; | 45 | part 'src/color.dart'; |
@@ -45,6 +47,8 @@ part 'src/document.dart'; | @@ -45,6 +47,8 @@ part 'src/document.dart'; | ||
45 | part 'src/font.dart'; | 47 | part 'src/font.dart'; |
46 | part 'src/font_descriptor.dart'; | 48 | part 'src/font_descriptor.dart'; |
47 | part 'src/formxobject.dart'; | 49 | part 'src/formxobject.dart'; |
50 | +part 'src/graphics.dart'; | ||
51 | +part 'src/image.dart'; | ||
48 | part 'src/info.dart'; | 52 | part 'src/info.dart'; |
49 | part 'src/object.dart'; | 53 | part 'src/object.dart'; |
50 | part 'src/object_stream.dart'; | 54 | part 'src/object_stream.dart'; |
@@ -55,11 +59,9 @@ part 'src/page_format.dart'; | @@ -55,11 +59,9 @@ part 'src/page_format.dart'; | ||
55 | part 'src/page_list.dart'; | 59 | part 'src/page_list.dart'; |
56 | part 'src/point.dart'; | 60 | part 'src/point.dart'; |
57 | part 'src/polygon.dart'; | 61 | part 'src/polygon.dart'; |
58 | -part 'src/annotation.dart'; | ||
59 | -part 'src/graphics.dart'; | ||
60 | -part 'src/image.dart'; | ||
61 | part 'src/rect.dart'; | 62 | part 'src/rect.dart'; |
62 | part 'src/stream.dart'; | 63 | part 'src/stream.dart'; |
63 | part 'src/ttffont.dart'; | 64 | part 'src/ttffont.dart'; |
64 | part 'src/xobject.dart'; | 65 | part 'src/xobject.dart'; |
65 | part 'src/xref.dart'; | 66 | part 'src/xref.dart'; |
67 | + |
@@ -19,10 +19,22 @@ | @@ -19,10 +19,22 @@ | ||
19 | part of pdf; | 19 | part of pdf; |
20 | 20 | ||
21 | class PDFImage extends PDFXObject { | 21 | class PDFImage extends PDFXObject { |
22 | - final Image _img; | 22 | + /// RGBA Image Data |
23 | + final Uint8List image; | ||
24 | + | ||
25 | + /// Image width | ||
26 | + final int width; | ||
27 | + | ||
28 | + /// Image height | ||
29 | + final int height; | ||
30 | + | ||
31 | + /// Image has alpha channel | ||
32 | + final bool alpha; | ||
33 | + | ||
23 | String _name; | 34 | String _name; |
24 | 35 | ||
25 | - final bool _alphaChannel; | 36 | + /// Process alphaChannel only |
37 | + final bool alphaChannel; | ||
26 | 38 | ||
27 | /// Creates a new <code>PDFImage</code> instance. | 39 | /// Creates a new <code>PDFImage</code> instance. |
28 | /// | 40 | /// |
@@ -32,63 +44,62 @@ class PDFImage extends PDFXObject { | @@ -32,63 +44,62 @@ class PDFImage extends PDFXObject { | ||
32 | /// @param w an <code>int</code> value | 44 | /// @param w an <code>int</code> value |
33 | /// @param h an <code>int</code> value | 45 | /// @param h an <code>int</code> value |
34 | /// @param obs an <code>ImageObserver</code> value | 46 | /// @param obs an <code>ImageObserver</code> value |
35 | - PDFImage(PDFDocument pdfDocument, this._img, [this._alphaChannel = false]) : super(pdfDocument, "/Image", isBinary: true) { | 47 | + PDFImage(PDFDocument pdfDocument, |
48 | + {@required this.image, | ||
49 | + @required this.width, | ||
50 | + @required this.height, | ||
51 | + this.alpha = true, | ||
52 | + this.alphaChannel = false}) | ||
53 | + : assert(alphaChannel == false || alpha == true), | ||
54 | + assert(width != null), | ||
55 | + assert(height != null), | ||
56 | + super(pdfDocument, "/Image", isBinary: true) { | ||
36 | _name = "/Image$objser"; | 57 | _name = "/Image$objser"; |
37 | - params["/Width"] = PDFStream.string(_img.width.toString()); | ||
38 | - params["/Height"] = PDFStream.string(_img.height.toString()); | 58 | + params["/Width"] = PDFStream.string(width.toString()); |
59 | + params["/Height"] = PDFStream.string(height.toString()); | ||
39 | params["/BitsPerComponent"] = PDFStream.intNum(8); | 60 | params["/BitsPerComponent"] = PDFStream.intNum(8); |
40 | params['/Name'] = PDFStream.string(_name); | 61 | params['/Name'] = PDFStream.string(_name); |
41 | 62 | ||
42 | - if (_alphaChannel == false && _img.numChannels == 4) { | ||
43 | - var _sMask = new PDFImage(pdfDocument, this._img, true); | 63 | + if (alphaChannel == false && alpha) { |
64 | + var _sMask = new PDFImage(pdfDocument, | ||
65 | + image: image, width: width, height: height, alpha: alpha, alphaChannel: true); | ||
44 | params["/SMask"] = PDFStream.string("${_sMask.objser} 0 R"); | 66 | params["/SMask"] = PDFStream.string("${_sMask.objser} 0 R"); |
45 | } | 67 | } |
46 | 68 | ||
47 | - if (_alphaChannel) { | 69 | + if (alphaChannel) { |
48 | params["/ColorSpace"] = PDFStream.string("/DeviceGray"); | 70 | params["/ColorSpace"] = PDFStream.string("/DeviceGray"); |
49 | } else { | 71 | } else { |
50 | params["/ColorSpace"] = PDFStream.string("/DeviceRGB"); | 72 | params["/ColorSpace"] = PDFStream.string("/DeviceRGB"); |
51 | } | 73 | } |
74 | + } | ||
52 | 75 | ||
76 | + @override | ||
77 | + void prepare() { | ||
53 | // write the pixels to the stream | 78 | // write the pixels to the stream |
54 | // print("Processing image ${img.width}x${img.height} pixels"); | 79 | // print("Processing image ${img.width}x${img.height} pixels"); |
55 | 80 | ||
56 | - int w = _img.width; | ||
57 | - int h = _img.height; | 81 | + int w = width; |
82 | + int h = height; | ||
58 | int s = w * h; | 83 | int s = w * h; |
59 | 84 | ||
60 | - Uint8List out = new Uint8List(_alphaChannel ? s : s * 3); | 85 | + Uint8List out = new Uint8List(alphaChannel ? s : s * 3); |
61 | 86 | ||
62 | - if (_alphaChannel) { | 87 | + if (alphaChannel) { |
63 | for (int i = 0; i < s; i++) { | 88 | for (int i = 0; i < s; i++) { |
64 | - final p = _img.data[i]; | ||
65 | - final int alpha = (p >> 24) & 0xff; | ||
66 | - | ||
67 | - out[i] = alpha; | 89 | + out[i] = image[i * 4 + 3]; |
68 | } | 90 | } |
69 | } else { | 91 | } else { |
70 | for (int i = 0; i < s; i++) { | 92 | for (int i = 0; i < s; i++) { |
71 | - final p = _img.data[i]; | ||
72 | - final int blue = (p >> 16) & 0xff; | ||
73 | - final int green = (p >> 8) & 0xff; | ||
74 | - final int red = p & 0xff; | ||
75 | - | ||
76 | - out[i * 3] = red; | ||
77 | - out[i * 3 + 1] = green; | ||
78 | - out[i * 3 + 2] = blue; | 93 | + out[i * 3] = image[i * 4]; |
94 | + out[i * 3 + 1] = image[i * 4 + 1]; | ||
95 | + out[i * 3 + 2] = image[i * 4 + 2]; | ||
79 | } | 96 | } |
80 | } | 97 | } |
81 | 98 | ||
82 | buf.putBytes(out); | 99 | buf.putBytes(out); |
83 | - } | ||
84 | 100 | ||
85 | - /// Get the value of width. | ||
86 | - /// @return value of width. | ||
87 | - int get width => _img.width; | ||
88 | - | ||
89 | - /// Get the value of height. | ||
90 | - /// @return value of height. | ||
91 | - int get height => _img.height; | 101 | + super.prepare(); |
102 | + } | ||
92 | 103 | ||
93 | /// Get the name | 104 | /// Get the name |
94 | /// | 105 | /// |
@@ -57,6 +57,7 @@ class PDFObject { | @@ -57,6 +57,7 @@ class PDFObject { | ||
57 | } | 57 | } |
58 | 58 | ||
59 | /// Prepare the object to be written to the stream | 59 | /// Prepare the object to be written to the stream |
60 | + @mustCallSuper | ||
60 | void prepare() {} | 61 | void prepare() {} |
61 | 62 | ||
62 | /// The write method should call this before writing anything to the | 63 | /// The write method should call this before writing anything to the |
@@ -31,7 +31,8 @@ class PDFObjectStream extends PDFObject { | @@ -31,7 +31,8 @@ class PDFObjectStream extends PDFObject { | ||
31 | /// <p>By default, the stream will be compressed. | 31 | /// <p>By default, the stream will be compressed. |
32 | /// @param type type for the stream | 32 | /// @param type type for the stream |
33 | /// @see PDFImage | 33 | /// @see PDFImage |
34 | - PDFObjectStream(PDFDocument pdfDocument, {String type, this.isBinary = false}) : super(pdfDocument, type); | 34 | + PDFObjectStream(PDFDocument pdfDocument, {String type, this.isBinary = false}) |
35 | + : super(pdfDocument, type); | ||
35 | 36 | ||
36 | Uint8List _data; | 37 | Uint8List _data; |
37 | 38 | ||
@@ -40,7 +41,7 @@ class PDFObjectStream extends PDFObject { | @@ -40,7 +41,7 @@ class PDFObjectStream extends PDFObject { | ||
40 | super.prepare(); | 41 | super.prepare(); |
41 | 42 | ||
42 | if (pdfDocument.deflate) { | 43 | if (pdfDocument.deflate) { |
43 | - var z = new ZLibCodec(level: ZLibOption.MAX_LEVEL); | 44 | + var z = new ZLibCodec(level: ZLibOption.maxLevel); |
44 | _data = z.encode(buf.output()); | 45 | _data = z.encode(buf.output()); |
45 | params["/Filter"] = PDFStream.string("/FlateDecode"); | 46 | params["/Filter"] = PDFStream.string("/FlateDecode"); |
46 | } else if (isBinary) { | 47 | } else if (isBinary) { |
1 | import 'dart:io'; | 1 | import 'dart:io'; |
2 | import 'dart:math'; | 2 | import 'dart:math'; |
3 | 3 | ||
4 | -import 'package:image/image.dart'; | ||
5 | import 'package:pdf/pdf.dart'; | 4 | import 'package:pdf/pdf.dart'; |
6 | import 'package:test/test.dart'; | 5 | import 'package:test/test.dart'; |
7 | import 'package:vector_math/vector_math_64.dart'; | 6 | import 'package:vector_math/vector_math_64.dart'; |
8 | 7 | ||
9 | - | ||
10 | void main() { | 8 | void main() { |
11 | test('Pdf', () { | 9 | test('Pdf', () { |
12 | - Image img = new Image(10, 10); | ||
13 | - img.fill(0x12345678); | 10 | +// Image img = new Image(10, 10); |
11 | +// img.fill(0x12345678); | ||
14 | 12 | ||
15 | var pdf = new PDFDocument(deflate: false); | 13 | var pdf = new PDFDocument(deflate: false); |
16 | var i = pdf.info; | 14 | var i = pdf.info; |
@@ -29,7 +27,8 @@ void main() { | @@ -29,7 +27,8 @@ void main() { | ||
29 | g.restoreContext(); | 27 | g.restoreContext(); |
30 | var font1 = new PDFFont(pdf); | 28 | var font1 = new PDFFont(pdf); |
31 | 29 | ||
32 | - var font2 = new PDFTTFFont(pdf, new File("../assets/Nunito-Regular.ttf").readAsBytesSync()); | 30 | + var font2 = |
31 | + new PDFTTFFont(pdf, new File("../assets/Nunito-Regular.ttf").readAsBytesSync()); | ||
33 | var s = "Hello World!"; | 32 | var s = "Hello World!"; |
34 | var r = font2.stringBounds(s); | 33 | var r = font2.stringBounds(s); |
35 | const FS = 20.0; | 34 | const FS = 20.0; |
@@ -47,7 +46,8 @@ void main() { | @@ -47,7 +46,8 @@ void main() { | ||
47 | g.drawRect(300.0, 150.0, 50.0, 50.0); | 46 | g.drawRect(300.0, 150.0, 50.0, 50.0); |
48 | g.fillPath(); | 47 | g.fillPath(); |
49 | g.setColor(new PDFColor(0.0, 0.5, 0.0)); | 48 | g.setColor(new PDFColor(0.0, 0.5, 0.0)); |
50 | - var image = new PDFImage(pdf, img); | 49 | +// var image = new PDFImage(pdf, |
50 | +// image: img.data.buffer.asUint8List(), width: img.width, height: img.height); | ||
51 | for (var i = 10.0; i < 90.0; i += 5.0) { | 51 | for (var i = 10.0; i < 90.0; i += 5.0) { |
52 | g.saveContext(); | 52 | g.saveContext(); |
53 | var tm = new Matrix4.identity(); | 53 | var tm = new Matrix4.identity(); |
@@ -55,7 +55,7 @@ void main() { | @@ -55,7 +55,7 @@ void main() { | ||
55 | tm.translate(300.0, -100.0); | 55 | tm.translate(300.0, -100.0); |
56 | g.setTransform(tm); | 56 | g.setTransform(tm); |
57 | g.drawString(font1, 12.0, "Hello $i", 20.0, 100.0); | 57 | g.drawString(font1, 12.0, "Hello $i", 20.0, 100.0); |
58 | - g.drawImage(image, 100.0, 100.0, 80.0); | 58 | +// g.drawImage(image, 100.0, 100.0, 80.0); |
59 | g.restoreContext(); | 59 | g.restoreContext(); |
60 | } | 60 | } |
61 | 61 |
-
Please register or login to post a comment