Replace Font difficulties
The Replace Font extension was updated a few days ago. The main new feature is an improved interface. There are three tabs now, and whichever one is open determines the action to be performed, so I’m both using the notebook control a visual tool, and abusing it as a fake optiongroup. The three modes now offered are: Find and Replace font, Replace font and List all fonts. There is also now a dropdown option below offering to work on the entire drawing or just the selected items only. The code has been made much cleaner, and is now commented too.
Back to the difficulties I’m having though though; replacing fonts is a slightly more complicated task than I’d realised. The nesting of various text elements (e.g. text and tspan elements), and the two different ways of applying styles to these text-ish elements makes it difficult to know how many replacements are being made. Of course it’s easy to just count all replacements, but the number of text styles that needed to be replaced will be possibly more than twice the number of actual elements affected. Here’s a concrete example, because I don’t quite believe in the explanation I just wrote:
- Open inkscape 0.47 on Ubuntu 10.10 Alpha 3
- Click anywhere with the text tool and type in: Hello<press return>World.
- Press Ctrl-A, change the font to Bitstream Charter.
If you do this simple sequence of steps, you will probably (and understandably) think that you have made a single text element. If you are the skeptical kind (like me), you might even think that you had made two text elements. You’ve actually made three. There are two tspan elements nested inside a text element.
So, you may now thinking, well, just count the one parent element then and style the two inner elements. But all three are style-able. And all three are style-able in two different ways. I haven’t found any documentation to explain inheritance or precedence of styles between the different elements, or between the two (potentially) conflicting style attributes themselves, one of which is unique to inkscape, and doesn’t appear anywhere in the SVG spec. The two style attributes are ” font-family” and “-inkscape-font-specification”, and they can both apply to parents and children.
We now have potentially six font styles to replace for two lines of text from one click of the text tool. It just so happens (for me) that inkscape sets the font styles in the following ways: The text element has just a font-family, and the tspans have both the -inkscape-font-specification and the font-family. So there are five to deal with, but of course it’s the potential number that matters to my code. I believe that my tool does replace all five (or six) correctly, but you can see it’s quite difficult to usefully send a message to the user such as “Replaced that font in X places”, for now, users will have to live with “Replaced all instances of that font successfully” or similar.
Note: I realise 0.48 has just come out, and there are internal improvements to text handling, but I haven’t had a chance to look at these yet. Also – I know I could probably nag the inkscape-devel list and ask for some guidelines about the different style attributes, and rules about applying them to the elements, but again haven’t yet had a chance.