Skip to content

Commit

Permalink
- Transformation stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Torben Köhn committed Feb 17, 2017
1 parent 88ca89c commit e168b14
Show file tree
Hide file tree
Showing 17 changed files with 593 additions and 129 deletions.
38 changes: 0 additions & 38 deletions src/Phim/Canvas/LayerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,42 +32,4 @@ public function add(ShapeInterface $shape);
* @return $this
*/
public function remove(ShapeInterface $shape);

/**
* @return TransformationInterface[]
*/
public function getTransformations();

/**
* @param TransformationInterface $transformation
*
* @return $this
*/
public function addTransformation(TransformationInterface $transformation);

/**
* @param TransformationInterface $transformation
*
* @return $this
*/
public function removeTransformation(TransformationInterface $transformation);

/**
* @return FilterInterface[]
*/
public function getFilters();

/**
* @param FilterInterface $filter
*
* @return $this
*/
public function addFilter(FilterInterface $filter);

/**
* @param FilterInterface $filter
*
* @return $this
*/
public function removeFilter(FilterInterface $filter);
}
52 changes: 0 additions & 52 deletions src/Phim/Canvas/LayerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ trait LayerTrait
{

private $shapes = [];
private $transformations = [];
private $filters = [];

public function getShapes()
{
Expand All @@ -37,54 +35,4 @@ public function remove(ShapeInterface $shape)

return $this;
}

public function getTransformations()
{

return $this->transformations;
}

public function addTransformation(TransformationInterface $transformation)
{

$this->transformations[] = $transformation;

return $this;
}

public function removeTransformation(TransformationInterface $transformation)
{

$this->transformations = array_filter($this->transformations, function (TransformationInterface $t) use ($transformation) {

return $t !== $transformation;
});

return $this;
}

public function getFilters()
{

return $this->filters;
}

public function addFilter(FilterInterface $filter)
{

$this->filters[] = $filter;

return $this;
}

public function removeFilter(FilterInterface $filter)
{

$this->filters = array_filter($this->filters, function (FilterInterface $f) use ($filter) {

return $f !== $filter;
});

return $this;
}
}
31 changes: 29 additions & 2 deletions src/Phim/CanvasTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,58 @@
namespace Phim;

use Phim\Canvas\Layer;
use Phim\Canvas\LayerInterface;

/**
* Class CanvasTrait
*
* @package Phim
*/
trait CanvasTrait
{
use SizeTrait;


/**
* @var LayerInterface[]
*/
private $layers = [];


/**
* @return LayerInterface[]
*/
public function getLayers()
{

return $this->layers;
}

/**
* @param $name
*
* @return bool
*/
public function hasLayer($name)
{

return isset($this->layers[$name]);
}

/**
* @param $name
*
* @return LayerInterface
*/
public function getLayer($name)
{

return $this->layers[$name];
}

/**
* @param $name
*
* @return LayerInterface
*/
public function createLayer($name)
{

Expand Down
48 changes: 43 additions & 5 deletions src/Phim/Engine/GdEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
use Phim\Format\PngFormat;
use Phim\FormatInterface;
use Phim\Path\AnchorInterface;
use Phim\PathInterface;
use Phim\Point;
use Phim\PointInterface;
use Phim\StyleInterface;

class GdEngine implements EngineInterface
{
Expand All @@ -33,6 +37,40 @@ private function allocate($im, ColorInterface $color)
return imagecolorallocatealpha($im, $color->getRed(), $color->getGreen(), $color->getBlue(), (int)((1 - $color->getAlpha()) * 127));
}

/**
* @param PathInterface $path
* @param StyleInterface $style
*
* @return AnchorInterface[]
*
*/
private function transformPathAnchors(PathInterface $path, StyleInterface $style)
{

$anchors = $path->getAnchors();
$origin = $style->getTransformOrigin();
$transformations = $style->getTransformations();

if ($origin) {

$bounds = $path->getBounds();

//Map relative values to absolute values
$origin = new Point($bounds->getWidth() * $origin->getX(), $bounds->getHeight() * $origin->getY());
}

return array_map(function (AnchorInterface $anchor) use ($transformations, $origin) {

//Transform the anchor
foreach ($transformations as $transformation) {

$anchor = $transformation->transformAnchor($anchor, $origin);
}

return $anchor;
}, $anchors);
}

public function render(CanvasInterface $canvas, FormatInterface $format)
{

Expand All @@ -58,13 +96,13 @@ public function render(CanvasInterface $canvas, FormatInterface $format)
imagesetthickness($im, $style->getStrokeWidth());

$path = $shape->getPath();
$anchors = $this->transformPathAnchors($path, $style);

//Convert anchors to simple polygon points for starters
//Convert anchors to simple polygon points for GD for starters
$points = [];
$count = count($path->getAnchors());
foreach ($path->getAnchors() as $anchor) {
$count = count($anchors);
foreach ($anchors as $anchor) {

//TODO: Apply transformations here
//TODO: Apply positionFilters here
$points[] = $anchor->getX();
$points[] = $anchor->getY();
Expand All @@ -84,7 +122,7 @@ public function render(CanvasInterface $canvas, FormatInterface $format)
//As imagepolygon doesn't draw open polygons and imageopenpolygon is too new, we iterate the anchors and draw lines
/** @var AnchorInterface $context */
$context = null;
foreach ($path->getAnchors() as $anchor) {
foreach ($anchors as $anchor) {

if ($context) {

Expand Down
2 changes: 1 addition & 1 deletion src/Phim/FactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function size($width, $height);
*
* @return StyleInterface
*/
public function style(array $options);
public function style(array $options = null);

/**
* @param array $points
Expand Down
2 changes: 1 addition & 1 deletion src/Phim/FactoryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function image($path)
//TODO: implement
}

public function style(array $options)
public function style(array $options = null)
{

return new Style($options);
Expand Down
2 changes: 2 additions & 0 deletions src/Phim/Geometry/RectangleInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ public function getRightBottom();
public function setRightBottom(PointInterface $rightBottom);
public function getLeftBottom();
public function setLeftBottom(PointInterface $leftBottom);

public function getCenter();
}
6 changes: 6 additions & 0 deletions src/Phim/Geometry/RectangleTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ public function setLeftBottom(PointInterface $leftBottom)
return $this;
}

public function getCenter()
{

return new Point($this->getX() + $this->getWidth() / 2, $this->getY() + $this->getHeight() / 2);
}

public function toPath()
{

Expand Down
1 change: 1 addition & 0 deletions src/Phim/Path/Anchor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Phim\Path;

use Phim\PointInterface;
use Phim\TransformationInterface;

class Anchor implements AnchorInterface
{
Expand Down
6 changes: 6 additions & 0 deletions src/Phim/PathInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Phim;

use Phim\Geometry\RectangleInterface;
use Phim\Path\AnchorInterface;

/**
Expand Down Expand Up @@ -30,4 +31,9 @@ public function addAnchor(AnchorInterface $anchor);
* @return $this
*/
public function removeAnchor(AnchorInterface $anchor);

/**
* @return RectangleInterface
*/
public function getBounds();
}
32 changes: 32 additions & 0 deletions src/Phim/PathTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

namespace Phim;

use Phim\Geometry\Rectangle;
use Phim\Path\AnchorInterface;

trait PathTrait
{

/**
* @var AnchorInterface[]
*/
private $anchors = [];

public function getAnchors()
Expand All @@ -33,4 +37,32 @@ public function removeAnchor(AnchorInterface $anchor)

return $this;
}

public function getBounds()
{

$lowestX = $highestX = $lowestY = $highestY = null;

foreach ($this->anchors as $anchor) {

if ($lowestX === null || $anchor->getX() < $lowestX)
$lowestX = $anchor->getX();

if ($highestX === null || $anchor->getX() > $highestX)
$highestX = $anchor->getX();

if ($lowestY === null || $anchor->getY() < $lowestY)
$lowestY = $anchor->getY();

if ($highestY === null || $anchor->getY() > $highestY)
$highestY = $anchor->getY();
}

return new Rectangle(
$lowestX,
$lowestY,
$highestX - $lowestX,
$highestY - $lowestY
);
}
}
Loading

0 comments on commit e168b14

Please sign in to comment.