Skip to content

Commit

Permalink
fix(Fabric.Text): Add path to text export and import (#6844)
Browse files Browse the repository at this point in the history
Co-authored-by: Steve Eberhardt <[email protected]>
Co-authored-by: Steve Eberhardt <[email protected]>
  • Loading branch information
3 people authored Jan 31, 2021
1 parent 5dcd92e commit 11146c4
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
26 changes: 24 additions & 2 deletions src/shapes/text.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,14 @@
*/
shadow: null,

/**
* fabric.Path that the text can follow.
* This feature is in BETA.
* @type fabric.Path
* @default
*/
path: null,

/**
* @private
*/
Expand Down Expand Up @@ -1412,9 +1420,11 @@
'textAlign',
'textBackgroundColor',
'charSpacing',
'path'
].concat(propertiesToInclude);
var obj = this.callSuper('toObject', additionalProperties);
obj.styles = clone(this.styles, true);
obj.path = this.path && this.path.toObject();
return obj;
},

Expand Down Expand Up @@ -1570,11 +1580,23 @@
* Returns fabric.Text instance from an object representation
* @static
* @memberOf fabric.Text
* @param {Object} object Object to create an instance from
* @param {Object} object plain js Object to create an instance from
* @param {Function} [callback] Callback to invoke when an fabric.Text instance is created
*/
fabric.Text.fromObject = function(object, callback) {
return fabric.Object._fromObject('Text', object, callback, 'text');
var objectCopy = clone(object), path = object.path;
delete objectCopy.path;
return fabric.Object._fromObject('Text', objectCopy, function(textInstance) {
if (path) {
fabric.Object._fromObject('Path', path, function(pathInstance) {
textInstance.set('path', pathInstance);
callback(textInstance);
}, 'path');
}
else {
callback(textInstance);
}
}, 'text');
};

fabric.Text.genericFonts = ['sans-serif', 'serif', 'cursive', 'fantasy', 'monospace'];
Expand Down
1 change: 1 addition & 0 deletions test/unit/itext.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
charSpacing: 0,
styles: { },
strokeUniform: false,
path: null,
};


Expand Down
23 changes: 23 additions & 0 deletions test/unit/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
skewY: 0,
charSpacing: 0,
styles: {},
path: null,
strokeUniform: false
};

Expand Down Expand Up @@ -841,6 +842,28 @@
assert.equal(text.height, 100, 'text is big as the path height');
});

QUnit.test('text with a path toObject', function(assert) {
var text = new fabric.Text('a', {
path: new fabric.Path('M0 0 h 100 v 100 h -100 z')
});
var toObject = text.toObject();
assert.ok(toObject.path, 'export has a path');
});

QUnit.test('text with a path fromObject', function(assert) {
var done = assert.async();
var text = new fabric.Text('a', {
path: new fabric.Path('M0 0 h 100 v 100 h -100 z')
});
var toObject = text.toObject();
fabric.Text.fromObject(toObject, function(text) {
assert.equal(text.path.type, 'path', 'the path is restored');
assert.ok(text.path instanceof fabric.Path, 'the path is a path');
assert.ok(toObject.path, 'the input has still a path property');
done();
});
});

QUnit.test('cacheProperties for text', function(assert) {
var text = new fabric.Text('a');
assert.equal(text.cacheProperties.join('-'), 'fill-stroke-strokeWidth-strokeDashArray-width-height-paintFirst-strokeUniform-strokeLineCap-strokeDashOffset-strokeLineJoin-strokeMiterLimit-backgroundColor-clipPath-fontFamily-fontWeight-fontSize-text-underline-overline-linethrough-textAlign-fontStyle-lineHeight-textBackgroundColor-charSpacing-styles-path');
Expand Down
3 changes: 2 additions & 1 deletion test/unit/textbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
styles: { },
minWidth: 20,
splitByGrapheme: false,
strokeUniform: false
strokeUniform: false,
path: null,
};

QUnit.test('constructor', function(assert) {
Expand Down

0 comments on commit 11146c4

Please sign in to comment.