home *** CD-ROM | disk | FTP | other *** search
/ jppd.dyndns.org / jppd.dyndns.org.tar / jppd.dyndns.org / QUERYPRO / Impressora_PDF / converter.exe / GPLGS / gs_cidfn.ps < prev    next >
Text File  |  2003-12-13  |  13KB  |  402 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,v 1.26.2.1 2003/12/12 22:07:58 giles Exp $
  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 not {
  66.     dup /FontMatrix [0.001 0 0 0.001 0 0] put
  67.     dup /FDArray get {
  68.        currentglobal exch dup gcheck setglobal
  69.        dup /FontMatrix get [1000 0 0 1000 0 0] matrix concatmatrix
  70.        /FontMatrix exch put
  71.        setglobal
  72.     } forall
  73.   } if
  74.   dup /FDArray get {
  75.         % Read the Subrs if necessary.
  76.     dup /Private get dup /Subrs known not {
  77.       dup /SubrCount .knownget {
  78.         % Stack: font Private SubrCount
  79.     currentglobal 3 1 roll 1 index gcheck setglobal
  80.     array 1 index /Subrs 3 -1 roll put
  81.         % Stack: font global Private
  82.     2 index begin begin .loadsubrs end end
  83.     setglobal
  84.       } {
  85.     pop
  86.       } ifelse readonly pop
  87.     } {
  88.       pop pop
  89.     } ifelse
  90.   } forall
  91.   3 -1 roll setglobal
  92. } bind def
  93.  
  94. % Read some Subrs for the current Type 1 subfont.
  95. % The subfont's Private dict is currentdict; the CIDFont itself is the
  96. % next dictionary on the stack.
  97. /.readsubrs {        % <Subrs> <start> .readsubrs <Subrs>
  98.   1 SubrCount 1 sub {
  99.     dup SDBytes mul SubrMapOffset add
  100.     dup SDBytes .readint exch SDBytes add SDBytes .readint
  101.     1 index sub string ReadString 2 index 3 1 roll put
  102.   } for
  103. } bind def
  104.  
  105. % Ensure that all the Subrs for the current Type 1 subfont are loaded.
  106. % The subfont's Private dict is currentdict; the CIDFont itself is the
  107. % next dictionary on the stack.
  108. /.loadsubrs {
  109.   Subrs length 0 ne {
  110.     SubrCache SubrMapOffset .knownget {
  111.         % We've already loaded some Subrs at this offset.
  112.         % Make sure we've got as many as we need.
  113.       dup length SubrCount lt {
  114.         % We need to load more.
  115.     SubrCount array exch 1 index copy length .readsubrs
  116.     SubrCache SubrMapOffset 2 index put
  117.       } if
  118.     } {
  119.         % We haven't loaded any Subrs at this offset yet.
  120.       SubrCount array 0 .readsubrs
  121.       SubrCache SubrMapOffset 2 index put
  122.     } ifelse
  123.     Subrs copy pop
  124.   } if
  125. } bind def
  126.  
  127. % ------ CIDFontType 1 (FontType 10) ------ %
  128.  
  129. dup 1 {
  130.   10 .checkfonttype pop
  131.   1 index exch .buildfont10 exch pop
  132. } put % Don't bind it here because gs_fapi.ps redefines .buildfont10
  133.  
  134. % ------ CIDFontType 2 (FontType 11) ------ %
  135.  
  136. dup 2 {
  137.   11 .checkfonttype pop
  138.   1 index exch .buildfont11 exch pop
  139. } put % Don't bind it here because gs_fapi.ps redefines .buildfont11
  140.  
  141. pop        % .cidfonttypes
  142.  
  143. % ---------------- Reading CIDFontType 0 files ---------------- %
  144.  
  145. /StartData {        % <(Binary)|(Hex)> <datalength> StartData -
  146.             %   (currentdict is CID font dict)
  147.         % If we're loading a resource file and the data format is
  148.         % binary, we can just save a pointer to the data and load it
  149.         % incrementally.
  150.   mark {
  151.         % Previous versions of this code made provisions for
  152.         % reading hex-encoded data incrementally.  Since hex data
  153.         % doesn't seem to be used in practice, we no longer bother.
  154.     2 index (Binary) ne { stop } if
  155.     currentfile .currentresourcefile ne { stop } if
  156.         % Hack: the pdfwrite driver relies on finalization to write
  157.         % out fonts.  However, the font may be finalized after the
  158.         % resource file, in which case the resource file will be
  159.         % closed.  So if the current output device is pdfwrite,
  160.         % don't use incremental loading.
  161.     currentdevice .devicename /pdfwrite eq { stop } if
  162.     currentfile fileposition
  163.   } .internalstopped {
  164.         % File is not positionable, or uses hex data.
  165.         % Load the data now.
  166.     cleartomark exch (Hex) eq
  167.       { { currentfile exch readhexstring pop } }
  168.       { { currentfile exch readstring pop } }
  169.     ifelse exch
  170.         % Stack: readproc length
  171.     dup 65400 le {
  172.         % readstring with a 0-length string causes a rangecheck,
  173.         % but a data length of 0 is allowed.
  174.       string dup () ne { 1 index exec } if
  175.     } {
  176.       mark 3 1 roll {
  177.         % Stack: mark str ... readproc length
  178.     dup 0 eq { pop exit } if
  179.     dup 65400 .min dup string 3 index exec
  180.         % Stack: mark str ... readproc length newstrlen newstr
  181.     4 1 roll sub
  182.       } loop
  183.       counttomark 1 add 1 roll ]
  184.     } ifelse
  185.     /GlyphData exch def
  186.         % If we were reading hex data, skip past the >.
  187.     2 get { readhexstring } 0 get eq {
  188.       currentfile 0 (>) .subfiledecode dup flushfile closefile
  189.     } if
  190.   } {
  191.         % File is positionable and binary, just save a pointer.
  192.         % Stack: (Binary) length -mark- pos
  193.     /GlyphData 0 def
  194.     exch pop 3 -1 roll pop exch
  195.         % Stack: pos length
  196.     /DataSource currentfile 2 index () .subfiledecode true .reusablestream def
  197.     currentfile 3 1 roll add setfileposition
  198.   } ifelse
  199.   /SubrCache 10 dict def
  200.   CIDFontName currentdict /CIDFont defineresource pop
  201.   end            % CID font dict
  202.   end            % resource category dict
  203. } bind def
  204.  
  205. % Some Adobe fonts include the line
  206. %   /Setup /cid_Setup load def
  207. % This is apparently included only to prevent proper, conforming PostScript
  208. % interpreters (as opposed to ATM or a special Adobe font loader) from
  209. % loading the font, since Setup is not referenced anywhere else in the file.
  210. /cid_Setup { } def
  211.  
  212. currentdict end
  213.  
  214. % ---------------- Rendering ---------------- %
  215.  
  216. % ------ Generic ------ %
  217.  
  218. % Read a string at a given offset in a "file" (binary file or
  219. % GlyphData in RAM).
  220. /ReadString {        % <pos> <string> ReadString <string>
  221.   GlyphData 0 eq {
  222.     % Read from the file.
  223.     DataSource 3 -1 roll setfileposition
  224.     DataSource exch readstring pop
  225.   } {
  226.     % Read from a string or an array of strings.
  227.     GlyphData .stringsreadstring
  228.   } ifelse
  229. } bind def
  230. /.stringsreadstring    % <pos> <string> <strings> .stringsreadstring
  231.             %   <vmstring>
  232. { dup type /stringtype eq
  233.    { 3 1 roll length getinterval
  234.    }
  235.    {  {        % Stack: pos string glyphdata
  236.     dup 0 get length dup 4 index gt { exit } if
  237.     4 -1 roll exch sub 3 1 roll
  238.     dup length 1 sub 1 exch getinterval
  239.       }
  240.      loop
  241.         % Stack: pos string glyphdata glyphdata[0]length
  242.         % We know no request can span more than 2 strings.
  243.      3 index 3 index length add 1 index le
  244.       {        % Request fits in a single string: just return a substring.
  245.     pop 0 get 3 1 roll length getinterval
  246.       }
  247.       {        % Request spans 2 strings.  Copy the first part.
  248.     1 index 0 get 4 index 3 -1 roll 1 index sub getinterval
  249.     2 index copy
  250.         % Copy the second part.
  251.         % Stack: pos str glyphdata str1
  252.     length exch 1 get 0 3 index length
  253.     3 index sub getinterval 2 index 3 1 roll putinterval
  254.     exch pop
  255.       }
  256.      ifelse
  257.    }
  258.   ifelse
  259. } bind def
  260.  
  261. % Interpret a byte string as a (big-endian) integer.
  262. /.cvbsi            % <bytes> .cvbsi <int>
  263. { 0 exch { exch 8 bitshift add } forall
  264. } bind def
  265.  
  266. % Read an integer from binary data.
  267. /.readint        % <pos> <nbytes> .readint <int>
  268. { string ReadString .cvbsi
  269. } bind def
  270.  
  271. % ------ CIDFontType 0 ------ %
  272.  
  273. /.readglyphdata {
  274.   currentfont exch .type9mapcid
  275.   FDArray exch get exch
  276. } bind def
  277.  
  278. % BuildGlyph procedure for CIDFontType 0.
  279. % The name %Type9BuildGlyph is known to the interpreter.
  280. /.cid0buildstring 10 string def
  281. (%Type9BuildGlyph) cvn {    % <cidfont> <cid> %Type9BuildGlyph -
  282.   .currentglobal 3 1 roll 1 index gcheck .setglobal
  283.   1 index begin
  284.   dup .readglyphdata dup null eq {
  285.         % Substitute CID 0. **** WRONG ****
  286.     pop pop 0 .readglyphdata
  287.   } if
  288.         % Stack: cidfont cid subfont charstring
  289.   dup null eq { pop pop pop pop } {    %**** WRONG ****
  290.     4 -1 roll pop
  291.     3 1 roll exch dup 4 -1 roll 0 0 moveto
  292.     3 index /FontType get 2 eq { .type2execchar } { .type1execchar } ifelse
  293.   } ifelse    %**** WRONG ****
  294.   end
  295.   .setglobal
  296. } bind def
  297.  
  298. % ------ CIDFontType 2 ------ %
  299.  
  300. % BuildGlyph procedure for CIDFontType 2.
  301. % The name %Type11BuildGlyph is known to the interpreter.
  302. (%Type11BuildGlyph) cvn {    % <cidfont> <cid> %Type11BuildGlyph -
  303.         % We must be prepared for out-of-range CIDs.
  304.   2 copy { .type11mapcid } .internalstopped {
  305.     pop /CharStrings get /.notdef get
  306.   } if
  307.             % Stack: cidfont cid glyphindex
  308.   1 index exch .type42execchar
  309. } bind def
  310.  
  311. % ---------------- Define resources ---------------- %
  312.  
  313. languagelevel exch 2 .setlanguagelevel
  314.  
  315. % Define the CIDInit ProcSet resource.
  316. % The ProcSet dictionary is still on the stack.
  317.  
  318. % We might have loaded CMap support already.  However, Adobe's
  319. % protected font downloader defines a CIDInit ProcSet that will be
  320. % loaded from the filesystem later, so we must check specifically
  321. % for the ProcSet being defined in VM.
  322. /CIDInit /ProcSet 2 copy resourcestatus { pop 0 eq } { false } ifelse {
  323.   pop pop findresource dup length 4 index length add dict .copydict
  324.   4 -1 roll exch .copydict
  325. } {
  326.   3 -1 roll
  327. } ifelse exch defineresource pop
  328.  
  329. % Define the CIDFont resource category.
  330. % We break out .buildcidfont because it appears that at least for
  331. % Type 32 (CIDFontType 4) fonts, the font can be registered in the Font
  332. % category with only a CIDFontType and no FontType.
  333. /.buildcidfont {        % <name> <fontdict> .buildcidfont
  334.                 %   <name> <cidfont>
  335.   systemdict /ProvideUnicode .knownget not { false } if {
  336.     /FontEmulationProcs /ProcSet findresource
  337.     /ProvideUnicodeDecoding get exec
  338.   } if
  339.   dup /CIDFontType get //.cidfonttypes exch get exec
  340. } odef
  341.  
  342. /CIDFont /Generic /Category findresource dup length dict .copydict
  343. dup /InstanceType /dicttype put
  344. dup /DefineResource {
  345.   .buildcidfont
  346.   /Generic /Category findresource /DefineResource get exec
  347. } put
  348. % CIDFonts may be defined in CFF OpenType files.
  349. % Check for this here.
  350. /.loadcidfontresource {
  351.   dup .ResourceFile {
  352.     {.loadfont} .execasresource
  353.   } {
  354.     dup /undefinedresource signalerror
  355.   } ifelse
  356. } bind def
  357. dup /.LoadResource {
  358.   currentglobal {
  359.     .loadcidfontresource
  360.   } {
  361.     true setglobal {.loadcidfontresource} stopped false setglobal {stop} if
  362.   } ifelse
  363. } bind put
  364.  
  365. /Category defineresource pop
  366.  
  367. % Add the new FontType resources.
  368.  
  369. 9 1 11 { dup /FontType defineresource pop } for
  370.  
  371. % Add the new FMapType resource.
  372.  
  373. 9 dup /FMapType defineresource pop
  374.  
  375. % Define the CIDMap resource category.
  376. % These aren't documented, but it's clear what they are for:
  377. % to give names to CIDMaps for CIDFontType 2 fonts.
  378.  
  379. /CIDMap /Generic /Category findresource dup length dict .copydict
  380. dup /.CheckResource {
  381.     % Allow a string, an array of strings, or (as of Adobe release 3011)
  382.     % a dictionary.
  383.   dup type dup dup /stringtype eq exch /dicttype eq or {
  384.     pop true
  385.   } {
  386.     dup /arraytype eq exch /packedarraytype eq or {
  387.       true exch { type /stringtype eq and } forall
  388.     } {
  389.       false
  390.     } ifelse
  391.   } ifelse
  392. } bind put
  393. /Category defineresource pop
  394.  
  395. .setlanguagelevel
  396.  
  397. %% Replace 1 (gs_ciddc.ps)
  398. (gs_ciddc.ps) runlibfile
  399.