2009-02-06 25 views

回答

6

stringwidth,就像它說的那樣,不返回字符串的高度。 (在我看過的所有情況中,在執行stringwidth後,堆棧中的第二個整數爲0 - 對於水平方向運行的字符串。)stringwidth給出執行(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 

它計算字符串的(印在紅色)的高度,並使用該信息來嘗試和中心小實心圓(黑色印刷)到其邊框的中心:

Sample PostScript visualized

3

這似乎是工作的大部分時間:

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

它不會對所有/FontType的工作。

+0

如果矩陣中存在任何Y切變,這也不起作用。如果`/ FontMatrix get 5 get`不爲零,那麼字形的高度將取決於其寬度。 – 2011-09-15 20:56:47

+0

工作更多的字體類型,你需要使用FontBBox以及:http://stackoverflow.com/questions/8296322/how-to-determine-height-and-depth-of-a-postscript-font/19263342#19263342 – 2013-10-09 04:49:32

4

我已經在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運算符。

保存和恢復圖形上下文保持當前位置,所以它不會影響文檔的「流動」。

希望我幫了忙。

相關問題