×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Word wrapping question

Word wrapping question

Word wrapping question

(OP)
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);   

}

Dano
dskryzer@hotmail.com

What's your major malfunction

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close