-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
measureText problems with web fonts #378
Comments
I encountered an infinite loop in Cesium caused by measureText when a label had a character comprised of two 16-bit character codes. You can reproduce by dropping the following into Sandcastle:
Hit run, browser hangs. Recommend using Chrome so you can kill the tab easily. |
Thanks for reporting this @schmidtk. I can easily reproduce it with the code example you posted. @shunter is this problem related to this issue or should I open a new issue to look into this? |
I'm starting to wonder if the KML parser incorrectly interpreted the character as |
Unrelated to the original issue. The loop in the code seems clearly wrong when the glyph renders blank, which is supposed to be checked by the whitespace regex at the top. @schmidtk What character are you expecting to be using here?
Fixing labels to work properly with unicode is likely to be a significant re-write, I suspect. see also #2543 |
I'll have to take a look at the KML again when I get back to the office on Monday. I didn't have the time to thoroughly debug and used the workaround to strip blank characters from the text instead. It may be expecting an Arabic font (the label was for Baghdad), which is why I thought it may be semi-related to this issue. |
Because web fonts are loaded asynchronously with the rest of the page, using them with labels introduces a potential race condition when we measure the size of the letters in order to make textures.
Both writeTextToCanvas and measureText internally create canvases, and set up the font information. If the font is a web font, I have already seen a case where this leads to an infinite loop inside measureText, which I assume results from the web font being loaded in between the canvas creations, resulting in different metrics which violate some of the assumptions in the algorithm.
Fixing this is going to be a major pain. Detecting if a web font has loaded or not seems to be a huge missing feature in web browsers, but I've found some libraries and sample code that might let us get it working.
http://www.lalit.org/lab/javascript-css-font-detect/ looks the most promising. The general idea is to use CSS fallback fonts and the assumption that the user's web font (whatever that is) will have a different width than the fallbacks, of course it fudges the widths more by making the font size really big and using the whole alphabet, etc.
The other thing to figure out is whether writeTextToCanvas needs to become asynchronous or not; it probably does since it can take an unknown amount of time to measure text correctly.
The text was updated successfully, but these errors were encountered: