File: | C4/Barcodes/PrinterConfig.pm |
Coverage: | 51.6% |
line | stmt | bran | cond | sub | time | code |
---|---|---|---|---|---|---|
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 | 2 2 2 | 637 3 70 | use strict; | |||
19 | #use warnings; FIXME - Bug 2505 | |||||
20 | 2 2 2 | 9 24 135 | use vars qw($VERSION @EXPORT); | |||
21 | ||||||
22 | 2 2 2 | 42651 2568036 82 | use PDF::API2; | |||
23 | 2 2 2 | 20 6 98 | use PDF::API2::Page; | |||
24 | ||||||
25 | BEGIN { | |||||
26 | # set the version for version checking | |||||
27 | 2 | 9 | $VERSION = 0.02; | |||
28 | 2 | 13 | require Exporter; | |||
29 | 2 | 1337 | @EXPORT = qw(&labelsPage &getLabelPosition setPositionsForX setPositionsForY); | |||
30 | } | |||||
31 | ||||||
32 - 74 | =head1 NAME C4::Barcodes::PrinterConfig - Koha module dealing with labels in a PDF. =head1 SYNOPSIS use C4::Barcodes::PrinterConfig; =head1 DESCRIPTION This package is used to deal with labels in a pdf file. Giving some parameters, this package contains several functions to handle every label considering the environment of the pdf file. =head1 FUNCTIONS =head2 my @positionsForX; Takes all the X positions of the pdf file. =head2 my @positionsForY; Takes all the Y positions of the pdf file. =head2 my $firstLabel = 1; Test if the label passed as a parameter is the first label to be printed into the pdf file. =head2 setPositionsForX C4::Barcodes::PrinterConfig::setPositionsForX($marginLeft, $labelWidth, $columns, $pageType); Calculate and stores all the X positions across the pdf page. C<$marginLeft> Indicates how much left margin do you want in your page type. C<$labelWidth> Indicates the width of the label that you are going to use. C<$columns> Indicates how many columns do you want in your page type. C<$pageType> Page type to print (eg: a4, legal, etc). =cut | |||||
75 | ||||||
76 | # Globals used by the functions | |||||
77 | my @positionsForX; | |||||
78 | my @positionsForY; | |||||
79 | my $firstLabel = 1; | |||||
80 | ||||||
81 | sub setPositionsForX { | |||||
82 | 2 | 17 | my ($marginLeft, $labelWidth, $columns, $pageType) = @_; | |||
83 | 2 | 16 | my $defaultDpi = 72/25.4; # By default we know 25.4 mm -> 1 inch -> 72 dots per inch | |||
84 | 2 | 17 | my $whereToStart = ($marginLeft + ($labelWidth/2)); | |||
85 | 2 | 13 | my $firstLabel = $whereToStart*$defaultDpi; | |||
86 | 2 | 13 | my $spaceBetweenLabels = $labelWidth*$defaultDpi; | |||
87 | 2 | 12 | my @positions; | |||
88 | for (my $i = 0; $i < $columns ; $i++) { | |||||
89 | 5 | 13 | push @positions, ($firstLabel+($spaceBetweenLabels*$i)); | |||
90 | 2 | 12 | } | |||
91 | 2 | 30 | @positionsForX = @positions; | |||
92 | } | |||||
93 | ||||||
94 - 108 | =head2 setPositionsForY C4::Barcodes::PrinterConfig::setPositionsForY($marginBottom, $labelHeigth, $rows, $pageType); Calculate and stores all tha Y positions across the pdf page. C<$marginBottom> Indicates how much bottom margin do you want in your page type. C<$labelHeigth> Indicates the height of the label that you are going to use. C<$rows> Indicates how many rows do you want in your page type. C<$pageType> Page type to print (eg: a4, legal, etc). =cut | |||||
109 | ||||||
110 | sub setPositionsForY { | |||||
111 | 2 | 12 | my ($marginBottom, $labelHeigth, $rows, $pageType) = @_; | |||
112 | 2 | 18 | my $defaultDpi = 72/25.4; # By default we know 25.4 mm -> 1 inch -> 72 dots per inch | |||
113 | 2 | 13 | my $whereToStart = ($marginBottom + ($labelHeigth/2)); | |||
114 | 2 | 8 | my $firstLabel = $whereToStart*$defaultDpi; | |||
115 | 2 | 9 | my $spaceBetweenLabels = $labelHeigth*$defaultDpi; | |||
116 | 2 | 8 | my @positions; | |||
117 | for (my $i = 0; $i < $rows; $i++) { | |||||
118 | 5 | 40 | unshift @positions, ($firstLabel+($spaceBetweenLabels*$i)); | |||
119 | 2 | 9 | } | |||
120 | 2 | 25 | @positionsForY = @positions; | |||
121 | } | |||||
122 | ||||||
123 - 144 | =head2 getLabelPosition (my $x, my $y, $pdfObject, $pageObject, $gfxObject, $textObject, $coreObject, $labelPosition) = C4::Barcodes::PrinterConfig::getLabelPosition($labelPosition, $pdfObject, $page, $gfx, $text, $fontObject, $pageType); Return the (x,y) position of the label that you are going to print considering the environment. C<$labelPosition> Indicates which label positions do you want to place by x and y coordinates. C<$pdfObject> The PDF object in use. C<$page> The page in use. C<$gfx> The gfx resource to handle with barcodes objects. C<$text> The text resource to handle with text. C<$fontObject> The font object C<$pageType> Page type to print (eg: a4, legal, etc). =cut | |||||
145 | ||||||
146 | sub getLabelPosition { | |||||
147 | 0 | 0 | my ($labelNum, $pdf, $page, $gfxObject, $textObject, $fontObject, $pageType) = @_; | |||
148 | 0 | 0 | my $indexX = $labelNum % @positionsForX; | |||
149 | 0 | 0 | my $indexY = int($labelNum / @positionsForX); | |||
150 | # Calculates the next label position and return that label number | |||||
151 | 0 | 0 | my $nextIndexX = $labelNum % @positionsForX; | |||
152 | 0 | 0 | my $nextIndexY = $labelNum % @positionsForY; | |||
153 | 0 | 0 | if ($firstLabel) { | |||
154 | 0 | 0 | $page = $pdf->page; | |||
155 | 0 | 0 | $page->mediabox($pageType); | |||
156 | 0 | 0 | $gfxObject = $page->gfx; | |||
157 | 0 | 0 | $textObject = $page->text; | |||
158 | 0 | 0 | $textObject->font($fontObject, 7); | |||
159 | 0 | 0 | $firstLabel = 0; | |||
160 | } elsif (($nextIndexX == 0) && ($nextIndexY == 0)) { | |||||
161 | 0 | 0 | $page = $pdf->page; | |||
162 | 0 | 0 | $page->mediabox($pageType); | |||
163 | 0 | 0 | $gfxObject = $page->gfx; | |||
164 | 0 | 0 | $textObject = $page->text; | |||
165 | 0 | 0 | $textObject->font($fontObject, 7); | |||
166 | } | |||||
167 | 0 | 0 | $labelNum = $labelNum + 1; | |||
168 | 0 | 0 | if ($labelNum == (@positionsForX*@positionsForY)) { | |||
169 | 0 | 0 | $labelNum = 0; | |||
170 | } | |||||
171 | 0 | 0 | return ($positionsForX[$indexX], $positionsForY[$indexY], $pdf, $page, $gfxObject, $textObject, $fontObject, $labelNum); | |||
172 | } | |||||
173 | ||||||
174 - 185 | =head2 labelsPage my @labelTable = C4::Barcodes::PrinterConfig::labelsPage($rows, $columns); This function will help you to build the labels panel, where you can choose wich label position do you want to start the printer process. C<$rows> Indicates how many rows do you want in your page type. C<$columns> Indicates how many rows do you want in your page type. =cut | |||||
186 | ||||||
187 | sub labelsPage{ | |||||
188 | 1 | 2 | my ($rows, $columns) = @_; | |||
189 | 1 | 2 | my @pageType; | |||
190 | 1 | 2 | my $tagname = 0; | |||
191 | 1 | 1 | my $labelname = 1; | |||
192 | 1 | 2 | my $check; | |||
193 | for (my $i = 1; $i <= $rows; $i++) { | |||||
194 | 0 | 0 | my @column; | |||
195 | for (my $j = 1; $j <= $columns; $j++) { | |||||
196 | 0 | 0 | my %cell; | |||
197 | 0 | 0 | if ($tagname == 0) { | |||
198 | 0 | 0 | $check = 'checked'; | |||
199 | } else { | |||||
200 | 0 | 0 | $check = ''; | |||
201 | } | |||||
202 | 0 | 0 | %cell = (check => $check, | |||
203 | tagname => $tagname, | |||||
204 | labelname => $labelname); | |||||
205 | 0 | 0 | $tagname = $tagname + 1; | |||
206 | 0 | 0 | $labelname = $labelname + 1; | |||
207 | 0 | 0 | push @column, \%cell; | |||
208 | 0 | 0 | } | |||
209 | 0 | 0 | my %columns = (columns => \@column); | |||
210 | 0 | 0 | push @pageType, \%columns; | |||
211 | 1 | 2 | } | |||
212 | 1 | 7 | return @pageType; | |||
213 | } | |||||
214 | ||||||
215 | 1; | |||||
216 |