Showing
3 changed files
with
164 additions
and
146 deletions
| @@ -36,6 +36,7 @@ part 'widgets/geometry.dart'; | @@ -36,6 +36,7 @@ part 'widgets/geometry.dart'; | ||
| 36 | part 'widgets/grid_view.dart'; | 36 | part 'widgets/grid_view.dart'; |
| 37 | part 'widgets/image.dart'; | 37 | part 'widgets/image.dart'; |
| 38 | part 'widgets/multi_page.dart'; | 38 | part 'widgets/multi_page.dart'; |
| 39 | +part 'widgets/page.dart'; | ||
| 39 | part 'widgets/placeholders.dart'; | 40 | part 'widgets/placeholders.dart'; |
| 40 | part 'widgets/progress.dart'; | 41 | part 'widgets/progress.dart'; |
| 41 | part 'widgets/stack.dart'; | 42 | part 'widgets/stack.dart'; |
| @@ -69,149 +69,3 @@ class Document { | @@ -69,149 +69,3 @@ class Document { | ||
| 69 | return document.save(); | 69 | return document.save(); |
| 70 | } | 70 | } |
| 71 | } | 71 | } |
| 72 | - | ||
| 73 | -typedef BuildCallback = Widget Function(Context context); | ||
| 74 | -typedef BuildListCallback = List<Widget> Function(Context context); | ||
| 75 | - | ||
| 76 | -enum PageOrientation { natural, landscape, portrait } | ||
| 77 | - | ||
| 78 | -class Page { | ||
| 79 | - Page( | ||
| 80 | - {this.pageFormat = PdfPageFormat.standard, | ||
| 81 | - BuildCallback build, | ||
| 82 | - this.theme, | ||
| 83 | - this.orientation = PageOrientation.natural, | ||
| 84 | - EdgeInsets margin}) | ||
| 85 | - : assert(pageFormat != null), | ||
| 86 | - _margin = margin, | ||
| 87 | - _build = build; | ||
| 88 | - | ||
| 89 | - final PdfPageFormat pageFormat; | ||
| 90 | - | ||
| 91 | - final PageOrientation orientation; | ||
| 92 | - | ||
| 93 | - final EdgeInsets _margin; | ||
| 94 | - | ||
| 95 | - final BuildCallback _build; | ||
| 96 | - | ||
| 97 | - final Theme theme; | ||
| 98 | - | ||
| 99 | - bool get mustRotate => | ||
| 100 | - (orientation == PageOrientation.landscape && | ||
| 101 | - pageFormat.height > pageFormat.width) || | ||
| 102 | - (orientation == PageOrientation.portrait && | ||
| 103 | - pageFormat.width > pageFormat.height); | ||
| 104 | - | ||
| 105 | - PdfPage _pdfPage; | ||
| 106 | - | ||
| 107 | - EdgeInsets get margin { | ||
| 108 | - if (_margin != null) { | ||
| 109 | - if (mustRotate) { | ||
| 110 | - return EdgeInsets.fromLTRB( | ||
| 111 | - _margin.bottom, _margin.left, _margin.top, _margin.right); | ||
| 112 | - } else { | ||
| 113 | - return _margin; | ||
| 114 | - } | ||
| 115 | - } | ||
| 116 | - | ||
| 117 | - if (mustRotate) { | ||
| 118 | - return EdgeInsets.fromLTRB(pageFormat.marginBottom, pageFormat.marginLeft, | ||
| 119 | - pageFormat.marginTop, pageFormat.marginRight); | ||
| 120 | - } else { | ||
| 121 | - return EdgeInsets.fromLTRB(pageFormat.marginLeft, pageFormat.marginTop, | ||
| 122 | - pageFormat.marginRight, pageFormat.marginBottom); | ||
| 123 | - } | ||
| 124 | - } | ||
| 125 | - | ||
| 126 | - @protected | ||
| 127 | - void debugPaint(Context context) { | ||
| 128 | - final EdgeInsets _margin = margin; | ||
| 129 | - context.canvas | ||
| 130 | - ..setFillColor(PdfColors.lightGreen) | ||
| 131 | - ..moveTo(0, 0) | ||
| 132 | - ..lineTo(pageFormat.width, 0) | ||
| 133 | - ..lineTo(pageFormat.width, pageFormat.height) | ||
| 134 | - ..lineTo(0, pageFormat.height) | ||
| 135 | - ..moveTo(_margin.left, _margin.bottom) | ||
| 136 | - ..lineTo(_margin.left, pageFormat.height - _margin.top) | ||
| 137 | - ..lineTo( | ||
| 138 | - pageFormat.width - _margin.right, pageFormat.height - _margin.top) | ||
| 139 | - ..lineTo(pageFormat.width - _margin.right, _margin.bottom) | ||
| 140 | - ..fillPath(); | ||
| 141 | - } | ||
| 142 | - | ||
| 143 | - @protected | ||
| 144 | - void generate(Document document) { | ||
| 145 | - _pdfPage = PdfPage(document.document, pageFormat: pageFormat); | ||
| 146 | - } | ||
| 147 | - | ||
| 148 | - @protected | ||
| 149 | - void postProcess(Document document) { | ||
| 150 | - final PdfGraphics canvas = _pdfPage.getGraphics(); | ||
| 151 | - final EdgeInsets _margin = margin; | ||
| 152 | - final BoxConstraints constraints = mustRotate | ||
| 153 | - ? BoxConstraints( | ||
| 154 | - maxWidth: pageFormat.height - _margin.vertical, | ||
| 155 | - maxHeight: pageFormat.width - _margin.horizontal) | ||
| 156 | - : BoxConstraints( | ||
| 157 | - maxWidth: pageFormat.width - _margin.horizontal, | ||
| 158 | - maxHeight: pageFormat.height - _margin.vertical); | ||
| 159 | - | ||
| 160 | - final Theme calculatedTheme = theme ?? document.theme ?? Theme.base(); | ||
| 161 | - final Context context = Context( | ||
| 162 | - document: document.document, | ||
| 163 | - page: _pdfPage, | ||
| 164 | - canvas: canvas, | ||
| 165 | - ).inheritFrom(calculatedTheme); | ||
| 166 | - if (_build != null) { | ||
| 167 | - final Widget child = _build(context); | ||
| 168 | - layout(child, context, constraints); | ||
| 169 | - paint(child, context); | ||
| 170 | - } | ||
| 171 | - } | ||
| 172 | - | ||
| 173 | - @protected | ||
| 174 | - void layout(Widget child, Context context, BoxConstraints constraints, | ||
| 175 | - {bool parentUsesSize = false}) { | ||
| 176 | - if (child != null) { | ||
| 177 | - final EdgeInsets _margin = margin; | ||
| 178 | - child.layout(context, constraints, parentUsesSize: parentUsesSize); | ||
| 179 | - assert(child.box != null); | ||
| 180 | - child.box = PdfRect( | ||
| 181 | - _margin.left, | ||
| 182 | - pageFormat.height - child.box.height - _margin.top, | ||
| 183 | - child.box.width, | ||
| 184 | - child.box.height); | ||
| 185 | - } | ||
| 186 | - } | ||
| 187 | - | ||
| 188 | - @protected | ||
| 189 | - void paint(Widget child, Context context) { | ||
| 190 | - assert(() { | ||
| 191 | - if (Document.debug) { | ||
| 192 | - debugPaint(context); | ||
| 193 | - } | ||
| 194 | - return true; | ||
| 195 | - }()); | ||
| 196 | - | ||
| 197 | - if (child == null) { | ||
| 198 | - return; | ||
| 199 | - } | ||
| 200 | - | ||
| 201 | - if (mustRotate) { | ||
| 202 | - final EdgeInsets _margin = margin; | ||
| 203 | - final Matrix4 mat = Matrix4.identity(); | ||
| 204 | - mat | ||
| 205 | - ..rotateZ(-math.pi / 2) | ||
| 206 | - ..translate(-pageFormat.height - _margin.left + _margin.top, | ||
| 207 | - child.box.height - child.box.width + _margin.left - _margin.bottom); | ||
| 208 | - context.canvas | ||
| 209 | - ..saveContext() | ||
| 210 | - ..setTransform(mat); | ||
| 211 | - child.paint(context); | ||
| 212 | - context.canvas.restoreContext(); | ||
| 213 | - } else { | ||
| 214 | - child.paint(context); | ||
| 215 | - } | ||
| 216 | - } | ||
| 217 | -} |
pdf/lib/widgets/page.dart
0 → 100644
| 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 | +part of widget; | ||
| 18 | + | ||
| 19 | +typedef BuildCallback = Widget Function(Context context); | ||
| 20 | +typedef BuildListCallback = List<Widget> Function(Context context); | ||
| 21 | + | ||
| 22 | +enum PageOrientation { natural, landscape, portrait } | ||
| 23 | + | ||
| 24 | +class Page { | ||
| 25 | + Page( | ||
| 26 | + {this.pageFormat = PdfPageFormat.standard, | ||
| 27 | + BuildCallback build, | ||
| 28 | + this.theme, | ||
| 29 | + this.orientation = PageOrientation.natural, | ||
| 30 | + EdgeInsets margin}) | ||
| 31 | + : assert(pageFormat != null), | ||
| 32 | + _margin = margin, | ||
| 33 | + _build = build; | ||
| 34 | + | ||
| 35 | + final PdfPageFormat pageFormat; | ||
| 36 | + | ||
| 37 | + final PageOrientation orientation; | ||
| 38 | + | ||
| 39 | + final EdgeInsets _margin; | ||
| 40 | + | ||
| 41 | + final BuildCallback _build; | ||
| 42 | + | ||
| 43 | + final Theme theme; | ||
| 44 | + | ||
| 45 | + bool get mustRotate => | ||
| 46 | + (orientation == PageOrientation.landscape && | ||
| 47 | + pageFormat.height > pageFormat.width) || | ||
| 48 | + (orientation == PageOrientation.portrait && | ||
| 49 | + pageFormat.width > pageFormat.height); | ||
| 50 | + | ||
| 51 | + PdfPage _pdfPage; | ||
| 52 | + | ||
| 53 | + EdgeInsets get margin { | ||
| 54 | + if (_margin != null) { | ||
| 55 | + if (mustRotate) { | ||
| 56 | + return EdgeInsets.fromLTRB( | ||
| 57 | + _margin.bottom, _margin.left, _margin.top, _margin.right); | ||
| 58 | + } else { | ||
| 59 | + return _margin; | ||
| 60 | + } | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + if (mustRotate) { | ||
| 64 | + return EdgeInsets.fromLTRB(pageFormat.marginBottom, pageFormat.marginLeft, | ||
| 65 | + pageFormat.marginTop, pageFormat.marginRight); | ||
| 66 | + } else { | ||
| 67 | + return EdgeInsets.fromLTRB(pageFormat.marginLeft, pageFormat.marginTop, | ||
| 68 | + pageFormat.marginRight, pageFormat.marginBottom); | ||
| 69 | + } | ||
| 70 | + } | ||
| 71 | + | ||
| 72 | + @protected | ||
| 73 | + void debugPaint(Context context) { | ||
| 74 | + final EdgeInsets _margin = margin; | ||
| 75 | + context.canvas | ||
| 76 | + ..setFillColor(PdfColors.lightGreen) | ||
| 77 | + ..moveTo(0, 0) | ||
| 78 | + ..lineTo(pageFormat.width, 0) | ||
| 79 | + ..lineTo(pageFormat.width, pageFormat.height) | ||
| 80 | + ..lineTo(0, pageFormat.height) | ||
| 81 | + ..moveTo(_margin.left, _margin.bottom) | ||
| 82 | + ..lineTo(_margin.left, pageFormat.height - _margin.top) | ||
| 83 | + ..lineTo( | ||
| 84 | + pageFormat.width - _margin.right, pageFormat.height - _margin.top) | ||
| 85 | + ..lineTo(pageFormat.width - _margin.right, _margin.bottom) | ||
| 86 | + ..fillPath(); | ||
| 87 | + } | ||
| 88 | + | ||
| 89 | + @protected | ||
| 90 | + void generate(Document document) { | ||
| 91 | + _pdfPage = PdfPage(document.document, pageFormat: pageFormat); | ||
| 92 | + } | ||
| 93 | + | ||
| 94 | + @protected | ||
| 95 | + void postProcess(Document document) { | ||
| 96 | + final PdfGraphics canvas = _pdfPage.getGraphics(); | ||
| 97 | + final EdgeInsets _margin = margin; | ||
| 98 | + final BoxConstraints constraints = mustRotate | ||
| 99 | + ? BoxConstraints( | ||
| 100 | + maxWidth: pageFormat.height - _margin.vertical, | ||
| 101 | + maxHeight: pageFormat.width - _margin.horizontal) | ||
| 102 | + : BoxConstraints( | ||
| 103 | + maxWidth: pageFormat.width - _margin.horizontal, | ||
| 104 | + maxHeight: pageFormat.height - _margin.vertical); | ||
| 105 | + | ||
| 106 | + final Theme calculatedTheme = theme ?? document.theme ?? Theme.base(); | ||
| 107 | + final Context context = Context( | ||
| 108 | + document: document.document, | ||
| 109 | + page: _pdfPage, | ||
| 110 | + canvas: canvas, | ||
| 111 | + ).inheritFrom(calculatedTheme); | ||
| 112 | + if (_build != null) { | ||
| 113 | + final Widget child = _build(context); | ||
| 114 | + layout(child, context, constraints); | ||
| 115 | + paint(child, context); | ||
| 116 | + } | ||
| 117 | + } | ||
| 118 | + | ||
| 119 | + @protected | ||
| 120 | + void layout(Widget child, Context context, BoxConstraints constraints, | ||
| 121 | + {bool parentUsesSize = false}) { | ||
| 122 | + if (child != null) { | ||
| 123 | + final EdgeInsets _margin = margin; | ||
| 124 | + child.layout(context, constraints, parentUsesSize: parentUsesSize); | ||
| 125 | + assert(child.box != null); | ||
| 126 | + child.box = PdfRect( | ||
| 127 | + _margin.left, | ||
| 128 | + pageFormat.height - child.box.height - _margin.top, | ||
| 129 | + child.box.width, | ||
| 130 | + child.box.height); | ||
| 131 | + } | ||
| 132 | + } | ||
| 133 | + | ||
| 134 | + @protected | ||
| 135 | + void paint(Widget child, Context context) { | ||
| 136 | + assert(() { | ||
| 137 | + if (Document.debug) { | ||
| 138 | + debugPaint(context); | ||
| 139 | + } | ||
| 140 | + return true; | ||
| 141 | + }()); | ||
| 142 | + | ||
| 143 | + if (child == null) { | ||
| 144 | + return; | ||
| 145 | + } | ||
| 146 | + | ||
| 147 | + if (mustRotate) { | ||
| 148 | + final EdgeInsets _margin = margin; | ||
| 149 | + final Matrix4 mat = Matrix4.identity(); | ||
| 150 | + mat | ||
| 151 | + ..rotateZ(-math.pi / 2) | ||
| 152 | + ..translate(-pageFormat.height - _margin.left + _margin.top, | ||
| 153 | + child.box.height - child.box.width + _margin.left - _margin.bottom); | ||
| 154 | + context.canvas | ||
| 155 | + ..saveContext() | ||
| 156 | + ..setTransform(mat); | ||
| 157 | + child.paint(context); | ||
| 158 | + context.canvas.restoreContext(); | ||
| 159 | + } else { | ||
| 160 | + child.paint(context); | ||
| 161 | + } | ||
| 162 | + } | ||
| 163 | +} |
-
Please register or login to post a comment