File Coverage

blib/lib/App/PDF/Link/Icons.pm
Criterion Covered Total %
statement 50 61 81.9
branch 15 30 50.0
condition n/a
subroutine 10 10 100.0
pod 0 1 0.0
total 75 102 73.5


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