These helpful short articles on the subject:
http://my.opera.com/MacDev_ed/blog/2009/01/21/getting-boundingbox-of-svg-elements
http://my.opera.com/MacDev_ed/blog/getting-screen-boundingboxes-in-svg
You might even find this to be useful at some point:
http://www.carto.net/papers/svg/textFlow/index.svg
Best, Jon
And the spec:
http://www.w3.org/TR/SVG/types.html#__svg__SVGLocatable__getBBox
Mike
String.prototype.width || (String.prototype.width = function(font) {
var f = font || '12px arial',
o = $('<div>' + this + '</div>')
.css({'position': 'absolute', 'float': 'left', 'visibility': 'hidden', 'font': f})
.appendTo($('body')),
w = o.width();
o.remove();
return w;
});
This is actually a slightly older implementation, I've also implemented this where instead of feeding it a font string, you could feed it a container, and it would take the font properties set on the container to do the measurement (pretty straight forward how to implement that into the above code).
To sum up converting this to use SVG, just have an invisible SVG container to do this in, OR you could use the SVG container you are using, and simply append, then remove, invisible text, using the getBBox for the measurement,
Its also easy to use this to implement a way to do automatic line breaks in SVG (and with a minor change you can implement a String.height() function as well. I use this for creating legends in the top right corner of my graphs.
Bob
________________________________________
From: d3...@googlegroups.com [d3...@googlegroups.com] on behalf of Mike Bostock [mbos...@cs.stanford.edu]
Sent: Sunday, August 21, 2011 2:06 PM
To: d3...@googlegroups.com
Subject: Re: pre-computing size of svg:text objects
And the spec:
http://www.w3.org/TR/SVG/types.html#__svg__SVGLocatable__getBBox
Mike
This e-mail message, and any attachments, is intended only for the use of the individual or entity identified in the alias address of this message and may contain information that is confidential, privileged and subject to legal restrictions and penalties regarding its unauthorized disclosure and use. Any unauthorized review, copying, disclosure, use or distribution is strictly prohibited. If you have received this e-mail message in error, please notify the sender immediately by reply e-mail and delete this message, and any attachments, from your system. Thank you.