You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
4.7 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  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-2014 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * PDF file reference table
  23. *
  24. * @category Zend
  25. * @package Zend_Pdf
  26. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. class Zend_Pdf_Element_Reference_Table
  30. {
  31. /**
  32. * Parent reference table
  33. *
  34. * @var Zend_Pdf_Element_Reference_Table
  35. */
  36. private $_parent;
  37. /**
  38. * Free entries
  39. * 'reference' => next free object number
  40. *
  41. * @var array
  42. */
  43. private $_free;
  44. /**
  45. * Generation numbers for free objects.
  46. * Array: objNum => nextGeneration
  47. *
  48. * @var array
  49. */
  50. private $_generations;
  51. /**
  52. * In use entries
  53. * 'reference' => offset
  54. *
  55. * @var array
  56. */
  57. private $_inuse;
  58. /**
  59. * Generation numbers for free objects.
  60. * Array: objNum => objGeneration
  61. *
  62. * @var array
  63. */
  64. private $_usedObjects;
  65. /**
  66. * Object constructor
  67. */
  68. public function __construct()
  69. {
  70. $this->_parent = null;
  71. $this->_free = array(); $this->_generations = array();
  72. $this->_inuse = array(); $this->_usedObjects = array();
  73. }
  74. /**
  75. * Add reference to the reference table
  76. *
  77. * @param string $ref
  78. * @param integer $offset
  79. * @param boolean $inuse
  80. */
  81. public function addReference($ref, $offset, $inuse = true)
  82. {
  83. $refElements = explode(' ', $ref);
  84. if (!is_numeric($refElements[0]) || !is_numeric($refElements[1]) || $refElements[2] != 'R') {
  85. // require_once 'Zend/Pdf/Exception.php';
  86. throw new Zend_Pdf_Exception("Incorrect reference: '$ref'");
  87. }
  88. $objNum = (int)$refElements[0];
  89. $genNum = (int)$refElements[1];
  90. if ($inuse) {
  91. $this->_inuse[$ref] = $offset;
  92. $this->_usedObjects[$objNum] = $objNum;
  93. } else {
  94. $this->_free[$ref] = $offset;
  95. $this->_generations[$objNum] = $genNum;
  96. }
  97. }
  98. /**
  99. * Set parent reference table
  100. *
  101. * @param Zend_Pdf_Element_Reference_Table $parent
  102. */
  103. public function setParent(self $parent)
  104. {
  105. $this->_parent = $parent;
  106. }
  107. /**
  108. * Get object offset
  109. *
  110. * @param string $ref
  111. * @return integer
  112. */
  113. public function getOffset($ref)
  114. {
  115. if (isset($this->_inuse[$ref])) {
  116. return $this->_inuse[$ref];
  117. }
  118. if (isset($this->_free[$ref])) {
  119. return null;
  120. }
  121. if (isset($this->_parent)) {
  122. return $this->_parent->getOffset($ref);
  123. }
  124. return null;
  125. }
  126. /**
  127. * Get next object from a list of free objects.
  128. *
  129. * @param string $ref
  130. * @return integer
  131. * @throws Zend_Pdf_Exception
  132. */
  133. public function getNextFree($ref)
  134. {
  135. if (isset($this->_inuse[$ref])) {
  136. // require_once 'Zend/Pdf/Exception.php';
  137. throw new Zend_Pdf_Exception('Object is not free');
  138. }
  139. if (isset($this->_free[$ref])) {
  140. return $this->_free[$ref];
  141. }
  142. if (isset($this->_parent)) {
  143. return $this->_parent->getNextFree($ref);
  144. }
  145. // require_once 'Zend/Pdf/Exception.php';
  146. throw new Zend_Pdf_Exception('Object not found.');
  147. }
  148. /**
  149. * Get next generation number for free object
  150. *
  151. * @param integer $objNum
  152. * @return unknown
  153. */
  154. public function getNewGeneration($objNum)
  155. {
  156. if (isset($this->_usedObjects[$objNum])) {
  157. // require_once 'Zend/Pdf/Exception.php';
  158. throw new Zend_Pdf_Exception('Object is not free');
  159. }
  160. if (isset($this->_generations[$objNum])) {
  161. return $this->_generations[$objNum];
  162. }
  163. if (isset($this->_parent)) {
  164. return $this->_parent->getNewGeneration($objNum);
  165. }
  166. // require_once 'Zend/Pdf/Exception.php';
  167. throw new Zend_Pdf_Exception('Object not found.');
  168. }
  169. }