我正在寻找一些PostScript代码,用于估算PostScript字体的高度(升高线空间)和深度(降低线空间)。可以使用字体的边界框(FontBBox
)来实现这个目标吗?
以下是我的问题背景:字体的字形坐落在基线上。显然,具有下降线的字形将会延伸到基线以下,我想知道下降线能够延伸多远以便在布局中提供足够的空间。
我已经看到了用于渲染给定字符串并检查其尺寸的PostScript代码,但我更感兴趣的是针对给定字体的通用答案。
我正在寻找一些PostScript代码,用于估算PostScript字体的高度(升高线空间)和深度(降低线空间)。可以使用字体的边界框(FontBBox
)来实现这个目标吗?
以下是我的问题背景:字体的字形坐落在基线上。显然,具有下降线的字形将会延伸到基线以下,我想知道下降线能够延伸多远以便在布局中提供足够的空间。
我已经看到了用于渲染给定字符串并检查其尺寸的PostScript代码,但我更感兴趣的是针对给定字体的通用答案。
luser droog的回答看起来相当完整,毫无疑问比我的更加健壮,但我不满意它是确定标准字体可用垂直空间并允许我创建可行的newline
的最简单方法。这是我想出的:
%!ps-nonconforming
/inch {72 mul} bind def
/Helvetica 10 selectfont
1 inch 10 inch moveto
/fontheight currentfont dup /FontBBox get dup 3 get % top
exch 1 get sub % top - bottom
exch /FontMatrix get 3 get mul def % adjusted by height multiplier
/lineheight fontheight 1.2 mul def % add 20% for line spacing
/newline {0 lineheight neg rmoveto} bind def % negate height to move downwards
gsave (lineheight: ) show lineheight 20 string cvs show grestore
newline gsave (that worked!) show grestore
showpage
使用gs test.ps
运行的结果如下图所示:
一天后重新审视,发现OP想要分别处理升高部和降低部。下面是仅使用降低部分的演示图片:
%!ps-nonconforming
/inch {72 mul} bind def
/Helvetica 30 selectfont
1 inch 2 inch moveto
/descender currentfont dup /FontBBox get 1 get % bottom (negative number!)
exch /FontMatrix get 3 get mul def % adjusted by height multiplier
% first draw a gray line at base of text
gsave 7.5 inch 0 rlineto 0.5 setgray stroke grestore
gsave (descender: ) show descender 20 string cvs show ( pixels) show grestore
gsave 0 descender 1 sub rmoveto % one pixel below lowest descender
7.5 inch 0 rlineto 0 setgray stroke grestore
showpage
以下是gs -sDEVICE=pnggray -g640x480 -o/tmp/descender.png descender.ps
的结果: