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.

373 lines
9.8 KiB

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