| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | # -*- perl -*- | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | # | 
| 4 |  |  |  |  |  |  | # $Id: Image_Info_SVG_LibXML.pm,v 1.2 2008/11/22 14:34:16 eserte Exp eserte $ | 
| 5 |  |  |  |  |  |  | # Author: Slaven Rezic | 
| 6 |  |  |  |  |  |  | # | 
| 7 |  |  |  |  |  |  | # Copyright (C) 2008,2009,2016 Slaven Rezic. All rights reserved. | 
| 8 |  |  |  |  |  |  | # This package is free software; you can redistribute it and/or | 
| 9 |  |  |  |  |  |  | # modify it under the same terms as Perl itself. | 
| 10 |  |  |  |  |  |  | # | 
| 11 |  |  |  |  |  |  | # Mail: slaven@rezic.de | 
| 12 |  |  |  |  |  |  | # WWW:  http://www.rezic.de/eserte/ | 
| 13 |  |  |  |  |  |  | # | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | package Image::Info::SVG::XMLLibXMLReader; | 
| 16 |  |  |  |  |  |  |  | 
| 17 | 2 |  |  | 2 |  | 13 | use strict; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 55 |  | 
| 18 | 2 |  |  | 2 |  | 8 | use vars qw($VERSION); | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 98 |  | 
| 19 |  |  |  |  |  |  | $VERSION = '1.05'; | 
| 20 |  |  |  |  |  |  |  | 
| 21 | 2 |  |  | 2 |  | 13 | use XML::LibXML::Reader; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 1055 |  | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | sub process_file { | 
| 24 | 5 |  |  | 5 | 0 | 10 | my($info, $source) = @_; | 
| 25 |  |  |  |  |  |  |  | 
| 26 | 5 |  |  |  |  | 8 | local $_; | 
| 27 |  |  |  |  |  |  |  | 
| 28 | 5 |  |  |  |  | 7 | my(@comments, @warnings); | 
| 29 |  |  |  |  |  |  | local $SIG{__WARN__} = sub { | 
| 30 | 0 |  |  | 0 |  | 0 | push(@warnings, @_); | 
| 31 | 5 |  |  |  |  | 34 | }; | 
| 32 |  |  |  |  |  |  |  | 
| 33 | 5 | 50 |  |  |  | 47 | my $reader = XML::LibXML::Reader->new(IO => $source, load_ext_dtd => 0, expand_entities => 0) | 
| 34 |  |  |  |  |  |  | or die "Cannot read SVG from handle '$source'"; | 
| 35 | 5 |  |  |  |  | 780 | while($reader->read) { | 
| 36 | 6 | 100 |  |  |  | 403 | last if $reader->nodeType == XML_READER_TYPE_ELEMENT; | 
| 37 |  |  |  |  |  |  | } | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | # first XML element | 
| 40 | 5 |  |  |  |  | 30 | my $root_name = $reader->name; | 
| 41 | 5 | 50 |  |  |  | 15 | if ($root_name eq 'svg') { | 
| 42 | 5 |  |  |  |  | 46 | $info->push_info(0, 'height', $reader->getAttribute('height')); | 
| 43 | 5 |  |  |  |  | 21 | $info->push_info(0, 'width', $reader->getAttribute('width')); | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 5 |  | 100 |  |  | 62 | my $version = $reader->getAttribute('version') || 'unknown'; | 
| 46 | 5 |  |  |  |  | 14 | $info->push_info(0, 'SVG_Version', $version); | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | } else { | 
| 49 | 0 |  |  |  |  | 0 | return $info->push_info(0, "error", "Not a valid SVG image, got a <$root_name>"); | 
| 50 |  |  |  |  |  |  | } | 
| 51 |  |  |  |  |  |  |  | 
| 52 | 5 |  |  |  |  | 9 | my $desc; | 
| 53 | 5 |  |  |  |  | 20 | while($reader->read) { | 
| 54 | 164 |  |  |  |  | 59421 | my $type = $reader->nodeType; | 
| 55 | 164 | 100 |  |  |  | 383 | if ($type == XML_READER_TYPE_COMMENT) { | 
|  |  | 100 |  |  |  |  |  | 
| 56 | 12 |  |  |  |  | 53 | push @comments, $reader->value; | 
| 57 |  |  |  |  |  |  | } elsif ($type == XML_READER_TYPE_ELEMENT) { | 
| 58 | 36 |  |  |  |  | 77 | my $name = $reader->name; | 
| 59 | 36 | 100 | 100 |  |  | 233 | if (!$desc && $name eq 'desc') { | 
|  |  | 100 |  |  |  |  |  | 
|  |  | 50 |  |  |  |  |  | 
| 60 | 4 |  |  |  |  | 23 | require XML::Simple; | 
| 61 |  |  |  |  |  |  | # Don't take any chances which XML::SAX is defaulted | 
| 62 |  |  |  |  |  |  | # by the user. We know that we have XML::LibXML, so | 
| 63 |  |  |  |  |  |  | # use this one. | 
| 64 | 4 |  |  |  |  | 7 | local $XML::Simple::PREFERRED_PARSER = 'XML::LibXML::SAX::Parser'; | 
| 65 | 4 |  |  |  |  | 25 | my $xs = XML::Simple->new; | 
| 66 | 4 |  |  |  |  | 460 | my $desc_xml = $reader->readOuterXml; | 
| 67 | 4 |  |  |  |  | 25 | $desc = $xs->XMLin($desc_xml); | 
| 68 |  |  |  |  |  |  | } elsif ($name eq 'title') { | 
| 69 | 1 |  |  |  |  | 32 | my $title = $reader->copyCurrentNode(1)->textContent; | 
| 70 | 1 | 50 |  |  |  | 5 | $info->push_info(0, 'SVG_Title', $title) if $title; | 
| 71 |  |  |  |  |  |  | } elsif ($name eq 'image') { | 
| 72 | 0 |  |  |  |  | 0 | my $href = $reader->getAttribute('xlink:href'); | 
| 73 | 0 | 0 |  |  |  | 0 | $info->push_info(0, 'SVG_Image', $href) if $href; | 
| 74 |  |  |  |  |  |  | } | 
| 75 |  |  |  |  |  |  | } | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  |  | 
| 78 | 5 | 100 |  |  |  | 42 | $info->push_info(0, 'SVG_StandAlone', $reader->standalone == 1 ? "yes" : "no"); | 
| 79 |  |  |  |  |  |  |  | 
| 80 | 5 | 100 |  |  |  | 18 | $info->push_info(0, 'ImageDescription', $desc) if $desc; | 
| 81 |  |  |  |  |  |  |  | 
| 82 | 5 |  |  |  |  | 14 | $info->push_info(0, "color_type" => "sRGB"); | 
| 83 | 5 |  |  |  |  | 14 | $info->push_info(0, "file_ext" => "svg"); | 
| 84 |  |  |  |  |  |  | # "image/svg+xml" is the official MIME type | 
| 85 | 5 |  |  |  |  | 27 | $info->push_info(0, "file_media_type" => "image/svg+xml"); | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | # XXX how to determine images? | 
| 88 | 5 |  |  |  |  | 10 | for (@comments) { | 
| 89 | 12 |  |  |  |  | 22 | $info->push_info(0, "Comment", $_); | 
| 90 |  |  |  |  |  |  | } | 
| 91 |  |  |  |  |  |  |  | 
| 92 | 5 |  |  |  |  | 31 | for (@warnings) { | 
| 93 | 0 |  |  |  |  |  | $info->push_info(0, "Warn", $_); | 
| 94 |  |  |  |  |  |  | } | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | } | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | 1; | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | __END__ |