source: trunk/client/inc/hpdf5/spipu/html2pdf/src/Tag/Svg/Path.php@ 347

Last change on this file since 347 was 347, checked in by roby, 3 years ago

Aggiornamento per compatibilità con php7.4

File size: 6.0 KB
Line 
1<?php
2/**
3 * Html2Pdf Library - Tag class
4 *
5 * HTML => PDF converter
6 * distributed under the OSL-3.0 License
7 *
8 * @package Html2pdf
9 * @author Laurent MINGUET <webmaster@html2pdf.fr>
10 * @copyright 2017 Laurent MINGUET
11 */
12namespace Spipu\Html2Pdf\Tag\Svg;
13
14use Spipu\Html2Pdf\Tag\AbstractSvgTag;
15
16/**
17 * Tag Path
18 */
19class Path extends AbstractSvgTag
20{
21 /**
22 * @inheritdoc
23 */
24 public function getName()
25 {
26 return 'path';
27 }
28
29 /**
30 * @inheritdoc
31 */
32 protected function drawSvg($properties)
33 {
34 $styles = $this->parsingCss->getSvgStyle($this->getName(), $properties);
35 $style = $this->pdf->svgSetStyle($styles);
36
37 $path = isset($properties['d']) ? $properties['d'] : null;
38
39 if ($path) {
40 // prepare the path
41 $path = str_replace(',', ' ', $path);
42 $path = preg_replace('/([a-zA-Z])([0-9\.\-])/', '$1 $2', $path);
43 $path = preg_replace('/([0-9\.])([a-zA-Z])/', '$1 $2', $path);
44 $path = preg_replace('/[\s]+/', ' ', trim($path));
45 $path = preg_replace('/ ([a-z]{2})/', '$1', $path);
46 $path = preg_replace('/Z([a-zA-Z])/', 'Z $1', $path);
47
48 $path = explode(' ', $path);
49 foreach ($path as $k => $v) {
50 $path[$k] = trim($v);
51 if ($path[$k] === '') {
52 unset($path[$k]);
53 }
54 }
55 $path = array_values($path);
56 $amountPath = count($path);
57
58 // read each actions in the path
59 $actions = array();
60 $lastAction = null; // last action found
61 for ($k=0; $k<$amountPath; true) {
62 // for this actions, we can not have multi coordinate
63 if (in_array($lastAction, array('z', 'Z'))) {
64 $lastAction = null;
65 }
66
67 // read the new action (forcing if no action before)
68 if (preg_match('/^[a-z]+$/i', $path[$k]) || $lastAction === null) {
69 $lastAction = $path[$k];
70 $k++;
71 }
72
73 // current action
74 $action = array();
75 $action[] = $lastAction;
76 switch ($lastAction) {
77 case 'C':
78 case 'c':
79 // x1 y1 x2 y2 x y
80 $action[] = $this->cssConverter->convertToMM($path[$k+0], $this->svgDrawer->getProperty('w'));
81 $action[] = $this->cssConverter->convertToMM($path[$k+1], $this->svgDrawer->getProperty('h'));
82 $action[] = $this->cssConverter->convertToMM($path[$k+2], $this->svgDrawer->getProperty('w'));
83 $action[] = $this->cssConverter->convertToMM($path[$k+3], $this->svgDrawer->getProperty('h'));
84 $action[] = $this->cssConverter->convertToMM($path[$k+4], $this->svgDrawer->getProperty('w'));
85 $action[] = $this->cssConverter->convertToMM($path[$k+5], $this->svgDrawer->getProperty('h'));
86 $k+= 6;
87 break;
88
89 case 'Q':
90 case 'S':
91 case 'q':
92 case 's':
93 // x2 y2 x y
94 $action[] = $this->cssConverter->convertToMM($path[$k+0], $this->svgDrawer->getProperty('w'));
95 $action[] = $this->cssConverter->convertToMM($path[$k+1], $this->svgDrawer->getProperty('h'));
96 $action[] = $this->cssConverter->convertToMM($path[$k+2], $this->svgDrawer->getProperty('w'));
97 $action[] = $this->cssConverter->convertToMM($path[$k+3], $this->svgDrawer->getProperty('h'));
98 $k+= 4;
99 break;
100
101 case 'A':
102 case 'a':
103 // rx ry (angle de deviation de l'axe X) (large-arc-flag) (sweep-flag) x y
104 $action[] = $this->cssConverter->convertToMM($path[$k+0], $this->svgDrawer->getProperty('w'));
105 $action[] = $this->cssConverter->convertToMM($path[$k+1], $this->svgDrawer->getProperty('h'));
106 $action[] = 1.*$path[$k+2];
107 $action[] = ($path[$k+3] === '1') ? 1 : 0;
108 $action[] = ($path[$k+4] === '1') ? 1 : 0;
109 $action[] = $this->cssConverter->convertToMM($path[$k+5], $this->svgDrawer->getProperty('w'));
110 $action[] = $this->cssConverter->convertToMM($path[$k+6], $this->svgDrawer->getProperty('h'));
111 $k+= 7;
112 break;
113
114 case 'M':
115 case 'L':
116 case 'T':
117 case 'm':
118 case 'l':
119 case 't':
120 // x y
121 $action[] = $this->cssConverter->convertToMM($path[$k+0], $this->svgDrawer->getProperty('w'));
122 $action[] = $this->cssConverter->convertToMM($path[$k+1], $this->svgDrawer->getProperty('h'));
123 $k+= 2;
124 break;
125
126 case 'H':
127 case 'h':
128 // x
129 $action[] = $this->cssConverter->convertToMM($path[$k+0], $this->svgDrawer->getProperty('w'));
130 $k+= 1;
131 break;
132
133 case 'V':
134 case 'v':
135 // y
136 $action[] = $this->cssConverter->convertToMM($path[$k+0], $this->svgDrawer->getProperty('h'));
137 $k+= 1;
138 break;
139
140 case 'z':
141 case 'Z':
142 break;
143
144 default:
145 $k+= 1;
146 break;
147 }
148 // add the action
149 $actions[] = $action;
150 }
151
152 // drawing
153 $this->pdf->svgPolygone($actions, $style);
154 }
155 }
156}
Note: See TracBrowser for help on using the repository browser.