Subversion Repositories cheapmusic

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
103 - 1
<?php
2
 
3
class grad {
4
 
5
var $mpdf = null;
6
 
7
function grad(&$mpdf) {
8
	$this->mpdf = $mpdf;
9
}
10
 
11
// mPDF 5.3.A1
12
function CoonsPatchMesh($x, $y, $w, $h, $patch_array=array(), $x_min=0, $x_max=1, $y_min=0, $y_max=1, $colspace='RGB', $return=false){
13
	$s=' q ';
14
	$s.=sprintf(' %.3F %.3F %.3F %.3F re W n ', $x*_MPDFK, ($this->mpdf->h-$y)*_MPDFK, $w*_MPDFK, -$h*_MPDFK);
15
	$s.=sprintf(' %.3F 0 0 %.3F %.3F %.3F cm ', $w*_MPDFK, $h*_MPDFK, $x*_MPDFK, ($this->mpdf->h-($y+$h))*_MPDFK);
16
	$n = count($this->mpdf->gradients)+1;
17
	$this->mpdf->gradients[$n]['type'] = 6; //coons patch mesh
18
	$this->mpdf->gradients[$n]['colorspace'] = $colspace; //coons patch mesh
19
	$bpcd=65535; //16 BitsPerCoordinate
20
	$trans = false;
21
	$this->mpdf->gradients[$n]['stream']='';
22
	for($i=0;$i<count($patch_array);$i++){
23
		$this->mpdf->gradients[$n]['stream'].=chr($patch_array[$i]['f']); //start with the edge flag as 8 bit
24
		for($j=0;$j<count($patch_array[$i]['points']);$j++){
25
			//each point as 16 bit
26
			if (($j % 2) == 1) {	// Y coordinate (adjusted as input is From top left)
27
				$patch_array[$i]['points'][$j]=(($patch_array[$i]['points'][$j]-$y_min)/($y_max-$y_min))*$bpcd;
28
				$patch_array[$i]['points'][$j]=$bpcd-$patch_array[$i]['points'][$j];
29
			}
30
			else {
31
				$patch_array[$i]['points'][$j]=(($patch_array[$i]['points'][$j]-$x_min)/($x_max-$x_min))*$bpcd;
32
			}
33
			if($patch_array[$i]['points'][$j]<0) $patch_array[$i]['points'][$j]=0;
34
			if($patch_array[$i]['points'][$j]>$bpcd) $patch_array[$i]['points'][$j]=$bpcd;
35
			$this->mpdf->gradients[$n]['stream'].=chr(floor($patch_array[$i]['points'][$j]/256));
36
			$this->mpdf->gradients[$n]['stream'].=chr(floor($patch_array[$i]['points'][$j]%256));
37
		}
38
		for($j=0;$j<count($patch_array[$i]['colors']);$j++){
39
			//each color component as 8 bit
40
			if ($colspace=='RGB') {
41
				$this->mpdf->gradients[$n]['stream'].=($patch_array[$i]['colors'][$j][1]);
42
				$this->mpdf->gradients[$n]['stream'].=($patch_array[$i]['colors'][$j][2]);
43
				$this->mpdf->gradients[$n]['stream'].=($patch_array[$i]['colors'][$j][3]);
44
				if (isset($patch_array[$i]['colors'][$j][4]) && ord($patch_array[$i]['colors'][$j][4])<100) { $trans = true; }
45
			}
46
			else if ($colspace=='CMYK') {
47
				$this->mpdf->gradients[$n]['stream'].=chr(ord($patch_array[$i]['colors'][$j][1])*2.55);
48
				$this->mpdf->gradients[$n]['stream'].=chr(ord($patch_array[$i]['colors'][$j][2])*2.55);
49
				$this->mpdf->gradients[$n]['stream'].=chr(ord($patch_array[$i]['colors'][$j][3])*2.55);
50
				$this->mpdf->gradients[$n]['stream'].=chr(ord($patch_array[$i]['colors'][$j][4])*2.55);
51
				if (isset($patch_array[$i]['colors'][$j][5]) && ord($patch_array[$i]['colors'][$j][5])<100) { $trans = true; }
52
			}
53
			else if ($colspace=='Gray') {
54
				$this->mpdf->gradients[$n]['stream'].=($patch_array[$i]['colors'][$j][1]);
55
				if ($patch_array[$i]['colors'][$j][2]==1) { $trans = true; }	// transparency converted from rgba or cmyka()
56
			}
57
		}
58
	}
59
	// TRANSPARENCY
60
	if ($trans) {
61
		$this->mpdf->gradients[$n]['stream_trans']='';
62
		for($i=0;$i<count($patch_array);$i++){
63
			$this->mpdf->gradients[$n]['stream_trans'].=chr($patch_array[$i]['f']);
64
			for($j=0;$j<count($patch_array[$i]['points']);$j++){
65
				//each point as 16 bit
66
				$this->mpdf->gradients[$n]['stream_trans'].=chr(floor($patch_array[$i]['points'][$j]/256));
67
				$this->mpdf->gradients[$n]['stream_trans'].=chr(floor($patch_array[$i]['points'][$j]%256));
68
			}
69
			for($j=0;$j<count($patch_array[$i]['colors']);$j++){
70
				//each color component as 8 bit // OPACITY
71
				if ($colspace=='RGB') {
72
					$this->mpdf->gradients[$n]['stream_trans'].=chr(intval(ord($patch_array[$i]['colors'][$j][4])*2.55));
73
				}
74
				else if ($colspace=='CMYK') {
75
					$this->mpdf->gradients[$n]['stream_trans'].=chr(intval(ord($patch_array[$i]['colors'][$j][5])*2.55));
76
				}
77
				else if ($colspace=='Gray') {
78
					$this->mpdf->gradients[$n]['stream_trans'].=chr(intval(ord($patch_array[$i]['colors'][$j][3])*2.55));
79
				}
80
			}
81
		}
82
		$this->mpdf->gradients[$n]['trans'] = true;
83
		$s .= ' /TGS'.$n.' gs ';
84
	}
85
	//paint the gradient
86
	$s .= '/Sh'.$n.' sh'."\n";
87
	//restore previous Graphic State
88
	$s .= 'Q'."\n";
89
	if ($return) { return $s; }
90
	else { $this->mpdf->_out($s); }
91
}
92
 
93
 
94
// type = linear:2; radial: 3;
95
// Linear: $coords - array of the form (x1, y1, x2, y2) which defines the gradient vector (see linear_gradient_coords.jpg).
96
//    The default value is from left to right (x1=0, y1=0, x2=1, y2=0).
97
// Radial: $coords - array of the form (fx, fy, cx, cy, r) where (fx, fy) is the starting point of the gradient with color1,
98
//    (cx, cy) is the center of the circle with color2, and r is the radius of the circle (see radial_gradient_coords.jpg).
99
//    (fx, fy) should be inside the circle, otherwise some areas will not be defined
100
// $col = array(R,G,B/255); or array(G/255); or array(C,M,Y,K/100)
101
// $stops = array('col'=>$col [, 'opacity'=>0-1] [, 'offset'=>0-1])
102
function Gradient($x, $y, $w, $h, $type, $stops=array(), $colorspace='RGB', $coords='', $extend='', $return=false, $is_mask=false) {
103
	if (strtoupper(substr($type,0,1)) == 'L') { $type = 2; }	// linear
104
	else if (strtoupper(substr($type,0,1)) == 'R') { $type = 3; }	// radial
105
	if ($colorspace != 'CMYK' && $colorspace != 'Gray') {
106
		$colorspace = 'RGB';
107
	}
108
	$bboxw = $w;
109
	$bboxh = $h;
110
	$usex = $x;
111
	$usey = $y;
112
	$usew = $bboxw;
113
	$useh = $bboxh;
114
	if ($type < 1) { $type = 2; }
115
	if ($coords[0]!==false && preg_match('/([0-9.]+(px|em|ex|pc|pt|cm|mm|in))/i',$coords[0],$m)) {
116
		$tmp = $this->mpdf->ConvertSize($m[1],$this->mpdf->w,$this->mpdf->FontSize,false);
117
		if ($tmp) { $coords[0] = $tmp/$w; }
118
	}
119
	if ($coords[1]!==false && preg_match('/([0-9.]+(px|em|ex|pc|pt|cm|mm|in))/i',$coords[1],$m)) {
120
		$tmp = $this->mpdf->ConvertSize($m[1],$this->mpdf->w,$this->mpdf->FontSize,false);
121
		if ($tmp) { $coords[1] = 1-($tmp/$h); }
122
	}
123
	// LINEAR
124
	if ($type == 2) {
125
		$angle = $coords[4];
126
		$repeat = $coords[5];
127
		// ALL POINTS SET (default for custom mPDF linear gradient) - no -moz
128
		if ($coords[0]!==false && $coords[1]!==false && $coords[2]!==false && $coords[3]!==false) {
129
			// do nothing - coords used as they are
130
		}
131
 
132
		// If both a <point> and <angle> are defined, the gradient axis starts from the point and runs along the angle. The end point is
133
		// defined as before - in this case start points may not be in corners, and axis may not correctly fall in the right quadrant.
134
		// NO end points (Angle defined & Start points)
135
		else if ($angle!==false && $coords[0]!==false && $coords[1]!==false && $coords[2]===false && $coords[3]===false) {
136
		  if ($angle==0 || $angle==360) { $coords[3]=$coords[1]; if ($coords[0]==1) $coords[2]=2; else $coords[2]=1; }
137
		  else if ($angle==90) { $coords[2]=$coords[0]; $coords[3]=1; if ($coords[1]==1) $coords[3]=2; else $coords[3]=1; }
138
		  else if ($angle==180) { if ($coords[4]==0) $coords[2]=-1; else $coords[2]=0; $coords[3]=$coords[1]; }
139
		  else if ($angle==270) { $coords[2]=$coords[0]; if ($coords[1]==0) $coords[3]=-1; else $coords[3]=0; }
140
		  else {
141
			$endx=1; $endy=1;
142
			if ($angle <=90) {
143
				if ($angle <=45) { $endy=tan(deg2rad($angle)); }
144
				else { $endx=tan(deg2rad(90-$angle)); }
145
				$b = atan2(($endy*$bboxh), ($endx*$bboxw));
146
				$ny = 1 - $coords[1] - (tan($b) * (1-$coords[0]));
147
				$tx = sin($b) * cos($b) * $ny;
148
				$ty = cos($b) * cos($b) * $ny;
149
				$coords[2] = 1+$tx; $coords[3] = 1-$ty;
150
			}
151
			else if ($angle <=180) {
152
				if ($angle <=135) { $endx=tan(deg2rad($angle-90)); }
153
				else { $endy=tan(deg2rad(180-$angle)); }
154
				$b = atan2(($endy*$bboxh), ($endx*$bboxw));
155
				$ny = 1 - $coords[1] - (tan($b) * ($coords[0]));
156
				$tx = sin($b) * cos($b) * $ny;
157
				$ty = cos($b) * cos($b) * $ny;
158
				$coords[2] =  -$tx; $coords[3] = 1-$ty;
159
			}
160
			else if ($angle <=270) {
161
				if ($angle <=225) { $endy=tan(deg2rad($angle-180)); }
162
				else { $endx=tan(deg2rad(270-$angle)); }
163
				$b = atan2(($endy*$bboxh), ($endx*$bboxw));
164
				$ny = $coords[1] - (tan($b) * ($coords[0]));
165
				$tx = sin($b) * cos($b) * $ny;
166
				$ty = cos($b) * cos($b) * $ny;
167
				$coords[2] = -$tx; $coords[3] = $ty;
168
			}
169
			else {
170
				if ($angle <=315) { $endx=tan(deg2rad($angle-270)); }
171
				else { $endy=tan(deg2rad(360-$angle));  }
172
				$b = atan2(($endy*$bboxh), ($endx*$bboxw));
173
				$ny = $coords[1] - (tan($b) * (1-$coords[0]));
174
				$tx = sin($b) * cos($b) * $ny;
175
				$ty = cos($b) * cos($b) * $ny;
176
				$coords[2] = 1+$tx; $coords[3] = $ty;
177
 
178
			}
179
		  }
180
		}
181
 
182
		// -moz If the first parameter is only an <angle>, the gradient axis starts from the box's corner that would ensure the
183
		// axis goes through the box. The axis runs along the specified angle. The end point of the axis is defined such that the
184
		// farthest corner of the box from the starting point is perpendicular to the gradient axis at that point.
185
		// NO end points or Start points (Angle defined)
186
		else if ($angle!==false && $coords[0]===false && $coords[1]===false) {
187
		  if ($angle==0 || $angle==360) { $coords[0]=0; $coords[1]=0; $coords[2]=1; $coords[3]=0; }
188
		  else if ($angle==90) { $coords[0]=0; $coords[1]=0; $coords[2]=0; $coords[3]=1; }
189
		  else if ($angle==180) { $coords[0]=1; $coords[1]=0; $coords[2]=0; $coords[3]=0; }
190
		  else if ($angle==270) { $coords[0]=0; $coords[1]=1; $coords[2]=0; $coords[3]=0; }
191
		  else {
192
			if ($angle <=90) {
193
				$coords[0]=0; $coords[1]=0;
194
				if ($angle <=45) { $endx=1; $endy=tan(deg2rad($angle)); }
195
				else { $endx=tan(deg2rad(90-$angle)); $endy=1; }
196
			}
197
			else if ($angle <=180) {
198
				$coords[0]=1; $coords[1]=0;
199
				if ($angle <=135) { $endx=tan(deg2rad($angle-90)); $endy=1; }
200
				else { $endx=1; $endy=tan(deg2rad(180-$angle)); }
201
			}
202
			else if ($angle <=270) {
203
				$coords[0]=1; $coords[1]=1;
204
				if ($angle <=225) { $endx=1; $endy=tan(deg2rad($angle-180)); }
205
				else { $endx=tan(deg2rad(270-$angle)); $endy=1; }
206
			}
207
			else {
208
				$coords[0]=0; $coords[1]=1;
209
				if ($angle <=315) { $endx=tan(deg2rad($angle-270)); $endy=1; }
210
				else { $endx=1; $endy=tan(deg2rad(360-$angle));  }
211
			}
212
			$b = atan2(($endy*$bboxh), ($endx*$bboxw));
213
			$h2 = $bboxh - ($bboxh * tan($b));
214
			$px = $bboxh + ($h2 * sin($b) * cos($b));
215
			$py = ($bboxh * tan($b)) + ($h2 * sin($b) * sin($b));
216
			$x1 = $px / $bboxh;
217
			$y1 = $py / $bboxh;
218
			if ($angle <=90) { $coords[2] = $x1; $coords[3] = $y1; }
219
			else if ($angle <=180) { $coords[2] = 1-$x1; $coords[3] = $y1; }
220
			else if ($angle <=270) { $coords[2] = 1-$x1; $coords[3] = 1-$y1; }
221
			else { $coords[2] = $x1; $coords[3] = 1-$y1; }
222
		  }
223
		}
224
		// -moz If the first parameter to the gradient function is only a <point>, the gradient axis starts from the specified point,
225
		// and ends at the point you would get if you rotated the starting point by 180 degrees about the center of the box that the
226
		// gradient is to be applied to.
227
		// NO angle and NO end points (Start points defined)
228
		else if ((!isset($angle) || $angle===false) && $coords[0]!==false && $coords[1]!==false) { 	// should have start and end defined
229
		  $coords[2] = 1-$coords[0]; $coords[3] = 1-$coords[1];
230
		  $angle = rad2deg(atan2($coords[3]-$coords[1],$coords[2]-$coords[0]));
231
		  if ($angle < 0) { $angle += 360; }
232
		  else if ($angle > 360) { $angle -= 360; }
233
		  if ($angle!=0 && $angle!=360 && $angle!=90 && $angle!=180 && $angle!=270) {
234
		    if ($w >= $h) {
235
			$coords[1] *= $h/$w ;
236
			$coords[3] *= $h/$w ;
237
			$usew = $useh = $bboxw;
238
			$usey -= ($w-$h);
239
		    }
240
		    else {
241
			$coords[0] *= $w/$h ;
242
			$coords[2] *= $w/$h ;
243
			$usew = $useh = $bboxh;
244
		    }
245
		  }
246
		}
247
 
248
		// -moz If neither a <point> or <angle> is specified, i.e. the entire function consists of only <stop> values, the gradient
249
		// axis starts from the top of the box and runs vertically downwards, ending at the bottom of the box.
250
		else {	// default values T2B
251
			// All values are set in parseMozGradient - so won't appear here
252
			$coords = array(0,0,1,0);	// default for original linear gradient (L2R)
253
		}
254
		$s = ' q';
255
		$s .= sprintf(' %.3F %.3F %.3F %.3F re W n', $x*_MPDFK, ($this->mpdf->h-$y)*_MPDFK, $w*_MPDFK, -$h*_MPDFK)."\n";
256
		$s .= sprintf(' %.3F 0 0 %.3F %.3F %.3F cm', $usew*_MPDFK, $useh*_MPDFK, $usex*_MPDFK, ($this->mpdf->h-($usey+$useh))*_MPDFK)."\n";
257
	}
258
 
259
	// RADIAL
260
	else if ($type == 3) {
261
		$radius = $coords[4];
262
		$angle = $coords[5];	// ?? no effect
263
		$shape = $coords[6];
264
		$size = $coords[7];
265
		$repeat = $coords[8];
266
		// ALL POINTS AND RADIUS SET (default for custom mPDF radial gradient) - no -moz
267
		if ($coords[0]!==false && $coords[1]!==false && $coords[2]!==false && $coords[3]!==false && $coords[4]!==false) {
268
			// do nothing - coords used as they are
269
		}
270
		// If a <point> is defined
271
		else if ($shape!==false && $size!==false) {
272
		   if ($coords[2]==false) { $coords[2] = $coords[0]; }
273
		   if ($coords[3]==false) { $coords[3] = $coords[1]; }
274
		   // ELLIPSE
275
		   if ($shape=='ellipse') {
276
			$corner1 = sqrt(pow($coords[0],2) + pow($coords[1],2));
277
			$corner2 = sqrt(pow($coords[0],2) + pow((1-$coords[1]),2));
278
			$corner3 = sqrt(pow((1-$coords[0]),2) + pow($coords[1],2));
279
			$corner4 = sqrt(pow((1-$coords[0]),2) + pow((1-$coords[1]),2));
280
			if ($size=='closest-side') { $radius = min($coords[0], $coords[1], (1-$coords[0]), (1-$coords[1])); }
281
			else if ($size=='closest-corner') { $radius = min($corner1, $corner2, $corner3, $corner4); }
282
			else if ($size=='farthest-side') { $radius = max($coords[0], $coords[1], (1-$coords[0]), (1-$coords[1])); }
283
			else { $radius = max($corner1, $corner2, $corner3, $corner4); }	// farthest corner (default)
284
		   }
285
		   // CIRCLE
286
		   else if ($shape=='circle') {
287
		    if ($w >= $h) {
288
			$coords[1] = $coords[3] = ($coords[1] * $h/$w) ;
289
			$corner1 = sqrt(pow($coords[0],2) + pow($coords[1],2));
290
			$corner2 = sqrt(pow($coords[0],2) + pow((($h/$w)-$coords[1]),2));
291
			$corner3 = sqrt(pow((1-$coords[0]),2) + pow($coords[1],2));
292
			$corner4 = sqrt(pow((1-$coords[0]),2) + pow((($h/$w)-$coords[1]),2));
293
			if ($size=='closest-side') { $radius = min($coords[0], $coords[1], (1-$coords[0]), (($h/$w)-$coords[1])); }
294
			else if ($size=='closest-corner') { $radius = min($corner1, $corner2, $corner3, $corner4); }
295
			else if ($size=='farthest-side') { $radius = max($coords[0], $coords[1], (1-$coords[0]), (($h/$w)-$coords[1])); }
296
			else if ($size=='farthest-corner') { $radius = max($corner1, $corner2, $corner3, $corner4); }	// farthest corner (default)
297
			$usew = $useh = $bboxw;
298
			$usey -= ($w-$h);
299
		    }
300
		    else {
301
			$coords[0] = $coords[2] = ($coords[0] * $w/$h) ;
302
			$corner1 = sqrt(pow($coords[0],2) + pow($coords[1],2));
303
			$corner2 = sqrt(pow($coords[0],2) + pow((1-$coords[1]),2));
304
			$corner3 = sqrt(pow((($w/$h)-$coords[0]),2) + pow($coords[1],2));
305
			$corner4 = sqrt(pow((($w/$h)-$coords[0]),2) + pow((1-$coords[1]),2));
306
			if ($size=='closest-side') { $radius = min($coords[0], $coords[1], (($w/$h)-$coords[0]), (1-$coords[1])); }
307
			else if ($size=='closest-corner') { $radius = min($corner1, $corner2, $corner3, $corner4); }
308
			else if ($size=='farthest-side') { $radius = max($coords[0], $coords[1], (($w/$h)-$coords[0]), (1-$coords[1])); }
309
			else if ($size=='farthest-corner') { $radius = max($corner1, $corner2, $corner3, $corner4); }	// farthest corner (default)
310
			$usew = $useh = $bboxh;
311
		    }
312
		   }
313
		   if ($radius==0) { $radius=0.001; }	// to prevent error
314
		   $coords[4] = $radius;
315
		}
316
 
317
		// -moz If entire function consists of only <stop> values
318
		else {	// default values
319
			// All values are set in parseMozGradient - so won't appear here
320
			$coords = array(0.5,0.5,0.5,0.5);	// default for radial gradient (centred)
321
		}
322
		$s = ' q';
323
		$s .= sprintf(' %.3F %.3F %.3F %.3F re W n', $x*_MPDFK, ($this->mpdf->h-$y)*_MPDFK, $w*_MPDFK, -$h*_MPDFK)."\n";
324
		$s .= sprintf(' %.3F 0 0 %.3F %.3F %.3F cm', $usew*_MPDFK, $useh*_MPDFK, $usex*_MPDFK, ($this->mpdf->h-($usey+$useh))*_MPDFK)."\n";
325
	}
326
 
327
	$n = count($this->mpdf->gradients) + 1;
328
	$this->mpdf->gradients[$n]['type'] = $type;
329
	$this->mpdf->gradients[$n]['colorspace'] = $colorspace;
330
	$trans = false;
331
	$this->mpdf->gradients[$n]['is_mask'] = $is_mask;
332
	if ($is_mask) { $trans = true; }
333
	if (count($stops) == 1) { $stops[1] = $stops[0]; }
334
	if (!isset($stops[0]['offset'])) { $stops[0]['offset'] = 0; }
335
	if (!isset($stops[(count($stops)-1)]['offset'])) { $stops[(count($stops)-1)]['offset'] = 1; }
336
 
337
	// Fix stop-offsets set as absolute lengths
338
	if ($type==2) {
339
		$axisx = ($coords[2]-$coords[0])*$usew;
340
		$axisy = ($coords[3]-$coords[1])*$useh;
341
		$axis_length = sqrt(pow($axisx,2) + pow($axisy,2));
342
	}
343
	else { $axis_length = $coords[4]*$usew; }	// Absolute lengths are meaningless for an ellipse - Firefox uses Width as reference
344
 
345
	for($i=0;$i<count($stops);$i++) {
346
	  if (isset($stops[$i]['offset']) && preg_match('/([0-9.]+(px|em|ex|pc|pt|cm|mm|in))/i',$stops[$i]['offset'],$m)) {
347
		$tmp = $this->mpdf->ConvertSize($m[1],$this->mpdf->w,$this->mpdf->FontSize,false);
348
		$stops[$i]['offset'] = $tmp/$axis_length;
349
	  }
350
	}
351
 
352
 
353
	if (isset($stops[0]['offset']) && $stops[0]['offset']>0) {
354
		$firststop = $stops[0];
355
		$firststop['offset'] = 0;
356
		array_unshift($stops, $firststop);
357
	}
358
	if (!$repeat && isset($stops[(count($stops)-1)]['offset']) && $stops[(count($stops)-1)]['offset']<1) {
359
		$endstop = $stops[(count($stops)-1)];
360
		$endstop['offset'] = 1;
361
		$stops[] = $endstop;
362
	}
363
	if ($stops[0]['offset'] > $stops[(count($stops)-1)]['offset']) {
364
		$stops[0]['offset'] = 0;
365
		$stops[(count($stops)-1)]['offset'] = 1;
366
	}
367
 
368
	for($i=0;$i<count($stops);$i++) {
369
		// mPDF 5.3.74
370
		if ($colorspace == 'CMYK') {
371
			$this->mpdf->gradients[$n]['stops'][$i]['col'] = sprintf('%.3F %.3F %.3F %.3F', (ord($stops[$i]['col']{1})/100), (ord($stops[$i]['col']{2})/100), (ord($stops[$i]['col']{3})/100), (ord($stops[$i]['col']{4})/100));
372
		}
373
		else if ($colorspace == 'Gray') {
374
			$this->mpdf->gradients[$n]['stops'][$i]['col'] = sprintf('%.3F', (ord($stops[$i]['col']{1})/255));
375
		}
376
		else {
377
			$this->mpdf->gradients[$n]['stops'][$i]['col'] = sprintf('%.3F %.3F %.3F', (ord($stops[$i]['col']{1})/255), (ord($stops[$i]['col']{2})/255), (ord($stops[$i]['col']{3})/255));
378
		}
379
		if (!isset($stops[$i]['opacity'])) { $stops[$i]['opacity'] = 1; }
380
		else if ($stops[$i]['opacity'] > 1 || $stops[$i]['opacity'] < 0) { $stops[$i]['opacity'] = 1; }
381
		else if ($stops[$i]['opacity'] < 1) {
382
			$trans = true;
383
		}
384
		$this->mpdf->gradients[$n]['stops'][$i]['opacity'] = $stops[$i]['opacity'];
385
		// OFFSET
386
		if ($i>0 && $i<(count($stops)-1)) {
387
		  if (!isset($stops[$i]['offset']) || (isset($stops[$i+1]['offset']) && $stops[$i]['offset']>$stops[$i+1]['offset']) || $stops[$i]['offset']<$stops[$i-1]['offset']) {
388
			if (isset($stops[$i-1]['offset']) && isset($stops[$i+1]['offset'])) {
389
				$stops[$i]['offset'] = ($stops[$i-1]['offset']+$stops[$i+1]['offset'])/2;
390
			}
391
			else {
392
				for($j=($i+1);$j<count($stops);$j++) {
393
					if(isset($stops[$j]['offset'])) { break; }
394
				}
395
				$int = ($stops[$j]['offset'] - $stops[($i-1)]['offset'])/($j-$i+1);
396
				for($f=0;$f<($j-$i-1);$f++) {
397
					$stops[($i+$f)]['offset'] = $stops[($i+$f-1)]['offset'] + ($int);
398
				}
399
			}
400
		  }
401
		}
402
		$this->mpdf->gradients[$n]['stops'][$i]['offset'] = $stops[$i]['offset'];
403
		$this->mpdf->gradients[$n]['stops'][$i]['offset'] = $stops[$i]['offset'];
404
	}
405
 
406
	if ($repeat) {
407
		$ns = count($this->mpdf->gradients[$n]['stops']);
408
		$offs = array();
409
		for($i=0;$i<$ns;$i++) {
410
			$offs[$i] = $this->mpdf->gradients[$n]['stops'][$i]['offset'];
411
		}
412
		$gp = 0;
413
		$inside=true;
414
		while($inside) {
415
		   $gp++;
416
		   for($i=0;$i<$ns;$i++) {
417
			$this->mpdf->gradients[$n]['stops'][(($ns*$gp)+$i)] = $this->mpdf->gradients[$n]['stops'][(($ns*($gp-1))+$i)];
418
			$tmp = $this->mpdf->gradients[$n]['stops'][(($ns*($gp-1))+($ns-1))]['offset']+$offs[$i] ;
419
			if ($tmp < 1) { $this->mpdf->gradients[$n]['stops'][(($ns*$gp)+$i)]['offset'] = $tmp; }
420
			else {
421
				$this->mpdf->gradients[$n]['stops'][(($ns*$gp)+$i)]['offset'] = 1;
422
				$inside = false;
423
				break(2);
424
			}
425
		   }
426
		}
427
	}
428
 
429
	if ($trans) {
430
		$this->mpdf->gradients[$n]['trans'] = true;
431
		$s .= ' /TGS'.$n.' gs ';
432
	}
433
	if (!is_array($extend) || count($extend) <1) {
434
		$extend=array('true', 'true');	// These are supposed to be quoted - appear in PDF file as text
435
	}
436
	$this->mpdf->gradients[$n]['coords'] = $coords;
437
	$this->mpdf->gradients[$n]['extend'] = $extend;
438
	//paint the gradient
439
	$s .= '/Sh'.$n.' sh '."\n";
440
	//restore previous Graphic State
441
	$s .= ' Q '."\n";
442
	if ($return) { return $s; }
443
	else { $this->mpdf->_out($s); }
444
}
445
 
446
 
447
function parseMozGradient($bg) {
448
   //	background[-image]: -moz-linear-gradient(left, #c7Fdde 20%, #FF0000 );
449
   //	background[-image]: linear-gradient(left, #c7Fdde 20%, #FF0000 ); // CSS3
450
   if (preg_match('/repeating-/',$bg)) { $repeat = true; }
451
   else { $repeat = false; }
452
   if (preg_match('/linear-gradient\((.*)\)/',$bg,$m)) {
453
	$g = array();
454
	$g['type'] = 2;
455
	$g['colorspace'] = 'RGB';
456
	$g['extend'] = array('true','true');
457
	$v = trim($m[1]);
458
	// Change commas inside e.g. rgb(x,x,x)
459
	while(preg_match('/(\([^\)]*?),/',$v)) { $v = preg_replace('/(\([^\)]*?),/','\\1@',$v); }
460
	// Remove spaces inside e.g. rgb(x, x, x)
461
	while(preg_match('/(\([^\)]*?)[ ]/',$v)) { $v = preg_replace('/(\([^\)]*?)[ ]/','\\1',$v); }
462
	$bgr = preg_split('/\s*,\s*/',$v);
463
	for($i=0;$i<count($bgr);$i++) { $bgr[$i] = preg_replace('/@/', ',', $bgr[$i]); }
464
	// Is first part $bgr[0] a valid point/angle?
465
	$first = preg_split('/\s+/',trim($bgr[0]));
466
	if (preg_match('/(left|center|right|bottom|top|deg|grad|rad)/i',$bgr[0]) && !preg_match('/(<#|rgb|rgba|hsl|hsla)/i',$bgr[0])) {
467
		$startStops = 1;
468
	}
469
	else if (trim($first[(count($first)-1)]) === "0") {
470
		$startStops = 1;
471
	}
472
	else {
473
		$check = $this->mpdf->ConvertColor($first[0]);
474
		if ($check) $startStops = 0;
475
		else $startStops = 1;
476
	}
477
	// first part a valid point/angle?
478
	if ($startStops == 1) {	// default values
479
		// [<point> || <angle>,] = [<% em px left center right bottom top> || <deg grad rad 0>,]
480
		if (preg_match('/([\-]*[0-9\.]+)(deg|grad|rad)/i',$bgr[0],$m)) {
481
			$angle = $m[1] + 0;
482
			if (strtolower($m[2])=='deg') { $angle = $angle; }
483
			else if (strtolower($m[2])=='grad') { $angle *= (360/400); }
484
			else if (strtolower($m[2])=='rad') { $angle = rad2deg($angle); }
485
			while($angle < 0) { $angle += 360; }
486
			$angle = ($angle % 360);
487
		}
488
		else if (trim($first[(count($first)-1)]) === "0") { $angle = 0; }
489
		if (preg_match('/left/i',$bgr[0])) { $startx = 0; }
490
		else if (preg_match('/right/i',$bgr[0])) { $startx = 1; }
491
		if (preg_match('/top/i',$bgr[0])) { $starty = 1; }
492
		else if (preg_match('/bottom/i',$bgr[0])) { $starty = 0; }
493
		// Check for %? ?% or %%
494
		if (preg_match('/(\d+)[%]/i',$first[0],$m)) { $startx = $m[1]/100; }
495
		else if (!isset($startx) && preg_match('/([0-9.]+(px|em|ex|pc|pt|cm|mm|in))/i',$first[0],$m)) {
496
			$tmp = $this->mpdf->ConvertSize($m[1],$this->mpdf->w,$this->mpdf->FontSize,false);
497
			if ($tmp) { $startx = $m[1]; }
498
		}
499
		if (isset($first[1]) && preg_match('/(\d+)[%]/i',$first[1],$m)) { $starty = 1 - ($m[1]/100); }
500
		else if (!isset($starty) && preg_match('/([0-9.]+(px|em|ex|pc|pt|cm|mm|in))/i',$first[1],$m)) {
501
			$tmp = $this->mpdf->ConvertSize($m[1],$this->mpdf->w,$this->mpdf->FontSize,false);
502
			if ($tmp) { $starty = $m[1]; }
503
		}
504
		if (isset($startx) && !isset($starty)) { $starty = 0.5; }
505
		if (!isset($startx) && isset($starty)) { $startx = 0.5; }
506
 
507
	}
508
	// If neither a <point> or <angle> is specified, i.e. the entire function consists of only <stop> values, the gradient axis starts from the top of the box and runs vertically downwards, ending at the bottom of the box.
509
	else {	// default values T2B
510
		$starty = 1; $startx = 0.5;
511
		$endy = 0; $endx = 0.5;
512
	}
513
	$coords = array();
514
	if (!isset($startx)) { $startx = false; }
515
	if (!isset($starty)) { $starty = false; }
516
	if (!isset($endx)) { $endx = false; }
517
	if (!isset($endy)) { $endy = false; }
518
	if (!isset($angle)) { $angle = false; }
519
	$g['coords'] = array($startx ,$starty ,$endx ,$endy, $angle, $repeat );
520
	$g['stops'] = array();
521
	for($i=$startStops;$i<count($bgr);$i++) {
522
		$stop = array();
523
		// parse stops
524
		$el = preg_split('/\s+/',trim($bgr[$i]));
525
		// mPDF 5.3.74
526
		$col = $this->mpdf->ConvertColor($el[0]);
527
		if ($col) { $stop['col'] = $col; }
528
		else { $stop['col'] = $col = $this->mpdf->ConvertColor(255); }
529
		if ($col{0}==1) $g['colorspace'] = 'Gray';
530
		else if ($col{0}==4 || $col{0}==6) $g['colorspace'] = 'CMYK';
531
		if ($col{0}==5) { $stop['opacity'] = ord($col{4})/100; }	// transparency from rgba()
532
		else if ($col{0}==6) { $stop['opacity'] = ord($col{5})/100; }	// transparency from cmyka()
533
		else if ($col{0}==1 && $col{2}==1) { $stop['opacity'] = ord($col{3})/100; }	// transparency converted from rgba or cmyka()
534
 
535
		if (isset($el[1]) && preg_match('/(\d+)[%]/',$el[1],$m)) {
536
			$stop['offset'] = $m[1]/100;
537
			if ($stop['offset']>1) { unset($stop['offset']); }
538
		}
539
		else if (isset($el[1]) && preg_match('/([0-9.]+(px|em|ex|pc|pt|cm|mm|in))/i',$el[1],$m)) {
540
			$tmp = $this->mpdf->ConvertSize($m[1],$this->mpdf->w,$this->mpdf->FontSize,false);
541
			if ($tmp) { $stop['offset'] = $m[1]; }
542
		}
543
		$g['stops'][] = $stop;
544
	}
545
	if (count($g['stops'] )) { return $g; }
546
   }
547
   else if (preg_match('/radial-gradient\((.*)\)/',$bg,$m)) {
548
	$g = array();
549
	$g['type'] = 3;
550
	$g['colorspace'] = 'RGB';
551
	$g['extend'] = array('true','true');
552
	$v = trim($m[1]);
553
	// Change commas inside e.g. rgb(x,x,x)
554
	while(preg_match('/(\([^\)]*?),/',$v)) { $v = preg_replace('/(\([^\)]*?),/','\\1@',$v); }
555
	// Remove spaces inside e.g. rgb(x, x, x)
556
	while(preg_match('/(\([^\)]*?)[ ]/',$v)) { $v = preg_replace('/(\([^\)]*?)[ ]/','\\1',$v); }
557
	$bgr = preg_split('/\s*,\s*/',$v);
558
	for($i=0;$i<count($bgr);$i++) { $bgr[$i] = preg_replace('/@/', ',', $bgr[$i]); }
559
 
560
	// Is first part $bgr[0] a valid point/angle?
561
	$startStops = 0;
562
	$pos_angle = false;
563
	$shape_size = false;
564
	$first = preg_split('/\s+/',trim($bgr[0]));
565
	$checkCol = $this->mpdf->ConvertColor($first[0]);
566
	if (preg_match('/(left|center|right|bottom|top|deg|grad|rad)/i',$bgr[0]) && !preg_match('/(<#|rgb|rgba|hsl|hsla)/i',$bgr[0])) {
567
		$startStops=1;
568
		$pos_angle = $bgr[0];
569
	}
570
	else if (trim($first[(count($first)-1)]) === "0") {
571
		$startStops=1;
572
		$pos_angle = $bgr[0];
573
	}
574
	else if (preg_match('/(circle|ellipse|closest-side|closest-corner|farthest-side|farthest-corner|contain|cover)/i',$bgr[0])) {
575
		$startStops=1;
576
		$shape_size = $bgr[0];
577
	}
578
	else if (!$checkCol) {
579
		$startStops=1;
580
 		$pos_angle = $bgr[0];
581
	}
582
	if (preg_match('/(circle|ellipse|closest-side|closest-corner|farthest-side|farthest-corner|contain|cover)/i',$bgr[1])) {
583
		$startStops=2;
584
		$shape_size = $bgr[1];
585
	}
586
 
587
	// If valid point/angle?
588
	if ($pos_angle) {	// default values
589
		// [<point> || <angle>,] = [<% em px left center right bottom top> || <deg grad rad 0>,]
590
		if (preg_match('/left/i',$pos_angle)) { $startx = 0; }
591
		else if (preg_match('/right/i',$pos_angle)) { $startx = 1; }
592
		if (preg_match('/top/i',$pos_angle)) { $starty = 1; }
593
		else if (preg_match('/bottom/i',$pos_angle)) { $starty = 0; }
594
		// Check for %? ?% or %%
595
		if (preg_match('/(\d+)[%]/i',$first[0],$m)) { $startx = $m[1]/100; }
596
		else if (!isset($startx) && preg_match('/([0-9.]+(px|em|ex|pc|pt|cm|mm|in))/i',$first[0],$m)) {
597
			$tmp = $this->mpdf->ConvertSize($m[1],$this->mpdf->w,$this->mpdf->FontSize,false);
598
			if ($tmp) { $startx = $m[1]; }
599
		}
600
		if (isset($first[1]) && preg_match('/(\d+)[%]/i',$first[1],$m)) { $starty = 1 - ($m[1]/100); }
601
		else if (!isset($starty) && preg_match('/([0-9.]+(px|em|ex|pc|pt|cm|mm|in))/i',$first[1],$m)) {
602
			$tmp = $this->mpdf->ConvertSize($m[1],$this->mpdf->w,$this->mpdf->FontSize,false);
603
			if ($tmp) { $starty = $m[1]; }
604
		}
605
 
606
/*
607
		// ?? Angle has no effect in radial gradient (does not exist in CSS3 spec.)
608
		if (preg_match('/([\-]*[0-9\.]+)(deg|grad|rad)/i',$pos_angle,$m)) {
609
			$angle = $m[1] + 0;
610
			if (strtolower($m[2])=='deg') { $angle = $angle; }
611
			else if (strtolower($m[2])=='grad') { $angle *= (360/400); }
612
			else if (strtolower($m[2])=='rad') { $angle = rad2deg($angle); }
613
			while($angle < 0) { $angle += 360; }
614
			$angle = ($angle % 360);
615
		}
616
*/
617
		if (!isset($starty)) { $starty = 0.5; }
618
		if (!isset($startx)) { $startx = 0.5; }
619
 
620
	}
621
	// If neither a <point> or <angle> is specified, i.e. the entire function consists of only <stop> values, the gradient axis starts from the top of the box and runs vertically downwards, ending at the bottom of the box.
622
	else {	// default values Center
623
		$starty = 0.5; $startx = 0.5;
624
		$endy = 0.5; $endx = 0.5;
625
	}
626
 
627
	// If valid shape/size?
628
	$shape = 'ellipse';	// default
629
	$size = 'farthest-corner';	// default
630
	if ($shape_size) {	// default values
631
		if (preg_match('/(circle|ellipse)/i',$shape_size, $m)) {
632
			$shape = $m[1];
633
		}
634
		if (preg_match('/(closest-side|closest-corner|farthest-side|farthest-corner|contain|cover)/i',$shape_size, $m)) {
635
			$size = $m[1];
636
			if ($size=='contain') { $size = 'closest-side'; }
637
			else if ($size=='cover') { $size = 'farthest-corner'; }
638
		}
639
	}
640
 
641
	$coords = array();
642
	if (!isset($startx)) { $startx = false; }
643
	if (!isset($starty)) { $starty = false; }
644
	if (!isset($endx)) { $endx = false; }
645
	if (!isset($endy)) { $endy = false; }
646
	if (!isset($radius)) { $radius = false; }
647
	if (!isset($angle)) { $angle = 0; }
648
	$g['coords'] = array($startx ,$starty ,$endx ,$endy, $radius, $angle, $shape, $size, $repeat );
649
 
650
	$g['stops'] = array();
651
	for($i=$startStops;$i<count($bgr);$i++) {
652
		$stop = array();
653
		// parse stops
654
		$el = preg_split('/\s+/',trim($bgr[$i]));
655
		// mPDF 5.3.74
656
		$col = $this->mpdf->ConvertColor($el[0]);
657
		if ($col) { $stop['col'] = $col; }
658
		else { $stop['col'] = $col = $this->mpdf->ConvertColor(255); }
659
		if ($col{0}==1) $g['colorspace'] = 'Gray';
660
		else if ($col{0}==4 || $col{0}==6) $g['colorspace'] = 'CMYK';
661
		if ($col{0}==5) { $stop['opacity'] = ord($col{4})/100; }	// transparency from rgba()
662
		else if ($col{0}==6) { $stop['opacity'] = ord($col{5})/100; }	// transparency from cmyka()
663
		else if ($col{0}==1 && $col{2}==1) { $stop['opacity'] = ord($col{3})/100; }	// transparency converted from rgba or cmyka()
664
 
665
		if (isset($el[1]) && preg_match('/(\d+)[%]/',$el[1],$m)) {
666
			$stop['offset'] = $m[1]/100;
667
			if ($stop['offset']>1) { unset($stop['offset']); }
668
		}
669
		else if (isset($el[1]) && preg_match('/([0-9.]+(px|em|ex|pc|pt|cm|mm|in))/i',$el[1],$m)) {
670
			$tmp = $this->mpdf->ConvertSize($m[1],$this->mpdf->w,$this->mpdf->FontSize,false);
671
			$stop['offset'] = $el[1];
672
		}
673
		$g['stops'][] = $stop;
674
	}
675
	if (count($g['stops'] )) { return $g; }
676
   }
677
   return array();
678
}
679
 
680
function parseBackgroundGradient($bg) {
681
	// background-gradient: linear #00FFFF #FFFF00 0 0.5 1 0.5;  or
682
	// background-gradient: radial #00FFFF #FFFF00 0.5 0.5 1 1 1.2;
683
 
684
	$v = trim($bg);
685
	$bgr = preg_split('/\s+/',$v);
686
	$g = array();
687
	if (count($bgr)> 6) {
688
		if (strtoupper(substr($bgr[0],0,1)) == 'L' && count($bgr)==7) {  // linear
689
			$g['type'] = 2;
690
			//$coords = array(0,0,1,1 );	// 0 0 1 0 or 0 1 1 1 is L 2 R; 1,1,0,1 is R2L; 1,1,1,0 is T2B; 1,0,1,1 is B2T
691
			// Linear: $coords - array of the form (x1, y1, x2, y2) which defines the gradient vector (see linear_gradient_coords.jpg).
692
			//    The default value is from left to right (x1=0, y1=0, x2=1, y2=0).
693
			$g['coords'] = array($bgr[3], $bgr[4], $bgr[5], $bgr[6]);
694
		}
695
		else if (count($bgr)==8) {	// radial
696
			$g['type'] = 3;
697
			// Radial: $coords - array of the form (fx, fy, cx, cy, r) where (fx, fy) is the starting point of the gradient with color1,
698
			//    (cx, cy) is the center of the circle with color2, and r is the radius of the circle (see radial_gradient_coords.jpg).
699
			//    (fx, fy) should be inside the circle, otherwise some areas will not be defined
700
			$g['coords'] = array($bgr[3], $bgr[4], $bgr[5], $bgr[6], $bgr[7]);
701
		}
702
		$g['colorspace'] = 'RGB';
703
		// mPDF 5.3.74
704
		$cor = $this->mpdf->ConvertColor($bgr[1]);
705
		if ($cor{0}==1) $g['colorspace'] = 'Gray';
706
		else if ($cor{0}==4 || $cor{0}==6) $g['colorspace'] = 'CMYK';
707
		if ($cor) { $g['col'] = $cor; }
708
		else { $g['col'] = $this->mpdf->ConvertColor(255); }
709
		$cor = $this->mpdf->ConvertColor($bgr[2]);
710
		if ($cor) { $g['col2'] = $cor; }
711
		else { $g['col2'] = $this->mpdf->ConvertColor(255); }
712
		$g['extend'] = array('true','true');
713
		$g['stops'] = array(array('col'=>$g['col'], 'opacity'=>1, 'offset'=>0), array('col'=>$g['col2'], 'opacity'=>1, 'offset'=>1));
714
		return $g;
715
	}
716
	return false;
717
}
718
 
719
 
720
 
721
}
722
 
723
?>