1 | /**
|
---|
2 | * Package: svgedit.units
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2
|
---|
5 | *
|
---|
6 | * Copyright(c) 2010 Alexis Deveria
|
---|
7 | * Copyright(c) 2010 Jeff Schiller
|
---|
8 | */
|
---|
9 |
|
---|
10 | // Dependencies:
|
---|
11 | // 1) jQuery
|
---|
12 |
|
---|
13 | var svgedit = svgedit || {};
|
---|
14 |
|
---|
15 | (function() {
|
---|
16 |
|
---|
17 | if (!svgedit.units) {
|
---|
18 | svgedit.units = {};
|
---|
19 | }
|
---|
20 |
|
---|
21 | var w_attrs = ['x', 'x1', 'cx', 'rx', 'width'];
|
---|
22 | var h_attrs = ['y', 'y1', 'cy', 'ry', 'height'];
|
---|
23 | var unit_attrs = $.merge(['r','radius'], w_attrs);
|
---|
24 |
|
---|
25 | var unitNumMap = {
|
---|
26 | '%': 2,
|
---|
27 | 'em': 3,
|
---|
28 | 'ex': 4,
|
---|
29 | 'px': 5,
|
---|
30 | 'cm': 6,
|
---|
31 | 'mm': 7,
|
---|
32 | 'in': 8,
|
---|
33 | 'pt': 9,
|
---|
34 | 'pc': 10
|
---|
35 | };
|
---|
36 |
|
---|
37 | $.merge(unit_attrs, h_attrs);
|
---|
38 |
|
---|
39 | // Container of elements.
|
---|
40 | var elementContainer_;
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Stores mapping of unit type to user coordinates.
|
---|
44 | */
|
---|
45 | var typeMap_ = {px: 1};
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * ElementContainer interface
|
---|
49 | *
|
---|
50 | * function getBaseUnit() - returns a string of the base unit type of the container ("em")
|
---|
51 | * function getElement() - returns an element in the container given an id
|
---|
52 | * function getHeight() - returns the container's height
|
---|
53 | * function getWidth() - returns the container's width
|
---|
54 | * function getRoundDigits() - returns the number of digits number should be rounded to
|
---|
55 | */
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Function: svgedit.units.init()
|
---|
59 | * Initializes this module.
|
---|
60 | *
|
---|
61 | * Parameters:
|
---|
62 | * elementContainer - an object implementing the ElementContainer interface.
|
---|
63 | */
|
---|
64 | svgedit.units.init = function(elementContainer) {
|
---|
65 | elementContainer_ = elementContainer;
|
---|
66 |
|
---|
67 | var svgns = 'http://www.w3.org/2000/svg';
|
---|
68 |
|
---|
69 | // Get correct em/ex values by creating a temporary SVG.
|
---|
70 | var svg = document.createElementNS(svgns, 'svg');
|
---|
71 | document.body.appendChild(svg);
|
---|
72 | var rect = document.createElementNS(svgns,'rect');
|
---|
73 | rect.setAttribute('width',"1em");
|
---|
74 | rect.setAttribute('height',"1ex");
|
---|
75 | rect.setAttribute('x',"1in");
|
---|
76 | svg.appendChild(rect);
|
---|
77 | var bb = rect.getBBox();
|
---|
78 | document.body.removeChild(svg);
|
---|
79 |
|
---|
80 | var inch = bb.x;
|
---|
81 | typeMap_['em'] = bb.width;
|
---|
82 | typeMap_['ex'] = bb.height;
|
---|
83 | typeMap_['in'] = inch;
|
---|
84 | typeMap_['cm'] = inch / 2.54;
|
---|
85 | typeMap_['mm'] = inch / 25.4;
|
---|
86 | typeMap_['pt'] = inch / 72;
|
---|
87 | typeMap_['pc'] = inch / 6;
|
---|
88 | typeMap_['%'] = 0;
|
---|
89 | };
|
---|
90 |
|
---|
91 | // Group: Unit conversion functions
|
---|
92 |
|
---|
93 | // Function: svgedit.units.getTypeMap
|
---|
94 | // Returns the unit object with values for each unit
|
---|
95 | svgedit.units.getTypeMap = function() {
|
---|
96 | return typeMap_;
|
---|
97 | };
|
---|
98 |
|
---|
99 | // Function: svgedit.units.shortFloat
|
---|
100 | // Rounds a given value to a float with number of digits defined in save_options
|
---|
101 | //
|
---|
102 | // Parameters:
|
---|
103 | // val - The value as a String, Number or Array of two numbers to be rounded
|
---|
104 | //
|
---|
105 | // Returns:
|
---|
106 | // If a string/number was given, returns a Float. If an array, return a string
|
---|
107 | // with comma-seperated floats
|
---|
108 | svgedit.units.shortFloat = function(val) {
|
---|
109 | var digits = elementContainer_.getRoundDigits();
|
---|
110 | if(!isNaN(val)) {
|
---|
111 | // Note that + converts to Number
|
---|
112 | return +((+val).toFixed(digits));
|
---|
113 | } else if($.isArray(val)) {
|
---|
114 | return svgedit.units.shortFloat(val[0]) + ',' + svgedit.units.shortFloat(val[1]);
|
---|
115 | }
|
---|
116 | return parseFloat(val).toFixed(digits) - 0;
|
---|
117 | };
|
---|
118 |
|
---|
119 | // Function: svgedit.units.convertUnit
|
---|
120 | // Converts the number to given unit or baseUnit
|
---|
121 | svgedit.units.convertUnit = function(val, unit) {
|
---|
122 | unit = unit || elementContainer_.getBaseUnit();
|
---|
123 | // baseVal.convertToSpecifiedUnits(unitNumMap[unit]);
|
---|
124 | // var val = baseVal.valueInSpecifiedUnits;
|
---|
125 | // baseVal.convertToSpecifiedUnits(1);
|
---|
126 | return svgedit.unit.shortFloat(val / typeMap_[unit]);
|
---|
127 | };
|
---|
128 |
|
---|
129 | // Function: svgedit.units.setUnitAttr
|
---|
130 | // Sets an element's attribute based on the unit in its current value.
|
---|
131 | //
|
---|
132 | // Parameters:
|
---|
133 | // elem - DOM element to be changed
|
---|
134 | // attr - String with the name of the attribute associated with the value
|
---|
135 | // val - String with the attribute value to convert
|
---|
136 | svgedit.units.setUnitAttr = function(elem, attr, val) {
|
---|
137 | if(!isNaN(val)) {
|
---|
138 | // New value is a number, so check currently used unit
|
---|
139 | var old_val = elem.getAttribute(attr);
|
---|
140 |
|
---|
141 | // Enable this for alternate mode
|
---|
142 | // if(old_val !== null && (isNaN(old_val) || elementContainer_.getBaseUnit() !== 'px')) {
|
---|
143 | // // Old value was a number, so get unit, then convert
|
---|
144 | // var unit;
|
---|
145 | // if(old_val.substr(-1) === '%') {
|
---|
146 | // var res = getResolution();
|
---|
147 | // unit = '%';
|
---|
148 | // val *= 100;
|
---|
149 | // if(w_attrs.indexOf(attr) >= 0) {
|
---|
150 | // val = val / res.w;
|
---|
151 | // } else if(h_attrs.indexOf(attr) >= 0) {
|
---|
152 | // val = val / res.h;
|
---|
153 | // } else {
|
---|
154 | // return val / Math.sqrt((res.w*res.w) + (res.h*res.h))/Math.sqrt(2);
|
---|
155 | // }
|
---|
156 | // } else {
|
---|
157 | // if(elementContainer_.getBaseUnit() !== 'px') {
|
---|
158 | // unit = elementContainer_.getBaseUnit();
|
---|
159 | // } else {
|
---|
160 | // unit = old_val.substr(-2);
|
---|
161 | // }
|
---|
162 | // val = val / typeMap_[unit];
|
---|
163 | // }
|
---|
164 | //
|
---|
165 | // val += unit;
|
---|
166 | // }
|
---|
167 | }
|
---|
168 | elem.setAttribute(attr, val);
|
---|
169 | };
|
---|
170 |
|
---|
171 | var attrsToConvert = {
|
---|
172 | "line": ['x1', 'x2', 'y1', 'y2'],
|
---|
173 | "circle": ['cx', 'cy', 'r'],
|
---|
174 | "ellipse": ['cx', 'cy', 'rx', 'ry'],
|
---|
175 | "foreignObject": ['x', 'y', 'width', 'height'],
|
---|
176 | "rect": ['x', 'y', 'width', 'height'],
|
---|
177 | "image": ['x', 'y', 'width', 'height'],
|
---|
178 | "use": ['x', 'y', 'width', 'height'],
|
---|
179 | "text": ['x', 'y']
|
---|
180 | };
|
---|
181 |
|
---|
182 | // Function: svgedit.units.convertAttrs
|
---|
183 | // Converts all applicable attributes to the configured baseUnit
|
---|
184 | //
|
---|
185 | // Parameters:
|
---|
186 | // element - a DOM element whose attributes should be converted
|
---|
187 | svgedit.units.convertAttrs = function(element) {
|
---|
188 | var elName = element.tagName;
|
---|
189 | var unit = elementContainer_.getBaseUnit();
|
---|
190 | var attrs = attrsToConvert[elName];
|
---|
191 | if(!attrs) return;
|
---|
192 | var len = attrs.length
|
---|
193 | for(var i = 0; i < len; i++) {
|
---|
194 | var attr = attrs[i];
|
---|
195 | var cur = element.getAttribute(attr);
|
---|
196 | if(cur) {
|
---|
197 | if(!isNaN(cur)) {
|
---|
198 | element.setAttribute(attr, (cur / typeMap_[unit]) + unit);
|
---|
199 | } else {
|
---|
200 | // Convert existing?
|
---|
201 | }
|
---|
202 | }
|
---|
203 | }
|
---|
204 | };
|
---|
205 |
|
---|
206 | // Function: svgedit.units.convertToNum
|
---|
207 | // Converts given values to numbers. Attributes must be supplied in
|
---|
208 | // case a percentage is given
|
---|
209 | //
|
---|
210 | // Parameters:
|
---|
211 | // attr - String with the name of the attribute associated with the value
|
---|
212 | // val - String with the attribute value to convert
|
---|
213 | svgedit.units.convertToNum = function(attr, val) {
|
---|
214 | // Return a number if that's what it already is
|
---|
215 | if(!isNaN(val)) return val-0;
|
---|
216 |
|
---|
217 | if(val.substr(-1) === '%') {
|
---|
218 | // Deal with percentage, depends on attribute
|
---|
219 | var num = val.substr(0, val.length-1)/100;
|
---|
220 | var width = elementContainer_.getWidth();
|
---|
221 | var height = elementContainer_.getHeight();
|
---|
222 |
|
---|
223 | if(w_attrs.indexOf(attr) >= 0) {
|
---|
224 | return num * width;
|
---|
225 | } else if(h_attrs.indexOf(attr) >= 0) {
|
---|
226 | return num * height;
|
---|
227 | } else {
|
---|
228 | return num * Math.sqrt((width*width) + (height*height))/Math.sqrt(2);
|
---|
229 | }
|
---|
230 | } else {
|
---|
231 | var unit = val.substr(-2);
|
---|
232 | var num = val.substr(0, val.length-2);
|
---|
233 | // Note that this multiplication turns the string into a number
|
---|
234 | return num * typeMap_[unit];
|
---|
235 | }
|
---|
236 | };
|
---|
237 |
|
---|
238 | // Function: svgedit.units.isValidUnit
|
---|
239 | // Check if an attribute's value is in a valid format
|
---|
240 | //
|
---|
241 | // Parameters:
|
---|
242 | // attr - String with the name of the attribute associated with the value
|
---|
243 | // val - String with the attribute value to check
|
---|
244 | svgedit.units.isValidUnit = function(attr, val, selectedElement) {
|
---|
245 | var valid = false;
|
---|
246 | if(unit_attrs.indexOf(attr) >= 0) {
|
---|
247 | // True if it's just a number
|
---|
248 | if(!isNaN(val)) {
|
---|
249 | valid = true;
|
---|
250 | } else {
|
---|
251 | // Not a number, check if it has a valid unit
|
---|
252 | val = val.toLowerCase();
|
---|
253 | $.each(typeMap_, function(unit) {
|
---|
254 | if(valid) return;
|
---|
255 | var re = new RegExp('^-?[\\d\\.]+' + unit + '$');
|
---|
256 | if(re.test(val)) valid = true;
|
---|
257 | });
|
---|
258 | }
|
---|
259 | } else if (attr == "id") {
|
---|
260 | // if we're trying to change the id, make sure it's not already present in the doc
|
---|
261 | // and the id value is valid.
|
---|
262 |
|
---|
263 | var result = false;
|
---|
264 | // because getElem() can throw an exception in the case of an invalid id
|
---|
265 | // (according to http://www.w3.org/TR/xml-id/ IDs must be a NCName)
|
---|
266 | // we wrap it in an exception and only return true if the ID was valid and
|
---|
267 | // not already present
|
---|
268 | try {
|
---|
269 | var elem = elementContainer_.getElement(val);
|
---|
270 | result = (elem == null || elem === selectedElement);
|
---|
271 | } catch(e) {}
|
---|
272 | return result;
|
---|
273 | } else {
|
---|
274 | valid = true;
|
---|
275 | }
|
---|
276 |
|
---|
277 | return valid;
|
---|
278 | };
|
---|
279 |
|
---|
280 |
|
---|
281 | })(); |
---|