Skip to content

Commit e168b14

Browse files
author
Torben Köhn
committed
- Transformation stuff
1 parent 88ca89c commit e168b14

17 files changed

+593
-129
lines changed

src/Phim/Canvas/LayerInterface.php

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -32,42 +32,4 @@ public function add(ShapeInterface $shape);
3232
* @return $this
3333
*/
3434
public function remove(ShapeInterface $shape);
35-
36-
/**
37-
* @return TransformationInterface[]
38-
*/
39-
public function getTransformations();
40-
41-
/**
42-
* @param TransformationInterface $transformation
43-
*
44-
* @return $this
45-
*/
46-
public function addTransformation(TransformationInterface $transformation);
47-
48-
/**
49-
* @param TransformationInterface $transformation
50-
*
51-
* @return $this
52-
*/
53-
public function removeTransformation(TransformationInterface $transformation);
54-
55-
/**
56-
* @return FilterInterface[]
57-
*/
58-
public function getFilters();
59-
60-
/**
61-
* @param FilterInterface $filter
62-
*
63-
* @return $this
64-
*/
65-
public function addFilter(FilterInterface $filter);
66-
67-
/**
68-
* @param FilterInterface $filter
69-
*
70-
* @return $this
71-
*/
72-
public function removeFilter(FilterInterface $filter);
7335
}

src/Phim/Canvas/LayerTrait.php

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ trait LayerTrait
1010
{
1111

1212
private $shapes = [];
13-
private $transformations = [];
14-
private $filters = [];
1513

1614
public function getShapes()
1715
{
@@ -37,54 +35,4 @@ public function remove(ShapeInterface $shape)
3735

3836
return $this;
3937
}
40-
41-
public function getTransformations()
42-
{
43-
44-
return $this->transformations;
45-
}
46-
47-
public function addTransformation(TransformationInterface $transformation)
48-
{
49-
50-
$this->transformations[] = $transformation;
51-
52-
return $this;
53-
}
54-
55-
public function removeTransformation(TransformationInterface $transformation)
56-
{
57-
58-
$this->transformations = array_filter($this->transformations, function (TransformationInterface $t) use ($transformation) {
59-
60-
return $t !== $transformation;
61-
});
62-
63-
return $this;
64-
}
65-
66-
public function getFilters()
67-
{
68-
69-
return $this->filters;
70-
}
71-
72-
public function addFilter(FilterInterface $filter)
73-
{
74-
75-
$this->filters[] = $filter;
76-
77-
return $this;
78-
}
79-
80-
public function removeFilter(FilterInterface $filter)
81-
{
82-
83-
$this->filters = array_filter($this->filters, function (FilterInterface $f) use ($filter) {
84-
85-
return $f !== $filter;
86-
});
87-
88-
return $this;
89-
}
9038
}

src/Phim/CanvasTrait.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,58 @@
33
namespace Phim;
44

55
use Phim\Canvas\Layer;
6+
use Phim\Canvas\LayerInterface;
67

8+
/**
9+
* Class CanvasTrait
10+
*
11+
* @package Phim
12+
*/
713
trait CanvasTrait
814
{
915
use SizeTrait;
10-
16+
17+
/**
18+
* @var LayerInterface[]
19+
*/
1120
private $layers = [];
12-
21+
22+
/**
23+
* @return LayerInterface[]
24+
*/
1325
public function getLayers()
1426
{
1527

1628
return $this->layers;
1729
}
1830

31+
/**
32+
* @param $name
33+
*
34+
* @return bool
35+
*/
1936
public function hasLayer($name)
2037
{
2138

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

42+
/**
43+
* @param $name
44+
*
45+
* @return LayerInterface
46+
*/
2547
public function getLayer($name)
2648
{
2749

2850
return $this->layers[$name];
2951
}
3052

53+
/**
54+
* @param $name
55+
*
56+
* @return LayerInterface
57+
*/
3158
public function createLayer($name)
3259
{
3360

src/Phim/Engine/GdEngine.php

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
use Phim\Format\PngFormat;
99
use Phim\FormatInterface;
1010
use Phim\Path\AnchorInterface;
11+
use Phim\PathInterface;
12+
use Phim\Point;
13+
use Phim\PointInterface;
14+
use Phim\StyleInterface;
1115

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

40+
/**
41+
* @param PathInterface $path
42+
* @param StyleInterface $style
43+
*
44+
* @return AnchorInterface[]
45+
*
46+
*/
47+
private function transformPathAnchors(PathInterface $path, StyleInterface $style)
48+
{
49+
50+
$anchors = $path->getAnchors();
51+
$origin = $style->getTransformOrigin();
52+
$transformations = $style->getTransformations();
53+
54+
if ($origin) {
55+
56+
$bounds = $path->getBounds();
57+
58+
//Map relative values to absolute values
59+
$origin = new Point($bounds->getWidth() * $origin->getX(), $bounds->getHeight() * $origin->getY());
60+
}
61+
62+
return array_map(function (AnchorInterface $anchor) use ($transformations, $origin) {
63+
64+
//Transform the anchor
65+
foreach ($transformations as $transformation) {
66+
67+
$anchor = $transformation->transformAnchor($anchor, $origin);
68+
}
69+
70+
return $anchor;
71+
}, $anchors);
72+
}
73+
3674
public function render(CanvasInterface $canvas, FormatInterface $format)
3775
{
3876

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

6098
$path = $shape->getPath();
99+
$anchors = $this->transformPathAnchors($path, $style);
61100

62-
//Convert anchors to simple polygon points for starters
101+
//Convert anchors to simple polygon points for GD for starters
63102
$points = [];
64-
$count = count($path->getAnchors());
65-
foreach ($path->getAnchors() as $anchor) {
103+
$count = count($anchors);
104+
foreach ($anchors as $anchor) {
66105

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

89127
if ($context) {
90128

src/Phim/FactoryInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function size($width, $height);
5252
*
5353
* @return StyleInterface
5454
*/
55-
public function style(array $options);
55+
public function style(array $options = null);
5656

5757
/**
5858
* @param array $points

src/Phim/FactoryTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function image($path)
3434
//TODO: implement
3535
}
3636

37-
public function style(array $options)
37+
public function style(array $options = null)
3838
{
3939

4040
return new Style($options);

src/Phim/Geometry/RectangleInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@ public function getRightBottom();
2727
public function setRightBottom(PointInterface $rightBottom);
2828
public function getLeftBottom();
2929
public function setLeftBottom(PointInterface $leftBottom);
30+
31+
public function getCenter();
3032
}

src/Phim/Geometry/RectangleTrait.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ public function setLeftBottom(PointInterface $leftBottom)
130130
return $this;
131131
}
132132

133+
public function getCenter()
134+
{
135+
136+
return new Point($this->getX() + $this->getWidth() / 2, $this->getY() + $this->getHeight() / 2);
137+
}
138+
133139
public function toPath()
134140
{
135141

src/Phim/Path/Anchor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Phim\Path;
44

55
use Phim\PointInterface;
6+
use Phim\TransformationInterface;
67

78
class Anchor implements AnchorInterface
89
{

src/Phim/PathInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Phim;
44

5+
use Phim\Geometry\RectangleInterface;
56
use Phim\Path\AnchorInterface;
67

78
/**
@@ -30,4 +31,9 @@ public function addAnchor(AnchorInterface $anchor);
3031
* @return $this
3132
*/
3233
public function removeAnchor(AnchorInterface $anchor);
34+
35+
/**
36+
* @return RectangleInterface
37+
*/
38+
public function getBounds();
3339
}

0 commit comments

Comments
 (0)