@open-pencil/scene-graph 0.17.0-beta.4 → 0.17.0-beta.5

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.
Files changed (283) hide show
  1. package/dist/arrays.d.ts +17 -0
  2. package/dist/arrays.d.ts.map +1 -0
  3. package/dist/arrays.js +38 -0
  4. package/dist/arrays.js.map +1 -0
  5. package/dist/buffered-events.js +1 -1
  6. package/dist/buffered-events.js.map +1 -1
  7. package/dist/clipping.d.ts +7 -1
  8. package/dist/clipping.d.ts.map +1 -1
  9. package/dist/clipping.js +23 -19
  10. package/dist/clipping.js.map +1 -1
  11. package/dist/components/index.d.ts +2 -2
  12. package/dist/components/index.js +2 -2
  13. package/dist/components/properties.d.ts +1 -3
  14. package/dist/components/properties.d.ts.map +1 -1
  15. package/dist/components/properties.js +2 -9
  16. package/dist/components/properties.js.map +1 -1
  17. package/dist/corners.d.ts +22 -0
  18. package/dist/corners.d.ts.map +1 -0
  19. package/dist/corners.js +52 -0
  20. package/dist/corners.js.map +1 -0
  21. package/dist/css/grid.d.ts +4 -1
  22. package/dist/css/grid.d.ts.map +1 -1
  23. package/dist/css/grid.js +16 -7
  24. package/dist/css/grid.js.map +1 -1
  25. package/dist/css/index.d.ts +2 -2
  26. package/dist/css/index.js +2 -2
  27. package/dist/css/values.d.ts +6 -1
  28. package/dist/css/values.d.ts.map +1 -1
  29. package/dist/font-style.d.ts +2 -1
  30. package/dist/font-style.d.ts.map +1 -1
  31. package/dist/font-style.js +31 -6
  32. package/dist/font-style.js.map +1 -1
  33. package/dist/geometry.d.ts +68 -3
  34. package/dist/geometry.d.ts.map +1 -1
  35. package/dist/geometry.js +155 -5
  36. package/dist/geometry.js.map +1 -1
  37. package/dist/gradient.d.ts +9 -2
  38. package/dist/gradient.d.ts.map +1 -1
  39. package/dist/gradient.js +29 -1
  40. package/dist/gradient.js.map +1 -1
  41. package/dist/graph.d.ts +4 -0
  42. package/dist/graph.d.ts.map +1 -1
  43. package/dist/graph.js +30 -4
  44. package/dist/graph.js.map +1 -1
  45. package/dist/group-bounds.d.ts.map +1 -1
  46. package/dist/group-bounds.js +4 -3
  47. package/dist/group-bounds.js.map +1 -1
  48. package/dist/guides.d.ts +11 -2
  49. package/dist/guides.d.ts.map +1 -1
  50. package/dist/guides.js +30 -1
  51. package/dist/guides.js.map +1 -1
  52. package/dist/hit-test.d.ts.map +1 -1
  53. package/dist/hit-test.js +57 -5
  54. package/dist/hit-test.js.map +1 -1
  55. package/dist/icons/fit.d.ts +17 -0
  56. package/dist/icons/fit.d.ts.map +1 -0
  57. package/dist/icons/fit.js +21 -0
  58. package/dist/icons/fit.js.map +1 -0
  59. package/dist/icons/glyph.d.ts.map +1 -1
  60. package/dist/icons/glyph.js +5 -4
  61. package/dist/icons/glyph.js.map +1 -1
  62. package/dist/icons/index.d.ts +2 -1
  63. package/dist/icons/index.js +2 -1
  64. package/dist/images.d.ts +14 -4
  65. package/dist/images.d.ts.map +1 -1
  66. package/dist/images.js +45 -5
  67. package/dist/images.js.map +1 -1
  68. package/dist/instances/assignments.js +39 -6
  69. package/dist/instances/assignments.js.map +1 -1
  70. package/dist/instances/index.d.ts.map +1 -1
  71. package/dist/instances/index.js +39 -2
  72. package/dist/instances/index.js.map +1 -1
  73. package/dist/instances/kept-overrides.js +2 -2
  74. package/dist/instances/kept-overrides.js.map +1 -1
  75. package/dist/instances/matching.js +68 -0
  76. package/dist/instances/matching.js.map +1 -0
  77. package/dist/instances/overrides.d.ts +4 -1
  78. package/dist/instances/overrides.d.ts.map +1 -1
  79. package/dist/instances/overrides.js +13 -1
  80. package/dist/instances/overrides.js.map +1 -1
  81. package/dist/instances/protection.js +67 -0
  82. package/dist/instances/protection.js.map +1 -0
  83. package/dist/instances/swap.js +7 -5
  84. package/dist/instances/swap.js.map +1 -1
  85. package/dist/instances/sync.js +10 -116
  86. package/dist/instances/sync.js.map +1 -1
  87. package/dist/layout/guides.d.ts +17 -1
  88. package/dist/layout/guides.d.ts.map +1 -1
  89. package/dist/layout/guides.js +31 -11
  90. package/dist/layout/guides.js.map +1 -1
  91. package/dist/layout/index.d.ts +4 -2
  92. package/dist/layout/index.js +4 -2
  93. package/dist/layout/limits.d.ts +40 -0
  94. package/dist/layout/limits.d.ts.map +1 -0
  95. package/dist/layout/limits.js +54 -0
  96. package/dist/layout/limits.js.map +1 -0
  97. package/dist/layout/sizing.d.ts.map +1 -1
  98. package/dist/layout/sizing.js +0 -1
  99. package/dist/layout/sizing.js.map +1 -1
  100. package/dist/layout/spacing.d.ts +10 -0
  101. package/dist/layout/spacing.d.ts.map +1 -0
  102. package/dist/layout/spacing.js +12 -0
  103. package/dist/layout/spacing.js.map +1 -0
  104. package/dist/masks.d.ts +28 -0
  105. package/dist/masks.d.ts.map +1 -0
  106. package/dist/masks.js +53 -0
  107. package/dist/masks.js.map +1 -0
  108. package/dist/matrix.d.ts +2 -1
  109. package/dist/matrix.d.ts.map +1 -1
  110. package/dist/matrix.js +14 -1
  111. package/dist/matrix.js.map +1 -1
  112. package/dist/node-defaults.d.ts +9 -1
  113. package/dist/node-defaults.d.ts.map +1 -1
  114. package/dist/node-defaults.js +16 -1
  115. package/dist/node-defaults.js.map +1 -1
  116. package/dist/outline/flatten.d.ts.map +1 -1
  117. package/dist/outline/flatten.js +24 -17
  118. package/dist/outline/flatten.js.map +1 -1
  119. package/dist/outline/hit.d.ts +2 -8
  120. package/dist/outline/hit.d.ts.map +1 -1
  121. package/dist/outline/hit.js +16 -25
  122. package/dist/outline/hit.js.map +1 -1
  123. package/dist/outline/index.d.ts +3 -3
  124. package/dist/outline/index.js +3 -3
  125. package/dist/outline/shapes.d.ts +3 -1
  126. package/dist/outline/shapes.d.ts.map +1 -1
  127. package/dist/outline/shapes.js +18 -39
  128. package/dist/outline/shapes.js.map +1 -1
  129. package/dist/paints/index.d.ts +3 -1
  130. package/dist/paints/index.js +3 -1
  131. package/dist/paints/pattern.d.ts +13 -0
  132. package/dist/paints/pattern.d.ts.map +1 -0
  133. package/dist/paints/pattern.js +47 -0
  134. package/dist/paints/pattern.js.map +1 -0
  135. package/dist/paints/shows.d.ts +11 -0
  136. package/dist/paints/shows.d.ts.map +1 -0
  137. package/dist/paints/shows.js +16 -0
  138. package/dist/paints/shows.js.map +1 -0
  139. package/dist/paths/arc.d.ts +32 -0
  140. package/dist/paths/arc.d.ts.map +1 -0
  141. package/dist/paths/arc.js +49 -0
  142. package/dist/paths/arc.js.map +1 -0
  143. package/dist/paths/index.d.ts +5 -0
  144. package/dist/paths/index.js +5 -0
  145. package/dist/paths/network.d.ts +43 -0
  146. package/dist/paths/network.d.ts.map +1 -0
  147. package/dist/paths/network.js +249 -0
  148. package/dist/paths/network.js.map +1 -0
  149. package/dist/paths/sink.d.ts +20 -0
  150. package/dist/paths/sink.d.ts.map +1 -0
  151. package/dist/paths/sink.js +32 -0
  152. package/dist/paths/sink.js.map +1 -0
  153. package/dist/paths/smooth-corners.d.ts +25 -0
  154. package/dist/paths/smooth-corners.d.ts.map +1 -0
  155. package/dist/paths/smooth-corners.js +196 -0
  156. package/dist/paths/smooth-corners.js.map +1 -0
  157. package/dist/plugin-data/fields.d.ts +4 -0
  158. package/dist/plugin-data/fields.d.ts.map +1 -1
  159. package/dist/plugin-data/fields.js +2 -0
  160. package/dist/plugin-data/fields.js.map +1 -1
  161. package/dist/polygon.d.ts +1 -3
  162. package/dist/polygon.d.ts.map +1 -1
  163. package/dist/polygon.js +1 -16
  164. package/dist/polygon.js.map +1 -1
  165. package/dist/resize.d.ts.map +1 -1
  166. package/dist/resize.js +5 -5
  167. package/dist/resize.js.map +1 -1
  168. package/dist/scaling/node.d.ts.map +1 -1
  169. package/dist/scaling/node.js +25 -3
  170. package/dist/scaling/node.js.map +1 -1
  171. package/dist/text/auto-resize.d.ts +15 -0
  172. package/dist/text/auto-resize.d.ts.map +1 -0
  173. package/dist/text/auto-resize.js +23 -0
  174. package/dist/text/auto-resize.js.map +1 -0
  175. package/dist/text/index.d.ts +4 -3
  176. package/dist/text/index.js +4 -3
  177. package/dist/text/lengths.d.ts +6 -1
  178. package/dist/text/lengths.d.ts.map +1 -1
  179. package/dist/text/lengths.js +10 -1
  180. package/dist/text/lengths.js.map +1 -1
  181. package/dist/text/overflow.d.ts +14 -5
  182. package/dist/text/overflow.d.ts.map +1 -1
  183. package/dist/text/overflow.js +24 -6
  184. package/dist/text/overflow.js.map +1 -1
  185. package/dist/text/path.js +10 -0
  186. package/dist/text/path.js.map +1 -0
  187. package/dist/text/picture.js +5 -5
  188. package/dist/text/picture.js.map +1 -1
  189. package/dist/transfer/dependencies.d.ts.map +1 -1
  190. package/dist/transfer/dependencies.js +3 -2
  191. package/dist/transfer/dependencies.js.map +1 -1
  192. package/dist/transfer/nodes.js +2 -1
  193. package/dist/transfer/nodes.js.map +1 -1
  194. package/dist/types.d.ts +21 -8
  195. package/dist/types.d.ts.map +1 -1
  196. package/dist/types.js.map +1 -1
  197. package/dist/variables/index.d.ts +4 -3
  198. package/dist/variables/index.d.ts.map +1 -1
  199. package/dist/variables/index.js +4 -3
  200. package/dist/variables/index.js.map +1 -1
  201. package/dist/variables/lookup.d.ts +20 -0
  202. package/dist/variables/lookup.d.ts.map +1 -0
  203. package/dist/variables/lookup.js +34 -0
  204. package/dist/variables/lookup.js.map +1 -0
  205. package/dist/variables/paints.d.ts +35 -4
  206. package/dist/variables/paints.d.ts.map +1 -1
  207. package/dist/variables/paints.js +67 -16
  208. package/dist/variables/paints.js.map +1 -1
  209. package/dist/variables/resolution.d.ts +3 -1
  210. package/dist/variables/resolution.d.ts.map +1 -1
  211. package/dist/variables/resolution.js +8 -3
  212. package/dist/variables/resolution.js.map +1 -1
  213. package/dist/variables/subtree.js +1 -1
  214. package/dist/variables/subtree.js.map +1 -1
  215. package/dist/vector-network.d.ts.map +1 -1
  216. package/dist/vector-network.js +12 -76
  217. package/dist/vector-network.js.map +1 -1
  218. package/package.json +25 -1
  219. package/src/arrays.ts +37 -0
  220. package/src/buffered-events.ts +2 -1
  221. package/src/clipping.ts +11 -8
  222. package/src/components/properties.ts +1 -13
  223. package/src/corners.ts +93 -0
  224. package/src/css/grid.ts +21 -7
  225. package/src/css/index.ts +1 -1
  226. package/src/font-style.ts +38 -4
  227. package/src/geometry.ts +219 -6
  228. package/src/gradient.ts +23 -1
  229. package/src/graph.ts +22 -4
  230. package/src/group-bounds.ts +7 -3
  231. package/src/guides.ts +26 -1
  232. package/src/hit-test.ts +68 -10
  233. package/src/icons/fit.ts +24 -0
  234. package/src/icons/glyph.ts +5 -4
  235. package/src/icons/index.ts +1 -0
  236. package/src/images.ts +67 -7
  237. package/src/instances/assignments.ts +65 -7
  238. package/src/instances/index.ts +53 -4
  239. package/src/instances/kept-overrides.ts +2 -5
  240. package/src/instances/matching.ts +101 -0
  241. package/src/instances/overrides.ts +18 -0
  242. package/src/instances/protection.ts +103 -0
  243. package/src/instances/swap.ts +11 -9
  244. package/src/instances/sync.ts +11 -184
  245. package/src/layout/guides.ts +42 -9
  246. package/src/layout/index.ts +2 -0
  247. package/src/layout/limits.ts +64 -0
  248. package/src/layout/sizing.ts +0 -2
  249. package/src/layout/spacing.ts +11 -0
  250. package/src/masks.ts +51 -0
  251. package/src/matrix.ts +15 -2
  252. package/src/node-defaults.ts +23 -3
  253. package/src/outline/flatten.ts +30 -18
  254. package/src/outline/hit.ts +16 -33
  255. package/src/outline/index.ts +2 -2
  256. package/src/outline/shapes.ts +19 -36
  257. package/src/paints/index.ts +2 -0
  258. package/src/paints/pattern.ts +45 -0
  259. package/src/paints/shows.ts +13 -0
  260. package/src/paths/arc.ts +59 -0
  261. package/src/paths/index.ts +4 -0
  262. package/src/paths/network.ts +334 -0
  263. package/src/paths/sink.ts +50 -0
  264. package/src/paths/smooth-corners.ts +356 -0
  265. package/src/plugin-data/fields.ts +2 -0
  266. package/src/polygon.ts +0 -17
  267. package/src/resize.ts +6 -7
  268. package/src/scaling/node.ts +29 -3
  269. package/src/text/auto-resize.ts +25 -0
  270. package/src/text/index.ts +1 -0
  271. package/src/text/lengths.ts +11 -0
  272. package/src/text/overflow.ts +32 -6
  273. package/src/text/path.ts +13 -0
  274. package/src/text/picture.ts +5 -5
  275. package/src/transfer/dependencies.ts +3 -2
  276. package/src/transfer/nodes.ts +2 -1
  277. package/src/types.ts +25 -8
  278. package/src/variables/index.ts +1 -0
  279. package/src/variables/lookup.ts +31 -0
  280. package/src/variables/paints.ts +101 -20
  281. package/src/variables/resolution.ts +14 -2
  282. package/src/variables/subtree.ts +1 -1
  283. package/src/vector-network.ts +17 -138
@@ -0,0 +1,50 @@
1
+ import type { PolygonOutlineCommand } from '../polygon'
2
+
3
+ /** Anything path commands can be written into: a CanvasKit path, SVG path data, a blob encoder. */
4
+ export interface PathSink {
5
+ moveTo(x: number, y: number): unknown
6
+ lineTo(x: number, y: number): unknown
7
+ cubicTo(x1: number, y1: number, x2: number, y2: number, x3: number, y3: number): unknown
8
+ close(): unknown
9
+ }
10
+
11
+ /** A coordinate rounded to two decimals, as exported path data writes it. */
12
+ export function roundCoordinate(value: number): number {
13
+ return Math.round(value * 100) / 100
14
+ }
15
+
16
+ /** Path commands written as SVG path data into `parts`, each coordinate written by `format`. */
17
+ export function svgPathSink(
18
+ parts: string[],
19
+ format: (value: number) => number = roundCoordinate
20
+ ): PathSink {
21
+ const point = (x: number, y: number) => `${format(x)} ${format(y)}`
22
+ return {
23
+ moveTo: (x, y) => parts.push(`M${point(x, y)}`),
24
+ lineTo: (x, y) => parts.push(`L${point(x, y)}`),
25
+ cubicTo: (x1, y1, x2, y2, x, y) =>
26
+ parts.push(`C${point(x1, y1)} ${point(x2, y2)} ${point(x, y)}`),
27
+ close: () => parts.push('Z')
28
+ }
29
+ }
30
+
31
+ /** Outline commands written into `sink`. */
32
+ export function replayOutline(commands: readonly PolygonOutlineCommand[], sink: PathSink): void {
33
+ for (const command of commands) {
34
+ if (command.type === 'M') sink.moveTo(command.x, command.y)
35
+ else if (command.type === 'L') sink.lineTo(command.x, command.y)
36
+ else if (command.type === 'C')
37
+ sink.cubicTo(command.x1, command.y1, command.x2, command.y2, command.x, command.y)
38
+ else sink.close()
39
+ }
40
+ }
41
+
42
+ /** Outline commands as SVG path data, each coordinate written by `format`. */
43
+ export function outlineSVGPath(
44
+ commands: readonly PolygonOutlineCommand[],
45
+ format: (value: number) => number = roundCoordinate
46
+ ): string {
47
+ const parts: string[] = []
48
+ replayOutline(commands, svgPathSink(parts, format))
49
+ return parts.join('')
50
+ }
@@ -0,0 +1,356 @@
1
+ import { minOf } from '../arrays'
2
+ import type { SceneNode } from '../types'
3
+ import type { PathSink } from './sink'
4
+
5
+ /** What a rectangle's smoothed corners are drawn from. */
6
+ export type SmoothRectNode = Pick<
7
+ SceneNode,
8
+ | 'cornerRadius'
9
+ | 'cornerSmoothing'
10
+ | 'independentCorners'
11
+ | 'topLeftRadius'
12
+ | 'topRightRadius'
13
+ | 'bottomRightRadius'
14
+ | 'bottomLeftRadius'
15
+ >
16
+
17
+ /**
18
+ * Where a smoothed rectangle is traced: a CanvasKit `PathBuilder`, or a writer of SVG path data.
19
+ * `arcToRotated` takes CanvasKit's arguments, which match an SVG arc's.
20
+ */
21
+ export interface SmoothCornerSink extends PathSink {
22
+ arcToRotated(
23
+ rx: number,
24
+ ry: number,
25
+ xAxisRotate: number,
26
+ useSmallArc: boolean,
27
+ isCCW: boolean,
28
+ x: number,
29
+ y: number
30
+ ): unknown
31
+ }
32
+
33
+ type SmoothCornerKey = 'topLeft' | 'topRight' | 'bottomRight' | 'bottomLeft'
34
+
35
+ type SmoothCorner = {
36
+ radius: number
37
+ budget: number
38
+ }
39
+
40
+ type SmoothCornerPathParams = {
41
+ a: number
42
+ b: number
43
+ c: number
44
+ d: number
45
+ p: number
46
+ radius: number
47
+ arcSectionLength: number
48
+ }
49
+
50
+ function smoothCornerRadii(
51
+ node: SmoothRectNode,
52
+ width: number,
53
+ height: number,
54
+ spread: number
55
+ ): Record<SmoothCornerKey, SmoothCorner> {
56
+ const radius = (value: number) => Math.max(0, value + spread)
57
+ const radii: Record<SmoothCornerKey, number> = node.independentCorners
58
+ ? {
59
+ topLeft: radius(node.topLeftRadius),
60
+ topRight: radius(node.topRightRadius),
61
+ bottomRight: radius(node.bottomRightRadius),
62
+ bottomLeft: radius(node.bottomLeftRadius)
63
+ }
64
+ : {
65
+ topLeft: radius(node.cornerRadius),
66
+ topRight: radius(node.cornerRadius),
67
+ bottomRight: radius(node.cornerRadius),
68
+ bottomLeft: radius(node.cornerRadius)
69
+ }
70
+
71
+ if (
72
+ radii.topLeft === radii.topRight &&
73
+ radii.topRight === radii.bottomRight &&
74
+ radii.bottomRight === radii.bottomLeft
75
+ ) {
76
+ const budget = Math.min(width, height) / 2
77
+ const clampedRadius = Math.min(radii.topLeft, budget)
78
+ return {
79
+ topLeft: { radius: clampedRadius, budget },
80
+ topRight: { radius: clampedRadius, budget },
81
+ bottomRight: { radius: clampedRadius, budget },
82
+ bottomLeft: { radius: clampedRadius, budget }
83
+ }
84
+ }
85
+
86
+ const budgets: Record<SmoothCornerKey, number> = {
87
+ topLeft: -1,
88
+ topRight: -1,
89
+ bottomRight: -1,
90
+ bottomLeft: -1
91
+ }
92
+ const adjacentByCorner: Record<
93
+ SmoothCornerKey,
94
+ Array<{ corner: SmoothCornerKey; sideLength: number }>
95
+ > = {
96
+ topLeft: [
97
+ { corner: 'topRight', sideLength: width },
98
+ { corner: 'bottomLeft', sideLength: height }
99
+ ],
100
+ topRight: [
101
+ { corner: 'topLeft', sideLength: width },
102
+ { corner: 'bottomRight', sideLength: height }
103
+ ],
104
+ bottomRight: [
105
+ { corner: 'bottomLeft', sideLength: width },
106
+ { corner: 'topRight', sideLength: height }
107
+ ],
108
+ bottomLeft: [
109
+ { corner: 'bottomRight', sideLength: width },
110
+ { corner: 'topLeft', sideLength: height }
111
+ ]
112
+ }
113
+
114
+ for (const corner of (Object.keys(radii) as SmoothCornerKey[]).sort(
115
+ (a, b) => radii[b] - radii[a]
116
+ )) {
117
+ const cornerRadius = radii[corner]
118
+ const budget = minOf(
119
+ adjacentByCorner[corner].map((adjacent) => {
120
+ const adjacentRadius = radii[adjacent.corner]
121
+ if (cornerRadius === 0 && adjacentRadius === 0) return 0
122
+ if (budgets[adjacent.corner] >= 0) return adjacent.sideLength - budgets[adjacent.corner]
123
+ return (cornerRadius / (cornerRadius + adjacentRadius)) * adjacent.sideLength
124
+ })
125
+ )
126
+ budgets[corner] = budget
127
+ radii[corner] = Math.min(cornerRadius, budget)
128
+ }
129
+
130
+ return {
131
+ topLeft: { radius: radii.topLeft, budget: budgets.topLeft },
132
+ topRight: { radius: radii.topRight, budget: budgets.topRight },
133
+ bottomRight: { radius: radii.bottomRight, budget: budgets.bottomRight },
134
+ bottomLeft: { radius: radii.bottomLeft, budget: budgets.bottomLeft }
135
+ }
136
+ }
137
+
138
+ function degreesToRadians(degrees: number): number {
139
+ return (degrees * Math.PI) / 180
140
+ }
141
+
142
+ function smoothCornerPathParams(corner: SmoothCorner, smoothing: number): SmoothCornerPathParams {
143
+ let cornerSmoothing = smoothing
144
+ let p = (1 + cornerSmoothing) * corner.radius
145
+ if (corner.radius > 0) {
146
+ const maxSmoothing = corner.budget / corner.radius - 1
147
+ cornerSmoothing = Math.min(cornerSmoothing, maxSmoothing)
148
+ p = Math.min(p, corner.budget)
149
+ }
150
+
151
+ const arcMeasure = 90 * (1 - cornerSmoothing)
152
+ const arcSectionLength = Math.sin(degreesToRadians(arcMeasure / 2)) * corner.radius * Math.sqrt(2)
153
+ const angleAlpha = (90 - arcMeasure) / 2
154
+ const p3ToP4Distance = corner.radius * Math.tan(degreesToRadians(angleAlpha / 2))
155
+ const angleBeta = 45 * cornerSmoothing
156
+ const c = p3ToP4Distance * Math.cos(degreesToRadians(angleBeta))
157
+ const d = c * Math.tan(degreesToRadians(angleBeta))
158
+ const b = (p - arcSectionLength - c - d) / 3
159
+
160
+ return {
161
+ a: 2 * b,
162
+ b,
163
+ c,
164
+ d,
165
+ p,
166
+ radius: corner.radius,
167
+ arcSectionLength
168
+ }
169
+ }
170
+
171
+ function drawTopRightSmoothCorner(
172
+ path: SmoothCornerSink,
173
+ corner: SmoothCornerPathParams,
174
+ x: number,
175
+ y: number
176
+ ) {
177
+ if (corner.radius === 0) {
178
+ path.lineTo(x + corner.p, y)
179
+ return
180
+ }
181
+ path.cubicTo(
182
+ x + corner.a,
183
+ y,
184
+ x + corner.a + corner.b,
185
+ y,
186
+ x + corner.a + corner.b + corner.c,
187
+ y + corner.d
188
+ )
189
+ path.arcToRotated(
190
+ corner.radius,
191
+ corner.radius,
192
+ 0,
193
+ true,
194
+ false,
195
+ x + corner.p - corner.d,
196
+ y + corner.p - corner.a - corner.b - corner.c
197
+ )
198
+ path.cubicTo(
199
+ x + corner.p,
200
+ y + corner.p - corner.a - corner.b,
201
+ x + corner.p,
202
+ y + corner.p - corner.a,
203
+ x + corner.p,
204
+ y + corner.p
205
+ )
206
+ }
207
+
208
+ function drawBottomRightSmoothCorner(
209
+ path: SmoothCornerSink,
210
+ corner: SmoothCornerPathParams,
211
+ x: number,
212
+ y: number
213
+ ) {
214
+ if (corner.radius === 0) {
215
+ path.lineTo(x, y + corner.p)
216
+ return
217
+ }
218
+ path.cubicTo(
219
+ x,
220
+ y + corner.a,
221
+ x,
222
+ y + corner.a + corner.b,
223
+ x - corner.d,
224
+ y + corner.a + corner.b + corner.c
225
+ )
226
+ path.arcToRotated(
227
+ corner.radius,
228
+ corner.radius,
229
+ 0,
230
+ true,
231
+ false,
232
+ x - corner.p + corner.a + corner.b + corner.c,
233
+ y + corner.p - corner.d
234
+ )
235
+ path.cubicTo(
236
+ x - corner.p + corner.a + corner.b,
237
+ y + corner.p,
238
+ x - corner.p + corner.a,
239
+ y + corner.p,
240
+ x - corner.p,
241
+ y + corner.p
242
+ )
243
+ }
244
+
245
+ function drawBottomLeftSmoothCorner(
246
+ path: SmoothCornerSink,
247
+ corner: SmoothCornerPathParams,
248
+ x: number,
249
+ y: number
250
+ ) {
251
+ if (corner.radius === 0) {
252
+ path.lineTo(x - corner.p, y)
253
+ return
254
+ }
255
+ path.cubicTo(
256
+ x - corner.a,
257
+ y,
258
+ x - corner.a - corner.b,
259
+ y,
260
+ x - corner.a - corner.b - corner.c,
261
+ y - corner.d
262
+ )
263
+ path.arcToRotated(
264
+ corner.radius,
265
+ corner.radius,
266
+ 0,
267
+ true,
268
+ false,
269
+ x - corner.p + corner.d,
270
+ y - corner.p + corner.a + corner.b + corner.c
271
+ )
272
+ path.cubicTo(
273
+ x - corner.p,
274
+ y - corner.p + corner.a + corner.b,
275
+ x - corner.p,
276
+ y - corner.p + corner.a,
277
+ x - corner.p,
278
+ y - corner.p
279
+ )
280
+ }
281
+
282
+ function drawTopLeftSmoothCorner(
283
+ path: SmoothCornerSink,
284
+ corner: SmoothCornerPathParams,
285
+ x: number,
286
+ y: number
287
+ ) {
288
+ if (corner.radius === 0) {
289
+ path.lineTo(x, y - corner.p)
290
+ return
291
+ }
292
+ path.cubicTo(
293
+ x,
294
+ y - corner.a,
295
+ x,
296
+ y - corner.a - corner.b,
297
+ x + corner.d,
298
+ y - corner.a - corner.b - corner.c
299
+ )
300
+ path.arcToRotated(
301
+ corner.radius,
302
+ corner.radius,
303
+ 0,
304
+ true,
305
+ false,
306
+ x + corner.p - corner.a - corner.b - corner.c,
307
+ y - corner.p + corner.d
308
+ )
309
+ path.cubicTo(
310
+ x + corner.p - corner.a - corner.b,
311
+ y - corner.p,
312
+ x + corner.p - corner.a,
313
+ y - corner.p,
314
+ x + corner.p,
315
+ y - corner.p
316
+ )
317
+ }
318
+
319
+ /**
320
+ * Trace a rectangle with smoothed corners into `path`, as Figma rounds them. False, with nothing
321
+ * traced, when no corner is rounded, so the caller can draw a plain rectangle.
322
+ */
323
+ export function traceSmoothRect(
324
+ path: SmoothCornerSink,
325
+ node: SmoothRectNode,
326
+ box: { left: number; top: number; right: number; bottom: number },
327
+ spread = 0
328
+ ): boolean {
329
+ const { left, top, right, bottom } = box
330
+ const smoothing = Math.max(0, Math.min(node.cornerSmoothing, 1))
331
+ const corners = smoothCornerRadii(node, right - left, bottom - top, spread)
332
+ const topLeftCorner = smoothCornerPathParams(corners.topLeft, smoothing)
333
+ const topRightCorner = smoothCornerPathParams(corners.topRight, smoothing)
334
+ const bottomRightCorner = smoothCornerPathParams(corners.bottomRight, smoothing)
335
+ const bottomLeftCorner = smoothCornerPathParams(corners.bottomLeft, smoothing)
336
+
337
+ if (
338
+ topLeftCorner.radius === 0 &&
339
+ topRightCorner.radius === 0 &&
340
+ bottomRightCorner.radius === 0 &&
341
+ bottomLeftCorner.radius === 0
342
+ ) {
343
+ return false
344
+ }
345
+
346
+ path.moveTo(right - topRightCorner.p, top)
347
+ drawTopRightSmoothCorner(path, topRightCorner, right - topRightCorner.p, top)
348
+ path.lineTo(right, bottom - bottomRightCorner.p)
349
+ drawBottomRightSmoothCorner(path, bottomRightCorner, right, bottom - bottomRightCorner.p)
350
+ path.lineTo(left + bottomLeftCorner.p, bottom)
351
+ drawBottomLeftSmoothCorner(path, bottomLeftCorner, left + bottomLeftCorner.p, bottom)
352
+ path.lineTo(left, top + topLeftCorner.p)
353
+ drawTopLeftSmoothCorner(path, topLeftCorner, left, top + topLeftCorner.p)
354
+ path.close()
355
+ return true
356
+ }
@@ -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/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 glyphs = orig.derivedTextGlyphs ?? []
309
- const pathText =
310
- !!orig.textPathData || !!orig.textPathBox || glyphs.some((glyph) => (glyph.rotation ?? 0) !== 0)
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 && glyphs.length > 0 && pathText) {
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 Yoga has resolved HUG/FILL sizing.
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
 
@@ -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
  }
@@ -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
+ }
package/src/text/index.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export * from './auto-resize'
1
2
  export * from './lists'
2
3
  export * from './paragraphs'
3
4
  export * from './references'
@@ -23,6 +23,17 @@ export function lineStepPixels(lineHeight: TextLength | null, fontSize: number):
23
23
  return lineHeight.unit === 'PIXELS' ? pixels : Math.round(pixels)
24
24
  }
25
25
 
26
+ /** How far an automatic line height steps lines when no font metrics measure them. */
27
+ const ESTIMATED_LINE_STEP = 1.2
28
+
29
+ /**
30
+ * How far apart lines step where no font metrics measure them: as `lineStepPixels` steps them,
31
+ * and 1.2 times the font size when automatic.
32
+ */
33
+ export function estimatedLineStep(lineHeight: TextLength | null, fontSize: number): number {
34
+ return lineStepPixels(lineHeight, fontSize) ?? fontSize * ESTIMATED_LINE_STEP
35
+ }
36
+
26
37
  /** A length in pixels, as a bound number variable and most authored formats give one. */
27
38
  export function pixelLength(value: number): TextLength {
28
39
  return { value, unit: 'PIXELS' }
@@ -1,12 +1,38 @@
1
+ import type { VisualBounds } from '../geometry'
1
2
  import type { SceneNode } from '../types'
2
3
 
3
- type TextBoundsNode = Pick<SceneNode, 'type' | 'height' | 'derivedTextHeight'>
4
+ type TextBoxNode = Pick<SceneNode, 'height' | 'textAlignVertical'>
5
+ type TextBoundsNode = TextBoxNode & Pick<SceneNode, 'type' | 'derivedTextHeight'>
6
+
7
+ export type TextLinesReach = Pick<VisualBounds, 'minY' | 'maxY'>
8
+
9
+ /**
10
+ * Where the top of laid-out lines `contentHeight` tall sits in a text's box, by its vertical
11
+ * alignment. Lines taller than the box overflow on the side away from the alignment, as Figma
12
+ * lays them out.
13
+ */
14
+ export function textVerticalOffset(node: TextBoxNode, contentHeight: number): number {
15
+ const available = node.height - contentHeight
16
+ if (node.textAlignVertical === 'CENTER') {
17
+ return available / 2
18
+ }
19
+ if (node.textAlignVertical === 'BOTTOM') {
20
+ return available
21
+ }
22
+ return 0
23
+ }
4
24
 
5
25
  /**
6
- * How far a text's lines reach below its box: a fixed-size box does not clip them, as in Figma,
7
- * and the renderer starts them at its top.
26
+ * The rows a text draws its lines over, in its own axes: its box, grown by the lines a fixed-size
27
+ * box lets run past it above or below, as in Figma.
8
28
  */
9
- export function textLinesOverflow(node: TextBoundsNode): number {
10
- if (node.type !== 'TEXT' || !node.derivedTextHeight) return 0
11
- return Math.max(0, node.derivedTextHeight - node.height)
29
+ export function textLinesReach(node: TextBoundsNode): TextLinesReach {
30
+ if (node.type !== 'TEXT' || !node.derivedTextHeight) {
31
+ return { minY: 0, maxY: node.height }
32
+ }
33
+ const linesTop = textVerticalOffset(node, node.derivedTextHeight)
34
+ return {
35
+ minY: Math.min(0, linesTop),
36
+ maxY: Math.max(node.height, linesTop + node.derivedTextHeight)
37
+ }
12
38
  }
@@ -0,0 +1,13 @@
1
+ import type { SceneNode } from '../types'
2
+
3
+ export function followsPath(
4
+ node: Pick<SceneNode, 'derivedTextGlyphs' | 'textPathData' | 'textPathBox'>
5
+ ): boolean {
6
+ const glyphs = node.derivedTextGlyphs ?? []
7
+ if (glyphs.length === 0) {
8
+ return false
9
+ }
10
+ return (
11
+ !!node.textPathData || !!node.textPathBox || glyphs.some((glyph) => (glyph.rotation ?? 0) !== 0)
12
+ )
13
+ }
@@ -36,16 +36,16 @@ export function textCacheInvalidationChanges(
36
36
  keys.some((key) => GLYPH_AFFECTING_KEYS.has(key)) ||
37
37
  TEXT_LAYOUT_FIELDS.some((key) => keys.includes(key))
38
38
  if (glyphsInvalidated && !('derivedLayout' in changes)) invalidated.derivedLayout = null
39
- // A successful path-text edit supplies reflowed glyphs in `changes`. Every
40
- // other mutation path must drop stale baked glyphs and path identity rather
41
- // than pair new text/style with old visible outlines.
42
- if (node.derivedTextGlyphs && glyphsInvalidated && !changes.derivedTextGlyphs) {
39
+ if (glyphsInvalidated && !changes.derivedTextGlyphs) {
43
40
  // Path-text resize supplies transformed glyphs through the resize workflow. A raw box
44
41
  // preview keeps them until that workflow commits or an actual shaping edit occurs.
45
42
  const onlyPathBoxResize =
46
43
  !!node.textPathData && keys.every((key) => key === 'width' || key === 'height')
47
- if (!onlyPathBoxResize) {
44
+ const placesPath = keys.includes('textPathBox') && !!changes.textPathBox
45
+ if (node.derivedTextGlyphs && !onlyPathBoxResize) {
48
46
  invalidated.derivedTextGlyphs = null
47
+ }
48
+ if (node.textPathData && !onlyPathBoxResize && !placesPath) {
49
49
  invalidated.textPathData = null
50
50
  }
51
51
  }