如何确定PostScript中的字符串高度? [英] How to determine string height in PostScript?

查看:109
本文介绍了如何确定PostScript中的字符串高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要确定后记中字符串的高度(以给定的比例和字体)。

I need to determine the height of a string (in given scale and font) in postscript.

/Helvetic-Oblique findfont
10 scalefont
setfont
10 10 1 0 360 arc fill
10 10 moveto (test) dup stringwidth pop 2 div neg 0 rmoveto show

将打印测试水平居中(但尚未垂直)在(10,10)。 (为此,我还在10,10处显示了一个小圆圈)。我还需要确定字符串高度,以使文本垂直居中对齐,但是我找不到它的函数。

will print test centered horizontally (but not yet vertically) at (10,10). (to see this, I also show a small circle at 10,10). I also need to determine the string height to center the text vertically as well, but I cant find a function for it.

推荐答案

您是否熟悉所使用的PostScript代码?还是只是从某个地方盲目复制并粘贴?如果您想了解它,则应该在Google上搜索 PostScript语言参考或红皮书或 PLRM。这些资源可从Adobe以PDF格式获得。

Are you familiar with the PostScript code you're using? Or is it just blindly copied and pasted from someplace? If you want to understand it, you should google for "PostScript Language Reference" or "Red Book" or "PLRM". These resources are available as PDFs from Adobe.

您的PostScript代码段使用以下步骤:

Your PostScript snippet uses the following steps:


  1. (测试)将字符串 test放置在堆栈顶部。

  2. dup 复制堆栈中最上面的项目。 (您现在将在堆栈上将字符串两次存储。)

  3. stringwidth 。执行完此运算符后,将使用最上面的测试字符串,并将两个值改为添加到堆栈中:字符串的高度(最顶部)和字符串的宽度(距离顶部第二)。 [更新: 实际上,字符串的高度并不完全正确-而是绘制完字符串后当前点的垂直偏移... ]

  4. 接下来,您使用 pop 。这只是删除堆栈上的最高值。现在只有字符串的宽度保留在堆栈的顶部。

  5. 2 div 将该值除以2并保留结果(字符串宽度的一半)。

  6. neg 否定堆栈中的最高值。现在,负值是堆栈中的最高值。

  7. 0 将值 0放在堆栈顶部。
  8. >
  9. rmoveto 然后消耗堆栈上的两个最高值,并将当前点向左移动该距离(字符串的一半宽度)。 / li>
  10. show 消耗第一个始终保持在堆栈底部的测试字符串并显示。
  11. li>
  1. (test) places the string "test" on the top of the stack.
  2. dup duplicates the topmost item on the stack. (You'll now have the string twice on the stack.)
  3. stringwidth. After this operator is executed, the topmost "test" string will have been consumed, and two values will have been added to the stack instead: the string's height (topmost) and the string's width (second from top). [Update: Actually, "string's height" isn't entirely correct -- it's rather the vertical offset of the current point after finishing to draw the string...]
  4. Next, you use pop. This simply deletes the topmost value on the stack. Now only string's width remains on the top of the stack.
  5. 2 div divides that value by 2 and leaves the result (half the stringwidth).
  6. neg negates the topmost value on the stack. Now that negative value is topmost on the stack.
  7. 0 places the value "0" on top of the stack.
  8. rmoveto then consumes the two topmost values on the stack and moves the current point by that distance (half the string's width) to the left.
  9. show consumes the first "test" string that remained all the time at the bottom of the stack and "shows" it.

那么考虑字符串的高度怎么办?尝试作为最后一行:

So what would work to take into account the string's height? Try as your last line:

200 700 moveto (test) dup true charpath pathbbox 3 -1 roll sub 2 div neg 3 1 roll sub 2 div exch 200 700 moveto rmoveto show"

要了解我的更改,请查找 charpath div exch 《红皮书》中的pathbbox roll sub 运算符。

To understand my changes look up the meaning of charpath, div, exch, pathbbox, roll and sub operators in The Red Book.

此命令使用Ghostscript在Windows上从代码创建PDF文件(更易于查看和检查结果):

This command uses Ghostscript to create a PDF file on Windows from the code (easier to view and check results):

 gswin32c.exe ^
      -o my.pdf ^
      -sDEVICE=pdfwrite ^
      -c "/Helvetic-Oblique findfont 10 scalefont setfont 200 700 1 0 360 arc fill 0 0 moveto (test test) dup true charpath pathbbox 3 -1 roll sub 2 div neg 3 1 roll sub 2 div exch 200 700 moveto rmoveto show"

在Linux上使用:

 gs \
      -o my.pdf \
      -sDEVICE=pdfwrite \
      -c "/Helvetic-Oblique findfont 10 scalefont setfont 200 700 1 0 360 arc fill 0 0 moveto (test test) dup true charpath pathbbox 3 -1 roll sub 2 div neg 3 1 roll sub 2 div exch 200 700 moveto rmoveto show"

可读性更好的形式是:

  gswin32c ^
     -o my.pdf ^
     -sDEVICE=pdfwrite ^
     -c "/Helvetic-Oblique findfont 10 scalefont setfont" ^
     -c "200 700 1 0 360 arc fill 0 0 moveto (test test) dup" ^
     -c "true charpath pathbbox 3 -1 roll sub 2 div neg 3 1 roll" ^
     -c "sub 2 div exch 200 700 moveto rmoveto show"

  gs \
     -o my.pdf \
     -sDEVICE=pdfwrite \
     -c "/Helvetic-Oblique findfont 10 scalefont setfont" \
     -c "200 700 1 0 360 arc fill 0 0 moveto (test test) dup" \
     -c "true charpath pathbbox 3 -1 roll sub 2 div neg 3 1 roll" \
     -c "sub 2 div exch 200 700 moveto rmoveto show"

这篇关于如何确定PostScript中的字符串高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆