Mercurial > projects > dynamin
comparison dynamin/painting/graphics.d @ 104:5c8c1c2e12c0
Change from real to double.
double is not dependant on the platform, and it uses less space.
author | Jordan Miner <jminer7@gmail.com> |
---|---|
date | Fri, 06 Jul 2012 18:39:45 -0500 |
parents | 73060bc3f004 |
children | acdbb30fee7e |
comparison
equal
deleted
inserted
replaced
103:73060bc3f004 | 104:5c8c1c2e12c0 |
---|---|
69 } | 69 } |
70 | 70 |
71 /// | 71 /// |
72 struct FontExtents { | 72 struct FontExtents { |
73 /// | 73 /// |
74 real ascent; | 74 double ascent; |
75 /// | 75 /// |
76 real descent; | 76 double descent; |
77 /// | 77 /// |
78 real leading() { return height - ascent - descent; } | 78 double leading() { return height - ascent - descent; } |
79 /// | 79 /// |
80 real height; | 80 double height; |
81 /// | 81 /// |
82 real maxAdvance; | 82 double maxAdvance; |
83 } | 83 } |
84 | 84 |
85 //import lodepng = dynamin.lodepng.decode; | 85 //import lodepng = dynamin.lodepng.decode; |
86 /// An RGBA 32-bit-per-pixel image. | 86 /// An RGBA 32-bit-per-pixel image. |
87 class Image { | 87 class Image { |
228 | 228 |
229 Stdout("Cairo error: ")(cairo_status_to_string(status)).newline; | 229 Stdout("Cairo error: ")(cairo_status_to_string(status)).newline; |
230 assert(0); | 230 assert(0); |
231 } | 231 } |
232 /// | 232 /// |
233 void moveTo(real x, real y) { | 233 void moveTo(double x, double y) { |
234 cairo_move_to(cr, x, y); | 234 cairo_move_to(cr, x, y); |
235 } | 235 } |
236 /// ditto | 236 /// ditto |
237 void moveTo(Point pt) { | 237 void moveTo(Point pt) { |
238 moveTo(pt.x, pt.y); | 238 moveTo(pt.x, pt.y); |
239 } | 239 } |
240 /// | 240 /// |
241 void lineTo(real x, real y) { | 241 void lineTo(double x, double y) { |
242 cairo_line_to(cr, x, y); | 242 cairo_line_to(cr, x, y); |
243 } | 243 } |
244 /// ditto | 244 /// ditto |
245 void lineTo(Point pt) { | 245 void lineTo(Point pt) { |
246 lineTo(pt.x, pt.y); | 246 lineTo(pt.x, pt.y); |
248 /// | 248 /// |
249 void curveTo(Point pt1, Point pt2, Point pt3) { | 249 void curveTo(Point pt1, Point pt2, Point pt3) { |
250 curveTo(pt1.x, pt1.y, pt2.x, pt2.y, pt3.x, pt3.y); | 250 curveTo(pt1.x, pt1.y, pt2.x, pt2.y, pt3.x, pt3.y); |
251 } | 251 } |
252 /// ditto | 252 /// ditto |
253 void curveTo(real x1, real y1, real x2, real y2, real x3, real y3) { | 253 void curveTo(double x1, double y1, double x2, double y2, double x3, double y3) { |
254 cairo_curve_to(cr, x1, y1, x2, y2, x3, y3); | 254 cairo_curve_to(cr, x1, y1, x2, y2, x3, y3); |
255 } | 255 } |
256 /// | 256 /// |
257 void relMoveTo(real x, real y) { | 257 void relMoveTo(double x, double y) { |
258 cairo_rel_move_to(cr, x, y); | 258 cairo_rel_move_to(cr, x, y); |
259 } | 259 } |
260 /// ditto | 260 /// ditto |
261 void relMoveTo(Point pt) { | 261 void relMoveTo(Point pt) { |
262 relMoveTo(pt.x, pt.y); | 262 relMoveTo(pt.x, pt.y); |
263 } | 263 } |
264 /// | 264 /// |
265 void relLineTo(real x, real y) { | 265 void relLineTo(double x, double y) { |
266 cairo_rel_line_to(cr, x, y); | 266 cairo_rel_line_to(cr, x, y); |
267 } | 267 } |
268 /// ditto | 268 /// ditto |
269 void relLineTo(Point pt) { | 269 void relLineTo(Point pt) { |
270 relLineTo(pt.x, pt.y); | 270 relLineTo(pt.x, pt.y); |
272 /// | 272 /// |
273 void relCurveTo(Point pt1, Point pt2, Point pt3) { | 273 void relCurveTo(Point pt1, Point pt2, Point pt3) { |
274 relCurveTo(pt1.x, pt1.y, pt2.x, pt2.y, pt3.x, pt3.y); | 274 relCurveTo(pt1.x, pt1.y, pt2.x, pt2.y, pt3.x, pt3.y); |
275 } | 275 } |
276 /// ditto | 276 /// ditto |
277 void relCurveTo(real x1, real y1, real x2, real y2, real x3, real y3) { | 277 void relCurveTo(double x1, double y1, double x2, double y2, double x3, double y3) { |
278 cairo_rel_curve_to(cr, x1, y1, x2, y2, x3, y3); | 278 cairo_rel_curve_to(cr, x1, y1, x2, y2, x3, y3); |
279 } | 279 } |
280 /** | 280 /** |
281 * Adds an arc to the current path. A line is added connecting the | 281 * Adds an arc to the current path. A line is added connecting the |
282 * current point to the beginning of the arc. | 282 * current point to the beginning of the arc. |
286 * graphics.arc(50.5, 80.5, 40, 40, -0.2, PI/2); | 286 * graphics.arc(50.5, 80.5, 40, 40, -0.2, PI/2); |
287 * graphics.stroke(); | 287 * graphics.stroke(); |
288 * ----- | 288 * ----- |
289 * $(IMAGE ../web/example_arc.png) | 289 * $(IMAGE ../web/example_arc.png) |
290 */ | 290 */ |
291 void arc(Point ptc, real radius, real angle1, real angle2) { | 291 void arc(Point ptc, double radius, double angle1, double angle2) { |
292 arc(ptc.x, ptc.y, radius, angle1, angle2); | 292 arc(ptc.x, ptc.y, radius, angle1, angle2); |
293 } | 293 } |
294 /// ditto | 294 /// ditto |
295 void arc(real xc, real yc, real radius, real angle1, real angle2) { | 295 void arc(double xc, double yc, double radius, double angle1, double angle2) { |
296 cairo_arc(cr, xc, yc, radius, angle1, angle2); | 296 cairo_arc(cr, xc, yc, radius, angle1, angle2); |
297 } | 297 } |
298 /// ditto | 298 /// ditto |
299 void arc(real xc, real yc, real xradius, real yradius, real angle1, real angle2) { | 299 void arc(double xc, double yc, double xradius, double yradius, double angle1, double angle2) { |
300 cairo_save(cr); | 300 cairo_save(cr); |
301 cairo_translate(cr, xc, yc); | 301 cairo_translate(cr, xc, yc); |
302 cairo_scale(cr, xradius, yradius); | 302 cairo_scale(cr, xradius, yradius); |
303 cairo_arc(cr, 0, 0, 1, angle1, angle2); | 303 cairo_arc(cr, 0, 0, 1, angle1, angle2); |
304 cairo_restore(cr); | 304 cairo_restore(cr); |
311 * graphics.ellipse(70.5, 50.5, 60, 25); | 311 * graphics.ellipse(70.5, 50.5, 60, 25); |
312 * graphics.stroke(); | 312 * graphics.stroke(); |
313 * ----- | 313 * ----- |
314 * $(IMAGE ../web/example_ellipse.png) | 314 * $(IMAGE ../web/example_ellipse.png) |
315 */ | 315 */ |
316 void ellipse(real xc, real yc, real radius) { | 316 void ellipse(double xc, double yc, double radius) { |
317 cairo_new_sub_path(cr); | 317 cairo_new_sub_path(cr); |
318 cairo_arc(cr, xc, yc, radius, 0, Pi * 2); | 318 cairo_arc(cr, xc, yc, radius, 0, Pi * 2); |
319 cairo_close_path(cr); | 319 cairo_close_path(cr); |
320 } | 320 } |
321 /// ditto | 321 /// ditto |
322 void ellipse(real xc, real yc, real xradius, real yradius) { | 322 void ellipse(double xc, double yc, double xradius, double yradius) { |
323 cairo_new_sub_path(cr); | 323 cairo_new_sub_path(cr); |
324 arc(xc, yc, xradius, yradius, 0, Pi * 2); | 324 arc(xc, yc, xradius, yradius, 0, Pi * 2); |
325 cairo_close_path(cr); | 325 cairo_close_path(cr); |
326 } | 326 } |
327 /** | 327 /** |
336 */ | 336 */ |
337 void rectangle(Rect rect) { | 337 void rectangle(Rect rect) { |
338 rectangle(rect.x, rect.y, rect.width, rect.height); | 338 rectangle(rect.x, rect.y, rect.width, rect.height); |
339 } | 339 } |
340 /// ditto | 340 /// ditto |
341 void rectangle(real x, real y, real width, real height) { | 341 void rectangle(double x, double y, double width, double height) { |
342 cairo_rectangle(cr, x, y, width, height); | 342 cairo_rectangle(cr, x, y, width, height); |
343 } | 343 } |
344 /** | 344 /** |
345 * Adds a rectangle with rounded corners as a sub-path--a line will | 345 * Adds a rectangle with rounded corners as a sub-path--a line will |
346 * not connect it to the current point. | 346 * not connect it to the current point. |
347 */ | 347 */ |
348 void roundedRectangle(Rect rect, real radius) { | 348 void roundedRectangle(Rect rect, double radius) { |
349 roundedRectangle(rect.x, rect.y, rect.width, rect.height, radius); | 349 roundedRectangle(rect.x, rect.y, rect.width, rect.height, radius); |
350 } | 350 } |
351 /// ditto | 351 /// ditto |
352 void roundedRectangle(real x, real y, real width, real height, real radius) { | 352 void roundedRectangle(double x, double y, double width, double height, double radius) { |
353 alias radius r; | 353 alias radius r; |
354 cairo_new_sub_path(cr); | 354 cairo_new_sub_path(cr); |
355 arc(x+r, y+r, r, Pi, 3*Pi/2); | 355 arc(x+r, y+r, r, Pi, 3*Pi/2); |
356 arc(x+width-r, y+r, r, 3*Pi/2, 0); | 356 arc(x+width-r, y+r, r, 3*Pi/2, 0); |
357 arc(x+width-r, y+height-r, r, 0, Pi/2); | 357 arc(x+width-r, y+height-r, r, 0, Pi/2); |
417 * graphics.lineWidth = 5; | 417 * graphics.lineWidth = 5; |
418 * graphics.stroke(); | 418 * graphics.stroke(); |
419 * ----- | 419 * ----- |
420 * $(IMAGE ../web/example_line_width.png) | 420 * $(IMAGE ../web/example_line_width.png) |
421 */ | 421 */ |
422 real lineWidth() { | 422 double lineWidth() { |
423 return cairo_get_line_width(cr); | 423 return cairo_get_line_width(cr); |
424 } | 424 } |
425 /// ditto | 425 /// ditto |
426 void lineWidth(real width) { | 426 void lineWidth(double width) { |
427 cairo_set_line_width(cr, width); | 427 cairo_set_line_width(cr, width); |
428 } | 428 } |
429 /** | 429 /** |
430 * Gets or sets the line cap used for stroking. | 430 * Gets or sets the line cap used for stroking. |
431 * | 431 * |
442 } | 442 } |
443 /** | 443 /** |
444 * Sets the font size to the specified size in user space units, not | 444 * Sets the font size to the specified size in user space units, not |
445 * in points. | 445 * in points. |
446 */ | 446 */ |
447 void fontSize(real size) { | 447 void fontSize(double size) { |
448 assert(size != 0); | 448 assert(size != 0); |
449 cairo_set_font_size(cr, size); | 449 cairo_set_font_size(cr, size); |
450 } | 450 } |
451 /** | 451 /** |
452 * Set the font used to draw text. | 452 * Set the font used to draw text. |
455 assert(f.size != 0); | 455 assert(f.size != 0); |
456 cairo_set_font_size(cr, f.size); | 456 cairo_set_font_size(cr, f.size); |
457 cairo_select_font_face(cr, toCharPtr(f.family), f.italic, f.bold); | 457 cairo_select_font_face(cr, toCharPtr(f.family), f.italic, f.bold); |
458 } | 458 } |
459 // TODO: if text is all ascii, do fast path with no uniscribe | 459 // TODO: if text is all ascii, do fast path with no uniscribe |
460 void drawText(string text, real x, real y) { | 460 void drawText(string text, double x, double y) { |
461 auto extents = getFontExtents; | 461 auto extents = getFontExtents; |
462 cairo_font_extents_t fextents; | 462 cairo_font_extents_t fextents; |
463 cairo_font_extents(cr, &fextents); | 463 cairo_font_extents(cr, &fextents); |
464 cairo_move_to(cr, x, y + fextents.ascent); | 464 cairo_move_to(cr, x, y + fextents.ascent); |
465 cairo_show_text(cr, toCharPtr(text)); // 99% of time spent in show_text | 465 cairo_show_text(cr, toCharPtr(text)); // 99% of time spent in show_text |
506 /// | 506 /// |
507 void translate(Point pt) { | 507 void translate(Point pt) { |
508 translate(pt.x, pt.y); | 508 translate(pt.x, pt.y); |
509 } | 509 } |
510 /// ditto | 510 /// ditto |
511 void translate(real x, real y) { | 511 void translate(double x, double y) { |
512 cairo_translate(cr, x, y); | 512 cairo_translate(cr, x, y); |
513 } | 513 } |
514 /// | 514 /// |
515 void scale(real x, real y) { | 515 void scale(double x, double y) { |
516 cairo_scale(cr, x, y); | 516 cairo_scale(cr, x, y); |
517 } | 517 } |
518 /// | 518 /// |
519 void rotate(real angle) { | 519 void rotate(double angle) { |
520 cairo_rotate(cr, angle); | 520 cairo_rotate(cr, angle); |
521 } | 521 } |
522 /// | 522 /// |
523 GraphicsOperator operator() { | 523 GraphicsOperator operator() { |
524 return cast(GraphicsOperator)cairo_get_operator(cr); | 524 return cast(GraphicsOperator)cairo_get_operator(cr); |
528 cairo_set_operator(cr, cast(cairo_operator_t)op); | 528 cairo_set_operator(cr, cast(cairo_operator_t)op); |
529 } | 529 } |
530 /** | 530 /** |
531 * Sets the dash pattern to be used when lines are drawn. | 531 * Sets the dash pattern to be used when lines are drawn. |
532 */ | 532 */ |
533 void setDash(real[] dashes, real offset) { | 533 void setDash(double[] dashes, double offset) { |
534 auto cdashes = new double[dashes.length]; | 534 cairo_set_dash(cr, dashes.ptr, dashes.length, offset); |
535 foreach(int i, real r; dashes) | |
536 cdashes[i] = r; | |
537 cairo_set_dash(cr, cdashes.ptr, cdashes.length, offset); | |
538 } | 535 } |
539 /** | 536 /** |
540 * Gets or sets the fill rule the current fill rule. | 537 * Gets or sets the fill rule the current fill rule. |
541 * The default is GraphicsFillRule.Winding. | 538 * The default is GraphicsFillRule.Winding. |
542 */ | 539 */ |
568 // TODO: figure out the best way to set the source and get the source | 565 // TODO: figure out the best way to set the source and get the source |
569 void source(Color c) { | 566 void source(Color c) { |
570 cairo_set_source_rgba(cr, c.R/255.0, c.G/255.0, c.B/255.0, c.A/255.0); | 567 cairo_set_source_rgba(cr, c.R/255.0, c.G/255.0, c.B/255.0, c.A/255.0); |
571 } | 568 } |
572 //void source(Pattern s) {} | 569 //void source(Pattern s) {} |
573 //void setSource(Surface s, real x = 0, real y = 0) {} | 570 //void setSource(Surface s, double x = 0, double y = 0) {} |
574 // TODO: remove this function and have users do: | 571 // TODO: remove this function and have users do: |
575 // g.setSource(img, x, y); | 572 // g.setSource(img, x, y); |
576 // g.paint(); | 573 // g.paint(); |
577 // ??? | 574 // ??? |
578 // paintSource(Image, real, real) ? | 575 // paintSource(Image, double, double) ? |
579 /// Draws the specified image unscaled. | 576 /// Draws the specified image unscaled. |
580 void drawImage(Image image, real x, real y) { | 577 void drawImage(Image image, double x, double y) { |
581 auto surface= cairo_image_surface_create_for_data(cast(char*)image.data, | 578 auto surface= cairo_image_surface_create_for_data(cast(char*)image.data, |
582 CAIRO_FORMAT_ARGB32, image.width, image.height, image.width*4); | 579 CAIRO_FORMAT_ARGB32, image.width, image.height, image.width*4); |
583 save(); | 580 save(); |
584 cairo_set_source_surface(cr, surface, x, y); | 581 cairo_set_source_surface(cr, surface, x, y); |
585 cairo_paint(cr); | 582 cairo_paint(cr); |
586 restore(); | 583 restore(); |
587 cairo_surface_destroy(surface); | 584 cairo_surface_destroy(surface); |
588 } | 585 } |
589 // Draws the specified image scaled to the specified width and height. | 586 // Draws the specified image scaled to the specified width and height. |
590 //void drawImage(Image image, real x, real y, real width, real height); | 587 //void drawImage(Image image, double x, double y, double width, double height); |
591 } | 588 } |