Created.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. * PDF outline representation class
  32. *
  33. * @todo Implement an ability to associate an outline item with a structure element (PDF 1.3 feature)
  34. *
  35. * @package Zend_Pdf
  36. * @subpackage Outlines
  37. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. */
  40. class Zend_Pdf_Outline_Created extends Zend_Pdf_Outline
  41. {
  42. /**
  43. * Outline title.
  44. *
  45. * @var string
  46. */
  47. protected $_title;
  48. /**
  49. * Color to be used for the outline entry’s text.
  50. * It uses the DeviceRGB color space for color representation.
  51. * Null means default value - black ([0.0 0.0 0.0] in RGB representation).
  52. *
  53. * @var Zend_Pdf_Color_Rgb
  54. */
  55. protected $_color = null;
  56. /**
  57. * True if outline item is displayed in italic.
  58. * Default value is false.
  59. *
  60. * @var boolean
  61. */
  62. protected $_italic = false;
  63. /**
  64. * True if outline item is displayed in bold.
  65. * Default value is false.
  66. *
  67. * @var boolean
  68. */
  69. protected $_bold = false;
  70. /**
  71. * Target destination or action.
  72. * String means named destination
  73. *
  74. * Null means no target.
  75. *
  76. * @var Zend_Pdf_Destination|Zend_Pdf_Action
  77. */
  78. protected $_target = null;
  79. /**
  80. * Get outline title.
  81. *
  82. * @return string
  83. */
  84. public function getTitle()
  85. {
  86. return $this->_title;
  87. }
  88. /**
  89. * Set outline title
  90. *
  91. * @param string $title
  92. * @return Zend_Pdf_Outline
  93. */
  94. public function setTitle($title)
  95. {
  96. $this->_title = $title;
  97. return $this;
  98. }
  99. /**
  100. * Returns true if outline item is displayed in italic
  101. *
  102. * @return boolean
  103. */
  104. public function isItalic()
  105. {
  106. return $this->_italic;
  107. }
  108. /**
  109. * Sets 'isItalic' outline flag
  110. *
  111. * @param boolean $isItalic
  112. * @return Zend_Pdf_Outline
  113. */
  114. public function setIsItalic($isItalic)
  115. {
  116. $this->_italic = $isItalic;
  117. return $this;
  118. }
  119. /**
  120. * Returns true if outline item is displayed in bold
  121. *
  122. * @return boolean
  123. */
  124. public function isBold()
  125. {
  126. return $this->_bold;
  127. }
  128. /**
  129. * Sets 'isBold' outline flag
  130. *
  131. * @param boolean $isBold
  132. * @return Zend_Pdf_Outline
  133. */
  134. public function setIsBold($isBold)
  135. {
  136. $this->_bold = $isBold;
  137. return $this;
  138. }
  139. /**
  140. * Get outline text color.
  141. *
  142. * @return Zend_Pdf_Color_Rgb
  143. */
  144. public function getColor()
  145. {
  146. return $this->_color;
  147. }
  148. /**
  149. * Set outline text color.
  150. * (null means default color which is black)
  151. *
  152. * @param Zend_Pdf_Color_Rgb $color
  153. * @return Zend_Pdf_Outline
  154. */
  155. public function setColor(Zend_Pdf_Color_Rgb $color)
  156. {
  157. $this->_color = $color;
  158. return $this;
  159. }
  160. /**
  161. * Get outline target.
  162. *
  163. * @return Zend_Pdf_Target
  164. */
  165. public function getTarget()
  166. {
  167. return $this->_target;
  168. }
  169. /**
  170. * Set outline target.
  171. * Null means no target
  172. *
  173. * @param Zend_Pdf_Target|string $target
  174. * @return Zend_Pdf_Outline
  175. * @throws Zend_Pdf_Exception
  176. */
  177. public function setTarget($target = null)
  178. {
  179. if (is_string($target)) {
  180. require_once 'Zend/Pdf/Destination/Named.php';
  181. $target = new Zend_Pdf_Destination_Named($target);
  182. }
  183. if ($target === null || $target instanceof Zend_Pdf_Target) {
  184. $this->_target = $target;
  185. } else {
  186. require_once 'Zend/Pdf/Exception.php';
  187. throw new Zend_Pdf_Exception('Outline target has to be Zend_Pdf_Destination or Zend_Pdf_Action object or string');
  188. }
  189. return $this;
  190. }
  191. /**
  192. * Object constructor
  193. *
  194. * @param array $options
  195. * @throws Zend_Pdf_Exception
  196. */
  197. public function __construct($options = array())
  198. {
  199. if (!isset($options['title'])) {
  200. require_once 'Zend/Pdf/Exception.php';
  201. throw new Zend_Pdf_Exception('Title parameter is required.');
  202. }
  203. $this->setOptions($options);
  204. }
  205. /**
  206. * Dump Outline and its child outlines into PDF structures
  207. *
  208. * Returns dictionary indirect object or reference
  209. *
  210. * @internal
  211. * @param Zend_Pdf_ElementFactory $factory object factory for newly created indirect objects
  212. * @param boolean $updateNavigation Update navigation flag
  213. * @param Zend_Pdf_Element $parent Parent outline dictionary reference
  214. * @param Zend_Pdf_Element $prev Previous outline dictionary reference
  215. * @param SplObjectStorage $processedOutlines List of already processed outlines
  216. * @return Zend_Pdf_Element
  217. * @throws Zend_Pdf_Exception
  218. */
  219. public function dumpOutline(Zend_Pdf_ElementFactory_Interface $factory,
  220. $updateNavigation,
  221. Zend_Pdf_Element $parent,
  222. Zend_Pdf_Element $prev = null,
  223. SplObjectStorage $processedOutlines = null)
  224. {
  225. if ($processedOutlines === null) {
  226. $processedOutlines = new SplObjectStorage();
  227. }
  228. $processedOutlines->attach($this);
  229. $outlineDictionary = $factory->newObject(new Zend_Pdf_Element_Dictionary());
  230. $outlineDictionary->Title = new Zend_Pdf_Element_String($this->getTitle());
  231. $target = $this->getTarget();
  232. if ($target === null) {
  233. // Do nothing
  234. } else if ($target instanceof Zend_Pdf_Destination) {
  235. $outlineDictionary->Dest = $target->getResource();
  236. } else if ($target instanceof Zend_Pdf_Action) {
  237. $outlineDictionary->A = $target->getResource();
  238. } else {
  239. require_once 'Zend/Pdf/Exception.php';
  240. throw new Zend_Pdf_Exception('Outline target has to be Zend_Pdf_Destination, Zend_Pdf_Action object or null');
  241. }
  242. $color = $this->getColor();
  243. if ($color !== null) {
  244. $components = $color->getComponents();
  245. $colorComponentElements = array(new Zend_Pdf_Element_Numeric($components[0]),
  246. new Zend_Pdf_Element_Numeric($components[1]),
  247. new Zend_Pdf_Element_Numeric($components[2]));
  248. $outlineDictionary->C = new Zend_Pdf_Element_Array($colorComponentElements);
  249. }
  250. if ($this->isItalic() || $this->isBold()) {
  251. $outlineDictionary->F = new Zend_Pdf_Element_Numeric(($this->isItalic()? 1 : 0) | // Bit 1 - Italic
  252. ($this->isBold()? 2 : 0)); // Bit 2 - Bold
  253. }
  254. $outlineDictionary->Parent = $parent;
  255. $outlineDictionary->Prev = $prev;
  256. $lastChild = null;
  257. foreach ($this->childOutlines as $childOutline) {
  258. if ($processedOutlines->contains($childOutline)) {
  259. require_once 'Zend/Pdf/Exception.php';
  260. throw new Zend_Pdf_Exception('Outlines cyclyc reference is detected.');
  261. }
  262. if ($lastChild === null) {
  263. $lastChild = $childOutline->dumpOutline($factory, true, $outlineDictionary, null, $processedOutlines);
  264. $outlineDictionary->First = $lastChild;
  265. } else {
  266. $childOutlineDictionary = $childOutline->dumpOutline($factory, true, $outlineDictionary, $lastChild, $processedOutlines);
  267. $lastChild->Next = $childOutlineDictionary;
  268. $lastChild = $childOutlineDictionary;
  269. }
  270. }
  271. $outlineDictionary->Last = $lastChild;
  272. if (count($this->childOutlines) != 0) {
  273. $outlineDictionary->Count = new Zend_Pdf_Element_Numeric(($this->isOpen()? 1 : -1)*count($this->childOutlines));
  274. }
  275. return $outlineDictionary;
  276. }
  277. }