Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
46 / 46 |
|
100.00% |
9 / 9 |
CRAP | |
100.00% |
1 / 1 |
Button | |
100.00% |
46 / 46 |
|
100.00% |
9 / 9 |
24 | |
100.00% |
1 / 1 |
addOption | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
hasOptions | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setNoToggleToOff | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
setRadio | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
setPushButton | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
setRadiosInUnison | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
isRadio | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isPushButton | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getStream | |
100.00% |
25 / 25 |
|
100.00% |
1 / 1 |
12 |
1 | <?php |
2 | /** |
3 | * Pop PHP Framework (http://www.popphp.org/) |
4 | * |
5 | * @link https://github.com/popphp/popphp-framework |
6 | * @author Nick Sagona, III <dev@nolainteractive.com> |
7 | * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
8 | * @license http://www.popphp.org/license New BSD License |
9 | */ |
10 | |
11 | /** |
12 | * @namespace |
13 | */ |
14 | namespace Pop\Pdf\Document\Page\Field; |
15 | |
16 | use Pop\Color\Color; |
17 | |
18 | /** |
19 | * Pdf page button field class |
20 | * |
21 | * @category Pop |
22 | * @package Pop\Pdf |
23 | * @author Nick Sagona, III <dev@nolainteractive.com> |
24 | * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
25 | * @license http://www.popphp.org/license New BSD License |
26 | * @version 5.0.0 |
27 | */ |
28 | class Button extends AbstractField |
29 | { |
30 | |
31 | /** |
32 | * Field options |
33 | * @var array |
34 | */ |
35 | protected array $options = []; |
36 | |
37 | /** |
38 | * Add an option |
39 | * |
40 | * @param string $option |
41 | * @param int $xOffset |
42 | * @param int $yOffset |
43 | * @return Button |
44 | */ |
45 | public function addOption(string $option, int $xOffset = 0, int $yOffset = 0): Button |
46 | { |
47 | $this->options[] = [ |
48 | 'option' => $option, |
49 | 'xOffset' => $xOffset, |
50 | 'yOffset' => $yOffset |
51 | ]; |
52 | |
53 | return $this; |
54 | } |
55 | |
56 | /** |
57 | * Has options |
58 | * |
59 | * @return bool |
60 | */ |
61 | public function hasOptions(): bool |
62 | { |
63 | return (count($this->options) > 0); |
64 | } |
65 | |
66 | /** |
67 | * Set no toggle to off |
68 | * |
69 | * @return Button |
70 | */ |
71 | public function setNoToggleToOff(): Button |
72 | { |
73 | if (!in_array(15, $this->flagBits)) { |
74 | $this->flagBits[] = 15; |
75 | } |
76 | return $this; |
77 | } |
78 | |
79 | /** |
80 | * Set radio |
81 | * |
82 | * @return Button |
83 | */ |
84 | public function setRadio(): Button |
85 | { |
86 | if (!in_array(16, $this->flagBits)) { |
87 | $this->flagBits[] = 16; |
88 | } |
89 | return $this; |
90 | } |
91 | |
92 | /** |
93 | * Set push button |
94 | * |
95 | * @return Button |
96 | */ |
97 | public function setPushButton(): Button |
98 | { |
99 | if (!in_array(17, $this->flagBits)) { |
100 | $this->flagBits[] = 17; |
101 | } |
102 | return $this; |
103 | } |
104 | |
105 | /** |
106 | * Set radios in unison |
107 | * |
108 | * @return Button |
109 | */ |
110 | public function setRadiosInUnison(): Button |
111 | { |
112 | if (!in_array(26, $this->flagBits)) { |
113 | $this->flagBits[] = 26; |
114 | } |
115 | return $this; |
116 | } |
117 | |
118 | /** |
119 | * Is radio |
120 | * |
121 | * @return bool |
122 | */ |
123 | public function isRadio(): bool |
124 | { |
125 | return in_array(16, $this->flagBits); |
126 | } |
127 | |
128 | /** |
129 | * Is push button |
130 | * |
131 | * @return bool |
132 | */ |
133 | public function isPushButton(): bool |
134 | { |
135 | return in_array(17, $this->flagBits); |
136 | } |
137 | |
138 | /** |
139 | * Get the field stream |
140 | * |
141 | * @param int $i |
142 | * @param int $pageIndex |
143 | * @param ?string $fontReference |
144 | * @param int $x |
145 | * @param int $y |
146 | * @return string |
147 | */ |
148 | public function getStream(int $i, int $pageIndex, ?string $fontReference, int $x, int $y): string |
149 | { |
150 | $text = null; |
151 | $options = null; |
152 | $color = '0 g'; |
153 | |
154 | if ($this->fontColor !== null) { |
155 | if ($this->fontColor instanceof Color\Rgb) { |
156 | $color = $this->fontColor->render(Color\Rgb::PERCENT) . " rg"; |
157 | } else if ($this->fontColor instanceof Color\Cmyk) { |
158 | $color = $this->fontColor->render(Color\Cmyk::PERCENT) . " k"; |
159 | } else if ($this->fontColor instanceof Color\Grayscale) { |
160 | $color = $this->fontColor->render(Color\Grayscale::PERCENT) . " g"; |
161 | } |
162 | } |
163 | |
164 | if ($fontReference !== null) { |
165 | $fontReference = substr($fontReference, 0, strpos($fontReference, ' ')); |
166 | $text = ' /DA(' . $fontReference . ' ' . $this->size . ' Tf ' . $color . ')'; |
167 | } |
168 | |
169 | $name = ($this->name !== null) ? ' /T(' . $this->name . ')/TU(' . $this->name . ')/TM(' . $this->name . ')' : ''; |
170 | $flags = (count($this->flagBits) > 0) ? "\n /Ff " . $this->getFlags() . "\n" : null; |
171 | $value = ($this->value !== null) ? "\n /V " . $this->value . "\n" : null; |
172 | $default = ($this->defaultValue !== null) ? "\n /DV " . $this->defaultValue . "\n" : null; |
173 | |
174 | if (count($this->options) > 0) { |
175 | $options = " /Opt [ "; |
176 | foreach ($this->options as $option) { |
177 | $options .= '(' . $option['option'] . ') '; |
178 | } |
179 | $options .= "]\n"; |
180 | } |
181 | |
182 | // Return the stream |
183 | return "{$i} 0 obj\n<<\n /Type /Annot\n /Subtype /Widget\n /FT /Btn\n /Rect [{$x} {$y} " . |
184 | ($this->width + $x) . " " . ($this->height + $y) . "]{$value}{$default}\n /P {$pageIndex} 0 R\n" . |
185 | " \n{$text}\n{$name}\n{$flags}\n{$options}>>\nendobj\n\n"; |
186 | } |
187 | |
188 | } |