source: trunk/client/inc/hpdf5/spipu/html2pdf/src/SvgDrawer.php@ 412

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

Aggiornamento per compatibilità con php7.4

File size: 7.8 KB
RevLine 
[347]1<?php
2/**
3 * Html2Pdf Library
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;
13
14use Spipu\Html2Pdf\Exception\HtmlParsingException;
15
16/**
17 * Class SvgDrawer
18 */
19class SvgDrawer
20{
21 /**
22 * @var MyPdf
23 */
24 private $pdf;
25
26 /**
27 * @var array
28 */
29 private $properties;
30 /**
31 * @var CssConverter
32 */
33 private $cssConverter;
34
35 /**
36 * SvgDrawer constructor.
37 *
38 * @param MyPdf $pdf
39 * @param CssConverter $cssConverter
40 */
41 public function __construct(
42 MyPdf $pdf,
43 CssConverter $cssConverter
44 ) {
45
46 $this->pdf = $pdf;
47 $this->cssConverter = $cssConverter;
48 }
49
50 /**
51 * Start Drawing
52 *
53 * @param array $properties
54 * @throws HtmlParsingException
55 */
56 public function startDrawing($properties)
57 {
58 if ($this->isDrawing()) {
59 $e = new HtmlParsingException('We are already in a draw tag');
60 $e->setInvalidTag('draw');
61 throw $e;
62 }
63
64 $this->properties = $properties;
65
66 // init the translate matrix : (0,0) => (x, y)
67 $this->pdf->doTransform(array(1,0,0,1,$this->getProperty('x'),$this->getProperty('y')));
68 $this->pdf->setAlpha(1.);
69 }
70
71 /**
72 * Stop Drawing
73 */
74 public function stopDrawing()
75 {
76 $this->properties = null;
77
78 $this->pdf->setAlpha(1.);
79 $this->pdf->undoTransform();
80 $this->pdf->clippingPathStop();
81 }
82
83 /**
84 * Are we drawing ?
85 *
86 * @return bool
87 */
88 public function isDrawing()
89 {
90 return is_array($this->properties);
91 }
92
93 /**
94 * Get the property
95 *
96 * @param string $key
97 * @return mixed
98 */
99 public function getProperty($key)
100 {
101 return $this->properties[$key];
102 }
103
104
105 /**
106 * prepare a transform matrix
107 *
108 * @param string $transform
109 * @return array
110 */
111 public function prepareTransform($transform)
112 {
113 // it can not be empty
114 if (!$transform) {
115 return null;
116 }
117
118 // sections must be like scale(...)
119 if (!preg_match_all('/([a-z]+)\(([^\)]*)\)/isU', $transform, $match)) {
120 return null;
121 }
122
123 // prepare the list of the actions
124 $actions = array();
125
126 // for actions
127 $amountMatches = count($match[0]);
128 for ($k=0; $k < $amountMatches; $k++) {
129 // get the name of the action
130 $name = strtolower($match[1][$k]);
131
132 // get the parameters of the action
133 $values = [];
134 $string = trim($match[2][$k]);
135 if ($string !== '') {
136 $values = explode(',', $string);
137 }
138 foreach ($values as $key => $value) {
139 $value = trim($value);
140 if ($value === '') {
141 unset($values[$key]);
142 continue;
143 }
144
145 $values[$key] = $value;
146 }
147
148 // prepare the matrix, depending on the action
149 switch ($name) {
150 case 'scale':
151 if (!array_key_exists(0, $values)) {
152 $values[0] = 1.;
153 }
154
155 if (!array_key_exists(1, $values)) {
156 $values[1] = $values[0];
157 }
158
159 $values[0] = floatval($values[0]);
160 $values[1] = floatval($values[1]);
161
162 $actions[] = array($values[0],0.,0.,$values[1],0.,0.);
163 break;
164
165 case 'translate':
166 if (!array_key_exists(0, $values)) {
167 $values[0] = 0.;
168 }
169
170 if (!array_key_exists(1, $values)) {
171 $values[1] = 0.;
172 }
173
174 $values[0] = $this->cssConverter->convertToMM($values[0], $this->getProperty('w'));
175 $values[1] = $this->cssConverter->convertToMM($values[1], $this->getProperty('h'));
176
177 $actions[] = array(1.,0.,0.,1.,$values[0],$values[1]);
178 break;
179
180 case 'rotate':
181 if (!array_key_exists(0, $values)) {
182 $values[0] = 0.;
183 }
184 if (!array_key_exists(1, $values)) {
185 $values[1] = 0.;
186 }
187 if (!array_key_exists(2, $values)) {
188 $values[2] = 0.;
189 }
190
191 $values[0] = $values[0]*M_PI/180.;
192 $values[1] = $this->cssConverter->convertToMM($values[1], $this->getProperty('w'));
193 $values[2] = $this->cssConverter->convertToMM($values[2], $this->getProperty('h'));
194
195 if ($values[1] || $values[2]) {
196 $actions[] = array(1.,0.,0.,1.,-$values[1],-$values[2]);
197 }
198
199 $actions[] = array(cos($values[0]),sin($values[0]),-sin($values[0]),cos($values[0]),0.,0.);
200
201 if ($values[1] || $values[2]) {
202 $actions[] = array(1.,0.,0.,1.,$values[1],$values[2]);
203 }
204 break;
205
206 case 'skewx':
207 if (!array_key_exists(0, $values)) {
208 $values[0] = 0.;
209 }
210
211 $values[0] = $values[0]*M_PI/180.;
212
213 $actions[] = array(1.,0.,tan($values[0]),1.,0.,0.);
214 break;
215
216 case 'skewy':
217 if (!array_key_exists(0, $values)) {
218 $values[0] = 0.;
219 }
220
221 $values[0] = $values[0]*M_PI/180.;
222
223 $actions[] = array(1.,tan($values[0]),0.,1.,0.,0.);
224 break;
225
226 case 'matrix':
227 if (!array_key_exists(0, $values)) {
228 $values[0] = 0.;
229 }
230
231 if (!array_key_exists(1, $values)) {
232 $values[1] = 0.;
233 }
234
235 if (!array_key_exists(2, $values)) {
236 $values[2] = 0.;
237 }
238
239 if (!array_key_exists(3, $values)) {
240 $values[3] = 0.;
241 }
242
243 if (!array_key_exists(4, $values)) {
244 $values[4] = 0.;
245 }
246
247 if (!array_key_exists(5, $values)) {
248 $values[5] = 0.;
249 }
250
251 $values[0] = floatval($values[0]);
252 $values[1] = floatval($values[1]);
253 $values[2] = floatval($values[2]);
254 $values[3] = floatval($values[3]);
255 $values[4] = $this->cssConverter->convertToMM($values[4], $this->getProperty('w'));
256 $values[5] = $this->cssConverter->convertToMM($values[5], $this->getProperty('h'));
257
258 $actions[] = $values;
259 break;
260 }
261 }
262
263 // if there are no actions => return
264 if (!$actions) {
265 return null;
266 }
267
268 // get the first matrix
269 $m = $actions[0];
270 unset($actions[0]);
271
272 // foreach matrix => multiply to the last matrix
273 foreach ($actions as $n) {
274 $m = array(
275 $m[0]*$n[0]+$m[2]*$n[1],
276 $m[1]*$n[0]+$m[3]*$n[1],
277 $m[0]*$n[2]+$m[2]*$n[3],
278 $m[1]*$n[2]+$m[3]*$n[3],
279 $m[0]*$n[4]+$m[2]*$n[5]+$m[4],
280 $m[1]*$n[4]+$m[3]*$n[5]+$m[5]
281 );
282 }
283
284 // return the matrix
285 return $m;
286 }
287}
Note: See TracBrowser for help on using the repository browser.