Calculating Text Size / Commandline Broken?

Discuss SVG code, accessible via the XML Editor.
James777
Posts: 12
Joined: Tue May 05, 2009 8:40 pm

Calculating Text Size / Commandline Broken?

Postby James777 » Tue Jun 04, 2013 5:05 am

Is there a formula anywhere which possible to calculate a SVG's text elements pixel size in PHP without having to use the inkscape commandline?

One reason i wanted to do this without the commandline was that it seems to return the wrong width and height dimensions or maybe inkscape uses some alternative scale system. For some tasks it will involve looping also so since the commandline can take a while, having a pure maths formula would be better to speed up the code. The version i currently have is Inkscape 0.48.4 r9939.

I know this is either a bug or due to different measurement values than pixels being returned because i made a function to center elements and for rectangles which gets the width and height directly from the SVG XMl rather than using the commandline and it's perfect and if i save the SVG then center using the inkscape align tools it keeps the same position.

For text however in the XML it does not give a width value and i am not fully sure how to calculate it taking into account everything like kerning, bold/italic, word spacing and so on so i instead use the inkscape commandline. When i use the same function i used on the rectangles with the text it is offset which shows that i am not being given the correct pixel width/height back in from the commandline. Also with the text height it seems i needed to add the texts height to center vertically however it also seems slightly offset on that axis.

If there is a way to calculate a SVG's text elements pixel size in PHP without having to use the inkscape commandline? That would be the most useful thing i think but otherwise if that is not possible for some reason and the commandline values problem is some sort of different sizing system or not in pixels for some reason, does anyone know the maths to correctly convert the value output by the commandline to one in a pixel size value if possible?

If anyone could help with this it would be a huge help to me, many thanks.

Lazur
Posts: 4717
Joined: Tue Jun 14, 2016 10:38 am

Re: Calculating Text Size / Commandline Broken?

Postby Lazur » Tue Jun 04, 2013 5:40 am

This might answer your question:
http://www.inkscapeforum.com/viewtopic.php?f=14&t=13744&p=53496
I remember here was a topic on kerning issues which explained
that the center aligned text uses the bounding box width to center align, not the setting of the text anchor in the svg codes.
Can't tell you much more on the subject.

James777
Posts: 12
Joined: Tue May 05, 2009 8:40 pm

Re: Calculating Text Size / Commandline Broken?

Postby James777 » Thu Jun 06, 2013 12:07 am

It could be related but there is not really a solution in that post so i still have the same issues. Could this be a bug maybe because it seems the offset position is scaled so the commandline probably has something broken in it's maths and gives the wrong value back.

For example i did a test and saved 2 SVG's with one via inkscape and a regular center in selection and the other done with php, here is the result.

x="44.045795" y="332.05405" (Centered by Inkscape)

x="44.915619" y="334.663535" (Centered by code using Inkscape Width/Height values from the commandline)

x="0.869824" y="2.609485" (Difference/Offset)

So you can see there is a difference where it is offsetting the position. The problem is if the text size changes then so does this offset so it is no simple workaround to correct it as i don't know how the offset is calculated to correct it, do any Inkscape developers knows what is happening?


Return to “SVG / XML Code”