[comp.lang.postscript] Dictionary Sorting

cplai@daisy.UUCP (Chung-Pang Lai) (12/20/88)

Carl Sutton (sutton@tektronix) posted a program that output all available
fonts in a printer.  Since the dictionary FontDirectory is a hash table,
the output is not in alphabetical order.  I modified it to print the
result in alphabetical order.

As a side product, I wrote a dictionary sorting routine which uses the
standard insertion sort algorithm.  The keys of the dictionary are left 
on stack as text strings.  The function can be used on any dictionary.

==== cut === fontname.ps === cut ====
%!
% Original by: sutton@tektronix.UUCP (Carl Sutton)
% Added insertion sort 		cplai@daisy (C.P.Lai)
%

% Sort Dictionary key		dict => (keyn) ... (key3) (key2) (key1) n
% Dictionary is a hash table, this function is useful when the keys
% needed to be printed in increasing order, the results are left on
% stack in string datatype.  Conversion may be needed if used for other 
% purposes.
% Limitation: According to the Red Book, the Operand stack's maximum
%             depth is 500, this function will fail if the results
%             overflow the stack.
% Written by C.P.Lai  Dec 17, 1988
% normal usage:
% 	anydictionary SortDictKey {
%    		show newline
% 	} repeat
/SortDictKey { % function def
    /dictcount 1 def
    { %forall  -  sort dict keys on operand stack
        pop     % consume the value part
	% convert Key to string since type is unknown, or else ge may choke
        80 string cvs    % leave each key in a unique string on stack
        dictcount -1 2 {% for
            dup /dictindex exch def     % save index of key being checked
            1 index exch     % copy latest key for ge
            index            % use dictindex to get compare key for ge 
            ge {dictindex 1 roll exit} if    % insert into place
        } for   % check from the already sorted high end
        /dictcount dictcount 1 add def
    } forall    % standard insertion sort algorithm
    dictcount 1 sub     % leave final count on stack
} def

% Original by: sutton@tektronix.UUCP (Carl Sutton)
/placesub {     % sub argument from place
    /place place 
    3 -1 roll   % < /p p arg >
    sub def
    place       % leave place on stack
} def

/getfont {      % arguments < size font >
    findfont
    exch scalefont
    setfont
} def
%%EndProlog

% pretty printing of all the font name available.
/x 72 def
/top 756 def
/place top def
FontDirectory SortDictKey {     %repeat
    dup cvn 28 exch getfont     % select the font
    place 72 lt { showpage /place top def } if 
    x 36 placesub moveto 
    show 
} repeat
showpage
-- 
.signature under construction ...

{cbosgd,fortune,hplabs,seismo!ihnp4,ucbvax!hpda}!nsc!daisy!cplai    C.P. Lai
Daisy Systems Corp, 700B Middlefield Road, Mtn View CA 94039.  (415)960-6961