Calculating flowRegion dimensions using font-size in pixels.
I'm currently working on some Java code that will export Inkscape ready SVG from an open source mapping application. One of the things I need to do is export map labels. I've been taking a look at how Inkscape represents text labels in its SVG flavor. I'm looking for advice on how to calculate the dimensions of the rectangle element used as the flow region.
If I know the height of the font for the text labels in pixels, is there a formula that I can use to calculate the width and height I need for the flow region? Is this calculation dependent on the type of font? (I'm not really worried about getting the flow region a little too big. I'm more worried about getting it too small, and then not being able to see the label in Inkscape.)
Thanks for any suggestions.
Landon
2010/3/15 Redefined Horizons <redefined.horizons@...400...>:
I'm currently working on some Java code that will export Inkscape ready SVG from an open source mapping application. One of the things I need to do is export map labels. I've been taking a look at how Inkscape represents text labels in its SVG flavor. I'm looking for advice on how to calculate the dimensions of the rectangle element used as the flow region.
Please don't do this... Our flowed text is not compatible with SVG 1.1. It might be compatible with SVG 1.2, but that specification is not yet finalized. Moreover the SVG version attribute on Inkscape SVGs is 1.1, which is clearly wrong. It's actually the worst SVG incompatibility in Inkscape.
Regards, Krzysztof
Krysztof:
Do you have another suggestion on how I can get map labels imported into Inkscape? My SVG text books both use CSS styling for text, which looked a little ugly.
I'm not really worried about compatability with the SVG standard. Inkscape is a great drawing and cartogrpahy tool and I'm just trying to get my data into it.
If there is another, more "standard compliant" way to generate text that Inkscape can read, please tell me about it.
Thanks for the feedback.
Landon
2010/3/15 Krzysztof Kosiński <tweenk.pl@...400...>:
2010/3/15 Redefined Horizons <redefined.horizons@...400...>:
I'm currently working on some Java code that will export Inkscape ready SVG from an open source mapping application. One of the things I need to do is export map labels. I've been taking a look at how Inkscape represents text labels in its SVG flavor. I'm looking for advice on how to calculate the dimensions of the rectangle element used as the flow region.
Please don't do this... Our flowed text is not compatible with SVG 1.1. It might be compatible with SVG 1.2, but that specification is not yet finalized. Moreover the SVG version attribute on Inkscape SVGs is 1.1, which is clearly wrong. It's actually the worst SVG incompatibility in Inkscape.
Regards, Krzysztof
Do you have another suggestion on how I can get map labels imported
into Inkscape?
For single line map labels, I would center the text, so that changing the size doesn't change the position, eg: .ctext { text-anchor: middle; text-align: center; alignment-baseline: middle; }
<text x=50 y=50 class="ctext">The text</text>
For multi-line labels, I would split the text into tspan's, eg:
.multiline tspan { text-anchor: middle; text-align: center; alignment-baseline: middle; }
<text x=50 y=50 class="multiline"> <tspan dy="-1.2em" x=50>Hello</tspan> <tspan dy="1.2em" x=50>World!</tspan> <tspan dy="1.2em" x=50>Line 3.</tspan> </text>
Where everything will be centered on x=50, y=50, and the line_height is 1.2em. The dy's should be: 1. first dy: - ( line_height * (num_lines - 1) / 2.0 ) 2. all other dy's: line_height
Hopefully this helps, - Alex
Alex,
This is helpful. You message seems to indicate that Inkscape can indeed handle the use of CSS for text elements, even though this isn't the default SVG output the program creates. However, I couldn't get Inkscape to open a file with embedded CSS elements. I am likely doing something wrong. Do you have an example of an Inkscape SVG document that uses CSS to style text in the same way as your e-mail example?
Your message did prompt me to do some digging on the way Inkscape handles text. On the wiki FAQ I found some useful information on flowed verus non-flowed text:
http://wiki.inkscape.org/wiki/index.php/FAQ#What_about_flowed_text.3F
It sounds like non-flowed text is the way for me to go until the SVG 1.2 spec gets sorted out.
I think I can use a text element that looks like this:
<text xml:space="preserve" style="font-size:40px;font-style:normal;font-weight:normal;fill:#000000;fill-opacity:1;stroke:none;font-family:Bitstream Vera Sans" x="480" y="398.07648" id="text3355"> <tspan sodipodi:role="line" id="tspan3357" x="480" y="398.07648">test</tspan> </text>
This is the SVG output I got when I didn't drag the text creation tool, which I had always done before reading the FAQ.
Thanks for the suggestions.
Landon
2010/3/16 Alex Leone <acleone@...400...>:
Do you have another suggestion on how I can get map labels imported
into Inkscape?
For single line map labels, I would center the text, so that changing the size doesn't change the position, eg: .ctext { text-anchor: middle; text-align: center; alignment-baseline: middle; }
<text x=50 y=50 class="ctext">The text</text>
For multi-line labels, I would split the text into tspan's, eg:
.multiline tspan { text-anchor: middle; text-align: center; alignment-baseline: middle; }
<text x=50 y=50 class="multiline"> <tspan dy="-1.2em" x=50>Hello</tspan> <tspan dy="1.2em" x=50>World!</tspan> <tspan dy="1.2em" x=50>Line 3.</tspan> </text>
Where everything will be centered on x=50, y=50, and the line_height is 1.2em. The dy's should be:
- first dy: - ( line_height * (num_lines - 1) / 2.0 )
- all other dy's: line_height
Hopefully this helps, - Alex
2010/3/15 Krzysztof Kosiński <tweenk.pl@...400...>:
Please don't do this... Our flowed text is not compatible with SVG 1.1. It might be compatible with SVG 1.2, but that specification is not yet finalized. Moreover the SVG version attribute on Inkscape SVGs is 1.1, which is clearly wrong. It's actually the worst SVG incompatibility in Inkscape.
That is true, but it doesn't address the original question. Whether you use flowed or regular text, the problem of finding its bbox from a script is the same.
And the solution is also the same: you should run Inkscape with --query-id or --query-all command line parameters and parse the output. In most cases this is infinitely easier than writing your own SVG processor that would correctly take into account chained transforms, font metrics, stroke and filters, etc. etc.
Thanks for the comments Bulia. You've helped me appreciate all of the complexities that could factor into calculating the bounding box for text elements. I should have known it wouldn't be that easy, as I've got some knowledge of graphics rendering in Java.
I'm writing a plug-in for an open source Java applicaiton, so I don't know that I can tightly couple to Inkscape as you suggest (although it sounds very interesting).
I think my best option at this point may be guestimating the bounding box size. This make take some trial and error, but I can probably get something to work with a default font, font size, stroke, fill, etc.
I appreciate everyone's comments. I will make sure I post my source code (GPL) and some of the results of my work when I get the kinks worked out.
Landon
2010/3/17 bulia byak <buliabyak@...400...>:
2010/3/15 Krzysztof Kosiński <tweenk.pl@...400...>:
Please don't do this... Our flowed text is not compatible with SVG 1.1. It might be compatible with SVG 1.2, but that specification is not yet finalized. Moreover the SVG version attribute on Inkscape SVGs is 1.1, which is clearly wrong. It's actually the worst SVG incompatibility in Inkscape.
That is true, but it doesn't address the original question. Whether you use flowed or regular text, the problem of finding its bbox from a script is the same.
And the solution is also the same: you should run Inkscape with --query-id or --query-all command line parameters and parse the output. In most cases this is infinitely easier than writing your own SVG processor that would correctly take into account chained transforms, font metrics, stroke and filters, etc. etc.
-- bulia byak Inkscape. Draw Freely. http://www.inkscape.org
As Alex indicated, you don't necessarily need to know the bounding box if using just a <text> element: the SVG only needs to know the position (and you can set text alignment according to whether it's most convenient to specify start, middle or end of the text).
If writing multiple lines, then you need to know a reasonable line height, though this is an aesthetic/typographical decision as much as anything; without checking what I've used myself, a reasonable starting point might be 1.2 * font-size.
The main reason to ask Inkscape for a bounding box (as Bulia describes in the previous message) would be if you need to make decisions based on avoiding overlap between text and other things.
Re getting this information from a Java program: without having looked for very long, I don't see a popen-like method in Java. The closest I found was http://www.devdaily.com/java/java-exec-system-command-pipeline-pipe (which mentions the Apache exec project as another implementation).
If all else fails, you can no doubt get an approximate answer from Java's text-formatting stuff, e.g. see http://java.sun.com/docs/books/tutorial/2d/text/measuringtext.html
pjrm.
Thanks for the response Peter. You've nicely condensed what I have learned from this thread but failed to state as eloquently.
I like your suggestion of using bounding box calculations to avoid labeling conflicts. I think I can handle a lot of this in the Java program. But I'll save that for a subsequent release. Right now I just want to get something simple working. :]
Landon
On Thu, Mar 18, 2010 at 5:12 AM, Peter Moulder <Peter.Moulder@...38...> wrote:
As Alex indicated, you don't necessarily need to know the bounding box if using just a <text> element: the SVG only needs to know the position (and you can set text alignment according to whether it's most convenient to specify start, middle or end of the text).
If writing multiple lines, then you need to know a reasonable line height, though this is an aesthetic/typographical decision as much as anything; without checking what I've used myself, a reasonable starting point might be 1.2 * font-size.
The main reason to ask Inkscape for a bounding box (as Bulia describes in the previous message) would be if you need to make decisions based on avoiding overlap between text and other things.
Re getting this information from a Java program: without having looked for very long, I don't see a popen-like method in Java. The closest I found was http://www.devdaily.com/java/java-exec-system-command-pipeline-pipe (which mentions the Apache exec project as another implementation).
If all else fails, you can no doubt get an approximate answer from Java's text-formatting stuff, e.g. see http://java.sun.com/docs/books/tutorial/2d/text/measuringtext.html
pjrm.
participants (5)
-
Alex Leone
-
bulia byak
-
Krzysztof Kosiński
-
Peter Moulder
-
Redefined Horizons