home *** CD-ROM | disk | FTP | other *** search
/ swCHIP 1991 January / swCHIP_95-1.bin / utility / gs333ini / gs3.33 / pdf_font.ps < prev    next >
Text File  |  1995-12-09  |  11KB  |  355 lines

  1. %    Copyright (C) 1994, 1995 Aladdin Enterprises.  All rights reserved.
  2.  
  3. % pdf_font.ps
  4. % PDF font operations.
  5.  
  6. /.setlanguagelevel where { pop 2 .setlanguagelevel } if
  7. .currentglobal true .setglobal
  8. /pdfdict where { pop } { /pdfdict 100 dict def } ifelse
  9. GS_PDF_ProcSet begin
  10. pdfdict begin
  11.  
  12. % We cache the PostScript font in an additional element of the
  13. % font resource dictionary, called PSFont.
  14.  
  15. % ---------------- Encodings ---------------- %
  16.  
  17. % Apply a list of differences to an Encoding.
  18. /updateencoding        % <encoding> <differences> updateencoding <enc'>
  19.  { exch dup length array copy
  20.    exch dup 0 get exch dup length 1 sub
  21.    1 exch getinterval
  22.     { dup type /nametype ne
  23.        { exch pop }
  24.        { 3 copy put pop 1 add }
  25.       ifelse
  26.     }
  27.    forall pop
  28.  } bdef
  29.  
  30. % Get the Encoding for a font.
  31. /getencoding        % <base-encoding> <font-resource> getencoding <enc>
  32.  { /Encoding knownoget
  33.     { dup type /nametype eq
  34.        { exch pop findencoding
  35.        }
  36.        { dup /BaseEncoding knownoget
  37.       { findencoding 3 -1 roll pop exch
  38.       }
  39.      if
  40.      /Differences knownoget { updateencoding } if
  41.        }
  42.       ifelse
  43.     }
  44.    if
  45.  } bdef
  46.  
  47. % Insert a modified encoding into a font if needed.
  48. /adjustencoding        % <font-resource> <font> adjustencoding
  49.             %   <font-resource> <font'>
  50.  { 1 index /Encoding known
  51.     { dup /Encoding get 2 index getencoding
  52.       dup 2 index /Encoding get ne
  53.        {    % Insert the new Encoding into the font.
  54.      exch copyfont
  55.      dup /Encoding 4 -1 roll put
  56.        }
  57.        { pop
  58.        }
  59.       ifelse
  60.     }
  61.    if
  62.  } bdef
  63.  
  64. % Insert modified metrics into a font if needed.
  65. /adjustmetrics        % <font-resource> <font> adjustmetrics
  66.             %   <font-resource> <font'>
  67.  { 1 index /Widths known
  68.     { copyfont begin
  69.       /Metrics Encoding length dict
  70.               % Stack: font-res /Metrics metrics
  71.       2 index /FontDescriptor oget
  72.       /MissingWidth knownoget not { 0 } if exch
  73.       Encoding
  74.        {    % Stack: ... missing-width metrics charname
  75.      2 copy 4 index put pop
  76.        }
  77.       forall exch pop def
  78.         % Stack: font-res
  79.       dup /FirstChar oget dup 1 3 index /LastChar oget
  80.        {    % Stack: font-res first-char index
  81.      Encoding 1 index get
  82.      3 index /Widths oget 2 index 4 index sub get
  83.      Metrics 3 1 roll put pop
  84.        }
  85.       for pop
  86.       currentdict end
  87.     }
  88.    if
  89.  } bdef
  90.  
  91. % ---------------- Descriptors ---------------- %
  92.  
  93. % Partial descriptors for the 14 built-in fonts.
  94. /standardfontdescriptors mark
  95.   /Courier mark /Flags 16#23 .dicttomark
  96.   /Courier-Oblique 1 index
  97.   /Courier-Bold 1 index
  98.   /Courier-BoldOblique 1 index
  99.   /Helvetica mark /Flags 16#20 .dicttomark
  100.   /Helvetica-Oblique 1 index
  101.   /Helvetica-Bold 1 index
  102.   /Helvetica-BoldOblique 1 index
  103.   /Times-Roman mark /Flags 16#22 .dicttomark
  104.   /Times-Bold 1 index
  105.   /Times-Italic mark /Flags 16#62 .dicttomark
  106.   /Times-BoldItalic 1 index
  107.   /Symbol mark /Flags 16#4 .dicttomark
  108.   /ZapfDingbats 1 index
  109. .dicttomark readonly def
  110.  
  111. % ---------------- Utilities ---------------- %
  112.  
  113. % Fabricate a font name by adding %'s on the end.
  114. /genfontname        % <name> genfontname <name>
  115.  { dup length string cvs
  116.     { (%) concatstrings
  117.       dup cvn FontDirectory exch known not { cvn exit } if
  118.     }
  119.    loop
  120.  } bdef
  121.  
  122. % Copy a font if needed.  We can recognize a copied font by
  123. % the absence of FID.
  124. /copyfont        % <font> copyfont <font'>
  125.  { dup /FID known
  126.     { dup length dict copy
  127.       dup /FID undef
  128.       dup /FontName 2 copy get genfontname put
  129.     }
  130.    if
  131.  } bdef
  132.  
  133. % Define a modified (copied) font if needed.
  134. /defmodfont        % <font> defmodfont <font'>
  135.  { dup /FID known not { dup /FontName get exch definefont } if
  136.  } bdef
  137.  
  138. % Find a font, and adjust its encoding if necessary.
  139. /pdffindfont        % <font-resource> <fontname> pdffindfont <font>
  140.  { findfont adjustencoding adjustmetrics exch pop defmodfont
  141.  } bdef
  142.  
  143. % ---------------- Type 1 fonts ---------------- %
  144.  
  145. /buildType1        % <Type1-font-resource> buildType1 <font>
  146.  { dup /BaseFont get pdffindfont
  147.  } bdef
  148.  
  149. % The state dictionary for the embedded Type 1 font reading procedure
  150. % has the following keys and values:
  151. %    data - stream (filter)
  152. %    buffer, buffer2 - string
  153. %    leftstr - string containing (non-negative) integer
  154. %    sectionstr - string containing a character 0 .. 2
  155. %    stream - (stream) dictionary
  156. %    proc - procedure of the form {-dict- type1read}
  157. % When the procedure is executing, this dictionary is current.
  158. % leftstr and sectionstr are strings so that we can change their values
  159. % reliably in case the font executes a restore!
  160.  
  161. % Read an embedded Type 1 font.
  162. /readfontfilter        % <proc> readfontfilter <filter>
  163.  {    % We make this a separate procedure so that we can
  164.     % redefine it when we're writing PostScript.
  165.    0 () /SubFileDecode filter
  166.  } bdef
  167. /readtype1        % <font-resource> <stream-dict> readtype1 <font>
  168.  {        % Read the definition, using a procedure-based filter
  169.         % that turns binary/hex conversion on and off
  170.         % at the right times.
  171.    PDFfile fileposition 3 1 roll
  172.    7 dict begin
  173.      /leftstr (          ) 10 string copy def
  174.        dup /Length1 oget leftstr cvs pop
  175.      /sectionstr <00> 1 string copy def
  176.      /stream 1 index def
  177.      true resolvestream /data exch def
  178.      /buffer 200 string def        % arbitrary
  179.      /buffer2 buffer length 2.1 div cvi 1 sub string def
  180.    currentdict end
  181.    /type1read cvx 2 array astore cvx dup 0 get /proc 2 index put
  182.    readfontfilter
  183.    systemdict begin run end
  184.    PDFfile 3 -1 roll setfileposition
  185.    /FontDescriptor oget /FontName oget findfont
  186.  } bdef
  187.  
  188. % Execute the appropriate reading procedure.
  189. /type1read        % <dict> type1read <string>
  190.  { begin leftstr cvi
  191.     { type1read1 type1read2 type1read3 } sectionstr 0 get get exec
  192.    (          ) leftstr copy cvs pop end
  193.  } bdef
  194.  
  195. % Read the next block of data into the buffer.
  196. /type1readdata        % <left> <buffer> type1readdata <substring> <left'>
  197.  { 0 2 index 2 index length min getinterval
  198.    data exch readstring pop
  199.    dup length 3 -1 roll exch sub
  200.    DEBUG
  201.     { dup =only ( read ) print
  202.       1 index length =only (: ) print
  203.       1 index == flush
  204.     } if
  205.  } bdef
  206.  
  207. % Read the next block of the initial text portion.
  208. /type1read1        % <left> type1read1 <string> <left'>
  209.  { DEBUG { (read1 ) print } if
  210.    dup 0 eq
  211.     { pop sectionstr 0 1 put
  212.       stream /Length2 oget type1read2
  213.     }
  214.     { buffer type1readdata
  215.     }
  216.    ifelse
  217.  } bdef
  218.  
  219. % Read the next block of the encrypted portion.
  220. /type1trailer
  221. (0000000000000000000000000000000000000000000000000000000000000000\n\
  222. 0000000000000000000000000000000000000000000000000000000000000000\n\
  223. 0000000000000000000000000000000000000000000000000000000000000000\n\
  224. 0000000000000000000000000000000000000000000000000000000000000000\n\
  225. 0000000000000000000000000000000000000000000000000000000000000000\n\
  226. 0000000000000000000000000000000000000000000000000000000000000000\n\
  227. 0000000000000000000000000000000000000000000000000000000000000000\n\
  228. 0000000000000000000000000000000000000000000000000000000000000000\n\
  229. cleartomark\n)
  230. readonly def
  231. /type1read2
  232.  { DEBUG { (read2 ) print } if
  233.    dup 0 eq
  234.     { pop sectionstr 0 2 put
  235.       stream /Length3 oget
  236.       dup 0 eq { type1trailer exch } { type1read3 } ifelse
  237.     }
  238.     { buffer2 type1readdata exch
  239.       buffer /ASCIIHexEncode filter dup 3 -1 roll writestring closefile
  240.       buffer (>) search pop exch pop exch pop exch
  241.     }
  242.    ifelse
  243.  } bdef
  244.  
  245. % Read the next block of the final text portion.
  246. % When finished, this procedure returns an empty string.
  247. /type1read3
  248.  { DEBUG { (read3 ) print } if
  249.    buffer type1readdata
  250.  } bdef
  251.  
  252. % ---------------- Type 3 fonts ---------------- %
  253.  
  254. /.notdefEncoding 256 { /.notdef } repeat 256 packedarray def
  255.  
  256. /buildType3        % <Type3-font-resource> buildType3 <font>
  257.   { 8 dict begin
  258.     /FontType 3 def
  259.     /FontBBox 1 index /FontBBox get cvx def
  260.     /FontMatrix 1 index /FontMatrix oget def
  261.     /CharProcs 1 index /CharProcs oget def
  262.     /FontName 1 index /Name get genfontname def
  263.     /Encoding .notdefEncoding 2 index getencoding def
  264.     /BuildGlyph
  265.      { exch /CharProcs get exch oget
  266.        PDFfile fileposition exch
  267.        false resolvestream
  268.         % Don't let setgcolor set the color inside the BuildGlyph
  269.         % procedure, because this causes an /undefined error
  270.        pdfgsave null /FillColor gput null /StrokeColor gput
  271.        pdfopdict pdfrun
  272.        pdfgrestore
  273.        PDFfile exch setfileposition
  274.      } bdef
  275.     FontName currentdict end definefont exch pop
  276.   } bdef
  277.  
  278. % ---------------- TrueType fonts ---------------- %
  279.  
  280. /TTfonts mark
  281.   /Arial /Helvetica
  282.   /Arial,Italic /Helvetica-Oblique
  283.   /Arial,Bold /Helvetica-Bold
  284.   /Arial,BoldItalic /Helvetica-BoldOblique
  285.   /TimesNewRoman /Times-Roman
  286.   /TimesNewRoman,Italic /Times-Italic
  287.   /TimesNewRoman,Bold /Times-Bold
  288.   /TimesNewRoman,BoldItalic /Times-BoldItalic
  289. .dicttomark readonly def
  290.  
  291. /buildTrueType        % <TrueType-font-resource> buildTrueType <font>
  292.  { dup /BaseFont get
  293.    dup TTfonts exch .knownget { exch pop } if pdffindfont
  294.  } bdef
  295.  
  296. % ---------------- Font lookup ---------------- %
  297.  
  298. /fonttypeprocs mark        % <font-resource> -proc- <font>
  299.   /Type1 /buildType1 cvx
  300.   /MMType1 1 index
  301.   /Type3 /buildType3 cvx
  302.   /TrueType /buildTrueType cvx
  303. .dicttomark readonly def
  304.  
  305. /resourcefont            % <font-resource> resourcefont <font>
  306.  { dup /PSFont .knownget
  307.     { /FID .knownget
  308.        { type /fonttype eq }
  309.        { false }
  310.       ifelse
  311.     }
  312.     { false
  313.     }
  314.    ifelse
  315.     { /PSFont get
  316.     }
  317.     { dup dup /FontDescriptor knownoget
  318.        { /FontFile knownoget }
  319.        { false }
  320.       ifelse
  321.        { 1 index 3 1 roll readtype1 adjustencoding adjustmetrics exch pop defmodfont }
  322.        { dup /Subtype get fonttypeprocs exch get exec }
  323.       ifelse
  324.       2 copy /PSFont exch put
  325.       exch pop
  326.     }
  327.    ifelse
  328.  } bdef
  329.  
  330. drawopdict begin
  331.   /d0 /setcharwidth load def
  332.   /d1 /setcachedevice load def
  333.   /Tf
  334.    { exch
  335.      Page /Resources oget /Font oget exch oget
  336.      resourcefont
  337. false
  338. {        %**************** PATCH for #exec ****************
  339.         /FontName get (xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx) cvs
  340.          { dup dup length 1 sub 1 getinterval (%) ne { exit } if
  341.            0 1 index length 1 sub getinterval
  342.          }
  343.         loop cvn exch Tf currentfont
  344.         %**************** END PATCH ****************
  345. }{
  346.      exch scalefont dup setfont
  347. }ifelse
  348.      /TextFont gput
  349.    } bdef
  350. end
  351.  
  352. end            % pdfdict
  353. end            % GS_PDF_ProcSet
  354. .setglobal
  355.