2 % Prologue for building troff width tables. The gsave/grestore pairs are
7 /flagduplicates false def
12 /startcomments 256 def
13 /currentfontdict null def
14 /scratchstring 512 string def
17 scratchstring cvs print flush
18 slowdown {1 pop} repeat
21 /ReEncode { % vector fontname ReEncode -
23 findfont dup length dict begin
24 {1 index /FID ne {def}{pop pop} ifelse} forall
25 /Encoding 3 -1 roll def
31 /SelectFont { % fontname SelectFont -
33 dup /PaintType get 0 eq {
35 unitwidth resolution 72.0 div mul
37 /scaling resolution 72 div def
41 /currentfontdict exch def
44 /ChangeMetrics {DpostPrologue begin addmetrics end} def
48 DpostPrologue exch known {
49 DpostPrologue exch get type /nametype eq {
50 (named in prologue\n) Print
59 currentfontdict setfont
62 str false charpath flattenpath pathbbox
63 /descenderdepth 4 -1 roll .5 mul def
68 str 0 1 getinterval false charpath flattenpath pathbbox
70 dup 3 1 roll sub .25 mul add
71 /ascenderheight exch def
76 ascenderheight descenderdepth ge {
78 currentfontdict setfont
81 ( ) dup 0 4 -1 roll put
82 false charpath flattenpath pathbbox
83 exch pop 3 -1 roll pop
84 ascenderheight gt {2}{0} ifelse
85 exch descenderdepth lt {1}{0} ifelse
93 currentfontdict setfont
94 ( ) dup 0 4 -1 roll put
95 stringwidth pop scaling mul round cvi
100 256 3 1 roll % last unprintable match
103 dup 127 and 32 ge {exit} if
110 dup 255 gt {pop}{exch pop} ifelse
113 % create a font with the given encoding array
115 dup length dict begin
116 { 1 index /FID ne {def} {pop pop} ifelse } forall
120 /tmp-font exch definefont
123 % print troff font glyph table for the given glyphs
125 % This function assumes "charset" global array contains troff
126 % character names (even entries) and PS font names (odd entries);
127 % see the list in StandardCharset function in shell.lib of devutf
128 % for an example. From this array, this function generates troff
129 % font tables in three steps:
131 % 1. Initialize chcodes array to contain the position of charset characters
132 % in the encoding array of the current font (256 for characters not present)
133 % 2. Generate a temporary font encoding array for the characters in charset
134 % (for finding the width of characters)
135 % 3. Create a temporary font with this new encoding array
136 % 4. Print troff table columns for each character in charset array
137 /BuildFontCharset256 {
138 % create the charcode array
139 /chcodes charset length 2 idiv array def
140 0 2 charset length 2 sub {
142 /key charset i get def
143 /val charset i 1 add get def
144 /chcode currentfontdict /Encoding get val GetCode def
145 chcodes i 2 idiv chcode put
147 % create a temporary array for charset
148 /tmpenc charset length array def
149 0 2 charset length 2 sub {
151 /val charset i 1 add get def
152 val type /stringtype eq not {
153 tmpenc i 2 idiv val put
158 /prevfontdict currentfontdict def
159 /currentfontdict tmpenc currentfontdict TMPFont def
160 currentfontdict setfont
161 /lastvalid 0 def % last character was valid if 1
164 0 2 charset length 2 sub {
166 /key charset i get def
167 /val charset i 1 add get def
168 /curcode i 2 idiv def % tmpenc chcode
169 /chcode chcodes curcode get def % original chcode
170 val type /stringtype eq {
177 % output only available glyphs
178 currentfontdict /CharStrings get val known {
179 chcode octalescapes ge key (---) eq and {
180 (\\0) Print chcode 8 ( ) cvrs Print
184 (\t) Print curcode GetWidth Print
185 (\t) Print curcode GetAscender Print
186 chcode startcomments lt {
187 (\t) Print chcode Print
198 % restore the original font
199 /currentfontdict prevfontdict def
202 % call BuildFontCharset256 for charsets of at most 256 characters
204 % BuildFontCharset256 assumes the charset array contains at most
205 % 256 characters. This function splits the charset array
206 % otherwise and calls BuildFontCharset256 multiple times.
208 /charset2 charset def
209 0 512 charset2 length {
211 /len charset2 length i 512 add le { charset2 length i sub } {512} ifelse def
212 /charset charset2 i len getinterval def
215 /charset charset2 def
219 /DescDict 512 dict def
222 0 1 charset length 1 sub {
224 /key charset i get def
227 DescDict key cvn known {
228 flagduplicates { % for debugging
229 (<<<duplicated character: ) Print
234 DescDict key cvn 1 put
236 /Characters Characters 1 add def
237 Characters 20 mod 0 eq {(\n)}{( )} ifelse Print