BR 3392687: clang miscompiles offsetin() for uninitialized pointer
[nasm.git] / doc / pspdf.pl
blob91986413bfef75cc7f86df30e7f15d49c65e4f7b
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, $fontpath) = @ARGV;
31 if (!defined($out)) {
32 die "Usage: $0 [-nocompress] infile outfile [fontpath]\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. Similary it uses ; in path lists rather than :.
100 # Call gs directly rather than ps2pdf, because -dSAFER
101 # breaks font discovery on some systems, apparently.
102 win32_gs_help();
103 my $o = $win32_ok ? '#' : '=';
104 my $p = $win32_ok ? ';' : ':';
105 my $fpopt;
106 if (defined($fontpath)) {
107 my @fplist = ();
108 open(my $fp, '<', $fontpath) or die "$0: $fontpath: $!\n";
109 while (my $fpe = <$fp>) {
110 chomp $fpe;
111 push(@fplist, $fpe);
113 close($fp);
114 $fpopt = "-sFONTPATH${o}" . join($p, @fplist);
117 my $r = system($gs, "-dCompatibilityLevel${o}1.3",
118 "-I".File::Spec->curdir(),
119 "-P-", "-dNOPAUSE", "-dBATCH", "-sDEVICE${o}pdfwrite",
120 "-sstdout${o}%stderr", "-sOutputFile${o}${out}",
121 "-dOptimize${o}true",
122 "-dEmbedAllFonts${o}true", "-dSubsetFonts${o}true",
123 "-dMaxSubsetPct${o}100",
124 $fpopt,
125 "-dCompressPages${o}" . ($compress ? 'true' : 'false'),
126 "-dUseFlateCompression${o}true",
127 "-c", ".setpdfwrite", "-f", $in);
128 exit 0 if ( !$r && -f $out );
130 # 3. pstopdf (BSD/MacOS X utility)
131 my $r = system('pstopdf', $in, '-o', $out);
132 exit 0 if ( !$r && -f $out );
134 # Otherwise, fail
135 unlink($out);
136 exit 1;