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

Fix svg base markup #2364

Merged
merged 1 commit into from
Aug 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 9 additions & 1 deletion src/mixins/object.svg_export.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fabric.util.object.extend(fabric.Object.prototype, /** @lends fabric.Object.prot
opacity = typeof this.opacity !== 'undefined' ? this.opacity : '1',

visibility = this.visible ? '' : ' visibility: hidden;',
filter = this.shadow ? 'filter: url(#SVGID_' + this.shadow.id + ');' : '';
filter = this.getSvgFilter();

return [
'stroke: ', stroke, '; ',
Expand All @@ -40,6 +40,14 @@ fabric.util.object.extend(fabric.Object.prototype, /** @lends fabric.Object.prot
].join('');
},

/**
* Returns filter for svg shadow
* @return {String}
*/
getSvgFilter: function() {
return this.shadow ? 'filter: url(#SVGID_' + this.shadow.id + ');' : '';
},

/**
* Returns transform-string for svg-export
* @return {String}
Expand Down
14 changes: 7 additions & 7 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -647,15 +647,15 @@
function _createSVGPattern(markup, canvas, property) {
if (canvas[property] && canvas[property].toSVG) {
markup.push(
'<pattern x="0" y="0" id="', property, 'Pattern" ',
'\t<pattern x="0" y="0" id="', property, 'Pattern" ',
'width="', canvas[property].source.width,
'" height="', canvas[property].source.height,
'" patternUnits="userSpaceOnUse">',
'<image x="0" y="0" ',
'" patternUnits="userSpaceOnUse">\n',
'\t\t<image x="0" y="0" ',
'width="', canvas[property].source.width,
'" height="', canvas[property].source.height,
'" xlink:href="', canvas[property].source.src,
'"></image></pattern>'
'"></image>\n\t</pattern>\n'
);
}
}
Expand Down Expand Up @@ -1018,19 +1018,19 @@
'@font-face {',
'font-family: ', objects[i].fontFamily, '; ',
'src: url(\'', objects[i].path, '\')',
'}'
'}\n'
//jscs:enable validateIndentation
].join('');
}

if (markup) {
markup = [
//jscs:disable validateIndentation
'<style type="text/css">',
'\t<style type="text/css">',
'<![CDATA[',
markup,
']]>',
'</style>'
'</style>\n'
//jscs:enable validateIndentation
].join('');
}
Expand Down
18 changes: 11 additions & 7 deletions src/shapes/group.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@
ctx.transform.apply(ctx, this.transformMatrix);
}
this.transform(ctx);
this._setShadow(ctx);
Copy link
Member Author

Choose a reason for hiding this comment

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

Added shadow rendering for whole group.

this.clipTo && fabric.util.clipContext(this, ctx);
// the array is now sorted in order of highest first, so start from end
for (var i = 0, len = this._objects.length; i < len; i++) {
Expand Down Expand Up @@ -531,16 +532,19 @@
* @return {String} svg representation of an instance
*/
toSVG: function(reviver) {
var markup = [
//jscs:disable validateIndentation
'<g ',
'transform="', this.getSvgTransform(),
var markup = this._createBaseSVGMarkup();
Copy link
Member

Choose a reason for hiding this comment

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

Since we're using createBaseSVGMarkup everywhere, perhaps we should just do it in fabric.Object#toSVG and then inherit method via callSuper.

markup.push(
'<g transform="',
/* avoiding styles intentionally */
this.getSvgTransform(),
this.getSvgTransformMatrix(),
'" style="',
this.getSvgFilter(),
'">\n'
//jscs:enable validateIndentation
];
);

for (var i = 0, len = this._objects.length; i < len; i++) {
markup.push(this._objects[i].toSVG(reviver));
markup.push('\t', this._objects[i].toSVG(reviver));
}

markup.push('</g>\n');
Expand Down
2 changes: 1 addition & 1 deletion src/shapes/image.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@
* @return {String} svg representation of an instance
*/
toSVG: function(reviver) {
var markup = [], x = -this.width / 2, y = -this.height / 2,
var markup = this._createBaseSVGMarkup(), x = -this.width / 2, y = -this.height / 2,
preserveAspectRatio = 'none';
if (this.group && this.group.type === 'path-group') {
x = this.left;
Expand Down
26 changes: 15 additions & 11 deletions src/shapes/path_group.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,17 +175,16 @@
var objects = this.getObjects(),
p = this.getPointByOrigin('left', 'top'),
translatePart = 'translate(' + p.x + ' ' + p.y + ')',
markup = [
//jscs:disable validateIndentation
'<g ',
'style="', this.getSvgStyles(), '" ',
'transform="', this.getSvgTransformMatrix(), translatePart, this.getSvgTransform(), '" ',
'>\n'
//jscs:enable validateIndentation
];
markup = this._createBaseSVGMarkup();
markup.push(
'<g ',
'style="', this.getSvgStyles(), '" ',
'transform="', this.getSvgTransformMatrix(), translatePart, this.getSvgTransform(), '" ',
'>\n'
);

for (var i = 0, len = objects.length; i < len; i++) {
markup.push(objects[i].toSVG(reviver));
markup.push('\t', objects[i].toSVG(reviver));
}
markup.push('</g>\n');

Expand All @@ -207,9 +206,14 @@
* @return {Boolean} true if all paths are of the same color (`fill`)
*/
isSameColor: function() {
Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed this function, but i'm more for deleting it or improve the patGroup._set method that as of now is useless.
(it checks if all the paths are same color and if yes it change all the paths filling, some sort of ancient delegated properties that as of now feels unfinished work).

Copy link
Member

Choose a reason for hiding this comment

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

I agree that it's an ancient method that is hardly useful

var firstPathFill = (this.getObjects()[0].get('fill') || '').toLowerCase();
var firstPathFill = this.getObjects()[0].get('fill') || '';
if (typeof firstPathFill !== 'string') {
return false;
}
firstPathFill = firstPathFill.toLowerCase();
return this.getObjects().every(function(path) {
return (path.get('fill') || '').toLowerCase() === firstPathFill;
var pathFill = path.get('fill') || '';
return typeof pathFill === 'string' && (pathFill).toLowerCase() === firstPathFill;
});
},

Expand Down
12 changes: 6 additions & 6 deletions src/static_canvas.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -1280,7 +1280,7 @@
_setSVGPreamble: function(markup, options) {
if (!options.suppressPreamble) {
markup.push(
'<?xml version="1.0" encoding="', (options.encoding || 'UTF-8'), '" standalone="no" ?>',
'<?xml version="1.0" encoding="', (options.encoding || 'UTF-8'), '" standalone="no" ?>\n',
'<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" ',
'"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n'
);
Expand Down Expand Up @@ -1324,12 +1324,12 @@
options.viewBox.width + ' ' +
options.viewBox.height + '" '
: null),
'xml:space="preserve">',
'<desc>Created with Fabric.js ', fabric.version, '</desc>',
'xml:space="preserve">\n',
'<desc>Created with Fabric.js ', fabric.version, '</desc>\n',
'<defs>',
fabric.createSVGFontFacesMarkup(this.getObjects()),
fabric.createSVGRefElementsMarkup(this),
'</defs>'
'</defs>\n'
);
},

Expand Down Expand Up @@ -1372,7 +1372,7 @@
? this[property].source.height
: this.height),
'" fill="url(#' + property + 'Pattern)"',
'></rect>'
'></rect>\n'
);
}
else if (this[property] && property === 'overlayColor') {
Expand All @@ -1381,7 +1381,7 @@
'width="', this.width,
'" height="', this.height,
'" fill="', this[property], '"',
'></rect>'
'></rect>\n'
);
}
},
Expand Down
8 changes: 4 additions & 4 deletions test/unit/canvas_static.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

// var emptyImageCanvasData = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAfQAAAH0CAYAAADL1t+KAAAH7ElEQVR4nO3VMQ0AMAzAsPInvYHoMS2yEeTLHADge/M6AADYM3QACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACD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AIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIuMjH4b7osLFBAAAAAElFTkSuQmCC";

var CANVAS_SVG = '<?xml version="1.0" encoding="UTF-8" standalone="no" ?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n'+
'<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="600" height="600" xml:space="preserve"><desc>Created with Fabric.js ' + fabric.version + '</desc><defs></defs></svg>';
var CANVAS_SVG = '<?xml version="1.0" encoding="UTF-8" standalone="no" ?>\n<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n'+
'<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="600" height="600" xml:space="preserve">\n<desc>Created with Fabric.js ' + fabric.version + '</desc>\n<defs></defs>\n</svg>';

var CANVAS_SVG_VIEWBOX = '<?xml version="1.0" encoding="UTF-8" standalone="no" ?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n'+
'<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="300" height="300" viewBox="100 100 300 300" xml:space="preserve"><desc>Created with Fabric.js ' + fabric.version + '</desc><defs></defs></svg>';
var CANVAS_SVG_VIEWBOX = '<?xml version="1.0" encoding="UTF-8" standalone="no" ?>\n<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n'+
'<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="300" height="300" viewBox="100 100 300 300" xml:space="preserve">\n<desc>Created with Fabric.js ' + fabric.version + '</desc>\n<defs></defs>\n</svg>';

var PATH_JSON = '{"objects": [{"type": "path", "originX": "left", "originY": "top", "left": 268, "top": 266, "width": 51, "height": 49,'+
' "fill": "rgb(0,0,0)", "stroke": null, "strokeWidth": 1, "scaleX": 1, "scaleY": 1, '+
Expand Down
2 changes: 1 addition & 1 deletion test/unit/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ test('toObject without default values', function() {
var group = makeGroupWith2Objects();
ok(typeof group.toSVG == 'function');

var expectedSVG = '<g transform="translate(90 130)">\n<rect x="-15" y="-5" rx="0" ry="0" width="30" height="10" style="stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(0,0,0); fill-rule: nonzero; opacity: 1;" transform="translate(25 -25)"/>\n<rect x="-5" y="-20" rx="0" ry="0" width="10" height="40" style="stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(0,0,0); fill-rule: nonzero; opacity: 1;" transform="translate(-35 10)"/>\n</g>\n';
var expectedSVG = '<g transform="translate(90 130)" style="">\n\t<rect x="-15" y="-5" rx="0" ry="0" width="30" height="10" style="stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(0,0,0); fill-rule: nonzero; opacity: 1;" transform="translate(25 -25)"/>\n\t<rect x="-5" y="-20" rx="0" ry="0" width="10" height="40" style="stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(0,0,0); fill-rule: nonzero; opacity: 1;" transform="translate(-35 10)"/>\n</g>\n';
equal(group.toSVG(), expectedSVG);
});

Expand Down
8 changes: 4 additions & 4 deletions test/unit/path_group.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@
};

var REFERENCE_PATH_GROUP_SVG = '<g style="stroke: none; stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: none; fill-rule: nonzero; opacity: 1;" transform="translate(0 0)" >\n' +
'<path d="M 100 100 L 300 100 L 200 300 z" style="stroke: blue; stroke-width: 3; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(255,0,0); fill-rule: nonzero; opacity: 1;" transform="" stroke-linecap="round" />\n' +
'<path d="M 200 200 L 100 200 L 400 50 z" style="stroke: blue; stroke-width: 3; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(255,0,0); fill-rule: nonzero; opacity: 1;" transform="" stroke-linecap="round" />\n' +
'\t<path d="M 100 100 L 300 100 L 200 300 z" style="stroke: blue; stroke-width: 3; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(255,0,0); fill-rule: nonzero; opacity: 1;" transform="" stroke-linecap="round" />\n' +
'\t<path d="M 200 200 L 100 200 L 400 50 z" style="stroke: blue; stroke-width: 3; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(255,0,0); fill-rule: nonzero; opacity: 1;" transform="" stroke-linecap="round" />\n' +
'</g>\n';

var REFERENCE_PATH_GROUP_SVG_WITH_MATRIX = '<g style="stroke: none; stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: none; fill-rule: nonzero; opacity: 1;" transform=" matrix(1 2 3 4 5 6) translate(0 0)" >\n' +
'<path d="M 100 100 L 300 100 L 200 300 z" style="stroke: blue; stroke-width: 3; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(255,0,0); fill-rule: nonzero; opacity: 1;" transform="" stroke-linecap="round" />\n' +
'<path d="M 200 200 L 100 200 L 400 50 z" style="stroke: blue; stroke-width: 3; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(255,0,0); fill-rule: nonzero; opacity: 1;" transform="" stroke-linecap="round" />\n' +
'\t<path d="M 100 100 L 300 100 L 200 300 z" style="stroke: blue; stroke-width: 3; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(255,0,0); fill-rule: nonzero; opacity: 1;" transform="" stroke-linecap="round" />\n' +
'\t<path d="M 200 200 L 100 200 L 400 50 z" style="stroke: blue; stroke-width: 3; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(255,0,0); fill-rule: nonzero; opacity: 1;" transform="" stroke-linecap="round" />\n' +
'</g>\n';

function getPathElement(path) {
Expand Down