Bug 15072: (followup) fix spaces and consistency
[koha.git] / C4 / Patroncards / Patroncard.pm
blobf6a18127c631ed50bf20225814f3a73d13f66b29
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>.
20 use strict;
21 use warnings;
23 use autouse 'Data::Dumper' => qw(Dumper);
24 use Text::Wrap qw(wrap);
25 #use Font::TTFMetrics;
27 use C4::Creators::Lib qw(get_font_types);
28 use C4::Creators::PDF qw(StrWidth);
29 use C4::Patroncards::Lib qw(unpack_UTF8 text_alignment leading box get_borrower_attributes);
31 BEGIN {
32 use version; our $VERSION = qv('3.07.00.049');
35 sub new {
36 my ($invocant, %params) = @_;
37 my $type = ref($invocant) || $invocant;
38 my $self = {
39 batch_id => $params{'batch_id'},
40 #card_number => $params{'card_number'},
41 borrower_number => $params{'borrower_number'},
42 llx => $params{'llx'},
43 lly => $params{'lly'},
44 height => $params{'height'},
45 width => $params{'width'},
46 layout => $params{'layout'},
47 text_wrap_cols => $params{'text_wrap_cols'},
48 barcode_height_scale => $params{'layout'}->{'barcode'}[0]->{'height_scale'} || 0.01,
49 barcode_width_scale => $params{'layout'}->{'barcode'}[0]->{'width_scale'} || 0.8,
51 bless ($self, $type);
52 return $self;
55 sub draw_barcode {
56 my ($self, $pdf) = @_;
57 # Default values for barcode scaling are set in constructor to work with pre-existing installations
58 my $barcode_height_scale = $self->{'barcode_height_scale'};
59 my $barcode_width_scale = $self->{'barcode_width_scale'};
61 _draw_barcode( $self,
62 llx => $self->{'llx'} + $self->{'layout'}->{'barcode'}->[0]->{'llx'},
63 lly => $self->{'lly'} + $self->{'layout'}->{'barcode'}->[0]->{'lly'},
64 width => $self->{'width'} * $barcode_width_scale,
65 y_scale_factor => $self->{'height'} * $barcode_height_scale,
66 barcode_type => $self->{'layout'}->{'barcode'}->[0]->{'type'},
67 barcode_data => $self->{'layout'}->{'barcode'}->[0]->{'data'},
68 text => $self->{'layout'}->{'barcode'}->[0]->{'text_print'},
72 sub draw_guide_box {
73 my ($self, $pdf) = @_;
74 warn sprintf('No pdf object passed in.') and return -1 if !$pdf;
75 my $obj_stream = "q\n"; # save the graphic state
76 $obj_stream .= "0.5 w\n"; # border line width
77 $obj_stream .= "1.0 0.0 0.0 RG\n"; # border color red
78 $obj_stream .= "1.0 1.0 1.0 rg\n"; # fill color white
79 $obj_stream .= "$self->{'llx'} $self->{'lly'} $self->{'width'} $self->{'height'} re\n"; # a rectangle
80 $obj_stream .= "B\n"; # fill (and a little more)
81 $obj_stream .= "Q\n"; # restore the graphic state
82 $pdf->Add($obj_stream);
85 sub draw_text {
86 my ($self, $pdf, %params) = @_;
87 warn sprintf('No pdf object passed in.') and return -1 if !$pdf;
88 my @card_text = ();
89 return unless (ref($self->{'layout'}->{'text'}) eq 'ARRAY'); # just in case there is not text
90 my $text = [@{$self->{'layout'}->{'text'}}]; # make a copy of the arrayref *not* simply a pointer
91 while (scalar @$text) {
92 my $line = shift @$text;
93 my $parse_line = $line;
94 my @orig_line = split(/ /,$line);
95 if ($parse_line =~ m/<[A-Za-z0-9]+>/) { # test to see if the line has db fields embedded...
96 my @fields = ();
97 while ($parse_line =~ m/<([A-Za-z0-9]+)>(.*$)/) {
98 push (@fields, $1);
99 $parse_line = $2;
101 my $borrower_attributes = get_borrower_attributes($self->{'borrower_number'},@fields);
102 grep{ # substitute data for db fields
103 if ($_ =~ m/<([A-Za-z0-9]+)>/) {
104 my $field = $1;
105 $_ =~ s/$_/$borrower_attributes->{$field}/;
107 } @orig_line;
108 $line = join(' ',@orig_line);
110 my $text_attribs = shift @$text;
111 my $origin_llx = $self->{'llx'} + $text_attribs->{'llx'};
112 my $origin_lly = $self->{'lly'} + $text_attribs->{'lly'};
113 my $Tx = 0; # final text llx
114 my $Ty = $origin_lly; # final text lly
115 my $Tw = 0; # final text word spacing. See http://www.adobe.com/devnet/pdf/pdf_reference.html ISO 32000-1
116 #FIXME: Move line wrapping code to its own sub if possible
117 my $trim = '';
118 my @lines = ();
119 #FIXME: Using embedded True Type fonts is a far superior way of handing things as well as being much more unicode friendly.
120 # However this will take significant work using better than PDF::Reuse to do it. For the time being, I'm leaving
121 # the basic code here commented out to preserve the basic method of accomplishing this. -chris_n
123 # my $m = Font::TTFMetrics->new("/usr/share/fonts/truetype/msttcorefonts/Times_New_Roman_Bold.ttf");
124 # my $units_per_em = $m->get_units_per_em();
125 # my $font_units_width = $m->string_width($line);
126 # my $string_width = ($font_units_width * $text_attribs->{'font_size'}) / $units_per_em;
127 my $string_width = C4::Creators::PDF->StrWidth($line, $text_attribs->{'font'}, $text_attribs->{'font_size'});
128 if (($string_width + $text_attribs->{'llx'}) > $self->{'width'}) {
129 WRAP_LINES:
130 while (1) {
131 # $line =~ m/^.*(\s\b.*\b\s*|\s&|\<\b.*\b\>)$/; # original regexp... can be removed after dev stage is over
132 $line =~ m/^.*(\s.*\s*|\s&|\<.*\>)$/;
133 warn sprintf('Line wrap failed. DEBUG INFO: Data: \'%s\'\n Method: C4::Patroncards->draw_text Additional Information: Line wrap regexp failed. (Please file in this information in a bug report at http://bugs.koha-community.org', $line) and last WRAP_LINES if !$1;
134 $trim = $1 . $trim;
135 $line =~ s/$1//;
136 $string_width = C4::Creators::PDF->StrWidth($line, $text_attribs->{'font'}, $text_attribs->{'font_size'});
137 # $font_units_width = $m->string_width($line);
138 # $string_width = ($font_units_width * $text_attribs->{'font_size'}) / $units_per_em;
139 if (($string_width + $text_attribs->{'llx'}) < $self->{'width'}) {
140 ($Tx, $Tw) = text_alignment($origin_llx, $self->{'width'}, $text_attribs->{'llx'}, $string_width, $line, $text_attribs->{'text_alignment'});
141 push @lines, {line=> $line, Tx => $Tx, Ty => $Ty, Tw => $Tw};
142 $line = undef;
143 last WRAP_LINES if $trim eq '';
144 $Ty -= leading($text_attribs->{'font_size'});
145 $line = $trim;
146 $trim = '';
147 $string_width = C4::Creators::PDF->StrWidth($line, $text_attribs->{'font'}, $text_attribs->{'font_size'});
148 #$font_units_width = $m->string_width($line);
149 #$string_width = ($font_units_width * $text_attribs->{'font_size'}) / $units_per_em;
150 if (($string_width + $text_attribs->{'llx'}) < $self->{'width'}) {
151 ($Tx, $Tw) = text_alignment($origin_llx, $self->{'width'}, $text_attribs->{'llx'}, $string_width, $line, $text_attribs->{'text_alignment'});
152 $line =~ s/^\s+//g; # strip naughty leading spaces
153 push @lines, {line=> $line, Tx => $Tx, Ty => $Ty, Tw => $Tw};
154 last WRAP_LINES;
159 else {
160 ($Tx, $Tw) = text_alignment($origin_llx, $self->{'width'}, $text_attribs->{'llx'}, $string_width, $line, $text_attribs->{'text_alignment'});
161 $line =~ s/^\s+//g; # strip naughty leading spaces
162 push @lines, {line=> $line, Tx => $Tx, Ty => $Ty, Tw => $Tw};
164 # Draw boxes around text box areas
165 # 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.
166 # 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.
167 if (0) {
168 my $box_height = 0;
169 my $box_lly = $origin_lly;
170 if (scalar(@lines) > 1) {
171 $box_height += scalar(@lines) * ($text_attribs->{'font_size'} * 1.2);
172 $box_lly -= ($text_attribs->{'font_size'} * 0.2);
174 else {
175 $box_height += $text_attribs->{'font_size'};
177 box ($origin_llx, $box_lly, $self->{'width'} - $text_attribs->{'llx'}, $box_height, $pdf);
179 $pdf->Font($text_attribs->{'font'});
180 $pdf->FontSize($text_attribs->{'font_size'});
181 foreach my $line (@lines) {
182 $pdf->Text($line->{'Tx'}, $line->{'Ty'}, $line->{'line'});
187 sub draw_image {
188 my ($self, $pdf) = @_;
189 warn sprintf('No pdf object passed in.') and return -1 if !$pdf;
190 my $images = $self->{'layout'}->{'images'};
191 PROCESS_IMAGES:
192 foreach my $image (keys %$images) {
193 next PROCESS_IMAGES if $images->{$image}->{'data_source'}->[0]->{'image_source'} eq 'none';
194 my $Tx = $self->{'llx'} + $images->{$image}->{'Tx'};
195 my $Ty = $self->{'lly'} + $images->{$image}->{'Ty'};
196 warn sprintf('No image passed in.') and next if !$images->{$image}->{'data'};
197 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);
198 my $obj_stream = "q\n";
199 $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
200 $obj_stream .= "$images->{$image}->{'scale'} 0 0 $images->{$image}->{'scale'} 0 0 cm\n"; #scale to 20%
201 $obj_stream .= "/$intName Do\n";
202 $obj_stream .= "Q\n";
203 $pdf->Add($obj_stream);
207 sub _draw_barcode { # this is cut-and-paste from Label.pm because there is no common place for it atm...
208 my $self = shift;
209 my %params = @_;
210 my $x_scale_factor = 1;
211 my $num_of_chars = length($params{'barcode_data'});
212 my $tot_bar_length = 0;
213 my $bar_length = 0;
214 my $guard_length = 10;
215 if ($params{'barcode_type'} =~ m/CODE39/) {
216 $bar_length = '17.5';
217 $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
218 $x_scale_factor = ($params{'width'} / $tot_bar_length);
219 if ($params{'barcode_type'} eq 'CODE39MOD') {
220 my $c39 = CheckDigits('code_39'); # get modulo 43 checksum
221 $params{'barcode_data'} = $c39->complete($params{'barcode_data'});
223 elsif ($params{'barcode_type'} eq 'CODE39MOD10') {
224 my $c39_10 = CheckDigits('siret'); # get modulo 10 checksum
225 $params{'barcode_data'} = $c39_10->complete($params{'barcode_data'});
227 eval {
228 PDF::Reuse::Barcode::Code39(
229 x => $params{'llx'},
230 y => $params{'lly'},
231 value => "*$params{barcode_data}*",
232 xSize => $x_scale_factor,
233 ySize => $params{'y_scale_factor'},
234 hide_asterisk => 1,
235 text => $params{'text'},
236 mode => 'graphic',
239 if ($@) {
240 warn sprintf('Barcode generation failed for item %s with this error: %s', $self->{'item_number'}, $@);
243 elsif ($params{'barcode_type'} eq 'COOP2OF5') {
244 $bar_length = '9.43333333333333';
245 $tot_bar_length = ($bar_length * $num_of_chars) + ($guard_length * 2);
246 $x_scale_factor = ($params{'width'} / $tot_bar_length) * 0.9;
247 eval {
248 PDF::Reuse::Barcode::COOP2of5(
249 x => $params{'llx'},
250 y => $params{'lly'},
251 value => "*$params{barcode_data}*",
252 xSize => $x_scale_factor,
253 ySize => $params{'y_scale_factor'},
254 mode => 'graphic',
257 if ($@) {
258 warn sprintf('Barcode generation failed for item %s with this error: %s', $self->{'item_number'}, $@);
261 elsif ( $params{'barcode_type'} eq 'INDUSTRIAL2OF5' ) {
262 $bar_length = '13.1333333333333';
263 $tot_bar_length = ($bar_length * $num_of_chars) + ($guard_length * 2);
264 $x_scale_factor = ($params{'width'} / $tot_bar_length) * 0.9;
265 eval {
266 PDF::Reuse::Barcode::Industrial2of5(
267 x => $params{'llx'},
268 y => $params{'lly'},
269 value => "*$params{barcode_data}*",
270 xSize => $x_scale_factor,
271 ySize => $params{'y_scale_factor'},
272 mode => 'graphic',
275 if ($@) {
276 warn sprintf('Barcode generation failed for item %s with this error: %s', $self->{'item_number'}, $@);
282 __END__
284 =head1 AUTHOR
286 Chris Nighswonger <cnighswonger AT foundations DOT edu>
288 =cut