File Coverage

blib/lib/PDF/API2/Basic/PDF/Filter.pm
Criterion Covered Total %
statement 28 41 68.2
branch 0 8 0.0
condition 0 6 0.0
subroutine 9 10 90.0
pod 1 2 50.0
total 38 67 56.7


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::Filter;
10              
11 44     44   318 use strict;
  44         88  
  44         1258  
12 44     44   216 use warnings;
  44         81  
  44         1821  
13              
14             our $VERSION = '2.045'; # VERSION
15              
16 44     44   18165 use PDF::API2::Basic::PDF::Filter::ASCII85Decode;
  44         112  
  44         1284  
17 44     44   17928 use PDF::API2::Basic::PDF::Filter::ASCIIHexDecode;
  44         109  
  44         1325  
18 44     44   18990 use PDF::API2::Basic::PDF::Filter::FlateDecode;
  44         178  
  44         2000  
19 44     44   21563 use PDF::API2::Basic::PDF::Filter::LZWDecode;
  44         138  
  44         1597  
20 44     44   18263 use PDF::API2::Basic::PDF::Filter::RunLengthDecode;
  44         126  
  44         1669  
21 44     44   289 use Scalar::Util qw(blessed reftype);
  44         95  
  44         12264  
22              
23             =head1 NAME
24              
25             PDF::API2::Basic::PDF::Filter - Abstract superclass for PDF stream filters
26              
27             =head1 SYNOPSIS
28              
29             $f = PDF::API2::Basic::PDF::Filter->new;
30             $str = $f->outfilt($str, 1);
31             print OUTFILE $str;
32              
33             while (read(INFILE, $dat, 4096))
34             { $store .= $f->infilt($dat, 0); }
35             $store .= $f->infilt("", 1);
36              
37             =head1 DESCRIPTION
38              
39             A Filter object contains state information for the process of outputting
40             and inputting data through the filter. The precise state information stored
41             is up to the particular filter and may range from nothing to whole objects
42             created and destroyed.
43              
44             Each filter stores different state information for input and output and thus
45             may handle one input filtering process and one output filtering process at
46             the same time.
47              
48             =head1 METHODS
49              
50             =head2 PDF::API2::Basic::PDF::Filter->new
51              
52             Creates a new filter object with empty state information ready for processing
53             data both input and output.
54              
55             =head2 $dat = $f->infilt($str, $isend)
56              
57             Filters from output to input the data. Notice that $isend == 0 implies that there
58             is more data to come and so following it $f may contain state information
59             (usually due to the break-off point of $str not being tidy). Subsequent calls
60             will incorporate this stored state information.
61              
62             $isend == 1 implies that there is no more data to follow. The
63             final state of $f will be that the state information is empty. Error messages
64             are most likely to occur here since if there is required state information to
65             be stored following this data, then that would imply an error in the data.
66              
67             =head2 $str = $f->outfilt($dat, $isend)
68              
69             Filter stored data ready for output. Parallels C.
70              
71             =cut
72              
73             sub new {
74 6     6 1 114 my $class = shift();
75 6         24 my $self = {};
76              
77 6         16 bless $self, $class;
78              
79 6         31 return $self;
80             }
81              
82             sub release {
83 0     0 0   my $self = shift();
84 0 0         return $self unless ref($self);
85              
86             # delete stuff that we know we can, here
87 0           my @tofree = map { delete $self->{$_} } keys %$self;
  0            
88              
89 0           while (my $item = shift @tofree) {
90 0           my $ref = ref($item);
91 0 0 0       if (blessed($item) and $item->can('release')) {
    0 0        
    0          
92 0           $item->release();
93             }
94             elsif ($ref eq 'ARRAY') {
95 0           push @tofree, @$item;
96             }
97             elsif (defined(reftype($ref)) and reftype($ref) eq 'HASH') {
98 0           release($item);
99             }
100             }
101              
102             # check that everything has gone
103 0           foreach my $key (keys %$self) {
104             # warn ref($self) . " still has '$key' key left after release.\n";
105 0           $self->{$key} = undef;
106 0           delete $self->{$key};
107             }
108             }
109              
110             1;