diff --git a/lib/PDF/Template/Base.pm b/lib/PDF/Template/Base.pm index 5f4abd2..50c01bf 100755 --- a/lib/PDF/Template/Base.pm +++ b/lib/PDF/Template/Base.pm @@ -116,8 +116,10 @@ sub resolve sub render { -# my $self = shift; -# my ($context) = @_; + my $self = shift; + my ($context) = @_; + + warn "\t\tXrendering $self->{NAME}" if $context->{DEBUG}; return 1; } diff --git a/lib/PDF/Template/Container.pm b/lib/PDF/Template/Container.pm index 4b76374..2d76c4c 100755 --- a/lib/PDF/Template/Container.pm +++ b/lib/PDF/Template/Container.pm @@ -78,28 +78,21 @@ sub iterate_over_children my $continue = 1; - if ($context->{DEBUG}) { - warn "rendering " . ref($self) . " at " . $context->{X} . ", " . - $context->{Y} . "\n"; - warn "rendered items\n\t" . join("\n\t", map { ref($_) } - grep { $_->has_rendered() } @{$self->{ELEMENTS}}) . "\n"; - warn "unrendered items\n\t" . join("\n\t", map { ref($_) } - grep { !$_->has_rendered() } @{$self->{ELEMENTS}}) . "\n"; - } + warn "\t" x $context->{LEVEL} . + "rendering $self->{_NAME} at $context->{X},$context->{Y}\n" + if ($context->{DEBUG}); + + $context->{LEVEL}++; for my $e (grep !$_->has_rendered, @{$self->{ELEMENTS}}) { $e->enter_scope($context); my $rc; - warn "\trendering " . ref($e) . " at " . $context->{X} . ", " . - $context->{Y} . "\n" if $context->{DEBUG}; if ($rc = $e->render($context)) { - warn "\trendered " . ref($e) . "\n" if $context->{DEBUG}; $e->mark_as_rendered; } - warn "\tresult $rc\n" if $context->{DEBUG}; $continue = $rc if $continue; $e->exit_scope($context); @@ -107,9 +100,10 @@ sub iterate_over_children last if (!$continue && $context->pagebreak_tripped()); } - if ($context->{DEBUG}) { - warn "rendered /" . ref($self) . " result $continue\n"; - } + $context->{LEVEL}--; + + warn "\t" x $context->{LEVEL} . "rendered /" . ref($self) . + " result $continue\n" if ($context->{DEBUG}); return $continue; } diff --git a/lib/PDF/Template/Element.pm b/lib/PDF/Template/Element.pm index 7da7fa1..7a819ef 100755 --- a/lib/PDF/Template/Element.pm +++ b/lib/PDF/Template/Element.pm @@ -22,6 +22,70 @@ sub set_color return 1; } +# default render does nothing +sub _render +{} + +sub render +{ + my ($self, $context) = @_; + + return 0 unless $self->should_render($context); + + $self->prerender($context); + $self->_render($context); + $self->postrender($context); + + return 1; +} + +sub prerender +{ + my ($self, $context) = @_; + + my ($X, $Y) = map { $context->get($self, $_) } qw/X Y/; + + # allow relative positioning of the cursor + given ($self->{Y}) { + when (undef) { + } + + when (m/^-(\d+)/) { + $Y -= $1; + } + + when (m/^[+](\d+)/) { + $Y += $1; + } + + default { + $Y = $self->{Y}; + } + } + + # save for later in case this element should not move the cursor + map { $self->{'OLD_' . $_} = $_ } qw/X Y/; + + my $p = $context->{PDF}; + $p->move($X, $Y); + @{$context}{qw/X Y/} = ($X, $Y); + + warn "\t" x $context->{LEVEL} . "rendering $self->{TAG} at $X,$Y" + if $context->{DEBUG}; +} + +sub postrender +{ + my ($self, $context) = @_; + my $reset_cursor = $self->{RESET_CURSOR}; + + # reset the X/Y coordinates if no_translate attribute is set + if ($reset_cursor) { + warn "reset cursor set, resetting coords" if $context->{DEBUG}; + @{$context}{qw/X Y/} = @{$self}{qw/OLD_X OLD_Y/}; + } +} + 1; __END__ diff --git a/lib/PDF/Template/Element/Bookmark.pm b/lib/PDF/Template/Element/Bookmark.pm index 970148e..76fbb9e 100755 --- a/lib/PDF/Template/Element/Bookmark.pm +++ b/lib/PDF/Template/Element/Bookmark.pm @@ -15,13 +15,11 @@ sub new return $self; } -sub render +sub _render { my $self = shift; my ($context) = @_; - return 0 unless $self->should_render($context); - return 1 if $context->{CALC_LAST_PAGE}; my $txt = $self->{TXTOBJ}->resolve($context); diff --git a/lib/PDF/Template/Element/Line.pm b/lib/PDF/Template/Element/Line.pm index 8840fb5..33f5535 100755 --- a/lib/PDF/Template/Element/Line.pm +++ b/lib/PDF/Template/Element/Line.pm @@ -5,20 +5,11 @@ use warnings; use base 'PDF::Template::Element'; -sub render +sub _render { my $self = shift; my ($context) = @_; - if (!$self->should_render($context)) { - warn "not rendering " . ref($self) . "\n"; - warn "\tbecause has rendered\n" if $self->{__THIS_HAS_RENDERED__}; - warn "\tbecause context\n" if !$context->should_render($self); - return 0; - } - - return 0 unless $self->should_render($context); - return 1 if $context->{CALC_LAST_PAGE}; my $p = $context->{PDF}; @@ -28,6 +19,9 @@ sub render my $vals = $self->make_vals($context); + warn(sprintf("rendering line %d,%d -> %d,%d", @{$vals}{qw/X1 Y1 X2 Y2/})) + if $context->{DEBUG}; + my $width = $context->get($self, 'WIDTH') || 1; $p->linewidth($width); diff --git a/lib/PDF/Template/Element/Pos.pm b/lib/PDF/Template/Element/Pos.pm index 02d87ef..07d1cc6 100644 --- a/lib/PDF/Template/Element/Pos.pm +++ b/lib/PDF/Template/Element/Pos.pm @@ -14,13 +14,56 @@ sub new return $self; } -sub render -{ - my $self = shift; - my ($context) = @_; - - $context->{Y} = $self->{Y}; -} - 1; __END__ + +=head1 NAME + +PDF::Template::Element::Pos + +=head1 PURPOSE + +Move the cursor to a new position + +=head1 NODE NAME + +POS + +=head1 INHERITANCE + +PDF::Template::Element + +=head1 ATTRIBUTES + +=over 1 + +=item * Y +The new Y position, either absolute or +/- to make a relative move + +=back + +=head1 CHILDREN + +None + +=head1 AFFECTS + +Resultant PDF + +=head1 DEPENDENCIES + +None + +=head1 USAGE + + + +That moves the cursor form its current position down the page 10 points. + +=head1 AUTHOR + +Andrew Wansink (andy@halogix.com) + +=head1 SEE ALSO + +=cut diff --git a/lib/PDF/Template/Element/Weblink.pm b/lib/PDF/Template/Element/Weblink.pm index bc82746..78c0428 100755 --- a/lib/PDF/Template/Element/Weblink.pm +++ b/lib/PDF/Template/Element/Weblink.pm @@ -15,13 +15,11 @@ sub new return $self; } -sub render +sub _render { my $self = shift; my ($context) = @_; - return 0 unless $self->should_render($context); - return 1 if $context->{CALC_LAST_PAGE}; my $url = $context->get($self, 'URL');