Fix fabric.Text#fontFamily in SVG output - Closes issue #813

Add unit tests
This commit is contained in:
Kienz 2013-08-29 22:06:24 +02:00
parent 434bfb2c6d
commit 345ed3f5e9
2 changed files with 21 additions and 2 deletions

View file

@ -678,7 +678,6 @@
* @return {String} svg representation of an instance
*/
toSVG: function() {
var textLines = this.text.split(/\r?\n/),
lineTopOffset = this.useNative
? this.fontSize * this.lineHeight
@ -699,7 +698,7 @@
'<g transform="', this.getSvgTransform(), '">',
textAndBg.textBgRects.join(''),
'<text ',
(this.fontFamily ? 'font-family="\'' + this.fontFamily + '\'" ': ''),
(this.fontFamily ? 'font-family="' + this.fontFamily.replace(/"/g,'\'') + '" ': ''),
(this.fontSize ? 'font-size="' + this.fontSize + '" ': ''),
(this.fontStyle ? 'font-style="' + this.fontStyle + '" ': ''),
(this.fontWeight ? 'font-weight="' + this.fontWeight + '" ': ''),

View file

@ -52,6 +52,8 @@
'useNative': true
};
var TEXT_SVG = '<g transform="translate(0 0)"><text font-family="Times New Roman" font-size="40" font-weight="normal" style="stroke: none; stroke-width: 1; stroke-dasharray: ; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(0,0,0); opacity: 1;" transform="translate(-10 39)"><tspan x="0" y="-26" fill="rgb(0,0,0)">x</tspan></text></g>';
test('constructor', function() {
ok(fabric.Text);
var text = createTextObject();
@ -228,6 +230,24 @@
text.set('fontFamily', 'foobar');
equal(text.get('fontFamily'), 'foobar');
text.set('fontFamily', '"Arial Black", Arial');
equal(text.get('fontFamily'), '"Arial Black", Arial');
});
test('toSVG', function() {
var text = new fabric.Text('x');
// temp workaround for text objects not obtaining width under node
text.width = 20;
equal(text.toSVG(), TEXT_SVG);
text.setFontFamily('"Arial Black", Arial');
// temp workaround for text objects not obtaining width under node
text.width = 20;
equal(text.toSVG(), TEXT_SVG.replace('font-family="Times New Roman"', 'font-family="\'Arial Black\', Arial"'));
});
})();