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 | 21344 5 58 | use strict; | |||
19 | #use warnings; FIXME - Bug 2505 | |||||
20 | 2 2 2 | 7 3 120 | use vars qw($VERSION @EXPORT); | |||
21 | ||||||
22 | 2 2 2 | 10285 2106635 69 | use PDF::API2; | |||
23 | 2 2 2 | 13 3 90 | use PDF::API2::Page; | |||
24 | ||||||
25 | BEGIN { | |||||
26 | # set the version for version checking | |||||
27 | 2 | 5 | $VERSION = 0.02; | |||
28 | 2 | 8 | require Exporter; | |||
29 | 2 | 1173 | @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 | 5 | my ($marginLeft, $labelWidth, $columns, $pageType) = @_; | |||
83 | 2 | 2 | my $defaultDpi = 72/25.4; # By default we know 25.4 mm -> 1 inch -> 72 dots per inch | |||
84 | 2 | 5 | my $whereToStart = ($marginLeft + ($labelWidth/2)); | |||
85 | 2 | 15 | my $firstLabel = $whereToStart*$defaultDpi; | |||
86 | 2 | 2 | my $spaceBetweenLabels = $labelWidth*$defaultDpi; | |||
87 | 2 | 3 | my @positions; | |||
88 | for (my $i = 0; $i < $columns ; $i++) { | |||||
89 | 5 | 12 | push @positions, ($firstLabel+($spaceBetweenLabels*$i)); | |||
90 | 2 | 2 | } | |||
91 | 2 | 16 | @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 | 1371433 | my ($marginBottom, $labelHeigth, $rows, $pageType) = @_; | |||
112 | 2 | 3 | my $defaultDpi = 72/25.4; # By default we know 25.4 mm -> 1 inch -> 72 dots per inch | |||
113 | 2 | 5 | my $whereToStart = ($marginBottom + ($labelHeigth/2)); | |||
114 | 2 | 4 | my $firstLabel = $whereToStart*$defaultDpi; | |||
115 | 2 | 3 | my $spaceBetweenLabels = $labelHeigth*$defaultDpi; | |||
116 | 2 | 2 | my @positions; | |||
117 | for (my $i = 0; $i < $rows; $i++) { | |||||
118 | 5 | 13 | unshift @positions, ($firstLabel+($spaceBetweenLabels*$i)); | |||
119 | 2 | 2 | } | |||
120 | 2 | 19 | @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 | 1 | my $tagname = 0; | |||
191 | 1 | 2 | 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 | 1 | } | |||
212 | 1 | 8 | return @pageType; | |||
213 | } | |||||
214 | ||||||
215 | 1; | |||||
216 |