Showing
6 changed files
with
158 additions
and
16 deletions
| @@ -42,7 +42,7 @@ class BoxBorder { | @@ -42,7 +42,7 @@ class BoxBorder { | ||
| 42 | /// The width of the | 42 | /// The width of the |
| 43 | final double width; | 43 | final double width; |
| 44 | 44 | ||
| 45 | - void paintBorders(Context context, PdfRect box) { | 45 | + void paintRect(Context context, PdfRect box) { |
| 46 | assert(box.x != null); | 46 | assert(box.x != null); |
| 47 | assert(box.y != null); | 47 | assert(box.y != null); |
| 48 | assert(box.width != null); | 48 | assert(box.width != null); |
| @@ -85,6 +85,34 @@ class BoxBorder { | @@ -85,6 +85,34 @@ class BoxBorder { | ||
| 85 | context.canvas.strokePath(); | 85 | context.canvas.strokePath(); |
| 86 | } | 86 | } |
| 87 | } | 87 | } |
| 88 | + | ||
| 89 | + void paintEllipse(Context context, PdfRect box) { | ||
| 90 | + assert(box.x != null); | ||
| 91 | + assert(box.y != null); | ||
| 92 | + assert(box.width != null); | ||
| 93 | + assert(box.height != null); | ||
| 94 | + | ||
| 95 | + context.canvas | ||
| 96 | + ..setStrokeColor(color) | ||
| 97 | + ..setLineWidth(width) | ||
| 98 | + ..drawEllipse(box.x + box.width / 2.0, box.y + box.height / 2.0, | ||
| 99 | + box.width / 2.0, box.height / 2.0) | ||
| 100 | + ..strokePath(); | ||
| 101 | + } | ||
| 102 | + | ||
| 103 | + void paintRRect(Context context, PdfRect box, double borderRadius) { | ||
| 104 | + assert(box.x != null); | ||
| 105 | + assert(box.y != null); | ||
| 106 | + assert(box.width != null); | ||
| 107 | + assert(box.height != null); | ||
| 108 | + | ||
| 109 | + context.canvas | ||
| 110 | + ..setStrokeColor(color) | ||
| 111 | + ..setLineWidth(width) | ||
| 112 | + ..drawRRect( | ||
| 113 | + box.x, box.y, box.width, box.height, borderRadius, borderRadius) | ||
| 114 | + ..strokePath(); | ||
| 115 | + } | ||
| 88 | } | 116 | } |
| 89 | 117 | ||
| 90 | @immutable | 118 | @immutable |
| @@ -101,7 +129,7 @@ class DecorationImage { | @@ -101,7 +129,7 @@ class DecorationImage { | ||
| 101 | final BoxFit fit; | 129 | final BoxFit fit; |
| 102 | final Alignment alignment; | 130 | final Alignment alignment; |
| 103 | 131 | ||
| 104 | - void paintImage(Context context, PdfRect box) { | 132 | + void paint(Context context, PdfRect box) { |
| 105 | final PdfPoint imageSize = | 133 | final PdfPoint imageSize = |
| 106 | PdfPoint(image.width.toDouble(), image.height.toDouble()); | 134 | PdfPoint(image.width.toDouble(), image.height.toDouble()); |
| 107 | final FittedSizes sizes = applyBoxFit(fit, imageSize, box.size); | 135 | final FittedSizes sizes = applyBoxFit(fit, imageSize, box.size); |
| @@ -134,7 +162,8 @@ class BoxDecoration { | @@ -134,7 +162,8 @@ class BoxDecoration { | ||
| 134 | this.border, | 162 | this.border, |
| 135 | this.borderRadius, | 163 | this.borderRadius, |
| 136 | this.image, | 164 | this.image, |
| 137 | - this.shape = BoxShape.rectangle}); | 165 | + this.shape = BoxShape.rectangle}) |
| 166 | + : assert(shape != null); | ||
| 138 | 167 | ||
| 139 | /// The color to fill in the background of the box. | 168 | /// The color to fill in the background of the box. |
| 140 | final PdfColor color; | 169 | final PdfColor color; |
| @@ -143,7 +172,7 @@ class BoxDecoration { | @@ -143,7 +172,7 @@ class BoxDecoration { | ||
| 143 | final BoxShape shape; | 172 | final BoxShape shape; |
| 144 | final DecorationImage image; | 173 | final DecorationImage image; |
| 145 | 174 | ||
| 146 | - void paintBackground(Context context, PdfRect box) { | 175 | + void paint(Context context, PdfRect box) { |
| 147 | assert(box.x != null); | 176 | assert(box.x != null); |
| 148 | assert(box.y != null); | 177 | assert(box.y != null); |
| 149 | assert(box.width != null); | 178 | assert(box.width != null); |
| @@ -168,6 +197,44 @@ class BoxDecoration { | @@ -168,6 +197,44 @@ class BoxDecoration { | ||
| 168 | ..setFillColor(color) | 197 | ..setFillColor(color) |
| 169 | ..fillPath(); | 198 | ..fillPath(); |
| 170 | } | 199 | } |
| 200 | + | ||
| 201 | + if (image != null) { | ||
| 202 | + context.canvas.saveContext(); | ||
| 203 | + switch (shape) { | ||
| 204 | + case BoxShape.circle: | ||
| 205 | + context.canvas | ||
| 206 | + ..drawEllipse(box.x + box.width / 2.0, box.y + box.height / 2.0, | ||
| 207 | + box.width / 2.0, box.height / 2.0) | ||
| 208 | + ..clipPath(); | ||
| 209 | + | ||
| 210 | + break; | ||
| 211 | + case BoxShape.rectangle: | ||
| 212 | + if (borderRadius != null) { | ||
| 213 | + context.canvas | ||
| 214 | + ..drawRRect(box.x, box.y, box.width, box.height, borderRadius, | ||
| 215 | + borderRadius) | ||
| 216 | + ..clipPath(); | ||
| 217 | + } | ||
| 218 | + break; | ||
| 219 | + } | ||
| 220 | + image.paint(context, box); | ||
| 221 | + context.canvas.restoreContext(); | ||
| 222 | + } | ||
| 223 | + | ||
| 224 | + if (border != null) { | ||
| 225 | + switch (shape) { | ||
| 226 | + case BoxShape.circle: | ||
| 227 | + border.paintEllipse(context, box); | ||
| 228 | + break; | ||
| 229 | + case BoxShape.rectangle: | ||
| 230 | + if (borderRadius != null) { | ||
| 231 | + border.paintRRect(context, box, borderRadius); | ||
| 232 | + } else { | ||
| 233 | + border.paintRect(context, box); | ||
| 234 | + } | ||
| 235 | + break; | ||
| 236 | + } | ||
| 237 | + } | ||
| 171 | } | 238 | } |
| 172 | } | 239 | } |
| 173 | 240 | ||
| @@ -190,15 +257,11 @@ class DecoratedBox extends SingleChildWidget { | @@ -190,15 +257,11 @@ class DecoratedBox extends SingleChildWidget { | ||
| 190 | void paint(Context context) { | 257 | void paint(Context context) { |
| 191 | super.paint(context); | 258 | super.paint(context); |
| 192 | if (position == DecorationPosition.background) { | 259 | if (position == DecorationPosition.background) { |
| 193 | - decoration.paintBackground(context, box); | ||
| 194 | - decoration.image?.paintImage(context, box); | ||
| 195 | - decoration.border?.paintBorders(context, box); | 260 | + decoration.paint(context, box); |
| 196 | } | 261 | } |
| 197 | paintChild(context); | 262 | paintChild(context); |
| 198 | if (position == DecorationPosition.foreground) { | 263 | if (position == DecorationPosition.foreground) { |
| 199 | - decoration.paintBackground(context, box); | ||
| 200 | - decoration.image?.paintImage(context, box); | ||
| 201 | - decoration.border?.paintBorders(context, box); | 264 | + decoration.paint(context, box); |
| 202 | } | 265 | } |
| 203 | } | 266 | } |
| 204 | } | 267 | } |
| @@ -53,10 +53,9 @@ class TableBorder extends BoxBorder { | @@ -53,10 +53,9 @@ class TableBorder extends BoxBorder { | ||
| 53 | final bool horizontalInside; | 53 | final bool horizontalInside; |
| 54 | final bool verticalInside; | 54 | final bool verticalInside; |
| 55 | 55 | ||
| 56 | - @override | ||
| 57 | - void paintBorders(Context context, PdfRect box, | 56 | + void paint(Context context, PdfRect box, |
| 58 | [List<double> widths, List<double> heights]) { | 57 | [List<double> widths, List<double> heights]) { |
| 59 | - super.paintBorders(context, box); | 58 | + super.paintRect(context, box); |
| 60 | 59 | ||
| 61 | if (verticalInside) { | 60 | if (verticalInside) { |
| 62 | double offset = box.x; | 61 | double offset = box.x; |
| @@ -307,7 +306,7 @@ class Table extends Widget implements SpanningWidget { | @@ -307,7 +306,7 @@ class Table extends Widget implements SpanningWidget { | ||
| 307 | context.canvas.restoreContext(); | 306 | context.canvas.restoreContext(); |
| 308 | 307 | ||
| 309 | if (border != null) { | 308 | if (border != null) { |
| 310 | - border.paintBorders(context, box, _widths, _heights); | 309 | + border.paint(context, box, _widths, _heights); |
| 311 | } | 310 | } |
| 312 | } | 311 | } |
| 313 | } | 312 | } |
| @@ -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.14 | 7 | +version: 1.3.15 |
| 8 | 8 | ||
| 9 | environment: | 9 | environment: |
| 10 | sdk: ">=2.1.0 <3.0.0" | 10 | sdk: ">=2.1.0 <3.0.0" |
| @@ -90,6 +90,80 @@ void main() { | @@ -90,6 +90,80 @@ void main() { | ||
| 90 | )); | 90 | )); |
| 91 | }); | 91 | }); |
| 92 | 92 | ||
| 93 | + test('Container Widgets BoxShape Image', () { | ||
| 94 | + final Uint32List img = generateBitmap(100, 100); | ||
| 95 | + final PdfImage image = PdfImage(pdf.document, | ||
| 96 | + image: img.buffer.asUint8List(), width: 100, height: 100); | ||
| 97 | + | ||
| 98 | + pdf.addPage(Page( | ||
| 99 | + build: (Context context) => Center( | ||
| 100 | + child: Column( | ||
| 101 | + mainAxisAlignment: MainAxisAlignment.spaceEvenly, | ||
| 102 | + children: <Widget>[ | ||
| 103 | + Container( | ||
| 104 | + height: 200.0, | ||
| 105 | + width: 200.0, | ||
| 106 | + decoration: BoxDecoration( | ||
| 107 | + shape: BoxShape.circle, | ||
| 108 | + image: DecorationImage(image: image, fit: BoxFit.cover), | ||
| 109 | + ), | ||
| 110 | + ), | ||
| 111 | + Container( | ||
| 112 | + height: 200.0, | ||
| 113 | + width: 200.0, | ||
| 114 | + decoration: BoxDecoration( | ||
| 115 | + shape: BoxShape.rectangle, | ||
| 116 | + borderRadius: 40, | ||
| 117 | + image: DecorationImage(image: image, fit: BoxFit.cover), | ||
| 118 | + ), | ||
| 119 | + ), | ||
| 120 | + ], | ||
| 121 | + ), | ||
| 122 | + ), | ||
| 123 | + )); | ||
| 124 | + }); | ||
| 125 | + | ||
| 126 | + test('Container Widgets BoxShape Border', () { | ||
| 127 | + pdf.addPage(Page( | ||
| 128 | + build: (Context context) => Center( | ||
| 129 | + child: Column( | ||
| 130 | + mainAxisAlignment: MainAxisAlignment.spaceEvenly, | ||
| 131 | + children: <Widget>[ | ||
| 132 | + Container( | ||
| 133 | + height: 200.0, | ||
| 134 | + width: 200.0, | ||
| 135 | + decoration: const BoxDecoration( | ||
| 136 | + shape: BoxShape.circle, | ||
| 137 | + border: BoxBorder( | ||
| 138 | + bottom: true, | ||
| 139 | + top: true, | ||
| 140 | + left: true, | ||
| 141 | + right: true, | ||
| 142 | + color: PdfColors.blue, | ||
| 143 | + width: 3), | ||
| 144 | + ), | ||
| 145 | + ), | ||
| 146 | + Container( | ||
| 147 | + height: 200.0, | ||
| 148 | + width: 200.0, | ||
| 149 | + decoration: const BoxDecoration( | ||
| 150 | + shape: BoxShape.rectangle, | ||
| 151 | + borderRadius: 40, | ||
| 152 | + border: BoxBorder( | ||
| 153 | + bottom: true, | ||
| 154 | + top: true, | ||
| 155 | + left: true, | ||
| 156 | + right: true, | ||
| 157 | + color: PdfColors.blue, | ||
| 158 | + width: 3), | ||
| 159 | + ), | ||
| 160 | + ), | ||
| 161 | + ], | ||
| 162 | + ), | ||
| 163 | + ), | ||
| 164 | + )); | ||
| 165 | + }); | ||
| 166 | + | ||
| 93 | tearDownAll(() { | 167 | tearDownAll(() { |
| 94 | final File file = File('widgets-container.pdf'); | 168 | final File file = File('widgets-container.pdf'); |
| 95 | file.writeAsBytesSync(pdf.save()); | 169 | file.writeAsBytesSync(pdf.save()); |
-
Please register or login to post a comment