查找字符串并使用 PostScript 转换为 ASCII

发布于 2024-11-01 17:10:45 字数 1364 浏览 0 评论 0原文

当谈到后记编程时,我是一个完全的新手,我试图在这里的天才的帮助下找到解决我的问题的方法。

我是一名学生,目前正在处理一个项目,我们应该从 postscript 文件中读取一些内容并将其放入新的 postscript 文件中。

我想在 Ghostscript 的帮助下编辑 postscript 文件,以使用我从原始 postscript 文件中挑选的某些值进行输出。

现在我有一个文件可以执行此操作,但我想获取嵌入在十六进制字符串中的字体并对其进行解码,以便从中获取 ASCII 符号。

原始文件的对象看起来像这样

/g5 <1C60D8A8C9B64EDFFB83C6241DB110BEE5AB2FAD9D94B39ED5E81E7411B 66E9361DDE78FC667AB91EF9824>

我想做的就是把它们挑出来,并根据后记代码(systemdict???)中的一些字典检查它们,然后选出它的值。

有没有人有类似的事情或例子来做到这一点?

现在,我挑选文件的代码开始如下: (%stdout) (w) file def

我在其中获取文件,现在我想搜索字符串“<1C60D8”并复制该字符串与符号“>”之间的所有内容

之后,我想检查十六进制代码隐藏在其下面的内容,在本例中是 Arial 中的“E”。

我想提取文本,这样我就可以将其放入一个新文件中,而无需使用十六进制字符串并采用纯 ASCII 格式。例如变成“P”。 之后我可以制作一个软件,从新的postscript文件中挑选出“P”并将其放在我的主页上,最终的结果是一个主页,上面有每个人上次考试的测试结果。

现在我正在使用 Ghostscript 和我在互联网上找到的以下命令行来提取文件中的信息。

gswin32c.exe -q -dNODISPLAY -dNOPAUSE -sFONTPATH=C:\WINDOWS\Fonts -dBATCH extract.ps input.ps > output.ps

其中 extract.ps 是我需要进行一些编码以检索十六进制字符串并对它进行编码并最后输出它的文件,以便它成为“正常”字体,并且还写入它们应该位于的位置的一些坐标。

如果您知道我应该如何开始,那就太棒了。现在我想这可能行不通,我必须像其他学生一样采取另一种方法,但自从我发现 PostScript 是一种编程语言后,我认为我可以做到。

现在我想我一开始可能需要做这样的事情。 (%标准输出)(w) 测试{ 杜普??? 写字符串 检查 systemdict 吗? 检查?? } 绑定定义 显示测试???

我目前正在尝试阅读一些在互联网上找到的文献,但这非常困难!

如果您知道如何解决问题,请举个例子,让我知道

最好的问候!

I am a total newbie when it comes to postscript programming and I was trying to find a solution to my problem with the help of the geniuses in here.

I am a student that is currently working with a project where we are supposed to read something from a postscript file and place it in a new postscript file.

I want to edit a postscript file with the help of ghostscript to make an output with certain values I have picked out of the original postscript file.

Right now I have a file that does this, but I want to get the fonts that are embedded in a hexadecimal string and decode it so I get the ASCII symbol from it.

The original file have objects that look something like this

/g5 <1C60D8A8C9B64EDFFB83C6241DB110BEE5AB2FAD9D94B39ED5E81E7411B
66E9361DDE78FC667AB91EF9824>

And what I want to do is to pick them out and check them against some dictionary in the postscript code (systemdict???) and pick out its value.

Does anyone have something like this or an example to do this?

Right now my code to pick out the file starts something like this:
(%stdout) (w) file def

where I get the file and now I want to search for let's say the string "<1C60D8" and copy everything that is between this and the symbol ">"

After that I want to check what the hexadecimal code hides underneath it, which in this case is a "E" in Arial.

I want to extract the text so I can have it in a new file without the hexadecimal strings and in pure ASCII format. So becomes a "P" for example.
After that I can make a software that picks out the "P" from the new postscript file and puts it on my homepage, and the final result is a homepage with everyones test results from the last exam for example.

Right now I am using ghostscript and the following command line which I found on the internet somewhere to extract the information in the file.

gswin32c.exe -q -dNODISPLAY -dNOPAUSE -sFONTPATH=C:\WINDOWS\Fonts -dBATCH extract.ps input.ps > output.ps

where extract.ps is the file in which I need to do some encoding to retrieve the hexadecimal string and encode it and lastly output it so it becomes an "normal" font, and also write in some coordinates of where they should lie.

If you have any knowlegde of how I should start that would be awesome. Right now I am thinking that this probably wont work and I'll have to do another approach like the other students, but since I found out that PostScript is a programming language I thought I could make it.

Right now I am thinking I probably need to do something like this in the beginning.
(%Stdout)(w)
test{

dup ????
writestring
check against systemdict?
gcheck??

} bind def
show test???

I am currently trying to read some literature that I found on the internet but it is very difficult!

If you know how to solve the problem, with an example, please let me know

Best regards!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北陌 2024-11-08 17:10:45

在我看来,你走在正确的道路上。 PostScript一种编程语言,因此出于一般目的,您必须使用 PostScript 解释器来处理它。简单地解析文件或任何其他类似的方法在一般情况下都不起作用(尽管它可能适用于简单的文件)。

您引用的原始对象似乎可能是字体中的字形描述,可能是 type 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

现在每当我们执行“show”时,我们都会检查当前字体是否已存储,如果没有则存储它。在页面末尾(执行 showpage 时)我们可以做其他事情,例如将存储的字体字典作为字体发出等。

您可能想要做的一件事是记录字符串到达​​“show”时的位置,currentpoint 运算符将为您提供 show 发生时的 xy 位置。您可以决定将字符串及其位置存储在数组中,而不是将它们写入文件。事实上,您可以构建一个包含有用信息的字典:

/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 以便您可以计算所需的字体大小等等。

You are on the right track, in my opinion. PostScript is a programming language, so for general purposes you must use a PostScript interpreter to process it. Simply parsing the file, or any other similar approach, won't work in the general case (though it might for a simple file).

The original object you have quoted appears likely to be a glyph description in a font, probably a type 3 font, probably produced by Fontographer, but that's just guessing. Note that's a glyph not a font, a font is a collection of glyphs.

If I were doing this I would start by redefining the various PostScript operators. For example, if you redefine 'show' you can pick up the text as it is drawn (actually there are several kinds of show operator, you need to redefine them all). At the same time you can pick the font dictionary up, and you can arrange to output it to the file.

For example you might start with:

%!PS
% redefine.ps
%

/OutputFile (/out.txt) (w) file def

/show {
  OutputFile exch writestring
} bind def

Then run this command:

gswin32 redefine.ps input.ps

It will run redefine.ps which redefines the operators, then it will interpret input.ps. The redefined 'show' operator will write the string arguments to any 'show' operations to a file called /out.txt.

Obviously you can extend this to the other show operators. You can also take copies of the font dictionaries and then emit them as required. There's a bit of programming involved but here's an outline:

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

Now whenever we execute a 'show', we will check to see if the current font is already stored, and store it if not. At the end of the page (when showpage is executed) we can do other things, like emitting the stored font dictionaries as fonts and so on.

One thing you will probably want to do is record the positions of the strings as they arrive at 'show', the currentpoint operator will give you the x.y position at the time show occurs. Instead of writing these to a file you may decide to store the string and its position in an array. In fact you might construct a dictionary with useful information:

/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

Now when you get to 'showpage', you have an array of fonts, and an array of string fragments with their properties. You can emit the fonts, then write out appropriate font selection criteria and strings to 'show' the strings in your output PostScript file.

There is much more you can do, you can capture the colour, you will need the CTM so you can calculate the font point size you need and so on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文