| 1 | #======================================================================= | ||
| 2 | # ____ ____ _____ _ _ | ||
| 3 | # | _ \| _ \| ___| _ _ | | __ _ _ _ ___ _ _| |_ | ||
| 4 | # | |_) | | | | |_ (_) (_) | | / _` | | | |/ _ \| | | | __| | ||
| 5 | # | __/| |_| | _| _ _ | |__| (_| | |_| | (_) | |_| | |_ | ||
| 6 | # |_| |____/|_| (_) (_) |_____\__,_|\__, |\___/ \__,_|\__| | ||
| 7 | # |___/ | ||
| 8 | # | ||
| 9 | # A Perl Module Chain to faciliate Layouts for PDF::API2. | ||
| 10 | # | ||
| 11 | # Copyright 1999-2005 Alfred Reibenschuh <areibens@cpan.org>. | ||
| 12 | # | ||
| 13 | #======================================================================= | ||
| 14 | # | ||
| 15 | # THIS LIBRARY IS FREE SOFTWARE; YOU CAN REDISTRIBUTE IT AND/OR | ||
| 16 | # MODIFY IT UNDER THE TERMS OF THE GNU LESSER GENERAL PUBLIC | ||
| 17 | # LICENSE AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION; EITHER | ||
| 18 | # VERSION 2 OF THE LICENSE, OR (AT YOUR OPTION) ANY LATER VERSION. | ||
| 19 | # | ||
| 20 | # THIS FILE IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, | ||
| 21 | # AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| 22 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | ||
| 23 | # FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT | ||
| 24 | # SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR CONTRIBUTORS | ||
| 25 | # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
| 26 | # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| 27 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS | ||
| 28 | # OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
| 29 | # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
| 30 | # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 31 | # ARISING IN ANY WAY OUT OF THE USE OF THIS FILE, EVEN IF | ||
| 32 | # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 33 | # | ||
| 34 | # SEE THE GNU LESSER GENERAL PUBLIC LICENSE FOR MORE DETAILS. | ||
| 35 | # | ||
| 36 | # YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC | ||
| 37 | # LICENSE ALONG WITH THIS LIBRARY; IF NOT, WRITE TO THE | ||
| 38 | # FREE SOFTWARE FOUNDATION, INC., 59 TEMPLE PLACE - SUITE 330, | ||
| 39 | # BOSTON, MA 02111-1307, USA. | ||
| 40 | # | ||
| 41 | # $Id: $ | ||
| 42 | # | ||
| 43 | #======================================================================= | ||
| 44 | |||
| 45 | package PDF::APIx::Layout::RawTextObjects; | ||
| 46 | |||
| 47 | BEGIN | ||
| 48 | { | ||
| 49 | use utf8; | ||
| 50 | use Encode qw(:all); | ||
| 51 | |||
| 52 | use PDF::API2::UniWrap; | ||
| 53 | use PDF::APIx::Layout::Object; | ||
| 54 | |||
| 55 | use vars qw( @ISA $VERSION $BlockJustification $BlockExpansion); | ||
| 56 | |||
| 57 | @ISA=qw[ PDF::APIx::Layout::Object ]; | ||
| 58 | |||
| 59 | ($VERSION) = '$Revision: $' =~ /Revision: (\S+)\s/; # $Date: $ | ||
| 60 | } | ||
| 61 | |||
| 62 | no warnings qw[ deprecated recursion uninitialized ]; | ||
| 63 | |||
| 64 | $BlockJustification=1.1; | ||
| 65 | $BlockExpansion=1.2; | ||
| 66 | |||
| 67 | sub _new | ||
| 68 | { | ||
| 69 | my ($class,%opts) = @_; | ||
| 70 | |||
| 71 | $class = ref($class) ? ref($class) : $class; | ||
| 72 | |||
| 73 | my $self=$class->SUPER::new(%opts); | ||
| 74 | |||
| 75 | $self->{-lead}||=1; | ||
| 76 | |||
| 77 | $self->{-maxw}||=0; | ||
| 78 | $self->{-minw}||=0; | ||
| 79 | $self->{-maxh}||=0; | ||
| 80 | $self->{-minh}||=0; | ||
| 81 | |||
| 82 | $self->{-h}||=0; | ||
| 83 | $self->{-w}||=0; | ||
| 84 | |||
| 85 | return($self); | ||
| 86 | } | ||
| 87 | |||
| 88 | sub _calc_w_space | ||
| 89 | { | ||
| 90 | my $chunk=shift @_; | ||
| 91 | my ($style,$attr,$text)=@{$chunk}; | ||
| 92 | |||
| 93 | $attr->{calcwidth}=$style->{-fontsize}*$style->{-font}->width($text)*($style->{-hspace}/100); | ||
| 94 | $attr->{calcheight}=$style->{-lead}; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub _calc_wo_space | ||
| 98 | { | ||
| 99 | my $chunk=shift @_; | ||
| 100 | my ($style,$attr,$text)=@{$chunk}; | ||
| 101 | $text=~s|\s+$||g; | ||
| 102 | $attr->{calcuwidth}=$style->{-fontsize}*$style->{-font}->width($text)*($style->{-hspace}/100); | ||
| 103 | $attr->{calcuheight}=$style->{-lead}; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub _proc_objects | ||
| 107 | { | ||
| 108 | my ($self,$textobjs) = @_; | ||
| 109 | |||
| 110 | $self->{-text}=[ @{$textobjs} ]; | ||
| 111 | my $lastHeight=0; | ||
| 112 | foreach my $chunk ( @{$self->{-text}} ) | ||
| 113 | { | ||
| 114 | my ($style,$attr,$text)=@{$chunk}; | ||
| 115 | if(scalar @{$chunk}) | ||
| 116 | { | ||
| 117 | $style->{-font} ||= $self->{-font}; | ||
| 118 | $style->{-fontsize} ||= $self->{-fontsize} || 10; | ||
| 119 | $style->{-textcolor} ||= $self->{-textcolor} || 'black'; | ||
| 120 | $style->{-lead} ||= $style->{-fontsize}*$self->{-lead}; | ||
| 121 | $style->{-hspace} ||= 100; | ||
| 122 | |||
| 123 | _calc_w_space($chunk); | ||
| 124 | _calc_wo_space($chunk); | ||
| 125 | } | ||
| 126 | else | ||
| 127 | { | ||
| 128 | $chunk->[0]=undef; | ||
| 129 | $chunk->[1]={}; | ||
| 130 | $chunk->[1]->{forcebreak}=1; | ||
| 131 | $chunk->[1]->{calcwidth}=0; | ||
| 132 | $chunk->[1]->{calcheight}=$lastHeight; | ||
| 133 | $chunk->[1]->{calcuwidth}=0; | ||
| 134 | $chunk->[1]->{calcuheight}=$lastHeight; | ||
| 135 | $chunk->[2]=''; | ||
| 136 | $attr=$chunk->[1]; | ||
| 137 | } | ||
| 138 | |||
| 139 | if($self->{-minw} <= $attr->{calcwidth}) | ||
| 140 | { | ||
| 141 | $self->{-minw}=$attr->{calcwidth}; | ||
| 142 | } | ||
| 143 | $self->{-maxw}+=$attr->{calcwidth}; | ||
| 144 | |||
| 145 | if($self->{-minh} <= $attr->{calcheight}) | ||
| 146 | { | ||
| 147 | $self->{-minh}=$attr->{calcheight}; | ||
| 148 | } | ||
| 149 | $self->{-maxh}+=$attr->{calcheight}; | ||
| 150 | $lastHeight=$attr->{calcheight}; | ||
| 151 | } | ||
| 152 | |||
| 153 | $self->{-maxw}+=($self->{-paddingleft} || $self->{-padding}); | ||
| 154 | $self->{-maxw}+=($self->{-paddingright} || $self->{-padding}); | ||
| 155 | $self->{-maxw}+=($self->{-marginleft} || $self->{-margin}); | ||
| 156 | $self->{-maxw}+=($self->{-marginright} || $self->{-margin}); | ||
| 157 | |||
| 158 | $self->{-minw}+=($self->{-paddingleft} || $self->{-padding}); | ||
| 159 | $self->{-minw}+=($self->{-paddingright} || $self->{-padding}); | ||
| 160 | $self->{-minw}+=($self->{-marginleft} || $self->{-margin}); | ||
| 161 | $self->{-minw}+=($self->{-marginright} || $self->{-margin}); | ||
| 162 | |||
| 163 | $self->{-maxh}+=($self->{-paddingtop} || $self->{-padding}); | ||
| 164 | $self->{-maxh}+=($self->{-paddingbottom} || $self->{-padding}); | ||
| 165 | $self->{-maxh}+=($self->{-margintop} || $self->{-margin}); | ||
| 166 | $self->{-maxh}+=($self->{-marginbottom} || $self->{-margin}); | ||
| 167 | |||
| 168 | $self->{-minh}+=($self->{-paddingtop} || $self->{-padding}); | ||
| 169 | $self->{-minh}+=($self->{-paddingbottom} || $self->{-padding}); | ||
| 170 | $self->{-minh}+=($self->{-margintop} || $self->{-margin}); | ||
| 171 | $self->{-minh}+=($self->{-marginbottom} || $self->{-margin}); | ||
| 172 | |||
| 173 | $self->{-w}=$self->{-maxw}; | ||
| 174 | $self->{-h}=$self->{-minh}; | ||
| 175 | |||
| 176 | $self->{-words}=scalar @{$self->{-text}}; | ||
| 177 | |||
| 178 | $self->{-wcache}={}; | ||
| 179 | |||
| 180 | return($self); | ||
| 181 | } | ||
| 182 | |||
| 183 | sub new | ||
| 184 | { | ||
| 185 | my ($class,$textobjs,%opts) = @_; | ||
| 186 | |||
| 187 | $class = ref($class) ? ref($class) : $class; | ||
| 188 | |||
| 189 | my $self=$class->_new(%opts); | ||
| 190 | $self->_proc_objects($textobjs); | ||
| 191 | |||
| 192 | return($self); | ||
| 193 | } | ||
| 194 | |||
| 195 | sub isReflowAble | ||
| 196 | { | ||
| 197 | my $self=shift @_; | ||
| 198 | return(1); | ||
| 199 | } | ||
| 200 | |||
| 201 | sub heightByWidth | ||
| 202 | { | ||
| 203 | my ($self,$w)=@_; | ||
| 204 | |||
| 205 | $w-=($self->{-paddingleft} || $self->{-padding}); | ||
| 206 | $w-=($self->{-paddingright} || $self->{-padding}); | ||
| 207 | $w-=($self->{-marginleft} || $self->{-margin}); | ||
| 208 | $w-=($self->{-marginright} || $self->{-margin}); | ||
| 209 | |||
| 210 | if(exists $self->{-wcache}->{$w}) | ||
| 211 | { | ||
| 212 | return($self->{-wcache}->{$w}); | ||
| 213 | } | ||
| 214 | |||
| 215 | my $wx=0; | ||
| 216 | my $wf=$self->{-align} eq 'j' ? ($self->{-blockjustify} || $BlockJustification) : 1; | ||
| 217 | my $h=0; | ||
| 218 | my @t; | ||
| 219 | |||
| 220 | foreach my $chunk ( @{$self->{-text}} ) | ||
| 221 | { | ||
| 222 | if(((($w*$wf) <= ($wx+$chunk->[1]->{calcuwidth})) && (scalar @t > 0)) || $chunk->[1]->{forcebreak}) | ||
| 223 | { | ||
| 224 | my $hx=0; | ||
| 225 | $wx=0; | ||
| 226 | map { if($hx < $_->[1]->{calcheight}){ $hx=$_->[1]->{calcheight}; } } @t; | ||
| 227 | $h+=$hx; | ||
| 228 | @t=(); | ||
| 229 | } | ||
| 230 | |||
| 231 | $wx+=$chunk->[1]->{calcwidth}; | ||
| 232 | push @t,$chunk; | ||
| 233 | } | ||
| 234 | |||
| 235 | if(scalar @t > 0) | ||
| 236 | { | ||
| 237 | my $hx=0; | ||
| 238 | $wx=0; | ||
| 239 | map { if($hx < $_->[1]->{calcheight}){ $hx=$_->[1]->{calcheight}; } } @t; | ||
| 240 | $h+=$hx; | ||
| 241 | @t=(); | ||
| 242 | } | ||
| 243 | |||
| 244 | $h+=($self->{-paddingtop} || $self->{-padding}); | ||
| 245 | $h+=($self->{-paddingbottom} || $self->{-padding}); | ||
| 246 | $h+=($self->{-margintop} || $self->{-margin}); | ||
| 247 | $h+=($self->{-marginbottom} || $self->{-margin}); | ||
| 248 | |||
| 249 | $self->{-wcache}->{$w}=$h; | ||
| 250 | |||
| 251 | return($h); | ||
| 252 | } | ||
| 253 | |||
| 254 | sub widthByHeight | ||
| 255 | { | ||
| 256 | my ($self,$h)=@_; | ||
| 257 | |||
| 258 | $h-=($self->{-paddingtop} || $self->{-padding}); | ||
| 259 | $h-=($self->{-paddingbottom} || $self->{-padding}); | ||
| 260 | $h-=($self->{-margintop} || $self->{-margin}); | ||
| 261 | $h-=($self->{-marginbottom} || $self->{-margin}); | ||
| 262 | |||
| 263 | # TODO | ||
| 264 | |||
| 265 | my $w=0; | ||
| 266 | |||
| 267 | # TODO | ||
| 268 | |||
| 269 | $w+=($self->{-paddingleft} || $self->{-padding}); | ||
| 270 | $w+=($self->{-paddingright} || $self->{-padding}); | ||
| 271 | $w+=($self->{-marginleft} || $self->{-margin}); | ||
| 272 | $w+=($self->{-marginright} || $self->{-margin}); | ||
| 273 | |||
| 274 | return($w); | ||
| 275 | } | ||
| 276 | |||
| 277 | sub _render | ||
| 278 | { | ||
| 279 | my ($self,$pdf,$page,$txt,$x,$y,$w,$h)=@_; | ||
| 280 | |||
| 281 | $txt->textstart; | ||
| 282 | $txt->transform(-translate => [ $x, $y ]); | ||
| 283 | |||
| 284 | my $wx=0; | ||
| 285 | my $wf=$self->{-align} eq 'j' ? ($self->{-blockjustify} || $BlockJustification) : 1; | ||
| 286 | my @t=(); | ||
| 287 | my @rend=(); | ||
| 288 | foreach my $chunk ( @{$self->{-text}} ) | ||
| 289 | { | ||
| 290 | my ($style,$attr,$text)=@{$chunk}; | ||
| 291 | if(((($w*$wf) <= ($wx+$attr->{calcuwidth})) && (scalar @t > 0)) || $attr->{forcebreak}) | ||
| 292 | { | ||
| 293 | push @rend,[$attr->{forcebreak} || 0,@t]; | ||
| 294 | $wx=0; @t=(); | ||
| 295 | } | ||
| 296 | $wx+=$attr->{calcwidth}; | ||
| 297 | next unless(defined $style); | ||
| 298 | push @t,$chunk; | ||
| 299 | } | ||
| 300 | if(scalar @t > 0) | ||
| 301 | { | ||
| 302 | push @rend,[0,@t]; | ||
| 303 | @t=(); | ||
| 304 | } | ||
| 305 | |||
| 306 | $rend[-1]->[0]=1; | ||
| 307 | |||
| 308 | my $line=0; | ||
| 309 | my $hl=0; my $of=0; | ||
| 310 | foreach my $tline (@rend) | ||
| 311 | { | ||
| 312 | my $islast=shift(@{$tline}); | ||
| 313 | my $hx=0; | ||
| 314 | $wx=0; | ||
| 315 | my $hf=0; | ||
| 316 | map | ||
| 317 | { | ||
| 318 | if($hf <= $_->[0]->{-fontsize}){ $hf=$_->[0]->{-fontsize}; } | ||
| 319 | if($hx <= $_->[1]->{calcheight}){ $hx=$_->[1]->{calcheight}; } | ||
| 320 | $wx+=$_->[1]->{calcwidth}; | ||
| 321 | } @{$tline}; | ||
| 322 | |||
| 323 | if(!$line) { $hx=$hf; } | ||
| 324 | |||
| 325 | $wx-=$tline->[-1]->[1]->{calcwidth}-$tline->[-1]->[1]->{calcuwidth}; | ||
| 326 | |||
| 327 | if($hl+$hx<$h) | ||
| 328 | { | ||
| 329 | $txt->lead($hx); | ||
| 330 | $txt->nl; | ||
| 331 | $hl+=$hx; | ||
| 332 | |||
| 333 | my $isfirst=1; | ||
| 334 | my $laststyle=0; | ||
| 335 | my $lasthspace=0; | ||
| 336 | foreach my $t (@{$tline}) | ||
| 337 | { | ||
| 338 | next unless(defined $t->[0]); | ||
| 339 | my %txtopt=(); $txtopt{-underline}=[$t->[0]->{-fontsize}*0.20,$t->[0]->{-fontsize}*0.1] if($t->[0]->{-underline} && $t->[0]->{-underline} ne 'none'); | ||
| 340 | |||
| 341 | if($self->{-align} eq 'r' && $isfirst) | ||
| 342 | { | ||
| 343 | $txtopt{-indent}=$w-$wx; | ||
| 344 | } | ||
| 345 | elsif($self->{-align} eq 'c' && $isfirst) | ||
| 346 | { | ||
| 347 | $txtopt{-indent}=($w-$wx)/2; | ||
| 348 | } | ||
| 349 | |||
| 350 | if(($self->{-align} eq 'j') && (!$islast || ($wx>$w))) | ||
| 351 | { | ||
| 352 | my $hsf=$w/$wx; | ||
| 353 | if(($hsf > ($self->{-blockexpand} || $BlockExpansion)) && (scalar @{$tline} > 1)) | ||
| 354 | { | ||
| 355 | $hsf=($self->{-blockexpand} || $BlockExpansion); | ||
| 356 | $txtopt{-indent}=(($w-($wx*$hsf))/((scalar @{$tline})-1)) unless($isfirst); | ||
| 357 | } | ||
| 358 | my $hspace=($t->[0]->{-hspace} || 100)*$hsf; | ||
| 359 | if($lasthspace != $hspace) | ||
| 360 | { | ||
| 361 | $txt->hspace($hspace); | ||
| 362 | } | ||
| 363 | $lasthspace=$hspace; | ||
| 364 | } | ||
| 365 | elsif($lasthspace!=($t->[0]->{-hspace} || 100)) | ||
| 366 | { | ||
| 367 | $txt->hspace($t->[0]->{-hspace} || 100); | ||
| 368 | $lasthspace=($t->[0]->{-hspace} || 100); | ||
| 369 | } | ||
| 370 | |||
| 371 | if($laststyle ne $t->[0]) | ||
| 372 | { | ||
| 373 | $txt->font($t->[0]->{-font},$t->[0]->{-fontsize}); | ||
| 374 | |||
| 375 | $txt->rise($t->[0]->{-rise} || 0); | ||
| 376 | |||
| 377 | if(ref $t->[0]->{-textcolor}) | ||
| 378 | { | ||
| 379 | $txt->fillcolor(@{$t->[0]->{-textcolor}}); | ||
| 380 | } | ||
| 381 | else | ||
| 382 | { | ||
| 383 | $txt->fillcolor($t->[0]->{-textcolor}); | ||
| 384 | } | ||
| 385 | } | ||
| 386 | |||
| 387 | my $xy1=[$txt->textpos2]; | ||
| 388 | $txt->text($t->[2],%txtopt); | ||
| 389 | my $xy2=[$txt->textpos2]; | ||
| 390 | |||
| 391 | if(defined $t->[0]->{-id}) | ||
| 392 | { | ||
| 393 | $pdf->named_destination('Dests','#'.$t->[0]->{-id})->link($page); | ||
| 394 | } | ||
| 395 | if(defined $t->[0]->{-href}) | ||
| 396 | { | ||
| 397 | my $at=$page->annotation; | ||
| 398 | $at->rect( | ||
| 399 | $txt->_textpos(@{$xy1}), | ||
| 400 | $txt->_textpos(@{$xy2},0,$t->[0]->{-fontsize}) | ||
| 401 | ); | ||
| 402 | if($t->[0]->{-href}=~m|^#|) | ||
| 403 | { | ||
| 404 | $at->link($t->[0]->{-href}); | ||
| 405 | } | ||
| 406 | else | ||
| 407 | { | ||
| 408 | $at->url($t->[0]->{-href}); | ||
| 409 | } | ||
| 410 | } | ||
| 411 | |||
| 412 | if($t->[0]->{-background}) | ||
| 413 | { | ||
| 414 | $gfx->save; | ||
| 415 | if(ref $t->[0]->{-background}) | ||
| 416 | { | ||
| 417 | $gfx->fillcolor(@{$t->[0]->{-background}}); | ||
| 418 | } | ||
| 419 | else | ||
| 420 | { | ||
| 421 | $gfx->fillcolor($t->[0]->{-background}); | ||
| 422 | } | ||
| 423 | $gfx->rectxy( | ||
| 424 | $txt->_textpos(@{$xy1}), | ||
| 425 | $txt->_textpos(@{$xy2},0,$t->[0]->{-fontsize}) | ||
| 426 | ); | ||
| 427 | $gfx->fill; | ||
| 428 | $gfx->restore; | ||
| 429 | } | ||
| 430 | $isfirst=0; | ||
| 431 | $laststyle=$t->[0]; | ||
| 432 | } | ||
| 433 | $line++; | ||
| 434 | } | ||
| 435 | else | ||
| 436 | { | ||
| 437 | unless(ref $of) | ||
| 438 | { | ||
| 439 | $of=[]; | ||
| 440 | } | ||
| 441 | push @{$of}, @{$tline}; | ||
| 442 | } | ||
| 443 | } | ||
| 444 | $txt->textend; | ||
| 445 | |||
| 446 | $h+=($self->{-paddingtop} || $self->{-padding}); | ||
| 447 | $h+=($self->{-paddingbottom} || $self->{-padding}); | ||
| 448 | |||
| 449 | $w+=($self->{-paddingleft} || $self->{-padding}); | ||
| 450 | $w+=($self->{-paddingright} || $self->{-padding}); | ||
| 451 | |||
| 452 | if(ref $of) | ||
| 453 | { | ||
| 454 | my $obj={%{$self}}; | ||
| 455 | bless $obj, ref($self); | ||
| 456 | $obj->{-text}=$of; | ||
| 457 | delete $obj->{-id}; | ||
| 458 | return($obj); | ||
| 459 | } | ||
| 460 | |||
| 461 | return(undef); | ||
| 462 | } | ||
| 463 | |||
| 464 | |||
| 465 | 1; | ||
| 466 | |||
| 467 | __END__ |