drawing.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * Module dependencies.
  3. */
  4. var escape = require('./utils').escape;
  5. /**
  6. * Extend proto.
  7. */
  8. module.exports = function (proto) {
  9. // http://www.graphicsmagick.org/GraphicsMagick.html#details-fill
  10. proto.fill = function fill (color) {
  11. return this.out("-fill", color || "none");
  12. }
  13. // http://www.graphicsmagick.org/GraphicsMagick.html#details-stroke
  14. proto.stroke = function stroke (color, width) {
  15. if (width) {
  16. this.strokeWidth(width);
  17. }
  18. return this.out("-stroke", color || "none");
  19. }
  20. // http://www.graphicsmagick.org/GraphicsMagick.html#details-strokewidth
  21. proto.strokeWidth = function strokeWidth (width) {
  22. return this.out("-strokewidth", width);
  23. }
  24. // http://www.graphicsmagick.org/GraphicsMagick.html#details-font
  25. proto.font = function font (font, size) {
  26. if (size) {
  27. this.fontSize(size);
  28. }
  29. return this.out("-font", font);
  30. }
  31. // http://www.graphicsmagick.org/GraphicsMagick.html
  32. proto.fontSize = function fontSize (size) {
  33. return this.out("-pointsize", size);
  34. }
  35. // http://www.graphicsmagick.org/GraphicsMagick.html#details-draw
  36. proto.draw = function draw (args) {
  37. return this.out("-draw", [].slice.call(arguments).join(" "));
  38. }
  39. // http://www.graphicsmagick.org/GraphicsMagick.html#details-draw
  40. proto.drawPoint = function drawPoint (x, y) {
  41. return this.draw("point", x +","+ y);
  42. }
  43. // http://www.graphicsmagick.org/GraphicsMagick.html#details-draw
  44. proto.drawLine = function drawLine (x0, y0, x1, y1) {
  45. return this.draw("line", x0+","+y0, x1+","+y1);
  46. }
  47. // http://www.graphicsmagick.org/GraphicsMagick.html#details-draw
  48. proto.drawRectangle = function drawRectangle (x0, y0, x1, y1, wc, hc) {
  49. var shape = "rectangle"
  50. , lastarg;
  51. if ("undefined" !== typeof wc) {
  52. shape = "roundRectangle";
  53. if ("undefined" === typeof hc) {
  54. hc = wc;
  55. }
  56. lastarg = wc+","+hc;
  57. }
  58. return this.draw(shape, x0+","+y0, x1+","+y1, lastarg);
  59. }
  60. // http://www.graphicsmagick.org/GraphicsMagick.html#details-draw
  61. proto.drawArc = function drawArc (x0, y0, x1, y1, a0, a1) {
  62. return this.draw("arc", x0+","+y0, x1+","+y1, a0+","+a1);
  63. }
  64. // http://www.graphicsmagick.org/GraphicsMagick.html#details-draw
  65. proto.drawEllipse = function drawEllipse (x0, y0, rx, ry, a0, a1) {
  66. if (a0 == undefined) a0 = 0;
  67. if (a1 == undefined) a1 = 360;
  68. return this.draw("ellipse", x0+","+y0, rx+","+ry, a0+","+a1);
  69. }
  70. // http://www.graphicsmagick.org/GraphicsMagick.html#details-draw
  71. proto.drawCircle = function drawCircle (x0, y0, x1, y1) {
  72. return this.draw("circle", x0+","+y0, x1+","+y1);
  73. }
  74. // http://www.graphicsmagick.org/GraphicsMagick.html#details-draw
  75. proto.drawPolyline = function drawPolyline () {
  76. return this.draw("polyline", formatPoints(arguments));
  77. }
  78. // http://www.graphicsmagick.org/GraphicsMagick.html#details-draw
  79. proto.drawPolygon = function drawPolygon () {
  80. return this.draw("polygon", formatPoints(arguments));
  81. }
  82. // http://www.graphicsmagick.org/GraphicsMagick.html#details-draw
  83. proto.drawBezier = function drawBezier () {
  84. return this.draw("bezier", formatPoints(arguments));
  85. }
  86. proto._gravities = [
  87. "northwest"
  88. , "north"
  89. , "northeast"
  90. , "west"
  91. , "center"
  92. , "east"
  93. , "southwest"
  94. , "south"
  95. , "southeast"];
  96. // http://www.graphicsmagick.org/GraphicsMagick.html#details-draw
  97. proto.drawText = function drawText (x0, y0, text, gravity) {
  98. var gravity = String(gravity || "").toLowerCase()
  99. , arg = ["text " + x0 + "," + y0 + " " + escape(text)];
  100. if (~this._gravities.indexOf(gravity)) {
  101. arg.unshift("gravity", gravity);
  102. }
  103. return this.draw.apply(this, arg);
  104. }
  105. proto._drawProps = ["color", "matte"];
  106. // http://www.graphicsmagick.org/GraphicsMagick.html#details-draw
  107. proto.setDraw = function setDraw (prop, x, y, method) {
  108. prop = String(prop || "").toLowerCase();
  109. if (!~this._drawProps.indexOf(prop)) {
  110. return this;
  111. }
  112. return this.draw(prop, x+","+y, method);
  113. }
  114. }
  115. function formatPoints (points) {
  116. var len = points.length
  117. , result = []
  118. , i = 0;
  119. for (; i < len; ++i) {
  120. result.push(points[i].join(","));
  121. }
  122. return result;
  123. }