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.

154 lines
3.8 KiB

11 years ago
9 years ago
11 years ago
10 years ago
11 years ago
9 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. * @subpackage Actions
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /** Internally used classes */
  23. // require_once 'Zend/Pdf/Element.php';
  24. /**
  25. * PDF name tree representation class
  26. *
  27. * @todo implement lazy resource loading so resources will be really loaded at access time
  28. *
  29. * @package Zend_Pdf
  30. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Pdf_NameTree implements ArrayAccess, Iterator, Countable
  34. {
  35. /**
  36. * Elements
  37. * Array of name => object tree entries
  38. *
  39. * @var array
  40. */
  41. protected $_items = array();
  42. /**
  43. * Object constructor
  44. *
  45. * @param Zend_Pdf_Element $rootDictionary root of name dictionary
  46. */
  47. public function __construct(Zend_Pdf_Element $rootDictionary)
  48. {
  49. if ($rootDictionary->getType() != Zend_Pdf_Element::TYPE_DICTIONARY) {
  50. // require_once 'Zend/Pdf/Exception.php';
  51. throw new Zend_Pdf_Exception('Name tree root must be a dictionary.');
  52. }
  53. $intermediateNodes = array();
  54. $leafNodes = array();
  55. if ($rootDictionary->Kids !== null) {
  56. $intermediateNodes[] = $rootDictionary;
  57. } else {
  58. $leafNodes[] = $rootDictionary;
  59. }
  60. while (count($intermediateNodes) != 0) {
  61. $newIntermediateNodes = array();
  62. foreach ($intermediateNodes as $node) {
  63. foreach ($node->Kids->items as $childNode) {
  64. if ($childNode->Kids !== null) {
  65. $newIntermediateNodes[] = $childNode;
  66. } else {
  67. $leafNodes[] = $childNode;
  68. }
  69. }
  70. }
  71. $intermediateNodes = $newIntermediateNodes;
  72. }
  73. foreach ($leafNodes as $leafNode) {
  74. $destinationsCount = count($leafNode->Names->items)/2;
  75. for ($count = 0; $count < $destinationsCount; $count++) {
  76. $this->_items[$leafNode->Names->items[$count*2]->value] = $leafNode->Names->items[$count*2 + 1];
  77. }
  78. }
  79. }
  80. public function current()
  81. {
  82. return current($this->_items);
  83. }
  84. public function next()
  85. {
  86. return next($this->_items);
  87. }
  88. public function key()
  89. {
  90. return key($this->_items);
  91. }
  92. public function valid() {
  93. return current($this->_items)!==false;
  94. }
  95. public function rewind()
  96. {
  97. reset($this->_items);
  98. }
  99. public function offsetExists($offset)
  100. {
  101. return array_key_exists($offset, $this->_items);
  102. }
  103. public function offsetGet($offset)
  104. {
  105. return $this->_items[$offset];
  106. }
  107. public function offsetSet($offset, $value)
  108. {
  109. if ($offset === null) {
  110. $this->_items[] = $value;
  111. } else {
  112. $this->_items[$offset] = $value;
  113. }
  114. }
  115. public function offsetUnset($offset)
  116. {
  117. unset($this->_items[$offset]);
  118. }
  119. public function clear()
  120. {
  121. $this->_items = array();
  122. }
  123. public function count()
  124. {
  125. return count($this->_items);
  126. }
  127. }