Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clippath transform parsing #5983

Merged
merged 4 commits into from
Nov 20, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions HEADER.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ fabric.SHARED_ATTRIBUTES = [
*/
fabric.DPI = 96;
fabric.reNum = '(?:[-+]?(?:\\d+|\\d*\\.\\d+)(?:[eE][-+]?\\d+)?)';
fabric.rePathCommand = /([-+]?((\d+\.\d+)|((\d+)|(\.\d+)))(?:[eE][-+]?\d+)?)/ig;
fabric.fontPaths = { };
fabric.iMatrix = [1, 0, 0, 1, 0, 0];

Expand Down
22 changes: 15 additions & 7 deletions src/elements_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,21 @@ fabric.ElementsParser = function(elements, callback, options, reviver, parsingOp
_options = obj.parsePreserveAspectRatioAttribute(el);
}
obj._removeTransformMatrix(_options);
_this.resolveClipPath(obj);
_this.resolveClipPath(obj, el);
_this.reviver && _this.reviver(el, obj);
_this.instances[index] = obj;
_this.checkIfDone();
};
};

proto.extractPropertyDefinition = function(obj, property, storage) {
var value = obj[property];
if (!(/^url\(/).test(value)) {
var value = obj[property], regex = this.regexUrl;
if (!regex.test(value)) {
return;
}
var id = this.regexUrl.exec(value)[1];
this.regexUrl.lastIndex = 0;
regex.lastIndex = 0;
var id = regex.exec(value)[1];
regex.lastIndex = 0;
return fabric[storage][this.svgUid][id];
};

Expand All @@ -86,12 +87,19 @@ fabric.ElementsParser = function(elements, callback, options, reviver, parsingOp
};
};

proto.resolveClipPath = function(obj) {
proto.resolveClipPath = function(obj, usingElement) {
var clipPath = this.extractPropertyDefinition(obj, 'clipPath', 'clipPaths'),
element, klass, objTransformInv, container, gTransform, options;
if (clipPath) {
container = [];
objTransformInv = fabric.util.invertTransform(obj.calcTransformMatrix());
// move the clipPath tag as sibling to the real element that is using it
var clipPathTag = clipPath[0].parentNode;
var clipPathOwner = usingElement;
while (clipPathOwner.getAttribute('clip-path') !== obj.clipPath && clipPathOwner.parentNode) {
clipPathOwner = clipPathOwner.parentNode;
}
clipPathOwner.parentNode.appendChild(clipPathTag);
for (var i = 0; i < clipPath.length; i++) {
element = clipPath[i];
klass = this.findTag(element);
Expand All @@ -112,7 +120,7 @@ fabric.ElementsParser = function(elements, callback, options, reviver, parsingOp
clipPath.calcTransformMatrix()
);
if (clipPath.clipPath) {
this.resolveClipPath(clipPath);
this.resolveClipPath(clipPath, clipPathOwner.parentNode);
}
var options = fabric.util.qrDecompose(gTransform);
clipPath.flipX = false;
Expand Down
2 changes: 1 addition & 1 deletion src/shapes/path.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@
coords = [],
currentPath,
parsed,
re = /([-+]?((\d+\.\d+)|((\d+)|(\.\d+)))(?:e[-+]?\d+)?)/ig,
re = fabric.rePathCommand,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this saved a bunch of memory GC during a high number of path parsing.

match,
coordsStr;

Expand Down