Skip to content

Commit

Permalink
Make polygon/-line subclasses of SVGPolygonalShape
Browse files Browse the repository at this point in the history
SVGPolygon and SVGPolyline shared almost the exact same code, with the
exception of outline drawing. A new common parent class,
SVGPolygonalShape, aims to fix that by providing an abstract method for
that purpose, and handling the rest (point manipulation,
stringification, ...) completely on its own.
  • Loading branch information
meyfa committed Sep 26, 2016
1 parent 0a10629 commit ca8c0a2
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 205 deletions.
107 changes: 4 additions & 103 deletions src/Nodes/Shapes/SVGPolygon.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,115 +2,16 @@

namespace JangoBrick\SVG\Nodes\Shapes;

use JangoBrick\SVG\Nodes\SVGNode;
use JangoBrick\SVG\SVG;
use JangoBrick\SVG\SVGRenderingHelper;

class SVGPolygon extends SVGNode
class SVGPolygon extends SVGPolygonalShape
{
private $points;

public function __construct($points = array())
{
parent::__construct();

$this->points = $points;
}

public function addPoint($a, $b = null)
{
if (!is_array($a)) {
$a = array($a, $b);
}

$this->points[] = $a;
return $this;
}

public function removePoint($index)
{
array_splice($this->points, $index, 1);
return $this;
}

public function countPoints()
{
return count($this->points);
}

public function getPoints()
{
return $this->points;
}

public function getPoint($index)
{
return $this->points[$index];
parent::__construct('polygon', $points);
}

public function setPoint($index, $point)
{
$this->points[$index] = $point;
return $this;
}

public function toXMLString()
{
$s = '<polygon';

$s .= ' points="';
for ($i = 0, $n = count($this->points); $i < $n; ++$i) {
$point = $this->points[$i];
if ($i > 0) {
$s .= ' ';
}
$s .= $point[0].','.$point[1];
}
$s .= '"';

$this->addStylesToXMLString($s);
$this->addAttributesToXMLString($s);

$s .= ' />';

return $s;
}

public function draw(SVGRenderingHelper $rh, $scaleX, $scaleY, $offsetX = 0, $offsetY = 0)
{
$rh->push();

$opacity = $this->getStyle('opacity');
if (isset($opacity) && is_numeric($opacity)) {
$opacity = floatval($opacity);
$rh->scaleOpacity($opacity);
}

// original (document fragment) width for unit parsing
$ow = $rh->getWidth() / $scaleX;

$p = array();
$np = count($this->points);

for ($i = 0; $i < $np; ++$i) {
$point = $this->points[$i];
$p[] = ($offsetX + $point[0]) * $scaleX;
$p[] = ($offsetY + $point[1]) * $scaleY;
}

$fill = $this->getComputedStyle('fill');
if (isset($fill) && $fill !== 'none') {
$fillColor = SVG::parseColor($fill, true);
$rh->fillPolygon($p, $np, $fillColor);
}

$stroke = $this->getComputedStyle('stroke');
if (isset($stroke) && $stroke !== 'none') {
$strokeColor = SVG::parseColor($stroke, true);
$rh->setStrokeWidth(SVG::convertUnit($this->getComputedStyle('stroke-width'), $ow) * $scaleX);
$rh->drawPolygon($p, $np, $strokeColor);
}

$rh->pop();
protected function drawOutline(SVGRenderingHelper $rh, $points, $numPoints, $strokeColor) {
$rh->drawPolygon($points, $numPoints, $strokeColor);
}
}
120 changes: 120 additions & 0 deletions src/Nodes/Shapes/SVGPolygonalShape.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

namespace JangoBrick\SVG\Nodes\Shapes;

use JangoBrick\SVG\Nodes\SVGNode;
use JangoBrick\SVG\SVG;
use JangoBrick\SVG\SVGRenderingHelper;

abstract class SVGPolygonalShape extends SVGNode
{
private $tagName;
private $points;

public function __construct($tagName, $points)
{
parent::__construct();

$this->tagName = $tagName;
$this->points = $points;
}

public function addPoint($a, $b = null)
{
if (!is_array($a)) {
$a = array($a, $b);
}

$this->points[] = $a;
return $this;
}

public function removePoint($index)
{
array_splice($this->points, $index, 1);
return $this;
}

public function countPoints()
{
return count($this->points);
}

public function getPoints()
{
return $this->points;
}

public function getPoint($index)
{
return $this->points[$index];
}

public function setPoint($index, $point)
{
$this->points[$index] = $point;
return $this;
}

public function toXMLString()
{
$s = '<'.$this->tagName;

$s .= ' points="';
for ($i = 0, $n = count($this->points); $i < $n; ++$i) {
$point = $this->points[$i];
if ($i > 0) {
$s .= ' ';
}
$s .= $point[0].','.$point[1];
}
$s .= '"';

$this->addStylesToXMLString($s);
$this->addAttributesToXMLString($s);

$s .= ' />';

return $s;
}

public function draw(SVGRenderingHelper $rh, $scaleX, $scaleY, $offsetX = 0, $offsetY = 0)
{
$rh->push();

$opacity = $this->getStyle('opacity');
if (isset($opacity) && is_numeric($opacity)) {
$opacity = floatval($opacity);
$rh->scaleOpacity($opacity);
}

// original (document fragment) width for unit parsing
$ow = $rh->getWidth() / $scaleX;

$p = array();
$np = count($this->points);

for ($i = 0; $i < $np; ++$i) {
$point = $this->points[$i];
$p[] = ($offsetX + $point[0]) * $scaleX;
$p[] = ($offsetY + $point[1]) * $scaleY;
}

$fill = $this->getComputedStyle('fill');
if (isset($fill) && $fill !== 'none') {
$fillColor = SVG::parseColor($fill, true);
$rh->fillPolygon($p, $np, $fillColor);
}

$stroke = $this->getComputedStyle('stroke');
if (isset($stroke) && $stroke !== 'none') {
$strokeColor = SVG::parseColor($stroke, true);
$rh->setStrokeWidth(SVG::convertUnit($this->getComputedStyle('stroke-width'), $ow) * $scaleX);
$this->drawOutline($rh, $p, $np, $strokeColor);
}

$rh->pop();
}

abstract protected function drawOutline(SVGRenderingHelper $rh, $points, $numPoints, $strokeColor);
}
106 changes: 4 additions & 102 deletions src/Nodes/Shapes/SVGPolyline.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,114 +2,16 @@

namespace JangoBrick\SVG\Nodes\Shapes;

use JangoBrick\SVG\Nodes\SVGNode;
use JangoBrick\SVG\SVG;
use JangoBrick\SVG\SVGRenderingHelper;

class SVGPolyline extends SVGNode
class SVGPolyline extends SVGPolygonalShape
{
private $points;

public function __construct($points = array())
{
parent::__construct();

$this->points = $points;
}

public function addPoint($a, $b = null)
{
if (!is_array($a)) {
$a = array($a, $b);
}

$this->points[] = $a;
}

public function removePoint($index)
{
array_splice($this->points, $index, 1);
return $this;
}

public function countPoints()
{
return count($this->points);
}

public function getPoints()
{
return $this->points;
}

public function getPoint($index)
{
return $this->points[$index];
parent::__construct('polyline', $points);
}

public function setPoint($index, $point)
{
$this->points[$index] = $point;
return $this;
}

public function toXMLString()
{
$s = '<polyline';

$s .= ' points="';
for ($i = 0, $n = count($this->points); $i < $n; ++$i) {
$point = $this->points[$i];
if ($i > 0) {
$s .= ' ';
}
$s .= $point[0].','.$point[1];
}
$s .= '"';

$this->addStylesToXMLString($s);
$this->addAttributesToXMLString($s);

$s .= ' />';

return $s;
}

public function draw(SVGRenderingHelper $rh, $scaleX, $scaleY, $offsetX = 0, $offsetY = 0)
{
$rh->push();

$opacity = $this->getStyle('opacity');
if (isset($opacity) && is_numeric($opacity)) {
$opacity = floatval($opacity);
$rh->scaleOpacity($opacity);
}

// original (document fragment) width for unit parsing
$ow = $rh->getWidth() / $scaleX;

$p = array();
$np = count($this->points);

for ($i = 0; $i < $np; ++$i) {
$point = $this->points[$i];
$p[] = ($offsetX + $point[0]) * $scaleX;
$p[] = ($offsetY + $point[1]) * $scaleY;
}

$fill = $this->getComputedStyle('fill');
if (isset($fill) && $fill !== 'none') {
$fillColor = SVG::parseColor($fill, true);
$rh->fillPolygon($p, $np, $fillColor);
}

$stroke = $this->getComputedStyle('stroke');
if (isset($stroke) && $stroke !== 'none') {
$strokeColor = SVG::parseColor($stroke, true);
$rh->setStrokeWidth(SVG::convertUnit($this->getComputedStyle('stroke-width'), $ow) * $scaleX);
$rh->drawPolyline($p, $np, $strokeColor);
}

$rh->pop();
protected function drawOutline(SVGRenderingHelper $rh, $points, $numPoints, $strokeColor) {
$rh->drawPolyline($points, $numPoints, $strokeColor);
}
}

0 comments on commit ca8c0a2

Please sign in to comment.