| 1 | package Apache::AxKit::Language::Svg2AnyFormat; | ||
| 2 | |||
| 3 | @ISA = ( 'Apache::AxKit::Language' ); | ||
| 4 | |||
| 5 | BEGIN { | ||
| 6 | $Apache::AxKit::Language::Svg2AnyFormat::VERSION = 0.06 | ||
| 7 | } | ||
| 8 | |||
| 9 | use Apache; | ||
| 10 | use Apache::Request; | ||
| 11 | use File::Copy (); | ||
| 12 | use File::Temp (); | ||
| 13 | use File::Path (); | ||
| 14 | use Cwd; | ||
| 15 | use strict; | ||
| 16 | use warnings; | ||
| 17 | |||
| 18 | my $TEMPDIR_; | ||
| 19 | |||
| 20 | my %Config = | ||
| 21 | ( | ||
| 22 | SVGOutputMimeType => "image/png", | ||
| 23 | SVGOutputSerializer => "ImageMagick" | ||
| 24 | ); | ||
| 25 | |||
| 26 | my %MimeTypeSuffixFormatMappings = | ||
| 27 | ( | ||
| 28 | "image/png" => [ "png" , "png" ], | ||
| 29 | "image/jpeg" => [ "jpg" , "jpeg" ], | ||
| 30 | "image/gif" => [ "gif" , "gif" ], | ||
| 31 | "image/tiff" => [ "tiff", "tiff" ], | ||
| 32 | "application/pdf" => [ "pdf" , "pdf" ], | ||
| 33 | "application/postscript" => [ "eps" , "eps" ] | ||
| 34 | ); | ||
| 35 | |||
| 36 | |||
| 37 | sub stylesheet_exists () { 0; } | ||
| 38 | |||
| 39 | sub handler | ||
| 40 | { | ||
| 41 | my $class = shift; | ||
| 42 | my ( $r, $xml_provider, undef, $last_in_chain ) = @_; | ||
| 43 | |||
| 44 | my $mime; | ||
| 45 | my $suffix; | ||
| 46 | my $serializer; | ||
| 47 | my $serialized_svg; | ||
| 48 | my $temp_svg; | ||
| 49 | |||
| 50 | AxKit::Debug(8, "Transform started!!!!"); | ||
| 51 | |||
| 52 | if( ! $last_in_chain ) { | ||
| 53 | fail( "This is a Serializer, hence it has to be the last in the chain!" ); | ||
| 54 | } | ||
| 55 | |||
| 56 | ( $mime, $suffix ) = &mimeTypeSuffixHandling( $r ); | ||
| 57 | |||
| 58 | AxKit::Debug(8, "MimeType is set to '$mime'"); | ||
| 59 | AxKit::Debug(8, "Suffix is set to '$suffix'"); | ||
| 60 | |||
| 61 | $serializer = $r->dir_config( "SVGOutputSerializer" ) || $Config{SVGOutputSerializer}; | ||
| 62 | |||
| 63 | $temp_svg = &createTempSVGFile( $r, $xml_provider ); | ||
| 64 | |||
| 65 | if( $serializer eq "ImageMagick" ) { | ||
| 66 | $serialized_svg = &serializeWithImageMagick( $temp_svg, $suffix ); | ||
| 67 | } elsif( $serializer eq "LibRSVG" ) { | ||
| 68 | AxKit::Debug(8, "Serializer is: LibRSVG"); | ||
| 69 | $serialized_svg = &serializeWithLibRSVG( $temp_svg, $MimeTypeSuffixFormatMappings{$mime}->[1], $suffix ); | ||
| 70 | } else { | ||
| 71 | fail( "This is an unknown serializer for me." ); | ||
| 72 | } | ||
| 73 | |||
| 74 | AxKit::Debug(8, "Serialization finished."); | ||
| 75 | |||
| 76 | $AxKit::Cfg->AllowOutputCharset(0); | ||
| 77 | |||
| 78 | my $pdfh = Apache->gensym(); | ||
| 79 | |||
| 80 | open( $pdfh, "<$serialized_svg" ) or fail( "Could not open $serialized_svg: $!" ); | ||
| 81 | $r->content_type( $mime ); | ||
| 82 | local $/; | ||
| 83 | |||
| 84 | $r->print(<$pdfh>); | ||
| 85 | |||
| 86 | &cleanup(); | ||
| 87 | |||
| 88 | return Apache::Constants::OK; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub serializeWithLibRSVG { | ||
| 92 | my $infile = shift; | ||
| 93 | my $format = shift; | ||
| 94 | my $suffix = shift; | ||
| 95 | my $converted_file = "$TEMPDIR_/temp." . $suffix; | ||
| 96 | |||
| 97 | require Image::LibRSVG; | ||
| 98 | |||
| 99 | my $rsvg = new Image::LibRSVG(); | ||
| 100 | |||
| 101 | if( Image::LibRSVG->isFormatSupported( $format ) ) { | ||
| 102 | if( ! $rsvg->convert( $infile, $converted_file, 0, $format ) ) { | ||
| 103 | fail( "The was an error when transforming with Image::LibRSVG\n" ); | ||
| 104 | } | ||
| 105 | } else { | ||
| 106 | if( ! $rsvg->convert( $infile, "$TEMPDIR_/temp.png" ) ) { | ||
| 107 | fail( "The was an error when transforming with Image::LibRSVG\n" ); | ||
| 108 | } else { | ||
| 109 | # print STDERR "FORMAT $format NOT SUPPORTED BY Image::LibRSVG\n"; | ||
| 110 | &serializeWithImageMagick( "$TEMPDIR_/temp.png", $suffix ); | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | return $converted_file; | ||
| 115 | } | ||
| 116 | |||
| 117 | sub serializeWithImageMagick { | ||
| 118 | my $infile = shift; | ||
| 119 | my $converted_file = "$TEMPDIR_/temp." . shift; | ||
| 120 | my $local_dir; | ||
| 121 | |||
| 122 | require Image::Magick; | ||
| 123 | |||
| 124 | AxKit::Debug(8, "Serializer is ImageMagick"); | ||
| 125 | |||
| 126 | $infile =~ s%$TEMPDIR_/%%; | ||
| 127 | $local_dir = cwd; | ||
| 128 | |||
| 129 | chdir( $TEMPDIR_ ); | ||
| 130 | |||
| 131 | my $image = new Image::Magick(); | ||
| 132 | my $retval = $image->Read( $infile ); | ||
| 133 | |||
| 134 | if( "$retval" ) { | ||
| 135 | chdir( $local_dir ); | ||
| 136 | fail( "ImageMagick Read of file '$infile' failed. Reason: $retval" ); | ||
| 137 | } | ||
| 138 | |||
| 139 | $retval = $image->Write( $converted_file ); | ||
| 140 | |||
| 141 | if( "$retval" ) { | ||
| 142 | chdir( $local_dir ); | ||
| 143 | fail( "ImageMagick Write of file '$converted_file' failed. Reason: $retval" ); | ||
| 144 | } | ||
| 145 | |||
| 146 | chdir( $local_dir ); | ||
| 147 | |||
| 148 | return $converted_file; | ||
| 149 | } | ||
| 150 | |||
| 151 | |||
| 152 | sub createTempSVGFile { | ||
| 153 | my $r = shift; | ||
| 154 | my $xml_provider = shift; | ||
| 155 | my $temp_svg; | ||
| 156 | my $fh; | ||
| 157 | my $xmlstring; | ||
| 158 | |||
| 159 | $TEMPDIR_ = File::Temp::tempdir(); | ||
| 160 | |||
| 161 | AxKit::Debug(8, "Got tempdir: $TEMPDIR_"); | ||
| 162 | |||
| 163 | if ( ! $TEMPDIR_ ) { | ||
| 164 | die "Cannot create tempdir: $!"; | ||
| 165 | } else { | ||
| 166 | $temp_svg = "$TEMPDIR_/temp.svg"; | ||
| 167 | $fh = Apache->gensym(); | ||
| 168 | } | ||
| 169 | |||
| 170 | if( my $dom = $r->pnotes('dom_tree') ) | ||
| 171 | { | ||
| 172 | AxKit::Debug(8, "Got a dom tree"); | ||
| 173 | |||
| 174 | $dom->toFile("$TEMPDIR_/temp.svg"); | ||
| 175 | delete $r->pnotes()->{'dom_tree'}; | ||
| 176 | |||
| 177 | } elsif( $xmlstring = $r->pnotes('xml_string') ) { | ||
| 178 | AxKit::Debug(8, "Got a xml-string"); | ||
| 179 | |||
| 180 | open($fh, ">$TEMPDIR_/temp.svg") || fail( "Cannot write: $!" ); | ||
| 181 | print $fh $xmlstring; | ||
| 182 | close( $fh ) || fail( "Cannot close: $!" ); | ||
| 183 | |||
| 184 | } else { | ||
| 185 | $xmlstring = eval { ${$xml_provider->get_strref()} }; | ||
| 186 | |||
| 187 | if ( $@ ) { | ||
| 188 | AxKit::Debug(8, "No ref"); | ||
| 189 | $fh = $xml_provider->get_fh(); | ||
| 190 | |||
| 191 | File::Copy::copy($fh, "$TEMPDIR_/temp.svg"); | ||
| 192 | } else { | ||
| 193 | AxKit::Debug(8, "It has been a ref"); | ||
| 194 | |||
| 195 | open($fh, ">$TEMPDIR_/temp.svg") || fail( "Cannot write: $!" ); | ||
| 196 | print $fh $xmlstring; | ||
| 197 | close($fh) || fail("Cannot close: $!"); | ||
| 198 | |||
| 199 | } | ||
| 200 | } | ||
| 201 | |||
| 202 | return $temp_svg; | ||
| 203 | } | ||
| 204 | |||
| 205 | |||
| 206 | sub mimeTypeSuffixHandling { | ||
| 207 | my $r = shift; | ||
| 208 | my $mime; | ||
| 209 | my $suffix; | ||
| 210 | |||
| 211 | if( $r->pnotes( "axkit_mime_type" ) ) { | ||
| 212 | AxKit::Debug(8, "MimeType retrieved from Plugin"); | ||
| 213 | $mime = $r->pnotes( "axkit_mime_type" ); | ||
| 214 | } else { | ||
| 215 | AxKit::Debug(8, "MimeType retrieved from CONF or using Default"); | ||
| 216 | $mime = $r->dir_config( "SVGOutputMimeType" ) || $Config{SVGOutputMimeType}; | ||
| 217 | } | ||
| 218 | |||
| 219 | if( ! exists $MimeTypeSuffixFormatMappings{$mime} ) { | ||
| 220 | AxKit::Debug(8, "MimeType is not known. We are using DEFAULTS"); | ||
| 221 | $mime = $Config{SVGOutputMimeType}; | ||
| 222 | $suffix = "png"; | ||
| 223 | } else { | ||
| 224 | AxKit::Debug(8, "Setting suffix. To mapped value"); | ||
| 225 | $suffix = $MimeTypeSuffixFormatMappings{$mime}[0]; | ||
| 226 | } | ||
| 227 | |||
| 228 | return ( $mime, $suffix ); | ||
| 229 | } | ||
| 230 | |||
| 231 | sub cleanup { | ||
| 232 | File::Path::rmtree( $TEMPDIR_ ); | ||
| 233 | } | ||
| 234 | |||
| 235 | sub fail { | ||
| 236 | &cleanup(); | ||
| 237 | die @_; | ||
| 238 | } | ||
| 239 | |||
| 240 | 1; | ||
| 241 | |||
| 242 | |||
| 243 | __END__ |