]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dojox/gfx/_base.js
add Dojo 1.1.1
[eow] / static / dojo-release-1.1.1 / dojox / gfx / _base.js
1 if(!dojo._hasResource["dojox.gfx._base"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojox.gfx._base"] = true;
3 dojo.provide("dojox.gfx._base");
4
5 (function(){
6         var g = dojox.gfx, b = g._base;
7         
8         // candidates for dojox.style (work on VML and SVG nodes)
9         g._hasClass = function(/*DomNode*/node, /*String*/classStr){
10                 //      summary:
11                 //              Returns whether or not the specified classes are a portion of the
12                 //              class list currently applied to the node. 
13                 // return (new RegExp('(^|\\s+)'+classStr+'(\\s+|$)')).test(node.className)     // Boolean
14                 return ((" "+node.getAttribute("className")+" ").indexOf(" "+classStr+" ") >= 0);  // Boolean
15         }
16         g._addClass = function(/*DomNode*/node, /*String*/classStr){
17                 //      summary:
18                 //              Adds the specified classes to the end of the class list on the
19                 //              passed node.
20                 var cls = node.getAttribute("className");
21                 if((" "+cls+" ").indexOf(" "+classStr+" ") < 0){
22                         node.setAttribute("className", cls + (cls ? ' ' : '') + classStr);
23                 }
24         }
25         g._removeClass = function(/*DomNode*/node, /*String*/classStr){
26                 //      summary: Removes classes from node.
27                 node.setAttribute("className", node.getAttribute("className").replace(new RegExp('(^|\\s+)'+classStr+'(\\s+|$)'), "$1$2"));
28         }
29         
30         
31         // candidate for dojox.html.metrics (dynamic font resize handler is not implemented here)
32         
33         //      derived from Morris John's emResized measurer
34         b._getFontMeasurements = function(){
35                 //      summary
36                 //      Returns an object that has pixel equivilents of standard font size values.
37                 var heights = {
38                         '1em':0, '1ex':0, '100%':0, '12pt':0, '16px':0, 'xx-small':0, 'x-small':0,
39                         'small':0, 'medium':0, 'large':0, 'x-large':0, 'xx-large':0
40                 };
41         
42                 if(dojo.isIE){
43                         //      we do a font-size fix if and only if one isn't applied already.
44                         //      NOTE: If someone set the fontSize on the HTML Element, this will kill it.
45                         dojo.doc.documentElement.style.fontSize="100%";
46                 }
47         
48                 //      set up the measuring node.
49                 var div=dojo.doc.createElement("div");
50                 div.style.position="absolute";
51                 div.style.left="-100px";
52                 div.style.top="0";
53                 div.style.width="30px";
54                 div.style.height="1000em";
55                 div.style.border="0";
56                 div.style.margin="0";
57                 div.style.padding="0";
58                 div.style.outline="0";
59                 div.style.lineHeight="1";
60                 div.style.overflow="hidden";
61                 dojo.body().appendChild(div);
62         
63                 //      do the measurements.
64                 for(var p in heights){
65                         div.style.fontSize = p;
66                         heights[p] = Math.round(div.offsetHeight * 12/16) * 16/12 / 1000;
67                 }
68                 
69                 dojo.body().removeChild(div);
70                 div = null;
71                 return heights;         //      object
72         };
73         
74         var fontMeasurements = null;
75         
76         b._getCachedFontMeasurements = function(recalculate){
77                 if(recalculate || !fontMeasurements){
78                         fontMeasurements = b._getFontMeasurements();
79                 }
80                 return fontMeasurements;
81         };
82         
83         // candidate for dojox.html.metrics
84         
85         var measuringNode = null, empty = {};
86         b._getTextBox = function(/* String */ text, /* Object */ style, /* String? */ className){
87                 var m;
88                 if(!measuringNode){
89                         m = measuringNode = dojo.doc.createElement("div");
90                         m.style.position = "absolute";
91                         m.style.left = "-10000px";
92                         m.style.top = "0";
93                         dojo.body().appendChild(m);
94                 }else{
95                         m = measuringNode;
96                 }
97                 // reset styles
98                 m.className = "";
99                 m.style.border = "0";
100                 m.style.margin = "0";
101                 m.style.padding = "0";
102                 m.style.outline = "0";
103                 // set new style
104                 if(arguments.length > 1 && style){
105                         for(var i in style){
106                                 if(i in empty){ continue; }
107                                 m.style[i] = style[i];
108                         }
109                 }
110                 // set classes
111                 if(arguments.length > 2 && className){
112                         m.className = className;
113                 }
114                 // take a measure
115                 m.innerHTML = text;
116                 return dojo.marginBox(m);
117         };
118         
119         // candidate for dojo.dom
120         
121         var uniqueId = 0;
122         b._getUniqueId = function(){
123                 // summary: returns a unique string for use with any DOM element
124                 var id;
125                 do{
126                         id = dojo._scopeName + "Unique" + (++uniqueId);
127                 }while(dojo.byId(id));
128                 return id;
129         };
130 })();
131
132 dojo.mixin(dojox.gfx, {
133         // summary: defines constants, prototypes, and utility functions
134         
135         // default shapes, which are used to fill in missing parameters
136         defaultPath:     {type: "path",     path: ""},
137         defaultPolyline: {type: "polyline", points: []},
138         defaultRect:     {type: "rect",     x: 0, y: 0, width: 100, height: 100, r: 0},
139         defaultEllipse:  {type: "ellipse",  cx: 0, cy: 0, rx: 200, ry: 100},
140         defaultCircle:   {type: "circle",   cx: 0, cy: 0, r: 100},
141         defaultLine:     {type: "line",     x1: 0, y1: 0, x2: 100, y2: 100},
142         defaultImage:    {type: "image",    x: 0, y: 0, width: 0, height: 0, src: ""},
143         defaultText:     {type: "text",     x: 0, y: 0, text: "",
144                 align: "start", decoration: "none", rotated: false, kerning: true },
145         defaultTextPath: {type: "textpath", text: "",
146                 align: "start", decoration: "none", rotated: false, kerning: true },
147
148         // default geometric attributes
149         defaultStroke: {type: "stroke", color: "black", style: "solid", width: 1, cap: "butt", join: 4},
150         defaultLinearGradient: {type: "linear", x1: 0, y1: 0, x2: 100, y2: 100, 
151                 colors: [{offset: 0, color: "black"}, {offset: 1, color: "white"}]},
152         defaultRadialGradient: {type: "radial", cx: 0, cy: 0, r: 100, 
153                 colors: [{offset: 0, color: "black"}, {offset: 1, color: "white"}]},
154         defaultPattern: {type: "pattern", x: 0, y: 0, width: 0, height: 0, src: ""},
155         defaultFont: {type: "font", style: "normal", variant: "normal", weight: "normal", 
156                 size: "10pt", family: "serif"},
157
158         normalizeColor: function(/*Color*/ color){
159                 // summary: converts any legal color representation to normalized dojo.Color object
160                 return (color instanceof dojo.Color) ? color : new dojo.Color(color); // dojo.Color
161         },
162         normalizeParameters: function(existed, update){
163                 // summary: updates an existing object with properties from an "update" object
164                 // existed: Object: the "target" object to be updated
165                 // update:  Object: the "update" object, whose properties will be used to update the existed object
166                 if(update){
167                         var empty = {};
168                         for(var x in existed){
169                                 if(x in update && !(x in empty)){
170                                         existed[x] = update[x];
171                                 }
172                         }
173                 }
174                 return existed; // Object
175         },
176         makeParameters: function(defaults, update){
177                 // summary: copies the original object, and all copied properties from the "update" object
178                 // defaults: Object: the object to be cloned before updating
179                 // update:   Object: the object, which properties are to be cloned during updating
180                 if(!update) return dojo.clone(defaults);
181                 var result = {};
182                 for(var i in defaults){
183                         if(!(i in result)){
184                                 result[i] = dojo.clone((i in update) ? update[i] : defaults[i]);
185                         }
186                 }
187                 return result; // Object
188         },
189         formatNumber: function(x, addSpace){
190                 // summary: converts a number to a string using a fixed notation
191                 // x:                   Number:         number to be converted
192                 // addSpace:    Boolean?:       if it is true, add a space before a positive number
193                 var val = x.toString();
194                 if(val.indexOf("e") >= 0){
195                         val = x.toFixed(4);
196                 }else{
197                         var point = val.indexOf(".");
198                         if(point >= 0 && val.length - point > 5){
199                                 val = x.toFixed(4);
200                         }
201                 }
202                 if(x < 0){
203                         return val; // String
204                 }
205                 return addSpace ? " " + val : val; // String
206         },
207         // font operations
208         makeFontString: function(font){
209                 // summary: converts a font object to a CSS font string
210                 // font:        Object: font object (see dojox.gfx.defaultFont)
211                 return font.style + " " + font.variant + " " + font.weight + " " + font.size + " " + font.family; // Object
212         },
213         splitFontString: function(str){
214                 // summary: converts a CSS font string to a font object
215                 // str:         String: a CSS font string
216                 var font = dojo.clone(dojox.gfx.defaultFont);
217                 var t = str.split(/\s+/);
218                 do{
219                         if(t.length < 5){ break; }
220                         font.style  = t[0];
221                         font.varian = t[1];
222                         font.weight = t[2];
223                         var i = t[3].indexOf("/");
224                         font.size = i < 0 ? t[3] : t[3].substring(0, i);
225                         var j = 4;
226                         if(i < 0){
227                                 if(t[4] == "/"){
228                                         j = 6;
229                                         break;
230                                 }
231                                 if(t[4].substr(0, 1) == "/"){
232                                         j = 5;
233                                         break;
234                                 }
235                         }
236                         if(j + 3 > t.length){ break; }
237                         font.size = t[j];
238                         font.family = t[j + 1];
239                 }while(false);
240                 return font;    // Object
241         },
242         // length operations
243         cm_in_pt: 72 / 2.54,    // Number: centimeters per inch
244         mm_in_pt: 7.2 / 2.54,   // Number: millimeters per inch
245         px_in_pt: function(){
246                 // summary: returns a number of pixels per point
247                 return dojox.gfx._base._getCachedFontMeasurements()["12pt"] / 12;       // Number
248         },
249         pt2px: function(len){
250                 // summary: converts points to pixels
251                 // len: Number: a value in points
252                 return len * dojox.gfx.px_in_pt();      // Number
253         },
254         px2pt: function(len){
255                 // summary: converts pixels to points
256                 // len: Number: a value in pixels
257                 return len / dojox.gfx.px_in_pt();      // Number
258         },
259         normalizedLength: function(len) {
260                 // summary: converts any length value to pixels
261                 // len: String: a length, e.g., "12pc"
262                 if(len.length == 0) return 0;
263                 if(len.length > 2){
264                         var px_in_pt = dojox.gfx.px_in_pt();
265                         var val = parseFloat(len);
266                         switch(len.slice(-2)){
267                                 case "px": return val;
268                                 case "pt": return val * px_in_pt;
269                                 case "in": return val * 72 * px_in_pt;
270                                 case "pc": return val * 12 * px_in_pt;
271                                 case "mm": return val / dojox.gfx.mm_in_pt * px_in_pt;
272                                 case "cm": return val / dojox.gfx.cm_in_pt * px_in_pt;
273                         }
274                 }
275                 return parseFloat(len); // Number
276         },
277         
278         // a constant used to split a SVG/VML path into primitive components
279         pathVmlRegExp: /([A-Za-z]+)|(\d+(\.\d+)?)|(\.\d+)|(-\d+(\.\d+)?)|(-\.\d+)/g,
280         pathSvgRegExp: /([A-Za-z])|(\d+(\.\d+)?)|(\.\d+)|(-\d+(\.\d+)?)|(-\.\d+)/g,
281         
282         equalSources: function(a, b){
283                 // summary: compares event sources, returns true if they are equal
284                 return a && b && a == b;
285         }
286 });
287
288 }