| 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 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | ### use warnings; | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 2 | our ($VERSION) = '$Revision: 1.11.2.5 $' =~ m{ \$Revision: \s+ (\S+) }x; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 0 | ▶ | sub fix_ps_title { | ||||||||||||||||||||||||||
|
Subroutine
fix_ps_title: not called CC=15 SCAR=54.8
Pod
documented
| |||||||||||||||||||||||||||||
| 18 | 0 | my ($sfile, $dir, $title, $log) = @_; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 0 | my $file = "$dir/$sfile"; | |||||||||||||||||||||||||||
| 21 | 0 | ▶ | if (!(-e $file && -r _)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-e $file and -r _
MC/DC0%0/2
-e $file and -r _
-e $file
-r _
Truth table0%0/3
A and B
A= -e $fileB= -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 | ▶ | if (!rename($file, "$file.xbak")) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | ▶ | if (!(open($CHANGED, '>', $file) && open($ORIG, '<', "$file.xbak"))) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
open($CHANGED, '>', $file) && open($ORIG, '<', "$file.xbak")
MC/DC0%0/2
open($CHANGED, '>', $file) && open($ORIG, '<', "$file.xbak")
open($CHANGED, '>', $file)
open($ORIG, '<', "$file.xbak")
Truth table0%0/3
A && B
A= open($CHANGED, '>', $file)B= 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 | ▶ | if (!rename "$file.xbak", $file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | # Only change first title line if any before line 6 | ||||||||||||||||||||||||||||
| 38 | 0 | ▶ | if (substr($_, 0, 7) eq '%%Title') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 0 | print {$CHANGED} "%%Title: $title\n"; | |||||||||||||||||||||||||||
| 40 | 0 | $log->verbose('%%Title: line found and changed.'); | |||||||||||||||||||||||||||
| 41 | 0 | last TITLE; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | 0 | print {$CHANGED} $_; | |||||||||||||||||||||||||||
| 44 | 0 | ▶ | last TITLE if $. > 5; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | ▶ | defined ($r = read $ORIG, $buf, $chunk) || | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | throw TeX::AutoTeX::FatalException('read after title change failed.'); | ||||||||||||||||||||||||||||
| 53 | 0 | ▶ | last if $r == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 0 | ▶ | print {$CHANGED} $buf | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | ▶ | if (-e "$file.xbak") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | unlink "$file.xbak" or $log->verbose("couldn't unlink '$file.xbak': $!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | 0 | return 0; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | sub stamp_postscript { | ||||||||||||||||||||||||||
|
Subroutine
stamp_postscript: not called CC=23 SCAR=63.1
Pod
documented
| |||||||||||||||||||||||||||||
| 70 | 0 | my ($sfile, $dir, $stampref, $log) = @_; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | my $file = "$dir/$sfile"; | |||||||||||||||||||||||||||
| 73 | 0 | ▶ | if (!(-e $file && -r _)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-e $file and -r _
MC/DC0%0/2
-e $file and -r _
-e $file
-r _
Truth table0%0/3
A and B
A= -e $fileB= -r _
| |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | $log->verbose("'$sfile' doesn't exist, or doesn't have adequate permissions, not stamping") if $log; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 0 | return; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | 0 | ▶ | $log->verbose("Backing up '$sfile'. Going to add a name/date stamp to it.") if $log; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | if (!rename $file, "$file.bak") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | ▶ | $log->verbose("failed to make backup of '$file', so we'll skip the stamping.") if $log; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | } else { | ||||||||||||||||||||||||||||
| 81 | 0 | my ($STAMPED, $ORIG); | |||||||||||||||||||||||||||
| 82 | 0 | ▶ | if (!(open($STAMPED, '>', $file) && open($ORIG, '<', "$file.bak"))) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
open($STAMPED, '>', $file) && open($ORIG, '<', "$file.bak")
MC/DC0%0/2
open($STAMPED, '>', $file) && open($ORIG, '<', "$file.bak")
open($STAMPED, '>', $file)
open($ORIG, '<', "$file.bak")
Truth table0%0/3
A && B
A= open($STAMPED, '>', $file)B= open($ORIG, '<', "$file.bak")
| |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | $log->verbose("failed to create new '$file' or read '$file.bak', so we'll revert to the old one.") if $log; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | if (!rename("$file.bak", $file) && $log) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not rename("$file.bak", $file) and $log
MC/DC0%0/2
not rename("$file.bak", $file) and $log
not rename("$file.bak", $file)
$log
Truth table0%0/3
not(A) and B
A= not rename("$file.bak", $file)B= $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>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 0 | print {$STAMPED} $_; | |||||||||||||||||||||||||||
| 90 | 0 | ▶ | last PAGE1 if /^%%Page:\s+-?\d+\s+1\s*$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | 0 | ▶ | STAMP: while (<$ORIG>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 0 | ▶ | if (substr($_, 0, 2) ne '%%') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | $log->verbose('OK, inserting the stamp') if $log; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | ▶ | if ($stampref->[1]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | print {$STAMPED} <<"EOSTAMP"; | |||||||||||||||||||||||||||
| 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 <</S/D/D[2 6]/W 1>> /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"; | |||||||||||||||||||||||||||
| 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} $_; | |||||||||||||||||||||||||||
| 124 | 0 | last STAMP; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | 0 | print {$STAMPED} $_; | |||||||||||||||||||||||||||
| 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 | ▶ | defined ($r = read $ORIG, $buf, $chunk) || | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | throw TeX::AutoTeX::FatalException('read after stamping failed.'); | ||||||||||||||||||||||||||||
| 135 | 0 | ▶ | last if $r == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 0 | ▶ | print {$STAMPED} $buf | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | or throw TeX::AutoTeX::FatalException('print after stamping failed'); | ||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | 0 | ▶ | $log->verbose('Stamping completed.') if $log; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | 0 | close $ORIG; | |||||||||||||||||||||||||||
| 142 | 0 | close $STAMPED; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | 0 | ▶ | unlink "$file.bak" if -e "$file.bak"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 0 | return 0; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 2 | 1; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | __END__ | ||||||||||||||||||||||||||||