在我看来,你走在正确的轨道上。 PostScript 是一种编程语言,因此出于一般目的,您必须使用 PostScript 解释器来处理它。简单地解析文件或任何其他类似方法在一般情况下都不起作用(尽管它可能适用于简单文件)。
您引用的原始对象可能是字体中的字形描述,可能是类型 3 字体,可能由 Fontographer 制作,但这只是猜测。请注意,字形不是字体,字体是字形的集合。
如果我这样做,我会从重新定义各种 PostScript 运算符开始。例如,如果重新定义'show',则可以在绘制文本时将其拾取(实际上有几种显示运算符,您需要将它们全部重新定义)。同时可以把字体字典捡起来,安排输出到文件中。
例如,您可以从以下开始:
%!PS
% redefine.ps
%
/OutputFile (/out.txt) (w) file def
/show {
OutputFile exch writestring
} bind def
然后运行这个命令:
gswin32 redefine.ps input.ps
它将运行 redefine.ps 重新定义运算符,然后解释 input.ps。重新定义的“show”操作符会将任何“show”操作的字符串参数写入一个名为/out.txt的文件中。
显然,您可以将其扩展到其他节目运营商。您还可以复制字体字典,然后根据需要发出它们。涉及到一些编程,但这里有一个大纲:
%!PS
%
/OutputFile (/out.txt) (w) file def
%% FontStore will be an array of font dictionaries
/FontStore 1 array def
/CheckFont {
currentfont /FontName get %% Extract the name of the current font from the
%% font dictionary
true %% termination condition
FontStore { %% forall is called for each member of the array
/FontName get %% get font name from stored font dictionary
2 index %% copy the current font name from the stack
eq %% See if they are the same
{
pop %% remove the 'false' condition
false %% replace it with a 'true'
exit %% and exit the loop
} if
} forall
exch pop %% remove stored font name
{
%% make the array one bigger, copy the old array, add the current font dict.
} if
} def
/show {
CheckFont
OutputFile exch writestring
} bind def
/showpage {
%% Emit the fonts if required, potentially reorder the stored strings etc.
} bind def
现在,每当我们执行“显示”时,我们将检查当前字体是否已存储,如果没有则存储它。在页面的末尾(执行 showpage 时)我们可以做其他事情,比如将存储的字体字典作为字体发出等等。
您可能想要做的一件事是记录字符串到达“show”时的位置,currentpoint 运算符将在 show 发生时为您提供 x.y 位置。您可以决定将字符串及其位置存储在数组中,而不是将这些写入文件。事实上,您可能会构建一个包含有用信息的字典:
/show {
5 dict %% make a dictionary
begin %% start it (put it on the dict stack as the current dict)
/String exch def %% put the string operand in the dict.
currentpoint %% get the current location
/Y exch def %% store in the dict
/X exch def %%
currentfont %% get current font dict
/FontName get %% get FontName
/Font exch def %% store name in dict
currentfont %% copy current dict to operand stack
end %% close dictionary and remove from dict stack
%%
%% In here, add the newly created dictionary to an array of dictionaries
%%
} bind def
现在,当您进入“showpage”时,您会看到一组字体和一组带有属性的字符串片段。您可以发出字体,然后写出适当的字体选择标准和字符串以“显示”输出 PostScript 文件中的字符串。
您可以做的还有很多,您可以捕捉颜色,您将需要 CTM,以便计算您需要的字体点大小等等。