using python, how do I predict the pixel width of text in an SVG?
I am using python to generate an SVG with text that can vary, and I'd like to put a translucent rectangle behind it to guarantee some contrast when it is overlayed on another graphical element later on.
Unfortunately, I do not know how to predict the pixel extents of a specific string of text in SVG. I can choose what font I use (but I don't think monospace will be acceptable) and what font size I'm using.
How would I go about using python to predict the extents of the SVG text so I can generate an appropriately sized rectangle?
I think things would get messy if you did this in Python. Probably the least stressful approach would be to install FreeType and use it to calculate the text dimensions for you. But that's a very large hammer for quite a small nut.
This answer shows an easier approach — use an SVG filter to apply a background directly to the text without having to resize anything.
You can use this method with translucent backgrounds too: