Why does this PostScript/PS file create way more top-margin than specified?

你离开我真会死。 提交于 2020-03-05 04:04:49

问题


The PS script takes a plaintext document and produces a PDF from it. A big thank you to @RedGrittyBrick for digging up this snippet:

%!
%
% From: Jonathan Monsarrat (jgm@cs.brown.edu)
% Subject: PostScript -> ASCII *and* ASCII -> PostScript programs
% Newsgroups: comp.lang.postscript
% Date: 1992-10-01 04:45:38 PST 
%
% "If anyone is interested, here is an interesting program written by
% Professor John Hughes here at Brown University that formats ASCII
% in PostScript without a machine generator of any kind."
%
%%%
%%% Plan:
%%% Start with an empty string.
%%% For each character in the input stream, 
%%%    check to see if it's a carriage return.
%%%    if so, show the current string and reset it to empty
%%%    if not, add it to the current string.

/Courier findfont 10 scalefont setfont  %% Choose a fixed width font
/lineheight 
currentfont /FontBBox get dup      %% bbox bbox
0 2 getinterval    %% bbox {xm ym}
exch     %% {xm ym} bbox
2 2 getinterval    %% {xm ym} {xM yM}
aload pop    %% {xm ym} xM yM
3 2 roll     %% xM yM {xm ym}
aload pop
currentfont /FontMatrix get  %% xM yM xm ym MAT
transform    %% xM yM xm' ym'
4 2 roll
currentfont /FontMatrix get  %% xm' ym' xM yM MAT
transform    %% xm' ym' xM' yM'
exch pop     %% xm' ym' yM'
sub     %% xm' ym'-yM'
exch pop    %% dy
neg def 

lineheight pstack pop

/str 500 string def   %% Room to store a long string...
/empty 500 string def   %% An empty string to work with
/stringindex 0 def   %% How far we've filled the string
/inch {72 mul } def   %% A useful tool...
/pageheight 11 inch def
/topmargin 1 inch def
/botmargin 1 inch def
/leftmargin 1 inch def
/linesperpage pageheight topmargin sub botmargin sub lineheight div cvi def
/linenumber 1 def   %% the line we're about to write on

/newline {   %% move to a new line; flush page if necessary
   linenumber linesperpage gt {/linenumber 1 def showpage } if
   leftmargin pageheight topmargin sub linenumber lineheight mul sub moveto
   /linenumber linenumber 1 add def
} def

/cleanup {  %% print out the last bit of whatever you had there...
   str show showpage
} def

/startstring {  %% empty the string and reset its counter.
   str 0 empty putinterval
   /stringindex 0 def
} def

/showstring {  %% print the string on a new line and flush it
   newline
   str show 
   startstring
} def

pstack 

/addtostring {  %% put another character in the string, if there's room
   dup 500 gt {pop}{str exch stringindex exch put
   /stringindex stringindex 1 add def} ifelse
} def

%
% Main program: get characters and deal with them
%
{
   currentfile read {}{cleanup exit} ifelse
   dup 10 eq                   %% if it's a carriage return...
      {pop showstring}         %% write out this line of text and start over
      {dup 0 eq         %% if it's an end-of-file mark...
       {exit}                %% stop!
       {addtostring}           %% otherwise, add the character to current string
       ifelse}
      ifelse                   %% Sample data follows.
} loop

It has:

/topmargin 1 inch def
/leftmargin 1 inch def

But it visually looks like the top margin is like 4 inches and not 1 inch as it says in the file. If I modify it to 0, the finished PDF visually appears to have 1 inch top margin. If I, on the other hand, modify the leftmargin to 0 inch, it goes all the way to the left border. This makes no sense to me.

I use SamutraPDF to open PDF files.

The way it visually looks right to me, with proper, even margins on top/right/bottom/left, is:

/topmargin 0 inch def
/leftmargin 0.8 inch def

This, according to the code, should look all wrong, but instead it looks correct.

This makes me very worried. Will everyone else see my documents with messed up margins? Is this somehow SumatraPDF doing something non-standard? Does it have a bug? What is going on? Does PDF have invisible top margin added for all documents by default?

I must admit that I don't understand anything of the language in that PostScript file. At one point, it mentions "500", which seems oddly specific. But my question really is about the "invisible top margin". Why does it happen? What am I doing wrong? Why doesn't the script which, according to the guy who gave it to me, produce perfect margins? He claims to have used it for a very long time in all kinds of environments, so I don't know what to make of this.

来源:https://stackoverflow.com/questions/59950318/why-does-this-postscript-ps-file-create-way-more-top-margin-than-specified

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!