summaryrefslogtreecommitdiff
path: root/fonts/utilities/fontload/lib/psfstat.ps
blob: e9e35cd379f998b3a2237a08737947bb23e5833b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
%!PS-GhostScript-2.6.1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%!PS-GhostScript-3.12  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% File: psfstat.ps                                                      %
%                                                                       %
%       This PostScript program calculate fonts and characters used     %
%       in PostScript file loaded after this.                           %
%       This program usefull for make partial font DownLoading...       %
%									%
% Usage:                                                                %
%       gs -DNODISPLAY -DWRITESYSTEMDICT                                %
%          -I... (must be synchronized with SubFont path)               %
%          -sRESIDENTFONTS=file_name_with_list_of_printer_fonts         %
%          -sINFILE=file_name_of_the_user_PostScript_file               %
%          -sOUTFILE=file_name_for_output_font/char_using_statistics    %
%          psfstat.ps quit.ps                                           %
%                                                                       %
% Result:                                                               %
%       If all right, It generates OUTFILE which contains names of fonts%
%       and required glyph names.                                       %
%       Zero length file mean that all fonts already in file.           %
%       OUTFILE absense means that some error was accured               %
%       in processing time. Typically, PS files refers to such fonts    %
%       which have no in file and not accesible by GS.                  %
%                                                                       %
% Date:  5-Dec-1994                                                     %
%                                                                       %
%    Copyright (C) 1994 Basil K. Malyshev.  All rights reserved.        %
%									%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

/usrdef { userdict   begin  def end } bind def
/sysdef { systemdict begin odef end } bind def
/savdef { load systemdict begin def end } bind def
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                 USAGE                                 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/usage
{
   (Usage:\n) print
   (    gs -q -DWRITESYSTEMDICT -DNODISPLAY psfstat.ps <user-ps-file>\n) print
   stop
} bind def

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%    Check SYSTEMDICT write permission and disable all access checks.	%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

systemdict dup wcheck not { usage } if
begin /readonly { } odef /executeonly { } odef /noaccess { } odef end
      /readonly { } odef /executeonly { } odef /noaccess { } odef 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                 Statistic collection routines ....                    %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

/PSF-Stat    100 dict def % Dictionary with statistics ...
/PSF-CurFont null def     % No Any current font there

/Create-Font-Stat-Pool
{
  256 dict        % Dictionary with charstrings ...
} bind def

/Use-Font                 % Select current font ...
{
% (Call Use-Font\n) print pstack
  dup /FontType get 1 eq
  {
    % { dup /OrigFont known { /OrigFont get } { exit } ifelse } loop
    % /FontName get % Name of original font there ...
    /FontInfo get /FullName get cvn
    dup PSF-Stat exch known
      { PSF-Stat exch get }
      { Create-Font-Stat-Pool PSF-Stat 2 index 2 index put exch pop }
    ifelse
  }
  { % Don't try collect statistics for NOT Type1 fonts.
    pop null % discard dictionary, and fill NONE font identifier ...
  } ifelse
  /PSF-CurFont exch usrdef
% (End of Call Use-Font\n) print pstack
} bind sysdef

/Use-Character
{
  PSF-CurFont dup null eq
  {
     pop
     (Calling font using routines (i.e. `show') before font selection\n) print
     (Refer to character ) print (....) cvs print (\n) print
  }
  {
    begin currentfont /Encoding get exch get 1 def end
  } ifelse
} bind sysdef

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%       Save original system command and redefine it by self hacks      %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

/Native-show       /show savdef
/Native-charpath   /charpath savdef
/Native-widthshow  /widthshow savdef
/Native-awidthshow /awidthshow savdef
/Native-ashow      /ashow savdef
/Native-setfont    /setfont savdef
/Native-showpage   /showpage savdef
/Native-findfont   /findfont savdef

% This important routine dump marking of the font preloading
% and font full names ...

/Can-not-find-font
{
  (Can not find font ) print
  (...........................................) cvs print
  (\n Process will be terminated in this reason...\n) print
  psfsFile closefile
  OUTFILE deletefile % File absence denotes some error...
  quit
} bind def

%%% Load map of our ATM compatible Type1 fonts.
%%% Load our map into separate dictionary (PSF-Fontmap)
/PSF-Fontmap 512 dict def
PSF-Fontmap begin
   /; { def } bind def
   (Fontmap.t1) findlibfile 
   {
     closefile
     run
%     exch pop .loadFontmap % It is run in GS2.61 and GS3.12
   }
   { pop
     (I can not found Type 1 fonts specific map Fontmap.t1\n) print 
     (It is probabbly error, but I will continue...\n) print
   }
   ifelse
   currentdict /; undef
end

%%% Load list of resident fonts now ....
/ResidentFonts 100 dict def
systemdict /RESIDENTFONTS known
{ % load list of resident fonts
  RESIDENTFONTS findlibfile 
  { % Load all described fonts...
     { % Read line by line in loop
       dup (....................................) readline not { pop exit } if
       cvn ResidentFonts begin true def end
     } loop
     closefile
  } if
  pop
} if

% In this dictionary is stored already prepared fonts to
% avoid recursion effects which is appeared in GS 3.12
/findfontCalledOn 100 dict def

/findfont
{ findfontCalledOn 1 index known { Native-findfont } {
  findfontCalledOn begin dup true def end 
  (/) psfsWrite dup (........................................) cvs psfsWrite
  dup FontDirectory exch known 
  { % Font exists in DontDirectory, this means that it is already preloaded ...
    ( true ) psfsWrite
    Native-findfont
  }
  { % Font yet not loaded, There I will try to load it ...
     ( false ) psfsWrite
     dup ResidentFonts exch known 
     { Native-findfont }
     {
        dup PSF-Fontmap exch known 
        { dup PSF-Fontmap exch get findlibfile 
           {.loadfont pop} {Can-not-find-font} ifelse 
        }
        {
           dup Fontmap exch known 
           { dup Fontmap exch get findlibfile 
              {.loadfont pop} {Can-not-find-font} ifelse 
           }
           { Can-not-find-font }
           ifelse
        }
        ifelse
        Native-findfont
     }
     ifelse
  } 
  ifelse 
  dup /FontInfo get /FullName get 
    (\() psfsWrite psfsWrite (\) psfsAlias\n) psfsWrite 
} ifelse } bind sysdef % bind systemdict begin def end % sysdef

/show
{
  PSF-CurFont null ne { dup { Use-Character } forall } if
  Native-show
} bind sysdef

/charpath
{
  PSF-CurFont null ne { 1 index { Use-Character } forall } if
  Native-charpath
} bind sysdef

/ashow          % ax ay string ashow -
{
  PSF-CurFont null ne { dup { Use-Character } forall } if
  Native-ashow
} bind sysdef

/awidthshow     % cx cy char ax ay string awidthshow -
{
  PSF-CurFont null ne { dup { Use-Character } forall } if
  Native-awidthshow
} bind sysdef

/widthshow     % cx cy char string widthshow -
{
  PSF-CurFont null ne { dup { Use-Character } forall } if
  Native-widthshow
} bind sysdef

/setfont
{
% (call setfont\n) print pstack
  dup Use-Font
  Native-setfont
} bind sysdef

/showpage
{
  Print-Stat
  Native-showpage
} bind sysdef

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                   Routines for output statistics ...                  %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

/Print-Stat
{
  PSF-Stat
  {
    % Prepare font name ...
    (\() psfsWrite
    exch (.........1.........2.........3.........4) cvs psfsWrite
    (\) cvn [ ) psfsWrite
    % Prepare statistics ...
    { pop (/) psfsWrite (.............................) cvs psfsWrite  } forall
    (] psfsChars\n) psfsWrite
  } forall
} bind sysdef

/Native-restore /restore load def
/restore
{
  Print-Stat
  Native-restore
} bind sysdef

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%           To avoid destruction our hacks Close up systemdict          %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

systemdict readonly pop

/psfsFile  OUTFILE (w) file def
/psfsWrite { dup print psfsFile exch writestring } bind sysdef

%%% Now executes user file 

{ INFILE run } stopped 
{  % There is error, finish process with removing output file ...
   psfsFile closefile
   OUTFILE deletefile
%   handleerror % command run already calls this ...
   quit 
} if

% Load raw statistic file, that will remove duplicate refernces.

/PSF-Loaded 100 dict def % List of loaded fonts ...
/PSF-Alias  100 dict def
/psfsChars
{
  PSF-Stat 2 index known not
  { PSF-Stat 2 index 256 dict put } if 
  exch PSF-Stat exch get begin { 1 def } forall end
} bind def
/psfsAlias
{
  % Check, if alias yet not exists put it into dictionary ...
  cvn dup PSF-Alias exch known not 
  {
     PSF-Alias exch 3 index put
     { % Mark this font as already loaded ...
       dup PSF-Loaded exch true put
     } if
     pop
  }
  { pop pop pop }
  ifelse
} bind def

  psfsFile closefile
  (ReLoad file ... \n) print
  OUTFILE run

% 
% There are fonts which share CharaterStrings.
% For example Univers & Univer-Oblique fonts.
% In this reason Univers-Oblique font contains Univers font.
% In this reason we have list of such fonts check that used both
% and in this case redirect request from first font to second.
% So, if PS files uses both Univers and Univers-Oblique fonts
% all references to Univers font will be replaced by references 
% to Univers-Oblique.
%
% NOTE. There is list of fonts of the Univers family !
%       One, before installing this tool, must detect such fonts
%       and expand this list...
% The most simplest method to detect such fonts is to call:
%   t1disasm font-file | grep FontName
% So in case of normal font (Univers) you will see:
%  /FontName /Univers def
%  dup/FontName get exch definefont pop
% but the same command for font Univers-Oblique will outputs:
%  /FontName /Univers-Oblique def
%  /FontName /Univers def
%  dup/FontName get exch definefont pop
%  dup/FontName get exch definefont pop
% So, one can see that Univers-Oblique font contains Univers font.
%
/BiFontList 20 dict begin
/Univers                /Univers-Oblique                    def
/Univers-Black          /Univers-BlackOblique               def
/Univers-Bold           /Univers-BoldOblique                def
/Univers-CondensedBold  /Univers-CondensedBoldOblique       def
/Univers-CondensedLight /Univers-CondensedLightOblique      def
/Univers-Condensed      /Univers-CondensedOblique           def
/Univers-Light          /Univers-LightOblique               def
currentdict end def

BiFontList
{
  PSF-Stat 1 index known 2 index PSF-Stat exch known and
  { % Redirect first font references into second font
     PSF-Stat exch get begin
        dup PSF-Stat exch get { def } forall
     end
     PSF-Stat exch undef
  }
  { pop pop }
  ifelse
} forall

% Write back prepared statistic ...

  /psfsFile  OUTFILE (w) file def
  PSF-Loaded { ==only == } forall 
  PSF-Stat
  {
    % Prepare font name ...
    exch PSF-Alias exch get
    dup PSF-Loaded exch known not
    1 index ResidentFonts exch known not and
    {
       (.........1.........2.........3.........4) cvs psfsWrite
       % Write list of used characters 
       { pop (/) psfsWrite (..........................) cvs psfsWrite  } forall
       (/\n) psfsWrite
    }
    { pop pop }
    ifelse
  } forall
  psfsFile closefile
  quit

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                        End of file `psfstat.ps'                       %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%