Skip to content

Commit e02e6f7

Browse files
committed
Improve T/t command point calc when drawing a path
fixes #81
1 parent fe9b0c3 commit e02e6f7

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

src/Svg/Tag/Path.php

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ public function start($attributes)
138138
$tempControlY = null;
139139
$l = 0; //-((this.width / 2) + $this.pathOffset.x),
140140
$t = 0; //-((this.height / 2) + $this.pathOffset.y),
141-
$methodName = null;
142141

143142
foreach ($path as $current) {
144143
switch ($current[0]) { // first letter
@@ -333,23 +332,16 @@ public function start($attributes)
333332
$tempX = $x + $current[1];
334333
$tempY = $y + $current[2];
335334

336-
if (preg_match("/[QqTt]/", $previous[0])) {
337-
// If there is no previous command or if the previous command was not a Q, q, T or t,
338-
// assume the control point is coincident with the current point
335+
// calculate reflection of previous control points
336+
if (preg_match('/[QqT]/', $previous[0])) {
337+
$controlX = 2 * $x - $controlX;
338+
$controlY = 2 * $y - $controlY;
339+
} elseif ($previous[0] === 't') {
340+
$controlX = 2 * $x - $tempControlX;
341+
$controlY = 2 * $y - $tempControlY;
342+
} else {
339343
$controlX = $x;
340344
$controlY = $y;
341-
} else {
342-
if ($previous[0] === 't') {
343-
// calculate reflection of previous control points for t
344-
$controlX = 2 * $x - $tempControlX;
345-
$controlY = 2 * $y - $tempControlY;
346-
} else {
347-
if ($previous[0] === 'q') {
348-
// calculate reflection of previous control points for q
349-
$controlX = 2 * $x - $controlX;
350-
$controlY = 2 * $y - $controlY;
351-
}
352-
}
353345
}
354346

355347
$tempControlX = $controlX;
@@ -363,17 +355,21 @@ public function start($attributes)
363355
);
364356
$x = $tempX;
365357
$y = $tempY;
366-
$controlX = $x + $current[1];
367-
$controlY = $y + $current[2];
368358
break;
369359

370360
case 'T':
371361
$tempX = $current[1];
372362
$tempY = $current[2];
373363

374364
// calculate reflection of previous control points
375-
$controlX = 2 * $x - $controlX;
376-
$controlY = 2 * $y - $controlY;
365+
if (preg_match('/[QqTt]/', $previous[0])) {
366+
$controlX = 2 * $x - $controlX;
367+
$controlY = 2 * $y - $controlY;
368+
} else {
369+
$controlX = $x;
370+
$controlY = $y;
371+
}
372+
377373
$surface->quadraticCurveTo(
378374
$controlX + $l,
379375
$controlY + $t,
@@ -385,7 +381,6 @@ public function start($attributes)
385381
break;
386382

387383
case 'a':
388-
// TODO: optimize this
389384
$this->drawArc(
390385
$surface,
391386
$x + $l,

0 commit comments

Comments
 (0)