Table.php 4.4 KB

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