Skip to content

Commit

Permalink
various small svg fixes (fabricjs#5424)
Browse files Browse the repository at this point in the history
* various small svg fixes

* no debugger

* fontsize parsing fix - again

* added a visual test

* use min 9px

* added comment
  • Loading branch information
asturur authored Dec 9, 2018
1 parent cb9a43a commit 83682b1
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/elements_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fabric.ElementsParser = function(elements, callback, options, reviver, parsingOp
var _options;
_this.resolveGradient(obj, 'fill');
_this.resolveGradient(obj, 'stroke');
if (obj instanceof fabric.Image) {
if (obj instanceof fabric.Image && obj._originalElement) {
_options = obj.parsePreserveAspectRatioAttribute(el);
}
obj._removeTransformMatrix(_options);
Expand Down
16 changes: 10 additions & 6 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@
value = null;
}
else {
value = value.replace(/,/g, ' ').split(/\s+/).map(function(n) {
return parseFloat(n);
});
value = value.replace(/,/g, ' ').split(/\s+/).map(parseFloat);
}
}
else if (attr === 'transformMatrix') {
Expand Down Expand Up @@ -130,6 +128,9 @@
value = 'stroke';
}
}
else if (attr === 'href' || attr === 'xlink:href') {
return value;
}
else {
parsed = isArray ? value.map(parseUnit) : parseUnit(value, fontSize);
}
Expand Down Expand Up @@ -821,7 +822,7 @@

var value,
parentAttributes = { },
fontSize;
fontSize, parentFontSize;

if (typeof svgUid === 'undefined') {
svgUid = element.getAttribute('svgUid');
Expand All @@ -843,8 +844,11 @@
ownAttributes = extend(ownAttributes,
extend(getGlobalStylesForElement(element, svgUid), fabric.parseStyleAttribute(element)));

fontSize = (parentAttributes && parentAttributes.fontSize ) ||
ownAttributes['font-size'] || fabric.Text.DEFAULT_SVG_FONT_SIZE;
fontSize = parentFontSize = parentAttributes.fontSize || fabric.Text.DEFAULT_SVG_FONT_SIZE;
if (ownAttributes['font-size']) {
// looks like the minimum should be 9px when dealing with ems. this is what looks like in browsers.
ownAttributes['font-size'] = fontSize = parseUnit(ownAttributes['font-size'], parentFontSize);
}

var normalizedAttr, normalizedValue, normalizedStyle = {};
for (var attr in ownAttributes) {
Expand Down
8 changes: 3 additions & 5 deletions src/shapes/path.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -471,15 +471,13 @@
* of the instance
*/
_toSVG: function() {
var specificTransform = this._getOffsetTransform(),
path = this.path.map(function(path) {
return path.join(' ');
}).join(' ');
var path = this.path.map(function(path) {
return path.join(' ');
}).join(' ');
return [
'<path ', 'COMMON_PARTS',
'd="', path,
'" stroke-linecap="round" ',
'transform="' + specificTransform + '" ',
'/>\n'
];
},
Expand Down
6 changes: 3 additions & 3 deletions src/static_canvas.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@
* crossOrigin: 'anonymous'
* });
*/
// TODO: fix stretched examples
setBackgroundImage: function (image, callback, options) {
return this.__setBgOverlayImage('backgroundImage', image, callback, options);
},
Expand Down Expand Up @@ -1293,12 +1294,11 @@

this._setSVGPreamble(markup, options);
this._setSVGHeader(markup, options);

this._setSVGBgOverlayColor(markup, 'backgroundColor');
this._setSVGBgOverlayImage(markup, 'backgroundImage', reviver);
if (this.clipPath) {
markup.push('<g clip-path="url(#' + this.clipPath.clipPathId + ')" >\n');
}
this._setSVGBgOverlayColor(markup, 'backgroundColor');
this._setSVGBgOverlayImage(markup, 'backgroundImage', reviver);
this._setSVGObjects(markup, reviver);
if (this.clipPath) {
markup.push('</g>\n');
Expand Down
6 changes: 3 additions & 3 deletions test/unit/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
var done = assert.async();
makePathObject(function(path) {
assert.ok(typeof path.toSVG === 'function');
assert.deepEqual(path.toSVG(), '<g transform=\"matrix(1 0 0 1 200.5 200.5)\" >\n<path style=\"stroke: rgb(0,0,255); stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-dashoffset: 0; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(255,0,0); fill-rule: nonzero; opacity: 1;\" d=\"M 100 100 L 300 100 L 200 300 z\" stroke-linecap=\"round\" transform=\" translate(-200, -200)\" />\n</g>\n');
assert.deepEqual(path.toSVG(), '<g transform=\"matrix(1 0 0 1 200.5 200.5)\" >\n<path style=\"stroke: rgb(0,0,255); stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-dashoffset: 0; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(255,0,0); fill-rule: nonzero; opacity: 1;\" d=\"M 100 100 L 300 100 L 200 300 z\" stroke-linecap=\"round\" />\n</g>\n');
done();
});
});
Expand All @@ -139,7 +139,7 @@
makePathObject(function(path) {
makePathObject(function(path2) {
path.clipPath = path2;
assert.deepEqual(path.toSVG(), '<g transform=\"matrix(1 0 0 1 200.5 200.5)\" clip-path=\"url(#CLIPPATH_0)\" >\n<clipPath id=\"CLIPPATH_0\" >\n\t<path transform=\"matrix(1 0 0 1 200.5 200.5) translate(-200, -200)\" d=\"M 100 100 L 300 100 L 200 300 z\" stroke-linecap=\"round\" transform=\" translate(-200, -200)\" />\n</clipPath>\n<path style=\"stroke: rgb(0,0,255); stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-dashoffset: 0; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(255,0,0); fill-rule: nonzero; opacity: 1;\" d=\"M 100 100 L 300 100 L 200 300 z\" stroke-linecap=\"round\" transform=\" translate(-200, -200)\" />\n</g>\n', 'path clipPath toSVG should match');
assert.deepEqual(path.toSVG(), '<g transform=\"matrix(1 0 0 1 200.5 200.5)\" clip-path=\"url(#CLIPPATH_0)\" >\n<clipPath id=\"CLIPPATH_0\" >\n\t<path transform=\"matrix(1 0 0 1 200.5 200.5) translate(-200, -200)\" d=\"M 100 100 L 300 100 L 200 300 z\" stroke-linecap=\"round\" />\n</clipPath>\n<path style=\"stroke: rgb(0,0,255); stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-dashoffset: 0; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(255,0,0); fill-rule: nonzero; opacity: 1;\" d=\"M 100 100 L 300 100 L 200 300 z\" stroke-linecap=\"round\" />\n</g>\n', 'path clipPath toSVG should match');
done();
});
});
Expand All @@ -152,7 +152,7 @@
makePathObject(function(path2) {
path.clipPath = path2;
path.clipPath.absolutePositioned = true;
assert.deepEqual(path.toSVG(), '<g clip-path=\"url(#CLIPPATH_0)\" >\n<g transform=\"matrix(1 0 0 1 200.5 200.5)\" >\n<clipPath id=\"CLIPPATH_0\" >\n\t<path transform=\"matrix(1 0 0 1 200.5 200.5) translate(-200, -200)\" d=\"M 100 100 L 300 100 L 200 300 z\" stroke-linecap=\"round\" transform=\" translate(-200, -200)\" />\n</clipPath>\n<path style=\"stroke: rgb(0,0,255); stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-dashoffset: 0; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(255,0,0); fill-rule: nonzero; opacity: 1;\" d=\"M 100 100 L 300 100 L 200 300 z\" stroke-linecap=\"round\" transform=\" translate(-200, -200)\" />\n</g>\n</g>\n', 'path clipPath toSVG absolute should match');
assert.deepEqual(path.toSVG(), '<g clip-path=\"url(#CLIPPATH_0)\" >\n<g transform=\"matrix(1 0 0 1 200.5 200.5)\" >\n<clipPath id=\"CLIPPATH_0\" >\n\t<path transform=\"matrix(1 0 0 1 200.5 200.5) translate(-200, -200)\" d=\"M 100 100 L 300 100 L 200 300 z\" stroke-linecap=\"round\" />\n</clipPath>\n<path style=\"stroke: rgb(0,0,255); stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-dashoffset: 0; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(255,0,0); fill-rule: nonzero; opacity: 1;\" d=\"M 100 100 L 300 100 L 200 300 z\" stroke-linecap=\"round\" />\n</g>\n</g>\n', 'path clipPath toSVG absolute should match');
done();
});
});
Expand Down
6 changes: 6 additions & 0 deletions test/visual/assets/svg_text_letterspacing.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/visual/golden/svg_text_letterspacing.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions test/visual/svg_import.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
'svg_radial_11',
'svg_radial_12',
'svg_radial_13',
'svg_text_letterspacing',
].map(createTestFromSVG);

tests.forEach(visualTestLoop(fabricCanvas, QUnit));
Expand Down

0 comments on commit 83682b1

Please sign in to comment.