PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/pgfouine-1.2/include/reporting/artichow/php4/inc/Label.class.php

#
PHP | 615 lines | 235 code | 113 blank | 267 comment | 29 complexity | 1812f62838dd3b0da9859b8bbca3671f MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0
  1. <?php
  2. /*
  3. * This work is hereby released into the Public Domain.
  4. * To view a copy of the public domain dedication,
  5. * visit http://creativecommons.org/licenses/publicdomain/ or send a letter to
  6. * Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
  7. *
  8. */
  9. /* <php4> */
  10. define("LABEL_LEFT", 1);
  11. define("LABEL_RIGHT", 2);
  12. define("LABEL_CENTER", 3);
  13. define("LABEL_TOP", 4);
  14. define("LABEL_BOTTOM", 5);
  15. define("LABEL_MIDDLE", 6);
  16. /* </php4> */
  17. /**
  18. * Draw labels
  19. *
  20. * @package Artichow
  21. */
  22. class awLabel {
  23. /**
  24. * Label border
  25. *
  26. * @var int
  27. */
  28. var $border;
  29. /**
  30. * Label texts
  31. *
  32. * @var array
  33. */
  34. var $texts;
  35. /**
  36. * Text font
  37. *
  38. * @var int
  39. */
  40. var $font;
  41. /**
  42. * Text angle
  43. *
  44. * @var int
  45. */
  46. var $angle = 0;
  47. /**
  48. * Text color
  49. *
  50. * @var Color
  51. */
  52. var $color;
  53. /**
  54. * Text background
  55. *
  56. * @var Color, Gradient
  57. */
  58. var $background;
  59. /**
  60. * Callback function
  61. *
  62. * @var string
  63. */
  64. var $function;
  65. /**
  66. * Callback function to format output
  67. *
  68. * @var string
  69. */
  70. var $formatFunction;
  71. /**
  72. * Padding
  73. *
  74. * @var int
  75. */
  76. var $padding;
  77. /**
  78. * Move position from this vector
  79. *
  80. * @var Point
  81. */
  82. var $move;
  83. /**
  84. * Label interval
  85. *
  86. * @var int
  87. */
  88. var $interval = 1;
  89. /**
  90. * Horizontal align
  91. *
  92. * @var int
  93. */
  94. var $hAlign = LABEL_CENTER;
  95. /**
  96. * Vertical align
  97. *
  98. * @var int
  99. */
  100. var $vAlign = LABEL_MIDDLE;
  101. /**
  102. * Hide all labels ?
  103. *
  104. * @var bool
  105. */
  106. var $hide = FALSE;
  107. /**
  108. * Keys to hide
  109. *
  110. * @var array
  111. */
  112. var $hideKey = array();
  113. /**
  114. * Values to hide
  115. *
  116. * @var array
  117. */
  118. var $hideValue = array();
  119. /**
  120. * Hide first label
  121. *
  122. * @var bool
  123. */
  124. var $hideFirst = FALSE;
  125. /**
  126. * Hide last label
  127. *
  128. * @var bool
  129. */
  130. var $hideLast = FALSE;
  131. /**
  132. * Build the label
  133. *
  134. * @param string $label First label
  135. */
  136. function awLabel($label = NULL, $font = NULL, $color = NULL, $angle = 0) {
  137. if(is_array($label)) {
  138. $this->set($label);
  139. } else if(is_string($label)) {
  140. $this->set(array($label));
  141. }
  142. if($font === NULL) {
  143. $font = new awFont2;
  144. }
  145. $this->setFont($font);
  146. $this->setAngle($angle);
  147. if(is_a($color, 'awColor')) {
  148. $this->setColor($color);
  149. } else {
  150. $this->setColor(new awColor(0, 0, 0));
  151. }
  152. $this->move = new awPoint(0, 0);
  153. $this->border = new awBorder;
  154. $this->border->hide();
  155. }
  156. /**
  157. * Get an element of the label from its key
  158. *
  159. * @param int $key Element key
  160. * @return string A value
  161. */
  162. function get($key) {
  163. return array_key_exists($key, $this->texts) ? $this->texts[$key] : NULL;
  164. }
  165. /**
  166. * Get all labels
  167. *
  168. * @return array
  169. */
  170. function all() {
  171. return $this->texts;
  172. }
  173. /**
  174. * Set one or several labels
  175. *
  176. * @param array $labels Array of string or a string
  177. */
  178. function set($labels) {
  179. if(is_string($labels)) {
  180. $this->texts = array($labels);
  181. } else if(is_array($labels)) {
  182. $this->texts = $labels;
  183. }
  184. }
  185. /**
  186. * Count number of texts in the label
  187. *
  188. * @return int
  189. */
  190. function count() {
  191. return is_array($this->texts) ? count($this->texts) : 0;
  192. }
  193. /**
  194. * Set a callback function for labels
  195. *
  196. * @param string $function
  197. */
  198. function setCallbackFunction($function) {
  199. $this->function = is_null($function) ? $function : (string)$function;
  200. }
  201. /**
  202. * Return the callback function for labels
  203. *
  204. * @return string
  205. */
  206. function getCallbackFunction() {
  207. return $this->function;
  208. }
  209. /**
  210. * Set a callback function to format the labels
  211. *
  212. * @param string $function
  213. */
  214. function setCallbackFormatFunction($function) {
  215. $this->formatFunction =& $function;
  216. }
  217. /**
  218. * Change labels format
  219. *
  220. * @param string $format New format (printf style: %.2f for example)
  221. */
  222. function setFormat($format) {
  223. $function = 'label'.time().'_'.(microtime() * 1000000);
  224. eval('function '.$function.'($value) {
  225. return sprintf("'.addcslashes($format, '"').'", $value);
  226. }');
  227. $this->setCallbackFunction($function);
  228. }
  229. /**
  230. * Change font for label
  231. *
  232. * @param &$font New font
  233. * @param $color Font color (can be NULL)
  234. */
  235. function setFont(&$font, $color = NULL) {
  236. $this->font = $font;
  237. if(is_a($color, 'awColor')) {
  238. $this->setColor($color);
  239. }
  240. }
  241. /**
  242. * Change font angle
  243. *
  244. * @param int $angle New angle
  245. */
  246. function setAngle($angle) {
  247. $this->angle = (int)$angle;
  248. }
  249. /**
  250. * Change font color
  251. *
  252. * @param $color
  253. */
  254. function setColor($color) {
  255. $this->color = $color;
  256. }
  257. /**
  258. * Change text background
  259. *
  260. * @param mixed $background
  261. */
  262. function setBackground($background) {
  263. $this->background = $background;
  264. }
  265. /**
  266. * Change text background color
  267. *
  268. * @param Color
  269. */
  270. function setBackgroundColor($color) {
  271. $this->background = $color;
  272. }
  273. /**
  274. * Change text background gradient
  275. *
  276. * @param Gradient
  277. */
  278. function setBackgroundGradient($gradient) {
  279. $this->background = $gradient;
  280. }
  281. /**
  282. * Change padding
  283. *
  284. * @param int $left Left padding
  285. * @param int $right Right padding
  286. * @param int $top Top padding
  287. * @param int $bottom Bottom padding
  288. */
  289. function setPadding($left, $right, $top, $bottom) {
  290. $this->padding = array((int)$left, (int)$right, (int)$top, (int)$bottom);
  291. }
  292. /**
  293. * Hide all labels ?
  294. *
  295. * @param bool $hide
  296. */
  297. function hide($hide = TRUE) {
  298. $this->hide = (bool)$hide;
  299. }
  300. /**
  301. * Show all labels ?
  302. *
  303. * @param bool $show
  304. */
  305. function show($show = TRUE) {
  306. $this->hide = (bool)!$show;
  307. }
  308. /**
  309. * Hide a key
  310. *
  311. * @param int $key The key to hide
  312. */
  313. function hideKey($key) {
  314. $this->hideKey[$key] = TRUE;
  315. }
  316. /**
  317. * Hide a value
  318. *
  319. * @param int $value The value to hide
  320. */
  321. function hideValue($value) {
  322. $this->hideValue[] = $value;
  323. }
  324. /**
  325. * Hide first label
  326. *
  327. * @param bool $hide
  328. */
  329. function hideFirst($hide) {
  330. $this->hideFirst = (bool)$hide;
  331. }
  332. /**
  333. * Hide last label
  334. *
  335. * @param bool $hide
  336. */
  337. function hideLast($hide) {
  338. $this->hideLast = (bool)$hide;
  339. }
  340. /**
  341. * Set label interval
  342. *
  343. * @param int
  344. */
  345. function setInterval($interval) {
  346. $this->interval = (int)$interval;
  347. }
  348. /**
  349. * Change label position
  350. *
  351. * @param int $x Add this interval to X coord
  352. * @param int $y Add this interval to Y coord
  353. */
  354. function move($x, $y) {
  355. $this->move = $this->move->move($x, $y);
  356. }
  357. /**
  358. * Change alignment
  359. *
  360. * @param int $h Horizontal alignment
  361. * @param int $v Vertical alignment
  362. */
  363. function setAlign($h = NULL, $v = NULL) {
  364. if($h !== NULL) {
  365. $this->hAlign = (int)$h;
  366. }
  367. if($v !== NULL) {
  368. $this->vAlign = (int)$v;
  369. }
  370. }
  371. /**
  372. * Get a text from the labele
  373. *
  374. * @param mixed $key Key in the array text
  375. * @return Text
  376. */
  377. function getText($key) {
  378. if(is_array($this->texts) and array_key_exists($key, $this->texts)) {
  379. $value = $this->texts[$key];
  380. if(is_string($this->function)) {
  381. $value = call_user_func($this->function, $value);
  382. }
  383. if(!is_null($this->formatFunction)) {
  384. $value = call_user_func($this->formatFunction, $value);
  385. }
  386. $text = new awText($value);
  387. $text->setFont($this->font);
  388. $text->setAngle($this->angle);
  389. $text->setColor($this->color);
  390. if(is_a($this->background, 'awColor')) {
  391. $text->setBackgroundColor($this->background);
  392. } else if(is_a($this->background, 'awGradient')) {
  393. $text->setBackgroundGradient($this->background);
  394. }
  395. $text->border = $this->border;
  396. if($this->padding !== NULL) {
  397. call_user_func_array(array(&$text, 'setPadding'), $this->padding);
  398. }
  399. return $text;
  400. } else {
  401. return NULL;
  402. }
  403. }
  404. /**
  405. * Get max width of all texts
  406. *
  407. * @param $drawer A drawer
  408. * @return int
  409. */
  410. function getMaxWidth($drawer) {
  411. return $this->getMax($drawer, 'getTextWidth');
  412. }
  413. /**
  414. * Get max height of all texts
  415. *
  416. * @param $drawer A drawer
  417. * @return int
  418. */
  419. function getMaxHeight($drawer) {
  420. return $this->getMax($drawer, 'getTextHeight');
  421. }
  422. /**
  423. * Draw the label
  424. *
  425. * @param $drawer
  426. * @param $p Label center
  427. * @param int $key Text position in the array of texts (default to zero)
  428. */
  429. function draw($drawer, $p, $key = 0) {
  430. if(($key % $this->interval) !== 0) {
  431. return;
  432. }
  433. // Hide all labels
  434. if($this->hide) {
  435. return;
  436. }
  437. // Key is hidden
  438. if(array_key_exists($key, $this->hideKey)) {
  439. return;
  440. }
  441. // Hide first label
  442. if($key === 0 and $this->hideFirst) {
  443. return;
  444. }
  445. // Hide last label
  446. if($key === count($this->texts) - 1 and $this->hideLast) {
  447. return;
  448. }
  449. $text = $this->getText($key);
  450. if($text !== NULL) {
  451. // Value must be hidden
  452. if(in_array($text->getText(), $this->hideValue)) {
  453. return;
  454. }
  455. $x = $p->x;
  456. $y = $p->y;
  457. // Get padding
  458. list($left, $right, $top, $bottom) = $text->getPadding();
  459. $font = $text->getFont();
  460. $width = $font->getTextWidth($text);
  461. $height = $font->getTextHeight($text);
  462. switch($this->hAlign) {
  463. case LABEL_RIGHT :
  464. $x -= ($width + $right);
  465. break;
  466. case LABEL_CENTER :
  467. $x -= ($width - $left + $right) / 2;
  468. break;
  469. case LABEL_LEFT :
  470. $x += $left;
  471. break;
  472. }
  473. switch($this->vAlign) {
  474. case LABEL_TOP :
  475. $y -= ($height + $bottom);
  476. break;
  477. case LABEL_MIDDLE :
  478. $y -= ($height - $top + $bottom) / 2;
  479. break;
  480. case LABEL_BOTTOM :
  481. $y += $top;
  482. break;
  483. }
  484. $drawer->string($text, $this->move->move($x, $y));
  485. }
  486. }
  487. function getMax($drawer, $function) {
  488. $max = NULL;
  489. foreach($this->texts as $key => $text) {
  490. $text = $this->getText($key);
  491. $font = $text->getFont();
  492. if(is_null($max)) {
  493. $max = $font->{$function}($text);
  494. } else {
  495. $max = max($max, $font->{$function}($text));
  496. }
  497. }
  498. return $max;
  499. }
  500. }
  501. registerClass('Label');
  502. ?>