fixmswrd.pl 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #!/usr/bin/perl
  2. # $Id: fixmswrd.pl,v 1.1 2000/03/09 08:40:39 lpd Exp $
  3. # $Id: fixmswrd.pl,v 1.1 2000/03/09 08:40:39 lpd Exp $
  4. # (C) 1997 Anthony Shipman
  5. #
  6. # This software is provided 'as-is', without any express or implied
  7. # warranty. In no event will the authors be held liable for any damages
  8. # arising from the use of this software.
  9. #
  10. # Permission is granted to anyone to use this software for any purpose,
  11. # including commercial applications, and to alter it and redistribute it
  12. # freely, subject to the following restrictions:
  13. #
  14. # 1. The origin of this software must not be misrepresented; you must not
  15. # claim that you wrote the original software. If you use this software
  16. # in a product, an acknowledgment in the product documentation would be
  17. # appreciated but is not required.
  18. # 2. Altered source versions must be plainly marked as such, and must not be
  19. # misrepresented as being the original software.
  20. # 3. This notice may not be removed or altered from any source distribution.
  21. #
  22. # Anthony Shipman shipmana@acm.org
  23. # This program patches the postscript generated by MS Word printer drivers
  24. # so that they work with ghostview 1.5. The problem is that the document
  25. # structuring conventions are not followed by Word. The pages are supposed
  26. # to be independent but they depend on a dictionary being opened outside
  27. # of the pages. The erroneous structure is
  28. #
  29. # %%EndSetup
  30. # NTPSOct95 begin
  31. # %%Page: 1 1
  32. # <text>
  33. # showpage
  34. # %%Page: 2 2
  35. # <text>
  36. # showpage
  37. # ......
  38. # %%Trailer
  39. # ...
  40. # end
  41. # %%EOF
  42. #
  43. # This only works if the all of the structure around the pages is preserved.
  44. # The opening of NTPSOct95 happens outside of any structured section so
  45. # it is never seen by ghostview. We change the structure to
  46. #
  47. # %%EndSetup
  48. # %%Page: 1 1
  49. # NTPSOct95 begin
  50. # <text>
  51. # showpage
  52. # end
  53. # %%Page: 2 2
  54. # NTPSOct95 begin
  55. # <text>
  56. # showpage
  57. # end
  58. # ......
  59. # %%Trailer
  60. # ...
  61. # %%EOF
  62. #
  63. # That is the dictionary opening is repeated inside each page.
  64. #
  65. # We add a comment to the document to mark that it has been converted.
  66. # This has the form
  67. # %LOCALGhostviewPatched
  68. #
  69. # Usage:
  70. # fixmswrd [-v] [file [output-file]]
  71. require 'getopts.pl';
  72. #=================================================================
  73. $program = "fixmswrd";
  74. sub usage {
  75. die "Usage: $program [-v] [file [output-file]]\n";
  76. }
  77. #=================================================================
  78. &Getopts("v") || &usage;
  79. $verbose = $opt_v;
  80. $infile = shift(@ARGV);
  81. if ($infile)
  82. {
  83. open(INFILE, $infile) || die "$program: Cannot read from $infile\n";
  84. $handle = "INFILE";
  85. }
  86. else
  87. {
  88. $handle = "STDIN";
  89. }
  90. $outfile = shift(@ARGV);
  91. if ($outfile)
  92. {
  93. open(OUTFILE, ">$outfile") || die "$program: Cannot write to $outfile\n";
  94. select(OUTFILE);
  95. }
  96. # This reads the header comments and detects the presence of the marker.
  97. $have_marker = 0;
  98. undef $dict_name;
  99. undef $dict_line;
  100. &read_comments;
  101. &put_comments;
  102. if ($have_marker)
  103. {
  104. $verbose && print STDERR "$program: Warning - already converted\n";
  105. while(<$handle>) # pass the file through unchanged.
  106. {
  107. print;
  108. }
  109. }
  110. else
  111. {
  112. $seen_trailer = 0;
  113. while(<$handle>) # massage the file
  114. {
  115. if ($dict_line)
  116. {
  117. next if (/$dict_line/o); # drop the old begin line
  118. $seen_trailer = 1 if (/^%%Trailer/);
  119. next if ($seen_trailer and /^end/); # drop the old end line
  120. }
  121. print;
  122. if (/^%%Page:/)
  123. {
  124. print "$dict_name begin\n"; # add at the start of the page
  125. }
  126. elsif (/^showpage/)
  127. {
  128. print "end\n"; # add at the end of the page
  129. }
  130. elsif (/^%%BeginResource: procset (\S+)/)
  131. {
  132. $dict_name = $1;
  133. $dict_line = "^$dict_name begin";
  134. }
  135. elsif (/^%%BeginProcSet: (\S+)/) # for older document versions
  136. {
  137. $dict_name = $1;
  138. $dict_line = "^$dict_name begin";
  139. }
  140. elsif (/^%%EndProlog:/)
  141. {
  142. unless ($dict_line)
  143. {
  144. $verbose &&
  145. print STDERR "$program: Warning - unrecognised document structure\n";
  146. }
  147. }
  148. }
  149. }
  150. exit 0;
  151. #=================================================================
  152. # This reads all of the header comments into an array which we can write
  153. # out again later. In addition we detect the presence of the marker comment.
  154. sub read_comments
  155. {
  156. @headers = ();
  157. while (<$handle>)
  158. { # without chopping
  159. push(@headers, $_);
  160. if (/^%LOCALGhostviewPatched/)
  161. {
  162. $have_marker = 1;
  163. }
  164. last if /^%%EndComments/;
  165. }
  166. }
  167. sub put_comments
  168. {
  169. foreach $h (@headers)
  170. {
  171. if (!$have_marker and ($h =~ /^%%EndComments/))
  172. {
  173. print "%LOCALGhostviewPatched\n";
  174. }
  175. print $h; # contains the newline
  176. }
  177. }