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

Word widths on justified line of text were calculated incorrectly. #3408

Conversation

belfz
Copy link
Contributor

@belfz belfz commented Nov 10, 2016

The width of words was incorrectly calculated for justified line of text. Please see the comments to the code changes I committed.

@@ -500,7 +500,7 @@
// stretch the line
var words = line.split(/\s+/),
charOffset = 0,
wordsWidth = this._getWidthOfWords(ctx, words.join(''), lineIndex, 0),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

That was wrong.
For line "lorem ipsum dolor"
a words array would be equal to ["lorem", "ipsum", "dolor"]
then after applying words.join('') a string "loremipsumdolor" would be produced.

Passing that string to _getWidthOfWords resulted in incorrect position of the characters relative to styles.

Moreover, _getWidthOfWords is ready for that case - notice the if statement in the method:

_getWidthOfWords: function (ctx, line, lineIndex, charOffset) {
      var width = 0;

      for (var charIndex = 0; charIndex < line.length; charIndex++) {
        var _char = line[charIndex];

        if (!_char.match(/\s/)) {
          width += this._getWidthOfChar(ctx, _char, lineIndex, charIndex + charOffset);
        }
      }

      return width;
    }

@@ -500,7 +500,7 @@
// stretch the line
var words = line.split(/\s+/),
charOffset = 0,
wordsWidth = this._getWidthOfWords(ctx, words.join(''), lineIndex, 0),
wordsWidth = this._getWidthOfWords(ctx, words.join(' '), lineIndex, 0),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hence, the solution was to preserve the space characters.

@asturur
Copy link
Member

asturur commented Nov 11, 2016

can you correct the lint error?

I m going to try this code, it looks good.
I have been very busy and i m behind with issue tracker one week.
As soon as i catch up i merge this i think.

itext needs a full rewrite in my opinion.

@asturur asturur merged commit f26a2dc into fabricjs:master Nov 12, 2016
asturur pushed a commit that referenced this pull request Nov 12, 2016
…3408)

* Calculation of word widths on justified line will now correctly take styles into account
* fix lint
@belfz
Copy link
Contributor Author

belfz commented Nov 14, 2016

Thank you @asturur! I was about to fix the lint error this morning, but I saw that you already did that and merged the fix. Appreciate that!

@asturur
Copy link
Member

asturur commented Nov 14, 2016

i wanted to ship in 1.6.7
i guess 1.7.0 will take longer or will be bugged. so better to have as much as possible in this release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants