blib/lib/PostScript/Convert.pm

filter: Statementstmt 22.1% 33 / 149 Branchbran 1.1% 1 / 90 Conditioncond 0.0% 0 / 29 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 43.7% 7 / 16 Podpod 11.1% 1 / 9 totaltotal 13.5% 42 / 311 CC 58 SCAR 46.7
CC
58
Cov
13%
check_options
272.0
convert_psfile
132.0
apply_format
90.0
CRAP 106.5
↑ Summary
1 #---------------------------------------------------------------------
2 package PostScript::Convert;
3 #
4 # Copyright 2012 Christopher J. Madsen
5 #
6 # Author: Christopher J. Madsen <perl@cjmweb.net>
7 # Created: November 9, 2009
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the same terms as Perl itself.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the
15 # GNU General Public License or the Artistic License for more details.
16 #
17 # ABSTRACT: Use Ghostscript to convert PostScript or PDF to other formats
18 #---------------------------------------------------------------------
19  
20 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 our $VERSION = '0.03'; ## no critic
22  
23 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use Carp qw(croak verbose);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use Scalar::Util qw(blessed openhandle reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29  
30 1 ▶use Exporter 5.57 'import'; # exported import method
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 1 our @EXPORT = qw(psconvert);
33  
34 #=====================================================================
35 # Package PostScript::Convert:
36  
37 1 our $Debug; # Set this to a true value for debugging output to STDERR
38  
39 1 ▶our %default = (
Branch: 1 branch
BranchTrueFalseTotal
$^O =~ /MSWin32/ ? : 0 1 1
40 ghostscript => ($^O =~ 'MSWin32' ? 'gswin32c.exe' : 'gs'),
41 );
42  
43 1 our %format = do {
44 1 my @png_param = (
45 extension => 'png',
46 format_param => [qw(-dTextAlphaBits=4 -dGraphicsAlphaBits=4)],
47 );
48  
49 1 my @pdf_param = (
50 device => 'pdfwrite',
51 extension => 'pdf',
52 format_code => [qw(-c .setpdfwrite)],
53 'format_param' # => VALUE
54 );
55  
56 (
57 1 png => { device => 'png16m', @png_param },
58 pnggray => { device => 'pnggray', @png_param },
59 pngmono => { device => 'pngmono', extension => 'png' },
60 jpeggray=> { device => 'jpeggray', extension => 'jpeg' },
61 jpeg => { device => 'jpeg', extension => 'jpeg' },
62 jpg => { device => 'jpeg', extension => 'jpg' },
63 pdf14 => { @pdf_param => ['-dCompatibilityLevel=1.4'] },
64 pdf13 => { @pdf_param => ['-dCompatibilityLevel=1.3'] },
65 pdf12 => { @pdf_param => ['-dCompatibilityLevel=1.2'] },
66 );
67 }; # end %format
68  
69 1 $format{pdf} = $format{pdf14};
70  
71 1 our %paper_size = (
72 executive => [522, 756],
73 folio => [595, 935],
74 'half-letter' => [612, 397],
75 letter => [612, 792],
76 legal => [612, 1008],
77 tabloid => [792, 1224],
78 superb => [843, 1227],
79 ledger => [1224, 792],
80  
81 'comm #10 envelope' => [297, 684],
82 'envelope-monarch' => [280, 542],
83 'envelope-c5' => [459.21260, 649.13386],
84 'envelope-dl' => [311.81102, 623.62205],
85  
86 a0 => [2383.93701, 3370.39370],
87 a1 => [1683.77953, 2383.93701],
88 a2 => [1190.55118, 1683.77953],
89 a3 => [ 841.88976, 1190.55118],
90 a4 => [ 595.27559, 841.88976],
91 a5 => [ 419.52756, 595.27559],
92 a6 => [ 297.63780, 419.52756],
93 a7 => [ 209.76378, 297.63780],
94 a8 => [ 147.40157, 209.76378],
95 a9 => [ 104.88189, 147.40157],
96 a10 => [ 73.70079, 104.88189],
97  
98 b0 => [2834.64567, 4008.18898],
99 b1 => [2004.09449, 2834.64567],
100 b2 => [1417.32283, 2004.09449],
101 b3 => [1000.62992, 1417.32283],
102 b4 => [ 708.66142, 1000.62992],
103 b5 => [ 498.89764, 708.66142],
104 b6 => [ 354.33071, 498.89764],
105 b7 => [ 249.44882, 354.33071],
106 b8 => [ 175.74803, 249.44882],
107 b9 => [ 124.72441, 175.74803],
108 b10 => [ 87.87402, 124.72441],
109 );
110  
111 1 $paper_size{"us-$_"} = $paper_size{$_} for qw(letter legal);
112 1 $paper_size{europostcard} = $paper_size{a6};
113  
114 #---------------------------------------------------------------------
115 sub psconvert
116 0 ▶{
Subroutine psconvert: not called CC=5 SCAR=34.0
Pod documented
117 0 my $ps = shift;
118  
119 0 ▶ unshift @_, 'filename' if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 0 0
120 0 my %opt = (%default, @_);
121  
122 0 ▶ return convert_fh( openhandle $ps, \%opt) if openhandle $ps;
Branch: 1 branch
BranchTrueFalseTotal
if &openhandle($ps) 0 0 0
123 0 ▶ return convert_object($ps, \%opt) if blessed $ps;
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($ps) 0 0 0
124 0 ▶ return convert_ref( $ps, \%opt) if ref $ps;
Branch: 1 branch
BranchTrueFalseTotal
if ref $ps 0 0 0
125 0 convert_filename( $ps, \%opt);
126 } # end psconvert
127  
128 #---------------------------------------------------------------------
129 sub convert_object
130 0 ▶{
Subroutine convert_object: not called CC=3 SCAR=24.8
Pod undocumented
131 0 my ($obj, $opt) = @_;
132  
133 0 ▶ return convert_psfile($obj, $opt) if $obj->isa('PostScript::File');
Branch: 1 branch
BranchTrueFalseTotal
if $obj->isa("PostScript::File") 0 0 0
134  
135 0 ▶ return convert_psfile($obj->get__PostScript_File, $opt)
Branch: 1 branch
BranchTrueFalseTotal
if $obj->can("get__PostScript_File") 0 0 0
136 if $obj->can('get__PostScript_File');
137  
138 0 croak "Don't know how to handle a " . blessed($obj);
139 } # end convert_object
140  
141  
142 #---------------------------------------------------------------------
143 sub convert_psfile
144 0 ▶{
Subroutine convert_psfile: not called CC=11 SCAR=48.8
Pod undocumented
145 0 my ($ps, $opt) = @_;
146  
147 # Check version of PostScript::File:
148 0 my $v = PostScript::File->VERSION;
149 0 ▶ croak "Must have PostScript::File 2.00 or later, this is only $v"
Branch: 1 branch
BranchTrueFalseTotal
unless $v >= 2 0 0 0
150 unless $v >= 2;
151  
152  
153 # Get paper size, if necessary:
154 0 ▶ $opt->{paper_size} ||= [ $ps->get_width, $ps->get_height ];
Condition0%0/2
$opt->{"paper_size"} ||= [$ps->get_width, $ps->get_height]
l!l
00
MC/DC0%0/1
$opt->{"paper_size"} ||= [$ps->get_width, $ps->get_height]
$opt->{"paper_size"}
Truth table0%0/2
A
A= $opt->{"paper_size"}
Aresult
11
00
155  
156 # Save old filename:
157 0 my $oldFN = $ps->get_filename;
158 0 ▶ $opt->{input} ||= "$oldFN.ps" if defined $oldFN;
Branch: 1 branch
BranchTrueFalseTotal
if defined $oldFN 0 0 0
Condition0%0/2
$opt->{"input"} ||= "$oldFN.ps"
l!l
00
MC/DC0%0/1
$opt->{"input"} ||= "$oldFN.ps"
$opt->{"input"}
Truth table0%0/2
A
A= $opt->{"input"}
Aresult
11
00
159  
160 0 require File::Temp; File::Temp->VERSION(0.19); # need newdir method
161  
162 0 ▶ if ($ps->get_eps and $ps->get_pagecount > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ps->get_eps and $ps->get_pagecount > 1) { } 0 0 0
Condition0%0/3
$ps->get_eps and $ps->get_pagecount > 1
!ll&&!rl&&r
000
MC/DC0%0/2
$ps->get_eps and $ps->get_pagecount > 1
$ps->get_eps $ps->get_pagecount > 1
Truth table0%0/3
A and B
A= $ps->get_epsB= $ps->get_pagecount > 1
ABresult
0X0
100
111
163 # Compute output filename:
164 0 apply_format($opt);
165 0 my ($outVol, $outDir, $outFN) =
166 File::Spec->splitpath( guess_output_filename($opt) );
167  
168 0 ▶ $outFN =~ s/(\.\w+)$// or croak "No extension in $outFN";
Branch: 1 branch
BranchTrueFalseTotal
$outFN =~ s/(\.\w+)$// or croak("No extension in $outFN") 0 0 0
169 0 my $ext = $1;
170  
171  
172 0 my $dir = File::Temp->newdir;
173  
174 0 my $oldExt = $ps->get_file_ext;
175 0 $ps->set_filename($outFN, $dir);
176 0 $ps->set_file_ext(undef);
177  
178 # Process the file(s):
179 0 my @files = $ps->output;
180  
181 0 foreach my $fn (@files) {
182 0 $outFN = (File::Spec->splitpath($fn))[2];
183 0 ▶ $outFN =~ s/\.\w+$/$ext/ or die "Expected extension in $outFN";
Branch: 1 branch
BranchTrueFalseTotal
$outFN =~ s/\.\w+$/$ext/ or die "Expected extension in $outFN" 0 0 0
184  
185 0 $opt->{filename} = File::Spec->catpath( $outVol, $outDir, $outFN );
186  
187 0 convert_filename($fn, $opt);
188 } # end foreach $fn in @files
189  
190 # Restore settings:
191 0 $ps->set_filename($oldFN);
192 0 $ps->set_file_ext($oldExt);
193 } # end if EPS with multiple pages
194 else {
195 # Only one file, we don't need a temporary directory:
196 0 my $fh = File::Temp->new;
197  
198 0 $ps->output($fh);
199  
200 0 ▶ seek($fh, 0,0) or croak "Can't seek temporary file: $!";
Branch: 1 branch
BranchTrueFalseTotal
seek $fh, 0, 0 or croak("Can't seek temporary file: $!") 0 0 0
201  
202 0 convert_fh($fh, $opt);
203 } # end else only one PostScript file to process
204 } # end convert_psfile
205  
206 #---------------------------------------------------------------------
207 sub convert_ref
208 0 ▶{
Subroutine convert_ref: not called CC=5 SCAR=34.0
Pod undocumented
209 0 my ($ref, $opt) = @_;
210  
211 0 my $type = reftype $ref;
212  
213 0 ▶ croak "Don't know how to handle a $type ref"
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "SCALAR" or $type eq "ARRAY" 0 0 0
Condition0%0/3
$type eq "SCALAR" or $type eq "ARRAY"
l!l&&r!l&&!r
000
MC/DC0%0/2
$type eq "SCALAR" or $type eq "ARRAY"
$type eq "SCALAR" $type eq "ARRAY"
Truth table0%0/3
A or B
A= $type eq "SCALAR"B= $type eq "ARRAY"
ABresult
1X1
011
000
214 unless $type eq 'SCALAR' or $type eq 'ARRAY';
215  
216  
217 0 require File::Temp;
218  
219 0 my $fh = File::Temp->new;
220  
221 0 ▶ if ($type eq 'ARRAY') { print $fh @$ref }
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'ARRAY') { } 0 0 0
222 0 else { print $fh $$ref }
223  
224 0 ▶ seek($fh, 0,0) or croak "Can't seek temporary file: $!";
Branch: 1 branch
BranchTrueFalseTotal
seek $fh, 0, 0 or croak("Can't seek temporary file: $!") 0 0 0
225  
226 0 convert_fh($fh, $opt);
227 } # end convert_ref
228  
229 #---------------------------------------------------------------------
230 sub convert_filename
231 0 ▶{
Subroutine convert_filename: not called CC=3 SCAR=24.8
Pod undocumented
232 0 my ($filename, $opt) = @_;
233  
234 0 ▶ $opt->{input} ||= $filename;
Condition0%0/3
$opt->{"input"} ||= $filename
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt->{"input"} ||= $filename
$opt->{"input"} $filename
Truth table0%0/3
A ||= B
A= $opt->{"input"}B= $filename
ABresult
1X1
011
000
235 0 ▶ open(my $in, '<:raw', $filename) or croak "Unable to open $filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $in, "<:raw", $filename or croak("Unable to open ${filename}: $!") 0 0 0
236  
237  
238 0 convert_fh($in, $opt);
239 } # end convert_filename
240  
241 #---------------------------------------------------------------------
242 sub check_options
243 0 ▶{
Subroutine check_options: not called CC=16 SCAR=56.1
Pod undocumented
244 0 my ($opt) = @_;
245  
246 0 ▶ my @cmd = ($opt->{ghostscript} || croak "ghostscript not defined");
Condition0%0/2
$opt->{'ghostscript'} || croak('ghostscript not defined')
l!l
00
MC/DC0%0/1
$opt->{'ghostscript'} || croak('ghostscript not defined')
$opt->{'ghostscript'}
Truth table0%0/2
A
A= $opt->{'ghostscript'}
Aresult
11
00
247  
248  
249 0 ▶ foreach my $dir (@{ $opt->{include} || [] }) {
Condition0%0/2
$opt->{'include'} || []
l!l
00
MC/DC0%0/1
$opt->{'include'} || []
$opt->{'include'}
Truth table0%0/2
A
A= $opt->{'include'}
Aresult
11
00
250 0 push @cmd, "-I$dir";
251 } # end foreach $dir
252  
253 0 push @cmd, qw(-q -sstdout=%stderr -dBATCH -dNOPAUSE);
254 0 ▶ push @cmd, ($opt->{unsafe} ? '-dNOSAFER' : '-dSAFER');
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'unsafe'} ? : 0 0 0
255  
256 0 apply_format($opt);
257  
258 0 push @cmd, "-sOutputFile=" . guess_output_filename($opt);
259  
260 0 my $device = $opt->{device};
261 0 ▶ croak "No output device supplied" unless defined $device and length $device;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $device and length $device 0 0 0
Condition0%0/3
defined $device and length $device
!ll&&!rl&&r
000
MC/DC0%0/2
defined $device and length $device
defined $device length $device
Truth table0%0/3
A and B
A= defined $deviceB= length $device
ABresult
0X0
100
111
262 0 push @cmd, "-sDEVICE=$device";
263  
264 0 ▶ if (defined(my $size = $opt->{paper_size})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $size = $opt->{"paper_size"})) 0 0 0
265 0 ▶ unless (ref $size) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $size) 0 0 0
266 0 ▶ if ($paper_size{lc $size}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($PostScript::Convert::paper_size{lc $size}) { } 0 0 0
elsif ($size =~ /\A(\d+(?:\.\d+)?)x(\d+(?:\.\d+)?)\Z/i) { } 0 0 0
267 0 $size = $paper_size{lc $size};
268 } elsif ($size =~ /\A(\d+(?:\.\d+)?)x(\d+(?:\.\d+)?)\Z/i) {
269 0 $size = [ $1 * 72, $2 * 72 ];
270 } else {
271 0 croak "Unknown paper size '$size'";
272  
273  
274 }
275 } # end unless ref $size
276 0 push @cmd, '-dDEVICEWIDTHPOINTS=' . $size->[0],
277 '-dDEVICEHEIGHTPOINTS=' . $size->[1];
278 } # end if $opt->{paper_size}
279  
280 0 ▶ push @cmd, "-r$opt->{resolution}" if $opt->{resolution};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"resolution"} 0 0 0
281 0 ▶ push @cmd, @{ $opt->{format_param} } if $opt->{format_param};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"format_param"} 0 0 0
282 0 ▶ push @cmd, @{ $opt->{gs_param} } if $opt->{gs_param};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"gs_param"} 0 0 0
283 0 ▶ push @cmd, @{ $opt->{format_code} } if $opt->{format_code};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"format_code"} 0 0 0
284  
285 0 ▶ print STDERR "@cmd\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $PostScript::Convert::Debug 0 0 0
286  
287 0 @cmd;
288 } # end check_options
289  
290 #---------------------------------------------------------------------
291 sub apply_format
292 0 ▶{
Subroutine apply_format: not called CC=9 SCAR=45.0
Pod undocumented
293 0 my ($opt) = @_;
294  
295 0 ▶ unless ($opt->{format}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt->{"format"}) 0 0 0
296 0 my $outFN = $opt->{filename};
297  
298 0 ▶ croak "No output format or filename supplied"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $outFN and length $outFN 0 0 0
Condition0%0/3
defined $outFN and length $outFN
!ll&&!rl&&r
000
MC/DC0%0/2
defined $outFN and length $outFN
defined $outFN length $outFN
Truth table0%0/3
A and B
A= defined $outFNB= length $outFN
ABresult
0X0
100
111
299 unless defined $outFN and length $outFN;
300  
301 0 ▶ $outFN =~ /\.([^.\s]+)$/ or croak "Unable to determine format from $outFN";
Branch: 1 branch
BranchTrueFalseTotal
$outFN =~ /\.([^.\s]+)$/ or croak("Unable to determine format from $outFN") 0 0 0
302 0 ▶ $format{ $opt->{format} = lc $1 } or croak "Unknown extension .$1";
Branch: 1 branch
BranchTrueFalseTotal
$PostScript::Convert::format{$opt->{"format"} = lc $1} or croak("Unknown extension .$1") 0 0 0
303 }
304  
305 0 ▶ my $fmt = $format{ $opt->{format} } or croak "Unknown format $opt->{format}";
Branch: 1 branch
BranchTrueFalseTotal
my $fmt = $PostScript::Convert::format{$opt->{"format"}} or croak(qq[Unknown format $opt->{"format"}]) 0 0 0
306  
307  
308 0 ▶ while (my ($key, $val) = each %$fmt) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$fmt) 0 0 0
309 0 ▶ $opt->{$key} = $val unless defined $opt->{key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt->{"key"} 0 0 0
310 }
311 } # end apply_format
312  
313 #---------------------------------------------------------------------
314 sub guess_output_filename
315 0 ▶{
Subroutine guess_output_filename: not called CC=7 SCAR=40.3
Pod undocumented
316 0 my ($opt) = @_;
317  
318 0 my $fn = $opt->{filename};
319  
320 CHOICE: {
321 0 ▶ last CHOICE if defined $fn;
Branch: 1 branch
BranchTrueFalseTotal
if defined $fn 0 0 0
322  
323 0 $fn = $opt->{input};
324 0 ▶ last CHOICE unless defined $fn and length $fn;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fn and length $fn 0 0 0
Condition0%0/3
defined $fn and length $fn
!ll&&!rl&&r
000
MC/DC0%0/2
defined $fn and length $fn
defined $fn length $fn
Truth table0%0/3
A and B
A= defined $fnB= length $fn
ABresult
0X0
100
111
325  
326 0 my $ext = $opt->{extension};
327 0 ▶ croak "No extension defined for format $opt->{format}" unless $ext;
Branch: 1 branch
BranchTrueFalseTotal
unless $ext 0 0 0
328  
329 0 $fn =~ s/(?:\.\w*)?$/.$ext/;
330 }
331  
332  
333 0 ▶ croak "No output filename supplied" unless defined $fn and length $fn;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fn and length $fn 0 0 0
Condition0%0/3
defined $fn and length $fn
!ll&&!rl&&r
000
MC/DC0%0/2
defined $fn and length $fn
defined $fn length $fn
Truth table0%0/3
A and B
A= defined $fnB= length $fn
ABresult
0X0
100
111
334  
335 0 $fn;
336 } # end guess_output_filename
337  
338 #---------------------------------------------------------------------
339 sub convert_fh
340 0 ▶{
Subroutine convert_fh: not called CC=7 SCAR=40.3
Pod undocumented
341 0 my ($fh, $opt) = @_;
342  
343 0 my @cmd = (check_options($opt), '-_');
344  
345 0 ▶ open(my $oldin, '<&STDIN') or croak "Can't dup STDIN: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $oldin, "<&STDIN" or croak("Can't dup STDIN: $!") 0 0 0
346 0 ▶ open(STDIN, '<&', $fh) or croak "Can't redirect STDIN: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "<&", $fh or croak("Can't redirect STDIN: $!") 0 0 0
347 0 system @cmd;
348 0 ▶ open(STDIN, '<&', $oldin) or croak "Can't restore STDIN: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "<&", $oldin or croak("Can't restore STDIN: $!") 0 0 0
349  
350  
351 0 ▶ if ($?) {
Branch: 1 branch
BranchTrueFalseTotal
if ($?) 0 0 0
352 0 my $exit = $? >> 8;
353 0 my $signal = $? & 127;
354 0 my $core = $? & 128;
355  
356 0 my $err = "Ghostscript failed: exit status $exit";
357 0 ▶ $err .= " (signal $signal)" if $signal;
Branch: 1 branch
BranchTrueFalseTotal
if $signal 0 0 0
358 0 ▶ $err .= " (core dumped)" if $core;
Branch: 1 branch
BranchTrueFalseTotal
if $core 0 0 0
359  
360 0 croak $err;
361 } # end if ghostscript failed
362 } # end convert_fh
363  
364 #=====================================================================
365 # Package Return Value:
366  
367 1 1;
368  
369 __END__
↑ Summary