home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / APPS / gs403osk.tgz / gs403osk.tar / gs_diskf.ps < prev    next >
Text File  |  1996-09-22  |  8KB  |  226 lines

  1. %    Copyright (C) 1996 Aladdin Enterprises.  All rights reserved.
  2. % This file is part of Aladdin Ghostscript.
  3. % Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  4. % or distributor accepts any responsibility for the consequences of using it,
  5. % or for whether it serves any particular purpose or works at all, unless he
  6. % or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  7. % License (the "License") for full details.
  8. % Every copy of Aladdin Ghostscript must include a copy of the License,
  9. % normally in a plain ASCII text file named PUBLIC.  The License grants you
  10. % the right to copy, modify and redistribute Aladdin Ghostscript, but only
  11. % under certain conditions described in the License.  Among other things, the
  12. % License requires that the copyright notice and this notice be preserved on
  13. % all copies.
  14.  
  15. % Support for converting Type 1 fonts without eexec encryption to
  16. % Type 4 fonts that load individual character outlines on demand.
  17.  
  18. % If DISKFONTS is true, we load individual CharStrings as they are needed.
  19. % (This is intended primarily for machines with very small memories.)
  20. % Initially, the character definition is the file position of the definition;
  21. % this gets replaced with the actual CharString.
  22. % Note that if we are loading characters lazily, CharStrings is writable.
  23.  
  24. % _Cstring must be long enough to hold the longest CharString for
  25. % a character defined using seac.  This is lenIV + 4 * 5 (for the operands
  26. % of sbw, assuming div is not used) + 2 (for sbw) + 3 * 5 (for the operands
  27. % of seac other than the character codes) + 2 * 2 (for the character codes)
  28. % + 2 (for seac), i.e., lenIV + 43.
  29.  
  30. /_Cstring 60 string def
  31.  
  32. % When we initially load the font, we call
  33. %   <index|charname> <length> <readproc> cskip_C
  34. % to skip over each character definition and return the file position instead.
  35. % This substitutes for the procedure
  36. %   <index|charname> <length> string currentfile exch read[hex]string pop
  37. %     [encrypt]
  38. % What we actually store in the CharString is fileposition * 1000 + length,
  39. %   negated if the string is stored in binary form.
  40.  
  41. /cskip_C
  42.  { exch dup 1000 ge 3 index type /nametype ne or
  43.     { % This is a Subrs string, or the string is so long we can't represent
  44.       % its length.  Load it now.
  45.       exch exec
  46.     }
  47.     { % Record the position and length, and skip the string.
  48.       dup currentfile fileposition 1000 mul add
  49.       2 index 3 get /readstring cvx eq { neg } if
  50.       3 1 roll
  51.       dup _Cstring length idiv
  52.        { currentfile _Cstring 3 index 3 get exec pop pop
  53.        } repeat
  54.       _Cstring length mod _Cstring exch 0 exch getinterval
  55.       currentfile exch 3 -1 roll 3 get exec pop pop
  56.     }
  57.    ifelse
  58.  } bind def
  59.  
  60. % Load a CharString from the file.  The font is the top entry
  61. % on the dictionary stack.
  62. /load_C     % <charname> <fileposandlength> load_C -
  63.  { dup abs 1000 idiv FontFile exch setfileposition
  64.    CharStrings 3 1 roll
  65.    .currentglobal CharStrings .gcheck .setglobal exch
  66.    dup 0 lt
  67.     { neg 1000 mod string FontFile exch readstring }
  68.     { 1000 mod string FontFile exch readhexstring }
  69.    ifelse pop
  70.    exch .setglobal
  71. % If the CharStrings aren't encrypted on the file, encrypt now.
  72.    Private /-| get 0 get
  73.    dup type /nametype ne
  74.     { dup length 5 sub 5 exch getinterval exec }
  75.     { pop }
  76.    ifelse dup 4 1 roll put
  77. % If the character is defined with seac, load its components now.
  78.    mark exch seac_C
  79.    counttomark
  80.     { StandardEncoding exch get dup CharStrings exch get
  81.       dup type /integertype eq { load_C } { pop pop } ifelse
  82.     } repeat
  83.    pop      % the mark
  84.  } bind def
  85.  
  86. /seac_C     % <charstring> seac_C <achar> <bchar> ..or nothing..
  87.  { dup length _Cstring length le
  88.     { 4330 exch _Cstring .type1decrypt exch pop
  89.       dup dup length 2 sub 2 getinterval <0c06> eq  % seac
  90.        { dup length
  91.          Private /lenIV known { Private /lenIV get } { 4 } ifelse
  92.      exch 1 index sub getinterval
  93. % Parse the string just enough to extract the seac information.
  94. % We assume that the only possible operators are hsbw, sbw, and seac,
  95. % and that there are no 5-byte numbers.
  96.      mark 0 3 -1 roll
  97.       { exch
  98.          { { dup 32 lt
  99.               { pop 0 }
  100.           { dup 247 lt
  101.              { 139 sub 0 }
  102.              { dup 251 lt
  103.             { 247 sub 256 mul 108 add 1 1 }
  104.             { 251 sub -256 mul -108 add -1 1 }
  105.                ifelse
  106.              }
  107.             ifelse
  108.           }
  109.          ifelse
  110.            }            % 0
  111.            { mul add 0 }        % 1
  112.          }
  113.         exch get exec
  114.       }
  115.      forall pop
  116.      counttomark 1 add 2 roll cleartomark   % pop all but achar bchar
  117.        }
  118.        { pop    % not seac
  119.        }
  120.       ifelse
  121.     }
  122.     { pop   % punt
  123.     }
  124.    ifelse
  125.  } bind def
  126.  
  127. % Define replacement procedures for loading fonts.
  128. % If DISKFONTS is true and the body of the font is not encrypted with eexec:
  129. %    - Prevent the CharStrings from being made read-only.
  130. %    - Substitute a different CharString-reading procedure.
  131. % (eexec disables this because the implicit 'systemdict begin' hides
  132. % the redefinitions that make the scheme work.)
  133. % We assume that:
  134. %    - The magic procedures (-|, -!, |-, and |) are defined with
  135. %   executeonly or readonly;
  136. %    - The contents of the reading procedures are as defined in bdftops.ps;
  137. %    - The font includes the code
  138. %   <font> /CharStrings <CharStrings> readonly put
  139. /.loadfontdict 6 dict def mark
  140.  /begin         % push this dict after systemdict
  141.   { dup begin
  142.     //systemdict eq { //.loadfontdict begin } if
  143.   } bind
  144.  /end           % match begin
  145.   { currentdict end
  146.     //.loadfontdict eq currentdict //systemdict eq and { end } if
  147.   } bind
  148.  /dict          % leave room for FontFile, BuildChar, BuildGlyph
  149.   { 3 add dict
  150.   } bind
  151.  /executeonly       % for reading procedures
  152.   { readonly
  153.   }
  154.  /noaccess      % for Subrs strings and Private dictionary
  155.   { readonly
  156.   }
  157.  /readonly      % for procedures and CharStrings dictionary
  158.   { % We want to take the following non-standard actions here:
  159.     %   - If the operand is the CharStrings dictionary, do nothing;
  160.     %   - If the operand is a number (a file position replacing the
  161.     %   actual CharString), do nothing;
  162.     %   - If the operand is either of the reading procedures (-| or -!),
  163.     %   substitute a different one.
  164.     dup type /dicttype eq       % CharStrings or Private
  165.     count 2 gt and
  166.      { 1 index /CharStrings ne { readonly } if }
  167.      { dup type /arraytype eq       % procedure or data array
  168.     { dup length 5 ge 1 index xcheck and
  169.        { dup 0 get /string eq
  170.          1 index 1 get /currentfile eq and
  171.          1 index 2 get /exch eq and
  172.          1 index 3 get dup /readstring eq exch /readhexstring eq or and
  173.          1 index 4 get /pop eq and
  174.           { /cskip_C cvx 2 packedarray cvx
  175.           }
  176.           { readonly
  177.           }
  178.          ifelse
  179.        }
  180.        { readonly
  181.        }
  182.       ifelse
  183.     }
  184.     { dup type /stringtype eq   % must be a Subr string
  185.        { readonly }
  186.       if
  187.     }
  188.        ifelse
  189.      }
  190.     ifelse
  191.   } bind
  192.  /definefont        % to insert BuildChar/Glyph and change FontType
  193.   { dup /FontType get 1 eq
  194.      { dup /FontType 4 put
  195.        dup /BuildChar /build_C load put
  196.        dup /BuildGlyph /build_C load put
  197.      }
  198.     if definefont
  199.   } bind
  200. counttomark 2 idiv { .loadfontdict 3 1 roll put } repeat pop
  201. .loadfontdict readonly pop
  202.  
  203. % Define the BuildChar and BuildGlyph procedures for modified fonts.
  204. % A single procedure serves for both.
  205. /build_C        % <font> <code|name> build_C -
  206.  { 1 index begin
  207.    dup dup type /integertype eq { Encoding exch get } if
  208.         % Stack: font code|name name
  209.    dup CharStrings exch .knownget not
  210.     { 2 copy eq { exch pop /.notdef exch } if
  211.       QUIET not
  212.        { (Substituting .notdef for ) print = flush }
  213.        { pop }
  214.       ifelse
  215.       /.notdef CharStrings /.notdef get
  216.     } if
  217.         % Stack: font code|name name charstring   
  218.    dup type /integertype eq
  219.     { load_C end build_C }
  220.     { end .type1execchar }
  221.    ifelse
  222.  } bind def
  223.