* Parse an SVG string as `SVGDocumentData`. * @param svg - And SVG string to be parsed. * @returns `SVGDocumentData` that can be used to build SVGDocument.
(svg: string)
| 401 | * @returns `SVGDocumentData` that can be used to build SVGDocument. |
| 402 | */ |
| 403 | protected static parseSVGData(svg: string) { |
| 404 | const cached = SVG.svgNodesPool[svg]; |
| 405 | if (cached && (cached.size.x > 0 || cached.size.y > 0)) return cached; |
| 406 | |
| 407 | SVG.containerElement.innerHTML = svg; |
| 408 | |
| 409 | const svgRoot = SVG.containerElement.querySelector('svg'); |
| 410 | |
| 411 | if (!svgRoot) { |
| 412 | useLogger().error({ |
| 413 | message: 'Invalid SVG', |
| 414 | object: svg, |
| 415 | }); |
| 416 | return { |
| 417 | size: new Vector2(0, 0), |
| 418 | nodes: [], |
| 419 | } as SVGDocumentData; |
| 420 | } |
| 421 | |
| 422 | let viewBox = new BBox(); |
| 423 | let size = new Vector2(); |
| 424 | |
| 425 | const hasViewBox = svgRoot.hasAttribute('viewBox'); |
| 426 | const hasSize = |
| 427 | svgRoot.hasAttribute('width') || svgRoot.hasAttribute('height'); |
| 428 | |
| 429 | if (hasViewBox) { |
| 430 | const {x, y, width, height} = svgRoot.viewBox.baseVal; |
| 431 | viewBox = new BBox(x, y, width, height); |
| 432 | |
| 433 | if (!hasSize) size = viewBox.size; |
| 434 | } |
| 435 | |
| 436 | if (hasSize) { |
| 437 | size = new Vector2( |
| 438 | svgRoot.width.baseVal.value, |
| 439 | svgRoot.height.baseVal.value, |
| 440 | ); |
| 441 | |
| 442 | if (!hasViewBox) viewBox = new BBox(0, 0, size.width, size.height); |
| 443 | } |
| 444 | |
| 445 | if (!hasViewBox && !hasSize) { |
| 446 | viewBox = new BBox(svgRoot.getBBox()); |
| 447 | size = viewBox.size; |
| 448 | } |
| 449 | |
| 450 | const scale = size.div(viewBox.size); |
| 451 | const center = viewBox.center; |
| 452 | |
| 453 | const rootTransform = new DOMMatrix() |
| 454 | .scaleSelf(scale.x, scale.y) |
| 455 | .translateSelf(-center.x, -center.y); |
| 456 | |
| 457 | const nodes = Array.from( |
| 458 | SVG.extractGroupNodes(svgRoot, svgRoot, rootTransform, {}), |
| 459 | ); |
| 460 | const builder: SVGDocumentData = { |
no test coverage detected