Archive for the ‘Inkscape Development’ Category
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.
News about my my inkscape extensions
Both extensions I’m developing have been given their own google code page: Guillotine and Replace Font. I will use those as a place to keep the code and downloads organised, and I will continue to use this blog as a way of verbally introducing the new features/updates. The second big piece of news (big for me), is that the guillotine extension has been accepted into the inkscape trunk, so next time there’s an Inkscape release, you’ll be running a tiny bit of my code!
Replace Font inkscape extension
Here’s another simple inkscape extension quickly following the previous one. It was inspired by someone on #inkscape wanting a way to replace fonts drawing-wide without it affecting the other styling on the text elements, such as boldface and italics.
It’s probably very rough and ready at the moment, but it should be useful. I plan to change the way the actual font is replaced, because it’s conceivable that it could break as is, but I haven’t had it do so.
Leave me some feedback if you have any problems. Once it’s had a fair bit of testing, I might try to get this one entered into the inkscape repository too.
So – here’s how you use it:
- Download the latest replace_font version and unzip it into your inkscape extensions directory
- Open your drawing in inkscape, preferably one that has some text elements, of course.
- Select Text->Replace Font from the Extensions menu
- Enter the font name that you want to find, e.g. Calibri or Times New Roman
- Enter the font name that you’d like to replace it with, e.g. Sans or Helvetica
- Press Apply
Enjoy!
Edit:
There were a few bug in this extension, as pointed out by su-v from #inkscape:
- Not all fonts were being replaced, because sometimes the font styling was attached to a text element, not a tspan.
- tspan elements without style attributes were causing the extension to crash
- Some fonts (such as Arial Narrow) were being incorrectly (partially) replaced by searches such as “Arial”.
- Fonts in flowed text regions weren’t being replaced at all.
The link above in the original instructions has been updated to point to a new zip file including a fixed version of the extension, along with a couple of new features. Please let me know if you run into any other bugs!
Guillotine inkscape extension
Well – this is my first inkscape extension, it’s a direct copy of the GIMP extension of the same name (under Image->Transform). It is designed to take an image that has had some guides dragged onto it, then slice it up into the rectangles that are formed by those guides. It should ignore guides that lie outside the canvas area, and it should ignore angled guides.
I’m sure it will need more work if and when more people start testing it, but it should be usable as is. Please give it a try and feel free to leave any feedback.
It would be nice to one day see it as part of inkscape proper, but I don’t know the process involved (or if there’s even any demand for this extension – there are similar but slightly different extensions floating around).
So – here’s how you use it:
- Download the latest guillotine version, and unzip it into your inkscape extensions directory
- Open your drawing in inkscape.
- Drag some vertical and horizontal guides onto your drawing
- Select Export->Guillotine from the Inkscape menu
- Enter a directory name and image base name for output (e.g. output will become output1.png, output2.png, etc.)
- (optional) If you’ve previously exported the drawing, you can tick the box to use export hints as the output values.
- Press Apply
Enjoy!