Skip to content

Commit 3166f53

Browse files
ssddanbrownbsweeney
authored andcommitted
Added support for percent-based x,y positions
1 parent d85d47d commit 3166f53

File tree

6 files changed

+40
-19
lines changed

6 files changed

+40
-19
lines changed

src/Svg/Tag/Circle.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
namespace Svg\Tag;
1010

11+
use Svg\Style;
12+
1113
class Circle extends Shape
1214
{
1315
protected $cx = 0;
@@ -17,10 +19,12 @@ class Circle extends Shape
1719
public function start($attributes)
1820
{
1921
if (isset($attributes['cx'])) {
20-
$this->cx = $attributes['cx'];
22+
$width = $this->document->getWidth();
23+
$this->cx = Style::convertSize($attributes['cx'], $width);
2124
}
2225
if (isset($attributes['cy'])) {
23-
$this->cy = $attributes['cy'];
26+
$height = $this->document->getHeight();
27+
$this->cy = Style::convertSize($attributes['cy'], $height);
2428
}
2529
if (isset($attributes['r'])) {
2630
$this->r = $attributes['r'];

src/Svg/Tag/Ellipse.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
namespace Svg\Tag;
1010

11+
use Svg\Style;
12+
1113
class Ellipse extends Shape
1214
{
1315
protected $cx = 0;
@@ -20,10 +22,12 @@ public function start($attributes)
2022
parent::start($attributes);
2123

2224
if (isset($attributes['cx'])) {
23-
$this->cx = $attributes['cx'];
25+
$width = $this->document->getWidth();
26+
$this->cx = Style::convertSize($attributes['cx'], $width);
2427
}
2528
if (isset($attributes['cy'])) {
26-
$this->cy = $attributes['cy'];
29+
$height = $this->document->getHeight();
30+
$this->cy = Style::convertSize($attributes['cy'], $height);
2731
}
2832
if (isset($attributes['rx'])) {
2933
$this->rx = $attributes['rx'];

src/Svg/Tag/Image.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
namespace Svg\Tag;
1010

11+
use Svg\Style;
12+
1113
class Image extends AbstractTag
1214
{
1315
protected $x = 0;
@@ -28,15 +30,15 @@ protected function before($attributes)
2830

2931
public function start($attributes)
3032
{
31-
$document = $this->document;
3233
$height = $this->document->getHeight();
3334
$this->y = $height;
3435

3536
if (isset($attributes['x'])) {
36-
$this->x = $attributes['x'];
37+
$width = $this->document->getWidth();
38+
$this->x = Style::convertSize($attributes['x'], $width);
3739
}
3840
if (isset($attributes['y'])) {
39-
$this->y = $height - $attributes['y'];
41+
$this->y = $height - Style::convertSize($attributes['y'], $height);
4042
}
4143

4244
if (isset($attributes['width'])) {
@@ -50,9 +52,9 @@ public function start($attributes)
5052
$this->href = $attributes['xlink:href'];
5153
}
5254

53-
$document->getSurface()->transform(1, 0, 0, -1, 0, $height);
55+
$this->document->getSurface()->transform(1, 0, 0, -1, 0, $height);
5456

55-
$document->getSurface()->drawImage($this->href, $this->x, $this->y, $this->width, $this->height);
57+
$this->document->getSurface()->drawImage($this->href, $this->x, $this->y, $this->width, $this->height);
5658
}
5759

5860
protected function after()

src/Svg/Tag/Line.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
namespace Svg\Tag;
1010

11+
use Svg\Style;
12+
1113
class Line extends Shape
1214
{
1315
protected $x1 = 0;
@@ -18,17 +20,20 @@ class Line extends Shape
1820

1921
public function start($attributes)
2022
{
23+
$height = $this->document->getHeight();
24+
$width = $this->document->getWidth();
25+
2126
if (isset($attributes['x1'])) {
22-
$this->x1 = $attributes['x1'];
27+
$this->x1 = Style::convertSize($attributes['x1'], $width);
2328
}
2429
if (isset($attributes['y1'])) {
25-
$this->y1 = $attributes['y1'];
30+
$this->y1 = Style::convertSize($attributes['y1'], $height);
2631
}
2732
if (isset($attributes['x2'])) {
28-
$this->x2 = $attributes['x2'];
33+
$this->x2 = Style::convertSize($attributes['x2'], $width);
2934
}
3035
if (isset($attributes['y2'])) {
31-
$this->y2 = $attributes['y2'];
36+
$this->y2 = Style::convertSize($attributes['y2'], $height);
3237
}
3338

3439
$surface = $this->document->getSurface();

src/Svg/Tag/Rect.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
namespace Svg\Tag;
1010

11+
use Svg\Style;
12+
1113
class Rect extends Shape
1214
{
1315
protected $x = 0;
@@ -20,10 +22,12 @@ class Rect extends Shape
2022
public function start($attributes)
2123
{
2224
if (isset($attributes['x'])) {
23-
$this->x = $attributes['x'];
25+
$width = $this->document->getWidth();
26+
$this->x = Style::convertSize($attributes['x'], $width);
2427
}
2528
if (isset($attributes['y'])) {
26-
$this->y = $attributes['y'];
29+
$height = $this->document->getHeight();
30+
$this->y = Style::convertSize($attributes['y'], $height);
2731
}
2832

2933
if (isset($attributes['width'])) {

src/Svg/Tag/Text.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
namespace Svg\Tag;
1010

11+
use Svg\Style;
12+
1113
class Text extends Shape
1214
{
1315
protected $x = 0;
@@ -16,18 +18,18 @@ class Text extends Shape
1618

1719
public function start($attributes)
1820
{
19-
$document = $this->document;
2021
$height = $this->document->getHeight();
2122
$this->y = $height;
2223

2324
if (isset($attributes['x'])) {
24-
$this->x = $attributes['x'];
25+
$width = $this->document->getWidth();
26+
$this->x = Style::convertSize($attributes['x'], $width);
2527
}
2628
if (isset($attributes['y'])) {
27-
$this->y = $height - $attributes['y'];
29+
$this->y = $height - Style::convertSize($attributes['y'], $height);
2830
}
2931

30-
$document->getSurface()->transform(1, 0, 0, -1, 0, $height);
32+
$this->document->getSurface()->transform(1, 0, 0, -1, 0, $height);
3133
}
3234

3335
public function end()

0 commit comments

Comments
 (0)