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

add a simple toSVG export for clipPath text #5591

Merged
merged 5 commits into from
Mar 24, 2019
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
18 changes: 14 additions & 4 deletions src/mixins/itext.svg_export.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,22 @@
* @param {Function} [reviver] Method for further parsing of svg representation.
* @return {String} svg representation of an instance
*/
toSVG: function(reviver) {
_toSVG: function() {
var offsets = this._getSVGLeftTopOffsets(),
textAndBg = this._getSVGTextAndBg(offsets.textTop, offsets.textLeft),
internalMarkup = this._wrapSVGTextAndBg(textAndBg);
textAndBg = this._getSVGTextAndBg(offsets.textTop, offsets.textLeft);
return this._wrapSVGTextAndBg(textAndBg);
},

/**
* 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(reviver) {
return this._createBaseSVGMarkup(
internalMarkup, { reviver: reviver, noStyle: true, withShadow: true });
this._toSVG(),
{ reviver: reviver, noStyle: true, withShadow: true }
);
},

/**
Expand Down
17 changes: 16 additions & 1 deletion src/shapes/group.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -526,10 +526,25 @@
}

return this._createBaseSVGMarkup(
svgString,
this._toSVG(),
{ reviver: reviver, noStyle: true, withShadow: true });
},

/**
* 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(reviver) {
var svgString = [];

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

return svgString;
},

/**
* Returns svg clipPath representation of an instance
* @param {Function} [reviver] Method for further parsing of svg representation.
Expand Down
11 changes: 10 additions & 1 deletion test/unit/text_to_svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,16 @@
5: {fontFamily: 'Times New Roman'}
}}
});

assert.equal(removeTranslate(text.toSVG()), removeTranslate(TEXT_SVG_WITH_FONT));
});
QUnit.test('toSVG with text as a clipPath', function(assert) {
fabric.Object.NUM_FRACTION_DIGITS = 0;
fabric.Object.__uid = 0;
var EXPECTED = '<g transform=\"\" clip-path=\"url(#CLIPPATH_0)\" >\n<clipPath id=\"CLIPPATH_0\" >\n\t\t\t<text xml:space=\"preserve\" font-family=\"Times New Roman\" font-size=\"40\" font-style=\"normal\" font-weight=\"normal\" style=\"stroke: none; stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-dashoffset: 0; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(0,0,0); fill-rule: nonzero; opacity: 1; white-space: pre;\" ><tspan x=\"-122\" y=\"13\" >text as clipPath</tspan></text>\n</clipPath>\n<rect style=\"stroke: none; stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-dashoffset: 0; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(0,0,0); fill-rule: nonzero; opacity: 1;\" x=\"-100\" y=\"-50\" rx=\"0\" ry=\"0\" width=\"200\" height=\"100\" />\n</g>\n';
var clipPath = new fabric.Text('text as clipPath');
var rect = new fabric.Rect({ width: 200, height: 100 });
rect.clipPath = clipPath;
assert.equal(removeTranslate(rect.toSVG()), removeTranslate(EXPECTED));
fabric.Object.NUM_FRACTION_DIGITS = 2;
});
})();