File Coverage

blib/lib/TeX/AutoTeX/PostScript.pm
Criterion Covered Total %
statement 3 81 3.7
branch 0 56 0.0
condition 0 15 0.0
subroutine 1 3 33.3
pod 2 2 100.0
total 6 157 3.8


line stmt bran cond sub pod time code
1             package TeX::AutoTeX::PostScript;
2              
3             #
4             # $Id: PostScript.pm,v 1.11.2.5 2011/01/27 18:42:28 thorstens Exp $
5             # $Revision: 1.11.2.5 $
6             # $Source: /cvsroot/arxivlib/arXivLib/lib/TeX/AutoTeX/PostScript.pm,v $
7             #
8             # $Date: 2011/01/27 18:42:28 $
9             # $Author: thorstens $
10             #
11              
12 2     2   8 use strict;
  2         4  
  2         2043  
13             ### use warnings;
14              
15             our ($VERSION) = '$Revision: 1.11.2.5 $' =~ m{ \$Revision: \s+ (\S+) }x;
16              
17             sub fix_ps_title {
18 0     0 1   my ($sfile, $dir, $title, $log) = @_;
19              
20 0           my $file = "$dir/$sfile";
21 0 0 0       if (!(-e $file && -r _)) {
22 0           $log->verbose("'$sfile' does not exist or doesn't have adequate permissions, not setting the %%Title");
23 0           return;
24             }
25 0           $log->verbose("Backing up '$sfile'. Going to change %%Title line.");
26 0 0         if (!rename($file, "$file.xbak")) {
27 0           $log->verbose("failed to make backup of '$file', so we'll skip the title change.");
28             } else {
29 0           my ($CHANGED, $ORIG);
30 0 0 0       if (!(open($CHANGED, '>', $file) && open($ORIG, '<', "$file.xbak"))) {
31 0           $log->verbose("failed to create new '$file' or read '$file.xbak', so we'll revert to the old one.");
32 0 0         if (!rename "$file.xbak", $file) {
33 0           throw TeX::AutoTeX::FatalException("woe is me, now that failed. We're doomed.\nGiving up!");
34             }
35             } else {
36 0           TITLE: while (<$ORIG>) {
37             # Only change first title line if any before line 6
38 0 0         if (substr($_, 0, 7) eq '%%Title') {
39 0           print {$CHANGED} "%%Title: $title\n";
  0            
40 0           $log->verbose('%%Title: line found and changed.');
41 0           last TITLE;
42             }
43 0           print {$CHANGED} $_;
  0            
44 0 0         last TITLE if $. > 5;
45             }
46             # TS: ps files can be huge, don't go line by line.
47             # consider sysread/syswrite a la File::Copy
48 0           my $chunk = 2097152; # 2MB = 1024 * 1024 * 2;
49 0           my ($r, $buf);
50 0           while (1) {
51 0 0         defined ($r = read $ORIG, $buf, $chunk) ||
52             throw TeX::AutoTeX::FatalException('read after title change failed.');
53 0 0         last if $r == 0;
54 0 0         print {$CHANGED} $buf
  0            
55             or throw TeX::AutoTeX::FatalException('print after title change failed');
56             }
57 0           close $ORIG;
58 0           close $CHANGED;
59              
60 0           $log->verbose('Title change completed.');
61 0 0         if (-e "$file.xbak") {
62 0 0         unlink "$file.xbak" or $log->verbose("couldn't unlink '$file.xbak': $!");
63             }
64             }
65             }
66 0           return 0;
67             }
68              
69             sub stamp_postscript {
70 0     0 1   my ($sfile, $dir, $stampref, $log) = @_;
71              
72 0           my $file = "$dir/$sfile";
73 0 0 0       if (!(-e $file && -r _)) {
74 0 0         $log->verbose("'$sfile' doesn't exist, or doesn't have adequate permissions, not stamping") if $log;
75 0           return;
76             }
77 0 0         $log->verbose("Backing up '$sfile'. Going to add a name/date stamp to it.") if $log;
78 0 0         if (!rename $file, "$file.bak") {
79 0 0         $log->verbose("failed to make backup of '$file', so we'll skip the stamping.") if $log;
80             } else {
81 0           my ($STAMPED, $ORIG);
82 0 0 0       if (!(open($STAMPED, '>', $file) && open($ORIG, '<', "$file.bak"))) {
83 0 0         $log->verbose("failed to create new '$file' or read '$file.bak', so we'll revert to the old one.") if $log;
84 0 0 0       if (!rename("$file.bak", $file) && $log) {
85 0           throw TeX::AutoTeX::FatalException("woe is me, now that failed. We're doomed.\nGiving up!");
86             }
87             } else {
88 0           PAGE1: while (<$ORIG>) {
89 0           print {$STAMPED} $_;
  0            
90 0 0         last PAGE1 if /^%%Page:\s+-?\d+\s+1\s*$/;
91             }
92 0           STAMP: while (<$ORIG>) {
93 0 0         if (substr($_, 0, 2) ne '%%') {
94 0 0         $log->verbose('OK, inserting the stamp') if $log;
95             # we had a request for extra space in front of the v
96             # on postscript files (only)
97 0           my $xmoveto = int(6*72 - length($stampref->[0])*9/2);
98             #6in - halflength, ymoveto=39=.54in
99              
100 0 0         if ($stampref->[1]) {
101 0           print {$STAMPED} <<"EOSTAMP";
  0            
102             gsave %matrix defaultmatrix setmatrix
103             90 rotate /stampsize 20 def /Times-Roman findfont stampsize scalefont setfont
104             currentfont /FontBBox get aload pop /pdf\@top exch 1000 div stampsize mul def
105             pop /pdf\@bottom exch 1000 div stampsize mul def pop
106             $xmoveto -32 moveto
107             currentpoint /pdf\@lly exch pdf\@bottom add def /pdf\@llx exch 2 sub def
108             0.5 setgray ($stampref->[0]) show
109             currentpoint /pdf\@ury exch pdf\@top add def /pdf\@urx exch 2 add def
110             /pdfmark where{pop}{userdict /pdfmark /cleartomark load put}ifelse
111             [ /H /I /Border [0 0 1] /BS <> /Color [0 1 1]
112             /Action << /Subtype /URI /URI ($stampref->[1])>>
113             /Subtype /Link /Rect[pdf\@llx pdf\@lly pdf\@urx pdf\@ury] /ANN pdfmark
114             grestore
115             EOSTAMP
116             } else {
117 0           print {$STAMPED} <<"EOPS";
  0            
118             gsave %matrix defaultmatrix setmatrix
119             90 rotate $xmoveto -39 moveto /Times-Roman findfont 20 scalefont setfont
120             0.3 setgray ($stampref->[0]) show grestore
121             EOPS
122             }
123 0           print {$STAMPED} $_;
  0            
124 0           last STAMP;
125             }
126 0           print {$STAMPED} $_;
  0            
127             }
128             # TS: ps files can be huge, don't go line by line.
129             # consider sysread/syswrite a la File::Copy
130 0           my $chunk = 2097152; # 2MB = 1024 * 1024 * 2;
131 0           my ($r, $buf);
132 0           while (1) {
133 0 0         defined ($r = read $ORIG, $buf, $chunk) ||
134             throw TeX::AutoTeX::FatalException('read after stamping failed.');
135 0 0         last if $r == 0;
136 0 0         print {$STAMPED} $buf
  0            
137             or throw TeX::AutoTeX::FatalException('print after stamping failed');
138             }
139 0 0         $log->verbose('Stamping completed.') if $log;
140             }
141 0           close $ORIG;
142 0           close $STAMPED;
143             }
144 0 0         unlink "$file.bak" if -e "$file.bak";
145              
146 0           return 0;
147             }
148              
149             1;
150              
151             __END__