Bug 29957: Adjust push @$cookie statements in Auth
[koha.git] / C4 / Patroncards / Patroncard.pm
1 package C4::Patroncards::Patroncard;
2
3 # Copyright 2009 Foundations Bible College.
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use strict;
21 use warnings;
22
23 use autouse 'Data::Dumper' => qw(Dumper);
24 #use Font::TTFMetrics;
25
26 use C4::Creators::Lib qw( get_unit_values );
27 use C4::Creators::PDF qw(StrWidth);
28 use C4::Patroncards::Lib qw(
29     box
30     get_borrower_attributes
31     leading
32     text_alignment
33 );
34
35 =head1 NAME
36
37 C4::Patroncards::Patroncard
38
39 =head1 SYNOPSIS
40
41     use C4::Patroncards::Patroncard;
42
43     # Please extend
44
45
46 =head1 DESCRIPTION
47
48    This module allows you to ...
49
50 =head1 FUNCTIONS
51
52 =head2 new
53
54 =cut
55
56 sub new {
57     my ($invocant, %params) = @_;
58     my $type = ref($invocant) || $invocant;
59
60     my $units = get_unit_values();
61     my $unitvalue = 1;
62     my $unitdesc = '';
63     foreach my $un (@$units){
64         if ($un->{'type'} eq $params{'layout'}->{'units'}) {
65             $unitvalue = $un->{'value'};
66             $unitdesc = $un->{'desc'};
67         }
68     }
69
70     my $self = {
71         batch_id                => $params{'batch_id'},
72         #card_number             => $params{'card_number'},
73         borrower_number         => $params{'borrower_number'},
74         llx                     => $params{'llx'},
75         lly                     => $params{'lly'},
76         height                  => $params{'height'},
77         width                   => $params{'width'},
78         layout                  => $params{'layout'},
79         unitvalue               => $unitvalue,
80         unitdesc                => $unitdesc,
81         text_wrap_cols          => $params{'text_wrap_cols'},
82         barcode_height_scale    => $params{'layout'}->{'barcode'}[0]->{'height_scale'} || 0.01,
83         barcode_width_scale     => $params{'layout'}->{'barcode'}[0]->{'width_scale'} || 0.8,
84     };
85     bless ($self, $type);
86     return $self;
87 }
88
89 =head2 draw_barcode
90
91 =cut
92
93 sub draw_barcode {
94     my ($self, $pdf) = @_;
95     # Default values for barcode scaling are set in constructor to work with pre-existing installations
96     my $barcode_height_scale = $self->{'barcode_height_scale'};
97     my $barcode_width_scale = $self->{'barcode_width_scale'};
98
99     _draw_barcode(      $self,
100                         llx     => $self->{'llx'} + $self->{'layout'}->{'barcode'}->[0]->{'llx'} * $self->{'unitvalue'},
101                         lly     => $self->{'lly'} + $self->{'layout'}->{'barcode'}->[0]->{'lly'} * $self->{'unitvalue'},
102                         width   => $self->{'width'} * $barcode_width_scale,
103                         y_scale_factor  => $self->{'height'} * $barcode_height_scale,
104                         barcode_type    => $self->{'layout'}->{'barcode'}->[0]->{'type'},
105                         barcode_data    => $self->{'layout'}->{'barcode'}->[0]->{'data'},
106                         text    => $self->{'layout'}->{'barcode'}->[0]->{'text_print'},
107     );
108 }
109
110 =head2 draw_guide_box
111
112 =cut
113
114 sub draw_guide_box {
115     my ($self, $pdf) = @_;
116     warn sprintf('No pdf object passed in.') and return -1 if !$pdf;
117
118     my $obj_stream = "q\n";                            # save the graphic state
119     $obj_stream .= "0.5 w\n";                          # border line width
120     $obj_stream .= "1.0 0.0 0.0  RG\n";                # border color red
121     $obj_stream .= "1.0 1.0 1.0  rg\n";                # fill color white
122     $obj_stream .= "$self->{'llx'} $self->{'lly'} $self->{'width'} $self->{'height'} re\n";    # a rectangle
123     $obj_stream .= "B\n";                              # fill (and a little more)
124     $obj_stream .= "Q\n";                              # restore the graphic state
125     $pdf->Add($obj_stream);
126 }
127
128 =head2 draw_guide_grid
129
130     $patron_card->draw_guide_grid($pdf)
131
132 Adds a grid to the PDF output ($pdf) to support layout design
133
134 =cut
135
136 sub draw_guide_grid {
137     my ($self, $pdf) = @_;
138     warn sprintf('No pdf object passed in.') and return -1 if !$pdf;
139
140     # Set up the grid in user defined units.
141     # Each 5th and 10th line get separate values
142
143     my $obj_stream = "q\n";   # save the graphic state
144     my $x = $self->{'llx'};
145     my $y = $self->{'lly'};
146
147     my $cnt = 0;
148     for ( $x = $self->{'llx'}/$self->{'unitvalue'}; $x <= ($self->{'llx'} + $self->{'width'})/$self->{'unitvalue'}; $x++) {
149         my $xx = $x*$self->{'unitvalue'};
150         my $yy = $y + $self->{'height'};
151         if ( ($cnt % 10) && ! ($cnt % 5) ) {
152             $obj_stream .= "0.0 1.0 0.0  RG\n";
153             $obj_stream .= "0 w\n";
154         } elsif ( $cnt % 5 ) {
155             $obj_stream .= "0.0 1.0 1.0  RG\n";
156             $obj_stream .= "0 w\n";
157         } else {
158             $obj_stream .= "0.0 0.0 1.0  RG\n";
159             $obj_stream .= "0 w\n";
160         }
161         $cnt ++;
162
163         $obj_stream .= "$xx $y m\n";
164         $obj_stream .= "$xx $yy l\n";
165
166         $obj_stream .= "s\n";
167     }
168
169     $x = $self->{'llx'};
170     $y = $self->{'lly'};
171     $cnt = 0;
172     for ( $y = $self->{'lly'}/$self->{'unitvalue'}; $y <= ($self->{'lly'} + $self->{'height'})/$self->{'unitvalue'}; $y++) {
173
174         my $xx = $x + $self->{'width'};
175         my $yy = $y*$self->{'unitvalue'};
176
177         if ( ($cnt % 10) && ! ($cnt % 5) ) {
178             $obj_stream .= "0.0 1.0 0.0  RG\n";
179             $obj_stream .= "0 w\n";
180         } elsif ( $cnt % 5 ) {
181             $obj_stream .= "0.0 1.0 1.0  RG\n";
182             $obj_stream .= "0 w\n";
183         } else {
184             $obj_stream .= "0.0 0.0 1.0  RG\n";
185             $obj_stream .= "0 w\n";
186         }
187         $cnt ++;
188
189         $obj_stream .= "$x $yy m\n";
190         $obj_stream .= "$xx $yy l\n";
191         $obj_stream .= "s\n";
192     }
193
194     $obj_stream .= "Q\n"; # restore the graphic state
195     $pdf->Add($obj_stream);
196
197     # Add info about units
198     my $strbottom = "0/0 $self->{'unitdesc'}";
199     my $strtop = sprintf('%.2f', $self->{'width'}/$self->{'unitvalue'}) .'/'. sprintf('%.2f', $self->{'height'}/$self->{'unitvalue'});
200     my $font_size = 6;
201     $pdf->Font( 'Courier' );
202     $pdf->FontSize( $font_size );
203     my $strtop_len = $pdf->StrWidth($strtop) * 1.5;
204     $pdf->Text( $self->{'llx'} + 2, $self->{'lly'} + 2, $strbottom );
205     $pdf->Text( $self->{'llx'} + $self->{'width'} - $strtop_len , $self->{'lly'} + $self->{'height'} - $font_size , $strtop );
206 }
207
208 =head2 draw_text
209
210     $patron_card->draw_text($pdf)
211
212 Draws text to PDF output ($pdf)
213
214 =cut
215
216 sub draw_text {
217     my ($self, $pdf, %params) = @_;
218     warn sprintf('No pdf object passed in.') and return -1 if !$pdf;
219     my @card_text = ();
220     return unless (ref($self->{'layout'}->{'text'}) eq 'ARRAY'); # just in case there is not text
221
222     my $text = [@{$self->{'layout'}->{'text'}}]; # make a copy of the arrayref *not* simply a pointer
223     while (scalar @$text) {
224         my $line = shift @$text;
225         my $parse_line = $line;
226         my @orig_line = split(/ /,$line);
227         if ($parse_line =~ m/<[A-Za-z0-9_]+>/) {     # test to see if the line has db fields embedded...
228             my @fields = ();
229             while ($parse_line =~ m/<([A-Za-z0-9_]+)>(.*$)/) {
230                 push (@fields, $1);
231                 $parse_line = $2;
232             }
233             my $borrower_attributes = get_borrower_attributes($self->{'borrower_number'},@fields);
234             @orig_line = map { # substitute data for db fields
235                 my $l = $_;
236                 if ($l =~ m/<([A-Za-z0-9_]+)>/) {
237                     my $field = $1;
238                     $l =~ s/$l/$borrower_attributes->{$field}/;
239                 }
240                 $l;
241             } @orig_line;
242             $line = join(' ',@orig_line);
243         }
244         my $text_attribs = shift @$text;
245         my $origin_llx = $self->{'llx'} + $text_attribs->{'llx'} * $self->{'unitvalue'};
246         my $origin_lly = $self->{'lly'} + $text_attribs->{'lly'} * $self->{'unitvalue'};
247         my $Tx = 0;     # final text llx
248         my $Ty = $origin_lly;   # final text lly
249         my $Tw = 0;     # final text word spacing. See http://www.adobe.com/devnet/pdf/pdf_reference.html ISO 32000-1
250 #FIXME: Move line wrapping code to its own sub if possible
251         my $trim = '';
252         my @lines = ();
253 #FIXME: Using embedded True Type fonts is a far superior way of handing things as well as being much more unicode friendly.
254 #       However this will take significant work using better than PDF::Reuse to do it. For the time being, I'm leaving
255 #       the basic code here commented out to preserve the basic method of accomplishing this. -chris_n
256 #
257 #        my $m = Font::TTFMetrics->new("/usr/share/fonts/truetype/msttcorefonts/Times_New_Roman_Bold.ttf");
258 #        my $units_per_em =  $m->get_units_per_em();
259 #        my $font_units_width = $m->string_width($line);
260 #        my $string_width = ($font_units_width * $text_attribs->{'font_size'}) / $units_per_em;
261         my $string_width = C4::Creators::PDF->StrWidth($line, $text_attribs->{'font'}, $text_attribs->{'font_size'});
262         if (($string_width + $text_attribs->{'llx'}) > $self->{'width'}) {
263             WRAP_LINES:
264             while (1) {
265 #                $line =~ m/^.*(\s\b.*\b\s*|\s&|\<\b.*\b\>)$/; # original regexp... can be removed after dev stage is over
266                 $line =~ m/^.*(\s.*\s*|\s&|\<.*\>)$/;
267                 $trim = $1 . $trim;
268                 #Sanitize the input into this regular expression so regex metacharacters are escaped as literal values (https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=22429)
269                 $line =~ s/\Q$1\E$//;
270                 $string_width = C4::Creators::PDF->StrWidth($line, $text_attribs->{'font'}, $text_attribs->{'font_size'});
271 #                $font_units_width = $m->string_width($line);
272 #                $string_width = ($font_units_width * $text_attribs->{'font_size'}) / $units_per_em;
273                 if (($string_width + $text_attribs->{'llx'}) < $self->{'width'}) {
274                     ($Tx, $Tw) = text_alignment($origin_llx, $self->{'width'}, $text_attribs->{'llx'}, $string_width, $line, $text_attribs->{'text_alignment'});
275                     push @lines, {line=> $line, Tx => $Tx, Ty => $Ty, Tw => $Tw};
276                     $line = undef;
277                     last WRAP_LINES if $trim eq '';
278                     $Ty -= leading($text_attribs->{'font_size'});
279                     $line = $trim;
280                     $trim = '';
281                     $string_width = C4::Creators::PDF->StrWidth($line, $text_attribs->{'font'}, $text_attribs->{'font_size'});
282                     #$font_units_width = $m->string_width($line);
283                     #$string_width = ($font_units_width * $text_attribs->{'font_size'}) / $units_per_em;
284                     if ( $string_width + ( $text_attribs->{'llx'} * $self->{'unitvalue'} ) < $self->{'width'}) {
285                         ($Tx, $Tw) = text_alignment($origin_llx, $self->{'width'}, $text_attribs->{'llx'} * $self->{'unitvalue'}, $string_width, $line, $text_attribs->{'text_alignment'});
286                         $line =~ s/^\s+//g;     # strip naughty leading spaces
287                         push @lines, {line=> $line, Tx => $Tx, Ty => $Ty, Tw => $Tw};
288                         last WRAP_LINES;
289                     }
290                 }
291             }
292         }
293         else {
294             ($Tx, $Tw) = text_alignment($origin_llx, $self->{'width'}, $text_attribs->{'llx'} * $self->{'unitvalue'}, $string_width, $line, $text_attribs->{'text_alignment'});
295             $line =~ s/^\s+//g;     # strip naughty leading spaces
296             push @lines, {line=> $line, Tx => $Tx, Ty => $Ty, Tw => $Tw};
297         }
298 # Draw boxes around text box areas
299 # FIXME: This needs to compensate for the point height of decenders. In its current form it is helpful but not really usable. The boxes are also not transparent atm.
300 #        If these things were fixed, it may be desirable to give the user control over whether or not to display these boxes for layout design.
301         if (0) {
302             my $box_height = 0;
303             my $box_lly = $origin_lly;
304             if (scalar(@lines) > 1) {
305                 $box_height += scalar(@lines) * ($text_attribs->{'font_size'} * 1.2);
306                 $box_lly -= ($text_attribs->{'font_size'} * 0.2);
307             }
308             else {
309                 $box_height += $text_attribs->{'font_size'};
310             }
311             box ($origin_llx, $box_lly, $self->{'width'} - ( $text_attribs->{'llx'} * $self->{'unitvalue'} ), $box_height, $pdf);
312         }
313         $pdf->Font($text_attribs->{'font'});
314         $pdf->FontSize($text_attribs->{'font_size'});
315         foreach my $line (@lines) {
316             $pdf->Text($line->{'Tx'}, $line->{'Ty'}, $line->{'line'});
317         }
318     }
319 }
320
321 =head2 draw_image
322
323     $patron_card->draw_image($pdf)
324
325 Draws images to PDF output ($pdf)
326
327 =cut
328
329 sub draw_image {
330     my ($self, $pdf) = @_;
331     warn sprintf('No pdf object passed in.') and return -1 if !$pdf;
332     my $images = $self->{'layout'}->{'images'};
333
334     PROCESS_IMAGES:
335     foreach my $image (keys %$images) {
336         next PROCESS_IMAGES if $images->{$image}->{'data_source'}->[0]->{'image_source'} eq 'none';
337         my $Tx = $self->{'llx'} + $images->{$image}->{'Tx'} * $self->{'unitvalue'};
338         my $Ty = $self->{'lly'} + $images->{$image}->{'Ty'} * $self->{'unitvalue'};
339         warn sprintf('No image passed in.') and next if !$images->{$image}->{'data'};
340         my $intName = $pdf->AltJpeg($images->{$image}->{'data'},$images->{$image}->{'Sx'}, $images->{$image}->{'Sy'}, 1, $images->{$image}->{'alt'}->{'data'},$images->{$image}->{'alt'}->{'Sx'}, $images->{$image}->{'alt'}->{'Sy'}, 1);
341         my $obj_stream = "q\n";
342         $obj_stream .= "$images->{$image}->{'Sx'} $images->{$image}->{'Ox'} $images->{$image}->{'Oy'} $images->{$image}->{'Sy'} $Tx $Ty cm\n";       # see http://www.adobe.com/devnet/pdf/pdf_reference.html sec 8.3.3 of ISO 32000-1
343         $obj_stream .= "$images->{$image}->{'scale'} 0 0 $images->{$image}->{'scale'} 0 0 cm\n"; #scale to 20%
344         $obj_stream .= "/$intName Do\n";
345         $obj_stream .= "Q\n";
346         $pdf->Add($obj_stream);
347     }
348 }
349
350 =head2 draw_barcode
351
352     $patron_card->draw_barcode($pdf)
353
354 Draws a barcode to PDF output ($pdf)
355
356 =cut
357
358 sub _draw_barcode {   # this is cut-and-paste from Label.pm because there is no common place for it atm...
359     my $self = shift;
360     my %params = @_;
361
362     my $x_scale_factor = 1;
363     my $num_of_chars = length($params{'barcode_data'});
364     my $tot_bar_length = 0;
365     my $bar_length = 0;
366     my $guard_length = 10;
367     if ($params{'barcode_type'} =~ m/CODE39/) {
368         $bar_length = '17.5';
369         $tot_bar_length = ($bar_length * $num_of_chars) + ($guard_length * 2);  # not sure what all is going on here and on the next line; this is old (very) code
370         $x_scale_factor = ($params{'width'} / $tot_bar_length);
371         if ($params{'barcode_type'} eq 'CODE39MOD') {
372             my $c39 = CheckDigits('code_39');   # get modulo 43 checksum
373             $params{'barcode_data'} = $c39->complete($params{'barcode_data'});
374         }
375         elsif ($params{'barcode_type'} eq 'CODE39MOD10') {
376             my $c39_10 = CheckDigits('siret');   # get modulo 10 checksum
377             $params{'barcode_data'} = $c39_10->complete($params{'barcode_data'});
378         }
379         eval {
380             PDF::Reuse::Barcode::Code39(
381                 x                   => $params{'llx'},
382                 y                   => $params{'lly'},
383                 value               => "*$params{barcode_data}*",
384                 xSize               => $x_scale_factor,
385                 ySize               => $params{'y_scale_factor'},
386                 hide_asterisk       => 1,
387                 text                => $params{'text'},
388                 mode                => 'graphic',
389             );
390         };
391         if ($@) {
392             warn sprintf('Barcode generation failed for item %s with this error: %s', $self->{'item_number'}, $@);
393         }
394     }
395     elsif ($params{'barcode_type'} eq 'COOP2OF5') {
396         $bar_length = '9.43333333333333';
397         $tot_bar_length = ($bar_length * $num_of_chars) + ($guard_length * 2);
398         $x_scale_factor = ($params{'width'} / $tot_bar_length) * 0.9;
399         eval {
400             PDF::Reuse::Barcode::COOP2of5(
401                 x                   => $params{'llx'},
402                 y                   => $params{'lly'},
403                 value               => $params{barcode_data},
404                 xSize               => $x_scale_factor,
405                 ySize               => $params{'y_scale_factor'},
406                 mode                    => 'graphic',
407             );
408         };
409         if ($@) {
410             warn sprintf('Barcode generation failed for item %s with this error: %s', $self->{'item_number'}, $@);
411         }
412     }
413     elsif ( $params{'barcode_type'} eq 'INDUSTRIAL2OF5' ) {
414         $bar_length = '13.1333333333333';
415         $tot_bar_length = ($bar_length * $num_of_chars) + ($guard_length * 2);
416         $x_scale_factor = ($params{'width'} / $tot_bar_length) * 0.9;
417         eval {
418             PDF::Reuse::Barcode::Industrial2of5(
419                 x                   => $params{'llx'},
420                 y                   => $params{'lly'},
421                 value               => $params{barcode_data},
422                 xSize               => $x_scale_factor,
423                 ySize               => $params{'y_scale_factor'},
424                 mode                    => 'graphic',
425             );
426         };
427         if ($@) {
428             warn sprintf('Barcode generation failed for item %s with this error: %s', $self->{'item_number'}, $@);
429         }
430     }
431 }
432
433 1;
434 __END__
435
436 =head1 AUTHOR
437
438 Chris Nighswonger <cnighswonger AT foundations DOT edu>
439
440 =cut