/** * 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 $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");
$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"]); $con->showText("FG"); break; case 2: $wi = floor($width * 9 / 10); $he = floor($height * 9 / 10); $x += 0.05 * $width; $y += 0.05 * $height; $con->newPath(); $con->moveTo($x, $y); $con->lineTo($x, $y + $he); $con->lineTo($x + $wi / 2, $y + 3 * $he / 4); $con->lineTo($x + $wi, $y + $he); $con->lineTo($x + $wi, $y); $con->lineTo($x + $wi / 2, $y + $he / 4); $con->closePath(); $con->fill();
<?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");
$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); $fo->setHintMetrics(CairoHintMetrics::METRICS_OFF);
<?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');