2010-10-22 02:54:00 +00:00
|
|
|
(function(global) {
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2010-10-22 02:54:00 +00:00
|
|
|
"use strict";
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2010-10-19 20:27:24 +00:00
|
|
|
/**
|
|
|
|
|
* @name fabric
|
|
|
|
|
* @namespace
|
|
|
|
|
*/
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2010-10-22 02:54:00 +00:00
|
|
|
var fabric = global.fabric || (global.fabric = { }),
|
2010-07-26 23:20:19 +00:00
|
|
|
extend = fabric.util.object.extend,
|
|
|
|
|
capitalize = fabric.util.string.capitalize,
|
2013-04-16 05:49:42 +00:00
|
|
|
clone = fabric.util.object.clone,
|
Parse SVG stroke-opacity and fill-opacity
- SVG attribute opacity is now used for object's opacity
- fill-opacity and stroke-opacity are added to stroke and fill color value
- Add hsl/hsla support (e.g. hsl(270, 80%, 10%), hsla(320, 10%, 66%, 0.5))
- Add support for rgb/rgba values with whitespaces around values (e.g. rgba( 255 , 100 , 50 , 0.1 )) and percentage values (e.g. rgb(100%, 67%, 15%, 0.8))
- Delete stroke and strokeWidth from fabric.Text (defined in fabric.Object)
- New unit test for parse stroke-opacity and fill-opacity
- Update unit tests (new tests for hsl/hsla and rgb/rgba (whitespaces and percentage values))
- Change equal and deepEqual parameter order (e.g. equal(actualValue, expectedValue, message))
- Doc additions
2013-05-25 09:03:09 +00:00
|
|
|
toFixed = fabric.util.toFixed,
|
2013-04-17 17:54:11 +00:00
|
|
|
multiplyTransformMatrices = fabric.util.multiplyTransformMatrices;
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
var attributesMap = {
|
Parse SVG stroke-opacity and fill-opacity
- SVG attribute opacity is now used for object's opacity
- fill-opacity and stroke-opacity are added to stroke and fill color value
- Add hsl/hsla support (e.g. hsl(270, 80%, 10%), hsla(320, 10%, 66%, 0.5))
- Add support for rgb/rgba values with whitespaces around values (e.g. rgba( 255 , 100 , 50 , 0.1 )) and percentage values (e.g. rgb(100%, 67%, 15%, 0.8))
- Delete stroke and strokeWidth from fabric.Text (defined in fabric.Object)
- New unit test for parse stroke-opacity and fill-opacity
- Update unit tests (new tests for hsl/hsla and rgb/rgba (whitespaces and percentage values))
- Change equal and deepEqual parameter order (e.g. equal(actualValue, expectedValue, message))
- Doc additions
2013-05-25 09:03:09 +00:00
|
|
|
'fill-opacity': 'fillOpacity',
|
|
|
|
|
'fill-rule': 'fillRule',
|
|
|
|
|
'font-family': 'fontFamily',
|
|
|
|
|
'font-size': 'fontSize',
|
|
|
|
|
'font-style': 'fontStyle',
|
|
|
|
|
'font-weight': 'fontWeight',
|
2012-08-11 16:59:53 +00:00
|
|
|
'cx': 'left',
|
|
|
|
|
'x': 'left',
|
|
|
|
|
'r': 'radius',
|
2013-05-18 11:01:34 +00:00
|
|
|
'stroke-dasharray': 'strokeDashArray',
|
|
|
|
|
'stroke-linecap': 'strokeLineCap',
|
|
|
|
|
'stroke-linejoin': 'strokeLineJoin',
|
|
|
|
|
'stroke-miterlimit':'strokeMiterLimit',
|
Parse SVG stroke-opacity and fill-opacity
- SVG attribute opacity is now used for object's opacity
- fill-opacity and stroke-opacity are added to stroke and fill color value
- Add hsl/hsla support (e.g. hsl(270, 80%, 10%), hsla(320, 10%, 66%, 0.5))
- Add support for rgb/rgba values with whitespaces around values (e.g. rgba( 255 , 100 , 50 , 0.1 )) and percentage values (e.g. rgb(100%, 67%, 15%, 0.8))
- Delete stroke and strokeWidth from fabric.Text (defined in fabric.Object)
- New unit test for parse stroke-opacity and fill-opacity
- Update unit tests (new tests for hsl/hsla and rgb/rgba (whitespaces and percentage values))
- Change equal and deepEqual parameter order (e.g. equal(actualValue, expectedValue, message))
- Doc additions
2013-05-25 09:03:09 +00:00
|
|
|
'stroke-opacity': 'strokeOpacity',
|
|
|
|
|
'stroke-width': 'strokeWidth',
|
2012-08-11 16:59:53 +00:00
|
|
|
'text-decoration': 'textDecoration',
|
Parse SVG stroke-opacity and fill-opacity
- SVG attribute opacity is now used for object's opacity
- fill-opacity and stroke-opacity are added to stroke and fill color value
- Add hsl/hsla support (e.g. hsl(270, 80%, 10%), hsla(320, 10%, 66%, 0.5))
- Add support for rgb/rgba values with whitespaces around values (e.g. rgba( 255 , 100 , 50 , 0.1 )) and percentage values (e.g. rgb(100%, 67%, 15%, 0.8))
- Delete stroke and strokeWidth from fabric.Text (defined in fabric.Object)
- New unit test for parse stroke-opacity and fill-opacity
- Update unit tests (new tests for hsl/hsla and rgb/rgba (whitespaces and percentage values))
- Change equal and deepEqual parameter order (e.g. equal(actualValue, expectedValue, message))
- Doc additions
2013-05-25 09:03:09 +00:00
|
|
|
'cy': 'top',
|
|
|
|
|
'y': 'top',
|
|
|
|
|
'transform': 'transformMatrix'
|
2010-06-09 22:34:55 +00:00
|
|
|
};
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2013-05-25 17:42:31 +00:00
|
|
|
var colorAttributes = {
|
|
|
|
|
'stroke': 'strokeOpacity',
|
|
|
|
|
'fill': 'fillOpacity'
|
|
|
|
|
};
|
|
|
|
|
|
2012-06-10 10:13:12 +00:00
|
|
|
function normalizeAttr(attr) {
|
|
|
|
|
// transform attribute names
|
|
|
|
|
if (attr in attributesMap) {
|
|
|
|
|
return attributesMap[attr];
|
|
|
|
|
}
|
|
|
|
|
return attr;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-01 06:58:52 +00:00
|
|
|
function normalizeValue(attr, value, parentAttributes) {
|
2013-05-18 11:01:34 +00:00
|
|
|
var isArray;
|
|
|
|
|
|
2013-05-01 06:58:52 +00:00
|
|
|
if ((attr === 'fill' || attr === 'stroke') && value === 'none') {
|
2013-05-18 11:01:34 +00:00
|
|
|
value = '';
|
|
|
|
|
}
|
|
|
|
|
else if (attr === 'fillRule') {
|
|
|
|
|
value = (value === 'evenodd') ? 'destination-over' : value;
|
2013-05-01 06:58:52 +00:00
|
|
|
}
|
2013-05-18 11:01:34 +00:00
|
|
|
else if (attr === 'strokeDashArray') {
|
|
|
|
|
value = value.replace(/,/g, ' ').split(/\s+/);
|
2013-05-01 06:58:52 +00:00
|
|
|
}
|
2013-05-18 11:01:34 +00:00
|
|
|
else if (attr === 'transformMatrix') {
|
2013-05-01 06:58:52 +00:00
|
|
|
if (parentAttributes && parentAttributes.transformMatrix) {
|
2013-05-18 11:01:34 +00:00
|
|
|
value = multiplyTransformMatrices(
|
2013-05-01 06:58:52 +00:00
|
|
|
parentAttributes.transformMatrix, fabric.parseTransformAttribute(value));
|
|
|
|
|
}
|
2013-06-13 21:10:27 +00:00
|
|
|
else {
|
|
|
|
|
value = fabric.parseTransformAttribute(value);
|
|
|
|
|
}
|
2013-05-01 06:58:52 +00:00
|
|
|
}
|
2013-05-18 11:01:34 +00:00
|
|
|
|
|
|
|
|
isArray = Object.prototype.toString.call(value) === '[object Array]';
|
|
|
|
|
|
|
|
|
|
// TODO: need to normalize em, %, pt, etc. to px (!)
|
|
|
|
|
var parsed = isArray ? value.map(parseFloat) : parseFloat(value);
|
|
|
|
|
|
|
|
|
|
return (!isArray && isNaN(parsed) ? value : parsed);
|
2013-05-01 06:58:52 +00:00
|
|
|
}
|
|
|
|
|
|
Parse SVG stroke-opacity and fill-opacity
- SVG attribute opacity is now used for object's opacity
- fill-opacity and stroke-opacity are added to stroke and fill color value
- Add hsl/hsla support (e.g. hsl(270, 80%, 10%), hsla(320, 10%, 66%, 0.5))
- Add support for rgb/rgba values with whitespaces around values (e.g. rgba( 255 , 100 , 50 , 0.1 )) and percentage values (e.g. rgb(100%, 67%, 15%, 0.8))
- Delete stroke and strokeWidth from fabric.Text (defined in fabric.Object)
- New unit test for parse stroke-opacity and fill-opacity
- Update unit tests (new tests for hsl/hsla and rgb/rgba (whitespaces and percentage values))
- Change equal and deepEqual parameter order (e.g. equal(actualValue, expectedValue, message))
- Doc additions
2013-05-25 09:03:09 +00:00
|
|
|
/**
|
|
|
|
|
* @private
|
|
|
|
|
* @param {Object} attributes Array of attributes to parse
|
|
|
|
|
*/
|
|
|
|
|
function _setStrokeFillOpacity(attributes) {
|
|
|
|
|
for (var attr in colorAttributes) {
|
2013-08-07 11:51:59 +00:00
|
|
|
|
Parse SVG stroke-opacity and fill-opacity
- SVG attribute opacity is now used for object's opacity
- fill-opacity and stroke-opacity are added to stroke and fill color value
- Add hsl/hsla support (e.g. hsl(270, 80%, 10%), hsla(320, 10%, 66%, 0.5))
- Add support for rgb/rgba values with whitespaces around values (e.g. rgba( 255 , 100 , 50 , 0.1 )) and percentage values (e.g. rgb(100%, 67%, 15%, 0.8))
- Delete stroke and strokeWidth from fabric.Text (defined in fabric.Object)
- New unit test for parse stroke-opacity and fill-opacity
- Update unit tests (new tests for hsl/hsla and rgb/rgba (whitespaces and percentage values))
- Change equal and deepEqual parameter order (e.g. equal(actualValue, expectedValue, message))
- Doc additions
2013-05-25 09:03:09 +00:00
|
|
|
if (!attributes[attr] || typeof attributes[colorAttributes[attr]] === 'undefined') continue;
|
|
|
|
|
|
2013-08-07 11:51:59 +00:00
|
|
|
if (attributes[attr].indexOf('url(') === 0) continue;
|
|
|
|
|
|
Parse SVG stroke-opacity and fill-opacity
- SVG attribute opacity is now used for object's opacity
- fill-opacity and stroke-opacity are added to stroke and fill color value
- Add hsl/hsla support (e.g. hsl(270, 80%, 10%), hsla(320, 10%, 66%, 0.5))
- Add support for rgb/rgba values with whitespaces around values (e.g. rgba( 255 , 100 , 50 , 0.1 )) and percentage values (e.g. rgb(100%, 67%, 15%, 0.8))
- Delete stroke and strokeWidth from fabric.Text (defined in fabric.Object)
- New unit test for parse stroke-opacity and fill-opacity
- Update unit tests (new tests for hsl/hsla and rgb/rgba (whitespaces and percentage values))
- Change equal and deepEqual parameter order (e.g. equal(actualValue, expectedValue, message))
- Doc additions
2013-05-25 09:03:09 +00:00
|
|
|
var color = new fabric.Color(attributes[attr]);
|
|
|
|
|
attributes[attr] = color.setAlpha(toFixed(color.getAlpha() * attributes[colorAttributes[attr]], 2)).toRgba();
|
|
|
|
|
|
|
|
|
|
delete attributes[colorAttributes[attr]];
|
|
|
|
|
}
|
|
|
|
|
return attributes;
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
/**
|
2010-10-14 21:42:39 +00:00
|
|
|
* Returns an object of attributes' name/value, given element and an array of attribute names;
|
|
|
|
|
* Parses parent "g" nodes recursively upwards.
|
|
|
|
|
* @static
|
|
|
|
|
* @memberOf fabric
|
2010-06-09 22:34:55 +00:00
|
|
|
* @param {DOMElement} element Element to parse
|
|
|
|
|
* @param {Array} attributes Array of attributes to parse
|
|
|
|
|
* @return {Object} object containing parsed attributes' names/values
|
|
|
|
|
*/
|
|
|
|
|
function parseAttributes(element, attributes) {
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2010-06-10 17:57:59 +00:00
|
|
|
if (!element) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2012-01-17 13:41:58 +00:00
|
|
|
|
|
|
|
|
var value,
|
2010-06-09 22:34:55 +00:00
|
|
|
parentAttributes = { };
|
|
|
|
|
|
|
|
|
|
// if there's a parent container (`g` node), parse its attributes recursively upwards
|
|
|
|
|
if (element.parentNode && /^g$/i.test(element.parentNode.nodeName)) {
|
2010-07-09 23:43:50 +00:00
|
|
|
parentAttributes = fabric.parseAttributes(element.parentNode, attributes);
|
2010-06-09 22:34:55 +00:00
|
|
|
}
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2010-06-17 14:00:47 +00:00
|
|
|
var ownAttributes = attributes.reduce(function(memo, attr) {
|
2010-06-09 22:34:55 +00:00
|
|
|
value = element.getAttribute(attr);
|
2012-01-17 13:41:58 +00:00
|
|
|
if (value) {
|
2012-06-10 10:13:12 +00:00
|
|
|
attr = normalizeAttr(attr);
|
2013-05-18 11:01:34 +00:00
|
|
|
value = normalizeValue(attr, value, parentAttributes);
|
2013-05-01 06:58:52 +00:00
|
|
|
|
2013-05-18 11:01:34 +00:00
|
|
|
memo[attr] = value;
|
2010-06-09 22:34:55 +00:00
|
|
|
}
|
|
|
|
|
return memo;
|
2010-06-17 14:00:47 +00:00
|
|
|
}, { });
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2011-01-06 22:13:07 +00:00
|
|
|
// add values parsed from style, which take precedence over attributes
|
|
|
|
|
// (see: http://www.w3.org/TR/SVG/styling.html#UsingPresentationAttributes)
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2013-05-01 06:58:52 +00:00
|
|
|
ownAttributes = extend(ownAttributes,
|
|
|
|
|
extend(getGlobalStylesForElement(element), fabric.parseStyleAttribute(element)));
|
Parse SVG stroke-opacity and fill-opacity
- SVG attribute opacity is now used for object's opacity
- fill-opacity and stroke-opacity are added to stroke and fill color value
- Add hsl/hsla support (e.g. hsl(270, 80%, 10%), hsla(320, 10%, 66%, 0.5))
- Add support for rgb/rgba values with whitespaces around values (e.g. rgba( 255 , 100 , 50 , 0.1 )) and percentage values (e.g. rgb(100%, 67%, 15%, 0.8))
- Delete stroke and strokeWidth from fabric.Text (defined in fabric.Object)
- New unit test for parse stroke-opacity and fill-opacity
- Update unit tests (new tests for hsl/hsla and rgb/rgba (whitespaces and percentage values))
- Change equal and deepEqual parameter order (e.g. equal(actualValue, expectedValue, message))
- Doc additions
2013-05-25 09:03:09 +00:00
|
|
|
return _setStrokeFillOpacity(extend(parentAttributes, ownAttributes));
|
2012-10-14 00:53:12 +00:00
|
|
|
}
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
/**
|
2010-10-19 20:27:24 +00:00
|
|
|
* Parses "transform" attribute, returning an array of values
|
2010-06-09 22:34:55 +00:00
|
|
|
* @static
|
2010-10-14 21:42:39 +00:00
|
|
|
* @function
|
|
|
|
|
* @memberOf fabric
|
2010-06-09 22:34:55 +00:00
|
|
|
* @param attributeValue {String} string containing attribute value
|
|
|
|
|
* @return {Array} array of 6 elements representing transformation matrix
|
|
|
|
|
*/
|
2010-07-09 23:43:50 +00:00
|
|
|
fabric.parseTransformAttribute = (function() {
|
2010-06-09 22:34:55 +00:00
|
|
|
function rotateMatrix(matrix, args) {
|
|
|
|
|
var angle = args[0];
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
matrix[0] = Math.cos(angle);
|
|
|
|
|
matrix[1] = Math.sin(angle);
|
|
|
|
|
matrix[2] = -Math.sin(angle);
|
|
|
|
|
matrix[3] = Math.cos(angle);
|
|
|
|
|
}
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
function scaleMatrix(matrix, args) {
|
|
|
|
|
var multiplierX = args[0],
|
|
|
|
|
multiplierY = (args.length === 2) ? args[1] : args[0];
|
|
|
|
|
|
|
|
|
|
matrix[0] = multiplierX;
|
|
|
|
|
matrix[3] = multiplierY;
|
|
|
|
|
}
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
function skewXMatrix(matrix, args) {
|
|
|
|
|
matrix[2] = args[0];
|
|
|
|
|
}
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
function skewYMatrix(matrix, args) {
|
|
|
|
|
matrix[1] = args[0];
|
|
|
|
|
}
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
function translateMatrix(matrix, args) {
|
|
|
|
|
matrix[4] = args[0];
|
|
|
|
|
if (args.length === 2) {
|
|
|
|
|
matrix[5] = args[1];
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
// identity matrix
|
|
|
|
|
var iMatrix = [
|
|
|
|
|
1, // a
|
|
|
|
|
0, // b
|
|
|
|
|
0, // c
|
|
|
|
|
1, // d
|
|
|
|
|
0, // e
|
|
|
|
|
0 // f
|
|
|
|
|
],
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
// == begin transform regexp
|
|
|
|
|
number = '(?:[-+]?\\d+(?:\\.\\d+)?(?:e[-+]?\\d+)?)',
|
2013-11-05 10:59:48 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
comma_wsp = '(?:\\s+,?\\s*|,\\s*)',
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
skewX = '(?:(skewX)\\s*\\(\\s*(' + number + ')\\s*\\))',
|
2013-11-05 10:59:48 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
skewY = '(?:(skewY)\\s*\\(\\s*(' + number + ')\\s*\\))',
|
2013-11-05 10:59:48 +00:00
|
|
|
|
|
|
|
|
rotate = '(?:(rotate)\\s*\\(\\s*(' + number + ')(?:' +
|
|
|
|
|
comma_wsp + '(' + number + ')' +
|
|
|
|
|
comma_wsp + '(' + number + '))?\\s*\\))',
|
|
|
|
|
|
|
|
|
|
scale = '(?:(scale)\\s*\\(\\s*(' + number + ')(?:' +
|
|
|
|
|
comma_wsp + '(' + number + '))?\\s*\\))',
|
|
|
|
|
|
|
|
|
|
translate = '(?:(translate)\\s*\\(\\s*(' + number + ')(?:' +
|
|
|
|
|
comma_wsp + '(' + number + '))?\\s*\\))',
|
2012-01-17 13:41:58 +00:00
|
|
|
|
|
|
|
|
matrix = '(?:(matrix)\\s*\\(\\s*' +
|
|
|
|
|
'(' + number + ')' + comma_wsp +
|
|
|
|
|
'(' + number + ')' + comma_wsp +
|
2010-06-09 22:34:55 +00:00
|
|
|
'(' + number + ')' + comma_wsp +
|
|
|
|
|
'(' + number + ')' + comma_wsp +
|
|
|
|
|
'(' + number + ')' + comma_wsp +
|
2012-01-17 13:41:58 +00:00
|
|
|
'(' + number + ')' +
|
2010-06-09 22:34:55 +00:00
|
|
|
'\\s*\\))',
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
transform = '(?:' +
|
|
|
|
|
matrix + '|' +
|
|
|
|
|
translate + '|' +
|
|
|
|
|
scale + '|' +
|
|
|
|
|
rotate + '|' +
|
|
|
|
|
skewX + '|' +
|
2012-01-17 13:41:58 +00:00
|
|
|
skewY +
|
2010-06-09 22:34:55 +00:00
|
|
|
')',
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
transforms = '(?:' + transform + '(?:' + comma_wsp + transform + ')*' + ')',
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
transform_list = '^\\s*(?:' + transforms + '?)\\s*$',
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
// http://www.w3.org/TR/SVG/coords.html#TransformAttribute
|
|
|
|
|
reTransformList = new RegExp(transform_list),
|
|
|
|
|
// == end transform regexp
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2013-04-19 01:42:27 +00:00
|
|
|
reTransform = new RegExp(transform, 'g');
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
return function(attributeValue) {
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
// start with identity matrix
|
2010-07-09 14:21:40 +00:00
|
|
|
var matrix = iMatrix.concat();
|
2013-04-19 01:42:27 +00:00
|
|
|
var matrices = [ ];
|
2012-01-17 13:41:58 +00:00
|
|
|
|
|
|
|
|
// return if no argument was given or
|
2010-06-09 22:34:55 +00:00
|
|
|
// an argument does not match transform attribute regexp
|
|
|
|
|
if (!attributeValue || (attributeValue && !reTransformList.test(attributeValue))) {
|
|
|
|
|
return matrix;
|
|
|
|
|
}
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
attributeValue.replace(reTransform, function(match) {
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2010-06-17 14:00:47 +00:00
|
|
|
var m = new RegExp(transform).exec(match).filter(function (match) {
|
|
|
|
|
return (match !== '' && match != null);
|
2010-06-09 22:34:55 +00:00
|
|
|
}),
|
|
|
|
|
operation = m[1],
|
|
|
|
|
args = m.slice(2).map(parseFloat);
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
switch(operation) {
|
|
|
|
|
case 'translate':
|
|
|
|
|
translateMatrix(matrix, args);
|
|
|
|
|
break;
|
|
|
|
|
case 'rotate':
|
|
|
|
|
rotateMatrix(matrix, args);
|
|
|
|
|
break;
|
|
|
|
|
case 'scale':
|
|
|
|
|
scaleMatrix(matrix, args);
|
|
|
|
|
break;
|
|
|
|
|
case 'skewX':
|
|
|
|
|
skewXMatrix(matrix, args);
|
|
|
|
|
break;
|
|
|
|
|
case 'skewY':
|
|
|
|
|
skewYMatrix(matrix, args);
|
|
|
|
|
break;
|
|
|
|
|
case 'matrix':
|
|
|
|
|
matrix = args;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2013-04-19 01:42:27 +00:00
|
|
|
|
|
|
|
|
// snapshot current matrix into matrices array
|
|
|
|
|
matrices.push(matrix.concat());
|
|
|
|
|
// reset
|
|
|
|
|
matrix = iMatrix.concat();
|
2012-10-14 00:53:12 +00:00
|
|
|
});
|
2013-04-19 01:42:27 +00:00
|
|
|
|
|
|
|
|
var combinedMatrix = matrices[0];
|
|
|
|
|
while (matrices.length > 1) {
|
|
|
|
|
matrices.shift();
|
|
|
|
|
combinedMatrix = fabric.util.multiplyTransformMatrices(combinedMatrix, matrices[0]);
|
|
|
|
|
}
|
|
|
|
|
return combinedMatrix;
|
2012-10-14 00:53:12 +00:00
|
|
|
};
|
2010-06-09 22:34:55 +00:00
|
|
|
})();
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
/**
|
2010-10-19 20:27:24 +00:00
|
|
|
* Parses "points" attribute, returning an array of values
|
2010-06-09 22:34:55 +00:00
|
|
|
* @static
|
2010-10-14 21:42:39 +00:00
|
|
|
* @memberOf fabric
|
2010-06-09 22:34:55 +00:00
|
|
|
* @param points {String} points attribute string
|
|
|
|
|
* @return {Array} array of points
|
|
|
|
|
*/
|
|
|
|
|
function parsePointsAttribute(points) {
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
// points attribute is required and must not be empty
|
|
|
|
|
if (!points) return null;
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2010-09-08 20:35:06 +00:00
|
|
|
points = points.trim();
|
|
|
|
|
var asPairs = points.indexOf(',') > -1;
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2010-09-08 20:35:06 +00:00
|
|
|
points = points.split(/\s+/);
|
2012-10-14 00:53:12 +00:00
|
|
|
var parsedPoints = [ ], i, len;
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2010-09-08 20:35:06 +00:00
|
|
|
// points could look like "10,20 30,40" or "10 20 30 40"
|
2012-01-17 13:41:58 +00:00
|
|
|
if (asPairs) {
|
2012-10-14 00:53:12 +00:00
|
|
|
i = 0;
|
|
|
|
|
len = points.length;
|
|
|
|
|
for (; i < len; i++) {
|
|
|
|
|
var pair = points[i].split(',');
|
|
|
|
|
parsedPoints.push({ x: parseFloat(pair[0]), y: parseFloat(pair[1]) });
|
|
|
|
|
}
|
2010-09-08 20:35:06 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2012-10-14 00:53:12 +00:00
|
|
|
i = 0;
|
|
|
|
|
len = points.length;
|
|
|
|
|
for (; i < len; i+=2) {
|
2010-09-08 20:35:06 +00:00
|
|
|
parsedPoints.push({ x: parseFloat(points[i]), y: parseFloat(points[i+1]) });
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
// odd number of points is an error
|
|
|
|
|
if (parsedPoints.length % 2 !== 0) {
|
2010-09-08 20:35:06 +00:00
|
|
|
// return null;
|
2010-06-09 22:34:55 +00:00
|
|
|
}
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
return parsedPoints;
|
2012-10-14 00:53:12 +00:00
|
|
|
}
|
2010-06-09 22:34:55 +00:00
|
|
|
|
2013-05-12 17:01:23 +00:00
|
|
|
function parseFontDeclaration(value, oStyle) {
|
|
|
|
|
|
|
|
|
|
// TODO: support non-px font size
|
2013-07-11 18:39:06 +00:00
|
|
|
var match = value.match(/(normal|italic)?\s*(normal|small-caps)?\s*(normal|bold|bolder|lighter|100|200|300|400|500|600|700|800|900)?\s*(\d+)px(?:\/(normal|[\d\.]+))?\s+(.*)/);
|
2013-05-12 17:01:23 +00:00
|
|
|
|
|
|
|
|
if (!match) return;
|
|
|
|
|
|
|
|
|
|
var fontStyle = match[1];
|
|
|
|
|
// Font variant is not used
|
|
|
|
|
// var fontVariant = match[2];
|
|
|
|
|
var fontWeight = match[3];
|
|
|
|
|
var fontSize = match[4];
|
2013-07-11 18:39:06 +00:00
|
|
|
var lineHeight = match[5];
|
|
|
|
|
var fontFamily = match[6];
|
2013-05-12 17:01:23 +00:00
|
|
|
|
|
|
|
|
if (fontStyle) {
|
|
|
|
|
oStyle.fontStyle = fontStyle;
|
|
|
|
|
}
|
|
|
|
|
if (fontWeight) {
|
|
|
|
|
oStyle.fontSize = isNaN(parseFloat(fontWeight)) ? fontWeight : parseFloat(fontWeight);
|
|
|
|
|
}
|
|
|
|
|
if (fontSize) {
|
|
|
|
|
oStyle.fontSize = parseFloat(fontSize);
|
|
|
|
|
}
|
|
|
|
|
if (fontFamily) {
|
|
|
|
|
oStyle.fontFamily = fontFamily;
|
|
|
|
|
}
|
2013-07-11 18:39:06 +00:00
|
|
|
if (lineHeight) {
|
|
|
|
|
oStyle.lineHeight = lineHeight === 'normal' ? 1 : lineHeight;
|
|
|
|
|
}
|
2013-05-12 17:01:23 +00:00
|
|
|
}
|
|
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
/**
|
2010-10-19 20:27:24 +00:00
|
|
|
* Parses "style" attribute, retuning an object with values
|
2010-06-09 22:34:55 +00:00
|
|
|
* @static
|
2010-10-14 21:42:39 +00:00
|
|
|
* @memberOf fabric
|
|
|
|
|
* @param {SVGElement} element Element to parse
|
|
|
|
|
* @return {Object} Objects with values parsed from style attribute of an element
|
2010-06-09 22:34:55 +00:00
|
|
|
*/
|
|
|
|
|
function parseStyleAttribute(element) {
|
|
|
|
|
var oStyle = { },
|
2013-11-04 15:41:26 +00:00
|
|
|
style = element.getAttribute('style');
|
2012-10-14 00:53:12 +00:00
|
|
|
|
|
|
|
|
if (!style) return oStyle;
|
|
|
|
|
|
|
|
|
|
if (typeof style === 'string') {
|
2013-11-04 15:41:26 +00:00
|
|
|
parseStyleString(style, oStyle);
|
2012-10-14 00:53:12 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2013-11-04 15:41:26 +00:00
|
|
|
parseStyleObject(style, oStyle);
|
|
|
|
|
}
|
2012-12-23 14:18:46 +00:00
|
|
|
|
2013-11-04 15:41:26 +00:00
|
|
|
return oStyle;
|
|
|
|
|
}
|
2013-05-01 06:58:52 +00:00
|
|
|
|
2013-11-04 15:41:26 +00:00
|
|
|
/**
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
function parseStyleString(style, oStyle) {
|
|
|
|
|
var attr, value;
|
|
|
|
|
style.replace(/;$/, '').split(';').forEach(function (chunk) {
|
|
|
|
|
var pair = chunk.split(':');
|
|
|
|
|
|
|
|
|
|
attr = normalizeAttr(pair[0].trim().toLowerCase());
|
|
|
|
|
value = normalizeValue(attr, pair[1].trim());
|
|
|
|
|
|
|
|
|
|
if (attr === 'font') {
|
|
|
|
|
parseFontDeclaration(value, oStyle);
|
2010-06-09 22:34:55 +00:00
|
|
|
}
|
2013-11-04 15:41:26 +00:00
|
|
|
else {
|
|
|
|
|
oStyle[attr] = value;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2012-10-14 00:53:12 +00:00
|
|
|
|
2013-11-04 15:41:26 +00:00
|
|
|
/**
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
function parseStyleObject(style, oStyle) {
|
|
|
|
|
var attr, value;
|
|
|
|
|
for (var prop in style) {
|
|
|
|
|
if (typeof style[prop] === 'undefined') continue;
|
|
|
|
|
|
|
|
|
|
attr = normalizeAttr(prop.toLowerCase());
|
|
|
|
|
value = normalizeValue(attr, style[prop]);
|
|
|
|
|
|
|
|
|
|
if (attr === 'font') {
|
|
|
|
|
parseFontDeclaration(value, oStyle);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
oStyle[attr] = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-10-14 00:53:12 +00:00
|
|
|
}
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2011-01-09 06:38:54 +00:00
|
|
|
function resolveGradients(instances) {
|
|
|
|
|
for (var i = instances.length; i--; ) {
|
|
|
|
|
var instanceFillValue = instances[i].get('fill');
|
2011-02-11 06:44:08 +00:00
|
|
|
|
2011-01-09 06:38:54 +00:00
|
|
|
if (/^url\(/.test(instanceFillValue)) {
|
2011-02-11 06:44:08 +00:00
|
|
|
|
2011-01-09 06:38:54 +00:00
|
|
|
var gradientId = instanceFillValue.slice(5, instanceFillValue.length - 1);
|
2011-02-11 06:44:08 +00:00
|
|
|
|
2011-01-09 06:38:54 +00:00
|
|
|
if (fabric.gradientDefs[gradientId]) {
|
2011-02-11 06:44:08 +00:00
|
|
|
instances[i].set('fill',
|
2012-08-20 16:22:11 +00:00
|
|
|
fabric.Gradient.fromElement(fabric.gradientDefs[gradientId], instances[i]));
|
2011-01-09 06:38:54 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-06-09 22:34:55 +00:00
|
|
|
|
|
|
|
|
/**
|
2010-10-19 20:27:24 +00:00
|
|
|
* Transforms an array of svg elements to corresponding fabric.* instances
|
2010-06-09 22:34:55 +00:00
|
|
|
* @static
|
2010-10-14 21:42:39 +00:00
|
|
|
* @memberOf fabric
|
|
|
|
|
* @param {Array} elements Array of elements to parse
|
2011-01-08 00:53:19 +00:00
|
|
|
* @param {Function} callback Being passed an array of fabric instances (transformed from SVG elements)
|
2012-12-13 14:36:43 +00:00
|
|
|
* @param {Object} [options] Options object
|
2012-08-20 00:36:37 +00:00
|
|
|
* @param {Function} [reviver] Method for further parsing of SVG elements, called after each fabric object created.
|
2010-06-09 22:34:55 +00:00
|
|
|
*/
|
2012-08-19 19:44:34 +00:00
|
|
|
function parseElements(elements, callback, options, reviver) {
|
2013-11-04 16:12:05 +00:00
|
|
|
fabric.ElementsParser.parse(elements, callback, options, reviver);
|
2012-10-14 00:53:12 +00:00
|
|
|
}
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2011-01-19 07:30:35 +00:00
|
|
|
/**
|
|
|
|
|
* Returns CSS rules for a given SVG document
|
|
|
|
|
* @static
|
|
|
|
|
* @function
|
|
|
|
|
* @memberOf fabric
|
|
|
|
|
* @param {SVGDocument} doc SVG document to parse
|
|
|
|
|
* @return {Object} CSS rules of this document
|
|
|
|
|
*/
|
|
|
|
|
function getCSSRules(doc) {
|
|
|
|
|
var styles = doc.getElementsByTagName('style'),
|
|
|
|
|
allRules = { },
|
|
|
|
|
rules;
|
2012-01-17 13:41:58 +00:00
|
|
|
|
|
|
|
|
// very crude parsing of style contents
|
2011-01-19 07:30:35 +00:00
|
|
|
for (var i = 0, len = styles.length; i < len; i++) {
|
|
|
|
|
var styleContents = styles[0].textContent;
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2011-01-19 07:30:35 +00:00
|
|
|
// remove comments
|
|
|
|
|
styleContents = styleContents.replace(/\/\*[\s\S]*?\*\//g, '');
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2011-01-19 07:30:35 +00:00
|
|
|
rules = styleContents.match(/[^{]*\{[\s\S]*?\}/g);
|
2012-10-14 00:53:12 +00:00
|
|
|
rules = rules.map(function(rule) { return rule.trim(); });
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2011-01-19 07:30:35 +00:00
|
|
|
rules.forEach(function(rule) {
|
2012-10-14 00:53:12 +00:00
|
|
|
var match = rule.match(/([\s\S]*?)\s*\{([^}]*)\}/);
|
|
|
|
|
rule = match[1];
|
|
|
|
|
var declaration = match[2].trim(),
|
2011-01-19 07:30:35 +00:00
|
|
|
propertyValuePairs = declaration.replace(/;$/, '').split(/\s*;\s*/);
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2011-01-19 07:30:35 +00:00
|
|
|
if (!allRules[rule]) {
|
|
|
|
|
allRules[rule] = { };
|
|
|
|
|
}
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2011-01-19 07:30:35 +00:00
|
|
|
for (var i = 0, len = propertyValuePairs.length; i < len; i++) {
|
|
|
|
|
var pair = propertyValuePairs[i].split(/\s*:\s*/),
|
|
|
|
|
property = pair[0],
|
|
|
|
|
value = pair[1];
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2011-01-19 07:30:35 +00:00
|
|
|
allRules[rule][property] = value;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2011-01-19 07:30:35 +00:00
|
|
|
return allRules;
|
|
|
|
|
}
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2012-12-16 19:44:26 +00:00
|
|
|
/**
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
2011-01-19 07:30:35 +00:00
|
|
|
function getGlobalStylesForElement(element) {
|
|
|
|
|
var nodeName = element.nodeName,
|
|
|
|
|
className = element.getAttribute('class'),
|
|
|
|
|
id = element.getAttribute('id'),
|
|
|
|
|
styles = { };
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2011-01-19 07:30:35 +00:00
|
|
|
for (var rule in fabric.cssRules) {
|
|
|
|
|
var ruleMatchesElement = (className && new RegExp('^\\.' + className).test(rule)) ||
|
|
|
|
|
(id && new RegExp('^#' + id).test(rule)) ||
|
|
|
|
|
(new RegExp('^' + nodeName).test(rule));
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2011-01-19 07:30:35 +00:00
|
|
|
if (ruleMatchesElement) {
|
|
|
|
|
for (var property in fabric.cssRules[rule]) {
|
|
|
|
|
styles[property] = fabric.cssRules[rule][property];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2011-01-19 07:30:35 +00:00
|
|
|
return styles;
|
|
|
|
|
}
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
/**
|
2010-10-19 20:31:39 +00:00
|
|
|
* Parses an SVG document, converts it to an array of corresponding fabric.* instances and passes them to a callback
|
2010-06-09 22:34:55 +00:00
|
|
|
* @static
|
2010-10-14 21:42:39 +00:00
|
|
|
* @function
|
|
|
|
|
* @memberOf fabric
|
|
|
|
|
* @param {SVGDocument} doc SVG document to parse
|
|
|
|
|
* @param {Function} callback Callback to call when parsing is finished; It's being passed an array of elements (parsed from a document).
|
2012-08-20 00:36:37 +00:00
|
|
|
* @param {Function} [reviver] Method for further parsing of SVG elements, called after each fabric object created.
|
2010-06-09 22:34:55 +00:00
|
|
|
*/
|
2010-10-14 21:42:39 +00:00
|
|
|
fabric.parseSVGDocument = (function() {
|
2010-06-09 22:34:55 +00:00
|
|
|
|
2012-08-11 16:59:53 +00:00
|
|
|
var reAllowedSVGTagNames = /^(path|circle|polygon|polyline|ellipse|rect|line|image|text)$/;
|
2010-06-09 22:34:55 +00:00
|
|
|
|
|
|
|
|
// http://www.w3.org/TR/SVG/coords.html#ViewBoxAttribute
|
|
|
|
|
// \d doesn't quite cut it (as we need to match an actual float number)
|
|
|
|
|
|
|
|
|
|
// matches, e.g.: +14.56e-12, etc.
|
|
|
|
|
var reNum = '(?:[-+]?\\d+(?:\\.\\d+)?(?:e[-+]?\\d+)?)';
|
|
|
|
|
|
|
|
|
|
var reViewBoxAttrValue = new RegExp(
|
|
|
|
|
'^' +
|
|
|
|
|
'\\s*(' + reNum + '+)\\s*,?' +
|
|
|
|
|
'\\s*(' + reNum + '+)\\s*,?' +
|
|
|
|
|
'\\s*(' + reNum + '+)\\s*,?' +
|
|
|
|
|
'\\s*(' + reNum + '+)\\s*' +
|
|
|
|
|
'$'
|
|
|
|
|
);
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2011-01-06 20:20:56 +00:00
|
|
|
function hasAncestorWithNodeName(element, nodeName) {
|
2010-06-09 22:34:55 +00:00
|
|
|
while (element && (element = element.parentNode)) {
|
2011-01-06 20:20:56 +00:00
|
|
|
if (nodeName.test(element.nodeName)) {
|
2010-06-09 22:34:55 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-19 19:44:34 +00:00
|
|
|
return function(doc, callback, reviver) {
|
2010-06-09 22:34:55 +00:00
|
|
|
if (!doc) return;
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2011-02-09 05:14:12 +00:00
|
|
|
var startTime = new Date(),
|
|
|
|
|
descendants = fabric.util.toArray(doc.getElementsByTagName('*'));
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2011-08-16 23:05:17 +00:00
|
|
|
if (descendants.length === 0) {
|
|
|
|
|
// we're likely in node, where "o3-xml" library fails to gEBTN("*")
|
|
|
|
|
// https://github.com/ajaxorg/node-o3-xml/issues/21
|
|
|
|
|
descendants = doc.selectNodes("//*[name(.)!='svg']");
|
|
|
|
|
var arr = [ ];
|
|
|
|
|
for (var i = 0, len = descendants.length; i < len; i++) {
|
|
|
|
|
arr[i] = descendants[i];
|
|
|
|
|
}
|
|
|
|
|
descendants = arr;
|
|
|
|
|
}
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2010-06-17 14:00:47 +00:00
|
|
|
var elements = descendants.filter(function(el) {
|
2012-01-17 13:41:58 +00:00
|
|
|
return reAllowedSVGTagNames.test(el.tagName) &&
|
2011-01-06 20:20:56 +00:00
|
|
|
!hasAncestorWithNodeName(el, /^(?:pattern|defs)$/); // http://www.w3.org/TR/SVG/struct.html#DefsElement
|
2010-06-09 22:34:55 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!elements || (elements && !elements.length)) return;
|
|
|
|
|
|
|
|
|
|
var viewBoxAttr = doc.getAttribute('viewBox'),
|
|
|
|
|
widthAttr = doc.getAttribute('width'),
|
|
|
|
|
heightAttr = doc.getAttribute('height'),
|
|
|
|
|
width = null,
|
|
|
|
|
height = null,
|
|
|
|
|
minX,
|
|
|
|
|
minY;
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
if (viewBoxAttr && (viewBoxAttr = viewBoxAttr.match(reViewBoxAttrValue))) {
|
|
|
|
|
minX = parseInt(viewBoxAttr[1], 10);
|
|
|
|
|
minY = parseInt(viewBoxAttr[2], 10);
|
|
|
|
|
width = parseInt(viewBoxAttr[3], 10);
|
|
|
|
|
height = parseInt(viewBoxAttr[4], 10);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// values of width/height attributes overwrite those extracted from viewbox attribute
|
|
|
|
|
width = widthAttr ? parseFloat(widthAttr) : width;
|
|
|
|
|
height = heightAttr ? parseFloat(heightAttr) : height;
|
|
|
|
|
|
2012-01-17 13:41:58 +00:00
|
|
|
var options = {
|
|
|
|
|
width: width,
|
2010-06-09 22:34:55 +00:00
|
|
|
height: height
|
|
|
|
|
};
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2011-01-09 06:38:54 +00:00
|
|
|
fabric.gradientDefs = fabric.getGradientDefs(doc);
|
2011-01-19 07:30:35 +00:00
|
|
|
fabric.cssRules = getCSSRules(doc);
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2011-01-19 07:30:35 +00:00
|
|
|
// Precedence of rules: style > class > attribute
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2011-01-08 00:53:19 +00:00
|
|
|
fabric.parseElements(elements, function(instances) {
|
2011-02-09 05:14:12 +00:00
|
|
|
fabric.documentParsingTime = new Date() - startTime;
|
2011-01-08 00:53:19 +00:00
|
|
|
if (callback) {
|
|
|
|
|
callback(instances, options);
|
|
|
|
|
}
|
2012-08-19 19:44:34 +00:00
|
|
|
}, clone(options), reviver);
|
2010-06-09 22:34:55 +00:00
|
|
|
};
|
|
|
|
|
})();
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2013-04-24 16:58:04 +00:00
|
|
|
/**
|
2011-08-14 21:35:36 +00:00
|
|
|
* Used for caching SVG documents (loaded via `fabric.Canvas#loadSVGFromURL`)
|
|
|
|
|
* @namespace
|
|
|
|
|
*/
|
|
|
|
|
var svgCache = {
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2011-08-14 21:35:36 +00:00
|
|
|
/**
|
|
|
|
|
* @param {String} name
|
|
|
|
|
* @param {Function} callback
|
|
|
|
|
*/
|
2012-01-17 13:41:58 +00:00
|
|
|
has: function (name, callback) {
|
2011-08-14 21:35:36 +00:00
|
|
|
callback(false);
|
|
|
|
|
},
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2011-08-14 21:35:36 +00:00
|
|
|
/**
|
|
|
|
|
* @param {String} url
|
|
|
|
|
* @param {Function} callback
|
|
|
|
|
*/
|
2012-10-14 00:53:12 +00:00
|
|
|
get: function () {
|
2011-08-14 21:35:36 +00:00
|
|
|
/* NOOP */
|
|
|
|
|
},
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2011-08-14 21:35:36 +00:00
|
|
|
/**
|
|
|
|
|
* @param {String} url
|
|
|
|
|
* @param {Object} object
|
|
|
|
|
*/
|
2012-10-14 00:53:12 +00:00
|
|
|
set: function () {
|
2011-08-14 21:35:36 +00:00
|
|
|
/* NOOP */
|
|
|
|
|
}
|
|
|
|
|
};
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2011-08-14 21:35:36 +00:00
|
|
|
/**
|
2013-04-02 11:47:33 +00:00
|
|
|
* Takes url corresponding to an SVG document, and parses it into a set of fabric objects. Note that SVG is fetched via XMLHttpRequest, so it needs to conform to SOP (Same Origin Policy)
|
2012-11-23 12:38:13 +00:00
|
|
|
* @memberof fabric
|
2011-08-14 21:35:36 +00:00
|
|
|
* @param {String} url
|
|
|
|
|
* @param {Function} callback
|
2012-08-20 00:36:37 +00:00
|
|
|
* @param {Function} [reviver] Method for further parsing of SVG elements, called after each fabric object created.
|
2011-08-14 21:35:36 +00:00
|
|
|
*/
|
2012-08-19 19:44:34 +00:00
|
|
|
function loadSVGFromURL(url, callback, reviver) {
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2012-01-20 10:29:51 +00:00
|
|
|
url = url.replace(/^\n\s*/, '').trim();
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2011-08-14 21:35:36 +00:00
|
|
|
svgCache.has(url, function (hasUrl) {
|
|
|
|
|
if (hasUrl) {
|
|
|
|
|
svgCache.get(url, function (value) {
|
|
|
|
|
var enlivedRecord = _enlivenCachedObject(value);
|
|
|
|
|
callback(enlivedRecord.objects, enlivedRecord.options);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
new fabric.util.request(url, {
|
|
|
|
|
method: 'get',
|
|
|
|
|
onComplete: onComplete
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2011-08-14 21:35:36 +00:00
|
|
|
function onComplete(r) {
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2011-08-14 21:35:36 +00:00
|
|
|
var xml = r.responseXML;
|
2012-01-18 09:44:03 +00:00
|
|
|
if (!xml.documentElement && fabric.window.ActiveXObject && r.responseText) {
|
|
|
|
|
xml = new ActiveXObject('Microsoft.XMLDOM');
|
|
|
|
|
xml.async = 'false';
|
|
|
|
|
//IE chokes on DOCTYPE
|
|
|
|
|
xml.loadXML(r.responseText.replace(/<!DOCTYPE[\s\S]*?(\[[\s\S]*\])*?>/i,''));
|
|
|
|
|
}
|
|
|
|
|
if (!xml.documentElement) return;
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2012-01-18 09:44:03 +00:00
|
|
|
fabric.parseSVGDocument(xml.documentElement, function (results, options) {
|
2011-08-14 21:35:36 +00:00
|
|
|
svgCache.set(url, {
|
|
|
|
|
objects: fabric.util.array.invoke(results, 'toObject'),
|
|
|
|
|
options: options
|
|
|
|
|
});
|
|
|
|
|
callback(results, options);
|
2012-08-19 19:44:34 +00:00
|
|
|
}, reviver);
|
2011-08-14 21:35:36 +00:00
|
|
|
}
|
|
|
|
|
}
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2011-08-14 21:35:36 +00:00
|
|
|
/**
|
2013-04-24 16:58:04 +00:00
|
|
|
* @private
|
|
|
|
|
*/
|
2011-08-14 21:35:36 +00:00
|
|
|
function _enlivenCachedObject(cachedObject) {
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2011-08-14 21:35:36 +00:00
|
|
|
var objects = cachedObject.objects,
|
|
|
|
|
options = cachedObject.options;
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2011-08-14 21:35:36 +00:00
|
|
|
objects = objects.map(function (o) {
|
|
|
|
|
return fabric[capitalize(o.type)].fromObject(o);
|
|
|
|
|
});
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2011-08-14 21:35:36 +00:00
|
|
|
return ({ objects: objects, options: options });
|
|
|
|
|
}
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2011-08-14 21:35:36 +00:00
|
|
|
/**
|
|
|
|
|
* Takes string corresponding to an SVG document, and parses it into a set of fabric objects
|
2012-11-23 12:38:13 +00:00
|
|
|
* @memberof fabric
|
2011-08-14 21:35:36 +00:00
|
|
|
* @param {String} string
|
|
|
|
|
* @param {Function} callback
|
2012-08-20 00:36:37 +00:00
|
|
|
* @param {Function} [reviver] Method for further parsing of SVG elements, called after each fabric object created.
|
2011-08-14 21:35:36 +00:00
|
|
|
*/
|
2012-08-19 19:44:34 +00:00
|
|
|
function loadSVGFromString(string, callback, reviver) {
|
2011-08-14 21:35:36 +00:00
|
|
|
string = string.trim();
|
|
|
|
|
var doc;
|
|
|
|
|
if (typeof DOMParser !== 'undefined') {
|
|
|
|
|
var parser = new DOMParser();
|
|
|
|
|
if (parser && parser.parseFromString) {
|
|
|
|
|
doc = parser.parseFromString(string, 'text/xml');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (fabric.window.ActiveXObject) {
|
2012-10-14 00:53:12 +00:00
|
|
|
doc = new ActiveXObject('Microsoft.XMLDOM');
|
2012-01-17 16:29:45 +00:00
|
|
|
doc.async = 'false';
|
|
|
|
|
//IE chokes on DOCTYPE
|
|
|
|
|
doc.loadXML(string.replace(/<!DOCTYPE[\s\S]*?(\[[\s\S]*\])*?>/i,''));
|
2011-08-14 21:35:36 +00:00
|
|
|
}
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2011-08-14 21:35:36 +00:00
|
|
|
fabric.parseSVGDocument(doc.documentElement, function (results, options) {
|
|
|
|
|
callback(results, options);
|
2012-08-19 19:44:34 +00:00
|
|
|
}, reviver);
|
2011-08-14 21:35:36 +00:00
|
|
|
}
|
2012-01-02 21:14:20 +00:00
|
|
|
|
2012-12-16 19:44:26 +00:00
|
|
|
/**
|
|
|
|
|
* Creates markup containing SVG font faces
|
|
|
|
|
* @param {Array} objects Array of fabric objects
|
|
|
|
|
* @return {String}
|
|
|
|
|
*/
|
2012-01-02 21:14:20 +00:00
|
|
|
function createSVGFontFacesMarkup(objects) {
|
|
|
|
|
var markup = '';
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2012-01-02 21:14:20 +00:00
|
|
|
for (var i = 0, len = objects.length; i < len; i++) {
|
|
|
|
|
if (objects[i].type !== 'text' || !objects[i].path) continue;
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2012-01-02 21:14:20 +00:00
|
|
|
markup += [
|
|
|
|
|
'@font-face {',
|
|
|
|
|
'font-family: ', objects[i].fontFamily, '; ',
|
|
|
|
|
'src: url(\'', objects[i].path, '\')',
|
|
|
|
|
'}'
|
|
|
|
|
].join('');
|
|
|
|
|
}
|
2012-01-17 13:41:58 +00:00
|
|
|
|
2012-01-02 21:14:20 +00:00
|
|
|
if (markup) {
|
|
|
|
|
markup = [
|
2013-02-14 21:06:01 +00:00
|
|
|
'<style type="text/css">',
|
|
|
|
|
'<![CDATA[',
|
|
|
|
|
markup,
|
|
|
|
|
']]>',
|
2013-02-17 12:26:45 +00:00
|
|
|
'</style>'
|
2013-02-14 21:06:01 +00:00
|
|
|
].join('');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return markup;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates markup containing SVG referenced elements like patterns, gradients etc.
|
|
|
|
|
* @param {fabric.Canvas} canvas instance of fabric.Canvas
|
|
|
|
|
* @return {String}
|
|
|
|
|
*/
|
|
|
|
|
function createSVGRefElementsMarkup(canvas) {
|
2013-11-03 12:09:49 +00:00
|
|
|
var markup = [ ];
|
2013-02-14 21:06:01 +00:00
|
|
|
|
2013-11-03 12:09:49 +00:00
|
|
|
_createSVGPattern(markup, canvas, 'backgroundColor');
|
|
|
|
|
_createSVGPattern(markup, canvas, 'overlayColor');
|
|
|
|
|
|
|
|
|
|
return markup.join('');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
function _createSVGPattern(markup, canvas, property) {
|
|
|
|
|
if (canvas[property] && canvas[property].toSVG) {
|
|
|
|
|
markup.push(
|
|
|
|
|
'<pattern x="0" y="0" id="', property, 'Pattern" ',
|
|
|
|
|
'width="', canvas[property].source.width,
|
|
|
|
|
'" height="', canvas[property].source.height,
|
2013-02-14 21:06:01 +00:00
|
|
|
'" patternUnits="userSpaceOnUse">',
|
2013-02-17 12:26:45 +00:00
|
|
|
'<image x="0" y="0" ',
|
2013-11-03 12:09:49 +00:00
|
|
|
'width="', canvas[property].source.width,
|
|
|
|
|
'" height="', canvas[property].source.height,
|
|
|
|
|
'" xlink:href="', canvas[property].source.src,
|
2013-02-14 21:06:01 +00:00
|
|
|
'"></image></pattern>'
|
2013-11-03 12:09:49 +00:00
|
|
|
);
|
2012-01-02 21:14:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-30 19:55:26 +00:00
|
|
|
/**
|
|
|
|
|
* Parses an SVG document, returning all of the gradient declarations found in it
|
|
|
|
|
* @static
|
|
|
|
|
* @function
|
|
|
|
|
* @memberOf fabric
|
|
|
|
|
* @param {SVGDocument} doc SVG document to parse
|
|
|
|
|
* @return {Object} Gradient definitions; key corresponds to element id, value -- to gradient definition element
|
|
|
|
|
*/
|
|
|
|
|
function getGradientDefs(doc) {
|
|
|
|
|
var linearGradientEls = doc.getElementsByTagName('linearGradient'),
|
|
|
|
|
radialGradientEls = doc.getElementsByTagName('radialGradient'),
|
|
|
|
|
el, i,
|
|
|
|
|
gradientDefs = { };
|
|
|
|
|
|
|
|
|
|
i = linearGradientEls.length;
|
|
|
|
|
for (; i--; ) {
|
|
|
|
|
el = linearGradientEls[i];
|
|
|
|
|
gradientDefs[el.getAttribute('id')] = el;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
i = radialGradientEls.length;
|
|
|
|
|
for (; i--; ) {
|
|
|
|
|
el = radialGradientEls[i];
|
|
|
|
|
gradientDefs[el.getAttribute('id')] = el;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return gradientDefs;
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-02 21:14:20 +00:00
|
|
|
extend(fabric, {
|
|
|
|
|
|
2013-02-14 21:06:01 +00:00
|
|
|
parseAttributes: parseAttributes,
|
|
|
|
|
parseElements: parseElements,
|
|
|
|
|
parseStyleAttribute: parseStyleAttribute,
|
|
|
|
|
parsePointsAttribute: parsePointsAttribute,
|
|
|
|
|
getCSSRules: getCSSRules,
|
2012-01-02 21:14:20 +00:00
|
|
|
|
2013-02-14 21:06:01 +00:00
|
|
|
loadSVGFromURL: loadSVGFromURL,
|
|
|
|
|
loadSVGFromString: loadSVGFromString,
|
2012-01-02 21:14:20 +00:00
|
|
|
|
2013-02-14 21:06:01 +00:00
|
|
|
createSVGFontFacesMarkup: createSVGFontFacesMarkup,
|
2013-05-30 19:55:26 +00:00
|
|
|
createSVGRefElementsMarkup: createSVGRefElementsMarkup,
|
|
|
|
|
|
2013-11-04 16:12:05 +00:00
|
|
|
getGradientDefs: getGradientDefs,
|
|
|
|
|
resolveGradients: resolveGradients
|
2010-06-09 22:34:55 +00:00
|
|
|
});
|
2012-01-02 21:14:20 +00:00
|
|
|
|
2012-10-14 00:53:12 +00:00
|
|
|
})(typeof exports !== 'undefined' ? exports : this);
|