Skip to content

Commit

Permalink
fix textbox wrapping with charspacing (#4803)
Browse files Browse the repository at this point in the history
* fix textbox and charspacing

* added test

* enabled all tests
  • Loading branch information
asturur authored Mar 12, 2018
1 parent c7f6154 commit 808faa6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/shapes/text.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -736,11 +736,12 @@
prevStyle = prevGrapheme ? this.getCompleteStyleDeclaration(lineIndex, charIndex - 1) : { },
info = this._measureChar(grapheme, style, prevGrapheme, prevStyle),
kernedWidth = info.kernedWidth,
width = info.width;
width = info.width, charSpacing;

if (this.charSpacing !== 0) {
width += this._getWidthOfCharSpacing();
kernedWidth += this._getWidthOfCharSpacing();
charSpacing = this._getWidthOfCharSpacing();
width += charSpacing;
kernedWidth += charSpacing;
}

var box = {
Expand Down
3 changes: 3 additions & 0 deletions src/shapes/textbox.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,9 @@
lineWidth = wordWidth;
lineJustStarted = true;
}
else {
lineWidth += additionalSpace;
}

if (!lineJustStarted) {
line.push(infix);
Expand Down
16 changes: 15 additions & 1 deletion test/unit/textbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,19 @@
assert.equal(textbox.isEmptyStyles(5), true, 'style is empty at line 5');
assert.equal(textbox.isEmptyStyles(6), false, 'style is empty at line 6');
});

QUnit.test('wrapping with charspacing', function(assert) {
var textbox = new fabric.Textbox('xa xb xc xd xe ya yb id', {
width: 190,
});
assert.equal(textbox.textLines[0], 'xa xb xc xd', 'first line match expectations');
textbox.charSpacing = 100;
textbox.initDimensions();
assert.equal(textbox.textLines[0], 'xa xb xc', 'first line match expectations spacing 100');
textbox.charSpacing = 300;
textbox.initDimensions();
assert.equal(textbox.textLines[0], 'xa xb', 'first line match expectations spacing 300');
textbox.charSpacing = 800;
textbox.initDimensions();
assert.equal(textbox.textLines[0], 'xa', 'first line match expectations spacing 800');
});
})();

0 comments on commit 808faa6

Please sign in to comment.