/** * Get SVG style definition * * Returns a string with SVG style definitions created from color, * fillstatus and line thickness. * * @param ezcGraphColor $color Color * @param mixed $filled Filled * @param float $thickness Line thickness. * @return string Formatstring */ protected function getStyle(ezcGraphColor $color, $filled = true, $thickness = 1.0) { switch (true) { case $color instanceof ezcGraphLinearGradient: $pattern = new CairoLinearGradient($color->startPoint->x, $color->startPoint->y, $color->endPoint->x, $color->endPoint->y); $pattern->addColorStopRgba(0, $color->startColor->red / 255, $color->startColor->green / 255, $color->startColor->blue / 255, 1 - $color->startColor->alpha / 255); $pattern->addColorStopRgba(1, $color->endColor->red / 255, $color->endColor->green / 255, $color->endColor->blue / 255, 1 - $color->endColor->alpha / 255); $this->context->setSource($pattern); $this->context->fill(); break; case $color instanceof ezcGraphRadialGradient: $pattern = new CairoRadialGradient(0, 0, 0, 0, 0, 1); $pattern->addColorStopRgba(0, $color->startColor->red / 255, $color->startColor->green / 255, $color->startColor->blue / 255, 1 - $color->startColor->alpha / 255); $pattern->addColorStopRgba(1, $color->endColor->red / 255, $color->endColor->green / 255, $color->endColor->blue / 255, 1 - $color->endColor->alpha / 255); // Scale pattern, and move it to the correct position $matrix = CairoMatrix::multiply($move = CairoMatrix::initTranslate(-$color->center->x, -$color->center->y), $scale = CairoMatrix::initScale(1 / $color->width, 1 / $color->height)); $pattern->setMatrix($matrix); $this->context->setSource($pattern); $this->context->fill(); break; default: $this->context->setSourceRgba($color->red / 255, $color->green / 255, $color->blue / 255, 1 - $color->alpha / 255); break; } // Set line width $this->context->setLineWidth($thickness); // Set requested fill state for context if ($filled) { $this->context->fillPreserve(); } }
$con->setSourceRgb(1, 0, 0); $con->moveTo($x, $y); $con->lineTo($x + $width, $y); $con->lineTo($x, $y + $height); $con->clip(); switch ($i) { case 0: $wi = floor($width * 0.9); $he = floor($height * 0.9); $x += 0.05 * $width; $y += 0.05 * $height; //$stemp = $con->get_group_target(); $msur = $sur->createSimilar(CairoContent::ALPHA, $wi, $he); $c2 = new CairoContext($msur); $c2->save(); $c2->setSourceRgba(0, 0, 0, 0); $c2->setOperator(CairoOperator::SOURCE); $c2->paint(); $c2->restore(); $c2->setSourceRgb(1, 1, 1); $c2->arc(0.5 * $wi, 0.5 * $he, 0.45 * $he, 0, 2 * M_PI); $c2->fill(); //unset($c2); $con->maskSurface($msur, $x, $y); //unset($msur); //unset($stemp); break; case 1: $con->setFontSize(0.9 * $height); $ext = $con->textExtents("FG"); $con->moveTo($x + floor(($width - $ext["width"]) / 2 + 0.5) - $ext["x_bearing"], $y + floor(($height - $ext["height"]) / 2 + 0.5) - $ext["y_bearing"]);
<?php $patwidth = 120; $patheight = 120; $size = $patwidth * 2; $pad = 2; $width = $pad + $size + $pad; $height = $width; $sur = new CairoImageSurface(CairoFormat::ARGB32, $width, $height); $con = new CairoContext($sur); //$s = new CairoImageSurface(FORMATARGB32, $width, $height); $con->translate($pad, $pad); $pat_surface = $sur->createSimilar(CairoContent::COLOR_ALPHA, $patwidth, $patheight); $cr2 = new CairoContext($pat_surface); $cr2->setSourceRgba(1, 0, 1, 0.5); $cr2->rectangle($patwidth / 6.0, $patheight / 6.0, $patwidth / 4.0, $patheight / 4.0); $cr2->fill(); $cr2->setSourceRgba(0, 1, 1, 0.5); $cr2->rectangle($patwidth / 2.0, $patheight / 2.0, $patwidth / 4.0, $patheight / 4.0); $cr2->fill(); $cr2->setLineWidth(1); $cr2->moveTo($patwidth / 6.0, 0); $cr2->lineTo(0, 0); $cr2->lineTo(0, $patheight / 6.0); $cr2->setSourceRgb(1, 0, 0); $cr2->stroke(); $cr2->moveTo($patwidth / 6.0, $patheight); $cr2->lineTo(0, $patheight); $cr2->lineTo(0, 5 * $patheight / 6.0); $cr2->setSourceRgb(0, 1, 0); $cr2->stroke();
$sur = new CairoImageSurface(CairoFormat::ARGB32, 5, 5); $con = new CairoContext($sur); $color = ''; $color .= chr(0x4c); $color .= chr(0x33); $color .= chr(0x19); $color .= chr(0x80); $s = new CairoImageSurface(CairoFormat::ARGB32, 1, 1); $s->createForData($color, CairoFormat::ARGB32, 1, 1, 4); $pat = new CairoSurfacePattern($s); $pat->setExtend(CairoExtend::REPEAT); for ($i = 0; $i < 5; $i++) { switch ($i) { case 0: $con->setSourceRgb(0.6, 0.7, 0.8); break; case 1: $con->setSourceRgba(0.2, 0.4, 0.6, 0.5); break; case 2: $con->setSourceRgba(0.2, 0.4, 0.6, 0.5); break; case 3: default: $con->setSource($pat); } $con->rectangle($i, 0, 1, 5); $con->fill(); } $sur->writeToPng(dirname(__FILE__) . '/set-source.png');
<?php $size = 40; $pad = 2; $width = $pad + $size + $pad; $height = $width; $sur = new CairoImageSurface(CairoFormat::ARGB32, $width, $height); $con = new CairoContext($sur); $con->translate($pad, $pad); $con->moveTo($size / 2, $size / 2); $con->relLineTo($size / 2, 0); $con->relLineTo($size / -2, $size / 2); $con->closePath(); $con->setOperator(CairoOperator::SOURCE); $con->setSourceRgba(1, 0, 0, 0.5); $con->fill(); $con->arc($size / 2, $size / 2, $size / 4, 0, 2 * M_PI); $con->setOperator(CairoOperator::OVER); $con->setSourceRgba(0, 1, 0, 0.5); $con->fill(); $sur->writeToPng(dirname(__FILE__) . "/over-above-source-php.png"); ?>
<?php $sur = new CairoImageSurface(CairoFormat::ARGB32, 100, 100); $con = new CairoContext($sur); $s = new CairoImageSurface(CairoFormat::ARGB32, 20, 20); $con2 = new CairoContext($s); $con2->setSourceRgba(1, 0, 0, 1); $con2->rectangle(0, 0, 10, 10); $con2->fill(); $con2->setSourceRgba(0, 1, 0, 1); $con2->rectangle(10, 0, 10, 10); $con2->fill(); $con2->setSourceRgba(0, 0, 1, 1); $con2->rectangle(0, 10, 10, 10); $con2->fill(); $con2->setSourceRgba(1, 1, 0, 1); $con2->rectangle(10, 10, 10, 10); $con2->fill(); $pat = new CairoSurfacePattern($s); $pat->setExtend(CairoExtend::REPEAT); $con->setSourceRgba(0, 0, 0, 1); $con->rectangle(0, 0, 100, 100); $con->fill(); $con->translate(10, 10); $con->setOperator(CairoOperator::OVER); $con->setSource($pat); $con->rectangle(0, 0, 90, 90); $con->fill(); $sur->writeToPng(dirname(__FILE__) . "/composite-integer-translate-over-repeat-php.png"); ?>
<?php $sur = new CairoImageSurface(CairoFormat::ARGB32, 90, 90); $con = new CairoContext($sur); $surface_size = (90 - 30) / 10; $con->setSourceRgba(0, 0, 0, 1); $con->rectangle(0, 0, 90, 90); $con->fill(); $surface = new CairoImageSurface(CairoFormat::RGB24, $surface_size, $surface_size); $con2 = new CairoContext($surface); $con2->setSourceRgb(1, 1, 1); $con2->rectangle(0, 0, $surface_size / 2, $surface_size / 2); $con2->fill(); $con2->setSourceRgb(1, 0, 0); $con2->rectangle($surface_size / 2, 0, $surface_size / 2, $surface_size / 2); $con2->fill(); $con2->setSourceRgb(0, 1, 0); $con2->rectangle(0, $surface_size / 2, $surface_size / 2, $surface_size / 2); $con2->fill(); $con2->setSourceRgb(0, 0, 1); $con2->rectangle($surface_size / 2, $surface_size / 2, $surface_size / 2, $surface_size / 2); $con2->fill(); $con->scale(10, 10); $con->setSourceSurface($surface, 1.5, 1.5); /* Using EXTEND_REFLECT makes this test pass for image and xlib backends */ $pattern = $con->getSource(); $pattern->setExtend(CairoExtend::PAD); $con->rectangle(1.5, 1.5, 6, 6); $con->clip(); $con->setSource($pattern); $con->paint();
<?php $char = 'Cairo'; $sur = new CairoImageSurface(0, 500, 500); $con = new CairoContext($sur); $con->selectFontFace('Sans'); $con->setFontSize(100); $ext = $con->textExtents($char); $x = 25.0; $y = 150.0; $con->moveTo($x, $y); $con->showText($char); $con->setSourceRgba(1, 0.2, 0.2, 0.6); $con->setLineWidth(6.0); $con->arc($x, $y, 10, 0, 2 * 3.14); $con->fill(); $con->moveTo($x, $y); $con->relLineTo(0, -1 * $ext['height']); $con->relLineTo($ext['width'], 0); $con->relLineTo($ext['x_bearing'], -1 * $ext['y_bearing']); $con->stroke(); $sur->writeToPng(dirname(__FILE__) . '/text-extents.png');