File Coverage

blib/lib/Text/Banner.pm
Criterion Covered Total %
statement 120 120 100.0
branch 32 32 100.0
condition 6 6 100.0
subroutine 10 10 100.0
pod 6 6 100.0
total 174 174 100.0


line stmt bran cond sub pod time code
1              
2             package Text::Banner;
3              
4 9     9   1608362 use warnings;
  9         22  
  9         309  
5 9     9   45 use strict;
  9         17  
  9         13204  
6              
7             our $VERSION = '2.01';
8              
9             my $data;
10             my $XL;
11              
12             sub new {
13 12   100 12 1 921 my $proto=shift; my $class=ref($proto)||$proto;
  12         76  
14 12         24 my $self={}; my $save=$/; undef $/; my ($byte,$var,$num);
  12         32  
  12         28  
  12         19  
15 12 100       38 unless (defined $XL) {
16 9         1003 foreach $byte (split //,unpack("u*", $data)) {
17 5238         5740 $var=ord $byte;
18 5238         7058 foreach $num (128,64,32,16,8,4,2,1) {
19 41904 100       63296 if (($var&$num)==$num) { $XL .=1; } else { $XL .=0; }
  12816         17009  
  29088         37993  
20             }
21             }
22             }
23 12         334 $self->{ORIENTATION}="H"; $self->{SIZE}=1; $/=$save;
  12         37  
  12         55  
24 12         82 return bless $self,$class;
25             }
26             sub rotate {
27 6     6 1 1338 my $self=shift;
28 6 100       21 return $self->{ORIENTATION} unless @_;
29 5         7 my $direction=shift;
30 5 100       22 return undef unless ($direction=~/^h|v/i);
31 4 100       11 if ($direction=~/h/i) {
32 2         4 $self->{ORIENTATION}="H";
33             } else {
34 2         5 $self->{ORIENTATION}="V";
35             }
36 4         8 return $self->{ORIENTATION};
37             }
38             sub size {
39 9     9 1 996 my $self=shift;
40 9 100       58 return $self->{SIZE} unless @_;
41 8         15 my $size=shift;
42             # Allow up to 5x blowup. After that its too grainy to be of decent use.
43 8 100 100     35 return undef unless ($size > 0 && $size <6);
44 6         13 $self->{SIZE}=$size;
45 6         13 return $size;
46             }
47             sub fill {
48 16     16 1 1442 my $self=shift;
49 16 100       67 return $self->{FILL} unless @_;
50 15         25 my $char=shift;
51 15 100       47 if ($char=~/reset/i) {
52 1         3 undef $self->{FILL};
53 1         2 return undef;
54             }
55 14         29 $char=substr($char,0,1); # only one character allowed for fill value.
56 14         32 $char=~s/[^\x20-\x7f]+//g;
57 14 100       50 $self->{FILL}=$char if length $char;
58 14         69 return $self->{FILL};
59             }
60             sub _CHANGE {
61 17     17   26 my $self=shift; my $char;
  17         26  
62 17         24 foreach $char (keys %{$self->{PIC}}) {
  17         55  
63 41         75 foreach (@{$self->{PIC}->{$char}}) {
  41         98  
64 369         821 s/0/ /g;
65 369 100       700 if (defined $self->{FILL}) { s/[^\s]|1/$self->{FILL}/g; } else { s/[^\s]/$char/g; }
  306         1458  
  63         202  
66             }
67             }
68             }
69             sub set {
70 16     16 1 1577 my $self=shift; my $string=shift; my ($char,$var,$pos,$temp,%map);
  16         39  
  16         27  
71 16 100       64 return undef unless defined $string;
72 15         23 undef @{$self->{STRING}};
  15         70  
73 15         64 undef $self->{PIC};
74 15         41 $string=~s/[^\x20-\x7f]+//g; # We only print ASCII characters 32 to 126. Strip out anything else.
75 15         27 @{$self->{STRING}}=split'',$string;
  15         63  
76 15         23 foreach (@{$self->{STRING}}) { $map{$_}=1 };
  15         43  
  42         95  
77 15         64 foreach $char (keys %map) {
78 35         46 $var=ord $char; $var-=32; $pos=$var*49;
  35         47  
  35         49  
79 35         68 $temp=substr($XL,$pos,49);
80 35         69 foreach (0,7,14,21,28,35,42,49) { push @{$self->{PIC}->{$char}}, substr($temp,$_,7); }
  280         309  
  280         703  
81 35         45 push @{$self->{PIC}->{$char}},"0000000"; # this is spacing between lines
  35         87  
82             }
83 15         69 return undef;
84             }
85             sub _BLOWUP {
86 9     9   13 my $self=shift; my ($dynamic,$output,$temp);
  9         10  
87 9         23 $dynamic='$self->{CURRENT_LINE}=~s/(.)/'.'$1' x $self->{SIZE}.'/mg;';
88 9         620 eval $dynamic;
89 9 100       61 if ($self->{ORIENTATION}=~/H/i) {
90 8         14 $temp=$self->{CURRENT_LINE};
91 8         21 $dynamic='$output .="'. '$temp\n' x $self->{SIZE}.'";';
92 8         415 eval $dynamic;
93             } else {
94 1         7 foreach (split /\n/,$self->{CURRENT_LINE}) {
95 14         34 $dynamic='$output .= "'.'$_\n' x $self->{SIZE}.'";';
96 14         719 eval $dynamic;
97             }
98             }
99 9         43 return $output;
100             }
101             sub get {
102 17     17 1 76 my $self=shift; my ($creation,$num,$char,$line,$pos,$temp);
  17         25  
103 17         61 $self->_CHANGE;
104 17 100       82 if ($self->{ORIENTATION}=~/h/i) {
105 15         48 foreach $num (0..7) {
106 120         307 undef $self->{CURRENT_LINE};
107 120         137 foreach (@{$self->{STRING}}) { $self->{CURRENT_LINE} .=${$self->{PIC}->{$_}}[$num]." "; }
  120         335  
  344         417  
  344         792  
108 120 100       253 if ($self->{SIZE}>1) {
109 8         18 $creation .=$self->_BLOWUP;
110             } else {
111 112         245 $creation .=$self->{CURRENT_LINE}."\n";
112             }
113             }
114             } else {
115 2         4 foreach $char (@{$self->{STRING}}) {
  2         5  
116 5         6 my @array=@{$self->{PIC}->{$char}};
  5         20  
117 5         9 undef $self->{CURRENT_LINE};
118 5         11 foreach $pos (0..6) {
119 35         47 foreach $line (6,5,4,3,2,1,0) { $self->{CURRENT_LINE}.=substr($array[$line],$pos,1); }
  245         354  
120 35         55 $self->{CURRENT_LINE}.="\n";
121             }
122 5         16 $creation .=$self->{CURRENT_LINE};
123             }
124 2 100       8 if ($self->{SIZE}>1) {
125 1         3 $self->{CURRENT_LINE}=$creation;
126 1         3 $creation=$self->_BLOWUP;
127             }
128             }
129 17         58 return $creation;
130             }
131              
132              
133             $data = <<'EOF';
134             M````````'#AP0`.'._=$````!0I_*?RA1])D/A,E]QI=!!=+',)##B+"
135             M@@```!A!`@0$!A@("!`@A@`B*?RB(``$"'P@0````!PX((```!\````````.
136             M'#@$$$$$$$`XB@P8*(X(,*!`@0^?00+Z!`_OH(%\!@OH$*%"_@@7^!`_`8+Y
137             M]!@?H,%]_A!!!`@0?08+Z#!?/H,%^!@OA!P0`$'!!PX`.'!!`(((("`@(``/
138             M@#X``("`@((((/H($<(`"'T&[=O0'P@HB@_X,']!@_H,'\^@P($""^_08,&#
139             M!_?X$#Y`@?_\"!\@0(#Z#`GP8+Z#!@_X,&"<$"!`@0<`@0(&#!?0HDCA(B0H
140             M$"!`@0/\''5DP8,&#AHR8L.#_@P8,&#__08/Z!`@/H,&#%A/?T&#^B0H+Z#`
141             M?`8+[^($"!`@1!@P8,&"^@P8,%$4$09,F3)DMH*(H(*(H,%$4$"!`C^"""""
142             M#^^0($"!`^@("`@("`O@0($"!/A!1$```````````'\X<$!`````&$D+]"A`
143             M/D+Y"A?`#R%`@0G@#Y"A0H7P!^@?($#\`_0/D"!``/(4"=">`(4+]"A0@`@0
144             M($"!``$"!`H3P!"B>)$2$`@0($"!^`0LUJ%"A`(6*E*C0@#R%"A0G@#Y"A?(
145             M$``\A0I41T`^0H7R)"`/(#P%">`'P@0($"`$*%"A0G@"%"A0DA@!"A0K6:$`
146             JA)#!A)"`(B@@0($`/P0000?G$"#`@0'!`@0`$"!!P$"!@@1QA)#`````
147             EOF
148              
149             1;
150              
151              
152             =head1 NAME
153              
154             Text::Banner - Create text resembling Unix banner command
155              
156             =head1 VERSION
157              
158             This document refers to Text::Banner version 2.01.
159              
160             =head1 SYNOPSIS
161              
162             use Text::Banner;
163             my $ban = Text::Banner->new();
164             $ban->set('MYTEXT');
165             $ban->size(3);
166             $ban->fill('*');
167             $ban->rotate('h');
168             print $ban->get();
169              
170             =head1 DESCRIPTION
171              
172             The B creates a large ASCII-representation of a defined string, like
173             the 'banner' command available in Unix. A string is passed to the module, and the
174             equivalent banner string is generated and returned for use. The string can be
175             scaled (blown up) from 100 to 500% of the base size. The characters used to
176             generate the banner image can be any character defined by the user (within a
177             limited range) or they can be the made up from whatever the current character
178             being generated happens to be. The banner can be created either vertically or
179             horizontally.
180              
181             =head1 METHODS
182              
183             =head2 new
184              
185             An object reference is created with the B method. The reference is then used
186             to define the string to create and for manipulation of the object. No specific order
187             is required for object manipulation, with the exception of the 'get' operation which
188             will return the string based upon the current object definitions.
189              
190             =head2 set
191              
192             The 'set' operation allows the user to specify the string to be generated.
193             There is no limit on the length of the string; however, generated strings that
194             are longer than the display output will continue onto the next line and
195             interlace with the first character that was generated - resulting in a messy,
196             difficult-to-read output. Some experimentation may be required to find the
197             ideal maximum length depending upon the environment you are using.
198              
199             =head2 size
200              
201             The 'size' operation provides functionality for blowing up the size of the
202             generated string from 100 to 500 percent of normal size. '1' is 100%, '2' is
203             200% and so on. The larger the defined size, the grainier the output
204             string becomes. When an object is first created, the size defaults to '1'.
205             Calling the 'size' method without any parameters will return the current
206             size definition.
207              
208             =head2 rotate
209              
210             The 'rotate' method allows switching between horizontal and vertical output.
211             Objects are created by default in horizontal mode. Calling the method
212             without any arguments will return the current output mode. Otherwise, specify
213             either 'h' for horizontal or 'v' for vertical output.
214              
215             =head2 fill
216              
217             The 'fill' operation defines how the returned string should be created. By
218             default, newly created objects will use the current ASCII character of the
219             character being generated. For example, creating the string 'Hello' without
220             changing the fill character will cause a string to be created where the 'H'
221             is made up of the letter 'H', the 'e' from the letter 'e', 'l' from 'l' and so
222             on. This can be changed if desired by calling the 'fill' operation with the
223             ASCII character you wish all characters of the string to be created from.
224             Once defined, the fill character remains constant until changed again. Calling
225             the fill operation with no parameters will return the currently defined fill
226             character. Calling the fill operation with the command 'reset' will remove the
227             fill character, and default back to the original behaviour as outlined above.
228              
229             =head2 get
230              
231             The 'get' operation is what causes the string to be generated based upon the
232             current object definitions. The object is generated and passed directly back
233             from the method. Therefore, it can either be printed directly or saved to a
234             variable for later use.
235              
236             =head1 EXAMPLES
237              
238             # Example 1:
239              
240             use Text::Banner;
241             my $h = Text::Banner->new();
242             $h->set('MYTEXT');
243             $h->fill('*');
244             foreach my $num (1..5) {
245             $h->size($num);
246             $h->rotate('h');
247             print $h->get();
248             $h->rotate('v');
249             print $h->get();
250             }
251              
252              
253             # Example 2:
254              
255             use Text::Banner;
256             my $h = Text::Banner->new();
257             $h->set('MYtext');
258             print $h->get();
259             $h->fill('/');
260             print $h->get();
261              
262             Example 2 would generate the following output:
263              
264             M M Y Y
265             MM MM Y Y ttttt eeeeee x x ttttt
266             M M M M Y Y t e x x t
267             M M M Y t eeeee xx t
268             M M Y t e xx t
269             M M Y t e x x t
270             M M Y t eeeeee x x t
271              
272             / / / /
273             // // / / ///// ////// / / /////
274             / / / / / / / / / / /
275             / / / / / ///// // /
276             / / / / / // /
277             / / / / / / / /
278             / / / / ////// / / /
279              
280             Consult the horizontal.txt and vertical.txt files that come with the
281             module for examples of what different sizes look like.
282              
283             =head1 NOTES
284              
285             Multiple objects can of course be generated; however, it should be kept in
286             mind that the object is not static and changing the defined string output
287             could be used as an alternative to multiple object creation as each created
288             object chews up about 4k of memory.
289              
290             Generated ASCII characters are restricted to those between 32 (space) and
291             126 (~). Those outside of these values are removed, and the resulting
292             generated string will not include them. The same restriction applies to the
293             fill character used for defining character generation.
294              
295             =head1 AUTHOR
296              
297             The original author is Stuart Lory (CPAN ID: LORY).
298              
299             Gene Sullivan (gsullivan@cpan.org) is a co-maintainer.
300              
301             =head1 COPYRIGHT AND LICENSE
302              
303             Copyright (c) 1999 Stuart Lory. All rights reserved.
304              
305             This module is free software; you can redistribute it and/or modify
306             it under the same terms as Perl itself. See L.
307              
308             =cut
309