File Coverage

lib/LEOCHARRE/FontFind.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package LEOCHARRE::FontFind;
2 1     1   11184 use strict;
  1         2  
  1         26  
3 1     1   3 use vars qw($VERSION @EXPORT_OK %EXPORT_TAGS @ISA @FONTPATH $CACHE $_files_found_in_fontpaths);
  1         1  
  1         55  
4 1     1   3 use Exporter;
  1         2  
  1         23  
5 1     1   3 use Carp;
  1         1  
  1         100  
6             $VERSION = sprintf "%d.%02d", q$Revision: 1.5 $ =~ /(\d+)/g;
7             # setting manually, no longer access to cvs
8             @ISA = qw/Exporter/;
9             @EXPORT_OK = qw(find_ttf find_ttfs);
10             %EXPORT_TAGS = ( all => \@EXPORT_OK );
11             @FONTPATH = ('/usr/share/fonts');
12 1     1   435 use Cache::File;
  0            
  0            
13             #use Smart::Comments;
14              
15             sub find_ttfs { # return whole list..
16             my $substring = shift;
17            
18             if( my @extra_FONTPATHS = @_ ){
19             push @FONTPATH, @extra_FONTPATHS;
20             _cache_reset();
21             }
22              
23             my @ttfs = _abs_ttfs();
24             my $ac = scalar @ttfs or warn("no ttf files at all") and return;
25            
26             ### ttf cached: $ac
27             ## cached are: @ttfs
28              
29             my @found;
30             if( $substring=~/\.ttf$/i){
31             ### then assume whole filename
32             @found = grep { /\Q$substring\E$/i } @ttfs;
33             }
34             else {
35             ### regular substring: $substring
36             @found = grep { /\Q$substring\E[^\/]*\.ttf$/i} @ttfs;
37             }
38              
39              
40             # sort by filename...
41             my %filename;
42             map { $_=~/([^\/]+)$/; $filename{$_} = $1 } @found;
43              
44             @found = sort { $filename{$a} cmp $filename{$b} } @found;
45              
46             ### grepped to: @found
47             @found or return;
48             @found; # return all
49             }
50              
51             sub find_ttf {
52             my $substring = shift;
53             my @found = find_ttfs($substring,@_) or return;
54             $found[0];
55             }
56              
57              
58              
59             sub _cache {
60             $CACHE ||= Cache::File->new( cache_root => "$ENV{HOME}/.fontfind/cache" ) or die;
61             }
62             sub _cache_reset { _cache()->clear('_files_found_in_fontpaths') }
63              
64             sub _files_found_in_fontpaths { # can be all files. . later we worry about
65             # grepping ttf, or whatever extension/name
66            
67             # did we already figure it out?
68             defined $_files_found_in_fontpaths and return $_files_found_in_fontpaths;
69            
70             # is it cached ?
71             if( $_files_found_in_fontpaths = _cache()->thaw('_files_found_in_fontpaths') ){
72             return $_files_found_in_fontpaths;
73             }
74            
75             ### finding fonts..
76            
77             # go ahead and find all files
78             my @found; # as is.. will not prevent dupes. . don't streamline early..
79             for (@FONTPATH){
80             push @found, (split(/\n/, `find '$_' -type f`));
81             }
82             _cache()->freeze('_files_found_in_fontpaths' => \@found, 'never' );
83              
84             $_files_found_in_fontpaths = [@found]; # or \@found ? .. may cause problems?
85              
86              
87             my $files_found = scalar @found;
88             ### $files_found
89              
90             return $_files_found_in_fontpaths;
91             }
92              
93             sub _abs_ttfs { ( grep { /[^\/]+.ttf$/i } @{_files_found_in_fontpaths()} ) }
94              
95              
96              
97             1;
98              
99              
100              
101              
102              
103              
104              
105              
106              
107              
108              
109             1;
110              
111             __END__