@moondraft/import 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (323) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/LICENSE +34 -0
  3. package/README.md +36 -2
  4. package/SOURCE.md +169 -0
  5. package/dist/adapter/convert.d.ts +122 -0
  6. package/dist/adapter/convert.js +2302 -0
  7. package/dist/adapter/figma.d.ts +9 -0
  8. package/dist/adapter/figma.js +30 -0
  9. package/dist/adapter/file-components.d.ts +14 -0
  10. package/dist/adapter/file-components.js +262 -0
  11. package/dist/adapter/file-resources.d.ts +29 -0
  12. package/dist/adapter/file-resources.js +562 -0
  13. package/dist/adapter/index.d.ts +6 -0
  14. package/dist/adapter/index.js +9 -0
  15. package/dist/adapter/link.d.ts +45 -0
  16. package/dist/adapter/link.js +688 -0
  17. package/dist/adapter/payload.d.ts +31 -0
  18. package/dist/adapter/payload.js +78 -0
  19. package/dist/adapter/shapes.d.ts +31 -0
  20. package/dist/adapter/shapes.js +75 -0
  21. package/dist/adapter/variables.d.ts +44 -0
  22. package/dist/adapter/variables.js +103 -0
  23. package/dist/figma-file.d.ts +38 -0
  24. package/dist/figma-file.js +97 -0
  25. package/dist/figma-worker.d.ts +11 -0
  26. package/dist/figma-worker.js +12 -0
  27. package/dist/figma.d.ts +26 -0
  28. package/dist/figma.js +96 -0
  29. package/dist/index.d.ts +28 -0
  30. package/dist/index.js +49 -0
  31. package/dist/report.d.ts +15 -0
  32. package/dist/report.js +69 -0
  33. package/dist/svg.d.ts +33 -0
  34. package/dist/svg.js +44 -0
  35. package/dist/syntax.d.ts +8 -0
  36. package/dist/syntax.js +19 -0
  37. package/dist/taco/figma-import/clipboard.d.ts +25 -0
  38. package/dist/taco/figma-import/clipboard.js +87 -0
  39. package/dist/taco/figma-import/clipboardSize.d.ts +31 -0
  40. package/dist/taco/figma-import/clipboardSize.js +107 -0
  41. package/dist/taco/figma-import/container/compression.d.ts +9 -0
  42. package/dist/taco/figma-import/container/compression.js +174 -0
  43. package/dist/taco/figma-import/container/error.d.ts +15 -0
  44. package/dist/taco/figma-import/container/error.js +19 -0
  45. package/dist/taco/figma-import/container/kiwiContainer.d.ts +38 -0
  46. package/dist/taco/figma-import/container/kiwiContainer.js +61 -0
  47. package/dist/taco/figma-import/container/limits.d.ts +22 -0
  48. package/dist/taco/figma-import/container/limits.js +22 -0
  49. package/dist/taco/figma-import/container/zip.d.ts +14 -0
  50. package/dist/taco/figma-import/container/zip.js +154 -0
  51. package/dist/taco/figma-import/figFile.d.ts +34 -0
  52. package/dist/taco/figma-import/figFile.js +91 -0
  53. package/dist/taco/figma-import/figFileDocument.d.ts +25 -0
  54. package/dist/taco/figma-import/figFileDocument.js +100 -0
  55. package/dist/taco/figma-import/import/blobs.d.ts +10 -0
  56. package/dist/taco/figma-import/import/blobs.js +23 -0
  57. package/dist/taco/figma-import/import/carry.d.ts +11 -0
  58. package/dist/taco/figma-import/import/carry.js +33 -0
  59. package/dist/taco/figma-import/import/componentProps.d.ts +84 -0
  60. package/dist/taco/figma-import/import/componentProps.js +313 -0
  61. package/dist/taco/figma-import/import/componentSets.d.ts +83 -0
  62. package/dist/taco/figma-import/import/componentSets.js +647 -0
  63. package/dist/taco/figma-import/import/converters/effects.d.ts +31 -0
  64. package/dist/taco/figma-import/import/converters/effects.js +127 -0
  65. package/dist/taco/figma-import/import/converters/ellipse.d.ts +7 -0
  66. package/dist/taco/figma-import/import/converters/ellipse.js +54 -0
  67. package/dist/taco/figma-import/import/converters/frame.d.ts +21 -0
  68. package/dist/taco/figma-import/import/converters/frame.js +55 -0
  69. package/dist/taco/figma-import/import/converters/gradient.d.ts +72 -0
  70. package/dist/taco/figma-import/import/converters/gradient.js +106 -0
  71. package/dist/taco/figma-import/import/converters/group.d.ts +21 -0
  72. package/dist/taco/figma-import/import/converters/group.js +39 -0
  73. package/dist/taco/figma-import/import/converters/image.d.ts +13 -0
  74. package/dist/taco/figma-import/import/converters/image.js +83 -0
  75. package/dist/taco/figma-import/import/converters/layout.d.ts +34 -0
  76. package/dist/taco/figma-import/import/converters/layout.js +168 -0
  77. package/dist/taco/figma-import/import/converters/line.d.ts +11 -0
  78. package/dist/taco/figma-import/import/converters/line.js +40 -0
  79. package/dist/taco/figma-import/import/converters/rectangle.d.ts +3 -0
  80. package/dist/taco/figma-import/import/converters/rectangle.js +11 -0
  81. package/dist/taco/figma-import/import/converters/section.d.ts +9 -0
  82. package/dist/taco/figma-import/import/converters/section.js +35 -0
  83. package/dist/taco/figma-import/import/converters/shared.d.ts +68 -0
  84. package/dist/taco/figma-import/import/converters/shared.js +362 -0
  85. package/dist/taco/figma-import/import/converters/text.d.ts +41 -0
  86. package/dist/taco/figma-import/import/converters/text.js +576 -0
  87. package/dist/taco/figma-import/import/converters/vector.d.ts +84 -0
  88. package/dist/taco/figma-import/import/converters/vector.js +468 -0
  89. package/dist/taco/figma-import/import/converters/vectorNetwork.d.ts +98 -0
  90. package/dist/taco/figma-import/import/converters/vectorNetwork.js +385 -0
  91. package/dist/taco/figma-import/import/fields.d.ts +15 -0
  92. package/dist/taco/figma-import/import/fields.js +29 -0
  93. package/dist/taco/figma-import/import/instances.d.ts +163 -0
  94. package/dist/taco/figma-import/import/instances.js +1323 -0
  95. package/dist/taco/figma-import/import/nestingCycles.d.ts +86 -0
  96. package/dist/taco/figma-import/import/nestingCycles.js +488 -0
  97. package/dist/taco/figma-import/import/slots.d.ts +56 -0
  98. package/dist/taco/figma-import/import/slots.js +96 -0
  99. package/dist/taco/figma-import/import/styles.d.ts +46 -0
  100. package/dist/taco/figma-import/import/styles.js +42 -0
  101. package/dist/taco/figma-import/import/tree.d.ts +66 -0
  102. package/dist/taco/figma-import/import/tree.js +194 -0
  103. package/dist/taco/figma-import/import/types.d.ts +232 -0
  104. package/dist/taco/figma-import/import/types.js +33 -0
  105. package/dist/taco/figma-import/import/variables.d.ts +136 -0
  106. package/dist/taco/figma-import/import/variables.js +762 -0
  107. package/dist/taco/figma-import/import/variantTrees.d.ts +20 -0
  108. package/dist/taco/figma-import/import/variantTrees.js +480 -0
  109. package/dist/taco/figma-import/import/walk.d.ts +159 -0
  110. package/dist/taco/figma-import/import/walk.js +1297 -0
  111. package/dist/taco/figma-import/index.d.ts +22 -0
  112. package/dist/taco/figma-import/index.js +13 -0
  113. package/dist/taco/figma-import/kiwi/byteReader.d.ts +37 -0
  114. package/dist/taco/figma-import/kiwi/byteReader.js +151 -0
  115. package/dist/taco/figma-import/kiwi/error.d.ts +14 -0
  116. package/dist/taco/figma-import/kiwi/error.js +29 -0
  117. package/dist/taco/figma-import/kiwi/limits.d.ts +18 -0
  118. package/dist/taco/figma-import/kiwi/limits.js +18 -0
  119. package/dist/taco/figma-import/kiwi/message.d.ts +10 -0
  120. package/dist/taco/figma-import/kiwi/message.js +145 -0
  121. package/dist/taco/figma-import/kiwi/schema.d.ts +42 -0
  122. package/dist/taco/figma-import/kiwi/schema.js +206 -0
  123. package/dist/taco/figma-import/kiwi/types.d.ts +46 -0
  124. package/dist/taco/figma-import/kiwi/types.js +18 -0
  125. package/dist/taco/model/agent-hidden-fields.d.ts +20 -0
  126. package/dist/taco/model/agent-hidden-fields.js +29 -0
  127. package/dist/taco/model/asset-users.d.ts +23 -0
  128. package/dist/taco/model/asset-users.js +128 -0
  129. package/dist/taco/model/auto-layout-lines.d.ts +32 -0
  130. package/dist/taco/model/auto-layout-lines.js +70 -0
  131. package/dist/taco/model/codec.d.ts +444 -0
  132. package/dist/taco/model/codec.js +5635 -0
  133. package/dist/taco/model/color.d.ts +11 -0
  134. package/dist/taco/model/color.js +61 -0
  135. package/dist/taco/model/comment-operations.d.ts +102 -0
  136. package/dist/taco/model/comment-operations.js +227 -0
  137. package/dist/taco/model/component-evaluation.d.ts +94 -0
  138. package/dist/taco/model/component-evaluation.js +548 -0
  139. package/dist/taco/model/component-expressions.d.ts +242 -0
  140. package/dist/taco/model/component-expressions.js +1082 -0
  141. package/dist/taco/model/component-properties.d.ts +111 -0
  142. package/dist/taco/model/component-properties.js +339 -0
  143. package/dist/taco/model/component-samples.d.ts +83 -0
  144. package/dist/taco/model/component-samples.js +290 -0
  145. package/dist/taco/model/component-validity.d.ts +85 -0
  146. package/dist/taco/model/component-validity.js +397 -0
  147. package/dist/taco/model/components.d.ts +155 -0
  148. package/dist/taco/model/components.js +505 -0
  149. package/dist/taco/model/connector-ends.d.ts +20 -0
  150. package/dist/taco/model/connector-ends.js +188 -0
  151. package/dist/taco/model/connector-geometry.d.ts +74 -0
  152. package/dist/taco/model/connector-geometry.js +293 -0
  153. package/dist/taco/model/corner-radii.d.ts +26 -0
  154. package/dist/taco/model/corner-radii.js +47 -0
  155. package/dist/taco/model/derivations.d.ts +81 -0
  156. package/dist/taco/model/derivations.js +151 -0
  157. package/dist/taco/model/export-settings.d.ts +53 -0
  158. package/dist/taco/model/export-settings.js +156 -0
  159. package/dist/taco/model/expressions/ast.d.ts +153 -0
  160. package/dist/taco/model/expressions/ast.js +6 -0
  161. package/dist/taco/model/expressions/cache.d.ts +23 -0
  162. package/dist/taco/model/expressions/cache.js +43 -0
  163. package/dist/taco/model/expressions/check.d.ts +72 -0
  164. package/dist/taco/model/expressions/check.js +517 -0
  165. package/dist/taco/model/expressions/completion.d.ts +61 -0
  166. package/dist/taco/model/expressions/completion.js +421 -0
  167. package/dist/taco/model/expressions/evaluate.d.ts +32 -0
  168. package/dist/taco/model/expressions/evaluate.js +519 -0
  169. package/dist/taco/model/expressions/forms.d.ts +84 -0
  170. package/dist/taco/model/expressions/forms.js +200 -0
  171. package/dist/taco/model/expressions/index.d.ts +14 -0
  172. package/dist/taco/model/expressions/index.js +17 -0
  173. package/dist/taco/model/expressions/lexer.d.ts +42 -0
  174. package/dist/taco/model/expressions/lexer.js +213 -0
  175. package/dist/taco/model/expressions/library.d.ts +33 -0
  176. package/dist/taco/model/expressions/library.js +117 -0
  177. package/dist/taco/model/expressions/names.d.ts +43 -0
  178. package/dist/taco/model/expressions/names.js +99 -0
  179. package/dist/taco/model/expressions/parser.d.ts +15 -0
  180. package/dist/taco/model/expressions/parser.js +438 -0
  181. package/dist/taco/model/expressions/rename.d.ts +34 -0
  182. package/dist/taco/model/expressions/rename.js +46 -0
  183. package/dist/taco/model/expressions/source.d.ts +15 -0
  184. package/dist/taco/model/expressions/source.js +161 -0
  185. package/dist/taco/model/expressions/types.d.ts +119 -0
  186. package/dist/taco/model/expressions/types.js +102 -0
  187. package/dist/taco/model/expressions/values.d.ts +97 -0
  188. package/dist/taco/model/expressions/values.js +160 -0
  189. package/dist/taco/model/geometry-limits.d.ts +16 -0
  190. package/dist/taco/model/geometry-limits.js +67 -0
  191. package/dist/taco/model/geometry.d.ts +47 -0
  192. package/dist/taco/model/geometry.js +193 -0
  193. package/dist/taco/model/group-bounds.d.ts +38 -0
  194. package/dist/taco/model/group-bounds.js +144 -0
  195. package/dist/taco/model/id-table.d.ts +37 -0
  196. package/dist/taco/model/id-table.js +80 -0
  197. package/dist/taco/model/index.d.ts +25 -0
  198. package/dist/taco/model/index.js +27 -0
  199. package/dist/taco/model/instance-resolver.d.ts +372 -0
  200. package/dist/taco/model/instance-resolver.js +1748 -0
  201. package/dist/taco/model/layer-syntax.d.ts +340 -0
  202. package/dist/taco/model/layer-syntax.js +3506 -0
  203. package/dist/taco/model/layout/constraints.d.ts +84 -0
  204. package/dist/taco/model/layout/constraints.js +445 -0
  205. package/dist/taco/model/layout/frame-layout.d.ts +46 -0
  206. package/dist/taco/model/layout/frame-layout.js +618 -0
  207. package/dist/taco/model/layout/node-sizing.d.ts +163 -0
  208. package/dist/taco/model/layout/node-sizing.js +486 -0
  209. package/dist/taco/model/legacy-connector-caps.d.ts +15 -0
  210. package/dist/taco/model/legacy-connector-caps.js +70 -0
  211. package/dist/taco/model/linear-call.d.ts +48 -0
  212. package/dist/taco/model/linear-call.js +58 -0
  213. package/dist/taco/model/new-nodes.d.ts +145 -0
  214. package/dist/taco/model/new-nodes.js +277 -0
  215. package/dist/taco/model/node-builder.d.ts +449 -0
  216. package/dist/taco/model/node-builder.js +2294 -0
  217. package/dist/taco/model/node-table-watch.d.ts +36 -0
  218. package/dist/taco/model/node-table-watch.js +63 -0
  219. package/dist/taco/model/node-transform.d.ts +39 -0
  220. package/dist/taco/model/node-transform.js +79 -0
  221. package/dist/taco/model/node-users-index.d.ts +10 -0
  222. package/dist/taco/model/node-users-index.js +82 -0
  223. package/dist/taco/model/operation-references.d.ts +80 -0
  224. package/dist/taco/model/operation-references.js +399 -0
  225. package/dist/taco/model/operations.d.ts +610 -0
  226. package/dist/taco/model/operations.js +2882 -0
  227. package/dist/taco/model/overlay-table.d.ts +71 -0
  228. package/dist/taco/model/overlay-table.js +171 -0
  229. package/dist/taco/model/override-remap.d.ts +69 -0
  230. package/dist/taco/model/override-remap.js +230 -0
  231. package/dist/taco/model/parent-table.d.ts +53 -0
  232. package/dist/taco/model/parent-table.js +193 -0
  233. package/dist/taco/model/problem-collector.d.ts +27 -0
  234. package/dist/taco/model/problem-collector.js +101 -0
  235. package/dist/taco/model/sha256.d.ts +10 -0
  236. package/dist/taco/model/sha256.js +74 -0
  237. package/dist/taco/model/shape-geometry.d.ts +96 -0
  238. package/dist/taco/model/shape-geometry.js +368 -0
  239. package/dist/taco/model/shared-node-table.d.ts +97 -0
  240. package/dist/taco/model/shared-node-table.js +546 -0
  241. package/dist/taco/model/slots.d.ts +113 -0
  242. package/dist/taco/model/slots.js +281 -0
  243. package/dist/taco/model/stroke-settings.d.ts +37 -0
  244. package/dist/taco/model/stroke-settings.js +90 -0
  245. package/dist/taco/model/svg-path-import.d.ts +89 -0
  246. package/dist/taco/model/svg-path-import.js +219 -0
  247. package/dist/taco/model/text-measurement.d.ts +142 -0
  248. package/dist/taco/model/text-measurement.js +846 -0
  249. package/dist/taco/model/text-reads.d.ts +66 -0
  250. package/dist/taco/model/text-reads.js +128 -0
  251. package/dist/taco/model/text-sizer.d.ts +358 -0
  252. package/dist/taco/model/text-sizer.js +996 -0
  253. package/dist/taco/model/theme-users.d.ts +59 -0
  254. package/dist/taco/model/theme-users.js +312 -0
  255. package/dist/taco/model/thumbnail.d.ts +15 -0
  256. package/dist/taco/model/thumbnail.js +34 -0
  257. package/dist/taco/model/types.d.ts +2347 -0
  258. package/dist/taco/model/types.js +366 -0
  259. package/dist/taco/model/unknown-property.d.ts +5 -0
  260. package/dist/taco/model/unknown-property.js +7 -0
  261. package/dist/taco/model/validated-documents.d.ts +22 -0
  262. package/dist/taco/model/validated-documents.js +22 -0
  263. package/dist/taco/model/variable-modes.d.ts +61 -0
  264. package/dist/taco/model/variable-modes.js +438 -0
  265. package/dist/taco/model/variable-references.d.ts +54 -0
  266. package/dist/taco/model/variable-references.js +183 -0
  267. package/dist/taco/model/variables.d.ts +24 -0
  268. package/dist/taco/model/variables.js +151 -0
  269. package/dist/taco/model/vector-network.d.ts +34 -0
  270. package/dist/taco/model/vector-network.js +411 -0
  271. package/dist/taco/svg-import/import-svg.d.ts +36 -0
  272. package/dist/taco/svg-import/import-svg.js +729 -0
  273. package/dist/taco/svg-import/index.d.ts +3 -0
  274. package/dist/taco/svg-import/index.js +3 -0
  275. package/dist/taco/svg-import/limits/limits.d.ts +79 -0
  276. package/dist/taco/svg-import/limits/limits.js +160 -0
  277. package/dist/taco/svg-import/paint/gradients.d.ts +64 -0
  278. package/dist/taco/svg-import/paint/gradients.js +429 -0
  279. package/dist/taco/svg-import/paint/paint.d.ts +32 -0
  280. package/dist/taco/svg-import/paint/paint.js +162 -0
  281. package/dist/taco/svg-import/paths/paths.d.ts +58 -0
  282. package/dist/taco/svg-import/paths/paths.js +205 -0
  283. package/dist/taco/svg-import/public-types.d.ts +279 -0
  284. package/dist/taco/svg-import/public-types.js +56 -0
  285. package/dist/taco/svg-import/report/report.d.ts +44 -0
  286. package/dist/taco/svg-import/report/report.js +79 -0
  287. package/dist/taco/svg-import/reuse/reuse.d.ts +99 -0
  288. package/dist/taco/svg-import/reuse/reuse.js +840 -0
  289. package/dist/taco/svg-import/shapes/shapes.d.ts +35 -0
  290. package/dist/taco/svg-import/shapes/shapes.js +386 -0
  291. package/dist/taco/svg-import/structure/effects.d.ts +26 -0
  292. package/dist/taco/svg-import/structure/effects.js +618 -0
  293. package/dist/taco/svg-import/structure/image.d.ts +24 -0
  294. package/dist/taco/svg-import/structure/image.js +495 -0
  295. package/dist/taco/svg-import/structure/walk.d.ts +85 -0
  296. package/dist/taco/svg-import/structure/walk.js +544 -0
  297. package/dist/taco/svg-import/style/color.d.ts +40 -0
  298. package/dist/taco/svg-import/style/color.js +275 -0
  299. package/dist/taco/svg-import/style/computed.d.ts +214 -0
  300. package/dist/taco/svg-import/style/computed.js +1534 -0
  301. package/dist/taco/svg-import/style/css.d.ts +127 -0
  302. package/dist/taco/svg-import/style/css.js +541 -0
  303. package/dist/taco/svg-import/style/media.d.ts +25 -0
  304. package/dist/taco/svg-import/style/media.js +307 -0
  305. package/dist/taco/svg-import/style/properties.d.ts +43 -0
  306. package/dist/taco/svg-import/style/properties.js +135 -0
  307. package/dist/taco/svg-import/style/selector-reach.d.ts +30 -0
  308. package/dist/taco/svg-import/style/selector-reach.js +169 -0
  309. package/dist/taco/svg-import/style/selector-validity.d.ts +14 -0
  310. package/dist/taco/svg-import/style/selector-validity.js +307 -0
  311. package/dist/taco/svg-import/text/text.d.ts +29 -0
  312. package/dist/taco/svg-import/text/text.js +545 -0
  313. package/dist/taco/svg-import/transform/matrix.d.ts +111 -0
  314. package/dist/taco/svg-import/transform/matrix.js +229 -0
  315. package/dist/taco/svg-import/tree/element-data.d.ts +52 -0
  316. package/dist/taco/svg-import/tree/element-data.js +155 -0
  317. package/dist/taco/svg-import/tree/elements.d.ts +36 -0
  318. package/dist/taco/svg-import/tree/elements.js +121 -0
  319. package/dist/taco/svg-import/units/length.d.ts +43 -0
  320. package/dist/taco/svg-import/units/length.js +125 -0
  321. package/dist/taco/svg-import/units/viewbox.d.ts +35 -0
  322. package/dist/taco/svg-import/units/viewbox.js +77 -0
  323. package/package.json +54 -4
@@ -0,0 +1,206 @@
1
+ import { ByteReader } from "./byteReader.js";
2
+ import { KiwiDecodeError } from "./error.js";
3
+ import { KIWI_DEFINITION_KINDS, KIWI_NATIVE_TYPES, } from "./types.js";
4
+ /** Smallest encodings: a definition is name (1) + kind (1) + field count (1). */
5
+ const MIN_DEFINITION_BYTES = 3;
6
+ /** A field is name (1) + type (1) + flags (1) + value (1). */
7
+ const MIN_FIELD_BYTES = 4;
8
+ /**
9
+ * Decodes a binary Kiwi schema, the format written by `encodeBinarySchema` in
10
+ * the reference implementation: a definition count, then for each definition
11
+ * its name, kind and fields, and for each field its name, type, flags and
12
+ * value. The schema is checked before it is returned; see `indexKiwiSchema`.
13
+ */
14
+ export function decodeKiwiSchema(bytes) {
15
+ if (!(bytes instanceof Uint8Array)) {
16
+ throw new TypeError("decodeKiwiSchema expects a Uint8Array");
17
+ }
18
+ const reader = new ByteReader(bytes);
19
+ const countOffset = reader.offset;
20
+ const definitionCount = reader.readVarUint();
21
+ if (definitionCount * MIN_DEFINITION_BYTES > reader.remaining) {
22
+ throw reader.fail(`definition count ${definitionCount} needs more bytes than the ${reader.remaining} that remain`, countOffset);
23
+ }
24
+ // Types are definition indexes until every definition has been read.
25
+ const raw = [];
26
+ const offsets = { definitions: [], fields: [] };
27
+ for (let index = 0; index < definitionCount; index += 1) {
28
+ offsets.definitions.push(reader.offset);
29
+ const name = reader.readString();
30
+ const kindOffset = reader.offset;
31
+ const kindByte = reader.readByte();
32
+ const kind = KIWI_DEFINITION_KINDS[kindByte];
33
+ if (kind === undefined)
34
+ throw reader.fail(`unknown definition kind ${kindByte}`, kindOffset);
35
+ const fieldCountOffset = reader.offset;
36
+ const fieldCount = reader.readVarUint();
37
+ if (fieldCount * MIN_FIELD_BYTES > reader.remaining) {
38
+ throw reader.fail(`field count ${fieldCount} of ${name} needs more bytes than the ${reader.remaining} that remain`, fieldCountOffset).within(name);
39
+ }
40
+ const fields = [];
41
+ const fieldOffsets = [];
42
+ for (let fieldIndex = 0; fieldIndex < fieldCount; fieldIndex += 1) {
43
+ fieldOffsets.push(reader.offset);
44
+ const fieldName = reader.readString();
45
+ const type = reader.readVarInt();
46
+ const flagsOffset = reader.offset;
47
+ const flags = reader.readByte();
48
+ if (flags > 1) {
49
+ throw reader.fail(`unknown field flags ${flags}`, flagsOffset).within(fieldName).within(name);
50
+ }
51
+ const value = reader.readVarUint();
52
+ fields.push({ name: fieldName, type, isArray: flags === 1, value });
53
+ }
54
+ raw.push({ name, kind, fields });
55
+ offsets.fields.push(fieldOffsets);
56
+ }
57
+ if (reader.remaining > 0) {
58
+ throw reader.fail(`${reader.remaining} unread bytes after the schema`);
59
+ }
60
+ const definitions = raw.map((definition, index) => ({
61
+ name: definition.name,
62
+ kind: definition.kind,
63
+ fields: definition.fields.map((field, fieldIndex) => {
64
+ if (definition.kind === "ENUM") {
65
+ // The reference writer stores no type for enum values.
66
+ return { name: field.name, type: null, isArray: false, value: field.value };
67
+ }
68
+ const at = offsets.fields[index]?.[fieldIndex] ?? null;
69
+ let type;
70
+ if (field.type < 0)
71
+ type = KIWI_NATIVE_TYPES[~field.type];
72
+ else
73
+ type = raw[field.type]?.name;
74
+ if (type === undefined) {
75
+ throw new KiwiDecodeError(`unknown type number ${field.type}`, at).within(field.name).within(definition.name);
76
+ }
77
+ return { name: field.name, type, isArray: field.isArray, value: field.value };
78
+ }),
79
+ }));
80
+ const schema = { definitions };
81
+ indexKiwiSchema(schema, offsets);
82
+ return schema;
83
+ }
84
+ /**
85
+ * Checks a schema and builds the lookups the message decoder uses. Refuses
86
+ * empty or repeated definition names, repeated field names, repeated enum
87
+ * numbers, message field ids that are 0 or repeated, unknown field types,
88
+ * array enum values, and structs that contain themselves (they would have no
89
+ * finite encoding).
90
+ */
91
+ export function indexKiwiSchema(schema, offsets) {
92
+ const definitionAt = (index) => offsets?.definitions[index] ?? null;
93
+ const fieldAt = (index, fieldIndex) => offsets?.fields[index]?.[fieldIndex] ?? null;
94
+ const byName = new Map();
95
+ schema.definitions.forEach((definition, index) => {
96
+ if (definition.name === "")
97
+ throw new KiwiDecodeError("a definition has an empty name", definitionAt(index));
98
+ if (KIWI_NATIVE_TYPES.includes(definition.name)) {
99
+ throw new KiwiDecodeError(`definition name ${definition.name} is a built-in type`, definitionAt(index));
100
+ }
101
+ if (byName.has(definition.name)) {
102
+ throw new KiwiDecodeError(`definition name ${definition.name} is used twice`, definitionAt(index));
103
+ }
104
+ if (!KIWI_DEFINITION_KINDS.includes(definition.kind)) {
105
+ throw new KiwiDecodeError(`unknown definition kind ${String(definition.kind)}`, definitionAt(index));
106
+ }
107
+ byName.set(definition.name, {
108
+ name: definition.name,
109
+ kind: definition.kind,
110
+ fields: [],
111
+ fieldsById: new Map(),
112
+ enumNames: new Map(),
113
+ minBytes: 1,
114
+ });
115
+ });
116
+ schema.definitions.forEach((definition, index) => {
117
+ const indexed = byName.get(definition.name);
118
+ const names = new Set();
119
+ definition.fields.forEach((field, fieldIndex) => {
120
+ const fail = (problem) => new KiwiDecodeError(problem, fieldAt(index, fieldIndex)).within(field.name).within(definition.name);
121
+ if (field.name === "")
122
+ throw fail("a field has an empty name");
123
+ if (names.has(field.name))
124
+ throw fail(`field name ${field.name} is used twice`);
125
+ names.add(field.name);
126
+ if (!Number.isInteger(field.value) || field.value < 0 || field.value > 0xffffffff) {
127
+ throw fail(`field value ${field.value} is not a uint`);
128
+ }
129
+ if (definition.kind === "ENUM") {
130
+ if (field.isArray)
131
+ throw fail("an enum value cannot be an array");
132
+ if (indexed.enumNames.has(field.value))
133
+ throw fail(`enum number ${field.value} is used twice`);
134
+ indexed.enumNames.set(field.value, field.name);
135
+ return;
136
+ }
137
+ const type = field.type ?? "";
138
+ const native = KIWI_NATIVE_TYPES.includes(type) ? type : null;
139
+ const target = native === null ? byName.get(type) : undefined;
140
+ if (native === null && target === undefined)
141
+ throw fail(`unknown type ${type || "(none)"}`);
142
+ const resolved = {
143
+ name: field.name,
144
+ isArray: field.isArray,
145
+ native,
146
+ definition: target ?? null,
147
+ };
148
+ indexed.fields.push(resolved);
149
+ if (definition.kind === "MESSAGE") {
150
+ if (field.value === 0)
151
+ throw fail("message field id 0 is reserved for the end marker");
152
+ if (indexed.fieldsById.has(field.value))
153
+ throw fail(`message field id ${field.value} is used twice`);
154
+ indexed.fieldsById.set(field.value, resolved);
155
+ }
156
+ });
157
+ });
158
+ // Every native type, enum and message takes at least 1 byte; a struct takes
159
+ // the sum of its fields, where an array takes at least its 1-byte length.
160
+ const state = new Map();
161
+ const positions = new Map();
162
+ schema.definitions.forEach((definition, index) => positions.set(byName.get(definition.name), index));
163
+ // Walked with an explicit stack, not recursion, so a long chain of structs
164
+ // that each hold the next cannot exhaust the call stack.
165
+ const structBytes = (root) => {
166
+ if (state.get(root) === "done")
167
+ return;
168
+ const stack = [];
169
+ const enter = (definition) => {
170
+ if (state.get(definition) === "visiting") {
171
+ throw new KiwiDecodeError(`struct ${definition.name} contains itself`, definitionAt(positions.get(definition) ?? -1));
172
+ }
173
+ state.set(definition, "visiting");
174
+ stack.push({ definition, next: 0, total: 0 });
175
+ };
176
+ enter(root);
177
+ while (stack.length > 0) {
178
+ const frame = stack[stack.length - 1];
179
+ if (frame.next < frame.definition.fields.length) {
180
+ const field = frame.definition.fields[frame.next];
181
+ frame.next += 1;
182
+ const target = field.definition;
183
+ if (field.isArray || target === null || target.kind !== "STRUCT") {
184
+ frame.total += 1;
185
+ }
186
+ else if (state.get(target) === "done") {
187
+ frame.total += target.minBytes;
188
+ }
189
+ else {
190
+ // Counted when the target finishes, below.
191
+ frame.next -= 1;
192
+ enter(target);
193
+ }
194
+ continue;
195
+ }
196
+ frame.definition.minBytes = frame.total;
197
+ state.set(frame.definition, "done");
198
+ stack.pop();
199
+ }
200
+ };
201
+ for (const indexed of byName.values()) {
202
+ if (indexed.kind === "STRUCT")
203
+ structBytes(indexed);
204
+ }
205
+ return byName;
206
+ }
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Kiwi is a schema-based binary format (https://github.com/evanw/kiwi). A
3
+ * Kiwi file carries its schema in binary form next to the data, so a reader
4
+ * decodes the schema first and then decodes the message with it.
5
+ */
6
+ /** The built-in types, in the order the binary schema numbers them. */
7
+ export declare const KIWI_NATIVE_TYPES: readonly ["bool", "byte", "int", "uint", "float", "string", "int64", "uint64"];
8
+ export type KiwiNativeType = (typeof KIWI_NATIVE_TYPES)[number];
9
+ /** The definition kinds, in the order the binary schema numbers them. */
10
+ export declare const KIWI_DEFINITION_KINDS: readonly ["ENUM", "STRUCT", "MESSAGE"];
11
+ export type KiwiDefinitionKind = (typeof KIWI_DEFINITION_KINDS)[number];
12
+ export interface KiwiField {
13
+ readonly name: string;
14
+ /**
15
+ * A native type name or the name of another definition. `null` for the
16
+ * values of an enum, which have no type.
17
+ */
18
+ readonly type: string | null;
19
+ readonly isArray: boolean;
20
+ /**
21
+ * The field id for a message field, the number for an enum value, and the
22
+ * position (starting at 1) for a struct field. Struct fields are always
23
+ * read in the order they are listed, whatever this number is.
24
+ */
25
+ readonly value: number;
26
+ }
27
+ export interface KiwiDefinition {
28
+ readonly name: string;
29
+ readonly kind: KiwiDefinitionKind;
30
+ readonly fields: readonly KiwiField[];
31
+ }
32
+ export interface KiwiSchema {
33
+ readonly definitions: readonly KiwiDefinition[];
34
+ }
35
+ /**
36
+ * A decoded value. `int64` and `uint64` become `bigint`, enum values become
37
+ * the value's name, and `byte[]` becomes a `Uint8Array` copy.
38
+ */
39
+ export type KiwiValue = boolean | number | bigint | string | Uint8Array | KiwiObject | KiwiValue[];
40
+ /**
41
+ * A decoded struct or message. A struct has every field; a message has only
42
+ * the fields present in the bytes.
43
+ */
44
+ export interface KiwiObject {
45
+ [field: string]: KiwiValue;
46
+ }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Kiwi is a schema-based binary format (https://github.com/evanw/kiwi). A
3
+ * Kiwi file carries its schema in binary form next to the data, so a reader
4
+ * decodes the schema first and then decodes the message with it.
5
+ */
6
+ /** The built-in types, in the order the binary schema numbers them. */
7
+ export const KIWI_NATIVE_TYPES = [
8
+ "bool",
9
+ "byte",
10
+ "int",
11
+ "uint",
12
+ "float",
13
+ "string",
14
+ "int64",
15
+ "uint64",
16
+ ];
17
+ /** The definition kinds, in the order the binary schema numbers them. */
18
+ export const KIWI_DEFINITION_KINDS = ["ENUM", "STRUCT", "MESSAGE"];
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Node fields owned by the person's editing, never shown to or changed by
3
+ * agents. Every agent surface reads and writes nodes through this list.
4
+ */
5
+ /**
6
+ * Node fields owned by the person's editing, never shown to or changed by
7
+ * agents: an instance's adjustments inside a prototype
8
+ * (`InstanceNode.adjustments`). An agent read leaves them out, and an agent
9
+ * write neither sets nor removes them.
10
+ * @since 0.43.0
11
+ */
12
+ export declare const AGENT_HIDDEN_NODE_FIELDS: readonly string[];
13
+ /** Whether `field` is one of `AGENT_HIDDEN_NODE_FIELDS`. @since 0.43.0 */
14
+ export declare function isAgentHiddenNodeField(field: string): boolean;
15
+ /**
16
+ * `node` without the `AGENT_HIDDEN_NODE_FIELDS`, for an agent to read; the
17
+ * same object when it has none of them.
18
+ * @since 0.43.0
19
+ */
20
+ export declare function withoutAgentHiddenFields<Node extends object>(node: Node): Node;
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Node fields owned by the person's editing, never shown to or changed by
3
+ * agents. Every agent surface reads and writes nodes through this list.
4
+ */
5
+ /**
6
+ * Node fields owned by the person's editing, never shown to or changed by
7
+ * agents: an instance's adjustments inside a prototype
8
+ * (`InstanceNode.adjustments`). An agent read leaves them out, and an agent
9
+ * write neither sets nor removes them.
10
+ * @since 0.43.0
11
+ */
12
+ export const AGENT_HIDDEN_NODE_FIELDS = Object.freeze(["adjustments"]);
13
+ /** Whether `field` is one of `AGENT_HIDDEN_NODE_FIELDS`. @since 0.43.0 */
14
+ export function isAgentHiddenNodeField(field) {
15
+ return AGENT_HIDDEN_NODE_FIELDS.includes(field);
16
+ }
17
+ /**
18
+ * `node` without the `AGENT_HIDDEN_NODE_FIELDS`, for an agent to read; the
19
+ * same object when it has none of them.
20
+ * @since 0.43.0
21
+ */
22
+ export function withoutAgentHiddenFields(node) {
23
+ if (!AGENT_HIDDEN_NODE_FIELDS.some((field) => Object.hasOwn(node, field)))
24
+ return node;
25
+ const rest = { ...node };
26
+ for (const field of AGENT_HIDDEN_NODE_FIELDS)
27
+ delete rest[field];
28
+ return rest;
29
+ }
@@ -0,0 +1,23 @@
1
+ import type { AssetId, DesignDocument, NodeId } from "./types.ts";
2
+ /**
3
+ * The nodes of `document` that use the asset `assetId` (`nodeUsesAsset`),
4
+ * the ones that name it while the document lacks it too.
5
+ */
6
+ export declare function nodesUsingAsset(document: DesignDocument, assetId: AssetId): readonly NodeId[];
7
+ /**
8
+ * The video layers of `document`, found through the index of the nodes that
9
+ * use each asset: each video asset of the document is asked for, so a
10
+ * document made by an edit lists no layers to find them. A video layer must
11
+ * name a video asset the document has (the codec and edits refuse one that
12
+ * does not), so every video layer of a valid document is found.
13
+ */
14
+ export declare function videoNodesOf(document: DesignDocument): readonly NodeId[];
15
+ /**
16
+ * Keeps the index of `derived`, made by an edit of `base`, when `base` has
17
+ * one: so a run of edits keeps it, reading only the nodes each edit wrote,
18
+ * and the next change of an asset reads no other node. After a theme change
19
+ * it is kept only when `themeUsersListed`: the edit's changed nodes then
20
+ * list every node that uses a part of the theme that changed
21
+ * (`nodesUsingThemeChange`), and what any other node uses is the same.
22
+ */
23
+ export declare function keepAssetUsers(base: DesignDocument, derived: DesignDocument, themeUsersListed?: boolean): void;
@@ -0,0 +1,128 @@
1
+ import { derivations, isFrozenNodeMap, MAX_DERIVATION_STEPS } from "./derivations.js";
2
+ import { entryOf } from "./id-table.js";
3
+ import { tableValues } from "./shared-node-table.js";
4
+ import { assetsUsedByNode, modelApplyStats } from "./operations.js";
5
+ import { NodeUsers, sameKeys } from "./node-users-index.js";
6
+ /** The index of each frozen document, with the theme it was read with. */
7
+ const indexes = new WeakMap();
8
+ function usesOf(document, id) {
9
+ const node = entryOf(document.nodes, id);
10
+ if (node === undefined)
11
+ return null;
12
+ const assets = assetsUsedByNode(document, node);
13
+ return assets.length === 0 ? null : assets;
14
+ }
15
+ /** `users` with the nodes `ids` read again from `document`. */
16
+ function withNodes(users, document, ids) {
17
+ const changes = new Map();
18
+ for (const id of ids) {
19
+ if (changes.has(id))
20
+ continue;
21
+ const uses = usesOf(document, id);
22
+ if (!sameKeys(uses, users.keysOf(id)))
23
+ changes.set(id, uses);
24
+ }
25
+ return users.with(changes);
26
+ }
27
+ function wholeIndex(document) {
28
+ modelApplyStats.assetUsersWholeDocumentScans += 1;
29
+ const assetsById = new Map();
30
+ for (const node of tableValues(document.nodes)) {
31
+ const assets = assetsUsedByNode(document, node);
32
+ if (assets.length > 0)
33
+ assetsById.set(node.id, assets);
34
+ }
35
+ return new NodeUsers(undefined, assetsById);
36
+ }
37
+ function canKeep(document) {
38
+ return Object.isFrozen(document) && isFrozenNodeMap(document.nodes);
39
+ }
40
+ /**
41
+ * The index of `document`: kept, or made from the index of the nearest
42
+ * document it was made from by edits (as far as `MAX_DERIVATION_STEPS`
43
+ * steps reach) with the same theme, or else read from every node of the
44
+ * earliest of them. Each document on the way from there keeps its own
45
+ * index, so the documents later made from any of them by edits
46
+ * (`keepAssetUsers`), as the layout views are made from earlier views, find
47
+ * one without reading every node again. Only a frozen document keeps its
48
+ * index.
49
+ */
50
+ function assetUsersOf(document) {
51
+ const kept = indexes.get(document);
52
+ if (kept !== undefined && kept.theme === document.theme)
53
+ return kept.users;
54
+ if (!canKeep(document))
55
+ return wholeIndex(document);
56
+ const chain = [document];
57
+ let users;
58
+ for (let step = 0; step < MAX_DERIVATION_STEPS; step += 1) {
59
+ const base = derivations.get(chain[chain.length - 1])?.base.deref();
60
+ if (base === undefined || !canKeep(base) || base.theme !== document.theme)
61
+ break;
62
+ const baseIndex = indexes.get(base);
63
+ if (baseIndex !== undefined && baseIndex.theme === document.theme) {
64
+ users = baseIndex.users;
65
+ break;
66
+ }
67
+ chain.push(base);
68
+ }
69
+ if (users === undefined) {
70
+ const earliest = chain.pop();
71
+ users = wholeIndex(earliest);
72
+ indexes.set(earliest, { theme: document.theme, users });
73
+ }
74
+ for (let index = chain.length - 1; index >= 0; index -= 1) {
75
+ const derived = chain[index];
76
+ users = withNodes(users, derived, derivations.get(derived).changedNodeIds);
77
+ indexes.set(derived, { theme: document.theme, users });
78
+ }
79
+ return users;
80
+ }
81
+ /**
82
+ * The nodes of `document` that use the asset `assetId` (`nodeUsesAsset`),
83
+ * the ones that name it while the document lacks it too.
84
+ */
85
+ export function nodesUsingAsset(document, assetId) {
86
+ return assetUsersOf(document).usersOf(assetId);
87
+ }
88
+ /**
89
+ * The video layers of `document`, found through the index of the nodes that
90
+ * use each asset: each video asset of the document is asked for, so a
91
+ * document made by an edit lists no layers to find them. A video layer must
92
+ * name a video asset the document has (the codec and edits refuse one that
93
+ * does not), so every video layer of a valid document is found.
94
+ */
95
+ export function videoNodesOf(document) {
96
+ let found;
97
+ // One index for every asset: a document that is not frozen keeps none, so
98
+ // asking `nodesUsingAsset` for each would read every node once per asset.
99
+ let users;
100
+ for (const asset of Object.values(document.assets)) {
101
+ if (asset.kind !== "video")
102
+ continue;
103
+ users ??= assetUsersOf(document);
104
+ for (const id of users.usersOf(asset.id)) {
105
+ const node = entryOf(document.nodes, id);
106
+ if (node?.type === "video" && node.assetId === asset.id)
107
+ (found ??= []).push(id);
108
+ }
109
+ }
110
+ return found ?? [];
111
+ }
112
+ /**
113
+ * Keeps the index of `derived`, made by an edit of `base`, when `base` has
114
+ * one: so a run of edits keeps it, reading only the nodes each edit wrote,
115
+ * and the next change of an asset reads no other node. After a theme change
116
+ * it is kept only when `themeUsersListed`: the edit's changed nodes then
117
+ * list every node that uses a part of the theme that changed
118
+ * (`nodesUsingThemeChange`), and what any other node uses is the same.
119
+ */
120
+ export function keepAssetUsers(base, derived, themeUsersListed = false) {
121
+ const kept = indexes.get(base);
122
+ if (kept === undefined || (kept.theme !== derived.theme && !themeUsersListed) || !canKeep(derived) || indexes.has(derived))
123
+ return;
124
+ const derivation = derivations.get(derived);
125
+ if (derivation === undefined || derivation.base.deref() !== base)
126
+ return;
127
+ indexes.set(derived, { theme: derived.theme, users: withNodes(kept.users, derived, derivation.changedNodeIds) });
128
+ }
@@ -0,0 +1,32 @@
1
+ import type { NodeResizing, SceneNode, StackAutoLayout } from "./types.ts";
2
+ /**
3
+ * Shared auto layout rules that both the layout (`apps/web`) and the editor's
4
+ * drop slots need, so they break a wrapping row or column into the same lines.
5
+ */
6
+ /** A size axis of a node: its width or its height. */
7
+ export type LayoutAxis = "width" | "height";
8
+ /** The node's Fill flags and size limits, or `undefined` for a path. */
9
+ export declare function nodeResizing(node: SceneNode): NodeResizing | undefined;
10
+ /**
11
+ * Whether auto layout may give `node` a Fill size: its box must be its frame
12
+ * (no path, no rotation), and it must not be a group or connector, whose size
13
+ * comes from other nodes.
14
+ */
15
+ export declare function canFillParent(node: SceneNode): boolean;
16
+ /** Whether `node` fills its auto layout parent on `axis`. */
17
+ export declare function fillsAxis(node: SceneNode, axis: LayoutAxis): boolean;
18
+ /**
19
+ * The main-axis size a stack child takes when lines are broken: its box size,
20
+ * or its minimum size when it fills the main axis.
21
+ */
22
+ export declare function stackLineBreakSize(node: SceneNode, mainAxis: LayoutAxis): number;
23
+ /**
24
+ * Breaks a row or column into lines, as Figma's wrap does: children stay in
25
+ * order and a child starts a new line when it would overflow `availableMain`
26
+ * (the frame's main size without padding). An automatic gap (space between,
27
+ * around or evenly) counts as zero here. Without wrap there is one line.
28
+ *
29
+ * `sizes` are the children's main sizes from `stackLineBreakSize`. Returns the
30
+ * children's indexes, grouped by line; no children gives no lines.
31
+ */
32
+ export declare function stackLineBreaks(layout: Pick<StackAutoLayout, "gap" | "justify" | "wrap">, availableMain: number, sizes: readonly number[]): number[][];
@@ -0,0 +1,70 @@
1
+ import { getNodeBounds } from "./geometry.js";
2
+ /** How much a line may overflow before the next child moves to a new line. */
3
+ const LINE_BREAK_TOLERANCE = 1e-6;
4
+ /** The node's Fill flags and size limits, or `undefined` for a path. */
5
+ export function nodeResizing(node) {
6
+ return node.type === "shape" && node.shape === "path" ? undefined : node.resizing;
7
+ }
8
+ /**
9
+ * Whether auto layout may give `node` a Fill size: its box must be its frame
10
+ * (no path, no rotation), and it must not be a group or connector, whose size
11
+ * comes from other nodes.
12
+ */
13
+ export function canFillParent(node) {
14
+ if (node.type === "group" || node.type === "connector")
15
+ return false;
16
+ if (node.type === "shape" && node.shape === "path")
17
+ return false;
18
+ return node.rotation === 0;
19
+ }
20
+ /** Whether `node` fills its auto layout parent on `axis`. */
21
+ export function fillsAxis(node, axis) {
22
+ // A line's height is always 0, so it fills only its length.
23
+ if (axis === "height" && node.type === "shape" && node.shape === "line")
24
+ return false;
25
+ return canFillParent(node) && nodeResizing(node)?.[axis] === "fill";
26
+ }
27
+ /**
28
+ * The main-axis size a stack child takes when lines are broken: its box size,
29
+ * or its minimum size when it fills the main axis.
30
+ */
31
+ export function stackLineBreakSize(node, mainAxis) {
32
+ if (fillsAxis(node, mainAxis)) {
33
+ const resizing = nodeResizing(node);
34
+ return (mainAxis === "width" ? resizing?.minWidth : resizing?.minHeight) ?? 0;
35
+ }
36
+ const box = getNodeBounds(node);
37
+ return mainAxis === "width" ? box.width : box.height;
38
+ }
39
+ /**
40
+ * Breaks a row or column into lines, as Figma's wrap does: children stay in
41
+ * order and a child starts a new line when it would overflow `availableMain`
42
+ * (the frame's main size without padding). An automatic gap (space between,
43
+ * around or evenly) counts as zero here. Without wrap there is one line.
44
+ *
45
+ * `sizes` are the children's main sizes from `stackLineBreakSize`. Returns the
46
+ * children's indexes, grouped by line; no children gives no lines.
47
+ */
48
+ export function stackLineBreaks(layout, availableMain, sizes) {
49
+ const autoGap = layout.justify === "space-between"
50
+ || layout.justify === "space-around"
51
+ || layout.justify === "space-evenly";
52
+ const gap = autoGap ? 0 : layout.gap;
53
+ const lines = [];
54
+ let current = [];
55
+ let used = 0;
56
+ sizes.forEach((size, index) => {
57
+ const next = current.length === 0 ? size : used + gap + size;
58
+ if (layout.wrap === true && current.length > 0 && next > availableMain + LINE_BREAK_TOLERANCE) {
59
+ lines.push(current);
60
+ current = [index];
61
+ used = size;
62
+ return;
63
+ }
64
+ current.push(index);
65
+ used = next;
66
+ });
67
+ if (current.length > 0)
68
+ lines.push(current);
69
+ return lines;
70
+ }