David PHAM-VAN

Implement rounded rect

@@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
2 * Change license to Apache 2.0 2 * Change license to Apache 2.0
3 * Improve PdfRect 3 * Improve PdfRect
4 * Add support for CMYK, HSL anf HSV colors 4 * Add support for CMYK, HSL anf HSV colors
  5 +* Implement rounded rect
5 6
6 # 1.1.1 7 # 1.1.1
7 * Improve PdfPoint and PdfRect 8 * Improve PdfPoint and PdfRect
@@ -19,6 +19,9 @@ part of pdf; @@ -19,6 +19,9 @@ part of pdf;
19 enum PdfLineCap { joinMiter, joinRound, joinBevel } 19 enum PdfLineCap { joinMiter, joinRound, joinBevel }
20 20
21 class PdfGraphics { 21 class PdfGraphics {
  22 + /// Ellipse 4-spline magic number
  23 + static const _m4 = 0.551784;
  24 +
22 /// Graphic context number 25 /// Graphic context number
23 var _context = 0; 26 var _context = 0;
24 27
@@ -122,28 +125,14 @@ class PdfGraphics { @@ -122,28 +125,14 @@ class PdfGraphics {
122 } 125 }
123 126
124 void drawEllipse(double x, double y, double r1, double r2) { 127 void drawEllipse(double x, double y, double r1, double r2) {
125 - // The best 4-spline magic number  
126 - double m4 = 0.551784;  
127 -  
128 - // Starting point  
129 moveTo(x, y - r2); 128 moveTo(x, y - r2);
130 -  
131 - buf.putNumList(  
132 - <double>[x + m4 * r1, y - r2, x + r1, y - m4 * r2, x + r1, y]);  
133 - buf.putString(" c\n");  
134 - buf.putNumList(  
135 - <double>[x + r1, y + m4 * r2, x + m4 * r1, y + r2, x, y + r2]);  
136 - buf.putString(" c\n");  
137 - buf.putNumList(  
138 - <double>[x - m4 * r1, y + r2, x - r1, y + m4 * r2, x - r1, y]);  
139 - buf.putString(" c\n");  
140 - buf.putNumList(  
141 - <double>[x - r1, y - m4 * r2, x - m4 * r1, y - r2, x, y - r2]);  
142 - buf.putString(" c\n"); 129 + curveTo(x + _m4 * r1, y - r2, x + r1, y - _m4 * r2, x + r1, y);
  130 + curveTo(x + r1, y + _m4 * r2, x + _m4 * r1, y + r2, x, y + r2);
  131 + curveTo(x - _m4 * r1, y + r2, x - r1, y + _m4 * r2, x - r1, y);
  132 + curveTo(x - r1, y - _m4 * r2, x - _m4 * r1, y - r2, x, y - r2);
143 } 133 }
144 134
145 - /// We override Graphics.drawRect as it doesn't join the 4 lines.  
146 - /// Also, Pdf provides us with a Rectangle operator, so we will use that. 135 + /// Draws a Rectangle
147 /// 136 ///
148 /// @param x coordinate 137 /// @param x coordinate
149 /// @param y coordinate 138 /// @param y coordinate
@@ -159,6 +148,27 @@ class PdfGraphics { @@ -159,6 +148,27 @@ class PdfGraphics {
159 buf.putString(" re\n"); 148 buf.putString(" re\n");
160 } 149 }
161 150
  151 + /// Draws a Rounded Rectangle
  152 + ///
  153 + /// @param x coordinate
  154 + /// @param y coordinate
  155 + /// @param w width
  156 + /// @param h height
  157 + /// @param rh horizontal radius
  158 + /// @param rv vertical radius
  159 + void drawRRect(double x, double y, double w, double h, double rv, double rh) {
  160 + moveTo(x, y + rv);
  161 + curveTo(x, y - _m4 * rv + rv, x - _m4 * rh + rh, y, x + rh, y);
  162 + lineTo(x + w - rh, y);
  163 + curveTo(x + _m4 * rh + w - rh, y, x + w, y - _m4 * rv + rv, x + w, y + rv);
  164 + lineTo(x + w, y + h - rv);
  165 + curveTo(x + w, y + _m4 * rv + h - rv, x + _m4 * rh + w - rh, y + h,
  166 + x + w - rh, y + h);
  167 + lineTo(x + rh, y + h);
  168 + curveTo(x - _m4 * rh + rh, y + h, x, y + _m4 * rv + h - rv, x, y + h - rv);
  169 + lineTo(x, y + rv);
  170 + }
  171 +
162 /// This draws a string. 172 /// This draws a string.
163 /// 173 ///
164 /// @param x coordinate 174 /// @param x coordinate