Just be sure to test that they perform correctly in the browsers,
because currently there is not a whole lot of consistency w/ regard to
the text and font properties as a whole.
This approach is also too bulky for your needs -
http://www.carto.net/papers/svg/textFlow/. From what I have heard,
text should be a lot easier to work with in svg 2.0 (n years from
now).
Jon
Sorry I didn't notice this thread was till going after my last post.
I take it you are looking for the SVG example using a clipping path?
(I think there are some posted already, though not finding it right
now).... if you still need it, I'll try to find time tonight, after
work, to post a minimal example.
On the other hand, if you're interested in the generic JavaScript
approach I was initially using (which works quite fine until you have
100's of strings that need to update quickly, and works whether you're
using HTML or CSS) I can post that code (I added a width function and
a shorten function onto the String prototype in JS).
Here's that code (in jQuery... could port to d3 in prob 5 minutes, and
generic JS in a few more):
//Also had a version of this function that took a DOM element or a
string as the font, if its a DOM element it would pull the font
information from it
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, 'white-space':'nowrap'})
.appendTo($('body')),
w = o.width();
o.remove();
return w;
});
/****
This implementation of shorten is kinda retarded, wasn't concerned
with performance for what I initially wrote this for, BUT I'm sure
with a little thought you could implement something better.
One easy improvement would be to count the number of characters in the
string, and the proportion of the current width vs the needed, and
remove more than a single character at a time...
could likely drastically improve the speed of this.
Another idea I had was to persist the container created by the width
function, so you don't have to generate and delete it on every call.
****/
String.prototype.shorten || (String.prototype.shorten =
function(width, font) {
var f = font || '12px arial',
s = this;
while ( width < s.width() ) {
s = s.substring(0, s.length-1);
}
return s;
});
Again, not really the best performing code... when it comes to SVG and
dealing with > 100 strings, or dealing with a very interactive
visualization that will need to recalculate string width rapidly, a
clipping mask works a lot better.
Bob