2011-11-28 78 views
5

我正在尋找PostScript代碼來估計PostScript字體的高度(上升空間)和深度(下降空間)。可以使用字體的邊界框(FontBBox)嗎?`如何確定PostScript字體的高度和深度?

以下是我的問題的一些背景:字體的字形坐在基線上。很明顯,下行符號的字形會到達基線以下,我想知道基線下行可以達到多遠,這樣我就可以在佈局中提供足夠的空間。我看過PostScript code that renders a given string to check its dimension。我對給定字體的一般答案很感興趣。

回答

7

那麼,你已經發現了兩個「捷徑」。 FontBBox將字體中所有字形的邊界框疊加在一起。 false charpath flattenpath pathbbox給出指定字符串的框。

對於Type 3(用戶定義的)字體,這是所有你可以反覆指望的;但對於更受歡迎的Type 1字體,在字體和「度量」文件(用於其他應用程序)中存在豐富的度量信息

但這是我不知道的部分。因此,我要閱讀(在Adobe Type 1手冊中)並稍後擴展此答案。

編輯:事實上,它看起來像這些可能是一般的最佳途徑。

There is在/ Private字典中的名稱/ BlueValues下的Type 1字體中的垂直對齊值數組;但是不能保證字體能夠尊重它們。陣列中的第一個數字是基準超調;這是字母底部的邊緣,如'O',低於基線。陣列中的最大值將爲上升高度超調高程超調(以較高者爲準)。但是任何個人角色都可能被繪製出來而不考慮這些值(因此,不能保證)。

在另一方面,FontBBox本身可能反映了存在於字體中「特」字的邊框,無論它們是否是通過訪問編碼向量(即你不能show他們,但它們必須是glyphshow)。

所以最好的辦法是將pathbbox中的所有字符串打算使用。這會忽略可能存在但與您的目的無關的任何其他角色的貢獻。並且不要忘記flattenpath從曲線上移除控制點(可能遠離「真實」邊界框)。

4

luser droog的答案看起來相當完整,毫無疑問比我的答案更可靠,但我並不滿意這是確定標準字體的可用垂直空間的最簡單方法,它允許我創建一個可行的newline。這裏是我想出了:與gs test.ps運行它的

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

結果:

ghostscript rendering of 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結果: line under lowest descender