View | Details | Raw Unified | Return to bug 11539
Collapse All | Expand All

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

Return to bug 11539