/** * Initilize cairo surface * * Initilize cairo surface from values provided in the options object, if * is has not been already initlized. * * @return void */ protected function initiliazeSurface() { // Immediatly exit, if surface already exists if ($this->surface !== null) { return; } $this->surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, $this->options->width, $this->options->height); $this->context = cairo_create($this->surface); cairo_set_line_width($this->context, 1); }
/** * Mirrors the image horizontally. * Uses an affine transformation matrix to mirror the image. * * 123 -> 321 * * @return void */ function mirror() { $outputSurface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, $this->img_x, $this->img_y); $outputContext = cairo_create($outputSurface); // xx, yx, xy, yy, x0, y0 $matrix = cairo_matrix_create(-1, 0, 0, 1, $this->img_x, 0); cairo_set_matrix($outputContext, $matrix); cairo_set_source_surface($outputContext, $this->surface, 0, 0); cairo_paint($outputContext); cairo_destroy($outputContext); cairo_surface_destroy($this->surface); $this->surface = $outputSurface; }
function render_svg($filepath, $w, $h, DOMElement $node) { static $svgRenderCache = array(); if (!isset($svgRenderCache[$filepath . $w . $h])) { $filename = tempnam(dirname($filepath), "ca_dompdf_img_"); $svg = file_get_contents($filepath); $renderSupersample = DOMPDF_SVG_SUPERSAMPLE; $rsvg = rsvg_create($svg); $dim = $rsvg->getDimensions(); if ($node) { $viewBox = explode(' ', $node->getAttribute('viewbox')); if (count($viewBox) == 4) { list($innerx, $innery, $innerwidth, $innerheight) = $viewBox; } else { list($innerx, $innery, $innerwidth, $innerheight) = [0, 0, $dim['width'], $dim['height']]; } } else { list($innerx, $innery, $innerwidth, $innerheight) = [0, 0, $dim['width'], $dim['height']]; } $scaleW = $w / $innerwidth; $scaleH = $h / $innerheight; $matrixScale = min($scaleW, $scaleH); $csf = cairo_image_surface_create(CairoFormat::ARGB32, floor($w * $renderSupersample), floor($h * $renderSupersample)); $cctx = new CairoContext($csf); $cctx->setMatrix(CairoMatrix::initScale($renderSupersample * $matrixScale, $renderSupersample * $matrixScale)); $cctx->translate(-$innerx, $innery); $rsvg->render($cctx); $csf->writeToPng($filename); return $svgRenderCache[$filepath . $w . $h] = $filename; } else { return $svgRenderCache[$filepath . $w . $h]; } }