home *** CD-ROM | disk | FTP | other *** search
/ PC World 2001 September / PCWorld_2001-09_cd.bin / Software / Vyzkuste / rychlokurz / gs700w32.exe / gs7.00 / lib / pdf_ops.ps < prev    next >
Text File  |  2001-03-16  |  16KB  |  518 lines

  1. %    Copyright (C) 1994, 2000 Aladdin Enterprises.  All rights reserved.
  2. % This file is part of AFPL Ghostscript.
  3. % AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  4. % distributor accepts any responsibility for the consequences of using it, or
  5. % for whether it serves any particular purpose or works at all, unless he or
  6. % she says so in writing.  Refer to the Aladdin Free Public License (the
  7. % "License") for full details.
  8. % Every copy of AFPL Ghostscript must include a copy of the License, normally
  9. % in a plain ASCII text file named PUBLIC.  The License grants you the right
  10. % to copy, modify and redistribute AFPL Ghostscript, but only under certain
  11. % conditions described in the License.  Among other things, the License
  12. % requires that the copyright notice and this notice be preserved on all
  13. % copies.
  14.  
  15. % $Id: pdf_ops.ps,v 1.16 2001/03/17 01:15:42 raph Exp $
  16. % Definitions for most of the PDF operators.
  17.  
  18. .currentglobal true .setglobal
  19.  
  20. % Define pdfmark.  Don't allow it to be bound in.
  21. % Also don't define it in systemdict, because this leads some Adobe code
  22. % to think this interpreter is a distiller.
  23. % (If this interpreter really is a distiller, don't do this.)
  24. systemdict /pdfmark known not
  25.  { userdict /pdfmark { cleartomark } bind put } if
  26.  
  27. userdict /GS_PDF_ProcSet 127 dict dup begin
  28.  
  29. % ---------------- Abbreviations ---------------- %
  30.  
  31. /bdef { bind def } bind def
  32.  
  33. % ---------------- Graphics state stack ---------------- %
  34.  
  35. % PDF adds a number of parameters to the graphics state.
  36. % We implement this by pushing and popping a dictionary
  37. % each time we do a PDF gsave or grestore.
  38. % The keys in this dictionary are as follows:
  39. %    self            % identifies the dictionary as one of ours
  40. %    ClipRect        % (optional)
  41. %    Show
  42. %    TextSaveMatrix        % matrix at time of BT (iff within BT/ET)
  43. % (The following correspond directly to PDF state parameters.)
  44. %    AlphaIsShape
  45. %    FillConstantAlpha
  46. %    FillColor
  47. %    FillColorSpace
  48. %    FillOverprint
  49. %    SoftMask
  50. %    StrokeConstantAlpha
  51. %    StrokeColor
  52. %    StrokeColorSpace
  53. %    StrokeOverprint
  54. %    TextSpacing
  55. %    TextHScaling
  56. %    Leading
  57. %    TextFont
  58. %    TextLineMatrix
  59. %    TextMatrix
  60. %    TextRise
  61. %    TextRenderingMode
  62. %    WordSpacing
  63.  
  64. /nodict 1 dict def
  65. nodict /self { //nodict } executeonly put
  66. nodict readonly pop
  67.  
  68. /dictbeginpage {    % <initialdict> dictbeginpage -
  69.   //nodict 20 dict .copydict begin { def } forall
  70.   graphicsbeginpage textbeginpage
  71. } bdef
  72. /endpage {    % - endpage -
  73.   showpage end
  74. } bdef
  75.  
  76. /graphicsbeginpage {
  77.   initgraphics
  78.   currentdict /ClipRect .knownget { aload pop rectclip } if
  79.   0 g  0 G  false op  false OP  0 OPM
  80.   1 ca  1 CA  null SMask  false AIS  /Compatible BM  true TK
  81. } bdef
  82.  
  83. /gput        % <value> <key> gput -
  84.  { exch currentdict //nodict eq { /self dup load end 5 dict begin def } if
  85.         % If we're in a Level 1 system, we need to grow the
  86.         % dictionary explicitly.
  87.    currentdict length currentdict maxlength ge %eq
  88.     { currentdict dup length 3 mul 2 idiv 1 add dict .copydict end begin 
  89.     }
  90.    if def
  91.  } bdef
  92.  
  93. /q {
  94.   gsave //nodict begin
  95. } bdef
  96. % Some PDF files have excess Q operators!
  97. /Q {
  98.   currentdict /self .knownget { exec //nodict eq { end grestore } if } if
  99. } bdef
  100.  
  101. % ---------------- Color setting ---------------- %
  102.  
  103. /fcput        % <color> <colorspace> fcput -
  104.  { /FillColorSpace gput /FillColor gput
  105.  } bdef
  106. /scput        % <color> <colorspace> scput -
  107.  { /StrokeColorSpace gput /StrokeColor gput
  108.  } bdef
  109.  
  110. /csdevgray [/DeviceGray] readonly def
  111. /csdevrgb [/DeviceRGB] readonly def
  112. /csdevcmyk [/DeviceCMYK] readonly def
  113. /cspattern [/Pattern] readonly def
  114. /nullpattern1 mark
  115.    /PatternType 1 /PaintType 1 /TilingType 3 /BBox [0 0 0 0]
  116.    /XStep 1 /YStep 1 /PaintProc { }
  117. .dicttomark readonly def
  118. /nullpattern2 nullpattern1 dup length dict copy readonly def
  119.  
  120. % Each entry in the color space dictionary is a procedure of the form
  121. %    <cspace> -proc- <cspace> <initial-color>
  122. /CSdict mark
  123.   /DeviceGray { pop //csdevgray 0 } bind
  124.   /DeviceRGB { pop //csdevrgb [0 0 0] cvx } bind
  125.   /DeviceCMYK { pop //csdevcmyk [0 0 0 1] cvx } bind
  126.   /CIEBasedA { 0 } bind
  127.   /CIEBasedABC { [0 0 0] cvx } bind
  128.   /ICCBased { [ 1 index 1 oget /N get { 0 } repeat ] cvx } bind
  129.   /Separation { 1 } bind
  130.   /DeviceN {    % What is the correct value??
  131.     [ 1 index 1 get length { 1 } repeat ] cvx
  132.   } bind
  133.   /Indexed { 0 } bind
  134.   /Pattern {
  135.     dup type /nametype eq 1 index length 1 eq or {
  136.       pop //cspattern //nullpattern1 matrix makepattern
  137.     } {
  138.       //nullpattern2 matrix makepattern 1 index 1 get csset
  139.         % Stack: patternspace nullpattern basecolor basespace
  140.       pop [ 3 1 roll dup type /arraytype eq { aload pop } if
  141.       counttomark -1 roll ] cvx
  142.     } ifelse
  143.   } bind
  144. .dicttomark readonly def
  145. /csset            % <cspace> csset <color> <cspace>
  146.  { dup dup type /nametype ne { 0 get } if //CSdict exch get exec exch
  147.  } bdef
  148.  
  149. /g { //csdevgray fcput } bdef
  150. /G { //csdevgray scput } bdef
  151. /rg { 3 array astore cvx //csdevrgb fcput } bdef
  152. /RG { 3 array astore cvx //csdevrgb scput } bdef
  153. /k { 4 array astore cvx //csdevcmyk fcput } bdef
  154. /K { 4 array astore cvx //csdevcmyk scput } bdef
  155. /cs { csset fcput } bdef
  156. /CS { csset scput } bdef
  157. /ri { pop } bdef
  158. % We have to break up sc according to the number of operands.
  159. /sc1 { /FillColor gput } bdef
  160. /SC1 { /StrokeColor gput } bdef
  161. % We have to avoid storing into a color array associated with an outer
  162. % gsave level, so we do a kind of "copy on write".
  163. /sc* {
  164.   currentdict /FillColor .knownget {
  165.     astore pop
  166.   } {
  167.     /FillColor load length array astore cvx /FillColor gput
  168.   } ifelse
  169. } bdef
  170. /SC* {
  171.   currentdict /StrokeColor .knownget {
  172.     astore pop
  173.   } {
  174.     /StrokeColor load length array astore cvx /StrokeColor gput
  175.   } ifelse
  176. } bdef
  177.  
  178. % ---------------- Overprint/transparency setting ---------------- %
  179.  
  180. /op { /FillOverprint gput } bdef
  181. /OP { /StrokeOverprint gput } bdef
  182. /OPM {
  183.   /.setoverprintmode where { pop .setoverprintmode } { pop } ifelse
  184. } bdef
  185. /ca { /FillConstantAlpha gput } bdef
  186. /CA { /StrokeConstantAlpha gput } bdef
  187. /SMask { /SoftMask gput } bdef
  188. /AIS { /AlphaIsShape gput } bdef
  189. /BM {
  190.   /.setblendmode where {
  191.     pop [ exch dup type /nametype ne { aload pop } if /Normal ] {
  192.       { .setblendmode } .internalstopped not { exit } if pop
  193.     } forall
  194.   } {
  195.     pop
  196.   } ifelse
  197. } bdef
  198. /TK {
  199.   /.settextknockout where { pop .settextknockout } { pop } ifelse
  200. } bdef
  201.  
  202. % ---------------- Color installation ---------------- %
  203.  
  204. % Establish a given color (and color space) as current.
  205. /.settransparencyparams {    % <alpha> <smask> .settransparencyparams -
  206.   /.begintransparencymask where {
  207.     pop AlphaIsShape {
  208.       1 .setopacityalpha 0 .inittransparencymask exch .setshapealpha 1
  209.     } {
  210.       1 .setshapealpha 1 .inittransparencymask exch .setopacityalpha 0
  211.     } ifelse
  212.     % Set the soft mask by rendering the XObject.  Doing this every time
  213.     % is obviously very inefficient; we'll improve it later.
  214.     .settransparencymask    
  215.   } {
  216.     pop pop
  217.   } ifelse
  218. } bdef
  219. /.settransparencymask {        % <paramdict> <masknum> .settransparencymask -
  220.   exch dup null eq {
  221.     pop .inittransparencymask
  222.   } {
  223.     dup /Draw get exec
  224.   } ifelse
  225. } bdef
  226. % (Non-mask) images must execute setfillblend.
  227. /setfillblend {
  228.   FillOverprint setoverprint
  229.   FillConstantAlpha SoftMask .settransparencyparams
  230. } def
  231. /setfillstate {
  232.   FillColor FillColorSpace setgcolor setfillblend
  233. } def
  234. /setstrokestate {
  235.   StrokeColor StrokeColorSpace setgcolor StrokeOverprint setoverprint
  236.   StrokeConstantAlpha SoftMask .settransparencyparams
  237. } def
  238. /Cdict 15 dict dup begin    % <color...> <colorspace> -proc- -
  239.   /DeviceGray { pop setgray } bdef
  240.   /DeviceRGB { pop setrgbcolor } bdef
  241.   /DeviceCMYK { pop setcmykcolor } bdef
  242.   /CIEBasedA
  243.    { dup currentcolorspace eq { pop } { setcolorspace } ifelse setcolor } bdef
  244.   /CIEBasedABC /CIEBasedA load def
  245.   /CIEBasedDEF /CIEBasedA load def
  246.   /CIEBasedDEFG /CIEBasedA load def
  247.   /ICCBased /CIEBasedA load def
  248.   /Separation /CIEBasedA load def
  249.   /DeviceN /CIEBasedA load def
  250.   /Indexed /CIEBasedA load def
  251.   /Pattern
  252.    { dup currentcolorspace eq { pop } { setcolorspace } ifelse
  253.      dup /Matrix .knownget not { matrix } if
  254.      gsave DefaultMatrix setmatrix makepattern grestore setcolor
  255.    } bdef
  256. end def
  257. /setgcolor    % (null | <color...>) <colorspace> setgcolor -
  258.  { 1 index null eq
  259.     { pop pop }
  260.     { dup 0 get //Cdict exch get exec }
  261.    ifelse
  262.  } bdef
  263. /fsexec        % <fillop|strokeop> fsexec -
  264.  {        % Preserve the current point, if any.
  265.     { currentpoint } stopped
  266.     { $error /newerror false put   cvx exec }
  267.     { 3 -1 roll cvx exec moveto }
  268.    ifelse
  269.  } bdef
  270.  
  271. % ---------------- Path painting and clipping ---------------- %
  272.  
  273. /S { setstrokestate /stroke fsexec } bdef
  274. /f { setfillstate /fill fsexec } bdef
  275. /f* { setfillstate /eofill fsexec } bdef
  276. /n { newpath } bdef        % don't allow n to get bound in
  277. /s { closepath S } bdef
  278. /B { gsave setfillstate fill grestore S } bdef
  279. /b { closepath B } bdef
  280. /B* { gsave setfillstate eofill grestore S } bdef
  281. /b* { closepath B* } bdef
  282.  
  283. % Clipping:
  284.  
  285. /Wdict 4 dict dup begin
  286. /S { gsave setstrokestate stroke grestore n } bdef
  287. /f { gsave setfillstate fill grestore n } bdef
  288. /f* { gsave setfillstate eofill grestore n } bdef
  289. /n { end clip newpath } bdef
  290. end readonly def
  291. /W { //Wdict begin } bdef
  292. /W*dict 4 dict dup begin
  293. Wdict { def } forall
  294. /n { end eoclip newpath } bdef
  295. end readonly def
  296. /W* { //W*dict begin } bdef
  297.  
  298. % ---------------- Text control ---------------- %
  299.  
  300. /textbeginpage
  301.  { /TextSpacing 0 def        % 0 Tc
  302.    /TextLeading 0 def        % 0 TL
  303.    /TextRenderingMode 0 def    % 0 Tr
  304.    /TextRise 0 def        % 0 Ts
  305.    /WordSpacing 0 def        % 0 Tw
  306.    /TextHScaling 1.0 def    % 100 Tz
  307.    /TextFont null def
  308.    /Show { showfirst } def
  309.  } bdef
  310.  
  311. % Contrary to the statement in the PDF manual, BT and ET *can* be nested,
  312. % if the CharProc for a Type 3 font does a BT/ET itself.
  313. % Since we always call the CharProc inside a q/Q, we simply ensure that
  314. % the text state is saved and restored like the rest of the extended
  315. % graphics state.
  316.  
  317. /settextmatrix {
  318.   TextMatrix concat
  319.   TextHScaling 1 ne { TextHScaling 1 scale } if
  320.   TextRise 0 ne { 0 TextRise translate } if
  321.   TextFont dup null eq { pop } { setfont } ifelse
  322. } bdef
  323. /settextstate {
  324.     % The text state can be set even outside BT/ET.
  325.   currentdict /TextSaveMatrix known {
  326.     TextSaveMatrix setmatrix settextmatrix
  327.   } if
  328. } bdef
  329. /settextposition {
  330.         % Update the TextMatrix translation.
  331.   gsave TextSaveMatrix setmatrix
  332.   currentpoint TextMatrix 4 2 getinterval astore pop
  333.         % We would like to do "grestore currentpoint translate"
  334.         % here, but some PDF files set a singular text matrix
  335.         % (0 0 0 0 <x> <y> Tm), so we can't do this.
  336.   TextTempMatrix identmatrix setmatrix currentpoint
  337.   grestore
  338.   TextTempMatrix currentmatrix 4 2 getinterval astore pop
  339.   TextTempMatrix setmatrix
  340. } bdef
  341.  
  342. /BT {
  343.   currentdict /TextLineMatrix .knownget
  344.     { identmatrix pop TextMatrix identmatrix pop }
  345.     { matrix /TextLineMatrix gput matrix /TextMatrix gput }
  346.   ifelse
  347.   { showfirst } /Show gput
  348.   currentdict /TextSaveMatrix .knownget not {
  349.     matrix dup /TextSaveMatrix gput
  350.   } if currentmatrix pop settextmatrix
  351.   matrix /TextTempMatrix gput        % see settextposition
  352. } bdef
  353. /ET {
  354.   TextRenderingMode 4 ge { clip newpath } if
  355.   TextSaveMatrix setmatrix
  356.   currentdict /TextSaveMatrix undef
  357. } bdef
  358. /Tc { /TextSpacing gput { showfirst } /Show gput } bdef
  359. /TL { /TextLeading gput } bdef
  360. /Tr { /TextRenderingMode gput { showfirst } /Show gput } bdef
  361. /Ts { /TextRise gput settextstate } bdef
  362. /Tw { /WordSpacing gput { showfirst } /Show gput } bdef
  363. /Tz { 100 div /TextHScaling gput settextstate} bdef
  364.  
  365. % ---------------- Font control ---------------- %
  366.  
  367. /Tf {        % <font> <scale> Tf -
  368.   dup 1 eq { pop } { scalefont } ifelse
  369.   /TextFont gput settextstate
  370. } bdef
  371.  
  372. % Read a CFF font.
  373. /FRD        % <resname> <file> FRD -
  374.  { /FontSetInit /ProcSet findresource begin true ReadData
  375.  } bdef
  376.  
  377. % Copy a font, removing its FID.  If changed is true, also remove
  378. % the UniqueID and XUID, if any.  If the original dictionary doesn't have
  379. % the keys being removed, don't copy it.
  380. /.copyfontdict        % <font> <changed> .copyfontdict <dict>
  381.  { 1 index /FID known
  382.    1 index { 2 index /UniqueID known or 2 index /XUID known or } if
  383.     {        % We add 1 to the length just in case the original
  384.         % didn't have a FID.
  385.       exch dup length 1 add dict exch
  386.        {        % Stack: changed newfont key value
  387.      1 index /FID eq 4 index
  388.       { 2 index /UniqueID eq or 2 index /XUID eq or }
  389.      if not { 3 copy put } if pop pop
  390.        }
  391.       forall exch
  392.     }
  393.    if pop
  394.  } bdef
  395.  
  396. % Insert a new Encoding or Metrics into a font if necessary.
  397. % Return a possibly updated font, and a flag to indicate whether
  398. % the font was actually copied.
  399. /.updatefontmetrics {    % <font> <Metrics|null> .updatefontmetrics
  400.             %   <font'> <copied>
  401.   dup null ne {
  402.     exch true .copyfontdict dup /Metrics 4 -1 roll put true
  403.   } {
  404.     pop false
  405.   } ifelse
  406. } bdef
  407. /.updatefontencoding {    % <font> <Encoding|null> .updatefontencoding
  408.             %   <font'> <copied>
  409.   dup null ne { dup 2 index /Encoding get ne } { false } ifelse {
  410.     exch false .copyfontdict dup /Encoding 4 -1 roll put true
  411.   } {
  412.     pop false
  413.   } ifelse
  414. } bdef
  415. /.updatefont {        % <font> <Encoding|null> <Metrics|null> .updatefont
  416.             %   <font'> <copied>
  417.   3 -1 roll exch .updatefontmetrics
  418.             % Stack: enc|null font' copied
  419.   3 1 roll exch .updatefontencoding 3 -1 roll or
  420. } bdef
  421.  
  422. % ---------------- Text positioning ---------------- %
  423.  
  424. /Td {
  425.   TextLineMatrix transform TextLineMatrix 4 2 getinterval astore pop
  426.   TextLineMatrix TextMatrix copy pop settextstate
  427. } bdef
  428. /TD { dup neg /TextLeading gput Td } bdef
  429. /T* { 0 TextLeading neg Td } bdef
  430. /Tm {
  431.   TextLineMatrix astore TextMatrix copy pop settextstate
  432. } bdef
  433.  
  434. % ---------------- Text painting ---------------- %
  435.  
  436. /textrenderingprocs [        % (0 is handled specially)
  437.     % Painting-only modes
  438.    { tf } { tS } { tB } { tn }
  439.     % Clipping modes
  440.    { gsave tf grestore tW }
  441.    { gsave tS grestore tW }
  442.    { gsave tB grestore tW }
  443.    { tW }
  444. ] readonly def
  445. /setshowstate
  446.  { WordSpacing 0 eq TextSpacing 0 eq and
  447.     { TextRenderingMode 0 eq
  448.        { { setfillstate show } }
  449.        { { false charpath textrenderingprocs TextRenderingMode get exec } }
  450.       ifelse
  451.     }
  452.     { TextRenderingMode 0 eq
  453.        { WordSpacing 0 eq
  454.           { { setfillstate TextSpacing exch 0 exch ashow } }
  455.       { TextSpacing 0 eq
  456.          { { setfillstate WordSpacing exch 0 exch 32 exch widthshow } }
  457.          { { setfillstate WordSpacing exch TextSpacing exch 0 32 4 2 roll 0 exch awidthshow } }
  458.         ifelse
  459.       }
  460.      ifelse
  461.        }
  462.        { { WordSpacing TextSpacing
  463.             % Implement the combination of t3 and false charpath.
  464.             % Note that we must use cshow for this, because we
  465.             % can't parse multi-byte strings any other way.
  466.             % Stack: string xword xchar
  467.         { pop pop (x) dup 0 3 index put false charpath
  468.             % Stack: xword xchar ccode
  469.           3 copy 32 eq { add } { exch pop } ifelse 0 rmoveto pop
  470.         }
  471.        4 -1 roll cshow pop pop
  472.        textrenderingprocs TextRenderingMode get exec
  473.      }
  474.        }
  475.       ifelse
  476.     }
  477.    ifelse /Show gput
  478.  } bdef
  479. /showfirst { setshowstate Show } def
  480.  
  481. /Tj {
  482.   0 0 moveto Show settextposition
  483. } bdef
  484. /' { T* Tj } bdef
  485. /" { exch Tc exch Tw T* Tj } bdef
  486. /TJ {
  487.   0 0 moveto {
  488.     dup type /stringtype eq {
  489.       Show
  490.     } { -1000 div
  491.       currentfont /ScaleMatrix .knownget { 0 get mul } if
  492.       0 rmoveto
  493.     } ifelse
  494.   } forall settextposition
  495. } bdef
  496.  
  497. /tf { setfillstate currentpoint fill moveto } bdef
  498. /tn { currentpoint newpath moveto } bdef
  499. % For stroking characters, temporarily restore the graphics CTM so that
  500. % the line width will be transformed properly.
  501. /Tmatrix matrix def
  502. /tS
  503.  { setstrokestate
  504.    currentpoint //Tmatrix currentmatrix TextSaveMatrix setmatrix stroke
  505.    setmatrix moveto
  506.  } bdef
  507. /tB { gsave tf grestore tS } bdef
  508. % This does the wrong thing if there have been multiple text operations
  509. % within a single BT/ET pair, but it's a start.
  510. /tW { } bdef
  511.  
  512. end readonly put        % GS_PDF_ProcSet
  513.  
  514. .setglobal
  515.