Subversion Repositories Applications.papyrus

Rev

Rev 1372 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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