%!PS-Adobe-2.0 EPSF-2.0
% Skeleton file for ps-to-ppm
% Last edited on 1999-02-04 13:27:26 by stolfi
%%Creator: JStolfi
%%Pages: 1
%%BoundingBox: 0 0 $(WIDTH) $(HEIGHT)
%%EndComments

% Font encoding

/testcharname
{ % FONT CHARNAME --> 
  % Tests whether character CHARNAME is defined in the given FONT
  exch 
  % stack: CHARNAME FONT
  dup 
  % stack: CHARNAME FONT FONT
  /Metrics known { /Metrics get } { /CharStrings get } ifelse
  % stack: CHARNAME DICT
  1 index known
    { % stack: CHARNAME 
      pop
    }
    { % stack: CHARNAME 
      (undefined:) == ==
    }
  ifelse
} def

/reencode
{ 
  % NEWENC NEWNAME OLDNAME --> 
  % Creates a new font NEWNAME equal to OLDNAME with the encoding
  %   modified by the given NEWENC.  The latter is a vector; each element 
  %   is either the index of the slot to be redefined next,
  %   of the name that is to be stored in the current slot
  %   (which gets incremented).  
  
  % stack: NEWENC NEWNAME OLDNAME 
  findfont begin
    % stack: NEWENC NEWNAME
    % Create new font:
    currentdict dup 
    length dict begin
      % stack: NEWENC NEWNAME OLDFONT
      { % stack: NEWENC NEWNAME KEY VAL
        1 index /FID ne { def } { pop pop } ifelse
      } forall
      % stack: NEWENC NEWNAME 
      /FontName exch def
      % stack: NEWENC
      dup length 0 eq
        { pop }
        { % stack: NEWENC
          /Encoding Encoding 256 array copy def
          % stack: NEWENC
          0 exch
          % stack: 0 NEWENC
          { % stack: CHNUM ENCELEM
            dup type /nametype eq
              { % stack: CHNUM CHNAME
                % check if defined
                dup currentdict exch
                % stack: CHNUM CHNAME NEWFONT CHNAME
                testcharname
                % stack: CHNUM CHNAME
                Encoding 2 index 2 index put
                % stack: CHNUM CHNAME
                pop 1 add
              }
              { % stack: CHNUM CHNUM2
                exch pop
              }
            ifelse
            % stack: CHNUM
          } forall
          % stack: CHNUM
          pop
        } ifelse
      % stack:
      currentdict dup 
      % stack: NEWFONT NEWFONT
    end % NEWFONT
    % stack: NEWFONT NEWFONT
  end % OLDFONT
  % stack: NEWFONT NEWFONT
  /FontName get exch 
  % stack: NEWNAME NEWFONT
  1 index ==
  definefont pop
} def
      
% NEWENC vector to use for ISO latin-1 encoding

/isoenc
[
  % Using characters 150-159 (dec) for some non-ISO Postscript
  150
    /quotesingle /pi /dagger /daggerdbl /infinity 
    /Delta /lozenge /Ohm /oe /OE
  % Characters 160-255 (dec) are official ISO-Latin-1.
  160
    /space /exclamdown /cent /sterling /currency
    /yen /brokenbar /section /dieresis /copyright
  170
    /ordfeminine /guillemotleft /logicalnot /hyphen /registered
    /macron /degree /plusminus /twosuperior /threesuperior
  180
    /acute /mu /paragraph /periodcentered /cedilla
    /onesuperior /ordmasculine /guillemotright /onequarter /onehalf
  190
    /threequarters /questiondown /Agrave /Aacute /Acircumflex
    /Atilde /Adieresis /Aring /AE /Ccedilla
  200
    /Egrave /Eacute /Ecircumflex /Edieresis /Igrave
    /Iacute /Icircumflex /Idieresis /Eth /Ntilde
  210
    /Ograve /Oacute /Ocircumflex /Otilde /Odieresis
    /multiply /Oslash /Ugrave /Uacute /Ucircumflex
  220
    /Udieresis /Yacute /Thorn /germandbls /agrave
    /aacute /acircumflex /atilde /adieresis /aring
  230
    /ae /ccedilla /egrave /eacute /ecircumflex
    /edieresis /igrave /iacute /icircumflex /idieresis
  240
    /eth /ntilde /ograve /oacute /ocircumflex
    /otilde /odieresis /divide /oslash /ugrave
  250
    /uacute /ucircumflex /udieresis /yacute /thorn
    /ydieresis
] def

/isoencode
{
  % NEWNAME OLDNAME
  % Reencodes the font OLDNAME with the iso encoding, defines
  % it as NEWNAME
  isoenc 3 1 roll reencode
} def

% RGB values that match the 6x6x6 color cube after gamma=1.8 mapping:

/v0  0.00000000 def
/v1  0.05518918 def
/v2  0.19217991 def
/v3  0.39872388 def
/v4  0.66920931 def
/v5  1.00000000 def 

/setfillcolor
  % R G B -> 
  % Sets the fill color for "oshow"
  { 
    /fillB exch def
    /fillG exch def
    /fillR exch def
  }
def

/setsize
  % width height ->
  % defines the image size
  { 
    /height exch def
    /width exch def
  }
def

% Jus in case:

$(WIDTH) $(HEIGHT) setsize

v5 v5 v0 setfillcolor

/setstrokecolor
  % R G B -> 
  % Sets the stroke color for "oshow"
  { 
    /strokeB exch def
    /strokeG exch def
    /strokeR exch def
  }
def

v0 v0 v5 setstrokecolor

/oshow  
  % STRING -> 
  % Show STRING as filled outline, with fill and stroke colors 
  % as set by "setfillcolor" and "setstrokecolor"
  % 
  { 
    dup
    gsave
      true charpath 
      gsave 
        fillR fillG fillB setrgbcolor fill
      grestore
      currentlinewidth 0 gt 
        { 
          gsave 
            strokeR strokeG strokeB setrgbcolor stroke
          grestore
        }
      if
    grestore
    stringwidth
    rmoveto
  }
def

/bground  
  % Paint background rectangle
  % bgR bgG bgB ->
  { 
    setrgbcolor
    newpath
      0 0 moveto
      width 0 lineto
      width height lineto
      0 height lineto
    closepath
    fill
    % debug:
    % 0 1 0 setrgbcolor
    % 1 setlinewidth
    % newpath
    %   2 2 moveto
    %   width 2 sub 2 lineto
    %   width 2 sub height 2 sub lineto
    %   2 height 2 sub lineto
    % closepath
    % stroke
  }
def

%%EndProlog
%%Page: 1 1
gsave

$(TEXT)

grestore
showpage
%%Trailer
