web/lib/Zend/Pdf/Element/Reference/Table.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Pdf
       
    17  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    18  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    19  * @version    $Id: Table.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    20  */
       
    21 
       
    22 
       
    23 /**
       
    24  * PDF file reference table
       
    25  *
       
    26  * @category   Zend
       
    27  * @package    Zend_Pdf
       
    28  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    29  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    30  */
       
    31 class Zend_Pdf_Element_Reference_Table
       
    32 {
       
    33     /**
       
    34      * Parent reference table
       
    35      *
       
    36      * @var Zend_Pdf_Element_Reference_Table
       
    37      */
       
    38     private $_parent;
       
    39 
       
    40     /**
       
    41      * Free entries
       
    42      * 'reference' => next free object number
       
    43      *
       
    44      * @var array
       
    45      */
       
    46     private $_free;
       
    47 
       
    48     /**
       
    49      * Generation numbers for free objects.
       
    50      * Array: objNum => nextGeneration
       
    51      *
       
    52      * @var array
       
    53      */
       
    54     private $_generations;
       
    55 
       
    56     /**
       
    57      * In use entries
       
    58      * 'reference' => offset
       
    59      *
       
    60      * @var array
       
    61      */
       
    62     private $_inuse;
       
    63 
       
    64     /**
       
    65      * Generation numbers for free objects.
       
    66      * Array: objNum => objGeneration
       
    67      *
       
    68      * @var array
       
    69      */
       
    70     private $_usedObjects;
       
    71 
       
    72 
       
    73 
       
    74     /**
       
    75      * Object constructor
       
    76      */
       
    77     public function  __construct()
       
    78     {
       
    79         $this->_parent = null;
       
    80         $this->_free   = array();  $this->_generations = array();
       
    81         $this->_inuse  = array();  $this->_usedObjects = array();
       
    82     }
       
    83 
       
    84 
       
    85     /**
       
    86      * Add reference to the reference table
       
    87      *
       
    88      * @param string $ref
       
    89      * @param integer $offset
       
    90      * @param boolean $inuse
       
    91      */
       
    92     public function addReference($ref, $offset, $inuse = true)
       
    93     {
       
    94         $refElements = explode(' ', $ref);
       
    95         if (!is_numeric($refElements[0]) || !is_numeric($refElements[1]) || $refElements[2] != 'R') {
       
    96             require_once 'Zend/Pdf/Exception.php';
       
    97             throw new Zend_Pdf_Exception("Incorrect reference: '$ref'");
       
    98         }
       
    99         $objNum = (int)$refElements[0];
       
   100         $genNum = (int)$refElements[1];
       
   101 
       
   102         if ($inuse) {
       
   103             $this->_inuse[$ref]          = $offset;
       
   104             $this->_usedObjects[$objNum] = $objNum;
       
   105         } else {
       
   106             $this->_free[$ref]           = $offset;
       
   107             $this->_generations[$objNum] = $genNum;
       
   108         }
       
   109     }
       
   110 
       
   111 
       
   112     /**
       
   113      * Set parent reference table
       
   114      *
       
   115      * @param Zend_Pdf_Element_Reference_Table $parent
       
   116      */
       
   117     public function setParent(self $parent)
       
   118     {
       
   119         $this->_parent = $parent;
       
   120     }
       
   121 
       
   122 
       
   123     /**
       
   124      * Get object offset
       
   125      *
       
   126      * @param string $ref
       
   127      * @return integer
       
   128      */
       
   129     public function getOffset($ref)
       
   130     {
       
   131         if (isset($this->_inuse[$ref])) {
       
   132             return $this->_inuse[$ref];
       
   133         }
       
   134 
       
   135         if (isset($this->_free[$ref])) {
       
   136             return null;
       
   137         }
       
   138 
       
   139         if (isset($this->_parent)) {
       
   140             return $this->_parent->getOffset($ref);
       
   141         }
       
   142 
       
   143         return null;
       
   144     }
       
   145 
       
   146 
       
   147     /**
       
   148      * Get next object from a list of free objects.
       
   149      *
       
   150      * @param string $ref
       
   151      * @return integer
       
   152      * @throws Zend_Pdf_Exception
       
   153      */
       
   154     public function getNextFree($ref)
       
   155     {
       
   156         if (isset($this->_inuse[$ref])) {
       
   157             require_once 'Zend/Pdf/Exception.php';
       
   158             throw new Zend_Pdf_Exception('Object is not free');
       
   159         }
       
   160 
       
   161         if (isset($this->_free[$ref])) {
       
   162             return $this->_free[$ref];
       
   163         }
       
   164 
       
   165         if (isset($this->_parent)) {
       
   166             return $this->_parent->getNextFree($ref);
       
   167         }
       
   168 
       
   169         require_once 'Zend/Pdf/Exception.php';
       
   170         throw new Zend_Pdf_Exception('Object not found.');
       
   171     }
       
   172 
       
   173 
       
   174     /**
       
   175      * Get next generation number for free object
       
   176      *
       
   177      * @param integer $objNum
       
   178      * @return unknown
       
   179      */
       
   180     public function getNewGeneration($objNum)
       
   181     {
       
   182         if (isset($this->_usedObjects[$objNum])) {
       
   183             require_once 'Zend/Pdf/Exception.php';
       
   184             throw new Zend_Pdf_Exception('Object is not free');
       
   185         }
       
   186 
       
   187         if (isset($this->_generations[$objNum])) {
       
   188             return $this->_generations[$objNum];
       
   189         }
       
   190 
       
   191         if (isset($this->_parent)) {
       
   192             return $this->_parent->getNewGeneration($objNum);
       
   193         }
       
   194 
       
   195         require_once 'Zend/Pdf/Exception.php';
       
   196         throw new Zend_Pdf_Exception('Object not found.');
       
   197     }
       
   198 }