Showing
2 changed files
with
54 additions
and
37 deletions
@@ -7,6 +7,7 @@ | @@ -7,6 +7,7 @@ | ||
7 | * Add Bézier Curve primitive | 7 | * Add Bézier Curve primitive |
8 | * Implement drawShape | 8 | * Implement drawShape |
9 | * Add support for Jpeg images | 9 | * Add support for Jpeg images |
10 | +* Fix numeric conversions in graphic operations | ||
10 | 11 | ||
11 | # 1.0.8 | 12 | # 1.0.8 |
12 | * Fix monospace TTF font loading | 13 | * Fix monospace TTF font loading |
@@ -95,7 +95,9 @@ class PdfGraphics { | @@ -95,7 +95,9 @@ class PdfGraphics { | ||
95 | page.xObjects[img.name] = img; | 95 | page.xObjects[img.name] = img; |
96 | 96 | ||
97 | // q w 0 0 h x y cm % the coordinate matrix | 97 | // q w 0 0 h x y cm % the coordinate matrix |
98 | - buf.putString("q $w 0 0 $h $x $y cm ${img.name} Do Q\n"); | 98 | + buf.putString("q "); |
99 | + buf.putNumList(<double>[w, 0.0, 0.0, h, x, y]); | ||
100 | + buf.putString(" cm ${img.name} Do Q\n"); | ||
99 | } | 101 | } |
100 | 102 | ||
101 | /// Draws a line between two coordinates. | 103 | /// Draws a line between two coordinates. |
@@ -128,14 +130,18 @@ class PdfGraphics { | @@ -128,14 +130,18 @@ class PdfGraphics { | ||
128 | // Starting point | 130 | // Starting point |
129 | moveTo(x, y - r2); | 131 | moveTo(x, y - r2); |
130 | 132 | ||
131 | - buf.putString( | ||
132 | - "${x + m4 * r1} ${y - r2} ${x + r1} ${y - m4 * r2} ${x + r1} $y c\n"); | ||
133 | - buf.putString( | ||
134 | - "${x + r1} ${y + m4 * r2} ${x + m4 * r1} ${y + r2} $x ${y + r2} c\n"); | ||
135 | - buf.putString( | ||
136 | - "${x - m4 * r1} ${y + r2} ${x - r1} ${y + m4 * r2} ${x - r1} $y c\n"); | ||
137 | - buf.putString( | ||
138 | - "${x - r1} ${y - m4 * r2} ${x - m4 * r1} ${y - r2} $x ${y - r2} c\n"); | 133 | + buf.putNumList( |
134 | + <double>[x + m4 * r1, y - r2, x + r1, y - m4 * r2, x + r1, y]); | ||
135 | + buf.putString(" c\n"); | ||
136 | + buf.putNumList( | ||
137 | + <double>[x + r1, y + m4 * r2, x + m4 * r1, y + r2, x, y + r2]); | ||
138 | + buf.putString(" c\n"); | ||
139 | + buf.putNumList( | ||
140 | + <double>[x - m4 * r1, y + r2, x - r1, y + m4 * r2, x - r1, y]); | ||
141 | + buf.putString(" c\n"); | ||
142 | + buf.putNumList( | ||
143 | + <double>[x - r1, y - m4 * r2, x - m4 * r1, y - r2, x, y - r2]); | ||
144 | + buf.putString(" c\n"); | ||
139 | } | 145 | } |
140 | 146 | ||
141 | /// We override Graphics.drawRect as it doesn't join the 4 lines. | 147 | /// We override Graphics.drawRect as it doesn't join the 4 lines. |
@@ -151,7 +157,8 @@ class PdfGraphics { | @@ -151,7 +157,8 @@ class PdfGraphics { | ||
151 | double w, | 157 | double w, |
152 | double h, | 158 | double h, |
153 | ) { | 159 | ) { |
154 | - buf.putString("$x $y $w $h re\n"); | 160 | + buf.putNumList(<double>[x, y, w, h]); |
161 | + buf.putString(" re\n"); | ||
155 | } | 162 | } |
156 | 163 | ||
157 | /// This draws a string. | 164 | /// This draws a string. |
@@ -164,7 +171,11 @@ class PdfGraphics { | @@ -164,7 +171,11 @@ class PdfGraphics { | ||
164 | page.fonts[font.name] = font; | 171 | page.fonts[font.name] = font; |
165 | } | 172 | } |
166 | 173 | ||
167 | - buf.putString("BT $x $y Td ${font.name} $size Tf "); | 174 | + buf.putString("BT "); |
175 | + buf.putNumList(<double>[x, y]); | ||
176 | + buf.putString(" Td ${font.name} "); | ||
177 | + buf.putNum(size); | ||
178 | + buf.putString(" Tf "); | ||
168 | buf.putText(s); | 179 | buf.putText(s); |
169 | buf.putString(" Tj ET\n"); | 180 | buf.putString(" Tj ET\n"); |
170 | } | 181 | } |
@@ -173,8 +184,10 @@ class PdfGraphics { | @@ -173,8 +184,10 @@ class PdfGraphics { | ||
173 | /// | 184 | /// |
174 | /// @param c Color to use | 185 | /// @param c Color to use |
175 | void setColor(PdfColor color) { | 186 | void setColor(PdfColor color) { |
176 | - buf.putString( | ||
177 | - "${color.r} ${color.g} ${color.b} rg ${color.r} ${color.g} ${color.b} RG\n"); | 187 | + buf.putNumList(<double>[color.r, color.g, color.b]); |
188 | + buf.putString(" rg "); | ||
189 | + buf.putNumList(<double>[color.r, color.g, color.b]); | ||
190 | + buf.putString(" RG\n"); | ||
178 | } | 191 | } |
179 | 192 | ||
180 | /// Set the transformation Matrix | 193 | /// Set the transformation Matrix |
@@ -189,7 +202,8 @@ class PdfGraphics { | @@ -189,7 +202,8 @@ class PdfGraphics { | ||
189 | /// @param x coordinate | 202 | /// @param x coordinate |
190 | /// @param y coordinate | 203 | /// @param y coordinate |
191 | void lineTo(double x, double y) { | 204 | void lineTo(double x, double y) { |
192 | - buf.putString("$x $y l\n"); | 205 | + buf.putNumList(<double>[x, y]); |
206 | + buf.putString(" l\n"); | ||
193 | } | 207 | } |
194 | 208 | ||
195 | /// This moves the current drawing point. | 209 | /// This moves the current drawing point. |
@@ -197,18 +211,19 @@ class PdfGraphics { | @@ -197,18 +211,19 @@ class PdfGraphics { | ||
197 | /// @param x coordinate | 211 | /// @param x coordinate |
198 | /// @param y coordinate | 212 | /// @param y coordinate |
199 | void moveTo(double x, double y) { | 213 | void moveTo(double x, double y) { |
200 | - buf.putString("$x $y m\n"); | 214 | + buf.putNumList(<double>[x, y]); |
215 | + buf.putString(" m\n"); | ||
201 | } | 216 | } |
202 | 217 | ||
203 | /// Draw a bézier curve | 218 | /// Draw a bézier curve |
204 | void curveTo( | 219 | void curveTo( |
205 | double x1, double y1, double x2, double y2, double x3, double y3) { | 220 | double x1, double y1, double x2, double y2, double x3, double y3) { |
206 | - buf.putString("$x1 $y1 $x2 $y2 $x3 $y3 c\n"); | 221 | + buf.putNumList(<double>[x1, y1, x2, y2, x3, y3]); |
222 | + buf.putString(" c\n"); | ||
207 | } | 223 | } |
208 | 224 | ||
209 | /// https://github.com/deeplook/svglib/blob/master/svglib/svglib.py#L911 | 225 | /// https://github.com/deeplook/svglib/blob/master/svglib/svglib.py#L911 |
210 | void drawShape(String d, {stroke = true}) { | 226 | void drawShape(String d, {stroke = true}) { |
211 | - final sb = StringBuffer(); | ||
212 | final exp = RegExp(r"([MmZzLlHhVvCcSsQqTtAaE])|(-[\.0-9]+)|([\.0-9]+)"); | 227 | final exp = RegExp(r"([MmZzLlHhVvCcSsQqTtAaE])|(-[\.0-9]+)|([\.0-9]+)"); |
213 | final matches = exp.allMatches(d + " E"); | 228 | final matches = exp.allMatches(d + " E"); |
214 | String action; | 229 | String action; |
@@ -230,42 +245,42 @@ class PdfGraphics { | @@ -230,42 +245,42 @@ class PdfGraphics { | ||
230 | case 'm': // moveto relative | 245 | case 'm': // moveto relative |
231 | lastPoint = | 246 | lastPoint = |
232 | PdfPoint(lastPoint.x + points[0], lastPoint.y + points[1]); | 247 | PdfPoint(lastPoint.x + points[0], lastPoint.y + points[1]); |
233 | - sb.write("${lastPoint.x} ${lastPoint.y} m "); | 248 | + moveTo(lastPoint.x, lastPoint.y); |
234 | break; | 249 | break; |
235 | case 'M': // moveto absolute | 250 | case 'M': // moveto absolute |
236 | lastPoint = PdfPoint(points[0], points[1]); | 251 | lastPoint = PdfPoint(points[0], points[1]); |
237 | - sb.write("${lastPoint.x} ${lastPoint.y} m "); | 252 | + moveTo(lastPoint.x, lastPoint.y); |
238 | break; | 253 | break; |
239 | case 'l': // lineto relative | 254 | case 'l': // lineto relative |
240 | lastPoint = | 255 | lastPoint = |
241 | PdfPoint(lastPoint.x + points[0], lastPoint.y + points[1]); | 256 | PdfPoint(lastPoint.x + points[0], lastPoint.y + points[1]); |
242 | - sb.write("${lastPoint.x} ${lastPoint.y} l "); | 257 | + lineTo(lastPoint.x, lastPoint.y); |
243 | break; | 258 | break; |
244 | case 'L': // lineto absolute | 259 | case 'L': // lineto absolute |
245 | lastPoint = PdfPoint(points[0], points[1]); | 260 | lastPoint = PdfPoint(points[0], points[1]); |
246 | - sb.write("${lastPoint.x} ${lastPoint.y} l "); | 261 | + lineTo(lastPoint.x, lastPoint.y); |
247 | break; | 262 | break; |
248 | case 'H': // horizontal line absolute | 263 | case 'H': // horizontal line absolute |
249 | lastPoint = PdfPoint(points[0], lastPoint.y); | 264 | lastPoint = PdfPoint(points[0], lastPoint.y); |
250 | - sb.write("${lastPoint.x} ${lastPoint.y} l "); | 265 | + lineTo(lastPoint.x, lastPoint.y); |
251 | break; | 266 | break; |
252 | case 'V': // vertical line absolute | 267 | case 'V': // vertical line absolute |
253 | lastPoint = PdfPoint(lastPoint.x, points[0]); | 268 | lastPoint = PdfPoint(lastPoint.x, points[0]); |
254 | - sb.write("${lastPoint.x} ${lastPoint.y} l "); | 269 | + lineTo(lastPoint.x, lastPoint.y); |
255 | break; | 270 | break; |
256 | case 'h': // horizontal line relative | 271 | case 'h': // horizontal line relative |
257 | lastPoint = PdfPoint(lastPoint.x + points[0], lastPoint.y); | 272 | lastPoint = PdfPoint(lastPoint.x + points[0], lastPoint.y); |
258 | - sb.write("${lastPoint.x} ${lastPoint.y} l "); | 273 | + lineTo(lastPoint.x, lastPoint.y); |
259 | break; | 274 | break; |
260 | case 'v': // vertical line relative | 275 | case 'v': // vertical line relative |
261 | lastPoint = PdfPoint(lastPoint.x, lastPoint.y + points[0]); | 276 | lastPoint = PdfPoint(lastPoint.x, lastPoint.y + points[0]); |
262 | - sb.write("${lastPoint.x} ${lastPoint.y} l "); | 277 | + lineTo(lastPoint.x, lastPoint.y); |
263 | break; | 278 | break; |
264 | case 'C': // cubic bezier, absolute | 279 | case 'C': // cubic bezier, absolute |
265 | var len = 0; | 280 | var len = 0; |
266 | while ((len + 1) * 6 <= points.length) { | 281 | while ((len + 1) * 6 <= points.length) { |
267 | - sb.write( | ||
268 | - points.sublist(len * 6, (len + 1) * 6).join(" ") + " c "); | 282 | + curveTo(points[len + 0], points[len + 1], points[len + 2], |
283 | + points[len + 3], points[len + 4], points[len + 5]); | ||
269 | len += 1; | 284 | len += 1; |
270 | } | 285 | } |
271 | lastPoint = | 286 | lastPoint = |
@@ -284,8 +299,8 @@ class PdfGraphics { | @@ -284,8 +299,8 @@ class PdfGraphics { | ||
284 | } | 299 | } |
285 | lastControl = PdfPoint(points[0], points[1]); | 300 | lastControl = PdfPoint(points[0], points[1]); |
286 | lastPoint = PdfPoint(points[2], points[3]); | 301 | lastPoint = PdfPoint(points[2], points[3]); |
287 | - sb.write( | ||
288 | - "${c1.x} ${c1.y} ${lastControl.x} ${lastControl.y} ${lastPoint.x} ${lastPoint.y} c "); | 302 | + curveTo(c1.x, c1.y, lastControl.x, lastControl.y, lastPoint.x, |
303 | + lastPoint.y); | ||
289 | points = points.sublist(4); | 304 | points = points.sublist(4); |
290 | lastAction = 'C'; | 305 | lastAction = 'C'; |
291 | } | 306 | } |
@@ -299,8 +314,8 @@ class PdfGraphics { | @@ -299,8 +314,8 @@ class PdfGraphics { | ||
299 | points[len + 3] += lastPoint.y; | 314 | points[len + 3] += lastPoint.y; |
300 | points[len + 4] += lastPoint.x; | 315 | points[len + 4] += lastPoint.x; |
301 | points[len + 5] += lastPoint.y; | 316 | points[len + 5] += lastPoint.y; |
302 | - sb.write( | ||
303 | - points.sublist(len * 6, (len + 1) * 6).join(" ") + " c "); | 317 | + curveTo(points[len + 0], points[len + 1], points[len + 2], |
318 | + points[len + 3], points[len + 4], points[len + 5]); | ||
304 | len += 1; | 319 | len += 1; |
305 | } | 320 | } |
306 | lastPoint = | 321 | lastPoint = |
@@ -321,8 +336,8 @@ class PdfGraphics { | @@ -321,8 +336,8 @@ class PdfGraphics { | ||
321 | PdfPoint(points[0] + lastPoint.x, points[1] + lastPoint.y); | 336 | PdfPoint(points[0] + lastPoint.x, points[1] + lastPoint.y); |
322 | lastPoint = | 337 | lastPoint = |
323 | PdfPoint(points[2] + lastPoint.x, points[3] + lastPoint.y); | 338 | PdfPoint(points[2] + lastPoint.x, points[3] + lastPoint.y); |
324 | - sb.write( | ||
325 | - "${c1.x} ${c1.y} ${lastControl.x} ${lastControl.y} ${lastPoint.x} ${lastPoint.y} c "); | 339 | + curveTo(c1.x, c1.y, lastControl.x, lastControl.y, lastPoint.x, |
340 | + lastPoint.y); | ||
326 | points = points.sublist(4); | 341 | points = points.sublist(4); |
327 | lastAction = 'c'; | 342 | lastAction = 'c'; |
328 | } | 343 | } |
@@ -341,7 +356,7 @@ class PdfGraphics { | @@ -341,7 +356,7 @@ class PdfGraphics { | ||
341 | // break; | 356 | // break; |
342 | case 'Z': // close path | 357 | case 'Z': // close path |
343 | case 'z': // close path | 358 | case 'z': // close path |
344 | - if (stroke) sb.write("s "); | 359 | + if (stroke) closePath(); |
345 | break; | 360 | break; |
346 | default: | 361 | default: |
347 | print("Unknown path action: $action"); | 362 | print("Unknown path action: $action"); |
@@ -351,7 +366,6 @@ class PdfGraphics { | @@ -351,7 +366,6 @@ class PdfGraphics { | ||
351 | action = a; | 366 | action = a; |
352 | points = List<double>(); | 367 | points = List<double>(); |
353 | } | 368 | } |
354 | - buf.putString(sb.toString()); | ||
355 | } | 369 | } |
356 | 370 | ||
357 | /// This is used to add a polygon to the current path. | 371 | /// This is used to add a polygon to the current path. |
@@ -377,10 +391,12 @@ class PdfGraphics { | @@ -377,10 +391,12 @@ class PdfGraphics { | ||
377 | } | 391 | } |
378 | 392 | ||
379 | void setLineWidth(double width) { | 393 | void setLineWidth(double width) { |
380 | - buf.putString("$width w\n"); | 394 | + buf.putNum(width); |
395 | + buf.putString(" w\n"); | ||
381 | } | 396 | } |
382 | 397 | ||
383 | void setMiterLimit(double limit) { | 398 | void setMiterLimit(double limit) { |
384 | - buf.putString("$limit M\n"); | 399 | + buf.putNum(limit); |
400 | + buf.putString(" M\n"); | ||
385 | } | 401 | } |
386 | } | 402 | } |
-
Please register or login to post a comment