File Coverage

blib/lib/App/PDF/Link/Icons.pm
Criterion Covered Total %
statement 54 64 84.3
branch 16 30 53.3
condition n/a
subroutine 10 10 100.0
pod 0 1 0.0
total 80 105 76.1


line stmt bran cond sub pod time code
1             #! perl
2              
3 2     2   248598 use strict;
  2         15  
  2         52  
4 2     2   9 use warnings;
  2         4  
  2         40  
5 2     2   8 use utf8;
  2         3  
  2         11  
6              
7             package App::PDF::Link::Icons;
8              
9 2     2   67 use Carp;
  2         3  
  2         109  
10 2     2   24 use parent qw(Exporter);
  2         3  
  2         11  
11 2     2   508 use App::Packager;
  2         1429  
  2         9  
12              
13             our @EXPORT = qw( load_icon_images get_icon );
14              
15             my %icons;
16             my %idef;
17              
18             sub _load_icon_images {
19              
20 2     2   3040 my ( $env ) = @_;
21              
22 2         19 %idef =
23             ( mscz => 'builtin:MuseScore',
24             html => 'builtin:iRealPro',
25             sib => 'builtin:Sibelius',
26             xml => 'builtin:XML',
27             mxl => 'builtin:MXL',
28             musicxml => 'builtin:MXL',
29             abc => 'builtin:ABC',
30             );
31              
32 2 100       7 if ( $env->{all} ) {
33 1         4 $idef{jpg} = 'builtin:JPG';
34 1         3 $idef{jpeg} = 'builtin:JPG';
35 1         2 $idef{png} = 'builtin:PNG';
36 1         2 $idef{pdf} = 'builtin:PDF';
37 1         3 $idef{biab} = 'builtin:BandInABox';
38 1         2 for my $t ( qw( s m ) ) {
39 2         4 for my $i ( 0 .. 9, 'u' ) {
40 22         57 $idef{sprintf("%sg%s", $t, $i)} = 'builtin:BandInABox';
41             }
42             }
43             }
44 2         4 foreach ( keys %{ $env->{icons} } ) {
  2         10  
45 0 0       0 if ( $env->{icons}->{$_} ) {
46 0         0 $idef{$_} = $env->{icons}->{$_};
47             }
48             else {
49 0         0 delete( $idef{$_} );
50             }
51             }
52 2 100       8 $idef{' fallback'} = 'builtin:Document' if $env->{all};
53              
54 2         3 return;
55             }
56              
57             sub get_icon {
58 44     44 0 845 my ( $env, $pdf, $ext ) = ( $_[0], $_[1], lc($_[2]) );
59              
60             # Return the image object if we already have it.
61 44 100       116 return $icons{$ext} if exists $icons{$ext};
62              
63             # Note. There's some work to do.
64 12 50       33 _load_icon_images($env) unless %idef;
65              
66 12         42 my ( $type, $file ) = ( $ext, $idef{$ext} );
67 12 100       37 if ( $file ) {
68 11 50       147 if ( $file =~ /^builtin:(.*)/ ) {
69 11         1592 my $data
70             = eval( "require " . __PACKAGE__ . "::" . $1 . ";" .
71             "\\" . __PACKAGE__ . "::" . $1 . "::" . "icon();" );
72 11 50       71 croak("No icon data for $file") unless $data;
73 11     1   158 open( my $fd, '<:raw', $data );
  1         5  
  1         2  
  1         6  
74 11         685 $icons{$type} = $pdf->image_png($fd);
75 11         17948587 close($fd);
76             }
77             else {
78 0 0       0 croak("$file: $!") unless -r $file;
79 0 0       0 if ( $file =~ /\.png$/i ) {
    0          
    0          
80 0         0 $icons{$type} = $pdf->image_png($file);
81             }
82             elsif ( $file =~ /\.jpe?g$/i ) {
83 0         0 $icons{$type} = $pdf->image_jpeg($file);
84             }
85             elsif ( $file =~ /\.gif$/i ) {
86 0         0 $icons{$type} = $pdf->image_gif($file);
87             }
88             else {
89 0         0 croak("$file: Unsupported file type");
90             }
91             }
92 11         156 foreach ( keys %idef ) {
93 217 100       433 $icons{$_} = $icons{$type} if $idef{$_} eq $file;
94             }
95             }
96 12 100       110 return $icons{$ext} if $icons{$ext};
97 1 50       5 return $icons{' fallback'} if defined $icons{' fallback'};
98 0         0 return;
99             }
100              
101             # For testing.
102             sub __icons {
103 2     2   10 my $pdf = shift;
104 2         11 get_icon( undef, $pdf, $_ ) for keys %idef;
105 2         15 \%icons;
106             };
107              
108             1;