Skip to content

Commit

Permalink
Use correct transforms for Text objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
fdb committed May 8, 2018
1 parent e6e46fc commit c37a9e4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/libraries/vg/objects/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ GText.prototype.draw = function (ctx) {
ctx.font = this._getFont();
ctx.textAlign = this.textAlign;
var m = this.transform.m;
ctx.transform(m[0], m[1], m[3], m[4], m[6], m[7]);
ctx.transform(m[0], m[1], m[2], m[3], m[4], m[5]);
ctx.fillStyle = Color.toCSS(this.fill);
ctx.fillText(this.text, this._x, this._y);
ctx.restore();
Expand Down Expand Up @@ -174,6 +174,9 @@ GText.prototype.toSVG = function () {
if (this.fill !== 'black') {
svg += ' fill="' + Color.toCSS(this.fill) + '"';
}
if (!this.transform.isIdentity()) {
svg += ' transform="matrix(' + this.transform.m.join(',') + ')"';
}
svg += '>';
svg += this.text;
svg += '</text>';
Expand Down
28 changes: 28 additions & 0 deletions test/textSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

var assert = require('assert');
var mocha = require('mocha');
var describe = mocha.describe;
var it = mocha.it;

var g = require('../src/g');

describe('The text object', function () {

it('generates text', function () {
var t = new g.Text('Hello');
assert.equal(t.text, 'Hello');
assert.equal(t.fontFamily, 'sans-serif');
assert.equal(t.fontSize, 24);
assert.deepEqual(t.transform.m, [1, 0, 0, 1, 0, 0]);
assert.equal(t.toSVG(), '<text x="0" y="0" font-family="sans-serif" font-size="24" text-anchor="start">Hello</text>');
});

it('can transform text', function () {
var t = new g.Text('Hello');
t.transform = t.transform.translate(10, 20);
assert.deepEqual(t.transform.m, [1, 0, 0, 1, 10, 20]);
assert.equal(t.toSVG(), '<text x="0" y="0" font-family="sans-serif" font-size="24" text-anchor="start" transform="matrix(1,0,0,1,10,20)">Hello</text>');
});

});

0 comments on commit c37a9e4

Please sign in to comment.