« lib/PDF/FromHTML/Template/Element/TextBox.pm
↑ Summary
lib/PDF/FromHTML/Template/Element/Line.pm »
| 1 | package PDF::FromHTML::Template::Context; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | |||
| 5 | BEGIN { | ||
| 6 | use vars qw(@ISA); | ||
| 7 | @ISA = qw(PDF::FromHTML::Template::Base); | ||
| 8 | |||
| 9 | use PDF::FromHTML::Template::Base; | ||
| 10 | |||
| 11 | use PDF::FromHTML::Template::Constants qw( %PointsPer ); | ||
| 12 | } | ||
| 13 | |||
| 14 | # This is a helper object. It is not instantiated by the user, | ||
| 15 | # nor does it represent an XML object. Rather, every container | ||
| 16 | # will use this object to maintain the context for its children. | ||
| 17 | |||
| 18 | sub new { | ||
| 19 | my $class = shift; | ||
| 20 | my $self = $class->SUPER::new(@_); | ||
| 21 | |||
| 22 | $self->{FONTS} = {} unless UNIVERSAL::isa($self->{FONTS}, 'HASH'); | ||
| 23 | $self->{IMAGES} = {} unless UNIVERSAL::isa($self->{IMAGES}, 'HASH'); | ||
| 24 | $self->{PARAM_MAP} = [] unless UNIVERSAL::isa($self->{PARAM_MAP}, 'ARRAY'); | ||
| 25 | $self->{STACK} = [] unless UNIVERSAL::isa($self->{STACK}, 'ARRAY'); | ||
| 26 | |||
| 27 | $self->reset_pagebreak; | ||
| 28 | |||
| 29 | return $self; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub param { | ||
| 33 | my $self = shift; | ||
| 34 | my ($param, $depth) = @_; | ||
| 35 | $param = uc $param; | ||
| 36 | $depth ||= 0; | ||
| 37 | |||
| 38 | my $val = undef; | ||
| 39 | my $found = 0; | ||
| 40 | for my $map (reverse @{$self->{PARAM_MAP}}) { | ||
| 41 | next unless exists $map->{$param}; | ||
| 42 | $depth--, next if $depth; | ||
| 43 | |||
| 44 | $found = 1; | ||
| 45 | $val = $map->{$param}; | ||
| 46 | last; | ||
| 47 | } | ||
| 48 | |||
| 49 | die "Parameter '$param' not found", $/ | ||
| 50 | if !$found && $self->{DIE_ON_NO_PARAM}; | ||
| 51 | |||
| 52 | return $val; | ||
| 53 | } | ||
| 54 | |||
| 55 | #GGG This is god-awful | ||
| 56 | my %isDimension = map { $_ => 1 } qw( | ||
| 57 | X Y W H R | ||
| 58 | START_Y END_Y | ||
| 59 | X1 X2 Y1 Y2 | ||
| 60 | PAGE_HEIGHT PAGE_WIDTH | ||
| 61 | HEADER_HEIGHT FOOTER_HEIGHT | ||
| 62 | LEFT_MARGIN RIGHT_MARGIN | ||
| 63 | LMARGIN RMARGIN | ||
| 64 | SIZE WIDTH SCALE | ||
| 65 | ); | ||
| 66 | |||
| 67 | sub resolve { | ||
| 68 | my $self = shift; | ||
| 69 | my ($obj, $key, $depth) = @_; | ||
| 70 | $key = uc $key; | ||
| 71 | $depth ||= 0; | ||
| 72 | |||
| 73 | my $obj_val = $obj->{$key}; | ||
| 74 | |||
| 75 | my $is_param = 0; | ||
| 76 | $is_param = 1 if $obj_val =~ s/\$(\w+)/$self->param($1)/eg; | ||
| 77 | return $obj_val unless $isDimension{$key}; | ||
| 78 | |||
| 79 | #GGG Does this adequately test values to make sure they're legal?? | ||
| 80 | # A value is defined as: | ||
| 81 | # 1) An optional operator (+, -, *, or /) | ||
| 82 | # 2) A decimal number | ||
| 83 | # 3) An optional unit (currently I, P, or C) or % (indicating percentage) | ||
| 84 | |||
| 85 | #GGG Convert this to use //x | ||
| 86 | my ($op, $val, $unit) = $obj_val =~ m!^\s*([\+\*\/\-])?\s*([\d.]*\d)\s*([a-z%]+)?\s*$!oi; | ||
| 87 | $op ||= ''; | ||
| 88 | |||
| 89 | if ($unit) { | ||
| 90 | # Only the first character of the unit is useful, and it needs to be uppercase to key | ||
| 91 | # into %PointsPer. | ||
| 92 | my $uom = uc substr($unit, 0, 1); | ||
| 93 | |||
| 94 | if ($uom eq '%') { | ||
| 95 | #GGG Is this all that's needed? | ||
| 96 | if ($key eq 'W') { | ||
| 97 | $val *= ($self->get($obj, 'PAGE_WIDTH') - | ||
| 98 | $self->get($obj, 'LEFT_MARGIN') - | ||
| 99 | $self->get($obj, 'RIGHT_MARGIN')); | ||
| 100 | } | ||
| 101 | elsif ($key eq 'H') { | ||
| 102 | $val *= ($self->get($obj, 'PAGE_HEIGHT') - | ||
| 103 | $self->get($obj, 'HEADER_HEIGHT') - | ||
| 104 | $self->get($obj, 'FOOTER_HEIGHT')); | ||
| 105 | } | ||
| 106 | $val /= 100; | ||
| 107 | } | ||
| 108 | elsif (exists $PointsPer{$uom}) { | ||
| 109 | $val *= $PointsPer{$uom}; | ||
| 110 | } | ||
| 111 | else { | ||
| 112 | warn "'$unit' is not a recognized unit of measurement.", $/; | ||
| 113 | } | ||
| 114 | |||
| 115 | $obj->{$key} = $op . $val unless $is_param; | ||
| 116 | |||
| 117 | $obj_val = $val; | ||
| 118 | } | ||
| 119 | |||
| 120 | return $obj_val unless $op; | ||
| 121 | |||
| 122 | my $prev_val = $key eq 'X' || $key eq 'Y' | ||
| 123 | ? $self->{$key} | ||
| 124 | : $self->get($obj, $key, $depth + 1); | ||
| 125 | |||
| 126 | return $obj_val unless defined $prev_val; | ||
| 127 | return $prev_val unless defined $obj_val; | ||
| 128 | |||
| 129 | # Prevent divide-by-zero issues. | ||
| 130 | return $val if $op eq '/' and $val == 0; | ||
| 131 | |||
| 132 | my $new_val; | ||
| 133 | for ($op) { | ||
| 134 | /^\+$/ && do { $new_val = ($prev_val + $val); last; }; | ||
| 135 | /^\-$/ && do { $new_val = ($prev_val - $val); last; }; | ||
| 136 | /^\*$/ && do { $new_val = ($prev_val * $val); last; }; | ||
| 137 | /^\/$/ && do { $new_val = ($prev_val / $val); last; }; | ||
| 138 | |||
| 139 | die "Unknown operator '$op' in arithmetic resolve", $/; | ||
| 140 | } | ||
| 141 | |||
| 142 | return $new_val if defined $new_val; | ||
| 143 | return; | ||
| 144 | } | ||
| 145 | |||
| 146 | sub enter_scope { | ||
| 147 | my $self = shift; | ||
| 148 | my ($obj) = @_; | ||
| 149 | |||
| 150 | push @{$self->{STACK}}, $obj; | ||
| 151 | |||
| 152 | for my $key (qw(X Y)) { | ||
| 153 | next unless exists $obj->{$key}; | ||
| 154 | $self->{$key} = $self->resolve($obj, $key); | ||
| 155 | } | ||
| 156 | |||
| 157 | return 1; | ||
| 158 | } | ||
| 159 | |||
| 160 | sub exit_scope { | ||
| 161 | my $self = shift; | ||
| 162 | my ($obj, $no_delta) = @_; | ||
| 163 | |||
| 164 | unless ($no_delta) { | ||
| 165 | my $deltas = $obj->deltas($self); | ||
| 166 | $self->{$_} += $deltas->{$_} for keys %$deltas; | ||
| 167 | } | ||
| 168 | |||
| 169 | pop @{$self->{STACK}}; | ||
| 170 | |||
| 171 | return 1; | ||
| 172 | } | ||
| 173 | |||
| 174 | sub get { | ||
| 175 | my $self = shift; | ||
| 176 | my ($dummy, $key, $depth) = @_; | ||
| 177 | $depth ||= 0; | ||
| 178 | $key = uc $key; | ||
| 179 | |||
| 180 | return unless @{$self->{STACK}}; | ||
| 181 | |||
| 182 | my $obj = $self->{STACK}[-1]; | ||
| 183 | if (exists $obj->{"TEMP_$key"}) { | ||
| 184 | my $val = delete $obj->{"TEMP_$key"}; | ||
| 185 | return $val; | ||
| 186 | } | ||
| 187 | |||
| 188 | return $self->{$key} if $key eq 'X' || $key eq 'Y'; | ||
| 189 | |||
| 190 | my $val = undef; | ||
| 191 | my $this_depth = $depth; | ||
| 192 | foreach my $e (reverse @{$self->{STACK}}) { | ||
| 193 | next unless exists $e->{$key}; | ||
| 194 | next if $this_depth-- > 0; | ||
| 195 | |||
| 196 | $val = $self->resolve($e, $key, $depth); | ||
| 197 | last; | ||
| 198 | } | ||
| 199 | |||
| 200 | $val = $self->{$key} unless defined $val; | ||
| 201 | return $val unless defined $val; | ||
| 202 | |||
| 203 | return $self->param($1, $depth) if $val =~ /^\$(\S+)$/o; | ||
| 204 | |||
| 205 | return $val; | ||
| 206 | } | ||
| 207 | |||
| 208 | sub should_render { | ||
| 209 | my $self = shift; | ||
| 210 | my ($obj) = @_; | ||
| 211 | |||
| 212 | # The objects for which this would be bad are going to bypass this check as they | ||
| 213 | # see fit. All other objects should not render if the pagebreak has been tripped. | ||
| 214 | return 0 if $self->pagebreak_tripped; | ||
| 215 | |||
| 216 | return $self->check_end_of_page($obj); | ||
| 217 | } | ||
| 218 | |||
| 219 | sub check_end_of_page { | ||
| 220 | my $self = shift; | ||
| 221 | my ($obj) = @_; | ||
| 222 | |||
| 223 | my $deltas = $obj->deltas($self); | ||
| 224 | |||
| 225 | if ( | ||
| 226 | ($self->get($obj, 'Y') || 0) + ($deltas->{Y} || 0) | ||
| 227 | < ($self->get($obj, 'END_Y') || 0) | ||
| 228 | ) { | ||
| 229 | $self->trip_pagebreak; | ||
| 230 | return 0; | ||
| 231 | } | ||
| 232 | |||
| 233 | return 1; | ||
| 234 | } | ||
| 235 | |||
| 236 | sub close_images { | ||
| 237 | my $self = shift; | ||
| 238 | my $p = $self->{PDF}; | ||
| 239 | |||
| 240 | $p->close_image($_) for values %{$self->{IMAGES}}; | ||
| 241 | } | ||
| 242 | |||
| 243 | sub new_page_def { | ||
| 244 | my $self = shift; | ||
| 245 | |||
| 246 | $self->{PARAM_MAP}[0]{__PAGEDEF__}++; | ||
| 247 | $self->{PARAM_MAP}[0]{__PAGEDEF_PAGE__} = 1; | ||
| 248 | } | ||
| 249 | |||
| 250 | sub trip_pagebreak { $_[0]{PB_TRIP} = 1 } | ||
| 251 | sub reset_pagebreak { $_[0]{PB_TRIP} = 0 } | ||
| 252 | sub pagebreak_tripped { $_[0]{PB_TRIP} = $_[1] if defined $_[1]; $_[0]{PB_TRIP} } | ||
| 253 | sub store_font { $_[0]{FONTS}{$_[1]} ||= $_[2] } | ||
| 254 | sub retrieve_font { $_[0]{FONTS}{$_[1]} } | ||
| 255 | sub delete_fonts { $_[0]{FONTS} = {}; } | ||
| 256 | sub store_image { $_[0]{IMAGES}{$_[1]} ||= $_[2] } | ||
| 257 | sub retrieve_image { $_[0]{IMAGES}{$_[1]} } | ||
| 258 | sub increment_pagenumber { $_[0]{PARAM_MAP}[0]{$_}++ for qw(__PAGE__ __PAGEDEF_PAGE__) } | ||
| 259 | |||
| 260 | 1; | ||
| 261 | __END__ |