« lib/PDF/FromHTML/Template/Element/Line.pm
↑ Summary
lib/PDF/FromHTML/Template/Container/Conditional.pm »
| 1 | package PDF::FromHTML::Template::Element::Image; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | |||
| 5 | BEGIN { | ||
| 6 | use vars qw(@ISA); | ||
| 7 | @ISA = qw(PDF::FromHTML::Template::Element); | ||
| 8 | |||
| 9 | use PDF::FromHTML::Template::Element; | ||
| 10 | } | ||
| 11 | |||
| 12 | sub new | ||
| 13 | { | ||
| 14 | my $class = shift; | ||
| 15 | my $self = $class->SUPER::new(@_); | ||
| 16 | |||
| 17 | $self->{TXTOBJ} = PDF::FromHTML::Template::Factory->create('TEXTOBJECT'); | ||
| 18 | |||
| 19 | return $self; | ||
| 20 | } | ||
| 21 | |||
| 22 | sub _deltas { | ||
| 23 | my $self = shift; | ||
| 24 | my ($context) = @_; | ||
| 25 | |||
| 26 | return { | ||
| 27 | X => $context->get($self, 'W'), | ||
| 28 | Y => $context->get($self, 'H'), | ||
| 29 | }; | ||
| 30 | } | ||
| 31 | |||
| 32 | my %convertImageType = ( | ||
| 33 | 'jpg' => 'jpeg', | ||
| 34 | ); | ||
| 35 | |||
| 36 | sub begin_page | ||
| 37 | { | ||
| 38 | my $self = shift; | ||
| 39 | my ($context) = @_; | ||
| 40 | |||
| 41 | return 1 if $context->{CALC_LAST_PAGE}; | ||
| 42 | |||
| 43 | my $txt = $context->get($self, 'FILENAME') || | ||
| 44 | $self->{TXTOBJ}->resolve($context) || | ||
| 45 | die "Image does not have a filename", $/; | ||
| 46 | |||
| 47 | my $image = $context->retrieve_image($txt); | ||
| 48 | my $p = $context->{PDF}; | ||
| 49 | |||
| 50 | unless ($image) | ||
| 51 | { | ||
| 52 | # automatically resolve type if extension is obvious and type was not specified | ||
| 53 | my $type = $context->get($self, 'TYPE'); | ||
| 54 | |||
| 55 | unless ($type) | ||
| 56 | { | ||
| 57 | ($type) = $txt =~ /\.(\w+)$/o; | ||
| 58 | } | ||
| 59 | unless ($type) | ||
| 60 | { | ||
| 61 | die "Undefined type for <image> '$txt'", $/; | ||
| 62 | } | ||
| 63 | |||
| 64 | $type = lc $type; | ||
| 65 | $type = $convertImageType{$type} if exists $convertImageType{$type}; | ||
| 66 | |||
| 67 | $image = $p->open_image($type, $txt, '', 0); | ||
| 68 | $image == -1 and die "Cannot open <image> file '$txt'", $/; | ||
| 69 | |||
| 70 | $context->store_image($txt, $image); | ||
| 71 | } | ||
| 72 | |||
| 73 | $self->{IMAGE_HEIGHT} = $p->image_height($image); | ||
| 74 | $self->{IMAGE_WIDTH} = $p->image_width($image); | ||
| 75 | |||
| 76 | die "Image '$txt' has 0 (or less) height.", $/ if $self->{IMAGE_HEIGHT} <= 0; | ||
| 77 | die "Image '$txt' has 0 (or less) width.", $/ if $self->{IMAGE_WIDTH} <= 0; | ||
| 78 | |||
| 79 | return 1; | ||
| 80 | } | ||
| 81 | |||
| 82 | sub render | ||
| 83 | { | ||
| 84 | my $self = shift; | ||
| 85 | my ($context) = @_; | ||
| 86 | |||
| 87 | return 0 unless $self->should_render($context); | ||
| 88 | |||
| 89 | return 1 if $context->{CALC_LAST_PAGE}; | ||
| 90 | |||
| 91 | my $txt = $context->get($self, 'FILENAME') || | ||
| 92 | $self->{TXTOBJ}->resolve($context) || | ||
| 93 | die "Image does not have a filename", $/; | ||
| 94 | |||
| 95 | my $image = $context->retrieve_image($txt); | ||
| 96 | $image == -1 && die "Image not found for '$txt' when <image> is rendered.", $/; | ||
| 97 | |||
| 98 | $self->set_values($context, $txt); | ||
| 99 | |||
| 100 | my $p = $context->{PDF}; | ||
| 101 | my ($x, $y, $scale) = map { $context->get($self, $_) } qw(X Y SCALE); | ||
| 102 | |||
| 103 | $p->place_image( $image, $x, $y - $self->{H}, $scale ); | ||
| 104 | |||
| 105 | if ($context->get($self, 'BORDER')) | ||
| 106 | { | ||
| 107 | $p->save_state; | ||
| 108 | |||
| 109 | $self->set_color($context, 'COLOR', 'both'); | ||
| 110 | |||
| 111 | my ($w, $h) = map { $context->get($self, $_) } qw(W H); | ||
| 112 | |||
| 113 | $p->rect($x, $y, $w, $h); | ||
| 114 | $p->stroke; | ||
| 115 | $p->restore_state; | ||
| 116 | } | ||
| 117 | |||
| 118 | return 1; | ||
| 119 | } | ||
| 120 | |||
| 121 | sub set_values | ||
| 122 | { | ||
| 123 | my $self = shift; | ||
| 124 | my ($context, $txt) = @_; | ||
| 125 | |||
| 126 | my $scale = $context->get($self, 'SCALE'); | ||
| 127 | |||
| 128 | if (defined $scale) | ||
| 129 | { | ||
| 130 | die "Scale is zero or less when rendering <image> '$txt'.", $/ if $scale <= 0; | ||
| 131 | $self->{W} = $self->{IMAGE_WIDTH} * $scale; | ||
| 132 | $self->{H} = $self->{IMAGE_HEIGHT} * $scale; | ||
| 133 | } | ||
| 134 | else | ||
| 135 | { | ||
| 136 | my ($w, $h) = map { $context->get($self, $_) } qw(W H); | ||
| 137 | if (defined $w && defined $h) | ||
| 138 | { | ||
| 139 | die "Height of zero or less in <image> '$txt'.", $/ if $h <= 0; | ||
| 140 | die "Width of zero or less in <image> '$txt'.", $/ if $w <= 0; | ||
| 141 | |||
| 142 | my $test_scale = $w / $h; | ||
| 143 | if ($test_scale == ($self->{IMAGE_WIDTH}/$self->{IMAGE_HEIGHT})) | ||
| 144 | { | ||
| 145 | $self->{SCALE} = $test_scale; | ||
| 146 | } | ||
| 147 | else | ||
| 148 | { | ||
| 149 | undef $h; | ||
| 150 | } | ||
| 151 | } | ||
| 152 | |||
| 153 | if (defined $w) | ||
| 154 | { | ||
| 155 | $self->{SCALE} = $w / $self->{IMAGE_WIDTH}; | ||
| 156 | $self->{H} = $self->{IMAGE_HEIGHT} * $self->{SCALE}; | ||
| 157 | } | ||
| 158 | elsif (defined $h) | ||
| 159 | { | ||
| 160 | $self->{SCALE} = $h / $self->{IMAGE_HEIGHT}; | ||
| 161 | $self->{W} = $self->{IMAGE_WIDTH} * $self->{SCALE}; | ||
| 162 | } | ||
| 163 | else | ||
| 164 | { | ||
| 165 | $self->{SCALE} = 0.5; | ||
| 166 | $self->{W} = $self->{IMAGE_WIDTH} * $self->{SCALE}; | ||
| 167 | $self->{H} = $self->{IMAGE_HEIGHT} * $self->{SCALE}; | ||
| 168 | } | ||
| 169 | } | ||
| 170 | |||
| 171 | return 1; | ||
| 172 | } | ||
| 173 | |||
| 174 | 1; | ||
| 175 | __END__ |