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

Text cursor position is incorrect after changing fonts (using font face observer) #4739

Closed
blobinabottle opened this issue Feb 18, 2018 · 20 comments · Fixed by #4772
Closed

Comments

@blobinabottle
Copy link
Contributor

Version

2.0.1

Test Case

http://jsfiddle.net/blobinabottle/xsLgppan/16/

Steps to reproduce

Change the text to Roboto and check last words of the fist line, the text cursor got an incorrect position.
screen shot 2018-02-18 at 18 22 37

It's worst if you select pacifico (I'm selecting SIT :-)).
image

Expected Behavior

Cursor should be a the right position.

Is there anything to do to avoid this situation? I tried clearFabricFontCache but not sure I've done it right.

@blobinabottle
Copy link
Contributor Author

fabricjs.com/loadfonts it's the same issue in this demo, just by switching to pacifico.

@blobinabottle
Copy link
Contributor Author

The only way to get it working correctly I found, is by bypassing the cache system in the text:
http://jsfiddle.net/blobinabottle/xsLgppan/42/

Is that really important for perf? When is it called?

@asturur
Copy link
Member

asturur commented Feb 18, 2018

Very weird.
What do you mean bypassing the cache system? that you return always empty object and force fabric to calculate every time?

I do not understand whey calculation should fail just from the cache.

@blobinabottle
Copy link
Contributor Author

yes in my getFontCache, I always return empty object... But I don't understand why it's working like that.

@blobinabottle
Copy link
Contributor Author

Ok the culprit is here. We change the fontCache[_char] and I see this value changing with no reason for the same character in a same text block :

// try to fix a MS browsers oddity
        if (kernedWidth > width) {
          var diff = kernedWidth - width;
          fontCache[_char] = kernedWidth;
          fontCache[couple] += diff;
          width = kernedWidth;
        }

If I comment this it's working.

@asturur
Copy link
Member

asturur commented Feb 21, 2018

What is weird is that it does not happens with all fonts.
Pacifico is a particular weird font.

Look this gif i recorded
testp

As you can see each gliph change to follow the next one, is like a kerning, inverse. This may bring to some kind of problem in measurement logic.

But needs to verified

@asturur
Copy link
Member

asturur commented Feb 21, 2018

Maybe when we measure the single char we should measure it with a space in front?

@blobinabottle
Copy link
Contributor Author

yes I see what you mean, but I think the font measurement is correct.
When the IE fix is removed everything is working fine (didn't get it again with many different fonts). Not sure to understand what the IE fix is doing ?

@asturur
Copy link
Member

asturur commented Feb 21, 2018

can you show me here the IE fix?

@blobinabottle
Copy link
Contributor Author

It's in _measureChar method. I can't find it in GitHub UI (only in the raw file if I search for "// try to fix a MS browsers oddity")

@asturur
Copy link
Member

asturur commented Feb 21, 2018

just make a copy paste of your file, for reference here.

@blobinabottle
Copy link
Contributor Author

jsfiddle.net/xsLgppan/72/ I updated the fiddle with the change.

@asturur
Copy link
Member

asturur commented Feb 21, 2018

i m sorry you already posted the fix here:

// try to fix a MS browsers oddity
        if (kernedWidth > width) {
          var diff = kernedWidth - width;
          fontCache[_char] = kernedWidth;
          fontCache[couple] += diff;
          width = kernedWidth;
        }

so what is happening is that MS measure fonts in pixels.
So a font of 11.1 is big 11 a font of 11.9 is big 12
Sometimes kerned Width was a little bit bigger because:

a = '11.3' => 11
b = '11.3 => 11

ab = 22.6 => 23

b kerned was then ab - a = 12.

and this was normally wrong.

But we see that with pacifico this is a possible use case.

So we need a better logic.

@blobinabottle
Copy link
Contributor Author

Okay I understand now. So a little difference accumulates on long lines even with simple font like Roboto. And I see some discussion about it already happened long time ago :D Automattic/node-canvas#331

@blobinabottle
Copy link
Contributor Author

Are you sure this fix really worth it? Testing on IE without the fix, and this is the worst I can get in terms of offset... Very small.
image

@blobinabottle
Copy link
Contributor Author

With font yellowtail on IE 11.0 with the fix, it's pretty bad. Without it it's working quite well.

@asturur
Copy link
Member

asturur commented Feb 21, 2018

After that fix i inserted another safety measure about ie

CACHE_FONT_SIZE = 200

so i measure everything at 200 so that rounding measurement are a bit killed in this way.
i think i can bump it up to bigger and remove the IE fix maybe.

@blobinabottle
Copy link
Contributor Author

indeed. And this is a smarter fix than the other condition

@asturur
Copy link
Member

asturur commented Feb 21, 2018

LOL is easy to say what fix is smarter when you did not had to find them in the first place :D :D :D :D

@blobinabottle
Copy link
Contributor Author

:-D In fact you already fixed all the bugs in advance, you just forget you did it :-D

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

Successfully merging a pull request may close this issue.

2 participants