| 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
| |||||||||||||||||||||||
| 20 | 4 | ▶ | croak "No default specified" unless defined($self->{-f}->{undef}); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 21 | 4 | ▶ | croak "Insufficient number of fonts, 2 or more please" unless keys(%{$self->{-f}}) > 1; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 61 | 380 | my($c, $R); | |||||||||||||||||||||
| 62 | 380 | ▶ | if( ref($self->{-f}) eq 'HASH' ){ | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 63 | 380 | ▶ | while( my($k,$v) = each(%{$self->{-f}}) ){ | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 64 | 380 | ▶ | if( chr($i) =~ /$v/ ){ | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 84 | |||||||||||||||||||||||
| 85 | |||||||||||||||||||||||
| 86 | 276 | ▶ | my $ws = $self->{-m} == 0 ? $c->[0] : sum(@$c[0,1,2]); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 87 | 276 | ▶ | if( $self->{-v} eq 'top' ){ | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 94 | 69 | ▶ | push(@$c, (' 'x$ws)x$b) if $b; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 103 | 69 | ▶ | push(@$c, (' 'x$ws)x$b) if $b; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 130 | 0 | $val = '[qw/'. join(' ', @$val) . '/]'; | |||||||||||||||||||||
| 131 | 0 | ▶ | if( $opt eq '-f' ){ | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 132 | 0 | ▶ | for(my $f=0; $f< scalar @{$self->{_map}}; $f++ ){ | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||
| 153 | 0 | return $font; | |||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||
| 155 | |||||||||||||||||||||||
| 156 | 4 | 1; | |||||||||||||||||||||
| 157 | __END__ | ||||||||||||||||||||||