File Coverage

blib/lib/PDF/Builder/Basic/PDF/Array.pm
Criterion Covered Total %
statement 43 51 84.3
branch 2 4 50.0
condition 2 6 33.3
subroutine 10 12 83.3
pod 7 9 77.7
total 64 82 78.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
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 41     41   339 use base 'PDF::Builder::Basic::PDF::Objind';
  41         82  
  41         20446  
19              
20 41     41   294 use strict;
  41         85  
  41         799  
21 41     41   206 use warnings;
  41         85  
  41         23336  
22              
23             our $VERSION = '3.025'; # VERSION
24             our $LAST_UPDATE = '3.024'; # manually update whenever code is changed
25              
26             =head1 NAME
27              
28             PDF::Builder::Basic::PDF::Array - Corresponds to a PDF array.
29             Inherits from L
30              
31             =head1 METHODS
32              
33             =over
34              
35             =item PDF::Array->new($parent, @values)
36              
37             Creates an array with the given storage parent and an optional list of values to
38             initialise the array with.
39              
40             =cut
41              
42             sub new {
43 1583     1583 1 3441 my ($class, @values) = @_;
44 1583         2989 my $self = {};
45              
46 1583         4209 $self->{' val'} = [@values];
47 1583         2697 $self->{' realised'} = 1;
48 1583         2732 bless $self, $class;
49 1583         5406 return $self;
50             }
51              
52             =item $a->outobjdeep($fh, $pdf)
53              
54             Outputs an array as a PDF array to the given filehandle.
55              
56             =cut
57              
58             sub outobjdeep {
59 952     952 1 1671 my ($self, $fh, $pdf) = @_;
60              
61 952         2439 $fh->print('[ ');
62 952         5053 foreach my $obj (@{$self->{' val'}}) {
  952         2090  
63 11100         64585 $obj->outobj($fh, $pdf);
64 11100         19625 $fh->print(' ');
65             }
66 952         6179 $fh->print(']');
67 952         5057 return;
68             }
69              
70             =item $a->elements()
71              
72             Returns the contents of the array.
73              
74             Formerly called C, which is now B.
75              
76             =cut
77              
78 1     1 0 7 sub elementsof { return elements(@_); }
79              
80             sub elements {
81 1168     1168 1 1951 my $self = shift();
82 1168         1705 return @{$self->{' val'}};
  1168         3563  
83             }
84              
85             =item $a->add_elements(@elements)
86              
87             Appends the given elements to the array. An element is only added if it
88             is defined.
89              
90             =cut
91              
92             sub add_elements {
93 28504     28504 1 39920 my $self = shift();
94              
95 28504         44583 foreach my $element (@_) {
96 28506 50       50587 next unless defined $element;
97 28506         36962 push @{$self->{' val'}}, $element;
  28506         57868  
98             }
99 28504         54716 return $self;
100             }
101              
102             =item $a->remove_element($element)
103              
104             Removes all occurrences of an element from an array.
105              
106             Formerly called C, which is now B and will be removed.
107              
108             =cut
109              
110             # not listed as deprecated, not used internally, should not have been
111             # used in external code. remove after July 2021.
112 0     0 0 0 sub removeobj { return remove_element(@_); }
113              
114             sub remove_element {
115 0     0 1 0 my ($self, $element) = @_;
116              
117 0         0 $self->{' val'} = [ grep { $_ ne $element } @{$self->{' val'}} ];
  0         0  
  0         0  
118 0         0 return $self;
119             }
120              
121             =item $a->val()
122              
123             Returns a reference to the contents of the array.
124              
125             =cut
126              
127             sub val {
128 4     4 1 17 return $_[0]->{' val'};
129             }
130              
131             =item $a->copy($pdf)
132              
133             Copies the array with deep-copy on elements which are not full PDF objects
134             with respect to a particular $pdf output context.
135              
136             =cut
137              
138             sub copy {
139 204     204 1 470 my ($self, $pdf) = @_;
140              
141 204         713 my $res = $self->SUPER::copy($pdf);
142              
143 204         508 $res->{' val'} = [];
144 204         353 foreach my $e (@{$self->{' val'}}) {
  204         468  
145 2209 50 33     10866 if (ref($e) and $e->can('is_obj') and not $e->is_obj($pdf)) {
      33        
146 2209         3161 push @{$res->{' val'}}, $e->copy($pdf);
  2209         5072  
147             } else {
148 0         0 push @{$res->{' val'}}, $e;
  0         0  
149             }
150             }
151 204         707 return $res;
152             }
153              
154             =back
155              
156             =cut
157              
158             1;