File Coverage

blib/lib/PDF/Builder/Basic/PDF/Array.pm
Criterion Covered Total %
statement 43 51 84.3
branch 3 6 50.0
condition 2 6 33.3
subroutine 9 10 90.0
pod 7 7 100.0
total 64 80 80.0


line stmt bran cond sub pod time code
1             #=======================================================================
2             #
3             # THIS IS A REUSED PERL MODULE, FOR PROPER LICENCING TERMS SEE BELOW:
4             #
5             # Copyright Martin Hosken <Martin_Hosken@sil.org>
6             #
7             # No warranty or expression of effectiveness, least of all regarding
8             # anyone's safety, is implied in this software or documentation.
9             #
10             # This specific module is licensed under the Perl Artistic License.
11             # Effective 28 January 2021, the original author and copyright holder,
12             # Martin Hosken, has given permission to use and redistribute this module
13             # under the MIT license.
14             #
15             #=======================================================================
16             package PDF::Builder::Basic::PDF::Array;
17              
18 42     42   295 use base 'PDF::Builder::Basic::PDF::Objind';
  42         91  
  42         24703  
19              
20 42     42   315 use strict;
  42         108  
  42         1092  
21 42     42   213 use warnings;
  42         83  
  42         29129  
22              
23             our $VERSION = '3.028'; # VERSION
24             our $LAST_UPDATE = '3.027'; # manually update whenever code is changed
25              
26             =head1 NAME
27              
28             PDF::Builder::Basic::PDF::Array - Corresponds to a PDF array
29              
30             Inherits from L<PDF::Builder::Basic::PDF::Objind>
31              
32             =head1 METHODS
33              
34             =head2 new
35              
36             PDF::Array->new($parent, @values)
37              
38             =over
39              
40             Creates an array with the given storage parent and an optional list of values to
41             initialise the array with.
42              
43             =back
44              
45             =cut
46              
47             sub new {
48 1605     1605 1 3862 my ($class, @values) = @_;
49 1605         3281 my $self = {};
50              
51 1605         4556 $self->{' val'} = [@values];
52 1605         3298 $self->{' realised'} = 1;
53 1605         3007 bless $self, $class;
54 1605         6846 return $self;
55             }
56              
57             =head2 outobjdeep
58              
59             $a->outobjdeep($fh, $pdf)
60              
61             =over
62              
63             Outputs an array as a PDF array to the given filehandle. It's unusual to
64             need to call this method from user code.
65              
66             =back
67              
68             =cut
69              
70             sub outobjdeep {
71 954     954 1 2151 my ($self, $fh, $pdf) = @_;
72              
73 954         2627 $fh->print('[ ');
74 954         5715 foreach my $obj (@{$self->{' val'}}) {
  954         2635  
75             # if no graphics object (page->gfx), creates an invalid Contents object
76             # (unblessed HASH containing no keys) for this page's graphics, and
77             # this function blows up
78 11088 50       71413 if ($obj !~ /^PDF::Builder/) { next; }
  0         0  
79              
80 11088         28980 $obj->outobj($fh, $pdf);
81 11088         21293 $fh->print(' ');
82             }
83 954         6973 $fh->print(']');
84 954         10666 return;
85             }
86              
87             =head2 elements
88              
89             $a->elements()
90              
91             =over
92              
93             Returns the contents of the array.
94              
95             =back
96              
97             =cut
98              
99             sub elements {
100 1168     1168 1 2130 my $self = shift();
101 1168         1867 return @{$self->{' val'}};
  1168         4540  
102             }
103              
104             =head2 add_elements
105              
106             $a->add_elements(@elements)
107              
108             =over
109              
110             Appends the given elements to the array. An element is only added if it
111             is defined.
112              
113             =back
114              
115             =cut
116              
117             sub add_elements {
118 21381     21381 1 32054 my $self = shift();
119              
120 21381         39097 foreach my $element (@_) {
121 21383 50       40090 next unless defined $element;
122 21383         32869 push @{$self->{' val'}}, $element;
  21383         48902  
123             }
124 21381         41540 return $self;
125             }
126              
127             =head2 remove_element
128              
129             $a->remove_element($element)
130              
131             =over
132              
133             Removes all occurrences of an element from an array.
134              
135             =back
136              
137             =cut
138              
139             sub remove_element {
140 0     0 1 0 my ($self, $element) = @_;
141              
142 0         0 $self->{' val'} = [ grep { $_ ne $element } @{$self->{' val'}} ];
  0         0  
  0         0  
143 0         0 return $self;
144             }
145              
146             =head2 val
147              
148             $a->val()
149              
150             =over
151              
152             Returns a reference to the contents of the array.
153              
154             =back
155              
156             =cut
157              
158             sub val {
159 4     4 1 15 return $_[0]->{' val'};
160             }
161              
162             =head2 copy
163              
164             $a->copy($pdf)
165              
166             =over
167              
168             Copies the array with deep-copy on elements which are not full PDF objects
169             with respect to a particular $pdf output context.
170              
171             =back
172              
173             =cut
174              
175             sub copy {
176 202     202 1 543 my ($self, $pdf) = @_;
177              
178 202         946 my $res = $self->SUPER::copy($pdf);
179              
180 202         714 $res->{' val'} = [];
181 202         405 foreach my $e (@{$self->{' val'}}) {
  202         612  
182 2199 50 33     11136 if (ref($e) and $e->can('is_obj') and not $e->is_obj($pdf)) {
      33        
183 2199         3630 push @{$res->{' val'}}, $e->copy($pdf);
  2199         6394  
184             } else {
185 0         0 push @{$res->{' val'}}, $e;
  0         0  
186             }
187             }
188 202         866 return $res;
189             }
190              
191             1;