123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- (function() {'use strict';
- if (!svgedit.units) {
- svgedit.units = {};
- }
- var NS = svgedit.NS;
- var wAttrs = ['x', 'x1', 'cx', 'rx', 'width'];
- var hAttrs = ['y', 'y1', 'cy', 'ry', 'height'];
- var unitAttrs = ['r', 'radius'].concat(wAttrs, hAttrs);
- var unitNumMap = {
- '%': 2,
- 'em': 3,
- 'ex': 4,
- 'px': 5,
- 'cm': 6,
- 'mm': 7,
- 'in': 8,
- 'pt': 9,
- 'pc': 10
- };
- var elementContainer_;
- var typeMap_ = {};
- svgedit.units.init = function(elementContainer) {
- elementContainer_ = elementContainer;
-
- var svg = document.createElementNS(NS.SVG, 'svg');
- document.body.appendChild(svg);
- var rect = document.createElementNS(NS.SVG, 'rect');
- rect.setAttribute('width', '1em');
- rect.setAttribute('height', '1ex');
- rect.setAttribute('x', '1in');
- svg.appendChild(rect);
- var bb = rect.getBBox();
- document.body.removeChild(svg);
- var inch = bb.x;
- typeMap_ = {
- 'em': bb.width,
- 'ex': bb.height,
- 'in': inch,
- 'cm': inch / 2.54,
- 'mm': inch / 25.4,
- 'pt': inch / 72,
- 'pc': inch / 6,
- 'px': 1,
- '%': 0
- };
- };
- svgedit.units.getTypeMap = function() {
- return typeMap_;
- };
- svgedit.units.shortFloat = function(val) {
- var digits = elementContainer_.getRoundDigits();
- if (!isNaN(val)) {
-
- return +((+val).toFixed(digits));
- }
- if ($.isArray(val)) {
- return svgedit.units.shortFloat(val[0]) + ',' + svgedit.units.shortFloat(val[1]);
- }
- return parseFloat(val).toFixed(digits) - 0;
- };
- svgedit.units.convertUnit = function(val, unit) {
- unit = unit || elementContainer_.getBaseUnit();
- return svgedit.units.shortFloat(val / typeMap_[unit]);
- };
- svgedit.units.setUnitAttr = function(elem, attr, val) {
-
-
- elem.setAttribute(attr, val);
- };
- var attrsToConvert = {
- 'line': ['x1', 'x2', 'y1', 'y2'],
- 'circle': ['cx', 'cy', 'r'],
- 'ellipse': ['cx', 'cy', 'rx', 'ry'],
- 'foreignObject': ['x', 'y', 'width', 'height'],
- 'rect': ['x', 'y', 'width', 'height'],
- 'image': ['x', 'y', 'width', 'height'],
- 'use': ['x', 'y', 'width', 'height'],
- 'text': ['x', 'y']
- };
- svgedit.units.convertAttrs = function(element) {
- var elName = element.tagName;
- var unit = elementContainer_.getBaseUnit();
- var attrs = attrsToConvert[elName];
- if (!attrs) {return;}
- var len = attrs.length;
- var i;
- for (i = 0; i < len; i++) {
- var attr = attrs[i];
- var cur = element.getAttribute(attr);
- if (cur) {
- if (!isNaN(cur)) {
- element.setAttribute(attr, (cur / typeMap_[unit]) + unit);
- }
-
-
-
- }
- }
- };
- svgedit.units.convertToNum = function(attr, val) {
-
- if (!isNaN(val)) {return val-0;}
- var num;
- if (val.substr(-1) === '%') {
-
- num = val.substr(0, val.length-1)/100;
- var width = elementContainer_.getWidth();
- var height = elementContainer_.getHeight();
- if (wAttrs.indexOf(attr) >= 0) {
- return num * width;
- }
- if (hAttrs.indexOf(attr) >= 0) {
- return num * height;
- }
- return num * Math.sqrt((width*width) + (height*height))/Math.sqrt(2);
- }
- var unit = val.substr(-2);
- num = val.substr(0, val.length-2);
-
- return num * typeMap_[unit];
- };
- svgedit.units.isValidUnit = function(attr, val, selectedElement) {
- var valid = false;
- if (unitAttrs.indexOf(attr) >= 0) {
-
- if (!isNaN(val)) {
- valid = true;
- } else {
-
- val = val.toLowerCase();
- $.each(typeMap_, function(unit) {
- if (valid) {return;}
- var re = new RegExp('^-?[\\d\\.]+' + unit + '$');
- if (re.test(val)) {valid = true;}
- });
- }
- } else if (attr == 'id') {
-
-
- var result = false;
-
-
-
-
- try {
- var elem = elementContainer_.getElement(val);
- result = (elem == null || elem === selectedElement);
- } catch(e) {}
- return result;
- }
- valid = true;
- return valid;
- };
- }());
|