MCPcopy
hub / github.com/Yqnn/svg-path-editor / reversePath

Function reversePath

src/lib/reverse-path.ts:10–104  ·  view source on GitHub ↗
(svg: SvgPath, subpathOfItem?: number)

Source from the content-addressed store, hash-verified

8
9
10export 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;

Callers 4

optimizePathFunction · 0.90
reverseMethod · 0.90
reverseSubPathMethod · 0.90

Calls 7

getSubPathBoundsFunction · 0.90
optimizePathFunction · 0.90
reverseMethod · 0.80
controlLocationsMethod · 0.80
toStrFunction · 0.70
setRelativeMethod · 0.45

Tested by

no test coverage detected