Skip to content

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

Closed

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

Activity

  1. changed the title [-][Bug]: [/-] [+][Bug]: problem with font property in text elements of svg figures[/+] on Feb 22, 2022
  2. anntzer commented on Feb 22, 2022

    @anntzer
    Contributor

    I agree with the report. The relevant code is at

    f'{prop.get_family()[0]!r}', # ensure quoting

    and I guess we simply need to hard-code the list of generic names that should not be quoted.

  3. cderemble commented on Feb 22, 2022

    @cderemble
    Author

    Or something like this
    font_parts.append(f'{short_float_fmt(prop.get_size())}px')
    font_parts.append(", ".join([f'{prop.get_name()!r}'] + prop.get_family()))

  4. added 2 commits that reference this issue on Aug 16, 2022
    b31ed3f
    1d42a5d
  5. added 2 commits that reference this issue on Sep 1, 2022
    a48f1a2
    5027f6c
  6. removed
    🌱 Good first issueOpen a pull request against these issues if there are no active ones!
    on Sep 1, 2022
  7. added this to the v3.6.1 milestone on Sep 1, 2022
  8. added 4 commits that reference this issue on Sep 29, 2022
    3296b47
    ea34677
    a1cd412
    a0fcf79
  9. added a commit that references this issue on Oct 18, 2022
    cdbf339
  10. added a commit that references this issue on Dec 19, 2022
    b5e7582
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions