Google SVG Search II (2/2) - exploring XML
Google SVG Search II
Helper functions
One of the major limitations in SVG that is currently being addressed is the weak support for clipping and wrapping text. Future versions of SVG will have more elaborate capabilities, but in order to make this tool work for the broadest audience possible we won't wait for them here but write our own helper functions:
sub stripHTML { my ($str) = @_; $str=~s///sg; return $str; }
The above function takes out any text in angle brackets from a given string. This is only safe if Google encodes all brackets that occur in the text but we'll assume that for the moment. I could not find any documentation on this, so handle with care.
Next we need a simplistic line break algorithm:
sub addtext { my ($tag, $str, $y, $w, $h) = @_; my @words = split(/ /, $str); my $line = ''; my $row = 1; foreach my $word (@words) { if (length($line) + length($word) > 30) { $tag->text(x=>$w+2, y=>$h+$y+12*$row)->cdata($line); $row++; $line = ''; } $line .= $word . ' '; } $tag->text(x=>$w+2, y=>$h+$y+12*$row)->cdata($line); }
This function breaks a string into multiple lines of less than 30 characters. Access to font metrics functions would be nice for more accurately calculating line breaks but again this is only work in progress for the SVG standardization effort.
The SOAP and SVG libraries indeed do their magic in our code to produce the following result, when searching for the phrase "exporing XML:"
Conclusion
We did it again! Not counting the helper functions, we produced an SVG image out of a list of text blocks in about ten lines of code. Of course there is a lot of room for styling, tweaking and fine-tuning, but for the purposes of this column it is quite sufficient. Feel free to download the script and extend it as you see fit. Come back next time, and we'll turn our script into a CGI application.
Produced by Michael Claßen
URL: https://www.webreference.com/xml/column56/2.html
Created: May 13, 2002
Revised: May 13, 2002