Skip to content

SVG fonts depend upon browser-only APIs #802

@gingerchris

Description

@gingerchris

When working with a font using the SVG table, in node, I'm experiencing the following error:

const svgDocument = new DOMParser().parseFromString(text, 'image/svg+xml');
                        ^
ReferenceError: DOMParser is not defined

On investigation I can see that the makeSvgImage function makes use of browser-specific DOM APIs:

  • DOMParser
  • document.createElement / document.appendChild
  • svg.getBBox
  • new Image

I think these APIs not being available in node pretty much makes opentype SVG fonts unusable in node at present, particularly given the rendering pipeline seems to rely on returning Images.

I have some experience of working with SVGs in node and would like to help resolve this but would need some direction please. Most of what I've done before relies pretty heavily on additional dependencies (svg-path-bbox for bounding box calculation, linkedom for DOM stuff, canvas for Image) but these are not particularly lightweight (particularly canvas which comes with a bunch of overheads).

It seems like it would be better to look at alternative ways of rendering here, like maybe returning SVGs as strings rather than Images? I wonder if it's also possible to get the bounding box dimensions from elsewhere in the font file - like from the fallback glyphs used in environments where the svg table is not supported?

Thanks for all your hard work on this project.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions