Loaded.php 14 KB

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