Skip to content

Commit

Permalink
Merge pull request #2354 from asturur/fix-text-glitche
Browse files Browse the repository at this point in the history
Fix text glitche
  • Loading branch information
kangax committed Jul 22, 2015
2 parents 989d1fa + 22d9f8d commit 2cedb1c
Showing 1 changed file with 41 additions and 28 deletions.
69 changes: 41 additions & 28 deletions src/shapes/text.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,11 @@
}
this._textLines = this._splitTextIntoLines();
this._clearCache();
var currentTextAlign = this.textAlign;
this.textAlign = 'left';
//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.textAlign = currentTextAlign;
this._cacheLinesWidth = true;
this.height = this._getTextHeight(ctx);
},

Expand All @@ -360,14 +361,14 @@
*/
_render: function(ctx) {
this.clipTo && fabric.util.clipContext(this, ctx);
ctx.save();
this._setOpacity(ctx);
this._setShadow(ctx);
this._setupCompositeOperation(ctx);
this._renderTextBackground(ctx);
this._setStrokeStyles(ctx);
this._setFillStyles(ctx);
this._renderText(ctx);
this._renderTextDecoration(ctx);
ctx.restore();
this.clipTo && ctx.restore();
},

Expand All @@ -380,16 +381,18 @@
this._translateForTextAlign(ctx);
this._renderTextFill(ctx);
this._renderTextStroke(ctx);

this._translateForTextAlign(ctx, true);
},

/**
* @private
* @param {CanvasRenderingContext2D} ctx Context to render on
* @param {Boolean} back Indicates if translate back or forward
*/
_translateForTextAlign: function(ctx) {
_translateForTextAlign: function(ctx, back) {
if (this.textAlign !== 'left' && this.textAlign !== 'justify') {
ctx.translate(this.textAlign === 'center' ? (this.width / 2) : this.width, 0);
var sign = back ? -1 : 1;
ctx.translate(this.textAlign === 'center' ? (sign * this.width / 2) : sign * this.width, 0);
}
},

Expand Down Expand Up @@ -613,7 +616,6 @@
return;
}

ctx.save();
ctx.fillStyle = this.backgroundColor;

ctx.fillRect(
Expand All @@ -623,29 +625,24 @@
this.height
);

ctx.restore();
},

/**
* @private
* @param {CanvasRenderingContext2D} ctx Context to render on
*/
_renderTextLinesBackground: function(ctx) {
var lineTopOffset = 0, heightOfLine = this._getHeightOfLine();
if (!this.textBackgroundColor) {
return;
}
var lineTopOffset = 0, heightOfLine = this._getHeightOfLine(),
lineWidth, lineLeftOffset;

ctx.save();
ctx.fillStyle = this.textBackgroundColor;

for (var i = 0, len = this._textLines.length; i < len; i++) {

if (this._textLines[i] !== '') {

var lineWidth = this._getLineWidth(ctx, i),
lineLeftOffset = this._getLineLeftOffset(lineWidth);

lineWidth = this._getLineWidth(ctx, i);
lineLeftOffset = this._getLineLeftOffset(lineWidth);
ctx.fillRect(
this._getLeftOffset() + lineLeftOffset,
this._getTopOffset() + lineTopOffset,
Expand All @@ -655,7 +652,6 @@
}
lineTopOffset += heightOfLine;
}
ctx.restore();
},

/**
Expand Down Expand Up @@ -703,14 +699,32 @@
/**
* @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] = ctx.measureText(this._textLines[lineIndex]).width;
return this.__lineWidths[lineIndex];
var width, wordCount, line = this._textLines[lineIndex];
if (line === '') {
width = 0;
}
else if (this.textAlign === 'justify' && this._cacheLinesWidth) {
wordCount = line.split(' ');
//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;
}
this._cacheLinesWidth && (this.__lineWidths[lineIndex] = width);
return width;
},

/**
Expand All @@ -727,12 +741,14 @@

/** @ignore */
function renderLinesAtOffset(offsets) {
var i, lineHeight = 0, len, j, oLen;
var i, lineHeight = 0, len, j, oLen, lineWidth,
lineLeftOffset, heightOfLine;

for (i = 0, len = _this._textLines.length; i < len; i++) {

var lineWidth = _this._getLineWidth(ctx, i),
lineLeftOffset = _this._getLineLeftOffset(lineWidth),
heightOfLine = _this._getHeightOfLine(ctx, i);
lineWidth = _this._getLineWidth(ctx, i),
lineLeftOffset = _this._getLineLeftOffset(lineWidth),
heightOfLine = _this._getHeightOfLine(ctx, i);

for (j = 0, oLen = offsets.length; j < oLen; j++) {
ctx.fillRect(
Expand All @@ -754,7 +770,6 @@
if (this.textDecoration.indexOf('overline') > -1) {
offsets.push(-0.12);
}

if (offsets.length > 0) {
renderLinesAtOffset(offsets);
}
Expand Down Expand Up @@ -792,8 +807,6 @@
if (!noTransform) {
this.transform(ctx);
}
this._setStrokeStyles(ctx);
this._setFillStyles(ctx);
if (this.transformMatrix) {
ctx.transform.apply(ctx, this.transformMatrix);
}
Expand Down

0 comments on commit 2cedb1c

Please sign in to comment.