From 27b5d2b0fd183cdddd8cedb70e2f1f180dc8094a Mon Sep 17 00:00:00 2001 From: Andrea Bogazzi Date: Tue, 28 Jul 2015 19:20:14 +0200 Subject: [PATCH] Update textbox.class.js --- src/shapes/itext.class.js | 21 +++++++-------------- src/shapes/text.class.js | 35 +++++++++++++++++------------------ src/shapes/textbox.class.js | 9 +-------- 3 files changed, 25 insertions(+), 40 deletions(-) diff --git a/src/shapes/itext.class.js b/src/shapes/itext.class.js index 3ef40c5a3b5..edaadde22bd 100644 --- a/src/shapes/itext.class.js +++ b/src/shapes/itext.class.js @@ -559,12 +559,13 @@ * @param {String} method * @param {CanvasRenderingContext2D} ctx Context to render on */ - _renderChars: function(method, ctx, line, left, top, lineIndex) { + _renderChars: function(method, ctx, line, left, top, lineIndex, charOffset) { if (this.isEmptyStyles()) { return this._renderCharsFast(method, ctx, line, left, top); } + charOffset = charOffset || 0; this.skipTextAlign = true; // set proper box offset @@ -578,24 +579,24 @@ var lineHeight = this._getHeightOfLine(ctx, lineIndex), lineLeftOffset = this._getLineLeftOffset(this._getLineWidth(ctx, lineIndex)), prevStyle, + thisStyle, charsToRender = ''; left += lineLeftOffset || 0; ctx.save(); top -= lineHeight / this.lineHeight * this._fontSizeFraction; - for (var i = 0, len = line.length; i <= len; i++) { + for (var i = charOffset, len = line.length + charOffset; i <= len; i++) { prevStyle = prevStyle || this.getCurrentCharStyle(lineIndex, i); - var thisStyle = this.getCurrentCharStyle(lineIndex, i + 1); + thisStyle = this.getCurrentCharStyle(lineIndex, i + 1); if (this._hasStyleChanged(prevStyle, thisStyle) || i === len) { this._renderChar(method, ctx, lineIndex, i - 1, charsToRender, left, top, lineHeight); charsToRender = ''; prevStyle = thisStyle; } - charsToRender += line[i]; + charsToRender += line[i - charOffset]; } - ctx.restore(); }, @@ -833,15 +834,7 @@ * @param {Object} [decl] */ _applyCharStylesGetWidth: function(ctx, _char, lineIndex, charIndex, decl) { - var styleDeclaration = decl || this._getStyleDeclaration(lineIndex, charIndex); - - if (styleDeclaration) { - // cloning so that original style object is not polluted with following font declarations - styleDeclaration = clone(styleDeclaration); - } - else { - styleDeclaration = { }; - } + var styleDeclaration = decl || this._getStyleDeclaration(lineIndex, charIndex, true); this._applyFontStyles(styleDeclaration); diff --git a/src/shapes/text.class.js b/src/shapes/text.class.js index f5d310e4ac0..fec9d62e2ac 100644 --- a/src/shapes/text.class.js +++ b/src/shapes/text.class.js @@ -471,29 +471,28 @@ top -= this.fontSize * this._fontSizeFraction; // short-circuit - if (this.textAlign !== 'justify') { + var lineWidth = this._getLineWidth(ctx, lineIndex); + if (this.textAlign !== 'justify' || this.width < lineWidth) { this._renderChars(method, ctx, line, left, top, lineIndex); return; } - var lineWidth = this._getLineWidth(ctx, lineIndex), - totalWidth = this.width; - if (totalWidth >= lineWidth) { - // stretch the line - var words = line.split(/\s+/), - wordsWidth = this._getWidthOfWords(ctx, line, lineIndex), - widthDiff = totalWidth - wordsWidth, - numSpaces = words.length - 1, - spaceWidth = widthDiff / numSpaces, - leftOffset = 0; - - for (var i = 0, len = words.length; i < len; i++) { - this._renderChars(method, ctx, words[i], left + leftOffset, top, lineIndex); - leftOffset += ctx.measureText(words[i]).width + spaceWidth; + // stretch the line + var words = line.split(/\s+/), + wordsWidth = this._getWidthOfWords(ctx, line, lineIndex), + widthDiff = this.width - wordsWidth, + numSpaces = words.length - 1, + spaceWidth = numSpaces > 0 ? widthDiff / numSpaces : 0, + leftOffset = 0, charOffset = 0, word; + + for (var i = 0, len = words.length; i < len; i++) { + while (line[charOffset] === ' ' && charOffset < line.length) { + charOffset++; } - } - else { - this._renderChars(method, ctx, line, left, top, lineIndex); + word = words[i]; + this._renderChars(method, ctx, word, left + leftOffset, top, lineIndex, charOffset); + leftOffset += ctx.measureText(word).width + spaceWidth; + charOffset += word.length; } }, diff --git a/src/shapes/textbox.class.js b/src/shapes/textbox.class.js index a1b0c46cd08..baa6f97c351 100644 --- a/src/shapes/textbox.class.js +++ b/src/shapes/textbox.class.js @@ -143,14 +143,7 @@ lineIndex = map.line; charIndex = map.offset + charIndex; } - - if (returnCloneOrEmpty) { - return (this.styles[lineIndex] && this.styles[lineIndex][charIndex]) - ? clone(this.styles[lineIndex][charIndex]) - : {}; - } - - return this.styles[lineIndex] && this.styles[lineIndex][charIndex] ? this.styles[lineIndex][charIndex] : null; + return this.callSuper('_getStyleDeclaration', lineIndex, charIndex, returnCloneOrEmpty); }, /**