Code: Select all
$image->Write("caption:'PerlMagick has support for a caption');
Code: Select all
$image->Write("caption:'PerlMagick has support for a caption');
First, yes you can word wrap using perl, BUT It will not do it correctly unless you also use a constant width font like 'courier'. Proportional fonts could result in large spaces at the end of the lines. I never did find a nice constant width font.
Second, the line spacing is set by the point size. That is the meaning of pointsize, the amount of space between lines! Fonts however may use all or none of that spacing, How much of the line spacing it uses, and the position of the baseline in that line space, is completely up to the font designer.
hermann2 wrote:anthony wrote: First, yes you can word wrap using perl, BUT It will not do it correctly unless you also use a constant width font like 'courier'. Proportional fonts could result in large spaces at the end of the lines. I never did find a nice constant width font.
This has not been my experience. I'm using a proportional font, and Gabe's code correctly adds up the individual width of each character, and wraps at exactly the right place.
Code: Select all
#!/usr/bin/perl
# This function will wrap at a space or hyphen, and if a word is longer than a
# line it will just break it at the end of the first line. To figure out the
# height of the text, pass the returned string to QueryMultilineFontMetrics.
#
# pass in the string to wrap, the IM object with font and size set, and the
# width you want to wrap to; returns new string
#
# From: Gabe Schaffer, IM Forum: f=7&t=3708 7 October 2004
#
sub Wrap
{
my ($text, $img, $maxwidth) = @_;
# figure out the width of every character in the string
#
my %widths = map(($_ => ($img->QueryFontMetrics(text=>$_))[4]),
keys %{{map(($_ => 1), split //, $text)}});
my (@newtext, $pos);
for (split //, $text) {
# check to see if we're about to go out of bounds
if ($widths{$_} + $pos > $maxwidth) {
$pos = 0;
my @word;
# if we aren't already at the end of the word,
# loop until we hit the beginning
if ( $newtext[-1] ne " "
&& $newtext[-1] ne "-"
&& $newtext[-1] ne "\n") {
unshift @word, pop @newtext
while ( @newtext && $newtext[-1] ne " "
&& $newtext[-1] ne "-"
&& $newtext[-1] ne "\n")
}
# if we hit the beginning of a line,
# we need to split a word in the middle
if ($newtext[-1] eq "\n" || @newtext == 0) {
push @newtext, @word, "\n";
} else {
push @newtext, "\n", @word;
$pos += $widths{$_} for (@word);
}
}
push @newtext, $_;
$pos += $widths{$_};
$pos = 0 if $newtext[-1] eq "\n";
}
return join "", @newtext;
}