-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Normally renders polygons, but can also render polylines if passed the 'open' option.
- Loading branch information
Showing
2 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters