David PHAM-VAN

Improve path operations

@@ -81,6 +81,7 @@ @@ -81,6 +81,7 @@
81 - Add Divider and VerticalDivider Widget 81 - Add Divider and VerticalDivider Widget
82 - Replace Theme with ThemeData 82 - Replace Theme with ThemeData
83 - Implement ImageProvider 83 - Implement ImageProvider
  84 +- Improve path operations
84 85
85 ## 1.6.2 86 ## 1.6.2
86 87
@@ -114,24 +114,31 @@ class PdfGraphics { @@ -114,24 +114,31 @@ class PdfGraphics {
114 PdfFont get defaultFont => _page.getDefaultFont(); 114 PdfFont get defaultFont => _page.getDefaultFont();
115 115
116 /// Draw a surface on the previously defined shape 116 /// Draw a surface on the previously defined shape
117 - void fillPath() {  
118 - buf.putString('f\n'); 117 + /// set evenOdd to false to use the nonzero winding number rule to determine the region to fill and to true to use the even-odd rule to determine the region to fill
  118 + void fillPath({bool evenOdd = false}) {
  119 + buf.putString('f${evenOdd ? '*' : ''}\n');
119 } 120 }
120 121
121 /// Draw the contour of the previously defined shape 122 /// Draw the contour of the previously defined shape
122 - void strokePath() {  
123 - buf.putString('S\n'); 123 + void strokePath({bool close = false}) {
  124 + buf.putString('${close ? 's' : 'S'}\n');
124 } 125 }
125 126
126 /// Close the path with a line 127 /// Close the path with a line
127 void closePath() { 128 void closePath() {
128 - buf.putString('s\n'); 129 + buf.putString('h\n');
129 } 130 }
130 131
131 /// Create a clipping surface from the previously defined shape, 132 /// Create a clipping surface from the previously defined shape,
132 /// to prevent any further drawing outside 133 /// to prevent any further drawing outside
133 - void clipPath() {  
134 - buf.putString('W n\n'); 134 + void clipPath({bool evenOdd = false, bool end = true}) {
  135 + buf.putString('W${evenOdd ? '*' : ''}${end ? ' n' : ''}\n');
  136 + }
  137 +
  138 + /// Draw a surface on the previously defined shape and then draw the contour
  139 + /// set evenOdd to false to use the nonzero winding number rule to determine the region to fill and to true to use the even-odd rule to determine the region to fill
  140 + void fillAndStrokePath({bool evenOdd = false, bool close = false}) {
  141 + buf.putString('${close ? 'b' : 'B'}${evenOdd ? '*' : ''}\n');
135 } 142 }
136 143
137 /// Apply a shader 144 /// Apply a shader
@@ -516,8 +523,8 @@ class PdfGraphics { @@ -516,8 +523,8 @@ class PdfGraphics {
516 } 523 }
517 524
518 /// Draw an SVG path 525 /// Draw an SVG path
519 - void drawShape(String d, {bool stroke = true}) {  
520 - final proxy = _PathProxy(this, stroke); 526 + void drawShape(String d) {
  527 + final proxy = _PathProxy(this);
521 writeSvgPathDataToPath(d, proxy); 528 writeSvgPathDataToPath(d, proxy);
522 } 529 }
523 530
@@ -554,17 +561,14 @@ class PdfGraphics { @@ -554,17 +561,14 @@ class PdfGraphics {
554 } 561 }
555 562
556 class _PathProxy extends PathProxy { 563 class _PathProxy extends PathProxy {
557 - _PathProxy(this.canvas, this.stroke); 564 + _PathProxy(this.canvas);
558 565
559 final PdfGraphics canvas; 566 final PdfGraphics canvas;
560 - final bool stroke;  
561 567
562 @override 568 @override
563 void close() { 569 void close() {
564 - if (stroke) {  
565 canvas.closePath(); 570 canvas.closePath();
566 } 571 }
567 - }  
568 572
569 @override 573 @override
570 void cubicTo( 574 void cubicTo(
@@ -112,7 +112,7 @@ class BoxBorder { @@ -112,7 +112,7 @@ class BoxBorder {
112 context.canvas.moveTo(box.x, box.y); 112 context.canvas.moveTo(box.x, box.y);
113 context.canvas.lineTo(box.x, box.top); 113 context.canvas.lineTo(box.x, box.top);
114 } else if (right && top) { 114 } else if (right && top) {
115 - context.canvas.closePath(); 115 + context.canvas.strokePath(close: true);
116 } else { 116 } else {
117 context.canvas.lineTo(box.x, box.top); 117 context.canvas.lineTo(box.x, box.top);
118 } 118 }
@@ -203,14 +203,15 @@ class Shape extends Widget { @@ -203,14 +203,15 @@ class Shape extends Widget {
203 if (fillColor != null) { 203 if (fillColor != null) {
204 context.canvas 204 context.canvas
205 ..setFillColor(fillColor) 205 ..setFillColor(fillColor)
206 - ..drawShape(shape, stroke: false) 206 + ..drawShape(shape)
207 ..fillPath(); 207 ..fillPath();
208 } 208 }
209 209
210 if (strokeColor != null) { 210 if (strokeColor != null) {
211 context.canvas 211 context.canvas
212 ..setStrokeColor(strokeColor) 212 ..setStrokeColor(strokeColor)
213 - ..drawShape(shape, stroke: true); 213 + ..drawShape(shape)
  214 + ..strokePath();
214 } 215 }
215 216
216 context.canvas.restoreContext(); 217 context.canvas.restoreContext();
@@ -45,7 +45,7 @@ void main() { @@ -45,7 +45,7 @@ void main() {
45 g.setColor(const PdfColor(0, 0, 0)); 45 g.setColor(const PdfColor(0, 0, 0));
46 g.drawShape( 46 g.drawShape(
47 'M37 0H9C6.24 0 4 2.24 4 5v38c0 2.76 2.24 5 5 5h28c2.76 0 5-2.24 5-5V5c0-2.76-2.24-5-5-5zM23 46c-1.66 0-3-1.34-3-3s1.34-3 3-3 3 1.34 3 3-1.34 3-3 3zm15-8H8V6h30v32z', 47 'M37 0H9C6.24 0 4 2.24 4 5v38c0 2.76 2.24 5 5 5h28c2.76 0 5-2.24 5-5V5c0-2.76-2.24-5-5-5zM23 46c-1.66 0-3-1.34-3-3s1.34-3 3-3 3 1.34 3 3-1.34 3-3 3zm15-8H8V6h30v32z',
48 - stroke: false); 48 + );
49 g.fillPath(); 49 g.fillPath();
50 g.restoreContext(); 50 g.restoreContext();
51 51
@@ -57,6 +57,7 @@ void main() { @@ -57,6 +57,7 @@ void main() {
57 g.setColor(const PdfColor(0, 0, 0)); 57 g.setColor(const PdfColor(0, 0, 0));
58 g.drawShape( 58 g.drawShape(
59 'M300,200 h-150 a150,150 0 1,0 150,-150 z M275,175 v-150 a150,150 0 0,0 -150,150 z'); 59 'M300,200 h-150 a150,150 0 1,0 150,-150 z M275,175 v-150 a150,150 0 0,0 -150,150 z');
  60 + g.strokePath();
60 g.restoreContext(); 61 g.restoreContext();
61 62
62 final font1 = g.defaultFont; 63 final font1 = g.defaultFont;
No preview for this file type