| 1 | package LEOCHARRE::FontFind; | ||
| 2 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use vars qw($VERSION @EXPORT_OK %EXPORT_TAGS @ISA @FONTPATH $CACHE $_files_found_in_fontpaths); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use Exporter; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 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 | 0 | ▶ | use Cache::File; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 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__ | ||