2018-05-16 00:53:27 +00:00
|
|
|
/* eslint-disable no-var */
|
|
|
|
/* globals $, svgedit */
|
2010-11-05 17:00:32 +00:00
|
|
|
/**
|
|
|
|
* Package: svgedit.units
|
|
|
|
*
|
2012-09-16 18:53:27 +00:00
|
|
|
* Licensed under the MIT License
|
2010-11-05 17:00:32 +00:00
|
|
|
*
|
|
|
|
* Copyright(c) 2010 Alexis Deveria
|
|
|
|
* Copyright(c) 2010 Jeff Schiller
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Dependencies:
|
|
|
|
// 1) jQuery
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
(function () {
|
|
|
|
'use strict';
|
2010-11-05 17:00:32 +00:00
|
|
|
|
|
|
|
if (!svgedit.units) {
|
2018-05-18 04:02:30 +00:00
|
|
|
svgedit.units = {};
|
2010-11-05 17:00:32 +00:00
|
|
|
}
|
|
|
|
|
2013-02-16 15:02:26 +00:00
|
|
|
var NS = svgedit.NS;
|
2013-02-14 12:18:13 +00:00
|
|
|
var wAttrs = ['x', 'x1', 'cx', 'rx', 'width'];
|
|
|
|
var hAttrs = ['y', 'y1', 'cy', 'ry', 'height'];
|
2013-02-15 16:51:48 +00:00
|
|
|
var unitAttrs = ['r', 'radius'].concat(wAttrs, hAttrs);
|
2013-02-14 12:18:13 +00:00
|
|
|
// unused
|
2018-05-16 00:53:27 +00:00
|
|
|
/*
|
2010-11-05 17:00:32 +00:00
|
|
|
var unitNumMap = {
|
2018-05-18 04:02:30 +00:00
|
|
|
'%': 2,
|
|
|
|
'em': 3,
|
|
|
|
'ex': 4,
|
|
|
|
'px': 5,
|
|
|
|
'cm': 6,
|
|
|
|
'mm': 7,
|
|
|
|
'in': 8,
|
|
|
|
'pt': 9,
|
|
|
|
'pc': 10
|
2010-11-05 17:00:32 +00:00
|
|
|
};
|
2018-05-16 00:53:27 +00:00
|
|
|
*/
|
2010-11-07 05:20:03 +00:00
|
|
|
// Container of elements.
|
|
|
|
var elementContainer_;
|
2010-11-06 00:45:21 +00:00
|
|
|
|
2010-11-05 17:00:32 +00:00
|
|
|
/**
|
|
|
|
* Stores mapping of unit type to user coordinates.
|
|
|
|
*/
|
2013-02-14 12:18:13 +00:00
|
|
|
var typeMap_ = {};
|
2010-11-07 05:20:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ElementContainer interface
|
|
|
|
*
|
2013-02-15 17:02:24 +00:00
|
|
|
* function getBaseUnit() - returns a string of the base unit type of the container ('em')
|
2010-11-07 05:20:03 +00:00
|
|
|
* function getElement() - returns an element in the container given an id
|
|
|
|
* function getHeight() - returns the container's height
|
|
|
|
* function getWidth() - returns the container's width
|
2011-02-02 17:28:43 +00:00
|
|
|
* function getRoundDigits() - returns the number of digits number should be rounded to
|
2010-11-07 05:20:03 +00:00
|
|
|
*/
|
2010-11-05 17:00:32 +00:00
|
|
|
|
|
|
|
/**
|
2010-11-06 00:45:21 +00:00
|
|
|
* Function: svgedit.units.init()
|
2010-11-05 17:00:32 +00:00
|
|
|
* Initializes this module.
|
2010-11-06 00:45:21 +00:00
|
|
|
*
|
|
|
|
* Parameters:
|
2010-11-15 09:25:49 +00:00
|
|
|
* elementContainer - an object implementing the ElementContainer interface.
|
2010-11-05 17:00:32 +00:00
|
|
|
*/
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.units.init = function (elementContainer) {
|
2018-05-18 04:02:30 +00:00
|
|
|
elementContainer_ = elementContainer;
|
2010-11-06 00:45:21 +00:00
|
|
|
|
2018-05-18 04:02:30 +00:00
|
|
|
// Get correct em/ex values by creating a temporary SVG.
|
|
|
|
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);
|
2010-11-05 17:00:32 +00:00
|
|
|
|
2018-05-18 04:02:30 +00:00
|
|
|
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
|
|
|
|
};
|
2010-11-06 00:45:21 +00:00
|
|
|
};
|
|
|
|
|
2010-11-07 16:46:57 +00:00
|
|
|
// Group: Unit conversion functions
|
|
|
|
|
2010-11-06 00:45:21 +00:00
|
|
|
// Function: svgedit.units.getTypeMap
|
|
|
|
// Returns the unit object with values for each unit
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.units.getTypeMap = function () {
|
2018-05-18 04:02:30 +00:00
|
|
|
return typeMap_;
|
2010-11-06 00:45:21 +00:00
|
|
|
};
|
|
|
|
|
2011-02-02 17:28:43 +00:00
|
|
|
// Function: svgedit.units.shortFloat
|
|
|
|
// Rounds a given value to a float with number of digits defined in save_options
|
|
|
|
//
|
2013-02-14 12:18:13 +00:00
|
|
|
// Parameters:
|
2011-02-02 17:28:43 +00:00
|
|
|
// val - The value as a String, Number or Array of two numbers to be rounded
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// If a string/number was given, returns a Float. If an array, return a string
|
2018-05-16 08:32:44 +00:00
|
|
|
// with comma-separated floats
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.units.shortFloat = function (val) {
|
2018-05-18 04:02:30 +00:00
|
|
|
var digits = elementContainer_.getRoundDigits();
|
|
|
|
if (!isNaN(val)) {
|
|
|
|
// Note that + converts to Number
|
|
|
|
return +((+val).toFixed(digits));
|
|
|
|
}
|
|
|
|
if ($.isArray(val)) {
|
|
|
|
return svgedit.units.shortFloat(val[0]) + ',' + svgedit.units.shortFloat(val[1]);
|
|
|
|
}
|
|
|
|
return parseFloat(val).toFixed(digits) - 0;
|
2011-02-02 17:28:43 +00:00
|
|
|
};
|
|
|
|
|
2010-11-06 00:45:21 +00:00
|
|
|
// Function: svgedit.units.convertUnit
|
|
|
|
// Converts the number to given unit or baseUnit
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.units.convertUnit = function (val, unit) {
|
2018-05-18 04:02:30 +00:00
|
|
|
unit = unit || elementContainer_.getBaseUnit();
|
|
|
|
// baseVal.convertToSpecifiedUnits(unitNumMap[unit]);
|
|
|
|
// var val = baseVal.valueInSpecifiedUnits;
|
|
|
|
// baseVal.convertToSpecifiedUnits(1);
|
|
|
|
return svgedit.units.shortFloat(val / typeMap_[unit]);
|
2010-11-06 00:45:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Function: svgedit.units.setUnitAttr
|
|
|
|
// Sets an element's attribute based on the unit in its current value.
|
|
|
|
//
|
2013-02-14 12:18:13 +00:00
|
|
|
// Parameters:
|
2010-11-06 00:45:21 +00:00
|
|
|
// elem - DOM element to be changed
|
|
|
|
// attr - String with the name of the attribute associated with the value
|
|
|
|
// val - String with the attribute value to convert
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.units.setUnitAttr = function (elem, attr, val) {
|
2018-05-18 06:41:43 +00:00
|
|
|
// if (!isNaN(val)) {
|
2018-05-18 04:02:30 +00:00
|
|
|
// New value is a number, so check currently used unit
|
|
|
|
// var old_val = elem.getAttribute(attr);
|
2013-02-14 12:18:13 +00:00
|
|
|
|
2018-05-18 04:02:30 +00:00
|
|
|
// Enable this for alternate mode
|
|
|
|
// if (old_val !== null && (isNaN(old_val) || elementContainer_.getBaseUnit() !== 'px')) {
|
2018-05-18 06:41:43 +00:00
|
|
|
// // Old value was a number, so get unit, then convert
|
|
|
|
// var unit;
|
|
|
|
// if (old_val.substr(-1) === '%') {
|
|
|
|
// var res = getResolution();
|
|
|
|
// unit = '%';
|
|
|
|
// val *= 100;
|
|
|
|
// if (wAttrs.indexOf(attr) >= 0) {
|
|
|
|
// val = val / res.w;
|
|
|
|
// } else if (hAttrs.indexOf(attr) >= 0) {
|
|
|
|
// val = val / res.h;
|
|
|
|
// } else {
|
|
|
|
// return val / Math.sqrt((res.w*res.w) + (res.h*res.h))/Math.sqrt(2);
|
|
|
|
// }
|
|
|
|
// } else {
|
|
|
|
// if (elementContainer_.getBaseUnit() !== 'px') {
|
|
|
|
// unit = elementContainer_.getBaseUnit();
|
|
|
|
// } else {
|
|
|
|
// unit = old_val.substr(-2);
|
|
|
|
// }
|
|
|
|
// val = val / typeMap_[unit];
|
|
|
|
// }
|
2018-05-18 04:02:30 +00:00
|
|
|
//
|
|
|
|
// val += unit;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
elem.setAttribute(attr, val);
|
2010-11-05 17:00:32 +00:00
|
|
|
};
|
|
|
|
|
2010-11-06 14:37:00 +00:00
|
|
|
var attrsToConvert = {
|
2018-05-18 04:02:30 +00:00
|
|
|
'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']
|
2010-11-06 14:37:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Function: svgedit.units.convertAttrs
|
|
|
|
// Converts all applicable attributes to the configured baseUnit
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// element - a DOM element whose attributes should be converted
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.units.convertAttrs = function (element) {
|
2018-05-18 04:02:30 +00:00
|
|
|
var elName = element.tagName;
|
|
|
|
var unit = elementContainer_.getBaseUnit();
|
|
|
|
var attrs = attrsToConvert[elName];
|
|
|
|
if (!attrs) { return; }
|
2013-02-14 12:18:13 +00:00
|
|
|
|
2018-05-18 04:02:30 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
// else {
|
|
|
|
// Convert existing?
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
}
|
2010-11-06 14:37:00 +00:00
|
|
|
};
|
|
|
|
|
2010-11-07 05:20:03 +00:00
|
|
|
// Function: svgedit.units.convertToNum
|
2018-05-16 00:53:27 +00:00
|
|
|
// Converts given values to numbers. Attributes must be supplied in
|
2010-11-07 05:20:03 +00:00
|
|
|
// case a percentage is given
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// attr - String with the name of the attribute associated with the value
|
|
|
|
// val - String with the attribute value to convert
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.units.convertToNum = function (attr, val) {
|
2018-05-18 04:02:30 +00:00
|
|
|
// Return a number if that's what it already is
|
|
|
|
if (!isNaN(val)) { return val - 0; }
|
|
|
|
var num;
|
|
|
|
if (val.substr(-1) === '%') {
|
|
|
|
// Deal with percentage, depends on attribute
|
|
|
|
num = val.substr(0, val.length - 1) / 100;
|
|
|
|
var width = elementContainer_.getWidth();
|
|
|
|
var height = elementContainer_.getHeight();
|
2013-02-14 12:18:13 +00:00
|
|
|
|
2018-05-18 04:02:30 +00:00
|
|
|
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);
|
|
|
|
// Note that this multiplication turns the string into a number
|
|
|
|
return num * typeMap_[unit];
|
2010-11-07 05:20:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Function: svgedit.units.isValidUnit
|
|
|
|
// Check if an attribute's value is in a valid format
|
|
|
|
//
|
2013-02-14 12:18:13 +00:00
|
|
|
// Parameters:
|
2010-11-07 05:20:03 +00:00
|
|
|
// attr - String with the name of the attribute associated with the value
|
|
|
|
// val - String with the attribute value to check
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.units.isValidUnit = function (attr, val, selectedElement) {
|
2018-05-18 04:02:30 +00:00
|
|
|
var valid = false;
|
|
|
|
if (unitAttrs.indexOf(attr) >= 0) {
|
|
|
|
// True if it's just a number
|
|
|
|
if (!isNaN(val)) {
|
|
|
|
valid = true;
|
|
|
|
} else {
|
|
|
|
// Not a number, check if it has a valid unit
|
|
|
|
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') {
|
|
|
|
// if we're trying to change the id, make sure it's not already present in the doc
|
|
|
|
// and the id value is valid.
|
2010-11-07 05:20:03 +00:00
|
|
|
|
2018-05-18 04:02:30 +00:00
|
|
|
var result = false;
|
|
|
|
// because getElem() can throw an exception in the case of an invalid id
|
2018-05-18 04:40:50 +00:00
|
|
|
// (according to https://www.w3.org/TR/xml-id/ IDs must be a NCName)
|
2018-05-18 04:02:30 +00:00
|
|
|
// we wrap it in an exception and only return true if the ID was valid and
|
|
|
|
// not already present
|
|
|
|
try {
|
|
|
|
var elem = elementContainer_.getElement(val);
|
|
|
|
result = (elem == null || elem === selectedElement);
|
|
|
|
} catch (e) {}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
valid = true;
|
2013-02-14 12:18:13 +00:00
|
|
|
|
2018-05-18 04:02:30 +00:00
|
|
|
return valid;
|
2010-11-07 05:20:03 +00:00
|
|
|
};
|
2018-05-16 00:53:27 +00:00
|
|
|
}());
|