Fixing page numbering in searchresultlist-auth.tmpl
[koha.git] / C4 / Barcodes / PrinterConfig.pm
blobd90cd9645f9843f13134c889eb14c70a687b2a79
1 package C4::Barcodes::PrinterConfig;
3 # This file is part of Koha.
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.
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.
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
18 use strict;
19 use vars qw($VERSION @EXPORT);
21 use PDF::API2;
22 use PDF::API2::Page;
24 BEGIN {
25 # set the version for version checking
26 $VERSION = 0.02;
27 require Exporter;
28 @EXPORT = qw(&labelsPage &getLabelPosition setPositionsForX setPositionsForY);
31 =head1 NAME
33 C4::Barcodes::PrinterConfig - Koha module dealing with labels in a PDF.
35 =head1 SYNOPSIS
37 use C4::Barcodes::PrinterConfig;
39 =head1 DESCRIPTION
41 This package is used to deal with labels in a pdf file. Giving some parameters,
42 this package contains several functions to handle every label considering the
43 environment of the pdf file.
45 =head1 FUNCTIONS
47 =over 2
49 =cut
51 my @positionsForX; # Takes all the X positions of the pdf file.
52 my @positionsForY; # Takes all the Y positions of the pdf file.
53 my $firstLabel = 1; # Test if the label passed as a parameter is the first label to be printed into the pdf file.
55 =item setPositionsForX
57 C4::Barcodes::PrinterConfig::setPositionsForX($marginLeft, $labelWidth, $columns, $pageType);
59 Calculate and stores all the X positions across the pdf page.
61 C<$marginLeft> Indicates how much left margin do you want in your page type.
63 C<$labelWidth> Indicates the width of the label that you are going to use.
65 C<$columns> Indicates how many columns do you want in your page type.
67 C<$pageType> Page type to print (eg: a4, legal, etc).
69 =cut
71 sub setPositionsForX {
72 my ($marginLeft, $labelWidth, $columns, $pageType) = @_;
73 my $defaultDpi = 72/25.4; # By default we know 25.4 mm -> 1 inch -> 72 dots per inch
74 my $whereToStart = ($marginLeft + ($labelWidth/2));
75 my $firstLabel = $whereToStart*$defaultDpi;
76 my $spaceBetweenLabels = $labelWidth*$defaultDpi;
77 my @positions;
78 for (my $i = 0; $i < $columns ; $i++) {
79 push @positions, ($firstLabel+($spaceBetweenLabels*$i));
81 @positionsForX = @positions;
84 =item setPositionsForY
86 C4::Barcodes::PrinterConfig::setPositionsForY($marginBottom, $labelHeigth, $rows, $pageType);
88 Calculate and stores all tha Y positions across the pdf page.
90 C<$marginBottom> Indicates how much bottom margin do you want in your page type.
92 C<$labelHeigth> Indicates the height of the label that you are going to use.
94 C<$rows> Indicates how many rows do you want in your page type.
96 C<$pageType> Page type to print (eg: a4, legal, etc).
98 =cut
100 sub setPositionsForY {
101 my ($marginBottom, $labelHeigth, $rows, $pageType) = @_;
102 my $defaultDpi = 72/25.4; # By default we know 25.4 mm -> 1 inch -> 72 dots per inch
103 my $whereToStart = ($marginBottom + ($labelHeigth/2));
104 my $firstLabel = $whereToStart*$defaultDpi;
105 my $spaceBetweenLabels = $labelHeigth*$defaultDpi;
106 my @positions;
107 for (my $i = 0; $i < $rows; $i++) {
108 unshift @positions, ($firstLabel+($spaceBetweenLabels*$i));
110 @positionsForY = @positions;
113 =item getLabelPosition
115 (my $x, my $y, $pdfObject, $pageObject, $gfxObject, $textObject, $coreObject, $labelPosition) =
116 C4::Barcodes::PrinterConfig::getLabelPosition($labelPosition,
117 $pdfObject,
118 $page,
119 $gfx,
120 $text,
121 $fontObject,
122 $pageType);
124 Return the (x,y) position of the label that you are going to print considering the environment.
126 C<$labelPosition> Indicates which label positions do you want to place by x and y coordinates.
128 C<$pdfObject> The PDF object in use.
130 C<$page> The page in use.
132 C<$gfx> The gfx resource to handle with barcodes objects.
134 C<$text> The text resource to handle with text.
136 C<$fontObject> The font object
138 C<$pageType> Page type to print (eg: a4, legal, etc).
140 =cut
142 sub getLabelPosition {
143 my ($labelNum, $pdf, $page, $gfxObject, $textObject, $fontObject, $pageType) = @_;
144 my $indexX = $labelNum % @positionsForX;
145 my $indexY = int($labelNum / @positionsForX);
146 # Calculates the next label position and return that label number
147 my $nextIndexX = $labelNum % @positionsForX;
148 my $nextIndexY = $labelNum % @positionsForY;
149 if ($firstLabel) {
150 $page = $pdf->page;
151 $page->mediabox($pageType);
152 $gfxObject = $page->gfx;
153 $textObject = $page->text;
154 $textObject->font($fontObject, 7);
155 $firstLabel = 0;
156 } elsif (($nextIndexX == 0) && ($nextIndexY == 0)) {
157 $page = $pdf->page;
158 $page->mediabox($pageType);
159 $gfxObject = $page->gfx;
160 $textObject = $page->text;
161 $textObject->font($fontObject, 7);
163 $labelNum = $labelNum + 1;
164 if ($labelNum == (@positionsForX*@positionsForY)) {
165 $labelNum = 0;
167 return ($positionsForX[$indexX], $positionsForY[$indexY], $pdf, $page, $gfxObject, $textObject, $fontObject, $labelNum);
170 =item labelsPage
172 my @labelTable = C4::Barcodes::PrinterConfig::labelsPage($rows, $columns);
174 This function will help you to build the labels panel, where you can choose
175 wich label position do you want to start the printer process.
177 C<$rows> Indicates how many rows do you want in your page type.
179 C<$columns> Indicates how many rows do you want in your page type.
181 =cut
183 sub labelsPage{
184 my ($rows, $columns) = @_;
185 my @pageType;
186 my $tagname = 0;
187 my $labelname = 1;
188 my $check;
189 for (my $i = 1; $i <= $rows; $i++) {
190 my @column;
191 for (my $j = 1; $j <= $columns; $j++) {
192 my %cell;
193 if ($tagname == 0) {
194 $check = 'checked';
195 } else {
196 $check = '';
198 %cell = (check => $check,
199 tagname => $tagname,
200 labelname => $labelname);
201 $tagname = $tagname + 1;
202 $labelname = $labelname + 1;
203 push @column, \%cell;
205 my %columns = (columns => \@column);
206 push @pageType, \%columns;
208 return @pageType;
213 __END__
215 =back
217 =head1 AUTHOR
219 Koha Physics Library UNLP <matias_veleda@hotmail.com>
221 =cut