lib/Text/FIGlet/Ransom.pm

filter: Statementstmt 67.6% 71 / 105 Branchbran 51.8% 28 / 54 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 85.7% 6 / 7 Podpod 50.0% 2 / 4 totaltotal 62.9% 107 / 170 CC 33 SCAR 36.8
CC
33
Cov
62%
freeze
90.0
new
30.1
sum
2.0
CRAP 39.5
« lib/Text/FIGlet/Font.pm ↑ Summary lib/Text/FIGlet/Control.pm »
1 package Text::FIGlet::Ransom;
2 4 require 5;
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use vars qw/$VERSION @ISA/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 $VERSION = 2.17;
7 4 @ISA = 'Text::FIGlet::Font';
8  
9 #Roll our own for 5.005, and remove somewhat heavy List::Util dependency
10 12 ▶sub max{ (sort @_)[-1]; }
Subroutine max: called CC=1 SCAR=0.0
Pod undocumented
11 276 ▶sub sum{ my $cnt; $cnt += $_ foreach @_; return $cnt}
Subroutine sum: called CC=2 SCAR=6.9
Pod undocumented
12  
13 4 ▶sub new{
Subroutine new: called CC=24 SCAR=34.0
Pod documented
14 4 shift();
15 4 my $self = {-U=>0, -v=>'center', -m=>-1, @_};
16 4 my(@fonts, %fonts);
17  
18  
19 4 ▶ if( ref($self->{-f}) eq 'HASH' ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'-f'} eq 'HASH') { } 4 0 4
20 4 ▶ croak "No default specified" unless defined($self->{-f}->{undef});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"-f"}{"undef"} 0 4 4
21 4 ▶ croak "Insufficient number of fonts, 2 or more please" unless keys(%{$self->{-f}}) > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$$self{"-f"};} > 1 0 4 4
22 4 $self->{_fonts} = [delete($self->{-f}->{undef}), keys %{$self->{-f}}];
23 }
24 else{
25 0 ▶ croak "Insufficient number of fonts, 2 or more please" unless scalar(@{$self->{-f}}) > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$self->{"-f"};} > 1 0 0 0
26 0 $self->{_fonts} = $self->{-f};
27 }
28  
29  
30 #Load the fonts
31 4 my $x =0;
32 4 foreach my $font ( @{$self->{_fonts}} ){
33 8 push(@fonts, Text::FIGlet::Font->new(%$self, -f=>$font));
34 8 $fonts{$font} = $x++;
35 }
36  
37  
38 #Synthesize a header
39 #Hardblank = DEL
40 4 $self->{_header}->[0] = "\x7F";
41 #Height
42 4 $self->{_header}->[1] = max( map {$_->{_header}->[1]} @fonts );
43 #Base height
44 4 $self->{_header}->[2] = max( map {$_->{_header}->[2]} @fonts );
45 #Max glyph width
46 4 $self->{_header}->[3] = $self->{_maxLen} = max( map {$_->{_maxLen}} @fonts );
47 #Smush = none
48 4 $self->{_header}->[4] = 0;
49 #Comment line count, calculated when dumping @_ ... include chr to font mapping?
50 #R2L = false
51 4 $self->{_header}->[6] = 0;
52  
53 4 ▶ if( $self->{-v} eq 'base' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"-v"} eq "base") 0 4 4
54 0 my $descender = max( map {$_->{_header}->[1] - $_->{_header}->[2]} @fonts );
55 0 $self->{_header}->[1] = $self->{_header}->[2] + $descender;
56 }
57  
58  
59 #Assemble the body
60 4 ▶ for(my $i=32; $i<127; $i++ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 127) 380 4 384
61 380 my($c, $R);
62 380 ▶ if( ref($self->{-f}) eq 'HASH' ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'-f'} eq 'HASH') { } 380 0 380
63 380 ▶ while( my($k,$v) = each(%{$self->{-f}}) ){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"-f"};}) 380 276 656
64 380 ▶ if( chr($i) =~ /$v/ ){
Branch: 1 branch
BranchTrueFalseTotal
if (chr($i) =~ /$v/) 104 276 380
65 104 $c = $fonts[$R=$fonts{$k}]->{_font}->[$i];
66 #Reset counter, may be more trouble than the short-circuit is worth
67 104 keys %{$self->{-f}};
68 104 last;
69 }
70 }
71 380 ▶ $c = $fonts[$R=0]->{_font}->[$i] unless $c;
Branch: 1 branch
BranchTrueFalseTotal
unless $c 276 104 380
72 }
73 else{
74 0 $R = rand(scalar(@fonts));
75 0 $c = $fonts[$R]->{_font}->[$i];
76 0 $self->{_map}->[$R] .= chr($i);
77 }
78  
79 #Vertical-alignment & padding
80 380 ▶ if( my $delta = $self->{_header}->[1] - $fonts[$R]->{_header}->[1] ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $delta = $self->{"_header"}[1] - $fonts[$R]{"_header"}[1]) 276 104 380
81 #Parens around qw for 5.005
82 local($self->{-v}) = (qw/top center center bottom/)[rand(4)]
83 276 ▶ if $self->{-v} eq 'random';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"-v"} eq "random" 0 276 276
84  
85  
86 276 ▶ my $ws = $self->{-m} == 0 ? $c->[0] : sum(@$c[0,1,2]);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'-m'} == 0 ? : 0 276 276
87 276 ▶ if( $self->{-v} eq 'top' ){
Branch: 4 branches
BranchTrueFalseTotal
if ($self->{'-v'} eq 'top') { } 69 207 276
elsif ($self->{'-v'} eq 'baseline') { } 69 138 207
elsif ($self->{'-v'} eq 'bottom') { } 69 69 138
elsif ($self->{'-v'} eq 'center') { } 69 0 69
88 69 push(@$c, (' 'x$ws)x$delta);
89 }
90 elsif( $self->{-v} eq 'baseline' ){
91 69 my $t = $self->{_header}->[2] - $fonts[$R]->{_header}->[2];
92 69 my $b = $self->{_header}->[1] - $fonts[$R]->{_header}->[1] - $t;
93 69 ▶ splice(@$c, 3, 0, (' 'x$ws)x$t) if $t;
Branch: 1 branch
BranchTrueFalseTotal
if $t 69 0 69
94 69 ▶ push(@$c, (' 'x$ws)x$b) if $b;
Branch: 1 branch
BranchTrueFalseTotal
if $b 69 0 69
95 }
96 elsif( $self->{-v} eq 'bottom' ){
97 69 splice(@$c, 3, 0, (' 'x$ws)x$delta);
98 }
99 elsif( $self->{-v} eq 'center' ){
100 69 my $t = int($delta/2);
101 69 my $b = $delta - $t;
102 69 ▶ splice(@$c, 3, 0, (' 'x$ws)x$t) if $t;
Branch: 1 branch
BranchTrueFalseTotal
if $t 69 0 69
103 69 ▶ push(@$c, (' 'x$ws)x$b) if $b;
Branch: 1 branch
BranchTrueFalseTotal
if $b 69 0 69
104 }
105 }
106  
107  
108 #XXX -m... freeze/thaw? horizontally center w/ padding -height..-1
109  
110 #Common hardblank
111 380 my $iHard=$fonts[$R]->{_header}->[0];
112 380 foreach my $j(-$self->{_header}->[1]..-1){
113 2280 $c->[$j]=~ s/$iHard/$self->{_header}->[0]/g;
114 #$c->[$j].= Text::FIGlet::UTF8len($c->[$j]);
115 }
116  
117 380 $self->{_font}->[$i] = $c;
118 }
119  
120 4 bless($self);
121 }
122  
123 0 ▶sub freeze{
Subroutine freeze: not called CC=9 SCAR=45.0
Pod documented
124 0 my $self = shift;
125 0 my $font;
126  
127 0 foreach my $opt ( sort grep {/^-/} keys %{$self} ){
128 0 my $val = $self->{$opt};
129 0 ▶ if( ref($val) eq 'ARRAY' ){
Branch: 2 branches
BranchTrueFalseTotal
if (ref $val eq 'ARRAY') { } 0 0 0
elsif (ref $val eq 'HASH') { } 0 0 0
130 0 $val = '[qw/'. join(' ', @$val) . '/]';
131 0 ▶ if( $opt eq '-f' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($opt eq "-f") 0 0 0
132 0 ▶ for(my $f=0; $f< scalar @{$self->{_map}}; $f++ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($f < scalar @{$self->{"_map"};}) 0 0 0
133 0 $val .= "\n#\tfont$f $self->{_map}->[$f]";
134 0 $self->{_header}->[5]++;
135 }
136 }
137 }
138 elsif( ref($val) eq 'HASH' ){
139 0 $val = '{undef,'. $self->{_fonts}->[0] .','. join(',',%{$val}) .'}';
140 }
141 0 $font .= sprintf "#%s => %s\n", $opt, $val;
142 0 $self->{_header}->[5]++;
143 }
144  
145 0 $font = sprintf("flf2a%s %s %s %s %s %s %s\n", @{$self->{_header}}). $font;
146  
147 0 ▶ for(my $i=32; $i<= scalar @{$self->{_font}}; $i++ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= scalar @{$self->{"_font"};}) 0 0 0
148 0 my $c = $self->{_font}->[$i];
149 0 foreach my $j(-$self->{_header}->[1]..-1){
150 0 ▶ $font .= $c->[$j] . ($j<-1?"\x1F\n":"\x1F\x1F\n");
Branch: 1 branch
BranchTrueFalseTotal
$j < -1 ? : 0 0 0
151 }
152 }
153 0 return $font;
154 }
155  
156 4 1;
157 __END__
« lib/Text/FIGlet/Font.pm ↑ Summary lib/Text/FIGlet/Control.pm »