为什么这个PostScript/PS文件创建的上边距比指定的要多?



PS脚本获取纯文本文档并从中生成PDF。非常感谢@RedGrittyBrick挖掘此片段:

%!
%
% 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

它具有:

/topmargin 1 inch def
/leftmargin 1 inch def

但它在视觉上看起来像上边距是 4 英寸,而不是文件中所说的 1 英寸。如果我将其修改为 0,则完成的 PDF 在视觉上看起来具有 1 英寸的边距。另一方面,如果我将左边距修改为 0 英寸,它会一直到左边框。这对我来说毫无意义。

我使用SamutraPDF打开PDF文件。

对我来说,它的视觉外观方式是正确的,在上/右/下/左边有适当、均匀的边距,是:

/topmargin 0 inch def
/leftmargin 0.8 inch def

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

这让我非常担心。其他人会看到我的文档边距混乱吗?这是苏门答腊在做一些非标准的事情吗?它有错误吗?这是怎么回事?默认情况下,PDF 是否为所有文档添加了不可见的上边距?

我必须承认,我不懂那个PostScript文件中的任何语言。有一次,它提到了"500",这似乎很奇怪。但我的问题实际上是关于"看不见的顶部边缘"。为什么会这样?我做错了什么?为什么根据给我的人的说法,剧本不能产生完美的利润?他声称已经在各种环境中使用了很长时间,所以我不知道该怎么做。

这个 1992 年的 postscript 程序在我的 Linux 上使用带有 ghostscript 9.52 的 ps2pdf 时可以正常工作。页面高度为 11 英寸,上边距为 1 英寸,因此第一行从 10 英寸高开始。我已经用信纸尺寸对此进行了测试,除了即使使用不同的纸张尺寸,文本也应该从 10 英寸的位置开始。也许苏门答腊腊PDF真的不一样,或者试试这个:

后记开始阅读"循环"后第一行的文本。该行的内容应为 10 英寸高。如果文本在第一行之后开始,或者如果有许多空白行,则 pdf 在打印之前将有那么多空白行。边距与后记指定的页边距相同,因此您的输入必须是在您自己的文本开头之前打印的空行。确保循环和要打印的文本之间没有空白行。

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

您需要做的就是注释掉对页面大小进行硬编码的行:

%/pageheight 11 inch def

并将其替换为从页面设备获取高度的这一行,

/pageheight currentpagedevice /PageSize get 1 get def

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

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

请注意,此程序没有任何类型的自动换行或文档布局。 文本必须具有明确的换行符。

最新更新