forked from zendframework/ZendPdf
/
Markup.php
131 lines (120 loc) · 5.07 KB
/
Markup.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_PDF
* @subpackage Zend_PDF_Annotation
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Pdf\Annotation;
use Zend\Pdf\Exception;
use Zend\Pdf;
use Zend\Pdf\InternalType;
/**
* A markup annotation
*
* @package Zend_PDF
* @subpackage Zend_PDF_Annotation
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Markup extends AbstractAnnotation
{
/**
* Annotation subtypes
*/
const SUBTYPE_HIGHLIGHT = 'Highlight';
const SUBTYPE_UNDERLINE = 'Underline';
const SUBTYPE_SQUIGGLY = 'Squiggly';
const SUBTYPE_STRIKEOUT = 'StrikeOut';
/**
* Annotation object constructor
*
* @throws \Zend\Pdf\Exception
*/
public function __construct(InternalType\AbstractTypeObject $annotationDictionary)
{
if ($annotationDictionary->getType() != InternalType\AbstractTypeObject::TYPE_DICTIONARY) {
throw new Exception\CorruptedPdfException('Annotation dictionary resource has to be a dictionary.');
}
if ($annotationDictionary->Subtype === null ||
$annotationDictionary->Subtype->getType() != InternalType\AbstractTypeObject::TYPE_NAME ||
!in_array( $annotationDictionary->Subtype->value,
array(self::SUBTYPE_HIGHLIGHT,
self::SUBTYPE_UNDERLINE,
self::SUBTYPE_SQUIGGLY,
self::SUBTYPE_STRIKEOUT) )) {
throw new Exception\CorruptedPdfException('Subtype => Markup entry is omitted or has wrong value.');
}
parent::__construct($annotationDictionary);
}
/**
* Create markup annotation object
*
* Text markup annotations appear as highlights, underlines, strikeouts or
* jagged ("squiggly") underlines in the text of a document. When opened,
* they display a pop-up window containing the text of the associated note.
*
* $subType parameter may contain
* \Zend\Pdf\Annotation\Markup::SUBTYPE_HIGHLIGHT
* \Zend\Pdf\Annotation\Markup::SUBTYPE_UNDERLINE
* \Zend\Pdf\Annotation\Markup::SUBTYPE_SQUIGGLY
* \Zend\Pdf\Annotation\Markup::SUBTYPE_STRIKEOUT
* for for a highlight, underline, squiggly-underline, or strikeout annotation,
* respectively.
*
* $quadPoints is an array of 8xN numbers specifying the coordinates of
* N quadrilaterals default user space. Each quadrilateral encompasses a word or
* group of contiguous words in the text underlying the annotation.
* The coordinates for each quadrilateral are given in the order
* x1 y1 x2 y2 x3 y3 x4 y4
* specifying the quadrilateral’s four vertices in counterclockwise order
* starting from left bottom corner.
* The text is oriented with respect to the edge connecting points
* (x1, y1) and (x2, y2).
*
* @param float $x1
* @param float $y1
* @param float $x2
* @param float $y2
* @param string $text
* @param string $subType
* @param array $quadPoints [x1 y1 x2 y2 x3 y3 x4 y4]
* @return \Zend\Pdf\Annotation\Markup
* @throws \Zend\Pdf\Exception
*/
public static function create($x1, $y1, $x2, $y2, $text, $subType, $quadPoints)
{
$annotationDictionary = new InternalType\DictionaryObject();
$annotationDictionary->Type = new InternalType\NameObject('Annot');
$annotationDictionary->Subtype = new InternalType\NameObject($subType);
$rectangle = new InternalType\ArrayObject();
$rectangle->items[] = new InternalType\NumericObject($x1);
$rectangle->items[] = new InternalType\NumericObject($y1);
$rectangle->items[] = new InternalType\NumericObject($x2);
$rectangle->items[] = new InternalType\NumericObject($y2);
$annotationDictionary->Rect = $rectangle;
$annotationDictionary->Contents = new InternalType\StringObject($text);
if (!is_array($quadPoints) || count($quadPoints) == 0 || count($quadPoints) % 8 != 0) {
throw new Exception\InvalidArgumentException('$quadPoints parameter must be an array of 8xN numbers');
}
$points = new InternalType\ArrayObject();
foreach ($quadPoints as $quadPoint) {
$points->items[] = new InternalType\NumericObject($quadPoint);
}
$annotationDictionary->QuadPoints = $points;
return new self($annotationDictionary);
}
}