Skip to content

Commit 44eb376

Browse files
committed
Merge pull request fabricjs#2370 from asturur/fix-textboxcursor
Remove duplicate code and fix justify style issue
2 parents f1e59c0 + 27b5d2b commit 44eb376

File tree

2 files changed

+24
-32
lines changed

2 files changed

+24
-32
lines changed

src/shapes/itext.class.js

+7-14
Original file line numberDiff line numberDiff line change
@@ -559,12 +559,13 @@
559559
* @param {String} method
560560
* @param {CanvasRenderingContext2D} ctx Context to render on
561561
*/
562-
_renderChars: function(method, ctx, line, left, top, lineIndex) {
562+
_renderChars: function(method, ctx, line, left, top, lineIndex, charOffset) {
563563

564564
if (this.isEmptyStyles()) {
565565
return this._renderCharsFast(method, ctx, line, left, top);
566566
}
567567

568+
charOffset = charOffset || 0;
568569
this.skipTextAlign = true;
569570

570571
// set proper box offset
@@ -578,24 +579,24 @@
578579
var lineHeight = this._getHeightOfLine(ctx, lineIndex),
579580
lineLeftOffset = this._getLineLeftOffset(this._getLineWidth(ctx, lineIndex)),
580581
prevStyle,
582+
thisStyle,
581583
charsToRender = '';
582584

583585
left += lineLeftOffset || 0;
584586

585587
ctx.save();
586588
top -= lineHeight / this.lineHeight * this._fontSizeFraction;
587-
for (var i = 0, len = line.length; i <= len; i++) {
589+
for (var i = charOffset, len = line.length + charOffset; i <= len; i++) {
588590
prevStyle = prevStyle || this.getCurrentCharStyle(lineIndex, i);
589-
var thisStyle = this.getCurrentCharStyle(lineIndex, i + 1);
591+
thisStyle = this.getCurrentCharStyle(lineIndex, i + 1);
590592

591593
if (this._hasStyleChanged(prevStyle, thisStyle) || i === len) {
592594
this._renderChar(method, ctx, lineIndex, i - 1, charsToRender, left, top, lineHeight);
593595
charsToRender = '';
594596
prevStyle = thisStyle;
595597
}
596-
charsToRender += line[i];
598+
charsToRender += line[i - charOffset];
597599
}
598-
599600
ctx.restore();
600601
},
601602

@@ -833,15 +834,7 @@
833834
* @param {Object} [decl]
834835
*/
835836
_applyCharStylesGetWidth: function(ctx, _char, lineIndex, charIndex, decl) {
836-
var styleDeclaration = decl || this._getStyleDeclaration(lineIndex, charIndex);
837-
838-
if (styleDeclaration) {
839-
// cloning so that original style object is not polluted with following font declarations
840-
styleDeclaration = clone(styleDeclaration);
841-
}
842-
else {
843-
styleDeclaration = { };
844-
}
837+
var styleDeclaration = decl || this._getStyleDeclaration(lineIndex, charIndex, true);
845838

846839
this._applyFontStyles(styleDeclaration);
847840

src/shapes/text.class.js

+17-18
Original file line numberDiff line numberDiff line change
@@ -471,29 +471,28 @@
471471
top -= this.fontSize * this._fontSizeFraction;
472472

473473
// short-circuit
474-
if (this.textAlign !== 'justify') {
474+
var lineWidth = this._getLineWidth(ctx, lineIndex);
475+
if (this.textAlign !== 'justify' || this.width < lineWidth) {
475476
this._renderChars(method, ctx, line, left, top, lineIndex);
476477
return;
477478
}
478479

479-
var lineWidth = this._getLineWidth(ctx, lineIndex),
480-
totalWidth = this.width;
481-
if (totalWidth >= lineWidth) {
482-
// stretch the line
483-
var words = line.split(/\s+/),
484-
wordsWidth = this._getWidthOfWords(ctx, line, lineIndex),
485-
widthDiff = totalWidth - wordsWidth,
486-
numSpaces = words.length - 1,
487-
spaceWidth = widthDiff / numSpaces,
488-
leftOffset = 0;
489-
490-
for (var i = 0, len = words.length; i < len; i++) {
491-
this._renderChars(method, ctx, words[i], left + leftOffset, top, lineIndex);
492-
leftOffset += ctx.measureText(words[i]).width + spaceWidth;
480+
// stretch the line
481+
var words = line.split(/\s+/),
482+
wordsWidth = this._getWidthOfWords(ctx, line, lineIndex),
483+
widthDiff = this.width - wordsWidth,
484+
numSpaces = words.length - 1,
485+
spaceWidth = numSpaces > 0 ? widthDiff / numSpaces : 0,
486+
leftOffset = 0, charOffset = 0, word;
487+
488+
for (var i = 0, len = words.length; i < len; i++) {
489+
while (line[charOffset] === ' ' && charOffset < line.length) {
490+
charOffset++;
493491
}
494-
}
495-
else {
496-
this._renderChars(method, ctx, line, left, top, lineIndex);
492+
word = words[i];
493+
this._renderChars(method, ctx, word, left + leftOffset, top, lineIndex, charOffset);
494+
leftOffset += ctx.measureText(word).width + spaceWidth;
495+
charOffset += word.length;
497496
}
498497
},
499498

0 commit comments

Comments
 (0)