From 36113f890aa3674013b8906614c6a2387b637944 Mon Sep 17 00:00:00 2001 From: David Cook Date: Wed, 23 Jul 2025 02:41:24 +0000 Subject: [PATCH] Bug 40473: Use better algorithm for calculating Code39 barcode length This patch uses the PDF::Reuse::Barcode algorithm for calculating barcode background box length relative to barcode length in order to determine the actual barcode width and thus the X scale factor required to re-size that barcode so that it fits on the label width (or rather the "Barcode width" which is a percentage applied to the label width to create a narrower usable surface for the barcode and barcode background box). To test: 0. Apply the patch 1. Go to http://localhost:8081/cgi-bin/koha/cataloguing/additem.pl?biblionumber=29 2. Add an item with barcode 123456, add an item with barcode 39999000001310399991, and ensure an item with barcode 39999000001310 already exists 3. Go to http://localhost:8081/cgi-bin/koha/labels/label-manage.pl?label_element=batch 4. Create a patch that uses 3 items using 39999000001310, 2 items with 123456, and 2 items with 39999000001310399991 4. Go to http://localhost:8081/cgi-bin/koha/labels/label-manage.pl?label_element=layout 5. Edit the layout "Label Test" 6. Change the "Barcode width" to 1, and enable "Draw guide boxes", and click "Save" 7. Export the batch using the template "Avery 5160 | 1 x 2-5/8" and the layout "Label Test" and open as PDF 8. Note that the whole barcode fits inside the red guidebox (there should be some overlap of white on top of the red guidebox but that is normal since we're using 100% of the barcode label width). 9. Edit the "Label Test" layout to try "Code39 + Modulo43" and "Code39 + Modulo10" and retry batch export 10. Edit the "Label Test" and try different "Barcode width" like the usual default .8 11. Celebrate that your barcodes fit on the label! --- C4/Labels/Label.pm | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/C4/Labels/Label.pm b/C4/Labels/Label.pm index dd289083ee..6a17316b4c 100644 --- a/C4/Labels/Label.pm +++ b/C4/Labels/Label.pm @@ -7,6 +7,7 @@ use Text::Wrap qw( wrap ); use Algorithm::CheckDigits qw( CheckDigits ); use Text::CSV_XS; use Text::Bidi qw( log2vis ); +use GD::Barcode::Code39; use C4::Context; use C4::Biblio qw( GetMarcFromKohaField ); @@ -526,9 +527,6 @@ sub barcode { my $hide_text = 'yes'; if ( $params{'barcode_type'} =~ m/CODE39/ ) { - $bar_length = '17.5'; - $tot_bar_length = ( $bar_length * $num_of_bars ) + ( $guard_length * 2 ); - $x_scale_factor = ( $params{'width'} / $tot_bar_length ); if ( $params{'barcode_type'} eq 'CODE39MOD' ) { my $c39 = CheckDigits('code_39'); # get modulo43 checksum $params{'barcode_data'} = $c39->complete( $params{'barcode_data'} ); @@ -538,6 +536,11 @@ sub barcode { $hide_text = ''; } eval { + #NOTE: Barcode length algorithm comes from PDF::Reuse::Barcode + #NOTE: 20 is arbitrary padding added to the barcode background by PDF::Reuse::Barcode + my $oGdB = GD::Barcode::Code39->new("*$params{'barcode_data'}*"); + my $whole_barcode_length = ( length( $oGdB->barcode() ) * 0.9 ) + 20; + $x_scale_factor = ( $params{'width'} / $whole_barcode_length ); PDF::Reuse::Barcode::Code39( x => $params{'llx'}, y => $params{'lly'}, -- 2.39.5