Skip to content

Commit dcc2eaf

Browse files
committed
draft SVG: path bbox
1 parent 5a80f8c commit dcc2eaf

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

src/SVG.php

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,13 @@ protected function getSVGPath(
10291029
$coord['firstcmd'] = false;
10301030
}
10311031

1032+
$this->bbox[] = [
1033+
'x' => $coord['xmin'],
1034+
'y' => $coord['ymin'],
1035+
'w' => ($coord['xmax'] - $coord['xmin']),
1036+
'h' => ($coord['ymax'] - $coord['ymin']),
1037+
];
1038+
10321039
$out .= $pop;
10331040

10341041
return $out;
@@ -1914,6 +1921,7 @@ protected function parseSVGStyleGradient(
19141921
$out .= $this->graph->getStartTransform();
19151922

19161923
if (!empty($clip_fnc)) {
1924+
$bboxid_start = array_key_last($this->bbox);
19171925
$fnout = null;
19181926
if (method_exists($this, $clip_fnc)) {
19191927
$fnout = $this->$clip_fnc(...$clip_par);
@@ -1922,14 +1930,18 @@ protected function parseSVGStyleGradient(
19221930
}
19231931
if (is_string($fnout)) {
19241932
$out .= $fnout;
1925-
} elseif (
1926-
is_array($fnout) && (count($fnout) == 4)
1933+
}
1934+
$bboxid_last = array_key_last($this->bbox);
1935+
1936+
if (
1937+
($bboxid_last > $bboxid_start)
19271938
&& (!isset($gradient['type']) || ($gradient['type'] != 3))
19281939
) {
1929-
$grx = is_numeric($fnout[0]) ? (float)$fnout[0] : 0.0;
1930-
$gry = is_numeric($fnout[1]) ? (float)$fnout[1] : 0.0;
1931-
$grw = is_numeric($fnout[2]) ? (float)$fnout[2] : 0.0;
1932-
$grh = is_numeric($fnout[3]) ? (float)$fnout[3] : 0.0;
1940+
$bbox = $this->bbox[$bboxid_last];
1941+
$grx = is_numeric($bbox['x']) ? (float)$bbox['x'] : 0.0;
1942+
$gry = is_numeric($bbox['y']) ? (float)$bbox['y'] : 0.0;
1943+
$grw = is_numeric($bbox['w']) ? (float)$bbox['w'] : 0.0;
1944+
$grh = is_numeric($bbox['h']) ? (float)$bbox['h'] : 0.0;
19331945
}
19341946
}
19351947

@@ -2889,9 +2901,6 @@ protected function parseSVGTagSTARTlinearGradient(int $soid, array $attr): strin
28892901
$this->svgobjs[$soid]['gradients'][$gid]['gradientUnits'] = 'objectBoundingBox';
28902902
}
28912903
// $attr['spreadMethod']
2892-
2893-
/** @var TRefUnitValues $ref */
2894-
$ref = $this->svgobjs[$soid]['refunitval'];
28952904
if (
28962905
((!isset($attr['x1'])) && (!isset($attr['y1']))
28972906
&& (!isset($attr['x2'])) && (!isset($attr['y2'])))
@@ -2901,14 +2910,13 @@ protected function parseSVGTagSTARTlinearGradient(int $soid, array $attr): strin
29012910
|| (isset($attr['y2']) && (substr($attr['y2'], -1) == '%')))
29022911
) {
29032912
$this->svgobjs[$soid]['gradients'][$gid]['mode'] = 'percentage';
2904-
$ref['parent'] = 100.0;
29052913
} else {
29062914
$this->svgobjs[$soid]['gradients'][$gid]['mode'] = 'measure';
29072915
}
2908-
$px1 = isset($attr['x1']) ? $this->svgUnitToUnit($attr['x1'], $soid, $ref) : 0.0;
2909-
$py1 = isset($attr['y1']) ? $this->svgUnitToUnit($attr['y1'], $soid, $ref) : 0.0;
2910-
$px2 = isset($attr['x2']) ? $this->svgUnitToUnit($attr['x2'], $soid, $ref) : 100.0;
2911-
$py2 = isset($attr['y2']) ? $this->svgUnitToUnit($attr['y2'], $soid, $ref) : 0.0;
2916+
$px1 = $attr['x1'] ?? 0.0;
2917+
$py1 = $attr['y1'] ?? 0.0;
2918+
$px2 = $attr['x2'] ?? 100.0;
2919+
$py2 = $attr['y2'] ?? 0.0;
29122920
if (isset($attr['gradientTransform'])) {
29132921
$this->svgobjs[$soid]['gradients'][$gid]['gradientTransform'] =
29142922
$this->getSVGTransformMatrix($attr['gradientTransform']);
@@ -2918,7 +2926,6 @@ protected function parseSVGTagSTARTlinearGradient(int $soid, array $attr): strin
29182926
// gradient is defined on another place
29192927
$this->svgobjs[$soid]['gradients'][$gid]['xref'] = substr($attr['xlink:href'], 1);
29202928
}
2921-
29222929
return '';
29232930
}
29242931

@@ -2951,9 +2958,6 @@ protected function parseSVGTagSTARTradialGradient(int $soid, array $attr): strin
29512958
$this->svgobjs[$soid]['gradients'][$gid]['gradientUnits'] = 'objectBoundingBox';
29522959
}
29532960
// $attr['spreadMethod']
2954-
2955-
/** @var TRefUnitValues $ref */
2956-
$ref = $this->svgobjs[$soid]['refunitval'];
29572961
if (
29582962
((!isset($attr['cx'])) && (!isset($attr['cy'])))
29592963
|| ((isset($attr['cx']) && (substr($attr['cx'], -1) == '%'))
@@ -2962,7 +2966,6 @@ protected function parseSVGTagSTARTradialGradient(int $soid, array $attr): strin
29622966
$this->svgobjs[$soid]['gradients'][$gid]['mode'] = 'percentage';
29632967
} elseif (isset($attr['r']) && is_numeric($attr['r']) && ($attr['r']) <= 1) {
29642968
$this->svgobjs[$soid]['gradients'][$gid]['mode'] = 'ratio';
2965-
$ref['parent'] = 100.0;
29662969
} else {
29672970
$this->svgobjs[$soid]['gradients'][$gid]['mode'] = 'measure';
29682971
}
@@ -2976,7 +2979,7 @@ protected function parseSVGTagSTARTradialGradient(int $soid, array $attr): strin
29762979
$this->getSVGTransformMatrix($attr['gradientTransform']);
29772980
}
29782981
$this->svgobjs[$soid]['gradients'][$gid]['coords'] = [$pcx, $pcy, $pfx, $pfy, $grr];
2979-
if (isset($attr['xlink:href']) && !empty($attr['xlink:href'])) {
2982+
if (!empty($attr['xlink:href'])) {
29802983
// gradient is defined on another place
29812984
$this->svgobjs[$soid]['gradients'][$gid]['xref'] = substr($attr['xlink:href'], 1);
29822985
}

0 commit comments

Comments
 (0)