File Coverage

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


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