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