CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Word wrapping question

 



kruzer
Deleted

Jun 26, 2000, 6:57 AM

Post #1 of 1 (615 views)
Word wrapping question Can't Post

Hello All,

I need some help on a word wrapping routine

($company) = _box_text(30,$company,1,4);

($slogan) = _box_text($slogan,$slogan,2,4,1);

I’ve been experimenting with the parameters and my results have not been how I want them to break exactly.

i.e. a test string -> “My Company Name”

result - > My
Company
Name
How would I solve this problem of breaking to soon?

sub _box_text($$$$$){

my $this = shift;

my $text = shift;

my $line_len = shift;

my $max_lines = shift;

my $right_pad = shift;

my $vertical_center = shift;

my $orig_text = $text;

my $n_lines = 0; #number of lines shown

my $left = $line_len;

my @lines;

my $line = ""; #first word in a string

my $word;

my $len;

my $new_text = ""; #text string is placed here

my $dropped = "";

my $i;

my $separator;

if ($text !~ /\s+$/)

{

$text = "$text" . " ";

}

while ($text =~ /\G(\S+)\s+/g)

{

$word = $1;

$separator = $2;

if ($n_lines < $max_lines)

{

if (($len = length($line)) == 0)

{

$line = $word;

$left -= length($word);

}

elsif ($left < (length($word) + 1))

{

$line =~ s/_/ /g;

push(@lines, $line);

$line = "$word";

if ($separator !~ /^\s$/)

{

$line .= "$separator";

}

$left = $line_len - length($word);

++$n_lines;

}

else

{

$line = "$line" . " " . "$word";

$left -= (length($word) + 1);

}

}

elsif (length($dropped) == 0)

{

$dropped = $word;

}

else

{

$dropped = "$dropped" . " " . "$word";

}

}

if ($dropped != "")

{

print "WARNING: '$orig_text' truncated: '$dropped' dropped.\n";

}

if ( (length($line) > 0) && ($n_lines < $max_lines) )

{

$line =~ s/_/ /g;

push(@lines, $line);

++$n_lines;

}

$n_lines = 0;

foreach $line (@lines)

{

$new_text .= "\\n" if ($n_lines > 0); #text is entered here and \n char is added

$new_text .= "$line";

for ($i = 0; $i < $right_pad; $i++)

{

$new_text .= " ";

}

++$n_lines;

}

if ($vertical_center)

{

for ($i = ($max_lines - $n_lines) / 2; $i >= 1; $i--)

{

$new_text = "\n" . $new_text;

++$n_lines;

}

}

return ($new_text, $n_lines);

}

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives