/** * Render text depending of font type and available font extensions * * @param string $id * @param string $text * @param string $font * @param ezcGraphColor $color * @param ezcGraphCoordinate $position * @param float $size * @param float $rotation * @return void */ protected function renderText($text, $font, ezcGraphColor $color, ezcGraphCoordinate $position, $size, $rotation = null) { $this->context->selectFontFace($font, CairoFontSlant::NORMAL, CairoFontWeight::NORMAL); $this->context->setFontSize($size); // Store current state of context $this->context->save(); $this->context->moveTo(0, 0); if ($rotation !== null) { // Move to the center $this->context->translate($rotation->getCenter()->x, $rotation->getCenter()->y); // Rotate around text center $this->context->rotate(deg2rad($rotation->getRotation())); // Center the text $this->context->translate($position->x - $rotation->getCenter()->x, $position->y - $rotation->getCenter()->y - $size * 0.15); } else { $this->context->translate($position->x, $position->y - $size * 0.15); } $this->context->newPath(); $this->getStyle($color, true); $this->context->showText($text); $this->context->stroke(); // Restore state of context $this->context->restore(); }
<?php $sur = new CairoImageSurface(CairoFormat::ARGB32, 10, 10); $con = new CairoContext($sur); $con->setSourceRgb(0, 0, 1); $con->paint(); $con->resetClip(); $con->clip(); $con->translate(0.5, 0.5); $con->setSourceRgb(0, 1, 0); $con->rectangle(0, 0, 10, 10); $con->fillPreserve(); $con->setSourceRgb(1, 0, 0); $con->stroke(); $con->selectFontFace("Bitstream Vera Sans", CairoFontSlant::NORMAL, CairoFontWeight::NORMAL); $con->moveTo(0, 10); $con->showText("cairo"); $sur->writeToPng(dirname(__FILE__) . "/clip-empty-php.png");
<?php $width = 247; $height = 26; $sur = new CairoImageSurface(CairoFormat::ARGB32, $width, $height); $con = new CairoContext($sur); $con->selectFontFace("6x13.pcf"); $con->setFontSize(11.5); $fo = new CairoFontOptions(); $fo->setHintMetrics(CairoHintMetrics::METRICS_ON); $con->setFontOptions($fo); $fe = $con->fontExtents(); $con->moveTo(1, $fe["ascent"] - 1); $con->setSourceRgb(0, 0, 1); $fo->setHintStyle(Cairo::HINT_STYLE_NONE); $con->setFontOptions($fo); $con->showText("the "); $fo->setHintStyle(Cairo::HINT_STYLE_SLIGHT); $con->setFontOptions($fo); $con->showText("quick "); $fo->setHintStyle(Cairo::HINT_STYLE_MEDIUM); $con->setFontOptions($fo); $con->showText("brown"); $fo->setHintStyle(Cairo::HINT_STYLE_FULL); $con->setFontOptions($fo); $con->showText(" fox"); $con->textPath(" jumps over a lazy dog"); $con->fill(); $con->translate($width, $height); $con->rotate(M_PI); $con->moveTo(1, $fe["height"] - $fe["descent"] - 1);
<?php $width = 16; $height = 16; $pad = 2; $noperator = 1 + CairoOperator::SATURATE - CairoOperator::CLEAR; $imagewidth = $noperator * ($width + $pad) + $pad; $imageheight = 4 * ($height + $pad) + $pad; $sur = new CairoImageSurface(CairoFormat::ARGB32, $imagewidth, $imageheight); $con = new CairoContext($sur); $con->selectFontFace("Bitstream Vera Sans"); $con->setFontSize(0.9 * $height); for ($i = 0; $i < 4; $i++) { for ($op = CairoOperator::CLEAR; $op < $noperator; $op++) { $x = $op * ($width + $pad) + $pad; $y = $i * ($height + $pad) + $pad; $con->save(); $pat = new CairoLinearGradient($x + $width, $y, $x, $y + $height); $pat->addColorStopRgba(0.2, 0, 0, 1, 1); $pat->addColorStopRgba(0.8, 0, 0, 1, 0); $con->setSource($pat); $con->rectangle($x, $y, $width, $height); $con->fill(); $con->setOperator($op); $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:
<?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');