(svg: SvgPath, subpathOfItem?: number)
| 8 | |
| 9 | |
| 10 | export const reversePath = (svg: SvgPath, subpathOfItem?: number)=> { |
| 11 | const {start, end} = getSubPathBounds(svg, subpathOfItem); |
| 12 | |
| 13 | if((end - start) <= 1) { |
| 14 | return; |
| 15 | } |
| 16 | |
| 17 | const isBeforeRelative = end < svg.path.length && svg.path[end].relative; |
| 18 | if(isBeforeRelative) { |
| 19 | svg.path[end].setRelative(false); |
| 20 | } |
| 21 | |
| 22 | const subPath = svg.path.slice(start, end); |
| 23 | const outputPath: SvgItem[] = []; |
| 24 | const reversedPath = [...subPath].reverse().slice(0, -1); |
| 25 | |
| 26 | const startPoint = reversedPath[0].targetLocation(); |
| 27 | outputPath.push(SvgItem.Make(['M', ...toStr(startPoint)])); |
| 28 | let previousType = ''; |
| 29 | let isClosed = false; |
| 30 | |
| 31 | for(const component of reversedPath) { |
| 32 | const pt = toStr(component.previousPoint); |
| 33 | const ctrl = component.absolutePoints.map(toStr); |
| 34 | const type = component.getType(true); |
| 35 | switch(type) { |
| 36 | case 'M' : |
| 37 | case 'Z' : |
| 38 | if(isClosed) { |
| 39 | outputPath.push(SvgItem.Make(['Z'])); |
| 40 | } |
| 41 | isClosed = type === 'Z'; |
| 42 | if(outputPath[outputPath.length - 1].getType(true) === 'M') { |
| 43 | outputPath[outputPath.length - 1] = SvgItem.Make(['M', ...pt]); |
| 44 | } else { |
| 45 | outputPath.push(SvgItem.Make(['M', ...pt])); |
| 46 | } |
| 47 | break; |
| 48 | case 'L' : |
| 49 | outputPath.push(SvgItem.Make(['L', ...pt])); |
| 50 | break; |
| 51 | case 'H' : |
| 52 | outputPath.push(SvgItem.Make(['H', pt[0]])); |
| 53 | break; |
| 54 | case 'V' : |
| 55 | outputPath.push(SvgItem.Make(['V', pt[1]])); |
| 56 | break; |
| 57 | case 'C' : |
| 58 | outputPath.push(SvgItem.Make(['C', ...ctrl[1], ...ctrl[0], ...pt])); |
| 59 | break; |
| 60 | case 'S' : { |
| 61 | const a = toStr(component.controlLocations()[0]); |
| 62 | if(previousType !== 'S') { |
| 63 | outputPath.push(SvgItem.Make(['C', ...ctrl[0], ...a, ...pt])); |
| 64 | } else { |
| 65 | outputPath.push(SvgItem.Make(['S', ...a, ...pt])); |
| 66 | } |
| 67 | break; |
no test coverage detected