home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / ghostscript / 8.64 / Resource / Init / gs_cidfn.ps < prev    next >
Encoding:
Text File  |  2009-04-17  |  12.6 KB  |  413 lines

  1. %    Copyright (C) 1995, 2000 Aladdin Enterprises.  All rights reserved.
  2. % This software is provided AS-IS with no warranty, either express or
  3. % implied.
  4. % This software is distributed under license and may not be copied,
  5. % modified or distributed except as expressly authorized under the terms
  6. % of the license contained in the file LICENSE in this distribution.
  7. % For more information about licensing, please refer to
  8. % http://www.ghostscript.com/licensing/. For information on
  9. % commercial licensing, go to http://www.artifex.com/licensing/ or
  10. % contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. % San Rafael, CA  94903, U.S.A., +1(415)492-9861.
  12.  
  13. % $Id: gs_cidfn.ps 8954 2008-08-08 04:22:38Z ray $
  14. % ProcSet for implementing CIDFont and CIDMap resources.
  15. % When this is run, systemdict is still writable.
  16.  
  17. % ---------------- Defining CIDFont resources ---------------- %
  18.  
  19. % Define a CIDFont resource.  This is the defineresource implementation for
  20. % the CIDFont resource category.
  21.  
  22. /.checkfonttype {    % <cidfont> <fonttype> .checkfonttype <cidfont> <new?>
  23.   1 index /FID known {
  24.     1 index /FontType get ne {
  25.       /definefont cvx /invalidfont signalerror
  26.     } if false
  27.   } {
  28.     1 index /FontType 3 -1 roll put true
  29.   } ifelse
  30. } bind def
  31.  
  32. /.cidfonttypes where { pop } { /.cidfonttypes 6 dict def } ifelse
  33. .cidfonttypes
  34.  
  35. 30 dict begin
  36.  
  37. % The key in .cidfonttypes is the CIDFontType value;
  38. % the value is a procedure that takes a font name and the CIDFont dictionary
  39. % and replaces the latter with a real font.
  40.  
  41. % ------ CIDFontType 0 (FontType 9) ------ %
  42.  
  43. % We add the following entries to the CIDFont dictionary, in addition to
  44. % the ones documented by Adobe:
  45. %    SubrCache - dictionary for caching Subr arrays
  46. % For CIDFonts where we read the data from disk incrementally:
  47. %    GlyphData is 0 (arbitrary)
  48. %    DataSource - a ReusableStreamDecode filter for the data
  49. % We also add a FID entry, and possibly a Subrs entry, to each element of
  50. % FDArray.
  51.  
  52. dup 0 {
  53.   9 .checkfonttype {
  54.     /CIDInit /ProcSet findresource begin
  55.     .completefont9
  56.     end
  57.   } if
  58.   1 index exch .buildfont9 exch pop
  59. } put % Don't bind it here, because gs_fapi.ps redefines .buildfont9
  60.  
  61. % Add entries to a new CIDFontType 0 font per documentation (FontMatrix)
  62. % or for .buildfont9 (FDArray.Private.Subrs).
  63. /.completefont9 {    % <cidfont0> .completefont9 <cidfont0>
  64.   currentglobal 3 1 roll dup gcheck setglobal
  65.   dup /FontMatrix known {
  66.     dup /FDArray get {
  67.       dup /FontMatrix get 0 get 1000000 ge {
  68.         /FontMatrix matrix put  % Bug 688517
  69.       } {
  70.         pop
  71.       } ifelse
  72.     } forall
  73.   } {
  74.     dup /FontMatrix [0.001 0 0 0.001 0 0] put
  75.     dup /FDArray get {
  76.        currentglobal exch dup gcheck setglobal
  77.        dup /FontMatrix get {1000 0 0 1000 0 0} matrix concatmatrix
  78.        /FontMatrix exch put
  79.        setglobal
  80.     } forall
  81.   } ifelse
  82.   dup /FDArray get {
  83.         % Read the Subrs if necessary.
  84.     dup /Private get dup /Subrs known not {
  85.       dup /SubrCount .knownget {
  86.         % Stack: font Private SubrCount
  87.     currentglobal 3 1 roll 1 index gcheck setglobal
  88.     array 1 index /Subrs 3 -1 roll put
  89.         % Stack: font global Private
  90.     2 index begin begin .loadsubrs end end
  91.     setglobal
  92.       } {
  93.     pop
  94.       } ifelse readonly pop
  95.     } {
  96.       pop pop
  97.     } ifelse
  98.   } forall
  99.   3 -1 roll setglobal
  100. } bind def
  101.  
  102. % Read some Subrs for the current Type 1 subfont.
  103. % The subfont's Private dict is currentdict; the CIDFont itself is the
  104. % next dictionary on the stack.
  105. /.readsubrs {        % <Subrs> <start> .readsubrs <Subrs>
  106.   1 SubrCount 1 sub {
  107.     dup SDBytes mul SubrMapOffset add
  108.     dup SDBytes .readint exch SDBytes add SDBytes .readint
  109.     1 index sub string ReadString 2 index 3 1 roll put
  110.   } for
  111. } bind def
  112.  
  113. % Ensure that all the Subrs for the current Type 1 subfont are loaded.
  114. % The subfont's Private dict is currentdict; the CIDFont itself is the
  115. % next dictionary on the stack.
  116. /.loadsubrs {
  117.   Subrs length 0 ne {
  118.     SubrCache SubrMapOffset .knownget {
  119.         % We've already loaded some Subrs at this offset.
  120.         % Make sure we've got as many as we need.
  121.       dup length SubrCount lt {
  122.         % We need to load more.
  123.     SubrCount array exch 1 index copy length .readsubrs
  124.     SubrCache SubrMapOffset 2 index put
  125.       } if
  126.     } {
  127.         % We haven't loaded any Subrs at this offset yet.
  128.       SubrCount array 0 .readsubrs
  129.       SubrCache SubrMapOffset 2 index put
  130.     } ifelse
  131.     Subrs copy pop
  132.   } if
  133. } bind def
  134.  
  135. % ------ CIDFontType 1 (FontType 10) ------ %
  136.  
  137. dup 1 {
  138.   10 .checkfonttype pop
  139.   1 index exch .buildfont10 exch pop
  140. } put % Don't bind it here because gs_fapi.ps redefines .buildfont10
  141.  
  142. % ------ CIDFontType 2 (FontType 11) ------ %
  143.  
  144. dup 2 {
  145.   11 .checkfonttype pop
  146.   1 index exch .buildfont11 exch pop
  147. } put % Don't bind it here because gs_fapi.ps redefines .buildfont11
  148.  
  149. pop        % .cidfonttypes
  150.  
  151. % ---------------- Reading CIDFontType 0 files ---------------- %
  152.  
  153. /StartData {        % <(Binary)|(Hex)> <datalength> StartData -
  154.             %   (currentdict is CID font dict)
  155.         % If we're loading a resource file and the data format is
  156.         % binary, we can just save a pointer to the data and load it
  157.         % incrementally.
  158.   mark {
  159.         % Previous versions of this code made provisions for
  160.         % reading hex-encoded data incrementally.  Since hex data
  161.         % doesn't seem to be used in practice, we no longer bother.
  162.     2 index (Binary) ne { stop } if
  163.     currentfile .currentresourcefile ne { stop } if
  164.         % Hack: the pdfwrite driver relies on finalization to write
  165.         % out fonts.  However, the font may be finalized after the
  166.         % resource file, in which case the resource file will be
  167.         % closed.  So if the current output device is pdfwrite,
  168.         % don't use incremental loading.
  169.     currentdevice .devicename /pdfwrite eq { stop } if
  170.     currentfile fileposition
  171.   } .internalstopped {
  172.         % File is not positionable, or uses hex data.
  173.         % Load the data now.
  174.     cleartomark exch
  175.     currentfile exch
  176.     (Hex) eq { /ASCIIHexDecode filter } if
  177.     exch
  178.         % Stack: file length
  179.     dup 65400 le {
  180.         % readstring with a 0-length string causes a rangecheck,
  181.         % but a data length of 0 is allowed.
  182.       string dup () ne { 1 index exch readstring pop } if
  183.     } {
  184.       mark 3 1 roll {
  185.         % Stack: mark str ... file length
  186.     dup 0 eq { pop exit } if
  187.     dup 65400 .min dup string 3 index exch readstring pop
  188.         % Stack: mark str ... file length newstrlen newstr
  189.     4 1 roll sub
  190.       } loop
  191.       counttomark 1 add 1 roll ]
  192.     } ifelse
  193.     /GlyphData exch def
  194.     dup currentfile eq { pop } { closefile } ifelse
  195.   } {
  196.         % File is positionable and binary, just save a pointer.
  197.         % Stack: (Binary) length -mark- pos
  198.     /GlyphData 0 def
  199.     exch pop 3 -1 roll pop exch
  200.         % Stack: pos length
  201.     /DataSource currentfile 2 index () .subfiledecode true .reusablestream def
  202.     currentfile 3 1 roll add setfileposition
  203.   } ifelse
  204.   /SubrCache 10 dict def
  205.   CIDFontName currentdict /CIDFont defineresource pop
  206.   end            % CID font dict
  207.   end            % resource category dict
  208. } bind def
  209.  
  210. % Some Adobe fonts include the line
  211. %   /Setup /cid_Setup load def
  212. % This is apparently included only to prevent proper, conforming PostScript
  213. % interpreters (as opposed to ATM or a special Adobe font loader) from
  214. % loading the font, since Setup is not referenced anywhere else in the file.
  215. /cid_Setup { } def
  216.  
  217. % ------ Generic ------ %
  218.  
  219. % Read a string at a given offset in a "file" (binary file or
  220. % GlyphData in RAM).
  221. /ReadString {        % <pos> <string> ReadString <string>
  222.   GlyphData 0 eq {
  223.     % Read from the file.
  224.     DataSource 3 -1 roll setfileposition
  225.     DataSource exch readstring pop
  226.   } {
  227.     % Read from a string or an array of strings.
  228.     GlyphData .stringsreadstring
  229.   } ifelse
  230. } bind def
  231. /.stringsreadstring    % <pos> <string> <strings> .stringsreadstring
  232.             %   <vmstring>
  233. { dup type /stringtype eq
  234.    { 3 1 roll length getinterval
  235.    }
  236.    {  {        % Stack: pos string glyphdata
  237.     dup 0 get length dup 4 index gt { exit } if
  238.     4 -1 roll exch sub 3 1 roll
  239.     dup length 1 sub 1 exch getinterval
  240.       }
  241.      loop
  242.         % Stack: pos string glyphdata glyphdata[0]length
  243.         % We know no request can span more than 2 strings.
  244.      3 index 3 index length add 1 index le
  245.       {        % Request fits in a single string: just return a substring.
  246.     pop 0 get 3 1 roll length getinterval
  247.       }
  248.       {        % Request spans 2 strings.  Copy the first part.
  249.     1 index 0 get 4 index 3 -1 roll 1 index sub getinterval
  250.     2 index copy
  251.         % Copy the second part.
  252.         % Stack: pos str glyphdata str1
  253.     length exch 1 get 0 3 index length
  254.     3 index sub getinterval 2 index 3 1 roll putinterval
  255.     exch pop
  256.       }
  257.      ifelse
  258.    }
  259.   ifelse
  260. } bind def
  261.  
  262. % Interpret a byte string as a (big-endian) integer.
  263. /.cvbsi            % <bytes> .cvbsi <int>
  264. { 0 exch { exch 8 bitshift add } forall
  265. } bind def
  266.  
  267. % Read an integer from binary data.
  268. /.readint        % <pos> <nbytes> .readint <int>
  269. { string ReadString .cvbsi
  270. } bind def
  271.  
  272. currentdict end
  273.  
  274. % ---------------- Rendering ---------------- %
  275.  
  276. % ------ CIDFontType 0 ------ %
  277.  
  278. /.readglyphdata {
  279.   currentfont exch .type9mapcid
  280.   FDArray exch get exch
  281. } bind def
  282.  
  283. % BuildGlyph procedure for CIDFontType 0.
  284. % The name %Type9BuildGlyph is known to the interpreter.
  285. /.cid0buildstring 10 string def
  286. (%Type9BuildGlyph) cvn {    % <cidfont> <cid> %Type9BuildGlyph -
  287.   .currentglobal 3 1 roll 1 index gcheck .setglobal
  288.   1 index begin
  289.   dup .readglyphdata dup null eq {
  290.         % Substitute CID 0. **** WRONG ****
  291.     pop pop 0 .readglyphdata
  292.   } if
  293.         % Stack: cidfont cid subfont charstring
  294.   dup null eq { pop pop pop pop } {    %**** WRONG ****
  295.     4 -1 roll pop
  296.     3 1 roll exch dup 4 -1 roll 0 0 moveto
  297.     3 index /FontType get 2 eq { .type2execchar } { .type1execchar } ifelse
  298.   } ifelse    %**** WRONG ****
  299.   end
  300.   .setglobal
  301. } bind def
  302.  
  303. % ------ CIDFontType 2 ------ %
  304.  
  305. % BuildGlyph procedure for CIDFontType 2.
  306. % The name %Type11BuildGlyph is known to the interpreter.
  307. (%Type11BuildGlyph) cvn {    % <cidfont> <cid> %Type11BuildGlyph -
  308.         % We must be prepared for out-of-range CIDs.
  309.   2 copy { .type11mapcid } .internalstopped {
  310.     pop /CharStrings get /.notdef get
  311.   } if
  312.             % Stack: cidfont cid glyphindex
  313.   1 index exch .type42execchar
  314. } bind def
  315.  
  316. % ---------------- Define resources ---------------- %
  317.  
  318. languagelevel exch 2 .setlanguagelevel
  319.  
  320. % Define the CIDInit ProcSet resource.
  321. % The ProcSet dictionary is still on the stack.
  322.  
  323. % We might have loaded CMap support already.  However, Adobe's
  324. % protected font downloader defines a CIDInit ProcSet that will be
  325. % loaded from the filesystem later, so we must check specifically
  326. % for the ProcSet being defined in VM.
  327. /CIDInit /ProcSet 2 copy resourcestatus { pop 0 eq } { false } ifelse {
  328.   pop pop findresource dup length 4 index length add dict .copydict
  329.   4 -1 roll exch .copydict
  330. } {
  331.   3 -1 roll
  332. } ifelse exch defineresource pop
  333.  
  334. % Define the CIDFont resource category.
  335. % We break out .buildcidfont because it appears that at least for
  336. % Type 32 (CIDFontType 4) fonts, the font can be registered in the Font
  337. % category with only a CIDFontType and no FontType.
  338. /.buildcidfont {        % <name> <fontdict> .buildcidfont
  339.                 %   <name> <cidfont>
  340.   systemdict /ProvideUnicode .knownget not { false } if {
  341.     /FontEmulationProcs /ProcSet findresource
  342.     /ProvideUnicodeDecoding get exec
  343.   } if
  344.   dup /CIDFontType get //.cidfonttypes exch get exec
  345. } odef
  346.  
  347. /CIDFont /Generic /Category findresource dup length dict .copydict
  348. dup /InstanceType /dicttype put
  349. dup /DefineResource {
  350.   .buildcidfont
  351.   dup /OrigFont known not {
  352.      dup dup /OrigFont exch .growput  % CPSI does it. Adding just for CET 33_all.PS conformity.
  353.   } if
  354.   dup /PaintType known not {
  355.      dup /PaintType 0 .growput  % CPSI does it. Adding just for CET 33_all.PS conformity.
  356.   } if
  357.   /Generic /Category findresource /DefineResource get exec
  358. } put
  359. % CIDFonts may be defined in CFF OpenType files.
  360. % Check for this here.
  361. /.loadcidfontresource {
  362.   dup .ResourceFile {
  363.     {.loadfont} .execasresource
  364.   } {
  365.     dup /undefinedresource signalerror
  366.   } ifelse
  367. } bind def
  368. dup /.LoadResource {
  369.   currentglobal {
  370.     .loadcidfontresource
  371.   } {
  372.     true setglobal {.loadcidfontresource} stopped false setglobal {stop} if
  373.   } ifelse
  374. } bind put
  375.  
  376. /Category defineresource pop
  377.  
  378. % Add the new FontType resources.
  379.  
  380. 9 1 11 { dup /FontType defineresource pop } for
  381.  
  382. % Add the new FMapType resource.
  383.  
  384. 9 dup /FMapType defineresource pop
  385.  
  386. % Define the CIDMap resource category.
  387. % These aren't documented, but it's clear what they are for:
  388. % to give names to CIDMaps for CIDFontType 2 fonts.
  389.  
  390. /CIDMap /Generic /Category findresource dup length dict .copydict
  391. dup /.CheckResource {
  392.     % Allow a string, an array of strings, or (as of Adobe release 3011)
  393.     % a dictionary.
  394.   dup type dup dup /stringtype eq exch /dicttype eq or {
  395.     pop true
  396.   } {
  397.     dup /arraytype eq exch /packedarraytype eq or {
  398.       true exch { type /stringtype eq and } forall
  399.     } {
  400.       false
  401.     } ifelse
  402.   } ifelse
  403. } bind put
  404. /Category defineresource pop
  405.  
  406. .setlanguagelevel
  407.  
  408. %% Replace 1 (gs_ciddc.ps)
  409. (gs_ciddc.ps) runlibfile
  410.