Merge branch '138-toggle-free-look-with-hotkey' into 'main/atys-live'
[ryzomcore.git] / web / public_php / admin / jpgraph / jpgraph_canvtools.php
blobf43ee5669e378b48eeb885da8bb3b5857d65c443
1 <?php
2 /*=======================================================================
3 // File: JPGRAPH_CANVTOOLS.PHP
4 // Description: Some utilities for text and shape drawing on a canvas
5 // Created: 2002-08-23
6 // Author: Johan Persson (johanp@aditus.nu)
7 // Ver: $Id: jpgraph_canvtools.php,v 1.1 2006/07/07 13:37:14 powles Exp $
8 //
9 // Copyright (c) Aditus Consulting. All rights reserved.
10 //========================================================================
13 DEFINE('CORNER_TOPLEFT',0);
14 DEFINE('CORNER_TOPRIGHT',1);
15 DEFINE('CORNER_BOTTOMRIGHT',2);
16 DEFINE('CORNER_BOTTOMLEFT',3);
19 //===================================================
20 // CLASS CanvasScale
21 // Description: Define a scale for canvas so we
22 // can abstract away with absolute pixels
23 //===================================================
25 class CanvasScale {
26 var $g;
27 var $w,$h;
28 var $ixmin=0,$ixmax=10,$iymin=0,$iymax=10;
30 function CanvasScale(&$graph,$xmin=0,$xmax=10,$ymin=0,$ymax=10) {
31 $this->g = &$graph;
32 $this->w = $graph->img->width;
33 $this->h = $graph->img->height;
34 $this->ixmin = $xmin;
35 $this->ixmax = $xmax;
36 $this->iymin = $ymin;
37 $this->iymax = $ymax;
40 function Set($xmin=0,$xmax=10,$ymin=0,$ymax=10) {
41 $this->ixmin = $xmin;
42 $this->ixmax = $xmax;
43 $this->iymin = $ymin;
44 $this->iymax = $ymax;
47 function Translate($x,$y) {
48 $xp = round(($x-$this->ixmin)/($this->ixmax - $this->ixmin) * $this->w);
49 $yp = round(($y-$this->iymin)/($this->iymax - $this->iymin) * $this->h);
50 return array($xp,$yp);
53 function TranslateX($x) {
54 $xp = round(($x-$this->ixmin)/($this->ixmax - $this->ixmin) * $this->w);
55 return $xp;
58 function TranslateY($y) {
59 $yp = round(($y-$this->iymin)/($this->iymax - $this->iymin) * $this->h);
60 return $yp;
66 //===================================================
67 // CLASS Shape
68 // Description: Methods to draw shapes on canvas
69 //===================================================
70 class Shape {
71 var $img,$scale;
73 function Shape(&$aGraph,&$scale) {
74 $this->img = &$aGraph->img;
75 $this->img->SetColor('black');
76 $this->scale = &$scale;
79 function SetColor($aColor) {
80 $this->img->SetColor($aColor);
83 function Line($x1,$y1,$x2,$y2) {
84 list($x1,$y1) = $this->scale->Translate($x1,$y1);
85 list($x2,$y2) = $this->scale->Translate($x2,$y2);
86 $this->img->Line($x1,$y1,$x2,$y2);
89 function Polygon($p,$aClosed=false) {
90 $n=count($p);
91 for($i=0; $i < $n; $i+=2 ) {
92 $p[$i] = $this->scale->TranslateX($p[$i]);
93 $p[$i+1] = $this->scale->TranslateY($p[$i+1]);
95 $this->img->Polygon($p,$aClosed);
98 function FilledPolygon($p) {
99 $n=count($p);
100 for($i=0; $i < $n; $i+=2 ) {
101 $p[$i] = $this->scale->TranslateX($p[$i]);
102 $p[$i+1] = $this->scale->TranslateY($p[$i+1]);
104 $this->img->FilledPolygon($p);
108 // Draw a bezier curve with defining points in the $aPnts array
109 // using $aSteps steps.
110 // 0=x0, 1=y0
111 // 2=x1, 3=y1
112 // 4=x2, 5=y2
113 // 6=x3, 7=y3
114 function Bezier($p,$aSteps=40) {
115 $x0 = $p[0];
116 $y0 = $p[1];
117 // Calculate coefficients
118 $cx = 3*($p[2]-$p[0]);
119 $bx = 3*($p[4]-$p[2])-$cx;
120 $ax = $p[6]-$p[0]-$cx-$bx;
121 $cy = 3*($p[3]-$p[1]);
122 $by = 3*($p[5]-$p[3])-$cy;
123 $ay = $p[7]-$p[1]-$cy-$by;
125 // Step size
126 $delta = 1.0/$aSteps;
128 $x_old = $x0;
129 $y_old = $y0;
130 for($t=$delta; $t<=1.0; $t+=$delta) {
131 $tt = $t*$t; $ttt=$tt*$t;
132 $x = $ax*$ttt + $bx*$tt + $cx*$t + $x0;
133 $y = $ay*$ttt + $by*$tt + $cy*$t + $y0;
134 $this->Line($x_old,$y_old,$x,$y);
135 $x_old = $x;
136 $y_old = $y;
138 $this->Line($x_old,$y_old,$p[6],$p[7]);
141 function Rectangle($x1,$y1,$x2,$y2) {
142 list($x1,$y1) = $this->scale->Translate($x1,$y1);
143 list($x2,$y2) = $this->scale->Translate($x2,$y2);
144 $this->img->Rectangle($x1,$y1,$x2,$y2);
147 function FilledRectangle($x1,$y1,$x2,$y2) {
148 list($x1,$y1) = $this->scale->Translate($x1,$y1);
149 list($x2,$y2) = $this->scale->Translate($x2,$y2);
150 $this->img->FilledRectangle($x1,$y1,$x2,$y2);
153 function Circle($x1,$y1,$r) {
154 list($x1,$y1) = $this->scale->Translate($x1,$y1);
155 if( $r >= 0 )
156 $r = $this->scale->TranslateX($r);
157 else
158 $r = -$r;
159 $this->img->Circle($x1,$y1,$r);
162 function FilledCircle($x1,$y1,$r) {
163 list($x1,$y1) = $this->scale->Translate($x1,$y1);
164 if( $r >= 0 )
165 $r = $this->scale->TranslateX($r);
166 else
167 $r = -$r;
168 $this->img->FilledCircle($x1,$y1,$r);
171 function RoundedRectangle($x1,$y1,$x2,$y2,$r=null) {
172 list($x1,$y1) = $this->scale->Translate($x1,$y1);
173 list($x2,$y2) = $this->scale->Translate($x2,$y2);
175 if( $r == null )
176 $r = 5;
177 elseif( $r >= 0 )
178 $r = $this->scale->TranslateX($r);
179 else
180 $r = -$r;
181 $this->img->RoundedRectangle($x1,$y1,$x2,$y2,$r);
184 function FilledRoundedRectangle($x1,$y1,$x2,$y2,$r=null) {
185 list($x1,$y1) = $this->scale->Translate($x1,$y1);
186 list($x2,$y2) = $this->scale->Translate($x2,$y2);
188 if( $r == null )
189 $r = 5;
190 elseif( $r > 0 )
191 $r = $this->scale->TranslateX($r);
192 else
193 $r = -$r;
194 $this->img->FilledRoundedRectangle($x1,$y1,$x2,$y2,$r);
197 function ShadowRectangle($x1,$y1,$x2,$y2,$fcolor=false,$shadow_width=null,$shadow_color=array(102,102,102)) {
198 list($x1,$y1) = $this->scale->Translate($x1,$y1);
199 list($x2,$y2) = $this->scale->Translate($x2,$y2);
200 if( $shadow_width == null )
201 $shadow_width=4;
202 else
203 $shadow_width=$this->scale->TranslateX($shadow_width);
204 $this->img->ShadowRectangle($x1,$y1,$x2,$y2,$fcolor,$shadow_width,$shadow_color);
207 function SetTextAlign($halign,$valign="bottom") {
208 $this->img->SetTextAlign($halign,$valign="bottom");
211 function StrokeText($x1,$y1,$txt,$dir=0,$paragraph_align="left") {
212 list($x1,$y1) = $this->scale->Translate($x1,$y1);
213 $this->img->StrokeText($x1,$y1,$txt,$dir,$paragraph_align);
216 // A rounded rectangle where one of the corner has been moved "into" the
217 // rectangle 'iw' width and 'ih' height. Corners:
218 // 0=Top left, 1=top right, 2=bottom right, 3=bottom left
219 function IndentedRectangle($xt,$yt,$w,$h,$iw=0,$ih=0,$aCorner=3,$aFillColor="",$r=4) {
221 list($xt,$yt) = $this->scale->Translate($xt,$yt);
222 list($w,$h) = $this->scale->Translate($w,$h);
223 list($iw,$ih) = $this->scale->Translate($iw,$ih);
225 $xr = $xt + $w - 0;
226 $yl = $yt + $h - 0;
228 switch( $aCorner ) {
229 case 0: // Upper left
231 // Bottom line, left & right arc
232 $this->img->Line($xt+$r,$yl,$xr-$r,$yl);
233 $this->img->Arc($xt+$r,$yl-$r,$r*2,$r*2,90,180);
234 $this->img->Arc($xr-$r,$yl-$r,$r*2,$r*2,0,90);
236 // Right line, Top right arc
237 $this->img->Line($xr,$yt+$r,$xr,$yl-$r);
238 $this->img->Arc($xr-$r,$yt+$r,$r*2,$r*2,270,360);
240 // Top line, Top left arc
241 $this->img->Line($xt+$iw+$r,$yt,$xr-$r,$yt);
242 $this->img->Arc($xt+$iw+$r,$yt+$r,$r*2,$r*2,180,270);
244 // Left line
245 $this->img->Line($xt,$yt+$ih+$r,$xt,$yl-$r);
247 // Indent horizontal, Lower left arc
248 $this->img->Line($xt+$r,$yt+$ih,$xt+$iw-$r,$yt+$ih);
249 $this->img->Arc($xt+$r,$yt+$ih+$r,$r*2,$r*2,180,270);
251 // Indent vertical, Indent arc
252 $this->img->Line($xt+$iw,$yt+$r,$xt+$iw,$yt+$ih-$r);
253 $this->img->Arc($xt+$iw-$r,$yt+$ih-$r,$r*2,$r*2,0,90);
255 if( $aFillColor != '' ) {
256 $bc = $this->img->current_color_name;
257 $this->img->PushColor($aFillColor);
258 $this->img->FillToBorder($xr-$r,$yl-$r,$bc);
259 $this->img->PopColor();
262 break;
264 case 1: // Upper right
266 // Bottom line, left & right arc
267 $this->img->Line($xt+$r,$yl,$xr-$r,$yl);
268 $this->img->Arc($xt+$r,$yl-$r,$r*2,$r*2,90,180);
269 $this->img->Arc($xr-$r,$yl-$r,$r*2,$r*2,0,90);
271 // Left line, Top left arc
272 $this->img->Line($xt,$yt+$r,$xt,$yl-$r);
273 $this->img->Arc($xt+$r,$yt+$r,$r*2,$r*2,180,270);
275 // Top line, Top right arc
276 $this->img->Line($xt+$r,$yt,$xr-$iw-$r,$yt);
277 $this->img->Arc($xr-$iw-$r,$yt+$r,$r*2,$r*2,270,360);
279 // Right line
280 $this->img->Line($xr,$yt+$ih+$r,$xr,$yl-$r);
282 // Indent horizontal, Lower right arc
283 $this->img->Line($xr-$iw+$r,$yt+$ih,$xr-$r,$yt+$ih);
284 $this->img->Arc($xr-$r,$yt+$ih+$r,$r*2,$r*2,270,360);
286 // Indent vertical, Indent arc
287 $this->img->Line($xr-$iw,$yt+$r,$xr-$iw,$yt+$ih-$r);
288 $this->img->Arc($xr-$iw+$r,$yt+$ih-$r,$r*2,$r*2,90,180);
290 if( $aFillColor != '' ) {
291 $bc = $this->img->current_color_name;
292 $this->img->PushColor($aFillColor);
293 $this->img->FillToBorder($xt+$r,$yl-$r,$bc);
294 $this->img->PopColor();
297 break;
299 case 2: // Lower right
300 // Top line, Top left & Top right arc
301 $this->img->Line($xt+$r,$yt,$xr-$r,$yt);
302 $this->img->Arc($xt+$r,$yt+$r,$r*2,$r*2,180,270);
303 $this->img->Arc($xr-$r,$yt+$r,$r*2,$r*2,270,360);
305 // Left line, Bottom left arc
306 $this->img->Line($xt,$yt+$r,$xt,$yl-$r);
307 $this->img->Arc($xt+$r,$yl-$r,$r*2,$r*2,90,180);
309 // Bottom line, Bottom right arc
310 $this->img->Line($xt+$r,$yl,$xr-$iw-$r,$yl);
311 $this->img->Arc($xr-$iw-$r,$yl-$r,$r*2,$r*2,0,90);
313 // Right line
314 $this->img->Line($xr,$yt+$r,$xr,$yl-$ih-$r);
316 // Indent horizontal, Lower right arc
317 $this->img->Line($xr-$r,$yl-$ih,$xr-$iw+$r,$yl-$ih);
318 $this->img->Arc($xr-$r,$yl-$ih-$r,$r*2,$r*2,0,90);
320 // Indent vertical, Indent arc
321 $this->img->Line($xr-$iw,$yl-$r,$xr-$iw,$yl-$ih+$r);
322 $this->img->Arc($xr-$iw+$r,$yl-$ih+$r,$r*2,$r*2,180,270);
324 if( $aFillColor != '' ) {
325 $bc = $this->img->current_color_name;
326 $this->img->PushColor($aFillColor);
327 $this->img->FillToBorder($xt+$r,$yt+$r,$bc);
328 $this->img->PopColor();
331 break;
333 case 3: // Lower left
334 // Top line, Top left & Top right arc
335 $this->img->Line($xt+$r,$yt,$xr-$r,$yt);
336 $this->img->Arc($xt+$r,$yt+$r,$r*2,$r*2,180,270);
337 $this->img->Arc($xr-$r,$yt+$r,$r*2,$r*2,270,360);
339 // Right line, Bottom right arc
340 $this->img->Line($xr,$yt+$r,$xr,$yl-$r);
341 $this->img->Arc($xr-$r,$yl-$r,$r*2,$r*2,0,90);
343 // Bottom line, Bottom left arc
344 $this->img->Line($xt+$iw+$r,$yl,$xr-$r,$yl);
345 $this->img->Arc($xt+$iw+$r,$yl-$r,$r*2,$r*2,90,180);
347 // Left line
348 $this->img->Line($xt,$yt+$r,$xt,$yl-$ih-$r);
350 // Indent horizontal, Lower left arc
351 $this->img->Line($xt+$r,$yl-$ih,$xt+$iw-$r,$yl-$ih);
352 $this->img->Arc($xt+$r,$yl-$ih-$r,$r*2,$r*2,90,180);
354 // Indent vertical, Indent arc
355 $this->img->Line($xt+$iw,$yl-$ih+$r,$xt+$iw,$yl-$r);
356 $this->img->Arc($xt+$iw-$r,$yl-$ih+$r,$r*2,$r*2,270,360);
358 if( $aFillColor != '' ) {
359 $bc = $this->img->current_color_name;
360 $this->img->PushColor($aFillColor);
361 $this->img->FillToBorder($xr-$r,$yt+$r,$bc);
362 $this->img->PopColor();
365 break;
371 //===================================================
372 // CLASS RectangleText
373 // Description: Draws a text paragraph inside a
374 // rounded, possible filled, rectangle.
375 //===================================================
376 class CanvasRectangleText {
377 var $ix,$iy,$iw,$ih,$ir=4;
378 var $iTxt,$iColor='black',$iFillColor='',$iFontColor='black';
379 var $iParaAlign='center';
380 var $iAutoBoxMargin=5;
381 var $iShadowWidth=3,$iShadowColor='';
383 function CanvasRectangleText($aTxt='',$xl=0,$yt=0,$w=0,$h=0) {
384 $this->iTxt = new Text($aTxt);
385 $this->ix = $xl;
386 $this->iy = $yt;
387 $this->iw = $w;
388 $this->ih = $h;
391 function SetShadow($aColor='gray',$aWidth=3) {
392 $this->iShadowColor = $aColor;
393 $this->iShadowWidth = $aWidth;
396 function SetFont($FontFam,$aFontStyle,$aFontSize=12) {
397 $this->iTxt->SetFont($FontFam,$aFontStyle,$aFontSize);
400 function SetTxt($aTxt) {
401 $this->iTxt->Set($aTxt);
404 function ParagraphAlign($aParaAlign) {
405 $this->iParaAlign = $aParaAlign;
408 function SetFillColor($aFillColor) {
409 $this->iFillColor = $aFillColor;
412 function SetAutoMargin($aMargin) {
413 $this->iAutoBoxMargin=$aMargin;
416 function SetColor($aColor) {
417 $this->iColor = $aColor;
420 function SetFontColor($aColor) {
421 $this->iFontColor = $aColor;
424 function SetPos($xl=0,$yt=0,$w=0,$h=0) {
425 $this->ix = $xl;
426 $this->iy = $yt;
427 $this->iw = $w;
428 $this->ih = $h;
431 function Pos($xl=0,$yt=0,$w=0,$h=0) {
432 $this->ix = $xl;
433 $this->iy = $yt;
434 $this->iw = $w;
435 $this->ih = $h;
438 function Set($aTxt,$xl,$yt,$w=0,$h=0) {
439 $this->iTxt->Set($aTxt);
440 $this->ix = $xl;
441 $this->iy = $yt;
442 $this->iw = $w;
443 $this->ih = $h;
446 function SetCornerRadius($aRad=5) {
447 $this->ir = $aRad;
450 function Stroke($aImg,$scale) {
452 // If coordinates are specifed as negative this means we should
453 // treat them as abolsute (pixels) coordinates
454 if( $this->ix > 0 ) {
455 $this->ix = $scale->TranslateX($this->ix) ;
457 else {
458 $this->ix = -$this->ix;
461 if( $this->iy > 0 ) {
462 $this->iy = $scale->TranslateY($this->iy) ;
464 else {
465 $this->iy = -$this->iy;
468 list($this->iw,$this->ih) = $scale->Translate($this->iw,$this->ih) ;
470 if( $this->iw == 0 )
471 $this->iw = round($this->iTxt->GetWidth($aImg) + $this->iAutoBoxMargin);
472 if( $this->ih == 0 ) {
473 $this->ih = round($this->iTxt->GetTextHeight($aImg) + $this->iAutoBoxMargin);
476 if( $this->iShadowColor != '' ) {
477 $aImg->PushColor($this->iShadowColor);
478 $aImg->FilledRoundedRectangle($this->ix+$this->iShadowWidth,
479 $this->iy+$this->iShadowWidth,
480 $this->ix+$this->iw-1+$this->iShadowWidth,
481 $this->iy+$this->ih-1+$this->iShadowWidth,
482 $this->ir);
483 $aImg->PopColor();
486 if( $this->iFillColor != '' ) {
487 $aImg->PushColor($this->iFillColor);
488 $aImg->FilledRoundedRectangle($this->ix,$this->iy,
489 $this->ix+$this->iw-1,
490 $this->iy+$this->ih-1,
491 $this->ir);
492 $aImg->PopColor();
495 if( $this->iColor != '' ) {
496 $aImg->PushColor($this->iColor);
497 $aImg->RoundedRectangle($this->ix,$this->iy,
498 $this->ix+$this->iw-1,
499 $this->iy+$this->ih-1,
500 $this->ir);
501 $aImg->PopColor();
504 $this->iTxt->Align('center','center');
505 $this->iTxt->ParagraphAlign($this->iParaAlign);
506 $this->iTxt->SetColor($this->iFontColor);
507 $this->iTxt->Stroke($aImg, $this->ix+$this->iw/2, $this->iy+$this->ih/2);
509 return array($this->iw, $this->ih);