为什么这个PostScriptPS文件创建的上边距比指定的多?

问题描述 投票:0回答:1

PS脚本接收一个纯文本文档并从中生成一个PDF。非常感谢 @RedGrittyBrick 挖掘出这个片段。

%!
%
% From: Jonathan Monsarrat ([email protected])
% 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

确实如此

/topmargin 1 inch def
/leftmargin 1 inch def

但它... 视觉上看 像上边距是像4英寸,而不是1英寸,因为它在文件中说。如果我把它修改为0,完成的PDF 视觉上 似乎有1英寸的上边距。另一方面,如果我把leftmargin修改为0英寸,它就会一直延伸到左边界。这对我来说毫无意义。

我使用SamutraPDF来打开PDF文件。

对我来说,视觉上看起来正确的方式是,在右上角和左下角都有适当的、均匀的边距。

/topmargin 0 inch def
/leftmargin 0.8 inch def

根据代码,这看起来应该是错误的,但它看起来却是正确的。

这让我非常担心。别人会不会看到我的文档的页边距乱七八糟?这是不是SumatraPDF做了什么非标准的事情?它是否有一个错误?到底发生了什么?PDF是否默认为所有文档添加了不可见的上边距?

我必须承认,我不明白PostScript文件中的任何语言。有一点,它提到了 "500",这似乎很奇怪的具体。但我的问题其实是关于 "看不见的上边距"。为什么会出现这种情况?我做错了什么?为什么根据给我的人说,这个脚本不能产生完美的边距?他声称已经在各种环境下使用了很长时间,所以我不知道该怎么解释。

pdf ghostscript postscript plaintext
1个回答
0
投票

这个1992年的postscript程序在我的Linux下使用ps2pdf和ghostscript 9.52时可以正常工作。页面高度是11",上边距是1",所以第一行确实从10 "高度开始。我已经用字母大小进行了测试,但即使在不同尺寸的纸张上,文本也应该从10 "的位置开始。也许SumatraPDF真的不同,或者试试这个。

后记从 "循环 "之后的第一行开始读取文本。该行的内容应该在10 "的高度。如果文本在第一行之后开始,或者有很多空行,那么在打印之前,pdf会有那么多空行。页边距与后记指定的一样,所以你的输入必须是空行被打印在自己的文本开始之前。确保循环和你要打印的文本之间没有空行。


0
投票

我读过这个程序,我认为可以安全地修改它来使用环境定义的页面参数。

你需要做的就是把硬编码的那行注释出来。

%/pageheight 11 inch def

然后用这行来代替,这行是从页面设备中获取高度的。

/pageheight currentpagedevice /PageSize get 1 get def

或者这个版本,它使用剪辑路径的边界框。

/pageheight newpath clippath pathbbox 4 1 roll pop pop pop def

请注意,该程序不进行任何形式的文字包装或文档布局,文本必须有明确的换行。

© www.soinside.com 2019 - 2024. All rights reserved.