Update text.class.js

This commit is contained in:
Andrea Bogazzi 2015-12-09 23:05:55 +01:00
parent db0b026eef
commit 1f41cb3f13

View file

@ -641,7 +641,7 @@
ctx.fillStyle = this.textBackgroundColor;
for (var i = 0, len = this._textLines.length; i < len; i++) {
if (this._textLines[i] !== '') {
lineWidth = this._getLineWidth(ctx, i);
lineWidth = this.textAlign === 'justify' ? this.width : this._getLineWidth(ctx, i);
lineLeftOffset = this._getLineLeftOffset(lineWidth);
ctx.fillRect(
this._getLeftOffset() + lineLeftOffset,
@ -710,7 +710,7 @@
width = 0;
}
else if (this.textAlign === 'justify' && this._cacheLinesWidth) {
wordCount = line.split(' ');
wordCount = line.split(/\s+/);
//consider not justify last line, not for now.
if (wordCount.length > 1) {
width = this.width;
@ -930,11 +930,11 @@
- textTopOffset + height - this.height / 2;
if (this.textAlign === 'justify') {
// i call from here to do not intefere with IText
this._setSVGTextLineJustifed(i, textSpans, height, textLeftOffset, textTopOffset);
this._setSVGTextLineJustifed(i, textSpans, yPos, textLeftOffset);
return;
}
textSpans.push(
'\t\t<tspan x="',
'\t\t\t<tspan x="',
toFixed(textLeftOffset + this._getLineLeftOffset(this._getLineWidth(this.ctx, i)), NUM_FRACTION_DIGITS), '" ',
'y="',
toFixed(yPos, NUM_FRACTION_DIGITS),
@ -947,18 +947,34 @@
);
},
_setSVGTextLineJustifed: function(i, textSpans, height, textLeftOffset, textTopOffset) {
_setSVGTextLineJustifed: function(i, textSpans, yPos, textLeftOffset) {
var ctx = fabric.util.createCanvasElement().getContext('2d');
this._setTextStyles(ctx);
line = this._textLines[i],
this._setTextStyles(ctx);
var line = this._textLines[i],
words = line.split(/\s+/),
wordsWidth = this._getWidthOfWords(ctx, line, i),
wordsWidth = this._getWidthOfWords(ctx, line),
widthDiff = this.width - wordsWidth,
numSpaces = words.length - 1,
spaceWidth = numSpaces > 0 ? widthDiff / numSpaces : 0,
word;
textSpans.push('justified');
word, attributes = this._getFillAttributes(this.fill);
textLeftOffset += this._getLineLeftOffset(this._getLineWidth(ctx, i))
for (var i = 0, len = words.length; i < len; i++) {
word = words[i];
textSpans.push(
'\t\t\t<tspan x="',
toFixed(textLeftOffset, NUM_FRACTION_DIGITS), '" ',
'y="',
toFixed(yPos, NUM_FRACTION_DIGITS),
'" ',
// doing this on <tspan> elements since setting opacity
// on containing <text> one doesn't work in Illustrator
attributes, '>',
fabric.util.string.escapeXml(word),
'</tspan>\n'
);
textLeftOffset += this._getWidthOfWords(ctx, word) + spaceWidth;
}
},
_setSVGTextLineBg: function(textBgRects, i, textLeftOffset, textTopOffset, height) {