UNPKG

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