PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/graph-engine/ps.php

https://gitlab.com/ElvisAns/tiki
PHP | 476 lines | 405 code | 51 blank | 20 comment | 17 complexity | d8150b6607a61395b356bd187b751be0 MD5 | raw file
  1. <?php
  2. // (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
  3. //
  4. // All Rights Reserved. See copyright.txt for details and a complete list of authors.
  5. // Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
  6. // $Id$
  7. /* This library is LGPL
  8. * written by Louis-Philippe Huberdeau
  9. *
  10. * vim: fdm=marker tabstop=4 shiftwidth=4 noet:
  11. *
  12. * This file contains the PostScript graphic renderer. (Using PSLib)
  13. */
  14. require_once('lib/graph-engine/core.php');
  15. class PS_GRenderer extends GRenderer // {{{1
  16. {
  17. public $ps;
  18. public $styles;
  19. public $font;
  20. public $width;
  21. public $height;
  22. public function __construct($format = null, $orientation = 'landscape') // {{{2
  23. {
  24. // Null size does not create a graphic.
  25. $this->styles = [];
  26. $this->font = null;
  27. if (! is_null($format)) {
  28. $size = $this->_getFormat($format, $orientation);
  29. $this->width = $size[0];
  30. $this->height = $size[1];
  31. $this->ps = ps_new();
  32. ps_open_file($this->ps, '');
  33. ps_begin_page($this->ps, $this->width, $this->height);
  34. $this->font = ps_findfont($this->ps, 'Helvetica', '', 0);
  35. }
  36. }
  37. public function addLink($target, $left, $top, $right, $bottom, $title = null) // {{{2
  38. {
  39. }
  40. public function drawLine($x1, $y1, $x2, $y2, $style) // {{{2
  41. {
  42. $this->_convertPosition($x1, $y1);
  43. $this->_convertPosition($x2, $y2);
  44. ps_setcolor(
  45. $this->ps,
  46. 'stroke',
  47. $style['line'][0],
  48. $style['line'][1],
  49. $style['line'][2],
  50. $style['line'][3],
  51. $style['line'][4]
  52. );
  53. ps_setlinewidth($this->ps, $style['line-width']);
  54. ps_moveto($this->ps, $x1, $y1);
  55. ps_lineto($this->ps, $x2, $y2);
  56. ps_stroke($this->ps);
  57. }
  58. public function drawRectangle($left, $top, $right, $bottom, $style) // {{{2
  59. {
  60. $this->_convertPosition($left, $top);
  61. $this->_convertPosition($right, $bottom);
  62. ps_setcolor(
  63. $this->ps,
  64. 'stroke',
  65. $style['line'][0],
  66. $style['line'][1],
  67. $style['line'][2],
  68. $style['line'][3],
  69. $style['line'][4]
  70. );
  71. if (isset($style['fill'])) {
  72. ps_setcolor(
  73. $this->ps,
  74. 'fill',
  75. $style['fill'][0],
  76. $style['fill'][1],
  77. $style['fill'][2],
  78. $style['fill'][3],
  79. $style['fill'][4]
  80. );
  81. }
  82. ps_setlinewidth($this->ps, $style['line-width']);
  83. ps_rect($this->ps, $left, $top, $right - $left, $bottom - $top);
  84. if (isset($style['fill'])) {
  85. ps_fill_stroke($this->ps);
  86. } else {
  87. ps_stroke($this->ps);
  88. }
  89. }
  90. public function drawPie($centerX, $centerY, $radius, $begin, $end, $style) // {{{2
  91. {
  92. $this->_convertPosition($centerX, $centerY);
  93. $radius = $radius * min($this->width, $this->height);
  94. ps_setcolor(
  95. $this->ps,
  96. 'stroke',
  97. $style['line'][0],
  98. $style['line'][1],
  99. $style['line'][2],
  100. $style['line'][3],
  101. $style['line'][4]
  102. );
  103. if (isset($style['fill'])) {
  104. ps_setcolor(
  105. $this->ps,
  106. 'fill',
  107. $style['fill'][0],
  108. $style['fill'][1],
  109. $style['fill'][2],
  110. $style['fill'][3],
  111. $style['fill'][4]
  112. );
  113. }
  114. ps_setlinewidth($this->ps, $style['line-width']);
  115. ps_moveto($this->ps, $centerX, $centerY);
  116. ps_arc($this->ps, $centerX, $centerY, $radius, $begin, $end);
  117. ps_lineto($this->ps, $centerX, $centerY);
  118. ps_closepath($this->ps);
  119. if (isset($style['fill'])) {
  120. ps_fill_stroke($this->ps);
  121. } else {
  122. ps_stroke($this->ps);
  123. }
  124. }
  125. public function drawText($text, $left, $right, $height, $style) // {{{2
  126. {
  127. $h = $height; // Creating duplicate (temp)
  128. $this->_convertPosition($left, $height);
  129. $this->_convertPosition($right, $h);
  130. ps_setcolor(
  131. $this->ps,
  132. 'fill',
  133. $style['fill'][0],
  134. $style['fill'][1],
  135. $style['fill'][2],
  136. $style['fill'][3],
  137. $style['fill'][4]
  138. );
  139. ps_setfont($this->ps, $this->font, $style['font']);
  140. ps_show_boxed($this->ps, $text, $left, $height - $style['font'], $right - $left, $style['font'], $style['align'], '');
  141. }
  142. public function getTextWidth($text, $style) // {{{2
  143. {
  144. return ps_stringwidth($this->ps, $text, $this->font, $style['font']) / $this->width;
  145. }
  146. public function getTextHeight($style) // {{{2
  147. {
  148. return $style['font'] / $this->height;
  149. }
  150. public function getStyle($name) // {{{2
  151. {
  152. if (isset($this->styles[$name])) {
  153. return $this->styles[$name];
  154. }
  155. return $this->styles[$name] = $this->_findStyle($name);
  156. }
  157. public function httpOutput($filename) // {{{2
  158. {
  159. ps_end_page($this->ps);
  160. ps_close($this->ps);
  161. $buf = ps_get_buffer($this->ps);
  162. $len = strlen($buf);
  163. header("Content-type: application/ps");
  164. header("Content-Length: $len");
  165. header("Content-Disposition: inline; filename=$name");
  166. echo $buf;
  167. ps_delete($this->ps);
  168. }
  169. public function writeToStream($stream) // {{{2
  170. {
  171. ps_end_page($this->ps);
  172. ps_close($this->ps);
  173. $buf = ps_get_buffer($this->ps);
  174. fwrite($stream, $buf);
  175. ps_delete($this->ps);
  176. }
  177. public function _convertLength($value, $type) // {{{2
  178. {
  179. // $type is either 'width' or 'height'
  180. // $value is a 0-1 float
  181. return floor($value * $this->$type);
  182. }
  183. public function _convertPosition(&$x, &$y) // {{{2
  184. {
  185. // Parameters passed by ref!
  186. $x = $this->_convertLength($x, 'width');
  187. $y = $this->height - $this->_convertLength($y, 'height');
  188. }
  189. public function _findStyle($name) // {{{2
  190. {
  191. $parts = explode('-', $name);
  192. $style = [];
  193. switch ($parts[0]) {
  194. case 'Thin':
  195. $style['line-width'] = 1;
  196. array_shift($parts);
  197. break;
  198. case 'Bold':
  199. $style['line-width'] = 2;
  200. array_shift($parts);
  201. break;
  202. case 'Bolder':
  203. $style['line-width'] = 3;
  204. array_shift($parts);
  205. break;
  206. case 'Large':
  207. $style['font'] = 16;
  208. array_shift($parts);
  209. break;
  210. case 'Small':
  211. $style['font'] = 8;
  212. array_shift($parts);
  213. break;
  214. case 'Normal':
  215. array_shift($parts);
  216. break;
  217. default:
  218. if ($parts[0] == 'Text') {
  219. $style['font'] = 12;
  220. } else {
  221. $style['line-width'] = 1;
  222. }
  223. break;
  224. }
  225. switch ($parts[0]) {
  226. case 'LineStroke':
  227. $style['line'] = $this->_getColor($parts[1]);
  228. break;
  229. case 'FillStroke':
  230. $style['fill'] = $this->_getColor($parts[1]);
  231. $style['line'] = $this->_getColor('Black');
  232. break;
  233. case 'Text':
  234. $style['fill'] = $this->_getColor('Black');
  235. switch ($parts[1]) {
  236. case 'Center':
  237. $style['align'] = 'center';
  238. break;
  239. case 'Right':
  240. $style['align'] = 'right';
  241. break;
  242. case 'Left':
  243. default:
  244. $style['align'] = 'left';
  245. break;
  246. }
  247. break;
  248. default:
  249. return GRenderer::getStyle($name);
  250. }
  251. return $style;
  252. }
  253. public function _getColor($name) // {{{2
  254. {
  255. $c = [ 'rgb' ];
  256. $color = $this->_getRawColor(strtolower($name));
  257. foreach ($color as $col) {
  258. $c[] = $col / 255;
  259. }
  260. $c[] = null;
  261. return $c;
  262. }
  263. public function _getFormat($format, $orientation) // {{{2
  264. {
  265. /*
  266. Taken from lib/pdflib/class.ezpdf.php
  267. Copyright notices are in that file.
  268. */
  269. switch (strtoupper($format)) {
  270. case '4A0':
  271. $size = [4767.87,6740.79];
  272. break;
  273. case '2A0':
  274. $size = [3370.39,4767.87];
  275. break;
  276. case 'A0':
  277. $size = [2383.94,3370.39];
  278. break;
  279. case 'A1':
  280. $size = [1683.78,2383.94];
  281. break;
  282. case 'A2':
  283. $size = [1190.55,1683.78];
  284. break;
  285. case 'A3':
  286. $size = [841.89,1190.55];
  287. break;
  288. case 'A4':
  289. $size = [595.28,841.89];
  290. break;
  291. case 'A5':
  292. $size = [419.53,595.28];
  293. break;
  294. case 'A6':
  295. $size = [297.64,419.53];
  296. break;
  297. case 'A7':
  298. $size = [209.76,297.64];
  299. break;
  300. case 'A8':
  301. $size = [147.40,209.76];
  302. break;
  303. case 'A9':
  304. $size = [104.88,147.40];
  305. break;
  306. case 'A10':
  307. $size = [73.70,104.88];
  308. break;
  309. case 'B0':
  310. $size = [2834.65,4008.19];
  311. break;
  312. case 'B1':
  313. $size = [2004.09,2834.65];
  314. break;
  315. case 'B2':
  316. $size = [1417.32,2004.09];
  317. break;
  318. case 'B3':
  319. $size = [1000.63,1417.32];
  320. break;
  321. case 'B4':
  322. $size = [708.66,1000.63];
  323. break;
  324. case 'B5':
  325. $size = [498.90,708.66];
  326. break;
  327. case 'B6':
  328. $size = [354.33,498.90];
  329. break;
  330. case 'B7':
  331. $size = [249.45,354.33];
  332. break;
  333. case 'B8':
  334. $size = [175.75,249.45];
  335. break;
  336. case 'B9':
  337. $size = [124.72,175.75];
  338. break;
  339. case 'B10':
  340. $size = [87.87,124.72];
  341. break;
  342. case 'C0':
  343. $size = [2599.37,3676.54];
  344. break;
  345. case 'C1':
  346. $size = [1836.85,2599.37];
  347. break;
  348. case 'C2':
  349. $size = [1298.27,1836.85];
  350. break;
  351. case 'C3':
  352. $size = [918.43,1298.27];
  353. break;
  354. case 'C4':
  355. $size = [649.13,918.43];
  356. break;
  357. case 'C5':
  358. $size = [459.21,649.13];
  359. break;
  360. case 'C6':
  361. $size = [323.15,459.21];
  362. break;
  363. case 'C7':
  364. $size = [229.61,323.15];
  365. break;
  366. case 'C8':
  367. $size = [161.57,229.61];
  368. break;
  369. case 'C9':
  370. $size = [113.39,161.57];
  371. break;
  372. case 'C10':
  373. $size = [79.37,113.39];
  374. break;
  375. case 'RA0':
  376. $size = [2437.80,3458.27];
  377. break;
  378. case 'RA1':
  379. $size = [1729.13,2437.80];
  380. break;
  381. case 'RA2':
  382. $size = [1218.90,1729.13];
  383. break;
  384. case 'RA3':
  385. $size = [864.57,1218.90];
  386. break;
  387. case 'RA4':
  388. $size = [609.45,864.57];
  389. break;
  390. case 'SRA0':
  391. $size = [2551.18,3628.35];
  392. break;
  393. case 'SRA1':
  394. $size = [1814.17,2551.18];
  395. break;
  396. case 'SRA2':
  397. $size = [1275.59,1814.17];
  398. break;
  399. case 'SRA3':
  400. $size = [907.09,1275.59];
  401. break;
  402. case 'SRA4':
  403. $size = [637.80,907.09];
  404. break;
  405. case 'LETTER':
  406. $size = [612.00,792.00];
  407. break;
  408. case 'LEGAL':
  409. $size = [612.00,1008.00];
  410. break;
  411. case 'EXECUTIVE':
  412. $size = [521.86,756.00];
  413. break;
  414. case 'FOLIO':
  415. $size = [612.00,936.00];
  416. break;
  417. }
  418. if (strtolower($orientation) == 'landscape') {
  419. $a = $size[1];
  420. $size[1] = $size[0];
  421. $size[0] = $a;
  422. }
  423. return $size;
  424. }
  425. }