blib/lib/PDF/Make/Field.pm

filter: Statementstmt 99.0% 107 / 108 Branchbran 95.4% 21 / 22 Conditioncond 100% 8 / 8 MC/DCmcdc 100% 4 / 4 Subroutinesub 97.5% 39 / 40 Podpod 100% 33 / 33 totaltotal 98.6% 212 / 215 CC 16 SCAR 6.9
CC
16
Cov
99%
add_radio_option
6.0
required
3.0
readonly
3.0
CRAP 2.0
« blib/lib/PDF/Make/Text/Word.pm ↑ Summary blib/lib/PDF/Make/Builder/Form/Field/Listbox.pm »
1 package PDF::Make::Field;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶no warnings qw(redefine prototype); # XS methods are overridden by Perl wrappers
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use PDF::Make;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use PDF::Make::Form;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 PDF::Make::Field - PDF form field for PDF::Make
12  
13 =head1 SYNOPSIS
14  
15 use PDF::Make;
16
17 my $pdf = PDF::Make->new();
18 my $page = $pdf->add_page(width => 612, height => 792);
19 my $form = $pdf->create_form();
20
21 # Create and configure a text field
22 my $field = $form->add_text_field(
23 name => 'name',
24 x => 100,
25 y => 700,
26 width => 200,
27 height => 20,
28 );
29
30 # Set properties
31 $field->set_value('John Doe');
32 $field->readonly(1);
33 $field->align_center();
34
35 # Add to page
36 $field->add_to_page($page);
37  
38 =head1 DESCRIPTION
39  
40 This class represents a PDF form field. Fields are created via the
41 L<PDF::Make::Form> methods like C<add_text_field>, C<add_checkbox>, etc.
42  
43 =head1 METHODS
44  
45 =cut
46  
47 # Constructor - internal use only
48 84 ▶sub _wrap {
Subroutine _wrap: called CC=1 SCAR=0.0
49 84 my ($class, $field, $form) = @_;
50 84 return bless {
51 _field => $field,
52 _form => $form,
53 }, $class;
54 }
55  
56 =head2 type
57  
58 my $type = $field->type();
59  
60 Return the field type as a string: 'text', 'button', 'choice', or 'signature'.
61  
62 =cut
63  
64 6 ▶sub type {
Subroutine type: called CC=1 SCAR=0.0
Pod documented
65 6 return $_[0]->{_field}->type();
66 }
67  
68 =head2 name
69  
70 my $name = $field->name();
71  
72 Return the partial field name.
73  
74 =cut
75  
76 7 ▶sub name {
Subroutine name: called CC=1 SCAR=0.0
Pod documented
77 7 return $_[0]->{_field}->name();
78 }
79  
80 =head2 full_name
81  
82 my $full_name = $field->full_name();
83  
84 Return the full field name (includes parent hierarchy).
85  
86 =cut
87  
88 1 ▶sub full_name {
Subroutine full_name: called CC=1 SCAR=0.0
Pod documented
89 1 return $_[0]->{_field}->full_name();
90 }
91  
92 =head2 value
93  
94 my $value = $field->value();
95 $field->value('new value');
96  
97 Get or set the field value.
98  
99 =cut
100  
101 17 ▶sub value {
Subroutine value: called CC=2 SCAR=6.9
Pod documented
102 17 my ($self, $val) = @_;
103 17 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 2 15 17
104 2 $self->{_field}->value($val);
105 2 return $self;
106 }
107 15 return $self->{_field}->value();
108 }
109  
110 =head2 set_value
111  
112 $field->set_value('value');
113  
114 Set the field value.
115  
116 =cut
117  
118 19 ▶sub set_value {
Subroutine set_value: called CC=1 SCAR=0.0
Pod documented
119 19 my ($self, $val) = @_;
120 19 $self->{_field}->set_value($val);
121 19 return $self;
122 }
123  
124 =head2 set_default_value
125  
126 $field->set_default_value('default');
127  
128 Set the default value (used when form is reset).
129  
130 =cut
131  
132 1 ▶sub set_default_value {
Subroutine set_default_value: called CC=1 SCAR=0.0
Pod documented
133 1 my ($self, $val) = @_;
134 1 $self->{_field}->set_default_value($val);
135 1 return $self;
136 }
137  
138 =head2 flags
139  
140 my $flags = $field->flags();
141  
142 Return the field flags as an integer.
143  
144 =cut
145  
146 7 ▶sub flags {
Subroutine flags: called CC=1 SCAR=0.0
Pod documented
147 7 return $_[0]->{_field}->flags();
148 }
149  
150 =head2 set_flags
151  
152 $field->set_flags($flags);
153  
154 Set the field flags (replaces existing).
155  
156 =cut
157  
158 1 ▶sub set_flags {
Subroutine set_flags: called CC=1 SCAR=0.0
Pod documented
159 1 my ($self, $flags) = @_;
160 1 $self->{_field}->set_flags($flags);
161 1 return $self;
162 }
163  
164 =head2 add_flags
165  
166 $field->add_flags($flags);
167  
168 Add flags to the existing set.
169  
170 =cut
171  
172 2 ▶sub add_flags {
Subroutine add_flags: called CC=1 SCAR=0.0
Pod documented
173 2 my ($self, $flags) = @_;
174 2 $self->{_field}->add_flags($flags);
175 2 return $self;
176 }
177  
178 =head2 clear_flags
179  
180 $field->clear_flags($flags);
181  
182 Clear specific flags.
183  
184 =cut
185  
186 1 ▶sub clear_flags {
Subroutine clear_flags: called CC=1 SCAR=0.0
Pod documented
187 1 my ($self, $flags) = @_;
188 1 $self->{_field}->clear_flags($flags);
189 1 return $self;
190 }
191  
192 =head2 readonly
193  
194 $field->readonly(1); # Make read-only
195 $field->readonly(0); # Make editable
196 my $ro = $field->readonly(); # Alias for is_readonly
197  
198 Set or check the read-only flag.
199  
200 =cut
201  
202 5 ▶sub readonly {
Subroutine readonly: called CC=3 SCAR=11.0
Pod documented
203 5 my ($self, $val) = @_;
204 5 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 4 1 5
205 4 ▶ $self->{_field}->readonly($val ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 2 2 4
206 4 return $self;
207 }
208 1 return $self->{_field}->is_readonly();
209 }
210  
211 =head2 is_readonly
212  
213 if ($field->is_readonly()) { ... }
214  
215 Check if field is read-only.
216  
217 =cut
218  
219 3 ▶sub is_readonly {
Subroutine is_readonly: called CC=1 SCAR=0.0
Pod documented
220 3 return $_[0]->{_field}->is_readonly();
221 }
222  
223 =head2 required
224  
225 $field->required(1); # Mark as required
226 $field->required(0); # Mark as optional
227 my $req = $field->required(); # Alias for is_required
228  
229 Set or check the required flag.
230  
231 =cut
232  
233 3 ▶sub required {
Subroutine required: called CC=3 SCAR=11.0
Pod documented
234 3 my ($self, $val) = @_;
235 3 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 2 1 3
236 2 ▶ $self->{_field}->required($val ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 0 2 2
237 2 return $self;
238 }
239 1 return $self->{_field}->is_required();
240 }
241  
242 =head2 is_required
243  
244 if ($field->is_required()) { ... }
245  
246 Check if field is required.
247  
248 =cut
249  
250 2 ▶sub is_required {
Subroutine is_required: called CC=1 SCAR=0.0
Pod documented
251 2 return $_[0]->{_field}->is_required();
252 }
253  
254 =head2 noexport
255  
256 $field->noexport(1);
257  
258 Set the no-export flag (field value not included in FDF export).
259  
260 =cut
261  
262 2 ▶sub noexport {
Subroutine noexport: called CC=2 SCAR=6.9
Pod documented
263 2 my ($self, $val) = @_;
264 2 ▶ $self->{_field}->noexport($val ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 1 1 2
265 2 return $self;
266 }
267  
268 =head2 multiline
269  
270 $field->multiline(1);
271  
272 Enable multiline text input (text fields only).
273  
274 =cut
275  
276 2 ▶sub multiline {
Subroutine multiline: called CC=2 SCAR=6.9
Pod documented
277 2 my ($self, $val) = @_;
278 2 ▶ $self->{_field}->multiline($val ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 1 1 2
279 2 return $self;
280 }
281  
282 =head2 password
283  
284 $field->password(1);
285  
286 Enable password masking (text fields only).
287  
288 =cut
289  
290 2 ▶sub password {
Subroutine password: called CC=2 SCAR=6.9
Pod documented
291 2 my ($self, $val) = @_;
292 2 ▶ $self->{_field}->password($val ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 1 1 2
293 2 return $self;
294 }
295  
296 =head2 set_da
297  
298 $field->set_da('/Helv 12 Tf 0 g');
299  
300 Set the default appearance string (font, size, color).
301  
302 =cut
303  
304 1 ▶sub set_da {
Subroutine set_da: called CC=1 SCAR=0.0
Pod documented
305 1 my ($self, $da) = @_;
306 1 $self->{_field}->set_da($da);
307 1 return $self;
308 }
309  
310 =head2 set_quadding
311  
312 $field->set_quadding(0); # Left
313 $field->set_quadding(1); # Center
314 $field->set_quadding(2); # Right
315  
316 Set text alignment.
317  
318 =cut
319  
320 1 ▶sub set_quadding {
Subroutine set_quadding: called CC=1 SCAR=0.0
Pod documented
321 1 my ($self, $q) = @_;
322 1 $self->{_field}->set_quadding($q);
323 1 return $self;
324 }
325  
326 =head2 align_left
327  
328 $field->align_left();
329  
330 Set left text alignment.
331  
332 =cut
333  
334 1 ▶sub align_left {
Subroutine align_left: called CC=1 SCAR=0.0
Pod documented
335 1 $_[0]->{_field}->align_left();
336 1 return $_[0];
337 }
338  
339 =head2 align_center
340  
341 $field->align_center();
342  
343 Set center text alignment.
344  
345 =cut
346  
347 2 ▶sub align_center {
Subroutine align_center: called CC=1 SCAR=0.0
Pod documented
348 2 $_[0]->{_field}->align_center();
349 2 return $_[0];
350 }
351  
352 =head2 align_right
353  
354 $field->align_right();
355  
356 Set right text alignment.
357  
358 =cut
359  
360 1 ▶sub align_right {
Subroutine align_right: called CC=1 SCAR=0.0
Pod documented
361 1 $_[0]->{_field}->align_right();
362 1 return $_[0];
363 }
364  
365 =head2 set_max_len
366  
367 $field->set_max_len(100);
368  
369 Set maximum character length (text fields only).
370  
371 =cut
372  
373 1 ▶sub set_max_len {
Subroutine set_max_len: called CC=1 SCAR=0.0
Pod documented
374 1 my ($self, $len) = @_;
375 1 $self->{_field}->set_max_len($len);
376 1 return $self;
377 }
378  
379 =head2 add_option
380  
381 $field->add_option('Display Text');
382 $field->add_option('Display Text', 'export_value');
383  
384 Add an option to a choice field.
385  
386 =cut
387  
388 7 ▶sub add_option {
Subroutine add_option: called CC=2 SCAR=6.9
Pod documented
389 7 my ($self, $display, $export) = @_;
390 7 ▶ if (defined $export) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $export) { } 3 4 7
391 3 $self->{_field}->add_option($display, $export);
392 } else {
393 4 $self->{_field}->add_option($display);
394 }
395 7 return $self;
396 }
397  
398 =head2 option_count
399  
400 my $count = $field->option_count();
401  
402 Return the number of options (choice fields only).
403  
404 =cut
405  
406 2 ▶sub option_count {
Subroutine option_count: called CC=1 SCAR=0.0
Pod documented
407 2 return $_[0]->{_field}->option_count();
408 }
409  
410 =head2 options
411  
412 my @opts = $field->options();
413  
414 Return all options as a list of hashrefs with 'display' and 'export' keys.
415  
416 =cut
417  
418 1 ▶sub options {
Subroutine options: called CC=1 SCAR=0.0
Pod documented
419 1 return $_[0]->{_field}->options();
420 }
421  
422 =head2 add_radio_option
423  
424 $group->add_radio_option(
425 x => 100,
426 y => 500,
427 width => 15,
428 height => 15,
429 value => 'Option1',
430 );
431  
432 Add a radio button option to a radio group. Returns the option field.
433  
434 =cut
435  
436 10 ▶sub add_radio_option {
Subroutine add_radio_option: called CC=6 SCAR=17.9
Pod documented
437 10 my ($self, %opts) = @_;
438
439 10 ▶ my $x = delete $opts{x} // 0;
Condition100%2/2
delete $opts{"x"} // 0
l!l
91
MC/DC100%1/1
delete $opts{"x"} // 0
delete $opts{"x"}
Truth table100%2/2
A
A= delete $opts{"x"}
Aresult
11
00
440 10 ▶ my $y = delete $opts{y} // 0;
Condition100%2/2
delete $opts{"y"} // 0
l!l
91
MC/DC100%1/1
delete $opts{"y"} // 0
delete $opts{"y"}
Truth table100%2/2
A
A= delete $opts{"y"}
Aresult
11
00
441 10 ▶ my $width = delete $opts{width} // 15;
Condition100%2/2
delete $opts{"width"} // 15
l!l
82
MC/DC100%1/1
delete $opts{"width"} // 15
delete $opts{"width"}
Truth table100%2/2
A
A= delete $opts{"width"}
Aresult
11
00
442 10 ▶ my $height = delete $opts{height} // 15;
Condition100%2/2
delete $opts{"height"} // 15
l!l
82
MC/DC100%1/1
delete $opts{"height"} // 15
delete $opts{"height"}
Truth table100%2/2
A
A= delete $opts{"height"}
Aresult
11
00
443 10 ▶ my $value = delete $opts{value} or die "add_radio_option: value required";
Branch: 1 branch
BranchTrueFalseTotal
my $value = delete $opts{"value"} or die "add_radio_option: value required" 9 1 10
444
445 9 my $option = $self->{_field}->add_radio_option($x, $y, $width, $height, $value);
446
447 9 return PDF::Make::Field->_wrap($option, $self->{_form});
448 }
449  
450 =head2 add_to_page
451  
452 $field->add_to_page($page);
453  
454 Add the field's widget annotation to a page. This makes the field visible.
455  
456 =cut
457  
458 43 ▶sub add_to_page {
Subroutine add_to_page: called CC=1 SCAR=0.0
Pod documented
459 43 my ($self, $page) = @_;
460
461 # Page is already the raw pdfmake_page_t pointer
462 43 $self->{_field}->add_to_page($page);
463 43 return $self;
464 }
465  
466 =head2 generate_appearance
467  
468 $field->generate_appearance();
469  
470 Generate the appearance stream for this field.
471  
472 =cut
473  
474 1 ▶sub generate_appearance {
Subroutine generate_appearance: called CC=1 SCAR=0.0
Pod documented
475 1 $_[0]->{_field}->generate_appearance();
476 1 return $_[0];
477 }
478  
479 =head2 flatten
480  
481 $field->flatten();
482  
483 Flatten this field: render its value into page content and remove it.
484  
485 =cut
486  
487 1 ▶sub flatten {
Subroutine flatten: called CC=1 SCAR=0.0
Pod documented
488 1 $_[0]->{_field}->flatten();
489 1 return $_[0];
490 }
491  
492 =head2 parent
493  
494 my $parent = $field->parent();
495  
496 Return the parent field (for hierarchical fields).
497  
498 =cut
499  
500 2 ▶sub parent {
Subroutine parent: called CC=2 SCAR=6.9
Pod documented
501 2 my $p = $_[0]->{_field}->parent();
502 2 ▶ return unless $p;
Branch: 1 branch
BranchTrueFalseTotal
unless $p 1 1 2
503 1 return PDF::Make::Field->_wrap($p, $_[0]->{_form});
504 }
505  
506 =head2 children
507  
508 my @children = $field->children();
509  
510 Return child fields (for hierarchical fields).
511  
512 =cut
513  
514 2 ▶sub children {
Subroutine children: called CC=1 SCAR=0.0
Pod documented
515 2 my ($self) = @_;
516 5 return map { PDF::Make::Field->_wrap($_, $self->{_form}) }
517 2 $self->{_field}->children();
518 }
519  
520 =head2 has_children
521  
522 if ($field->has_children()) { ... }
523  
524 Check if field has children.
525  
526 =cut
527  
528 2 ▶sub has_children {
Subroutine has_children: called CC=1 SCAR=0.0
Pod documented
529 2 return $_[0]->{_field}->has_children();
530 }
531  
532 # Internal: get raw field pointer
533 0 ▶sub _field {
Subroutine _field: not called CC=1 SCAR=6.9
534 0 return $_[0]->{_field};
535 }
536  
537 3 1;
538  
539 __END__
« blib/lib/PDF/Make/Text/Word.pm ↑ Summary blib/lib/PDF/Make/Builder/Form/Field/Listbox.pm »