1 package C4::Patroncards::Patroncard;
3 # Copyright 2009 Foundations Bible College.
5 # This file is part of Koha.
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.
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.
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>.
23 use autouse 'Data::Dumper' => qw(Dumper);
24 #use Font::TTFMetrics;
26 use C4::Creators::Lib qw( get_unit_values );
27 use C4::Creators::PDF qw(StrWidth);
28 use C4::Patroncards::Lib qw(
30 get_borrower_attributes
37 C4::Patroncards::Patroncard
41 use C4::Patroncards::Patroncard;
48 This module allows you to ...
57 my ($invocant, %params) = @_;
58 my $type = ref($invocant) || $invocant;
60 my $units = get_unit_values();
63 foreach my $un (@$units){
64 if ($un->{'type'} eq $params{'layout'}->{'units'}) {
65 $unitvalue = $un->{'value'};
66 $unitdesc = $un->{'desc'};
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,
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'};
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'},
110 =head2 draw_guide_box
115 my ($self, $pdf) = @_;
116 warn sprintf('No pdf object passed in.') and return -1 if !$pdf;
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);
128 =head2 draw_guide_grid
130 $patron_card->draw_guide_grid($pdf)
132 Adds a grid to the PDF output ($pdf) to support layout design
136 sub draw_guide_grid {
137 my ($self, $pdf) = @_;
138 warn sprintf('No pdf object passed in.') and return -1 if !$pdf;
140 # Set up the grid in user defined units.
141 # Each 5th and 10th line get separate values
143 my $obj_stream = "q\n"; # save the graphic state
144 my $x = $self->{'llx'};
145 my $y = $self->{'lly'};
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";
158 $obj_stream .= "0.0 0.0 1.0 RG\n";
159 $obj_stream .= "0 w\n";
163 $obj_stream .= "$xx $y m\n";
164 $obj_stream .= "$xx $yy l\n";
166 $obj_stream .= "s\n";
172 for ( $y = $self->{'lly'}/$self->{'unitvalue'}; $y <= ($self->{'lly'} + $self->{'height'})/$self->{'unitvalue'}; $y++) {
174 my $xx = $x + $self->{'width'};
175 my $yy = $y*$self->{'unitvalue'};
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";
184 $obj_stream .= "0.0 0.0 1.0 RG\n";
185 $obj_stream .= "0 w\n";
189 $obj_stream .= "$x $yy m\n";
190 $obj_stream .= "$xx $yy l\n";
191 $obj_stream .= "s\n";
194 $obj_stream .= "Q\n"; # restore the graphic state
195 $pdf->Add($obj_stream);
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'});
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 );
210 $patron_card->draw_text($pdf)
212 Draws text to PDF output ($pdf)
217 my ($self, $pdf, %params) = @_;
218 warn sprintf('No pdf object passed in.') and return -1 if !$pdf;
220 return unless (ref($self->{'layout'}->{'text'}) eq 'ARRAY'); # just in case there is not text
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...
229 while ($parse_line =~ m/<([A-Za-z0-9_]+)>(.*$)/) {
233 my $borrower_attributes = get_borrower_attributes($self->{'borrower_number'},@fields);
234 @orig_line = map { # substitute data for db fields
236 if ($l =~ m/<([A-Za-z0-9_]+)>/) {
238 $l =~ s/$l/$borrower_attributes->{$field}/;
242 $line = join(' ',@orig_line);
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
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
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'}) {
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&|\<.*\>)$/;
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};
277 last WRAP_LINES if $trim eq '';
278 $Ty -= leading($text_attribs->{'font_size'});
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};
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};
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.
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);
309 $box_height += $text_attribs->{'font_size'};
311 box ($origin_llx, $box_lly, $self->{'width'} - ( $text_attribs->{'llx'} * $self->{'unitvalue'} ), $box_height, $pdf);
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'});
323 $patron_card->draw_image($pdf)
325 Draws images to PDF output ($pdf)
330 my ($self, $pdf) = @_;
331 warn sprintf('No pdf object passed in.') and return -1 if !$pdf;
332 my $images = $self->{'layout'}->{'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);
352 $patron_card->draw_barcode($pdf)
354 Draws a barcode to PDF output ($pdf)
358 sub _draw_barcode { # this is cut-and-paste from Label.pm because there is no common place for it atm...
362 my $x_scale_factor = 1;
363 my $num_of_chars = length($params{'barcode_data'});
364 my $tot_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'});
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'});
380 PDF::Reuse::Barcode::Code39(
383 value => "*$params{barcode_data}*",
384 xSize => $x_scale_factor,
385 ySize => $params{'y_scale_factor'},
387 text => $params{'text'},
392 warn sprintf('Barcode generation failed for item %s with this error: %s', $self->{'item_number'}, $@);
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;
400 PDF::Reuse::Barcode::COOP2of5(
403 value => $params{barcode_data},
404 xSize => $x_scale_factor,
405 ySize => $params{'y_scale_factor'},
410 warn sprintf('Barcode generation failed for item %s with this error: %s', $self->{'item_number'}, $@);
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;
418 PDF::Reuse::Barcode::Industrial2of5(
421 value => $params{barcode_data},
422 xSize => $x_scale_factor,
423 ySize => $params{'y_scale_factor'},
428 warn sprintf('Barcode generation failed for item %s with this error: %s', $self->{'item_number'}, $@);
438 Chris Nighswonger <cnighswonger AT foundations DOT edu>