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