- edited description
Labels are not searchable in SVG output
Consider the following code:
#!/usr/bin/env python3
from pathlib import Path
import matplotlib
import schemdraw
from schemdraw import ImageFormat
import schemdraw.elements as elm
matplotlib.use("Agg")
def main() -> None:
d = schemdraw.Drawing()
d.add(elm.Resistor(d="right", label="1Ω"))
d.add(elm.Capacitor(d="down", label="10μF"))
d.add(elm.Line(d="left"))
d.add(elm.SourceSin(d="up", label="ABC"))
d.draw(show=False)
svg_data = d.get_imagedata(ImageFormat.SVG)
Path("out.svg").write_bytes(svg_data)
pdf_data = d.get_imagedata(ImageFormat.PDF)
Path("out.pdf").write_bytes(pdf_data)
if __name__ == '__main__':
main()
When out.pdf
is created, the label ABC
is searchable.
But when out.svg
is created, it seems as though each letter is rastered a separate object, so searching (in Inkscape 1.0.2 for example) does not work.
Note, in Inkscape, I typically Select All, and run Object | Ungroup twice to make individual elements movable.
Is it possible to change how the SVG is rendered? Or is this out of schemdraw
's control?
Comments (5)
-
reporter -
repo owner This looks like a Matplotlib setting. See this. Matplotlib’s default behavior for SVG output is to convert text to paths, so Inkscape doesn’t recognize the characters as text, but as path elements. It can be changed to output <text> svg elements, which will be searchable, using
matplotlib.rcParams['svg.fonttype'] = 'none'
The downside is that an SVG output with this setting relies on having the fonts installed; it may not look identical on another machine without the same fonts used by Matplotlib.
-
reporter Thanks this works great! I opened a PR to add docs for this to help others in the future.
-
repo owner Merged, thanks!
-
repo owner - changed status to resolved
- Log in to comment