PDF/Document.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« PDF/Page.pm ↑ Summary PDF/Devel/TestHelper.pm »
1 2 ▶use Renard::Incunabula::Common::Setup;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package Renard::Block::Format::PDF::Document;
3 # ABSTRACT: document that represents a PDF file
4 $Renard::Block::Format::PDF::Document::VERSION = '0.005';
5 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 0 ▶use Renard::API::MuPDF::mutool;
Subroutine BEGIN: called CC=1 SCAR=2.6
7 use Renard::Block::Format::PDF::Page;
8 use Renard::Incunabula::Outline;
9 use Renard::Incunabula::Document::Types qw(PageNumber ZoomLevel);
10 use Renard::Incunabula::Common::Types qw(InstanceOf);
11  
12 use Math::Trig;
13 use Math::Polygon;
14  
15 use String::Tagged;
16  
17 use Function::Parameters;
18  
19 extends qw(Renard::Incunabula::Document);
20  
21 has _raw_bounds => (
22 is => 'lazy', # _build_raw_bounds
23 );
24  
25 method _build_last_page_number() :ReturnType(PageNumber) {
26 my $info = Renard::API::MuPDF::mutool::get_mutool_page_info_xml(
27 $self->filename
28 );
29  
30 return scalar @{ $info->{page} };
31 }
32  
33 method get_rendered_page( (PageNumber) :$page_number, (ZoomLevel) :$zoom_level = 1.0 ) {
34 return Renard::Block::Format::PDF::Page->new(
35 document => $self,
36 page_number => $page_number,
37 zoom_level => $zoom_level,
38 );
39 }
40  
41 method _build_outline() {
42 my $outline_data = Renard::API::MuPDF::mutool::get_mutool_outline_simple(
43 $self->filename
44 );
45  
46 return Renard::Incunabula::Outline->new( items => $outline_data );
47 }
48  
49 method _build__raw_bounds() {
50 my $info = Renard::API::MuPDF::mutool::get_mutool_page_info_xml(
51 $self->filename
52 );
53 }
54  
55 method _build_identity_bounds() {
56 my $compute_rotate_dim = sub {
57 my ($info) = @_;
58 my $theta_deg = $info->{rotate} // 0;
59 my $theta_rad = $theta_deg * pi / 180;
60  
61 my ($x, $y) = ($info->{x}, $info->{y});
62 my $poly = Math::Polygon->new(
63 points => [
64 [0, 0],
65 [$x, 0],
66 [$x, $y],
67 [0, $y],
68 ],
69 );
70  
71 my $rotated_poly = $poly->rotate(
72 degrees => $theta_deg,
73 center => [ $x/2, $y/2 ],
74 );
75  
76 my ($xmin, $ymin, $xmax, $ymax) = $rotated_poly->bbox;
77  
78  
79 return { w => $xmax - $xmin, h => $ymax - $ymin };
80 };
81  
82 my $bounds = $self->_raw_bounds;
83 my @page_xy = map {
84 my $p = {
85 x => $_->{CropBox}{r}-$_->{CropBox}{l},
86 y => $_->{CropBox}{t}-$_->{CropBox}{b},
87 rotate => $_->{Rotate}{v} // 0,
88 pageno => $_->{pagenum},
89 };
90 if( exists $p->{rotate} ) {
91 $p->{dims} = $compute_rotate_dim->( $p );
92 }
93  
94 $p;
95 } @{ $bounds->{page} };
96  
97 return \@page_xy;
98 }
99  
100 method get_textual_page( (PageNumber) $page_number )
101 :ReturnType(InstanceOf['String::Tagged']) {
102 my $page_st = String::Tagged->new;
103  
104 my $stext = Renard::API::MuPDF::mutool::get_mutool_text_stext_xml(
105 $self->filename,
106 $page_number
107 );
108  
109 my $levels = [ qw(document page block line font char) ];
110 _walk_page_data( $page_st, $stext, 0, $levels );
111  
112 $page_st;
113 }
114  
115 fun _walk_page_data( $tagged, $data, $depth, $levels ) {
116 my $level_tagged = String::Tagged->new("");
117  
118 if( $depth == @$levels - 1 ) {
119 # last level is the character, so we append that to the string
120 $level_tagged .= $data->{c};
121 } else {
122 # empty pages will not have this data
123 return unless exists $data->{ $levels->[$depth+1] };
124  
125 my @data_next = @{ $data->{ $levels->[$depth+1] } };
126 for my $next_data (@data_next) {
127 _walk_page_data( $level_tagged, $next_data, $depth+1, $levels );
128 }
129 }
130 $level_tagged->apply_tag(0, $level_tagged->length, $levels->[$depth] => $data );
131  
132 $tagged->append_tagged($level_tagged);
133  
134 return;
135 }
136  
137  
138 with qw(
139 Renard::Incunabula::Document::Role::FromFile
140 Renard::Incunabula::Document::Role::Pageable
141 Renard::Incunabula::Document::Role::Renderable
142 Renard::Incunabula::Document::Role::Cacheable
143 Renard::Incunabula::Document::Role::Outlineable
144 Renard::Incunabula::Document::Role::Boundable
145 );
146  
147 1;
148  
149 __END__
« PDF/Page.pm ↑ Summary PDF/Devel/TestHelper.pm »