Dom-to-image: Font variation after Chrome update

Created on 12 Jun 2018  路  9Comments  路  Source: tsayen/dom-to-image

Expected behavior

An image is created that is identical to the DOM element being referenced

Actual behavior

Font styles (weights, size, etc...) seems to be reverting to the browser defaults.

Library version

2.5.2

Browsers

  • [ ] Chrome 67.0.3396.79.

Most helpful comment

I've resolved this bug on our company product. The real problem is a css font property you getting here:

function cloneStyle() {
copyStyle(window.getComputedStyle(original), clone.style);

function copyStyle(source, target) {
    target.fontStretch == '';

    if (source.cssText) target.cssText = source.cssText;
    else copyProperties(source, target);

    target.fontStretch = 'normal';
    // here's my fix

    function copyProperties(source, target) {
        util.asArray(source).forEach(function (name) {
            target.setProperty(
                name,
                source.getPropertyValue(name),
                source.getPropertyPriority(name)
            );
        });
    }
}

If I don't do it such way, my font css property become something like
font: 400 100% 14px/14px Arial, Helvetica, sans-serif;

Firefox & Chrome will warn you that whole font statement is wrong and no one of your font property except browser default will not be applied for svg that will become wrong canvas.

image

You can check it on your own if you set breakpoint to the if (source.cssText) target.cssText = source.cssText; string.

It's not looks like a "silver bullet" so I'll wait for more elegant solution with community help.

All 9 comments

+1. Have the same issue. Very bad

+1. Have the same issue.

+1. Have the same issue.

it applies to 2.6.0 (latest at the time of writing) as well

I've resolved this bug on our company product. The real problem is a css font property you getting here:

function cloneStyle() {
copyStyle(window.getComputedStyle(original), clone.style);

function copyStyle(source, target) {
    target.fontStretch == '';

    if (source.cssText) target.cssText = source.cssText;
    else copyProperties(source, target);

    target.fontStretch = 'normal';
    // here's my fix

    function copyProperties(source, target) {
        util.asArray(source).forEach(function (name) {
            target.setProperty(
                name,
                source.getPropertyValue(name),
                source.getPropertyPriority(name)
            );
        });
    }
}

If I don't do it such way, my font css property become something like
font: 400 100% 14px/14px Arial, Helvetica, sans-serif;

Firefox & Chrome will warn you that whole font statement is wrong and no one of your font property except browser default will not be applied for svg that will become wrong canvas.

image

You can check it on your own if you set breakpoint to the if (source.cssText) target.cssText = source.cssText; string.

It's not looks like a "silver bullet" so I'll wait for more elegant solution with community help.

@tsayen Considering how much a loss of functionality this is for Chrome users, what is the process for expediting a new release that fixes this?

The one-liner that @newmandani suggested seems fine as long as the fontStretch was originally 100% (also since no browsers support font-stretch right now, it's a fine bandaid for any case at worst).

Also related: #220, #235

This is actually better fixed by copying the font itself over.

See https://github.com/IDisposable/dom-to-image-more/commit/7d1476ab36a8b9bdc22073b3e3553d3648696064#diff-94b43d5a54b46e49799cce165ab4fec0R236

Was this page helpful?
0 / 5 - 0 ratings

Related issues

da1z picture da1z  路  6Comments

shahebaz444 picture shahebaz444  路  5Comments

piyushSinghalDemo picture piyushSinghalDemo  路  8Comments

AvanishKumar008 picture AvanishKumar008  路  8Comments

rhmg picture rhmg  路  9Comments