Skip to content

[Bug]: problem with font property in text elements of svg figures #22528

Closed
@cderemble

Description

@cderemble

Bug summary

Starting with 3.5.0, figures exported as SVG without font embedded in the file (i.e. “svg.fonttype” set to “none”) use the font CSS shorthand property.
The problem is that the font-family generic name is quoted and some viewers don’t like it (chrome for exemple). My understanding (reading https://developer.mozilla.org/en-US/docs/Web/CSS/font-family) is that only the family name should be quoted if it contains spaces.

Code for reproduction

import matplotlib
import matplotlib.pyplot
matplotlib.rcParams["svg.fonttype"] = "none"
matplotlib.rcParams["font.family"] = "sans-serif"
matplotlib.use("SVG")
 
fig = matplotlib.pyplot.figure()
axes = fig.add_subplot()
axes.plot([1, 2], [3, 4], label="test")
axes.legend()
fig.savefig(f"test.svg")

Actual outcome

In the text elements, the generic name of the font-family is quoted (here 'sans-serif')
<text style="font: 10px 'sans-serif'; text-anchor: start" x="94.6" y="57.629812" transform="rotate(-0, 94.6, 57.629812)">test</text>

Expected outcome

The generic name of the font-family should not be quoted
<text style="font: 10px sans-serif; text-anchor: start" x="94.6" y="57.629812" transform="rotate(-0, 94.6, 57.629812)">test</text>

Additional information

No response

Operating system

No response

Matplotlib Version

3.5.1

Matplotlib Backend

SVG

Python version

3.9.10

Jupyter version

No response

Installation

conda

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions