@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
package/src/node-defaults.ts
CHANGED
|
@@ -52,30 +52,48 @@ export function newStrokeGeometry(
|
|
|
52
52
|
return { weight: first?.weight ?? node.strokeWeight, align: first?.align ?? node.strokeAlign }
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
+
/** A caller's list, or a fresh empty one: a list default belongs to the one layer it was made for. */
|
|
56
|
+
function ownList<T>(given: T[] | undefined): T[] {
|
|
57
|
+
return given ?? []
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** A caller's record, or a fresh empty one, for the same reason. */
|
|
61
|
+
function ownRecord<V>(given: Record<string, V> | undefined): Record<string, V> {
|
|
62
|
+
return given ?? {}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** A new layer's fills: black on text, none elsewhere. */
|
|
66
|
+
function defaultFills(type: NodeType): SceneNode['fills'] {
|
|
67
|
+
return type === 'TEXT' ? [{ type: 'SOLID', color: BLACK, opacity: 1, visible: true }] : []
|
|
68
|
+
}
|
|
69
|
+
|
|
55
70
|
export function createDefaultNode(
|
|
56
71
|
generateId: () => string,
|
|
57
72
|
type: NodeType,
|
|
58
73
|
overrides: Partial<SceneNode> = {}
|
|
59
74
|
): SceneNode {
|
|
60
|
-
|
|
75
|
+
// A literal ending in a spread is built one field at a time, and V8 keeps an object given this
|
|
76
|
+
// many fields that way in dictionary mode, about 3 KB more per layer; a spread copy of a
|
|
77
|
+
// finished literal keeps fast fields, in JavaScriptCore as well.
|
|
78
|
+
// Imports supply most lists and records, so a default is made only where the caller gives none.
|
|
79
|
+
const defaults = {
|
|
61
80
|
id: generateId(),
|
|
62
81
|
type,
|
|
63
82
|
name: type.charAt(0) + type.slice(1).toLowerCase(),
|
|
64
83
|
parentId: null,
|
|
65
|
-
childIds:
|
|
84
|
+
childIds: ownList(overrides.childIds),
|
|
66
85
|
x: 0,
|
|
67
86
|
y: 0,
|
|
68
87
|
width: 100,
|
|
69
88
|
height: 100,
|
|
70
89
|
rotation: 0,
|
|
71
|
-
source: createDefaultSourceMetadata(),
|
|
90
|
+
source: overrides.source ?? createDefaultSourceMetadata(),
|
|
72
91
|
derivedLayout: null,
|
|
73
|
-
fills:
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
guides: [],
|
|
92
|
+
fills: overrides.fills ?? defaultFills(type),
|
|
93
|
+
strokes: ownList(overrides.strokes),
|
|
94
|
+
effects: ownList(overrides.effects),
|
|
95
|
+
layoutGrids: ownList(overrides.layoutGrids),
|
|
96
|
+
guides: ownList(overrides.guides),
|
|
79
97
|
fillStyleId: null,
|
|
80
98
|
strokeStyleId: null,
|
|
81
99
|
textStyleId: null,
|
|
@@ -124,8 +142,8 @@ export function createDefaultNode(
|
|
|
124
142
|
layoutAlignSelf: 'AUTO',
|
|
125
143
|
vectorNetwork: null,
|
|
126
144
|
handleMirroring: 'NONE',
|
|
127
|
-
fillGeometry:
|
|
128
|
-
strokeGeometry:
|
|
145
|
+
fillGeometry: ownList(overrides.fillGeometry),
|
|
146
|
+
strokeGeometry: ownList(overrides.strokeGeometry),
|
|
129
147
|
arcData: null,
|
|
130
148
|
textAlignVertical: 'TOP',
|
|
131
149
|
textAutoResize: 'NONE',
|
|
@@ -133,24 +151,24 @@ export function createDefaultNode(
|
|
|
133
151
|
textDecoration: 'NONE',
|
|
134
152
|
textDecorationStyle: 'SOLID',
|
|
135
153
|
textDecorationThickness: null,
|
|
136
|
-
textDecorationFills:
|
|
154
|
+
textDecorationFills: ownList(overrides.textDecorationFills),
|
|
137
155
|
textDecorationSkipInk: true,
|
|
138
156
|
textUnderlineOffset: null,
|
|
139
157
|
maxLines: null,
|
|
140
|
-
textParagraphs:
|
|
158
|
+
textParagraphs: ownList(overrides.textParagraphs),
|
|
141
159
|
listSpacing: 0,
|
|
142
160
|
paragraphSpacing: 0,
|
|
143
161
|
paragraphIndent: 0,
|
|
144
162
|
hangingList: false,
|
|
145
|
-
styleRuns:
|
|
146
|
-
fontVariations:
|
|
163
|
+
styleRuns: ownList(overrides.styleRuns),
|
|
164
|
+
fontVariations: ownList(overrides.fontVariations),
|
|
147
165
|
opticalSizeFromFontSize: true,
|
|
148
|
-
fontFeatures:
|
|
166
|
+
fontFeatures: ownList(overrides.fontFeatures),
|
|
149
167
|
horizontalConstraint: 'MIN',
|
|
150
168
|
verticalConstraint: 'MIN',
|
|
151
169
|
strokeCap: 'NONE',
|
|
152
170
|
strokeJoin: 'MITER',
|
|
153
|
-
dashPattern:
|
|
171
|
+
dashPattern: ownList(overrides.dashPattern),
|
|
154
172
|
borderTopWeight: 0,
|
|
155
173
|
borderRightWeight: 0,
|
|
156
174
|
borderBottomWeight: 0,
|
|
@@ -169,14 +187,14 @@ export function createDefaultNode(
|
|
|
169
187
|
connectorLineType: 'ELBOWED',
|
|
170
188
|
connectorStartCap: 'ROUND',
|
|
171
189
|
connectorEndCap: 'ARROW_LINES',
|
|
172
|
-
connectorControlPoints:
|
|
190
|
+
connectorControlPoints: ownList(overrides.connectorControlPoints),
|
|
173
191
|
connectorCurveHandles: null,
|
|
174
192
|
connectorLabelPosition: null,
|
|
175
193
|
isMask: false,
|
|
176
194
|
maskType: 'ALPHA',
|
|
177
195
|
maskIsOutline: false,
|
|
178
|
-
gridTemplateColumns:
|
|
179
|
-
gridTemplateRows:
|
|
196
|
+
gridTemplateColumns: ownList(overrides.gridTemplateColumns),
|
|
197
|
+
gridTemplateRows: ownList(overrides.gridTemplateRows),
|
|
180
198
|
gridColumnGap: 0,
|
|
181
199
|
gridRowGap: 0,
|
|
182
200
|
gridPosition: null,
|
|
@@ -193,11 +211,11 @@ export function createDefaultNode(
|
|
|
193
211
|
shapeUserHeight: 0,
|
|
194
212
|
shapeTruncates: false,
|
|
195
213
|
componentId: null,
|
|
196
|
-
instanceOverrides: createInstanceOverrideState(),
|
|
197
|
-
componentPropertyDefinitions:
|
|
198
|
-
componentPropertyReferences:
|
|
199
|
-
componentPropertyAssignments:
|
|
200
|
-
componentPropertyValues:
|
|
214
|
+
instanceOverrides: overrides.instanceOverrides ?? createInstanceOverrideState(),
|
|
215
|
+
componentPropertyDefinitions: ownList(overrides.componentPropertyDefinitions),
|
|
216
|
+
componentPropertyReferences: ownList(overrides.componentPropertyReferences),
|
|
217
|
+
componentPropertyAssignments: ownRecord(overrides.componentPropertyAssignments),
|
|
218
|
+
componentPropertyValues: ownRecord(overrides.componentPropertyValues),
|
|
201
219
|
componentKey: null,
|
|
202
220
|
sourceLibraryKey: null,
|
|
203
221
|
publishId: null,
|
|
@@ -210,17 +228,17 @@ export function createDefaultNode(
|
|
|
210
228
|
isSymbolPublishable: false,
|
|
211
229
|
isExposedInstance: false,
|
|
212
230
|
symbolDescription: '',
|
|
213
|
-
symbolLinks:
|
|
214
|
-
variantPropSpecs:
|
|
215
|
-
boundVariables:
|
|
216
|
-
variableBindingScales:
|
|
217
|
-
variableAssignmentScales:
|
|
231
|
+
symbolLinks: ownList(overrides.symbolLinks),
|
|
232
|
+
variantPropSpecs: ownList(overrides.variantPropSpecs),
|
|
233
|
+
boundVariables: ownRecord(overrides.boundVariables),
|
|
234
|
+
variableBindingScales: ownRecord(overrides.variableBindingScales),
|
|
235
|
+
variableAssignmentScales: ownRecord(overrides.variableAssignmentScales),
|
|
218
236
|
componentScale: 1,
|
|
219
|
-
variableModes:
|
|
220
|
-
exportSettings:
|
|
237
|
+
variableModes: ownRecord(overrides.variableModes),
|
|
238
|
+
exportSettings: ownList(overrides.exportSettings),
|
|
221
239
|
status: null,
|
|
222
|
-
pluginData:
|
|
223
|
-
pluginRelaunchData:
|
|
240
|
+
pluginData: ownList(overrides.pluginData),
|
|
241
|
+
pluginRelaunchData: ownList(overrides.pluginRelaunchData),
|
|
224
242
|
internalOnly: false,
|
|
225
243
|
flipX: false,
|
|
226
244
|
flipY: false,
|
|
@@ -229,9 +247,9 @@ export function createDefaultNode(
|
|
|
229
247
|
derivedTextHeight: null,
|
|
230
248
|
textPathData: null,
|
|
231
249
|
textPathBox: null,
|
|
232
|
-
booleanOperation: undefined
|
|
233
|
-
...overrides
|
|
250
|
+
booleanOperation: undefined
|
|
234
251
|
} satisfies CompleteNodeFields
|
|
252
|
+
return { ...defaults, ...overrides }
|
|
235
253
|
}
|
|
236
254
|
|
|
237
255
|
/** Containers whose bounds follow their children and that set no coordinate space, as in Figma. */
|
|
@@ -263,6 +281,23 @@ export function isLayerContainer(node: Pick<SceneNode, 'type'>): boolean {
|
|
|
263
281
|
return LAYER_CONTAINER_TYPES.has(node.type)
|
|
264
282
|
}
|
|
265
283
|
|
|
284
|
+
/** The kinds that clip their children when their clip content is on. */
|
|
285
|
+
export const CLIPPING_TYPES: ReadonlySet<NodeType> = new Set<NodeType>([
|
|
286
|
+
'FRAME',
|
|
287
|
+
'COMPONENT',
|
|
288
|
+
'COMPONENT_SET',
|
|
289
|
+
'INSTANCE'
|
|
290
|
+
])
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* Whether a layer clips its children: a frame, component, component set or instance with clip
|
|
294
|
+
* content on. A section or group has no such setting in Figma and never clips, whatever an import
|
|
295
|
+
* left in `clipsContent` (Figma desktop, live).
|
|
296
|
+
*/
|
|
297
|
+
export function clipsChildren(node: Pick<SceneNode, 'type' | 'clipsContent'>): boolean {
|
|
298
|
+
return node.clipsContent && CLIPPING_TYPES.has(node.type)
|
|
299
|
+
}
|
|
300
|
+
|
|
266
301
|
export const CONTAINER_TYPES = new Set<NodeType>([
|
|
267
302
|
'CANVAS',
|
|
268
303
|
'FRAME',
|
package/src/outline/flatten.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
+
import { appendAll } from '@open-pencil/std/arrays'
|
|
2
|
+
|
|
1
3
|
import { cubicPoint } from '../cubic'
|
|
2
4
|
import { GeometryCommand, readGeometryBlob } from '../geometry'
|
|
5
|
+
import { buildChains } from '../paths/network'
|
|
3
6
|
import type { PolygonOutlineCommand } from '../polygon'
|
|
4
7
|
import type { Vector } from '../primitives'
|
|
5
8
|
import type { VectorNetwork } from '../types'
|
|
@@ -72,31 +75,37 @@ export function loopPoints(network: VectorNetwork, loop: number[], tolerance: nu
|
|
|
72
75
|
const along = segmentPoints(network, index, tolerance)
|
|
73
76
|
if (!segment || !along) continue
|
|
74
77
|
const forward = segment.start === current
|
|
75
|
-
points
|
|
78
|
+
appendAll(points, forward ? along : along.toReversed())
|
|
76
79
|
current = forward ? segment.end : segment.start
|
|
77
80
|
}
|
|
78
81
|
return points
|
|
79
82
|
}
|
|
80
83
|
|
|
81
84
|
/**
|
|
82
|
-
* The network's segments joined into runs,
|
|
83
|
-
*
|
|
85
|
+
* The network's segments joined into runs, chained through the vertices they share as the canvas
|
|
86
|
+
* draws an open network (`buildChains`), each run walked from its open end.
|
|
84
87
|
*/
|
|
85
88
|
export function segmentRuns(network: VectorNetwork, tolerance: number): Vector[][] {
|
|
86
89
|
const runs: Vector[][] = []
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
run
|
|
90
|
+
for (const chain of buildChains(network.segments)) {
|
|
91
|
+
let current = chain.start
|
|
92
|
+
let run: Vector[] = []
|
|
93
|
+
for (const index of chain.segments) {
|
|
94
|
+
const segment = network.segments[index]
|
|
95
|
+
const forward = segment.start === current
|
|
96
|
+
current = forward ? segment.end : segment.start
|
|
97
|
+
const along = segmentPoints(network, index, tolerance)
|
|
98
|
+
// A segment naming a missing vertex breaks the run: what follows starts a run of its own.
|
|
99
|
+
if (!along) {
|
|
100
|
+
if (run.length > 1) runs.push(run)
|
|
101
|
+
run = []
|
|
102
|
+
continue
|
|
103
|
+
}
|
|
104
|
+
const points = forward ? along : along.toReversed()
|
|
105
|
+
appendAll(run, run.length > 0 ? points.slice(1) : points)
|
|
96
106
|
}
|
|
97
|
-
|
|
107
|
+
if (run.length > 1) runs.push(run)
|
|
98
108
|
}
|
|
99
|
-
if (run.length > 1) runs.push(run)
|
|
100
109
|
return runs
|
|
101
110
|
}
|
|
102
111
|
|
|
@@ -124,9 +133,12 @@ export function blobContours(blob: Uint8Array, tolerance: number): Vector[][] {
|
|
|
124
133
|
const q = { x: c[0], y: c[1] }
|
|
125
134
|
const c1 = { x: last.x + (2 / 3) * (q.x - last.x), y: last.y + (2 / 3) * (q.y - last.y) }
|
|
126
135
|
const c2 = { x: end.x + (2 / 3) * (q.x - end.x), y: end.y + (2 / 3) * (q.y - end.y) }
|
|
127
|
-
contour
|
|
136
|
+
appendAll(contour, cubicPoints(last, c1, c2, end, tolerance))
|
|
128
137
|
} else {
|
|
129
|
-
|
|
138
|
+
appendAll(
|
|
139
|
+
contour,
|
|
140
|
+
cubicPoints(last, { x: c[0], y: c[1] }, { x: c[2], y: c[3] }, end, tolerance)
|
|
141
|
+
)
|
|
130
142
|
}
|
|
131
143
|
})
|
|
132
144
|
flush()
|
|
@@ -149,8 +161,9 @@ export function commandContours(
|
|
|
149
161
|
const last = contour.at(-1) ?? { x: 0, y: 0 }
|
|
150
162
|
if (command.type !== 'C') contour.push({ x: command.x, y: command.y })
|
|
151
163
|
else
|
|
152
|
-
|
|
153
|
-
|
|
164
|
+
appendAll(
|
|
165
|
+
contour,
|
|
166
|
+
cubicPoints(
|
|
154
167
|
last,
|
|
155
168
|
{ x: command.x1, y: command.y1 },
|
|
156
169
|
{ x: command.x2, y: command.y2 },
|
package/src/outline/hit.ts
CHANGED
|
@@ -2,10 +2,12 @@ import { compact } from 'es-toolkit'
|
|
|
2
2
|
|
|
3
3
|
import { arrowCapOverflow, isArrowCap, lineStrokeEnds } from '../arrow-caps'
|
|
4
4
|
import { getWorldMatrix } from '../coordinate'
|
|
5
|
+
import { strokeWeightOf } from '../geometry'
|
|
5
6
|
import type { SceneGraph } from '../graph'
|
|
6
7
|
import Matrix from '../matrix'
|
|
8
|
+
import { paintShows } from '../paints/shows'
|
|
7
9
|
import type { Vector } from '../primitives'
|
|
8
|
-
import type {
|
|
10
|
+
import type { SceneNode, Stroke } from '../types'
|
|
9
11
|
import { hitsArrowHeads } from './heads'
|
|
10
12
|
import { containsByWinding, distanceToLines, distanceToSegment } from './point'
|
|
11
13
|
import { shapeOutline, type OutlineArea, type ShapeOutline } from './shapes'
|
|
@@ -16,47 +18,28 @@ const EDGE_SAMPLES = 8
|
|
|
16
18
|
/** How far a flattened curve may stray from the shape when nothing tighter is asked for. */
|
|
17
19
|
const DEFAULT_TOLERANCE = 0.25
|
|
18
20
|
|
|
19
|
-
/**
|
|
20
|
-
* Whether a paint makes its layer clickable, as in Figma: a visible paint that shows something.
|
|
21
|
-
* A paint at 0% opacity, or a gradient whose stops are all transparent, does not; 1% does, and
|
|
22
|
-
* the layer's own opacity does not matter.
|
|
23
|
-
*/
|
|
24
|
-
export function isHitPaint(paint: Fill): boolean {
|
|
25
|
-
if (!paint.visible || !(paint.opacity > 0)) return false
|
|
26
|
-
if (paint.gradientStops) return paint.gradientStops.some((stop) => stop.color.a > 0)
|
|
27
|
-
if (paint.type === 'SOLID') return paint.color.a > 0
|
|
28
|
-
return true
|
|
29
|
-
}
|
|
30
|
-
|
|
31
21
|
function insideArea(area: OutlineArea, point: Vector): boolean {
|
|
32
22
|
return containsByWinding(area.contours, area.windingRule, point)
|
|
33
23
|
}
|
|
34
24
|
|
|
35
|
-
/** A stroke's weight
|
|
36
|
-
function
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
node.borderTopWeight,
|
|
40
|
-
node.borderRightWeight,
|
|
41
|
-
node.borderBottomWeight,
|
|
42
|
-
node.borderLeftWeight
|
|
43
|
-
)
|
|
44
|
-
}
|
|
45
|
-
return Number.isFinite(stroke.weight) ? stroke.weight : node.strokeWeight
|
|
25
|
+
/** A stroke's weight, the widest side with sides of their own, for layers without per-side lines. */
|
|
26
|
+
function hitWeight(node: SceneNode, stroke: Stroke): number {
|
|
27
|
+
const weight = strokeWeightOf(node, stroke)
|
|
28
|
+
return Number.isFinite(weight) ? weight : node.strokeWeight
|
|
46
29
|
}
|
|
47
30
|
|
|
48
31
|
/** The farthest a layer's clickable strokes reach outside its box, in its own units. */
|
|
49
32
|
export function strokeReach(node: SceneNode): number {
|
|
50
33
|
let reach = 0
|
|
51
34
|
for (const stroke of node.strokes) {
|
|
52
|
-
if (!
|
|
53
|
-
const weight =
|
|
35
|
+
if (!paintShows(stroke)) continue
|
|
36
|
+
const weight = hitWeight(node, stroke)
|
|
54
37
|
// A line's stroke lies a full weight to one side of it.
|
|
55
38
|
const side = node.type === 'LINE' || stroke.align === 'OUTSIDE'
|
|
56
39
|
reach = Math.max(reach, side ? weight : weight / 2)
|
|
57
40
|
}
|
|
58
41
|
// Arrow heads reach past the stroke.
|
|
59
|
-
const hitStrokes = node.strokes.filter(
|
|
42
|
+
const hitStrokes = node.strokes.filter(paintShows)
|
|
60
43
|
return Math.max(reach, arrowCapOverflow(hitStrokes, node.strokeCap, node.vectorNetwork))
|
|
61
44
|
}
|
|
62
45
|
|
|
@@ -306,7 +289,7 @@ function strokeHits(node: SceneNode, stroke: Stroke, probe: Probe, pad: number):
|
|
|
306
289
|
if (probe.strokeAreaWithin) return probe.strokeAreaWithin(pad)
|
|
307
290
|
const align = probe.closed && !probe.centered ? stroke.align : 'CENTER'
|
|
308
291
|
if (probe.strokeOnEdge) {
|
|
309
|
-
const weight =
|
|
292
|
+
const weight = hitWeight(node, stroke)
|
|
310
293
|
return probe.edgeWithin(strokeReachAt(align, weight, probe.inside) + pad)
|
|
311
294
|
}
|
|
312
295
|
// A flat cap ends the stroke at the line's end; round and square caps paint past it.
|
|
@@ -321,7 +304,7 @@ function strokeHits(node: SceneNode, stroke: Stroke, probe: Probe, pad: number):
|
|
|
321
304
|
}
|
|
322
305
|
if (node.type === 'LINE' && probe.local) {
|
|
323
306
|
// Figma strokes a line a full weight to one side, along the path the renderer draws.
|
|
324
|
-
const weight =
|
|
307
|
+
const weight = hitWeight(node, stroke)
|
|
325
308
|
if (!(weight > 0)) return false
|
|
326
309
|
const { start, end } = lineStrokeEnds(node.width, node.height, weight, cap)
|
|
327
310
|
if (reaches(measureLine([start, end], [true, true], probe.local), weight / 2)) return true
|
|
@@ -336,11 +319,11 @@ function strokeHits(node: SceneNode, stroke: Stroke, probe: Probe, pad: number):
|
|
|
336
319
|
}
|
|
337
320
|
const network = node.type === 'VECTOR' ? node.vectorNetwork : null
|
|
338
321
|
if (network && probe.local) {
|
|
339
|
-
const weight =
|
|
322
|
+
const weight = hitWeight(node, stroke)
|
|
340
323
|
if (weight > 0 && hitsArrowHeads(network, cap, weight, probe.local, pad)) return true
|
|
341
324
|
}
|
|
342
325
|
return probe.lines.some((line, index) => {
|
|
343
|
-
const weight = probe.lineWeights?.[index] ??
|
|
326
|
+
const weight = probe.lineWeights?.[index] ?? hitWeight(node, stroke)
|
|
344
327
|
// A side with no weight paints no border.
|
|
345
328
|
if (!(weight > 0)) return false
|
|
346
329
|
return reaches(line, strokeReachAt(align, weight, probe.inside))
|
|
@@ -363,7 +346,7 @@ export function hitsShape(
|
|
|
363
346
|
const tolerance = pad > 0 ? Math.min(DEFAULT_TOLERANCE, pad / 4) : DEFAULT_TOLERANCE
|
|
364
347
|
const probe = probeNode(graph, node, px, py, tolerance)
|
|
365
348
|
if (!probe) return null
|
|
366
|
-
const fills = node.fills.filter(
|
|
349
|
+
const fills = node.fills.filter(paintShows)
|
|
367
350
|
if (fills.length > 0 && probe.closed && (probe.inside || probe.edgeWithin(pad))) return true
|
|
368
|
-
return node.strokes.some((stroke) =>
|
|
351
|
+
return node.strokes.some((stroke) => paintShows(stroke) && strokeHits(node, stroke, probe, pad))
|
|
369
352
|
}
|
package/src/outline/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { hitsShape,
|
|
2
|
-
export { shapeOutline, type OutlineArea, type ShapeOutline } from './shapes'
|
|
1
|
+
export { hitsShape, strokeReach } from './hit'
|
|
2
|
+
export { isWholeEllipse, shapeOutline, type OutlineArea, type ShapeOutline } from './shapes'
|
|
3
3
|
export { blobContours, loopPoints, segmentPoints } from './flatten'
|
|
4
4
|
export { containsByWinding, distanceToLines, distanceToSegment, windingNumber } from './point'
|
package/src/outline/shapes.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { nodeCornerRadii } from '../corners'
|
|
2
|
+
import { borderWeights, sweepsWholeTurn } from '../geometry'
|
|
3
|
+
import { arcOutline } from '../paths/arc'
|
|
1
4
|
import { polygonOutline } from '../polygon'
|
|
2
5
|
import type { Vector } from '../primitives'
|
|
3
6
|
import type { SceneNode, WindingRule } from '../types'
|
|
@@ -47,17 +50,9 @@ function fromContours(contours: Vector[][], windingRule: WindingRule = 'NONZERO'
|
|
|
47
50
|
return { areas: [{ windingRule, contours }], lines: contours.map(closed) }
|
|
48
51
|
}
|
|
49
52
|
|
|
50
|
-
function cornerRadii(node: SceneNode): [number, number, number, number] {
|
|
51
|
-
const radii: [number, number, number, number] = node.independentCorners
|
|
52
|
-
? [node.topLeftRadius, node.topRightRadius, node.bottomRightRadius, node.bottomLeftRadius]
|
|
53
|
-
: [node.cornerRadius, node.cornerRadius, node.cornerRadius, node.cornerRadius]
|
|
54
|
-
const limit = Math.min(node.width, node.height) / 2
|
|
55
|
-
return radii.map((radius) => Math.max(0, Math.min(radius, limit))) as typeof radii
|
|
56
|
-
}
|
|
57
|
-
|
|
58
53
|
function rectangleContour(node: SceneNode, tolerance: number): Vector[] {
|
|
59
54
|
const { width: w, height: h } = node
|
|
60
|
-
const [tl, tr, br, bl] =
|
|
55
|
+
const [tl, tr, br, bl] = nodeCornerRadii(node)
|
|
61
56
|
const corner = (cx: number, cy: number, r: number, from: number): Vector[] =>
|
|
62
57
|
r > 0 ? arcPoints(cx, cy, r, r, from, from + Math.PI / 2, tolerance) : [{ x: cx, y: cy }]
|
|
63
58
|
return [
|
|
@@ -78,7 +73,7 @@ function rectangleSides(
|
|
|
78
73
|
tolerance: number
|
|
79
74
|
): Pick<ShapeOutline, 'lines' | 'lineWeights'> {
|
|
80
75
|
const { width: w, height: h } = node
|
|
81
|
-
const [tl, tr, br, bl] =
|
|
76
|
+
const [tl, tr, br, bl] = nodeCornerRadii(node)
|
|
82
77
|
const quarter = Math.PI / 4
|
|
83
78
|
const arc = (cx: number, cy: number, r: number, from: number): Vector[] =>
|
|
84
79
|
r > 0 ? arcPoints(cx, cy, r, r, from, from + quarter, tolerance) : [{ x: cx, y: cy }]
|
|
@@ -89,38 +84,26 @@ function rectangleSides(
|
|
|
89
84
|
[...arc(w - br, h - br, br, quarter), ...arc(bl, h - bl, bl, 2 * quarter)],
|
|
90
85
|
[...arc(bl, h - bl, bl, 3 * quarter), ...arc(tl, tl, tl, 4 * quarter)]
|
|
91
86
|
],
|
|
92
|
-
lineWeights: [
|
|
93
|
-
node.borderTopWeight,
|
|
94
|
-
node.borderRightWeight,
|
|
95
|
-
node.borderBottomWeight,
|
|
96
|
-
node.borderLeftWeight
|
|
97
|
-
]
|
|
87
|
+
lineWeights: [...borderWeights(node)]
|
|
98
88
|
}
|
|
99
89
|
}
|
|
100
90
|
|
|
101
|
-
|
|
91
|
+
/** Whether a layer is an unbroken ellipse: an ELLIPSE whose arc, if any, is a whole turn with no hole. */
|
|
92
|
+
export function isWholeEllipse(node: Pick<SceneNode, 'type' | 'arcData'>): boolean {
|
|
93
|
+
if (node.type !== 'ELLIPSE') return false
|
|
94
|
+
const arc = node.arcData
|
|
95
|
+
return !arc || (arc.innerRadius <= 0 && sweepsWholeTurn(arc.startingAngle, arc.endingAngle))
|
|
96
|
+
}
|
|
102
97
|
|
|
103
98
|
function ellipseContours(node: SceneNode, tolerance: number): Vector[][] {
|
|
104
|
-
const
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
const outer = arcPoints(rx, ry, rx, ry, start, whole ? start + FULL_TURN : end, tolerance)
|
|
112
|
-
if (inner <= 0) return [whole ? outer : [{ x: rx, y: ry }, ...outer]]
|
|
113
|
-
const hole = arcPoints(
|
|
114
|
-
rx,
|
|
115
|
-
ry,
|
|
116
|
-
rx * inner,
|
|
117
|
-
ry * inner,
|
|
118
|
-
whole ? start + FULL_TURN : end,
|
|
119
|
-
start,
|
|
120
|
-
tolerance
|
|
99
|
+
const { cx, cy, contours } = arcOutline(node)
|
|
100
|
+
return contours.map((contour) =>
|
|
101
|
+
contour.flatMap((step) =>
|
|
102
|
+
step.type === 'centre'
|
|
103
|
+
? [{ x: cx, y: cy }]
|
|
104
|
+
: arcPoints(cx, cy, step.rx, step.ry, step.start, step.start + step.sweep, tolerance)
|
|
105
|
+
)
|
|
121
106
|
)
|
|
122
|
-
// A whole ring is two loops wound opposite ways; a part ring is one outline across its ends.
|
|
123
|
-
return whole ? [outer, hole] : [[...outer, ...hole]]
|
|
124
107
|
}
|
|
125
108
|
|
|
126
109
|
function vectorOutline(node: SceneNode, tolerance: number): ShapeOutline | null {
|
package/src/paints/index.ts
CHANGED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { Rect, Vector } from '../primitives'
|
|
2
|
+
import type { Fill, SceneNode } from '../types'
|
|
3
|
+
|
|
4
|
+
/** Where a pattern's source repeats in one tile: the tile, the source's scale, and its copies. */
|
|
5
|
+
export interface PatternTileLayout {
|
|
6
|
+
rect: Rect
|
|
7
|
+
scale: number
|
|
8
|
+
positions: Vector[]
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function patternAlignmentOffset(
|
|
12
|
+
alignment: Fill['horizontalAlignment'],
|
|
13
|
+
gap: number,
|
|
14
|
+
sourceSize: number,
|
|
15
|
+
axis: 'x' | 'y'
|
|
16
|
+
): number {
|
|
17
|
+
if (alignment === 'CENTER') return axis === 'x' ? -gap / 2 : -sourceSize / 2
|
|
18
|
+
if (alignment === 'END') return -gap
|
|
19
|
+
return 0
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function patternTileLayout(
|
|
23
|
+
source: Pick<SceneNode, 'width' | 'height'>,
|
|
24
|
+
fill: Fill
|
|
25
|
+
): PatternTileLayout {
|
|
26
|
+
const scale = fill.scale && fill.scale > 0 ? fill.scale : 1
|
|
27
|
+
const spacing = fill.patternSpacing ?? { x: 0, y: 0 }
|
|
28
|
+
const scaledWidth = source.width * scale
|
|
29
|
+
const scaledHeight = source.height * scale
|
|
30
|
+
const gapX = scaledWidth * spacing.x
|
|
31
|
+
const gapY = scaledHeight * spacing.y
|
|
32
|
+
const width = scaledWidth + gapX
|
|
33
|
+
const height = scaledHeight + gapY
|
|
34
|
+
const x = patternAlignmentOffset(fill.horizontalAlignment, gapX, scaledWidth, 'x')
|
|
35
|
+
const y = patternAlignmentOffset(fill.verticalAlignment, gapY, scaledHeight, 'y')
|
|
36
|
+
const positions = [{ x, y }]
|
|
37
|
+
|
|
38
|
+
if (fill.patternTileType === 'HORIZONTAL_HEXAGONAL') {
|
|
39
|
+
positions.push({ x: x + width / 2, y: y + height / 2 })
|
|
40
|
+
} else if (fill.patternTileType === 'VERTICAL_HEXAGONAL') {
|
|
41
|
+
positions.push({ x: x + width / 2, y: y - height / 2 })
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return { rect: { x: 0, y: 0, width, height }, scale, positions }
|
|
45
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Fill } from '../types'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Whether a paint shows anything: visible, above 0% opacity, and not wholly transparent in its own
|
|
5
|
+
* color or every gradient stop. Figma casts no shadow from a paint that does not, counts it in no
|
|
6
|
+
* render bounds, and does not click through it to its layer (Figma desktop, live).
|
|
7
|
+
*/
|
|
8
|
+
export function paintShows(paint: Fill): boolean {
|
|
9
|
+
if (!paint.visible || !(paint.opacity > 0)) return false
|
|
10
|
+
if (paint.gradientStops) return paint.gradientStops.some((stop) => stop.color.a > 0)
|
|
11
|
+
if (paint.type === 'SOLID') return paint.color.a > 0
|
|
12
|
+
return true
|
|
13
|
+
}
|
package/src/parse-path.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import svgpath from 'svgpath'
|
|
2
2
|
|
|
3
|
+
import type { Rect } from './primitives'
|
|
3
4
|
import type { VectorNetwork, VectorVertex, VectorSegment, VectorRegion, WindingRule } from './types'
|
|
4
5
|
|
|
5
6
|
interface SubPath {
|
|
@@ -242,3 +243,16 @@ function parsePath(
|
|
|
242
243
|
|
|
243
244
|
return { ok: true, network: { vertices, segments, regions } }
|
|
244
245
|
}
|
|
246
|
+
|
|
247
|
+
/** A `viewBox` value as a rect, or null when it is malformed or does not establish a viewport. */
|
|
248
|
+
export function parseSVGViewBoxValue(value: string | null | undefined): Rect | null {
|
|
249
|
+
if (!value) return null
|
|
250
|
+
const values = value
|
|
251
|
+
.trim()
|
|
252
|
+
.split(/[\s,]+/)
|
|
253
|
+
.map(Number)
|
|
254
|
+
if (values.length !== 4 || values.some((entry) => !Number.isFinite(entry))) return null
|
|
255
|
+
const [x = 0, y = 0, width = 0, height = 0] = values
|
|
256
|
+
if (width <= 0 || height <= 0) return null
|
|
257
|
+
return { x, y, width, height }
|
|
258
|
+
}
|
package/src/paths/arc.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { sweepsWholeTurn } from '../geometry'
|
|
2
|
+
import type { SceneNode } from '../types'
|
|
3
|
+
|
|
4
|
+
const FULL_TURN = Math.PI * 2
|
|
5
|
+
|
|
6
|
+
/** A sweep around the ellipse's centre, from `start` by `sweep` radians, negative to turn back. */
|
|
7
|
+
export interface ArcSweep {
|
|
8
|
+
type: 'arc'
|
|
9
|
+
rx: number
|
|
10
|
+
ry: number
|
|
11
|
+
start: number
|
|
12
|
+
sweep: number
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** A step to the ellipse's centre, as a pie slice's sides run. */
|
|
16
|
+
export interface ArcCentre {
|
|
17
|
+
type: 'centre'
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export type ArcStep = ArcSweep | ArcCentre
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* An ellipse or arc as the canvas, SVG export and hit testing draw it: contours of sweeps around
|
|
24
|
+
* its centre, each closed. A whole turn is one sweep, and with a hole a second sweep wound back,
|
|
25
|
+
* so a nonzero fill leaves the hole empty; a part ring is one contour whose ends run straight
|
|
26
|
+
* across the ring; a pie slice runs its sweep, then to the centre.
|
|
27
|
+
*/
|
|
28
|
+
export interface ArcOutline {
|
|
29
|
+
cx: number
|
|
30
|
+
cy: number
|
|
31
|
+
contours: ArcStep[][]
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const WHOLE_ELLIPSE = { startingAngle: 0, endingAngle: FULL_TURN, innerRadius: 0 }
|
|
35
|
+
|
|
36
|
+
export function arcOutline(node: Pick<SceneNode, 'width' | 'height' | 'arcData'>): ArcOutline {
|
|
37
|
+
const { startingAngle: start, endingAngle: end, innerRadius } = node.arcData ?? WHOLE_ELLIPSE
|
|
38
|
+
const rx = node.width / 2
|
|
39
|
+
const ry = node.height / 2
|
|
40
|
+
const whole = sweepsWholeTurn(start, end)
|
|
41
|
+
const sweep = whole ? FULL_TURN : end - start
|
|
42
|
+
const outer: ArcSweep = { type: 'arc', rx, ry, start, sweep }
|
|
43
|
+
if (innerRadius > 0) {
|
|
44
|
+
const hole: ArcSweep = {
|
|
45
|
+
type: 'arc',
|
|
46
|
+
rx: rx * innerRadius,
|
|
47
|
+
ry: ry * innerRadius,
|
|
48
|
+
start: start + sweep,
|
|
49
|
+
sweep: -sweep
|
|
50
|
+
}
|
|
51
|
+
return { cx: rx, cy: ry, contours: whole ? [[outer], [hole]] : [[outer, hole]] }
|
|
52
|
+
}
|
|
53
|
+
return { cx: rx, cy: ry, contours: [whole ? [outer] : [outer, { type: 'centre' }]] }
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Whether a sweep goes all the way round, drawn as a whole ellipse rather than an arc. */
|
|
57
|
+
export function sweepsWholeEllipse(step: ArcSweep): boolean {
|
|
58
|
+
return Math.abs(step.sweep) >= FULL_TURN
|
|
59
|
+
}
|