From f26a2dc8aa08af73a70f321dc1283dea50b9a444 Mon Sep 17 00:00:00 2001 From: Marcin Date: Sun, 13 Nov 2016 00:35:40 +0100 Subject: [PATCH] Word widths on justified line of text were calculated incorrectly. (#3408) * Calculation of word widths on justified line will now correctly take styles into account * fix lint --- src/shapes/text.class.js | 2 +- test/unit/itext.js | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/shapes/text.class.js b/src/shapes/text.class.js index e9bf682cf6d..64ff0bc82f0 100644 --- a/src/shapes/text.class.js +++ b/src/shapes/text.class.js @@ -499,7 +499,7 @@ // stretch the line var words = line.split(/\s+/), charOffset = 0, - wordsWidth = this._getWidthOfWords(ctx, words.join(''), lineIndex, 0), + wordsWidth = this._getWidthOfWords(ctx, words.join(' '), lineIndex, 0), widthDiff = this.width - wordsWidth, numSpaces = words.length - 1, spaceWidth = numSpaces > 0 ? widthDiff / numSpaces : 0, diff --git a/test/unit/itext.js b/test/unit/itext.js index 01c4c949bef..51a789f5544 100644 --- a/test/unit/itext.js +++ b/test/unit/itext.js @@ -791,4 +791,25 @@ style = doc.getElementsByTagName('style')[0].firstChild.data; equal(style, '\n\t\t@font-face {\n\t\t\tfont-family: \'Plaster\';\n\t\t\tsrc: url(\'path-to-plaster-font-file\');\n\t\t}\n\t\t@font-face {\n\t\t\tfont-family: \'Engagement\';\n\t\t\tsrc: url(\'path-to-engagement-font-file\');\n\t\t}\n'); }); + + test('measuring width of words', function () { + var ctx = canvas.getContext('2d'); + var text = 'test foo bar'; + var iText = new fabric.IText(text, { + styles: { + 0: { + 9: { fontWeight: 'bold' }, + 10: { fontWeight: 'bold' }, + 11: { fontWeight: 'bold' }, + } + } + }); + + var textSplitted = text.split(' '); + var measuredBy_getWidthOfWords_preservedSpaces = iText._getWidthOfWords(ctx, textSplitted.join(' '), 0, 0); + var measuredBy_getWidthOfWords_omittedSpaces = iText._getWidthOfWords(ctx, textSplitted.join(''), 0, 0); + + notEqual(measuredBy_getWidthOfWords_preservedSpaces, measuredBy_getWidthOfWords_omittedSpaces); + }); + })();