PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.7
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.7
1.10.20 1.10.19 1.10.18 1.10.17 1.10.16 1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 1.10.1 1.10.0 1.9.17 1.9.15 1.9.16 All 164 releases
woocommerce-pos / vendor_prefixed / dompdf / php-svg-lib / src / Svg / Document.php

Document.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.10.7, at vendor_prefixed/dompdf/php-svg-lib/src/Svg/Document.php

359 lines 10.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * @package php-svg-lib
5 * @link http://github.com/dompdf/php-svg-lib
6 * @license GNU LGPLv3+ http://www.gnu.org/copyleft/lesser.html
7 */
8 namespace WCPOS\Vendor\Svg;
9
10 use WCPOS\Vendor\Svg\Surface\SurfaceInterface;
11 use WCPOS\Vendor\Svg\Tag\AbstractTag;
12 use WCPOS\Vendor\Svg\Tag\Anchor;
13 use WCPOS\Vendor\Svg\Tag\Circle;
14 use WCPOS\Vendor\Svg\Tag\Ellipse;
15 use WCPOS\Vendor\Svg\Tag\Group;
16 use WCPOS\Vendor\Svg\Tag\ClipPath;
17 use WCPOS\Vendor\Svg\Tag\Image;
18 use WCPOS\Vendor\Svg\Tag\Line;
19 use WCPOS\Vendor\Svg\Tag\LinearGradient;
20 use WCPOS\Vendor\Svg\Tag\Path;
21 use WCPOS\Vendor\Svg\Tag\Polygon;
22 use WCPOS\Vendor\Svg\Tag\Polyline;
23 use WCPOS\Vendor\Svg\Tag\Rect;
24 use WCPOS\Vendor\Svg\Tag\Stop;
25 use WCPOS\Vendor\Svg\Tag\Symbol;
26 use WCPOS\Vendor\Svg\Tag\Text;
27 use WCPOS\Vendor\Svg\Tag\StyleTag;
28 use WCPOS\Vendor\Svg\Tag\UseTag;
29 class Document extends AbstractTag
30 {
31 protected $filename;
32 protected $_defs_depth = 0;
33 public $inDefs = \false;
34 protected $x;
35 protected $y;
36 protected $width;
37 protected $height;
38 protected $subPathInit;
39 protected $pathBBox;
40 protected $viewBox;
41 /** @var SurfaceInterface */
42 protected $surface;
43 /** @var AbstractTag[] */
44 protected $stack = array();
45 /** @var AbstractTag[] */
46 protected $defs = array();
47 /** @var \Sabberworm\CSS\CSSList\Document[] */
48 protected $styleSheets = array();
49 public $allowExternalReferences = \true;
50 public function loadFile($filename)
51 {
52 $this->filename = $filename;
53 }
54 protected function initParser()
55 {
56 $parser = \xml_parser_create("utf-8");
57 \xml_parser_set_option($parser, \XML_OPTION_CASE_FOLDING, \false);
58 \xml_set_element_handler($parser, array($this, "_tagStart"), array($this, "_tagEnd"));
59 \xml_set_character_data_handler($parser, array($this, "_charData"));
60 return $parser;
61 }
62 public function __construct()
63 {
64 }
65 /**
66 * Increase the nesting level for defs-like elements
67 *
68 * @return int
69 */
70 public function enterDefs()
71 {
72 $this->_defs_depth++;
73 $this->inDefs = \true;
74 return $this->_defs_depth;
75 }
76 /**
77 * Decrease the nesting level for defs-like elements
78 *
79 * @return int
80 */
81 public function exitDefs()
82 {
83 $this->_defs_depth--;
84 if ($this->_defs_depth < 0) {
85 $this->_defs_depth = 0;
86 }
87 $this->inDefs = $this->_defs_depth > 0 ? \true : \false;
88 return $this->_defs_depth;
89 }
90 /**
91 * @return SurfaceInterface
92 */
93 public function getSurface()
94 {
95 return $this->surface;
96 }
97 public function getStack()
98 {
99 return $this->stack;
100 }
101 public function getWidth()
102 {
103 return $this->width;
104 }
105 public function getHeight()
106 {
107 return $this->height;
108 }
109 public function getDiagonal()
110 {
111 return \sqrt($this->width ** 2 + $this->height ** 2) / \sqrt(2);
112 }
113 public function getDimensions()
114 {
115 $rootAttributes = null;
116 $parser = \xml_parser_create("utf-8");
117 \xml_parser_set_option($parser, \XML_OPTION_CASE_FOLDING, \false);
118 \xml_set_element_handler($parser, function ($parser, $name, $attributes) use(&$rootAttributes) {
119 if ($name === "svg" && $rootAttributes === null) {
120 $attributes = \array_change_key_case($attributes, \CASE_LOWER);
121 $rootAttributes = $attributes;
122 }
123 }, function ($parser, $name) {
124 });
125 $fp = \fopen($this->filename, "r");
126 while ($line = \fread($fp, 8192)) {
127 \xml_parse($parser, $line, \false);
128 if ($rootAttributes !== null) {
129 break;
130 }
131 }
132 \xml_parse($parser, "", \true);
133 if (\PHP_MAJOR_VERSION < 8) {
134 \xml_parser_free($parser);
135 }
136 return $this->handleSizeAttributes($rootAttributes);
137 }
138 public function handleSizeAttributes($attributes)
139 {
140 if ($this->width === null) {
141 if (isset($attributes["width"])) {
142 $width = $this->convertSize($attributes["width"], 400);
143 $this->width = $width;
144 }
145 if (isset($attributes["height"])) {
146 $height = $this->convertSize($attributes["height"], 300);
147 $this->height = $height;
148 }
149 if (isset($attributes['viewbox'])) {
150 $viewBox = \preg_split('/[\\s,]+/is', \trim($attributes['viewbox']));
151 if (\count($viewBox) == 4) {
152 $this->x = $viewBox[0];
153 $this->y = $viewBox[1];
154 if (!$this->width) {
155 $this->width = $viewBox[2];
156 }
157 if (!$this->height) {
158 $this->height = $viewBox[3];
159 }
160 }
161 }
162 }
163 return array(0 => $this->width, 1 => $this->height, "width" => $this->width, "height" => $this->height);
164 }
165 public function getDocument()
166 {
167 return $this;
168 }
169 /**
170 * Append a style sheet
171 *
172 * @param \Sabberworm\CSS\CSSList\Document $stylesheet
173 */
174 public function appendStyleSheet($stylesheet)
175 {
176 $this->styleSheets[] = $stylesheet;
177 }
178 /**
179 * Get the document style sheets
180 *
181 * @return \Sabberworm\CSS\CSSList\Document[]
182 */
183 public function getStyleSheets()
184 {
185 return $this->styleSheets;
186 }
187 protected function before($attributes)
188 {
189 $surface = $this->getSurface();
190 $style = new DefaultStyle($this);
191 $style->inherit($this);
192 $style->fromAttributes($attributes);
193 $this->setStyle($style);
194 $surface->setStyle($style);
195 }
196 public function render(SurfaceInterface $surface)
197 {
198 $this->_defs_depth = 0;
199 $this->inDefs = \false;
200 $this->surface = $surface;
201 $parser = $this->initParser();
202 if ($this->x || $this->y) {
203 $surface->translate(-$this->x, -$this->y);
204 }
205 $fp = \fopen($this->filename, "r");
206 while ($line = \fread($fp, 8192)) {
207 \xml_parse($parser, $line, \false);
208 }
209 \xml_parse($parser, "", \true);
210 if (\PHP_MAJOR_VERSION < 8) {
211 \xml_parser_free($parser);
212 }
213 }
214 protected function svgOffset($attributes)
215 {
216 $this->attributes = $attributes;
217 $this->handleSizeAttributes($attributes);
218 }
219 public function getDef($id)
220 {
221 $id = \ltrim($id, "#");
222 return isset($this->defs[$id]) ? $this->defs[$id] : null;
223 }
224 private function _tagStart($parser, $name, $attributes)
225 {
226 $this->x = 0;
227 $this->y = 0;
228 $tag = null;
229 $attributes = \array_change_key_case($attributes, \CASE_LOWER);
230 switch (\strtolower($name)) {
231 case 'defs':
232 $this->enterDefs();
233 return;
234 case 'svg':
235 if (\count($this->attributes)) {
236 $tag = new Group($this, $name);
237 } else {
238 $tag = $this;
239 $this->svgOffset($attributes);
240 }
241 break;
242 case 'path':
243 $tag = new Path($this, $name);
244 break;
245 case 'rect':
246 $tag = new Rect($this, $name);
247 break;
248 case 'circle':
249 $tag = new Circle($this, $name);
250 break;
251 case 'ellipse':
252 $tag = new Ellipse($this, $name);
253 break;
254 case 'image':
255 $tag = new Image($this, $name);
256 break;
257 case 'line':
258 $tag = new Line($this, $name);
259 break;
260 case 'polyline':
261 $tag = new Polyline($this, $name);
262 break;
263 case 'polygon':
264 $tag = new Polygon($this, $name);
265 break;
266 case 'lineargradient':
267 $tag = new LinearGradient($this, $name);
268 break;
269 case 'radialgradient':
270 $tag = new LinearGradient($this, $name);
271 break;
272 case 'stop':
273 $tag = new Stop($this, $name);
274 break;
275 case 'style':
276 $tag = new StyleTag($this, $name);
277 break;
278 case 'a':
279 $tag = new Anchor($this, $name);
280 break;
281 case 'g':
282 $tag = new Group($this, $name);
283 break;
284 case 'symbol':
285 $this->enterDefs();
286 $tag = new Symbol($this, $name);
287 break;
288 case 'clippath':
289 $tag = new ClipPath($this, $name);
290 break;
291 case 'use':
292 $tag = new UseTag($this, $name);
293 break;
294 case 'text':
295 $tag = new Text($this, $name);
296 break;
297 case 'desc':
298 return;
299 }
300 if ($tag) {
301 if (isset($attributes["id"])) {
302 $this->defs[$attributes["id"]] = $tag;
303 } else {
304 /** @var AbstractTag $top */
305 $top = \end($this->stack);
306 if ($top && $top != $tag) {
307 $top->children[] = $tag;
308 }
309 }
310 $this->stack[] = $tag;
311 $tag->handle($attributes);
312 }
313 }
314 function _charData($parser, $data)
315 {
316 $stack_top = \end($this->stack);
317 if ($stack_top instanceof Text || $stack_top instanceof StyleTag) {
318 $stack_top->appendText($data);
319 }
320 }
321 function _tagEnd($parser, $name)
322 {
323 /** @var AbstractTag $tag */
324 $tag = null;
325 switch (\strtolower($name)) {
326 case 'defs':
327 $this->exitDefs();
328 return;
329 case 'symbol':
330 $this->exitDefs();
331 $tag = \array_pop($this->stack);
332 break;
333 case 'svg':
334 case 'path':
335 case 'rect':
336 case 'circle':
337 case 'ellipse':
338 case 'image':
339 case 'line':
340 case 'polyline':
341 case 'polygon':
342 case 'radialgradient':
343 case 'lineargradient':
344 case 'stop':
345 case 'style':
346 case 'text':
347 case 'g':
348 case 'clippath':
349 case 'use':
350 case 'a':
351 $tag = \array_pop($this->stack);
352 break;
353 }
354 if (!$this->inDefs && $tag || $tag instanceof StyleTag) {
355 $tag->handleEnd();
356 }
357 }
358 }
359