Loaded.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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-2009 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. /** Zend_Pdf_ElementFactory */
  23. require_once 'Zend/Pdf/ElementFactory.php';
  24. /** Zend_Pdf_Outline */
  25. require_once 'Zend/Pdf/Outline.php';
  26. /** Zend_Pdf_Destination */
  27. require_once 'Zend/Pdf/Destination.php';
  28. /** Zend_Pdf_Action */
  29. require_once 'Zend/Pdf/Action.php';
  30. /**
  31. * Traceable PDF outline representation class
  32. *
  33. * Instances of this class trace object update uperations. That allows to avoid outlines PDF tree update
  34. * which should be performed at each document update otherwise.
  35. *
  36. * @package Zend_Pdf
  37. * @subpackage Outlines
  38. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. */
  41. class Zend_Pdf_Outline_Loaded extends Zend_Pdf_Outline
  42. {
  43. /**
  44. * Outline dictionary object
  45. *
  46. * @var Zend_Pdf_Element_Dictionary|Zend_Pdf_Element_Object|Zend_Pdf_Element_Reference
  47. */
  48. protected $_outlineDictionary;
  49. /**
  50. * original array of child outlines
  51. *
  52. * @var array
  53. */
  54. protected $_originalChildOutlines = array();
  55. /**
  56. * Get outline title.
  57. *
  58. * @return string
  59. * @throws Zend_Pdf_Exception
  60. */
  61. public function getTitle()
  62. {
  63. if ($this->_outlineDictionary->Title === null) {
  64. require_once 'Zend/Pdf/Exception.php';
  65. throw new Zend_Pdf_Exception('Outline dictionary Title entry is required.');
  66. }
  67. return $this->_outlineDictionary->Title->value;
  68. }
  69. /**
  70. * Set outline title
  71. *
  72. * @param string $title
  73. * @return Zend_Pdf_Outline
  74. */
  75. public function setTitle($title)
  76. {
  77. $this->_outlineDictionary->Title->touch();
  78. $this->_outlineDictionary->Title = new Zend_Pdf_Element_String($title);
  79. return $this;
  80. }
  81. /**
  82. * Sets 'isOpen' outline flag
  83. *
  84. * @param boolean $isOpen
  85. * @return Zend_Pdf_Outline
  86. */
  87. public function setIsOpen($isOpen)
  88. {
  89. parent::setIsOpen($isOpen);
  90. if ($this->_outlineDictionary->Count === null) {
  91. // Do Nothing.
  92. return this;
  93. }
  94. $childrenCount = $this->_outlineDictionary->Count->value;
  95. $isOpenCurrentState = ($childrenCount > 0);
  96. if ($isOpen != $isOpenCurrentState) {
  97. $this->_outlineDictionary->Count->touch();
  98. $this->_outlineDictionary->Count->value = ($isOpen? 1 : -1)*abs($childrenCount);
  99. }
  100. return $this;
  101. }
  102. /**
  103. * Returns true if outline item is displayed in italic
  104. *
  105. * @return boolean
  106. */
  107. public function isItalic()
  108. {
  109. if ($this->_outlineDictionary->F === null) {
  110. return false;
  111. }
  112. return $this->_outlineDictionary->F->value & 1;
  113. }
  114. /**
  115. * Sets 'isItalic' outline flag
  116. *
  117. * @param boolean $isItalic
  118. * @return Zend_Pdf_Outline
  119. */
  120. public function setIsItalic($isItalic)
  121. {
  122. if ($this->_outlineDictionary->F === null) {
  123. $this->_outlineDictionary->touch();
  124. $this->_outlineDictionary->F = new Zend_Pdf_Element_Numeric($isItalic? 1 : 0);
  125. } else {
  126. $this->_outlineDictionary->F->touch();
  127. if ($isItalic) {
  128. $this->_outlineDictionary->F->value = $this->_outlineDictionary->F->value | 1;
  129. } else {
  130. $this->_outlineDictionary->F->value = $this->_outlineDictionary->F->value | ~1;
  131. }
  132. }
  133. return $this;
  134. }
  135. /**
  136. * Returns true if outline item is displayed in bold
  137. *
  138. * @return boolean
  139. */
  140. public function isBold()
  141. {
  142. if ($this->_outlineDictionary->F === null) {
  143. return false;
  144. }
  145. return $this->_outlineDictionary->F->value & 2;
  146. }
  147. /**
  148. * Sets 'isBold' outline flag
  149. *
  150. * @param boolean $isBold
  151. * @return Zend_Pdf_Outline
  152. */
  153. public function setIsBold($isBold)
  154. {
  155. if ($this->_outlineDictionary->F === null) {
  156. $this->_outlineDictionary->touch();
  157. $this->_outlineDictionary->F = new Zend_Pdf_Element_Numeric($isBold? 2 : 0);
  158. } else {
  159. $this->_outlineDictionary->F->touch();
  160. if ($isBold) {
  161. $this->_outlineDictionary->F->value = $this->_outlineDictionary->F->value | 2;
  162. } else {
  163. $this->_outlineDictionary->F->value = $this->_outlineDictionary->F->value | ~2;
  164. }
  165. }
  166. return $this;
  167. }
  168. /**
  169. * Get outline text color.
  170. *
  171. * @return Zend_Pdf_Color_Rgb
  172. */
  173. public function getColor()
  174. {
  175. if ($this->_outlineDictionary->C === null) {
  176. return null;
  177. }
  178. $components = $this->_outlineDictionary->C->items;
  179. require_once 'Zend/Pdf/Color/Rgb.php';
  180. return new Zend_Pdf_Color_Rgb($components[0], $components[1], $components[2]);
  181. }
  182. /**
  183. * Set outline text color.
  184. * (null means default color which is black)
  185. *
  186. * @param Zend_Pdf_Color_Rgb $color
  187. * @return Zend_Pdf_Outline
  188. */
  189. public function setColor(Zend_Pdf_Color_Rgb $color)
  190. {
  191. $this->_outlineDictionary->touch();
  192. if ($color === null) {
  193. $this->_outlineDictionary->C = null;
  194. } else {
  195. $components = $color->getComponents();
  196. $colorComponentElements = array(new Zend_Pdf_Element_Numeric($components[0]),
  197. new Zend_Pdf_Element_Numeric($components[1]),
  198. new Zend_Pdf_Element_Numeric($components[2]));
  199. $this->_outlineDictionary->C = new Zend_Pdf_Element_Array($colorComponentElements);
  200. }
  201. return $this;
  202. }
  203. /**
  204. * Get outline target.
  205. *
  206. * @return Zend_Pdf_Target
  207. * @throws Zend_Pdf_Exception
  208. */
  209. public function getTarget()
  210. {
  211. if ($this->_outlineDictionary->Dest !== null) {
  212. if ($this->_outlineDictionary->A !== null) {
  213. require_once 'Zend/Pdf/Exception.php';
  214. throw new Zend_Pdf_Exception('Outline dictionary may contain Dest or A entry, but not both.');
  215. }
  216. return Zend_Pdf_Destination::load($this->_outlineDictionary->Dest);
  217. } else if ($this->_outlineDictionary->A !== null) {
  218. return Zend_Pdf_Action::load($this->_outlineDictionary->A);
  219. }
  220. return null;
  221. }
  222. /**
  223. * Set outline target.
  224. * Null means no target
  225. *
  226. * @param Zend_Pdf_Target|string $target
  227. * @return Zend_Pdf_Outline
  228. * @throws Zend_Pdf_Exception
  229. */
  230. public function setTarget($target = null)
  231. {
  232. $this->_outlineDictionary->touch();
  233. if (is_string($target)) {
  234. require_once 'Zend/Pdf/Destination/Named.php';
  235. $target = Zend_Pdf_Destination_Named::create($target);
  236. }
  237. if ($target === null) {
  238. $this->_outlineDictionary->Dest = null;
  239. $this->_outlineDictionary->A = null;
  240. } else if ($target instanceof Zend_Pdf_Destination) {
  241. $this->_outlineDictionary->Dest = $target->getResource();
  242. $this->_outlineDictionary->A = null;
  243. } else if ($target instanceof Zend_Pdf_Action) {
  244. $this->_outlineDictionary->Dest = null;
  245. $this->_outlineDictionary->A = $target->getResource();
  246. } else {
  247. require_once 'Zend/Pdf/Exception.php';
  248. throw new Zend_Pdf_Exception('Outline target has to be Zend_Pdf_Destination or Zend_Pdf_Action object or string');
  249. }
  250. return $this;
  251. }
  252. /**
  253. * Set outline options
  254. *
  255. * @param array $options
  256. * @return Zend_Pdf_Actions_Traceable
  257. * @throws Zend_Pdf_Exception
  258. */
  259. public function setOptions(array $options)
  260. {
  261. parent::setOptions($options);
  262. return $this;
  263. }
  264. /**
  265. * Create PDF outline object using specified dictionary
  266. *
  267. * @internal
  268. * @param Zend_Pdf_Element $dictionary (It's actually Dictionary or Dictionary Object or Reference to a Dictionary Object)
  269. * @param Zend_Pdf_Action $parentAction
  270. * @param SplObjectStorage $processedOutlines List of already processed Outline dictionaries,
  271. * used to avoid cyclic references
  272. * @return Zend_Pdf_Action
  273. * @throws Zend_Pdf_Exception
  274. */
  275. public function __construct(Zend_Pdf_Element $dictionary, SplObjectStorage $processedDictionaries = null)
  276. {
  277. if ($dictionary->getType() != Zend_Pdf_Element::TYPE_DICTIONARY) {
  278. require_once 'Zend/Pdf/Exception.php';
  279. throw new Zend_Pdf_Exception('$dictionary mast be an indirect dictionary object.');
  280. }
  281. if ($processedDictionaries === null) {
  282. $processedDictionaries = new SplObjectStorage();
  283. }
  284. $processedDictionaries->attach($dictionary);
  285. $this->_outlineDictionary = $dictionary;
  286. if ($dictionary->Count !== null) {
  287. if ($dictionary->Count->getType() != Zend_Pdf_Element::TYPE_NUMERIC) {
  288. require_once 'Zend/Pdf/Exception.php';
  289. throw new Zend_Pdf_Exception('Outline dictionary Count entry must be a numeric element.');
  290. }
  291. $childOutlinesCount = $dictionary->Count->value;
  292. if ($childOutlinesCount > 0) {
  293. $this->_open = true;
  294. }
  295. $childOutlinesCount = abs($childOutlinesCount);
  296. $childDictionary = $dictionary->First;
  297. for ($count = 0; $count < $childOutlinesCount; $count++) {
  298. if ($childDictionary === null) {
  299. require_once 'Zend/Pdf/Exception.php';
  300. throw new Zend_Pdf_Exception('Outline childs load error.');
  301. }
  302. if (!$processedDictionaries->contains($childDictionary)) {
  303. $this->childOutlines[] = new Zend_Pdf_Outline_Loaded($childDictionary, $processedDictionaries);
  304. }
  305. $childDictionary = $childDictionary->Next;
  306. }
  307. if ($childDictionary !== null) {
  308. require_once 'Zend/Pdf/Exception.php';
  309. throw new Zend_Pdf_Exception('Outline childs load error.');
  310. }
  311. $this->_originalChildOutlines = $this->childOutlines;
  312. }
  313. }
  314. /**
  315. * Dump Outline and its child outlines into PDF structures
  316. *
  317. * Returns dictionary indirect object or reference
  318. *
  319. * @internal
  320. * @param Zend_Pdf_ElementFactory $factory object factory for newly created indirect objects
  321. * @param boolean $updateNavigation Update navigation flag
  322. * @param Zend_Pdf_Element $parent Parent outline dictionary reference
  323. * @param Zend_Pdf_Element $prev Previous outline dictionary reference
  324. * @param SplObjectStorage $processedOutlines List of already processed outlines
  325. * @return Zend_Pdf_Element
  326. * @throws Zend_Pdf_Exception
  327. */
  328. public function dumpOutline(Zend_Pdf_ElementFactory_Interface $factory,
  329. $updateNavigation,
  330. Zend_Pdf_Element $parent,
  331. Zend_Pdf_Element $prev = null,
  332. SplObjectStorage $processedOutlines = null)
  333. {
  334. if ($processedOutlines === null) {
  335. $processedOutlines = new SplObjectStorage();
  336. }
  337. $processedOutlines->attach($this);
  338. if ($updateNavigation) {
  339. $this->_outlineDictionary->touch();
  340. $this->_outlineDictionary->Parent = $parent;
  341. $this->_outlineDictionary->Prev = $prev;
  342. $this->_outlineDictionary->Next = null;
  343. }
  344. $updateChildNavigation = false;
  345. if (count($this->_originalChildOutlines) != count($this->childOutlines)) {
  346. // If original and current children arrays have different size then children list was updated
  347. $updateChildNavigation = true;
  348. } else if ( !(array_keys($this->_originalChildOutlines) === array_keys($this->childOutlines)) ) {
  349. // If original and current children arrays have different keys (with a glance to an order) then children list was updated
  350. $updateChildNavigation = true;
  351. } else {
  352. foreach ($this->childOutlines as $key => $childOutline) {
  353. if ($this->_originalChildOutlines[$key] !== $childOutline) {
  354. $updateChildNavigation = true;
  355. break;
  356. }
  357. }
  358. }
  359. $lastChild = null;
  360. if ($updateChildNavigation) {
  361. $this->_outlineDictionary->touch();
  362. $this->_outlineDictionary->First = null;
  363. foreach ($this->childOutlines as $childOutline) {
  364. if ($processedOutlines->contains($childOutline)) {
  365. require_once 'Zend/Pdf/Exception.php';
  366. throw new Zend_Pdf_Exception('Outlines cyclyc reference is detected.');
  367. }
  368. if ($lastChild === null) {
  369. // First pass. Update Outlines dictionary First entry using corresponding value
  370. $lastChild = $childOutline->dumpOutline($factory, $updateChildNavigation, $this->_outlineDictionary, null, $processedOutlines);
  371. $this->_outlineDictionary->First = $lastChild;
  372. } else {
  373. // Update previous outline dictionary Next entry (Prev is updated within dumpOutline() method)
  374. $childOutlineDictionary = $childOutline->dumpOutline($factory, $updateChildNavigation, $this->_outlineDictionary, $lastChild, $processedOutlines);
  375. $lastChild->Next = $childOutlineDictionary;
  376. $lastChild = $childOutlineDictionary;
  377. }
  378. }
  379. $this->_outlineDictionary->Last = $lastChild;
  380. if (count($this->childOutlines) != 0) {
  381. $this->_outlineDictionary->Count = new Zend_Pdf_Element_Numeric(($this->isOpen()? 1 : -1)*count($this->childOutlines));
  382. } else {
  383. $this->_outlineDictionary->Count = null;
  384. }
  385. } else {
  386. foreach ($this->childOutlines as $childOutline) {
  387. if ($processedOutlines->contains($childOutline)) {
  388. require_once 'Zend/Pdf/Exception.php';
  389. throw new Zend_Pdf_Exception('Outlines cyclyc reference is detected.');
  390. }
  391. $lastChild = $childOutline->dumpOutline($factory, $updateChildNavigation, $this->_outlineDictionary, $lastChild, $processedOutlines);
  392. }
  393. }
  394. return $this->_outlineDictionary;
  395. }
  396. public function dump($level = 0)
  397. {
  398. printf(":%3d:%s:%s:%s%s :\n", count($this->childOutlines),$this->isItalic()? 'i':' ', $this->isBold()? 'b':' ', str_pad('', 4*$level), $this->getTitle());
  399. if ($this->isOpen() || true) {
  400. foreach ($this->childOutlines as $child) {
  401. $child->dump($level + 1);
  402. }
  403. }
  404. }
  405. }