@pairdraw/scene-graph 0.16.1 → 0.17.0
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/checkpoint.d.ts.map +1 -1
- package/dist/checkpoint.js +8 -3
- package/dist/checkpoint.js.map +1 -1
- package/dist/color/index.d.ts.map +1 -1
- package/dist/color/index.js +3 -3
- package/dist/color/index.js.map +1 -1
- package/dist/color/management.d.ts.map +1 -1
- package/dist/color/management.js +4 -2
- package/dist/color/management.js.map +1 -1
- package/dist/coordinate.d.ts.map +1 -1
- package/dist/coordinate.js +3 -1
- package/dist/coordinate.js.map +1 -1
- package/dist/copy.d.ts.map +1 -1
- package/dist/copy.js +1 -0
- package/dist/copy.js.map +1 -1
- package/dist/css/values.d.ts.map +1 -1
- package/dist/css/values.js +2 -2
- package/dist/css/values.js.map +1 -1
- package/dist/geometry.d.ts +14 -7
- package/dist/geometry.d.ts.map +1 -1
- package/dist/geometry.js +130 -26
- package/dist/geometry.js.map +1 -1
- package/dist/graph.d.ts.map +1 -1
- package/dist/graph.js +7 -3
- package/dist/graph.js.map +1 -1
- package/dist/hit-test.d.ts.map +1 -1
- package/dist/hit-test.js +100 -8
- package/dist/hit-test.js.map +1 -1
- package/dist/instances/content-size.js +1 -1
- package/dist/instances/index.d.ts.map +1 -1
- package/dist/instances/index.js +4 -1
- package/dist/instances/index.js.map +1 -1
- package/dist/instances/sync.js +4 -3
- package/dist/instances/sync.js.map +1 -1
- package/dist/layout/index.js +1 -1
- package/dist/resize.d.ts +4 -1
- package/dist/resize.d.ts.map +1 -1
- package/dist/resize.js +18 -12
- package/dist/resize.js.map +1 -1
- package/dist/scaling/node.d.ts.map +1 -1
- package/dist/scaling/node.js +3 -1
- package/dist/scaling/node.js.map +1 -1
- package/dist/scaling/tree.d.ts.map +1 -1
- package/dist/scaling/tree.js +8 -5
- package/dist/scaling/tree.js.map +1 -1
- package/dist/slots/authoring.js +3 -6
- package/dist/slots/authoring.js.map +1 -1
- package/dist/slots/content.d.ts.map +1 -1
- package/dist/slots/content.js +15 -25
- package/dist/slots/content.js.map +1 -1
- package/dist/slots/frames.js +1 -2
- package/dist/slots/frames.js.map +1 -1
- package/dist/snap.d.ts.map +1 -1
- package/dist/snap.js +12 -4
- package/dist/snap.js.map +1 -1
- package/dist/types.d.ts +5 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/undo.d.ts +2 -1
- package/dist/undo.d.ts.map +1 -1
- package/dist/undo.js +3 -2
- package/dist/undo.js.map +1 -1
- package/dist/variables/bindings.d.ts +2 -1
- package/dist/variables/bindings.d.ts.map +1 -1
- package/dist/variables/bindings.js +6 -2
- package/dist/variables/bindings.js.map +1 -1
- package/dist/variables/index.d.ts +2 -1
- package/dist/variables/index.d.ts.map +1 -1
- package/dist/variables/index.js +23 -5
- package/dist/variables/index.js.map +1 -1
- package/dist/variables/units.d.ts +31 -0
- package/dist/variables/units.d.ts.map +1 -0
- package/dist/variables/units.js +79 -4
- package/dist/variables/units.js.map +1 -1
- package/package.json +1 -1
- package/src/checkpoint.ts +8 -2
- package/src/color/index.ts +3 -6
- package/src/color/management.ts +8 -2
- package/src/coordinate.ts +4 -1
- package/src/copy.ts +2 -0
- package/src/css/values.ts +3 -2
- package/src/geometry.ts +147 -24
- package/src/graph.ts +9 -3
- package/src/hit-test.ts +138 -16
- package/src/instances/index.ts +5 -1
- package/src/instances/sync.ts +5 -3
- package/src/resize.ts +40 -20
- package/src/scaling/node.ts +3 -1
- package/src/scaling/tree.ts +3 -2
- package/src/slots/authoring.ts +7 -7
- package/src/slots/content.ts +13 -19
- package/src/slots/frames.ts +3 -3
- package/src/snap.ts +14 -4
- package/src/types.ts +5 -0
- package/src/undo.ts +3 -2
- package/src/variables/bindings.ts +6 -2
- package/src/variables/index.ts +29 -6
- package/src/variables/units.ts +97 -4
package/src/coordinate.ts
CHANGED
|
@@ -6,9 +6,12 @@ import type { SceneNode } from './types'
|
|
|
6
6
|
|
|
7
7
|
export function getWorldMatrix(node: SceneNode, graph: Pick<SceneGraph, 'getNode'>): Mat3 {
|
|
8
8
|
const chain: SceneNode[] = []
|
|
9
|
+
const visited = new Set<string>()
|
|
9
10
|
let current: SceneNode | undefined = node
|
|
10
11
|
|
|
11
|
-
|
|
12
|
+
// A parent cycle in bad data must end the walk instead of hanging it.
|
|
13
|
+
while (current && !visited.has(current.id)) {
|
|
14
|
+
visited.add(current.id)
|
|
12
15
|
chain.unshift(current)
|
|
13
16
|
if (!current.parentId) break
|
|
14
17
|
current = graph.getNode(current.parentId)
|
package/src/copy.ts
CHANGED
|
@@ -81,6 +81,8 @@ export function copyStyleRun(r: StyleRun): StyleRun {
|
|
|
81
81
|
if (r.style.lineHeight) copy.style.lineHeight = { ...r.style.lineHeight }
|
|
82
82
|
if (r.style.letterSpacing) copy.style.letterSpacing = { ...r.style.letterSpacing }
|
|
83
83
|
if (r.style.boundVariables) copy.style.boundVariables = { ...r.style.boundVariables }
|
|
84
|
+
if (r.style.variableBindingScales)
|
|
85
|
+
copy.style.variableBindingScales = { ...r.style.variableBindingScales }
|
|
84
86
|
return copy
|
|
85
87
|
}
|
|
86
88
|
|
package/src/css/values.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import valueParser from 'postcss-value-parser'
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { parseDisplayableColor } from '../color'
|
|
4
4
|
import type { Color } from '../primitives'
|
|
5
5
|
|
|
6
6
|
/** Pixels in a rem, as browsers use by default. */
|
|
@@ -30,7 +30,8 @@ export function parseCSSNumber(value: string | undefined): number | null {
|
|
|
30
30
|
/** A CSS color that paints something; null for an invalid or fully transparent one. */
|
|
31
31
|
export function parseCSSColor(value: string | undefined): Color | null {
|
|
32
32
|
if (!value) return null
|
|
33
|
-
|
|
33
|
+
// CSS clamps `rgb()` channels and maps wide-gamut colors into sRGB, as a stored color must be.
|
|
34
|
+
const color = parseDisplayableColor(value.trim())
|
|
34
35
|
return color && color.a > 0 ? color : null
|
|
35
36
|
}
|
|
36
37
|
|
package/src/geometry.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { arrowCapOverflow } from './arrow-caps'
|
|
2
|
+
import { getAxisAlignedWorldBounds } from './coordinate'
|
|
3
|
+
import type { SceneGraph } from './graph'
|
|
2
4
|
import type { Rect, Vector } from './primitives'
|
|
3
5
|
import { textLinesOverflow } from './text/overflow'
|
|
4
|
-
import type { Effect, Stroke, StrokeCap, StrokeJoin, VectorNetwork } from './types'
|
|
6
|
+
import type { Effect, SceneNode, Stroke, StrokeCap, StrokeJoin, VectorNetwork } from './types'
|
|
5
7
|
|
|
6
8
|
export function degToRad(degrees: number): number {
|
|
7
9
|
return (degrees * Math.PI) / 180
|
|
@@ -241,15 +243,13 @@ export function effectOverflow(effects?: Effect[]) {
|
|
|
241
243
|
return { left, right, top, bottom }
|
|
242
244
|
}
|
|
243
245
|
|
|
246
|
+
/** Axis-aligned canvas box around the layers, through each one's rotation, flips, and ancestors. */
|
|
244
247
|
export function computeAbsoluteBounds(
|
|
245
|
-
nodes: Iterable<
|
|
246
|
-
|
|
248
|
+
nodes: Iterable<SceneNode>,
|
|
249
|
+
graph: Pick<SceneGraph, 'getNode'>
|
|
247
250
|
): Rect {
|
|
248
251
|
const bounds = createBoundsAccumulator()
|
|
249
|
-
for (const n of nodes)
|
|
250
|
-
const abs = getAbsolutePosition(n.id)
|
|
251
|
-
includeRect(bounds, { x: abs.x, y: abs.y, width: n.width, height: n.height })
|
|
252
|
-
}
|
|
252
|
+
for (const n of nodes) includeRect(bounds, getAxisAlignedWorldBounds(n, graph))
|
|
253
253
|
return boundsToRect(bounds)
|
|
254
254
|
}
|
|
255
255
|
|
|
@@ -260,6 +260,8 @@ export function computeVisualBounds(
|
|
|
260
260
|
width: number
|
|
261
261
|
height: number
|
|
262
262
|
rotation?: number
|
|
263
|
+
flipX?: boolean
|
|
264
|
+
flipY?: boolean
|
|
263
265
|
strokes?: Stroke[]
|
|
264
266
|
strokeCap?: StrokeCap
|
|
265
267
|
vectorNetwork?: VectorNetwork | null
|
|
@@ -271,7 +273,7 @@ export function computeVisualBounds(
|
|
|
271
273
|
const bounds = createBoundsAccumulator()
|
|
272
274
|
|
|
273
275
|
for (const n of nodes) {
|
|
274
|
-
const abs = getAbsolutePosition(n.id)
|
|
276
|
+
const abs = unturnedTopLeft(n, getAbsolutePosition(n.id))
|
|
275
277
|
const bbox = rotatedBBox(abs.x, abs.y, n.width, n.height, n.rotation ?? 0)
|
|
276
278
|
const stroke = strokeOverflow(n.strokes, n.strokeCap, n.vectorNetwork, n)
|
|
277
279
|
const effects = effectOverflow(n.effects)
|
|
@@ -365,18 +367,138 @@ export function geometryBlobBounds(paths: Array<{ commandsBlob: Uint8Array }>):
|
|
|
365
367
|
return bounds.minX === Infinity ? null : boundsToRect(bounds)
|
|
366
368
|
}
|
|
367
369
|
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
370
|
+
/** How many straight steps a curve is sampled in when its outline is needed as points. */
|
|
371
|
+
const OUTLINE_CURVE_STEPS = 8
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Points along a quad (`command` 3) or cubic (4) from `start`, sampled so the curve's outline can
|
|
375
|
+
* be tested without a path engine. The last coordinate pair is the curve's end.
|
|
376
|
+
*/
|
|
377
|
+
function curvePoints(
|
|
378
|
+
command: number,
|
|
379
|
+
start: Vector,
|
|
380
|
+
points: readonly Vector[],
|
|
381
|
+
steps: number
|
|
382
|
+
): Vector[] {
|
|
383
|
+
const end = points[points.length - 1]
|
|
384
|
+
const sampled: Vector[] = []
|
|
385
|
+
for (let step = 1; step <= steps; step++) {
|
|
386
|
+
const t = step / steps
|
|
387
|
+
const u = 1 - t
|
|
388
|
+
if (command === 3) {
|
|
389
|
+
const control = points[0]
|
|
390
|
+
sampled.push({
|
|
391
|
+
x: u * u * start.x + 2 * u * t * control.x + t * t * end.x,
|
|
392
|
+
y: u * u * start.y + 2 * u * t * control.y + t * t * end.y
|
|
393
|
+
})
|
|
394
|
+
continue
|
|
395
|
+
}
|
|
396
|
+
const [first, second] = points
|
|
397
|
+
sampled.push({
|
|
398
|
+
x:
|
|
399
|
+
u * u * u * start.x +
|
|
400
|
+
3 * u * u * t * first.x +
|
|
401
|
+
3 * u * t * t * second.x +
|
|
402
|
+
t * t * t * end.x,
|
|
403
|
+
y:
|
|
404
|
+
u * u * u * start.y + 3 * u * u * t * first.y + 3 * u * t * t * second.y + t * t * t * end.y
|
|
405
|
+
})
|
|
406
|
+
}
|
|
407
|
+
return sampled
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/** One blob's subpaths as polylines in the layer's own coordinates. */
|
|
411
|
+
function blobPolylines(blob: Uint8Array, curveSteps: number): Vector[][] {
|
|
412
|
+
const polylines: Vector[][] = []
|
|
413
|
+
const view = new DataView(blob.buffer, blob.byteOffset, blob.byteLength)
|
|
414
|
+
let offset = 0
|
|
415
|
+
let current: Vector[] = []
|
|
416
|
+
let cursor: Vector = { x: 0, y: 0 }
|
|
417
|
+
const readPoint = (): Vector | null => {
|
|
418
|
+
if (offset + 8 > blob.length) return null
|
|
419
|
+
const point = { x: view.getFloat32(offset, true), y: view.getFloat32(offset + 4, true) }
|
|
420
|
+
offset += 8
|
|
421
|
+
return point
|
|
422
|
+
}
|
|
423
|
+
while (offset < blob.length) {
|
|
424
|
+
const command = blob[offset++]
|
|
425
|
+
const coords = geometryCommandCoordCount(command)
|
|
426
|
+
if (coords === null) break
|
|
427
|
+
if (command === 0) {
|
|
428
|
+
if (current.length > 0) polylines.push(current)
|
|
429
|
+
current = []
|
|
430
|
+
continue
|
|
431
|
+
}
|
|
432
|
+
const points: Vector[] = []
|
|
433
|
+
let complete = true
|
|
434
|
+
for (let index = 0; index < Math.max(coords, 1); index++) {
|
|
435
|
+
const point = readPoint()
|
|
436
|
+
if (!point) {
|
|
437
|
+
complete = false
|
|
438
|
+
break
|
|
439
|
+
}
|
|
440
|
+
points.push(point)
|
|
441
|
+
}
|
|
442
|
+
if (!complete) break
|
|
443
|
+
const end = points[points.length - 1]
|
|
444
|
+
if (command === 1) {
|
|
445
|
+
if (current.length > 0) polylines.push(current)
|
|
446
|
+
current = [end]
|
|
447
|
+
} else if (command === 2) {
|
|
448
|
+
if (current.length === 0) current.push(cursor)
|
|
449
|
+
current.push(end)
|
|
450
|
+
} else {
|
|
451
|
+
if (current.length === 0) current.push(cursor)
|
|
452
|
+
current.push(...curvePoints(command, cursor, points, curveSteps))
|
|
453
|
+
}
|
|
454
|
+
cursor = end
|
|
455
|
+
}
|
|
456
|
+
if (current.length > 0) polylines.push(current)
|
|
457
|
+
return polylines
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* The subpaths a layer's geometry blobs draw, as polylines in its own coordinates. Curves are
|
|
462
|
+
* sampled, so a caller can test a point against the outline without a path engine.
|
|
463
|
+
*/
|
|
464
|
+
export function geometryBlobPolylines(
|
|
465
|
+
paths: Array<{ commandsBlob: Uint8Array }>,
|
|
466
|
+
curveSteps = OUTLINE_CURVE_STEPS
|
|
467
|
+
): Vector[][] {
|
|
468
|
+
const polylines: Vector[][] = []
|
|
469
|
+
for (const path of paths) polylines.push(...blobPolylines(path.commandsBlob, curveSteps))
|
|
470
|
+
return polylines
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
type TurnedBox = Pick<VisualBoundsNode, 'width' | 'height' | 'rotation' | 'flipX' | 'flipY'>
|
|
474
|
+
|
|
475
|
+
/**
|
|
476
|
+
* Where a local point lands relative to the box's unturned top-left, turned about the center and
|
|
477
|
+
* then flipped about it, in the order `getNodeLocalMatrix` composes the same two steps.
|
|
478
|
+
*/
|
|
479
|
+
function transformLocalPoint(node: TurnedBox, point: Vector): Vector {
|
|
480
|
+
let { x, y } = point
|
|
371
481
|
const rotation = node.rotation ?? 0
|
|
372
482
|
if (rotation !== 0) {
|
|
373
483
|
const rotated = rotatePoint(x, y, node.width / 2, node.height / 2, degToRad(rotation))
|
|
374
484
|
x = rotated.x
|
|
375
485
|
y = rotated.y
|
|
376
486
|
}
|
|
487
|
+
if (node.flipX) x = node.width - x
|
|
488
|
+
if (node.flipY) y = node.height - y
|
|
377
489
|
return { x, y }
|
|
378
490
|
}
|
|
379
491
|
|
|
492
|
+
/**
|
|
493
|
+
* The unturned top-left of a box whose origin (its own 0,0, as `SceneGraph.getAbsolutePosition`
|
|
494
|
+
* returns it) sits at `origin` after the rotation.
|
|
495
|
+
*/
|
|
496
|
+
function unturnedTopLeft(node: TurnedBox, origin: Vector): Vector {
|
|
497
|
+
// `getAbsolutePosition` maps the box's own origin through the world matrix, flips included.
|
|
498
|
+
const offset = transformLocalPoint(node, { x: 0, y: 0 })
|
|
499
|
+
return { x: origin.x - offset.x, y: origin.y - offset.y }
|
|
500
|
+
}
|
|
501
|
+
|
|
380
502
|
function transformedLocalBounds(node: VisualBoundsNode, local: Rect, abs: Vector): VisualBounds {
|
|
381
503
|
const points = [
|
|
382
504
|
{ x: local.x, y: local.y },
|
|
@@ -397,7 +519,7 @@ export function nodeVisualBounds(
|
|
|
397
519
|
node: VisualBoundsNode,
|
|
398
520
|
getAbsolutePosition: (id: string) => Vector
|
|
399
521
|
): VisualBounds {
|
|
400
|
-
const abs = getAbsolutePosition(node.id)
|
|
522
|
+
const abs = unturnedTopLeft(node, getAbsolutePosition(node.id))
|
|
401
523
|
const base = computeVisualBounds([node], getAbsolutePosition)
|
|
402
524
|
let bounds: VisualBounds = {
|
|
403
525
|
minX: base.x,
|
|
@@ -417,14 +539,14 @@ export function nodeVisualBounds(
|
|
|
417
539
|
bounds = unionVisualBounds(bounds, transformedLocalBounds(node, localGeometry, abs)) ?? bounds
|
|
418
540
|
}
|
|
419
541
|
|
|
420
|
-
const linesOverflow = textLinesOverflow(node)
|
|
421
|
-
|
|
422
|
-
|
|
542
|
+
const linesOverflow = Math.max(textLinesOverflow(node), 0)
|
|
543
|
+
// An underline or strikethrough reaches below the last line, turning with the text.
|
|
544
|
+
const below = linesOverflow + textDecorationOverflow(node)
|
|
545
|
+
if (below > 0) {
|
|
546
|
+
const lines = { x: 0, y: 0, width: node.width, height: node.height + below }
|
|
423
547
|
bounds = unionVisualBounds(bounds, transformedLocalBounds(node, lines, abs)) ?? bounds
|
|
424
548
|
}
|
|
425
549
|
|
|
426
|
-
bounds.maxY += textDecorationOverflow(node)
|
|
427
|
-
|
|
428
550
|
return bounds
|
|
429
551
|
}
|
|
430
552
|
|
|
@@ -453,13 +575,14 @@ function collectDescendantVisualBounds(
|
|
|
453
575
|
node.type === 'FRAME' || node.type === 'COMPONENT' || node.type === 'INSTANCE'
|
|
454
576
|
let childClip = clip
|
|
455
577
|
if (isClippableContainer && node.clipsContent) {
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
578
|
+
// The frame's own drawn box, so a turned frame clips where it is drawn. For an unturned,
|
|
579
|
+
// unflipped frame this is the same box as its position and size give.
|
|
580
|
+
const abs = unturnedTopLeft(node, getAbsolutePosition(node.id))
|
|
581
|
+
const nodeClip = transformedLocalBounds(
|
|
582
|
+
node,
|
|
583
|
+
{ x: 0, y: 0, width: node.width, height: node.height },
|
|
584
|
+
abs
|
|
585
|
+
)
|
|
463
586
|
childClip = childClip ? intersectVisualBounds(childClip, nodeClip) : nodeClip
|
|
464
587
|
if (!childClip) return bounds
|
|
465
588
|
}
|
package/src/graph.ts
CHANGED
|
@@ -425,10 +425,11 @@ export class SceneGraph {
|
|
|
425
425
|
}
|
|
426
426
|
|
|
427
427
|
createNode(type: NodeType, parentId: string, overrides: Partial<SceneNode> = {}): SceneNode {
|
|
428
|
-
const node = createDefaultNode(() => this.generateEntityId(), type, overrides)
|
|
429
428
|
const parent = this.nodes.get(parentId)
|
|
429
|
+
if (!parent) throw new Error(`Parent "${parentId}" does not exist`)
|
|
430
|
+
const node = createDefaultNode(() => this.generateEntityId(), type, overrides)
|
|
430
431
|
this.willChangeNode(parent)
|
|
431
|
-
parent
|
|
432
|
+
parent.childIds.push(node.id)
|
|
432
433
|
return this.registerNode(node, parentId)
|
|
433
434
|
}
|
|
434
435
|
createNodeWithId(
|
|
@@ -437,10 +438,15 @@ export class SceneGraph {
|
|
|
437
438
|
parentId: string | null,
|
|
438
439
|
overrides: Partial<SceneNode> = {}
|
|
439
440
|
): SceneNode {
|
|
441
|
+
// A caller restoring or importing a layer must know it brings a new one: replacing a layer
|
|
442
|
+
// that already has the ID would drop it, and its children, from the document.
|
|
443
|
+
if (this.nodes.has(id)) throw new Error(`Node "${id}" already exists`)
|
|
444
|
+
if (parentId === id) throw new Error(`Node "${id}" cannot be its own parent`)
|
|
445
|
+
const parent = parentId === null ? undefined : this.nodes.get(parentId)
|
|
446
|
+
if (parentId !== null && !parent) throw new Error(`Parent "${parentId}" does not exist`)
|
|
440
447
|
const node = createDefaultNode(() => id, type, overrides)
|
|
441
448
|
// `overrides` may be another node's snapshot, ID included.
|
|
442
449
|
node.id = id
|
|
443
|
-
const parent = parentId ? this.nodes.get(parentId) : undefined
|
|
444
450
|
if (parent && !parent.childIds.includes(id)) {
|
|
445
451
|
this.willChangeNode(parent)
|
|
446
452
|
parent.childIds.push(id)
|
package/src/hit-test.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { hitsConnectorLine } from './connectors/hit'
|
|
2
2
|
import { getAxisAlignedBoundsAt, getNodeLocalMatrix, getWorldMatrix } from './coordinate'
|
|
3
|
+
import { geometryBlobPolylines } from './geometry'
|
|
3
4
|
import type { SceneGraph } from './graph'
|
|
4
5
|
import Matrix, { type Mat3 } from './matrix'
|
|
5
|
-
import type { Rect } from './primitives'
|
|
6
|
+
import type { Rect, Vector } from './primitives'
|
|
6
7
|
import type { SceneNode, NodeType } from './types'
|
|
7
8
|
|
|
8
9
|
const CONTAINER_TYPES = new Set<NodeType>([
|
|
@@ -15,6 +16,11 @@ const CONTAINER_TYPES = new Set<NodeType>([
|
|
|
15
16
|
'INSTANCE'
|
|
16
17
|
])
|
|
17
18
|
const OPAQUE_CONTAINER_TYPES = new Set<NodeType>(['COMPONENT', 'INSTANCE'])
|
|
19
|
+
/**
|
|
20
|
+
* How far from a zero-width or zero-height layer, in canvas units, a point still lands on it, so
|
|
21
|
+
* a flat line or an empty box stays clickable; matches a connector line's slop (`connectors/hit.ts`).
|
|
22
|
+
*/
|
|
23
|
+
const DEGENERATE_AXIS_SLOP = 4
|
|
18
24
|
|
|
19
25
|
function hasVisibleFillOrStroke(node: SceneNode): boolean {
|
|
20
26
|
return node.fills.some((f) => f.visible) || node.strokes.some((s) => s.visible)
|
|
@@ -52,6 +58,27 @@ function hasTransformedAncestor(
|
|
|
52
58
|
return transformed
|
|
53
59
|
}
|
|
54
60
|
|
|
61
|
+
/**
|
|
62
|
+
* Where the point lies in the node's own coordinates: its offset from the position when nothing
|
|
63
|
+
* above it is turned, else through the inverse world matrix. Null when that cannot be inverted.
|
|
64
|
+
*/
|
|
65
|
+
function localPoint(
|
|
66
|
+
px: number,
|
|
67
|
+
py: number,
|
|
68
|
+
node: SceneNode,
|
|
69
|
+
graph: SceneGraph,
|
|
70
|
+
transformCache: Map<string, boolean>
|
|
71
|
+
): Vector | null {
|
|
72
|
+
if (!hasTransformedAncestor(node, graph, transformCache)) {
|
|
73
|
+
const absolute = graph.getAbsolutePosition(node.id)
|
|
74
|
+
return { x: px - absolute.x, y: py - absolute.y }
|
|
75
|
+
}
|
|
76
|
+
const inv = Matrix.invert(getWorldMatrix(node, graph))
|
|
77
|
+
if (!inv) return null
|
|
78
|
+
const [x, y] = Matrix.mapPoints(inv, [px, py])
|
|
79
|
+
return { x, y }
|
|
80
|
+
}
|
|
81
|
+
|
|
55
82
|
/** Whether the point lies in the node's box, grown by `pad` canvas units on every side. */
|
|
56
83
|
function containsPoint(
|
|
57
84
|
px: number,
|
|
@@ -61,27 +88,105 @@ function containsPoint(
|
|
|
61
88
|
transformCache: Map<string, boolean>,
|
|
62
89
|
pad = 0
|
|
63
90
|
): boolean {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
91
|
+
const local = localPoint(px, py, node, graph, transformCache)
|
|
92
|
+
if (!local) return false
|
|
93
|
+
const padX = node.width > 0 ? pad : Math.max(pad, DEGENERATE_AXIS_SLOP)
|
|
94
|
+
const padY = node.height > 0 ? pad : Math.max(pad, DEGENERATE_AXIS_SLOP)
|
|
95
|
+
return (
|
|
96
|
+
local.x >= -padX &&
|
|
97
|
+
local.x <= node.width + padX &&
|
|
98
|
+
local.y >= -padY &&
|
|
99
|
+
local.y <= node.height + padY
|
|
100
|
+
)
|
|
101
|
+
}
|
|
73
102
|
|
|
74
|
-
|
|
103
|
+
/** Whether a rounded rectangle of `width` × `height` holds the point. */
|
|
104
|
+
function roundedRectContains(
|
|
105
|
+
point: Vector,
|
|
106
|
+
width: number,
|
|
107
|
+
height: number,
|
|
108
|
+
radius: number
|
|
109
|
+
): boolean {
|
|
110
|
+
if (point.x < 0 || point.y < 0 || point.x > width || point.y > height) return false
|
|
111
|
+
const r = Math.max(0, Math.min(radius, width / 2, height / 2))
|
|
112
|
+
const cx = Math.min(Math.max(point.x, r), width - r)
|
|
113
|
+
const cy = Math.min(Math.max(point.y, r), height - r)
|
|
114
|
+
return Math.hypot(point.x - cx, point.y - cy) <= r
|
|
115
|
+
}
|
|
75
116
|
|
|
76
|
-
|
|
77
|
-
|
|
117
|
+
/** Whether the point is inside a closed polyline, by the even-odd rule. */
|
|
118
|
+
function insidePolyline(polyline: readonly Vector[], x: number, y: number): boolean {
|
|
119
|
+
let inside = false
|
|
120
|
+
for (let i = 0, j = polyline.length - 1; i < polyline.length; j = i++) {
|
|
121
|
+
const a = polyline[i]
|
|
122
|
+
const b = polyline[j]
|
|
123
|
+
if (a.y > y === b.y > y) continue
|
|
124
|
+
if (x < ((b.x - a.x) * (y - a.y)) / (b.y - a.y) + a.x) inside = !inside
|
|
125
|
+
}
|
|
126
|
+
return inside
|
|
127
|
+
}
|
|
78
128
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
129
|
+
/**
|
|
130
|
+
* Whether a mask lets the point through, by the outline it draws: an ellipse's curve, a rounded
|
|
131
|
+
* rectangle's corners, or a vector's filled path. Other shapes clip by their box.
|
|
132
|
+
*/
|
|
133
|
+
function maskContainsPoint(
|
|
134
|
+
px: number,
|
|
135
|
+
py: number,
|
|
136
|
+
mask: SceneNode,
|
|
137
|
+
graph: SceneGraph,
|
|
138
|
+
transformCache: Map<string, boolean>
|
|
139
|
+
): boolean {
|
|
140
|
+
const outlined =
|
|
141
|
+
(mask.type === 'ELLIPSE' && mask.width > 0 && mask.height > 0) ||
|
|
142
|
+
(mask.type === 'RECTANGLE' && mask.cornerRadius > 0) ||
|
|
143
|
+
(mask.type === 'VECTOR' && mask.fillGeometry.length > 0)
|
|
144
|
+
if (!outlined) return containsPoint(px, py, mask, graph, transformCache)
|
|
145
|
+
const local = localPoint(px, py, mask, graph, transformCache)
|
|
146
|
+
if (!local) return false
|
|
147
|
+
if (mask.type === 'ELLIPSE') {
|
|
148
|
+
const rx = mask.width / 2
|
|
149
|
+
const ry = mask.height / 2
|
|
150
|
+
const dx = (local.x - rx) / rx
|
|
151
|
+
const dy = (local.y - ry) / ry
|
|
152
|
+
return dx * dx + dy * dy <= 1
|
|
153
|
+
}
|
|
154
|
+
if (mask.type === 'RECTANGLE') {
|
|
155
|
+
return roundedRectContains(local, mask.width, mask.height, mask.cornerRadius)
|
|
156
|
+
}
|
|
157
|
+
return geometryBlobPolylines(mask.fillGeometry).some((points) =>
|
|
158
|
+
insidePolyline(points, local.x, local.y)
|
|
82
159
|
)
|
|
83
160
|
}
|
|
84
161
|
|
|
162
|
+
function isVisibleMask(node: SceneNode | undefined): node is SceneNode {
|
|
163
|
+
return node !== undefined && node.visible && node.isMask
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* The masks that clip each child, by index, as the renderer groups them (`core/canvas/masks.ts`):
|
|
168
|
+
* a run of visible masks clips the layers after it up to the next visible mask. Empty when the
|
|
169
|
+
* container has no visible mask.
|
|
170
|
+
*/
|
|
171
|
+
function clippingMasks(graph: SceneGraph, childIds: readonly string[]): SceneNode[][] {
|
|
172
|
+
const children = childIds.map((childId) => graph.nodes.get(childId))
|
|
173
|
+
if (!children.some(isVisibleMask)) return []
|
|
174
|
+
const result: SceneNode[][] = []
|
|
175
|
+
let run: SceneNode[] = []
|
|
176
|
+
let previousWasMask = false
|
|
177
|
+
for (const child of children) {
|
|
178
|
+
if (isVisibleMask(child)) {
|
|
179
|
+
run = previousWasMask ? [...run, child] : [child]
|
|
180
|
+
previousWasMask = true
|
|
181
|
+
result.push([])
|
|
182
|
+
continue
|
|
183
|
+
}
|
|
184
|
+
previousWasMask = false
|
|
185
|
+
result.push(run)
|
|
186
|
+
}
|
|
187
|
+
return result
|
|
188
|
+
}
|
|
189
|
+
|
|
85
190
|
function hitTestOpaqueContainer(
|
|
86
191
|
graph: SceneGraph,
|
|
87
192
|
px: number,
|
|
@@ -130,6 +235,21 @@ function hitTestTransparentContainer(
|
|
|
130
235
|
return null
|
|
131
236
|
}
|
|
132
237
|
|
|
238
|
+
/** Whether the masks that clip a layer leave the point outside every one of their outlines. */
|
|
239
|
+
function isMaskedOut(
|
|
240
|
+
px: number,
|
|
241
|
+
py: number,
|
|
242
|
+
masks: readonly SceneNode[] | undefined,
|
|
243
|
+
graph: SceneGraph,
|
|
244
|
+
transformCache: Map<string, boolean>
|
|
245
|
+
): boolean {
|
|
246
|
+
if (!masks) return false
|
|
247
|
+
return (
|
|
248
|
+
masks.length > 0 &&
|
|
249
|
+
!masks.some((mask) => maskContainsPoint(px, py, mask, graph, transformCache))
|
|
250
|
+
)
|
|
251
|
+
}
|
|
252
|
+
|
|
133
253
|
/**
|
|
134
254
|
* The topmost layer under the point among a container's children. `pad`, in canvas units, grows
|
|
135
255
|
* each leaf layer's box, as Figma takes a click a few screen pixels outside a shape.
|
|
@@ -150,10 +270,12 @@ function hitTestChildren(
|
|
|
150
270
|
if (!containsPoint(px, py, parent, graph, transformCache)) return null
|
|
151
271
|
}
|
|
152
272
|
|
|
273
|
+
const masks = clippingMasks(graph, parent.childIds)
|
|
153
274
|
for (let i = parent.childIds.length - 1; i >= 0; i--) {
|
|
154
275
|
const childId = parent.childIds[i]
|
|
155
276
|
const child = graph.nodes.get(childId)
|
|
156
277
|
if (!child || child.internalOnly || !child.visible) continue
|
|
278
|
+
if (isMaskedOut(px, py, masks[i], graph, transformCache)) continue
|
|
157
279
|
if (CONTAINER_TYPES.has(child.type)) {
|
|
158
280
|
if (OPAQUE_CONTAINER_TYPES.has(child.type) && !deep) {
|
|
159
281
|
const hit = hitTestOpaqueContainer(graph, px, py, child, childId, deep, transformCache, pad)
|
package/src/instances/index.ts
CHANGED
|
@@ -153,7 +153,11 @@ export function swapInstanceComponent(
|
|
|
153
153
|
|
|
154
154
|
const childIds = Array.from(instance.childIds)
|
|
155
155
|
const slotContent = detachOwnedSlotContent(graph, instance)
|
|
156
|
-
for (const childId of childIds)
|
|
156
|
+
for (const childId of childIds) {
|
|
157
|
+
// The clones take new IDs, so what was recorded for the removed layers would never apply.
|
|
158
|
+
forgetOverridesOf(graph, childId)
|
|
159
|
+
graph.deleteNode(childId)
|
|
160
|
+
}
|
|
157
161
|
graph.updateNode(instanceId, updates)
|
|
158
162
|
cloneChildrenWithMapping(graph, componentId, instanceId)
|
|
159
163
|
restoreOwnedSlotContent(graph, instance, slotContent)
|
package/src/instances/sync.ts
CHANGED
|
@@ -141,10 +141,11 @@ export function sourceInTargetCoordinates(source: SceneNode, targetScale: number
|
|
|
141
141
|
const factor = targetScale / source.componentScale
|
|
142
142
|
if (!Number.isFinite(factor) || factor <= 0) throw new Error('Invalid component coordinate scale')
|
|
143
143
|
if (factor === 1) return source
|
|
144
|
+
const scaled = scaleNodeChanges(source, factor, true)
|
|
144
145
|
return {
|
|
145
146
|
...source,
|
|
146
|
-
...
|
|
147
|
-
...scaleVariableBindingUnits(source, factor)
|
|
147
|
+
...scaled,
|
|
148
|
+
...scaleVariableBindingUnits(source, factor, scaled.styleRuns)
|
|
148
149
|
}
|
|
149
150
|
}
|
|
150
151
|
|
|
@@ -396,7 +397,8 @@ function matchLinkedChildren(
|
|
|
396
397
|
if (!child || replacements.has(child.id)) continue
|
|
397
398
|
const mappedComponentId = copiedComponentLayerId(overrides, instParentId, child)
|
|
398
399
|
if (mappedComponentId && compChildIdSet.has(mappedComponentId)) {
|
|
399
|
-
|
|
400
|
+
// The first copy of a layer keeps it; a duplicate stays unmatched for the sync to remove.
|
|
401
|
+
if (instChildMap.has(mappedComponentId)) continue
|
|
400
402
|
instChildMap.set(mappedComponentId, child)
|
|
401
403
|
usedInstChildIds.add(child.id)
|
|
402
404
|
}
|
package/src/resize.ts
CHANGED
|
@@ -314,33 +314,53 @@ export function scaledGeometryChanges(
|
|
|
314
314
|
if (orig.strokeGeometry.length > 0) {
|
|
315
315
|
changes.strokeGeometry = outlineOnly ? scaleGeometryPaths(orig.strokeGeometry, sx, sy) : []
|
|
316
316
|
}
|
|
317
|
-
|
|
318
|
-
// other text reflows in the new box, so its glyphs are dropped.
|
|
319
|
-
const glyphs = orig.derivedTextGlyphs ?? []
|
|
320
|
-
const pathText =
|
|
321
|
-
!!orig.textPathData || !!orig.textPathBox || glyphs.some((glyph) => (glyph.rotation ?? 0) !== 0)
|
|
322
|
-
if (glyphs.length > 0 && pathText) {
|
|
323
|
-
changes.derivedTextGlyphs = scaleDerivedGlyphs(glyphs, sx, sy)
|
|
324
|
-
// Keep the layout/selection box in sync with the scaled glyphs. reflow
|
|
325
|
-
// overrides this when it applies; this covers the fallback where reflow
|
|
326
|
-
// returns null (no path data) so the box doesn't carry a stale size.
|
|
327
|
-
if (orig.textPathBox) {
|
|
328
|
-
changes.textPathBox = {
|
|
329
|
-
x: orig.textPathBox.x * sx,
|
|
330
|
-
y: orig.textPathBox.y * sy,
|
|
331
|
-
width: orig.textPathBox.width * sx,
|
|
332
|
-
height: orig.textPathBox.height * sy
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
}
|
|
317
|
+
Object.assign(changes, scaledPathTextChanges(orig, sx, sy))
|
|
336
318
|
// Glyphs scaled as a picture take their strokes along; any other stroke keeps its weight, as
|
|
337
319
|
// Figma reports it, even where only a scaled outline can draw it.
|
|
338
|
-
if (orig.strokes.length > 0 &&
|
|
320
|
+
if (orig.strokes.length > 0 && isScaledPathText(orig)) {
|
|
339
321
|
changes.strokes = scaleStrokes(orig.strokes, sx, sy)
|
|
340
322
|
}
|
|
341
323
|
return changes
|
|
342
324
|
}
|
|
343
325
|
|
|
326
|
+
type PathTextFields = Pick<SceneNode, 'derivedTextGlyphs' | 'textPathData' | 'textPathBox'>
|
|
327
|
+
|
|
328
|
+
// Path text scales with its path, including baked path text that kept only rotated glyphs;
|
|
329
|
+
// other text reflows in the new box, so its glyphs are dropped.
|
|
330
|
+
function isScaledPathText(orig: PathTextFields): boolean {
|
|
331
|
+
const glyphs = orig.derivedTextGlyphs ?? []
|
|
332
|
+
return (
|
|
333
|
+
glyphs.length > 0 &&
|
|
334
|
+
(!!orig.textPathData ||
|
|
335
|
+
!!orig.textPathBox ||
|
|
336
|
+
glyphs.some((glyph) => (glyph.rotation ?? 0) !== 0))
|
|
337
|
+
)
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/** The baked glyphs and layout box of path text, scaled with the layer; nothing for other text. */
|
|
341
|
+
export function scaledPathTextChanges(
|
|
342
|
+
orig: PathTextFields,
|
|
343
|
+
sx: number,
|
|
344
|
+
sy: number
|
|
345
|
+
): Pick<Partial<SceneNode>, 'derivedTextGlyphs' | 'textPathBox'> {
|
|
346
|
+
if (!isScaledPathText(orig)) return {}
|
|
347
|
+
const changes: Pick<Partial<SceneNode>, 'derivedTextGlyphs' | 'textPathBox'> = {
|
|
348
|
+
derivedTextGlyphs: scaleDerivedGlyphs(orig.derivedTextGlyphs, sx, sy)
|
|
349
|
+
}
|
|
350
|
+
// Keep the layout/selection box in sync with the scaled glyphs. reflow
|
|
351
|
+
// overrides this when it applies; this covers the fallback where reflow
|
|
352
|
+
// returns null (no path data) so the box doesn't carry a stale size.
|
|
353
|
+
if (orig.textPathBox) {
|
|
354
|
+
changes.textPathBox = {
|
|
355
|
+
x: orig.textPathBox.x * sx,
|
|
356
|
+
y: orig.textPathBox.y * sy,
|
|
357
|
+
width: orig.textPathBox.width * sx,
|
|
358
|
+
height: orig.textPathBox.height * sy
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
return changes
|
|
362
|
+
}
|
|
363
|
+
|
|
344
364
|
export function collectResizeDescendants(
|
|
345
365
|
graph: ResizeGraph,
|
|
346
366
|
rootId: string
|
package/src/scaling/node.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { scaleGeometryPaths } from '../copy'
|
|
2
|
+
import { scaledPathTextChanges } from '../resize'
|
|
2
3
|
import { scaledTextLength } from '../text/lengths'
|
|
3
4
|
import { TEXT_PARAGRAPH_SPACING_FIELDS } from '../text/paragraphs'
|
|
4
5
|
import {
|
|
@@ -163,6 +164,7 @@ export function scaleNodeChanges(
|
|
|
163
164
|
layoutGrids: scaledLayoutGrids(node.layoutGrids, scale),
|
|
164
165
|
vectorNetwork: scaledVectorNetwork(node, scale),
|
|
165
166
|
fillGeometry: scaleGeometryPaths(node.fillGeometry, scale, scale),
|
|
166
|
-
strokeGeometry: scaleGeometryPaths(node.strokeGeometry, scale, scale)
|
|
167
|
+
strokeGeometry: scaleGeometryPaths(node.strokeGeometry, scale, scale),
|
|
168
|
+
...scaledPathTextChanges(node, scale, scale)
|
|
167
169
|
}
|
|
168
170
|
}
|
package/src/scaling/tree.ts
CHANGED
|
@@ -56,9 +56,10 @@ export function rescaleNodeTreeChanges(
|
|
|
56
56
|
): Map<string, Partial<SceneNode>> {
|
|
57
57
|
const changes = new Map<string, Partial<SceneNode>>()
|
|
58
58
|
for (const node of nodes) {
|
|
59
|
+
const scaled = scaleNodeChanges(node, scale, node.id !== rootId)
|
|
59
60
|
changes.set(node.id, {
|
|
60
|
-
...
|
|
61
|
-
...scaleVariableBindingUnits(node, scale),
|
|
61
|
+
...scaled,
|
|
62
|
+
...scaleVariableBindingUnits(node, scale, scaled.styleRuns),
|
|
62
63
|
componentScale: findInstanceAncestor(graph, node.id)
|
|
63
64
|
? node.componentScale * scale
|
|
64
65
|
: node.componentScale
|