Examples updated to the latest Camel Case .. Was a pain converting them
[phpCairo.git] / testcases / phpCairo / clip-operator.php
blob1dab39d1e88767698dc3bcf1da15e81608aaa2ec
1 <?
2 $width = 16;
3 $height = 16;
4 $pad = 2;
5 $noperator = 1 + OPERATOR_SATURATE - OPERATOR_CLEAR;
6 $imagewidth = $noperator * ($width + $pad) + $pad;
7 $imageheight = 4 * ($height + $pad) + $pad;
8 $sur = new CairoImageSurface(FORMAT_ARGB32, $imagewidth, $imageheight);
9 $con = new CairoContext($sur);
10 $con->selectFontFace("Bitstream Vera Sans");
11 $con->setFontSize(0.9 * $height);
13 for($i=0; $i < 4; $i++) {
14 for($op = OPERATOR_CLEAR; $op < $noperator; $op++) {
15 $x = $op *($width + $pad) + $pad;
16 $y = $i * ($height + $pad) + $pad;
18 $con->save();
19 $pat = new CairoLinearGradient($x + $width, $y, $x, $y + $height);
20 $pat->addColorStopRgba(0.2,0,0,1,1);
21 $pat->addColorStopRgba(0.8,0,0,1,0);
23 $con->setSource($pat);
24 $con->rectangle($x, $y, $width, $height);
25 $con->fill();
26 $con->setOperator($op);
27 $con->setSourceRgb(1,0,0);
28 $con->moveTo($x,$y);
29 $con->lineTo($x+$width, $y);
30 $con->lineTo($x, $y+$height);
31 $con->clip();
33 switch($i) {
34 case 0:
35 $wi = floor($width * 0.9);
36 $he = floor($height * 0.9);
37 $x += 0.05 * $width;
38 $y += 0.05 * $height;
40 //$stemp = $con->get_group_target();
41 $msur = $sur->createSimilar(CONTENT_ALPHA, $wi, $he);
43 $c2 = new CairoContext($msur);
44 $c2->save();
45 $c2->setSourceRgba(0,0,0,0);
46 $c2->setOperator(OPERATOR_SOURCE);
47 $c2->paint();
48 $c2->restore();
50 $c2->setSourceRgb(1,1,1);
51 $c2->arc(0.5 * $wi, 0.5 * $he, 0.45 * $he, 0, 2 * M_PI);
52 $c2->fill();
53 //unset($c2);
55 $con->maskSurface($msur, $x, $y);
56 //unset($msur);
57 //unset($stemp);
58 break;
60 case 1:
61 $con->setFontSize(0.9 * $height);
62 $ext = $con->textExtents("FG");
63 $con->moveTo($x + floor(($width - $ext["width"])/2 + 0.5) - $ext["x_bearing"], $y + floor(($height - $ext["height"])/2 + 0.5) - $ext["y_bearing"]);
64 $con->showText("FG");
65 break;
67 case 2:
68 $wi = floor($width * 9 / 10);
69 $he = floor($height * 9 / 10);
70 $x += 0.05 * $width;
71 $y += 0.05 * $height;
72 $con->newPath();
73 $con->moveTo($x, $y);
74 $con->lineTo($x, $y + $he);
75 $con->lineTo($x + $wi/2, $y + 3 * $he/4);
76 $con->lineTo($x + $wi, $y + $he);
77 $con->lineTo($x+$wi, $y);
78 $con->lineTo($x + $wi/2, $y + $he/4);
79 $con->closePath();
80 $con->fill();
81 break;
83 case 3:
84 $bw = floor(0.33 * $width + 0.5);
85 $bh = floor(0.33 * $height + 0.5);
87 for($t1 = 0; $t1 < 3; $t1++)
88 for($t2 = 0; $t2 < 3; $t2++)
89 if (($t1 + $t2) % 2 == 0)
90 $con->rectangle($x + $bw*$t1, $y + $bh*$t2, $bw, $bh);
91 $con->fill();
94 break;
96 $con->restore();
99 $sur->writeToPng("clip-operator-php.png");