/** * 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, 300, 16); $con = new CairoContext($sur); $con->save(); $con->setSourceRgb(1.0, 1.0, 1.0); /* white */ $con->paint(); $con->restore(); $con->selectFontFace('Bitstream Vera Sans', CairoFontSlant::NORMAL, CairoFontWeight::NORMAL); $con->setFontSize(12); $con->setSourceRgb(0, 0, 0); /* black */ $con->moveTo(0, 12); $con->showText('Hello from the '); $con->showText('show-text-current-point-php'); $con->showText(' test.'); $sur->writeToPng(dirname(__FILE__) . '/show-text-current-point.png');
<?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 $TEXT_SIZE = 12; $sur = new CairoImageSurface(CairoFormat::ARGB32, 192, 16); $con = new CairoContext($sur); $con->save(); $con->setSourceRgb(1.0, 1.0, 1.0); /* white */ $con->paint(); $con->restore(); $con->setSourceRgb(0, 0, 0); /* black */ $con->selectFontFace("Bitstream Vera Serif", CairoFontSlant::NORMAL, CairoFontWeight::NORMAL); $con->setFontSize($TEXT_SIZE); $con->moveTo(0, $TEXT_SIZE); $con->showText("i-am-serif"); $con->selectFontFace("Bitstream Vera Sans", CairoFontSlant::NORMAL, CairoFontWeight::NORMAL); $con->showText(" i-am-sans"); $con->selectFontFace("Bitstream Vera Sans Mono", CairoFontSlant::NORMAL, CairoFontWeight::NORMAL); $con->showText(" i-am-mono"); $sur->writeToPng(dirname(__FILE__) . "/select-font-face-php.png");
<?php $width = 800; $height = 800; $sur = new CairoImageSurface(CairoFormat::ARGB32, $width, $height); $con = new CairoContext($sur); $con->setSourceRgb(1, 1, 1); $con->paint(); $con->selectFontFace("Bitstream Vera Sans"); $con->setFontSize(10000); $con->setSourceRgb(0, 0, 0); $con->moveTo(-5000, 5000); $con->showText("xW"); $sur->writeToPng(dirname(__FILE__) . "/large-font-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);