/** * Returns boundings of text depending on the available font extension * * @param float $size Textsize * @param ezcGraphFontOptions $font Font * @param string $text Text * @return ezcGraphBoundings Boundings of text */ protected function getTextBoundings($size, ezcGraphFontOptions $font, $text) { $this->context->selectFontFace($font->name, CairoFontSlant::NORMAL, CairoFontWeight::NORMAL); $this->context->setFontSize($size); $extents = $this->context->textExtents($text); return new ezcGraphBoundings(0, 0, $extents['width'], $extents['height']); }
<?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');
$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"]); $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);
$ext = $con->textExtents($str); $sf = $con->getScaledFont(); $sext = $sf->textExtents("text"); $lw = $con->getLineWidth(); $con->rectangle($x + $ext["x_bearing"] - $lw / 2, $y + $ext["y_bearing"] - $lw / 2, $ext["width"] + $lw, $ext["height"] + $lw); $con->stroke(); $con->moveTo($x, $y); $con->showText($str); $con->restore(); } $sur = new CairoImageSurface(CairoFormat::ARGB32, 38, 34); $con = new CairoContext($sur); $con->setSourceRgb(1, 1, 1); $con->paint(); $con->selectFontFace("Bitstream Vera Sans"); $con->setFontSize(12); $con->translate(4, 4); $con->setLineWidth(1); $ext = $con->textExtents("text"); $con->setSourceRgb(0, 0, 0); box_text("text", 0, -$ext["y_bearing"]); $mat = new CairoMatrix(); $mat->translate(6, 16); $mat->scale(12, 12); $con->setFontMatrix($mat); $con->setSourceRgb(0, 0, 1); box_text("text", 0, -$ext["y_bearing"]); $sur->writeToPng(dirname(__FILE__) . "/font-matrix-translation-php.png"); ?>