@open-pencil/scene-graph 0.17.0-beta.4 → 0.17.0-beta.6
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/buffered-events.js +1 -1
- package/dist/buffered-events.js.map +1 -1
- package/dist/clipping.d.ts +7 -1
- package/dist/clipping.d.ts.map +1 -1
- package/dist/clipping.js +23 -19
- package/dist/clipping.js.map +1 -1
- package/dist/comments/schema.d.ts.map +1 -1
- package/dist/comments/schema.js +1 -0
- package/dist/comments/schema.js.map +1 -1
- package/dist/comments/threads.d.ts +2 -0
- package/dist/comments/threads.d.ts.map +1 -1
- package/dist/comments/threads.js +11 -5
- package/dist/comments/threads.js.map +1 -1
- package/dist/components/index.d.ts +2 -2
- package/dist/components/index.js +2 -2
- package/dist/components/properties.d.ts +1 -3
- package/dist/components/properties.d.ts.map +1 -1
- package/dist/components/properties.js +3 -10
- package/dist/components/properties.js.map +1 -1
- package/dist/constants.d.ts +12 -2
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +42 -1
- package/dist/constants.js.map +1 -1
- package/dist/copy.d.ts.map +1 -1
- package/dist/copy.js +2 -0
- package/dist/copy.js.map +1 -1
- package/dist/corners.d.ts +22 -0
- package/dist/corners.d.ts.map +1 -0
- package/dist/corners.js +52 -0
- package/dist/corners.js.map +1 -0
- package/dist/css/grid.d.ts +4 -1
- package/dist/css/grid.d.ts.map +1 -1
- package/dist/css/grid.js +40 -7
- package/dist/css/grid.js.map +1 -1
- package/dist/css/index.d.ts +3 -3
- package/dist/css/index.js +3 -3
- package/dist/css/layout.d.ts +13 -1
- package/dist/css/layout.d.ts.map +1 -1
- package/dist/css/layout.js +26 -1
- package/dist/css/layout.js.map +1 -1
- package/dist/css/values.d.ts +23 -1
- package/dist/css/values.d.ts.map +1 -1
- package/dist/css/values.js +39 -1
- package/dist/css/values.js.map +1 -1
- package/dist/font-style.d.ts +2 -1
- package/dist/font-style.d.ts.map +1 -1
- package/dist/font-style.js +31 -6
- package/dist/font-style.js.map +1 -1
- package/dist/geometry.d.ts +68 -3
- package/dist/geometry.d.ts.map +1 -1
- package/dist/geometry.js +159 -25
- package/dist/geometry.js.map +1 -1
- package/dist/gradient.d.ts +9 -2
- package/dist/gradient.d.ts.map +1 -1
- package/dist/gradient.js +29 -1
- package/dist/gradient.js.map +1 -1
- package/dist/graph.d.ts +9 -2
- package/dist/graph.d.ts.map +1 -1
- package/dist/graph.js +46 -10
- package/dist/graph.js.map +1 -1
- package/dist/group-bounds.d.ts.map +1 -1
- package/dist/group-bounds.js +4 -3
- package/dist/group-bounds.js.map +1 -1
- package/dist/guides.d.ts +11 -2
- package/dist/guides.d.ts.map +1 -1
- package/dist/guides.js +30 -1
- package/dist/guides.js.map +1 -1
- package/dist/hit-test.d.ts.map +1 -1
- package/dist/hit-test.js +57 -5
- package/dist/hit-test.js.map +1 -1
- package/dist/icons/fit.d.ts +17 -0
- package/dist/icons/fit.d.ts.map +1 -0
- package/dist/icons/fit.js +21 -0
- package/dist/icons/fit.js.map +1 -0
- package/dist/icons/glyph.d.ts.map +1 -1
- package/dist/icons/glyph.js +5 -4
- package/dist/icons/glyph.js.map +1 -1
- package/dist/icons/index.d.ts +2 -1
- package/dist/icons/index.js +2 -1
- package/dist/ids.js +1 -1
- package/dist/ids.js.map +1 -1
- package/dist/images.d.ts +14 -4
- package/dist/images.d.ts.map +1 -1
- package/dist/images.js +45 -5
- package/dist/images.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/instances/assignments.js +39 -6
- package/dist/instances/assignments.js.map +1 -1
- package/dist/instances/index.d.ts +6 -1
- package/dist/instances/index.d.ts.map +1 -1
- package/dist/instances/index.js +58 -9
- package/dist/instances/index.js.map +1 -1
- package/dist/instances/kept-overrides.js +2 -2
- package/dist/instances/kept-overrides.js.map +1 -1
- package/dist/instances/matching.js +68 -0
- package/dist/instances/matching.js.map +1 -0
- package/dist/instances/overrides.d.ts +4 -1
- package/dist/instances/overrides.d.ts.map +1 -1
- package/dist/instances/overrides.js +13 -1
- package/dist/instances/overrides.js.map +1 -1
- package/dist/instances/protection.js +67 -0
- package/dist/instances/protection.js.map +1 -0
- package/dist/instances/swap.js +7 -5
- package/dist/instances/swap.js.map +1 -1
- package/dist/instances/sync.js +34 -116
- package/dist/instances/sync.js.map +1 -1
- package/dist/layout/guides.d.ts +17 -1
- package/dist/layout/guides.d.ts.map +1 -1
- package/dist/layout/guides.js +31 -11
- package/dist/layout/guides.js.map +1 -1
- package/dist/layout/index.d.ts +5 -2
- package/dist/layout/index.js +5 -2
- package/dist/layout/limits.d.ts +40 -0
- package/dist/layout/limits.d.ts.map +1 -0
- package/dist/layout/limits.js +54 -0
- package/dist/layout/limits.js.map +1 -0
- package/dist/layout/sizing.d.ts.map +1 -1
- package/dist/layout/sizing.js +0 -1
- package/dist/layout/sizing.js.map +1 -1
- package/dist/layout/spacing.d.ts +10 -0
- package/dist/layout/spacing.d.ts.map +1 -0
- package/dist/layout/spacing.js +12 -0
- package/dist/layout/spacing.js.map +1 -0
- package/dist/layout/strokes.d.ts +10 -0
- package/dist/layout/strokes.d.ts.map +1 -0
- package/dist/layout/strokes.js +30 -0
- package/dist/layout/strokes.js.map +1 -0
- package/dist/masks.d.ts +28 -0
- package/dist/masks.d.ts.map +1 -0
- package/dist/masks.js +53 -0
- package/dist/masks.js.map +1 -0
- package/dist/matrix.d.ts +2 -1
- package/dist/matrix.d.ts.map +1 -1
- package/dist/matrix.js +14 -1
- package/dist/matrix.js.map +1 -1
- package/dist/node-defaults.d.ts +9 -1
- package/dist/node-defaults.d.ts.map +1 -1
- package/dist/node-defaults.js +65 -38
- package/dist/node-defaults.js.map +1 -1
- package/dist/outline/flatten.d.ts.map +1 -1
- package/dist/outline/flatten.js +24 -17
- package/dist/outline/flatten.js.map +1 -1
- package/dist/outline/hit.d.ts +2 -8
- package/dist/outline/hit.d.ts.map +1 -1
- package/dist/outline/hit.js +16 -25
- package/dist/outline/hit.js.map +1 -1
- package/dist/outline/index.d.ts +3 -3
- package/dist/outline/index.js +3 -3
- package/dist/outline/shapes.d.ts +3 -1
- package/dist/outline/shapes.d.ts.map +1 -1
- package/dist/outline/shapes.js +18 -39
- package/dist/outline/shapes.js.map +1 -1
- package/dist/paints/index.d.ts +3 -1
- package/dist/paints/index.js +3 -1
- package/dist/paints/pattern.d.ts +13 -0
- package/dist/paints/pattern.d.ts.map +1 -0
- package/dist/paints/pattern.js +47 -0
- package/dist/paints/pattern.js.map +1 -0
- package/dist/paints/shows.d.ts +11 -0
- package/dist/paints/shows.d.ts.map +1 -0
- package/dist/paints/shows.js +16 -0
- package/dist/paints/shows.js.map +1 -0
- package/dist/parse-path.d.ts +4 -1
- package/dist/parse-path.d.ts.map +1 -1
- package/dist/parse-path.js +15 -1
- package/dist/parse-path.js.map +1 -1
- package/dist/paths/arc.d.ts +32 -0
- package/dist/paths/arc.d.ts.map +1 -0
- package/dist/paths/arc.js +49 -0
- package/dist/paths/arc.js.map +1 -0
- package/dist/paths/index.d.ts +5 -0
- package/dist/paths/index.js +5 -0
- package/dist/paths/network.d.ts +63 -0
- package/dist/paths/network.d.ts.map +1 -0
- package/dist/paths/network.js +315 -0
- package/dist/paths/network.js.map +1 -0
- package/dist/paths/sink.d.ts +20 -0
- package/dist/paths/sink.d.ts.map +1 -0
- package/dist/paths/sink.js +32 -0
- package/dist/paths/sink.js.map +1 -0
- package/dist/paths/smooth-corners.d.ts +25 -0
- package/dist/paths/smooth-corners.d.ts.map +1 -0
- package/dist/paths/smooth-corners.js +196 -0
- package/dist/paths/smooth-corners.js.map +1 -0
- package/dist/plugin-data/fields.d.ts +4 -0
- package/dist/plugin-data/fields.d.ts.map +1 -1
- package/dist/plugin-data/fields.js +2 -0
- package/dist/plugin-data/fields.js.map +1 -1
- package/dist/polygon.d.ts +1 -3
- package/dist/polygon.d.ts.map +1 -1
- package/dist/polygon.js +1 -16
- package/dist/polygon.js.map +1 -1
- package/dist/primitives/index.d.ts +1 -7
- package/dist/primitives/index.d.ts.map +1 -1
- package/dist/resize.d.ts.map +1 -1
- package/dist/resize.js +5 -5
- package/dist/resize.js.map +1 -1
- package/dist/scaling/node.d.ts.map +1 -1
- package/dist/scaling/node.js +25 -3
- package/dist/scaling/node.js.map +1 -1
- package/dist/shaders/model.d.ts.map +1 -1
- package/dist/shaders/model.js +1 -1
- package/dist/shaders/model.js.map +1 -1
- package/dist/shared-styles.d.ts.map +1 -1
- package/dist/shared-styles.js +2 -9
- package/dist/shared-styles.js.map +1 -1
- package/dist/strokes.d.ts +39 -0
- package/dist/strokes.d.ts.map +1 -0
- package/dist/strokes.js +104 -0
- package/dist/strokes.js.map +1 -0
- package/dist/text/auto-resize.d.ts +15 -0
- package/dist/text/auto-resize.d.ts.map +1 -0
- package/dist/text/auto-resize.js +23 -0
- package/dist/text/auto-resize.js.map +1 -0
- package/dist/text/index.d.ts +5 -3
- package/dist/text/index.js +5 -3
- package/dist/text/lengths.d.ts +6 -1
- package/dist/text/lengths.d.ts.map +1 -1
- package/dist/text/lengths.js +10 -1
- package/dist/text/lengths.js.map +1 -1
- package/dist/text/overflow.d.ts +14 -5
- package/dist/text/overflow.d.ts.map +1 -1
- package/dist/text/overflow.js +24 -6
- package/dist/text/overflow.js.map +1 -1
- package/dist/text/path.js +10 -0
- package/dist/text/path.js.map +1 -0
- package/dist/text/picture.js +5 -5
- package/dist/text/picture.js.map +1 -1
- package/dist/text/runs.d.ts +29 -0
- package/dist/text/runs.d.ts.map +1 -0
- package/dist/text/runs.js +72 -0
- package/dist/text/runs.js.map +1 -0
- package/dist/transfer/dependencies.d.ts.map +1 -1
- package/dist/transfer/dependencies.js +3 -2
- package/dist/transfer/dependencies.js.map +1 -1
- package/dist/transfer/nodes.js +2 -1
- package/dist/transfer/nodes.js.map +1 -1
- package/dist/types.d.ts +56 -10
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/variables/index.d.ts +4 -3
- package/dist/variables/index.d.ts.map +1 -1
- package/dist/variables/index.js +4 -3
- package/dist/variables/index.js.map +1 -1
- package/dist/variables/lookup.d.ts +20 -0
- package/dist/variables/lookup.d.ts.map +1 -0
- package/dist/variables/lookup.js +34 -0
- package/dist/variables/lookup.js.map +1 -0
- package/dist/variables/paints.d.ts +35 -4
- package/dist/variables/paints.d.ts.map +1 -1
- package/dist/variables/paints.js +67 -16
- package/dist/variables/paints.js.map +1 -1
- package/dist/variables/resolution.d.ts +3 -1
- package/dist/variables/resolution.d.ts.map +1 -1
- package/dist/variables/resolution.js +8 -3
- package/dist/variables/resolution.js.map +1 -1
- package/dist/variables/subtree.js +1 -1
- package/dist/variables/subtree.js.map +1 -1
- package/dist/vector-network.d.ts.map +1 -1
- package/dist/vector-network.js +13 -79
- package/dist/vector-network.js.map +1 -1
- package/package.json +28 -7
- package/src/buffered-events.ts +2 -1
- package/src/clipping.ts +11 -8
- package/src/comments/schema.ts +1 -0
- package/src/comments/threads.ts +13 -5
- package/src/components/properties.ts +3 -14
- package/src/constants.ts +25 -1
- package/src/copy.ts +2 -0
- package/src/corners.ts +93 -0
- package/src/css/grid.ts +49 -7
- package/src/css/index.ts +13 -2
- package/src/css/layout.ts +36 -0
- package/src/css/values.ts +49 -0
- package/src/font-style.ts +38 -4
- package/src/geometry.ts +223 -28
- package/src/gradient.ts +23 -1
- package/src/graph.ts +35 -10
- package/src/group-bounds.ts +7 -3
- package/src/guides.ts +26 -1
- package/src/hit-test.ts +68 -10
- package/src/icons/fit.ts +24 -0
- package/src/icons/glyph.ts +6 -4
- package/src/icons/index.ts +1 -0
- package/src/ids.ts +1 -1
- package/src/images.ts +67 -7
- package/src/instances/assignments.ts +65 -7
- package/src/instances/index.ts +77 -13
- package/src/instances/kept-overrides.ts +2 -5
- package/src/instances/matching.ts +101 -0
- package/src/instances/overrides.ts +18 -0
- package/src/instances/protection.ts +103 -0
- package/src/instances/swap.ts +12 -9
- package/src/instances/sync.ts +52 -184
- package/src/layout/guides.ts +42 -9
- package/src/layout/index.ts +3 -0
- package/src/layout/limits.ts +64 -0
- package/src/layout/sizing.ts +0 -2
- package/src/layout/spacing.ts +11 -0
- package/src/layout/strokes.ts +23 -0
- package/src/masks.ts +51 -0
- package/src/matrix.ts +15 -2
- package/src/node-defaults.ts +71 -36
- package/src/outline/flatten.ts +31 -18
- package/src/outline/hit.ts +16 -33
- package/src/outline/index.ts +2 -2
- package/src/outline/shapes.ts +19 -36
- package/src/paints/index.ts +2 -0
- package/src/paints/pattern.ts +45 -0
- package/src/paints/shows.ts +13 -0
- package/src/parse-path.ts +14 -0
- package/src/paths/arc.ts +59 -0
- package/src/paths/index.ts +4 -0
- package/src/paths/network.ts +411 -0
- package/src/paths/sink.ts +50 -0
- package/src/paths/smooth-corners.ts +357 -0
- package/src/plugin-data/fields.ts +2 -0
- package/src/polygon.ts +0 -17
- package/src/primitives/index.ts +0 -5
- package/src/resize.ts +6 -7
- package/src/scaling/node.ts +29 -3
- package/src/shaders/model.ts +2 -1
- package/src/shared-styles.ts +3 -4
- package/src/strokes.ts +121 -0
- package/src/text/auto-resize.ts +25 -0
- package/src/text/index.ts +2 -0
- package/src/text/lengths.ts +11 -0
- package/src/text/overflow.ts +32 -6
- package/src/text/path.ts +13 -0
- package/src/text/picture.ts +5 -5
- package/src/text/runs.ts +80 -0
- package/src/transfer/dependencies.ts +4 -2
- package/src/transfer/nodes.ts +2 -1
- package/src/types.ts +62 -8
- package/src/variables/index.ts +1 -0
- package/src/variables/lookup.ts +31 -0
- package/src/variables/paints.ts +101 -20
- package/src/variables/resolution.ts +15 -2
- package/src/variables/subtree.ts +1 -1
- package/src/vector-network.ts +19 -142
- package/dist/random.d.ts +0 -7
- package/dist/random.d.ts.map +0 -1
- package/dist/random.js +0 -25
- package/dist/random.js.map +0 -1
- package/src/random.ts +0 -28
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
import { minOf } from '@open-pencil/std/arrays'
|
|
2
|
+
|
|
3
|
+
import type { SceneNode } from '../types'
|
|
4
|
+
import type { PathSink } from './sink'
|
|
5
|
+
|
|
6
|
+
/** What a rectangle's smoothed corners are drawn from. */
|
|
7
|
+
export type SmoothRectNode = Pick<
|
|
8
|
+
SceneNode,
|
|
9
|
+
| 'cornerRadius'
|
|
10
|
+
| 'cornerSmoothing'
|
|
11
|
+
| 'independentCorners'
|
|
12
|
+
| 'topLeftRadius'
|
|
13
|
+
| 'topRightRadius'
|
|
14
|
+
| 'bottomRightRadius'
|
|
15
|
+
| 'bottomLeftRadius'
|
|
16
|
+
>
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Where a smoothed rectangle is traced: a CanvasKit `PathBuilder`, or a writer of SVG path data.
|
|
20
|
+
* `arcToRotated` takes CanvasKit's arguments, which match an SVG arc's.
|
|
21
|
+
*/
|
|
22
|
+
export interface SmoothCornerSink extends PathSink {
|
|
23
|
+
arcToRotated(
|
|
24
|
+
rx: number,
|
|
25
|
+
ry: number,
|
|
26
|
+
xAxisRotate: number,
|
|
27
|
+
useSmallArc: boolean,
|
|
28
|
+
isCCW: boolean,
|
|
29
|
+
x: number,
|
|
30
|
+
y: number
|
|
31
|
+
): unknown
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
type SmoothCornerKey = 'topLeft' | 'topRight' | 'bottomRight' | 'bottomLeft'
|
|
35
|
+
|
|
36
|
+
type SmoothCorner = {
|
|
37
|
+
radius: number
|
|
38
|
+
budget: number
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
type SmoothCornerPathParams = {
|
|
42
|
+
a: number
|
|
43
|
+
b: number
|
|
44
|
+
c: number
|
|
45
|
+
d: number
|
|
46
|
+
p: number
|
|
47
|
+
radius: number
|
|
48
|
+
arcSectionLength: number
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function smoothCornerRadii(
|
|
52
|
+
node: SmoothRectNode,
|
|
53
|
+
width: number,
|
|
54
|
+
height: number,
|
|
55
|
+
spread: number
|
|
56
|
+
): Record<SmoothCornerKey, SmoothCorner> {
|
|
57
|
+
const radius = (value: number) => Math.max(0, value + spread)
|
|
58
|
+
const radii: Record<SmoothCornerKey, number> = node.independentCorners
|
|
59
|
+
? {
|
|
60
|
+
topLeft: radius(node.topLeftRadius),
|
|
61
|
+
topRight: radius(node.topRightRadius),
|
|
62
|
+
bottomRight: radius(node.bottomRightRadius),
|
|
63
|
+
bottomLeft: radius(node.bottomLeftRadius)
|
|
64
|
+
}
|
|
65
|
+
: {
|
|
66
|
+
topLeft: radius(node.cornerRadius),
|
|
67
|
+
topRight: radius(node.cornerRadius),
|
|
68
|
+
bottomRight: radius(node.cornerRadius),
|
|
69
|
+
bottomLeft: radius(node.cornerRadius)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (
|
|
73
|
+
radii.topLeft === radii.topRight &&
|
|
74
|
+
radii.topRight === radii.bottomRight &&
|
|
75
|
+
radii.bottomRight === radii.bottomLeft
|
|
76
|
+
) {
|
|
77
|
+
const budget = Math.min(width, height) / 2
|
|
78
|
+
const clampedRadius = Math.min(radii.topLeft, budget)
|
|
79
|
+
return {
|
|
80
|
+
topLeft: { radius: clampedRadius, budget },
|
|
81
|
+
topRight: { radius: clampedRadius, budget },
|
|
82
|
+
bottomRight: { radius: clampedRadius, budget },
|
|
83
|
+
bottomLeft: { radius: clampedRadius, budget }
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const budgets: Record<SmoothCornerKey, number> = {
|
|
88
|
+
topLeft: -1,
|
|
89
|
+
topRight: -1,
|
|
90
|
+
bottomRight: -1,
|
|
91
|
+
bottomLeft: -1
|
|
92
|
+
}
|
|
93
|
+
const adjacentByCorner: Record<
|
|
94
|
+
SmoothCornerKey,
|
|
95
|
+
Array<{ corner: SmoothCornerKey; sideLength: number }>
|
|
96
|
+
> = {
|
|
97
|
+
topLeft: [
|
|
98
|
+
{ corner: 'topRight', sideLength: width },
|
|
99
|
+
{ corner: 'bottomLeft', sideLength: height }
|
|
100
|
+
],
|
|
101
|
+
topRight: [
|
|
102
|
+
{ corner: 'topLeft', sideLength: width },
|
|
103
|
+
{ corner: 'bottomRight', sideLength: height }
|
|
104
|
+
],
|
|
105
|
+
bottomRight: [
|
|
106
|
+
{ corner: 'bottomLeft', sideLength: width },
|
|
107
|
+
{ corner: 'topRight', sideLength: height }
|
|
108
|
+
],
|
|
109
|
+
bottomLeft: [
|
|
110
|
+
{ corner: 'bottomRight', sideLength: width },
|
|
111
|
+
{ corner: 'topLeft', sideLength: height }
|
|
112
|
+
]
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
for (const corner of (Object.keys(radii) as SmoothCornerKey[]).sort(
|
|
116
|
+
(a, b) => radii[b] - radii[a]
|
|
117
|
+
)) {
|
|
118
|
+
const cornerRadius = radii[corner]
|
|
119
|
+
const budget = minOf(
|
|
120
|
+
adjacentByCorner[corner].map((adjacent) => {
|
|
121
|
+
const adjacentRadius = radii[adjacent.corner]
|
|
122
|
+
if (cornerRadius === 0 && adjacentRadius === 0) return 0
|
|
123
|
+
if (budgets[adjacent.corner] >= 0) return adjacent.sideLength - budgets[adjacent.corner]
|
|
124
|
+
return (cornerRadius / (cornerRadius + adjacentRadius)) * adjacent.sideLength
|
|
125
|
+
})
|
|
126
|
+
)
|
|
127
|
+
budgets[corner] = budget
|
|
128
|
+
radii[corner] = Math.min(cornerRadius, budget)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return {
|
|
132
|
+
topLeft: { radius: radii.topLeft, budget: budgets.topLeft },
|
|
133
|
+
topRight: { radius: radii.topRight, budget: budgets.topRight },
|
|
134
|
+
bottomRight: { radius: radii.bottomRight, budget: budgets.bottomRight },
|
|
135
|
+
bottomLeft: { radius: radii.bottomLeft, budget: budgets.bottomLeft }
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function degreesToRadians(degrees: number): number {
|
|
140
|
+
return (degrees * Math.PI) / 180
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function smoothCornerPathParams(corner: SmoothCorner, smoothing: number): SmoothCornerPathParams {
|
|
144
|
+
let cornerSmoothing = smoothing
|
|
145
|
+
let p = (1 + cornerSmoothing) * corner.radius
|
|
146
|
+
if (corner.radius > 0) {
|
|
147
|
+
const maxSmoothing = corner.budget / corner.radius - 1
|
|
148
|
+
cornerSmoothing = Math.min(cornerSmoothing, maxSmoothing)
|
|
149
|
+
p = Math.min(p, corner.budget)
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const arcMeasure = 90 * (1 - cornerSmoothing)
|
|
153
|
+
const arcSectionLength = Math.sin(degreesToRadians(arcMeasure / 2)) * corner.radius * Math.sqrt(2)
|
|
154
|
+
const angleAlpha = (90 - arcMeasure) / 2
|
|
155
|
+
const p3ToP4Distance = corner.radius * Math.tan(degreesToRadians(angleAlpha / 2))
|
|
156
|
+
const angleBeta = 45 * cornerSmoothing
|
|
157
|
+
const c = p3ToP4Distance * Math.cos(degreesToRadians(angleBeta))
|
|
158
|
+
const d = c * Math.tan(degreesToRadians(angleBeta))
|
|
159
|
+
const b = (p - arcSectionLength - c - d) / 3
|
|
160
|
+
|
|
161
|
+
return {
|
|
162
|
+
a: 2 * b,
|
|
163
|
+
b,
|
|
164
|
+
c,
|
|
165
|
+
d,
|
|
166
|
+
p,
|
|
167
|
+
radius: corner.radius,
|
|
168
|
+
arcSectionLength
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function drawTopRightSmoothCorner(
|
|
173
|
+
path: SmoothCornerSink,
|
|
174
|
+
corner: SmoothCornerPathParams,
|
|
175
|
+
x: number,
|
|
176
|
+
y: number
|
|
177
|
+
) {
|
|
178
|
+
if (corner.radius === 0) {
|
|
179
|
+
path.lineTo(x + corner.p, y)
|
|
180
|
+
return
|
|
181
|
+
}
|
|
182
|
+
path.cubicTo(
|
|
183
|
+
x + corner.a,
|
|
184
|
+
y,
|
|
185
|
+
x + corner.a + corner.b,
|
|
186
|
+
y,
|
|
187
|
+
x + corner.a + corner.b + corner.c,
|
|
188
|
+
y + corner.d
|
|
189
|
+
)
|
|
190
|
+
path.arcToRotated(
|
|
191
|
+
corner.radius,
|
|
192
|
+
corner.radius,
|
|
193
|
+
0,
|
|
194
|
+
true,
|
|
195
|
+
false,
|
|
196
|
+
x + corner.p - corner.d,
|
|
197
|
+
y + corner.p - corner.a - corner.b - corner.c
|
|
198
|
+
)
|
|
199
|
+
path.cubicTo(
|
|
200
|
+
x + corner.p,
|
|
201
|
+
y + corner.p - corner.a - corner.b,
|
|
202
|
+
x + corner.p,
|
|
203
|
+
y + corner.p - corner.a,
|
|
204
|
+
x + corner.p,
|
|
205
|
+
y + corner.p
|
|
206
|
+
)
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function drawBottomRightSmoothCorner(
|
|
210
|
+
path: SmoothCornerSink,
|
|
211
|
+
corner: SmoothCornerPathParams,
|
|
212
|
+
x: number,
|
|
213
|
+
y: number
|
|
214
|
+
) {
|
|
215
|
+
if (corner.radius === 0) {
|
|
216
|
+
path.lineTo(x, y + corner.p)
|
|
217
|
+
return
|
|
218
|
+
}
|
|
219
|
+
path.cubicTo(
|
|
220
|
+
x,
|
|
221
|
+
y + corner.a,
|
|
222
|
+
x,
|
|
223
|
+
y + corner.a + corner.b,
|
|
224
|
+
x - corner.d,
|
|
225
|
+
y + corner.a + corner.b + corner.c
|
|
226
|
+
)
|
|
227
|
+
path.arcToRotated(
|
|
228
|
+
corner.radius,
|
|
229
|
+
corner.radius,
|
|
230
|
+
0,
|
|
231
|
+
true,
|
|
232
|
+
false,
|
|
233
|
+
x - corner.p + corner.a + corner.b + corner.c,
|
|
234
|
+
y + corner.p - corner.d
|
|
235
|
+
)
|
|
236
|
+
path.cubicTo(
|
|
237
|
+
x - corner.p + corner.a + corner.b,
|
|
238
|
+
y + corner.p,
|
|
239
|
+
x - corner.p + corner.a,
|
|
240
|
+
y + corner.p,
|
|
241
|
+
x - corner.p,
|
|
242
|
+
y + corner.p
|
|
243
|
+
)
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function drawBottomLeftSmoothCorner(
|
|
247
|
+
path: SmoothCornerSink,
|
|
248
|
+
corner: SmoothCornerPathParams,
|
|
249
|
+
x: number,
|
|
250
|
+
y: number
|
|
251
|
+
) {
|
|
252
|
+
if (corner.radius === 0) {
|
|
253
|
+
path.lineTo(x - corner.p, y)
|
|
254
|
+
return
|
|
255
|
+
}
|
|
256
|
+
path.cubicTo(
|
|
257
|
+
x - corner.a,
|
|
258
|
+
y,
|
|
259
|
+
x - corner.a - corner.b,
|
|
260
|
+
y,
|
|
261
|
+
x - corner.a - corner.b - corner.c,
|
|
262
|
+
y - corner.d
|
|
263
|
+
)
|
|
264
|
+
path.arcToRotated(
|
|
265
|
+
corner.radius,
|
|
266
|
+
corner.radius,
|
|
267
|
+
0,
|
|
268
|
+
true,
|
|
269
|
+
false,
|
|
270
|
+
x - corner.p + corner.d,
|
|
271
|
+
y - corner.p + corner.a + corner.b + corner.c
|
|
272
|
+
)
|
|
273
|
+
path.cubicTo(
|
|
274
|
+
x - corner.p,
|
|
275
|
+
y - corner.p + corner.a + corner.b,
|
|
276
|
+
x - corner.p,
|
|
277
|
+
y - corner.p + corner.a,
|
|
278
|
+
x - corner.p,
|
|
279
|
+
y - corner.p
|
|
280
|
+
)
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function drawTopLeftSmoothCorner(
|
|
284
|
+
path: SmoothCornerSink,
|
|
285
|
+
corner: SmoothCornerPathParams,
|
|
286
|
+
x: number,
|
|
287
|
+
y: number
|
|
288
|
+
) {
|
|
289
|
+
if (corner.radius === 0) {
|
|
290
|
+
path.lineTo(x, y - corner.p)
|
|
291
|
+
return
|
|
292
|
+
}
|
|
293
|
+
path.cubicTo(
|
|
294
|
+
x,
|
|
295
|
+
y - corner.a,
|
|
296
|
+
x,
|
|
297
|
+
y - corner.a - corner.b,
|
|
298
|
+
x + corner.d,
|
|
299
|
+
y - corner.a - corner.b - corner.c
|
|
300
|
+
)
|
|
301
|
+
path.arcToRotated(
|
|
302
|
+
corner.radius,
|
|
303
|
+
corner.radius,
|
|
304
|
+
0,
|
|
305
|
+
true,
|
|
306
|
+
false,
|
|
307
|
+
x + corner.p - corner.a - corner.b - corner.c,
|
|
308
|
+
y - corner.p + corner.d
|
|
309
|
+
)
|
|
310
|
+
path.cubicTo(
|
|
311
|
+
x + corner.p - corner.a - corner.b,
|
|
312
|
+
y - corner.p,
|
|
313
|
+
x + corner.p - corner.a,
|
|
314
|
+
y - corner.p,
|
|
315
|
+
x + corner.p,
|
|
316
|
+
y - corner.p
|
|
317
|
+
)
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Trace a rectangle with smoothed corners into `path`, as Figma rounds them. False, with nothing
|
|
322
|
+
* traced, when no corner is rounded, so the caller can draw a plain rectangle.
|
|
323
|
+
*/
|
|
324
|
+
export function traceSmoothRect(
|
|
325
|
+
path: SmoothCornerSink,
|
|
326
|
+
node: SmoothRectNode,
|
|
327
|
+
box: { left: number; top: number; right: number; bottom: number },
|
|
328
|
+
spread = 0
|
|
329
|
+
): boolean {
|
|
330
|
+
const { left, top, right, bottom } = box
|
|
331
|
+
const smoothing = Math.max(0, Math.min(node.cornerSmoothing, 1))
|
|
332
|
+
const corners = smoothCornerRadii(node, right - left, bottom - top, spread)
|
|
333
|
+
const topLeftCorner = smoothCornerPathParams(corners.topLeft, smoothing)
|
|
334
|
+
const topRightCorner = smoothCornerPathParams(corners.topRight, smoothing)
|
|
335
|
+
const bottomRightCorner = smoothCornerPathParams(corners.bottomRight, smoothing)
|
|
336
|
+
const bottomLeftCorner = smoothCornerPathParams(corners.bottomLeft, smoothing)
|
|
337
|
+
|
|
338
|
+
if (
|
|
339
|
+
topLeftCorner.radius === 0 &&
|
|
340
|
+
topRightCorner.radius === 0 &&
|
|
341
|
+
bottomRightCorner.radius === 0 &&
|
|
342
|
+
bottomLeftCorner.radius === 0
|
|
343
|
+
) {
|
|
344
|
+
return false
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
path.moveTo(right - topRightCorner.p, top)
|
|
348
|
+
drawTopRightSmoothCorner(path, topRightCorner, right - topRightCorner.p, top)
|
|
349
|
+
path.lineTo(right, bottom - bottomRightCorner.p)
|
|
350
|
+
drawBottomRightSmoothCorner(path, bottomRightCorner, right, bottom - bottomRightCorner.p)
|
|
351
|
+
path.lineTo(left + bottomLeftCorner.p, bottom)
|
|
352
|
+
drawBottomLeftSmoothCorner(path, bottomLeftCorner, left + bottomLeftCorner.p, bottom)
|
|
353
|
+
path.lineTo(left, top + topLeftCorner.p)
|
|
354
|
+
drawTopLeftSmoothCorner(path, topLeftCorner, left, top + topLeftCorner.p)
|
|
355
|
+
path.close()
|
|
356
|
+
return true
|
|
357
|
+
}
|
|
@@ -216,6 +216,8 @@ export const OPEN_PENCIL_PLUGIN_DATA = {
|
|
|
216
216
|
'format',
|
|
217
217
|
v.pipe(v.string(), v.trim(), v.maxLength(100), v.regex(MODE_ATTRIBUTE_PATTERN))
|
|
218
218
|
),
|
|
219
|
+
/** The library mode each mode of a copied collection came from, by the file's mode id. */
|
|
220
|
+
libraryModes: jsonPluginDataField('libraryModes', 'format', v.record(v.string(), v.string())),
|
|
219
221
|
/** One entry per paint picked in OkHCL, so the picker reopens on the same coordinates. */
|
|
220
222
|
okhcl: jsonPluginDataField('okhcl', 'content', okhclSchema),
|
|
221
223
|
/** How a main component or component set behaves as a control in preview. */
|
package/src/polygon.ts
CHANGED
|
@@ -16,23 +16,6 @@ export type PolygonOutlineCommand =
|
|
|
16
16
|
| { type: 'C'; x1: number; y1: number; x2: number; y2: number; x: number; y: number }
|
|
17
17
|
| { type: 'Z' }
|
|
18
18
|
|
|
19
|
-
/** Outline commands as SVG path data, with each coordinate passed through `round`. */
|
|
20
|
-
export function outlineSVGPath(
|
|
21
|
-
commands: readonly PolygonOutlineCommand[],
|
|
22
|
-
round: (value: number) => number = (value) => Math.round(value * 100) / 100
|
|
23
|
-
): string {
|
|
24
|
-
return commands
|
|
25
|
-
.map((command) => {
|
|
26
|
-
if (command.type === 'Z') return 'Z'
|
|
27
|
-
if (command.type === 'C') {
|
|
28
|
-
const points = [command.x1, command.y1, command.x2, command.y2, command.x, command.y]
|
|
29
|
-
return `C${points.map(round).join(' ')}`
|
|
30
|
-
}
|
|
31
|
-
return `${command.type}${round(command.x)} ${round(command.y)}`
|
|
32
|
-
})
|
|
33
|
-
.join('')
|
|
34
|
-
}
|
|
35
|
-
|
|
36
19
|
type PolygonNode = Pick<
|
|
37
20
|
SceneNode,
|
|
38
21
|
| 'type'
|
package/src/primitives/index.ts
CHANGED
|
@@ -36,9 +36,4 @@ export interface Rect {
|
|
|
36
36
|
height: number
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
export type JSONPrimitive = string | number | boolean | null
|
|
40
|
-
export type JSONValue = JSONPrimitive | JSONObject | JSONArray
|
|
41
|
-
export type JSONObject = { [key: string]: unknown }
|
|
42
|
-
export type JSONArray = unknown[]
|
|
43
|
-
|
|
44
39
|
export * from './schema'
|
package/src/resize.ts
CHANGED
|
@@ -13,6 +13,7 @@ import { enforcedAspectRatio } from './layout/aspect-ratio'
|
|
|
13
13
|
import Matrix, { type Mat3 } from './matrix'
|
|
14
14
|
import { FITTED_CONTAINER_TYPES } from './node-defaults'
|
|
15
15
|
import type { Rect } from './primitives'
|
|
16
|
+
import { followsPath } from './text/path'
|
|
16
17
|
import type { ConstraintType, DerivedTextGlyph, SceneNode, Stroke, VectorNetwork } from './types'
|
|
17
18
|
|
|
18
19
|
/**
|
|
@@ -305,11 +306,9 @@ export function scaledGeometryChanges(
|
|
|
305
306
|
}
|
|
306
307
|
// Path text scales with its path, including baked path text that kept only rotated glyphs;
|
|
307
308
|
// other text reflows in the new box, so its glyphs are dropped.
|
|
308
|
-
const
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
if (glyphs.length > 0 && pathText) {
|
|
312
|
-
changes.derivedTextGlyphs = scaleDerivedGlyphs(glyphs, sx, sy)
|
|
309
|
+
const pathText = followsPath(orig)
|
|
310
|
+
if (pathText) {
|
|
311
|
+
changes.derivedTextGlyphs = scaleDerivedGlyphs(orig.derivedTextGlyphs, sx, sy)
|
|
313
312
|
// Keep the layout/selection box in sync with the scaled glyphs. reflow
|
|
314
313
|
// overrides this when it applies; this covers the fallback where reflow
|
|
315
314
|
// returns null (no path data) so the box doesn't carry a stale size.
|
|
@@ -324,7 +323,7 @@ export function scaledGeometryChanges(
|
|
|
324
323
|
}
|
|
325
324
|
// Glyphs scaled as a picture take their strokes along; any other stroke keeps its weight, as
|
|
326
325
|
// Figma reports it, even where only a scaled outline can draw it.
|
|
327
|
-
if (orig.strokes.length > 0 &&
|
|
326
|
+
if (orig.strokes.length > 0 && pathText) {
|
|
328
327
|
changes.strokes = scaleStrokes(orig.strokes, sx, sy)
|
|
329
328
|
}
|
|
330
329
|
return changes
|
|
@@ -445,7 +444,7 @@ function placeConstrained(
|
|
|
445
444
|
...flipChanges(original, child, flipsX, flipsY),
|
|
446
445
|
...constrainedGeometryChanges(original, child, box)
|
|
447
446
|
})
|
|
448
|
-
// The final pass sees layout containers after
|
|
447
|
+
// The final pass sees layout containers after layout has resolved HUG/FILL sizing.
|
|
449
448
|
placeChildren(plan, child.id, original, child.layoutMode === 'NONE' ? box : child)
|
|
450
449
|
}
|
|
451
450
|
|
package/src/scaling/node.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { cloneVectorNetwork, scaleGeometryPaths } from '../copy'
|
|
1
|
+
import { cloneVectorNetwork, copyDerivedGlyphs, scaleGeometryPaths } from '../copy'
|
|
2
2
|
import { scaledTextLength } from '../text/lengths'
|
|
3
3
|
import { TEXT_PARAGRAPH_SPACING_FIELDS } from '../text/paragraphs'
|
|
4
|
+
import { followsPath } from '../text/path'
|
|
4
5
|
import {
|
|
5
6
|
type Effect,
|
|
6
7
|
type Fill,
|
|
@@ -119,6 +120,29 @@ function scaledVectorNetwork(node: SceneNode, scale: number): SceneNode['vectorN
|
|
|
119
120
|
return network
|
|
120
121
|
}
|
|
121
122
|
|
|
123
|
+
function scaledPathText(node: SceneNode, scale: number): Partial<SceneNode> {
|
|
124
|
+
if (!node.textPathData && !followsPath(node)) {
|
|
125
|
+
return {}
|
|
126
|
+
}
|
|
127
|
+
const box = node.textPathBox
|
|
128
|
+
return {
|
|
129
|
+
derivedTextGlyphs:
|
|
130
|
+
copyDerivedGlyphs(node.derivedTextGlyphs)?.map((glyph) => ({
|
|
131
|
+
...glyph,
|
|
132
|
+
x: glyph.x * scale,
|
|
133
|
+
y: glyph.y * scale,
|
|
134
|
+
fontSize: glyph.fontSize * scale,
|
|
135
|
+
...(glyph.advance === undefined ? {} : { advance: glyph.advance * scale })
|
|
136
|
+
})) ?? null,
|
|
137
|
+
textPathBox: box && {
|
|
138
|
+
x: box.x * scale,
|
|
139
|
+
y: box.y * scale,
|
|
140
|
+
width: box.width * scale,
|
|
141
|
+
height: box.height * scale
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
122
146
|
export function scaleNodeChanges(
|
|
123
147
|
node: SceneNode,
|
|
124
148
|
scale: number,
|
|
@@ -148,8 +172,9 @@ export function scaleNodeChanges(
|
|
|
148
172
|
paragraphSpacing: node.paragraphSpacing * scale,
|
|
149
173
|
paragraphIndent: node.paragraphIndent * scale,
|
|
150
174
|
textParagraphs: scaledParagraphStyles(node.textParagraphs, scale),
|
|
175
|
+
derivedTextHeight: scaledOptional(node.derivedTextHeight, scale),
|
|
151
176
|
itemSpacing: node.itemSpacing * scale,
|
|
152
|
-
counterAxisSpacing: node.counterAxisSpacing * scale,
|
|
177
|
+
counterAxisSpacing: node.counterAxisSpacing === null ? null : node.counterAxisSpacing * scale,
|
|
153
178
|
paddingTop: node.paddingTop * scale,
|
|
154
179
|
paddingRight: node.paddingRight * scale,
|
|
155
180
|
paddingBottom: node.paddingBottom * scale,
|
|
@@ -171,6 +196,7 @@ export function scaleNodeChanges(
|
|
|
171
196
|
layoutGrids: scaledLayoutGrids(node.layoutGrids, scale),
|
|
172
197
|
vectorNetwork: scaledVectorNetwork(node, scale),
|
|
173
198
|
fillGeometry: scaleGeometryPaths(node.fillGeometry, scale, scale),
|
|
174
|
-
strokeGeometry: scaleGeometryPaths(node.strokeGeometry, scale, scale)
|
|
199
|
+
strokeGeometry: scaleGeometryPaths(node.strokeGeometry, scale, scale),
|
|
200
|
+
...scaledPathText(node, scale)
|
|
175
201
|
}
|
|
176
202
|
}
|
package/src/shaders/model.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { sha1 } from '@noble/hashes/legacy.js'
|
|
2
2
|
import { bytesToHex, utf8ToBytes } from '@noble/hashes/utils.js'
|
|
3
3
|
|
|
4
|
+
import { randomHex } from '@open-pencil/std/random'
|
|
5
|
+
|
|
4
6
|
import { TRANSPARENT } from '../constants'
|
|
5
7
|
import { imagePaintHashes } from '../images'
|
|
6
8
|
import { readAllPluginData, withAllPluginData } from '../plugin-data/field'
|
|
7
9
|
import { OPEN_PENCIL_PLUGIN_DATA } from '../plugin-data/fields'
|
|
8
|
-
import { randomHex } from '../random'
|
|
9
10
|
import type { Fill, SceneNode } from '../types'
|
|
10
11
|
import type { ShaderPaint, ShaderPreset } from './schema'
|
|
11
12
|
|
package/src/shared-styles.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { compact, isEqual, omit, pick } from 'es-toolkit'
|
|
2
2
|
|
|
3
|
+
import { randomHex } from '@open-pencil/std/random'
|
|
4
|
+
|
|
3
5
|
import { BLACK } from './constants'
|
|
4
6
|
import { copyEffects, copyFills, copyLayoutGrids } from './copy'
|
|
5
7
|
import type { SceneGraph } from './graph'
|
|
6
8
|
import { newStrokeGeometry } from './node-defaults'
|
|
7
|
-
import {
|
|
9
|
+
import { STROKE_LINE_KEYS } from './strokes'
|
|
8
10
|
import { styleRunReferences } from './text/references'
|
|
9
11
|
import type {
|
|
10
12
|
CharacterStyleOverride,
|
|
@@ -121,9 +123,6 @@ export function getSharedStyles(graph: SceneGraph, kind: SharedStyleKind): Share
|
|
|
121
123
|
return styles.sort((left, right) => left.name.localeCompare(right.name))
|
|
122
124
|
}
|
|
123
125
|
|
|
124
|
-
/** What a stroke draws along beside its paint: the layer's, not its stroke style's. */
|
|
125
|
-
const STROKE_LINE_KEYS = ['weight', 'align', 'cap', 'join', 'dashPattern'] as const
|
|
126
|
-
|
|
127
126
|
/**
|
|
128
127
|
* Whether new strokes only change how their lines run, keeping every paint. A stroke style gives
|
|
129
128
|
* paints only, so such a write keeps it; any other strokes write leaves it, as setting even the
|
package/src/strokes.ts
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { isEqual } from 'es-toolkit'
|
|
2
|
+
|
|
3
|
+
import { copyStroke } from './copy'
|
|
4
|
+
import type { SceneNode, Stroke } from './types'
|
|
5
|
+
|
|
6
|
+
/** What a stroke draws along beside its paint. */
|
|
7
|
+
export const STROKE_LINE_KEYS = [
|
|
8
|
+
'weight',
|
|
9
|
+
'align',
|
|
10
|
+
'cap',
|
|
11
|
+
'join',
|
|
12
|
+
'dashPattern'
|
|
13
|
+
] as const satisfies readonly (keyof Stroke)[]
|
|
14
|
+
|
|
15
|
+
type StrokeLineKey = (typeof STROKE_LINE_KEYS)[number]
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The layer field that keeps each line value. Figma keeps one of each per layer; a layer here
|
|
19
|
+
* also keeps them on each stroke, which the renderer draws.
|
|
20
|
+
*/
|
|
21
|
+
export const STROKE_LINE_FIELDS = {
|
|
22
|
+
weight: 'strokeWeight',
|
|
23
|
+
align: 'strokeAlign',
|
|
24
|
+
cap: 'strokeCap',
|
|
25
|
+
join: 'strokeJoin',
|
|
26
|
+
dashPattern: 'dashPattern'
|
|
27
|
+
} as const satisfies Record<StrokeLineKey, keyof SceneNode>
|
|
28
|
+
|
|
29
|
+
export type StrokeLines = Partial<Pick<Stroke, StrokeLineKey>>
|
|
30
|
+
|
|
31
|
+
/** The value a stroke draws with; a stroke without a dash draws solid. */
|
|
32
|
+
export function strokeLine(stroke: Stroke, key: StrokeLineKey): Stroke[StrokeLineKey] {
|
|
33
|
+
return key === 'dashPattern' ? (stroke.dashPattern ?? []) : stroke[key]
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** `strokes` with each line field `values` names, as a layer field set alone gives every stroke. */
|
|
37
|
+
export function withStrokeLines(strokes: readonly Stroke[], values: StrokeLines): Stroke[] {
|
|
38
|
+
return strokes.map((stroke) => {
|
|
39
|
+
const next = copyStroke(stroke)
|
|
40
|
+
for (const key of STROKE_LINE_KEYS) {
|
|
41
|
+
const value = values[key]
|
|
42
|
+
if (value !== undefined) Reflect.set(next, key, structuredClone(value))
|
|
43
|
+
}
|
|
44
|
+
return next
|
|
45
|
+
})
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* What else `changes` sets so a layer's line fields and its strokes agree: new strokes set the
|
|
50
|
+
* layer's fields to the first stroke's, and a layer field set without strokes goes to every
|
|
51
|
+
* stroke. Strokes win when `changes` sets both, so restoring strokes restores what they draw.
|
|
52
|
+
* Only fields whose value changes are returned, so an instance overrides no more than an edit
|
|
53
|
+
* changed. A layer without strokes keeps its fields for the next stroke (`newStrokeGeometry`).
|
|
54
|
+
*/
|
|
55
|
+
export function strokeLineChanges(
|
|
56
|
+
node: SceneNode,
|
|
57
|
+
changes: Partial<SceneNode>
|
|
58
|
+
): Partial<SceneNode> {
|
|
59
|
+
if (changes.strokes) {
|
|
60
|
+
const first = changes.strokes.at(0)
|
|
61
|
+
if (!first) return {}
|
|
62
|
+
const derived: Partial<SceneNode> = {}
|
|
63
|
+
for (const key of STROKE_LINE_KEYS) {
|
|
64
|
+
const field = STROKE_LINE_FIELDS[key]
|
|
65
|
+
const current = field in changes ? changes[field] : node[field]
|
|
66
|
+
// A stroke without its own cap or join draws with the layer's.
|
|
67
|
+
const value = strokeLine(first, key) ?? current
|
|
68
|
+
if (!isEqual(current, value)) Reflect.set(derived, field, structuredClone(value))
|
|
69
|
+
}
|
|
70
|
+
// Strokes without their own cap or join take the layer's, so every stroke says what it draws.
|
|
71
|
+
const cap = derived.strokeCap ?? changes.strokeCap ?? node.strokeCap
|
|
72
|
+
const join = derived.strokeJoin ?? changes.strokeJoin ?? node.strokeJoin
|
|
73
|
+
if (changes.strokes.some((stroke) => stroke.cap === undefined || stroke.join === undefined)) {
|
|
74
|
+
derived.strokes = changes.strokes.map((stroke) => ({
|
|
75
|
+
...copyStroke(stroke),
|
|
76
|
+
cap: stroke.cap ?? cap,
|
|
77
|
+
join: stroke.join ?? join
|
|
78
|
+
}))
|
|
79
|
+
}
|
|
80
|
+
return derived
|
|
81
|
+
}
|
|
82
|
+
if (node.strokes.length === 0) return {}
|
|
83
|
+
const values: StrokeLines = {}
|
|
84
|
+
let set = false
|
|
85
|
+
for (const key of STROKE_LINE_KEYS) {
|
|
86
|
+
const value = changes[STROKE_LINE_FIELDS[key]]
|
|
87
|
+
if (value === undefined) continue
|
|
88
|
+
Reflect.set(values, key, value)
|
|
89
|
+
set = true
|
|
90
|
+
}
|
|
91
|
+
// Most writes, such as a move or a layout pass, set no line field.
|
|
92
|
+
if (!set) return {}
|
|
93
|
+
const strokes = withStrokeLines(node.strokes, values)
|
|
94
|
+
return isEqual(strokes, node.strokes) ? {} : { strokes }
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Whether the strokes `changes` sets differ from `node`'s by more than the layer fields the same
|
|
99
|
+
* write sets: a paint, a stroke added or removed, or a stroke's line that no layer field records.
|
|
100
|
+
* Figma overrides a weight or dash set on an instance layer alone, so a component's later paint
|
|
101
|
+
* change still reaches the copy (live).
|
|
102
|
+
*/
|
|
103
|
+
export function changesMoreThanStrokeLines(node: SceneNode, changes: Partial<SceneNode>): boolean {
|
|
104
|
+
const next = changes.strokes
|
|
105
|
+
if (!next) return false
|
|
106
|
+
if (next.length !== node.strokes.length) return true
|
|
107
|
+
const lines: StrokeLines = {}
|
|
108
|
+
for (const key of STROKE_LINE_KEYS) {
|
|
109
|
+
const value = changes[STROKE_LINE_FIELDS[key]]
|
|
110
|
+
if (value !== undefined) Reflect.set(lines, key, value)
|
|
111
|
+
}
|
|
112
|
+
// Strokes written without a cap or join take the layer's, as `strokeLineChanges` gives them,
|
|
113
|
+
// and a stroke without a dash draws as solid as one with an empty dash.
|
|
114
|
+
const drawn = (stroke: Stroke): Stroke => ({
|
|
115
|
+
...stroke,
|
|
116
|
+
cap: stroke.cap ?? node.strokeCap,
|
|
117
|
+
join: stroke.join ?? node.strokeJoin,
|
|
118
|
+
dashPattern: stroke.dashPattern ?? []
|
|
119
|
+
})
|
|
120
|
+
return !isEqual(withStrokeLines(node.strokes, lines).map(drawn), next.map(drawn))
|
|
121
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { SceneNode, TextAutoResize } from '../types'
|
|
2
|
+
|
|
3
|
+
/** A text's resizing as Figma's plugin API names it, where `TRUNCATE` is a fixed box that truncates. */
|
|
4
|
+
export type FigmaTextAutoResize = TextAutoResize | 'TRUNCATE'
|
|
5
|
+
|
|
6
|
+
type TextResizeFields = Pick<SceneNode, 'textAutoResize' | 'textTruncation' | 'maxLines'>
|
|
7
|
+
|
|
8
|
+
/** The resizing Figma's plugin API reads for a text: a fixed box that truncates reads `TRUNCATE`. */
|
|
9
|
+
export function figmaTextAutoResize(
|
|
10
|
+
node: Pick<SceneNode, 'textAutoResize' | 'textTruncation'>
|
|
11
|
+
): FigmaTextAutoResize {
|
|
12
|
+
return node.textAutoResize === 'NONE' && node.textTruncation === 'ENDING'
|
|
13
|
+
? 'TRUNCATE'
|
|
14
|
+
: node.textAutoResize
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The fields setting a resizing writes, as Figma's setter does: `TRUNCATE` fixes the box and
|
|
19
|
+
* truncates its end with no line limit, and any other mode stops truncating.
|
|
20
|
+
*/
|
|
21
|
+
export function textAutoResizeFields(mode: FigmaTextAutoResize): Partial<TextResizeFields> {
|
|
22
|
+
return mode === 'TRUNCATE'
|
|
23
|
+
? { textAutoResize: 'NONE', textTruncation: 'ENDING', maxLines: null }
|
|
24
|
+
: { textAutoResize: mode, textTruncation: 'DISABLED' }
|
|
25
|
+
}
|