11

您可以使用当前字体获取字符串的宽度,stringwidth尽管这实际上会在堆栈上推送偏移坐标,但 y 值似乎总是没用。有没有办法确定字符串的确切高度,可能包括也可能不包括下降线?

4

3 回答 3

5

stringwidth,正如它所说,不返回字符串的高度。(在我查看的所有情况下,执行后堆栈上的第二个整数stringwidth0-- 对于在水平方向上运行的字符串。)stringwidth在执行 a 后给出当前点的相对坐标(string) show

PLRM 有这样的说法stringwidth

请注意,stringwidth返回的宽度定义为当前点的移动。它与字形轮廓的尺寸无关。

那么考虑到字符串的高度有什么用呢?在 PRLM 中阅读的神奇词汇是charpathpathbbox。试试这个:

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

它计算字符串(以红色打印)的高度,并使用该信息尝试将一个小实心圆(以黑色打印)居中到其边界框的中心:

示例 PostScript 可视化

于 2010-09-13T15:10:37.293 回答
5

我已经在如何确定 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运算符。

保存和恢复图形上下文将当前点保持在原位,因此它不会影响文档的“流程”。

希望我有所帮助。

于 2011-08-19T14:08:37.343 回答
3

这似乎在大多数情况下都有效:

/fontheight { currentfont /FontMatrix get 3 get } bind def
/lineheight { fontheight 1.2 mul } bind def

它不适用于所有/FontTypes。

于 2009-03-09T21:41:45.747 回答