Skip to content

Commit f1ccba6

Browse files
committed
draft svg polygon
1 parent 5bec87e commit f1ccba6

File tree

1 file changed

+67
-22
lines changed

1 file changed

+67
-22
lines changed

src/SVG.php

Lines changed: 67 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,12 @@ abstract class SVG extends \Com\Tecnick\Pdf\Text
409409
*/
410410
protected const SVGMINPNTLEN = 0.01;
411411

412+
/**
413+
* Default SVG maximum value for float.
414+
*
415+
* @var float
416+
*/
417+
protected const SVGMAXVAL = 2147483647.0;
412418

413419
/**
414420
* Array of inheritable SVG properties.
@@ -901,9 +907,9 @@ protected function getSVGPath(
901907
'y' => 0.0,
902908
'x0' => 0.0,
903909
'y0' => 0.0,
904-
'xmin' => 2147483647.0,
910+
'xmin' => self::SVGMAXVAL,
905911
'xmax' => 0.0,
906-
'ymin' => 2147483647.0,
912+
'ymin' => self::SVGMAXVAL,
907913
'ymax' => 0.0,
908914
'xinit' => 0.0,
909915
'yinit' => 0.0,
@@ -2515,8 +2521,8 @@ protected function handleSVGTagStart(
25152521
'circle' => $this->parseSVGTagSTARTcircle($soid, $attribs['attr'], $svgstyle),
25162522
'ellipse' => $this->parseSVGTagSTARTellipse($soid, $attribs['attr'], $svgstyle),
25172523
'line' => $this->parseSVGTagSTARTline($soid, $attribs['attr'], $svgstyle),
2518-
'polyline' => $this->parseSVGTagSTARTpolyline($soid),
2519-
'polygon' => $this->parseSVGTagSTARTpolygon($soid),
2524+
'polyline' => $this->parseSVGTagSTARTpolygon($soid, $attribs['attr'], $svgstyle),
2525+
'polygon' => $this->parseSVGTagSTARTpolygon($soid, $attribs['attr'], $svgstyle),
25202526
'image' => $this->parseSVGTagSTARTimage($soid),
25212527
'text' => $this->parseSVGTagSTARTtext($soid),
25222528
'tspan' => $this->parseSVGTagSTARTtspan($soid),
@@ -3107,35 +3113,74 @@ protected function parseSVGTagSTARTline(int $soid, array $attr, array $svgstyle)
31073113
}
31083114

31093115
/**
3110-
* Parse the SVG Start tag 'polyline'.
3116+
* Parse the SVG Start tag 'polygon'.
31113117
*
31123118
* @param int $soid ID of the current SVG object.
3119+
* @param TSVGAttributes $attr SVG attributes.
3120+
* @param TSVGStyle $svgstyle Current SVG style.
31133121
*
31143122
* @return void
31153123
*/
3116-
protected function parseSVGTagSTARTpolyline(int $soid)
3124+
protected function parseSVGTagSTARTpolygon(int $soid, array $attr, array $svgstyle)
31173125
{
31183126
if (!empty($this->svgobjs[$soid]['textmode']['invisible'])) {
31193127
return;
31203128
}
3121-
$soid = $soid; // @phpstan-ignore-line
3122-
//@TODO
3123-
}
3124-
3125-
/**
3126-
* Parse the SVG Start tag 'polygon'.
3127-
*
3128-
* @param int $soid ID of the current SVG object.
3129-
*
3130-
* @return void
3131-
*/
3132-
protected function parseSVGTagSTARTpolygon(int $soid)
3133-
{
3134-
if (!empty($this->svgobjs[$soid]['textmode']['invisible'])) {
3129+
$attrpoints = (!empty($attr['points']) ? trim($attr['points']) : '0 0');
3130+
// note that point may use a complex syntax not covered here
3131+
$points = preg_split('/[\,\s]+/si', $attrpoints);
3132+
if (!is_array($points) || count($points) < 4) {
31353133
return;
31363134
}
3137-
$soid = $soid; // @phpstan-ignore-line
3138-
//@TODO
3135+
$pset = [];
3136+
$xmin = self::SVGMAXVAL;
3137+
$xmax = 0.0;
3138+
$ymin = self::SVGMAXVAL;
3139+
$ymax = 0.0;
3140+
foreach ($points as $key => $val) {
3141+
$pset[$key] = $this->toUnit(
3142+
$this->getUnitValuePoints($val, self::REFUNITVAL, self::SVGUNIT)
3143+
);
3144+
if (($key % 2) == 0) {
3145+
// X coordinate
3146+
$xmin = min($xmin, $pset[$key]);
3147+
$xmax = max($xmax, $pset[$key]);
3148+
} else {
3149+
// Y coordinate
3150+
$ymin = min($ymin, $pset[$key]);
3151+
$ymax = max($ymax, $pset[$key]);
3152+
}
3153+
}
3154+
$posx = $xmin;
3155+
$posy = $ymin;
3156+
$width = ($xmax - $xmin);
3157+
$height = ($ymax - $ymin);
3158+
if ($this->svgobjs[$soid]['clipmode']) {
3159+
$this->svgobjs[$soid]['out'] .= $this->getOutSVGTransformation($svgstyle['transfmatrix']);
3160+
$this->svgobjs[$soid]['out'] .= $this->graph->getPolygon(
3161+
$pset,
3162+
'CNZ',
3163+
);
3164+
return;
3165+
}
3166+
$this->svgobjs[$soid]['out'] .= $this->graph->getStartTransform();
3167+
$this->svgobjs[$soid]['out'] .= $this->getOutSVGTransformation($svgstyle['transfmatrix']);
3168+
$obstyle = $this->parseSVGStyle(
3169+
$this->svgobjs[$soid],
3170+
$posx,
3171+
$posy,
3172+
$width,
3173+
$height,
3174+
'getPolygon',
3175+
[$pset, 'CNZ']
3176+
);
3177+
if (!empty($obstyle)) {
3178+
$this->svgobjs[$soid]['out'] .= $this->graph->getPolygon(
3179+
$pset,
3180+
$obstyle,
3181+
);
3182+
}
3183+
$this->svgobjs[$soid]['out'] .= $this->graph->getStopTransform();
31393184
}
31403185

31413186
/**

0 commit comments

Comments
 (0)