Skip to content

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

Merged
merged 1 commit into from
Jul 31, 2017
Merged

Faster, more compliant parseFont #944

merged 1 commit into from
Jul 31, 2017

Conversation

zbjornson
Copy link
Collaborator

@zbjornson zbjornson commented Jul 24, 2017

  • 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 order
    • supports font-stretch (although it will be ignored)
    • supports more units: em, rem and q work; ch and ex will parse but not work. Supporting ch and ex would be fastest if the API was reworked so that the actual number of device units was calculated in the same call as fill/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 now 20% parses to 20 px. (@chearon?)

Fixes #920
Fixes or at least improves #566

// TODO disabled because existing unit tests assume 100
// font.size *= defaultHeight / 100 / 0.75
break
case 'em':
Copy link
Collaborator

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 '%':
Copy link
Collaborator

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]
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

@LinusU LinusU merged commit ed005a2 into Automattic:master Jul 31, 2017
@LinusU
Copy link
Collaborator

LinusU commented Jul 31, 2017

Awesome!

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.

Text rendering becomes horribly slow after upgrading from 1.6.4 to 1.6.5
3 participants