mirror of
https://github.com/Hopiu/fabric.js.git
synced 2026-04-13 19:00:59 +00:00
Merge pull request #878 from Kienz/patch-1
Add reviver function to `fabric.Canvas.toSVG` - Closes #690
This commit is contained in:
commit
bfcd95b7a4
14 changed files with 93 additions and 34 deletions
|
|
@ -67,9 +67,10 @@
|
|||
/* _TO_SVG_START_ */
|
||||
/**
|
||||
* Returns svg representation of an instance
|
||||
* @param {Function} [reviver] Method for further parsing of svg representation.
|
||||
* @return {String} svg representation of an instance
|
||||
*/
|
||||
toSVG: function() {
|
||||
toSVG: function(reviver) {
|
||||
var markup = this._createBaseSVGMarkup();
|
||||
|
||||
markup.push(
|
||||
|
|
@ -81,7 +82,7 @@
|
|||
'"/>'
|
||||
);
|
||||
|
||||
return markup.join('');
|
||||
return reviver ? reviver(markup.join('')) : markup.join('');
|
||||
},
|
||||
/* _TO_SVG_END_ */
|
||||
|
||||
|
|
|
|||
|
|
@ -72,9 +72,10 @@
|
|||
/* _TO_SVG_START_ */
|
||||
/**
|
||||
* Returns svg representation of an instance
|
||||
* @param {Function} [reviver] Method for further parsing of svg representation.
|
||||
* @return {String} svg representation of an instance
|
||||
*/
|
||||
toSVG: function() {
|
||||
toSVG: function(reviver) {
|
||||
var markup = this._createBaseSVGMarkup();
|
||||
|
||||
markup.push(
|
||||
|
|
@ -86,7 +87,7 @@
|
|||
'"/>'
|
||||
);
|
||||
|
||||
return markup.join('');
|
||||
return reviver ? reviver(markup.join('')) : markup.join('');
|
||||
},
|
||||
/* _TO_SVG_END_ */
|
||||
|
||||
|
|
|
|||
|
|
@ -425,18 +425,23 @@
|
|||
/* _TO_SVG_START_ */
|
||||
/**
|
||||
* Returns svg representation of an instance
|
||||
* @param {Function} [reviver] Method for further parsing of svg representation.
|
||||
* @return {String} svg representation of an instance
|
||||
*/
|
||||
toSVG: function() {
|
||||
var objectsMarkup = [ ];
|
||||
toSVG: function(reviver) {
|
||||
var markup = [
|
||||
'<g ',
|
||||
'transform="', this.getSvgTransform(),
|
||||
'">'
|
||||
];
|
||||
|
||||
for (var i = 0, len = this._objects.length; i < len; i++) {
|
||||
objectsMarkup.push(this._objects[i].toSVG());
|
||||
markup.push(this._objects[i].toSVG(reviver));
|
||||
}
|
||||
|
||||
return (
|
||||
'<g transform="' + this.getSvgTransform() + '">' +
|
||||
objectsMarkup.join('') +
|
||||
'</g>');
|
||||
markup.push('</g>');
|
||||
|
||||
return reviver ? reviver(markup.join('')) : markup.join('');
|
||||
},
|
||||
/* _TO_SVG_END_ */
|
||||
|
||||
|
|
|
|||
|
|
@ -197,9 +197,10 @@
|
|||
/* _TO_SVG_START_ */
|
||||
/**
|
||||
* Returns SVG representation of an instance
|
||||
* @param {Function} [reviver] Method for further parsing of svg representation.
|
||||
* @return {String} svg representation of an instance
|
||||
*/
|
||||
toSVG: function() {
|
||||
toSVG: function(reviver) {
|
||||
var markup = [];
|
||||
|
||||
markup.push(
|
||||
|
|
@ -230,7 +231,7 @@
|
|||
|
||||
markup.push('</g>');
|
||||
|
||||
return markup.join('');
|
||||
return reviver ? reviver(markup.join('')) : markup.join('');
|
||||
},
|
||||
/* _TO_SVG_END_ */
|
||||
|
||||
|
|
|
|||
|
|
@ -149,9 +149,10 @@
|
|||
/* _TO_SVG_START_ */
|
||||
/**
|
||||
* Returns SVG representation of an instance
|
||||
* @param {Function} [reviver] Method for further parsing of svg representation.
|
||||
* @return {String} svg representation of an instance
|
||||
*/
|
||||
toSVG: function() {
|
||||
toSVG: function(reviver) {
|
||||
var markup = this._createBaseSVGMarkup();
|
||||
|
||||
markup.push(
|
||||
|
|
@ -164,7 +165,7 @@
|
|||
'"/>'
|
||||
);
|
||||
|
||||
return markup.join('');
|
||||
return reviver ? reviver(markup.join('')) : markup.join('');
|
||||
},
|
||||
/* _TO_SVG_END_ */
|
||||
|
||||
|
|
|
|||
|
|
@ -517,9 +517,10 @@
|
|||
/* _TO_SVG_START_ */
|
||||
/**
|
||||
* Returns svg representation of an instance
|
||||
* @param {Function} [reviver] Method for further parsing of svg representation.
|
||||
* @return {String} svg representation of an instance
|
||||
*/
|
||||
toSVG: function() {
|
||||
toSVG: function(reviver) {
|
||||
var chunks = [],
|
||||
markup = this._createBaseSVGMarkup();
|
||||
|
||||
|
|
@ -539,7 +540,7 @@
|
|||
'</g>'
|
||||
);
|
||||
|
||||
return markup.join('');
|
||||
return reviver ? reviver(markup.join('')) : markup.join('');
|
||||
},
|
||||
/* _TO_SVG_END_ */
|
||||
|
||||
|
|
|
|||
|
|
@ -138,9 +138,10 @@
|
|||
/* _TO_SVG_START_ */
|
||||
/**
|
||||
* Returns svg representation of an instance
|
||||
* @param {Function} [reviver] Method for further parsing of svg representation.
|
||||
* @return {String} svg representation of an instance
|
||||
*/
|
||||
toSVG: function() {
|
||||
toSVG: function(reviver) {
|
||||
var objects = this.getObjects();
|
||||
var markup = [
|
||||
'<g ',
|
||||
|
|
@ -150,11 +151,11 @@
|
|||
];
|
||||
|
||||
for (var i = 0, len = objects.length; i < len; i++) {
|
||||
markup.push(objects[i].toSVG());
|
||||
markup.push(objects[i].toSVG(reviver));
|
||||
}
|
||||
markup.push('</g>');
|
||||
|
||||
return markup.join('');
|
||||
return reviver ? reviver(markup.join('')) : markup.join('');
|
||||
},
|
||||
/* _TO_SVG_END_ */
|
||||
|
||||
|
|
|
|||
|
|
@ -85,9 +85,10 @@
|
|||
/* _TO_SVG_START_ */
|
||||
/**
|
||||
* Returns svg representation of an instance
|
||||
* @param {Function} [reviver] Method for further parsing of svg representation.
|
||||
* @return {String} svg representation of an instance
|
||||
*/
|
||||
toSVG: function() {
|
||||
toSVG: function(reviver) {
|
||||
var points = [],
|
||||
markup = this._createBaseSVGMarkup();
|
||||
|
||||
|
|
@ -103,7 +104,7 @@
|
|||
'"/>'
|
||||
);
|
||||
|
||||
return markup.join('');
|
||||
return reviver ? reviver(markup.join('')) : markup.join('');
|
||||
},
|
||||
/* _TO_SVG_END_ */
|
||||
|
||||
|
|
|
|||
|
|
@ -59,9 +59,10 @@
|
|||
/* _TO_SVG_START_ */
|
||||
/**
|
||||
* Returns SVG representation of an instance
|
||||
* @param {Function} [reviver] Method for further parsing of svg representation.
|
||||
* @return {String} svg representation of an instance
|
||||
*/
|
||||
toSVG: function() {
|
||||
toSVG: function(reviver) {
|
||||
var points = [],
|
||||
markup = this._createBaseSVGMarkup();
|
||||
|
||||
|
|
@ -77,7 +78,7 @@
|
|||
'"/>'
|
||||
);
|
||||
|
||||
return markup.join('');
|
||||
return reviver ? reviver(markup.join('')) : markup.join('');
|
||||
},
|
||||
/* _TO_SVG_END_ */
|
||||
|
||||
|
|
|
|||
|
|
@ -194,9 +194,10 @@
|
|||
/* _TO_SVG_START_ */
|
||||
/**
|
||||
* Returns svg representation of an instance
|
||||
* @param {Function} [reviver] Method for further parsing of svg representation.
|
||||
* @return {String} svg representation of an instance
|
||||
*/
|
||||
toSVG: function() {
|
||||
toSVG: function(reviver) {
|
||||
var markup = this._createBaseSVGMarkup();
|
||||
|
||||
markup.push(
|
||||
|
|
@ -209,7 +210,7 @@
|
|||
'"/>'
|
||||
);
|
||||
|
||||
return markup.join('');
|
||||
return reviver ? reviver(markup.join('')) : markup.join('');
|
||||
},
|
||||
/* _TO_SVG_END_ */
|
||||
|
||||
|
|
|
|||
|
|
@ -801,10 +801,12 @@
|
|||
/* _TO_SVG_START_ */
|
||||
/**
|
||||
* Returns SVG representation of an instance
|
||||
* @param {Function} [reviver] Method for further parsing of svg representation.
|
||||
* @return {String} svg representation of an instance
|
||||
*/
|
||||
toSVG: function() {
|
||||
var textLines = this.text.split(/\r?\n/),
|
||||
toSVG: function(reviver) {
|
||||
var markup = [ ],
|
||||
textLines = this.text.split(/\r?\n/),
|
||||
lineTopOffset = this.useNative
|
||||
? this.fontSize * this.lineHeight
|
||||
: (-this._fontAscent - ((this._fontAscent / 5) * this.lineHeight)),
|
||||
|
|
@ -820,7 +822,7 @@
|
|||
// move top offset by an ascent
|
||||
textTopOffset += (this._fontAscent ? ((this._fontAscent / 5) * this.lineHeight) : 0);
|
||||
|
||||
return [
|
||||
markup.push(
|
||||
'<g transform="', this.getSvgTransform(), '">',
|
||||
textAndBg.textBgRects.join(''),
|
||||
'<text ',
|
||||
|
|
@ -836,7 +838,9 @@
|
|||
textAndBg.textSpans.join(''),
|
||||
'</text>',
|
||||
'</g>'
|
||||
].join('');
|
||||
);
|
||||
|
||||
return reviver ? reviver(markup.join('')) : markup.join('');
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -74,9 +74,10 @@
|
|||
/* _TO_SVG_START_ */
|
||||
/**
|
||||
* Returns SVG representation of an instance
|
||||
* @param {Function} [reviver] Method for further parsing of svg representation.
|
||||
* @return {String} svg representation of an instance
|
||||
*/
|
||||
toSVG: function() {
|
||||
toSVG: function(reviver) {
|
||||
var markup = this._createBaseSVGMarkup(),
|
||||
widthBy2 = this.width / 2,
|
||||
heightBy2 = this.height / 2;
|
||||
|
|
@ -95,7 +96,7 @@
|
|||
'"/>'
|
||||
);
|
||||
|
||||
return markup.join('');
|
||||
return reviver ? reviver(markup.join('')) : markup.join('');
|
||||
},
|
||||
/* _TO_SVG_END_ */
|
||||
|
||||
|
|
|
|||
|
|
@ -800,6 +800,7 @@
|
|||
* @param {Number} [options.viewBox.width] Width of viewbox
|
||||
* @param {Number} [options.viewBox.height] Height of viewbox
|
||||
* @param {String} [options.encoding=UTF-8] Encoding of SVG output
|
||||
* @param {Function} [reviver] Method for further parsing of svg elements, called after each fabric object converted into svg representation.
|
||||
* @return {String} SVG string
|
||||
* @tutorial {@link http://fabricjs.com/fabric-intro-part-3/#serialization}
|
||||
* @see {@link http://jsfiddle.net/fabricjs/jQ3ZZ/|jsFiddle demo}
|
||||
|
|
@ -818,8 +819,12 @@
|
|||
* });
|
||||
* @example <caption>SVG output with different encoding (default: UTF-8)</caption>
|
||||
* var svg = canvas.toSVG({encoding: 'ISO-8859-1'});
|
||||
* @example <caption>Modify SVG output with reviver function</caption>
|
||||
* var svg = canvas.toSVG(null, function(svg) {
|
||||
* return svg.replace('stroke-dasharray: ; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; ', '');
|
||||
* });
|
||||
*/
|
||||
toSVG: function(options) {
|
||||
toSVG: function(options, reviver) {
|
||||
options || (options = { });
|
||||
var markup = [];
|
||||
|
||||
|
|
@ -882,7 +887,7 @@
|
|||
this.discardActiveGroup();
|
||||
}
|
||||
for (var i = 0, objects = this.getObjects(), len = objects.length; i < len; i++) {
|
||||
markup.push(objects[i].toSVG());
|
||||
markup.push(objects[i].toSVG(reviver));
|
||||
}
|
||||
if (activeGroup) {
|
||||
this.setActiveGroup(new fabric.Group(activeGroup.getObjects()));
|
||||
|
|
|
|||
|
|
@ -313,6 +313,41 @@
|
|||
equal(svg, CANVAS_SVG_VIEWBOX);
|
||||
});
|
||||
|
||||
test('toSVG with reviver', function() {
|
||||
ok(typeof canvas.toSVG == 'function');
|
||||
canvas.clear();
|
||||
|
||||
var circle = new fabric.Circle(),
|
||||
rect = new fabric.Rect(),
|
||||
path1 = new fabric.Path('M 100 100 L 300 100 L 200 300 z'),
|
||||
tria = new fabric.Triangle(),
|
||||
polygon = new fabric.Polygon([{x: 10, y: 12},{x: 20, y: 22}]),
|
||||
polyline = new fabric.Polyline([{x: 10, y: 12},{x: 20, y: 22}]),
|
||||
line = new fabric.Line(),
|
||||
text = new fabric.Text('Text'),
|
||||
group = new fabric.Group([text, line]),
|
||||
ellipse = new fabric.Ellipse(),
|
||||
image = new fabric.Image({width: 0, height: 0}),
|
||||
path2 = new fabric.Path('M 0 0 L 200 100 L 200 300 z'),
|
||||
path3 = new fabric.Path('M 50 50 L 100 300 L 400 400 z'),
|
||||
pathGroup = new fabric.PathGroup([path2, path3]);
|
||||
|
||||
canvas.renderOnAddRemove = false;
|
||||
canvas.add(circle, rect, path1, tria, polygon, polyline, group, ellipse, image, pathGroup);
|
||||
|
||||
var reviverCount = 0,
|
||||
len = canvas.size() + group.size() + pathGroup.paths.length;
|
||||
|
||||
function reviver(svg) {
|
||||
reviverCount++;
|
||||
return svg;
|
||||
}
|
||||
|
||||
var svg = canvas.toSVG(null, reviver);
|
||||
equal(reviverCount, len);
|
||||
canvas.renderOnAddRemove = true;
|
||||
});
|
||||
|
||||
test('toJSON', function() {
|
||||
ok(typeof canvas.toJSON == 'function');
|
||||
equal(JSON.stringify(canvas.toJSON()), '{"objects":[],"background":""}');
|
||||
|
|
|
|||
Loading…
Reference in a new issue