Skip to content

Commit

Permalink
Implement SVGPolygonRenderer
Browse files Browse the repository at this point in the history
Normally renders polygons, but can also render polylines if passed the
'open' option.
  • Loading branch information
meyfa committed Oct 8, 2016
1 parent 9298045 commit 62923e0
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/Rasterization/Renderers/SVGPolygonRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace JangoBrick\SVG\Rasterization\Renderers;

use JangoBrick\SVG\Rasterization\SVGRasterizer;

class SVGPolygonRenderer extends SVGRenderer
{
protected function prepareRenderParams(SVGRasterizer $rasterizer, array $options)
{
$scaleX = $rasterizer->getScaleX();
$scaleY = $rasterizer->getScaleY();

$points = array();
foreach($options['points'] as $point) {
$points[] = $point[0] * $scaleX;
$points[] = $point[1] * $scaleY;
}

return array(
'open' => isset($options['open']) ? $options['open'] : false,
'points' => $points,
'numpoints' => count($options['points']),
);
}

protected function renderFill($image, array $params, $color)
{
// somehow imagesetthickness() affects the polygon drawing. reset to 0.
imagesetthickness($image, 0);
imagefilledpolygon($image, $params['points'], $params['numpoints'], $color);
}

protected function renderStroke($image, array $params, $color, $strokeWidth)
{
imagesetthickness($image, $strokeWidth);

if ($params['open']) {
$this->renderStrokeOpen($image, $params['points'], $color);
return;
}

imagepolygon($image, $params['points'], $params['numpoints'], $color);
}

private function renderStrokeOpen($image, array $points, $color)
{
$px = $points[0];
$py = $points[1];

for ($i = 2, $n = count($points); $i < $n; $i += 2) {
$x = $points[$i];
$y = $points[$i + 1];
imageline($image, $px, $py, $x, $y, $color);
$px = $x;
$py = $y;
}
}
}
1 change: 1 addition & 0 deletions src/Rasterization/SVGRasterizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ private static function createDependencies()
'rect' => new Renderers\SVGRectRenderer(),
'line' => new Renderers\SVGLineRenderer(),
'ellipse' => new Renderers\SVGEllipseRenderer(),
'polygon' => new Renderers\SVGPolygonRenderer(),
);
}

Expand Down

0 comments on commit 62923e0

Please sign in to comment.