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

Proposal for different lineJustify. #4437

Merged
merged 5 commits into from
Nov 12, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/mixins/itext.svg_export.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
textLeftOffset += charBox.kernedWidth - charBox.width;
}
boxWidth += charBox.kernedWidth;
if (this.textAlign === 'justify' && !timeToRender) {
if (this.textAlign.indexOf('justify') !== -1 && !timeToRender) {
Copy link
Member Author

Choose a reason for hiding this comment

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

this was not replaced in the whole code in the other PR,

if (this._reSpaceAndTab.test(line[i])) {
timeToRender = true;
}
Expand Down
22 changes: 20 additions & 2 deletions src/shapes/text.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@
this._splitText();
this._clearCache();
this.width = this.calcTextWidth() || this.cursorWidth || MIN_TEXT_WIDTH;
if (this.textAlign === 'justify') {
if (this.textAlign.indexOf('justify') !== -1) {
// once text is misured we need to make space fatter to make justified text.
this.enlargeSpaces();
}
Expand All @@ -348,6 +348,9 @@
enlargeSpaces: function() {
var diffSpace, currentLineWidth, numberOfSpaces, accumulatedSpace, line, charBound, spaces;
for (var i = 0, len = this._textLines.length; i < len; i++) {
if (i === len - 1 || this.isEndOfWrapping(i)) {
Copy link
Member Author

Choose a reason for hiding this comment

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

ok here needs a this.textAlign !== 'justify' && ( ..... )

Copy link
Member Author

Choose a reason for hiding this comment

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

i still think this change is needed.
Since we are giving the user

justify-left, jsutify-right, justify-center, this thing here should change that we do not want the old plain justify.

Who built an app over 2.0b7 or earlier, does not want to see old JSON saved with plain justify loaded differently just because we think is better to have last line not justified.

In this way we give an option to everyone at expense of littled added complexity, and we do not break old stuff.

Copy link

@jilster jilster Nov 8, 2017

Choose a reason for hiding this comment

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

Sorry for the late response. Yes, i agree, backwards compatibility is important. Will you change it or should i?

continue;
}
accumulatedSpace = 0;
line = this._textLines[i];
currentLineWidth = this.getLineWidth(i);
Expand All @@ -370,6 +373,15 @@
}
},

/**
* Detect if the text line is ended with an hard break
* text and itext do not have wrapping, return false
* @return {Boolean}
*/
isEndOfWrapping: function(/* lineIndex */) {
return false;
},

/**
* Returns string representation of an instance
* @return {String} String representation of text object
Expand Down Expand Up @@ -855,7 +867,7 @@
left += charBox.kernedWidth - charBox.width;
}
boxWidth += charBox.kernedWidth;
if (this.textAlign === 'justify' && !timeToRender) {
if (this.textAlign.indexOf('justify') !== -1 && !timeToRender) {
if (this._reSpaceAndTab.test(line[i])) {
timeToRender = true;
}
Expand Down Expand Up @@ -938,6 +950,12 @@
if (this.textAlign === 'right') {
return this.width - lineWidth;
}
if (this.textAlign === 'justify-center' && this.isEndOfWrapping(lineIndex)) {
return (this.width - lineWidth) / 2;
}
if (this.textAlign === 'justify-right' && this.isEndOfWrapping(lineIndex)) {
return this.width - lineWidth;
}
return 0;
},

Expand Down
20 changes: 19 additions & 1 deletion src/shapes/textbox.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
if (this.dynamicMinWidth > this.width) {
this._set('width', this.dynamicMinWidth);
}
if (this.textAlign === 'justify') {
if (this.textAlign.indexOf('justify') !== -1) {
// once text is measured we need to make space fatter to make justified text.
this.enlargeSpaces();
}
Expand Down Expand Up @@ -324,6 +324,24 @@
return graphemeLines;
},

/**
* Detect if the text line is ended with an hard break
* text and itext do not have wrapping, return false
* @param {Number} lineIndex text to split
* @return {Boolean}
*/
isEndOfWrapping: function(lineIndex) {
if (!this.styleMap[lineIndex + 1]) {
// is last line, return true;
return true;
}
if (this.styleMap[lineIndex + 1].line !== this.styleMap[lineIndex].line) {
// this is last line before a line break, return true;
return true;
}
return false;
},

/**
* Gets lines of text to render in the Textbox. This function calculates
* text wrapping on the fly every time it is called.
Expand Down