| 1 | package Image::Info::SVG::XMLSimple; | ||
| 2 | |||
| 3 | $VERSION = '1.05'; | ||
| 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/<!--\s*//; | ||
| 23 | $comment =~ s/\s*-->//; | ||
| 24 | chomp($comment); | ||
| 25 | push @comments, $comment; | ||
| 26 | $comment = ''; | ||
| 27 | } | ||
| 28 | $imgdata .= $_; | ||
| 29 | } | ||
| 30 | |||
| 31 | if( $imgdata !~ /<svg/ ){ | ||
| 32 | return $info->push_info(0, "error", "Not a valid SVG image"); | ||
| 33 | } | ||
| 34 | |||
| 35 | local $SIG{__WARN__} = sub { | ||
| 36 | push(@warnings, @_); | ||
| 37 | }; | ||
| 38 | |||
| 39 | # XML::SAX::PurePerl is the only SAX parser which is not capable | ||
| 40 | # of expanding external entities, so it's the only one not | ||
| 41 | # vulnerable against XXE processing. On the other hand, | ||
| 42 | # XML::SAX::PurePerl is probably the slowest parser, but for | ||
| 43 | # speed one should use XML::LibXML instead. | ||
| 44 | local $XML::Simple::PREFERRED_PARSER = 'XML::SAX::PurePerl'; | ||
| 45 | $xs = XML::Simple->new; | ||
| 46 | $img = $xs->XMLin($imgdata); | ||
| 47 | |||
| 48 | # use Data::Dumper; print Dumper($img); | ||
| 49 | |||
| 50 | $info->push_info(0, "color_type" => "sRGB"); | ||
| 51 | $info->push_info(0, "file_ext" => "svg"); | ||
| 52 | # "image/svg+xml" is the official MIME type | ||
| 53 | $info->push_info(0, "file_media_type" => "image/svg+xml"); | ||
| 54 | |||
| 55 | $info->push_info(0, "height", $img->{height}); | ||
| 56 | $info->push_info(0, "width", $img->{width}); | ||
| 57 | $info->push_info(0, "SVG_StandAlone", $info{standalone}); | ||
| 58 | $info->push_info(0, "SVG_Version", $img->{version} || 'unknown'); | ||
| 59 | |||
| 60 | # XXX Description, title etc. could be tucked away in a <g> :-( | ||
| 61 | $info->push_info(0, "ImageDescription", $img->{desc}) if $img->{desc}; | ||
| 62 | $info->push_info(0, "SVG_Title", $img->{title}) if $img->{title}; | ||
| 63 | |||
| 64 | # $info->push_info(0, "SamplesPerPixel", -1); | ||
| 65 | # $info->push_info(0, "resolution", "1/1"); | ||
| 66 | # $info->push_info(0, "BitsPerSample", 8); | ||
| 67 | |||
| 68 | if( $img->{image} ){ | ||
| 69 | if( ref($img->{image}) eq 'ARRAY' ){ | ||
| 70 | foreach my $img (@{$img->{image}}){ | ||
| 71 | $info->push_info(0, "SVG_Image", $img->{'xlink:href'}); | ||
| 72 | } | ||
| 73 | } | ||
| 74 | else{ | ||
| 75 | $info->push_info(0, "SVG_Image", $img->{image}->{'xlink:href'}); | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | for (@comments) { | ||
| 80 | $info->push_info(0, "Comment", $_); | ||
| 81 | } | ||
| 82 | |||
| 83 | for (@warnings) { | ||
| 84 | $info->push_info(0, "Warn", $_); | ||
| 85 | } | ||
| 86 | } | ||
| 87 | 1; | ||
| 88 | __END__ |