File: Section.pm

package info (click to toggle)
libpdf-fromhtml-perl 0.34-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 576 kB
  • sloc: perl: 4,876; makefile: 15
file content (130 lines) | stat: -rw-r--r-- 2,208 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package PDF::FromHTML::Template::Container::Section;

use strict;

BEGIN {
    use vars qw(@ISA);
    @ISA = qw(PDF::FromHTML::Template::Container);

    use PDF::FromHTML::Template::Container;
}

# Sections are used to keep text together and not allow page-breaking
# within this branch of the tree, if possible.

sub new
{
    my $class = shift;
    my $self = $class->SUPER::new(@_);

    $self->{__CHECK_FOR_SPACE__} = 1;

    return $self;
}

sub reset
{
    my $self = shift;

    $self->{__CHECK_FOR_SPACE__} = 1;

    return $self->SUPER::reset;
}

sub render
{
    my $self = shift;
    my ($context) = @_;

    return 0 unless $self->should_render($context);

    my $child_success = $self->iterate_over_children($context);

    $self->{__CHECK_FOR_SPACE__} = $child_success;

    return $child_success;
}

sub should_render
{
    my $self = shift;
    my ($context) = @_;

    return 0 if $context->pagebreak_tripped;

    unless ($self->{__CHECK_FOR_SPACE__})
    {
        $self->{__CHECK_FOR_SPACE__} = 1;
        return 1;
    }

    my $y_shift = $self->total_of($context, 'H');
    my $end_y = $context->get($self, 'END_Y');

    if ($context->{Y} - $y_shift < $end_y)
    {
        my $start_y = $context->get($self, 'START_Y');

        $self->{__CHECK_FOR_SPACE__} = 0 if $y_shift > ($start_y - $end_y);

        return 1 if $context->{Y} == $start_y;

        $context->trip_pagebreak;
        return 0;
    }

    return 1;
}

1;
__END__

=head1 NAME

PDF::FromHTML::Template::Container::Section - Provide pagebreak control for a group of children

-head1 DESCRIPTION

To provide a keep-together for children. If a pagebreak would occur within the
section tag, then the entire branch is rendered on the next page. If the branch
would take more than a page anyways, the section tag is ignored.

=head1 NODE NAME

SECTION

=head1 INHERITANCE

PDF::FromHTML::Template::Container

=head1 ATTRIBUTES

None

=head1 CHILDREN

None

=head1 AFFECTS

Nothing

=head1 DEPENDENCIES

None

=head1 USAGE

  <section>
    .. Children here ...
  </section>

The children will be rendered on the same page, if at all possible.

=head1 AUTHOR

Rob Kinyon (rkinyon@columbus.rr.com)

=head1 SEE ALSO

=cut