1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | var escape = require('./utils').escape;
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | module.exports = function (proto) {
|
13 |
|
14 |
|
15 | proto.fill = function fill (color) {
|
16 | return this.out("-fill", color || "none");
|
17 | }
|
18 |
|
19 |
|
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 |
|
29 | proto.strokeWidth = function strokeWidth (width) {
|
30 | return this.out("-strokewidth", width);
|
31 | }
|
32 |
|
33 |
|
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 |
|
43 | proto.fontSize = function fontSize (size) {
|
44 | return this.out("-pointsize", size);
|
45 | }
|
46 |
|
47 |
|
48 | proto.draw = function draw (args) {
|
49 | return this.out("-draw", [].slice.call(arguments).join(" "));
|
50 | }
|
51 |
|
52 |
|
53 | proto.drawPoint = function drawPoint (x, y) {
|
54 | return this.draw("point", x +","+ y);
|
55 | }
|
56 |
|
57 |
|
58 | proto.drawLine = function drawLine (x0, y0, x1, y1) {
|
59 | return this.draw("line", x0+","+y0, x1+","+y1);
|
60 | }
|
61 |
|
62 |
|
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 |
|
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 |
|
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 |
|
93 | proto.drawCircle = function drawCircle (x0, y0, x1, y1) {
|
94 | return this.draw("circle", x0+","+y0, x1+","+y1);
|
95 | }
|
96 |
|
97 |
|
98 | proto.drawPolyline = function drawPolyline () {
|
99 | return this.draw("polyline", formatPoints(arguments));
|
100 | }
|
101 |
|
102 |
|
103 | proto.drawPolygon = function drawPolygon () {
|
104 | return this.draw("polygon", formatPoints(arguments));
|
105 | }
|
106 |
|
107 |
|
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 |
|
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 |
|
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 |
|
150 | function 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 | }
|