File: Line.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 (164 lines) | stat: -rw-r--r-- 3,173 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package PDF::FromHTML::Template::Element::Line;

use strict;

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

    use PDF::FromHTML::Template::Element;
}

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

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

    return 1 if $context->{CALC_LAST_PAGE};

    my $p = $context->{PDF};
    $p->save_state;

    $self->set_color($context, 'COLOR', 'both');

    my $vals = $self->make_vals($context);

    my $width = $context->get($self, 'WIDTH') || 1;

    $p->linewidth($width);
    $p->move($vals->{X1}, $vals->{Y1});
    $p->line($vals->{X2}, $vals->{Y2});
    $p->stroke;

    $p->restore_state;

    return 1;
}

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

    my ($x1, $x2, $y1, $y2) = map { $context->get($self, $_) } qw(X1 X2 Y1 Y2);

    my %vals;
    unless (defined $x1 && defined $x2)
    {
#GGG Is the use of W a potential bug here?
        my ($pw, $left, $right, $w) = map {
            $context->get($self, $_)
        } qw( PAGE_WIDTH LEFT_MARGIN RIGHT_MARGIN W );

        $w = $pw - $right - $left unless defined $w;

        if (defined $x1)
        {
            $x2 = $x1 + $w;
            $x2 = $right if $x2 > $right;
        }
        elsif (defined $x2)
        {
            $x1 = $x2 - $w;
            $x1 = $left if $x1 < $left;
        }
        else
        {
            $x1 = $left;
            $x2 = $x1 + $w;
        }
    }
    @vals{qw(X1 X2)} = ($x1, $x2);

    unless (defined $y1 && defined $y2)
    {
        if (defined $y1)
        {
            $y2 = $y1;
        }
        elsif (defined $y2)
        {
            $y1 = $y2;
        }
        else
        {
            $y1 = $y2 = $context->get($self, 'Y');
        }
    }
    @vals{qw(Y1 Y2)} = ($y1, $y2);

    $self->{VALS} = \%vals;

    return \%vals;
}

1;
__END__

=head1 NAME

PDF::FromHTML::Template::Element::Line - To draw lines

=head1 NODE NAME

LINE

=head1 INHERITANCE

PDF::FromHTML::Template::Element

=head1 ATTRIBUTES

=over 4

=item * X1 / X2 / Y1 / Y2
The line is drawn from (X1,Y1) to (X2,Y2).

If neither X1 nor X2 are set, X1 is set to the lefthand margin and X2 is set
to X1 + W. If only one is set, the other is set to the first +/- W.

If either of the Y values is not set, it is set to the current Y value.

=item * W
This is the width of the line to be drawn. Used only in calculating X1/X2/Y1/Y2
and only if needed. (q.v. above) Defaults to the distance between the left and
right margins. (q.v. PAGEDEF for more information on these parameters.)

=item * WIDTH
This is the thickness of the line to be drawn. Defaults to 1 pixel.

=item * COLOR
This is the color to draw the line in. Defaults to black.

=back

=head1 CHILDREN

PDF::FromHTML::Template::Element::HorizontalRule

=head1 AFFECTS

Nothing

=head1 DEPENDENCIES

None

=head1 USAGE

  <line X1="1i" Y1="1i" X2="3i" Y2="2i" WIDTH="3" COLOR="0,0,255"/>

This will draw a blue line 3 pixels thick from the spot 1" in from the left and
top to the spot 3" from the left and 2" from the top.

=head1 AUTHOR

Rob Kinyon (rkinyon@columbus.rr.com)

=head1 SEE ALSO

PAGEDEF, HR

=cut