@open-pencil/scene-graph 0.14.0 → 0.15.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 (130) hide show
  1. package/dist/arrow-caps.d.ts +44 -0
  2. package/dist/arrow-caps.d.ts.map +1 -0
  3. package/dist/arrow-caps.js +131 -0
  4. package/dist/arrow-caps.js.map +1 -0
  5. package/dist/chunks/checkpoint.js +78 -0
  6. package/dist/chunks/checkpoint.js.map +1 -0
  7. package/dist/chunks/components/properties.js +1571 -0
  8. package/dist/chunks/components/properties.js.map +1 -0
  9. package/dist/chunks/coordinate.js +87 -33
  10. package/dist/chunks/coordinate.js.map +1 -1
  11. package/dist/chunks/font-style.js +2 -1
  12. package/dist/chunks/font-style.js.map +1 -1
  13. package/dist/chunks/index.js +1283 -0
  14. package/dist/chunks/index.js.map +1 -0
  15. package/dist/constants.js +1 -18
  16. package/dist/constants2.d.ts +0 -1
  17. package/dist/constants2.d.ts.map +1 -1
  18. package/dist/coordinate.d.ts +107 -11
  19. package/dist/coordinate.d.ts.map +1 -1
  20. package/dist/coordinate.js +2 -2
  21. package/dist/coordinate2.d.ts +2 -2
  22. package/dist/copy.d.ts +2 -2
  23. package/dist/copy.js +2 -2
  24. package/dist/geometry.d.ts +2 -2
  25. package/dist/geometry.js +2 -431
  26. package/dist/guides.d.ts +2 -0
  27. package/dist/guides.js +0 -0
  28. package/dist/guides2.d.ts +11 -0
  29. package/dist/guides2.d.ts.map +1 -0
  30. package/dist/hit-test.d.ts +1 -2
  31. package/dist/hit-test.d.ts.map +1 -1
  32. package/dist/hit-test.js +26 -14
  33. package/dist/hit-test.js.map +1 -1
  34. package/dist/images.js +0 -1
  35. package/dist/images.js.map +1 -1
  36. package/dist/images2.d.ts.map +1 -1
  37. package/dist/index.d.ts +5 -3
  38. package/dist/index.js +5 -6
  39. package/dist/instances.d.ts +2 -2
  40. package/dist/instances.js +2 -2
  41. package/dist/layout-guides.d.ts +2 -0
  42. package/dist/layout-guides.js +2 -0
  43. package/dist/layout-guides2.d.ts +16 -0
  44. package/dist/layout-guides2.d.ts.map +1 -0
  45. package/dist/matrix2.d.ts +0 -1
  46. package/dist/matrix2.d.ts.map +1 -1
  47. package/dist/node-defaults.d.ts +1 -2
  48. package/dist/node-defaults.d.ts.map +1 -1
  49. package/dist/node-defaults.js +1 -1
  50. package/dist/parse-path.d.ts +1 -2
  51. package/dist/parse-path.d.ts.map +1 -1
  52. package/dist/parse-path.js.map +1 -1
  53. package/dist/preview.d.ts +2 -12
  54. package/dist/preview.js +1 -1
  55. package/dist/preview2.d.ts +12 -0
  56. package/dist/preview2.d.ts.map +1 -0
  57. package/dist/primitives2.d.ts.map +1 -1
  58. package/dist/resize.d.ts +18 -4
  59. package/dist/resize.d.ts.map +1 -1
  60. package/dist/resize.js +70 -22
  61. package/dist/resize.js.map +1 -1
  62. package/dist/snap.js +1 -1
  63. package/dist/source-metadata.d.ts +1 -2
  64. package/dist/source-metadata.d.ts.map +1 -1
  65. package/dist/source-metadata.js +1 -1
  66. package/dist/text-picture.d.ts +13 -2
  67. package/dist/text-picture.d.ts.map +1 -1
  68. package/dist/text-picture.js +2 -2
  69. package/dist/types.d.ts +499 -2
  70. package/dist/types.d.ts.map +1 -0
  71. package/dist/types.js +1 -1296
  72. package/dist/types2.d.ts +2 -430
  73. package/dist/undo.js +1 -1
  74. package/dist/undo2.d.ts +5 -0
  75. package/dist/undo2.d.ts.map +1 -1
  76. package/dist/variables.d.ts +1 -2
  77. package/dist/variables.d.ts.map +1 -1
  78. package/dist/variables.js +1 -1
  79. package/dist/variant-name.d.ts.map +1 -1
  80. package/dist/variant-properties.d.ts +10 -0
  81. package/dist/variant-properties.d.ts.map +1 -0
  82. package/dist/variant-properties.js +39 -0
  83. package/dist/variant-properties.js.map +1 -0
  84. package/dist/vector-network.js +1 -1
  85. package/dist/vector-network2.d.ts +1 -2
  86. package/dist/vector-network2.d.ts.map +1 -1
  87. package/package.json +40 -15
  88. package/src/arrow-caps.ts +146 -0
  89. package/src/bindings.ts +20 -0
  90. package/src/checkpoint.ts +123 -0
  91. package/src/components/properties.ts +238 -0
  92. package/src/constants.ts +6 -0
  93. package/src/coordinate.ts +226 -0
  94. package/src/copy.ts +294 -0
  95. package/src/events.ts +21 -0
  96. package/src/export-scale.ts +16 -0
  97. package/src/font-style.ts +77 -0
  98. package/src/geometry.ts +530 -0
  99. package/src/guides.ts +8 -0
  100. package/src/hit-test.ts +205 -0
  101. package/src/images.ts +34 -0
  102. package/src/index.ts +686 -0
  103. package/src/instance-overrides.test.ts +67 -0
  104. package/src/instance-overrides.ts +152 -0
  105. package/src/instances.ts +480 -0
  106. package/src/layout-guides.ts +103 -0
  107. package/src/matrix.ts +116 -0
  108. package/src/mutation-impact.ts +70 -0
  109. package/src/node-defaults.ts +186 -0
  110. package/src/parse-path.ts +191 -0
  111. package/src/preview.ts +80 -0
  112. package/src/primitives.ts +42 -0
  113. package/src/resize.ts +302 -0
  114. package/src/shared-styles.ts +73 -0
  115. package/src/snap.ts +129 -0
  116. package/src/source-metadata.ts +8 -0
  117. package/src/text-picture.ts +65 -0
  118. package/src/types.ts +635 -0
  119. package/src/undo.ts +171 -0
  120. package/src/variables.ts +399 -0
  121. package/src/variant-name.ts +15 -0
  122. package/src/variant-properties.ts +48 -0
  123. package/src/vector-network.ts +293 -0
  124. package/dist/chunks/copy.js +0 -576
  125. package/dist/chunks/copy.js.map +0 -1
  126. package/dist/constants.js.map +0 -1
  127. package/dist/geometry.js.map +0 -1
  128. package/dist/preview.d.ts.map +0 -1
  129. package/dist/types.js.map +0 -1
  130. package/dist/types2.d.ts.map +0 -1
@@ -0,0 +1,293 @@
1
+ import { isEqual } from 'es-toolkit/predicate'
2
+
3
+ import type { Mat3 } from './matrix'
4
+ import type { Vector } from './primitives'
5
+ import type { VectorNetwork, VectorSegment } from './types'
6
+
7
+ /** Concatenate vector networks while preserving independent geometry. */
8
+ export function mergeVectorNetworks(networks: readonly VectorNetwork[]): VectorNetwork {
9
+ const vertices: VectorNetwork['vertices'] = []
10
+ const segments: VectorNetwork['segments'] = []
11
+ const regions: VectorNetwork['regions'] = []
12
+ let vertexOffset = 0
13
+ let segmentOffset = 0
14
+
15
+ for (const network of networks) {
16
+ for (const vertex of network.vertices) vertices.push({ ...vertex })
17
+ for (const segment of network.segments) {
18
+ segments.push({
19
+ ...segment,
20
+ start: segment.start + vertexOffset,
21
+ end: segment.end + vertexOffset,
22
+ tangentStart: { ...segment.tangentStart },
23
+ tangentEnd: { ...segment.tangentEnd }
24
+ })
25
+ }
26
+ for (const region of network.regions) {
27
+ const loops: number[][] = []
28
+ for (const loop of region.loops) {
29
+ const shifted: number[] = []
30
+ for (const segmentIndex of loop) shifted.push(segmentIndex + segmentOffset)
31
+ loops.push(shifted)
32
+ }
33
+ regions.push({ windingRule: region.windingRule, loops })
34
+ }
35
+ vertexOffset += network.vertices.length
36
+ segmentOffset += network.segments.length
37
+ }
38
+
39
+ return { vertices, segments, regions }
40
+ }
41
+
42
+ /**
43
+ * Map a VectorNetwork through an affine matrix: vertices as points,
44
+ * tangents as direction vectors (linear part only, no translation).
45
+ * Returns a deep copy; the input is not mutated.
46
+ */
47
+ export function transformVectorNetwork(m: Mat3, vn: VectorNetwork): VectorNetwork {
48
+ const mapVector = (v: Vector): Vector => ({
49
+ x: m[0] * v.x + m[1] * v.y,
50
+ y: m[3] * v.x + m[4] * v.y
51
+ })
52
+ return {
53
+ vertices: vn.vertices.map((v) => ({
54
+ ...v,
55
+ x: m[0] * v.x + m[1] * v.y + m[2],
56
+ y: m[3] * v.x + m[4] * v.y + m[5]
57
+ })),
58
+ segments: vn.segments.map((s) => ({
59
+ ...s,
60
+ tangentStart: mapVector(s.tangentStart),
61
+ tangentEnd: mapVector(s.tangentEnd)
62
+ })),
63
+ regions: vn.regions.map((r) => ({
64
+ windingRule: r.windingRule,
65
+ loops: r.loops.map((l) => [...l])
66
+ }))
67
+ }
68
+ }
69
+
70
+ /** Structural equality of two VectorNetworks (order-sensitive, exact values). */
71
+ export function vectorNetworksEqual(a: VectorNetwork, b: VectorNetwork): boolean {
72
+ return (
73
+ isEqual(a.vertices, b.vertices) &&
74
+ isEqual(a.segments, b.segments) &&
75
+ isEqual(a.regions, b.regions)
76
+ )
77
+ }
78
+
79
+ /** Deep-copy a VectorNetwork, stripping any Vue Proxy wrappers. */
80
+ export function cloneVectorNetwork(vn: VectorNetwork): VectorNetwork {
81
+ return {
82
+ vertices: vn.vertices.map((v) => ({ ...v })),
83
+ segments: vn.segments.map((s) => ({
84
+ ...s,
85
+ tangentStart: { ...s.tangentStart },
86
+ tangentEnd: { ...s.tangentEnd }
87
+ })),
88
+ regions: vn.regions.map((r) => ({
89
+ windingRule: r.windingRule,
90
+ loops: r.loops.map((l) => [...l])
91
+ }))
92
+ }
93
+ }
94
+
95
+ /**
96
+ * Validate a VectorNetwork structure, returning an array of error messages.
97
+ * Empty array means the network is valid.
98
+ */
99
+ export function validateVectorNetwork(value: unknown): string[] {
100
+ if (!isRecord(value)) return ['network must be an object']
101
+ if (!Array.isArray(value.vertices)) return ['vertices must be an array']
102
+ if (!Array.isArray(value.segments)) return ['segments must be an array']
103
+
104
+ const errors: string[] = []
105
+ validateVertices(value.vertices, errors)
106
+ validateSegments(value.segments, value.vertices.length, errors)
107
+ const typedSegments = value.segments.filter(isSegmentRecord)
108
+ if (value.regions !== undefined) {
109
+ if (Array.isArray(value.regions)) {
110
+ validateRegions(
111
+ value.regions,
112
+ value.segments.length,
113
+ typedSegments.length === value.segments.length ? typedSegments : null,
114
+ errors
115
+ )
116
+ } else {
117
+ errors.push('regions must be an array when provided')
118
+ }
119
+ }
120
+ return errors
121
+ }
122
+
123
+ function validateVertices(vertices: unknown[], errors: string[]): void {
124
+ for (let index = 0; index < vertices.length; index++) {
125
+ if (!isFiniteVector(vertices[index])) {
126
+ errors.push(`vertex[${index}]: x and y must be finite numbers`)
127
+ }
128
+ }
129
+ }
130
+
131
+ function validateSegments(segments: unknown[], vertexCount: number, errors: string[]): void {
132
+ for (let index = 0; index < segments.length; index++) {
133
+ const segment = segments[index]
134
+ if (!isRecord(segment)) {
135
+ errors.push(`segment[${index}] must be an object`)
136
+ continue
137
+ }
138
+ if (!isInteger(segment.start) || !isInteger(segment.end)) {
139
+ errors.push(`segment[${index}]: start and end must be integers`)
140
+ continue
141
+ }
142
+ if (segment.start < 0 || segment.start >= vertexCount) {
143
+ errors.push(`segment[${index}]: start index ${segment.start} out of range`)
144
+ }
145
+ if (segment.end < 0 || segment.end >= vertexCount) {
146
+ errors.push(`segment[${index}]: end index ${segment.end} out of range`)
147
+ }
148
+ validateSegmentTangents(segment, index, errors)
149
+ }
150
+ }
151
+
152
+ function validateSegmentTangents(
153
+ segment: Record<string, unknown>,
154
+ index: number,
155
+ errors: string[]
156
+ ): void {
157
+ for (const tangentKey of ['tangentStart', 'tangentEnd'] as const) {
158
+ const tangent = segment[tangentKey]
159
+ if (tangent !== undefined && !isFiniteVector(tangent)) {
160
+ errors.push(`segment[${index}]: ${tangentKey} must contain finite x and y numbers`)
161
+ }
162
+ }
163
+ }
164
+
165
+ function validateRegions(
166
+ regions: unknown[],
167
+ segmentCount: number,
168
+ segments: Array<Record<'start' | 'end', number>> | null,
169
+ errors: string[]
170
+ ): void {
171
+ for (let regionIndex = 0; regionIndex < regions.length; regionIndex++) {
172
+ const region = regions[regionIndex]
173
+ if (!isRecord(region) || !Array.isArray(region.loops)) {
174
+ errors.push(`region[${regionIndex}]: loops must be an array`)
175
+ continue
176
+ }
177
+ if (region.windingRule !== 'NONZERO' && region.windingRule !== 'EVENODD') {
178
+ errors.push(`region[${regionIndex}]: windingRule must be NONZERO or EVENODD`)
179
+ }
180
+ if (region.loops.length === 0) {
181
+ errors.push(`region[${regionIndex}]: loops must contain at least one loop`)
182
+ }
183
+ validateRegionLoops(region.loops, regionIndex, segmentCount, segments, errors)
184
+ }
185
+ }
186
+
187
+ function validateRegionLoops(
188
+ loops: unknown[],
189
+ regionIndex: number,
190
+ segmentCount: number,
191
+ segments: Array<Record<'start' | 'end', number>> | null,
192
+ errors: string[]
193
+ ): void {
194
+ for (let loopIndex = 0; loopIndex < loops.length; loopIndex++) {
195
+ const loop = loops[loopIndex]
196
+ if (!Array.isArray(loop)) {
197
+ errors.push(`region[${regionIndex}].loop[${loopIndex}] must be an array`)
198
+ continue
199
+ }
200
+ if (loop.length === 0) {
201
+ errors.push(`region[${regionIndex}].loop[${loopIndex}] must contain at least one segment`)
202
+ continue
203
+ }
204
+
205
+ const segmentIndices: number[] = []
206
+ for (const segmentIndex of loop) {
207
+ if (!isInteger(segmentIndex) || segmentIndex < 0 || segmentIndex >= segmentCount) {
208
+ errors.push(
209
+ `region[${regionIndex}].loop[${loopIndex}]: segment index ${String(segmentIndex)} out of range`
210
+ )
211
+ } else {
212
+ segmentIndices.push(segmentIndex)
213
+ }
214
+ }
215
+ if (segmentIndices.length !== loop.length) continue
216
+ if (new Set(segmentIndices).size !== segmentIndices.length) {
217
+ errors.push(`region[${regionIndex}].loop[${loopIndex}] must not repeat segments`)
218
+ continue
219
+ }
220
+ if (segments && !formsContinuousChain(segmentIndices, segments)) {
221
+ errors.push(`region[${regionIndex}].loop[${loopIndex}] segments must form a continuous chain`)
222
+ }
223
+ }
224
+ }
225
+
226
+ function formsContinuousChain(
227
+ indices: number[],
228
+ segments: Array<Record<'start' | 'end', number>>
229
+ ): boolean {
230
+ if (indices.length <= 1) return true
231
+ const first = segments[indices[0]]
232
+ return followsChain(indices, segments, first.end) || followsChain(indices, segments, first.start)
233
+ }
234
+
235
+ function followsChain(
236
+ indices: number[],
237
+ segments: Array<Record<'start' | 'end', number>>,
238
+ initialEnd: number
239
+ ): boolean {
240
+ let current = initialEnd
241
+ for (let index = 1; index < indices.length; index++) {
242
+ const segment = segments[indices[index]]
243
+ if (segment.start === current) current = segment.end
244
+ else if (segment.end === current) current = segment.start
245
+ else return false
246
+ }
247
+ return true
248
+ }
249
+
250
+ function isSegmentRecord(value: unknown): value is Record<'start' | 'end', number> {
251
+ return isRecord(value) && isInteger(value.start) && isInteger(value.end)
252
+ }
253
+
254
+ function isFiniteVector(value: unknown): value is Vector {
255
+ return (
256
+ isRecord(value) &&
257
+ typeof value.x === 'number' &&
258
+ Number.isFinite(value.x) &&
259
+ typeof value.y === 'number' &&
260
+ Number.isFinite(value.y)
261
+ )
262
+ }
263
+
264
+ function isInteger(value: unknown): value is number {
265
+ return typeof value === 'number' && Number.isInteger(value)
266
+ }
267
+
268
+ function isRecord(value: unknown): value is Record<string, unknown> {
269
+ return typeof value === 'object' && value !== null
270
+ }
271
+
272
+ export type NormalizableVectorNetwork = Omit<VectorNetwork, 'regions'> & {
273
+ regions?: VectorNetwork['regions']
274
+ }
275
+
276
+ /**
277
+ * Ensure every segment has tangentStart/tangentEnd and a regions array.
278
+ * Missing tangents default to {x:0, y:0} (straight line segments).
279
+ * Use at system boundaries where input may come from JSON/MCP.
280
+ */
281
+ export function normalizeVectorNetwork(vn: NormalizableVectorNetwork): VectorNetwork {
282
+ const ZERO: Vector = { x: 0, y: 0 }
283
+ return {
284
+ vertices: vn.vertices,
285
+ segments: vn.segments.map((s) => ({
286
+ start: s.start,
287
+ end: s.end,
288
+ tangentStart: (s as Partial<VectorSegment>).tangentStart ?? { ...ZERO },
289
+ tangentEnd: (s as Partial<VectorSegment>).tangentEnd ?? { ...ZERO }
290
+ })),
291
+ regions: vn.regions ?? []
292
+ }
293
+ }