Showing
2 changed files
with
64 additions
and
0 deletions
@@ -10,6 +10,7 @@ | @@ -10,6 +10,7 @@ | ||
10 | - Add document loading | 10 | - Add document loading |
11 | - Remove deprecated methods | 11 | - Remove deprecated methods |
12 | - Document.save() now returns a Future | 12 | - Document.save() now returns a Future |
13 | +- Add Widget.draw() to paint any widget on a canvas | ||
13 | 14 | ||
14 | ## 1.13.0 | 15 | ## 1.13.0 |
15 | 16 |
@@ -24,6 +24,7 @@ import 'package:vector_math/vector_math_64.dart'; | @@ -24,6 +24,7 @@ import 'package:vector_math/vector_math_64.dart'; | ||
24 | import 'document.dart'; | 24 | import 'document.dart'; |
25 | import 'geometry.dart'; | 25 | import 'geometry.dart'; |
26 | import 'page.dart'; | 26 | import 'page.dart'; |
27 | +import 'theme.dart'; | ||
27 | 28 | ||
28 | @immutable | 29 | @immutable |
29 | class Context { | 30 | class Context { |
@@ -113,6 +114,68 @@ abstract class Widget { | @@ -113,6 +114,68 @@ abstract class Widget { | ||
113 | /// The bounding box of this widget, calculated at layout time | 114 | /// The bounding box of this widget, calculated at layout time |
114 | PdfRect box; | 115 | PdfRect box; |
115 | 116 | ||
117 | + /// Draw a widget to a page canvas. | ||
118 | + static void draw( | ||
119 | + Widget widget, { | ||
120 | + PdfPage page, | ||
121 | + PdfGraphics canvas, | ||
122 | + BoxConstraints constraints, | ||
123 | + @required PdfPoint offset, | ||
124 | + Alignment alignment, | ||
125 | + Context context, | ||
126 | + }) { | ||
127 | + assert(offset != null); | ||
128 | + | ||
129 | + context ??= Context( | ||
130 | + document: page?.pdfDocument, | ||
131 | + page: page, | ||
132 | + canvas: canvas, | ||
133 | + ).inheritFromAll(<Inherited>[ | ||
134 | + ThemeData.base(), | ||
135 | + ]); | ||
136 | + | ||
137 | + widget.layout( | ||
138 | + context, | ||
139 | + constraints ?? const BoxConstraints(), | ||
140 | + ); | ||
141 | + | ||
142 | + assert(widget.box != null); | ||
143 | + | ||
144 | + if (alignment != null) { | ||
145 | + final d = alignment.withinRect(widget.box); | ||
146 | + offset = PdfPoint(offset.x - d.x, offset.y - d.y); | ||
147 | + } | ||
148 | + | ||
149 | + widget.box = PdfRect.fromPoints(offset, widget.box.size); | ||
150 | + | ||
151 | + widget.paint(context); | ||
152 | + } | ||
153 | + | ||
154 | + /// Measure the size of a widget to a page canvas. | ||
155 | + static PdfPoint measure( | ||
156 | + Widget widget, { | ||
157 | + PdfPage page, | ||
158 | + PdfGraphics canvas, | ||
159 | + BoxConstraints constraints, | ||
160 | + Context context, | ||
161 | + }) { | ||
162 | + context ??= Context( | ||
163 | + document: page?.pdfDocument, | ||
164 | + page: page, | ||
165 | + canvas: canvas, | ||
166 | + ).inheritFromAll(<Inherited>[ | ||
167 | + ThemeData.base(), | ||
168 | + ]); | ||
169 | + | ||
170 | + widget.layout( | ||
171 | + context, | ||
172 | + constraints ?? const BoxConstraints(), | ||
173 | + ); | ||
174 | + | ||
175 | + assert(widget.box != null); | ||
176 | + return widget.box.size; | ||
177 | + } | ||
178 | + | ||
116 | /// First widget pass to calculate the children layout and | 179 | /// First widget pass to calculate the children layout and |
117 | /// bounding [box] | 180 | /// bounding [box] |
118 | void layout(Context context, BoxConstraints constraints, | 181 | void layout(Context context, BoxConstraints constraints, |
-
Please register or login to post a comment