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