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