« blib/lib/PDF/Make/Builder/Page/HeaderFooterContext.pm
↑ Summary
blib/lib/PDF/Make/Linearization.pm »
| 1 | package PDF::Make::Builder::Form::Field; | ||||||||||||||||||||||||||||
| 2 | 53 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 53 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 53 | ▶ | use Object::Proto; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 53 | ▶ | use PDF::Make (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 53 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 53 | Object::Proto::define('PDF::Make::Builder::Form::Field', | |||||||||||||||||||||||||||
| 9 | 'field_name:Str:required', | ||||||||||||||||||||||||||||
| 10 | 'x:Num', | ||||||||||||||||||||||||||||
| 11 | 'y:Num', | ||||||||||||||||||||||||||||
| 12 | 'w:Num:default(200)', | ||||||||||||||||||||||||||||
| 13 | 'h:Num:default(24)', | ||||||||||||||||||||||||||||
| 14 | 'default_value:Str', | ||||||||||||||||||||||||||||
| 15 | 'da:Str', | ||||||||||||||||||||||||||||
| 16 | 'label:Str', | ||||||||||||||||||||||||||||
| 17 | 'label_colour:Str:default(#444)', | ||||||||||||||||||||||||||||
| 18 | 'label_size:Num:default(9)', | ||||||||||||||||||||||||||||
| 19 | 'font_size:Num:default(10)', | ||||||||||||||||||||||||||||
| 20 | 'font_name:Str:default(Helv)', | ||||||||||||||||||||||||||||
| 21 | 'border_colour:Str:default(#aaa)', | ||||||||||||||||||||||||||||
| 22 | 'bg_colour:Str:default(#fff)', | ||||||||||||||||||||||||||||
| 23 | 'readonly:Bool:default(0)', | ||||||||||||||||||||||||||||
| 24 | 'required:Bool:default(0)', | ||||||||||||||||||||||||||||
| 25 | 'inline_label:Bool:default(0)', | ||||||||||||||||||||||||||||
| 26 | 'raw_mode:Bool:default(0)', | ||||||||||||||||||||||||||||
| 27 | 'margin_top:Num:default(4)', | ||||||||||||||||||||||||||||
| 28 | 'margin_bottom:Num:default(8)', | ||||||||||||||||||||||||||||
| 29 | ); | ||||||||||||||||||||||||||||
| 30 | 53 | Object::Proto::import_accessors('PDF::Make::Builder::Form::Field'); | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 24 | ▶ | sub _ensure_form { | ||||||||||||||||||||||||||
|
Subroutine
_ensure_form: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 34 | 24 | my ($doc) = @_; | |||||||||||||||||||||||||||
| 35 | 24 | my $form = eval { PDF::Make::FormPtr::get($doc) }; | |||||||||||||||||||||||||||
| 36 | 24 | ▶ | unless ($form) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 6 | $form = PDF::Make::FormPtr::create($doc); | |||||||||||||||||||||||||||
| 38 | 6 | PDF::Make::FormPtr::set_need_appearances($form, 1); | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | 24 | return $form; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 0 | ▶ | sub _create_field { | ||||||||||||||||||||||||||
|
Subroutine
_create_field: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 44 | 0 | die "PDF::Make::Builder::Form::Field::_create_field must be overridden"; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 24 | ▶ | sub add { | ||||||||||||||||||||||||||
|
Subroutine
add: called CC=12 SCAR=24.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 48 | 24 | my ($self, $builder) = @_; | |||||||||||||||||||||||||||
| 49 | 24 | my $page = $builder->page; | |||||||||||||||||||||||||||
| 50 | 24 | my $canvas = $page->canvas; | |||||||||||||||||||||||||||
| 51 | 24 | my $doc = $builder->doc; | |||||||||||||||||||||||||||
| 52 | 24 | my $bfont = $builder->font; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 24 | _ensure_form($doc); | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 24 | ▶ | my $fx = $self->x // $page->content_x; | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->x // $page->content_x
MC/DC0%0/2
$self->x // $page->content_x
$self->x
$page->content_x
Truth table66%2/3
A // B
A= $self->xB= $page->content_x
| |||||||||||||||||||||||||||||
| 57 | 24 | my $fw = $self->w; | |||||||||||||||||||||||||||
| 58 | 24 | my $fh = $self->h; | |||||||||||||||||||||||||||
| 59 | 24 | my $name = field_name $self; | |||||||||||||||||||||||||||
| 60 | 24 | my $is_inline = inline_label $self; | |||||||||||||||||||||||||||
| 61 | 24 | my $lbl = $self->label; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | # ── Raw mode: create/place widget without layout/chrome/cursor changes ── | ||||||||||||||||||||||||||||
| 64 | 24 | ▶ | if ($self->raw_mode) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 9 | ▶ | my $fy = defined($self->y) ? $self->y : ($page->cursor_y - $fh); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 9 | my $field = $self->_create_field($doc, $name, $fx, $fy, $fw, $fh); | |||||||||||||||||||||||||||
| 67 | 9 | $self->_apply_field_props($field); | |||||||||||||||||||||||||||
| 68 | 9 | $field->add_to_page($page->xs_page); | |||||||||||||||||||||||||||
| 69 | 9 | return $self; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | # Top margin | ||||||||||||||||||||||||||||
| 73 | 15 | $page->advance_y(margin_top $self); | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 15 | ▶ | if ($is_inline) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | # ── Inline layout (checkbox/radio): field then label beside it ── | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 2 | my $fy = $page->cursor_y - $fh; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | # Draw label to the right of the field | ||||||||||||||||||||||||||||
| 81 | 2 | ▶ | if (defined $lbl && length $lbl) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $lbl and length $lbl
MC/DC0%0/2
defined $lbl and length $lbl
defined $lbl
length $lbl
Truth table33%1/3
A and B
A= defined $lblB= length $lbl
| |||||||||||||||||||||||||||||
| 82 | 2 | my $lsize = label_size $self; | |||||||||||||||||||||||||||
| 83 | 2 | my ($lr, $lg, $lb) = $bfont->hex_to_rgb(label_colour $self); | |||||||||||||||||||||||||||
| 84 | 2 | $bfont->ensure_loaded($page->xs_page); | |||||||||||||||||||||||||||
| 85 | 2 | my $res = $bfont->resource_name; | |||||||||||||||||||||||||||
| 86 | 2 | my $tj = $bfont->show_op; | |||||||||||||||||||||||||||
| 87 | 2 | $canvas->BT | |||||||||||||||||||||||||||
| 88 | ->Tf($res, $lsize) | ||||||||||||||||||||||||||||
| 89 | ->rg($lr, $lg, $lb) | ||||||||||||||||||||||||||||
| 90 | ->Td($fx + $fw + 8, $fy + ($fh - $lsize) / 2 + 1) | ||||||||||||||||||||||||||||
| 91 | ->$tj($bfont->encode($lbl)) | ||||||||||||||||||||||||||||
| 92 | ->ET; | ||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # Create XS field | ||||||||||||||||||||||||||||
| 96 | 2 | my $field = $self->_create_field($doc, $name, $fx, $fy, $fw, $fh); | |||||||||||||||||||||||||||
| 97 | 2 | $self->_apply_field_props($field); | |||||||||||||||||||||||||||
| 98 | 2 | $field->add_to_page($page->xs_page); | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | # Draw styled border (unless subclass handles its own appearance) | ||||||||||||||||||||||||||||
| 101 | 2 | ▶ | $self->_draw_field_chrome($canvas, $bfont, $fx, $fy, $fw, $fh) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | unless $self->_draws_own_chrome; | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # Advance past the field | ||||||||||||||||||||||||||||
| 105 | 2 | $page->advance_y($fh + margin_bottom $self); | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | } else { | ||||||||||||||||||||||||||||
| 108 | # ── Stacked layout (text/combo/list): label above, field below ── | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | # Draw label first | ||||||||||||||||||||||||||||
| 111 | 13 | ▶ | if (defined $lbl && length $lbl) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $lbl and length $lbl
MC/DC50%1/2
defined $lbl and length $lbl
defined $lbl
length $lbl
Truth table66%2/3
A and B
A= defined $lblB= length $lbl
| |||||||||||||||||||||||||||||
| 112 | 10 | my $lsize = label_size $self; | |||||||||||||||||||||||||||
| 113 | 10 | my ($lr, $lg, $lb) = $bfont->hex_to_rgb(label_colour $self); | |||||||||||||||||||||||||||
| 114 | 10 | $bfont->ensure_loaded($page->xs_page); | |||||||||||||||||||||||||||
| 115 | 10 | my $res = $bfont->resource_name; | |||||||||||||||||||||||||||
| 116 | 10 | ▶ | my $req_marker = ($self->required) ? ' *' : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 10 | my $label_y = $page->cursor_y - $lsize; | |||||||||||||||||||||||||||
| 118 | 10 | my $tj = $bfont->show_op; | |||||||||||||||||||||||||||
| 119 | 10 | $canvas->BT | |||||||||||||||||||||||||||
| 120 | ->Tf($res, $lsize) | ||||||||||||||||||||||||||||
| 121 | ->rg($lr, $lg, $lb) | ||||||||||||||||||||||||||||
| 122 | ->Td($fx, $label_y) | ||||||||||||||||||||||||||||
| 123 | ->$tj($bfont->encode($lbl . $req_marker)) | ||||||||||||||||||||||||||||
| 124 | ->ET; | ||||||||||||||||||||||||||||
| 125 | 10 | $page->advance_y($lsize + 3); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | # Field goes at current cursor | ||||||||||||||||||||||||||||
| 129 | 13 | my $fy = $page->cursor_y - $fh; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | # Create XS field | ||||||||||||||||||||||||||||
| 132 | 13 | my $field = $self->_create_field($doc, $name, $fx, $fy, $fw, $fh); | |||||||||||||||||||||||||||
| 133 | 13 | $self->_apply_field_props($field); | |||||||||||||||||||||||||||
| 134 | 13 | $field->add_to_page($page->xs_page); | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | # Draw styled border (unless subclass handles its own appearance) | ||||||||||||||||||||||||||||
| 137 | 13 | ▶ | $self->_draw_field_chrome($canvas, $bfont, $fx, $fy, $fw, $fh) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | unless $self->_draws_own_chrome; | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | # Advance past the field | ||||||||||||||||||||||||||||
| 141 | 13 | $page->advance_y($fh + margin_bottom $self); | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 15 | return $self; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 24 | ▶ | sub _apply_field_props { | ||||||||||||||||||||||||||
|
Subroutine
_apply_field_props: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 148 | 24 | my ($self, $field) = @_; | |||||||||||||||||||||||||||
| 149 | 24 | my $da = $self->da; | |||||||||||||||||||||||||||
| 150 | 24 | ▶ | $da = sprintf('/%s %g Tf 0 g', $self->font_name, $self->font_size) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $da and length $da
MC/DC50%1/2
defined $da and length $da
defined $da
length $da
Truth table66%2/3
A and B
A= defined $daB= length $da
| |||||||||||||||||||||||||||||
| 151 | unless defined $da && length $da; | ||||||||||||||||||||||||||||
| 152 | 24 | $field->set_da($da); | |||||||||||||||||||||||||||
| 153 | 24 | my $dv = default_value $self; | |||||||||||||||||||||||||||
| 154 | 24 | ▶ | $field->set_value($dv) if defined $dv; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 24 | ▶ | $field->readonly if $self->readonly; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 24 | ▶ | $field->required if $self->required; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 11 | ▶ | sub _draws_own_chrome { 0 } | ||||||||||||||||||||||||||
|
Subroutine
_draws_own_chrome: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 11 | ▶ | sub _draw_field_chrome { | ||||||||||||||||||||||||||
|
Subroutine
_draw_field_chrome: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 162 | 11 | my ($self, $canvas, $bfont, $fx, $fy, $fw, $fh) = @_; | |||||||||||||||||||||||||||
| 163 | 11 | my ($bgr, $bgg, $bgb) = $bfont->hex_to_rgb(bg_colour $self); | |||||||||||||||||||||||||||
| 164 | 11 | my ($bdr, $bdg, $bdb) = $bfont->hex_to_rgb(border_colour $self); | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 11 | $canvas->q; | |||||||||||||||||||||||||||
| 167 | # White fill | ||||||||||||||||||||||||||||
| 168 | 11 | $canvas->rg($bgr, $bgg, $bgb) | |||||||||||||||||||||||||||
| 169 | ->re($fx, $fy, $fw, $fh)->f; | ||||||||||||||||||||||||||||
| 170 | # Border stroke | ||||||||||||||||||||||||||||
| 171 | 11 | $canvas->w(0.75) | |||||||||||||||||||||||||||
| 172 | ->RG($bdr, $bdg, $bdb) | ||||||||||||||||||||||||||||
| 173 | ->re($fx + 0.25, $fy + 0.25, $fw - 0.5, $fh - 0.5)->S; | ||||||||||||||||||||||||||||
| 174 | 11 | $canvas->Q; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 53 | 1; | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | __END__ | ||||||||||||||||||||||||||||