PDF.pm

filter: Statementstmt 18.4% 36 / 195 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 31 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 24.2% 8 / 33 Podpod 100% 13 / 13 totaltotal 16.7% 57 / 340 CC 47 SCAR 40.7
CC
47
Cov
13%
_text
240.0
table
42.0
color
30.0
CRAP 58.4
↑ Summary PDF/Primitive/Table.pm »
1 package Mojo::PDF;
2  
3 1 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = '1.005004'; # VERSION
6  
7 1 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 $Carp::Internal{ (__PACKAGE__) }++;
9 1 ▶use PDF::Reuse 0.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Number::RGB 1.41;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Image::Size;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use List::AllUtils qw/sum/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Mojo::PDF::Primitive::Table;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 0 ▶$SIG{'__WARN__'} = sub { warn @_ unless caller eq 'PDF::Reuse'; };
Subroutine __ANON__: not called CC=2 SCAR=13.0
Branch: 1 branch
BranchTrueFalseTotal
unless caller eq "PDF::Reuse" 0 0 0
17  
18 0 ▶has page_size => sub { [612, 792] }; # default US-Letter pages
Subroutine __ANON__: not called CC=1 SCAR=1.2
19 1 has [qw/_line_height _x _y /] => 0;
20 0 ▶has _cur_color => sub { [0, 0, 0] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
21 1 has _cur_font => 'TR';
22 1 has _cur_size => 12;
23 0 ▶has _fonts => sub { +{} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
24 0 ▶has _rules => sub { +{} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
25  
26 1 my %STD_FONTS = (
27 'Times-Roman' => 'Times-Roman',
28 'Times-Bold' => 'Times-Bold',
29 'Times-Italic' => 'Times-Italic',
30 'Times-BoldItalic' => 'Times-BoldItalic',
31 'Courier' => 'Courier',
32 'Courier-Bold' => 'Courier-Bold',
33 'Courier-Oblique' => 'Courier-Oblique',
34 'Courier-BoldOblique' => 'Courier-BoldOblique',
35 'Helvetica' => 'Helvetica',
36 'Helvetica-Bold' => 'Helvetica-Bold',
37 'Helvetica-Oblique' => 'Helvetica-Oblique',
38 'Helvetica-BoldOblique' => 'Helvetica-BoldOblique',
39 'Symbol' => 'Symbol',
40 'ZapfDingbats' => 'ZapfDingbats',
41 'TR' => 'Times-Roman',
42 'TB' => 'Times-Bold',
43 'TI' => 'Times-Italic',
44 'TBI' => 'Times-BoldItalic',
45 'C' => 'Courier',
46 'CB' => 'Courier-Bold',
47 'CO' => 'Courier-Oblique',
48 'CBO' => 'Courier-BoldOblique',
49 'H' => 'Helvetica',
50 'HB' => 'Helvetica-Bold',
51 'HO' => 'Helvetica-Oblique',
52 'HBO' => 'Helvetica-BoldOblique',
53 'S' => 'Symbol',
54 'Z' => 'ZapfDingbats'
55 );
56  
57 0 ▶sub __hex2rgb {
Subroutine __hex2rgb: not called CC=2 SCAR=17.9
58 0 my $hex = shift;
59 0 ▶ my $c = eval { Number::RGB->new( hex => $hex ) }
Branch: 1 branch
BranchTrueFalseTotal
my $c = eval { do { "Number::RGB"->new("hex", $hex) } } or croak("Could not interpret color '${hex}' as hex") 0 0 0
60 or croak "Could not interpret color '$hex' as hex";
61  
62 0 return map $_/255, @{ $c->rgb };
63 }
64  
65 0 ▶sub __inv_y {
Subroutine __inv_y: not called CC=2 SCAR=17.9
66 0 my $self = shift;
67 0 $_ = $self->page_size->[1] - $_ for @_; @_
68 0 }
69  
70 0 ▶sub _line {
Subroutine _line: not called CC=1 SCAR=6.9
71 0 my $self = shift;
72 0 my ( $x1, $y1, $x2, $y2 ) = @_;
73 0 $self->__inv_y($y1, $y2);
74 0 prAdd "$x1 $y1 m $x2 $y2 l S";
75  
76 0 $self;
77 }
78  
79 0 ▶sub _str_width {
Subroutine _str_width: not called CC=4 SCAR=30.0
80 0 my $self = shift;
81 0 my $str = shift;
82  
83 0 ▶ return prStrWidth(
Condition0%0/6
prStrWidth($str, $self->_cur_font // "Helvetica", $self->_cur_size // 12) // 0
l!l
00
$self->_cur_font // "Helvetica"
l!l
00
$self->_cur_size // 12
l!l
00
MC/DC0%0/1
prStrWidth($str, $self->_cur_font // "Helvetica", $self->_cur_size // 12) // 0
prStrWidth($str, $self->_cur_font // "Helvetica", $self->_cur_size // 12)
MC/DC0%0/1
$self->_cur_font // "Helvetica"
$self->_cur_font
MC/DC0%0/1
$self->_cur_size // 12
$self->_cur_size
Truth table0%0/2
A
A= prStrWidth($str, $self->_cur_font // "Helvetica", $self->_cur_size // 12)
Aresult
11
00
Truth table0%0/2
A
A= $self->_cur_font
Aresult
11
00
Truth table0%0/2
A
A= $self->_cur_size
Aresult
11
00
84 $str,
85 $self->_cur_font//'Helvetica',
86 $self->_cur_size//12
87 ) // 0;
88 }
89  
90 0 ▶sub _stroke {
Subroutine _stroke: not called CC=1 SCAR=6.9
91 0 my $self = shift;
92 0 my $weight = shift;
93 0 prAdd "$weight w";
94  
95 0 $self;
96 }
97  
98 0 ▶sub add_fonts {
Subroutine add_fonts: not called CC=3 SCAR=24.8
Pod documented
99 0 my $self = shift;
100 0 my %fonts = @_;
101  
102 0 for ( keys %fonts ) {
103 0 ▶ $STD_FONTS{$_} and croak "Font name '$_' conflicts with one of the "
Branch: 1 branch
BranchTrueFalseTotal
$STD_FONTS{$_} and croak("Font name '${_}' conflicts with one of the " . 'standard font names. Please choose another one') 0 0 0
104 . 'standard font names. Please choose another one';
105 0 $self->_fonts->{$_} = $fonts{ $_ };
106 };
107  
108 0 $self;
109 }
110  
111 0 ▶sub color {
Subroutine color: not called CC=5 SCAR=34.0
Pod documented
112 0 my $self = shift;
113 0 ▶ @_ = @{$_[0]} if @_ == 1 and ref $_[0] eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 and ref $_[0] eq "ARRAY" 0 0 0
Condition0%0/3
@_ == 1 and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "ARRAY"
@_ == 1 ref $_[0] eq "ARRAY"
Truth table0%0/3
A and B
A= @_ == 1B= ref $_[0] eq "ARRAY"
ABresult
0X0
100
111
114  
115 0 ▶ my ( $r, $g, $b )
Branch: 2 branches
BranchTrueFalseTotal
@_ == 0 ? : 0 0 0
@_ == 1 ? : 0 0 0
116 = @_ == 0 ? (0, 0, 0) # default to black
117 : @_ == 1
118 ? __hex2rgb( $_[0] ) # hex color
119 : @_; # rgb tuple
120  
121 0 $self->_cur_color([$r, $g, $b]);
122 0 prAdd "n $r $g $b RG $r $g $b rg\n";
123  
124 0 $self;
125 }
126  
127 0 ▶sub end {
Subroutine end: not called CC=1 SCAR=6.9
Pod documented
128 0 my $self = shift;
129 0 prEnd;
130 }
131  
132 0 ▶sub font {
Subroutine font: not called CC=4 SCAR=30.0
Pod documented
133 0 my $self = shift;
134 0 my $name = shift;
135  
136 0 ▶ $STD_FONTS{$name} or $self->_fonts->{$name}
Branch: 1 branch
BranchTrueFalseTotal
$STD_FONTS{$name} or $self->_fonts->{$name} or croak("Unknown font '${name}'") 0 0 0
Condition0%0/3
$STD_FONTS{$name} or $self->_fonts->{$name}
l!l&&r!l&&!r
000
MC/DC0%0/2
$STD_FONTS{$name} or $self->_fonts->{$name}
$STD_FONTS{$name} $self->_fonts->{$name}
Truth table0%0/3
A or B
A= $STD_FONTS{$name}B= $self->_fonts->{$name}
ABresult
1X1
011
000
137 or croak "Unknown font '$name'";
138  
139 0 ▶ $STD_FONTS{$name} ? prFont($name) : prTTFont( $self->_fonts->{$name} );
Branch: 1 branch
BranchTrueFalseTotal
$STD_FONTS{$name} ? : 0 0 0
140 0 $self->_cur_font($name);
141  
142 0 $self;
143 }
144  
145 0 ▶sub mixin {
Subroutine mixin: not called CC=2 SCAR=17.9
Pod documented
146 0 my $self = shift;
147 0 my ( $doc, $page ) = @_;
148 0 ▶ prForm { file => $doc, page => $page//1 };
Condition0%0/2
$page // 1
l!l
00
MC/DC0%0/1
$page // 1
$page
Truth table0%0/2
A
A= $page
Aresult
11
00
149  
150 0 $self;
151 }
152  
153 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
154 0 my ( $class, $filename ) = ( shift, shift );
155 0 my $self = $class->SUPER::new( @_ );
156  
157 0 prFile $filename;
158 0 prMbox ( 0, 0, $self->page_size->[0], $self->page_size->[1] );
159 0 $self->size;
160  
161 0 $self;
162 }
163  
164 0 ▶sub page {
Subroutine page: not called CC=1 SCAR=6.9
Pod documented
165 0 my $self = shift;
166 0 prPage;
167  
168 0 $self;
169 }
170  
171 0 ▶sub pic {
Subroutine pic: not called CC=5 SCAR=34.0
Pod documented
172 0 my ($self, $pic, %args) = @_;
173 0 my ($width, $height, $int_name);
174 0 ▶ if (ref $pic eq 'SCALAR') { # we have a data string rather than file
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pic eq 'SCALAR') { } 0 0 0
175 0 ▶ open my $fh, '<', $pic or die "Failed to read image bytes: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $pic or die "Failed to read image bytes: $!" 0 0 0
176 0 ($width, $height) = Image::Size::imgsize($fh);
177 0 $int_name = prJpeg $$pic, $width, $height, 1;
178 }
179 else {
180 0 ($width, $height) = Image::Size::imgsize($pic);
181 0 $int_name = prJpeg $pic, $width, $height, 0;
182 }
183  
184 0 ▶ if ($args{scale}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"scale"}) 0 0 0
185 0 $width *= $args{scale};
186 0 $height *= $args{scale};
187 }
188  
189 0 ▶ my ($x, $y) = map $_||0, @args{qw/x y/};
Condition0%0/2
$_ || 0
l!l
00
MC/DC0%0/1
$_ || 0
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
190 0 $self->__inv_y($y);
191 0 $y -= $height;
192  
193 0 my $str = "q\n";
194 0 $str .= "$width 0 0 $height $x $y cm\n";
195 0 $str .= "/$int_name Do\n";
196 0 $str .= "Q\n";
197 0 prAdd $str;
198  
199 0 $self;
200 }
201  
202 0 ▶sub raw {
Subroutine raw: not called CC=1 SCAR=6.9
Pod documented
203 0 my $self = shift;
204 0 prAdd shift;
205 0 $self;
206 }
207  
208 0 ▶sub rule {
Subroutine rule: not called CC=3 SCAR=24.8
Pod documented
209 0 my ( $self, %new ) = @_;
210 0 my $rules = $self->_rules;
211  
212 0 for ( keys %new ) {
213 0 $rules->{$_} = $new{$_};
214 0 ▶ delete $rules->{$_} unless defined $rules->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rules->{$_} 0 0 0
215 }
216  
217 0 $self;
218 }
219  
220 0 ▶sub size {
Subroutine size: not called CC=2 SCAR=17.9
Pod documented
221 0 my $self = shift;
222 0 ▶ my $size = shift // 12;
Condition0%0/2
shift() // 12
l!l
00
MC/DC0%0/1
shift() // 12
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
223  
224 0 $self->_cur_size( $size );
225 0 prFontSize $size;
226 0 $self->_line_height( $size*1.4 );
227  
228 0 $self;
229 }
230  
231 0 ▶sub table {
Subroutine table: not called CC=6 SCAR=37.4
Pod documented
232 0 my $self = shift;
233 0 my %conf = @_;
234  
235 DRAW: {
236 0 ▶ $conf{row_height} ||= $self->_line_height;
Condition0%0/3
$conf{"row_height"} ||= $self->_line_height
l!l&&r!l&&!r
000
MC/DC0%0/2
$conf{"row_height"} ||= $self->_line_height
$conf{"row_height"} $self->_line_height
Truth table0%0/3
A ||= B
A= $conf{"row_height"}B= $self->_line_height
ABresult
1X1
011
000
237  
238 0 my $t = Mojo::PDF::Primitive::Table->new( pdf => $self, %conf );
239 my @overflow = $t->draw
240 and $conf{max_height}
241 0 ▶ or return $self;
Branch: 1 branch
BranchTrueFalseTotal
my(@overflow) = $t->draw and $conf{"max_height"} or return $self 0 0 0
Condition0%0/3
my(@overflow) = $t->draw and $conf{"max_height"}
!ll&&!rl&&r
000
MC/DC0%0/2
my(@overflow) = $t->draw and $conf{"max_height"}
my(@overflow) = $t->draw $conf{"max_height"}
Truth table0%0/3
A and B
A= my(@overflow) = $t->drawB= $conf{"max_height"}
ABresult
0X0
100
111
242  
243 0 $conf{data} = $conf{max_height}->[1]->(\@overflow, \%conf, $self);
244 0 ▶ @{ $conf{data} || [] }
Branch: 1 branch
BranchTrueFalseTotal
@{$conf{"data"} or [];} and redo DRAW 0 0 0
Condition0%0/2
$conf{'data'} || []
l!l
00
MC/DC0%0/1
$conf{'data'} || []
$conf{'data'}
Truth table0%0/2
A
A= $conf{'data'}
Aresult
11
00
245 and redo DRAW;
246 }
247  
248 0 $self;
249 }
250  
251 0 ▶sub text {
Subroutine text: not called CC=5 SCAR=34.0
Pod documented
252 0 my $self = shift;
253 0 my ( $string, $x, $y, $align, $rotation ) = @_;
254 0 ▶ $self->_x( $x //= $self->_x );
Condition0%0/3
$x //= $self->_x
l!l&&r!l&&!r
000
MC/DC0%0/2
$x //= $self->_x
$x $self->_x
Truth table0%0/3
A //= B
A= $xB= $self->_x
ABresult
1X1
011
000
255  
256 # Don't switch to new line, if neither X nor Y were given;
257 0 ▶ $y //= $self->_y + ( @_ > 1 ? $self->_line_height : 0 );
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 0 0
Condition0%0/2
$y //= $self->_y + (@_ > 1 ? $self->_line_height : 0)
l!l
00
MC/DC0%0/1
$y //= $self->_y + (@_ > 1 ? $self->_line_height : 0)
$y
Truth table0%0/2
A
A= $y
Aresult
11
00
258 0 $self->_y( $y );
259  
260 0 my @lines = split /\n/, $string;
261 0 for ( 0 .. $#lines ) {
262 0 $self->_text(
263 $lines[$_],
264 $x,
265 $self->_y + $self->_line_height * $_,
266 $align,
267 $rotation,
268 );
269 }
270  
271 0 $self;
272 }
273  
274 0 ▶sub _text {
Subroutine _text: not called CC=15 SCAR=54.8
275 0 my $self = shift;
276 0 my ( $string, $x, $y, $align, $rotation ) = @_;
277 0 $self->_x( $x );
278 0 $self->_y( $y );
279  
280 0 my @text = ( { text => $string } );
281 0 for my $r ( values %{$self->_rules} ) {
282 0 my @new_text;
283 0 for my $bit ( @text ) {
284 0 ▶ if ( $bit->{marked} ) { push @new_text, $bit; next; }
Branch: 1 branch
BranchTrueFalseTotal
if ($bit->{"marked"}) 0 0 0
285  
286 0 my $mark = 0;
287  
288 0 for ( split /($r->{re})/, $bit->{text} ) {
289 0 ▶ if ( $_ =~ /$r->{re}/ ) { $mark = 1; next; }
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /$r->{"re"}/u) 0 0 0
290 0 ▶ if ( $mark ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mark) 0 0 0
291 0 $mark = 0;
292 0 push @new_text, { marked => 1, text => $_, %$r };
293 0 next;
294 }
295  
296 0 push @new_text, { text => $_ };
297 }
298 }
299  
300 0 @text = @new_text;
301 }
302  
303 0 my ( $orig_font, $orig_size, $orig_color )
304 = map $self->$_, qw/_cur_font _cur_size _cur_color/;
305  
306 0 for my $bit ( @text ) {
307 0 ▶ if ( $bit->{marked} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bit->{"marked"}) 0 0 0
308 0 ▶ if ( $bit->{font} ) { $self->font( $bit->{font} ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($bit->{"font"}) 0 0 0
309 0 ▶ if ( $bit->{size} ) { $self->size( $bit->{size} ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($bit->{"size"}) 0 0 0
310 0 ▶ if ( $bit->{color} ) { $self->color( $bit->{color} ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($bit->{"color"}) 0 0 0
311 0 $self->_text($bit->{text}, $self->_x, $self->_y,$align,$rotation );
312 0 ▶ if ( $bit->{font} ) { $self->font( $orig_font ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($bit->{"font"}) 0 0 0
313 0 ▶ if ( $bit->{size} ) { $self->size( $orig_size ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($bit->{"size"}) 0 0 0
314 0 ▶ if ( $bit->{color} ) { $self->color( $orig_color ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($bit->{"color"}) 0 0 0
315 0 next;
316 }
317 $self->_render_text_bit(
318 $bit->{text},
319 0 $self->_x,
320 $self->_y,
321 $align,
322 $rotation,
323 );
324 }
325  
326 0 $self;
327 }
328  
329 0 ▶sub _render_text_bit {
Subroutine _render_text_bit: not called CC=1 SCAR=6.9
330 0 my $self = shift;
331 0 my ( $string, $x, $y, $align, $rotation ) = @_;
332 0 $self->_x( (prText($x, $self->__inv_y($y), $string, $align, $rotation))[1] );
333  
334 0 $self;
335 }
336  
337 1 q|
338 There are only two hard problems in distributed systems:
339 2. Exactly-once delivery
340 1. Guaranteed order of messages 2. Exactly-once delivery
341 |;
342  
343 __END__
↑ Summary PDF/Primitive/Table.pm »