MCPcopy
hub / github.com/midrender/revideo / parseSVGData

Method parseSVGData

packages/2d/src/lib/components/SVG.ts:403–466  ·  view source on GitHub ↗

* 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)

Source from the content-addressed store, hash-verified

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 = {

Callers 1

parseSVGMethod · 0.80

Calls 4

divMethod · 0.95
useLoggerFunction · 0.90
extractGroupNodesMethod · 0.80
errorMethod · 0.45

Tested by

no test coverage detected