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_base.ps < prev    next >
Text File  |  2001-03-19  |  22KB  |  642 lines

  1. %    Copyright (C) 1994, 1996, 1997, 1998, 1999, 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_base.ps,v 1.14 2001/03/20 05:04:59 alexcher Exp $
  16. % pdf_base.ps
  17. % Basic parser for PDF reader.
  18.  
  19. % This handles basic parsing of the file (including the trailer
  20. % and cross-reference table), as well as objects, object references,
  21. % streams, and name/number trees; it doesn't include any facilities for
  22. % making marks on the page.
  23.  
  24. /.setlanguagelevel where { pop 2 .setlanguagelevel } if
  25. .currentglobal true .setglobal
  26. /pdfdict where { pop } { /pdfdict 100 dict def } ifelse
  27. pdfdict begin
  28.  
  29. % Define the name interpretation dictionary for reading values.
  30. /valueopdict mark
  31.   (<<) cvn { mark } bind    % don't push an actual mark!
  32.   (>>) cvn /.dicttomark load
  33.   ([) cvn { mark } bind        % ditto
  34.   (]) cvn dup load
  35. %  /true true        % see .pdfexectoken below
  36. %  /false false        % ibid.
  37. %  /null null        % ibid.
  38.   /F dup cvx        % see Objects section below
  39.   /R dup cvx        % see Objects section below
  40.   /stream dup cvx    % see Streams section below
  41. .dicttomark readonly def
  42.  
  43. % ------ Utilities ------ %
  44.  
  45. % Define a scratch string.  The PDF language definition says that
  46. % no line in a PDF file can exceed 255 characters.
  47. /pdfstring 255 string def
  48.  
  49. % Read the previous line of a file.  If we aren't at a line boundary,
  50. % read the line containing the current position.
  51. % Skip any blank lines.
  52. /prevline        % - prevline <startpos> <substring>
  53.  { PDFfile fileposition dup () pdfstring
  54.    2 index 257 sub 0 .max PDFfile exch setfileposition
  55.     {        % Stack: initpos linepos line string
  56.       PDFfile fileposition
  57.       PDFfile 2 index readline pop
  58.       dup length 0 gt
  59.        { 3 2 roll 5 -2 roll pop pop 2 index }
  60.        { pop }
  61.       ifelse
  62.         % Stack: initpos linepos line string startpos
  63.       PDFfile fileposition 5 index ge { exit } if
  64.       pop
  65.     }
  66.    loop pop pop 3 -1 roll pop
  67.  } bind def
  68.  
  69. % Handle the PDF 1.2 #nn escape convention when reading from a file.
  70. % This should eventually be done in C.
  71. /.pdffixname {            % <execname> .pdffixname <execname'>
  72.   PDFversion 1.2 ge {
  73.     dup .namestring (#) search {
  74.       name#escape cvn exch pop
  75.     } {
  76.       pop
  77.     } ifelse
  78.   } if
  79. } bind def
  80. /name#escape            % <post> <(#)> <pre> name#escape <string>
  81. { exch pop
  82.   1 index 2 () /SubFileDecode filter dup (x) readhexstring
  83.         % Stack: post pre stream char t/f
  84.   not { /.pdftoken cvx /syntaxerror signalerror } if
  85.   exch closefile concatstrings
  86.   exch 2 1 index length 2 sub getinterval
  87.   (#) search { name#escape } if concatstrings
  88. } bind def
  89.  
  90. % Execute a file, interpreting its executable names in a given
  91. % dictionary.  The name procedures may do whatever they want
  92. % to the operand stack.
  93. /.pdftokenerror {        % <count> <opdict> <errtoken> .pdftokenerror -
  94.   BXlevel 0 le {
  95.     (%stderr) (w) file
  96.     dup (****************Unknown operator: ) writestring
  97.     dup 2 index .writecvs dup (\n) writestring flushfile
  98.   } if pop pop
  99.   count exch sub { pop } repeat    % pop all the operands
  100. } bind def
  101. /.pdfexectoken {        % <count> <opdict> <exectoken> .pdfexectoken ?
  102.   DEBUG { dup == flush } if
  103.   2 copy .knownget {
  104.     exch pop exch pop exch pop exec
  105.   } {
  106.         % Normally, true, false, and null would appear in opdict
  107.         % and be treated as "operators".  However, there is a
  108.         % special fast case in the PostScript interpreter for names
  109.         % that are defined in, and only in, systemdict and/or
  110.         % userdict: putting these three names in the PDF dictionaries
  111.         % destroys this property for them, slowing down their
  112.         % interpretation in all PostScript code.  Therefore, we
  113.         % check for them explicitly here instead.
  114.     dup dup dup /true eq exch /false eq or exch /null eq or {
  115.       exch pop exch pop //systemdict exch get
  116.     } {
  117.       .pdftokenerror
  118.     } ifelse
  119.   } ifelse
  120. } bind def
  121. /.pdfrun {            % <file> <opdict> .pdfrun -
  122.     % Construct a procedure with the stack depth, file and opdict
  123.     % bound into it.
  124.   1 index cvlit count 2 sub 3 1 roll mark mark 5 2 roll
  125.   {    % Stack: ..operands.. count opdict file
  126.     token {
  127.       dup type /nametype eq {
  128.     dup xcheck {
  129.       .pdfexectoken
  130.     } {
  131.       .pdffixname
  132.       exch pop exch pop DEBUG { dup ==only ( ) print flush } if
  133.     } ifelse
  134.       } {
  135.     exch pop exch pop DEBUG { dup ==only ( ) print flush } if
  136.       } ifelse
  137.     } {
  138.       (%%EOF) cvn cvx .pdfexectoken
  139.     } ifelse
  140.   }
  141.   aload pop .packtomark cvx
  142.   /loop cvx 2 packedarray cvx
  143.   { stopped /PDFsource } aload pop
  144.   PDFsource
  145.   { store { stop } if } aload pop .packtomark cvx
  146.   /PDFsource 3 -1 roll store exec
  147. } bind def
  148.  
  149. % Execute a file, like .pdfrun, for a marking context.
  150. % This temporarily rebinds LocalResources and DefaultMatrix.
  151. /.pdfruncontext {        % <resdict> <file> <opdict> .pdfruncontext -
  152.   /.pdfrun load LocalResources DefaultMatrix
  153.   /LocalResources 7 -1 roll store
  154.   /DefaultMatrix matrix currentmatrix store
  155.   3 .execn
  156.   /DefaultMatrix exch store
  157.   /LocalResources exch store
  158. } bind def
  159.  
  160. % Get the depth of the PDF operand stack.  The main program (pdf_main.ps)
  161. % sets pdfemptycount before calling .pdfrun.
  162. /.pdfcount {        % - .pdfcount <count>
  163.   count pdfemptycount sub
  164. } bind def
  165.  
  166. % ------ File reading ------ %
  167.  
  168. % Read the cross-reference entry for an (unresolved) object.
  169. % The caller must save and restore the PDFfile position if desired.
  170. % For invalid (free) objects, we return 0.
  171. /readxrefentry        % <object#> readxrefentry <objpos>
  172.  { dup Objects exch lget
  173.    PDFfile exch setfileposition
  174.    PDFfile token pop        % object position
  175.    PDFfile token pop        % generation #
  176.    PDFfile token pop        % n or f
  177.    dup /n eq
  178.     { pop 1 add dup 255 gt
  179.        { Generations ltype /stringtype eq
  180.       {        % Convert Generations from a string to an array.
  181.         larray Generations llength lgrowto dup
  182.         0 1 2 index llength 1 sub
  183.          { Generations 1 index lget lput dup
  184.          }
  185.         for pop /Generations exch store
  186.       }
  187.      if
  188.        }
  189.       if
  190.     }
  191.     { /f eq
  192.        { pop 0 }
  193.        { /readxrefentry cvx /syntaxerror signalerror }
  194.       ifelse
  195.     }
  196.    ifelse
  197.         % Stack: obj# objpos 1+gen#
  198.    Generations 4 -1 roll 3 -1 roll lput
  199.  } bind def
  200.  
  201. % ================================ Objects ================================ %
  202.  
  203. % Since we may have more than 64K objects, we have to use a 2-D array to
  204. % hold them (and the parallel Generations structure).
  205. /lshift 9 def
  206. /lnshift lshift neg def
  207. /lsubmask 1 lshift bitshift 1 sub def
  208. /lsublen lsubmask 1 add def
  209. /larray {    % - larray <larray>
  210.   [ [] ]
  211. } bind def
  212. /lstring {    % - lstring <lstring>
  213.   [ () ]
  214. } bind def
  215. /ltype {    % <lseq> type <type>
  216.   0 get type
  217. } bind def
  218. /lget {        % <lseq> <index> lget <value>
  219.   dup //lsubmask and 3 1 roll //lnshift bitshift get exch get
  220. } bind def
  221. /lput {        % <lseq> <index> <value> lput -
  222.   3 1 roll
  223.   dup //lsubmask and 4 1 roll //lnshift bitshift get
  224.   3 1 roll put
  225. } bind def
  226. /llength {    % <lseq> llength <length>
  227.   dup length 1 sub dup //lshift bitshift
  228.   3 1 roll get length add
  229. } bind def
  230. % lgrowto assumes newlength > llength(lseq)
  231. /growto {    % <string/array> <length> growto <string'/array'>
  232.   1 index type /stringtype eq { string } { array } ifelse
  233.   2 copy copy pop exch pop
  234. } bind def
  235. /lgrowto {    % <lseq> <newlength> lgrowto <lseq'>
  236.     dup //lsubmask add //lnshift bitshift dup 3 index length gt {
  237.     % Add more sub-arrays.  Start by completing the last existing one.
  238.         % Stack: lseq newlen newtoplen
  239.     3 -1 roll dup llength 1 sub //lsubmask or 1 add lgrowto
  240.         % Stack: newlen newtoplen lseq
  241.     [ exch aload pop
  242.     counttomark 2 add -1 roll        % newtoplen
  243.     counttomark sub { dup 0 0 getinterval lsublen growto } repeat
  244.     dup 0 0 getinterval ] exch
  245.   } {
  246.     pop
  247.   } ifelse
  248.     % Expand the last sub-array.
  249.   1 sub //lsubmask and 1 add
  250.   exch dup dup length 1 sub 2 copy
  251.         % Stack: newsublen lseq lseq len-1 lseq len-1
  252.   get 5 -1 roll growto put
  253. } bind def
  254. /lforall {    % <lseq> <proc> lforall -
  255.   /forall cvx 2 packedarray cvx forall
  256. } bind def
  257.  
  258. % We keep track of PDF objects using the following PostScript variables:
  259. %
  260. %    Generations (lstring): Generations[N] holds 1+ the current
  261. %        generation number for object number N.  (As far as we can tell,
  262. %        this is needed only for error checking.)  For free objects,
  263. %        Generations[N] is 0.
  264. %
  265. %    Objects (larray): If object N is loaded, Objects[N] is the actual
  266. %        object; otherwise, Objects[N] is an executable integer giving
  267. %        the file offset of the object's entry in the cross-reference
  268. %        table.
  269. %
  270. %    GlobalObjects (dictionary): If object N has been resolved in
  271. %        global VM, GlobalObjects[N] is the same as Objects[N]
  272. %        (except that GlobalObjects itself is stored in global VM,
  273. %        so the entry will not be deleted at the end of the page).
  274. %
  275. %    IsGlobal (lstring): IsGlobal[N] = 1 iff object N was resolved in
  276. %        global VM.  This is an accelerator to avoid having to do a
  277. %        dictionary lookup in GlobalObjects when resolving every object.
  278.  
  279. % Initialize the PDF object tables.
  280. /initPDFobjects {        % - initPDFobjects -
  281.   /Objects larray def
  282.   /Generations lstring def
  283.   .currentglobal true .setglobal
  284.   /GlobalObjects 20 dict def
  285.   .setglobal
  286.   /IsGlobal lstring def
  287. } bind def
  288.  
  289. % Grow the tables to a specified size.
  290. /growPDFobjects {        % <minsize> growPDFobjects -
  291.   dup Objects llength gt {
  292.     dup Objects exch lgrowto /Objects exch def
  293.   } if
  294.   dup Generations llength gt {
  295.     dup Generations exch lgrowto /Generations exch def
  296.   } if
  297.   dup IsGlobal llength gt {
  298.     dup IsGlobal exch lgrowto /IsGlobal exch def
  299.   } if
  300.   pop
  301. } bind def
  302.  
  303. % We represent an unresolved object reference by a procedure of the form
  304. % {obj# gen# resolveR}.  This is not a possible PDF object, because PDF has
  305. % no way to represent procedures.  Since PDF in fact has no way to represent
  306. % any PostScript object that doesn't evaluate to itself, we can 'force'
  307. % a possibly indirect object painlessly with 'exec'.
  308. % Note that since we represent streams by executable dictionaries
  309. % (see below), we need both an xcheck and a type check to determine
  310. % whether an object has been resolved.
  311. /resolved? {        % <object#> resolved? <value> true
  312.             % <object#> resolved? false
  313.   Objects 1 index lget dup xcheck {
  314.     dup type /integertype eq {
  315.         % Check whether the object is in GlobalObjects.
  316.       pop IsGlobal 1 index lget 0 eq {
  317.     pop false
  318.       } {
  319.         % Update Objects from GlobalObjects
  320.     DEBUG { (%Global=>local: ) print dup == } if
  321.     GlobalObjects 1 index get dup Objects 4 1 roll lput true
  322.       } ifelse
  323.     } {
  324.       exch pop true
  325.     } ifelse
  326.   } {
  327.     exch pop true
  328.   } ifelse
  329. } bind def
  330. /oforce /exec load def
  331. /oget {        % <array> <index> oget <object>
  332.         % <dict> <key> oget <object>
  333.         % Before release 6.20, this procedure stored the resolved
  334.         % object back into the referring slot.  In order to support
  335.         % PDF linearization, we no longer do this.
  336.   get oforce
  337. } bind def
  338. % A null value in a dictionary is equivalent to an omitted key;
  339. % we must check for this specially.
  340. /knownoget {    % <dict> <key> knownoget <value> true
  341.         % <dict> <key> knownoget false
  342.         % See oget above regarding this procedure.
  343.   .knownget {
  344.     oforce dup null eq { pop false } { true } ifelse
  345.   } {
  346.     false
  347.   } ifelse
  348. } bind def
  349.  
  350. % PDF 1.1 defines a 'foreign file reference', but not its meaning.
  351. % Per the specification, we convert these to nulls.
  352. /F {        % <file#> <object#> <generation#> F <object>
  353.         % Some PDF 1.1 files use F as a synonym for f!
  354.    .pdfcount 3 lt { f } { pop pop pop null } ifelse
  355. } bind def
  356.  
  357. /checkgeneration {  % <object#> <generation#> checkgeneration <object#> <OK>
  358.   Generations 2 index lget 1 sub 1 index eq {
  359.     pop true
  360.   } {
  361.     QUIET not {
  362.       Generations 2 index lget 0 eq {
  363.     (Warning: reference to free object: )
  364.       } {
  365.     (Warning: wrong generation: )
  366.       } ifelse print 1 index =only ( ) print =only ( R) =
  367.     } {
  368.       pop
  369.     } ifelse false
  370.   } ifelse
  371. } bind def
  372. /R {        % <object#> <generation#> R <object>
  373.   /resolveR cvx 3 packedarray cvx
  374. } bind def
  375.  
  376. % If we encounter an object definition while reading sequentially,
  377. % we just store it away and keep going.
  378. /objopdict mark
  379.   valueopdict { } forall
  380.   /endobj dup cvx
  381. .dicttomark readonly def
  382. /obj {            % <object#> <generation#> obj <object>
  383.   PDFfile objopdict .pdfrun
  384. } bind def
  385. /endobj {        % <object#> <generation#> <object> endobj <object>
  386.   3 1 roll
  387.         % Read the xref entry if we haven't yet done so.
  388.         % This is only needed for generation # checking.
  389.   1 index resolved? {
  390.     pop
  391.   } {
  392.     PDFfile fileposition
  393.     2 index readxrefentry pop
  394.     PDFoffset add PDFfile exch setfileposition
  395.   } ifelse
  396.   checkgeneration {
  397.         % The only global objects we bother to save are
  398.         % (resource) dictionaries.
  399.     1 index dup gcheck exch type /dicttype eq and {
  400.       DEBUG { (%Local=>global: ) print dup == } if
  401.       GlobalObjects 1 index 3 index put
  402.       IsGlobal 1 index 1 put
  403.     } if
  404.     Objects exch 2 index lput
  405.   } {
  406.     pop pop null
  407.   } ifelse
  408. } bind def
  409.  
  410. % When resolving an object reference, we stop at the endobj.
  411. /resolveopdict mark
  412.   valueopdict { } forall
  413.   /endobj { endobj exit } bind
  414.                 % OmniForm generates PDF file with endobj missing in some
  415.                 % objects. AR ignores this. So we have to do it too.
  416.   /obj { pop pop endobj exit } bind
  417. .dicttomark readonly def
  418. /resolveR {        % <object#> <generation#> resolveR <object>
  419.   DEBUG { (%Resolving: ) print 2 copy 2 array astore == } if
  420.   1 index resolved? {
  421.     exch pop exch pop
  422.   } {
  423.     PDFfile fileposition 3 1 roll
  424.     1 index readxrefentry
  425.     3 1 roll checkgeneration {
  426.             % Stack: savepos objpos obj#
  427.      exch PDFoffset add PDFfile exch setfileposition
  428.      PDFfile token pop 2 copy ne
  429.       { (xref error!) = /resolveR cvx /rangecheck signalerror
  430.       }
  431.      if pop PDFfile token pop
  432.      PDFfile token pop /obj ne
  433.       { (xref error!) = /resolveR cvx /rangecheck signalerror
  434.       }
  435.      if
  436.      pdf_run_resolve    % PDFfile resolveopdict .pdfrun
  437.     }
  438.     {        % Don't cache if the generation # is wrong.
  439.      pop pop null
  440.     } ifelse
  441.     exch PDFfile exch setfileposition
  442.   } ifelse
  443. } bind def      
  444.  
  445. % ================================ Streams ================================ %
  446.  
  447. % We represent a stream by an executable dictionary that contains,
  448. % in addition to the contents of the original stream dictionary:
  449. %    /File - the file or string where the stream contents are stored,
  450. %      if the stream is not an external one.
  451. %    /FilePosition - iff File is a file, the position in the file
  452. %      where the contents start.
  453. %    /StreamKey - the key used to decrypt this stream, if any.
  454. % We do the real work of constructing the data stream only when the
  455. % contents are needed.
  456.  
  457. % Construct a stream.  The length is not reliable in the face of
  458. % different end-of-line conventions, but it's all we've got.
  459. %
  460. % PDF files are inconsistent about what may fall between the 'stream' keyword
  461. % and the actual stream data, and it appears that no one algorithm can
  462. % detect this reliably.  We used to try to guess whether the file included
  463. % extraneous \r and/or \n characters, but we no longer attempt to do so,
  464. % especially since the PDF 1.2 specification states flatly that the only
  465. % legal terminators following the 'stream' keyword are \n or \r\n, both of
  466. % which are properly skipped and discarded by the token operator.
  467. /stream {    % <dict> stream <modified_dict>
  468.   dup /F known dup PDFsource PDFfile eq or {
  469.     not {
  470.       dup /File PDFfile put
  471.       dup /FilePosition PDFfile fileposition put
  472.       DEBUG { (%FilePosition: ) print dup /FilePosition get == } if
  473.     } if
  474.     PDFfile fileposition 1 index /Length oget add
  475.       PDFfile exch setfileposition
  476.   } {
  477.     pop
  478.     % We're already reading from a stream, which we can't reposition.
  479.     % Capture the sub-stream contents in a string.
  480.     dup /Length oget string PDFsource exch readstring
  481.     not {
  482.       (Unexpected EOF in stream!) =
  483.       /stream cvx /rangecheck signalerror
  484.     } if
  485.     1 index exch /File exch put
  486.   } ifelse
  487.   PDFsource token pop
  488.     /endstream ne { /stream cvx /syntaxerror signalerror } if
  489.   cvx
  490. } bind def
  491. /endstream {
  492.   exit
  493. } bind def
  494.  
  495. % Contrary to the published PDF (1.3) specification, Acrobat Reader
  496. % accepts abbreviated filter names everywhere, not just for in-line images,
  497. % and some applications (notably htmldoc) rely on this.
  498. /unabbrevfilterdict mark
  499.   /AHx /ASCIIHexDecode  /A85 /ASCII85Decode  /CCF /CCITTFaxDecode
  500.   /DCT /DCTDecode  /Fl /FlateDecode  /LZW /LZWDecode  /RL /RunLengthDecode
  501. .dicttomark readonly def
  502.  
  503. % Extract and apply filters.
  504. /filterparms {        % <dict> <DPkey> <Fkey> filterparms
  505.             %   <dict> <parms> <filternames>
  506.   2 index exch .knownget {
  507.     exch 2 index exch .knownget {
  508.         % Both filters and parameters.
  509.       exch dup type /nametype eq {
  510.     1 array astore exch 1 array astore exch
  511.       } if
  512.     } {
  513.         % Filters, but no parameters.
  514.       null exch
  515.       dup type /nametype eq { 1 array astore } if
  516.     } ifelse
  517.   } {
  518.         % No filters: ignore parameters, if any.
  519.     pop null { }
  520.   } ifelse
  521. } bind def
  522. /filtername {        % <filtername> filtername <filtername'>
  523.   //unabbrevfilterdict 1 index .knownget { exch pop } if
  524. } bind def
  525. /applyfilters {        % <parms> <source> <filternames> applyfilters <stream>
  526.   2 index null eq {
  527.     { filtername filter }
  528.   } {
  529.     {        % Stack: parms stream filtername
  530.       2 index 0 oget dup null eq { pop } { exch } ifelse filtername filter
  531.       exch dup length 1 sub 1 exch getinterval exch
  532.     }
  533.   } ifelse forall exch pop
  534. } bind def
  535.  
  536. % Resolve a stream dictionary to a PostScript stream.
  537. % Streams with no filters require special handling:
  538. %    - If we are going to interpret their contents, we let endstream
  539. %      terminate the interpretation loop;
  540. %    - If we are just going to read data from them, we impose
  541. %      a SubFileDecode filter that reads just the requisite amount of data.
  542. % Note that, in general, resolving a stream repositions PDFfile.
  543. % Clients must save and restore the position of PDFfile themselves.
  544. /resolvestream {    % <streamdict> <readdata?> resolvestream <stream>
  545.   1 index /F .knownget {
  546.         % This stream is stored on an external file.
  547.     (r) file 3 -1 roll
  548.     /FDecodeParms /FFilter filterparms
  549.         % Stack: readdata? file dict parms filternames
  550.     4 -1 roll exch
  551.     pdf_decrypt_stream
  552.     applyfilters
  553.   } {
  554.     exch dup /FilePosition .knownget {
  555.       1 index /File get exch setfileposition
  556.     } if
  557.         % Stack: readdata? dict
  558.     /DecodeParms /Filter filterparms
  559.         % Stack: readdata? dict parms filternames
  560.     2 index /File get exch
  561.         % Stack: readdata? dict parms file/string filternames
  562.     pdf_decrypt_stream        % add decryption if needed
  563.     dup length 0 eq {
  564.         % All the PDF filters have EOD markers, but in this case
  565.         % there is no specified filter.
  566.       pop exch pop
  567.         % Stack: readdata? dict file/string
  568.       2 index {
  569.         % We're going to read data; use a SubFileDecode filter.
  570.     1 index /Length oget () /SubFileDecode filter
  571.       } {
  572.     dup type /filetype ne {
  573.         % Use a SubFileDecode filter to read from a string.
  574.       0 () /SubFileDecode filter
  575.     } if
  576.       } ifelse
  577.     } {
  578.       applyfilters
  579.     } ifelse
  580.   } ifelse
  581.         % Stack: readdata? dict file
  582.   exch pop exch pop
  583. } bind def
  584.  
  585. % ============================ Name/number trees ============================ %
  586.  
  587. /nameoget {        % <nametree> <key> nameoget <obj|null>
  588.   exch /Names exch .treeget
  589. } bind def
  590.  
  591. /numoget {        % <numtree> <key> numoget <obj|null>
  592.   exch /Nums exch .treeget
  593. } bind def
  594.  
  595. /.treeget {        % <key> <leafkey> <tree> .treeget <obj|null>
  596.   dup /Kids knownoget {
  597.     exch pop .branchget
  598.   } {
  599.     exch get .leafget
  600.   } ifelse
  601. } bind def
  602.  
  603. /.branchget {        %  <key> <leafkey> <kids> .branchget <obj|null>
  604.   dup length 0 eq {
  605.     pop pop pop null
  606.   } {
  607.     dup length -1 bitshift 2 copy oget
  608.             % Stack: key leafkey kids mid kids[mid]
  609.     dup /Limits oget aload pop
  610.             % Stack: key leafkey kids mid kids[mid] min max
  611.     6 index lt {
  612.       pop pop
  613.       1 add 1 index length 1 index sub getinterval .branchget
  614.     } {
  615.       5 index gt {
  616.     pop
  617.     0 exch getinterval .branchget
  618.       } {
  619.     exch pop exch pop .treeget
  620.       } ifelse
  621.     } ifelse
  622.   } ifelse
  623. } bind def
  624.  
  625. /.leafget {        % <key> <pairs> .leafget <obj|null>
  626.   dup length 2 eq {
  627.     dup 0 get 2 index eq { 1 oget } { pop null } ifelse
  628.     exch pop
  629.   } {
  630.     dup length -1 bitshift -2 and 2 copy oget
  631.             % Stack: key pairs mid pairs[mid]
  632.     3 index gt { 0 exch } { 1 index length 1 index sub } ifelse
  633.     getinterval .leafget
  634.   } ifelse
  635. } bind def
  636.  
  637. end            % pdfdict
  638. .setglobal
  639.