Subversion Repositories Applications.papyrus

Rev

Rev 1371 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1318 alexandre_ 1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
4
 
5
	Licensed under the Academic Free License version 2.1 or above OR the
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
10
 
11
dojo.provide("dojo.gfx.path");
12
dojo.require("dojo.math");
13
dojo.require("dojo.gfx.shape");
14
dojo.declare("dojo.gfx.path.Path", dojo.gfx.Shape, {initializer:function (rawNode) {
15
	this.shape = dojo.lang.shallowCopy(dojo.gfx.defaultPath, true);
16
	this.segments = [];
17
	this.absolute = true;
18
	this.last = {};
19
	this.attach(rawNode);
20
}, setAbsoluteMode:function (mode) {
21
	this.absolute = typeof (mode) == "string" ? (mode == "absolute") : mode;
22
	return this;
23
}, getAbsoluteMode:function () {
24
	return this.absolute;
25
}, getBoundingBox:function () {
26
	return "l" in this.bbox ? {x:this.bbox.l, y:this.bbox.t, width:this.bbox.r - this.bbox.l, height:this.bbox.b - this.bbox.t} : null;
27
}, getLastPosition:function () {
28
	return "x" in this.last ? this.last : null;
29
}, _updateBBox:function (x, y) {
30
	if ("l" in this.bbox) {
31
		if (this.bbox.l > x) {
32
			this.bbox.l = x;
33
		}
34
		if (this.bbox.r < x) {
35
			this.bbox.r = x;
36
		}
37
		if (this.bbox.t > y) {
38
			this.bbox.t = y;
39
		}
40
		if (this.bbox.b < y) {
41
			this.bbox.b = y;
42
		}
43
	} else {
44
		this.bbox = {l:x, b:y, r:x, t:y};
45
	}
46
}, _updateWithSegment:function (segment) {
47
	var n = segment.args;
48
	var l = n.length;
49
	switch (segment.action) {
50
	  case "M":
51
	  case "L":
52
	  case "C":
53
	  case "S":
54
	  case "Q":
55
	  case "T":
56
		for (var i = 0; i < l; i += 2) {
57
			this._updateBBox(this.bbox, n[i], n[i + 1]);
58
		}
59
		this.last.x = n[l - 2];
60
		this.last.y = n[l - 1];
61
		this.absolute = true;
62
		break;
63
	  case "H":
64
		for (var i = 0; i < l; ++i) {
65
			this._updateBBox(this.bbox, n[i], this.last.y);
66
		}
67
		this.last.x = n[l - 1];
68
		this.absolute = true;
69
		break;
70
	  case "V":
71
		for (var i = 0; i < l; ++i) {
72
			this._updateBBox(this.bbox, this.last.x, n[i]);
73
		}
74
		this.last.y = n[l - 1];
75
		this.absolute = true;
76
		break;
77
	  case "m":
78
		var start = 0;
79
		if (!("x" in this.last)) {
80
			this._updateBBox(this.bbox, this.last.x = n[0], this.last.y = n[1]);
81
			start = 2;
82
		}
83
		for (var i = start; i < l; i += 2) {
84
			this._updateBBox(this.bbox, this.last.x += n[i], this.last.y += n[i + 1]);
85
		}
86
		this.absolute = false;
87
		break;
88
	  case "l":
89
	  case "t":
90
		for (var i = 0; i < l; i += 2) {
91
			this._updateBBox(this.bbox, this.last.x += n[i], this.last.y += n[i + 1]);
92
		}
93
		this.absolute = false;
94
		break;
95
	  case "h":
96
		for (var i = 0; i < l; ++i) {
97
			this._updateBBox(this.bbox, this.last.x += n[i], this.last.y);
98
		}
99
		this.absolute = false;
100
		break;
101
	  case "v":
102
		for (var i = 0; i < l; ++i) {
103
			this._updateBBox(this.bbox, this.last.x, this.last.y += n[i]);
104
		}
105
		this.absolute = false;
106
		break;
107
	  case "c":
108
		for (var i = 0; i < l; i += 6) {
109
			this._updateBBox(this.bbox, this.last.x + n[i], this.last.y + n[i + 1]);
110
			this._updateBBox(this.bbox, this.last.x + n[i + 2], this.last.y + n[i + 3]);
111
			this._updateBBox(this.bbox, this.last.x += n[i + 4], this.last.y += n[i + 5]);
112
		}
113
		this.absolute = false;
114
		break;
115
	  case "s":
116
	  case "q":
117
		for (var i = 0; i < l; i += 4) {
118
			this._updateBBox(this.bbox, this.last.x + n[i], this.last.y + n[i + 1]);
119
			this._updateBBox(this.bbox, this.last.x += n[i + 2], this.last.y += n[i + 3]);
120
		}
121
		this.absolute = false;
122
		break;
123
	  case "A":
124
		for (var i = 0; i < l; i += 7) {
125
			this._updateBBox(this.bbox, n[i + 5], n[i + 6]);
126
		}
127
		this.last.x = n[l - 2];
128
		this.last.y = n[l - 1];
129
		this.absolute = true;
130
		break;
131
	  case "a":
132
		for (var i = 0; i < l; i += 7) {
133
			this._updateBBox(this.bbox, this.last.x += n[i + 5], this.last.y += n[i + 6]);
134
		}
135
		this.absolute = false;
136
		break;
137
	}
138
	var path = [segment.action];
139
	for (var i = 0; i < l; ++i) {
140
		path.push(dojo.gfx.formatNumber(n[i], true));
141
	}
142
	if (typeof (this.shape.path) == "string") {
143
		this.shape.path += path.join("");
144
	} else {
145
		this.shape.path = this.shape.path.concat(path);
146
	}
147
}, _validSegments:{m:2, l:2, h:1, v:1, c:6, s:4, q:4, t:2, a:7, z:0}, _pushSegment:function (action, args) {
148
	var group = this._validSegments[action.toLowerCase()];
149
	if (typeof (group) == "number") {
150
		if (group) {
151
			if (args.length >= group) {
152
				var segment = {action:action, args:args.slice(0, args.length - args.length % group)};
153
				this.segments.push(segment);
154
				this._updateWithSegment(segment);
155
			}
156
		} else {
157
			var segment = {action:action, args:[]};
158
			this.segments.push(segment);
159
			this._updateWithSegment(segment);
160
		}
161
	}
162
}, _collectArgs:function (array, args) {
163
	for (var i = 0; i < args.length; ++i) {
164
		var t = args[i];
165
		if (typeof (t) == "boolean") {
166
			array.push(t ? 1 : 0);
167
		} else {
168
			if (typeof (t) == "number") {
169
				array.push(t);
170
			} else {
171
				if (t instanceof Array) {
172
					this._collectArgs(array, t);
173
				} else {
174
					if ("x" in t && "y" in t) {
175
						array.push(t.x);
176
						array.push(t.y);
177
					}
178
				}
179
			}
180
		}
181
	}
182
}, moveTo:function () {
183
	var args = [];
184
	this._collectArgs(args, arguments);
185
	this._pushSegment(this.absolute ? "M" : "m", args);
186
	return this;
187
}, lineTo:function () {
188
	var args = [];
189
	this._collectArgs(args, arguments);
190
	this._pushSegment(this.absolute ? "L" : "l", args);
191
	return this;
192
}, hLineTo:function () {
193
	var args = [];
194
	this._collectArgs(args, arguments);
195
	this._pushSegment(this.absolute ? "H" : "h", args);
196
	return this;
197
}, vLineTo:function () {
198
	var args = [];
199
	this._collectArgs(args, arguments);
200
	this._pushSegment(this.absolute ? "V" : "v", args);
201
	return this;
202
}, curveTo:function () {
203
	var args = [];
204
	this._collectArgs(args, arguments);
205
	this._pushSegment(this.absolute ? "C" : "c", args);
206
	return this;
207
}, smoothCurveTo:function () {
208
	var args = [];
209
	this._collectArgs(args, arguments);
210
	this._pushSegment(this.absolute ? "S" : "s", args);
211
	return this;
212
}, qCurveTo:function () {
213
	var args = [];
214
	this._collectArgs(args, arguments);
215
	this._pushSegment(this.absolute ? "Q" : "q", args);
216
	return this;
217
}, qSmoothCurveTo:function () {
218
	var args = [];
219
	this._collectArgs(args, arguments);
220
	this._pushSegment(this.absolute ? "T" : "t", args);
221
	return this;
222
}, arcTo:function () {
223
	var args = [];
224
	this._collectArgs(args, arguments);
225
	for (var i = 2; i < args.length; i += 7) {
226
		args[i] = -args[i];
227
	}
228
	this._pushSegment(this.absolute ? "A" : "a", args);
229
	return this;
230
}, closePath:function () {
231
	this._pushSegment("Z", []);
232
	return this;
233
}, _setPath:function (path) {
234
	var p = path.match(dojo.gfx.pathRegExp);
235
	this.segments = [];
236
	this.absolute = true;
237
	this.bbox = {};
238
	this.last = {};
239
	if (!p) {
240
		return;
241
	}
242
	var action = "";
243
	var args = [];
244
	for (var i = 0; i < p.length; ++i) {
245
		var t = p[i];
246
		var x = parseFloat(t);
247
		if (isNaN(x)) {
248
			if (action) {
249
				this._pushSegment(action, args);
250
			}
251
			args = [];
252
			action = t;
253
		} else {
254
			args.push(x);
255
		}
256
	}
257
	this._pushSegment(action, args);
258
}, setShape:function (newShape) {
259
	this.shape = dojo.gfx.makeParameters(this.shape, typeof (newShape) == "string" ? {path:newShape} : newShape);
260
	var path = this.shape.path;
261
	this.shape.path = [];
262
	this._setPath(path);
263
	this.shape.path = this.shape.path.join("");
264
	return this;
265
}, _2PI:Math.PI * 2});
266