@open-pencil/scene-graph 0.17.0-beta.2 → 0.17.0-beta.4
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/asset-keys.d.ts +11 -1
- package/dist/asset-keys.d.ts.map +1 -1
- package/dist/asset-keys.js +16 -1
- package/dist/asset-keys.js.map +1 -1
- package/dist/checkpoint.d.ts +3 -3
- package/dist/checkpoint.d.ts.map +1 -1
- package/dist/checkpoint.js +53 -23
- package/dist/checkpoint.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 +6 -1
- package/dist/components/properties.d.ts.map +1 -1
- package/dist/components/properties.js +9 -1
- package/dist/components/properties.js.map +1 -1
- package/dist/constants.d.ts +6 -1
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +17 -1
- package/dist/constants.js.map +1 -1
- package/dist/constraints.d.ts +25 -0
- package/dist/constraints.d.ts.map +1 -0
- package/dist/constraints.js +51 -0
- package/dist/constraints.js.map +1 -0
- package/dist/coordinate.d.ts +64 -1
- package/dist/coordinate.d.ts.map +1 -1
- package/dist/coordinate.js +217 -1
- package/dist/coordinate.js.map +1 -1
- package/dist/copy.d.ts.map +1 -1
- package/dist/copy.js +8 -1
- package/dist/copy.js.map +1 -1
- package/dist/css/index.d.ts +3 -2
- package/dist/css/index.js +3 -2
- package/dist/css/text.d.ts +28 -1
- package/dist/css/text.d.ts.map +1 -1
- package/dist/css/text.js +84 -17
- package/dist/css/text.js.map +1 -1
- package/dist/css/transform.d.ts +34 -0
- package/dist/css/transform.d.ts.map +1 -0
- package/dist/css/transform.js +320 -0
- package/dist/css/transform.js.map +1 -0
- package/dist/css/values.d.ts.map +1 -1
- package/dist/css/values.js +11 -1
- package/dist/css/values.js.map +1 -1
- package/dist/figjam/palette.d.ts +1 -1
- package/dist/figjam/palette.js +1 -1
- package/dist/figjam/palette.js.map +1 -1
- package/dist/geometry.d.ts +8 -1
- package/dist/geometry.d.ts.map +1 -1
- package/dist/geometry.js +19 -8
- package/dist/geometry.js.map +1 -1
- package/dist/graph.d.ts +48 -12
- package/dist/graph.d.ts.map +1 -1
- package/dist/graph.js +110 -24
- package/dist/graph.js.map +1 -1
- package/dist/group-bounds.d.ts +14 -4
- package/dist/group-bounds.d.ts.map +1 -1
- package/dist/group-bounds.js +76 -11
- package/dist/group-bounds.js.map +1 -1
- package/dist/hit-test.d.ts +5 -0
- package/dist/hit-test.d.ts.map +1 -1
- package/dist/hit-test.js +18 -12
- package/dist/hit-test.js.map +1 -1
- package/dist/images.d.ts +27 -3
- package/dist/images.d.ts.map +1 -1
- package/dist/images.js +57 -3
- package/dist/images.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/instances/index.d.ts.map +1 -1
- package/dist/instances/index.js +3 -0
- package/dist/instances/index.js.map +1 -1
- package/dist/instances/placement.js +55 -12
- package/dist/instances/placement.js.map +1 -1
- package/dist/instances/swap.js +3 -0
- package/dist/instances/swap.js.map +1 -1
- package/dist/instances/sync.js +34 -13
- package/dist/instances/sync.js.map +1 -1
- package/dist/node-defaults.d.ts.map +1 -1
- package/dist/node-defaults.js +1 -0
- package/dist/node-defaults.js.map +1 -1
- package/dist/order-keys.d.ts +3 -1
- package/dist/order-keys.d.ts.map +1 -1
- package/dist/order-keys.js +6 -1
- package/dist/order-keys.js.map +1 -1
- package/dist/plugin-data/fields.d.ts +2 -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/resize.d.ts +22 -11
- package/dist/resize.d.ts.map +1 -1
- package/dist/resize.js +227 -93
- package/dist/resize.js.map +1 -1
- package/dist/scaling/index.d.ts +3 -2
- package/dist/scaling/index.js +3 -2
- package/dist/scaling/node.d.ts.map +1 -1
- package/dist/scaling/node.js +5 -1
- package/dist/scaling/node.js.map +1 -1
- package/dist/scaling/snapshot.d.ts +25 -0
- package/dist/scaling/snapshot.d.ts.map +1 -0
- package/dist/scaling/snapshot.js +39 -0
- package/dist/scaling/snapshot.js.map +1 -0
- package/dist/scaling/tree.d.ts +24 -2
- package/dist/scaling/tree.d.ts.map +1 -1
- package/dist/scaling/tree.js +37 -14
- package/dist/scaling/tree.js.map +1 -1
- package/dist/shaders/model.d.ts.map +1 -1
- package/dist/shaders/model.js +2 -1
- package/dist/shaders/model.js.map +1 -1
- package/dist/shared-styles.d.ts +3 -1
- package/dist/shared-styles.d.ts.map +1 -1
- package/dist/shared-styles.js +7 -1
- package/dist/shared-styles.js.map +1 -1
- package/dist/slots/authoring.d.ts +3 -2
- package/dist/slots/authoring.d.ts.map +1 -1
- package/dist/slots/authoring.js +5 -3
- package/dist/slots/authoring.js.map +1 -1
- package/dist/slots/content.d.ts.map +1 -1
- package/dist/slots/content.js +4 -1
- package/dist/slots/content.js.map +1 -1
- package/dist/text/direction.d.ts +3 -1
- package/dist/text/direction.d.ts.map +1 -1
- package/dist/text/direction.js +5 -1
- package/dist/text/direction.js.map +1 -1
- package/dist/transfer/apply.d.ts.map +1 -1
- package/dist/transfer/apply.js +1 -6
- package/dist/transfer/apply.js.map +1 -1
- package/dist/transfer/copied.d.ts +35 -3
- package/dist/transfer/copied.d.ts.map +1 -1
- package/dist/transfer/copied.js +44 -13
- package/dist/transfer/copied.js.map +1 -1
- package/dist/transfer/dependencies.d.ts +38 -0
- package/dist/transfer/dependencies.d.ts.map +1 -0
- package/dist/transfer/dependencies.js +100 -0
- package/dist/transfer/dependencies.js.map +1 -0
- package/dist/transfer/history.js +0 -1
- package/dist/transfer/history.js.map +1 -1
- package/dist/transfer/index.d.ts +5 -3
- package/dist/transfer/index.js +5 -3
- package/dist/transfer/nodes.d.ts.map +1 -1
- package/dist/transfer/nodes.js +7 -14
- package/dist/transfer/nodes.js.map +1 -1
- package/dist/transfer/plan.d.ts +0 -1
- package/dist/transfer/plan.js +3 -5
- package/dist/transfer/plan.js.map +1 -1
- package/dist/transfer/remove.js +5 -2
- package/dist/transfer/remove.js.map +1 -1
- package/dist/transfer/resources.d.ts +21 -0
- package/dist/transfer/resources.d.ts.map +1 -0
- package/dist/transfer/resources.js +38 -0
- package/dist/transfer/resources.js.map +1 -0
- package/dist/transfer/variables.d.ts +8 -1
- package/dist/transfer/variables.d.ts.map +1 -1
- package/dist/transfer/variables.js +24 -1
- package/dist/transfer/variables.js.map +1 -1
- package/dist/types.d.ts +47 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/undo.d.ts +28 -3
- package/dist/undo.d.ts.map +1 -1
- package/dist/undo.js +24 -7
- package/dist/undo.js.map +1 -1
- package/dist/variables/bindings.d.ts +17 -14
- package/dist/variables/bindings.d.ts.map +1 -1
- package/dist/variables/bindings.js +30 -72
- package/dist/variables/bindings.js.map +1 -1
- package/dist/variables/fields.d.ts +1 -1
- package/dist/variables/fields.js +1 -1
- package/dist/variables/fields.js.map +1 -1
- package/dist/variables/formula/check.d.ts +1 -1
- package/dist/variables/formula/index.d.ts +2 -2
- package/dist/variables/formula/index.js +1 -1
- package/dist/variables/formula/index.js.map +1 -1
- package/dist/variables/formula/print.d.ts +1 -1
- package/dist/variables/formula/signatures.js +1 -1
- package/dist/variables/index.d.ts +6 -6
- package/dist/variables/index.d.ts.map +1 -1
- package/dist/variables/index.js +40 -24
- package/dist/variables/index.js.map +1 -1
- package/dist/variables/paints.d.ts +57 -0
- package/dist/variables/paints.d.ts.map +1 -0
- package/dist/variables/paints.js +231 -0
- package/dist/variables/paints.js.map +1 -0
- package/dist/variables/resolution.d.ts +14 -11
- package/dist/variables/resolution.d.ts.map +1 -1
- package/dist/variables/resolution.js +36 -17
- package/dist/variables/resolution.js.map +1 -1
- package/dist/variables/subtree.js +17 -0
- package/dist/variables/subtree.js.map +1 -0
- package/dist/vector-network.d.ts.map +1 -1
- package/dist/vector-network.js +2 -8
- package/dist/vector-network.js.map +1 -1
- package/package.json +8 -1
- package/src/asset-keys.ts +20 -0
- package/src/checkpoint.ts +87 -32
- package/src/components/properties.ts +9 -0
- package/src/constants.ts +17 -0
- package/src/constraints.ts +60 -0
- package/src/coordinate.ts +220 -0
- package/src/copy.ts +9 -4
- package/src/css/index.ts +17 -1
- package/src/css/text.ts +85 -17
- package/src/css/transform.ts +266 -0
- package/src/css/values.ts +8 -0
- package/src/figjam/palette.ts +1 -1
- package/src/geometry.ts +20 -9
- package/src/graph.ts +128 -42
- package/src/group-bounds.ts +84 -17
- package/src/hit-test.ts +30 -14
- package/src/images.ts +71 -3
- package/src/instances/index.ts +4 -0
- package/src/instances/placement.ts +63 -10
- package/src/instances/swap.ts +5 -0
- package/src/instances/sync.ts +48 -21
- package/src/node-defaults.ts +1 -0
- package/src/order-keys.ts +6 -0
- package/src/plugin-data/fields.ts +6 -0
- package/src/resize.ts +267 -119
- package/src/scaling/index.ts +9 -1
- package/src/scaling/node.ts +9 -1
- package/src/scaling/snapshot.ts +51 -0
- package/src/scaling/tree.ts +63 -19
- package/src/shaders/model.ts +2 -3
- package/src/shared-styles.ts +8 -0
- package/src/slots/authoring.ts +5 -3
- package/src/slots/content.ts +4 -1
- package/src/text/direction.ts +5 -0
- package/src/transfer/apply.ts +1 -13
- package/src/transfer/copied.ts +83 -15
- package/src/transfer/dependencies.ts +126 -0
- package/src/transfer/history.ts +0 -6
- package/src/transfer/index.ts +20 -2
- package/src/transfer/nodes.ts +10 -26
- package/src/transfer/plan.ts +3 -11
- package/src/transfer/remove.ts +5 -2
- package/src/transfer/resources.ts +54 -0
- package/src/transfer/variables.ts +34 -0
- package/src/types.ts +55 -1
- package/src/undo.ts +48 -10
- package/src/variables/bindings.ts +44 -91
- package/src/variables/fields.ts +1 -1
- package/src/variables/formula/index.ts +3 -1
- package/src/variables/index.ts +56 -38
- package/src/variables/paints.ts +296 -0
- package/src/variables/resolution.ts +39 -28
- package/src/variables/subtree.ts +15 -0
- package/src/vector-network.ts +4 -2
package/src/coordinate.ts
CHANGED
|
@@ -288,3 +288,223 @@ export function getParentToContainerMatrix(
|
|
|
288
288
|
}
|
|
289
289
|
return matrix
|
|
290
290
|
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* A selected layer's place in a multi-selection's canvas bounds, taken when a resize starts so
|
|
294
|
+
* every step re-places the layer from where it began rather than from its previewed state.
|
|
295
|
+
*/
|
|
296
|
+
export interface SelectionPlacement {
|
|
297
|
+
node: SceneNode
|
|
298
|
+
center: Vector
|
|
299
|
+
/** Draws the layer from its own (0,0)…(width,height) on the canvas. */
|
|
300
|
+
world: Mat3
|
|
301
|
+
/** Draws the layer's parent on the canvas. */
|
|
302
|
+
parentWorld: Mat3
|
|
303
|
+
/** -1 when the layer is drawn mirrored, so its rotation turns the other way on the canvas. */
|
|
304
|
+
direction: 1 | -1
|
|
305
|
+
/** Maps canvas points into the layer's parent's space, or null when it cannot be inverted. */
|
|
306
|
+
parentInverse: Mat3 | null
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
export function selectionPlacement(
|
|
310
|
+
node: SceneNode,
|
|
311
|
+
graph: Pick<SceneGraph, 'getNode' | 'rootId'>
|
|
312
|
+
): SelectionPlacement {
|
|
313
|
+
const world = getWorldMatrix(node, graph)
|
|
314
|
+
const [cx, cy] = Matrix.mapPoints(world, [node.width / 2, node.height / 2])
|
|
315
|
+
const parent = node.parentId ? graph.getNode(node.parentId) : undefined
|
|
316
|
+
const parentWorld = getParentWorldMatrix(parent, graph)
|
|
317
|
+
return {
|
|
318
|
+
node: { ...node },
|
|
319
|
+
center: { x: cx, y: cy },
|
|
320
|
+
world,
|
|
321
|
+
parentWorld,
|
|
322
|
+
direction: world[0] * world[4] - world[1] * world[3] < 0 ? -1 : 1,
|
|
323
|
+
parentInverse: Matrix.invert(parentWorld)
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/** The position, in its parent's space, that puts `node`'s center at a canvas point. */
|
|
328
|
+
function positionCenteredAt(node: SceneNode, parentInverse: Mat3 | null, center: Vector): Vector {
|
|
329
|
+
const [px, py] = parentInverse
|
|
330
|
+
? Matrix.mapPoints(parentInverse, [center.x, center.y])
|
|
331
|
+
: [center.x, center.y]
|
|
332
|
+
// The local matrix translates last, so the layer's center at the origin gives the offset.
|
|
333
|
+
const [qx, qy] = Matrix.mapPoints(getNodeLocalMatrix(node, { x: 0, y: 0 }), [
|
|
334
|
+
node.width / 2,
|
|
335
|
+
node.height / 2
|
|
336
|
+
])
|
|
337
|
+
return { x: px - qx, y: py - qy }
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
export interface ResizedBoundsOptions {
|
|
341
|
+
/** The layer is locked to its ratio: it scales evenly instead of stretching. */
|
|
342
|
+
keepRatio?: boolean
|
|
343
|
+
/** Adjusts a locked layer's sizes, such as to whole pixels. */
|
|
344
|
+
round?: (size: number) => number
|
|
345
|
+
/**
|
|
346
|
+
* Lands an upright layer's edges on whole pixels from the box's top-left corner, as Figma places
|
|
347
|
+
* the layers of a selection a handle resizes while Snap to pixel grid is on.
|
|
348
|
+
*/
|
|
349
|
+
wholeEdges?: boolean
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/** Within floating-point noise of each other. */
|
|
353
|
+
const sameEdge = (a: number, b: number) => Math.abs(a - b) < 1e-6
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* `from`..`to` on the canvas with both ends a whole number of pixels from the box's `start`. An end
|
|
357
|
+
* on the box's own edge stays there, so a box that snapped to a fractional guide keeps its layers on
|
|
358
|
+
* it; a span of no length, such as a level line's height, stays at none.
|
|
359
|
+
*/
|
|
360
|
+
function wholeSpan(
|
|
361
|
+
box: { start: number; end: number },
|
|
362
|
+
from: number,
|
|
363
|
+
to: number
|
|
364
|
+
): { start: number; size: number } {
|
|
365
|
+
const land = (edge: number) =>
|
|
366
|
+
sameEdge(edge, box.end) ? box.end : box.start + Math.round(edge - box.start)
|
|
367
|
+
const start = land(from)
|
|
368
|
+
if (sameEdge(from, to)) return { start, size: 0 }
|
|
369
|
+
return { start, size: Math.max(1, land(to) - start) }
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/** Where a layer sits in its parent and how large it is, with its angle and flips. */
|
|
373
|
+
export type LayerPlacement = Pick<
|
|
374
|
+
SceneNode,
|
|
375
|
+
'x' | 'y' | 'width' | 'height' | 'rotation' | 'flipX' | 'flipY'
|
|
376
|
+
>
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* Where a layer lands, in its parent's space, when a multi-selection's canvas box `from` is
|
|
380
|
+
* resized to `to`. Figma scales the layer's whole transform with the box, which skews a rotated
|
|
381
|
+
* layer under an uneven scale. A layer here cannot skew, so it takes what Figma then reports for
|
|
382
|
+
* it, and what a `.fig` file of that result opens as: its top-left corner and x axis where the
|
|
383
|
+
* scale puts them, its width and height the scaled lengths of its own axes, and its y axis square
|
|
384
|
+
* to its x axis. An even scale, or a layer upright in the box, comes out exactly as in Figma.
|
|
385
|
+
*/
|
|
386
|
+
export function placeInResizedBounds(
|
|
387
|
+
placement: SelectionPlacement,
|
|
388
|
+
from: Rect,
|
|
389
|
+
to: Rect,
|
|
390
|
+
{
|
|
391
|
+
keepRatio = false,
|
|
392
|
+
round = (size: number) => size,
|
|
393
|
+
wholeEdges = false
|
|
394
|
+
}: ResizedBoundsOptions = {}
|
|
395
|
+
): LayerPlacement {
|
|
396
|
+
const { node, world, parentWorld } = placement
|
|
397
|
+
const scaleX = from.width > 0 ? to.width / from.width : 1
|
|
398
|
+
const scaleY = from.height > 0 ? to.height / from.height : 1
|
|
399
|
+
const scale = Matrix.multiply(
|
|
400
|
+
Matrix.translated(to.x - from.x * scaleX, to.y - from.y * scaleY),
|
|
401
|
+
Matrix.scaled(scaleX, scaleY)
|
|
402
|
+
)
|
|
403
|
+
const [a, b, tx, c, d, ty] = Matrix.multiply(scale, world)
|
|
404
|
+
// The scaled axes: x along the first column, y along the second.
|
|
405
|
+
const xLength = Math.hypot(a, c)
|
|
406
|
+
const yLength = Math.hypot(b, d)
|
|
407
|
+
// A layer locked to its ratio scales evenly, by the mean of the box's scales, as in Figma.
|
|
408
|
+
const even = Math.sqrt(Math.abs(scaleX * scaleY))
|
|
409
|
+
let width = keepRatio ? round(node.width * even) : node.width * xLength
|
|
410
|
+
let height = keepRatio ? round(node.height * even) : node.height * yLength
|
|
411
|
+
const ux = xLength > 0 ? a / xLength : 1
|
|
412
|
+
const uy = xLength > 0 ? c / xLength : 0
|
|
413
|
+
const mirrored = a * d - b * c < 0 ? -1 : 1
|
|
414
|
+
let [left, top] = [tx, ty]
|
|
415
|
+
const upright = Math.abs(uy) < 1e-9 && ux > 0 && mirrored > 0
|
|
416
|
+
if (wholeEdges && !keepRatio && upright) {
|
|
417
|
+
const across = wholeSpan({ start: to.x, end: to.x + to.width }, tx, tx + width)
|
|
418
|
+
const down = wholeSpan({ start: to.y, end: to.y + to.height }, ty, ty + height)
|
|
419
|
+
;[left, width, top, height] = [across.start, across.size, down.start, down.size]
|
|
420
|
+
}
|
|
421
|
+
const squared: Mat3 = [ux, -uy * mirrored, left, uy, ux * mirrored, top, 0, 0, 1]
|
|
422
|
+
const sized = { ...node, width, height }
|
|
423
|
+
const { x, y, rotation, flipX, flipY } =
|
|
424
|
+
localTransformFromWorld(sized, squared, parentWorld) ?? node
|
|
425
|
+
return { x, y, width, height, rotation, flipX, flipY }
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* Where a layer lands, in its parent's space, when it scales evenly by `scale` about a canvas
|
|
430
|
+
* point, as Figma's Scale tool scales it: its angle and flips stay, and the point stays put.
|
|
431
|
+
*/
|
|
432
|
+
export function placeScaledAbout(
|
|
433
|
+
placement: SelectionPlacement,
|
|
434
|
+
anchor: Vector,
|
|
435
|
+
scale: number
|
|
436
|
+
): LayerPlacement {
|
|
437
|
+
const from = { x: anchor.x, y: anchor.y, width: 1, height: 1 }
|
|
438
|
+
return placeInResizedBounds(placement, from, { ...from, width: scale, height: scale })
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/** A canvas pointer movement along the layer's own axes, as its handles measure it. */
|
|
442
|
+
export function canvasDeltaInLayer(placement: SelectionPlacement, dx: number, dy: number): Vector {
|
|
443
|
+
const [a, b, , c, d] = placement.world
|
|
444
|
+
const det = a * d - b * c
|
|
445
|
+
if (det === 0) return { x: dx, y: dy }
|
|
446
|
+
return { x: (d * dx - b * dy) / det, y: (a * dy - c * dx) / det }
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* Where a layer lands when one of its handles resizes it in its own frame, from `from` to `to`,
|
|
451
|
+
* both its box in its parent's axes as an upright layer would have it. `to` is signed: a handle
|
|
452
|
+
* dragged past the opposite edge gives a negative size and mirrors the layer, as Figma flips it.
|
|
453
|
+
* The edges the handle leaves stay where they are on the canvas, whatever the layer's angle.
|
|
454
|
+
*/
|
|
455
|
+
export function placeInResizedFrame(
|
|
456
|
+
placement: SelectionPlacement,
|
|
457
|
+
from: Rect,
|
|
458
|
+
to: Rect
|
|
459
|
+
): LayerPlacement {
|
|
460
|
+
const { node, world, parentWorld } = placement
|
|
461
|
+
const width = Math.abs(to.width)
|
|
462
|
+
const height = Math.abs(to.height)
|
|
463
|
+
const mirrorX = to.width < 0
|
|
464
|
+
const mirrorY = to.height < 0
|
|
465
|
+
const turned = node.rotation !== 0 || node.flipX || node.flipY
|
|
466
|
+
if (!turned && !mirrorX && !mirrorY) {
|
|
467
|
+
return { x: to.x, y: to.y, width, height, rotation: 0, flipX: false, flipY: false }
|
|
468
|
+
}
|
|
469
|
+
const placed = Matrix.multiply(
|
|
470
|
+
Matrix.multiply(world, Matrix.translated(to.x - from.x, to.y - from.y)),
|
|
471
|
+
Matrix.scaled(mirrorX ? -1 : 1, mirrorY ? -1 : 1)
|
|
472
|
+
)
|
|
473
|
+
const local = localTransformFromWorld({ ...node, width, height }, placed, parentWorld)
|
|
474
|
+
if (!local)
|
|
475
|
+
return { ...to, width, height, rotation: node.rotation, flipX: node.flipX, flipY: node.flipY }
|
|
476
|
+
// Unmirrored, the angle and flips stay exactly as they were; only the position follows.
|
|
477
|
+
return mirrorX || mirrorY
|
|
478
|
+
? { ...local, width, height }
|
|
479
|
+
: {
|
|
480
|
+
x: local.x,
|
|
481
|
+
y: local.y,
|
|
482
|
+
width,
|
|
483
|
+
height,
|
|
484
|
+
rotation: node.rotation,
|
|
485
|
+
flipX: node.flipX,
|
|
486
|
+
flipY: node.flipY
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
/**
|
|
491
|
+
* Where a layer lands, in its parent's space, when a multi-selection turns `degrees` about a
|
|
492
|
+
* canvas pivot, as Figma rotates a selection about its bounds' center: the layer's center swings
|
|
493
|
+
* around the pivot and the layer itself turns by the same angle on the canvas.
|
|
494
|
+
*/
|
|
495
|
+
export function placeRotatedAbout(
|
|
496
|
+
placement: SelectionPlacement,
|
|
497
|
+
pivot: Vector,
|
|
498
|
+
degrees: number
|
|
499
|
+
): { x: number; y: number; rotation: number } {
|
|
500
|
+
const { node, center, direction, parentInverse } = placement
|
|
501
|
+
const radians = (degrees * Math.PI) / 180
|
|
502
|
+
const cos = Math.cos(radians)
|
|
503
|
+
const sin = Math.sin(radians)
|
|
504
|
+
const dx = center.x - pivot.x
|
|
505
|
+
const dy = center.y - pivot.y
|
|
506
|
+
const target = { x: pivot.x + dx * cos - dy * sin, y: pivot.y + dx * sin + dy * cos }
|
|
507
|
+
const turned = node.rotation + degrees * direction
|
|
508
|
+
const rotation = ((((turned + 180) % 360) + 360) % 360) - 180
|
|
509
|
+
return { ...positionCenteredAt({ ...node, rotation }, parentInverse, target), rotation }
|
|
510
|
+
}
|
package/src/copy.ts
CHANGED
|
@@ -50,11 +50,16 @@ export function copyStroke(s: Stroke): Stroke {
|
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
export function copyEffect(e: Effect): Effect {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
53
|
+
const copy: Effect = { ...e, color: { ...e.color }, offset: { ...e.offset } }
|
|
54
|
+
if (e.glass) copy.glass = { ...e.glass }
|
|
55
|
+
if (e.progressive) {
|
|
56
|
+
copy.progressive = {
|
|
57
|
+
startRadius: e.progressive.startRadius,
|
|
58
|
+
startOffset: { ...e.progressive.startOffset },
|
|
59
|
+
endOffset: { ...e.progressive.endOffset }
|
|
60
|
+
}
|
|
57
61
|
}
|
|
62
|
+
return copy
|
|
58
63
|
}
|
|
59
64
|
|
|
60
65
|
export function copyStyleRun(r: StyleRun): StyleRun {
|
package/src/css/index.ts
CHANGED
|
@@ -7,5 +7,21 @@ export {
|
|
|
7
7
|
parseCSSJustifyContent
|
|
8
8
|
} from './layout'
|
|
9
9
|
export { parseCSSShadows } from './shadows'
|
|
10
|
-
export {
|
|
10
|
+
export {
|
|
11
|
+
CSS_GENERIC_FONT_FAMILIES,
|
|
12
|
+
cssFontFamilyName,
|
|
13
|
+
parseCSSFontFamilies,
|
|
14
|
+
parseCSSFontFamily,
|
|
15
|
+
parseCSSLetterSpacing,
|
|
16
|
+
parseCSSLineHeight,
|
|
17
|
+
resolveCSSFontFamily,
|
|
18
|
+
type CSSFontFamily
|
|
19
|
+
} from './text'
|
|
20
|
+
export {
|
|
21
|
+
parseCSSTransform,
|
|
22
|
+
parseCSSTransformOrigin,
|
|
23
|
+
parseCSSTransforms,
|
|
24
|
+
type CSSTransformBox,
|
|
25
|
+
type CSSTransformStyle
|
|
26
|
+
} from './transform'
|
|
11
27
|
export { parseCSSColor, parseCSSNumber, splitCSSValue } from './values'
|
package/src/css/text.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import { DEFAULT_FONT_FAMILY } from '../constants'
|
|
3
2
|
import { percentLength, pixelLength } from '../text/lengths'
|
|
4
3
|
import type { TextLength } from '../types'
|
|
5
|
-
import { parseCSSNumber } from './values'
|
|
4
|
+
import { dimension, parseCSSNumber } from './values'
|
|
6
5
|
|
|
7
6
|
/** A CSS escape: hex digits and one optional whitespace, an escaped line break, or any one character. */
|
|
8
7
|
const CSS_ESCAPE = /\\(?:([0-9a-fA-F]{1,6})(?:\r\n|[ \t\n\r\f])?|(?:\r\n|[\n\r\f])|([\s\S]))?/gu
|
|
@@ -29,28 +28,97 @@ function unescapedIndexOf(text: string, stop: string, from: number): number {
|
|
|
29
28
|
return index
|
|
30
29
|
}
|
|
31
30
|
|
|
31
|
+
/** A family of a CSS `font-family` list: its name, and whether it was a quoted string. */
|
|
32
|
+
export interface CSSFontFamily {
|
|
33
|
+
name: string
|
|
34
|
+
quoted: boolean
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** The family at the start of `text` and where the next one starts, or null when none is left. */
|
|
38
|
+
function nextFontFamily(text: string): { family: CSSFontFamily | null; rest: string } | null {
|
|
39
|
+
const trimmed = text.trimStart()
|
|
40
|
+
if (!trimmed) return null
|
|
41
|
+
const quote = trimmed[0]
|
|
42
|
+
if (quote === '"' || quote === "'") {
|
|
43
|
+
const end = unescapedIndexOf(trimmed, quote, 1)
|
|
44
|
+
const name = decodeCSSEscapes(trimmed.slice(1, end))
|
|
45
|
+
const comma = unescapedIndexOf(trimmed, ',', end)
|
|
46
|
+
return { family: name ? { name, quoted: true } : null, rest: trimmed.slice(comma + 1) }
|
|
47
|
+
}
|
|
48
|
+
const comma = unescapedIndexOf(trimmed, ',', 0)
|
|
49
|
+
const name = decodeCSSEscapes(trimmed.slice(0, comma)).trim().replace(/\s+/gu, ' ')
|
|
50
|
+
return { family: name ? { name, quoted: false } : null, rest: trimmed.slice(comma + 1) }
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Every family of a CSS `font-family` list in order, without quotes and escapes, as
|
|
55
|
+
* `parseCSSFontFamily` reads the first.
|
|
56
|
+
*/
|
|
57
|
+
export function parseCSSFontFamilies(value: string | undefined): CSSFontFamily[] {
|
|
58
|
+
const families: CSSFontFamily[] = []
|
|
59
|
+
for (let next = nextFontFamily(value ?? ''); next; next = nextFontFamily(next.rest)) {
|
|
60
|
+
if (next.family) families.push(next.family)
|
|
61
|
+
}
|
|
62
|
+
return families
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function firstFontFamily(value: string | undefined): CSSFontFamily | null {
|
|
66
|
+
return parseCSSFontFamilies(value)[0] ?? null
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The real family a family of a `font-family` list names: a generic one, such as `sans-serif`,
|
|
71
|
+
* mapped to a real one, and any other name as it is. A quoted name is never a generic.
|
|
72
|
+
*/
|
|
73
|
+
export function cssFontFamilyName(family: CSSFontFamily): string {
|
|
74
|
+
if (family.quoted) return family.name
|
|
75
|
+
return CSS_GENERIC_FONT_FAMILIES[family.name.toLowerCase()] ?? family.name
|
|
76
|
+
}
|
|
77
|
+
|
|
32
78
|
/**
|
|
33
79
|
* The first family of a CSS `font-family` list, without its quotes and escapes. A family is a quoted
|
|
34
80
|
* string or unquoted words, which join with single spaces; a quote opens a string only at the start
|
|
35
81
|
* of the family, so a name list such as draw.io's keeps `O'Reilly`. Null for an empty list.
|
|
36
82
|
*/
|
|
37
83
|
export function parseCSSFontFamily(value: string | undefined): string | null {
|
|
38
|
-
|
|
39
|
-
if (!text) return null
|
|
40
|
-
const quote = text[0]
|
|
41
|
-
if (quote === '"' || quote === "'") {
|
|
42
|
-
return decodeCSSEscapes(text.slice(1, unescapedIndexOf(text, quote, 1))) || null
|
|
43
|
-
}
|
|
44
|
-
const words = decodeCSSEscapes(text.slice(0, unescapedIndexOf(text, ',', 0)))
|
|
45
|
-
return words.trim().replace(/\s+/gu, ' ') || null
|
|
84
|
+
return firstFontFamily(value)?.name ?? null
|
|
46
85
|
}
|
|
47
86
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
87
|
+
const SANS_SERIF_FAMILY = DEFAULT_FONT_FAMILY
|
|
88
|
+
/** The families FigJam sets its Bookish and Technical typefaces in, which every font provider serves. */
|
|
89
|
+
const SERIF_FAMILY = 'Merriweather'
|
|
90
|
+
const MONOSPACE_FAMILY = 'Roboto Mono'
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Real families for CSS generic families, which name no font but whatever the browser picks for
|
|
94
|
+
* them, and for `-apple-system` and `BlinkMacSystemFont`, which browsers read as `system-ui`.
|
|
95
|
+
* Keyed by lowercase name; the rest of the generics, such as `cursive`, draw in the sans-serif one.
|
|
96
|
+
*/
|
|
97
|
+
export const CSS_GENERIC_FONT_FAMILIES: Readonly<Record<string, string>> = {
|
|
98
|
+
serif: SERIF_FAMILY,
|
|
99
|
+
'ui-serif': SERIF_FAMILY,
|
|
100
|
+
monospace: MONOSPACE_FAMILY,
|
|
101
|
+
'ui-monospace': MONOSPACE_FAMILY,
|
|
102
|
+
'sans-serif': SANS_SERIF_FAMILY,
|
|
103
|
+
'ui-sans-serif': SANS_SERIF_FAMILY,
|
|
104
|
+
'ui-rounded': SANS_SERIF_FAMILY,
|
|
105
|
+
'system-ui': SANS_SERIF_FAMILY,
|
|
106
|
+
'-apple-system': SANS_SERIF_FAMILY,
|
|
107
|
+
blinkmacsystemfont: SANS_SERIF_FAMILY,
|
|
108
|
+
cursive: SANS_SERIF_FAMILY,
|
|
109
|
+
fantasy: SANS_SERIF_FAMILY,
|
|
110
|
+
math: SANS_SERIF_FAMILY,
|
|
111
|
+
fangsong: SANS_SERIF_FAMILY
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* The family a CSS `font-family` list sets text in: its first family, with a generic family
|
|
116
|
+
* such as `sans-serif` or `system-ui` mapped to a real one. A quoted name is a family's own name,
|
|
117
|
+
* never a generic. Null for an empty list.
|
|
118
|
+
*/
|
|
119
|
+
export function resolveCSSFontFamily(value: string | undefined): string | null {
|
|
120
|
+
const family = firstFontFamily(value)
|
|
121
|
+
return family ? cssFontFamilyName(family) : null
|
|
54
122
|
}
|
|
55
123
|
|
|
56
124
|
/**
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import valueParser from 'postcss-value-parser'
|
|
2
|
+
|
|
3
|
+
import Matrix, { type Mat3 } from '../matrix'
|
|
4
|
+
import type { Vector } from '../primitives'
|
|
5
|
+
import { dimension, splitCSSValue } from './values'
|
|
6
|
+
|
|
7
|
+
/** The size percentages in a transform resolve against: the element's border box. */
|
|
8
|
+
export interface CSSTransformBox {
|
|
9
|
+
width: number
|
|
10
|
+
height: number
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** The properties that transform an element, as CSS applies them: translate, rotate, scale, transform. */
|
|
14
|
+
export interface CSSTransformStyle {
|
|
15
|
+
transform?: string
|
|
16
|
+
translate?: string
|
|
17
|
+
rotate?: string
|
|
18
|
+
scale?: string
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const ANGLE_UNITS: Partial<Record<string, number>> = {
|
|
22
|
+
deg: Math.PI / 180,
|
|
23
|
+
rad: 1,
|
|
24
|
+
grad: Math.PI / 200,
|
|
25
|
+
turn: Math.PI * 2
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** An angle in radians; a bare zero counts, as CSS allows. Null for anything else. */
|
|
29
|
+
function angle(value: string): number | null {
|
|
30
|
+
const parsed = dimension(value.trim())
|
|
31
|
+
if (!parsed) return null
|
|
32
|
+
if (parsed.unit === '') return parsed.number === 0 ? 0 : null
|
|
33
|
+
const radians = ANGLE_UNITS[parsed.unit]
|
|
34
|
+
return radians === undefined ? null : parsed.number * radians
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** A length in pixels, a percentage of `extent`, or null for units this cannot resolve. */
|
|
38
|
+
function length(value: string, extent: number): number | null {
|
|
39
|
+
const parsed = dimension(value.trim())
|
|
40
|
+
if (!parsed) return null
|
|
41
|
+
if (parsed.unit === '%') return (parsed.number / 100) * extent
|
|
42
|
+
return parsed.unit === 'px' || (parsed.unit === '' && parsed.number === 0) ? parsed.number : null
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** A plain number, or a percentage as a fraction, as `scale` takes them. */
|
|
46
|
+
function factor(value: string): number | null {
|
|
47
|
+
const parsed = dimension(value.trim())
|
|
48
|
+
if (!parsed) return null
|
|
49
|
+
if (parsed.unit === '%') return parsed.number / 100
|
|
50
|
+
return parsed.unit === '' ? parsed.number : null
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function skewed(x: number, y: number): Mat3 {
|
|
54
|
+
return [1, Math.tan(x), 0, Math.tan(y), 1, 0, 0, 0, 1]
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** The 2D matrix `matrix(a, b, c, d, e, f)` names. */
|
|
58
|
+
function affine([a, b, c, d, e, f]: number[]): Mat3 {
|
|
59
|
+
return [a, c, e, b, d, f, 0, 0, 1]
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function translation(x: string, y: string | undefined, box: CSSTransformBox): Mat3 | null {
|
|
63
|
+
const dx = length(x, box.width)
|
|
64
|
+
const dy = y === undefined ? 0 : length(y, box.height)
|
|
65
|
+
return dx === null || dy === null ? null : Matrix.translated(dx, dy)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function scaling(x: string, y: string | undefined): Mat3 | null {
|
|
69
|
+
const sx = factor(x)
|
|
70
|
+
const sy = y === undefined ? sx : factor(y)
|
|
71
|
+
return sx === null || sy === null ? null : Matrix.scaled(sx, sy)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function numbers(values: string[]): number[] | null {
|
|
75
|
+
const parsed = values.map(Number)
|
|
76
|
+
return parsed.every(Number.isFinite) ? parsed : null
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function rotation(value: string | undefined): Mat3 | null {
|
|
80
|
+
const radians = value === undefined ? null : angle(value)
|
|
81
|
+
return radians === null ? null : Matrix.rotated(radians)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function skewing(x: string | undefined, y: string | undefined): Mat3 | null {
|
|
85
|
+
const ax = x === undefined ? 0 : angle(x)
|
|
86
|
+
const ay = y === undefined ? 0 : angle(y)
|
|
87
|
+
return ax === null || ay === null ? null : skewed(ax, ay)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** A transform function: how many arguments it takes, and the matrix they make. */
|
|
91
|
+
interface TransformFunction {
|
|
92
|
+
arity: readonly [min: number, max: number]
|
|
93
|
+
build: (args: string[], box: CSSTransformBox) => Mat3 | null
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** The 2D transform functions by lowercase name; 3D turns and perspective are left out. */
|
|
97
|
+
const TRANSFORM_FUNCTIONS: Partial<Record<string, TransformFunction>> = {
|
|
98
|
+
matrix: {
|
|
99
|
+
arity: [6, 6],
|
|
100
|
+
build: (args) => {
|
|
101
|
+
const m = numbers(args)
|
|
102
|
+
return m && affine(m)
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
matrix3d: {
|
|
106
|
+
arity: [16, 16],
|
|
107
|
+
build: (args) => {
|
|
108
|
+
const m = numbers(args)
|
|
109
|
+
return m && inPlane(m) ? affine([m[0], m[1], m[4], m[5], m[12], m[13]]) : null
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
translate: { arity: [1, 2], build: (args, box) => translation(args[0], args.at(1), box) },
|
|
113
|
+
translate3d: {
|
|
114
|
+
arity: [3, 3],
|
|
115
|
+
build: (args, box) => (depth(args[2]) ? translation(args[0], args[1], box) : null)
|
|
116
|
+
},
|
|
117
|
+
translatex: { arity: [1, 1], build: (args, box) => translation(args[0], undefined, box) },
|
|
118
|
+
translatey: { arity: [1, 1], build: (args, box) => translation('0', args[0], box) },
|
|
119
|
+
rotate: { arity: [1, 1], build: (args) => rotation(args[0]) },
|
|
120
|
+
rotatez: { arity: [1, 1], build: (args) => rotation(args[0]) },
|
|
121
|
+
scale: { arity: [1, 2], build: (args) => scaling(args[0], args.at(1)) },
|
|
122
|
+
scale3d: {
|
|
123
|
+
arity: [3, 3],
|
|
124
|
+
build: (args) => (factor(args[2]) === null ? null : scaling(args[0], args[1]))
|
|
125
|
+
},
|
|
126
|
+
scalex: { arity: [1, 1], build: (args) => scaling(args[0], '1') },
|
|
127
|
+
scaley: { arity: [1, 1], build: (args) => scaling('1', args[0]) },
|
|
128
|
+
skew: { arity: [1, 2], build: (args) => skewing(args[0], args.at(1)) },
|
|
129
|
+
skewx: { arity: [1, 1], build: (args) => skewing(args[0], undefined) },
|
|
130
|
+
skewy: { arity: [1, 1], build: (args) => skewing(undefined, args[0]) }
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Whether a `matrix3d`, column by column, keeps the plane where it was: no turn out of it and no
|
|
135
|
+
* perspective. Depth, which moves nothing on the plane without perspective, may change.
|
|
136
|
+
*/
|
|
137
|
+
function inPlane(m: number[]): boolean {
|
|
138
|
+
const outOfPlane = [m[2], m[3], m[6], m[7], m[8], m[9], m[11]]
|
|
139
|
+
return outOfPlane.every((value) => value === 0) && m[15] === 1
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Whether a z offset is a length, which a percentage never is. It moves nothing on the plane without perspective, which a
|
|
144
|
+
* layer cannot hold, so it is read and dropped.
|
|
145
|
+
*/
|
|
146
|
+
function depth(value: string): boolean {
|
|
147
|
+
return !value.trim().endsWith('%') && length(value, 0) !== null
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* A function's arguments, which CSS separates with commas: null when one is missing, or two
|
|
152
|
+
* stand without a comma between them.
|
|
153
|
+
*/
|
|
154
|
+
function functionArguments(nodes: valueParser.Node[]): string[] | null {
|
|
155
|
+
const parts = nodes.filter((node) => node.type !== 'space' && node.type !== 'comment')
|
|
156
|
+
const args: string[] = []
|
|
157
|
+
for (const [index, node] of parts.entries()) {
|
|
158
|
+
const expectsArgument = index % 2 === 0
|
|
159
|
+
if (expectsArgument && node.type !== 'word') return null
|
|
160
|
+
if (!expectsArgument && (node.type !== 'div' || node.value !== ',')) return null
|
|
161
|
+
if (expectsArgument) args.push(node.value)
|
|
162
|
+
}
|
|
163
|
+
return parts.length % 2 === 1 ? args : null
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** One transform function as a matrix; null for one this does not know or cannot read. */
|
|
167
|
+
function transformFunction(name: string, args: string[], box: CSSTransformBox): Mat3 | null {
|
|
168
|
+
// Own names only, so `constructor` or `__proto__` reads as unknown instead of a prototype member.
|
|
169
|
+
const fn = Object.hasOwn(TRANSFORM_FUNCTIONS, name) ? TRANSFORM_FUNCTIONS[name] : undefined
|
|
170
|
+
if (!fn || args.length < fn.arity[0] || args.length > fn.arity[1]) return null
|
|
171
|
+
return fn.build(args, box)
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* A CSS `transform` as a 2D matrix about the transform origin, with percentages resolved against
|
|
176
|
+
* `box`. The identity for `none`; null when it turns out of the plane or cannot be read.
|
|
177
|
+
*/
|
|
178
|
+
export function parseCSSTransform(value: string | undefined, box: CSSTransformBox): Mat3 | null {
|
|
179
|
+
const text = value?.trim()
|
|
180
|
+
if (!text || text === 'none') return Matrix.identity()
|
|
181
|
+
let matrix = Matrix.identity()
|
|
182
|
+
for (const node of valueParser(text).nodes) {
|
|
183
|
+
if (node.type === 'space' || node.type === 'comment') continue
|
|
184
|
+
if (node.type !== 'function') return null
|
|
185
|
+
const args = functionArguments(node.nodes)
|
|
186
|
+
const step = args && transformFunction(node.value.toLowerCase(), args, box)
|
|
187
|
+
if (!step) return null
|
|
188
|
+
matrix = Matrix.multiply(matrix, step)
|
|
189
|
+
}
|
|
190
|
+
return matrix
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** The `rotate` property: an angle, or one about the z axis. Null when it turns out of the plane. */
|
|
194
|
+
function rotateProperty(parts: string[]): Mat3 | null {
|
|
195
|
+
const last = parts.at(-1)
|
|
196
|
+
const axis = parts.slice(0, -1).join(' ').toLowerCase()
|
|
197
|
+
const radians = last === undefined ? null : angle(last)
|
|
198
|
+
if (radians === null || !['', 'z', '0 0 1'].includes(axis)) return null
|
|
199
|
+
return Matrix.rotated(radians)
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** A transform property's value as a matrix; the identity when it is unset or `none`. */
|
|
203
|
+
function property(value: string | undefined, parse: (parts: string[]) => Mat3 | null): Mat3 | null {
|
|
204
|
+
const text = value?.trim()
|
|
205
|
+
if (!text || text === 'none') return Matrix.identity()
|
|
206
|
+
return parse(splitCSSValue(text))
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* The 2D matrix about the transform origin that an element's `translate`, `rotate`, `scale`, and
|
|
211
|
+
* `transform` make together, applied in that order as CSS applies them. Null when one of them
|
|
212
|
+
* turns out of the plane or cannot be read.
|
|
213
|
+
*/
|
|
214
|
+
export function parseCSSTransforms(style: CSSTransformStyle, box: CSSTransformBox): Mat3 | null {
|
|
215
|
+
const steps = [
|
|
216
|
+
property(style.translate, (parts) => {
|
|
217
|
+
const [x, y, z] = [parts.at(0), parts.at(1), parts.at(2)]
|
|
218
|
+
if (x === undefined || parts.length > 3 || (z !== undefined && !depth(z))) return null
|
|
219
|
+
return translation(x, y, box)
|
|
220
|
+
}),
|
|
221
|
+
property(style.rotate, rotateProperty),
|
|
222
|
+
property(style.scale, (parts) => {
|
|
223
|
+
const [x, y, z] = [parts.at(0), parts.at(1), parts.at(2)]
|
|
224
|
+
if (x === undefined || parts.length > 3 || (z !== undefined && factor(z) === null)) {
|
|
225
|
+
return null
|
|
226
|
+
}
|
|
227
|
+
return scaling(x, y)
|
|
228
|
+
}),
|
|
229
|
+
parseCSSTransform(style.transform, box)
|
|
230
|
+
]
|
|
231
|
+
if (steps.some((step) => step === null)) return null
|
|
232
|
+
return Matrix.multiply(...steps.filter((step) => step !== null))
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const ORIGIN_KEYWORDS: Partial<Record<string, { axis: 'x' | 'y' | 'either'; fraction: number }>> = {
|
|
236
|
+
left: { axis: 'x', fraction: 0 },
|
|
237
|
+
right: { axis: 'x', fraction: 1 },
|
|
238
|
+
top: { axis: 'y', fraction: 0 },
|
|
239
|
+
bottom: { axis: 'y', fraction: 1 },
|
|
240
|
+
center: { axis: 'either', fraction: 0.5 }
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Where a CSS `transform-origin` puts the point an element transforms about, from its border
|
|
245
|
+
* box's top left corner. Its center when it is unset or cannot be read.
|
|
246
|
+
*/
|
|
247
|
+
export function parseCSSTransformOrigin(value: string | undefined, box: CSSTransformBox): Vector {
|
|
248
|
+
const center = { x: box.width / 2, y: box.height / 2 }
|
|
249
|
+
const tokens = splitCSSValue(value?.trim() ?? '')
|
|
250
|
+
// A third value is the depth, which must be a length; anything past it is not an origin.
|
|
251
|
+
const depthOk = tokens.length < 3 || (tokens.length === 3 && depth(tokens[2]))
|
|
252
|
+
if (tokens.length === 0 || !depthOk) return center
|
|
253
|
+
const parts = tokens.slice(0, 2)
|
|
254
|
+
// A vertical keyword first, as in `top left`, names the y offset.
|
|
255
|
+
const [first = 'center', second = 'center'] = parts.map((part) => part.toLowerCase())
|
|
256
|
+
const swapped = ORIGIN_KEYWORDS[first]?.axis === 'y' || ORIGIN_KEYWORDS[second]?.axis === 'x'
|
|
257
|
+
const [x, y] = swapped ? [second, first] : [first, second]
|
|
258
|
+
const resolve = (part: string, extent: number, axis: 'x' | 'y') => {
|
|
259
|
+
const keyword = ORIGIN_KEYWORDS[part]
|
|
260
|
+
if (!keyword) return length(part, extent)
|
|
261
|
+
return keyword.axis === axis || keyword.axis === 'either' ? keyword.fraction * extent : null
|
|
262
|
+
}
|
|
263
|
+
const ox = resolve(x, box.width, 'x')
|
|
264
|
+
const oy = resolve(y, box.height, 'y')
|
|
265
|
+
return ox === null || oy === null ? center : { x: ox, y: oy }
|
|
266
|
+
}
|
package/src/css/values.ts
CHANGED
|
@@ -3,6 +3,14 @@ import valueParser from 'postcss-value-parser'
|
|
|
3
3
|
import { tryParseColor } from '../color'
|
|
4
4
|
import type { Color } from '../primitives'
|
|
5
5
|
|
|
6
|
+
/** A CSS dimension's number and lowercase unit, or null. */
|
|
7
|
+
export function dimension(value: string): { number: number; unit: string } | null {
|
|
8
|
+
const parsed = valueParser.unit(value)
|
|
9
|
+
if (!parsed) return null
|
|
10
|
+
const number = Number(parsed.number)
|
|
11
|
+
return Number.isFinite(number) ? { number, unit: parsed.unit.toLowerCase() } : null
|
|
12
|
+
}
|
|
13
|
+
|
|
6
14
|
/** Pixels in a rem, as browsers use by default. */
|
|
7
15
|
const ROOT_FONT_SIZE = 16
|
|
8
16
|
|
package/src/figjam/palette.ts
CHANGED
|
@@ -248,7 +248,7 @@ export function figJamOutlineColorName(color: Color | undefined): FigJamColorNam
|
|
|
248
248
|
* How FigJam draws a section's title on a board, measured in FigJam: a pill in the colour of the
|
|
249
249
|
* outline drawn on top, with white text on the palette's darkest colours and black on the rest;
|
|
250
250
|
* null for a section without an outline from the palette. `colorOf` reads an outline's colour as
|
|
251
|
-
* drawn, such as a variable's value in the
|
|
251
|
+
* drawn, such as a variable's value in the section's mode.
|
|
252
252
|
*/
|
|
253
253
|
export function figJamSectionTitleColors(
|
|
254
254
|
section: Pick<SceneNode, 'strokes'>,
|