【发布时间】:2026-01-26 12:35:01
【问题描述】:
您可以使用stringwidth 获取当前字体中字符串的宽度,虽然这实际上会将偏移坐标推送到堆栈上,但 y 值似乎总是没用。有没有办法确定字符串的确切高度,可能包括也可能不包括下降线?
【问题讨论】:
标签: string fonts metrics postscript
您可以使用stringwidth 获取当前字体中字符串的宽度,虽然这实际上会将偏移坐标推送到堆栈上,但 y 值似乎总是没用。有没有办法确定字符串的确切高度,可能包括也可能不包括下降线?
【问题讨论】:
标签: string fonts metrics postscript
stringwidth,正如它所说,不返回字符串的高度。 (在我查看的所有情况下,执行stringwidth 后堆栈上的第二个整数是0 - 对于在水平方向上运行的字符串。)stringwidth 在执行(string) show 后给出当前点的相对坐标.
PLRM 对stringwidth 有这样的看法:
注意stringwidth返回的宽度定义为当前的移动 观点。它与字形轮廓的尺寸无关。
那么考虑字符串的高度有什么用呢?在 PRLM 中阅读的神奇词汇是 charpath 和 pathbbox。试试这个:
%!
/Helvetica findfont 60 scalefont setfont
200 700 4 0 360 arc fill
200 700 moveto (test test) dup
true charpath pathbbox
3 -1 roll sub 2 div neg 3 1 roll sub 2 div exch
1 0 0 setrgbcolor
200 700 moveto rmoveto show showpage
它计算字符串(以红色打印)的高度,并使用该信息尝试将一个小实心圆(以黑色打印)居中到其边界框的中心:
【讨论】:
我已经在How to determine string height in PostScript? 回答了这个问题,但在这里也很有用。
只是添加到 pipitas 答案:
/textheight {
gsave % save graphic context
{
100 100 moveto % move to some point
(HÍpg) true charpath pathbbox % gets text path bounding box (LLx LLy URx URy)
exch pop 3 -1 roll pop % keeps LLy and URy
exch sub % URy - LLy
}
stopped % did the last block fail?
{
pop pop % get rid of "stopped" junk
currentfont /FontMatrix get 3 get % gets alternative text height
}
if
grestore % restore graphic context
} bind def
/jumpTextLine {
textheight 1.25 mul % gets textheight and adds 1/4
0 exch neg rmoveto % move down only in Y axis
} bind def
该方法要求已经设置了一些字体。它适用于所选字体 (setfont) 及其大小 (scalefont)。
我使用 (HÍpg) 来获得最大的边界框,使用强调的大写字符和“下线”字符。结果已经足够好了。
另一种方法从 dreamlax 的答案中窃取——某些字体不支持 charpath 运算符。
保存和恢复图形上下文会保留当前点,因此它不会影响文档的“流程”。
希望我有所帮助。
【讨论】:
currentfont /FontBBox get 作为边界框,而不是使用charpath pathbbox 来获取它...在我的情况下,它在GhostScript 下完美运行;然而,当被打印机解释时,结果并不好。
这似乎大部分时间都有效:
/fontheight { currentfont /FontMatrix get 3 get } bind def
/lineheight { fontheight 1.2 mul } bind def
它不适用于所有/FontTypes。
【讨论】:
/FontMatrix get 5 get 不为零,则字形的高度将取决于其宽度。