David PHAM-VAN

Remove new in class instanciations

... ... @@ -14,27 +14,27 @@ The coordinate system is using the internal Pdf system:
Example:
```dart
final pdf = new PdfDocument();
final page = new PdfPage(pdf, pageFormat: PdfPageFormat.letter);
final pdf = PdfDocument();
final page = PdfPage(pdf, pageFormat: PdfPageFormat.letter);
final g = page.getGraphics();
final font = new PdfFont(pdf);
final font = PdfFont(pdf);
g.setColor(new PdfColor(0.0, 1.0, 1.0));
g.setColor(PdfColor(0.0, 1.0, 1.0));
g.drawRect(50.0, 30.0, 100.0, 50.0);
g.fillPath();
g.setColor(new PdfColor(0.3, 0.3, 0.3));
g.setColor(PdfColor(0.3, 0.3, 0.3));
g.drawString(font, 12.0, "Hello World!", 5.0 * PdfPageFormat.mm, 300.0);
var file = new File('file.pdf');
var file = File('file.pdf');
file.writeAsBytesSync(pdf.save());
```
To load an image it is possible to use the dart library [image](https://pub.dartlang.org/packages/image):
```dart
Image image = decodeImage(new Io.File('test.webp').readAsBytesSync());
PdfImage image = new PdfImage(
Image image = decodeImage(Io.File('test.webp').readAsBytesSync());
PdfImage image = PdfImage(
pdf,
image: img.data.buffer.asUint8List(),
width: img.width,
... ... @@ -45,9 +45,9 @@ g.drawImage(image, 100.0, 100.0, 80.0);
To use a TrueType font:
```dart
PdfTtfFont ttf = new PdfTtfFont(
PdfTtfFont ttf = PdfTtfFont(
pdf,
(new File("open-sans.ttf").readAsBytesSync() as Uint8List).buffer.asByteData());
g.setColor(new PdfColor(0.3, 0.3, 0.3));
(File("open-sans.ttf").readAsBytesSync() as Uint8List).buffer.asByteData());
g.setColor(PdfColor(0.3, 0.3, 0.3));
g.drawString(ttf, 20.0, "Dart is awesome", 50.0, 30.0);
```
... ...
... ... @@ -3,21 +3,21 @@ import 'dart:io';
import 'package:pdf/pdf.dart';
void main() {
final pdf = new PdfDocument(deflate: zlib.encode);
final page = new PdfPage(pdf, pageFormat: PdfPageFormat.letter);
final pdf = PdfDocument(deflate: zlib.encode);
final page = PdfPage(pdf, pageFormat: PdfPageFormat.letter);
final g = page.getGraphics();
final font = new PdfFont(pdf);
final font = PdfFont(pdf);
final top = page.pageFormat.height;
g.setColor(new PdfColor(0.0, 1.0, 1.0));
g.setColor(PdfColor(0.0, 1.0, 1.0));
g.drawRect(50.0 * PdfPageFormat.mm, top - 80.0 * PdfPageFormat.mm,
100.0 * PdfPageFormat.mm, 50.0 * PdfPageFormat.mm);
g.fillPath();
g.setColor(new PdfColor(0.3, 0.3, 0.3));
g.setColor(PdfColor(0.3, 0.3, 0.3));
g.drawString(font, 12.0, "Hello World!", 10.0 * PdfPageFormat.mm,
top - 10.0 * PdfPageFormat.mm);
var file = new File('example.pdf');
var file = File('example.pdf');
file.writeAsBytesSync(pdf.save());
}
... ...
... ... @@ -53,13 +53,13 @@ class PdfAnnot extends PdfObject {
/// @param s Subtype for this annotation
/// @param rect coordinates
factory PdfAnnot.annotation(PdfPage pdfPage, String s, PdfRect rect) =>
new PdfAnnot(pdfPage, type: "/Annot", s: s, srcRect: rect);
PdfAnnot(pdfPage, type: "/Annot", s: s, srcRect: rect);
/// Creates a text annotation
/// @param rect coordinates
/// @param s Text for this annotation
factory PdfAnnot.text(PdfPage pdfPage, PdfRect rect, String s) =>
new PdfAnnot(pdfPage, type: "/Text", srcRect: rect, s: s);
PdfAnnot(pdfPage, type: "/Text", srcRect: rect, s: s);
/// Creates a link annotation
/// @param srcRect coordinates
... ... @@ -68,7 +68,7 @@ class PdfAnnot extends PdfObject {
/// (must be in User Coordinates)
factory PdfAnnot.link(PdfPage pdfPage, PdfRect srcRect, PdfObject dest,
[PdfRect destRect]) =>
new PdfAnnot(pdfPage,
PdfAnnot(pdfPage,
type: "/Link", srcRect: srcRect, dest: dest, destRect: destRect);
/// Sets the border for the annotation. By default, no border is defined.
... ... @@ -86,7 +86,7 @@ class PdfAnnot extends PdfObject {
/// is null, then the default of {3} is used.
void setBorder(double width,
{PdfBorderStyle style = PdfBorderStyle.solid, List<double> dash}) {
border = new PdfBorder(pdfDocument, width, style: style, dash: dash);
border = PdfBorder(pdfDocument, width, style: style, dash: dash);
}
/// Output the annotation
... ... @@ -111,7 +111,7 @@ class PdfAnnot extends PdfObject {
if (subtype == "/Text") {
params["/Contents"] = PdfStream.string(s);
} else if (subtype == "/Link") {
var dests = new List<PdfStream>();
var dests = List<PdfStream>();
dests.add(dest.ref());
if (destRect == null)
dests.add(PdfStream.string("/Fit"));
... ...
... ... @@ -20,7 +20,7 @@ part of pdf;
class Ascii85Encoder extends Converter<List<int>, List<int>> {
List<int> convert(List<int> input) {
Uint8List buffer = new Uint8List(_maxEncodedLen(input.length) + 2);
Uint8List buffer = Uint8List(_maxEncodedLen(input.length) + 2);
var b = 0;
var s = 0;
... ...
... ... @@ -64,7 +64,7 @@ class PdfBorder extends PdfObject {
void writeContent(PdfStream os) {
super.writeContent(os);
var data = new List<PdfStream>();
var data = List<PdfStream>();
data.add(PdfStream.string("/S"));
data.add(PdfStream.string(
"/" + "SDBIU".substring(style.index, style.index + 1)));
... ...
... ... @@ -24,20 +24,17 @@ class PdfColor {
final double g;
final double b;
static var black = new PdfColor(0.0, 0.0, 0.0);
static var black = PdfColor(0.0, 0.0, 0.0);
const PdfColor(this.r, this.g, this.b, [this.a = 1.0]);
factory PdfColor.fromInt(int color) {
return new PdfColor(
(color >> 16 & 0xff) / 255.0,
(color >> 8 & 0xff) / 255.0,
(color & 0xff) / 255.0,
(color >> 24 & 0xff) / 255.0);
return PdfColor((color >> 16 & 0xff) / 255.0, (color >> 8 & 0xff) / 255.0,
(color & 0xff) / 255.0, (color >> 24 & 0xff) / 255.0);
}
factory PdfColor.fromHex(String color) {
return new PdfColor(
return PdfColor(
(int.parse(color.substring(0, 1), radix: 16) >> 16 & 0xff) / 255.0,
(int.parse(color.substring(2, 3), radix: 16) >> 8 & 0xff) / 255.0,
(int.parse(color.substring(4, 5), radix: 16) & 0xff) / 255.0,
... ...
... ... @@ -50,11 +50,11 @@ class PDFAnnot extends PdfAnnot {
factory PDFAnnot.annotation(
PdfPage pdfPage, String s, double l, double b, double r, double t) =>
new PDFAnnot(pdfPage, type: "/Annot", s: s, l: l, b: b, r: r, t: t);
PDFAnnot(pdfPage, type: "/Annot", s: s, l: l, b: b, r: r, t: t);
factory PDFAnnot.text(
PdfPage pdfPage, double l, double b, double r, double t, String s) =>
new PDFAnnot(pdfPage, type: "/Text", l: l, b: b, r: r, t: t, s: s);
PDFAnnot(pdfPage, type: "/Text", l: l, b: b, r: r, t: t, s: s);
factory PDFAnnot.link(PdfPage pdfPage, double l, double b, double r, double t,
PdfObject dest,
... ... @@ -62,7 +62,7 @@ class PDFAnnot extends PdfAnnot {
double fb = FULL_PAGE,
double fr = FULL_PAGE,
double ft = FULL_PAGE]) =>
new PDFAnnot(pdfPage,
PDFAnnot(pdfPage,
type: "/Link",
l: l,
b: b,
... ... @@ -249,7 +249,7 @@ class PDFPage extends PdfPage {
/// @return a Dimension object containing the width and height of the page.
/// use pageFormat.dimension
@deprecated
PdfPoint getDimension() => new PdfPoint(pageFormat.width, pageFormat.height);
PdfPoint getDimension() => PdfPoint(pageFormat.width, pageFormat.height);
/// This method adds a text note to the document.
/// @param note Text of the note
... ... @@ -262,8 +262,8 @@ class PDFPage extends PdfPage {
PdfAnnot addNote(String note, double x, y, w, h) {
var xy1 = cxy(x, y + h);
var xy2 = cxy(x + w, y);
PdfAnnot ob = new PdfAnnot.text(
this, PdfRect.fromLTRB(xy1.x, xy1.y, xy2.x, xy2.y), note);
PdfAnnot ob =
PdfAnnot.text(this, PdfRect.fromLTRB(xy1.x, xy1.y, xy2.x, xy2.y), note);
return ob;
}
... ... @@ -289,7 +289,7 @@ class PDFPage extends PdfPage {
var xy2 = cxy(x + w, y);
var xy3 = cxy(vx, vy + vh);
var xy4 = cxy(vx + vw, vy);
PdfAnnot ob = new PdfAnnot.link(
PdfAnnot ob = PdfAnnot.link(
this,
PdfRect.fromLTRB(xy1.x, xy1.y, xy2.x, xy2.y),
dest,
... ... @@ -310,7 +310,7 @@ class PDFPage extends PdfPage {
{double x, double y, double w, double h}) {
PdfPoint xy1 = cxy(x, y + h);
PdfPoint xy2 = cxy(x + w, y);
PdfOutline outline = new PdfOutline(pdfDocument,
PdfOutline outline = PdfOutline(pdfDocument,
title: title,
dest: this,
rect: PdfRect.fromLTRB(xy1.x, xy2.y, xy2.x, xy1.y));
... ... @@ -340,7 +340,7 @@ class PDFPage extends PdfPage {
/// @param y Coordinate in User space
/// @return array containing the x & y Coordinate in User space
@deprecated
PdfPoint cxy(double x, double y) => new PdfPoint(x, pageFormat.height - y);
PdfPoint cxy(double x, double y) => PdfPoint(x, pageFormat.height - y);
}
@deprecated
... ...
... ... @@ -49,7 +49,7 @@ class PdfDocument {
int _objser;
/// This vector contains each indirect object within the document.
final Set<PdfObject> objects = new Set<PdfObject>();
final Set<PdfObject> objects = Set<PdfObject>();
/// This is the Catalog object, which is required by each Pdf Document
PdfCatalog catalog;
... ... @@ -82,7 +82,7 @@ class PdfDocument {
];
/// This holds the current fonts
final Set<PdfFont> fonts = new Set<PdfFont>();
final Set<PdfFont> fonts = Set<PdfFont>();
/// Creates a new serial number
int _genSerial() => _objser++;
... ... @@ -94,9 +94,9 @@ class PdfDocument {
_objser = 1;
// Now create some standard objects
pdfPageList = new PdfPageList(this);
catalog = new PdfCatalog(this, pdfPageList, pageMode);
info = new PdfInfo(this);
pdfPageList = PdfPageList(this);
catalog = PdfCatalog(this, pdfPageList, pageMode);
info = PdfInfo(this);
}
/// This returns a specific page. It's used mainly when using a
... ... @@ -113,7 +113,7 @@ class PdfDocument {
/// @return the root outline
PdfOutline get outline {
if (_outline == null) {
_outline = new PdfOutline(this);
_outline = PdfOutline(this);
catalog.outlines = _outline;
}
return _outline;
... ... @@ -131,7 +131,7 @@ class PdfDocument {
///
/// @param os OutputStream to write the document to
void write(PdfStream os) {
PdfOutput pos = new PdfOutput(os);
PdfOutput pos = PdfOutput(os);
// Write each object to the [PdfStream]. We call via the output
// as that builds the xref table
... ... @@ -144,7 +144,7 @@ class PdfDocument {
}
List<int> save() {
PdfStream os = new PdfStream();
PdfStream os = PdfStream();
write(os);
return os.output();
}
... ...
... ... @@ -79,7 +79,7 @@ class PdfFont extends PdfObject {
w += n == chars.length - 1 ? r.w : glyphAdvance(c);
}
return new PdfRect(x, y, w, h);
return PdfRect(x, y, w, h);
}
PdfPoint stringSize(String s) {
... ... @@ -94,6 +94,6 @@ class PdfFont extends PdfObject {
w += glyphAdvance(c);
}
return new PdfPoint(w, h);
return PdfPoint(w, h);
}
}
... ...
... ... @@ -32,7 +32,7 @@ class PdfFontDescriptor extends PdfObject {
params["/FontName"] = PdfStream.string(ttfFont.baseFont);
params["/FontFile2"] = file.ref();
params["/Flags"] = PdfStream.intNum(32);
params["/FontBBox"] = new PdfStream()
params["/FontBBox"] = PdfStream()
..putStringArray([
ttfFont.font.xMin,
ttfFont.font.yMin,
... ...
... ... @@ -20,10 +20,10 @@ part of pdf;
class PdfFormXObject extends PdfXObject {
/// The fonts associated with this page
final fonts = new Map<String, PdfFont>();
final fonts = Map<String, PdfFont>();
/// The xobjects or other images in the pdf
final xobjects = new Map<String, PdfXObject>();
final xobjects = Map<String, PdfXObject>();
PdfFormXObject(PdfDocument pdfDocument) : super(pdfDocument, '/Form') {
params["/FormType"] = PdfStream.string("1");
... ... @@ -43,16 +43,16 @@ class PdfFormXObject extends PdfXObject {
// Now the resources
/// This holds any resources for this FormXObject
final resources = new Map<String, PdfStream>();
final resources = Map<String, PdfStream>();
// fonts
if (fonts.length > 0) {
resources["/Font"] = new PdfStream()..putObjectDictionary(fonts);
resources["/Font"] = PdfStream()..putObjectDictionary(fonts);
}
// Now the XObjects
if (xobjects.length > 0) {
resources["/XObject"] = new PdfStream()..putObjectDictionary(xobjects);
resources["/XObject"] = PdfStream()..putObjectDictionary(xobjects);
}
if (resources.length > 0) {
... ...
... ... @@ -32,7 +32,7 @@ class PdfGraphics {
PdfFont get defaultFont {
if (page.pdfDocument.fonts.length == 0) {
new PdfFont(page.pdfDocument);
PdfFont(page.pdfDocument);
}
return page.pdfDocument.fonts.elementAt(0);
... ... @@ -200,9 +200,9 @@ class PdfGraphics {
}
void drawShape(String d) {
var sb = new StringBuffer();
var sb = StringBuffer();
RegExp exp = new RegExp(r"([MmZzLlHhVvCcSsQqTtAa])|(-[\.0-9]+)|([\.0-9]+)");
RegExp exp = RegExp(r"([MmZzLlHhVvCcSsQqTtAa])|(-[\.0-9]+)|([\.0-9]+)");
var matches = exp.allMatches(d);
var action;
for (var m in matches) {
... ...
... ... @@ -60,7 +60,7 @@ class PdfImage extends PdfXObject {
params['/Name'] = PdfStream.string(_name);
if (alphaChannel == false && alpha) {
var _sMask = new PdfImage(pdfDocument,
var _sMask = PdfImage(pdfDocument,
image: image,
width: width,
height: height,
... ... @@ -85,7 +85,7 @@ class PdfImage extends PdfXObject {
int h = height;
int s = w * h;
Uint8List out = new Uint8List(alphaChannel ? s : s * 3);
Uint8List out = Uint8List(alphaChannel ? s : s * 3);
if (alphaChannel) {
for (int i = 0; i < s; i++) {
... ...
... ... @@ -20,7 +20,7 @@ part of pdf;
class PdfObject {
/// This is the object parameters.
final params = new Map<String, PdfStream>();
final params = Map<String, PdfStream>();
/// This is the unique serial number for this object.
final int objser;
... ...
... ... @@ -20,7 +20,7 @@ part of pdf;
class PdfObjectStream extends PdfObject {
/// This holds the stream's content.
final PdfStream buf = new PdfStream();
final PdfStream buf = PdfStream();
/// defines if the stream needs to be converted to ascii85
final bool isBinary;
... ... @@ -46,7 +46,7 @@ class PdfObjectStream extends PdfObject {
params["/Filter"] = PdfStream.string("/FlateDecode");
} else if (isBinary) {
// This is a Ascii85 stream
var e = new Ascii85Encoder();
var e = Ascii85Encoder();
_data = e.convert(buf.output());
params["/Filter"] = PdfStream.string("/ASCII85Decode");
} else {
... ...
... ... @@ -74,7 +74,7 @@ class PdfOutline extends PdfObject {
/// @return [PdfOutline] object created, for creating sub-outlines
PdfOutline add({String title, PdfPage dest, PdfRect rect}) {
PdfOutline outline =
new PdfOutline(pdfDocument, title: title, dest: dest, rect: rect);
PdfOutline(pdfDocument, title: title, dest: dest, rect: rect);
// Tell the outline of ourselves
outline.parent = this;
return outline;
... ... @@ -88,7 +88,7 @@ class PdfOutline extends PdfObject {
// These are for kids only
if (parent != null) {
params["/Title"] = PdfStream.string(title);
var dests = new List<PdfStream>();
var dests = List<PdfStream>();
dests.add(dest.ref());
if (destMode == PdfOutlineMode.fitpage) {
... ...
... ... @@ -48,7 +48,7 @@ class PdfOutput {
if (ob is PdfCatalog) rootID = ob;
if (ob is PdfInfo) infoID = ob;
offsets.add(new PdfXref(ob.objser, os.offset));
offsets.add(PdfXref(ob.objser, os.offset));
ob.write(os);
}
... ... @@ -72,7 +72,7 @@ class PdfOutput {
var block = []; // xrefs in this block
// We need block 0 to exist
block.add(new PdfXref(0, 0, generation: 65535));
block.add(PdfXref(0, 0, generation: 65535));
for (PdfXref x in offsets) {
if (firstid == -1) firstid = x.id;
... ... @@ -107,7 +107,7 @@ class PdfOutput {
os.putStream(rootID.ref());
os.putString("\n");
} else
throw new Exception("Root object is not present in document");
throw Exception("Root object is not present in document");
// the /Info reference (OPTIONAL)
if (infoID != null) {
... ...
... ... @@ -33,10 +33,10 @@ class PdfPage extends PdfObject {
List<PdfAnnot> annotations = [];
/// The fonts associated with this page
final fonts = new Map<String, PdfFont>();
final fonts = Map<String, PdfFont>();
/// The xobjects or other images in the pdf
final xObjects = new Map<String, PdfXObject>();
final xObjects = Map<String, PdfXObject>();
/// This constructs a Page object, which will hold any contents for this
/// page.
... ... @@ -58,8 +58,8 @@ class PdfPage extends PdfObject {
///
/// @return a new [PdfGraphics] object to be used to draw this page.
PdfGraphics getGraphics() {
var stream = new PdfObjectStream(pdfDocument);
var g = new PdfGraphics(this, stream.buf);
var stream = PdfObjectStream(pdfDocument);
var g = PdfGraphics(this, stream.buf);
contents.add(stream);
return g;
}
... ... @@ -83,7 +83,7 @@ class PdfPage extends PdfObject {
params["/Parent"] = pdfDocument.pdfPageList.ref();
// the /MediaBox for the page size
params["/MediaBox"] = new PdfStream()
params["/MediaBox"] = PdfStream()
..putStringArray([0, 0, pageFormat.width, pageFormat.height]);
// Rotation (if not zero)
... ... @@ -98,22 +98,22 @@ class PdfPage extends PdfObject {
if (contents.length == 1) {
params["/Contents"] = contents[0].ref();
} else {
params["/Contents"] = new PdfStream()..putObjectArray(contents);
params["/Contents"] = PdfStream()..putObjectArray(contents);
}
}
// Now the resources
/// This holds any resources for this page
final resources = new Map<String, PdfStream>();
final resources = Map<String, PdfStream>();
// fonts
if (fonts.length > 0) {
resources["/Font"] = new PdfStream()..putObjectDictionary(fonts);
resources["/Font"] = PdfStream()..putObjectDictionary(fonts);
}
// Now the XObjects
if (xObjects.length > 0) {
resources["/XObject"] = new PdfStream()..putObjectDictionary(xObjects);
resources["/XObject"] = PdfStream()..putObjectDictionary(xObjects);
}
params["/Resources"] = PdfStream.dictionary(resources);
... ... @@ -125,7 +125,7 @@ class PdfPage extends PdfObject {
// The /Annots object
if (annotations.length > 0) {
params["/Annots"] = new PdfStream()..putObjectArray(annotations);
params["/Annots"] = PdfStream()..putObjectArray(annotations);
}
}
}
... ...
... ... @@ -35,7 +35,7 @@ class PdfPageFormat {
const PdfPageFormat(this.width, this.height);
PdfPoint get dimension => new PdfPoint(width, height);
PdfPoint get dimension => PdfPoint(width, height);
PdfPageFormat get landscape =>
width >= height ? this : PdfPageFormat(height, width);
... ...
... ... @@ -34,7 +34,7 @@ class PdfPageList extends PdfObject {
void prepare() {
super.prepare();
params["/Kids"] = new PdfStream()..putObjectArray(pages);
params["/Kids"] = PdfStream()..putObjectArray(pages);
params["/Count"] = PdfStream.intNum(pages.length);
}
}
... ...
... ... @@ -35,7 +35,7 @@ class PdfStream {
}
}
static PdfStream string(String s) => new PdfStream()..putString(s);
static PdfStream string(String s) => PdfStream()..putString(s);
void putStringUtf16(String s) {
for (int codeUnit in s.codeUnits) {
... ... @@ -52,8 +52,8 @@ class PdfStream {
putString(d.toString());
}
static PdfStream num(double d) => new PdfStream()..putNum(d);
static PdfStream intNum(int i) => new PdfStream()..putString(i.toString());
static PdfStream num(double d) => PdfStream()..putNum(d);
static PdfStream intNum(int i) => PdfStream()..putString(i.toString());
void putText(String s) {
// Escape special characters
... ... @@ -79,7 +79,7 @@ class PdfStream {
putBytes(latin1.encode('(' + s + ')'));
}
static PdfStream text(String s) => new PdfStream()..putText(s);
static PdfStream text(String s) => PdfStream()..putText(s);
void putBool(bool value) {
putString(value ? "true" : "false");
... ... @@ -108,7 +108,7 @@ class PdfStream {
}
static PdfStream array(List<PdfStream> values) =>
new PdfStream()..putArray(values);
PdfStream()..putArray(values);
void putDictionary(Map<String, PdfStream> values) {
putString("<< ");
... ... @@ -121,7 +121,7 @@ class PdfStream {
}
static PdfStream dictionary(Map<String, PdfStream> values) =>
new PdfStream()..putDictionary(values);
PdfStream()..putDictionary(values);
void putObjectDictionary(Map<String, PdfObject> values) {
putString("<< ");
... ...
... ... @@ -29,12 +29,12 @@ class TtfParser {
static const _GLYF = "glyf";
final ByteData bytes;
final _tableOffsets = new Map<String, int>();
final _tableOffsets = Map<String, int>();
String _fontName;
final advanceWidth = new List<double>();
final charToGlyphIndexMap = new Map<int, int>();
final glyphOffsets = new List<int>();
final glyphInfoMap = new Map<int, PdfRect>();
final advanceWidth = List<double>();
final charToGlyphIndexMap = Map<int, int>();
final glyphOffsets = List<int>();
final glyphInfoMap = Map<int, PdfRect>();
TtfParser(this.bytes) {
final numTables = bytes.getUint16(4);
... ... @@ -138,20 +138,20 @@ class TtfParser {
void _parseCMapFormat4(int basePosition, int length) {
final segCount = bytes.getUint16(basePosition + 2) ~/ 2;
final endCodes = new List<int>();
final endCodes = List<int>();
for (var i = 0; i < segCount; i++) {
endCodes.add(bytes.getUint16(basePosition + i * 2 + 10));
}
final startCodes = new List<int>();
final startCodes = List<int>();
for (var i = 0; i < segCount; i++) {
startCodes.add(bytes.getUint16(basePosition + (segCount + i) * 2 + 12));
}
final idDeltas = new List<int>();
final idDeltas = List<int>();
for (var i = 0; i < segCount; i++) {
idDeltas.add(bytes.getUint16(basePosition + (segCount * 2 + i) * 2 + 12));
}
final idRangeOffsetBasePos = basePosition + segCount * 6 + 12;
final idRangeOffsets = new List<int>();
final idRangeOffsets = List<int>();
for (var i = 0; i < segCount; i++) {
idRangeOffsets.add(bytes.getUint16(idRangeOffsetBasePos + i * 2));
}
... ... @@ -210,7 +210,7 @@ class TtfParser {
final yMin = bytes.getInt16(baseOffset + offset + 4); // 4
final xMax = bytes.getInt16(baseOffset + offset + 6); // 6
final yMax = bytes.getInt16(baseOffset + offset + 8); // 8
glyphInfoMap[glyphIndex] = new PdfRect(
glyphInfoMap[glyphIndex] = PdfRect(
xMin.toDouble() / unitsPerEm,
yMin.toDouble() / unitsPerEm,
xMax.toDouble() / unitsPerEm,
... ...
... ... @@ -22,18 +22,18 @@ class PdfTtfFont extends PdfFont {
PdfObject unicodeCMap;
PdfFontDescriptor descriptor;
PdfArrayObject widthsObject;
final widths = new List<String>();
final widths = List<String>();
final TtfParser font;
int _charMin;
int _charMax;
/// Constructs a [PdfTtfFont]
PdfTtfFont(PdfDocument pdfDocument, ByteData bytes)
: font = new TtfParser(bytes),
: font = TtfParser(bytes),
super(pdfDocument, subtype: "/TrueType") {
baseFont = "/" + font.fontName.replaceAll(" ", "");
PdfObjectStream file = new PdfObjectStream(pdfDocument, isBinary: true);
PdfObjectStream file = PdfObjectStream(pdfDocument, isBinary: true);
final data = bytes.buffer.asUint8List();
file.buf.putBytes(data);
file.params["/Length1"] = PdfStream.intNum(data.length);
... ... @@ -45,9 +45,9 @@ class PdfTtfFont extends PdfFont {
widths.add((glyphAdvance(i) * 1000.0).toString());
}
unicodeCMap = new PdfObject(pdfDocument);
descriptor = new PdfFontDescriptor(this, file);
widthsObject = new PdfArrayObject(pdfDocument, widths);
unicodeCMap = PdfObject(pdfDocument);
descriptor = PdfFontDescriptor(this, file);
widthsObject = PdfArrayObject(pdfDocument, widths);
}
@override
... ...
... ... @@ -8,53 +8,53 @@ import 'package:vector_math/vector_math_64.dart';
void main() {
test('Pdf', () {
var img = new Uint32List(10 * 10);
var img = Uint32List(10 * 10);
img.fillRange(0, img.length - 1, 0x12345678);
var pdf = new PdfDocument(deflate: zlib.encode);
var pdf = PdfDocument(deflate: zlib.encode);
var i = pdf.info;
i.author = "David PHAM-VAN";
i.creator = i.author;
i.title = "My Title";
i.subject = "My Subject";
var page = new PdfPage(pdf, pageFormat: const PdfPageFormat(500.0, 300.0));
var page = PdfPage(pdf, pageFormat: const PdfPageFormat(500.0, 300.0));
var g = page.getGraphics();
g.saveContext();
var tm = new Matrix4.identity();
var tm = Matrix4.identity();
tm.translate(100.0, 700.0);
g.setTransform(tm);
// g.drawShape("M37 0H9C6.24 0 4 2.24 4 5v38c0 2.76 2.24 5 5 5h28c2.76 0 5-2.24 5-5V5c0-2.76-2.24-5-5-5zM23 46c-1.66 0-3-1.34-3-3s1.34-3 3-3 3 1.34 3 3-1.34 3-3 3zm15-8H8V6h30v32z");
g.restoreContext();
var font1 = new PdfFont(pdf);
var font1 = PdfFont(pdf);
var font2 = new PdfTtfFont(
var font2 = PdfTtfFont(
pdf,
(new File("open-sans.ttf").readAsBytesSync() as Uint8List)
(File("open-sans.ttf").readAsBytesSync() as Uint8List)
.buffer
.asByteData());
var s = "Hello World!";
var r = font2.stringBounds(s);
const FS = 20.0;
g.setColor(new PdfColor(0.0, 1.0, 1.0));
g.setColor(PdfColor(0.0, 1.0, 1.0));
g.drawRect(50.0 + r.x * FS, 30.0 + r.y * FS, r.w * FS, r.h * FS);
g.fillPath();
g.setColor(new PdfColor(0.3, 0.3, 0.3));
g.setColor(PdfColor(0.3, 0.3, 0.3));
g.drawString(font2, FS, s, 50.0, 30.0);
g.setColor(new PdfColor(1.0, 0.0, 0.0));
g.setColor(PdfColor(1.0, 0.0, 0.0));
g.drawString(font2, 20.0, "Hé (Olà)", 50.0, 10.0);
g.drawLine(30.0, 30.0, 200.0, 200.0);
g.strokePath();
g.setColor(new PdfColor(1.0, 0.0, 0.0));
g.setColor(PdfColor(1.0, 0.0, 0.0));
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,
image: img.buffer.asUint8List(), width: 10, height: 10);
g.setColor(PdfColor(0.0, 0.5, 0.0));
var image =
PdfImage(pdf, image: img.buffer.asUint8List(), width: 10, height: 10);
for (var i = 10.0; i < 90.0; i += 5.0) {
g.saveContext();
var tm = new Matrix4.identity();
var tm = Matrix4.identity();
tm.rotateZ(i * pi / 360.0);
tm.translate(300.0, -100.0);
g.setTransform(tm);
... ... @@ -63,7 +63,7 @@ void main() {
g.restoreContext();
}
var file = new File('file.pdf');
var file = File('file.pdf');
file.writeAsBytesSync(pdf.save());
});
}
... ...
... ... @@ -5,14 +5,14 @@ import "package:test/test.dart";
void main() {
test('Pdf1', () {
var pdf = new PdfDocument();
var page = new PdfPage(pdf, pageFormat: PdfPageFormat.a4);
var pdf = PdfDocument();
var page = PdfPage(pdf, pageFormat: PdfPageFormat.a4);
var g = page.getGraphics();
g.drawLine(30.0, 30.0, 200.0, 200.0);
g.strokePath();
var file = new File('file1.pdf');
var file = File('file1.pdf');
file.writeAsBytesSync(pdf.save());
});
}
... ...
... ... @@ -6,43 +6,43 @@ import 'package:test/test.dart';
void main() {
test('Pdf', () {
var pdf = new PdfDocument();
var pdf = PdfDocument();
var i = pdf.info;
i.author = "David PHAM-VAN";
i.creator = i.author;
i.title = "My Title";
i.subject = "My Subject";
var page = new PdfPage(pdf, pageFormat: const PdfPageFormat(500.0, 300.0));
var page = PdfPage(pdf, pageFormat: const PdfPageFormat(500.0, 300.0));
var g = page.getGraphics();
var ttf = new PdfTtfFont(
var ttf = PdfTtfFont(
pdf,
(new File("open-sans.ttf").readAsBytesSync() as Uint8List)
(File("open-sans.ttf").readAsBytesSync() as Uint8List)
.buffer
.asByteData());
var s = "Hello World!";
var r = ttf.stringBounds(s);
const FS = 20.0;
g.setColor(new PdfColor(0.0, 1.0, 1.0));
g.setColor(PdfColor(0.0, 1.0, 1.0));
g.drawRect(50.0 + r.x * FS, 30.0 + r.y * FS, r.w * FS, r.h * FS);
g.fillPath();
g.setColor(new PdfColor(0.3, 0.3, 0.3));
g.setColor(PdfColor(0.3, 0.3, 0.3));
g.drawString(ttf, FS, s, 50.0, 30.0);
var roboto = new PdfTtfFont(
var roboto = PdfTtfFont(
pdf,
(new File("roboto.ttf").readAsBytesSync() as Uint8List)
(File("roboto.ttf").readAsBytesSync() as Uint8List)
.buffer
.asByteData());
r = roboto.stringBounds(s);
g.setColor(new PdfColor(0.0, 1.0, 1.0));
g.setColor(PdfColor(0.0, 1.0, 1.0));
g.drawRect(50.0 + r.x * FS, 130.0 + r.y * FS, r.w * FS, r.h * FS);
g.fillPath();
g.setColor(new PdfColor(0.3, 0.3, 0.3));
g.setColor(PdfColor(0.3, 0.3, 0.3));
g.drawString(roboto, FS, s, 50.0, 130.0);
var file = new File('file2.pdf');
var file = File('file2.pdf');
file.writeAsBytesSync(pdf.save());
});
}
... ...
... ... @@ -28,7 +28,7 @@ To use a TrueType font from a flutter bundle:
```dart
var font = await rootBundle.load("assets/open-sans.ttf");
PdfTtfFont ttf = new PdfTtfFont(pdf, font);
g.setColor(new PdfColor(0.3, 0.3, 0.3));
PdfTtfFont ttf = PdfTtfFont(pdf, font);
g.setColor(PdfColor(0.3, 0.3, 0.3));
g.drawString(ttf, 20.0, "Dart is awesome", 50.0, 30.0);
```
... ...
... ... @@ -6,32 +6,32 @@ import 'package:flutter/rendering.dart';
import 'package:pdf/pdf.dart';
import 'package:printing/printing.dart';
void main() => runApp(new MaterialApp(home: new MyApp()));
void main() => runApp(MaterialApp(home: MyApp()));
class MyApp extends StatefulWidget {
@override
MyAppState createState() {
return new MyAppState();
return MyAppState();
}
}
class MyAppState extends State<MyApp> {
final shareWidget = new GlobalKey();
final previewContainer = new GlobalKey();
final shareWidget = GlobalKey();
final previewContainer = GlobalKey();
PdfDocument _generateDocument() {
final pdf = new PdfDocument(deflate: zlib.encode);
final page = new PdfPage(pdf, pageFormat: PdfPageFormat.a4);
final pdf = PdfDocument(deflate: zlib.encode);
final page = PdfPage(pdf, pageFormat: PdfPageFormat.a4);
final g = page.getGraphics();
final font = new PdfFont(pdf);
final font = PdfFont(pdf);
final top = page.pageFormat.height;
g.setColor(new PdfColor(0.0, 1.0, 1.0));
g.setColor(PdfColor(0.0, 1.0, 1.0));
g.drawRect(50.0 * PdfPageFormat.mm, top - 80.0 * PdfPageFormat.mm,
100.0 * PdfPageFormat.mm, 50.0 * PdfPageFormat.mm);
g.fillPath();
g.setColor(new PdfColor(0.3, 0.3, 0.3));
g.setColor(PdfColor(0.3, 0.3, 0.3));
g.drawString(font, 12.0, "Hello World!", 10.0 * PdfPageFormat.mm,
top - 10.0 * PdfPageFormat.mm);
... ... @@ -55,15 +55,15 @@ class MyAppState extends State<MyApp> {
referenceBox.localToGlobal(referenceBox.paintBounds.topLeft);
final bottomRight =
referenceBox.localToGlobal(referenceBox.paintBounds.bottomRight);
final bounds = new Rect.fromPoints(topLeft, bottomRight);
final bounds = Rect.fromPoints(topLeft, bottomRight);
Printing.sharePdf(document: pdf, bounds: bounds);
}
Future<void> _printScreen() async {
const margin = 10.0 * PdfPageFormat.mm;
final pdf = new PdfDocument(deflate: zlib.encode);
final page = new PdfPage(pdf, pageFormat: PdfPageFormat.a4);
final pdf = PdfDocument(deflate: zlib.encode);
final page = PdfPage(pdf, pageFormat: PdfPageFormat.a4);
final g = page.getGraphics();
RenderRepaintBoundary boundary =
... ... @@ -94,25 +94,24 @@ class MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return new RepaintBoundary(
return RepaintBoundary(
key: previewContainer,
child: new Scaffold(
appBar: new AppBar(
child: Scaffold(
appBar: AppBar(
title: const Text('Pdf Printing Example'),
),
body: new Center(
child: new Column(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
new RaisedButton(
child: new Text('Print Document'), onPressed: _printPdf),
new RaisedButton(
RaisedButton(
child: Text('Print Document'), onPressed: _printPdf),
RaisedButton(
key: shareWidget,
child: new Text('Share Document'),
child: Text('Share Document'),
onPressed: _sharePdf),
new RaisedButton(
child: new Text('Print Screenshot'),
onPressed: _printScreen),
RaisedButton(
child: Text('Print Screenshot'), onPressed: _printScreen),
],
),
),
... ...
... ... @@ -34,7 +34,7 @@ class Printing {
if (document != null) bytes = document.save();
final Map<String, dynamic> params = <String, dynamic>{
'doc': new Uint8List.fromList(bytes),
'doc': Uint8List.fromList(bytes),
};
await _channel.invokeMethod('printPdf', params);
... ... @@ -48,11 +48,11 @@ class Printing {
if (document != null) bytes = document.save();
if (bounds == null) {
bounds = new Rect.fromCircle(center: Offset.zero, radius: 10.0);
bounds = Rect.fromCircle(center: Offset.zero, radius: 10.0);
}
final Map<String, dynamic> params = <String, dynamic>{
'doc': new Uint8List.fromList(bytes),
'doc': Uint8List.fromList(bytes),
'x': bounds.left,
'y': bounds.top,
'w': bounds.width,
... ...