Skip to content

Commit 9d511f5

Browse files
committed
draft svg refactoring
1 parent 7b895a6 commit 9d511f5

File tree

2 files changed

+78
-80
lines changed

2 files changed

+78
-80
lines changed

examples/index.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,8 @@
118118
$pdf->page->addContent($iid12_out);
119119

120120
// test SVG
121-
//$svgid01 = $pdf->addSVG('./images/testsvg.svg');
121+
//$svgid01 = $pdf->addSVG('./images/tcpdf_box.svg');
122122
//$svgid01_out = $pdf->getSetSVG($svgid01, 0, 60, 100, 50, $page01['height']);
123-
//echo $svgid01_out; exit();
124123
//$pdf->page->addContent($svgid01_out);
125124

126125
// ----------

src/SVG.php

Lines changed: 77 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -2138,36 +2138,39 @@ protected function parseSVGStyle(
21382138
array $clip_par = [],
21392139
): string {
21402140
$sid = (int)array_key_last($this->svgobjs[$soid]['styles']);
2141+
$psid = max(0, $sid - 1);
21412142

21422143
if (empty($this->svgobjs[$soid]['styles'][$sid]['opacity'])) {
21432144
return '';
21442145
}
21452146

21462147
$this->parseSVGStyleClipPath($parser, $soid, $this->svgobjs[$soid]['clippaths']);
21472148

2148-
return $this->parseSVGStyleColor($this->svgobjs[$soid]['styles'][$sid]) .
2149-
$this->parseSVGStyleClip(
2150-
$this->svgobjs[$soid]['styles'][$sid],
2151-
$posx,
2152-
$posy,
2153-
$width,
2154-
$height
2155-
) .
2156-
$this->parseSVGStyleFill(
2157-
$this->svgobjs[$soid]['styles'][$sid],
2158-
$this->svgobjs[$soid]['gradients'],
2159-
$posx,
2160-
$posy,
2161-
$width,
2162-
$height,
2163-
$clip_fnc,
2164-
$clip_par
2165-
) .
2166-
$this->parseSVGStyleStroke($this->svgobjs[$soid]['styles'][$sid]) .
2167-
$this->parseSVGStyleFont(
2168-
$this->svgobjs[$soid]['styles'][$sid],
2169-
$this->svgobjs[$soid]['styles'][($sid - 1)]
2170-
);
2149+
$out = $this->parseSVGStyleColor($this->svgobjs[$soid]['styles'][$sid]);
2150+
$out .= $this->parseSVGStyleClip(
2151+
$this->svgobjs[$soid]['styles'][$sid],
2152+
$posx,
2153+
$posy,
2154+
$width,
2155+
$height
2156+
);
2157+
$out .= $this->parseSVGStyleFill(
2158+
$this->svgobjs[$soid]['styles'][$sid],
2159+
$this->svgobjs[$soid]['gradients'],
2160+
$posx,
2161+
$posy,
2162+
$width,
2163+
$height,
2164+
$clip_fnc,
2165+
$clip_par
2166+
);
2167+
$out .= $this->parseSVGStyleStroke($this->svgobjs[$soid]['styles'][$sid]);
2168+
$out .= $this->parseSVGStyleFont(
2169+
$this->svgobjs[$soid]['styles'][$sid],
2170+
$this->svgobjs[$soid]['styles'][$psid]
2171+
);
2172+
2173+
return $out;
21712174
}
21722175

21732176
/**
@@ -2388,7 +2391,7 @@ protected function parseSVGTagENDtext(int $soid): void
23882391
*
23892392
* @param \XMLParser $parser The XML parser calling the handler.
23902393
* @param string $name Name of the element for which this handler is called.
2391-
* @param TSVGAttribs $attribs Associative array with the element's attributes.
2394+
* @param TSVGAttributes $attr Associative array with the element's attributes.
23922395
* @param int $soid ID of the current SVG object.
23932396
* @param bool $clipmode Clip-path mode (optional).
23942397
* @param TTMatrix $ctm Current transformation matrix (optional).
@@ -2400,25 +2403,24 @@ protected function parseSVGTagENDtext(int $soid): void
24002403
protected function handleSVGTagStart(
24012404
\XMLParser $parser,
24022405
string $name,
2403-
array $attribs,
2406+
array $attr,
24042407
int $soid = -1,
24052408
bool $clipmode = false,
24062409
array $ctm = [1.0,0.0,0.0,1.0,0.0,0.0], // identity matrix
24072410
): void {
2411+
if ($soid < 0) {
2412+
$soid = (int)array_key_last($this->svgobjs);
2413+
}
24082414
if (empty($this->svgobjs[$soid])) {
24092415
return;
24102416
}
24112417

24122418
$name = $this->removeTagNamespace($name);
24132419

2414-
if ($soid < 0) {
2415-
$soid = (int)array_key_last($this->svgobjs);
2416-
}
2417-
24182420
if ($this->svgobjs[$soid]['clipmode']) {
24192421
$this->svgobjs[$soid]['clippaths'][] = [
24202422
'name' => $name,
2421-
'attribs' => $attribs,
2423+
'attribs' => ['attr' => $attr, 'child' => []],
24222424
'tm' => $this->svgobjs[$soid]['cliptm'],
24232425
];
24242426
return;
@@ -2432,11 +2434,10 @@ protected function handleSVGTagStart(
24322434
$this->svgobjs[$soid]['clippaths'] = [];
24332435
}
24342436

2435-
if (isset($attribs['attr']['id'])) {
2436-
$attribs['child'] = [];
2437-
$this->svgobjs[$soid]['defs'][$attribs['attr']['id']] = [
2437+
if (isset($attr['id'])) {
2438+
$this->svgobjs[$soid]['defs'][$attr['id']] = [
24382439
'name' => $name,
2439-
'attr' => $attribs,
2440+
'attr' => ['attr' => $attr, 'child' => []],
24402441
];
24412442
return;
24422443
}
@@ -2447,11 +2448,11 @@ protected function handleSVGTagStart(
24472448
!empty($this->svgobjs[$soid]['defs'][$last_svgdefs_id]['attr']['child'])
24482449
&& is_array($this->svgobjs[$soid]['defs'][$last_svgdefs_id]['attr']['child'])
24492450
) {
2450-
$attribs['attr']['id'] = 'DF_' .
2451+
$attr['id'] = 'DF_' .
24512452
(count($this->svgobjs[$soid]['defs'][$last_svgdefs_id]['attr']['child']) + 1);
2452-
$this->svgobjs[$soid]['defs'][$last_svgdefs_id]['attr']['child'][$attribs['attr']['id']] = [
2453+
$this->svgobjs[$soid]['defs'][$last_svgdefs_id]['attr']['child'][$attr['id']] = [
24532454
'name' => $name,
2454-
'attr' => $attribs,
2455+
'attr' => ['attr' => $attr, 'child' => []],
24552456
];
24562457
return;
24572458
}
@@ -2468,42 +2469,42 @@ protected function handleSVGTagStart(
24682469

24692470
if (
24702471
$this->svgobjs[$soid]['clipmode'] &&
2471-
!isset($attribs['attr']['fill']) &&
2472-
(!isset($attribs['attr']['style']) ||
2473-
(!preg_match('/[;\"\s]{1}fill[\s]*:[\s]*([^;\"]*)/si', $attribs['attr']['style'], $attrval)))
2472+
!isset($attr['fill']) &&
2473+
(!isset($attr['style']) ||
2474+
(!preg_match('/[;\"\s]{1}fill[\s]*:[\s]*([^;\"]*)/si', $attr['style'], $attrval)))
24742475
) {
24752476
// default fill attribute for clipping
2476-
$attribs['attr']['fill'] = 'none';
2477+
$attr['fill'] = 'none';
24772478
}
24782479

24792480
if (
2480-
isset($attribs['attr']['style']) &&
2481-
!empty($attribs['attr']['style']) &&
2482-
($attribs['attr']['style'][0] != ';')
2481+
isset($attr['style']) &&
2482+
!empty($attr['style']) &&
2483+
($attr['style'][0] != ';')
24832484
) {
24842485
// fix style for regular expression
2485-
$attribs['attr']['style'] = ';' . $attribs['attr']['style'];
2486+
$attr['style'] = ';' . $attr['style'];
24862487
}
24872488

24882489
foreach ($prev_svgstyle as $key => $val) {
24892490
if (in_array($key, self::SVGINHPROP)) {
24902491
// inherit previous value
24912492
$svgstyle[$key] = $val;
24922493
}
2493-
if (!empty($attribs['attr'][$key])) {
2494+
if (!empty($attr[$key])) {
24942495
// specific attribute settings
2495-
if ($attribs['attr'][$key] == 'inherit') {
2496+
if ($attr[$key] == 'inherit') {
24962497
$svgstyle[$key] = $val;
24972498
} else {
2498-
$svgstyle[$key] = $attribs['attr'][$key];
2499+
$svgstyle[$key] = $attr[$key];
24992500
}
2500-
} elseif (!empty($attribs['attr']['style'])) {
2501+
} elseif (!empty($attr['style'])) {
25012502
// CSS style syntax
25022503
$attrval = [];
25032504
if (
25042505
preg_match(
25052506
'/[;\"\s]{1}' . $key . '[\s]*:[\s]*([^;\"]*)/si',
2506-
$attribs['attr']['style'],
2507+
$attr['style'],
25072508
$attrval
25082509
)
25092510
&& isset($attrval[1])
@@ -2518,8 +2519,8 @@ protected function handleSVGTagStart(
25182519
}
25192520

25202521
$tmx = $ctm;
2521-
if (!empty($attribs['attr']['transform'])) {
2522-
$tmx = $this->graph->getCtmProduct($tmx, $this->getSVGTransformMatrix($attribs['attr']['transform']));
2522+
if (!empty($attr['transform'])) {
2523+
$tmx = $this->graph->getCtmProduct($tmx, $this->getSVGTransformMatrix($attr['transform']));
25232524
}
25242525

25252526
$svgstyle['transfmatrix'] = $tmx;
@@ -2537,32 +2538,32 @@ protected function handleSVGTagStart(
25372538
match ($name) {
25382539
'defs' => $this->parseSVGTagSTARTdefs($soid),
25392540
'clipPath' => $this->parseSVGTagSTARTclipPath($soid, $tmx),
2540-
'svg' => $this->parseSVGTagSTARTsvg($parser, $soid, $attribs['attr'], $svgstyle),
2541-
'g' => $this->parseSVGTagSTARTg($parser, $soid, $attribs['attr'], $svgstyle),
2542-
'linearGradient' => $this->parseSVGTagSTARTlinearGradient($soid, $attribs['attr']),
2543-
'radialGradient' => $this->parseSVGTagSTARTradialGradient($soid, $attribs['attr']),
2544-
'stop' => $this->parseSVGTagSTARTstop($soid, $attribs['attr'], $svgstyle),
2545-
'path' => $this->parseSVGTagSTARTpath($parser, $soid, $attribs['attr'], $svgstyle),
2546-
'rect' => $this->parseSVGTagSTARTrect($parser, $soid, $attribs['attr'], $svgstyle),
2547-
'circle' => $this->parseSVGTagSTARTcircle($parser, $soid, $attribs['attr'], $svgstyle),
2548-
'ellipse' => $this->parseSVGTagSTARTellipse($parser, $soid, $attribs['attr'], $svgstyle),
2549-
'line' => $this->parseSVGTagSTARTline($parser, $soid, $attribs['attr'], $svgstyle),
2550-
'polyline' => $this->parseSVGTagSTARTpolygon($parser, $soid, $attribs['attr'], $svgstyle),
2551-
'polygon' => $this->parseSVGTagSTARTpolygon($parser, $soid, $attribs['attr'], $svgstyle),
2552-
'image' => $this->parseSVGTagSTARTimage($parser, $soid, $attribs['attr'], $svgstyle),
2553-
'text' => $this->parseSVGTagSTARTtext($parser, $soid, $attribs['attr'], $svgstyle),
2554-
'tspan' => $this->parseSVGTagSTARTtspan($parser, $soid, $attribs['attr'], $svgstyle),
2555-
'use' => $this->parseSVGTagSTARTuse($parser, $soid, $attribs),
2541+
'svg' => $this->parseSVGTagSTARTsvg($parser, $soid, $attr, $svgstyle),
2542+
'g' => $this->parseSVGTagSTARTg($parser, $soid, $attr, $svgstyle),
2543+
'linearGradient' => $this->parseSVGTagSTARTlinearGradient($soid, $attr),
2544+
'radialGradient' => $this->parseSVGTagSTARTradialGradient($soid, $attr),
2545+
'stop' => $this->parseSVGTagSTARTstop($soid, $attr, $svgstyle),
2546+
'path' => $this->parseSVGTagSTARTpath($parser, $soid, $attr, $svgstyle),
2547+
'rect' => $this->parseSVGTagSTARTrect($parser, $soid, $attr, $svgstyle),
2548+
'circle' => $this->parseSVGTagSTARTcircle($parser, $soid, $attr, $svgstyle),
2549+
'ellipse' => $this->parseSVGTagSTARTellipse($parser, $soid, $attr, $svgstyle),
2550+
'line' => $this->parseSVGTagSTARTline($parser, $soid, $attr, $svgstyle),
2551+
'polyline' => $this->parseSVGTagSTARTpolygon($parser, $soid, $attr, $svgstyle),
2552+
'polygon' => $this->parseSVGTagSTARTpolygon($parser, $soid, $attr, $svgstyle),
2553+
'image' => $this->parseSVGTagSTARTimage($parser, $soid, $attr, $svgstyle),
2554+
'text' => $this->parseSVGTagSTARTtext($parser, $soid, $attr, $svgstyle),
2555+
'tspan' => $this->parseSVGTagSTARTtspan($parser, $soid, $attr, $svgstyle),
2556+
'use' => $this->parseSVGTagSTARTuse($parser, $soid, $attr),
25562557
default => null,
25572558
};
25582559

25592560
// process child elements
2560-
2561+
/*
25612562
if (empty($attribs['child'])) {
25622563
return;
25632564
}
25642565
2565-
$children = $attribs['child'];
2566+
$children = $attr['child'];
25662567
unset($attribs['child']);
25672568
25682569
foreach ($children as $child) {
@@ -2573,7 +2574,7 @@ protected function handleSVGTagStart(
25732574
$this->handleSVGTagStart(
25742575
$parser,
25752576
$child['name'],
2576-
$child['attr'], // @phpstan-ignore argument.type
2577+
$child['attr'],
25772578
$soid,
25782579
);
25792580
continue;
@@ -2583,6 +2584,7 @@ protected function handleSVGTagStart(
25832584
}
25842585
$this->handleSVGTagEnd($parser, $child['name']);
25852586
}
2587+
*/
25862588
}
25872589

25882590
/**
@@ -3539,13 +3541,12 @@ protected function parseSVGTagSTARTtspan(\XMLParser $parser, int $soid, array $a
35393541
*
35403542
* @param \XMLParser $parser The XML parser calling the handler.
35413543
* @param int $soid ID of the current SVG object.
3542-
* @param TSVGAttribs $attribs Associative array with the element's attributes.
3544+
* @param TSVGAttributes $attr SVG attributes.
35433545
*
35443546
* @return void
35453547
*/
3546-
protected function parseSVGTagSTARTuse(\XMLParser $parser, int $soid, array $attribs)
3548+
protected function parseSVGTagSTARTuse(\XMLParser $parser, int $soid, array $attr)
35473549
{
3548-
$attr = $attribs['attr'];
35493550
if (empty($attr['xlink:href'])) {
35503551
return;
35513552
}
@@ -3573,13 +3574,11 @@ protected function parseSVGTagSTARTuse(\XMLParser $parser, int $soid, array $att
35733574
// merge styles
35743575
$attr['style'] = str_replace(';;', ';', ';' . $use['attr']['attr']['style'] . $attr['style']);
35753576
}
3576-
$attribs['attr'] = array_merge($use['attr']['attr'], $attr);
3577-
/** @var TSVGAttribs $attribs */
3578-
$attribs = (array) $attribs;
3577+
$attr = array_merge($use['attr']['attr'], $attr);
35793578
$this->handleSVGTagStart(
35803579
$parser,
35813580
$use['name'],
3582-
$attribs,
3581+
$attr, // @phpstan-ignore-line argument.type
35833582
$soid,
35843583
);
35853584
}

0 commit comments

Comments
 (0)