Skip to content

Commit

Permalink
More generic elements.
Browse files Browse the repository at this point in the history
This commit moves the codebase toward more generic elements where
each element type has common options X/Y which can be specified
absolutely or relative to the current cursor position.
  • Loading branch information
ajwans committed Apr 18, 2011
1 parent f431149 commit 490a613
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 41 deletions.
6 changes: 4 additions & 2 deletions lib/PDF/Template/Base.pm
Expand Up @@ -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;
}
Expand Down
24 changes: 9 additions & 15 deletions lib/PDF/Template/Container.pm
Expand Up @@ -78,38 +78,32 @@ 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);

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;
}
Expand Down
64 changes: 64 additions & 0 deletions lib/PDF/Template/Element.pm
Expand Up @@ -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__
Expand Down
4 changes: 1 addition & 3 deletions lib/PDF/Template/Element/Bookmark.pm
Expand Up @@ -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);
Expand Down
14 changes: 4 additions & 10 deletions lib/PDF/Template/Element/Line.pm
Expand Up @@ -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};
Expand All @@ -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);
Expand Down
59 changes: 51 additions & 8 deletions lib/PDF/Template/Element/Pos.pm
Expand Up @@ -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
<pos y="-10"/>
That moves the cursor form its current position down the page 10 points.
=head1 AUTHOR
Andrew Wansink (andy@halogix.com)
=head1 SEE ALSO
=cut
4 changes: 1 addition & 3 deletions lib/PDF/Template/Element/Weblink.pm
Expand Up @@ -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');
Expand Down

0 comments on commit 490a613

Please sign in to comment.