root / trunk / web / dojo / dojox / gfx / path.js @ 13
History | View | Annotate | Download (7.35 KB)
1 | 9 | andrej.cim | /*
|
---|---|---|---|
2 | Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
||
3 | Available via Academic Free License >= 2.1 OR the modified BSD license.
|
||
4 | see: http://dojotoolkit.org/license for details
|
||
5 | */
|
||
6 | |||
7 | |||
8 | if(!dojo._hasResource["dojox.gfx.path"]){ |
||
9 | dojo._hasResource["dojox.gfx.path"]=true; |
||
10 | dojo.provide("dojox.gfx.path");
|
||
11 | dojo.require("dojox.gfx.matrix");
|
||
12 | dojo.require("dojox.gfx.shape");
|
||
13 | dojo.declare("dojox.gfx.path.Path",dojox.gfx.Shape,{constructor:function(_1){ |
||
14 | this.shape=dojo.clone(dojox.gfx.defaultPath);
|
||
15 | this.segments=[];
|
||
16 | this.tbbox=null; |
||
17 | this.absolute=true; |
||
18 | this.last={};
|
||
19 | this.rawNode=_1;
|
||
20 | this.segmented=false; |
||
21 | },setAbsoluteMode:function(_2){ |
||
22 | this._confirmSegmented();
|
||
23 | this.absolute=typeof _2=="string"?(_2=="absolute"):_2; |
||
24 | return this; |
||
25 | },getAbsoluteMode:function(){ |
||
26 | this._confirmSegmented();
|
||
27 | return this.absolute; |
||
28 | },getBoundingBox:function(){ |
||
29 | this._confirmSegmented();
|
||
30 | return (this.bbox&&("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; |
||
31 | },_getRealBBox:function(){ |
||
32 | this._confirmSegmented();
|
||
33 | if(this.tbbox){ |
||
34 | return this.tbbox; |
||
35 | } |
||
36 | var _3=this.bbox,_4=this._getRealMatrix(); |
||
37 | this.bbox=null; |
||
38 | for(var i=0,_5=this.segments.length;i<_5;++i){ |
||
39 | this._updateWithSegment(this.segments[i],_4); |
||
40 | } |
||
41 | var t=this.bbox; |
||
42 | this.bbox=_3;
|
||
43 | this.tbbox=t?[{x:t.l,y:t.t},{x:t.r,y:t.t},{x:t.r,y:t.b},{x:t.l,y:t.b}]:null; |
||
44 | return this.tbbox; |
||
45 | },getLastPosition:function(){ |
||
46 | this._confirmSegmented();
|
||
47 | return "x" in this.last?this.last:null; |
||
48 | },_applyTransform:function(){ |
||
49 | this.tbbox=null; |
||
50 | return dojox.gfx.Shape.prototype._applyTransform.call(this); |
||
51 | },_updateBBox:function(x,y,_6){ |
||
52 | if(_6){
|
||
53 | var t=dojox.gfx.matrix.multiplyPoint(_6,x,y);
|
||
54 | x=t.x; |
||
55 | y=t.y; |
||
56 | } |
||
57 | if(this.bbox&&("l" in this.bbox)){ |
||
58 | if(this.bbox.l>x){ |
||
59 | this.bbox.l=x;
|
||
60 | } |
||
61 | if(this.bbox.r<x){ |
||
62 | this.bbox.r=x;
|
||
63 | } |
||
64 | if(this.bbox.t>y){ |
||
65 | this.bbox.t=y;
|
||
66 | } |
||
67 | if(this.bbox.b<y){ |
||
68 | this.bbox.b=y;
|
||
69 | } |
||
70 | }else{
|
||
71 | this.bbox={l:x,b:y,r:x,t:y}; |
||
72 | } |
||
73 | },_updateWithSegment:function(_7,_8){ |
||
74 | var n=_7.args,l=n.length;
|
||
75 | switch(_7.action){
|
||
76 | case "M": |
||
77 | case "L": |
||
78 | case "C": |
||
79 | case "S": |
||
80 | case "Q": |
||
81 | case "T": |
||
82 | for(var i=0;i<l;i+=2){ |
||
83 | this._updateBBox(n[i],n[i+1],_8); |
||
84 | } |
||
85 | this.last.x=n[l-2]; |
||
86 | this.last.y=n[l-1]; |
||
87 | this.absolute=true; |
||
88 | break;
|
||
89 | case "H": |
||
90 | for(var i=0;i<l;++i){ |
||
91 | this._updateBBox(n[i],this.last.y,_8); |
||
92 | } |
||
93 | this.last.x=n[l-1]; |
||
94 | this.absolute=true; |
||
95 | break;
|
||
96 | case "V": |
||
97 | for(var i=0;i<l;++i){ |
||
98 | this._updateBBox(this.last.x,n[i],_8); |
||
99 | } |
||
100 | this.last.y=n[l-1]; |
||
101 | this.absolute=true; |
||
102 | break;
|
||
103 | case "m": |
||
104 | var _9=0; |
||
105 | if(!("x" in this.last)){ |
||
106 | this._updateBBox(this.last.x=n[0],this.last.y=n[1],_8); |
||
107 | _9=2;
|
||
108 | } |
||
109 | for(var i=_9;i<l;i+=2){ |
||
110 | this._updateBBox(this.last.x+=n[i],this.last.y+=n[i+1],_8); |
||
111 | } |
||
112 | this.absolute=false; |
||
113 | break;
|
||
114 | case "l": |
||
115 | case "t": |
||
116 | for(var i=0;i<l;i+=2){ |
||
117 | this._updateBBox(this.last.x+=n[i],this.last.y+=n[i+1],_8); |
||
118 | } |
||
119 | this.absolute=false; |
||
120 | break;
|
||
121 | case "h": |
||
122 | for(var i=0;i<l;++i){ |
||
123 | this._updateBBox(this.last.x+=n[i],this.last.y,_8); |
||
124 | } |
||
125 | this.absolute=false; |
||
126 | break;
|
||
127 | case "v": |
||
128 | for(var i=0;i<l;++i){ |
||
129 | this._updateBBox(this.last.x,this.last.y+=n[i],_8); |
||
130 | } |
||
131 | this.absolute=false; |
||
132 | break;
|
||
133 | case "c": |
||
134 | for(var i=0;i<l;i+=6){ |
||
135 | this._updateBBox(this.last.x+n[i],this.last.y+n[i+1],_8); |
||
136 | this._updateBBox(this.last.x+n[i+2],this.last.y+n[i+3],_8); |
||
137 | this._updateBBox(this.last.x+=n[i+4],this.last.y+=n[i+5],_8); |
||
138 | } |
||
139 | this.absolute=false; |
||
140 | break;
|
||
141 | case "s": |
||
142 | case "q": |
||
143 | for(var i=0;i<l;i+=4){ |
||
144 | this._updateBBox(this.last.x+n[i],this.last.y+n[i+1],_8); |
||
145 | this._updateBBox(this.last.x+=n[i+2],this.last.y+=n[i+3],_8); |
||
146 | } |
||
147 | this.absolute=false; |
||
148 | break;
|
||
149 | case "A": |
||
150 | for(var i=0;i<l;i+=7){ |
||
151 | this._updateBBox(n[i+5],n[i+6],_8); |
||
152 | } |
||
153 | this.last.x=n[l-2]; |
||
154 | this.last.y=n[l-1]; |
||
155 | this.absolute=true; |
||
156 | break;
|
||
157 | case "a": |
||
158 | for(var i=0;i<l;i+=7){ |
||
159 | this._updateBBox(this.last.x+=n[i+5],this.last.y+=n[i+6],_8); |
||
160 | } |
||
161 | this.absolute=false; |
||
162 | break;
|
||
163 | } |
||
164 | var _a=[_7.action];
|
||
165 | for(var i=0;i<l;++i){ |
||
166 | _a.push(dojox.gfx.formatNumber(n[i],true));
|
||
167 | } |
||
168 | if(typeof this.shape.path=="string"){ |
||
169 | this.shape.path+=_a.join(""); |
||
170 | }else{
|
||
171 | Array.prototype.push.apply(this.shape.path,_a);
|
||
172 | } |
||
173 | },_validSegments:{m:2,l:2,h:1,v:1,c:6,s:4,q:4,t:2,a:7,z:0},_pushSegment:function(_b,_c){ |
||
174 | this.tbbox=null; |
||
175 | var _d=this._validSegments[_b.toLowerCase()]; |
||
176 | if(typeof _d=="number"){ |
||
177 | if(_d){
|
||
178 | if(_c.length>=_d){
|
||
179 | var _e={action:_b,args:_c.slice(0,_c.length-_c.length%_d)}; |
||
180 | this.segments.push(_e);
|
||
181 | this._updateWithSegment(_e);
|
||
182 | } |
||
183 | }else{
|
||
184 | var _e={action:_b,args:[]}; |
||
185 | this.segments.push(_e);
|
||
186 | this._updateWithSegment(_e);
|
||
187 | } |
||
188 | } |
||
189 | },_collectArgs:function(_f,_10){ |
||
190 | for(var i=0;i<_10.length;++i){ |
||
191 | var t=_10[i];
|
||
192 | if(typeof t=="boolean"){ |
||
193 | _f.push(t?1:0); |
||
194 | }else{
|
||
195 | if(typeof t=="number"){ |
||
196 | _f.push(t); |
||
197 | }else{
|
||
198 | if(t instanceof Array){ |
||
199 | this._collectArgs(_f,t);
|
||
200 | }else{
|
||
201 | if("x" in t&&"y" in t){ |
||
202 | _f.push(t.x,t.y); |
||
203 | } |
||
204 | } |
||
205 | } |
||
206 | } |
||
207 | } |
||
208 | },moveTo:function(){ |
||
209 | this._confirmSegmented();
|
||
210 | var _11=[];
|
||
211 | this._collectArgs(_11,arguments); |
||
212 | this._pushSegment(this.absolute?"M":"m",_11); |
||
213 | return this; |
||
214 | },lineTo:function(){ |
||
215 | this._confirmSegmented();
|
||
216 | var _12=[];
|
||
217 | this._collectArgs(_12,arguments); |
||
218 | this._pushSegment(this.absolute?"L":"l",_12); |
||
219 | return this; |
||
220 | },hLineTo:function(){ |
||
221 | this._confirmSegmented();
|
||
222 | var _13=[];
|
||
223 | this._collectArgs(_13,arguments); |
||
224 | this._pushSegment(this.absolute?"H":"h",_13); |
||
225 | return this; |
||
226 | },vLineTo:function(){ |
||
227 | this._confirmSegmented();
|
||
228 | var _14=[];
|
||
229 | this._collectArgs(_14,arguments); |
||
230 | this._pushSegment(this.absolute?"V":"v",_14); |
||
231 | return this; |
||
232 | },curveTo:function(){ |
||
233 | this._confirmSegmented();
|
||
234 | var _15=[];
|
||
235 | this._collectArgs(_15,arguments); |
||
236 | this._pushSegment(this.absolute?"C":"c",_15); |
||
237 | return this; |
||
238 | },smoothCurveTo:function(){ |
||
239 | this._confirmSegmented();
|
||
240 | var _16=[];
|
||
241 | this._collectArgs(_16,arguments); |
||
242 | this._pushSegment(this.absolute?"S":"s",_16); |
||
243 | return this; |
||
244 | },qCurveTo:function(){ |
||
245 | this._confirmSegmented();
|
||
246 | var _17=[];
|
||
247 | this._collectArgs(_17,arguments); |
||
248 | this._pushSegment(this.absolute?"Q":"q",_17); |
||
249 | return this; |
||
250 | },qSmoothCurveTo:function(){ |
||
251 | this._confirmSegmented();
|
||
252 | var _18=[];
|
||
253 | this._collectArgs(_18,arguments); |
||
254 | this._pushSegment(this.absolute?"T":"t",_18); |
||
255 | return this; |
||
256 | },arcTo:function(){ |
||
257 | this._confirmSegmented();
|
||
258 | var _19=[];
|
||
259 | this._collectArgs(_19,arguments); |
||
260 | this._pushSegment(this.absolute?"A":"a",_19); |
||
261 | return this; |
||
262 | },closePath:function(){ |
||
263 | this._confirmSegmented();
|
||
264 | this._pushSegment("Z",[]); |
||
265 | return this; |
||
266 | },_confirmSegmented:function(){ |
||
267 | if(!this.segmented){ |
||
268 | var _1a=this.shape.path; |
||
269 | this.shape.path=[];
|
||
270 | this._setPath(_1a);
|
||
271 | this.shape.path=this.shape.path.join(""); |
||
272 | this.segmented=true; |
||
273 | } |
||
274 | },_setPath:function(_1b){ |
||
275 | var p=dojo.isArray(_1b)?_1b:_1b.match(dojox.gfx.pathSvgRegExp);
|
||
276 | this.segments=[];
|
||
277 | this.absolute=true; |
||
278 | this.bbox={};
|
||
279 | this.last={};
|
||
280 | if(!p){
|
||
281 | return;
|
||
282 | } |
||
283 | var _1c="",_1d=[],l=p.length; |
||
284 | for(var i=0;i<l;++i){ |
||
285 | var t=p[i],x=parseFloat(t);
|
||
286 | if(isNaN(x)){
|
||
287 | if(_1c){
|
||
288 | this._pushSegment(_1c,_1d);
|
||
289 | } |
||
290 | _1d=[]; |
||
291 | _1c=t; |
||
292 | }else{
|
||
293 | _1d.push(x); |
||
294 | } |
||
295 | } |
||
296 | this._pushSegment(_1c,_1d);
|
||
297 | },setShape:function(_1e){ |
||
298 | dojox.gfx.Shape.prototype.setShape.call(this,typeof _1e=="string"?{path:_1e}:_1e); |
||
299 | this.segmented=false; |
||
300 | this.segments=[];
|
||
301 | if(!dojox.gfx.lazyPathSegmentation){
|
||
302 | this._confirmSegmented();
|
||
303 | } |
||
304 | return this; |
||
305 | },_2PI:Math.PI*2}); |
||
306 | dojo.declare("dojox.gfx.path.TextPath",dojox.gfx.path.Path,{constructor:function(_1f){ |
||
307 | if(!("text" in this)){ |
||
308 | this.text=dojo.clone(dojox.gfx.defaultTextPath);
|
||
309 | } |
||
310 | if(!("fontStyle" in this)){ |
||
311 | this.fontStyle=dojo.clone(dojox.gfx.defaultFont);
|
||
312 | } |
||
313 | },getText:function(){ |
||
314 | return this.text; |
||
315 | },setText:function(_20){ |
||
316 | this.text=dojox.gfx.makeParameters(this.text,typeof _20=="string"?{text:_20}:_20); |
||
317 | this._setText();
|
||
318 | return this; |
||
319 | },getFont:function(){ |
||
320 | return this.fontStyle; |
||
321 | },setFont:function(_21){ |
||
322 | this.fontStyle=typeof _21=="string"?dojox.gfx.splitFontString(_21):dojox.gfx.makeParameters(dojox.gfx.defaultFont,_21); |
||
323 | this._setFont();
|
||
324 | return this; |
||
325 | }}); |
||
326 | } |