David PHAM-VAN

Add Roll Paper support

# Changelog
## 1.3.27
- Add Roll Paper support
## 1.3.26
- Update Analysis options
... ...
... ... @@ -32,7 +32,7 @@ class PdfPage extends PdfObject {
}
/// This is this page format, ie the size of the page, margins, and rotation
final PdfPageFormat pageFormat;
PdfPageFormat pageFormat;
/// This holds the contents of the page.
List<PdfObjectStream> contents = <PdfObjectStream>[];
... ...
... ... @@ -23,7 +23,9 @@ class PdfPageFormat {
double marginLeft = 0.0,
double marginRight = 0.0,
double marginAll})
: marginTop = marginAll ?? marginTop,
: assert(width > 0),
assert(height > 0),
marginTop = marginAll ?? marginTop,
marginBottom = marginAll ?? marginBottom,
marginLeft = marginAll ?? marginLeft,
marginRight = marginAll ?? marginRight;
... ... @@ -39,6 +41,14 @@ class PdfPageFormat {
static const PdfPageFormat legal =
PdfPageFormat(8.5 * inch, 14.0 * inch, marginAll: inch);
static const PdfPageFormat roll57 =
PdfPageFormat(57 * mm, double.infinity, marginAll: 5 * mm);
static const PdfPageFormat roll80 =
PdfPageFormat(80 * mm, double.infinity, marginAll: 5 * mm);
static const PdfPageFormat undefined =
PdfPageFormat(double.infinity, double.infinity);
static const PdfPageFormat standard = a4;
static const double point = 1.0;
... ...
... ... @@ -50,11 +50,15 @@ class PdfStream {
}
void putNum(double d) {
assert(d != double.infinity);
putString(d.toStringAsFixed(precision));
}
void putNumList(List<double> d) {
putString(d.map((double v) => v.toStringAsFixed(precision)).join(' '));
putString(d.map((double v) {
assert(v != double.infinity);
return v.toStringAsFixed(precision);
}).join(' '));
}
void putIntList(List<int> d) {
... ...
... ... @@ -679,6 +679,9 @@ class FullPage extends SingleChildWidget {
final bool ignoreMargins;
BoxConstraints _getConstraints(Context context) {
assert(context.page.pageFormat.width != double.infinity);
assert(context.page.pageFormat.height != double.infinity);
return ignoreMargins
? BoxConstraints.tightFor(
width: context.page.pageFormat.width,
... ...
... ... @@ -111,6 +111,9 @@ class MultiPage extends Page {
return;
}
assert(pageFormat.width > 0 && pageFormat.width < double.infinity);
assert(pageFormat.height > 0 && pageFormat.height < double.infinity);
final EdgeInsets _margin = margin;
final bool _mustRotate = mustRotate;
final double pageHeight =
... ...
... ... @@ -52,7 +52,7 @@ class Page {
final PageTheme pageTheme;
PdfPageFormat get pageFormat => pageTheme.pageFormat;
PdfPageFormat get pageFormat => _pdfPage?.pageFormat ?? pageTheme.pageFormat;
PageOrientation get orientation => pageTheme.orientation;
... ... @@ -92,7 +92,7 @@ class Page {
void postProcess(Document document) {
final PdfGraphics canvas = _pdfPage.getGraphics();
final EdgeInsets _margin = margin;
final BoxConstraints constraints = mustRotate
BoxConstraints constraints = mustRotate
? BoxConstraints(
maxWidth: pageFormat.height - _margin.vertical,
maxHeight: pageFormat.width - _margin.horizontal)
... ... @@ -107,6 +107,43 @@ class Page {
canvas: canvas,
).inheritFrom(calculatedTheme);
Widget background;
Widget content;
Widget foreground;
if (_build != null) {
content = _build(context);
if (content != null) {
final PdfPoint size = layout(content, context, constraints);
if (_pdfPage.pageFormat.height == double.infinity) {
_pdfPage.pageFormat =
_pdfPage.pageFormat.copyWith(width: size.x, height: size.y);
constraints = mustRotate
? BoxConstraints(
maxWidth: _pdfPage.pageFormat.height - _margin.vertical,
maxHeight: _pdfPage.pageFormat.width - _margin.horizontal)
: BoxConstraints(
maxWidth: _pdfPage.pageFormat.width - _margin.horizontal,
maxHeight: _pdfPage.pageFormat.height - _margin.vertical);
}
}
}
if (pageTheme.buildBackground != null) {
background = pageTheme.buildBackground(context);
if (background != null) {
layout(background, context, constraints);
}
}
if (pageTheme.buildForeground != null) {
foreground = pageTheme.buildForeground(context);
if (foreground != null) {
layout(foreground, context, constraints);
}
}
assert(() {
if (Document.debug) {
debugPaint(context);
... ... @@ -114,44 +151,42 @@ class Page {
return true;
}());
if (pageTheme.buildBackground != null) {
final Widget child = pageTheme.buildBackground(context);
if (child != null) {
layout(child, context, constraints);
paint(child, context);
}
if (background != null) {
paint(background, context);
}
if (_build != null) {
final Widget child = _build(context);
if (child != null) {
layout(child, context, constraints);
paint(child, context);
}
if (content != null) {
paint(content, context);
}
if (pageTheme.buildForeground != null) {
final Widget child = pageTheme.buildForeground(context);
if (child != null) {
layout(child, context, constraints);
paint(child, context);
}
if (foreground != null) {
paint(foreground, context);
}
}
@protected
void layout(Widget child, Context context, BoxConstraints constraints,
PdfPoint layout(Widget child, Context context, BoxConstraints constraints,
{bool parentUsesSize = false}) {
if (child != null) {
final EdgeInsets _margin = margin;
child.layout(context, constraints, parentUsesSize: parentUsesSize);
assert(child.box != null);
child.box = PdfRect(
_margin.left,
pageFormat.height - child.box.height - _margin.top,
child.box.width,
child.box.height);
if (child == null) {
return PdfPoint(pageFormat.width, pageFormat.height);
}
final EdgeInsets _margin = margin;
child.layout(context, constraints, parentUsesSize: parentUsesSize);
assert(child.box != null);
final double width = pageFormat.width == double.infinity
? child.box.width + _margin.left + _margin.right
: pageFormat.width;
final double height = pageFormat.height == double.infinity
? child.box.height + _margin.top + _margin.bottom
: pageFormat.height;
child.box = PdfRect(_margin.left, height - child.box.height - _margin.top,
child.box.width, child.box.height);
return PdfPoint(width, height);
}
@protected
... ...
... ... @@ -4,7 +4,7 @@ description: A pdf producer for Dart. It can create pdf files for both web or fl
homepage: https://github.com/DavBfr/dart_pdf/tree/master/pdf
repository: https://github.com/DavBfr/dart_pdf
issue_tracker: https://github.com/DavBfr/dart_pdf/issues
version: 1.3.26
version: 1.3.27
environment:
sdk: ">=2.3.0 <3.0.0"
... ...
... ... @@ -25,6 +25,7 @@ import 'jpeg_test.dart' as jpeg;
import 'metrics_test.dart' as metrics;
import 'minimal_test.dart' as minimal;
import 'orientation_test.dart' as orientation;
import 'roll_paper_test.dart' as roll;
import 'ttf_test.dart' as ttf;
import 'type1_test.dart' as type1;
import 'widget_basic_test.dart' as widget_basic;
... ... @@ -49,6 +50,7 @@ void main() {
metrics.main();
minimal.main();
orientation.main();
roll.main();
ttf.main();
type1.main();
widget_basic.main();
... ...
/*
* Copyright (C) 2017, David PHAM-VAN <dev.nfet.net@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// ignore_for_file: omit_local_variable_types
import 'dart:io';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart';
import 'package:test/test.dart';
Document pdf;
void main() {
setUpAll(() {
Document.debug = true;
pdf = Document();
});
test('Pdf Roll Paper', () async {
pdf.addPage(Page(
pageFormat: PdfPageFormat.roll80,
build: (Context context) => Padding(
padding: const EdgeInsets.all(30),
child: Center(
child: Text('Hello World!'),
),
),
));
});
test('Pdf Automatic Paper', () async {
pdf.addPage(Page(
pageFormat: PdfPageFormat.undefined,
build: (Context context) => Text('Hello World!')));
});
tearDownAll(() {
final File file = File('roll-paper.pdf');
file.writeAsBytesSync(pdf.save());
});
}
... ...
No preview for this file type