Added some test cases ....
[phpCairo.git] / testcases / phpCairo / clip-operator.php
blob81681bfc7e1807d11efb6f8952f63b6498a15117
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->select_font_face("Bitstream Vera Sans");
11 $con->set_font_size(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->add_color_stop_rgba(0.2,0,0,1,1);
21 $pat->add_color_stop_rgba(0.8,0,0,1,0);
23 $con->set_source($pat);
24 $con->rectangle($x, $y, $width, $height);
25 $con->fill();
26 $con->set_operator($op);
27 $con->set_source_rgb(1,0,0);
28 $con->move_to($x,$y);
29 $con->line_to($x+$width, $y);
30 $con->line_to($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->create_similar(CONTENT_ALPHA, $wi, $he);
43 $c2 = new CairoContext($msur);
44 $c2->save();
45 $c2->set_source_rgba(0,0,0,0);
46 $c2->set_operator(OPERATOR_SOURCE);
47 $c2->paint();
48 $c2->restore();
50 $c2->set_source_rgb(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->mask_surface($msur, $x, $y);
56 //unset($msur);
57 //unset($stemp);
58 break;
60 case 1:
61 $con->set_font_size(0.9 * $height);
62 $ext = $con->text_extents("FG");
63 $con->move_to($x + floor(($width - $ext["width"])/2 + 0.5) - $ext["x_bearing"], $y + floor(($height - $ext["height"])/2 + 0.5) - $ext["y_bearing"]);
64 $con->show_text("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->new_path();
73 $con->move_to($x, $y);
74 $con->line_to($x, $y + $he);
75 $con->line_to($x + $wi/2, $y + 3 * $he/4);
76 $con->line_to($x + $wi, $y + $he);
77 $con->line_to($x+$wi, $y);
78 $con->line_to($x + $wi/2, $y + $he/4);
79 $con->close_path();
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->write_to_png("clip-operator-php.png");