@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.
- package/dist/arrow-caps.d.ts +44 -0
- package/dist/arrow-caps.d.ts.map +1 -0
- package/dist/arrow-caps.js +131 -0
- package/dist/arrow-caps.js.map +1 -0
- package/dist/chunks/checkpoint.js +78 -0
- package/dist/chunks/checkpoint.js.map +1 -0
- package/dist/chunks/components/properties.js +1571 -0
- package/dist/chunks/components/properties.js.map +1 -0
- package/dist/chunks/coordinate.js +87 -33
- package/dist/chunks/coordinate.js.map +1 -1
- package/dist/chunks/font-style.js +2 -1
- package/dist/chunks/font-style.js.map +1 -1
- package/dist/chunks/index.js +1283 -0
- package/dist/chunks/index.js.map +1 -0
- package/dist/constants.js +1 -18
- package/dist/constants2.d.ts +0 -1
- package/dist/constants2.d.ts.map +1 -1
- package/dist/coordinate.d.ts +107 -11
- package/dist/coordinate.d.ts.map +1 -1
- package/dist/coordinate.js +2 -2
- package/dist/coordinate2.d.ts +2 -2
- package/dist/copy.d.ts +2 -2
- package/dist/copy.js +2 -2
- package/dist/geometry.d.ts +2 -2
- package/dist/geometry.js +2 -431
- package/dist/guides.d.ts +2 -0
- package/dist/guides.js +0 -0
- package/dist/guides2.d.ts +11 -0
- package/dist/guides2.d.ts.map +1 -0
- package/dist/hit-test.d.ts +1 -2
- package/dist/hit-test.d.ts.map +1 -1
- package/dist/hit-test.js +26 -14
- package/dist/hit-test.js.map +1 -1
- package/dist/images.js +0 -1
- package/dist/images.js.map +1 -1
- package/dist/images2.d.ts.map +1 -1
- package/dist/index.d.ts +5 -3
- package/dist/index.js +5 -6
- package/dist/instances.d.ts +2 -2
- package/dist/instances.js +2 -2
- package/dist/layout-guides.d.ts +2 -0
- package/dist/layout-guides.js +2 -0
- package/dist/layout-guides2.d.ts +16 -0
- package/dist/layout-guides2.d.ts.map +1 -0
- package/dist/matrix2.d.ts +0 -1
- package/dist/matrix2.d.ts.map +1 -1
- package/dist/node-defaults.d.ts +1 -2
- package/dist/node-defaults.d.ts.map +1 -1
- package/dist/node-defaults.js +1 -1
- package/dist/parse-path.d.ts +1 -2
- package/dist/parse-path.d.ts.map +1 -1
- package/dist/parse-path.js.map +1 -1
- package/dist/preview.d.ts +2 -12
- package/dist/preview.js +1 -1
- package/dist/preview2.d.ts +12 -0
- package/dist/preview2.d.ts.map +1 -0
- package/dist/primitives2.d.ts.map +1 -1
- package/dist/resize.d.ts +18 -4
- package/dist/resize.d.ts.map +1 -1
- package/dist/resize.js +70 -22
- package/dist/resize.js.map +1 -1
- package/dist/snap.js +1 -1
- package/dist/source-metadata.d.ts +1 -2
- package/dist/source-metadata.d.ts.map +1 -1
- package/dist/source-metadata.js +1 -1
- package/dist/text-picture.d.ts +13 -2
- package/dist/text-picture.d.ts.map +1 -1
- package/dist/text-picture.js +2 -2
- package/dist/types.d.ts +499 -2
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -1296
- package/dist/types2.d.ts +2 -430
- package/dist/undo.js +1 -1
- package/dist/undo2.d.ts +5 -0
- package/dist/undo2.d.ts.map +1 -1
- package/dist/variables.d.ts +1 -2
- package/dist/variables.d.ts.map +1 -1
- package/dist/variables.js +1 -1
- package/dist/variant-name.d.ts.map +1 -1
- package/dist/variant-properties.d.ts +10 -0
- package/dist/variant-properties.d.ts.map +1 -0
- package/dist/variant-properties.js +39 -0
- package/dist/variant-properties.js.map +1 -0
- package/dist/vector-network.js +1 -1
- package/dist/vector-network2.d.ts +1 -2
- package/dist/vector-network2.d.ts.map +1 -1
- package/package.json +40 -15
- package/src/arrow-caps.ts +146 -0
- package/src/bindings.ts +20 -0
- package/src/checkpoint.ts +123 -0
- package/src/components/properties.ts +238 -0
- package/src/constants.ts +6 -0
- package/src/coordinate.ts +226 -0
- package/src/copy.ts +294 -0
- package/src/events.ts +21 -0
- package/src/export-scale.ts +16 -0
- package/src/font-style.ts +77 -0
- package/src/geometry.ts +530 -0
- package/src/guides.ts +8 -0
- package/src/hit-test.ts +205 -0
- package/src/images.ts +34 -0
- package/src/index.ts +686 -0
- package/src/instance-overrides.test.ts +67 -0
- package/src/instance-overrides.ts +152 -0
- package/src/instances.ts +480 -0
- package/src/layout-guides.ts +103 -0
- package/src/matrix.ts +116 -0
- package/src/mutation-impact.ts +70 -0
- package/src/node-defaults.ts +186 -0
- package/src/parse-path.ts +191 -0
- package/src/preview.ts +80 -0
- package/src/primitives.ts +42 -0
- package/src/resize.ts +302 -0
- package/src/shared-styles.ts +73 -0
- package/src/snap.ts +129 -0
- package/src/source-metadata.ts +8 -0
- package/src/text-picture.ts +65 -0
- package/src/types.ts +635 -0
- package/src/undo.ts +171 -0
- package/src/variables.ts +399 -0
- package/src/variant-name.ts +15 -0
- package/src/variant-properties.ts +48 -0
- package/src/vector-network.ts +293 -0
- package/dist/chunks/copy.js +0 -576
- package/dist/chunks/copy.js.map +0 -1
- package/dist/constants.js.map +0 -1
- package/dist/geometry.js.map +0 -1
- package/dist/preview.d.ts.map +0 -1
- package/dist/types.js.map +0 -1
- package/dist/types2.d.ts.map +0 -1
package/src/geometry.ts
ADDED
|
@@ -0,0 +1,530 @@
|
|
|
1
|
+
import { arrowCapOverflow } from './arrow-caps'
|
|
2
|
+
import type { Effect, Stroke, StrokeCap, VectorNetwork } from './index'
|
|
3
|
+
import type { Rect, Vector } from './primitives'
|
|
4
|
+
|
|
5
|
+
export function degToRad(degrees: number): number {
|
|
6
|
+
return (degrees * Math.PI) / 180
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function radToDeg(radians: number): number {
|
|
10
|
+
return (radians * 180) / Math.PI
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function rotatePoint(px: number, py: number, cx: number, cy: number, rad: number): Vector {
|
|
14
|
+
const cos = Math.cos(rad)
|
|
15
|
+
const sin = Math.sin(rad)
|
|
16
|
+
return {
|
|
17
|
+
x: cx + (px - cx) * cos - (py - cy) * sin,
|
|
18
|
+
y: cy + (px - cx) * sin + (py - cy) * cos
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function rotatedCorners(
|
|
23
|
+
cx: number,
|
|
24
|
+
cy: number,
|
|
25
|
+
hw: number,
|
|
26
|
+
hh: number,
|
|
27
|
+
rotationDeg: number
|
|
28
|
+
): [Vector, Vector, Vector, Vector] {
|
|
29
|
+
const rad = degToRad(rotationDeg)
|
|
30
|
+
const cos = Math.cos(rad)
|
|
31
|
+
const sin = Math.sin(rad)
|
|
32
|
+
return [
|
|
33
|
+
{ x: cx + -hw * cos - -hh * sin, y: cy + -hw * sin + -hh * cos },
|
|
34
|
+
{ x: cx + hw * cos - -hh * sin, y: cy + hw * sin + -hh * cos },
|
|
35
|
+
{ x: cx + hw * cos - hh * sin, y: cy + hw * sin + hh * cos },
|
|
36
|
+
{ x: cx + -hw * cos - hh * sin, y: cy + -hw * sin + hh * cos }
|
|
37
|
+
]
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function rotatedBBox(
|
|
41
|
+
x: number,
|
|
42
|
+
y: number,
|
|
43
|
+
w: number,
|
|
44
|
+
h: number,
|
|
45
|
+
rotationDeg: number
|
|
46
|
+
): { left: number; right: number; top: number; bottom: number; centerX: number; centerY: number } {
|
|
47
|
+
if (rotationDeg === 0) {
|
|
48
|
+
return {
|
|
49
|
+
left: x,
|
|
50
|
+
right: x + w,
|
|
51
|
+
top: y,
|
|
52
|
+
bottom: y + h,
|
|
53
|
+
centerX: x + w / 2,
|
|
54
|
+
centerY: y + h / 2
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
const corners = rotatedCorners(x + w / 2, y + h / 2, w / 2, h / 2, rotationDeg)
|
|
58
|
+
let left = Infinity,
|
|
59
|
+
right = -Infinity,
|
|
60
|
+
top = Infinity,
|
|
61
|
+
bottom = -Infinity
|
|
62
|
+
for (const c of corners) {
|
|
63
|
+
left = Math.min(left, c.x)
|
|
64
|
+
right = Math.max(right, c.x)
|
|
65
|
+
top = Math.min(top, c.y)
|
|
66
|
+
bottom = Math.max(bottom, c.y)
|
|
67
|
+
}
|
|
68
|
+
return { left, right, top, bottom, centerX: (left + right) / 2, centerY: (top + bottom) / 2 }
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface VisualBounds {
|
|
72
|
+
minX: number
|
|
73
|
+
minY: number
|
|
74
|
+
maxX: number
|
|
75
|
+
maxY: number
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
type BoundsAccumulator = VisualBounds
|
|
79
|
+
|
|
80
|
+
function createBoundsAccumulator(): BoundsAccumulator {
|
|
81
|
+
return { minX: Infinity, minY: Infinity, maxX: -Infinity, maxY: -Infinity }
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function includePoint(bounds: BoundsAccumulator, x: number, y: number): void {
|
|
85
|
+
bounds.minX = Math.min(bounds.minX, x)
|
|
86
|
+
bounds.minY = Math.min(bounds.minY, y)
|
|
87
|
+
bounds.maxX = Math.max(bounds.maxX, x)
|
|
88
|
+
bounds.maxY = Math.max(bounds.maxY, y)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function includeRect(bounds: BoundsAccumulator, rect: Rect): void {
|
|
92
|
+
includePoint(bounds, rect.x, rect.y)
|
|
93
|
+
includePoint(bounds, rect.x + rect.width, rect.y + rect.height)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function boundsToRect(bounds: BoundsAccumulator): Rect {
|
|
97
|
+
return bounds.minX === Infinity
|
|
98
|
+
? { x: 0, y: 0, width: 0, height: 0 }
|
|
99
|
+
: {
|
|
100
|
+
x: bounds.minX,
|
|
101
|
+
y: bounds.minY,
|
|
102
|
+
width: bounds.maxX - bounds.minX,
|
|
103
|
+
height: bounds.maxY - bounds.minY
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function computeBounds(items: Iterable<Rect>): Rect {
|
|
108
|
+
const bounds = createBoundsAccumulator()
|
|
109
|
+
for (const item of items) includeRect(bounds, item)
|
|
110
|
+
return boundsToRect(bounds)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function polygonVertices(node: {
|
|
114
|
+
width: number
|
|
115
|
+
height: number
|
|
116
|
+
pointCount: number
|
|
117
|
+
type: string
|
|
118
|
+
starInnerRadius: number
|
|
119
|
+
}): Vector[] {
|
|
120
|
+
const cx = node.width / 2
|
|
121
|
+
const cy = node.height / 2
|
|
122
|
+
const rx = node.width / 2
|
|
123
|
+
const ry = node.height / 2
|
|
124
|
+
const pointCount = Math.max(3, node.pointCount)
|
|
125
|
+
const isStar = node.type === 'STAR'
|
|
126
|
+
const innerRatio = isStar ? node.starInnerRadius : 1
|
|
127
|
+
const totalPoints = isStar ? pointCount * 2 : pointCount
|
|
128
|
+
const angleOffset = -Math.PI / 2
|
|
129
|
+
|
|
130
|
+
return Array.from({ length: totalPoints }, (_, index) => {
|
|
131
|
+
const angle = angleOffset + (2 * Math.PI * index) / totalPoints
|
|
132
|
+
const isInner = isStar && index % 2 === 1
|
|
133
|
+
const radius = isInner ? innerRatio : 1
|
|
134
|
+
return {
|
|
135
|
+
x: cx + rx * radius * Math.cos(angle),
|
|
136
|
+
y: cy + ry * radius * Math.sin(angle)
|
|
137
|
+
}
|
|
138
|
+
})
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function strokeOverflow(
|
|
142
|
+
strokes?: Stroke[],
|
|
143
|
+
fallbackCap?: StrokeCap,
|
|
144
|
+
network?: VectorNetwork | null
|
|
145
|
+
): number {
|
|
146
|
+
let overflow = 0
|
|
147
|
+
for (const stroke of strokes ?? []) {
|
|
148
|
+
if (!stroke.visible) continue
|
|
149
|
+
let extra = 0
|
|
150
|
+
if (stroke.align === 'OUTSIDE') extra = stroke.weight
|
|
151
|
+
else if (stroke.align === 'CENTER') extra = stroke.weight / 2
|
|
152
|
+
overflow = Math.max(overflow, extra)
|
|
153
|
+
}
|
|
154
|
+
return Math.max(overflow, arrowCapOverflow(strokes, fallbackCap, network))
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export function effectOverflow(effects?: Effect[]) {
|
|
158
|
+
let left = 0
|
|
159
|
+
let right = 0
|
|
160
|
+
let top = 0
|
|
161
|
+
let bottom = 0
|
|
162
|
+
|
|
163
|
+
for (const effect of effects ?? []) {
|
|
164
|
+
if (!effect.visible) continue
|
|
165
|
+
if (
|
|
166
|
+
effect.type !== 'DROP_SHADOW' &&
|
|
167
|
+
effect.type !== 'LAYER_BLUR' &&
|
|
168
|
+
effect.type !== 'FOREGROUND_BLUR'
|
|
169
|
+
) {
|
|
170
|
+
continue
|
|
171
|
+
}
|
|
172
|
+
const blurSpread = effect.radius + effect.spread
|
|
173
|
+
left = Math.max(left, blurSpread + Math.max(0, -effect.offset.x))
|
|
174
|
+
right = Math.max(right, blurSpread + Math.max(0, effect.offset.x))
|
|
175
|
+
top = Math.max(top, blurSpread + Math.max(0, -effect.offset.y))
|
|
176
|
+
bottom = Math.max(bottom, blurSpread + Math.max(0, effect.offset.y))
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
return { left, right, top, bottom }
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export function computeAbsoluteBounds(
|
|
183
|
+
nodes: Iterable<{ id: string; width: number; height: number }>,
|
|
184
|
+
getAbsolutePosition: (id: string) => Vector
|
|
185
|
+
): Rect {
|
|
186
|
+
const bounds = createBoundsAccumulator()
|
|
187
|
+
for (const n of nodes) {
|
|
188
|
+
const abs = getAbsolutePosition(n.id)
|
|
189
|
+
includeRect(bounds, { x: abs.x, y: abs.y, width: n.width, height: n.height })
|
|
190
|
+
}
|
|
191
|
+
return boundsToRect(bounds)
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export function computeVisualBounds(
|
|
195
|
+
nodes: Iterable<{
|
|
196
|
+
id: string
|
|
197
|
+
width: number
|
|
198
|
+
height: number
|
|
199
|
+
rotation?: number
|
|
200
|
+
strokes?: Stroke[]
|
|
201
|
+
strokeCap?: StrokeCap
|
|
202
|
+
vectorNetwork?: VectorNetwork | null
|
|
203
|
+
effects?: Effect[]
|
|
204
|
+
}>,
|
|
205
|
+
getAbsolutePosition: (id: string) => Vector
|
|
206
|
+
): Rect {
|
|
207
|
+
const bounds = createBoundsAccumulator()
|
|
208
|
+
|
|
209
|
+
for (const n of nodes) {
|
|
210
|
+
const abs = getAbsolutePosition(n.id)
|
|
211
|
+
const bbox = rotatedBBox(abs.x, abs.y, n.width, n.height, n.rotation ?? 0)
|
|
212
|
+
const stroke = strokeOverflow(n.strokes, n.strokeCap, n.vectorNetwork)
|
|
213
|
+
const effects = effectOverflow(n.effects)
|
|
214
|
+
includePoint(bounds, bbox.left - stroke - effects.left, bbox.top - stroke - effects.top)
|
|
215
|
+
includePoint(bounds, bbox.right + stroke + effects.right, bbox.bottom + stroke + effects.bottom)
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return boundsToRect(bounds)
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export interface VisualBoundsNode {
|
|
222
|
+
id: string
|
|
223
|
+
width: number
|
|
224
|
+
height: number
|
|
225
|
+
rotation?: number
|
|
226
|
+
flipX?: boolean
|
|
227
|
+
flipY?: boolean
|
|
228
|
+
strokes?: Stroke[]
|
|
229
|
+
strokeCap?: StrokeCap
|
|
230
|
+
vectorNetwork?: VectorNetwork | null
|
|
231
|
+
effects?: Effect[]
|
|
232
|
+
fillGeometry?: Array<{ commandsBlob: Uint8Array }>
|
|
233
|
+
strokeGeometry?: Array<{ commandsBlob: Uint8Array }>
|
|
234
|
+
childIds?: string[]
|
|
235
|
+
visible?: boolean
|
|
236
|
+
type?: string
|
|
237
|
+
clipsContent?: boolean
|
|
238
|
+
fontSize?: number
|
|
239
|
+
textDecoration?: string
|
|
240
|
+
textUnderlineOffset?: number | null
|
|
241
|
+
textDecorationThickness?: number | null
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export function unionVisualBounds(
|
|
245
|
+
a: VisualBounds | null,
|
|
246
|
+
b: VisualBounds | null
|
|
247
|
+
): VisualBounds | null {
|
|
248
|
+
if (!a) return b
|
|
249
|
+
if (!b) return a
|
|
250
|
+
return {
|
|
251
|
+
minX: Math.min(a.minX, b.minX),
|
|
252
|
+
minY: Math.min(a.minY, b.minY),
|
|
253
|
+
maxX: Math.max(a.maxX, b.maxX),
|
|
254
|
+
maxY: Math.max(a.maxY, b.maxY)
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
export function intersectVisualBounds(a: VisualBounds, b: VisualBounds): VisualBounds | null {
|
|
259
|
+
const minX = Math.max(a.minX, b.minX)
|
|
260
|
+
const minY = Math.max(a.minY, b.minY)
|
|
261
|
+
const maxX = Math.min(a.maxX, b.maxX)
|
|
262
|
+
const maxY = Math.min(a.maxY, b.maxY)
|
|
263
|
+
return minX < maxX && minY < maxY ? { minX, minY, maxX, maxY } : null
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Coordinate pairs per path blob command: 0=close, 1=move, 2=line,
|
|
268
|
+
* 3=quad (glyph-derived blobs are quad-heavy), 4=cubic. Unknown → null so
|
|
269
|
+
* walkers stop instead of desyncing into coordinate bytes.
|
|
270
|
+
*/
|
|
271
|
+
export function geometryCommandCoordCount(command: number): number | null {
|
|
272
|
+
if (command === 0) return 0
|
|
273
|
+
if (command === 1 || command === 2) return 1
|
|
274
|
+
if (command === 3) return 2
|
|
275
|
+
if (command === 4) return 3
|
|
276
|
+
return null
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
export function geometryBlobBounds(paths: Array<{ commandsBlob: Uint8Array }>): Rect | null {
|
|
280
|
+
const bounds = createBoundsAccumulator()
|
|
281
|
+
|
|
282
|
+
for (const path of paths) {
|
|
283
|
+
const blob = path.commandsBlob
|
|
284
|
+
const dv = new DataView(blob.buffer, blob.byteOffset, blob.byteLength)
|
|
285
|
+
let offset = 0
|
|
286
|
+
while (offset < blob.length) {
|
|
287
|
+
const command = blob[offset++]
|
|
288
|
+
const coords = geometryCommandCoordCount(command)
|
|
289
|
+
if (coords == null) break
|
|
290
|
+
for (let i = 0; i < coords; i++) {
|
|
291
|
+
if (offset + 8 > blob.length) break
|
|
292
|
+
const x = dv.getFloat32(offset, true)
|
|
293
|
+
const y = dv.getFloat32(offset + 4, true)
|
|
294
|
+
includePoint(bounds, x, y)
|
|
295
|
+
offset += 8
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
return bounds.minX === Infinity ? null : boundsToRect(bounds)
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function transformLocalPoint(node: VisualBoundsNode, point: Vector): Vector {
|
|
304
|
+
let x = node.flipX ? node.width - point.x : point.x
|
|
305
|
+
let y = node.flipY ? node.height - point.y : point.y
|
|
306
|
+
const rotation = node.rotation ?? 0
|
|
307
|
+
if (rotation !== 0) {
|
|
308
|
+
const rotated = rotatePoint(x, y, node.width / 2, node.height / 2, degToRad(rotation))
|
|
309
|
+
x = rotated.x
|
|
310
|
+
y = rotated.y
|
|
311
|
+
}
|
|
312
|
+
return { x, y }
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function transformedLocalBounds(node: VisualBoundsNode, local: Rect, abs: Vector): VisualBounds {
|
|
316
|
+
const points = [
|
|
317
|
+
{ x: local.x, y: local.y },
|
|
318
|
+
{ x: local.x + local.width, y: local.y },
|
|
319
|
+
{ x: local.x + local.width, y: local.y + local.height },
|
|
320
|
+
{ x: local.x, y: local.y + local.height }
|
|
321
|
+
].map((point) => transformLocalPoint(node, point))
|
|
322
|
+
|
|
323
|
+
return {
|
|
324
|
+
minX: abs.x + Math.min(...points.map((point) => point.x)),
|
|
325
|
+
minY: abs.y + Math.min(...points.map((point) => point.y)),
|
|
326
|
+
maxX: abs.x + Math.max(...points.map((point) => point.x)),
|
|
327
|
+
maxY: abs.y + Math.max(...points.map((point) => point.y))
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
export function nodeVisualBounds(
|
|
332
|
+
node: VisualBoundsNode,
|
|
333
|
+
getAbsolutePosition: (id: string) => Vector
|
|
334
|
+
): VisualBounds {
|
|
335
|
+
const abs = getAbsolutePosition(node.id)
|
|
336
|
+
const base = computeVisualBounds([node], getAbsolutePosition)
|
|
337
|
+
let bounds: VisualBounds = {
|
|
338
|
+
minX: base.x,
|
|
339
|
+
minY: base.y,
|
|
340
|
+
maxX: base.x + base.width,
|
|
341
|
+
maxY: base.y + base.height
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
const hasNonInsideStroke = node.strokes?.some(
|
|
345
|
+
(stroke) => stroke.visible && stroke.align !== 'INSIDE'
|
|
346
|
+
)
|
|
347
|
+
const localGeometry = geometryBlobBounds([
|
|
348
|
+
...(node.fillGeometry ?? []),
|
|
349
|
+
...(hasNonInsideStroke ? (node.strokeGeometry ?? []) : [])
|
|
350
|
+
])
|
|
351
|
+
if (localGeometry) {
|
|
352
|
+
bounds = unionVisualBounds(bounds, transformedLocalBounds(node, localGeometry, abs)) ?? bounds
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
if (node.type === 'TEXT' && node.textDecoration && node.textDecoration !== 'NONE') {
|
|
356
|
+
const fontSize = node.fontSize ?? 14
|
|
357
|
+
const underlineOffset = node.textUnderlineOffset ?? fontSize * 0.18
|
|
358
|
+
const thickness = node.textDecorationThickness ?? Math.max(1, fontSize / 16)
|
|
359
|
+
bounds.maxY += underlineOffset + thickness + fontSize * 0.35
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
return bounds
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
function collectDescendantVisualBounds(
|
|
366
|
+
nodeId: string,
|
|
367
|
+
getNode: (id: string) => VisualBoundsNode | undefined,
|
|
368
|
+
getAbsolutePosition: (id: string) => Vector,
|
|
369
|
+
clip: VisualBounds | null = null
|
|
370
|
+
): VisualBounds | null {
|
|
371
|
+
const node = getNode(nodeId)
|
|
372
|
+
if (!node?.visible) return null
|
|
373
|
+
|
|
374
|
+
const own = nodeVisualBounds(node, getAbsolutePosition)
|
|
375
|
+
let bounds = clip ? intersectVisualBounds(own, clip) : own
|
|
376
|
+
|
|
377
|
+
const isClippableContainer =
|
|
378
|
+
node.type === 'FRAME' || node.type === 'COMPONENT' || node.type === 'INSTANCE'
|
|
379
|
+
let childClip = clip
|
|
380
|
+
if (isClippableContainer && node.clipsContent) {
|
|
381
|
+
const abs = getAbsolutePosition(node.id)
|
|
382
|
+
const nodeClip = {
|
|
383
|
+
minX: abs.x,
|
|
384
|
+
minY: abs.y,
|
|
385
|
+
maxX: abs.x + node.width,
|
|
386
|
+
maxY: abs.y + node.height
|
|
387
|
+
}
|
|
388
|
+
childClip = childClip ? intersectVisualBounds(childClip, nodeClip) : nodeClip
|
|
389
|
+
if (!childClip) return bounds
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
for (const childId of node.childIds ?? []) {
|
|
393
|
+
bounds = unionVisualBounds(
|
|
394
|
+
bounds,
|
|
395
|
+
collectDescendantVisualBounds(childId, getNode, getAbsolutePosition, childClip)
|
|
396
|
+
)
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
return bounds
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
export function computeDescendantVisualBounds(
|
|
403
|
+
nodeIds: string[],
|
|
404
|
+
getNode: (id: string) => VisualBoundsNode | undefined,
|
|
405
|
+
getAbsolutePosition: (id: string) => Vector
|
|
406
|
+
): VisualBounds | null {
|
|
407
|
+
let bounds: VisualBounds | null = null
|
|
408
|
+
for (const nodeId of nodeIds) {
|
|
409
|
+
bounds = unionVisualBounds(
|
|
410
|
+
bounds,
|
|
411
|
+
collectDescendantVisualBounds(nodeId, getNode, getAbsolutePosition)
|
|
412
|
+
)
|
|
413
|
+
}
|
|
414
|
+
return bounds
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
// ── Rotated-rectangle clipping ──
|
|
418
|
+
|
|
419
|
+
function crossProduct(a: Vector, b: Vector, p: Vector): number {
|
|
420
|
+
return (b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x)
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
function lineSegmentIntersect(p1: Vector, p2: Vector, p3: Vector, p4: Vector): Vector {
|
|
424
|
+
const dx1 = p2.x - p1.x
|
|
425
|
+
const dy1 = p2.y - p1.y
|
|
426
|
+
const dx2 = p4.x - p3.x
|
|
427
|
+
const dy2 = p4.y - p3.y
|
|
428
|
+
const denom = dx1 * dy2 - dy1 * dx2
|
|
429
|
+
if (denom === 0) return p1
|
|
430
|
+
const t = ((p3.x - p1.x) * dy2 - (p3.y - p1.y) * dx2) / denom
|
|
431
|
+
return { x: p1.x + t * dx1, y: p1.y + t * dy1 }
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
function clipHalfPlane(polygon: Vector[], a: Vector, b: Vector, wantPositive: boolean): Vector[] {
|
|
435
|
+
const output: Vector[] = []
|
|
436
|
+
const isInside = (p: Vector): boolean => {
|
|
437
|
+
const cross = crossProduct(a, b, p)
|
|
438
|
+
return wantPositive ? cross >= 0 : cross <= 0
|
|
439
|
+
}
|
|
440
|
+
for (let i = 0; i < polygon.length; i++) {
|
|
441
|
+
const curr = polygon[i]
|
|
442
|
+
const prev = polygon[i === 0 ? polygon.length - 1 : i - 1]
|
|
443
|
+
const currInside = isInside(curr)
|
|
444
|
+
const prevInside = isInside(prev)
|
|
445
|
+
if (currInside) {
|
|
446
|
+
if (!prevInside) output.push(lineSegmentIntersect(prev, curr, a, b))
|
|
447
|
+
output.push(curr)
|
|
448
|
+
} else if (prevInside) {
|
|
449
|
+
output.push(lineSegmentIntersect(prev, curr, a, b))
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
return output
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* Clip a subject polygon against a convex polygon (e.g. the 4 canvas-space
|
|
457
|
+
* corners of a rotated clipping ancestor).
|
|
458
|
+
*
|
|
459
|
+
* Uses Sutherland–Hodgman polygon clipping with centroid-based interior
|
|
460
|
+
* detection, making it robust to either winding order of the clip polygon.
|
|
461
|
+
* Returns the clipped polygon, or null if the subject is fully outside the
|
|
462
|
+
* clip polygon. When `clipCorners` has fewer than 3 points the subject is
|
|
463
|
+
* returned unchanged (no clipping).
|
|
464
|
+
*
|
|
465
|
+
* Preserving the polygon (rather than collapsing to an AABB) lets callers
|
|
466
|
+
* chain multiple clips without reintroducing corners removed by an inner clip.
|
|
467
|
+
*/
|
|
468
|
+
export function clipPolygon(subject: Vector[], clipCorners: Vector[]): Vector[] | null {
|
|
469
|
+
if (clipCorners.length < 3) return subject
|
|
470
|
+
|
|
471
|
+
let cx = 0
|
|
472
|
+
let cy = 0
|
|
473
|
+
for (const c of clipCorners) {
|
|
474
|
+
cx += c.x
|
|
475
|
+
cy += c.y
|
|
476
|
+
}
|
|
477
|
+
cx /= clipCorners.length
|
|
478
|
+
cy /= clipCorners.length
|
|
479
|
+
|
|
480
|
+
let polygon: Vector[] = subject
|
|
481
|
+
|
|
482
|
+
for (let i = 0; i < clipCorners.length; i++) {
|
|
483
|
+
if (polygon.length === 0) return null
|
|
484
|
+
const a = clipCorners[i]
|
|
485
|
+
const b = clipCorners[(i + 1) % clipCorners.length]
|
|
486
|
+
const centroidCross = crossProduct(a, b, { x: cx, y: cy })
|
|
487
|
+
polygon = clipHalfPlane(polygon, a, b, centroidCross >= 0)
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
return polygon.length === 0 ? null : polygon
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/**
|
|
494
|
+
* Clip an axis-aligned VisualBounds rectangle against a convex polygon
|
|
495
|
+
* (e.g. the 4 canvas-space corners of a rotated clipping ancestor).
|
|
496
|
+
*
|
|
497
|
+
* Returns the AABB of the intersection, or null if the bounds are fully
|
|
498
|
+
* outside the clip polygon. Delegates to {@link clipPolygon}.
|
|
499
|
+
*
|
|
500
|
+
* For a non-rotated clip (axis-aligned corners) the result is identical
|
|
501
|
+
* to `intersectVisualBounds`.
|
|
502
|
+
*/
|
|
503
|
+
export function clipBoundsToPolygon(
|
|
504
|
+
bounds: VisualBounds,
|
|
505
|
+
clipCorners: Vector[]
|
|
506
|
+
): VisualBounds | null {
|
|
507
|
+
if (clipCorners.length < 3) return bounds
|
|
508
|
+
|
|
509
|
+
const subject: Vector[] = [
|
|
510
|
+
{ x: bounds.minX, y: bounds.minY },
|
|
511
|
+
{ x: bounds.maxX, y: bounds.minY },
|
|
512
|
+
{ x: bounds.maxX, y: bounds.maxY },
|
|
513
|
+
{ x: bounds.minX, y: bounds.maxY }
|
|
514
|
+
]
|
|
515
|
+
|
|
516
|
+
const polygon = clipPolygon(subject, clipCorners)
|
|
517
|
+
if (!polygon) return null
|
|
518
|
+
|
|
519
|
+
let minX = Infinity
|
|
520
|
+
let minY = Infinity
|
|
521
|
+
let maxX = -Infinity
|
|
522
|
+
let maxY = -Infinity
|
|
523
|
+
for (const p of polygon) {
|
|
524
|
+
if (p.x < minX) minX = p.x
|
|
525
|
+
if (p.y < minY) minY = p.y
|
|
526
|
+
if (p.x > maxX) maxX = p.x
|
|
527
|
+
if (p.y > maxY) maxY = p.y
|
|
528
|
+
}
|
|
529
|
+
return minX < maxX && minY < maxY ? { minX, minY, maxX, maxY } : null
|
|
530
|
+
}
|