spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / xml / column56

Google SVG Search II

Sr Instructional Designer D2L-Moodle,Clearance
WSI Nationwide, Inc.
US-NJ-Fort Monmouth

Justtechjobs.com Post A Job | Post A Resume
Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs


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

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint

URL: http://www.webreference.com/xml/column56/2.html
Created: May 13, 2002
Revised: May 13, 2002