88use Phim \Format \PngFormat ;
99use Phim \FormatInterface ;
1010use Phim \Path \AnchorInterface ;
11+ use Phim \PathInterface ;
12+ use Phim \Point ;
13+ use Phim \PointInterface ;
14+ use Phim \StyleInterface ;
1115
1216class 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
0 commit comments