Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
/*
3
pBarcode128 - class to create barcodes (128B)
4
 
5
Version     : 2.1.4
6
Made by     : Jean-Damien POGOLOTTI
7
Last Update : 19/01/2014
8
 
9
This file can be distributed under the license you can find at :
10
 
11
http://www.pchart.net/license
12
 
13
You can find the whole class documentation on the pChart web site.
14
*/
15
/* pData class definition */
16
 
17
class pBarcode128
18
{
19
	var $Codes = [];
20
	var $Reverse = [];
21
	var $Result;
22
	var $pChartObject;
23
 
24
	/* Class creator */
25
	function __construct($BasePath = "")
26
	{
27
		$FileHandle = fopen($BasePath . "data/128B.db", "r");
28
		if (!$FileHandle) {
29
			die("Cannot find barcode database (" . $BasePath . "128B.db).");
30
		}
31
 
32
		while (!feof($FileHandle)) {
33
			$Buffer = fgets($FileHandle, 4096);
34
			$Buffer = str_replace(chr(10), "", $Buffer);
35
			$Buffer = str_replace(chr(13), "", $Buffer);
36
			$Values = explode(";", $Buffer);
37
			$this->Codes[$Values[1]]["ID"] = $Values[0];
38
			$this->Codes[$Values[1]]["Code"] = $Values[2];
39
			$this->Reverse[$Values[0]]["Code"] = $Values[2];
40
			$this->Reverse[$Values[0]]["Asc"] = $Values[1];
41
		}
42
 
43
		fclose($FileHandle);
44
	}
45
 
46
	/* Return the projected size of a barcode */
47
	function getSize($TextString, array $Format = [])
48
	{
49
		$Angle = 0;
50
		$ShowLegend = FALSE;
51
		$LegendOffset = 5;
52
		$DrawArea = FALSE;
53
		$FontSize = 12;
54
		$Height = 30;
55
 
56
		/* Override defaults */
57
		extract($Format);
58
 
59
		$TextString = $this->encode128($TextString);
60
		$BarcodeLength = strlen($this->Result);
61
		$WOffset = ($DrawArea) ? 20 : 0;
62
		$HOffset = ($ShowLegend) ? $FontSize + $LegendOffset + $WOffset : 0;
63
		$X1 = cos($Angle * PI / 180) * ($WOffset + $BarcodeLength);
64
		$Y1 = sin($Angle * PI / 180) * ($WOffset + $BarcodeLength);
65
		$X2 = $X1 + cos(($Angle + 90) * PI / 180) * ($HOffset + $Height);
66
		$Y2 = $Y1 + sin(($Angle + 90) * PI / 180) * ($HOffset + $Height);
67
 
68
		return ["Width" => max(abs($X1), abs($X2)),"Height" => max(abs($Y1), abs($Y2))];
69
	}
70
 
71
	function encode128($Value, $Format = "")
72
	{
73
		$this->Result = "11010010000";
74
		$CRC = 104;
75
		$TextString = "";
76
		for ($i = 1; $i <= strlen($Value); $i++) {
77
			$CharCode = ord($this->mid($Value, $i, 1));
78
			if (isset($this->Codes[$CharCode])) {
79
				$this->Result = $this->Result . $this->Codes[$CharCode]["Code"];
80
				$CRC = $CRC + $i * $this->Codes[$CharCode]["ID"];
81
				$TextString = $TextString . chr($CharCode);
82
			}
83
		}
84
 
85
		$CRC = $CRC - floor($CRC / 103) * 103;
86
		$this->Result = $this->Result . $this->Reverse[$CRC]["Code"]. "1100011101011";
87
 
88
		return $TextString;
89
	}
90
 
91
	/* Create the encoded string */
92
	function draw($Object, $Value, $X, $Y, array $Format = [])
93
	{
94
		$this->pChartObject = $Object;
95
		$R = 0;
96
		$G = 0;
97
		$B = 0;
98
		$Alpha = 100;
99
		$Height = 30;
100
		$Angle = 0;
101
		$ShowLegend = FALSE;
102
		$LegendOffset = 5;
103
		$DrawArea = FALSE;
104
		$AreaR = isset($Format["AreaR"]) ? $Format["AreaR"] : 255;
105
		$AreaG = isset($Format["AreaG"]) ? $Format["AreaG"] : 255;
106
		$AreaB = isset($Format["AreaB"]) ? $Format["AreaB"] : 255;
107
		$AreaBorderR = $AreaR;
108
		$AreaBorderG = $AreaG;
109
		$AreaBorderB = $AreaB;
110
 
111
		/* Override defaults */
112
		extract($Format);
113
 
114
		$TextString = $this->encode128($Value);
115
		if ($DrawArea) {
116
			$X1 = $X + cos(($Angle - 135) * PI / 180) * 10;
117
			$Y1 = $Y + sin(($Angle - 135) * PI / 180) * 10;
118
			$X2 = $X1 + cos($Angle * PI / 180) * (strlen($this->Result) + 20);
119
			$Y2 = $Y1 + sin($Angle * PI / 180) * (strlen($this->Result) + 20);
120
			if ($ShowLegend) {
121
				$X3 = $X2 + cos(($Angle + 90) * PI / 180) * ($Height + $LegendOffset + $this->pChartObject->FontSize + 10);
122
				$Y3 = $Y2 + sin(($Angle + 90) * PI / 180) * ($Height + $LegendOffset + $this->pChartObject->FontSize + 10);
123
			} else {
124
				$X3 = $X2 + cos(($Angle + 90) * PI / 180) * ($Height + 20);
125
				$Y3 = $Y2 + sin(($Angle + 90) * PI / 180) * ($Height + 20);
126
			}
127
 
128
			$X4 = $X3 + cos(($Angle + 180) * PI / 180) * (strlen($this->Result) + 20);
129
			$Y4 = $Y3 + sin(($Angle + 180) * PI / 180) * (strlen($this->Result) + 20);
130
			$this->pChartObject->drawPolygon([$X1,$Y1,$X2,$Y2,$X3,$Y3,$X4,$Y4], ["R" => $AreaR,"G" => $AreaG,"B" => $AreaB,"BorderR" => $AreaBorderR,"BorderG" => $AreaBorderG,"BorderB" => $AreaBorderB]);
131
		}
132
 
133
		for ($i = 1; $i <= strlen($this->Result); $i++) {
134
			if ($this->mid($this->Result, $i, 1) == 1) {
135
				$X1 = $X + cos($Angle * PI / 180) * $i;
136
				$Y1 = $Y + sin($Angle * PI / 180) * $i;
137
				$X2 = $X1 + cos(($Angle + 90) * PI / 180) * $Height;
138
				$Y2 = $Y1 + sin(($Angle + 90) * PI / 180) * $Height;
139
				$this->pChartObject->drawLine($X1, $Y1, $X2, $Y2, ["R" => $R,"G" => $G,"B" => $B,"Alpha" => $Alpha]);
140
			}
141
		}
142
 
143
		if ($ShowLegend) {
144
			$X1 = $X + cos($Angle * PI / 180) * (strlen($this->Result) / 2);
145
			$Y1 = $Y + sin($Angle * PI / 180) * (strlen($this->Result) / 2);
146
			$LegendX = $X1 + cos(($Angle + 90) * PI / 180) * ($Height + $LegendOffset);
147
			$LegendY = $Y1 + sin(($Angle + 90) * PI / 180) * ($Height + $LegendOffset);
148
			$this->pChartObject->drawText($LegendX, $LegendY, $TextString, ["R" => $R,"G" => $G,"B" => $B,"Alpha" => $Alpha,"Angle" => - $Angle,"Align" => TEXT_ALIGN_TOPMIDDLE]);
149
		}
150
	}
151
 
152
	function left($value, $NbChar)
153
	{
154
		return substr($value, 0, $NbChar);
155
	}
156
 
157
	function right($value, $NbChar)
158
	{
159
		return substr($value, strlen($value) - $NbChar, $NbChar);
160
	}
161
 
162
	function mid($value, $Depart, $NbChar)
163
	{
164
		return substr($value, $Depart - 1, $NbChar);
165
	}
166
}
167
 
168
?>