-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Faster, more compliant parseFont #944
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
Conversation
// TODO disabled because existing unit tests assume 100 | ||
// font.size *= defaultHeight / 100 / 0.75 | ||
break | ||
case 'em': |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice 👍
break | ||
case 'cm': | ||
size.value *= 96.0 / 2.54 | ||
case '%': |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So yeah, I don't think the test is right... the units do get sent to C++, but aren't used. We probably don't need to send the units, just the normalized px
value.
It looks like the current version would be doing percentages and mm
wrong, I'll have to look at if the non-Pango build was ever right about that.
]; | ||
|
||
for (var i = 0, len = tests.length; i < len; ++i) { | ||
var str = tests[i++] | ||
, obj = tests[i] | ||
, expected = tests[i] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Awesome! |
Improves perf by ~23x. This is the fastest method that I can come up with, short of dropping into C++, and is faster than parse-css-font and css-font-parser.
Fixes caching -- should be done before parsing the string.
Supports more of the spec:
font-style
,font-variant
,font-weight
,font-stretch
may appear in any orderfont-stretch
(although it will be ignored)em
,rem
andq
work;ch
andex
will parse but not work. Supportingch
andex
would be fastest if the API was reworked so that the actual number of device units was calculated in the same call asfill/strokeText
.Also, I think handling of
%
is wrong but I didn't fix it because there's an existing assertion for it. As I read it, that should be percent of the parent (default/root) font height, and with this commit I've standardized that to 16 pt (common in browsers). Right now20%
parses to20 px
. (@chearon?)Fixes #920
Fixes or at least improves #566