2
0

Outline.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. /**
  25. * Abstract PDF outline representation class
  26. *
  27. * @todo Implement an ability to associate an outline item with a structure element (PDF 1.3 feature)
  28. *
  29. * @package Zend_Pdf
  30. * @subpackage Outlines
  31. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. abstract class Zend_Pdf_Outline implements RecursiveIterator, Countable
  35. {
  36. /**
  37. * True if outline is open.
  38. *
  39. * @var boolean
  40. */
  41. protected $_open = false;
  42. /**
  43. * Array of child outlines (array of Zend_Pdf_Outline objects)
  44. *
  45. * @var array
  46. */
  47. public $childOutlines = array();
  48. /**
  49. * Get outline title.
  50. *
  51. * @return string
  52. */
  53. abstract public function getTitle();
  54. /**
  55. * Set outline title
  56. *
  57. * @param string $title
  58. * @return Zend_Pdf_Outline
  59. */
  60. abstract public function setTitle($title);
  61. /**
  62. * Returns true if outline item is open by default
  63. *
  64. * @return boolean
  65. */
  66. public function isOpen()
  67. {
  68. return $this->_open;
  69. }
  70. /**
  71. * Sets 'isOpen' outline flag
  72. *
  73. * @param boolean $isOpen
  74. * @return Zend_Pdf_Outline
  75. */
  76. public function setIsOpen($isOpen)
  77. {
  78. $this->_open = $isOpen;
  79. return $this;
  80. }
  81. /**
  82. * Returns true if outline item is displayed in italic
  83. *
  84. * @return boolean
  85. */
  86. abstract public function isItalic();
  87. /**
  88. * Sets 'isItalic' outline flag
  89. *
  90. * @param boolean $isItalic
  91. * @return Zend_Pdf_Outline
  92. */
  93. abstract public function setIsItalic($isItalic);
  94. /**
  95. * Returns true if outline item is displayed in bold
  96. *
  97. * @return boolean
  98. */
  99. abstract public function isBold();
  100. /**
  101. * Sets 'isBold' outline flag
  102. *
  103. * @param boolean $isBold
  104. * @return Zend_Pdf_Outline
  105. */
  106. abstract public function setIsBold($isBold);
  107. /**
  108. * Get outline text color.
  109. *
  110. * @return Zend_Pdf_Color_Rgb
  111. */
  112. abstract public function getColor();
  113. /**
  114. * Set outline text color.
  115. * (null means default color which is black)
  116. *
  117. * @param Zend_Pdf_Color_Rgb $color
  118. * @return Zend_Pdf_Outline
  119. */
  120. abstract public function setColor(Zend_Pdf_Color_Rgb $color);
  121. /**
  122. * Get outline target.
  123. *
  124. * @return Zend_Pdf_Target
  125. */
  126. abstract public function getTarget();
  127. /**
  128. * Set outline target.
  129. * Null means no target
  130. *
  131. * @param Zend_Pdf_Target|string $target
  132. * @return Zend_Pdf_Outline
  133. */
  134. abstract public function setTarget($target = null);
  135. /**
  136. * Get outline options
  137. *
  138. * @return array
  139. */
  140. public function getOptions()
  141. {
  142. return array('title' => $this->_title,
  143. 'open' => $this->_open,
  144. 'color' => $this->_color,
  145. 'italic' => $this->_italic,
  146. 'bold' => $this->_bold,
  147. 'target' => $this->_target);
  148. }
  149. /**
  150. * Set outline options
  151. *
  152. * @param array $options
  153. * @return Zend_Pdf_Actions
  154. * @throws Zend_Pdf_Exception
  155. */
  156. public function setOptions(array $options)
  157. {
  158. foreach ($options as $key => $value) {
  159. switch ($key) {
  160. case 'title':
  161. $this->setTitle($value);
  162. break;
  163. case 'open':
  164. $this->setIsOpen($value);
  165. break;
  166. case 'color':
  167. $this->setColor($value);
  168. break;
  169. case 'italic':
  170. $this->setIsItalic($value);
  171. break;
  172. case 'bold':
  173. $this->setIsBold($value);
  174. break;
  175. case 'target':
  176. $this->setTarget($value);
  177. break;
  178. default:
  179. require_once 'Zend/Pdf/Exception.php';
  180. throw new Zend_Pdf_Exception("Unknown option name - '$key'.");
  181. break;
  182. }
  183. }
  184. return $this;
  185. }
  186. /**
  187. * Create new Outline object
  188. *
  189. * It provides two forms of input parameters:
  190. *
  191. * 1. Zend_Pdf_Outline::create(string $title[, Zend_Pdf_Target $target])
  192. * 2. Zend_Pdf_Outline::create(array $options)
  193. *
  194. * Second form allows to provide outline options as an array.
  195. * The followed options are supported:
  196. * 'title' - string, outline title, required
  197. * 'open' - boolean, true if outline entry is open (default value is false)
  198. * 'color' - Zend_Pdf_Color_Rgb object, true if outline entry is open (default value is null - black)
  199. * 'italic' - boolean, true if outline entry is displayed in italic (default value is false)
  200. * 'bold' - boolean, true if outline entry is displayed in bold (default value is false)
  201. * 'target' - Zend_Pdf_Target object or string, outline item destination
  202. *
  203. * @return Zend_Pdf_Outline
  204. * @throws Zend_Pdf_Exception
  205. */
  206. public static function create($param1, $param2 = null)
  207. {
  208. if (is_string($param1)) {
  209. if ($param2 !== null && !($param2 instanceof Zend_Pdf_Target || is_string($param2))) {
  210. require_once 'Zend/Pdf/Exception.php';
  211. throw new Zend_Pdf_Exception('Outline create method takes $title (string) and $target (Zend_Pdf_Target or string) or an array as an input');
  212. }
  213. require_once 'Zend/Pdf/Outline/Created.php';
  214. return new Zend_Pdf_Outline_Created(array('title' => $param1,
  215. 'target' => $param2));
  216. } else {
  217. if (!is_array($param1) || $param2 !== null) {
  218. require_once 'Zend/Pdf/Exception.php';
  219. throw new Zend_Pdf_Exception('Outline create method takes $title (string) and $destination (Zend_Pdf_Destination) or an array as an input');
  220. }
  221. return new Zend_Pdf_Outline_Created($param1);
  222. }
  223. }
  224. /**
  225. * Returns number of the total number of open items at all levels of the outline.
  226. *
  227. * @internal
  228. * @return integer
  229. */
  230. public function openOutlinesCount()
  231. {
  232. $count = 1; // Include this outline
  233. if ($this->isOpen()) {
  234. foreach ($this->childOutlines as $child) {
  235. $count += $child->openOutlinesCount();
  236. }
  237. }
  238. return $count;
  239. }
  240. /**
  241. * Dump Outline and its child outlines into PDF structures
  242. *
  243. * Returns dictionary indirect object or reference
  244. *
  245. * @param Zend_Pdf_ElementFactory $factory object factory for newly created indirect objects
  246. * @param boolean $updateNavigation Update navigation flag
  247. * @param Zend_Pdf_Element $parent Parent outline dictionary reference
  248. * @param Zend_Pdf_Element $prev Previous outline dictionary reference
  249. * @param SplObjectStorage $processedOutlines List of already processed outlines
  250. * @return Zend_Pdf_Element
  251. */
  252. abstract public function dumpOutline(Zend_Pdf_ElementFactory_Interface $factory,
  253. $updateNavigation,
  254. Zend_Pdf_Element $parent,
  255. Zend_Pdf_Element $prev = null,
  256. SplObjectStorage $processedOutlines = null);
  257. ////////////////////////////////////////////////////////////////////////
  258. // RecursiveIterator interface methods
  259. //////////////
  260. /**
  261. * Returns the child outline.
  262. *
  263. * @return Zend_Pdf_Outline
  264. */
  265. public function current()
  266. {
  267. return current($this->childOutlines);
  268. }
  269. /**
  270. * Returns current iterator key
  271. *
  272. * @return integer
  273. */
  274. public function key()
  275. {
  276. return key($this->childOutlines);
  277. }
  278. /**
  279. * Go to next child
  280. */
  281. public function next()
  282. {
  283. return next($this->childOutlines);
  284. }
  285. /**
  286. * Rewind children
  287. */
  288. public function rewind()
  289. {
  290. return reset($this->childOutlines);
  291. }
  292. /**
  293. * Check if current position is valid
  294. *
  295. * @return boolean
  296. */
  297. public function valid()
  298. {
  299. return current($this->childOutlines) !== false;
  300. }
  301. /**
  302. * Returns the child outline.
  303. *
  304. * @return Zend_Pdf_Outline|null
  305. */
  306. public function getChildren()
  307. {
  308. return current($this->childOutlines);
  309. }
  310. /**
  311. * Implements RecursiveIterator interface.
  312. *
  313. * @return bool whether container has any pages
  314. */
  315. public function hasChildren()
  316. {
  317. return count($this->childOutlines) > 0;
  318. }
  319. ////////////////////////////////////////////////////////////////////////
  320. // Countable interface methods
  321. //////////////
  322. /**
  323. * count()
  324. *
  325. * @return int
  326. */
  327. public function count()
  328. {
  329. return count($this->childOutlines);
  330. }
  331. }