1package PDF::FromHTML::Template::Container::Section;
2
3use strict;
4
5BEGIN {
6    use vars qw(@ISA);
7    @ISA = qw(PDF::FromHTML::Template::Container);
8
9    use PDF::FromHTML::Template::Container;
10}
11
12# Sections are used to keep text together and not allow page-breaking
13# within this branch of the tree, if possible.
14
15sub new
16{
17    my $class = shift;
18    my $self = $class->SUPER::new(@_);
19
20    $self->{__CHECK_FOR_SPACE__} = 1;
21
22    return $self;
23}
24
25sub reset
26{
27    my $self = shift;
28
29    $self->{__CHECK_FOR_SPACE__} = 1;
30
31    return $self->SUPER::reset;
32}
33
34sub render
35{
36    my $self = shift;
37    my ($context) = @_;
38
39    return 0 unless $self->should_render($context);
40
41    my $child_success = $self->iterate_over_children($context);
42
43    $self->{__CHECK_FOR_SPACE__} = $child_success;
44
45    return $child_success;
46}
47
48sub should_render
49{
50    my $self = shift;
51    my ($context) = @_;
52
53    return 0 if $context->pagebreak_tripped;
54
55    unless ($self->{__CHECK_FOR_SPACE__})
56    {
57        $self->{__CHECK_FOR_SPACE__} = 1;
58        return 1;
59    }
60
61    my $y_shift = $self->total_of($context, 'H');
62    my $end_y = $context->get($self, 'END_Y');
63
64    if ($context->{Y} - $y_shift < $end_y)
65    {
66        my $start_y = $context->get($self, 'START_Y');
67
68        $self->{__CHECK_FOR_SPACE__} = 0 if $y_shift > ($start_y - $end_y);
69
70        return 1 if $context->{Y} == $start_y;
71
72        $context->trip_pagebreak;
73        return 0;
74    }
75
76    return 1;
77}
78
791;
80__END__
81
82=head1 NAME
83
84PDF::FromHTML::Template::Container::Section
85
86=head1 PURPOSE
87
88To provide a keep-together for children. If a pagebreak would occur within the
89section tag, then the entire branch is rendered on the next page. If the branch
90would take more than a page anyways, the section tag is ignored.
91
92=head1 NODE NAME
93
94SECTION
95
96=head1 INHERITANCE
97
98PDF::FromHTML::Template::Container
99
100=head1 ATTRIBUTES
101
102None
103
104=head1 CHILDREN
105
106None
107
108=head1 AFFECTS
109
110Nothing
111
112=head1 DEPENDENCIES
113
114None
115
116=head1 USAGE
117
118  <section>
119    .. Children here ...
120  </section>
121
122The children will be rendered on the same page, if at all possible.
123
124=head1 AUTHOR
125
126Rob Kinyon (rkinyon@columbus.rr.com)
127
128=head1 SEE ALSO
129
130=cut
131