doc: Update changes
[nasm.git] / doc / pspdf.pl
blobef9f2900ed20c4b6122fc16be7898e0c331a4748
1 #!/usr/bin/perl
3 # Wrapper around a variety of programs that can do PS -> PDF conversion
6 use strict;
7 use File::Spec;
9 my $compress = 1;
11 my $win32_ok = eval {
12 require Win32::TieRegistry;
13 Win32::TieRegistry->import();
17 while ($ARGV[0] =~ /^-(.*)$/) {
18 my $opt = $1;
19 shift @ARGV;
21 if ($opt eq '-nocompress') {
22 $compress = 0;
26 # Ghostscript executable name. "gs" on Unix-based systems.
27 my $gs = 'gs';
29 my ($in, $out) = @ARGV;
31 if (!defined($out)) {
32 die "Usage: $0 [-nocompress] infile ou{ tfile\n";
35 # If Win32, help GhostScript out with some defaults
36 sub win32_gs_help() {
37 return if (!$win32_ok);
39 use Sort::Versions;
40 use sort 'stable';
42 my $Reg = $::Registry->Open('', {Access => 'KEY_READ', Delimiter => '/'});
43 my $dir;
44 my @gs;
46 foreach my $k1 ('HKEY_CURRENT_USER/Software/',
47 'HKEY_LOCAL_MACHINE/SOFTWARE/') {
48 foreach my $k2 ('Artifex/', '') {
49 foreach my $k3 ('GPL Ghostscript/', 'AFPL Ghostscript/',
50 'Ghostscript/') {
51 my $r = $Reg->{$k1.$k2.$k3};
52 if (ref($r) eq 'Win32::TieRegistry') {
53 foreach my $k (keys(%$r)) {
54 my $rk = $r->{$k};
55 if (ref($rk) eq 'Win32::TieRegistry' &&
56 defined($rk->{'/GS_LIB'})) {
57 push @gs, $rk;
65 @gs = sort {
66 my($av) = $a->Path =~ m:^.*/([^/]+)/$:;
67 my($bv) = $b->Path =~ m:^.*/([^/]+)/$:;
68 versioncmp($av, $bv);
69 } @gs;
71 return unless (scalar(@gs));
73 $ENV{'PATH'} .= ';' . $gs[0]->{'/GS_LIB'};
74 $ENV{'GS_FONTPATH'} .= (defined($ENV{'GS_FONTPATH'}) ? ';' : '')
75 . $ENV{'windir'}.'\\fonts';
77 my $gsp = undef;
78 foreach my $p (split(/\;/, $gs[0]->{'/GS_LIB'})) {
79 foreach my $exe ('gswin64c.exe', 'gswin32c.exe', 'gs.exe') {
80 last if (defined($gsp));
81 my $e = File::Spec->catpath($p, $exe);
82 $gsp = $e if (-f $e && -x _);
86 $gs = $gsp if (defined($gsp));
89 # Remove output file
90 unlink($out);
92 # 1. Acrobat distiller
93 my $r = system('acrodist', '-n', '-q', '--nosecurity', '-o', $out, $in);
94 exit 0 if ( !$r && -f $out );
96 # 2. ps2pdf (from Ghostscript)
98 # GhostScript uses # rather than = to separate options and values on Windows,
99 # it seems. Call gs directly rather than ps2pdf, because -dSAFER
100 # breaks font discovery on some systems, apparently.
101 win32_gs_help();
102 my $o = $win32_ok ? '#' : '=';
103 my $r = system($gs, "-dCompatibilityLevel${o}1.4", "-q",
104 "-P-", "-dNOPAUSE", "-dBATCH", "-sDEVICE${o}pdfwrite",
105 "-sstdout${o}%stderr", "-sOutputFile${o}${out}",
106 "-dOptimize${o}true", "-dEmbedAllFonts${o}true",
107 "-dCompressPages${o}" . ($compress ? 'true' : 'false'),
108 "-dUseFlateCompression${o}true",
109 "-c", ".setpdfwrite", "-f", $in);
110 exit 0 if ( !$r && -f $out );
112 # 3. pstopdf (BSD/MacOS X utility)
113 my $r = system('pstopdf', $in, '-o', $out);
114 exit 0 if ( !$r && -f $out );
116 # Otherwise, fail
117 unlink($out);
118 exit 1;