import { parseSVG } from 'svg-path-parser' import { NemiCurve } from './nemiCurve' export const fromDPath = (d: string) => { const parsedPath = parseSVG(d) const firstElement = parsedPath[0] if (firstElement.code !== 'M') { throw new Error('First element of path must be a move command') } let cursor = { x: firstElement.x, y: firstElement.y } const curves: NemiCurve[] = [] const restOfTheElements = parsedPath.slice(1) restOfTheElements.forEach((element, index) => { switch (element.code) { case 'H': curves.push({ type: 'line', startPoint: { x: cursor.x, y: cursor.y, }, endPoint: { x: element.x, y: cursor.y }, }) cursor = { x: element.x, y: cursor.y } break case 'C': curves.push({ type: 'bezier', startPoint: { x: cursor.x, y: cursor.y, }, controlPoint1: { x: element.x1, y: element.y1, }, controlPoint2: { x: element.x2, y: element.y2, }, endPoint: { x: element.x, y: element.y, }, }) cursor = { x: element.x, y: element.y } break case 'Z': break default: throw new Error(`Unknown path element code ${element.code} at index ${index}`) } }) return curves }