@annotorious/annotorious 3.1.7 → 3.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/dist/annotation/editors/editorsRegistry.d.ts +1 -1
  2. package/dist/annotation/editors/multipolygon/MultiPolygonEditor.svelte.d.ts +1 -0
  3. package/dist/annotation/editors/multipolygon/index.d.ts +1 -0
  4. package/dist/annotation/shapes/MultiPolygon.svelte.d.ts +1 -0
  5. package/dist/annotation/shapes/index.d.ts +1 -0
  6. package/dist/annotorious.css +1 -1
  7. package/dist/annotorious.es.js +3644 -2444
  8. package/dist/annotorious.es.js.map +1 -1
  9. package/dist/annotorious.js +2 -1
  10. package/dist/annotorious.js.map +1 -1
  11. package/dist/model/core/Shape.d.ts +1 -0
  12. package/dist/model/core/index.d.ts +1 -0
  13. package/dist/model/core/multipolygon/MultiPolygon.d.ts +15 -0
  14. package/dist/model/core/multipolygon/index.d.ts +2 -0
  15. package/dist/model/core/multipolygon/multiPolygonUtils.d.ts +3 -0
  16. package/dist/model/core/shapeUtils.d.ts +3 -0
  17. package/dist/model/w3c/svg/SVG.d.ts +1 -0
  18. package/dist/model/w3c/svg/pathParser.d.ts +2 -0
  19. package/package.json +4 -3
  20. package/src/Annotorious.css +1 -0
  21. package/src/annotation/SVGAnnotationLayer.svelte +6 -0
  22. package/src/annotation/editors/editorsRegistry.ts +4 -2
  23. package/src/annotation/editors/multipolygon/MultiPolygonEditor.svelte +116 -0
  24. package/src/annotation/editors/multipolygon/index.ts +1 -0
  25. package/src/annotation/shapes/MultiPolygon.svelte +31 -0
  26. package/src/annotation/shapes/index.ts +1 -0
  27. package/src/model/core/Shape.ts +2 -0
  28. package/src/model/core/index.ts +1 -0
  29. package/src/model/core/multipolygon/MultiPolygon.ts +30 -0
  30. package/src/model/core/multipolygon/index.ts +2 -0
  31. package/src/model/core/multipolygon/multiPolygonUtils.ts +63 -0
  32. package/src/model/core/polygon/polygonUtils.ts +6 -29
  33. package/src/model/core/shapeUtils.ts +49 -1
  34. package/src/model/w3c/svg/SVG.ts +16 -0
  35. package/src/model/w3c/svg/SVGSelector.ts +57 -21
  36. package/src/model/w3c/svg/pathParser.ts +73 -0
@@ -0,0 +1,73 @@
1
+ import { SVGPathData } from 'svg-pathdata';
2
+ import { boundsFromPoints, type MultiPolygonElement, type MultiPolygonRing } from '../../core';
3
+
4
+ export const svgPathToMultiPolygonElement = (d: string): MultiPolygonElement | undefined => {
5
+ const commands = new SVGPathData(d).toAbs().commands;
6
+
7
+ const rings: MultiPolygonRing[] = [];
8
+
9
+ let currentRing: [number, number][] = [];
10
+ let startPoint: [number, number] | null = null;
11
+
12
+ for (const cmd of commands) {
13
+ switch (cmd.type) {
14
+ case SVGPathData.MOVE_TO:
15
+ // Start a new ring
16
+ if (currentRing.length > 0) {
17
+ rings.push({ points: currentRing });
18
+ currentRing = [];
19
+ }
20
+
21
+ startPoint = [cmd.x, cmd.y];
22
+ currentRing.push(startPoint);
23
+ break;
24
+
25
+ case SVGPathData.LINE_TO:
26
+ currentRing.push([cmd.x, cmd.y]);
27
+ break;
28
+
29
+ case SVGPathData.HORIZ_LINE_TO:
30
+ // Get the last Y coordinate since H command only specifies X
31
+ const lastY = currentRing[currentRing.length - 1][1];
32
+ currentRing.push([cmd.x, lastY]);
33
+ break;
34
+
35
+ case SVGPathData.VERT_LINE_TO:
36
+ // Get the last X coordinate since V command only specifies Y
37
+ const lastX = currentRing[currentRing.length - 1][0];
38
+ currentRing.push([lastX, cmd.y]);
39
+ break;
40
+
41
+ /** Might be needed in the future **
42
+ case SVGPathData.CLOSE_PATH:
43
+ if (startPoint &&
44
+ (currentRing[currentRing.length - 1][0] !== startPoint[0] ||
45
+ currentRing[currentRing.length - 1][1] !== startPoint[1])) {
46
+ currentRing.push([...startPoint]);
47
+ }
48
+ rings.push({ points: currentRing });
49
+ currentRing = [];
50
+ startPoint = null;
51
+ break;
52
+ **/
53
+
54
+ // For curve commands, we just add the end point
55
+ case SVGPathData.CURVE_TO:
56
+ case SVGPathData.SMOOTH_CURVE_TO:
57
+ case SVGPathData.QUAD_TO:
58
+ case SVGPathData.SMOOTH_QUAD_TO:
59
+ case SVGPathData.ARC:
60
+ currentRing.push([cmd.x, cmd.y]);
61
+ break;
62
+ }
63
+ }
64
+
65
+ if (currentRing.length > 2)
66
+ rings.push({ points: currentRing });
67
+
68
+ if (rings.length > 0) {
69
+ const bounds = boundsFromPoints(rings[0].points);
70
+ return { rings, bounds };
71
+ }
72
+
73
+ }