David PHAM-VAN

Add Roll Paper support

1 # Changelog 1 # Changelog
2 2
  3 +## 1.3.27
  4 +
  5 +- Add Roll Paper support
  6 +
3 ## 1.3.26 7 ## 1.3.26
4 8
5 - Update Analysis options 9 - Update Analysis options
@@ -32,7 +32,7 @@ class PdfPage extends PdfObject { @@ -32,7 +32,7 @@ class PdfPage extends PdfObject {
32 } 32 }
33 33
34 /// This is this page format, ie the size of the page, margins, and rotation 34 /// This is this page format, ie the size of the page, margins, and rotation
35 - final PdfPageFormat pageFormat; 35 + PdfPageFormat pageFormat;
36 36
37 /// This holds the contents of the page. 37 /// This holds the contents of the page.
38 List<PdfObjectStream> contents = <PdfObjectStream>[]; 38 List<PdfObjectStream> contents = <PdfObjectStream>[];
@@ -23,7 +23,9 @@ class PdfPageFormat { @@ -23,7 +23,9 @@ class PdfPageFormat {
23 double marginLeft = 0.0, 23 double marginLeft = 0.0,
24 double marginRight = 0.0, 24 double marginRight = 0.0,
25 double marginAll}) 25 double marginAll})
26 - : marginTop = marginAll ?? marginTop, 26 + : assert(width > 0),
  27 + assert(height > 0),
  28 + marginTop = marginAll ?? marginTop,
27 marginBottom = marginAll ?? marginBottom, 29 marginBottom = marginAll ?? marginBottom,
28 marginLeft = marginAll ?? marginLeft, 30 marginLeft = marginAll ?? marginLeft,
29 marginRight = marginAll ?? marginRight; 31 marginRight = marginAll ?? marginRight;
@@ -39,6 +41,14 @@ class PdfPageFormat { @@ -39,6 +41,14 @@ class PdfPageFormat {
39 static const PdfPageFormat legal = 41 static const PdfPageFormat legal =
40 PdfPageFormat(8.5 * inch, 14.0 * inch, marginAll: inch); 42 PdfPageFormat(8.5 * inch, 14.0 * inch, marginAll: inch);
41 43
  44 + static const PdfPageFormat roll57 =
  45 + PdfPageFormat(57 * mm, double.infinity, marginAll: 5 * mm);
  46 + static const PdfPageFormat roll80 =
  47 + PdfPageFormat(80 * mm, double.infinity, marginAll: 5 * mm);
  48 +
  49 + static const PdfPageFormat undefined =
  50 + PdfPageFormat(double.infinity, double.infinity);
  51 +
42 static const PdfPageFormat standard = a4; 52 static const PdfPageFormat standard = a4;
43 53
44 static const double point = 1.0; 54 static const double point = 1.0;
@@ -50,11 +50,15 @@ class PdfStream { @@ -50,11 +50,15 @@ class PdfStream {
50 } 50 }
51 51
52 void putNum(double d) { 52 void putNum(double d) {
  53 + assert(d != double.infinity);
53 putString(d.toStringAsFixed(precision)); 54 putString(d.toStringAsFixed(precision));
54 } 55 }
55 56
56 void putNumList(List<double> d) { 57 void putNumList(List<double> d) {
57 - putString(d.map((double v) => v.toStringAsFixed(precision)).join(' ')); 58 + putString(d.map((double v) {
  59 + assert(v != double.infinity);
  60 + return v.toStringAsFixed(precision);
  61 + }).join(' '));
58 } 62 }
59 63
60 void putIntList(List<int> d) { 64 void putIntList(List<int> d) {
@@ -679,6 +679,9 @@ class FullPage extends SingleChildWidget { @@ -679,6 +679,9 @@ class FullPage extends SingleChildWidget {
679 final bool ignoreMargins; 679 final bool ignoreMargins;
680 680
681 BoxConstraints _getConstraints(Context context) { 681 BoxConstraints _getConstraints(Context context) {
  682 + assert(context.page.pageFormat.width != double.infinity);
  683 + assert(context.page.pageFormat.height != double.infinity);
  684 +
682 return ignoreMargins 685 return ignoreMargins
683 ? BoxConstraints.tightFor( 686 ? BoxConstraints.tightFor(
684 width: context.page.pageFormat.width, 687 width: context.page.pageFormat.width,
@@ -111,6 +111,9 @@ class MultiPage extends Page { @@ -111,6 +111,9 @@ class MultiPage extends Page {
111 return; 111 return;
112 } 112 }
113 113
  114 + assert(pageFormat.width > 0 && pageFormat.width < double.infinity);
  115 + assert(pageFormat.height > 0 && pageFormat.height < double.infinity);
  116 +
114 final EdgeInsets _margin = margin; 117 final EdgeInsets _margin = margin;
115 final bool _mustRotate = mustRotate; 118 final bool _mustRotate = mustRotate;
116 final double pageHeight = 119 final double pageHeight =
@@ -52,7 +52,7 @@ class Page { @@ -52,7 +52,7 @@ class Page {
52 52
53 final PageTheme pageTheme; 53 final PageTheme pageTheme;
54 54
55 - PdfPageFormat get pageFormat => pageTheme.pageFormat; 55 + PdfPageFormat get pageFormat => _pdfPage?.pageFormat ?? pageTheme.pageFormat;
56 56
57 PageOrientation get orientation => pageTheme.orientation; 57 PageOrientation get orientation => pageTheme.orientation;
58 58
@@ -92,7 +92,7 @@ class Page { @@ -92,7 +92,7 @@ class Page {
92 void postProcess(Document document) { 92 void postProcess(Document document) {
93 final PdfGraphics canvas = _pdfPage.getGraphics(); 93 final PdfGraphics canvas = _pdfPage.getGraphics();
94 final EdgeInsets _margin = margin; 94 final EdgeInsets _margin = margin;
95 - final BoxConstraints constraints = mustRotate 95 + BoxConstraints constraints = mustRotate
96 ? BoxConstraints( 96 ? BoxConstraints(
97 maxWidth: pageFormat.height - _margin.vertical, 97 maxWidth: pageFormat.height - _margin.vertical,
98 maxHeight: pageFormat.width - _margin.horizontal) 98 maxHeight: pageFormat.width - _margin.horizontal)
@@ -107,6 +107,43 @@ class Page { @@ -107,6 +107,43 @@ class Page {
107 canvas: canvas, 107 canvas: canvas,
108 ).inheritFrom(calculatedTheme); 108 ).inheritFrom(calculatedTheme);
109 109
  110 + Widget background;
  111 + Widget content;
  112 + Widget foreground;
  113 +
  114 + if (_build != null) {
  115 + content = _build(context);
  116 + if (content != null) {
  117 + final PdfPoint size = layout(content, context, constraints);
  118 +
  119 + if (_pdfPage.pageFormat.height == double.infinity) {
  120 + _pdfPage.pageFormat =
  121 + _pdfPage.pageFormat.copyWith(width: size.x, height: size.y);
  122 + constraints = mustRotate
  123 + ? BoxConstraints(
  124 + maxWidth: _pdfPage.pageFormat.height - _margin.vertical,
  125 + maxHeight: _pdfPage.pageFormat.width - _margin.horizontal)
  126 + : BoxConstraints(
  127 + maxWidth: _pdfPage.pageFormat.width - _margin.horizontal,
  128 + maxHeight: _pdfPage.pageFormat.height - _margin.vertical);
  129 + }
  130 + }
  131 + }
  132 +
  133 + if (pageTheme.buildBackground != null) {
  134 + background = pageTheme.buildBackground(context);
  135 + if (background != null) {
  136 + layout(background, context, constraints);
  137 + }
  138 + }
  139 +
  140 + if (pageTheme.buildForeground != null) {
  141 + foreground = pageTheme.buildForeground(context);
  142 + if (foreground != null) {
  143 + layout(foreground, context, constraints);
  144 + }
  145 + }
  146 +
110 assert(() { 147 assert(() {
111 if (Document.debug) { 148 if (Document.debug) {
112 debugPaint(context); 149 debugPaint(context);
@@ -114,44 +151,42 @@ class Page { @@ -114,44 +151,42 @@ class Page {
114 return true; 151 return true;
115 }()); 152 }());
116 153
117 - if (pageTheme.buildBackground != null) {  
118 - final Widget child = pageTheme.buildBackground(context);  
119 - if (child != null) {  
120 - layout(child, context, constraints);  
121 - paint(child, context);  
122 - } 154 + if (background != null) {
  155 + paint(background, context);
123 } 156 }
124 157
125 - if (_build != null) {  
126 - final Widget child = _build(context);  
127 - if (child != null) {  
128 - layout(child, context, constraints);  
129 - paint(child, context);  
130 - } 158 + if (content != null) {
  159 + paint(content, context);
131 } 160 }
132 161
133 - if (pageTheme.buildForeground != null) {  
134 - final Widget child = pageTheme.buildForeground(context);  
135 - if (child != null) {  
136 - layout(child, context, constraints);  
137 - paint(child, context);  
138 - } 162 + if (foreground != null) {
  163 + paint(foreground, context);
139 } 164 }
140 } 165 }
141 166
142 @protected 167 @protected
143 - void layout(Widget child, Context context, BoxConstraints constraints, 168 + PdfPoint layout(Widget child, Context context, BoxConstraints constraints,
144 {bool parentUsesSize = false}) { 169 {bool parentUsesSize = false}) {
145 - if (child != null) {  
146 - final EdgeInsets _margin = margin;  
147 - child.layout(context, constraints, parentUsesSize: parentUsesSize);  
148 - assert(child.box != null);  
149 - child.box = PdfRect(  
150 - _margin.left,  
151 - pageFormat.height - child.box.height - _margin.top,  
152 - child.box.width,  
153 - child.box.height); 170 + if (child == null) {
  171 + return PdfPoint(pageFormat.width, pageFormat.height);
154 } 172 }
  173 +
  174 + final EdgeInsets _margin = margin;
  175 + child.layout(context, constraints, parentUsesSize: parentUsesSize);
  176 + assert(child.box != null);
  177 +
  178 + final double width = pageFormat.width == double.infinity
  179 + ? child.box.width + _margin.left + _margin.right
  180 + : pageFormat.width;
  181 +
  182 + final double height = pageFormat.height == double.infinity
  183 + ? child.box.height + _margin.top + _margin.bottom
  184 + : pageFormat.height;
  185 +
  186 + child.box = PdfRect(_margin.left, height - child.box.height - _margin.top,
  187 + child.box.width, child.box.height);
  188 +
  189 + return PdfPoint(width, height);
155 } 190 }
156 191
157 @protected 192 @protected
@@ -4,7 +4,7 @@ description: A pdf producer for Dart. It can create pdf files for both web or fl @@ -4,7 +4,7 @@ description: A pdf producer for Dart. It can create pdf files for both web or fl
4 homepage: https://github.com/DavBfr/dart_pdf/tree/master/pdf 4 homepage: https://github.com/DavBfr/dart_pdf/tree/master/pdf
5 repository: https://github.com/DavBfr/dart_pdf 5 repository: https://github.com/DavBfr/dart_pdf
6 issue_tracker: https://github.com/DavBfr/dart_pdf/issues 6 issue_tracker: https://github.com/DavBfr/dart_pdf/issues
7 -version: 1.3.26 7 +version: 1.3.27
8 8
9 environment: 9 environment:
10 sdk: ">=2.3.0 <3.0.0" 10 sdk: ">=2.3.0 <3.0.0"
@@ -25,6 +25,7 @@ import 'jpeg_test.dart' as jpeg; @@ -25,6 +25,7 @@ import 'jpeg_test.dart' as jpeg;
25 import 'metrics_test.dart' as metrics; 25 import 'metrics_test.dart' as metrics;
26 import 'minimal_test.dart' as minimal; 26 import 'minimal_test.dart' as minimal;
27 import 'orientation_test.dart' as orientation; 27 import 'orientation_test.dart' as orientation;
  28 +import 'roll_paper_test.dart' as roll;
28 import 'ttf_test.dart' as ttf; 29 import 'ttf_test.dart' as ttf;
29 import 'type1_test.dart' as type1; 30 import 'type1_test.dart' as type1;
30 import 'widget_basic_test.dart' as widget_basic; 31 import 'widget_basic_test.dart' as widget_basic;
@@ -49,6 +50,7 @@ void main() { @@ -49,6 +50,7 @@ void main() {
49 metrics.main(); 50 metrics.main();
50 minimal.main(); 51 minimal.main();
51 orientation.main(); 52 orientation.main();
  53 + roll.main();
52 ttf.main(); 54 ttf.main();
53 type1.main(); 55 type1.main();
54 widget_basic.main(); 56 widget_basic.main();
  1 +/*
  2 + * Copyright (C) 2017, David PHAM-VAN <dev.nfet.net@gmail.com>
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +
  17 +// ignore_for_file: omit_local_variable_types
  18 +
  19 +import 'dart:io';
  20 +
  21 +import 'package:pdf/pdf.dart';
  22 +import 'package:pdf/widgets.dart';
  23 +import 'package:test/test.dart';
  24 +
  25 +Document pdf;
  26 +
  27 +void main() {
  28 + setUpAll(() {
  29 + Document.debug = true;
  30 + pdf = Document();
  31 + });
  32 +
  33 + test('Pdf Roll Paper', () async {
  34 + pdf.addPage(Page(
  35 + pageFormat: PdfPageFormat.roll80,
  36 + build: (Context context) => Padding(
  37 + padding: const EdgeInsets.all(30),
  38 + child: Center(
  39 + child: Text('Hello World!'),
  40 + ),
  41 + ),
  42 + ));
  43 + });
  44 +
  45 + test('Pdf Automatic Paper', () async {
  46 + pdf.addPage(Page(
  47 + pageFormat: PdfPageFormat.undefined,
  48 + build: (Context context) => Text('Hello World!')));
  49 + });
  50 +
  51 + tearDownAll(() {
  52 + final File file = File('roll-paper.pdf');
  53 + file.writeAsBytesSync(pdf.save());
  54 + });
  55 +}
No preview for this file type