File Coverage

blib/lib/PDF/API2/Basic/PDF/Array.pm
Criterion Covered Total %
statement 41 49 83.6
branch 2 4 50.0
condition 2 6 33.3
subroutine 9 12 75.0
pod 7 9 77.7
total 61 80 76.2


line stmt bran cond sub pod time code
1             # Code in the PDF::API2::Basic::PDF namespace was originally copied from the
2             # Text::PDF distribution.
3             #
4             # Copyright Martin Hosken
5             #
6             # Martin Hosken's code may be used under the terms of the MIT license.
7             # Subsequent versions of the code have the same license as PDF::API2.
8              
9             package PDF::API2::Basic::PDF::Array;
10              
11 41     41   291 use base 'PDF::API2::Basic::PDF::Objind';
  41         76  
  41         18428  
12              
13 41     41   270 use strict;
  41         119  
  41         754  
14 41     41   187 use warnings;
  41         79  
  41         20430  
15              
16             our $VERSION = '2.045'; # VERSION
17              
18             =head1 NAME
19              
20             PDF::API2::Basic::PDF::Array - Low-level PDF array object
21              
22             =head1 METHODS
23              
24             =head2 PDF::Array->new($parent, @values)
25              
26             Creates an array with the given storage parent and an optional list of values to
27             initialise the array with.
28              
29             =cut
30              
31             sub new {
32 1095     1095 1 2187 my ($class, @values) = @_;
33 1095         1779 my $self = {};
34              
35 1095         2862 $self->{' val'} = [@values];
36 1095         1783 $self->{' realised'} = 1;
37              
38 1095         1733 bless $self, $class;
39 1095         3400 return $self;
40             }
41              
42             =head2 $a->outobjdeep($fh, $pdf)
43              
44             Outputs an array as a PDF array to the given filehandle.
45              
46             =cut
47              
48             sub outobjdeep {
49 619     619 1 1052 my ($self, $fh, $pdf) = @_;
50              
51 619         1480 $fh->print('[ ');
52 619         3224 foreach my $obj (@{$self->{' val'}}) {
  619         1378  
53 1841         9298 $obj->outobj($fh, $pdf);
54 1841         12129 $fh->print(' ');
55             }
56 619         3568 $fh->print(']');
57             }
58              
59             =head2 $a->elements()
60              
61             Returns the contents of the array.
62              
63             =cut
64              
65 0     0 0 0 sub elementsof { return elements(@_) }
66              
67             sub elements {
68 665     665 1 982 my $self = shift();
69 665         902 return @{$self->{' val'}};
  665         2042  
70             }
71              
72             =head2 $a->add_elements(@elements)
73              
74             Appends the given elements to the array. An element is only added if it
75             is defined.
76              
77             =cut
78              
79             sub add_elements {
80 27380     27380 1 38713 my $self = shift();
81              
82 27380         41452 foreach my $element (@_) {
83 27382 50       48870 next unless defined $element;
84 27382         34570 push @{$self->{' val'}}, $element;
  27382         56135  
85             }
86              
87 27380         51280 return $self;
88             }
89              
90             =head2 $a->remove_element($element)
91              
92             Removes all occurrences of an element from an array.
93              
94             =cut
95              
96 0     0 0 0 sub removeobj { return remove_element(@_) }
97              
98             sub remove_element {
99 0     0 1 0 my ($self, $element) = @_;
100              
101 0         0 $self->{' val'} = [ grep { $_ ne $element } @{$self->{' val'}} ];
  0         0  
  0         0  
102             }
103              
104             =head2 $a->val()
105              
106             Returns a reference to the contents of the array.
107              
108             =cut
109              
110             sub val {
111 4     4 1 14 return $_[0]->{' val'};
112             }
113              
114             =head2 $a->copy($pdf)
115              
116             Copies the array with deep-copy on elements which are not full PDF objects
117             with respect to a particular $pdf output context
118              
119             =cut
120              
121             sub copy {
122 144     144 1 295 my ($self, $pdf) = @_;
123 144         473 my $res = $self->SUPER::copy($pdf);
124              
125 144         309 $res->{' val'} = [];
126 144         241 foreach my $e (@{$self->{' val'}}) {
  144         348  
127 718 50 33     3605 if (ref($e) and $e->can('is_obj') and not $e->is_obj($pdf)) {
      33        
128 718         1062 push(@{$res->{' val'}}, $e->copy($pdf));
  718         1676  
129             }
130             else {
131 0         0 push(@{$res->{' val'}}, $e);
  0         0  
132             }
133             }
134 144         446 return $res;
135             }
136              
137             1;