mirror of
https://github.com/Hopiu/fabric.js.git
synced 2026-05-02 11:04:43 +00:00
Fix iText space width justification (#2971)
This commit is contained in:
parent
cb0c71e1fe
commit
0297041e2f
2 changed files with 35 additions and 27 deletions
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
@ -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;
|
||||
|
|
@ -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;
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
@ -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;
|
||||
},
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
},
|
||||
|
||||
|
|
@ -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,
|
||||
|
|
@ -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) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
else {
|
||||
width = ctx.measureText(line).width;
|
||||
width = this._measureLine(ctx, lineIndex);
|
||||
}
|
||||
this.__lineWidths[lineIndex] = width;
|
||||
|
||||
if (width && this.textAlign === 'justify') {
|
||||
wordCount = line.split(/\s+/);
|
||||
if (wordCount.length > 1) {
|
||||
this.__lineWidths[lineIndex] = -1;
|
||||
}
|
||||
}
|
||||
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
|
||||
|
|
|
|||
Loading…
Reference in a new issue