| 1 | # -*- Perl -*- | ||
| 2 | # TextBlock.pm | ||
| 3 | # An object that may be used to construct a block of text in PostScript | ||
| 4 | # | ||
| 5 | package PostScript::TextBlock; | ||
| 6 | use strict; | ||
| 7 | use PostScript::Metrics; | ||
| 8 | |||
| 9 | use vars qw($VERSION); | ||
| 10 | $VERSION = '0.06'; | ||
| 11 | |||
| 12 | # The valid text block attribute names | ||
| 13 | # | ||
| 14 | my @paramnames = ( 'text', 'font', 'size', 'leading'); | ||
| 15 | |||
| 16 | # The default attribute values | ||
| 17 | # | ||
| 18 | my %defaults = ( | ||
| 19 | text => '', | ||
| 20 | font => 'CharterBT-Roman', | ||
| 21 | size => 12, | ||
| 22 | leading => 16 | ||
| 23 | ); | ||
| 24 | sub new { | ||
| 25 | # The constructor method | ||
| 26 | # | ||
| 27 | my $proto = shift; # allow use as a class or object method | ||
| 28 | my $class = ref($proto) || $proto; # see perltoot man page | ||
| 29 | |||
| 30 | # A text block consists of a list of 'elements', | ||
| 31 | # (not to be confused with the PostScript::Elements object) | ||
| 32 | # | ||
| 33 | my $self = []; | ||
| 34 | |||
| 35 | return bless($self,$class); | ||
| 36 | } | ||
| 37 | |||
| 38 | sub addText { | ||
| 39 | # Add an element of text to the TextBlock | ||
| 40 | # | ||
| 41 | my $self = shift; | ||
| 42 | my %params = @_; | ||
| 43 | $params{'text'} =~ s/(\(|\))/\\$1/g; # escape parentheses | ||
| 44 | |||
| 45 | # Use the default values if an attribute is not given | ||
| 46 | # | ||
| 47 | foreach (@paramnames) { | ||
| 48 | $params{$_} = $defaults{$_} unless ($params{$_}); | ||
| 49 | } | ||
| 50 | push @$self, { %params }; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub numElements { | ||
| 54 | # Returns the number of elements in the TextBlock | ||
| 55 | # | ||
| 56 | my $self = shift; | ||
| 57 | return $#{@$self}+1; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub Write { | ||
| 61 | # The Write() method takes four parameters: w, h, x , and y, | ||
| 62 | # where w and h are the width and height of the block (in points), | ||
| 63 | # and x and y specify the upper left corner of the TextBlock (in the | ||
| 64 | # PostScript coordinate system). This method returns a string containing | ||
| 65 | # the PostScript code that generated the block, and a TextBlock object | ||
| 66 | # the contains the portion that doesn't fit within the given bounds. | ||
| 67 | # | ||
| 68 | my $self = shift; | ||
| 69 | my ($w, $h, $x, $y) = @_; | ||
| 70 | |||
| 71 | my ($x1, $y1) = ($x, $y); | ||
| 72 | my $returnval = ""; | ||
| 73 | my @remainder = (); | ||
| 74 | my ($maxlead, $wcount, $linebuffer) = (0, 0, ""); | ||
| 75 | my ($line, $word, @words); | ||
| 76 | my $wordwidth; | ||
| 77 | $returnval .= "0 setgray\n"; | ||
| 78 | |||
| 79 | my $element = {}; | ||
| 80 | my $index = 0; | ||
| 81 | $element = $self->[$index]; | ||
| 82 | |||
| 83 | my $maxindex = $self->numElements; | ||
| 84 | my $firstindex = 0; | ||
| 85 | |||
| 86 | ELEMENT: while (($index < $maxindex) && ($y1 >= ($y-$h))) { | ||
| 87 | $wcount = 0; | ||
| 88 | ($line, $word) = (undef, undef); | ||
| 89 | @words = (); | ||
| 90 | $linebuffer = ""; | ||
| 91 | $maxlead = 0; | ||
| 92 | $firstindex = $index; | ||
| 93 | |||
| 94 | # Loop until a complete line is formed, or | ||
| 95 | # until we run out of elements | ||
| 96 | # | ||
| 97 | LINE: while (($index < $maxindex) && $wcount<=$w) { | ||
| 98 | $linebuffer .= "/$element->{font} findfont\n"; | ||
| 99 | $linebuffer .= "$element->{size} scalefont setfont\n"; | ||
| 100 | |||
| 101 | # Calculate the maximum leading on this line | ||
| 102 | # | ||
| 103 | $maxlead = $element->{leading} if ($element->{leading} > $maxlead); | ||
| 104 | |||
| 105 | @words = split /( +|\t|\n)/, $element->{text}; | ||
| 106 | while (@words) { | ||
| 107 | $word = shift @words; | ||
| 108 | $wordwidth = PostScript::Metrics::stringwidth($word, | ||
| 109 | $element->{font}, | ||
| 110 | $element->{size}); | ||
| 111 | |||
| 112 | # If the word is longer than the line, break by character. | ||
| 113 | # Note that we could still have the problem of a single | ||
| 114 | # character not fitting the width, which we will leave | ||
| 115 | # as an exercise for the reader. | ||
| 116 | # | ||
| 117 | if ($wordwidth > $w) { | ||
| 118 | unshift @words, split //, $word; | ||
| 119 | $word = shift @words; | ||
| 120 | $wordwidth = PostScript::Metrics::stringwidth($word, | ||
| 121 | $element->{font}, | ||
| 122 | $element->{size}); | ||
| 123 | } | ||
| 124 | $wcount += $wordwidth; | ||
| 125 | |||
| 126 | # If we've gone over, push the word back on | ||
| 127 | # for later processing. | ||
| 128 | # | ||
| 129 | if ( ($wcount>$w) || ($word =~ s/\n//) ) { | ||
| 130 | if ($word =~ /^ /) { $word =~ s/^[ ]+//; } | ||
| 131 | unshift @words, $word; | ||
| 132 | last LINE; | ||
| 133 | } | ||
| 134 | $line .= $word; | ||
| 135 | } | ||
| 136 | $index++; | ||
| 137 | $element = $self->[$index]; | ||
| 138 | } | ||
| 139 | |||
| 140 | # Show the line | ||
| 141 | # | ||
| 142 | if (defined($line)) { | ||
| 143 | $linebuffer .= "($line) show\n"; | ||
| 144 | } | ||
| 145 | |||
| 146 | # Subtract the maximum leading from the current coordinate | ||
| 147 | # | ||
| 148 | $y1 -= $maxlead; | ||
| 149 | |||
| 150 | # If this line doesn't fit, put the elements making up the line | ||
| 151 | # back on for later processing... | ||
| 152 | # | ||
| 153 | if ($y1 < ($y-$h)) { | ||
| 154 | for (my $i=$firstindex; $i < $maxindex; $i++) { | ||
| 155 | push @remainder, $self->[$i]; | ||
| 156 | } | ||
| 157 | last ELEMENT; | ||
| 158 | } else { | ||
| 159 | # Put any remaining words back for later processing | ||
| 160 | # | ||
| 161 | if (@words) { | ||
| 162 | $element->{text} = join '', @words; | ||
| 163 | } else { | ||
| 164 | $index++; | ||
| 165 | $element = $self->[$index]; | ||
| 166 | } | ||
| 167 | $returnval .= "0 setgray $x1 $y1 moveto\n"; | ||
| 168 | $returnval .= $linebuffer; | ||
| 169 | } | ||
| 170 | } | ||
| 171 | return ($returnval, bless([@remainder], 'PostScript::TextBlock')); | ||
| 172 | } | ||
| 173 | |||
| 174 | |||
| 175 | sub FitToRegion { | ||
| 176 | # The FitToRegion() method takes four parameters: w, h, x , and y, | ||
| 177 | # where w and h are the width and height of the block (in points), | ||
| 178 | # and x and y specify the upper left corner of the TextBlock (in the | ||
| 179 | # PostScript coordinate system). This method returns a string containing | ||
| 180 | # the PostScript code that generated the block, and a TextBlock object | ||
| 181 | # the contains the portion that doesn't fit within the given bounds. | ||
| 182 | # | ||
| 183 | my $self = shift; | ||
| 184 | my ($w, $h, $x, $y, $minimum_font_size) = @_; | ||
| 185 | |||
| 186 | my ($x1, $y1) = ($x, $y); | ||
| 187 | my $returnval = ""; | ||
| 188 | my @remainder = (); | ||
| 189 | my ($maxlead, $wcount, $linebuffer) = (0, 0, ""); | ||
| 190 | my ($line, $word, @words); | ||
| 191 | my $wordwidth; | ||
| 192 | $returnval .= "0 setgray\n"; | ||
| 193 | |||
| 194 | my $element = {}; | ||
| 195 | my $index = 0; | ||
| 196 | |||
| 197 | $element = $self->[$index]; | ||
| 198 | |||
| 199 | my %original_element = {}; | ||
| 200 | foreach (keys %$element) { | ||
| 201 | $original_element{$_} = $self->[$index]->{$_}; | ||
| 202 | } # foreach | ||
| 203 | |||
| 204 | my $maxindex = $self->numElements; | ||
| 205 | my $firstindex = 0; | ||
| 206 | |||
| 207 | ELEMENT: while (($index < $maxindex) && ($y1 >= ($y-$h))) { | ||
| 208 | $wcount = 0; | ||
| 209 | ($line, $word) = (undef, undef); | ||
| 210 | @words = (); | ||
| 211 | $linebuffer = ""; | ||
| 212 | $maxlead = 0; | ||
| 213 | $firstindex = $index; | ||
| 214 | |||
| 215 | # Loop until a complete line is formed, or | ||
| 216 | # until we run out of elements | ||
| 217 | # | ||
| 218 | LINE: while (($index < $maxindex) && $wcount<=$w) { | ||
| 219 | $linebuffer .= "/$element->{font} findfont\n"; | ||
| 220 | $linebuffer .= "$element->{size} scalefont setfont\n"; | ||
| 221 | |||
| 222 | # Calculate the maximum leading on this line | ||
| 223 | # | ||
| 224 | $maxlead = $element->{leading} if ($element->{leading} > $maxlead); | ||
| 225 | |||
| 226 | @words = split /( +|\t|\n)/, $element->{text}; | ||
| 227 | while (@words) { | ||
| 228 | $word = shift @words; | ||
| 229 | $wordwidth = PostScript::Metrics::stringwidth($word, | ||
| 230 | $element->{font}, | ||
| 231 | $element->{size}); | ||
| 232 | |||
| 233 | # If the word is longer than the line, break by character. | ||
| 234 | # Note that we could still have the problem of a single | ||
| 235 | # character not fitting the width, which we will leave | ||
| 236 | # as an exercise for the reader. | ||
| 237 | # | ||
| 238 | # if ($wordwidth > $w) { | ||
| 239 | # unshift @words, split //, $word; | ||
| 240 | # $word = shift @words; | ||
| 241 | # $wordwidth = PostScript::Metrics::stringwidth($word, | ||
| 242 | # $element->{font}, | ||
| 243 | # $element->{size}); | ||
| 244 | # } # if | ||
| 245 | $wcount += $wordwidth; | ||
| 246 | |||
| 247 | # If we've gone over, push the word back on | ||
| 248 | # for later processing. | ||
| 249 | # | ||
| 250 | if (($wcount > $w) || ($word =~ s/\n//)) { | ||
| 251 | unshift @words, $word; | ||
| 252 | last LINE; | ||
| 253 | } # if | ||
| 254 | $line .= $word; | ||
| 255 | } # while | ||
| 256 | $index++; | ||
| 257 | $element = $self->[$index]; | ||
| 258 | } # while | ||
| 259 | |||
| 260 | # Show the line | ||
| 261 | # | ||
| 262 | if (defined($line)) { | ||
| 263 | $linebuffer .= "($line) show\n"; | ||
| 264 | } | ||
| 265 | |||
| 266 | # Subtract the maximum leading from the current coordinate | ||
| 267 | # | ||
| 268 | $y1 -= $maxlead; | ||
| 269 | |||
| 270 | # If this line doesn't fit, put the elements making up the line | ||
| 271 | # back on for later processing... | ||
| 272 | # | ||
| 273 | if ($y1 < ($y-$h)) { | ||
| 274 | for (my $i=$firstindex; $i < $maxindex; $i++) { | ||
| 275 | push @remainder, $self->[$i]; | ||
| 276 | } | ||
| 277 | last ELEMENT; | ||
| 278 | } else { | ||
| 279 | # Put any remaining words back for later processing | ||
| 280 | # | ||
| 281 | if (@words) { | ||
| 282 | $element->{text} = join '', @words; | ||
| 283 | } else { | ||
| 284 | $index++; | ||
| 285 | $element = $self->[$index]; | ||
| 286 | } # if | ||
| 287 | $returnval .= "0 setgray $x1 $y1 moveto\n"; | ||
| 288 | $returnval .= $linebuffer; | ||
| 289 | } # else | ||
| 290 | } # while | ||
| 291 | |||
| 292 | if (@remainder and ($original_element{size} - 1 >= $minimum_font_size)) { | ||
| 293 | --$original_element{size}; | ||
| 294 | if ($original_element{leading}) { | ||
| 295 | --$original_element{leading}; | ||
| 296 | } # if | ||
| 297 | $self->[0] = { %original_element }; | ||
| 298 | ($returnval, @remainder) = &FitToRegion($self, $w, $h, $x, $y, $minimum_font_size); | ||
| 299 | } # if | ||
| 300 | |||
| 301 | return ($returnval, bless([@remainder], 'PostScript::TextBlock')); | ||
| 302 | # return $returnval; | ||
| 303 | } # FitToRegion | ||
| 304 | 1; # All Perl modules should return true | ||
| 305 | |||
| 306 | |||
| 307 | __END__ |