Tweaking searching
[koha.git] / C4 / Barcodes / PrinterConfig.pm
1 package C4::Barcodes::PrinterConfig;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA  02111-1307 USA
17
18 use strict;
19 require Exporter;
20 use vars qw($VERSION @EXPORT);
21
22 use PDF::API2;
23 use PDF::API2::Page;
24
25 # set the version for version checking
26 $VERSION = 0.01;
27
28 =head1 NAME
29
30 C4::Barcodes::PrinterConfig - Koha module dealing with labels in a PDF.
31
32 =head1 SYNOPSIS
33
34         use C4::Barcodes::PrinterConfig;
35
36 =head1 DESCRIPTION
37
38 This package is used to deal with labels in a pdf file. Giving some parameters,
39 this package contains several functions to handle every label considering the 
40 environment of the pdf file.
41
42 =head1 FUNCTIONS
43
44 =over 2
45
46 =cut
47
48 @EXPORT = qw(&labelsPage &getLabelPosition setPositionsForX setPositionsForY);
49
50 my @positionsForX; # Takes all the X positions of the pdf file.
51 my @positionsForY; # Takes all the Y positions of the pdf file.
52 my $firstLabel = 1; # Test if the label passed as a parameter is the first label to be printed into the pdf file.
53
54 =item setPositionsForX
55
56         C4::Barcodes::PrinterConfig::setPositionsForX($marginLeft, $labelWidth, $columns, $pageType);
57
58 Calculate and stores all the X positions across the pdf page.
59
60 C<$marginLeft> Indicates how much left margin do you want in your page type.
61
62 C<$labelWidth> Indicates the width of the label that you are going to use.
63
64 C<$columns> Indicates how many columns do you want in your page type.
65
66 C<$pageType> Page type to print (eg: a4, legal, etc).
67
68 =cut
69 #'
70 sub setPositionsForX {
71         my ($marginLeft, $labelWidth, $columns, $pageType) = @_;
72         my $defaultDpi = 72/25.4; # By default we know 25.4 mm -> 1 inch -> 72 dots per inch
73         my $whereToStart = ($marginLeft + ($labelWidth/2));
74         my $firstLabel = $whereToStart*$defaultDpi;
75         my $spaceBetweenLabels = $labelWidth*$defaultDpi;
76         my @positions;
77         for (my $i = 0; $i < $columns ; $i++) {
78                 push @positions, ($firstLabel+($spaceBetweenLabels*$i));
79         }
80         @positionsForX = @positions;
81 }
82
83 =item setPositionsForY
84
85         C4::Barcodes::PrinterConfig::setPositionsForY($marginBottom, $labelHeigth, $rows, $pageType);
86
87 Calculate and stores all tha Y positions across the pdf page.
88
89 C<$marginBottom> Indicates how much bottom margin do you want in your page type.
90
91 C<$labelHeigth> Indicates the height of the label that you are going to use.
92
93 C<$rows> Indicates how many rows do you want in your page type.
94
95 C<$pageType> Page type to print (eg: a4, legal, etc).
96
97 =cut
98 #'
99 sub setPositionsForY {
100         my ($marginBottom, $labelHeigth, $rows, $pageType) = @_;
101         my $defaultDpi = 72/25.4; # By default we know 25.4 mm -> 1 inch -> 72 dots per inch
102         my $whereToStart = ($marginBottom + ($labelHeigth/2));
103         my $firstLabel = $whereToStart*$defaultDpi;
104         my $spaceBetweenLabels = $labelHeigth*$defaultDpi;
105         my @positions;
106         for (my $i = 0; $i < $rows; $i++) {
107                 unshift @positions, ($firstLabel+($spaceBetweenLabels*$i));
108         }
109         @positionsForY = @positions;
110 }
111
112 =item getLabelPosition
113
114         (my $x, my $y, $pdfObject, $pageObject, $gfxObject, $textObject, $coreObject, $labelPosition) = 
115                                         C4::Barcodes::PrinterConfig::getLabelPosition($labelPosition, 
116                                                                                                                                   $pdfObject, 
117                                                                                                                                   $page,
118                                                                                                                                   $gfx,
119                                                                                                                                   $text,
120                                                                                                                                   $fontObject,
121                                                                                                                                   $pageType);   
122
123 Return the (x,y) position of the label that you are going to print considering the environment.
124
125 C<$labelPosition> Indicates which label positions do you want to place by x and y coordinates.
126
127 C<$pdfObject> The PDF object in use.
128
129 C<$page> The page in use.
130
131 C<$gfx> The gfx resource to handle with barcodes objects.
132
133 C<$text> The text resource to handle with text.
134
135 C<$fontObject> The font object
136
137 C<$pageType> Page type to print (eg: a4, legal, etc).
138
139 =cut
140 #'
141 sub getLabelPosition {
142         my ($labelNum, $pdf, $page, $gfxObject, $textObject, $fontObject, $pageType) = @_;
143         my $indexX = $labelNum % @positionsForX;
144         my $indexY = int($labelNum / @positionsForX);
145         # Calculates the next label position and return that label number
146         my $nextIndexX = $labelNum % @positionsForX;
147         my $nextIndexY = $labelNum % @positionsForY;
148         if ($firstLabel) {
149           $page = $pdf->page;
150           $page->mediabox($pageType);
151           $gfxObject = $page->gfx;
152           $textObject = $page->text;
153           $textObject->font($fontObject, 7);
154                   $firstLabel = 0;
155         } elsif (($nextIndexX == 0) && ($nextIndexY == 0)) {
156           $page = $pdf->page;
157           $page->mediabox($pageType);
158           $gfxObject = $page->gfx;
159           $textObject = $page->text;
160           $textObject->font($fontObject, 7);
161         }
162         $labelNum = $labelNum + 1;      
163         if ($labelNum == (@positionsForX*@positionsForY)) {
164                 $labelNum = 0;
165         }
166         return ($positionsForX[$indexX], $positionsForY[$indexY], $pdf, $page, $gfxObject, $textObject, $fontObject, $labelNum);
167 }
168
169 =item labelsPage
170
171         my @labelTable = C4::Barcodes::PrinterConfig::labelsPage($rows, $columns);
172
173 This function will help you to build the labels panel, where you can choose
174 wich label position do you want to start the printer process.
175
176 C<$rows> Indicates how many rows do you want in your page type.
177
178 C<$columns> Indicates how many rows do you want in your page type.
179
180 =cut
181 #'
182 sub labelsPage{
183         my ($rows, $columns) = @_;
184         my @pageType;
185         my $tagname = 0;
186         my $labelname = 1;
187         my $check;
188         for (my $i = 1; $i <= $rows; $i++) {
189                 my @column;
190                 for (my $j = 1; $j <= $columns; $j++) {
191                         my %cell;
192                         if ($tagname == 0) {
193                                 $check = 'checked';
194                         } else {
195                                 $check = '';
196                         }               
197                         %cell = (check => $check,
198                                          tagname => $tagname,
199                                  labelname => $labelname);
200                         $tagname = $tagname + 1;        
201                         $labelname = $labelname + 1;    
202                         push @column, \%cell;
203                 }
204                 my %columns = (columns => \@column);
205                 push @pageType, \%columns;
206         }
207         return @pageType;
208 }
209
210 1;
211
212 __END__
213
214 =back
215
216 =head1 AUTHOR
217
218 Koha Physics Library UNLP <matias_veleda@hotmail.com>
219
220 =cut