home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / SVG.pm < prev    next >
Encoding:
Perl POD Document  |  2004-01-07  |  4.0 KB  |  177 lines

  1. package Image::Info::SVG;
  2.  
  3. $VERSION = '1.02';
  4.  
  5. use strict;
  6. no strict 'refs';
  7. use XML::Simple;
  8.  
  9. sub process_file{
  10.     my($info, $source) = @_;
  11.     my(@comments, @warnings, %info, $comment, $img, $imgdata, $xs);
  12.     local($_);
  13.  
  14.     while(<$source>){
  15.     if( ! exists($info{standalone}) && /standalone="(.+?)"/ ){
  16.         $info{standalone} = $1;
  17.     }
  18.     if( /<!--/ .. /-->/ ){
  19.         $comment .= $_;
  20.     }
  21.     if( /-->/ ){
  22.         $comment =~ s/<!--//;
  23.         $comment =~ s/-->//;
  24.         chomp($comment);
  25.         push @comments, $comment;
  26.         $comment = '';
  27.     }
  28.     $imgdata .= $_;
  29.     }
  30.     if( $imgdata =~ /<!DOCTYPE\s+svg\s+.+?\s+"(.+?)">/ ){
  31.     $info{dtd} = $1;
  32.     }
  33.     elsif( $imgdata !~ /<svg/ ){
  34.     return $info->push_info(0, "error", "Not a valid SVG image");
  35.     }
  36.  
  37.     local $SIG{__WARN__} = sub {
  38.     push(@warnings, @_);
  39.     };
  40.  
  41.     $xs = XML::Simple->new;
  42.     $img = $xs->XMLin($imgdata);
  43.  
  44.     $info->push_info(0, "color_type" => "sRGB");
  45.     $info->push_info(0, "file_ext" => "svg");
  46.     # XXX not official type yet, may be image/svg+xml
  47.     $info->push_info(0, "file_media_type" => "image/svg-xml");
  48.     $info->push_info(0, "height", $img->{height});
  49. #    $info->push_info(0, "resolution", "1/1");
  50.     $info->push_info(0, "width", $img->{width});
  51. #    $info->push_info(0, "BitsPerSample", 8);
  52.     #$info->push_info(0, "SamplesPerPixel", -1);
  53.  
  54.     # XXX Description, title etc. could be tucked away in a <g> :-(
  55.     $info->push_info(0, "ImageDescription", $img->{desc}) if $img->{desc};
  56.     if( $img->{image} ){
  57.     if( ref($img->{image}) eq 'ARRAY' ){
  58.         foreach my $img (@{$img->{image}}){
  59.         $info->push_info(0, "SVG_Image", $img->{'xlink:href'});
  60.         }
  61.     }
  62.     else{
  63.         $info->push_info(0, "SVG_Image", $img->{image}->{'xlink:href'});
  64.     }
  65.     }
  66.     $info->push_info(0, "SVG_StandAlone", $info{standalone});
  67.     $info->push_info(0, "SVG_Title", $img->{title}) if $img->{title};
  68.     $info->push_info(0, "SVG_Version", $info{dtd}||'unknown');
  69.  
  70.     for (@comments) {
  71.     $info->push_info(0, "Comment", $_);
  72.     }
  73.     
  74.     for (@warnings) {
  75.     $info->push_info(0, "Warn", $_);
  76.     }
  77. }
  78. 1;
  79. __END__
  80. Colors
  81.     # iterate over polygon,rect,circle,ellipse,line,polyline,text for style->stroke: style->fill:?
  82.     #  and iterate over each of these within <g> too?! and recurse?!
  83.     # append <color>'s
  84.     # perhaps even deep recursion through <svg>'s?
  85. ColorProfile <color-profile>
  86. RenderingIntent ?
  87. requiredFeatures
  88. requiredExtensions
  89. systemLanguage
  90.  
  91. =pod
  92.  
  93. =head1 NAME
  94.  
  95. Image::Info::SVG - SVG support for Image::Info
  96.  
  97. =head1 SYNOPSIS
  98.  
  99.  use Image::Info qw(image_info dim);
  100.  
  101.  my $info = image_info("image.svg");
  102.  if (my $error = $info->{error}) {
  103.      die "Can't parse image info: $error\n";
  104.  }
  105.  my $color = $info->{color_type};
  106.  
  107.  my($w, $h) = dim($info);
  108.  
  109. =head1 DESCRIPTION
  110.  
  111. A functional yet thus far rudimentary SVG implementation.
  112. SVG also provides (for) a plethora of attributes and metadata of an image.
  113.  
  114. This modules supplies the standard key names except for
  115. BitsPerSample, Compression, Gamma, Interlace, LastModificationTime, as well as:
  116.  
  117. =over
  118.  
  119. =item ImageDescription
  120.  
  121. The image description, corresponds to <desc>.
  122.  
  123. =item SVG_Image
  124.  
  125. A scalar or reference to an array of scalars containing the URI's of
  126. embedded images (JPG or PNG) that are embedded in the image.
  127.  
  128. =item SVG_StandAlone
  129.  
  130. Whether or not the image is standalone.
  131.  
  132. =item SVG_Title
  133.  
  134. The image title, corresponds to <title>
  135.  
  136. =item SVG_Version
  137.  
  138. The URI of the DTD the image conforms to.
  139.  
  140. =back
  141.  
  142. =item FILES
  143.  
  144. This module requires L<XML::Simple>
  145.  
  146. =head1 SEE ALSO
  147.  
  148. L<Image::Info>, L<XML::Simple>, L<expat>
  149.  
  150. =head1 NOTES
  151.  
  152. SVG is not yet a standard,
  153. though much software exists which is capable of creating and displaying SVG images. 
  154. For more information about SVG see:
  155.  
  156.  http://www.w3.org/Graphics/SVG/
  157.  
  158. =head1 AUTHOR
  159.  
  160. Jerrad Pierce <belg4mit@mit.edu>/<webmaster@pthbb.org>
  161.  
  162. This library is free software; you can redistribute it and/or
  163. modify it under the same terms as Perl itself.
  164.  
  165. =cut
  166.  
  167. =begin register
  168.  
  169. MAGIC: /^<\?xml/
  170.  
  171. SVG also provides (for) a plethora of attributes and metadata of an image.
  172. See L<Image::Info::SVG> for details.
  173.  
  174. =end register
  175.  
  176. =cut
  177.