Added some test cases ....
[phpCairo.git] / testcases / phpCairo / operator-clear.php
blob4f44989915f29764a6c2b64aff7d1c4b6154b3ac
1 <?
3 $width = 16;
4 $height = 16;
5 $pad = 2;
7 function set_solid_pattern ($x, $y)
9 global $con;
10 $con->set_source_rgb (1.0, 0, 0.0);
13 function set_gradient_pattern ($x, $y)
15 global $con,$width,$height;
17 $pat = new CairoLinearGradient ($x, $y, $x + $width, $y + $height);
18 $pat->add_color_stop_rgba (0.2, 1, 0, 0, 1);
19 $pat->add_color_stop_rgba ( 0.8, 1, 0, 0, 0.0);
20 $con->set_source ($pat);
24 function draw_mask ($x, $y)
27 global $con,$width,$height,$sur;
28 $wi = floor(0.9 * $width);
29 $he = floor(0.9 * $height);
30 $x += 0.05 * $width;
31 $y += 0.05 * $height;
32 //$s = new CairoImageSurface(FORMAT_ARGB32, 1, 1);
33 $s = $sur->create_similar (
34 CONTENT_ALPHA,
35 $wi, $he);
36 $con2 = new CairoContext($s);
38 $con2->set_source_rgb (1, 1, 1); /* white */
40 $con2->arc (0.5 * $wi, 0.5 * $he, 0.45 * $he, 0, 2 * M_PI);
41 $con2->fill ();
44 $con->mask_surface ($s, $x, $y);
49 function draw_glyphs ($x, $y)
52 global $con,$width,$height;
53 $con->set_font_size (0.8 * $height);
55 $extents = $con->text_extents ("FG");
56 $con->move_to (
57 $x + floor (($width - $extents["width"]) / 2 + 0.5) - $extents["x_bearing"],
58 $y + floor (($height - $extents["height"]) / 2 + 0.5) - $extents["y_bearing"]);
59 $con->show_text ("FG");
62 function draw_polygon ($x, $y)
64 global $con,$width,$height;
66 $wi = floor(0.9 * $width);
67 $he = floor(0.9 * $height);
68 $x += 0.05 * $width;
69 $y += 0.05 * $height;
71 $con->new_path ();
72 $con->move_to ($x, $y);
73 $con->line_to ($x, $y + $he);
74 $con->line_to ($x + $wi / 2, $y + 3 * $he / 4);
75 $con->line_to ($x + $wi, $y + $he);
76 $con->line_to ($x + $wi, $y);
77 $con->line_to ($x + $wi / 2, $y + $he / 4);
78 $con->close_path ();
79 $con->fill ();
82 function draw_rects ($x, $y)
84 global $con,$width,$height;
86 $block_width = floor(0.33 * $width + 0.5);
87 $block_height = floor(0.33 * $height + 0.5);
90 for ($i = 0; $i < 3; $i++)
91 for ($j = 0; $j < 3; $j++)
92 if (($i + $j) % 2 == 0)
93 $con->rectangle (
94 $x + $block_width * $i, $y + $block_height * $j,
95 $block_width, $block_height);
97 $con->fill ();
100 $imwidth = 2*($width+$pad) + $pad;
101 $imheight = 4*($height+$pad) + $pad;
103 $sur = new CairoImageSurface(FORMAT_ARGB32, $imwidth, $imheight);
104 $con = new CairoContext($sur);
106 $con->select_font_face ( "Bitstream Vera Sans",
107 FONT_SLANT_NORMAL,
108 FONT_WEIGHT_NORMAL);
110 for ($j = 0; $j < 4; $j++) {
111 for ($i = 0; $i < 2; $i++) {
112 $x = $i * ($width + $pad) + $pad;
113 $y = $j * ($height + $pad) + $pad;
115 $con->save ();
117 $pat = new CairoLinearGradient ($x + $width, $y,
118 $x, $y + $height);
119 $pat->add_color_stop_rgba (0.2,
120 0.0, 0.0, 1.0, 1.0); /* Solid blue */
121 $pat->add_color_stop_rgba ( 0.8,
122 0.0, 0.0, 1.0, 0.0); /* Transparent blue */
123 $con->set_source ($pat);
126 $con->rectangle ($x, $y, $width, $height);
127 $con->fill_preserve ();
128 $con->clip ();
130 $con->set_operator (OPERATOR_CLEAR);
131 //pattern_funcs[i] ($x, $y);
132 switch($i) {
133 case 0:
134 set_solid_pattern($x,$y);
135 break;
136 case 1:
137 set_gradient_pattern($x,$y);
138 break;
141 //draw_funcs[j] (cr, x, y);
142 switch($j) {
143 case 0:
144 draw_mask($x,$y);
145 break;
146 case 1:
147 draw_glyphs($x,$y);
148 break;
149 case 2:
150 draw_polygon($x,$y);
151 break;
152 case 3:
153 draw_rects($x,$y);
154 break;
156 $con->restore ();
160 $sur->write_to_png("operator-clear-php.png");