File Coverage

blib/lib/PDF/Builder/Basic/PDF/Filter.pm
Criterion Covered Total %
statement 28 42 66.6
branch 0 8 0.0
condition 0 6 0.0
subroutine 9 10 90.0
pod 1 2 50.0
total 38 68 55.8


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::Filter;
17              
18 45     45   315 use strict;
  45         99  
  45         1985  
19 45     45   234 use warnings;
  45         96  
  45         4361  
20              
21             our $VERSION = '3.028'; # VERSION
22             our $LAST_UPDATE = '3.027'; # manually update whenever code is changed
23              
24 45     45   24877 use PDF::Builder::Basic::PDF::Filter::ASCII85Decode;
  45         154  
  45         1728  
25 45     45   24925 use PDF::Builder::Basic::PDF::Filter::ASCIIHexDecode;
  45         160  
  45         1839  
26 45     45   26510 use PDF::Builder::Basic::PDF::Filter::FlateDecode;
  45         247  
  45         3179  
27 45     45   31282 use PDF::Builder::Basic::PDF::Filter::LZWDecode;
  45         560  
  45         2416  
28 45     45   36391 use PDF::Builder::Basic::PDF::Filter::RunLengthDecode;
  45         217  
  45         2570  
29             # use PDF::Builder::Basic::PDF::Filter::CCITTFaxDecode; when TIFF changes in
30 45     45   419 use Scalar::Util qw(blessed reftype);
  45         121  
  45         17514  
31              
32             =head1 NAME
33              
34             PDF::Builder::Basic::PDF::Filter - Abstract superclass for PDF stream filters
35              
36             =head1 SYNOPSIS
37              
38             $f = PDF::Builder::Basic::PDF::Filter->new();
39             $str = $f->outfilt($str, 1);
40             print OUTFILE $str;
41              
42             while (read(INFILE, $dat, 4096))
43             { $store .= $f->infilt($dat, 0); }
44             $store .= $f->infilt("", 1);
45              
46             =head1 DESCRIPTION
47              
48             A Filter object contains state information for the process of outputting
49             and inputting data through the filter. The precise state information stored
50             is up to the particular filter and may range from nothing to whole objects
51             created and destroyed.
52              
53             Each filter stores different state information for input and output and thus
54             may handle one input filtering process and one output filtering process at
55             the same time.
56              
57             =head1 METHODS
58              
59             =head2 new
60              
61             PDF::Builder::Basic::PDF::Filter->new()
62              
63             =over
64              
65             Creates a new filter object with empty state information ready for processing
66             data both input and output.
67              
68             =back
69              
70             =head2 infilt
71              
72             $dat = $f->infilt($str, $isend)
73              
74             =over
75              
76             Filters from output to input the data. Notice that C<$isend == 0> implies that
77             there is more data to come and so following it C<$f> may contain state
78             information (usually due to the break-off point of C<$str> not being tidy).
79             Subsequent calls will incorporate this stored state information.
80              
81             C<$isend == 1> implies that there is no more data to follow. The final state of
82             C<$f> will be that the state information is empty. Error messages are most
83             likely to occur here since if there is required state information to be stored
84             following this data, then that would imply an error in the data.
85              
86             =back
87              
88             =head2 outfilt
89              
90             $str = $f->outfilt($dat, $isend)
91              
92             =over
93              
94             Filter stored data ready for output. Parallels C<infilt>.
95              
96             =back
97              
98             =cut
99              
100             sub new {
101 5     5 1 260405 my $class = shift();
102 5         14 my $self = {};
103              
104 5         14 bless $self, $class;
105              
106 5         28 return $self;
107             }
108              
109             sub release {
110 0     0 0   my $self = shift();
111 0 0         return $self unless ref($self);
112              
113             # delete stuff that we know we can, here
114 0           my @tofree = map { delete $self->{$_} } keys %$self;
  0            
115              
116 0           while (my $item = shift @tofree) {
117 0           my $ref = ref($item);
118 0 0 0       if (blessed($item) and $item->can('release')) {
    0 0        
    0          
119 0           $item->release();
120             } elsif ($ref eq 'ARRAY') {
121 0           push @tofree, @$item ;
122             } elsif (defined(reftype($ref)) and reftype($ref) eq 'HASH') {
123 0           release($item);
124             }
125             }
126              
127             # check that everything has gone
128 0           foreach my $key (keys %$self) {
129             # warn ref($self) . " still has '$key' key left after release.\n";
130 0           $self->{$key} = undef;
131 0           delete $self->{$key};
132             }
133 0           return;
134             }
135              
136             1;