| 1 | #!/usr/local/bin/perl -w | ||
| 2 | package Text::FIGlet; | ||
| 3 | use strict; | ||
| 4 | use vars qw'$VERSION %RE'; | ||
| 5 | $VERSION = '2.19.4'; #Actual code version: 2.19.1 | ||
| 6 | |||
| 7 | #~50us penalty w/ 2 constant calls for 5.005 | ||
| 8 | use constant PRIVb => 0xF0000; #Map neg chars into Unicode's private area | ||
| 9 | use constant PRIVe => 0xFFFFD; #0-31 are also available but unused. | ||
| 10 | use Carp qw(carp croak); | ||
| 11 | use File::Spec; | ||
| 12 | use File::Basename 'fileparse'; | ||
| 13 | |||
| 14 | |||
| 15 | if( $] >= 5.008 ){ | ||
| 16 | require Encode; #Run-time rather than compile-time, without an eval | ||
| 17 | import Encode; | ||
| 18 | eval 'sub _utf8_on {Encode::decode("utf8",shift)}'; | ||
| 19 | # sub _utf8_off {Encode::_utf8_off(@_)}'; | ||
| 20 | } #Next block from Encode::compat, but broadened from 5.6.1 to 5.6 | ||
| 21 | elsif ($] >= 5.006 and $] <= 5.007) { | ||
| 22 | eval 'sub _utf8_on { $_[0] = pack("U*", unpack("U0U*", $_[0])) } | ||
| 23 | sub Encode::_utf8_off { $_[0] = pack("C*", unpack("C*", $_[0])) }'; | ||
| 24 | } | ||
| 25 | else{ | ||
| 26 | local $^W = 0; | ||
| 27 | eval "sub _utf8_on{}; sub Encode::_utf8_off{};"; | ||
| 28 | } | ||
| 29 | |||
| 30 | |||
| 31 | my $thByte = '[\x80-\xBF]'; | ||
| 32 | %RE = ( | ||
| 33 | #XXX Should perhaps put 1 byte UTF-8 last as . instead, to catch ANSI | ||
| 34 | #XXX Alas that catches many other unfortunate things... | ||
| 35 | UTFchar => qr/([\x20-\x7F]|[\xC2-\xDF]$thByte|[\xE0-\xEF]$thByte{2}|[\xF0-\xF4]$thByte{3})/, | ||
| 36 | bytechar=> qr/(.)/s, | ||
| 37 | no => qr/(-?)((0?)(?:x[\da-fA-F]+|\d+))/, | ||
| 38 | ); | ||
| 39 | |||
| 40 | |||
| 41 | sub import{ | ||
| 42 | @_ = qw/UTF8chr UTF8ord UTF8len/ if grep(/:Encode/, @_); | ||
| 43 | |||
| 44 | if( @_ ) { | ||
| 45 | no strict 'refs'; | ||
| 46 | *{scalar(caller).'::'.$_} = $_ for grep/UTF8chr|UTF8ord|UTF8len/, @_; | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | |||
| 51 | sub new { | ||
| 52 | local $_; | ||
| 53 | my $proto = shift; | ||
| 54 | my %opt = @_; | ||
| 55 | my($class, @isect, %count); | ||
| 56 | my %class = (-f => 'Font', -C => 'Control'); | ||
| 57 | |||
| 58 | |||
| 59 | if( ref($opt{-f}) =~ /ARRAY|HASH/ ){ | ||
| 60 | $class = 'Text::FIGlet::Ransom'; | ||
| 61 | } | ||
| 62 | else{ | ||
| 63 | $count{$_}++ for (keys %opt, keys %class); | ||
| 64 | $count{$_} == 2 && push(@isect, $_) for keys %count; | ||
| 65 | croak("Cannot new both -C and -f") if scalar @isect > 1; | ||
| 66 | $class = 'Text::FIGlet::' . $class{shift(@isect) || '-f'}; | ||
| 67 | } | ||
| 68 | $class->new(@_); | ||
| 69 | } | ||
| 70 | |||
| 71 | |||
| 72 | sub UTF8chr{ | ||
| 73 | my $ord = shift || $_; | ||
| 74 | my @n; | ||
| 75 | |||
| 76 | #x00-x7f #1 byte | ||
| 77 | if( $ord < 0x80 ){ | ||
| 78 | @n = $ord; } | ||
| 79 | #x80-x7ff #2 bytes | ||
| 80 | elsif( $ord < 0x800 ){ | ||
| 81 | @n = (0xc0|$ord>>6, 0x80|$ord&0x3F ); } | ||
| 82 | #x800-xffff #3 bytes | ||
| 83 | elsif( $ord < 0x10000 ){ | ||
| 84 | @n = (0xe0|$ord>>12, | ||
| 85 | 0x80|($ord>>6)&0x3F, | ||
| 86 | 0x80|$ord&0x3F ); } | ||
| 87 | #x10000-x10ffff #4 bytes | ||
| 88 | elsif( $ord<0x20000 ){ | ||
| 89 | @n = (0xf0|$ord>>18, | ||
| 90 | 0x80|($ord>>12)&0x3F, | ||
| 91 | 0x80|($ord>>6)&0x3F, | ||
| 92 | 0x80|$ord&0x3F); } | ||
| 93 | else{ | ||
| 94 | warn "Out of range for UTF-8: $ord"; } | ||
| 95 | |||
| 96 | return pack "C*", @n; | ||
| 97 | } | ||
| 98 | |||
| 99 | |||
| 100 | sub UTF8len{ | ||
| 101 | my $str = shift || $_; | ||
| 102 | my $count = () = $str =~ m/$Text::FIGlet::RE{UTFchar}/g; | ||
| 103 | } | ||
| 104 | |||
| 105 | |||
| 106 | sub UTF8ord{ | ||
| 107 | my $str = shift || $_; | ||
| 108 | my $len = length ($str); | ||
| 109 | |||
| 110 | return ord($str) if $len == 1; | ||
| 111 | #This is a FIGlet specific error value | ||
| 112 | return 128 if $len > 4 || $len == 0; | ||
| 113 | |||
| 114 | my @n = unpack "C*", $str; | ||
| 115 | $str = (($n[-2] & 0x3F) << 6) + ($n[-1] & 0x3F); | ||
| 116 | $str += (($n[-3] & 0x1F) << 12) if $len ==3; | ||
| 117 | $str += (($n[-3] & 0x3F) << 12) if $len ==4; | ||
| 118 | $str += (($n[-4] & 0x0F) << 18) if $len == 4; | ||
| 119 | return $str; | ||
| 120 | } | ||
| 121 | |||
| 122 | |||
| 123 | sub _no{ | ||
| 124 | my($one, $two, $thr, $over) = @_; | ||
| 125 | |||
| 126 | my $val = ($one ? -1 : 1) * ( $thr eq 0 ? oct($two) : $two); | ||
| 127 | |||
| 128 | #+2 is to map -2 to offset zero (-1 is forbidden, modern systems have no -0) | ||
| 129 | $val += PRIVe + 2 if $one; | ||
| 130 | if( $one && $over && $val < PRIVb ){ | ||
| 131 | carp("Extended character out of bounds"); | ||
| 132 | return 0; | ||
| 133 | } | ||
| 134 | |||
| 135 | $val; | ||
| 136 | } | ||
| 137 | |||
| 138 | |||
| 139 | sub _canonical{ | ||
| 140 | my($defdir, $usrfile, $extre, $backslash) = @_; | ||
| 141 | return -e $usrfile ? $usrfile : | ||
| 142 | File::Spec->catfile($defdir, $usrfile); | ||
| 143 | |||
| 144 | #Dragons be here, was for pseudo-Windows tests/old Perls? | ||
| 145 | |||
| 146 | #Split things up | ||
| 147 | my($file, $path, $ext) = fileparse($usrfile, $extre); | ||
| 148 | |||
| 149 | $path =~ y/\\/\// if $backslash; | ||
| 150 | |||
| 151 | #Handle paths relative to current directory | ||
| 152 | my $curdir = File::Spec->catfile(File::Spec->curdir, ""); | ||
| 153 | $path = $defdir if $path eq $curdir && index($usrfile, $curdir) < 0; | ||
| 154 | |||
| 155 | |||
| 156 | #return canonicaled path | ||
| 157 | return File::Spec->catfile($path, $file.$ext); | ||
| 158 | } | ||
| 159 | |||
| 160 | local $_="Act kind of random and practice less beauty sense --ginoh"; | ||
| 161 | |||
| 162 | package Text::FIGlet::Control; | ||
| 163 | use strict; | ||
| 164 | use vars '$VERSION'; | ||
| 165 | use Carp 'croak'; | ||
| 166 | $VERSION = 2.15; | ||
| 167 | |||
| 168 | #'import' core support functions from parent with circular dependency | ||
| 169 | foreach( qw/_canonical _no/){ | ||
| 170 | no strict 'refs'; | ||
| 171 | *$_ = *{'Text::FIGlet::'.$_}; | ||
| 172 | } | ||
| 173 | |||
| 174 | sub new{ | ||
| 175 | my $proto = shift; | ||
| 176 | my $self = {-C=>[]}; | ||
| 177 | local($_, *FLC); | ||
| 178 | |||
| 179 | my $code = ''; | ||
| 180 | my(@t_pre, @t_post); | ||
| 181 | while( @_ ){ | ||
| 182 | my $s = shift; | ||
| 183 | if( $s eq '-C' ){ | ||
| 184 | push(@{$self->{-C}}, shift); } | ||
| 185 | else{ | ||
| 186 | $self->{$s} = shift; } | ||
| 187 | } | ||
| 188 | $self->{-d} ||= $ENV{FIGLIB} || '/usr/games/lib/figlet/'; | ||
| 189 | $self->{"_\\"} = 1 if $^O =~ /MSWin32|DOS/i; | ||
| 190 | |||
| 191 | |||
| 192 | # my $no = qr/0x[\da-fA-F]+|\d+/; | ||
| 193 | |||
| 194 | foreach my $flc ( @{$self->{-C}} ){ | ||
| 195 | $self->{'_file'} = _canonical($self->{-d}, | ||
| 196 | $flc, | ||
| 197 | qr/\.flc/, | ||
| 198 | $self->{"_\\"}); | ||
| 199 | open(FLC, $self->{'_file'}) || croak("$!: $flc [$self->{_file}]"); | ||
| 200 | while(<FLC>){ | ||
| 201 | next if /^flc2a|\s*#|^\s*$/; | ||
| 202 | |||
| 203 | #XXX Is this adequate? | ||
| 204 | $code .= 'use utf8;' if /^\s*u/; | ||
| 205 | |||
| 206 | if( /^\s*$Text::FIGlet::RE{no}\s+$Text::FIGlet::RE{no}\s*/ ){ | ||
| 207 | #Only needed for decimals?! | ||
| 208 | |||
| 209 | push @t_pre, sprintf('\\x{%x}', _no($1, $2, $3)); | ||
| 210 | push @t_post, sprintf('\\x{%x}', _no($4, $5, $6)); | ||
| 211 | } | ||
| 212 | elsif( /^\s*t\s+\\?$Text::FIGlet::RE{no}(?:-\\$Text::FIGlet::RE{no})?\s+\\?$Text::FIGlet::RE{no}(?:-\\$Text::FIGlet::RE{no})?\s*/ ){ | ||
| 213 | push @t_pre, sprintf( '\\x{%x}', _no( $1, $2, $3)); | ||
| 214 | push @t_post, sprintf( '\\x{%x}', _no( $7, $8, $9)); | ||
| 215 | $t_pre[-1] .= sprintf('-\\x{%x}', _no( $4, $5, $6)) if$5; | ||
| 216 | $t_post[-1].= sprintf('-\\x{%x}', _no($10,$11,$12))if$11; | ||
| 217 | } | ||
| 218 | elsif( /^\s*t\s+([^\s](?:-[^\s])?)\s+([^\s](?:-[^\s])?)\s*/ ){ | ||
| 219 | push @t_pre, $1; | ||
| 220 | push @t_post, $2; | ||
| 221 | } | ||
| 222 | if( /^\s*f/ || eof(FLC) ){ | ||
| 223 | @{$_} = map { s%/%\\/%g, $_ } @{$_} for( \@t_pre, \@t_post ); | ||
| 224 | $code .= 'tr/' . join('', @t_pre) . '/' . join('', @t_post) . '/;'; | ||
| 225 | @t_pre = @t_post = (); | ||
| 226 | } | ||
| 227 | } | ||
| 228 | close(FLC); | ||
| 229 | } | ||
| 230 | $self->{_sub} = eval "sub { local \$_ = shift; $code; return \$_ }"; | ||
| 231 | bless($self); | ||
| 232 | } | ||
| 233 | |||
| 234 | sub tr($){ | ||
| 235 | my $self = shift; | ||
| 236 | $self->{_sub}->( shift || $_ ); | ||
| 237 | } | ||
| 238 | 1; | ||
| 239 | package Text::FIGlet::Font; | ||
| 240 | use strict; | ||
| 241 | use vars qw($REwhite $VERSION); | ||
| 242 | use Carp qw(cluck confess); | ||
| 243 | use Symbol; #5.005 support | ||
| 244 | use Text::Wrap; | ||
| 245 | $VERSION = '2.19.3'; | ||
| 246 | |||
| 247 | #'import' core support functions from parent with circular dependency | ||
| 248 | foreach( qw/UTF8len UTF8ord _canonical _no _utf8_on/){ | ||
| 249 | no strict 'refs'; | ||
| 250 | *$_ = *{'Text::FIGlet::'.$_}; | ||
| 251 | } | ||
| 252 | |||
| 253 | |||
| 254 | sub new{ | ||
| 255 | shift(); | ||
| 256 | my $self = {_maxLen=>0, -U=>-1, -m=>-2, @_}; | ||
| 257 | $self->{-m} = -3 if defined($self->{-m}) && $self->{-m} eq '-0'; | ||
| 258 | $self->{-f} ||= $ENV{FIGFONT} ; | ||
| 259 | $self->{-d} ||= $ENV{FIGLIB} || '/usr/games/lib/figlet/'; | ||
| 260 | _load_font($self); | ||
| 261 | bless($self); | ||
| 262 | } | ||
| 263 | |||
| 264 | sub _load_font{ | ||
| 265 | my $self = shift(); | ||
| 266 | my $font = $self->{_font} = []; | ||
| 267 | my(@header, $header, $path, $ext); | ||
| 268 | local($_); | ||
| 269 | |||
| 270 | if ( $self->{-f} ) { | ||
| 271 | $self->{_file} = _canonical($self->{-d}, $self->{-f}, qr/\.[ft]lf/, | ||
| 272 | $^O =~ /MSWin32|DOS/i); | ||
| 273 | #XXX bsd_glob .[ft]lf | ||
| 274 | $self->{_file} = (glob($self->{_file}.'.?lf'))[0] unless -e $self->{_file}; | ||
| 275 | |||
| 276 | #open(FLF, $self->{_file}) || confess("$!: $self->{_file}"); | ||
| 277 | $self->{_fh} = gensym; #5.005 support | ||
| 278 | eval "use IO::Uncompress::Unzip"; #XXX sniff for 'PK\003\004'instead? | ||
| 279 | unless( $@ ){ | ||
| 280 | $self->{_fh} = eval{ IO::Uncompress::Unzip->new($self->{_file}) } || | ||
| 281 | confess("No such file or directory: $self->{_file}"); | ||
| 282 | } | ||
| 283 | else{ | ||
| 284 | open($self->{_fh}, '<'.$self->{_file}) || confess("$!: $self->{_file}"); | ||
| 285 | #$^W isn't mutable at runtime in 5.005, so we have to conditional eval | ||
| 286 | #to avoid "Useless use of constant in void context" | ||
| 287 | eval "binmode(\$fh, ':encoding(utf8)')" unless $] < 5.006; | ||
| 288 | } | ||
| 289 | } else { | ||
| 290 | my $fh = $self->{_fh} = *main::DATA; | ||
| 291 | while ( <$fh> ) { | ||
| 292 | last if /__END__/; | ||
| 293 | } | ||
| 294 | } | ||
| 295 | |||
| 296 | |||
| 297 | my $fh = $self->{_fh}; #5.005 support | ||
| 298 | chomp($header = <$fh>); #5.005 hates readline & $self->{_fh} :-/ | ||
| 299 | confess("Invalid FIGlet 2/TOIlet font") unless $header =~ /^[ft]lf2/; | ||
| 300 | |||
| 301 | #flf2ahardblank height up_ht maxlen smushmode cmt_count rtol | ||
| 302 | @header = split(/\s+/, $header); | ||
| 303 | $header[0] =~ s/^[ft]lf2.//; | ||
| 304 | #$header[0] = qr/@{[sprintf "\\%o", ord($header[0])]}/; | ||
| 305 | $header[0] = quotemeta($header[0]); | ||
| 306 | $self->{_header} = \@header; | ||
| 307 | |||
| 308 | if( defined($self->{-m}) && $self->{-m} eq '-2' ){ | ||
| 309 | $self->{-m} = $header[4]; | ||
| 310 | } | ||
| 311 | |||
| 312 | #Discard comments | ||
| 313 | <$fh> for 1 .. $header[5] || cluck("Unexpected end of font file") && last; | ||
| 314 | |||
| 315 | #Get ASCII characters | ||
| 316 | foreach my $i(32..126){ | ||
| 317 | &_load_char($self, $i) || last; | ||
| 318 | } | ||
| 319 | |||
| 320 | #German characters? | ||
| 321 | unless( eof($fh) ){ | ||
| 322 | my %D =(91=>196, 92=>214, 93=>220, 123=>228, 124=>246, 125=>252, 126=>223); | ||
| 323 | |||
| 324 | foreach my $k ( sort {$a <=> $b} keys %D ){ | ||
| 325 | &_load_char($self, $D{$k}) || last; | ||
| 326 | } | ||
| 327 | if( $self->{-D} ){ | ||
| 328 | $font->[$_] = $font->[$D{$_}] for keys %D; | ||
| 329 | #removal is necessary to prevent 2nd reference to same figchar, | ||
| 330 | #which would then become over-smushed; alas 5.005 can't delete arrays | ||
| 331 | $#{$font} = 126; #undef($font->[$_]) for values %D; | ||
| 332 | } | ||
| 333 | } | ||
| 334 | |||
| 335 | #ASCII bypass | ||
| 336 | close($fh) unless $self->{-U}; | ||
| 337 | |||
| 338 | #Extended characters, with extra readline to get code | ||
| 339 | until( eof($fh) ){ | ||
| 340 | $_ = <$fh> || cluck("Unexpected end of font file") && last; | ||
| 341 | |||
| 342 | /^\s*$Text::FIGlet::RE{no}/; | ||
| 343 | last unless $2; | ||
| 344 | my $val = _no($1, $2, $3, 1); | ||
| 345 | |||
| 346 | #Bypass negative chars? | ||
| 347 | if( $val > Text::FIGlet->PRIVb && $self->{-U} == -1 ){ | ||
| 348 | readline($fh) for 0..$self->{_header}->[1]-1; | ||
| 349 | } | ||
| 350 | else{ | ||
| 351 | #Clobber German chars | ||
| 352 | $font->[$val] = ''; | ||
| 353 | &_load_char($self, $val) || last; | ||
| 354 | } | ||
| 355 | } | ||
| 356 | close($fh); | ||
| 357 | |||
| 358 | |||
| 359 | #Fixed width | ||
| 360 | if( defined($self->{-m}) && $self->{-m} == -3 ){ | ||
| 361 | my $pad; | ||
| 362 | for(my $ord=0; $ord < scalar @{$font}; $ord++){ | ||
| 363 | next unless defined $font->[$ord]; | ||
| 364 | foreach my $i (-$header[1]..-1){ | ||
| 365 | #next unless exists($font->[$ord]->[2]); #55compat | ||
| 366 | next unless defined($font->[$ord]->[2]); | ||
| 367 | |||
| 368 | # The if protects from a a 5.6(.0)? bug | ||
| 369 | $font->[$ord]->[$i] =~ s/^\s{1,$font->[$ord]->[1]}// | ||
| 370 | if $font->[$ord]->[1]; | ||
| 371 | |||
| 372 | $pad = $self->{_maxLen} - UTF8len($font->[$ord]->[$i]); | ||
| 373 | # print STDERR "$pad = $self->{_maxLen} - UTF8len($font->[$ord]->[$i]);\n"; | ||
| 374 | $font->[$ord]->[$i] = " " x int($pad/2) . | ||
| 375 | $font->[$ord]->[$i] . " " x ($pad-int($pad/2)); | ||
| 376 | } | ||
| 377 | } | ||
| 378 | } | ||
| 379 | #Full width | ||
| 380 | elsif( defined($self->{-m}) && $self->{-m} == -1 ){ | ||
| 381 | for(my $ord=32; $ord < scalar @{$font}; $ord++){ | ||
| 382 | next unless defined $font->[$ord]; | ||
| 383 | foreach my $i (-$header[1]..-1){ | ||
| 384 | next unless $font->[$ord]->[$i]; | ||
| 385 | # The if protects from a a 5.6(.0)? bug | ||
| 386 | $font->[$ord]->[$i] =~ s/^\s{1,$font->[$ord]->[1]}// | ||
| 387 | if $font->[$ord]->[1]; | ||
| 388 | substr($font->[$ord]->[$i], 0, 0, ' 'x$font->[$ord]->[1]); | ||
| 389 | $font->[$ord]->[$i] .= ' 'x$font->[$ord]->[2]; | ||
| 390 | } | ||
| 391 | } | ||
| 392 | } | ||
| 393 | #Kern glyph boxes | ||
| 394 | elsif( !defined($self->{-m}) || $self->{-m} > -1 ){ | ||
| 395 | for(my $ord=32; $ord < scalar @{$font}; $ord++){ | ||
| 396 | next unless defined $font->[$ord]; | ||
| 397 | foreach my $i (-$header[1]..-1){ | ||
| 398 | next unless $font->[$ord]->[$i]; | ||
| 399 | # The if protects from a a 5.6(.0)? bug | ||
| 400 | $font->[$ord]->[$i] =~ s/^\s{1,$font->[$ord]->[1]}// | ||
| 401 | if $font->[$ord]->[1]; | ||
| 402 | } | ||
| 403 | } | ||
| 404 | } | ||
| 405 | } | ||
| 406 | |||
| 407 | |||
| 408 | sub _load_char{ | ||
| 409 | my($self, $i) = @_; | ||
| 410 | my $font = $self->{_font}; | ||
| 411 | my($length, $wLead, $wTrail, $end, $line, $l) = 0; | ||
| 412 | |||
| 413 | $wLead = $wTrail = $self->{_header}->[3]; | ||
| 414 | |||
| 415 | my $fh = $self->{_fh}; #5.005 support | ||
| 416 | |||
| 417 | my $REtrail; | ||
| 418 | foreach my $j (0..$self->{_header}->[1]-1){ | ||
| 419 | $line = $_ = <$fh> || | ||
| 420 | cluck("Unexpected end of font file") && return 0; | ||
| 421 | #This is the end.... this is the end my friend | ||
| 422 | unless( $REtrail ){ | ||
| 423 | /(.)\s*$/; | ||
| 424 | $end = $1; | ||
| 425 | #The negative leading anchor is for term.flf 0x40 | ||
| 426 | $REtrail = qr/(?<!^)([ $self->{_header}->[0]]+)\Q$end{1,2}\E?\s*$/; | ||
| 427 | } | ||
| 428 | if( $wLead && s/^(\s+)// ){ | ||
| 429 | $wLead = $l if ($l = length($1)) < $wLead; | ||
| 430 | } | ||
| 431 | else{ | ||
| 432 | $wLead = 0; | ||
| 433 | } | ||
| 434 | if( $wTrail && /$REtrail/ ){ | ||
| 435 | $wTrail = $l if ($l = length($1)) < $wTrail; | ||
| 436 | } | ||
| 437 | else{ | ||
| 438 | $wTrail = 0; | ||
| 439 | } | ||
| 440 | $length = $l if ($l = UTF8len($_) | ||
| 441 | -(s/(\Q$end\E+)$/$end/&&UTF8len($1)) ) > $length; | ||
| 442 | $font->[$i] .= $line; | ||
| 443 | } | ||
| 444 | #XXX :-/ stop trying at 125 in case of charmap in ~ or extended.... | ||
| 445 | $self->{_maxLen} = $length if $i < 126 && $self->{_maxLen} < $length; | ||
| 446 | |||
| 447 | #Ideally this would be /o but then all figchar's must have same EOL | ||
| 448 | $font->[$i] =~ s/\015|\Q$end\E{1,2}\s*\r?$//mg; | ||
| 449 | $font->[$i] = [$length,#maxLen | ||
| 450 | $wLead, #wLead | ||
| 451 | $wTrail,#wTrail | ||
| 452 | split(/\r|\r?\n/, $font->[$i])]; | ||
| 453 | return 1; | ||
| 454 | } | ||
| 455 | |||
| 456 | |||
| 457 | sub figify{ | ||
| 458 | my $self = shift(); | ||
| 459 | my $font = $self->{_font}; | ||
| 460 | my %opts = (-A=>'', -X=>'', -x=>'', -w=>'', -U=>0, @_); | ||
| 461 | my @buffer; | ||
| 462 | local $_; | ||
| 463 | |||
| 464 | $opts{-w} ||= 80; | ||
| 465 | |||
| 466 | #Prepare the input | ||
| 467 | $opts{-X} ||= $self->{_header}->[6] ? 'R' : 'L'; | ||
| 468 | if( $opts{-X} eq 'R' ){ | ||
| 469 | $opts{-A} = join('', reverse(split('', $opts{-A}))); | ||
| 470 | } | ||
| 471 | |||
| 472 | $opts{-A} =~ y/\t/ /; | ||
| 473 | $opts{-A} =~ s%$/%\n% unless $/ eq "\n"; | ||
| 474 | if( defined($self->{-m}) && $self->{-m} == -3 ){ | ||
| 475 | $Text::Wrap::columns = int($opts{-w} / $self->{_maxLen})+1; | ||
| 476 | $Text::Wrap::columns =2 if $Text::Wrap::columns < 2; | ||
| 477 | $opts{-A} = Text::Wrap::wrap('', '', $opts{-A}); | ||
| 478 | &Encode::_utf8_off($opts{-A}) if $] >= 5.008; | ||
| 479 | } | ||
| 480 | elsif( $opts{-w} > 0 ){ | ||
| 481 | &Encode::_utf8_off($opts{-A}) if $] >= 5.008; | ||
| 482 | $Text::Wrap::columns = $opts{-w}+1; | ||
| 483 | unless( $opts{-w} == 1 ){ | ||
| 484 | ($_, $opts{-A}) = ($opts{-A}, ''); | ||
| 485 | # $opts{-A} .= "\0"x(($font->[ ord($1) ]->[0]||1)-1) . $1 while /(.)/g; | ||
| 486 | while( $opts{-U} ? | ||
| 487 | /$Text::FIGlet::RE{UTFchar}/g : | ||
| 488 | /$Text::FIGlet::RE{bytechar}/g ){ | ||
| 489 | $opts{-A} .= "\0"x(($font->[ | ||
| 490 | $opts{-U} ? UTF8ord($1) : ord($1) | ||
| 491 | ]->[0]||1)-1) . $1; | ||
| 492 | } | ||
| 493 | } | ||
| 494 | #XXX pre 5.8 Text::Wrap is not Unicode happy? | ||
| 495 | $opts{-A} = Text::Wrap::wrap('', '', $opts{-A}); | ||
| 496 | $opts{-A} =~ tr/\0//d; | ||
| 497 | } | ||
| 498 | |||
| 499 | #Assemble glyphs | ||
| 500 | my $X = defined($self->{-m}) && $self->{-m} < 0 ? '' : "\000"; | ||
| 501 | foreach( split("\n", $opts{-A}) ){ | ||
| 502 | my(@lchars, @lines); | ||
| 503 | s/^\s*//o; #XXX | ||
| 504 | # push(@lchars, ord $1) while /(.)/g; | ||
| 505 | while( $opts{-U} ? | ||
| 506 | /$Text::FIGlet::RE{UTFchar}/g : | ||
| 507 | /$Text::FIGlet::RE{bytechar}/g ){ | ||
| 508 | push @lchars, ($opts{-U} ? UTF8ord($1) : ord($1)); | ||
| 509 | } | ||
| 510 | |||
| 511 | foreach my $i (-$self->{_header}->[1]..-1){ | ||
| 512 | my $line=''; | ||
| 513 | foreach my $lchar (@lchars){ | ||
| 514 | if( $font->[$lchar] ){ | ||
| 515 | $line .= $font->[$lchar]->[$i] . $X if $font->[$lchar]->[$i]; | ||
| 516 | } | ||
| 517 | else{ | ||
| 518 | $line .= $font->[32]->[$i] . $X; | ||
| 519 | } | ||
| 520 | } | ||
| 521 | |||
| 522 | $line =~ s/\000$//; | ||
| 523 | push @lines, $line; | ||
| 524 | } | ||
| 525 | |||
| 526 | #Kern glyphs? | ||
| 527 | if( !defined($self->{-m}) || $self->{-m} > -1 ){ | ||
| 528 | for(my $nulls = 0; $nulls < scalar @lchars ; $nulls++){ | ||
| 529 | my $matches = 0; | ||
| 530 | my @temp; | ||
| 531 | for(my $i=0; $i<scalar @lines; $i++){ | ||
| 532 | $matches += ($temp[$i] = $lines[$i]) =~ | ||
| 533 | s/^([^\000]*(?:\000[^\000]*){$nulls})(?: \000|\000(?: |\Z))/$1\000/; | ||
| 534 | |||
| 535 | #($_ = $temp[$i]) =~ s/(${stem}{$nulls})/$1@/; | ||
| 536 | #print "$nulls, $i) $matches == @{[scalar @lines]} #$_\n"; | ||
| 537 | if( $i == scalar(@lines)-1 && $matches == scalar @lines ){ | ||
| 538 | @lines = @temp; | ||
| 539 | $matches = 0; | ||
| 540 | $i = -1; | ||
| 541 | } | ||
| 542 | } | ||
| 543 | } | ||
| 544 | } | ||
| 545 | |||
| 546 | push @buffer, @lines; | ||
| 547 | } | ||
| 548 | |||
| 549 | |||
| 550 | #Layout | ||
| 551 | $opts{-x} ||= $opts{-X} eq 'R' ? 'r' : 'l'; | ||
| 552 | foreach my $line (@buffer){ | ||
| 553 | #Smush | ||
| 554 | if( !defined($self->{-m}) || $self->{-m} > 0 ){ | ||
| 555 | |||
| 556 | |||
| 557 | #Universal smush/overlap | ||
| 558 | $line =~ s/\000 //g; | ||
| 559 | $line =~ s/$Text::FIGlet::RE{UTFchar}\000//g; | ||
| 560 | } | ||
| 561 | else{ | ||
| 562 | $line =~ y/\000//d; | ||
| 563 | } | ||
| 564 | |||
| 565 | #Alignment | ||
| 566 | if( $opts{-x} eq 'c' ){ | ||
| 567 | $line = " "x(($opts{-w}-UTF8len($line))/2) . $line; | ||
| 568 | } | ||
| 569 | elsif( $opts{-x} eq 'r' ){ | ||
| 570 | $line = " "x($opts{-w}-UTF8len($line)) . $line; | ||
| 571 | } | ||
| 572 | |||
| 573 | #Replace hardblanks | ||
| 574 | $line =~ s/$self->{_header}->[0]/ /g; | ||
| 575 | } | ||
| 576 | |||
| 577 | |||
| 578 | if( $] < 5.006 ){ | ||
| 579 | return wantarray ? @buffer : join($/, @buffer).$/; | ||
| 580 | } | ||
| 581 | else{ | ||
| 582 | #Properly promote (back) to utf-8 | ||
| 583 | return wantarray ? map{_utf8_on($_)} @buffer : | ||
| 584 | _utf8_on($_=join($/, @buffer).$/); | ||
| 585 | } | ||
| 586 | |||
| 587 | |||
| 588 | } | ||
| 589 | 1; | ||
| 590 | #!/usr/bin/perl -w | ||
| 591 | package main; | ||
| 592 | use strict; | ||
| 593 | use vars '$VERSION'; | ||
| 594 | $VERSION = 2.19.2; | ||
| 595 | |||
| 596 | my %opts; | ||
| 597 | $opts{-C} = []; | ||
| 598 | $opts{$_} = undef for | ||
| 599 | qw(A D E L R U X c d e f h help -help l r x); | ||
| 600 | $opts{$_} = "0 but true" for | ||
| 601 | qw(I m w); | ||
| 602 | for (my $i=0; $i <= scalar @ARGV; $i++) { | ||
| 603 | last unless defined($ARGV[$i]); | ||
| 604 | shift && last if $ARGV[$i] eq '--'; | ||
| 605 | if( $ARGV[$i] =~ /^-N$/ ) { | ||
| 606 | shift; $i--; | ||
| 607 | $opts{-C} = []; | ||
| 608 | } | ||
| 609 | if( $ARGV[$i] =~ /^-C=?(.*)/ ) { | ||
| 610 | shift; $i--; | ||
| 611 | $opts{-C} = [ @{$opts{-C}}, | ||
| 612 | defined($1) && $1 ne '' ? | ||
| 613 | $1 : defined($opts{-C}) ? do{$i--; shift} : undef | ||
| 614 | ]; | ||
| 615 | next; | ||
| 616 | } | ||
| 617 | foreach my $key ( sort { length($b)-length($a) } keys %opts) { | ||
| 618 | if( $ARGV[$i] =~ /^-$key=?(.*)/ ){ | ||
| 619 | shift; $i--; | ||
| 620 | $opts{$key} = defined($1) && $1 ne '' ? | ||
| 621 | $1 : defined($opts{$key}) ? do{$i--; shift} : 1; | ||
| 622 | last; | ||
| 623 | } | ||
| 624 | } | ||
| 625 | } | ||
| 626 | defined($_) && $_ eq '0 but true' && ($_ = undef) for values %opts; | ||
| 627 | if( $opts{help}||$opts{h}||$opts{-help} ){ | ||
| 628 | eval "use Pod::Text;"; | ||
| 629 | die("Unable to print man page: $@\n") if $@; | ||
| 630 | pod2text(__FILE__); | ||
| 631 | exit 0; | ||
| 632 | } | ||
| 633 | |||
| 634 | $opts{I} ||= 0; | ||
| 635 | if( $opts{I} == 1 ){ | ||
| 636 | my @F = unpack("c*", $VERSION); | ||
| 637 | die(10_000 * $F[0] + 100 * $F[1] +$F[2], "\n"); | ||
| 638 | } | ||
| 639 | |||
| 640 | my $font = Text::FIGlet->new(-D=>$opts{D}&&!$opts{E}, | ||
| 641 | -d=>$opts{d}, | ||
| 642 | -m=>$opts{m}||-2, | ||
| 643 | -f=>$opts{f}); | ||
| 644 | @_ = map { -C=> $_ } @{$opts{-C}} if scalar @{$opts{-C}}; | ||
| 645 | my $ctrl = Text::FIGlet->new( @_, | ||
| 646 | -d=>$opts{d}) if @_; | ||
| 647 | |||
| 648 | if( $opts{I} == 2 ){ | ||
| 649 | die("$font->{-d}\n"); | ||
| 650 | } | ||
| 651 | if( $opts{I} == 3 ){ | ||
| 652 | die("$font->{-f}\n"); | ||
| 653 | } | ||
| 654 | |||
| 655 | my %figify = ( | ||
| 656 | -U=>$opts{U}, | ||
| 657 | -X=>($opts{L}&&'L')||($opts{R}&&'R'), | ||
| 658 | -m=>$opts{m}, | ||
| 659 | -w=>$opts{w}, | ||
| 660 | -x=>($opts{l}&&'l')||($opts{c}&&'c')||($opts{r}&&'r') ); | ||
| 661 | croak("figlet.pl: cannot use both -A and -e") if $opts{A} && $opts{e}; | ||
| 662 | if( $opts{e} ){ | ||
| 663 | $opts{A} = 1; | ||
| 664 | @ARGV = eval "@ARGV"; | ||
| 665 | croak($@) if $@; | ||
| 666 | } | ||
| 667 | if( $opts{A} ){ | ||
| 668 | $_ = join(' ', map($_ = $_ eq '' ? $/ : $_, @ARGV)); | ||
| 669 | #XXXtraneous space... | ||
| 670 | print scalar $font->figify(-A=>($ctrl ? $ctrl->tr() : $_), %figify); | ||
| 671 | exit 0; | ||
| 672 | } | ||
| 673 | else{ | ||
| 674 | Text::FIGlet::croak("Usage: figlet.pl -help\n") if @ARGV; | ||
| 675 | print scalar $font->figify(-A=>($ctrl ? $ctrl->tr() : $_), %figify) | ||
| 676 | while <STDIN>; | ||
| 677 | } | ||
| 678 | __DATA__ |