David PHAM-VAN

Remove dependency to Image

... ... @@ -16,8 +16,6 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
library pdf;
/// http://www.mactech.com/articles/mactech/Vol.15/15.09/PDFIntro/index.html
/// https://brendanzagaeski.appspot.com/0004.html
/// http://blog.idrsolutions.com/?s=%22Make+your+own+PDF+file%22
... ... @@ -29,15 +27,19 @@ library pdf;
/// https://www.w3.org/TR/SVG/paths.html#PathDataGeneralInformation
/// https://www.pdf-online.com/osa/validate.aspx
library pdf;
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'package:vector_math/vector_math_64.dart';
import 'package:image/image.dart';
import 'package:meta/meta.dart';
import 'package:ttf_parser/ttf_parser.dart';
import 'package:vector_math/vector_math_64.dart';
part 'src/ascii85.dart';
part 'src/annotation.dart';
part 'src/array.dart';
part 'src/ascii85.dart';
part 'src/border.dart';
part 'src/catalog.dart';
part 'src/color.dart';
... ... @@ -45,6 +47,8 @@ part 'src/document.dart';
part 'src/font.dart';
part 'src/font_descriptor.dart';
part 'src/formxobject.dart';
part 'src/graphics.dart';
part 'src/image.dart';
part 'src/info.dart';
part 'src/object.dart';
part 'src/object_stream.dart';
... ... @@ -55,11 +59,9 @@ part 'src/page_format.dart';
part 'src/page_list.dart';
part 'src/point.dart';
part 'src/polygon.dart';
part 'src/annotation.dart';
part 'src/graphics.dart';
part 'src/image.dart';
part 'src/rect.dart';
part 'src/stream.dart';
part 'src/ttffont.dart';
part 'src/xobject.dart';
part 'src/xref.dart';
... ...
... ... @@ -19,10 +19,22 @@
part of pdf;
class PDFImage extends PDFXObject {
final Image _img;
/// RGBA Image Data
final Uint8List image;
/// Image width
final int width;
/// Image height
final int height;
/// Image has alpha channel
final bool alpha;
String _name;
final bool _alphaChannel;
/// Process alphaChannel only
final bool alphaChannel;
/// Creates a new <code>PDFImage</code> instance.
///
... ... @@ -32,63 +44,62 @@ class PDFImage extends PDFXObject {
/// @param w an <code>int</code> value
/// @param h an <code>int</code> value
/// @param obs an <code>ImageObserver</code> value
PDFImage(PDFDocument pdfDocument, this._img, [this._alphaChannel = false]) : super(pdfDocument, "/Image", isBinary: true) {
PDFImage(PDFDocument pdfDocument,
{@required this.image,
@required this.width,
@required this.height,
this.alpha = true,
this.alphaChannel = false})
: assert(alphaChannel == false || alpha == true),
assert(width != null),
assert(height != null),
super(pdfDocument, "/Image", isBinary: true) {
_name = "/Image$objser";
params["/Width"] = PDFStream.string(_img.width.toString());
params["/Height"] = PDFStream.string(_img.height.toString());
params["/Width"] = PDFStream.string(width.toString());
params["/Height"] = PDFStream.string(height.toString());
params["/BitsPerComponent"] = PDFStream.intNum(8);
params['/Name'] = PDFStream.string(_name);
if (_alphaChannel == false && _img.numChannels == 4) {
var _sMask = new PDFImage(pdfDocument, this._img, true);
if (alphaChannel == false && alpha) {
var _sMask = new PDFImage(pdfDocument,
image: image, width: width, height: height, alpha: alpha, alphaChannel: true);
params["/SMask"] = PDFStream.string("${_sMask.objser} 0 R");
}
if (_alphaChannel) {
if (alphaChannel) {
params["/ColorSpace"] = PDFStream.string("/DeviceGray");
} else {
params["/ColorSpace"] = PDFStream.string("/DeviceRGB");
}
}
@override
void prepare() {
// write the pixels to the stream
// print("Processing image ${img.width}x${img.height} pixels");
int w = _img.width;
int h = _img.height;
int w = width;
int h = height;
int s = w * h;
Uint8List out = new Uint8List(_alphaChannel ? s : s * 3);
Uint8List out = new Uint8List(alphaChannel ? s : s * 3);
if (_alphaChannel) {
if (alphaChannel) {
for (int i = 0; i < s; i++) {
final p = _img.data[i];
final int alpha = (p >> 24) & 0xff;
out[i] = alpha;
out[i] = image[i * 4 + 3];
}
} else {
for (int i = 0; i < s; i++) {
final p = _img.data[i];
final int blue = (p >> 16) & 0xff;
final int green = (p >> 8) & 0xff;
final int red = p & 0xff;
out[i * 3] = red;
out[i * 3 + 1] = green;
out[i * 3 + 2] = blue;
out[i * 3] = image[i * 4];
out[i * 3 + 1] = image[i * 4 + 1];
out[i * 3 + 2] = image[i * 4 + 2];
}
}
buf.putBytes(out);
}
/// Get the value of width.
/// @return value of width.
int get width => _img.width;
/// Get the value of height.
/// @return value of height.
int get height => _img.height;
super.prepare();
}
/// Get the name
///
... ...
... ... @@ -57,6 +57,7 @@ class PDFObject {
}
/// Prepare the object to be written to the stream
@mustCallSuper
void prepare() {}
/// The write method should call this before writing anything to the
... ...
... ... @@ -31,7 +31,8 @@ class PDFObjectStream extends PDFObject {
/// <p>By default, the stream will be compressed.
/// @param type type for the stream
/// @see PDFImage
PDFObjectStream(PDFDocument pdfDocument, {String type, this.isBinary = false}) : super(pdfDocument, type);
PDFObjectStream(PDFDocument pdfDocument, {String type, this.isBinary = false})
: super(pdfDocument, type);
Uint8List _data;
... ... @@ -40,7 +41,7 @@ class PDFObjectStream extends PDFObject {
super.prepare();
if (pdfDocument.deflate) {
var z = new ZLibCodec(level: ZLibOption.MAX_LEVEL);
var z = new ZLibCodec(level: ZLibOption.maxLevel);
_data = z.encode(buf.output());
params["/Filter"] = PDFStream.string("/FlateDecode");
} else if (isBinary) {
... ...
... ... @@ -5,7 +5,6 @@ homepage: https://github.com/davbfr/dart_pdf
version: 1.0.0
dependencies:
image: "^1.1.29"
ttf_parser: "^1.0.0"
vector_math:
... ...
import 'dart:io';
import 'dart:math';
import 'package:image/image.dart';
import 'package:pdf/pdf.dart';
import 'package:test/test.dart';
import 'package:vector_math/vector_math_64.dart';
void main() {
test('Pdf', () {
Image img = new Image(10, 10);
img.fill(0x12345678);
// Image img = new Image(10, 10);
// img.fill(0x12345678);
var pdf = new PDFDocument(deflate: false);
var i = pdf.info;
... ... @@ -29,7 +27,8 @@ void main() {
g.restoreContext();
var font1 = new PDFFont(pdf);
var font2 = new PDFTTFFont(pdf, new File("../assets/Nunito-Regular.ttf").readAsBytesSync());
var font2 =
new PDFTTFFont(pdf, new File("../assets/Nunito-Regular.ttf").readAsBytesSync());
var s = "Hello World!";
var r = font2.stringBounds(s);
const FS = 20.0;
... ... @@ -47,7 +46,8 @@ void main() {
g.drawRect(300.0, 150.0, 50.0, 50.0);
g.fillPath();
g.setColor(new PDFColor(0.0, 0.5, 0.0));
var image = new PDFImage(pdf, img);
// var image = new PDFImage(pdf,
// image: img.data.buffer.asUint8List(), width: img.width, height: img.height);
for (var i = 10.0; i < 90.0; i += 5.0) {
g.saveContext();
var tm = new Matrix4.identity();
... ... @@ -55,7 +55,7 @@ void main() {
tm.translate(300.0, -100.0);
g.setTransform(tm);
g.drawString(font1, 12.0, "Hello $i", 20.0, 100.0);
g.drawImage(image, 100.0, 100.0, 80.0);
// g.drawImage(image, 100.0, 100.0, 80.0);
g.restoreContext();
}
... ...