Bug 18541 - Patron card creator: Add a grid to support layout design
[srvgit] / C4 / Patroncards / Patroncard.pm
index d780607..f13d137 100644 (file)
@@ -24,17 +24,25 @@ use autouse 'Data::Dumper' => qw(Dumper);
 use Text::Wrap qw(wrap);
 #use Font::TTFMetrics;
 
-use C4::Creators::Lib qw(get_font_types);
+use C4::Creators::Lib qw(get_font_types get_unit_values);
 use C4::Creators::PDF qw(StrWidth);
 use C4::Patroncards::Lib qw(unpack_UTF8 text_alignment leading box get_borrower_attributes);
 
-BEGIN {
-    use version; our $VERSION = qv('3.07.00.049');
-}
 
 sub new {
     my ($invocant, %params) = @_;
     my $type = ref($invocant) || $invocant;
+
+    my $units = get_unit_values();
+    my $unitvalue = 1;
+    my $unitdesc = '';
+    foreach my $un (@$units){
+        if ($un->{'type'} eq $params{'layout'}->{'units'}) {
+            $unitvalue = $un->{'value'};
+            $unitdesc = $un->{'desc'};
+        }
+    }
+
     my $self = {
         batch_id                => $params{'batch_id'},
         #card_number             => $params{'card_number'},
@@ -44,7 +52,11 @@ sub new {
         height                  => $params{'height'},
         width                   => $params{'width'},
         layout                  => $params{'layout'},
+        unitvalue               => $unitvalue,
+        unitdesc                => $unitdesc,
         text_wrap_cols          => $params{'text_wrap_cols'},
+        barcode_height_scale    => $params{'layout'}->{'barcode'}[0]->{'height_scale'} || 0.01,
+        barcode_width_scale     => $params{'layout'}->{'barcode'}[0]->{'width_scale'} || 0.8,
     };
     bless ($self, $type);
     return $self;
@@ -52,14 +64,15 @@ sub new {
 
 sub draw_barcode {
     my ($self, $pdf) = @_;
-#FIXME: We do some scaling foo on the barcode here which probably should be done by the one invoking draw_barcode
-    my $barcode_width = 0.8 * $self->{'width'};                         # this scales the barcode width to 80% of the label width
-    my $barcode_y_scale_factor = 0.01 * $self->{'height'};              # this scales the barcode height to 1% of the label height
+    # Default values for barcode scaling are set in constructor to work with pre-existing installations
+    my $barcode_height_scale = $self->{'barcode_height_scale'};
+    my $barcode_width_scale = $self->{'barcode_width_scale'};
+
     _draw_barcode(      $self,
                         llx     => $self->{'llx'} + $self->{'layout'}->{'barcode'}->[0]->{'llx'},
                         lly     => $self->{'lly'} + $self->{'layout'}->{'barcode'}->[0]->{'lly'},
-                        width   => $barcode_width,
-                        y_scale_factor  => $barcode_y_scale_factor,
+                        width   => $self->{'width'} * $barcode_width_scale,
+                        y_scale_factor  => $self->{'height'} * $barcode_height_scale,
                         barcode_type    => $self->{'layout'}->{'barcode'}->[0]->{'type'},
                         barcode_data    => $self->{'layout'}->{'barcode'}->[0]->{'data'},
                         text    => $self->{'layout'}->{'barcode'}->[0]->{'text_print'},
@@ -69,6 +82,7 @@ sub draw_barcode {
 sub draw_guide_box {
     my ($self, $pdf) = @_;
     warn sprintf('No pdf object passed in.') and return -1 if !$pdf;
+
     my $obj_stream = "q\n";                            # save the graphic state
     $obj_stream .= "0.5 w\n";                          # border line width
     $obj_stream .= "1.0 0.0 0.0  RG\n";                # border color red
@@ -79,6 +93,79 @@ sub draw_guide_box {
     $pdf->Add($obj_stream);
 }
 
+sub draw_guide_grid {
+    my ($self, $pdf) = @_;
+    warn sprintf('No pdf object passed in.') and return -1 if !$pdf;
+
+    # Set up the grid in user defined units.
+    # Each 5th and 10th line get separate values
+
+    my $obj_stream = "q\n";   # save the graphic state
+    my $x = $self->{'llx'};
+    my $y = $self->{'lly'};
+
+    my $cnt = 0;
+    for ( $x = $self->{'llx'}/$self->{'unitvalue'}; $x <= ($self->{'llx'} + $self->{'width'})/$self->{'unitvalue'}; $x++) {
+        my $xx = $x*$self->{'unitvalue'};
+        my $yy = $y + $self->{'height'};
+        if ( ($cnt % 10) && ! ($cnt % 5) ) {
+            $obj_stream .= "0.0 1.0 0.0  RG\n";
+            $obj_stream .= "0 w\n";
+        } elsif ( $cnt % 5 ) {
+            $obj_stream .= "0.0 1.0 1.0  RG\n";
+            $obj_stream .= "0 w\n";
+        } else {
+            $obj_stream .= "0.0 0.0 1.0  RG\n";
+            $obj_stream .= "0 w\n";
+        }
+        $cnt ++;
+
+        $obj_stream .= "$xx $y m\n";
+        $obj_stream .= "$xx $yy l\n";
+
+        $obj_stream .= "s\n";
+    }
+
+    $x = $self->{'llx'};
+    $y = $self->{'lly'};
+    $cnt = 0;
+    for ( $y = $self->{'lly'}/$self->{'unitvalue'}; $y <= ($self->{'lly'} + $self->{'height'})/$self->{'unitvalue'}; $y++) {
+
+        my $xx = $x + $self->{'width'};
+        my $yy = $y*$self->{'unitvalue'};
+
+        if ( ($cnt % 10) && ! ($cnt % 5) ) {
+            $obj_stream .= "0.0 1.0 0.0  RG\n";
+            $obj_stream .= "0 w\n";
+        } elsif ( $cnt % 5 ) {
+            $obj_stream .= "0.0 1.0 1.0  RG\n";
+            $obj_stream .= "0 w\n";
+        } else {
+            $obj_stream .= "0.0 0.0 1.0  RG\n";
+            $obj_stream .= "0 w\n";
+        }
+        $cnt ++;
+
+        $obj_stream .= "$x $yy m\n";
+        $obj_stream .= "$xx $yy l\n";
+        $obj_stream .= "s\n";
+    }
+
+    $obj_stream .= "Q\n"; # restore the graphic state
+    $pdf->Add($obj_stream);
+
+    # Add info about units
+    my $strbottom = "0/0 $self->{'unitdesc'}";
+    my $strtop = sprintf('%.2f', $self->{'width'}/$self->{'unitvalue'}) .'/'. sprintf('%.2f', $self->{'height'}/$self->{'unitvalue'});
+    my $font_size = 6;
+    $pdf->Font( 'Courier' );
+    $pdf->FontSize( $font_size );
+    my $strtop_len = $pdf->StrWidth($strtop) * 1.5;
+    $pdf->Text( $self->{'llx'} + 2, $self->{'lly'} + 2, $strbottom );
+    $pdf->Text( $self->{'llx'} + $self->{'width'} - $strtop_len , $self->{'lly'} + $self->{'height'} - $font_size , $strtop );
+}
+
+
 sub draw_text {
     my ($self, $pdf, %params) = @_;
     warn sprintf('No pdf object passed in.') and return -1 if !$pdf;
@@ -89,15 +176,15 @@ sub draw_text {
         my $line = shift @$text;
         my $parse_line = $line;
         my @orig_line = split(/ /,$line);
-        if ($parse_line =~ m/<[A-Za-z0-9]+>/) {     # test to see if the line has db fields embedded...
+        if ($parse_line =~ m/<[A-Za-z0-9_]+>/) {     # test to see if the line has db fields embedded...
             my @fields = ();
-            while ($parse_line =~ m/<([A-Za-z0-9]+)>(.*$)/) {
+            while ($parse_line =~ m/<([A-Za-z0-9_]+)>(.*$)/) {
                 push (@fields, $1);
                 $parse_line = $2;
             }
             my $borrower_attributes = get_borrower_attributes($self->{'borrower_number'},@fields);
             grep{ # substitute data for db fields
-                if ($_ =~ m/<([A-Za-z0-9]+)>/) {
+                if ($_ =~ m/<([A-Za-z0-9_]+)>/) {
                     my $field = $1;
                     $_ =~ s/$_/$borrower_attributes->{$field}/;
                 }