/** * 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(); }
$surface_size = sqrt(($size - 2 * $pad) * ($size - 2 * $pad) / 2); $sur = new CairoImageSurface(CairoFormat::ARGB32, $size, $size); $con = new CairoContext($sur); $s = new CairoImageSurface(CairoFormat::RGB24, $surface_size, $surface_size); $con2 = new CairoContext($s); $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(); /* First paint opaque background (black) so we don't need separate * ARGB32 and RGB24 reference images. */ $con->setSourceRgb(0, 0, 0); /* black */ $con->paint(); $con->translate($size / 2, $size / 2); $con->rotate(M_PI / 4.0); $con->translate(-$surface_size / 2, -$surface_size / 2); $con->setSourceSurface($s, 0, 0); $pat = $con->getSource(); $pat->setFilter(CairoFilter::NEAREST); $con->setSource($pat); $con->paint(); $sur->writeToPng(dirname(__FILE__) . "/rotate-image-surface-paint-php.png");
$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); $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");