| 1 | package Imager::Font::Wrap; | ||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use Imager; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use Imager::Font; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | our $VERSION = "1.005"; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 3 | *_first = \&Imager::Font::_first; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | # we can't accept the utf8 parameter, too hard at this level | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # the %state contains: | ||||||||||||||||||||||||||||
| 14 | # font - the font | ||||||||||||||||||||||||||||
| 15 | # im - the image | ||||||||||||||||||||||||||||
| 16 | # x - the left position | ||||||||||||||||||||||||||||
| 17 | # w - the width | ||||||||||||||||||||||||||||
| 18 | # justify - fill, left, right or center | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 0 | ▶ | sub _format_line { | ||||||||||||||||||||||||||
|
Subroutine
_format_line: not called CC=13 SCAR=52.0
| |||||||||||||||||||||||||||||
| 21 | 0 | my ($state, $spaces, $text, $fill) = @_; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 0 | $text =~ s/ +$//; | |||||||||||||||||||||||||||
| 24 | my $box = $state->{font}->bounding_box(string=>$text, | ||||||||||||||||||||||||||||
| 25 | 0 | size=>$state->{size}); | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 0 | my $y = $state->{linepos} + $box->global_ascent; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 0 | ▶ | if ($state->{bottom} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$state->{"bottom"} and $state->{"linepos"} + $box->font_height > $state->{"bottom"}
MC/DC0%0/2
$state->{"bottom"} and $state->{"linepos"} + $box->font_height > $state->{"bottom"}
$state->{"bottom"}
$state->{"linepos"} + $box->font_height > $state->{"bottom"}
Truth table0%0/3
A and B
A= $state->{"bottom"}B= $state->{"linepos"} + $box->font_height > $state->{"bottom"}
| |||||||||||||||||||||||||||||
| 30 | && $state->{linepos} + $box->font_height > $state->{bottom}) { | ||||||||||||||||||||||||||||
| 31 | 0 | $state->{full} = 1; | |||||||||||||||||||||||||||
| 32 | 0 | return 0; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 0 | ▶ | if ($text =~ /\S/ && $state->{im}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$text =~ /\S/ and $state->{"im"}
MC/DC0%0/2
$text =~ /\S/ and $state->{"im"}
$text =~ /\S/
$state->{"im"}
Truth table0%0/3
A and B
A= $text =~ /\S/B= $state->{"im"}
| |||||||||||||||||||||||||||||
| 36 | my $justify = $fill ? $state->{justify} : | ||||||||||||||||||||||||||||
| 37 | 0 | ▶ | $state->{justify} eq 'fill' ? 'left' : $state->{justify}; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 38 | 0 | ▶ | if ($justify ne 'fill') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 0 | my $x = $state->{x}; | |||||||||||||||||||||||||||
| 40 | 0 | ▶ | if ($justify eq 'right') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 41 | 0 | $x += $state->{w} - $box->advance_width; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | elsif ($justify eq 'center') { | ||||||||||||||||||||||||||||
| 44 | 0 | $x += ($state->{w} - $box->advance_width) / 2; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | $state->{font}->draw(image=>$state->{im}, string=>$text, | ||||||||||||||||||||||||||||
| 47 | x=>$x, 'y'=>$y, | ||||||||||||||||||||||||||||
| 48 | 0 | size=>$state->{size}, %{$state->{input}}); | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | else { | ||||||||||||||||||||||||||||
| 51 | 0 | (my $nospaces = $text) =~ tr/ //d; | |||||||||||||||||||||||||||
| 52 | my $nospace_bbox = $state->{font}->bounding_box(string=>$nospaces, | ||||||||||||||||||||||||||||
| 53 | 0 | size=>$state->{size}); | |||||||||||||||||||||||||||
| 54 | 0 | my $gap = $state->{w} - $nospace_bbox->advance_width; | |||||||||||||||||||||||||||
| 55 | 0 | my $x = $state->{x}; | |||||||||||||||||||||||||||
| 56 | 0 | $spaces = $text =~ tr/ / /; | |||||||||||||||||||||||||||
| 57 | 0 | ▶ | while (length $text) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | if ($text =~ s/^(\S+)//) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 59 | 0 | my $word = $1; | |||||||||||||||||||||||||||
| 60 | my $bbox = $state->{font}->bounding_box(string=>$word, | ||||||||||||||||||||||||||||
| 61 | 0 | size=>$state->{size}); | |||||||||||||||||||||||||||
| 62 | $state->{font}->draw(image=>$state->{im}, string=>$1, | ||||||||||||||||||||||||||||
| 63 | x=>$x, 'y'=>$y, | ||||||||||||||||||||||||||||
| 64 | 0 | size=>$state->{size}, %{$state->{input}}); | |||||||||||||||||||||||||||
| 65 | 0 | $x += $bbox->advance_width; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | elsif ($text =~ s/^( +)//) { | ||||||||||||||||||||||||||||
| 68 | 0 | my $sep = $1; | |||||||||||||||||||||||||||
| 69 | 0 | my $advance = int($gap * length($sep) / $spaces); | |||||||||||||||||||||||||||
| 70 | 0 | $spaces -= length $sep; | |||||||||||||||||||||||||||
| 71 | 0 | $gap -= $advance; | |||||||||||||||||||||||||||
| 72 | 0 | $x += $advance; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | else { | ||||||||||||||||||||||||||||
| 75 | 0 | die "This shouldn't happen\n"; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | 0 | $state->{linepos} += $box->font_height + $state->{linegap}; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 0 | 1; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | ▶ | sub wrap_text { | ||||||||||||||||||||||||||
|
Subroutine
wrap_text: not called CC=26 SCAR=65.5
Pod
documented
| |||||||||||||||||||||||||||||
| 86 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 87 | 0 | my %input = @_; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # try to get something useful | ||||||||||||||||||||||||||||
| 90 | 0 | my $x = _first(delete $input{'x'}, 0); | |||||||||||||||||||||||||||
| 91 | 0 | my $y = _first(delete $input{'y'}, 0); | |||||||||||||||||||||||||||
| 92 | 0 | my $im = delete $input{image}; | |||||||||||||||||||||||||||
| 93 | 0 | ▶ | my $imerr = $im || 'Imager'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$im || 'Imager'
MC/DC0%0/1
$im || 'Imager'
$im
Truth table0%0/2
A
A= $im
| |||||||||||||||||||||||||||||
| 94 | 0 | my $width = delete $input{width}; | |||||||||||||||||||||||||||
| 95 | 0 | ▶ | if (!defined $width) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 0 | ▶ | defined $im && $im->getwidth > $x | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $im and $im->getwidth > $x
MC/DC0%0/2
defined $im and $im->getwidth > $x
defined $im
$im->getwidth > $x
Truth table0%0/3
A and B
A= defined $imB= $im->getwidth > $x
| |||||||||||||||||||||||||||||
| 97 | or return $imerr->_set_error("No width supplied and can't guess"); | ||||||||||||||||||||||||||||
| 98 | 0 | $width = $im->getwidth - $x; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | my $font = delete $input{font} | ||||||||||||||||||||||||||||
| 101 | 0 | ▶ | or return $imerr->_set_error("No font parameter supplied"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 0 | my $size = _first(delete $input{size}, $font->{size}); | |||||||||||||||||||||||||||
| 103 | 0 | ▶ | defined $size | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | or return $imerr->_set_error("No font size supplied"); | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 0 | ▶ | 2 * $size < $width | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | or return $imerr->_set_error("Width too small for font size"); | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | my $text = delete $input{string}; | |||||||||||||||||||||||||||
| 110 | 0 | ▶ | defined $text | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | or return $imerr->_set_error("No string parameter supplied"); | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | my $justify = _first($input{justify}, "left"); | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | my %state = | ||||||||||||||||||||||||||||
| 116 | ( | ||||||||||||||||||||||||||||
| 117 | font => $font, | ||||||||||||||||||||||||||||
| 118 | im => $im, | ||||||||||||||||||||||||||||
| 119 | x => $x, | ||||||||||||||||||||||||||||
| 120 | w => $width, | ||||||||||||||||||||||||||||
| 121 | justify => $justify, | ||||||||||||||||||||||||||||
| 122 | 'y' => $y, | ||||||||||||||||||||||||||||
| 123 | linepos=>$y, | ||||||||||||||||||||||||||||
| 124 | size=>$size, | ||||||||||||||||||||||||||||
| 125 | input => \%input, | ||||||||||||||||||||||||||||
| 126 | 0 | ▶ | linegap => delete $input{linegap} || 0, | ||||||||||||||||||||||||||
|
Condition0%0/2
delete $input{'linegap'} || 0
MC/DC0%0/1
delete $input{'linegap'} || 0
delete $input{'linegap'}
Truth table0%0/2
A
A= delete $input{'linegap'}
| |||||||||||||||||||||||||||||
| 127 | ); | ||||||||||||||||||||||||||||
| 128 | 0 | $state{height} = delete $input{height}; | |||||||||||||||||||||||||||
| 129 | 0 | ▶ | if ($state{height}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 0 | $state{bottom} = $y + $state{height}; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | 0 | my $line = ''; | |||||||||||||||||||||||||||
| 133 | 0 | my $spaces = 0; | |||||||||||||||||||||||||||
| 134 | 0 | my $charpos = 0; | |||||||||||||||||||||||||||
| 135 | 0 | my $linepos = 0; | |||||||||||||||||||||||||||
| 136 | 0 | pos($text) = 0; # avoid a warning | |||||||||||||||||||||||||||
| 137 | 0 | ▶ | while (pos($text) < length($text)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | #print pos($text), "\n"; | ||||||||||||||||||||||||||||
| 139 | 0 | ▶ | if ($text =~ /\G( +)/gc) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 140 | #print "spaces\n"; | ||||||||||||||||||||||||||||
| 141 | 0 | $line .= $1; | |||||||||||||||||||||||||||
| 142 | 0 | $spaces += length($1); | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | elsif ($text =~ /\G(?:\x0D\x0A?|\x0A\x0D?)/gc) { | ||||||||||||||||||||||||||||
| 145 | #print "newline\n"; | ||||||||||||||||||||||||||||
| 146 | 0 | ▶ | _format_line(\%state, $spaces, $line, 0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | or last; | ||||||||||||||||||||||||||||
| 148 | 0 | $line = ''; | |||||||||||||||||||||||||||
| 149 | 0 | $spaces = 0; | |||||||||||||||||||||||||||
| 150 | 0 | $linepos = pos($text); | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | elsif ($text =~ /\G(\S+)/gc) { | ||||||||||||||||||||||||||||
| 153 | #print "word\n"; | ||||||||||||||||||||||||||||
| 154 | 0 | my $word = $1; | |||||||||||||||||||||||||||
| 155 | 0 | my $bbox = $font->bounding_box(string=>$line . $word, size=>$size); | |||||||||||||||||||||||||||
| 156 | 0 | ▶ | if ($bbox->advance_width > $width) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | 0 | ▶ | _format_line(\%state, $spaces, $line, 1) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | or last; | ||||||||||||||||||||||||||||
| 159 | 0 | $line = ''; | |||||||||||||||||||||||||||
| 160 | 0 | $spaces = 0; | |||||||||||||||||||||||||||
| 161 | 0 | $linepos = pos($text) - length($word); | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | 0 | $line .= $word; | |||||||||||||||||||||||||||
| 164 | # check for long words | ||||||||||||||||||||||||||||
| 165 | 0 | $bbox = $font->bounding_box(string=>$line, size=>$size); | |||||||||||||||||||||||||||
| 166 | 0 | ▶ | while ($bbox->advance_width > $width) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 0 | my $len = length($line) - 1; | |||||||||||||||||||||||||||
| 168 | 0 | $bbox = $font->bounding_box(string=>substr($line, 0, $len), | |||||||||||||||||||||||||||
| 169 | size=>$size); | ||||||||||||||||||||||||||||
| 170 | 0 | ▶ | while ($bbox->advance_width > $width) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | 0 | --$len; | |||||||||||||||||||||||||||
| 172 | 0 | $bbox = $font->bounding_box(string=>substr($line, 0, $len), | |||||||||||||||||||||||||||
| 173 | size=>$size); | ||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | 0 | ▶ | _format_line(\%state, 0, substr($line, 0, $len), 0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | or last; | ||||||||||||||||||||||||||||
| 177 | 0 | $line = substr($line, $len); | |||||||||||||||||||||||||||
| 178 | 0 | $bbox = $font->bounding_box(string=>$line, size=>$size); | |||||||||||||||||||||||||||
| 179 | 0 | $linepos = pos($text) - length($line); | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | elsif ($text =~ /\G\s/gc) { | ||||||||||||||||||||||||||||
| 183 | # skip a single unrecognized whitespace char | ||||||||||||||||||||||||||||
| 184 | #print "skip\n"; | ||||||||||||||||||||||||||||
| 185 | 0 | $linepos = pos($text); | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 0 | ▶ | if (length $line && !$state{full}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
length $line and not $state{"full"}
MC/DC0%0/2
length $line and not $state{"full"}
length $line
not $state{"full"}
Truth table0%0/3
A and not(B)
A= length $lineB= not $state{"full"}
| |||||||||||||||||||||||||||||
| 190 | 0 | ▶ | $linepos += length $line | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | if _format_line(\%state, 0, $line, 0); | ||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 0 | ▶ | if ($input{savepos}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | 0 | ${$input{savepos}} = $linepos; | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 0 | return ($x, $y, $x+$width, $state{linepos}); | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 3 | 1; | |||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | __END__ | ||||||||||||||||||||||||||||