| 1 | package PDF::FromHTML::Template::Element::TextBox; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | |||
| 5 | BEGIN { | ||
| 6 | use vars qw(@ISA); | ||
| 7 | @ISA = qw(PDF::FromHTML::Template::Element); | ||
| 8 | |||
| 9 | use PDF::FromHTML::Template::Element; | ||
| 10 | use Encode; | ||
| 11 | } | ||
| 12 | |||
| 13 | sub new | ||
| 14 | { | ||
| 15 | my $class = shift; | ||
| 16 | my $self = $class->SUPER::new(@_); | ||
| 17 | |||
| 18 | $self->{TXTOBJ} = PDF::FromHTML::Template::Factory->create('TEXTOBJECT'); | ||
| 19 | |||
| 20 | return $self; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub get_text | ||
| 24 | { | ||
| 25 | my $self = shift; | ||
| 26 | my ($context) = @_; | ||
| 27 | |||
| 28 | my $txt = $context->get($self, 'TEXT'); | ||
| 29 | if (defined $txt) | ||
| 30 | { | ||
| 31 | my $txt_obj = PDF::FromHTML::Template::Factory->create('TEXTOBJECT'); | ||
| 32 | push @{$txt_obj->{STACK}}, $txt; | ||
| 33 | $txt = $txt_obj->resolve($context); | ||
| 34 | } | ||
| 35 | elsif ($self->{TXTOBJ}) | ||
| 36 | { | ||
| 37 | $txt = $self->{TXTOBJ}->resolve($context) | ||
| 38 | } | ||
| 39 | else | ||
| 40 | { | ||
| 41 | # $txt = Unicode::String::utf8(''); | ||
| 42 | $txt = ''; | ||
| 43 | } | ||
| 44 | |||
| 45 | return $txt; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub render | ||
| 49 | { | ||
| 50 | my $self = shift; | ||
| 51 | my ($context) = @_; | ||
| 52 | |||
| 53 | delete $self->{TEMP_H} if exists $self->{TEMP_H}; | ||
| 54 | |||
| 55 | return 0 unless $self->should_render($context); | ||
| 56 | |||
| 57 | if ($context->{CALC_LAST_PAGE}) | ||
| 58 | { | ||
| 59 | $self->{TEMP_H} = $self->calculate($context, 'H'); | ||
| 60 | return 1; | ||
| 61 | } | ||
| 62 | |||
| 63 | my $x = $context->get($self, 'X'); | ||
| 64 | my $y = $context->get($self, 'Y'); | ||
| 65 | my $w = $context->get($self, 'W'); | ||
| 66 | my $h = $context->get($self, 'H'); | ||
| 67 | |||
| 68 | my $align = $context->get($self, 'ALIGN') || | ||
| 69 | $context->get($self, 'JUSTIFY'); | ||
| 70 | |||
| 71 | $self->_validate_option('ALIGN', \$align); | ||
| 72 | |||
| 73 | $self->set_color($context, 'COLOR', 'both'); | ||
| 74 | |||
| 75 | my ($orig_x, $orig_w) = ($x, $w); | ||
| 76 | |||
| 77 | if (defined(my $lmargin = $context->get($self, 'LMARGIN'))) | ||
| 78 | { | ||
| 79 | $x += $lmargin; | ||
| 80 | $w -= $lmargin; | ||
| 81 | } | ||
| 82 | |||
| 83 | if (defined(my $rmargin = $context->get($self, 'RMARGIN'))) | ||
| 84 | { | ||
| 85 | $w -= $rmargin; | ||
| 86 | } | ||
| 87 | |||
| 88 | my $txt = $self->get_text($context); | ||
| 89 | |||
| 90 | $self->{TEMP_H} = $self->show_boxed( | ||
| 91 | $context, $txt, | ||
| 92 | $x, $y, $w, $h, | ||
| 93 | $align, '', | ||
| 94 | ); | ||
| 95 | |||
| 96 | my $max_h = $context->{STACK}[-2]{MAX_H} || $self->{TEMP_H}; | ||
| 97 | |||
| 98 | if ($context->get($self, 'BGCOLOR')) | ||
| 99 | { | ||
| 100 | $context->{PDF}->save_state; | ||
| 101 | |||
| 102 | $self->set_color($context, 'BGCOLOR', 'fill'); | ||
| 103 | |||
| 104 | $context->{PDF}->rect($orig_x, $y - $max_h + $h, $orig_w, $max_h); | ||
| 105 | $context->{PDF}->fill; | ||
| 106 | $context->{PDF}->restore_state; | ||
| 107 | } | ||
| 108 | |||
| 109 | if (my $border = $context->get($self, 'BORDER')) | ||
| 110 | { | ||
| 111 | $context->{PDF}->rect($orig_x, $y - $max_h + $h, $orig_w, $max_h, $border); | ||
| 112 | $context->{PDF}->stroke; | ||
| 113 | } | ||
| 114 | |||
| 115 | $self->set_color($context, 'COLOR', 'both', 1); | ||
| 116 | |||
| 117 | return 1; | ||
| 118 | } | ||
| 119 | |||
| 120 | sub deltas | ||
| 121 | { | ||
| 122 | my $self = shift; | ||
| 123 | my ($context) = @_; | ||
| 124 | |||
| 125 | return { | ||
| 126 | X => $context->get($self, 'W'), | ||
| 127 | Y => 0, | ||
| 128 | }; | ||
| 129 | } | ||
| 130 | |||
| 131 | sub _display_doublebyte | ||
| 132 | { | ||
| 133 | my $self = shift; | ||
| 134 | my ($p, $str, $x, $y, $j, $font_size) = @_; | ||
| 135 | |||
| 136 | my $halfwidth_len = ($str =~ tr/\x00-\xff//); | ||
| 137 | my $len = (length($str) * 2) - $halfwidth_len; | ||
| 138 | if ($j eq 'right') | ||
| 139 | { | ||
| 140 | $x -= $len * $font_size / 2; | ||
| 141 | } | ||
| 142 | elsif ($j eq 'center') | ||
| 143 | { | ||
| 144 | $x -= ($len / 4) * $font_size; | ||
| 145 | } | ||
| 146 | |||
| 147 | $p->show_xy($str, $x, $y); | ||
| 148 | |||
| 149 | return 0; | ||
| 150 | } | ||
| 151 | |||
| 152 | sub _show_boxed | ||
| 153 | { | ||
| 154 | my $self = shift; | ||
| 155 | my $context = shift; | ||
| 156 | |||
| 157 | my $encoding = $context->get($self, 'PDF_ENCODING') || 'host'; | ||
| 158 | |||
| 159 | if (my $text_encoding = $context->get($self, 'TEXT_ENCODING')) | ||
| 160 | { | ||
| 161 | require Encode::compat if $] <= 5.008; | ||
| 162 | require Encode; | ||
| 163 | unshift @_, Encode::decode($text_encoding => shift(@_)) | ||
| 164 | unless Encode::is_utf8($_[0]); | ||
| 165 | } | ||
| 166 | |||
| 167 | # On non-DBCS, use PDF::Writer's builtin show_boxed. | ||
| 168 | if ($_[0] !~ /[^\x00-\xff]/) { | ||
| 169 | my $str = shift; | ||
| 170 | my $leftovers = $context->{PDF}->show_boxed($str, @_); | ||
| 171 | |||
| 172 | $leftovers++ if $leftovers && $leftovers == length($str) - 1; | ||
| 173 | return $leftovers; | ||
| 174 | } | ||
| 175 | |||
| 176 | my ($p, $str, $x, $y, $w, $h, $j, $m) = ($context->{PDF}, @_); | ||
| 177 | |||
| 178 | my $font_size = $p->font_size; | ||
| 179 | die "Fontsize of 0!", $/ if $font_size <= 0; | ||
| 180 | |||
| 181 | if ($w == 0 && $h == 0) | ||
| 182 | { | ||
| 183 | return 0 if $m eq 'blind'; | ||
| 184 | return $self->_display_doublebyte($p, $str, $x, $y, $j, $font_size); | ||
| 185 | } | ||
| 186 | |||
| 187 | my $num_lines = int($h / $font_size); | ||
| 188 | |||
| 189 | # This is half-width measure | ||
| 190 | my $chars_per_line = int($w / $font_size * 2); | ||
| 191 | |||
| 192 | my $right = $x + $w; | ||
| 193 | my $mid = int(($x + $right) / 2); | ||
| 194 | |||
| 195 | my $current_y = $y + $h - $font_size; | ||
| 196 | |||
| 197 | foreach my $line_num (0 .. $num_lines - 1) | ||
| 198 | { | ||
| 199 | my $start_x = $x; | ||
| 200 | $start_x = $right if $j eq 'right'; | ||
| 201 | $start_x = $mid if $j eq 'center'; | ||
| 202 | |||
| 203 | # if ($str->length <= $chars_per_line) | ||
| 204 | if (len($str) <= $chars_per_line) | ||
| 205 | { | ||
| 206 | return 0 if $m eq 'blind'; | ||
| 207 | return $self->_display_doublebyte($p, $str, $start_x, $current_y, $j, $font_size); | ||
| 208 | } | ||
| 209 | |||
| 210 | # my $str_this_line = $str->substr(0, $chars_per_line); | ||
| 211 | my $str_this_line; | ||
| 212 | while (len($str_this_line) <= $chars_per_line) { | ||
| 213 | $str_this_line .= substr($str, 0, 1, ''); | ||
| 214 | } | ||
| 215 | |||
| 216 | $self->_display_doublebyte($p, $str_this_line, $start_x, $current_y, $j, $font_size) | ||
| 217 | unless $m eq 'blind'; | ||
| 218 | |||
| 219 | $current_y -= $font_size; | ||
| 220 | # $str = $str->substr($chars_per_line); | ||
| 221 | } | ||
| 222 | |||
| 223 | # return $str->length; | ||
| 224 | return length($str); | ||
| 225 | } | ||
| 226 | |||
| 227 | sub len { | ||
| 228 | my $str = shift; | ||
| 229 | my $halfwidth_len = ($str =~ tr/\x00-\xff//); | ||
| 230 | return ((length($str) * 2) - $halfwidth_len); | ||
| 231 | } | ||
| 232 | |||
| 233 | sub show_boxed | ||
| 234 | { | ||
| 235 | my $self = shift; | ||
| 236 | my ($context, $str, $x, $y, $w, $h, $align, $mode) = @_; | ||
| 237 | |||
| 238 | my $fsize = $context->{PDF}->font_size; | ||
| 239 | $fsize = 0 if $fsize < 0; | ||
| 240 | |||
| 241 | # return $h unless $str->length && ($fsize && $h / $fsize >= 1); | ||
| 242 | return $h unless length($str) && ($fsize && $h / $fsize >= 1); | ||
| 243 | |||
| 244 | my $total_h = $h; | ||
| 245 | # my $excess_txt = Unicode::String::utf8(''); | ||
| 246 | my $excess_txt = ''; | ||
| 247 | |||
| 248 | LOOP: | ||
| 249 | { | ||
| 250 | my $leftovers = $self->_show_boxed( | ||
| 251 | $context, $str, | ||
| 252 | $x, $y, $w, $h, | ||
| 253 | $align, $mode, | ||
| 254 | ); | ||
| 255 | die "Invalid return ($leftovers) from _show_boxed() on string '$str'", $/ | ||
| 256 | if $leftovers > length($str); | ||
| 257 | |||
| 258 | last LOOP if $context->get($self, 'TRUNCATE_TEXT'); | ||
| 259 | |||
| 260 | if ($leftovers < length($str)) | ||
| 261 | { | ||
| 262 | last LOOP unless $excess_txt || $leftovers; | ||
| 263 | |||
| 264 | $str = ($leftovers ? substr($str, -1 * $leftovers) : '' ) . $excess_txt; | ||
| 265 | $excess_txt = ''; | ||
| 266 | |||
| 267 | $str =~ s/^[\r\n\s]+//go; | ||
| 268 | |||
| 269 | $y -= $h; | ||
| 270 | $total_h += $h; | ||
| 271 | |||
| 272 | redo LOOP; | ||
| 273 | } | ||
| 274 | |||
| 275 | last LOOP unless $leftovers; | ||
| 276 | |||
| 277 | $total_h += $h; | ||
| 278 | $excess_txt = chop($str) . $excess_txt; | ||
| 279 | $excess_txt = chop($str) . $excess_txt | ||
| 280 | while $str =~ /[\r\n\s]$/o; | ||
| 281 | |||
| 282 | redo LOOP; | ||
| 283 | } | ||
| 284 | |||
| 285 | if ($mode eq 'blind') { | ||
| 286 | return $total_h if @{$context->{STACK}} < 2; | ||
| 287 | my $prev_max = $context->{STACK}[-2]{MAX_H}; | ||
| 288 | $context->{STACK}[-2]{MAX_H} = $total_h if $prev_max < $total_h; | ||
| 289 | } | ||
| 290 | |||
| 291 | return $total_h; | ||
| 292 | } | ||
| 293 | |||
| 294 | sub calculate | ||
| 295 | { | ||
| 296 | my $self = shift; | ||
| 297 | my ($context, $attr) = @_; | ||
| 298 | |||
| 299 | return $self->SUPER::calculate($context, $attr) unless $attr eq 'H'; | ||
| 300 | |||
| 301 | return delete $self->{TEMP_H} if exists $self->{TEMP_H}; | ||
| 302 | |||
| 303 | my $txt = $self->get_text($context); | ||
| 304 | |||
| 305 | my $x = $context->get($self, 'X'); | ||
| 306 | my $y = $context->get($self, 'Y'); | ||
| 307 | my $w = $context->get($self, 'W'); | ||
| 308 | my $h = $context->get($self, 'H'); | ||
| 309 | |||
| 310 | if (defined(my $lmargin = $context->get($self, 'LMARGIN'))) | ||
| 311 | { | ||
| 312 | $x += $lmargin; | ||
| 313 | $w -= $lmargin; | ||
| 314 | } | ||
| 315 | |||
| 316 | if (defined(my $rmargin = $context->get($self, 'RMARGIN'))) | ||
| 317 | { | ||
| 318 | $w -= $rmargin; | ||
| 319 | } | ||
| 320 | |||
| 321 | return $self->show_boxed( | ||
| 322 | $context, $txt, | ||
| 323 | $x, $y, $w, $h, | ||
| 324 | 'left', 'blind', | ||
| 325 | ); | ||
| 326 | } | ||
| 327 | |||
| 328 | 1; | ||
| 329 | __END__ |