Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove duplicate code and fix justify style issue #2370

Merged
merged 1 commit into from
Jul 31, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 7 additions & 14 deletions src/shapes/itext.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -559,12 +559,13 @@
* @param {String} method
* @param {CanvasRenderingContext2D} ctx Context to render on
*/
_renderChars: function(method, ctx, line, left, top, lineIndex) {
_renderChars: function(method, ctx, line, left, top, lineIndex, charOffset) {

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

charOffset = charOffset || 0;
this.skipTextAlign = true;

// set proper box offset
Expand All @@ -578,24 +579,24 @@
var lineHeight = this._getHeightOfLine(ctx, lineIndex),
lineLeftOffset = this._getLineLeftOffset(this._getLineWidth(ctx, lineIndex)),
prevStyle,
thisStyle,
charsToRender = '';

left += lineLeftOffset || 0;

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

if (this._hasStyleChanged(prevStyle, thisStyle) || i === len) {
this._renderChar(method, ctx, lineIndex, i - 1, charsToRender, left, top, lineHeight);
charsToRender = '';
prevStyle = thisStyle;
}
charsToRender += line[i];
charsToRender += line[i - charOffset];
}

ctx.restore();
},

Expand Down Expand Up @@ -833,15 +834,7 @@
* @param {Object} [decl]
*/
_applyCharStylesGetWidth: function(ctx, _char, lineIndex, charIndex, decl) {
var styleDeclaration = decl || this._getStyleDeclaration(lineIndex, charIndex);

if (styleDeclaration) {
// cloning so that original style object is not polluted with following font declarations
styleDeclaration = clone(styleDeclaration);
}
else {
styleDeclaration = { };
}
var styleDeclaration = decl || this._getStyleDeclaration(lineIndex, charIndex, true);

this._applyFontStyles(styleDeclaration);

Expand Down
35 changes: 17 additions & 18 deletions src/shapes/text.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -471,29 +471,28 @@
top -= this.fontSize * this._fontSizeFraction;

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

var lineWidth = this._getLineWidth(ctx, lineIndex),
totalWidth = this.width;
if (totalWidth >= lineWidth) {
// stretch the line
var words = line.split(/\s+/),
wordsWidth = this._getWidthOfWords(ctx, line, lineIndex),
widthDiff = totalWidth - wordsWidth,
numSpaces = words.length - 1,
spaceWidth = widthDiff / numSpaces,
leftOffset = 0;

for (var i = 0, len = words.length; i < len; i++) {
this._renderChars(method, ctx, words[i], left + leftOffset, top, lineIndex);
leftOffset += ctx.measureText(words[i]).width + spaceWidth;
// stretch the line
var words = line.split(/\s+/),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, don't we have a separate method for this now?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you mean splitting words, no i don t think so.
i can check.
anyway that code needs a review.

measuring width of words without calculating styles is very wrong and produce bad ( rare ) result.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no we do not have any method for splitting line in words.

wordsWidth = this._getWidthOfWords(ctx, line, lineIndex),
widthDiff = this.width - wordsWidth,
numSpaces = words.length - 1,
spaceWidth = numSpaces > 0 ? widthDiff / numSpaces : 0,
leftOffset = 0, charOffset = 0, word;

for (var i = 0, len = words.length; i < len; i++) {
while (line[charOffset] === ' ' && charOffset < line.length) {
charOffset++;
}
}
else {
this._renderChars(method, ctx, line, left, top, lineIndex);
word = words[i];
this._renderChars(method, ctx, word, left + leftOffset, top, lineIndex, charOffset);
leftOffset += ctx.measureText(word).width + spaceWidth;
charOffset += word.length;
}
},

Expand Down
9 changes: 1 addition & 8 deletions src/shapes/textbox.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,7 @@
lineIndex = map.line;
charIndex = map.offset + charIndex;
}

if (returnCloneOrEmpty) {
return (this.styles[lineIndex] && this.styles[lineIndex][charIndex])
? clone(this.styles[lineIndex][charIndex])
: {};
}

return this.styles[lineIndex] && this.styles[lineIndex][charIndex] ? this.styles[lineIndex][charIndex] : null;
return this.callSuper('_getStyleDeclaration', lineIndex, charIndex, returnCloneOrEmpty);
},

/**
Expand Down