Skip to content

piet-svg will embed standard fonts in svg files #593

@twanvl

Description

@twanvl

Piet-svg will always embed a font when using text, which means that as soon as you use any text you get large files. I would have thought that if I used a standard font like FontFamily::SANS_SERIF, that this would be translated to the system sans-serif font in the svg file. But the generated svg files will contain a font-face definition embedding the entire font:

@font-face {
font-family: "sans-serif";
font-weight: 400;
font-style: normal;
src: url(data:...)
}

I want to use piet to generate a lot of small plots, and wasting half a megabyte per file on font data is unacceptable.

The piet-svg source code says "If we are using a named font, then mark it for inclusion." (piet-svg/src/lib.rs:272), which implies that unnamed fonts (like sans-serif) should not be included. The solution is to actually perform the promised check:

        // If we are using a named font, then mark it for inclusion.
        if !layout.font_face.is_generic() {
            self.text()
                .seen_fonts
                .lock()
                .unwrap()
                .insert(layout.font_face.clone());
        }

It could also make sense to let the user specify if fonts should be embedded, because there can be reasons not to do so (I am thinking of license restrictions for example, or in the context of an application where we know a certain font is available).

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