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