Examples updated to the latest Camel Case .. Was a pain converting them
[phpCairo.git] / testcases / phpCairo / operator-source.php
blob46f5d490d694750a7680788c91f14c08b763be0a
1 <?
3 $width = 16;
4 $height = 16;
5 $pad = 2;
7 function set_solid_pattern ($x, $y)
9 global $con;
10 $con->setSourceRgb (1.0, 0, 0.0);
13 function set_translucent_pattern ($x, $y)
15 global $con;
16 $con->setSourceRgba (1,0, 0, 0.5);
20 function set_gradient_pattern ($x, $y)
22 global $con,$width,$height;
24 $pat = new CairoLinearGradient ($x, $y, $x + $width, $y + $height);
25 $pat->addColorStopRgba (0.2, 1, 0, 0, 1);
26 $pat->addColorStopRgba ( 0.8, 1, 0, 0, 0.0);
27 $con->setSource ($pat);
31 function set_surface_pattern($x, $y)
33 global $con,$sur,$width,$height;
34 $wi = floor(0.6*$width);
35 $he = floor(0.6*$height);
36 $x+=0.2 * $width;
37 $y+=0.2 * $height;
38 $s = $sur->createSimilar(CONTENT_COLOR_ALPHA, $wi, $he);
39 $con2 = new CairoContext($s);
40 $con2->setSourceRgb(1,0,0);
41 $con2->paint();
42 $con2->setSourceRgb(1,1,1);
43 $con2->arc ( 0.5 * $wi, 0.5 * $he, 0.5 * $he, 0, 2 * M_PI);
44 $con2->fill();
45 $s->writeToPng("temp1.png");
46 $con->setSourceSurface($s,$x,$y);
47 unset($s);
48 //unset($con2);
52 function draw_mask ($x, $y)
55 global $con,$width,$height,$sur;
56 $wi = floor(0.9 * $width);
57 $he = floor(0.9 * $height);
58 $x += 0.05 * $width;
59 $y += 0.05 * $height;
60 $s = new CairoImageSurface(FORMAT_ARGB32, 1, 1);
61 $s = $sur->createSimilar (
62 CONTENT_ALPHA,
63 $wi, $he);
64 $con2 = new CairoContext($s);
66 $con2->setSourceRgb (1, 1, 1); /* white */
68 $con2->arc (0.5 * $wi, 0.5 * $he, 0.45 * $he, 0, 2 * M_PI);
69 $con2->fill ();
72 $con->maskSurface ($s, $x, $y);
73 unset($s);
77 function draw_glyphs ($x, $y)
80 global $con,$width,$height;
81 $con->setFontSize (0.8 * $height);
83 $extents = $con->textExtents ("FG");
84 $con->moveTo (
85 $x + floor (($width - $extents["width"]) / 2 + 0.5) - $extents["x_bearing"],
86 $y + floor (($height - $extents["height"]) / 2 + 0.5) - $extents["y_bearing"]);
87 $con->showText ("FG");
90 function draw_polygon ($x, $y)
92 global $con,$width,$height;
94 $wi = floor(0.9 * $width);
95 $he = floor(0.9 * $height);
96 $x += 0.05 * $width;
97 $y += 0.05 * $height;
99 $con->newPath ();
100 $con->moveTo ($x, $y);
101 $con->lineTo ($x, $y + $he);
102 $con->lineTo ($x + $wi / 2, $y + 3 * $he / 4);
103 $con->lineTo ($x + $wi, $y + $he);
104 $con->lineTo ($x + $wi, $y);
105 $con->lineTo ($x + $wi / 2, $y + $he / 4);
106 $con->closePath ();
107 $con->fill ();
110 function draw_rects ($x, $y)
112 global $con,$width,$height;
114 $block_width = floor(0.33 * $width + 0.5);
115 $block_height = floor(0.33 * $height + 0.5);
118 for ($i = 0; $i < 3; $i++)
119 for ($j = 0; $j < 3; $j++)
120 if (($i + $j) % 2 == 0)
121 $con->rectangle (
122 $x + $block_width * $i, $y + $block_height * $j,
123 $block_width, $block_height);
125 $con->fill ();
128 $imwidth = 4*($width+$pad) + $pad;
129 $imheight = 4*($height+$pad) + $pad;
131 $sur = new CairoImageSurface(FORMAT_ARGB32, $imwidth, $imheight);
132 $con = new CairoContext($sur);
134 $con->selectFontFace ( "Bitstream Vera Sans",
135 FONT_SLANT_NORMAL,
136 FONT_WEIGHT_NORMAL);
138 for ($j = 0; $j < 4; $j++) {
139 for ($i = 0; $i < 4; $i++) {
140 $x = $i * ($width + $pad) + $pad;
141 $y = $j * ($height + $pad) + $pad;
143 $con->save ();
145 $pat = new CairoLinearGradient ($x + $width, $y,
146 $x, $y + $height);
147 $pat->addColorStopRgba (0.2,
148 0.0, 0.0, 1.0, 1.0); /* Solid blue */
149 $pat->addColorStopRgba ( 0.8,
150 0.0, 0.0, 1.0, 0.0); /* Transparent blue */
151 $con->setSource ($pat);
154 $con->rectangle ($x, $y, $width, $height);
155 $con->fillPreserve ();
156 $con->clip ();
158 $con->setOperator (OPERATOR_SOURCE);
159 //pattern_funcs[i] ($x, $y);
160 switch($i) {
161 case 0:
162 set_solid_pattern($x,$y);
163 break;
164 case 1:
165 set_translucent_pattern($x,$y);
166 break;
167 case 2:
168 set_gradient_pattern($x,$y);
169 break;
170 case 3:
171 set_surface_pattern($x,$y);
172 break;
175 //draw_funcs[j] (cr, x, y);
176 switch($j) {
177 case 0:
178 draw_mask($x,$y);
179 break;
180 case 1:
181 draw_glyphs($x,$y);
182 break;
183 case 2:
184 draw_polygon($x,$y);
185 break;
186 case 3:
187 draw_rects($x,$y);
188 break;
190 $con->restore ();
194 $sur->writeToPng("operator-source-php.png");