PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Pdf/Annotation/Link.php

https://bitbucket.org/dbaltas/zend-framework-1.x-on-git
PHP | 162 lines | 85 code | 17 blank | 60 comment | 16 complexity | f3001beba383c83285e8b500198da426 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT
  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 Annotation
  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: Link.php 24593 2012-01-05 20:35:02Z matthew $
  21. */
  22. /** Internally used classes */
  23. require_once 'Zend/Pdf/Element.php';
  24. require_once 'Zend/Pdf/Element/Array.php';
  25. require_once 'Zend/Pdf/Element/Dictionary.php';
  26. require_once 'Zend/Pdf/Element/Name.php';
  27. require_once 'Zend/Pdf/Element/Numeric.php';
  28. /** Zend_Pdf_Annotation */
  29. require_once 'Zend/Pdf/Annotation.php';
  30. /**
  31. * A link annotation represents either a hypertext link to a destination elsewhere in
  32. * the document or an action to be performed.
  33. *
  34. * Only destinations are used now since only GoTo action can be created by user
  35. * in current implementation.
  36. *
  37. * @package Zend_Pdf
  38. * @subpackage Annotation
  39. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  40. * @license http://framework.zend.com/license/new-bsd New BSD License
  41. */
  42. class Zend_Pdf_Annotation_Link extends Zend_Pdf_Annotation
  43. {
  44. /**
  45. * Annotation object constructor
  46. *
  47. * @throws Zend_Pdf_Exception
  48. */
  49. public function __construct(Zend_Pdf_Element $annotationDictionary)
  50. {
  51. if ($annotationDictionary->getType() != Zend_Pdf_Element::TYPE_DICTIONARY) {
  52. require_once 'Zend/Pdf/Exception.php';
  53. throw new Zend_Pdf_Exception('Annotation dictionary resource has to be a dictionary.');
  54. }
  55. if ($annotationDictionary->Subtype === null ||
  56. $annotationDictionary->Subtype->getType() != Zend_Pdf_Element::TYPE_NAME ||
  57. $annotationDictionary->Subtype->value != 'Link') {
  58. require_once 'Zend/Pdf/Exception.php';
  59. throw new Zend_Pdf_Exception('Subtype => Link entry is requires');
  60. }
  61. parent::__construct($annotationDictionary);
  62. }
  63. /**
  64. * Create link annotation object
  65. *
  66. * @param float $x1
  67. * @param float $y1
  68. * @param float $x2
  69. * @param float $y2
  70. * @param Zend_Pdf_Target|string $target
  71. * @return Zend_Pdf_Annotation_Link
  72. */
  73. public static function create($x1, $y1, $x2, $y2, $target)
  74. {
  75. if (is_string($target)) {
  76. require_once 'Zend/Pdf/Destination/Named.php';
  77. $destination = Zend_Pdf_Destination_Named::create($target);
  78. }
  79. if (!$target instanceof Zend_Pdf_Target) {
  80. require_once 'Zend/Pdf/Exception.php';
  81. throw new Zend_Pdf_Exception('$target parameter must be a Zend_Pdf_Target object or a string.');
  82. }
  83. $annotationDictionary = new Zend_Pdf_Element_Dictionary();
  84. $annotationDictionary->Type = new Zend_Pdf_Element_Name('Annot');
  85. $annotationDictionary->Subtype = new Zend_Pdf_Element_Name('Link');
  86. $rectangle = new Zend_Pdf_Element_Array();
  87. $rectangle->items[] = new Zend_Pdf_Element_Numeric($x1);
  88. $rectangle->items[] = new Zend_Pdf_Element_Numeric($y1);
  89. $rectangle->items[] = new Zend_Pdf_Element_Numeric($x2);
  90. $rectangle->items[] = new Zend_Pdf_Element_Numeric($y2);
  91. $annotationDictionary->Rect = $rectangle;
  92. if ($target instanceof Zend_Pdf_Destination) {
  93. $annotationDictionary->Dest = $target->getResource();
  94. } else {
  95. $annotationDictionary->A = $target->getResource();
  96. }
  97. return new Zend_Pdf_Annotation_Link($annotationDictionary);
  98. }
  99. /**
  100. * Set link annotation destination
  101. *
  102. * @param Zend_Pdf_Target|string $target
  103. * @return Zend_Pdf_Annotation_Link
  104. */
  105. public function setDestination($target)
  106. {
  107. if (is_string($target)) {
  108. require_once 'Zend/Pdf/Destination/Named.php';
  109. $destination = Zend_Pdf_Destination_Named::create($target);
  110. }
  111. if (!$target instanceof Zend_Pdf_Target) {
  112. require_once 'Zend/Pdf/Exception.php';
  113. throw new Zend_Pdf_Exception('$target parameter must be a Zend_Pdf_Target object or a string.');
  114. }
  115. $this->_annotationDictionary->touch();
  116. $this->_annotationDictionary->Dest = $destination->getResource();
  117. if ($target instanceof Zend_Pdf_Destination) {
  118. $this->_annotationDictionary->Dest = $target->getResource();
  119. $this->_annotationDictionary->A = null;
  120. } else {
  121. $this->_annotationDictionary->Dest = null;
  122. $this->_annotationDictionary->A = $target->getResource();
  123. }
  124. return $this;
  125. }
  126. /**
  127. * Get link annotation destination
  128. *
  129. * @return Zend_Pdf_Target|null
  130. */
  131. public function getDestination()
  132. {
  133. if ($this->_annotationDictionary->Dest === null &&
  134. $this->_annotationDictionary->A === null) {
  135. return null;
  136. }
  137. if ($this->_annotationDictionary->Dest !== null) {
  138. require_once 'Zend/Pdf/Destination.php';
  139. return Zend_Pdf_Destination::load($this->_annotationDictionary->Dest);
  140. } else {
  141. require_once 'Zend/Pdf/Action.php';
  142. return Zend_Pdf_Action::load($this->_annotationDictionary->A);
  143. }
  144. }
  145. }