Skip to content

Commit

Permalink
Fix iText space width justification (#2971)
Browse files Browse the repository at this point in the history
  • Loading branch information
asturur committed May 15, 2016
1 parent cb0c71e commit 0297041
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
19 changes: 11 additions & 8 deletions src/shapes/itext.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@
* @param {CanvasRenderingContext2D} ctx Context to render on
*/
_getWidthOfChar: function(ctx, _char, lineIndex, charIndex) {
if (this.textAlign === 'justify' && this._reSpacesAndTabs.test(_char)) {
if (!this._isMeasuring && this.textAlign === 'justify' && this._reSpacesAndTabs.test(_char)) {
return this._getWidthOfSpace(ctx, lineIndex);
}

Expand Down Expand Up @@ -965,6 +965,8 @@
/**
* @private
* @param {CanvasRenderingContext2D} ctx Context to render on
* @param {Number} lineIndex
* @param {Number} charIndex
*/
_getWidthOfCharsAt: function(ctx, lineIndex, charIndex) {
var width = 0, i, _char;
Expand All @@ -978,13 +980,14 @@
/**
* @private
* @param {CanvasRenderingContext2D} ctx Context to render on
* @param {Number} lineIndex line number
* @return {Number} Line width
*/
_getLineWidth: function(ctx, lineIndex) {
if (this.__lineWidths[lineIndex]) {
return this.__lineWidths[lineIndex];
}
this.__lineWidths[lineIndex] = this._getWidthOfCharsAt(ctx, lineIndex, this._textLines[lineIndex].length);
return this.__lineWidths[lineIndex];
_measureLine: function(ctx, lineIndex) {
this._isMeasuring = true;
var width = this._getWidthOfCharsAt(ctx, lineIndex, this._textLines[lineIndex].length);
this._isMeasuring = false;
return width;
},

/**
Expand All @@ -1000,7 +1003,7 @@
wordsWidth = this._getWidthOfWords(ctx, line, lineIndex, 0),
widthDiff = this.width - wordsWidth,
numSpaces = line.length - line.replace(this._reSpacesAndTabs, '').length,
width = widthDiff / numSpaces;
width = Math.max(widthDiff / numSpaces, ctx.measureText(' ').width);
this.__widthOfSpace[lineIndex] = width;
return width;
},
Expand Down
39 changes: 22 additions & 17 deletions src/shapes/text.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,11 +352,7 @@
}
this._textLines = this._splitTextIntoLines();
this._clearCache();
//if textAlign is 'justify' i have to disable caching
//when calculating width of text and widths of line.
this._cacheLinesWidth = (this.textAlign !== 'justify');
this.width = this._getTextWidth(ctx);
this._cacheLinesWidth = true;
this.height = this._getTextHeight(ctx);
},

Expand Down Expand Up @@ -664,8 +660,8 @@
ctx.fillStyle = this.textBackgroundColor;
for (var i = 0, len = this._textLines.length; i < len; i++) {
heightOfLine = this._getHeightOfLine(ctx, i);
if (this._textLines[i] !== '') {
lineWidth = this.textAlign === 'justify' ? this.width : this._getLineWidth(ctx, i);
lineWidth = this._getLineWidth(ctx, i);
if (lineWidth > 0) {
lineLeftOffset = this._getLineLeftOffset(lineWidth);
ctx.fillRect(
this._getLeftOffset() + lineLeftOffset,
Expand Down Expand Up @@ -730,29 +726,38 @@
*/
_getLineWidth: function(ctx, lineIndex) {
if (this.__lineWidths[lineIndex]) {
return this.__lineWidths[lineIndex];
return this.__lineWidths[lineIndex] === -1 ? this.width : this.__lineWidths[lineIndex];
}

var width, wordCount, line = this._textLines[lineIndex];

if (line === '') {
width = 0;
}
else if (this.textAlign === 'justify' && this._cacheLinesWidth) {
else {
width = this._measureLine(ctx, lineIndex);
}
this.__lineWidths[lineIndex] = width;

if (width && this.textAlign === 'justify') {
wordCount = line.split(/\s+/);
//consider not justify last line, not for now.
if (wordCount.length > 1) {
width = this.width;
}
else {
width = ctx.measureText(line).width;
this.__lineWidths[lineIndex] = -1;
}
}
else {
width = ctx.measureText(line).width;
}
this._cacheLinesWidth && (this.__lineWidths[lineIndex] = width);
return width;
},

/**
* @private
* @param {CanvasRenderingContext2D} ctx Context to render on
* @param {Number} lineIndex line number
* @return {Number} Line width
*/
_measureLine: function(ctx, lineIndex) {
return ctx.measureText(this._textLines[lineIndex]).width;
},

/**
* @private
* @param {CanvasRenderingContext2D} ctx Context to render on
Expand Down

0 comments on commit 0297041

Please sign in to comment.