grad.php 32 KB

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