[20/40] Work on label printing code.
authorChris Nighswonger <cnighswonger@foundations.edu>
Wed, 5 Aug 2009 19:53:40 +0000 (15:53 -0400)
committerChris Nighswonger <cnighswonger@foundations.edu>
Tue, 1 Sep 2009 19:51:16 +0000 (15:51 -0400)
C4/Labels/Batch.pm
C4/Labels/Label.pm
C4/Labels/Layout.pm
C4/Labels/Profile.pm
C4/Labels/Template.pm
labels/label-print-pdf.pl

index 56c970c..d7d483a 100644 (file)
@@ -216,7 +216,7 @@ sub retrieve {
     while (my $record = $sth->fetchrow_hashref) {
         $self->{'branch_code'} = $record->{'branch_code'};
         push (@{$self->{'items'}}, {item_number => $record->{'item_number'}, label_id => $record->{'label_id'}});
-        $record_flag = 1;
+        $record_flag = 1;       # true if one or more rows were retrieved
     }
     return -2 if $record_flag == 0;     # a hackish sort of way of indicating no such record exists
     if ($sth->err) {
index ff8dfbb..97a0a32 100644 (file)
@@ -66,7 +66,7 @@ sub _get_label_item {
     my $data = $sth->fetchrow_hashref;
     # Replaced item's itemtype with the more user-friendly description...
     my $sth1 = $dbh->prepare("SELECT itemtype,description FROM itemtypes WHERE itemtype = ?");
-    $sth1->execute($data->{itemtype});
+    $sth1->execute($data->{'itemtype'});
     if ($sth1->err) {
         syslog("LOG_ERR", "C4::Labels::Label::_get_label_item : Database returned the following error: %s", $sth1->errstr);
     }
@@ -178,9 +178,9 @@ sub _get_barcode_data {
     my $match_kohatable = join(
         '|',
         (
-            @{ $kohatables->{biblio} },
-            @{ $kohatables->{biblioitems} },
-            @{ $kohatables->{items} }
+            @{ $kohatables->{'biblio'} },
+            @{ $kohatables->{'biblioitems'} },
+            @{ $kohatables->{'items'} }
         )
     );
     FIELD_LIST:
@@ -258,24 +258,25 @@ sub _desc_koha_tables {
 sub new {
     my ($invocant, %params) = @_;
     my $type = ref($invocant) || $invocant;
-    my $layout = C4::Labels::Layout->retrieve(layout_id => $params{'layout_id'});
     my $self = {
-        batch_id        => $params{'batch_id'},
-        layout_id       => $params{'layout_id'},
-        item_number     => $params{'item_number'},
-        height          => $params{'height'},
-        width           => $params{'width'},
-        top_text_margin => $params{'top_text_margin'},
-        left_text_margin => $params{'left_text_margin'},
-        font            => $params{'font'},
-        font_size       => $params{'font_size'},
-        justify         => $params{'justify'},
-        text_wrap_cols  => $params{'text_wrap_cols'},
-        layout          => $layout,
-        guidebox        => '',
-        barcode         => 0,
+        batch_id                => $params{'batch_id'},
+        item_number             => $params{'item_number'},
+        height                  => $params{'height'},
+        width                   => $params{'width'},
+        top_text_margin         => $params{'top_text_margin'},
+        left_text_margin        => $params{'left_text_margin'},
+        barcode_type            => $params{'barcode_type'},
+        printing_type           => $params{'printing_type'},
+        guidebox                => $params{'guidebox'},
+        font                    => $params{'font'},
+        font_size               => $params{'font_size'},
+        callnum_split           => $params{'callnum_split'},
+        justify                 => $params{'justify'},
+        format_string           => $params{'format_string'},
+        text_wrap_cols          => $params{'text_wrap_cols'},
+        barcode                 => 0,
     };
-    if ($layout->get_attr('guidebox')) {
+    if ($self->{'guidebox'}) {
         $self->{'guidebox'} = _guide_box($self->{'llx'}, $self->{'lly'}, $self->{'width'}, $self->{'height'});
     }
     bless ($self, $type);
@@ -284,7 +285,31 @@ sub new {
 
 sub get_label_type {
     my $self = shift;
-    return $self->{'layout'}->get_attr('printing_type');
+    return $self->{'printing_type'};
+}
+
+=head2 $label->get_attr("attr")
+
+    Invoking the I<get_attr> method will return the value of the requested attribute or 1 on errors.
+
+    example:
+        my $value = $label->get_attr("attr");
+
+=cut
+
+sub get_attr {
+    my $self = shift;
+#    if (_check_params(@_) eq 1) {
+#        return -1;
+#    }
+    my ($attr) = @_;
+    if (exists($self->{$attr})) {
+        return $self->{$attr};
+    }
+    else {
+        return -1;
+    }
+    return;
 }
 
 =head2 $label->draw_label_text()
@@ -308,9 +333,9 @@ sub draw_label_text {
     my $text_llx = 0;
     my $text_lly = $params{'lly'};
     my $font = $self->{'font'};
-    my $item = _get_label_item($self->{item_number});
-    my $label_fields = _get_text_fields($self->{layout}->get_attr('format_string'));
-    my $record = GetMarcBiblio($item->{biblionumber});
+    my $item = _get_label_item($self->{'item_number'});
+    my $label_fields = _get_text_fields($self->{'format_string'});
+    my $record = GetMarcBiblio($item->{'biblionumber'});
     # FIXME - returns all items, so you can't get data from an embedded holdings field.
     # TODO - add a GetMarcBiblio1item(bibnum,itemnum) or a GetMarcItem(itemnum).
     my $cn_source = ($item->{'cn_source'} ? $item->{'cn_source'} : C4::Context->preference('DefaultClassificationSource'));
@@ -328,7 +353,7 @@ sub draw_label_text {
         $field_data =~ s/\r//g;
         my @label_lines;
         my @callnumber_list = ('itemcallnumber', '050a', '050b', '082a', '952o'); # Fields which hold call number data  FIXME: ( 060? 090? 092? 099? )
-        if ((grep {$field->{'code'} =~ m/$_/} @callnumber_list) and ($self->{layout}->get_attr('printing_type') eq 'BIB') and ($self->{layout}->get_attr('callnum_split'))) { # If the field contains the call number, we do some sp
+        if ((grep {$field->{'code'} =~ m/$_/} @callnumber_list) and ($self->{'printing_type'} eq 'BIB') and ($self->{'callnum_split'})) { # If the field contains the call number, we do some sp
             if ($cn_source eq 'lcc') {
                 @label_lines = _split_lccn($field_data);
                 @label_lines = _split_fcn($field_data) if !@label_lines;    # If it was not a true lccn, try it as a fiction call number
@@ -366,11 +391,13 @@ sub draw_label_text {
             my $string_width = C4::Labels::PDF->StrWidth($line, $font, $self->{'font_size'});
             if ($self->{'justify'} eq 'R') { 
                 $text_llx = $params{'llx'} + $self->{'width'} - ($self->{'left_text_margin'} + $string_width);
-            } elsif($self->{'justify'} eq 'C') {
+            } 
+            elsif($self->{'justify'} eq 'C') {
                  # some code to try and center each line on the label based on font size and string point width...
                  my $whitespace = ($self->{'width'} - ($string_width + (2 * $self->{'left_text_margin'})));
                  $text_llx = (($whitespace  / 2) + $params{'llx'} + $self->{'left_text_margin'});
-            } else {
+            } 
+            else {
                 $text_llx = ($params{'llx'} + $self->{'left_text_margin'});
             }
             push @label_text,   {
@@ -408,7 +435,7 @@ sub barcode {
     my $self = shift;
     my %params = @_;
     $params{'barcode'} = _get_label_item($self->{'item_number'}, 1) if !$params{'barcode'};
-    $params{'barcode_type'} = $self->{'layout'}->get_attr('barcode_type') if !$params{'barcode_type'};
+    $params{'barcode_type'} = $self->{'barcode_type'} if !$params{'barcode_type'};
     my $x_scale_factor = 1;
     my $num_of_bars = length($params{'barcode'});
     my $tot_bar_length = 0;
index f0776cc..80ae808 100644 (file)
@@ -285,6 +285,33 @@ sub set_attr {
     };
     return 0;
 }
+
+=head2 $layout->get_text_wrap_cols()
+
+    Invoking the I<get_text_wrap_cols> method will return the number of columns that can be printed on the
+    label before wrapping to the next line.
+
+    examples:
+        my $text_wrap_cols = $layout->get_text_wrap_cols();
+
+=cut
+
+sub get_text_wrap_cols {
+    my $self = shift;
+    my %params = @_;
+    my $string = '';
+    my $strwidth = 0;
+    my $col_count = 0;
+    my $textlimit = $params{'label_width'} - ( 3 * $params{'left_text_margin'});
+
+    while ($strwidth < $textlimit) {
+        $string .= '0';
+        $col_count++;
+        $strwidth = C4::Labels::PDF->StrWidth( $string, $self->{'font'}, $self->{'font_size'} );
+    }
+    return $col_count;
+}
+
 1;
 __END__
 
index faa9008..1353fdd 100644 (file)
@@ -63,7 +63,7 @@ sub _check_params {
 
 sub _conv_points {
     my $self = shift;
-    my @unit_value = grep {$_->{'type'} eq $self->{units}} get_unit_values();
+    my @unit_value = grep {$_->{'type'} eq $self->{units}} @{get_unit_values()};
     $self->{offset_horz}        = $self->{offset_horz} * $unit_value[0]->{'value'};
     $self->{offset_vert}        = $self->{offset_vert} * $unit_value[0]->{'value'};
     $self->{creep_horz}         = $self->{creep_horz} * $unit_value[0]->{'value'};
index f972ce6..5410d99 100644 (file)
@@ -74,17 +74,17 @@ sub _check_params {
 
 sub _conv_points {
     my $self = shift;
-    my @unit_value = grep {$_->{'type'} eq $self->{units}} get_unit_values();
-    $self->{page_width}         = $self->{page_width} * $unit_value[0]->{'value'};
-    $self->{page_height}        = $self->{page_height} * $unit_value[0]->{'value'};
-    $self->{label_width}        = $self->{label_width} * $unit_value[0]->{'value'};
-    $self->{label_height}       = $self->{label_height} * $unit_value[0]->{'value'};
-    $self->{top_text_margin}    = $self->{top_text_margin} * $unit_value[0]->{'value'};
-    $self->{left_text_margin}   = $self->{left_text_margin} * $unit_value[0]->{'value'};
-    $self->{top_margin}         = $self->{top_margin} * $unit_value[0]->{'value'};
-    $self->{left_margin}        = $self->{left_margin} * $unit_value[0]->{'value'};
-    $self->{col_gap}            = $self->{col_gap} * $unit_value[0]->{'value'};
-    $self->{row_gap}            = $self->{row_gap} * $unit_value[0]->{'value'};
+    my @unit_value = grep {$_->{'type'} eq $self->{'units'}} @{get_unit_values()};
+    $self->{'page_width'}         = $self->{'page_width'} * $unit_value[0]->{'value'};
+    $self->{'page_height'}        = $self->{'page_height'} * $unit_value[0]->{'value'};
+    $self->{'label_width'}        = $self->{'label_width'} * $unit_value[0]->{'value'};
+    $self->{'label_height'}       = $self->{'label_height'} * $unit_value[0]->{'value'};
+    $self->{'top_text_margin'}    = $self->{'top_text_margin'} * $unit_value[0]->{'value'};
+    $self->{'left_text_margin'}   = $self->{'left_text_margin'} * $unit_value[0]->{'value'};
+    $self->{'top_margin'}         = $self->{'top_margin'} * $unit_value[0]->{'value'};
+    $self->{'left_margin'}        = $self->{'left_margin'} * $unit_value[0]->{'value'};
+    $self->{'col_gap'}            = $self->{'col_gap'} * $unit_value[0]->{'value'};
+    $self->{'row_gap'}            = $self->{'row_gap'} * $unit_value[0]->{'value'};
     return $self;
 }
 
@@ -92,10 +92,10 @@ sub _apply_profile {
     my $self = shift;
     my $profile_id = shift;
     my $profile = C4::Labels::Profile->retrieve(profile_id => $profile_id, convert => 1);
-    $self->{top_margin} = $self->{top_margin} + $profile->get_attr('offset_vert');      # controls vertical offset
-    $self->{left_margin} = $self->{left_margin} + $profile->get_attr('offset_horz');    # controls horizontal offset
-    $self->{label_height} = $self->{label_height} + $profile->get_attr('creep_vert');   # controls vertical creep
-    $self->{label_width} = $self->{label_width} + $profile->get_attr('creep_horz');     # controls horizontal creep
+    $self->{'top_margin'} = $self->{'top_margin'} + $profile->get_attr('offset_vert');      # controls vertical offset
+    $self->{'left_margin'} = $self->{'left_margin'} + $profile->get_attr('offset_horz');    # controls horizontal offset
+    $self->{'label_height'} = $self->{'label_height'} + $profile->get_attr('creep_vert');   # controls vertical creep
+    $self->{'label_width'} = $self->{'label_width'} + $profile->get_attr('creep_horz');     # controls horizontal creep
     return $self;
 }
 
@@ -180,7 +180,7 @@ sub retrieve {
     my $self = $sth->fetchrow_hashref;
     $self = _conv_points($self) if (($opts{convert} && $opts{convert} == 1) || $opts{profile_id});
     $self = _apply_profile($self, $opts{profile_id}) if $opts{profile_id};
-    $self->{tmpl_stat} = 1;
+    $self->{'tmpl_stat'} = 1;
     bless ($self, $type);
     return $self;
 }
@@ -218,7 +218,7 @@ sub delete {
     my $query = "DELETE FROM labels_templates WHERE template_id = ?";  
     my $sth = C4::Context->dbh->prepare($query);
     $sth->execute($query_param);
-    $self->{tmpl_stat} = 0;
+    $self->{'tmpl_stat'} = 0;
     return 0;
 }
 
@@ -246,14 +246,13 @@ sub save {
         $query = substr($query, 0, (length($query)-2));
         push (@params, $self->{'template_id'});
         $query .= " WHERE template_id=?;";
-        warn "DEBUG: Updating: $query\n" if $debug;
         my $sth = C4::Context->dbh->prepare($query);
         $sth->execute(@params);
         if ($sth->err) {
             syslog("LOG_ERR", "Database returned the following error: %s", $sth->errstr);
             return -1;
         }
-        $self->{tmpl_stat} = 1;
+        $self->{'tmpl_stat'} = 1;
         return $self->{'template_id'};
     }
     else {                      # otherwise create a new record
@@ -271,7 +270,6 @@ sub save {
         }
         $query = substr($query, 0, (length($query)-1));
         $query .= ");";
-        warn "DEBUG: Saving: $query\n" if $debug;
         my $sth = C4::Context->dbh->prepare($query);
         $sth->execute(@params);
         if ($sth->err) {
@@ -281,8 +279,8 @@ sub save {
         my $sth1 = C4::Context->dbh->prepare("SELECT MAX(template_id) FROM labels_templates;");
         $sth1->execute();
         my $template_id = $sth1->fetchrow_array;
-        $self->{template_id} = $template_id;
-        $self->{tmpl_stat} = 1;
+        $self->{'template_id'} = $template_id;
+        $self->{'tmpl_stat'} = 1;
         return $template_id;
     }
 }
@@ -330,31 +328,6 @@ sub set_attr {
     };
 }
 
-=head2 $template->get_text_wrap_cols()
-
-    Invoking the I<get_text_wrap_cols> method will return the number of columns that can be printed on the
-    label before wrapping to the next line.
-
-    examples:
-        my $text_wrap_cols = $template->get_text_wrap_cols();
-
-=cut
-
-sub get_text_wrap_cols {
-    my $self = shift;
-    my $string = '';
-    my $strwidth = 0;
-    my $col_count = 0;
-    my $textlimit = $self->{label_width} - ( 3 * $self->{left_text_margin});
-
-    while ($strwidth < $textlimit) {
-        $string .= '0';
-        $col_count++;
-        $strwidth = C4::Labels::PDF->StrWidth( $string, $self->{font}, $self->{font_size} );
-    }
-    return $col_count;
-}
-
 =head2 $template->get_label_position($start_label)
 
     Invoking the I<get_label_position> method will return the row, column coordinates on the starting page
@@ -371,15 +344,15 @@ sub get_label_position {
     if ($start_label eq 1) {
         $row_count = 1;
         $col_count = 1;
-        $llx = $self->{left_margin};
-        $lly = ($self->{page_height} - $self->{top_margin} - $self->{label_height});
+        $llx = $self->{'left_margin'};
+        $lly = ($self->{'page_height'} - $self->{'top_margin'} - $self->{'label_height'});
         return ($row_count, $col_count, $llx, $lly);
     }
     else {
-        $row_count = ceil($start_label / $self->{cols});
-        $col_count = ($start_label - (($row_count - 1) * $self->{cols}));
-        $llx = $self->{left_margin} + ($self->{label_width} * ($col_count - 1)) + ($self->{col_gap} * ($col_count - 1));
-        $lly = $self->{page_height} - $self->{top_margin} - ($self->{label_height} * $row_count) - ($self->{row_gap} * ($row_count - 1));
+        $row_count = ceil($start_label / $self->{'cols'});
+        $col_count = ($start_label - (($row_count - 1) * $self->{'cols'}));
+        $llx = $self->{'left_margin'} + ($self->{'label_width'} * ($col_count - 1)) + ($self->{'col_gap'} * ($col_count - 1));
+        $lly = $self->{'page_height'} - $self->{'top_margin'} - ($self->{'label_height'} * $row_count) - ($self->{'row_gap'} * ($row_count - 1));
         return ($row_count, $col_count, $llx, $lly);
     }
 }
index 3534af2..1c6bbba 100755 (executable)
 #!/usr/bin/perl
 
+# Copyright 2006 Katipo Communications.
+# Some parts Copyright 2009 Foundations Bible College.
+#
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 2 of the License, or (at your option) any later
+# version.
+#
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along with
+# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
+# Suite 330, Boston, MA  02111-1307 USA
+
 use strict;
 use warnings;
 
 use CGI;
-use C4::Labels;     # GetActiveLabelTemplate get_label_options GetAssociatedProfile 
-# GetPatronCardItems GetLabelItems GetUnitsValue...
+use HTML::Template::Pro;
+use POSIX qw(ceil);
+use Data::Dumper;
+use Sys::Syslog qw(syslog);
+
+use C4::Labels;
 use C4::Auth;
 use C4::Output;
 use C4::Context;
 use C4::Members;
 use C4::Branch;
-use HTML::Template::Pro;
-use PDF::Reuse;
-use PDF::Reuse::Barcode;
-use POSIX;  # ceil
-use Data::Dumper;
-
-my $DEBUG = 0;
-my $DEBUG_LPT = 0;
-
-my $cgi         = new CGI;
-
-#### Tons of Initialization ####
-# get the printing settings
-my $template    = GetActiveLabelTemplate();
-my $conf_data   = get_label_options() or die "get_label_options failed";
-my $profile     = GetAssociatedProfile($template->{'tmpl_id'});
-
-my $batch_id =   $cgi->param('batch_id');
-my @resultsloop;
-
-my $batch_type   = $conf_data->{'type'};
-my $barcodetype  = $conf_data->{'barcodetype'};
-my $printingtype = $conf_data->{'printingtype'} or die "No printingtype in conf_data";
-my $guidebox     = $conf_data->{'guidebox'};
-my $start_label  = $conf_data->{'startlabel'};
-if ($cgi->param('startlabel')) {
-    $start_label = $cgi->param('startlabel');       # A bit of a hack to allow setting the starting label from the address bar... -fbcit
+use C4::Debug;
+use C4::Labels::Batch 1.000000;
+use C4::Labels::Template 1.000000;
+use C4::Labels::Layout 1.000000;
+use C4::Labels::PDF 1.000000;
+use C4::Labels::Label 1.000000;
+
+my $cgi = new CGI;
+
+my $htdocs_path = C4::Context->config('intrahtdocs');
+my $batch_id    = $cgi->param('batch_id') || $ARGV[0];
+my $template_id = $cgi->param('template_id') || $ARGV[1];
+my $layout_id   = $cgi->param('layout_id') || $ARGV[2];
+my $start_label = $cgi->param('start_label') || $ARGV[3];
+
+print $cgi->header( -type => 'application/pdf', -attachment => "koha_batch_$batch_id.pdf" );
+
+my $pdf = C4::Labels::PDF->new(InitVars => 0);
+my $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
+my $template = C4::Labels::Template->retrieve(template_id => $template_id, profile_id => 1);
+my $layout = C4::Labels::Layout->retrieve(layout_id => $layout_id);
+
+sub _calc_next_label_pos {
+    my ($row_count, $col_count, $llx, $lly) = @_;
+    if ($col_count lt $template->get_attr('cols')) {
+        $llx = ($llx + $template->get_attr('label_width') + $template->get_attr('col_gap'));
+        $col_count++;
+    }
+    else {
+        $llx = $template->get_attr('left_margin');
+        if ($row_count eq $template->get_attr('rows')) {
+            $pdf->Page();
+            $lly = ($template->get_attr('page_height') - $template->get_attr('top_margin') - $template->get_attr('label_height'));
+            $row_count = 1;
+        }
+        else {
+            $lly = ($lly - $template->get_attr('row_gap') - $template->get_attr('label_height'));
+            $row_count++;
+        }
+        $col_count = 1;
+    }
+    return ($row_count, $col_count, $llx, $lly);
 }
-warn "Starting on label #$start_label" if $DEBUG;
-my $units        = $template->{'units'};
 
-if ($printingtype eq 'PATCRD') {
-    @resultsloop = GetPatronCardItems($batch_id);
-} else {
-    @resultsloop = GetLabelItems($batch_id);
+sub _print_text {
+    my $label_text = shift;
+    foreach my $text_line (@$label_text) {
+        my $pdf_font = $pdf->Font($text_line->{'font'});
+        my $line = "BT /$pdf_font $text_line->{'font_size'} Tf $text_line->{'text_llx'} $text_line->{'text_lly'} Td ($text_line->{'line'}) Tj ET";
+        $pdf->Add($line);
+    }
 }
 
-#warn "UNITS $units";
-#warn "fontsize = $fontsize";
-#warn Dumper $template;
-
-my $unitvalue      = GetUnitsValue($units);
-my $prof_unitvalue = GetUnitsValue($profile->{'unit'});
-
-warn "Template units: $units which converts to $unitvalue PostScript Points" if $DEBUG;
-warn "Profile units: $profile->{'unit'} which converts to $prof_unitvalue PostScript Points" if $DEBUG;
-
-my $tmpl_code = $template->{'tmpl_code'};
-my $tmpl_desc = $template->{'tmpl_desc'};
-
-my $page_height  = ( $template->{'page_height'}  * $unitvalue );
-my $page_width   = ( $template->{'page_width'}   * $unitvalue );
-my $label_height = ( $template->{'label_height'} * $unitvalue );
-my $label_width  = ( $template->{'label_width'}  * $unitvalue );
-my $spine_width  = ( $template->{'label_width'}  * $unitvalue );
-my $circ_width   = ( $template->{'label_width'}  * $unitvalue );
-my $top_margin   = ( $template->{'topmargin'}    * $unitvalue );
-my $left_margin  = ( $template->{'leftmargin'}   * $unitvalue );
-my $colspace     = ( $template->{'colgap'}       * $unitvalue );
-my $rowspace     = ( $template->{'rowgap'}       * $unitvalue );
-
-warn "Converted dimensions are:" if $DEBUG;
-warn "pghth=$page_height, pgwth=$page_width, lblhth=$label_height, lblwth=$label_width, spinwth=$spine_width, circwth=$circ_width, tpmar=$top_margin, lmar=$left_margin, colsp=$colspace, rowsp=$rowspace" if $DEBUG;
-
-my $label_cols = $template->{'cols'};
-my $label_rows = $template->{'rows'};
-
-my $margin           = $top_margin;
-my $left_text_margin = 3;       # FIXME: This value should not be hardcoded
-my $str;
-
-# Some peritent notes from PDF::Reuse regarding prFont()...
-# If a font wasn't found, Helvetica will be set.
-# These names are always recognized: Times-Roman, Times-Bold, Times-Italic, Times-BoldItalic, Courier, Courier-Bold,
-#   Courier-Oblique, Courier-BoldOblique, Helvetica, Helvetica-Bold, Helvetica-Oblique, Helvetica-BoldOblique
-# They can be abbreviated: TR, TB, TI, TBI, C, CB, CO, CBO, H, HB, HO, HBO
-
-my $fontsize    = $template->{'fontsize'};
-my $fontname    = $template->{'font'};
-
-my $text_wrap_cols = GetTextWrapCols( $fontname, $fontsize, $label_width, $left_text_margin );
-
-#warn $label_cols, $label_rows;
+$| = 1;
 
 # set the paper size
 my $lowerLeftX  = 0;
 my $lowerLeftY  = 0;
-my $upperRightX = $page_width;
-my $upperRightY = $page_height;
-my $codetype; # = 'Code39';
-
-warn "Active profile: " . ($profile->{prof_id} || "None") if $DEBUG;
-
-#### PRINT PRELIMINARY DATA ####
-print $cgi->header( -type => 'application/pdf', -attachment => 'barcode.pdf' ); 
-    # Don't print header until very last possible moment
-    # That way if error or die occurs, fatals_to_browser will still work.
-    # After we print this header, there is no way back to HTML.  All we can do is deliver PDF.
-prInitVars();
-$| = 1;
-prFile();   # No args means to STDOUT
-prCompress(1);  # turn on zip compression which dramatically reduces file size
-prMbox( $lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY );
-
-# drawbox( $lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY );  #do page border
-# draw margin box for alignment page
-drawbox($left_margin, $top_margin, $page_width-(2*$left_margin), $page_height-(2*$top_margin)) if $DEBUG_LPT;
-
-
-#### TWEAKS and DEBUGGING ###
-# Adjustments for image position and creep -fbcit
-# NOTE: *All* of these factor in to image position and creep. Keep this in mind when makeing adjustments.
-# Suggested proceedure: Adjust margins until both top and left margins are correct. Then adjust the label
-# height and width to correct label creep across and down page. Units are PostScript Points (72 per inch).
-
-sub debug_drop {
-    my $title = @_ || "";
-    warn "-------------------------$title-----------------------------\n"
-     . "  top margin = $top_margin points\n" 
-     . " left margin = $left_margin points\n"
-     . "label height = $label_height points\n"
-     . "label width  = $label_width points\n";
-}
-
-debug_drop('INITIAL VALUES') if ($DEBUG);
-
-if ( $profile->{'prof_id'} ) {
-    $top_margin   += ($profile->{'offset_vert'} * $prof_unitvalue);    # controls vertical offset
-    $label_height += ($profile->{'creep_vert'}  * $prof_unitvalue);    # controls vertical creep
-    $left_margin  += ($profile->{'offset_horz'} * $prof_unitvalue);    # controls horizontal offset
-    $label_width  += ($profile->{'creep_horz'}  * $prof_unitvalue);    # controls horizontal creep
-}
-
-if ($DEBUG) {
-    if ($profile->{'prof_id'}) {
-        debug_drop('ADJUSTED VALUES');
-    } else {
-        warn "No profile associated so no adjustment applied.";
-    }
-}
-
-#warn " $lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY";
-#warn "$label_rows, $label_cols\n";
-#warn "$label_height, $label_width\n";
-#warn "$page_height, $page_width\n";
-
-my ($rowcount, $colcount, $x_pos, $y_pos, $rowtemp, $coltemp);
-
-if ( $start_label and $start_label == 1 ) {
-    $rowcount = 1;
-    $colcount = 1;
-    $x_pos    = $left_margin;
-    $y_pos    = ( $page_height - $top_margin - $label_height );
-} else {
-    $rowcount = ceil( $start_label / $label_cols );
-    $colcount = ( $start_label - ( ( $rowcount - 1 ) * $label_cols ) );
-    $x_pos = $left_margin + ( $label_width * ( $colcount - 1 ) ) +
-      ( $colspace * ( $colcount - 1 ) );
-    $y_pos = $page_height - $top_margin - ( $label_height * $rowcount ) -
-      ( $rowspace * ( $rowcount - 1 ) );
-    $DEBUG and warn "Start label: $start_label. Beginning in row $rowcount, column $colcount\n"
-        . "(X,Y) positions = ($x_pos,$y_pos)\n"
-        . "Rowspace = $rowspace, Label height = $label_height";
-}
-
-#
-#### main foreach loop #### 
-#
-
-foreach my $item (@resultsloop) {
-    warn "Label parameters: xpos=$x_pos, ypos=$y_pos, lblwid=$label_width, lblhig=$label_height" if $DEBUG;
-
-    drawbox($x_pos, $y_pos, $label_width, $label_height) if $guidebox;  # regardless of printingtype
-
-    if ( $printingtype eq 'BAR' ) {
-        DrawBarcode( $x_pos, $y_pos, $label_height, $label_width, $item->{'barcode'}, $barcodetype );
+my $upperRightX = $template->get_attr('page_width');
+my $upperRightY = $template->get_attr('page_height');
+
+$pdf->Compress(1);
+$pdf->Mbox($lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY);
+
+my ($row_count, $col_count, $llx, $lly) = $template->get_label_position($start_label);
+LABEL_ITEMS:
+foreach my $item (@{$batch->get_attr('items')}) {
+    my ($barcode_llx, $barcode_lly, $barcode_width, $barcode_y_scale_factor) = 0,0,0,0;
+    my $label = C4::Labels::Label->new(
+                                    batch_id            => $batch_id,
+                                    item_number         => $item->{'item_number'},
+                                    width               => $template->get_attr('label_width'),
+                                    height              => $template->get_attr('label_height'),
+                                    top_text_margin     => $template->get_attr('top_text_margin'),
+                                    left_text_margin    => $template->get_attr('left_text_margin'),
+                                    barcode_type        => $layout->get_attr('barcode_type'),
+                                    printing_type       => $layout->get_attr('printing_type'),
+                                    guidebox            => $layout->get_attr('guidebox'),
+                                    font                => $layout->get_attr('font'),
+                                    font_size           => $layout->get_attr('font_size'),
+                                    callnum_split       => $layout->get_attr('callnum_split'),
+                                    justify             => $layout->get_attr('text_justify'),
+                                    format_string       => $layout->get_attr('format_string'),
+                                    text_wrap_cols      => $layout->get_text_wrap_cols(label_width => $template->get_attr('label_width'), left_text_margin => $template->get_attr('left_text_margin')),
+                                      );
+    my $label_type = $label->get_label_type;
+    if ($label_type eq 'BIB') {
+        my $line_spacer = ($label->get_attr('font_size') * 1);    # number of pixels between text rows (This is actually leading: baseline to baseline minus font size. Recommended starting point is 20% of font size.).
+        my $text_lly = ($lly + ($template->get_attr('label_height') - $template->get_attr('top_text_margin')));
+        my $label_text = $label->draw_label_text(
+                                        llx             => $llx,
+                                        lly             => $text_lly,
+                                        line_spacer     => $line_spacer,
+                                        );
+        _print_text($label_text);
+        ($row_count, $col_count, $llx, $lly) = _calc_next_label_pos($row_count, $col_count, $llx, $lly);
+        next LABEL_ITEMS;
     }
-    elsif ( $printingtype eq 'BARBIB' ) {
-        # reposoitioning barcode up the top of label
-        my $barcode_height = ($label_height / 1.5 );    ## scaling voodoo
-        my $text_height    = $label_height / 2;
-        my $barcode_y      = $y_pos + ( $label_height / 2.5  );   ## scaling voodoo
-
-        DrawBarcode( $x_pos, $barcode_y, $barcode_height, $label_width, $item->{'barcode'}, $barcodetype );
-        DrawSpineText( $x_pos, $y_pos, $label_height, $label_width, $fontname, $fontsize,
-            $left_text_margin, $text_wrap_cols, \$item, \$conf_data, $printingtype );
-    }    # correct
-    elsif ( $printingtype eq 'BIBBAR' ) {
-        my $barcode_height = $label_height / 2;
-        DrawBarcode( $x_pos, $y_pos, $barcode_height, $label_width, $item->{'barcode'}, $barcodetype );
-        DrawSpineText( $x_pos, $y_pos, $label_height, $label_width, $fontname, $fontsize,
-            $left_text_margin, $text_wrap_cols, \$item, \$conf_data, $printingtype );
-    }
-    elsif ( $printingtype eq 'ALT' ) {
-        DrawBarcode( $x_pos, $y_pos, $label_height, $label_width, $item->{'barcode'}, $barcodetype );
-        CalcNextLabelPos();
-        drawbox( $x_pos, $y_pos, $label_width, $label_height ) if $guidebox;
-        DrawSpineText( $x_pos, $y_pos, $label_height, $label_width, $fontname, $fontsize,
-            $left_text_margin, $text_wrap_cols, \$item, \$conf_data, $printingtype );
-    }
-    elsif ( $printingtype eq 'BIB' ) {
-        DrawSpineText( $x_pos, $y_pos, $label_height, $label_width, $fontname, $fontsize,
-            $left_text_margin, $text_wrap_cols, \$item, \$conf_data, $printingtype );
-    }
-    elsif ( $printingtype eq 'PATCRD' ) {
-        my $patron_data = $item;
-        #FIXME: This needs to be paramatized and passed in from the user...
-        #Each element of this hash is a separate line on the patron card. Keys are the text to print and the associated data is the point size.
-        my $text = {        
-            $patron_data->{'description'}  => $fontsize,
-            $patron_data->{'branchname'}   => ($fontsize + 3),
-        };
-        $DEBUG and warn "Generating patron card for cardnumber $patron_data->{'cardnumber'}";
-        my $barcode_height = $label_height / 2.75; #FIXME: Scaling barcode height; this needs to be a user parameter.
-        DrawBarcode( $x_pos, $y_pos, $barcode_height, $label_width, $patron_data->{'cardnumber'}, $barcodetype );
-        DrawPatronCardText( $x_pos, $y_pos, $label_height, $label_width, $fontname, $fontsize,
-            $left_text_margin, $text_wrap_cols, $text, $printingtype );
+    elsif ($label_type eq 'BARBIB') {
+        $barcode_llx = $llx + $template->get_attr('left_text_margin');                             # this places the bottom left of the barcode the left text margin distance to right of the the left edge of the label ($llx)
+        $barcode_lly = ($lly + $template->get_attr('label_height')) - $template->get_attr('top_text_margin');        # this places the bottom left of the barcode the top text margin distance below the top of the label ($lly)
+        $barcode_width = 0.8 * $template->get_attr('label_width');                                 # this scales the barcode width to 80% of the label width
+        $barcode_y_scale_factor = 0.01 * $template->get_attr('label_height');                      # this scales the barcode height to 10% of the label height
+        my $line_spacer = ($label->get_attr('font_size') * 1);    # number of pixels between text rows (This is actually leading: baseline to baseline minus font size. Recommended starting point is 20% of font size.).
+        my $text_lly = ($lly + ($template->get_attr('label_height') - $template->get_attr('top_text_margin')));
+        my $label_text = $label->draw_label_text(
+                                        llx             => $llx,
+                                        lly             => $text_lly,
+                                        line_spacer     => $line_spacer,
+                                        );
+        _print_text($label_text);
     }
     else {
-        die "CANNOT PRINT: Unknown printingtype '$printingtype'";
-    }
-
-    CalcNextLabelPos();     # regardless of printingtype
-}    # end for item loop
-prEnd();
-
-sub CalcNextLabelPos {
-    if ($colcount < $label_cols) {
-        # warn "new col";
-        $x_pos = ( $x_pos + $label_width + $colspace );
-        $colcount++;
-    } else {
-        $x_pos = $left_margin;
-        if ($rowcount == $label_rows) {
-            # warn "new page";
-            prPage();
-            $y_pos    = ( $page_height - $top_margin - $label_height );
-            $rowcount = 1;
-        } else {
-            # warn "new row";
-            $y_pos = ( $y_pos - $rowspace - $label_height );
-            $rowcount++;
+        $barcode_llx = $llx + $template->get_attr('left_text_margin');             # this places the bottom left of the barcode the left text margin distance to right of the the left edge of the label ($llx)
+        $barcode_lly = $lly + $template->get_attr('top_text_margin');              # this places the bottom left of the barcode the top text margin distance above the bottom of the label ($lly)
+        $barcode_width = 0.8 * $template->get_attr('label_width');                 # this scales the barcode width to 80% of the label width
+        $barcode_y_scale_factor = 0.01 * $template->get_attr('label_height');      # this scales the barcode height to 10% of the label height
+        if ($label_type eq 'BIBBAR' || $label_type eq 'ALT') {
+            my $line_spacer = ($label->get_attr('font_size') * 1);    # number of pixels between text rows (This is actually leading: baseline to baseline minus font size. Recommended starting point is 20% of font size.).
+            my $text_lly = ($lly + ($template->get_attr('label_height') - $template->get_attr('top_text_margin')));
+            my $label_text = $label->draw_label_text(
+                                            llx             => $llx,
+                                            lly             => $text_lly,
+                                            line_spacer     => $line_spacer,
+                                            );
+            _print_text($label_text);
+        }
+        if ($label_type eq 'ALT') {
+        ($row_count, $col_count, $llx, $lly) = _calc_next_label_pos($row_count, $col_count, $llx, $lly);
         }
-        $colcount = 1;
     }
+    $label->barcode(
+                llx                 => $barcode_llx,
+                lly                 => $barcode_lly,
+                width               => $barcode_width,
+                y_scale_factor      => $barcode_y_scale_factor,
+    );
+    ($row_count, $col_count, $llx, $lly) = _calc_next_label_pos($row_count, $col_count, $llx, $lly);
 }
 
+$pdf->End();