lib/PDF/OCR2.pm

filter: Statementstmt 66.6% 4 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 75.0% 6 / 8 CC 1 SCAR 1.4
CC
1
Cov
67%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
« blib/lib/PDF/OCR2/Base/Image.pm ↑ Summary lib/PDF/OCR2/Base/Image.pm »
1 package PDF::OCR2;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use PDF::OCR2::Page;
Subroutine BEGIN: called CC=1 SCAR=2.6
4 use PDF::OCR2::Base;
5 use LEOCHARRE::Class2;
6 use Carp;
7 use vars qw($VERSION $DEBUG @TRASH $CHECK_PDF $NO_TRASH_CLEANUP $REPAIR_XREF);
8 __PACKAGE__->make_accessor_setget( 'abs_path', );
9 #__PACKAGE__->make_accessor_setget_unique_array(')
10 __PACKAGE__->make_count_for( '_abs_bursts' );
11 $VERSION = sprintf "%d.%02d", q$Revision: 1.21 $ =~ /(\d+)/g;
12  
13 sub debug { $DEBUG or return 1; print STDERR __PACKAGE__.": @_\n"; 1 }
14 *page = \&_page;
15 *pages_count = \&_abs_bursts_count;
16  
17 sub new {
18 my($class,$arg) = @_;
19 if( $arg and ref $arg ){ croak("argument to constructor must be path to pdf"); }
20 $arg or croak('missing arg to constructor');
21
22 my $self = {};
23 bless $self, $class;
24  
25 # this checks the pdf with PDF::API2 if PDF::OCR2::CHECK_PDF is set
26 ( $self->{abs_path} = PDF::OCR2::Base::get_abs_pdf($arg) ) or return;
27  
28 return $self;
29 }
30  
31  
32  
33  
34  
35  
36  
37 sub _abs_bursts {
38 my $self = shift;
39  
40 unless( $self->{_abs_bursts} ){
41 my $abs = $self->abs_path or warn("Cant burst, no abs path") and return;
42 print STDERR __PACKAGE__."::_abs_bursts() bursting '$abs'.. " if $DEBUG;
43  
44 require PDF::Burst;
45 my @abs = PDF::Burst::pdf_burst($abs) or warn('error'); #carp($PDF::Burst::errstr);
46 $self->{_abs_bursts} = [@abs]; # even if none returned, now contains aref
47 push @TRASH, @abs;
48  
49 print STDERR "Done. Got: @abs\n" if $DEBUG;
50 }
51
52 wantarray and return @{$self->{_abs_bursts}};
53 return $self->{_abs_bursts};
54 }
55  
56 sub _page { # return page object
57 my($self,$pagenum) = @_;
58
59 $pagenum=~/\D/ and croak("arg must be page number");
60
61 unless( $self->{page}->{$pagenum} ){
62 debug("instancing page object page $pagenum");
63 my $abs = $self->_abs_bursts->[($pagenum - 1 )]
64 or croak("No such page num: $pagenum");
65 debug($abs);
66 my $o =
67 PDF::OCR2::Page->new({ abs_pdf => $abs })
68 or die("Could not instance PDF::OCR2::Page for $abs");
69 $self->{page}->{$pagenum} = $o;
70 }
71 $self->{page}->{$pagenum};
72 }
73  
74  
75  
76 sub text {
77 my $self = shift;
78  
79 my @texts;
80  
81 debug( " bursts count: ". $self->_abs_bursts_count);
82  
83 for my $pagenum ( 1 .. $self->_abs_bursts_count ){
84 my $p = $self->_page($pagenum);
85 push @texts, $p->text;
86 }
87  
88 wantarray ? @texts : join( "\f", @texts);
89 }
90  
91 sub text_length { length( scalar $_[0]->text ) }
92  
93  
94 sub DESTROY { unlink @TRASH unless ( $DEBUG or $NO_TRASH_CLEANUP ) }
95  
96  
97 1;
98  
99  
100  
« blib/lib/PDF/OCR2/Base/Image.pm ↑ Summary lib/PDF/OCR2/Base/Image.pm »