@astryxdesign/cli 0.7.18-canary.gfca8508 → 0.7.18

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 (341) hide show
  1. package/CHANGELOG.md +185 -19
  2. package/README.md +19 -19
  3. package/api/component/_adapter.d.mts +2 -1
  4. package/api/component/_adapter.mjs +2 -1
  5. package/api/docs/_adapter.d.mts +6 -5
  6. package/api/docs/_adapter.mjs +36 -5
  7. package/api/doctor/_adapter.d.mts +6 -3
  8. package/api/doctor/_adapter.mjs +13 -5
  9. package/api/doctor/doctor.mjs +2 -2
  10. package/api/doctor/font-packages.d.mts +36 -0
  11. package/api/doctor/font-packages.mjs +124 -0
  12. package/api/doctor/theme-checks.mjs +110 -49
  13. package/api/experimental.d.mts +0 -2
  14. package/api/experimental.mjs +0 -12
  15. package/api/icon/_adapter.d.mts +1 -1
  16. package/api/icon/_adapter.mjs +1 -0
  17. package/api/icon/_authoring.mjs +17 -8
  18. package/api/icon/_default-look.mjs +15 -15
  19. package/api/icon/add/add.mjs +9 -4
  20. package/api/icon/exportIconIndex.doc.mjs +1 -1
  21. package/api/icon/icon.doc.mjs +1 -1
  22. package/api/icon/icon.type.d.mts +33 -33
  23. package/api/icon/icon.type.mjs +45 -45
  24. package/api/icon/iconAdd.doc.mjs +1 -1
  25. package/api/icon/iconNewSet.doc.mjs +3 -3
  26. package/api/icon/iconUse.doc.mjs +5 -5
  27. package/api/icon/rankIconIndex.doc.mjs +1 -1
  28. package/api/icon/use/use.mjs +4 -4
  29. package/api/init/run/run.mjs +0 -1
  30. package/api/integration/add-helpers.mjs +1 -1
  31. package/api/render/_adapter.mjs +7 -1
  32. package/api/search/ORDERING.md +1 -1
  33. package/api/search/_candidate-score.mjs +20 -0
  34. package/api/search/_words.d.mts +8 -0
  35. package/api/search/_words.mjs +15 -0
  36. package/api/template/template.type.d.mts +2 -2
  37. package/api/template/template.type.mjs +2 -2
  38. package/api/theme/_adapter.mjs +19 -11
  39. package/api/theme/build/build.d.mts +19 -51
  40. package/api/theme/build/build.mjs +195 -189
  41. package/api/theme/build/font-warning.d.mts +19 -2
  42. package/api/theme/build/font-warning.mjs +46 -5
  43. package/api/theme/generateTonalPalette.doc.mjs +1 -1
  44. package/api/theme/palette/generate/derive-look.mjs +108 -37
  45. package/api/theme/palette/generate/mapping.mjs +2 -0
  46. package/api/theme/template/template.d.mts +0 -17
  47. package/api/theme/template/template.mjs +1 -42
  48. package/api/theme/theme.type.d.mts +5 -4
  49. package/api/theme/theme.type.mjs +5 -4
  50. package/api/theme/themePaletteGenerate.doc.mjs +1 -1
  51. package/api/theme/themeRemove.doc.mjs +1 -1
  52. package/assets/codemods/transforms/v0.7.0/preserve-bottom-sheet-content-padding.mjs +3 -1
  53. package/assets/component-list.generated.json +1 -1
  54. package/assets/discover/official-packages.json +26 -2
  55. package/assets/docs/author-a-theme.doc.mjs +6 -5
  56. package/assets/docs/browser-support.doc.mjs +1 -0
  57. package/assets/docs/icons.doc.mjs +8 -7
  58. package/assets/docs/motion.doc.mjs +46 -0
  59. package/assets/docs/theme.doc.dense.mjs +6 -17
  60. package/assets/docs/theme.doc.mjs +6 -16
  61. package/assets/docs/theme.doc.zh.mjs +6 -16
  62. package/assets/docs/tree/ci-and-containers.doc.mjs +1 -1
  63. package/assets/docs/tree/font-setup.doc.mjs +30 -0
  64. package/assets/docs/tree/icon-sets.doc.mjs +1 -1
  65. package/assets/docs/tree/template-icons.doc.mjs +3 -3
  66. package/assets/docs/tree/tokens-color.doc.mjs +5 -0
  67. package/assets/docs/tree/tokens.doc.mjs +4 -3
  68. package/assets/docs/tree/updates.doc.mjs +1 -1
  69. package/assets/docs-routes.generated.json +1 -1
  70. package/assets/docs.generated.json +2 -2
  71. package/assets/icons/definitions/drag.doc.mjs +1 -0
  72. package/assets/search-catalog.generated.json +2 -2
  73. package/assets/self-audit.generated.json +1 -1
  74. package/assets/templates/blocks/components/Card/SelectableCardElevated.doc.mjs +1 -1
  75. package/assets/templates/blocks/components/Card/SelectableCardElevated.tsx +20 -10
  76. package/assets/templates/blocks/components/Card/SelectableCardShowcase.doc.mjs +1 -1
  77. package/assets/templates/blocks/components/Card/SelectableCardShowcase.tsx +36 -16
  78. package/assets/templates/blocks/components/ChatComposerInput/ChatComposerInputMentionTrigger.tsx +6 -0
  79. package/assets/templates/blocks/components/ChatComposerInput/ChatComposerInputMultipleTriggers.tsx +6 -0
  80. package/assets/templates/blocks/components/Hooks/useIndicatorFocusRingHookUsage.doc.mjs +14 -0
  81. package/assets/templates/blocks/components/Hooks/useIndicatorFocusRingHookUsage.tsx +63 -0
  82. package/assets/templates/blocks/components/Hooks/useLongPressHookUsage.doc.mjs +29 -0
  83. package/assets/templates/blocks/components/Hooks/useLongPressHookUsage.tsx +237 -0
  84. package/assets/templates/blocks/components/SideNav/SideNavShowcase.tsx +10 -72
  85. package/assets/templates/blocks/components/SideNav/SideNavWithHeaderMenu.doc.mjs +1 -1
  86. package/assets/templates/blocks/components/SideNav/SideNavWithHeaderMenu.tsx +7 -7
  87. package/assets/templates/blocks/components/Table/TableCustomRowTable.doc.mjs +14 -0
  88. package/assets/templates/blocks/components/Table/TableCustomRowTable.tsx +96 -0
  89. package/assets/templates/components.generated.json +1 -1
  90. package/assets/templates/pages/ai-chat-landing/page.tsx +4 -0
  91. package/assets/templates/pages/dashboard-scorecard/page.tsx +26 -7
  92. package/assets/templates/pages/editor/page.tsx +13 -3
  93. package/assets/templates/themes/butter/butterPaletteRefs.generated.ts +2 -2
  94. package/assets/templates/themes/butter/butterTheme.ts +12 -10
  95. package/assets/templates/themes/butter/icons.tsx +2 -0
  96. package/assets/templates/themes/chocolate/chocolateTheme.ts +1 -0
  97. package/assets/templates/themes/chocolate/icons.tsx +2 -0
  98. package/assets/templates/themes/gothic/gothicPaletteRefs.generated.ts +1 -1
  99. package/assets/templates/themes/gothic/gothicTheme.ts +1 -0
  100. package/assets/templates/themes/gothic/icons.tsx +2 -0
  101. package/assets/templates/themes/matcha/icons.tsx +2 -0
  102. package/assets/templates/themes/matcha/matchaPaletteRefs.generated.ts +2 -2
  103. package/assets/templates/themes/matcha/matchaTheme.ts +1 -0
  104. package/assets/templates/themes/neutral/icons.tsx +2 -0
  105. package/assets/templates/themes/neutral/neutralPaletteRefs.generated.ts +2 -2
  106. package/assets/templates/themes/neutral/neutralTheme.ts +1 -0
  107. package/assets/templates/themes/stone/icons.tsx +2 -0
  108. package/assets/templates/themes/stone/stonePaletteRefs.generated.ts +1 -1
  109. package/assets/templates/themes/stone/stoneTheme.ts +1 -0
  110. package/assets/templates/themes/y2k/icons.tsx +2 -0
  111. package/assets/templates/themes/y2k/y2kTheme.ts +1 -0
  112. package/assets/theme.template.ts +9 -5
  113. package/authoring/config/parse.mjs +1 -1
  114. package/authoring/doctypes/base/type.ts +2 -3
  115. package/authoring/doctypes/component/component.doc.mjs +1 -1
  116. package/authoring/doctypes/icon-set/icon-names.mjs +1 -0
  117. package/authoring/integration/integration.doc.mjs +1 -1
  118. package/authoring/integration/type.ts +1 -1
  119. package/clients/cli/build-id.generated.json +1 -1
  120. package/clients/cli/commands/experimental/icon.mjs +7 -7
  121. package/clients/cli/commands/experimental/index.mjs +0 -5
  122. package/clients/cli/commands/experimental-icon-new-set.doc.mjs +2 -2
  123. package/clients/cli/commands/experimental-icon-use.doc.mjs +4 -4
  124. package/clients/cli/commands/experimental-icon.doc.mjs +1 -1
  125. package/clients/cli/commands/experimental.doc.mjs +2 -6
  126. package/clients/cli/commands/template.mjs +1 -1
  127. package/clients/cli/commands/theme-build.doc.mjs +8 -1
  128. package/clients/cli/commands/theme-palette-generate.doc.mjs +1 -1
  129. package/clients/cli/commands/theme-remove.doc.mjs +1 -1
  130. package/clients/cli/commands/theme.doc.mjs +1 -11
  131. package/clients/cli/commands/theme.mjs +110 -475
  132. package/clients/cli/commands.generated.json +1 -1
  133. package/clients/cli/index.mjs +4 -34
  134. package/clients/cli/lib/base-gate.mjs +2 -3
  135. package/clients/cli/lib/cli-error.mjs +8 -72
  136. package/clients/cli/lib/define-command.mjs +0 -19
  137. package/clients/cli/lib/json-shim.mjs +11 -633
  138. package/clients/cli/lib/manifest.mjs +0 -4
  139. package/clients/cli/lib/stray-option.mjs +1 -2
  140. package/clients/cli/manifest.generated.json +1 -1
  141. package/foundation/agent-docs/agent-docs.mjs +1 -1
  142. package/foundation/color/css-color.mjs +2 -3
  143. package/foundation/discovery/app-layout.d.mts +13 -0
  144. package/foundation/discovery/app-layout.mjs +35 -0
  145. package/foundation/discovery/fixture-refs.d.mts +51 -0
  146. package/foundation/discovery/fixture-refs.mjs +351 -0
  147. package/foundation/discovery/icon-definition-discovery.mjs +1 -1
  148. package/foundation/discovery/icon-ranking.mjs +2 -2
  149. package/foundation/discovery/icon-set-checks.mjs +1 -1
  150. package/foundation/discovery/icon-set-discovery.mjs +1 -1
  151. package/foundation/discovery/icon-set-writer.d.mts +24 -7
  152. package/foundation/discovery/icon-set-writer.mjs +66 -22
  153. package/foundation/discovery/static-descriptor.d.mts +49 -0
  154. package/foundation/discovery/static-descriptor.mjs +548 -0
  155. package/foundation/discovery/template-adapter.d.mts +0 -47
  156. package/foundation/discovery/template-adapter.mjs +1 -333
  157. package/foundation/discovery/theme-discovery.d.mts +6 -47
  158. package/foundation/discovery/theme-discovery.mjs +15 -537
  159. package/foundation/doc-compiler/import.mjs +1 -1
  160. package/foundation/env/user-dirs.d.mts +5 -2
  161. package/foundation/env/user-dirs.mjs +12 -5
  162. package/foundation/fonts/next-font.d.mts +26 -0
  163. package/foundation/fonts/next-font.mjs +149 -0
  164. package/foundation/fs/empty-folder.mjs +2 -2
  165. package/foundation/fs/read-record.mjs +11 -4
  166. package/foundation/pictures/pictures.d.mts +1 -45
  167. package/foundation/pictures/pictures.mjs +8 -8
  168. package/foundation/release/folder-lock.mjs +3 -2
  169. package/foundation/render/cut-short.d.mts +2 -2
  170. package/foundation/render/cut-short.mjs +2 -2
  171. package/foundation/render/drawn-contrast.d.mts +2 -2
  172. package/foundation/render/drawn-contrast.mjs +2 -2
  173. package/foundation/render/font-probe.d.mts +2 -2
  174. package/foundation/render/font-probe.mjs +2 -2
  175. package/foundation/render/layers.mjs +1 -2
  176. package/foundation/render/package-resolve-hooks.d.mts +25 -5
  177. package/foundation/render/package-resolve-hooks.mjs +77 -14
  178. package/foundation/render/page-worker.mjs +24 -12
  179. package/foundation/render/part-boxes.d.mts +2 -3
  180. package/foundation/render/part-boxes.mjs +2 -3
  181. package/foundation/render/picture-chrome.mjs +5 -0
  182. package/foundation/render/render-page.d.mts +1 -1
  183. package/foundation/render/render-page.mjs +4 -5
  184. package/foundation/render/single-copy-hooks.d.mts +2 -0
  185. package/foundation/render/single-copy-hooks.mjs +2 -0
  186. package/foundation/response/error-codes.doc.mjs +14 -14
  187. package/foundation/response/error-codes.mjs +13 -13
  188. package/foundation/response/response-types.doc.mjs +7 -68
  189. package/foundation/runtime/css-tree.mjs +2 -2
  190. package/foundation/runtime/word-table.d.mts +1 -1
  191. package/foundation/runtime/word-table.mjs +1 -1
  192. package/foundation/suggest/other-command.d.mts +52 -15
  193. package/foundation/suggest/other-command.mjs +168 -45
  194. package/foundation/text/suggest-order.d.mts +7 -0
  195. package/foundation/text/suggest-order.mjs +1 -1
  196. package/package.json +7 -8
  197. package/api/theme-edit/_adapter.d.mts +0 -228
  198. package/api/theme-edit/_adapter.mjs +0 -2025
  199. package/api/theme-edit/_catalog.d.mts +0 -172
  200. package/api/theme-edit/_catalog.mjs +0 -806
  201. package/api/theme-edit/_check.d.mts +0 -391
  202. package/api/theme-edit/_check.mjs +0 -9370
  203. package/api/theme-edit/_color.d.mts +0 -135
  204. package/api/theme-edit/_color.mjs +0 -480
  205. package/api/theme-edit/_compiler.d.mts +0 -77
  206. package/api/theme-edit/_compiler.mjs +0 -1053
  207. package/api/theme-edit/_context.d.mts +0 -399
  208. package/api/theme-edit/_context.mjs +0 -1778
  209. package/api/theme-edit/_css.d.mts +0 -169
  210. package/api/theme-edit/_css.mjs +0 -1167
  211. package/api/theme-edit/_edit.d.mts +0 -167
  212. package/api/theme-edit/_edit.mjs +0 -11220
  213. package/api/theme-edit/_how-commands.d.mts +0 -315
  214. package/api/theme-edit/_how-commands.mjs +0 -1664
  215. package/api/theme-edit/_how-question.d.mts +0 -341
  216. package/api/theme-edit/_how-question.mjs +0 -33339
  217. package/api/theme-edit/_how-sides.d.mts +0 -22
  218. package/api/theme-edit/_how-sides.mjs +0 -173
  219. package/api/theme-edit/_how-steps.d.mts +0 -42
  220. package/api/theme-edit/_how-steps.mjs +0 -208
  221. package/api/theme-edit/_how-values.d.mts +0 -29
  222. package/api/theme-edit/_how-values.mjs +0 -111
  223. package/api/theme-edit/_how-words.d.mts +0 -15
  224. package/api/theme-edit/_how-words.mjs +0 -239
  225. package/api/theme-edit/_how.d.mts +0 -55
  226. package/api/theme-edit/_how.mjs +0 -2388
  227. package/api/theme-edit/_importers.d.mts +0 -38
  228. package/api/theme-edit/_importers.mjs +0 -188
  229. package/api/theme-edit/_marks.d.mts +0 -39
  230. package/api/theme-edit/_marks.mjs +0 -67
  231. package/api/theme-edit/_missed.d.mts +0 -56
  232. package/api/theme-edit/_missed.mjs +0 -1128
  233. package/api/theme-edit/_name-rule.d.mts +0 -11
  234. package/api/theme-edit/_name-rule.mjs +0 -12
  235. package/api/theme-edit/_normal.d.mts +0 -365
  236. package/api/theme-edit/_normal.mjs +0 -1978
  237. package/api/theme-edit/_overview.d.mts +0 -9
  238. package/api/theme-edit/_overview.mjs +0 -293
  239. package/api/theme-edit/_palette.d.mts +0 -616
  240. package/api/theme-edit/_palette.mjs +0 -2776
  241. package/api/theme-edit/_part-pieces.d.mts +0 -54
  242. package/api/theme-edit/_part-pieces.mjs +0 -179
  243. package/api/theme-edit/_part-words.d.mts +0 -39
  244. package/api/theme-edit/_part-words.mjs +0 -242
  245. package/api/theme-edit/_parts.d.mts +0 -402
  246. package/api/theme-edit/_parts.mjs +0 -2322
  247. package/api/theme-edit/_picture-traits.d.mts +0 -259
  248. package/api/theme-edit/_picture-traits.mjs +0 -1030
  249. package/api/theme-edit/_png.d.mts +0 -94
  250. package/api/theme-edit/_png.mjs +0 -479
  251. package/api/theme-edit/_preview-app.d.mts +0 -172
  252. package/api/theme-edit/_preview-app.mjs +0 -6430
  253. package/api/theme-edit/_preview.d.mts +0 -484
  254. package/api/theme-edit/_preview.mjs +0 -2775
  255. package/api/theme-edit/_project.d.mts +0 -207
  256. package/api/theme-edit/_project.mjs +0 -643
  257. package/api/theme-edit/_resolved.d.mts +0 -46
  258. package/api/theme-edit/_resolved.mjs +0 -800
  259. package/api/theme-edit/_set-line.d.mts +0 -29
  260. package/api/theme-edit/_set-line.mjs +0 -100
  261. package/api/theme-edit/_settings.d.mts +0 -2426
  262. package/api/theme-edit/_settings.mjs +0 -18449
  263. package/api/theme-edit/_tables.d.mts +0 -227
  264. package/api/theme-edit/_tables.mjs +0 -1128
  265. package/api/theme-edit/_text-looks.d.mts +0 -78
  266. package/api/theme-edit/_text-looks.mjs +0 -376
  267. package/api/theme-edit/_vet.d.mts +0 -50
  268. package/api/theme-edit/_vet.mjs +0 -166
  269. package/api/theme-edit/how/how.d.mts +0 -17
  270. package/api/theme-edit/how/how.mjs +0 -28
  271. package/api/theme-edit/new/new.d.mts +0 -11
  272. package/api/theme-edit/new/new.mjs +0 -22
  273. package/api/theme-edit/preview-add/preview-add.d.mts +0 -14
  274. package/api/theme-edit/preview-add/preview-add.mjs +0 -25
  275. package/api/theme-edit/preview-app/preview-app.d.mts +0 -13
  276. package/api/theme-edit/preview-app/preview-app.mjs +0 -24
  277. package/api/theme-edit/preview-list/preview-list.d.mts +0 -10
  278. package/api/theme-edit/preview-list/preview-list.mjs +0 -21
  279. package/api/theme-edit/preview-remove/preview-remove.d.mts +0 -12
  280. package/api/theme-edit/preview-remove/preview-remove.mjs +0 -23
  281. package/api/theme-edit/set/set.d.mts +0 -13
  282. package/api/theme-edit/set/set.mjs +0 -24
  283. package/api/theme-edit/show/show.d.mts +0 -13
  284. package/api/theme-edit/show/show.mjs +0 -24
  285. package/api/theme-edit/theme-edit.d.mts +0 -12
  286. package/api/theme-edit/theme-edit.mjs +0 -36
  287. package/api/theme-edit/theme-edit.type.d.mts +0 -730
  288. package/api/theme-edit/theme-edit.type.mjs +0 -342
  289. package/api/theme-edit/themeEditHow.doc.d.mts +0 -6
  290. package/api/theme-edit/themeEditHow.doc.mjs +0 -105
  291. package/api/theme-edit/themeEditNew.doc.d.mts +0 -5
  292. package/api/theme-edit/themeEditNew.doc.mjs +0 -112
  293. package/api/theme-edit/themeEditPreviewAdd.doc.d.mts +0 -6
  294. package/api/theme-edit/themeEditPreviewAdd.doc.mjs +0 -121
  295. package/api/theme-edit/themeEditPreviewApp.doc.d.mts +0 -6
  296. package/api/theme-edit/themeEditPreviewApp.doc.mjs +0 -87
  297. package/api/theme-edit/themeEditPreviewList.doc.d.mts +0 -6
  298. package/api/theme-edit/themeEditPreviewList.doc.mjs +0 -62
  299. package/api/theme-edit/themeEditPreviewRemove.doc.d.mts +0 -6
  300. package/api/theme-edit/themeEditPreviewRemove.doc.mjs +0 -96
  301. package/api/theme-edit/themeEditSet.doc.d.mts +0 -6
  302. package/api/theme-edit/themeEditSet.doc.mjs +0 -175
  303. package/api/theme-edit/themeEditShow.doc.d.mts +0 -6
  304. package/api/theme-edit/themeEditShow.doc.mjs +0 -118
  305. package/api/theme-edit/themeEditUndo.doc.d.mts +0 -6
  306. package/api/theme-edit/themeEditUndo.doc.mjs +0 -133
  307. package/api/theme-edit/undo/undo.d.mts +0 -13
  308. package/api/theme-edit/undo/undo.mjs +0 -24
  309. package/assets/preview-app/README.md +0 -6
  310. package/assets/preview-app/css.d.ts +0 -5
  311. package/assets/preview-app/gitignore +0 -4
  312. package/assets/preview-app/next.config.mjs +0 -12
  313. package/assets/preview-app/package.json +0 -24
  314. package/assets/preview-app/src/app/globals.css +0 -6
  315. package/assets/preview-app/src/app/layout.tsx +0 -21
  316. package/assets/preview-app/src/app/page.tsx +0 -94
  317. package/assets/preview-app/src/app/providers.tsx +0 -16
  318. package/assets/preview-app/tsconfig.json +0 -27
  319. package/clients/cli/commands/experimental/theme.mjs +0 -2990
  320. package/clients/cli/commands/experimental-theme-add.doc.mjs +0 -80
  321. package/clients/cli/commands/experimental-theme-compile.doc.mjs +0 -98
  322. package/clients/cli/commands/experimental-theme-editor-how.doc.mjs +0 -196
  323. package/clients/cli/commands/experimental-theme-editor-new.doc.mjs +0 -174
  324. package/clients/cli/commands/experimental-theme-editor-set.doc.mjs +0 -139
  325. package/clients/cli/commands/experimental-theme-editor-show.doc.mjs +0 -77
  326. package/clients/cli/commands/experimental-theme-editor-undo.doc.mjs +0 -105
  327. package/clients/cli/commands/experimental-theme-editor.doc.mjs +0 -49
  328. package/clients/cli/commands/experimental-theme-eject.doc.mjs +0 -72
  329. package/clients/cli/commands/experimental-theme-list.doc.mjs +0 -49
  330. package/clients/cli/commands/experimental-theme-preview-add.doc.mjs +0 -88
  331. package/clients/cli/commands/experimental-theme-preview-app.doc.mjs +0 -61
  332. package/clients/cli/commands/experimental-theme-preview-list.doc.mjs +0 -48
  333. package/clients/cli/commands/experimental-theme-preview-remove.doc.mjs +0 -59
  334. package/clients/cli/commands/experimental-theme-preview.doc.mjs +0 -41
  335. package/clients/cli/commands/experimental-theme-remove.doc.mjs +0 -57
  336. package/clients/cli/commands/experimental-theme-targets.doc.mjs +0 -62
  337. package/clients/cli/commands/experimental-theme-template.doc.mjs +0 -63
  338. package/clients/cli/commands/experimental-theme-use.doc.mjs +0 -57
  339. package/clients/cli/commands/experimental-theme.doc.mjs +0 -60
  340. package/foundation/fonts/fontsource-families.generated.d.mts +0 -5
  341. package/foundation/fonts/fontsource-families.generated.mjs +0 -2110
@@ -1,1030 +0,0 @@
1
- // Copyright (c) Meta Platforms, Inc. and affiliates.
2
-
3
- /**
4
- * @file Measure a look's traits from a picture's pixels, deterministically:
5
- * the boxes it draws (height, corners, edge width and contrast, fill or
6
- * gradient, shadow) and its text (size, leading, weight). No recognition of
7
- * what the picture shows: boxes are grouped by their measured shape only,
8
- * and every value says where it was read and how sure it is.
9
- * @position api/theme-edit — pure helpers for `experimental theme editor how <picture.png>`
10
- */
11
-
12
- /** @typedef {{width: number, height: number, rgba: Uint8Array}} Picture */
13
-
14
- /**
15
- * @typedef {object} Box
16
- * @property {number} id its fill surface
17
- * @property {number} x left edge, outer
18
- * @property {number} y top edge, outer
19
- * @property {number} w outer width
20
- * @property {number} h outer height
21
- * @property {number} edge edge (border) width in pixels
22
- * @property {string | null} edgeColor
23
- * @property {string} fill the fill color, or the top stop of a gradient
24
- * @property {{direction: 'vertical' | 'horizontal', stops: string[]} | null} gradient
25
- * @property {number} radius corner radius in pixels
26
- * @property {boolean} round whether the corners are half the height (a pill or circle)
27
- * @property {'none' | 'soft' | 'hard'} shadow
28
- * @property {boolean} bevel light top-left edge and dark bottom-right edge
29
- * @property {{light: string, dark: string} | null} bevelColors the bevel's edge colors next to the fill
30
- * @property {string} outside the color around the box
31
- * @property {'centered' | 'start' | 'none'} text where its text sits
32
- */
33
-
34
- /** Neighbouring pixels this close (per channel, 0–255) are one surface. */
35
- const SAME = 6;
36
-
37
- /**
38
- * The most pixels the look is measured from: about a 2x screenshot of a
39
- * large screen. Past it the colors are still read, and the look is named
40
- * as not measured.
41
- */
42
- export const MEASURE_MOST = 8_000_000;
43
-
44
- /** The most surfaces a picture may have for its boxes to be read. */
45
- const SURFACES_MOST = 1_000_000;
46
-
47
- /** A surface smaller than this many pixels on a side is never a box. */
48
- const LEAST_SIDE = 8;
49
-
50
- /** @param {number} v */
51
- const hx = v => Math.round(v).toString(16).padStart(2, '0');
52
-
53
- /**
54
- * The picture's colors over white, one byte per channel.
55
- * @param {Picture} pic
56
- */
57
- function flatten(pic) {
58
- const n = pic.width * pic.height;
59
- const rgb = new Uint8Array(n * 3);
60
- for (let i = 0; i < n; i++) {
61
- const a = pic.rgba[i * 4 + 3] / 255;
62
- for (let c = 0; c < 3; c++)
63
- rgb[i * 3 + c] = Math.round(pic.rgba[i * 4 + c] * a + 255 * (1 - a));
64
- }
65
- return rgb;
66
- }
67
-
68
- /**
69
- * The surfaces of a picture: neighbouring pixels within SAME of each other,
70
- * joined (a gradient is one surface; an edge, a glyph or its antialiasing is
71
- * not part of the surface around it).
72
- * @param {Uint8Array} rgb
73
- * @param {number} W
74
- * @param {number} H
75
- */
76
- function surfaces(rgb, W, H) {
77
- const parent = new Int32Array(W * H);
78
- for (let i = 0; i < parent.length; i++) parent[i] = i;
79
- /** @param {number} i */
80
- const find = i => {
81
- while (parent[i] !== i) {
82
- parent[i] = parent[parent[i]];
83
- i = parent[i];
84
- }
85
- return i;
86
- };
87
- /** @param {number} a @param {number} b */
88
- const near = (a, b) =>
89
- Math.abs(rgb[a * 3] - rgb[b * 3]) <= SAME &&
90
- Math.abs(rgb[a * 3 + 1] - rgb[b * 3 + 1]) <= SAME &&
91
- Math.abs(rgb[a * 3 + 2] - rgb[b * 3 + 2]) <= SAME;
92
- for (let y = 0; y < H; y++)
93
- for (let x = 0; x < W; x++) {
94
- const i = y * W + x;
95
- if (x && near(i, i - 1)) {
96
- const a = find(i), b = find(i - 1);
97
- if (a !== b) parent[a] = b;
98
- }
99
- if (y && near(i, i - W)) {
100
- const a = find(i), b = find(i - W);
101
- if (a !== b) parent[a] = b;
102
- }
103
- }
104
- const label = new Int32Array(W * H);
105
- // A grainy or photographic picture is nearly a surface per pixel: no
106
- // boxes to read, and too many to hold, so it is not read.
107
- let roots = 0;
108
- for (let i = 0; i < W * H; i++) if (find(i) === i && ++roots > SURFACES_MOST) return null;
109
- // Each root's surface number, by its pixel index (-1: none yet).
110
- const ids = new Int32Array(W * H).fill(-1);
111
- /** @type {Array<{x0: number, y0: number, x1: number, y1: number, n: number, r: number, g: number, b: number, solid: boolean}>} */
112
- const stats = [];
113
- for (let i = 0; i < W * H; i++) {
114
- const root = find(i);
115
- let id = ids[root];
116
- if (id < 0) {
117
- id = stats.length;
118
- ids[root] = id;
119
- stats.push({x0: W, y0: H, x1: -1, y1: -1, n: 0, r: 0, g: 0, b: 0, solid: false});
120
- }
121
- label[i] = id;
122
- const s = stats[id];
123
- const x = i % W, y = (i - x) / W;
124
- if (x < s.x0) s.x0 = x;
125
- if (x > s.x1) s.x1 = x;
126
- if (y < s.y0) s.y0 = y;
127
- if (y > s.y1) s.y1 = y;
128
- s.n++;
129
- s.r += rgb[i * 3];
130
- s.g += rgb[i * 3 + 1];
131
- s.b += rgb[i * 3 + 2];
132
- }
133
- return {label, stats};
134
- }
135
-
136
- /**
137
- * Distance between two colors, the largest channel difference.
138
- * @param {ArrayLike<number>} a @param {number} ai @param {ArrayLike<number>} b @param {number} bi
139
- */
140
- const dist = (a, ai, b, bi) =>
141
- Math.max(
142
- Math.abs(a[ai] - b[bi]),
143
- Math.abs(a[ai + 1] - b[bi + 1]),
144
- Math.abs(a[ai + 2] - b[bi + 2]),
145
- );
146
-
147
- /** @param {ArrayLike<number>} rgb @param {number} i */
148
- const hexAt = (rgb, i) => `#${hx(rgb[i * 3])}${hx(rgb[i * 3 + 1])}${hx(rgb[i * 3 + 2])}`;
149
-
150
- /** @param {ArrayLike<number>} c */
151
- const lum = c => 0.2126 * c[0] + 0.7152 * c[1] + 0.0722 * c[2];
152
-
153
- /**
154
- * Measure every box in a picture.
155
- * @param {Picture} pic
156
- * @returns {null | {boxes: Box[], pageId: number, page: string, rgb: Uint8Array, label: Int32Array, stats: NonNullable<ReturnType<typeof surfaces>>['stats']}}
157
- */
158
- export function measureBoxes(pic) {
159
- const W = pic.width, H = pic.height;
160
- const rgb = flatten(pic);
161
- const read = surfaces(rgb, W, H);
162
- if (!read) return null;
163
- const {label, stats} = read;
164
- let pageId = 0;
165
- for (let i = 1; i < stats.length; i++) if (stats[i].n > stats[pageId].n) pageId = i;
166
- const ps = stats[pageId];
167
- const page = `#${hx(ps.r / ps.n)}${hx(ps.g / ps.n)}${hx(ps.b / ps.n)}`;
168
- // A surface something can sit on: the page, or a fill (most of its bounds).
169
- for (let i = 0; i < stats.length; i++) {
170
- const o = stats[i];
171
- o.solid = i === pageId || o.n >= 0.45 * (o.x1 - o.x0 + 1) * (o.y1 - o.y0 + 1);
172
- }
173
- /** @param {number} x @param {number} y */
174
- const at = (x, y) => y * W + x;
175
- /** @type {Box[]} */
176
- const boxes = [];
177
- for (let id = 0; id < stats.length; id++) {
178
- if (id === pageId) continue;
179
- const s = stats[id];
180
- const w = s.x1 - s.x0 + 1, h = s.y1 - s.y0 + 1;
181
- if (w < LEAST_SIDE || h < LEAST_SIDE) continue;
182
- // A fill: most of its bounds is the surface (text holes allowed);
183
- // an edge ring or a glyph is mostly empty.
184
- if (s.n < 0.45 * w * h) continue;
185
- // A fill as wide as the picture's own edge rows is the page's band, read
186
- // as a bar still.
187
- const box = readBox(id, s, rgb, label, stats, W, H, at);
188
- if (box) boxes.push(box);
189
- }
190
- return {boxes, pageId, page, rgb, label, stats};
191
- }
192
-
193
- /**
194
- * Walk out from a fill's edge to the surface around it: the edge width is
195
- * the pixels between, the outside is the first surface whose bounds hold
196
- * the fill grown by that step.
197
- * @param {number} id
198
- * @param {{x0: number, y0: number, x1: number, y1: number}} s
199
- * @param {Int32Array} label
200
- * @param {NonNullable<ReturnType<typeof surfaces>>['stats']} stats
201
- * @param {number} W @param {number} H
202
- * @param {(x: number, y: number) => number} at
203
- * @param {number} sx start x (on the fill) @param {number} sy start y @param {number} dx @param {number} dy
204
- */
205
- function walkOut(id, s, label, stats, W, H, at, sx, sy, dx, dy) {
206
- let x = sx, y = sy;
207
- // From the fill's last pixel on this line.
208
- while (true) {
209
- const nx = x + dx, ny = y + dy;
210
- if (nx < 0 || ny < 0 || nx >= W || ny >= H) return {edge: 0, outside: -1, last: at(x, y), first: -1};
211
- if (label[at(nx, ny)] !== id) break;
212
- x = nx;
213
- y = ny;
214
- }
215
- for (let k = 1; k <= 8; k++) {
216
- const px = x + dx * k, py = y + dy * k;
217
- if (px < 0 || py < 0 || px >= W || py >= H) return {edge: k - 1, outside: -1, last: at(x, y), first: k > 1 ? at(x + dx, y + dy) : -1};
218
- const o = stats[label[at(px, py)]];
219
- // One past the step: an edge ring's bounds end exactly at its step.
220
- const grow = k + 1;
221
- // (An edge grid shared with neighbours is not a surface to sit on.)
222
- const holds =
223
- o.solid &&
224
- o.x0 <= s.x0 - grow && o.y0 <= s.y0 - grow && o.x1 >= s.x1 + grow && o.y1 >= s.y1 + grow;
225
- if (holds) return {edge: k - 1, outside: at(px, py), last: at(x, y), first: k > 1 ? at(x + dx, y + dy) : -1};
226
- }
227
- return null;
228
- }
229
-
230
- /**
231
- * @param {number} id
232
- * @param {NonNullable<ReturnType<typeof surfaces>>['stats'][number]} s
233
- * @param {Uint8Array} rgb @param {Int32Array} label
234
- * @param {NonNullable<ReturnType<typeof surfaces>>['stats']} stats
235
- * @param {number} W @param {number} H @param {(x: number, y: number) => number} at
236
- * @returns {Box | null}
237
- */
238
- function readBox(id, s, rgb, label, stats, W, H, at) {
239
- const mx = (s.x0 + s.x1) >> 1, my = (s.y0 + s.y1) >> 1;
240
- // Each side from the middle of the fill outward along its own row or column.
241
- const sides = {
242
- top: walkOut(id, s, label, stats, W, H, at, mx, s.y0 + 1, 0, -1),
243
- bottom: walkOut(id, s, label, stats, W, H, at, mx, s.y1 - 1, 0, 1),
244
- left: walkOut(id, s, label, stats, W, H, at, s.x0 + 1, my, -1, 0),
245
- right: walkOut(id, s, label, stats, W, H, at, s.x1 - 1, my, 1, 0),
246
- };
247
- if (!sides.top || !sides.bottom || !sides.left || !sides.right) return null;
248
- const t = sides.top, b = sides.bottom, l = sides.left, r = sides.right;
249
- // An edge drawn on every side; a bottom or right side wider than the
250
- // rest is a shadow there, not edge (a top highlight is the box's own).
251
- const usual = Math.ceil(median([t.edge, b.edge, l.edge, r.edge]));
252
- for (const o of [b, r]) if (o.edge > usual) o.edge = usual;
253
- const x = s.x0 - l.edge, y = s.y0 - t.edge;
254
- const w = s.x1 + r.edge - x + 1, h = s.y1 + b.edge - y + 1;
255
- const outside_ = [t, b, l, r].map(o => o.outside).find(o => o >= 0) ?? -1;
256
- if (outside_ < 0) return null;
257
- const outI = outside_ * 3;
258
- const outside = hexAt(rgb, outside_);
259
- // The edge (border) is what all four sides draw.
260
- const edge = Math.min(t.edge, b.edge, l.edge, r.edge);
261
- const edgeColor = edge && l.first >= 0 ? hexAt(rgb, l.first) : null;
262
- // Light top-left edge and dark bottom-right: a bevel.
263
- /** @param {{first: number, last: number}} o how much lighter its edge is than the fill next to it */
264
- const lift = o => (o.first >= 0 ? lum(rgb.subarray(o.first * 3)) - lum(rgb.subarray(o.last * 3)) : 0);
265
- const bevel = [t, l].every(o => lift(o) > 12) && [b, r].every(o => lift(o) < -12);
266
- // A bevel's outermost light (top) and dark (bottom) colors.
267
- const bevelColors = bevel && t.first >= 0 && b.first >= 0
268
- ? {light: hexAt(rgb, t.first), dark: hexAt(rgb, b.first)}
269
- : null;
270
- // The box's outermost color at the middle of its left side.
271
- const edgeI = (edge && l.first >= 0 ? l.first : l.last) * 3;
272
- const reach = dist(rgb, edgeI, rgb, outI);
273
- // How far each side's own outermost color is from the outside.
274
- const sideReach = [t, b, l, r].map(o => dist(rgb, (o.first >= 0 ? o.first : o.last) * 3, rgb, outI));
275
- const radius = reach < 10 ? 0 : cornerRadius(rgb, label, W, x, y, w, h, outside_, sideReach);
276
- return {
277
- id,
278
- x,
279
- y,
280
- w,
281
- h,
282
- edge,
283
- edgeColor,
284
- fill: `#${hx(s.r / s.n)}${hx(s.g / s.n)}${hx(s.b / s.n)}`,
285
- gradient: gradientOf(id, s, rgb, label, W),
286
- radius,
287
- round: radius >= h / 2 - 1,
288
- shadow: shadowBelow(rgb, W, H, x, y + h, w, outI),
289
- bevel,
290
- bevelColors,
291
- outside,
292
- text: textPlace(id, s, label, W, radius),
293
- };
294
- }
295
-
296
- /**
297
- * The corner radius from the area each corner leaves out: a quarter circle of
298
- * radius r leaves r²(1 − π/4) of its square empty.
299
- * @param {Uint8Array} rgb @param {number} W
300
- * @param {number} x @param {number} y @param {number} w @param {number} h
301
- * @param {Int32Array} label
302
- * @param {number} outAt the index of a pixel of the outside surface
303
- * @param {number[]} sideReach how far each side's own color (top, bottom, left, right) is from the outside
304
- */
305
- function cornerRadius(rgb, label, W, x, y, w, h, outAt, sideReach) {
306
- const most = Math.floor(Math.min(w, h) / 2);
307
- const out = label[outAt];
308
- const [top, bottom, left, right] = sideReach;
309
- const corners = [
310
- [x, y, 1, 1, Math.max(top, left)],
311
- [x + w - 1, y, -1, 1, Math.max(top, right)],
312
- [x, y + h - 1, 1, -1, Math.max(bottom, left)],
313
- [x + w - 1, y + h - 1, -1, -1, Math.max(bottom, right)],
314
- ];
315
- const radii = corners.map(([cx, cy, sx, sy, full]) => {
316
- // Each row in from the outer side up to the first pixel the box covers:
317
- // the empty share of the pixels passed is that row's inset. Inner
318
- // pixels are never read, so a fill the color of the page does not count.
319
- let missing = 0;
320
- let still = 0;
321
- for (let k = 0; k < most; k++) {
322
- let inset = 0;
323
- for (let i = 0; i < most; i++) {
324
- const j = (cy + sy * k) * W + cx + sx * i;
325
- if (label[j] === out) {
326
- inset++;
327
- continue;
328
- }
329
- // The first pixel off the outside: as empty as it is nearer the
330
- // outside than the box's own edge color (or the pixel after it).
331
- const here = dist(rgb, j * 3, rgb, outAt * 3);
332
- const next = dist(rgb, (j + sx) * 3, rgb, outAt * 3);
333
- inset += 1 - here / Math.max(here, next, full, 1);
334
- break;
335
- }
336
- missing += inset;
337
- if (inset < 0.05) {
338
- if (++still >= 2) break;
339
- } else still = 0;
340
- }
341
- return Math.sqrt(missing / (1 - Math.PI / 4));
342
- });
343
- radii.sort((a, b) => a - b);
344
- // The two middle corners, so one corner touching a neighbour is outvoted.
345
- return Math.round(((radii[1] + radii[2]) / 2) * 2) / 2;
346
- }
347
-
348
- /**
349
- * A fill's gradient: its own pixels down three columns (or across three rows),
350
- * top fifth against bottom fifth. Text pixels are not the fill, so they never count.
351
- * @param {number} id
352
- * @param {NonNullable<ReturnType<typeof surfaces>>['stats'][number]} s
353
- * @param {Uint8Array} rgb @param {Int32Array} label @param {number} W
354
- * @returns {Box['gradient']}
355
- */
356
- function gradientOf(id, s, rgb, label, W) {
357
- const w = s.x1 - s.x0 + 1, h = s.y1 - s.y0 + 1;
358
- /** @param {boolean} down */
359
- const run = down => {
360
- const len = down ? h : w;
361
- const across = down ? w : h;
362
- const sums = Array.from({length: len}, () => [0, 0, 0, 0]);
363
- for (const f of [0.2, 0.5, 0.8]) {
364
- const c = Math.floor(across * f);
365
- for (let k = 0; k < len; k++) {
366
- const px = down ? s.x0 + c : s.x0 + k;
367
- const py = down ? s.y0 + k : s.y0 + c;
368
- const i = py * W + px;
369
- if (label[i] !== id) continue;
370
- sums[k][0] += rgb[i * 3];
371
- sums[k][1] += rgb[i * 3 + 1];
372
- sums[k][2] += rgb[i * 3 + 2];
373
- sums[k][3]++;
374
- }
375
- }
376
- const part = (/** @type {number} */ a, /** @type {number} */ b) => {
377
- const t = [0, 0, 0, 0];
378
- for (let k = a; k < b; k++) for (let c = 0; c < 4; c++) t[c] += sums[k][c];
379
- return t[3] ? [t[0] / t[3], t[1] / t[3], t[2] / t[3]] : null;
380
- };
381
- const fifth = Math.max(1, Math.floor(len / 5));
382
- const first = part(0, fifth), last = part(len - fifth, len), mid = part((len - fifth) >> 1, ((len - fifth) >> 1) + fifth);
383
- if (!first || !last || !mid) return null;
384
- const d = Math.max(...[0, 1, 2].map(c => Math.abs(first[c] - last[c])));
385
- // Monotone: the middle lies between the ends.
386
- const between = lum(mid) >= Math.min(lum(first), lum(last)) - 2 && lum(mid) <= Math.max(lum(first), lum(last)) + 2;
387
- return d >= 10 && between ? {d, stops: [first, last].map(c => `#${c.map(hx).join('')}`)} : null;
388
- };
389
- if (h < LEAST_SIDE) return null;
390
- const v = run(true), hz = run(false);
391
- if (v && (!hz || v.d >= hz.d)) return {direction: 'vertical', stops: v.stops};
392
- if (hz) return {direction: 'horizontal', stops: hz.stops};
393
- return null;
394
- }
395
-
396
- /**
397
- * The shadow under a box: how the color below its bottom edge, down the
398
- * middle column, differs from the outside color.
399
- * @param {Uint8Array} rgb @param {number} W @param {number} H
400
- * @param {number} x @param {number} below the row under the box @param {number} w @param {number} outI
401
- * @returns {Box['shadow']}
402
- */
403
- function shadowBelow(rgb, W, H, x, below, w, outI) {
404
- const out = lum(rgb.subarray(outI, outI + 3));
405
- const steps = [];
406
- for (let k = 0; k < 10 && below + k < H; k++) {
407
- let sum = 0;
408
- for (const f of [0.3, 0.5, 0.7]) {
409
- const i = ((below + k) * W + x + Math.floor(w * f)) * 3;
410
- sum += out - lum(rgb.subarray(i, i + 3));
411
- }
412
- steps.push(sum / 3);
413
- }
414
- if (!steps.length || steps[0] < 3) return 'none';
415
- const deep = steps.findIndex(d => d < 1.5);
416
- const reach = deep < 0 ? steps.length : deep;
417
- // A hard shadow holds its darkness then stops; a soft one fades.
418
- const holds = reach >= 2 && steps[reach - 1] > steps[0] * 0.7;
419
- return holds ? 'hard' : 'soft';
420
- }
421
-
422
- /**
423
- * Where a box's text sits: the holes in its fill, left gap against right gap.
424
- * The pixels a rounded corner leaves out of the fill are no holes.
425
- * @param {number} id
426
- * @param {NonNullable<ReturnType<typeof surfaces>>['stats'][number]} s
427
- * @param {Int32Array} label @param {number} W
428
- * @param {number} [radius] the box's corner radius in pixels
429
- * @returns {Box['text']}
430
- */
431
- export function textPlace(id, s, label, W, radius = 0) {
432
- const r = Math.max(0, radius);
433
- /** @param {number} x @param {number} y */
434
- const outsideCorner = (x, y) => {
435
- const dx = x < s.x0 + r ? s.x0 + r - x : x > s.x1 - r ? x - (s.x1 - r) : 0;
436
- const dy = y < s.y0 + r ? s.y0 + r - y : y > s.y1 - r ? y - (s.y1 - r) : 0;
437
- return dx > 0 && dy > 0 && dx * dx + dy * dy > r * r;
438
- };
439
- let lo = Infinity, hi = -Infinity;
440
- for (let y = s.y0 + 2; y <= s.y1 - 2; y++)
441
- for (let x = s.x0 + 2; x <= s.x1 - 2; x++)
442
- if (label[y * W + x] !== id && !outsideCorner(x, y)) {
443
- if (x < lo) lo = x;
444
- if (x > hi) hi = x;
445
- }
446
- if (hi < 0) return 'none';
447
- const left = lo - s.x0, right = s.x1 - hi;
448
- return Math.abs(left - right) <= Math.max(3, 0.15 * (s.x1 - s.x0)) ? 'centered' : 'start';
449
- }
450
-
451
- /**
452
- * @typedef {object} TextLine
453
- * @property {number} x left of its first glyph
454
- * @property {number} y top of its tallest glyphs
455
- * @property {number} w
456
- * @property {number} baseline the row its glyphs stand on
457
- * @property {number} tall pixels from its tallest glyphs' top to the baseline
458
- * @property {number} xHeight pixels from its short glyphs' top to the baseline
459
- * @property {number} stem the common stroke width in pixels
460
- * @property {number} glyphs how many glyph shapes it has
461
- * @property {string} ink its text color
462
- * @property {string} on the color behind it
463
- */
464
-
465
- /** A surface this small is a glyph's core or its antialiasing, never a fill. */
466
- const GLYPH_MOST = 600;
467
-
468
- /**
469
- * Read the text lines of a picture: glyph shapes (small surfaces, joined),
470
- * set on a common baseline. Heights count a row only where the ink covers at
471
- * least half, so antialiasing does not add a pixel.
472
- * @param {Picture} pic
473
- * @param {{rgb: Uint8Array, label: Int32Array, stats: NonNullable<ReturnType<typeof surfaces>>['stats']}} read from measureBoxes
474
- * @param {Set<number>} fills the surfaces text sits on (the page and the boxes read as parts)
475
- * @returns {TextLine[]}
476
- */
477
- export function measureText(pic, {rgb, label, stats}, fills) {
478
- const W = pic.width, H = pic.height;
479
- const n = W * H;
480
- const ink = new Uint8Array(n);
481
- for (let i = 0; i < n; i++) {
482
- const s = stats[label[i]];
483
- // Small surfaces that are no box's fill (a glyph's core or its
484
- // antialiasing; a large letter's core too).
485
- if (s.n <= 10 * GLYPH_MOST && !fills.has(label[i]) && s.y1 - s.y0 < 100 && s.x1 - s.x0 < 160) ink[i] = 1;
486
- }
487
- // Glyph shapes: ink pixels joined with their eight neighbours.
488
- const seen = new Int32Array(n).fill(-1);
489
- /** @type {Array<{x0: number, y0: number, x1: number, y1: number, px: number[]}>} */
490
- const blobs = [];
491
- const stack = [];
492
- for (let i = 0; i < n; i++) {
493
- if (!ink[i] || seen[i] >= 0) continue;
494
- const b = {x0: W, y0: H, x1: -1, y1: -1, px: /** @type {number[]} */ ([])};
495
- seen[i] = blobs.length;
496
- stack.push(i);
497
- while (stack.length) {
498
- const j = /** @type {number} */ (stack.pop());
499
- const x = j % W, y = (j - x) / W;
500
- b.px.push(j);
501
- if (x < b.x0) b.x0 = x;
502
- if (x > b.x1) b.x1 = x;
503
- if (y < b.y0) b.y0 = y;
504
- if (y > b.y1) b.y1 = y;
505
- for (let dy = -1; dy <= 1; dy++)
506
- for (let dx = -1; dx <= 1; dx++) {
507
- const nx = x + dx, ny = y + dy;
508
- if (nx < 0 || ny < 0 || nx >= W || ny >= H) continue;
509
- const k = ny * W + nx;
510
- if (ink[k] && seen[k] < 0) {
511
- seen[k] = blobs.length;
512
- stack.push(k);
513
- }
514
- }
515
- }
516
- blobs.push(b);
517
- }
518
- /** @type {Array<{x0: number, x1: number, top: number, bottom: number, stem: number, inkI: number, bgI: number}>} */
519
- const glyphs = [];
520
- for (const b of blobs) {
521
- const h = b.y1 - b.y0 + 1, w = b.x1 - b.x0 + 1;
522
- if (h < 4 || h > 80 || w > 4 * h || b.px.length < 6) continue;
523
- // The color behind: the surface just left of the shape, mid-height.
524
- const my = (b.y0 + b.y1) >> 1;
525
- const bx = Math.max(0, b.x0 - 1);
526
- const bgI = my * W + bx;
527
- if (ink[bgI]) continue;
528
- let far = 0, inkI = b.px[0];
529
- for (const j of b.px) {
530
- const d = dist(rgb, j * 3, rgb, bgI * 3);
531
- if (d > far) {
532
- far = d;
533
- inkI = j;
534
- }
535
- }
536
- if (far < 40) continue;
537
- // Rows where the ink covers at least half; the stroke width from the
538
- // shape's inked area against its outline (a stroke of width w and
539
- // length L has area wL and outline about 2L), so it reads below a pixel.
540
- const id = seen[b.px[0]];
541
- /** @param {number} j */
542
- const cover = j => (seen[j] === id ? Math.min(1, dist(rgb, j * 3, rgb, bgI * 3) / far) : 0);
543
- let top = -1, bottom = -1, area = 0, outline = 0;
544
- for (let y = b.y0; y <= b.y1; y++) {
545
- let any = false;
546
- for (let x = b.x0; x <= b.x1; x++) {
547
- const j = y * W + x;
548
- const c = cover(j);
549
- area += c;
550
- if (c < 0.5) continue;
551
- any = true;
552
- if (x === b.x0 || cover(j - 1) < 0.5) outline++;
553
- if (x === b.x1 || cover(j + 1) < 0.5) outline++;
554
- if (y === b.y0 || cover(j - W) < 0.5) outline++;
555
- if (y === b.y1 || cover(j + W) < 0.5) outline++;
556
- }
557
- if (any) {
558
- if (top < 0) top = y;
559
- bottom = y;
560
- }
561
- }
562
- if (top < 0 || !outline) continue;
563
- glyphs.push({x0: b.x0, x1: b.x1, top, bottom, stem: (2 * area) / outline, inkI, bgI});
564
- }
565
- // Lines: glyphs whose bottoms agree, near each other along the row.
566
- glyphs.sort((a, c) => a.bottom - c.bottom || a.x0 - c.x0);
567
- /** @type {Array<typeof glyphs>} */
568
- const groups = [];
569
- for (const g of glyphs) {
570
- const h = g.bottom - g.top + 1;
571
- const line = groups.find(l => {
572
- const last = l[l.length - 1];
573
- return Math.abs(last.bottom - g.bottom) <= 0 && l.some(o => g.x0 - o.x1 <= Math.max(h, o.bottom - o.top + 1) && o.x0 - g.x1 <= Math.max(h, o.bottom - o.top + 1));
574
- });
575
- if (line) line.push(g);
576
- else groups.push([g]);
577
- }
578
- /** @type {TextLine[]} */
579
- const lines = [];
580
- for (const l of groups) {
581
- if (l.length < 3) continue;
582
- const heights = l.map(g => g.bottom - g.top + 1).sort((a, c) => a - c);
583
- // Short glyphs (x-height) are the most common height; tall ones stand
584
- // well above them. The shortest tall glyph is the capital height (an
585
- // ascender may rise a little past it).
586
- const xHeight = mode(heights);
587
- const talls = heights.filter(v => v >= xHeight * 1.2);
588
- const tall = talls.length ? talls[0] : heights[heights.length - 1];
589
- const x0 = Math.min(...l.map(g => g.x0)), x1 = Math.max(...l.map(g => g.x1));
590
- lines.push({
591
- x: x0,
592
- y: l[0].bottom - tall + 1,
593
- w: x1 - x0 + 1,
594
- baseline: l[0].bottom,
595
- tall,
596
- xHeight,
597
- stem: median(l.map(g => g.stem)),
598
- glyphs: l.length,
599
- ink: hexAt(rgb, l[0].inkI),
600
- on: hexAt(rgb, l[0].bgI),
601
- });
602
- }
603
- return lines.sort((a, c) => a.baseline - c.baseline || a.x - c.x);
604
- }
605
-
606
- /** The most common value, the smaller on a tie. @param {number[]} values */
607
- function mode(values) {
608
- /** @type {Map<number, number>} */
609
- const n = new Map();
610
- for (const v of values) n.set(v, (n.get(v) ?? 0) + 1);
611
- return [...n].sort((a, b) => b[1] - a[1] || a[0] - b[0])[0][0];
612
- }
613
-
614
- /**
615
- * @typedef {object} Trait
616
- * @property {string} trait what it is ("corners", "height", "edges", ...)
617
- * @property {string} on the part-map group it was read on, or "theme"
618
- * @property {string} value the measured value as the CLI takes it
619
- * @property {string} read where it was read and from how many
620
- * @property {'sure' | 'about'} sure
621
- * @property {{verb: 'set', thing: string, value: string, on?: string}} step the setting that makes it
622
- */
623
-
624
- /**
625
- * @typedef {object} Unsure
626
- * @property {string} trait
627
- * @property {string} on
628
- * @property {string} why what was seen and why it is not written
629
- * @property {Array<Trait['step']>} [options] settings that would make it, offered and never written for you
630
- */
631
-
632
- /**
633
- * The cap height of the common sans, serif and mono faces as a share of the
634
- * font size (Helvetica, Arial, Roboto, Times, Courier all sit at 0.66–0.73).
635
- * Text size is read from it, so it is "about": a face outside that range
636
- * reads a pixel or two off.
637
- */
638
- const CAP_SHARE = 0.72;
639
-
640
- /** @param {number[]} v */
641
- const median = v => {
642
- const s = [...v].sort((a, b) => a - b);
643
- return s.length % 2 ? s[s.length >> 1] : (s[s.length / 2 - 1] + s[s.length / 2]) / 2;
644
- };
645
-
646
- /**
647
- * Where in the picture boxes sit, in words.
648
- * @param {Array<{x: number, y: number, w: number, h: number}>} items
649
- * @param {number} W @param {number} H
650
- */
651
- function whereIn(items, W, H) {
652
- const cx = items.reduce((a, b) => a + b.x + b.w / 2, 0) / items.length / W;
653
- const cy = items.reduce((a, b) => a + b.y + b.h / 2, 0) / items.length / H;
654
- return `${cy < 0.33 ? 'top' : cy > 0.67 ? 'bottom' : 'middle'}${cx < 0.33 ? '-left' : cx > 0.67 ? '-right' : ''}`;
655
- }
656
-
657
- /**
658
- * The value most of the items agree on (within `near`), with how many agree:
659
- * their most common value (whole-pixel readings), or with `median` their
660
- * middle one (half-pixel readings that scatter around the true value).
661
- * @param {number[]} values @param {number} near @param {(v: number[]) => number} [middle]
662
- */
663
- export function agreed(values, near, middle = mode) {
664
- let best = {value: 0, n: 0};
665
- for (const v of values) {
666
- // (Equal values agree, pills' Infinity included: Infinity − Infinity is
667
- // no number.)
668
- const close = values.filter(o => o === v || Math.abs(o - v) <= near);
669
- if (close.length > best.n) best = {value: middle(close), n: close.length};
670
- }
671
- return best;
672
- }
673
-
674
- /** @param {string} hex */
675
- function chroma(hex) {
676
- const v = [1, 3, 5].map(i => parseInt(hex.slice(i, i + 2), 16));
677
- return Math.max(...v) - Math.min(...v);
678
- }
679
-
680
- /**
681
- * The pixel density a PNG says it was saved at: 2 for a 144-dpi (Retina)
682
- * screenshot, 1 for 72 or 96 dpi or when it names none.
683
- * @param {Buffer} bytes
684
- */
685
- export function pixelScale(bytes) {
686
- let at = 8;
687
- while (at + 12 <= bytes.length) {
688
- const len = bytes.readUInt32BE(at);
689
- const type = bytes.toString('latin1', at + 4, at + 8);
690
- if (type === 'pHYs' && len >= 9 && bytes[at + 16] === 1) {
691
- const perMeter = bytes.readUInt32BE(at + 8);
692
- // Screenshots mark 72 dpi per screen pixel (a Mac's 1x), so 144 dpi is
693
- // a 2x picture; 96 dpi, a Windows 1x, still rounds to 1.
694
- return Math.max(1, Math.round((perMeter * 0.0254) / 72));
695
- }
696
- if (type === 'IDAT' || type === 'IEND') break;
697
- at += 12 + len;
698
- }
699
- return 1;
700
- }
701
-
702
- /**
703
- * Measure a picture's look as traits on the part map's groups. Boxes are
704
- * sorted into groups by their measured shape only: small squares are checks,
705
- * control-sized boxes with centred text are button-like, with text at the
706
- * start (or none) inputs, big panels containers. A trait is written only
707
- * where enough boxes agree; the rest is named with what was seen.
708
- * @param {Picture} pic
709
- * @param {number} [scale] picture pixels per CSS pixel
710
- * @returns {{traits: Trait[], unsure: Unsure[], slots: Record<string, Record<string, {value: unknown, source: 'picture', confidence: number}>>, scale: number, boxes: number, lines: number}}
711
- */
712
- export function readTraits(pic, scale = 1) {
713
- /** @param {string} why */
714
- const notRead = why => ({traits: [], unsure: [{trait: 'look', on: 'theme', why}], slots: {}, scale, boxes: 0, lines: 0});
715
- const pixels = pic.width * pic.height;
716
- if (pixels > MEASURE_MOST)
717
- return notRead(`the picture has ${(pixels / 1e6).toFixed(1)} million pixels, more than the ${MEASURE_MOST / 1e6} million its look is measured from; crop it to the part that shows the controls, or save it at screen size`);
718
- const read = measureBoxes(pic);
719
- if (!read)
720
- return notRead('the picture is too grainy to read boxes from (nearly every pixel differs from the next); a screenshot of an app reads best');
721
- const W = pic.width, H = pic.height;
722
- const px = (/** @type {number} */ v) => Math.round((v / scale) * 2) / 2;
723
- // Corners as whole pixels.
724
- const cornerPx = (/** @type {number} */ v) => Math.round(v / scale);
725
- /** @type {Record<string, Box[]>} */
726
- const groups = {checks: [], 'button-like': [], inputs: [], containers: []};
727
- for (const b of read.boxes) {
728
- if (b.w <= 22 * scale && b.h <= 22 * scale && Math.abs(b.w - b.h) <= 1) {
729
- // A check box is square with square-ish corners and a thin edge; a
730
- // round one is a radio or a letter's counter, not read.
731
- if (!b.round && b.edge <= 2 * scale && b.w >= 10 * scale) groups.checks.push(b);
732
- }
733
- else if (b.w >= 0.9 * W) continue;
734
- // (A control holds text, or is an empty field far wider than tall; a
735
- // solid shape with neither is a letter's core.)
736
- // (A thin field may be much wider than tall; a taller strip that wide is
737
- // a banner, not a field.)
738
- else if (b.h >= 16 * scale && b.h <= 64 * scale && b.w >= 1.2 * b.h && (b.w <= 8 * b.h || (b.w <= 12 * b.h && b.h <= 40 * scale)) && (b.text !== 'none' || b.w >= 5 * b.h))
739
- groups[b.text === 'centered' ? 'button-like' : 'inputs'].push(b);
740
- else if (b.h > 64 * scale && b.w >= 120 * scale) groups.containers.push(b);
741
- }
742
- const lines = measureText(pic, read, new Set([read.pageId, ...Object.values(groups).flat().map(b => b.id)]));
743
- /** @type {Trait[]} */
744
- const traits = [];
745
- /** @type {Unsure[]} */
746
- const unsure = [];
747
- /**
748
- * A trait from the boxes that agree, or named when too few do.
749
- * @param {string} trait @param {string} on @param {Box[]} boxes
750
- * @param {(b: Box) => number} of @param {number} near
751
- * @param {(v: number) => Trait['step'] | null} step
752
- * @param {(v: number) => string} show
753
- * @param {(v: number[]) => number} [middle]
754
- */
755
- const fromBoxes = (trait, on, boxes, of, near, step, show, middle = mode) => {
756
- if (!boxes.length) return;
757
- const a = agreed(boxes.map(of), near * scale, middle);
758
- const where = whereIn(boxes, W, H);
759
- if (a.n >= 2 && a.n >= boxes.length * 0.6) {
760
- const st = step(a.value);
761
- if (st)
762
- traits.push({
763
- trait,
764
- on,
765
- value: show(a.value),
766
- read: `${a.n} of ${boxes.length} ${on} boxes (${where})`,
767
- sure: a.n >= 3 ? 'sure' : 'about',
768
- step: st,
769
- });
770
- } else
771
- unsure.push({
772
- trait,
773
- on,
774
- why: `${boxes.length} ${on} box${boxes.length > 1 ? 'es' : ''} (${where}) read ${[...new Set(boxes.map(b => show(of(b))))].join(', ')}: too few agree to write it`,
775
- });
776
- };
777
- const controls = [...groups['button-like'], ...groups.inputs];
778
- // (The family a reading comes from: the inputs, the buttons, or the
779
- // controls when it holds both.)
780
- const familyOf = (/** @type {Box[]} */ boxes) =>
781
- boxes.every(b => groups.inputs.includes(b))
782
- ? 'inputs'
783
- : boxes.every(b => groups['button-like'].includes(b))
784
- ? 'button-like'
785
- : 'controls';
786
- // Height of controls: the control size token set on the control families
787
- // only, buttons (button-like) and inputs, each from its own boxes. It is
788
- // never set for the whole theme: parts that are not controls read it too
789
- // (a dialog header centers its close button on it), and set on a family
790
- // it reaches only the parts that size themselves by it.
791
- const showPx = (/** @type {number} */ v) => `${px(v)}px`;
792
- // An input's focus ring reads as a wider edge around the same field: a
793
- // ring drawn outside adds to the height, one drawn inside does not, so
794
- // inputs agree when their heights agree either with or inside the edge.
795
- // Focus only adds, so the inputs' own edge is the thinnest one drawn.
796
- const usualEdge = groups.inputs.length ? Math.min(...groups.inputs.map(b => b.edge)) : 0;
797
- const outerAgree = agreed(groups.inputs.map(b => b.h), 2 * scale);
798
- const innerAgree = agreed(groups.inputs.map(b => b.h - 2 * b.edge), 2 * scale);
799
- const inner = outerAgree.n >= innerAgree.n ? outerAgree : innerAgree;
800
- const plainInputs = groups.inputs.filter(b => b.edge === usualEdge);
801
- const inputsAgree = groups.inputs.length >= 2 && inner.n >= 2 && inner.n >= groups.inputs.length * 0.6;
802
- fromBoxes('height', 'button-like', groups['button-like'], b => b.h, 1, v => ({verb: 'set', thing: '--size-element-md', value: showPx(v), on: 'button-like'}), showPx);
803
- if (inputsAgree) {
804
- const inputsH = mode(plainInputs.map(b => b.h));
805
- traits.push({
806
- trait: 'height',
807
- on: 'inputs',
808
- value: showPx(inputsH),
809
- read: `${inner.n} of ${groups.inputs.length} inputs boxes (${whereIn(groups.inputs, W, H)})`,
810
- sure: inner.n >= 3 ? 'sure' : 'about',
811
- step: {verb: 'set', thing: '--size-element-md', value: showPx(inputsH), on: 'inputs'},
812
- });
813
- } else if (groups.inputs.length)
814
- unsure.push({trait: 'height', on: 'inputs', why: `${groups.inputs.length} inputs box${groups.inputs.length > 1 ? 'es' : ''} (${whereIn(groups.inputs, W, H)}) read ${[...new Set(groups.inputs.map(b => showPx(b.h)))].join(', ')}: too few agree to write it`});
815
- // Corners: controls and inputs together when they agree, else apart.
816
- const cornerOf = (/** @type {Box} */ b) => (b.round ? Infinity : b.radius);
817
- const cornerStep = (/** @type {string} */ on, /** @type {string | null} */ token) => (/** @type {number} */ v) =>
818
- v === Infinity
819
- ? {verb: /** @type {const} */ ('set'), thing: 'corners', value: 'pill', ...(on === 'theme' ? {} : {on})}
820
- : token
821
- ? {verb: /** @type {const} */ ('set'), thing: token, value: `${cornerPx(v)}px`}
822
- : {verb: /** @type {const} */ ('set'), thing: 'corners', value: `${cornerPx(v)}px`, on};
823
- const showCorner = (/** @type {number} */ v) => (v === Infinity ? 'pill' : `${cornerPx(v)}px`);
824
- // (One theme-wide radius only when every control agrees: a majority
825
- // would give the other family's boxes a radius they do not draw.)
826
- const both = agreed(controls.map(cornerOf), 1 * scale);
827
- if (controls.length && both.n === controls.length && both.value !== Infinity)
828
- fromBoxes('corners', familyOf(controls), controls, cornerOf, 1, cornerStep('theme', '--radius-element'), showCorner, median);
829
- else {
830
- fromBoxes('corners', 'button-like', groups['button-like'], cornerOf, 1, cornerStep('button-like', null), showCorner, median);
831
- fromBoxes('corners', 'inputs', groups.inputs, cornerOf, 1, cornerStep('inputs', null), showCorner, median);
832
- }
833
- fromBoxes('corners', 'containers', groups.containers, cornerOf, 1, cornerStep('containers', '--radius-container'), showCorner, median);
834
- fromBoxes('corners', 'checks', groups.checks, cornerOf, 1, cornerStep('checks', null), showCorner, median);
835
- // Edge width: read only from boxes that draw an edge. A filled button
836
- // draws none, which says nothing about the theme's border width: writing
837
- // 0px from it would strip the fields', cards' and checkboxes' borders.
838
- const edged = controls.filter(b => b.edge > 0);
839
- if (inputsAgree && usualEdge > 0)
840
- traits.push({
841
- trait: 'edges',
842
- on: 'inputs',
843
- value: showPx(usualEdge),
844
- read: `${plainInputs.length} of ${groups.inputs.length} inputs boxes (${whereIn(groups.inputs, W, H)}), the thinnest edge (a focused one draws more)`,
845
- sure: plainInputs.length >= 2 ? 'sure' : 'about',
846
- step: {verb: 'set', thing: '--border-width', value: showPx(usualEdge)},
847
- });
848
- else if (edged.length) fromBoxes('edges', familyOf(edged), edged, b => b.edge, 0, v => ({verb: 'set', thing: '--border-width', value: `${px(v)}px`}), v => `${px(v)}px`);
849
- else if (controls.length)
850
- unsure.push({trait: 'edges', on: familyOf(controls), why: `no control box (${whereIn(controls, W, H)}) draws an edge, so the theme's border width is not read from the picture`});
851
- // Checks size: the box, square or round, written as width and height per
852
- // size where the theme is read (checksBoxSteps).
853
- fromBoxes('box size', 'checks', groups.checks, b => b.h, 1, v => ({verb: 'set', thing: 'height', value: `${px(v)}px`, on: 'checks'}), v => `${px(v)}px`);
854
- // Surfaces: how containers sit on the page.
855
- const surfaceOf = (/** @type {Box} */ b) => (b.shadow !== 'none' ? 2 : b.edge > 0 ? 1 : 0);
856
- const SURFACE = ['flat', 'outlined', 'raised'];
857
- fromBoxes('surfaces', 'containers', groups.containers, surfaceOf, 0, v => ({verb: 'set', thing: 'surfaces', value: SURFACE[v]}), v => SURFACE[v]);
858
- // Accent: the fill of the most colorful button-like boxes.
859
- const colorful = groups['button-like'].filter(b => chroma(b.gradient ? b.gradient.stops[1] : b.fill) >= 60);
860
- if (colorful.length) {
861
- const fills = colorful.map(b => (b.gradient ? b.gradient.stops[1] : b.fill));
862
- /** @type {Map<string, number>} */
863
- const n = new Map();
864
- for (const f of fills) n.set(f, (n.get(f) ?? 0) + 1);
865
- const [top, count] = [...n].sort((a, b) => b[1] - a[1])[0];
866
- if (count >= 2)
867
- traits.push({trait: 'color', on: 'button-like', value: top, read: `${count} of ${groups['button-like'].length} button-like boxes, the colorful ones (${whereIn(colorful, W, H)})`, sure: count >= 3 ? 'sure' : 'about', step: {verb: 'set', thing: 'accent', value: top}});
868
- }
869
- // Gradients and bevels: seen, named, never written (no group setting draws them).
870
- const graded = controls.filter(b => b.gradient);
871
- if (graded.length >= 2) {
872
- const g = /** @type {NonNullable<Box['gradient']>} */ (graded[0].gradient);
873
- unsure.push({
874
- trait: 'gradient',
875
- on: 'button-like',
876
- why: `${graded.length} of ${controls.length} control boxes draw a ${g.direction} gradient (one reads ${g.stops.join(' to ')}); no group setting draws gradients yet, so set it on the parts you want`,
877
- options: [{verb: 'set', thing: 'background', value: `linear-gradient(${g.direction === 'vertical' ? 'to bottom' : 'to right'}, ${g.stops.join(', ')})`, on: 'button'}],
878
- });
879
- }
880
- const beveled = read.boxes.filter(b => b.bevel);
881
- if (beveled.length >= 2)
882
- unsure.push({trait: 'bevel', on: 'button-like', why: `${beveled.length} boxes draw a light top-left edge and a dark bottom-right edge (a bevel); no setting draws a bevel yet`});
883
- const shadowed = controls.filter(b => b.shadow !== 'none');
884
- if (shadowed.length >= 2)
885
- unsure.push({trait: 'shadow', on: 'button-like', why: `${shadowed.length} of ${controls.length} control boxes cast a ${shadowed[0].shadow} shadow`});
886
- // Text.
887
- const sized = lines.map(l => ({...l, size: Math.round(l.tall / CAP_SHARE / scale)}));
888
- if (sized.length) {
889
- /** @type {Map<number, number>} */
890
- const weight = new Map();
891
- for (const l of sized) weight.set(l.size, (weight.get(l.size) ?? 0) + l.glyphs);
892
- const [body, bodyGlyphs] = [...weight].sort((a, b) => b[1] - a[1] || a[0] - b[0])[0];
893
- const total = sized.reduce((a, l) => a + l.glyphs, 0);
894
- const bodyLines = sized.filter(l => l.size === body);
895
- // The size is read from the capital height, which fonts and their
896
- // pixel snapping vary by a pixel or so: named with the command, not written.
897
- if (bodyGlyphs >= total * 0.3 && bodyLines.length >= 2 && body >= 10 && body <= 32)
898
- unsure.push({
899
- trait: 'type',
900
- on: 'theme',
901
- why: `body text reads about ${body}px (±1px, from the capital height of ${bodyLines.length} lines); faces differ by a pixel, so check it against the picture`,
902
- options: [{verb: 'set', thing: 'text-size', value: String(body)}],
903
- });
904
- else unsure.push({trait: 'type', on: 'theme', why: `text reads at ${[...weight.keys()].sort((a, b) => a - b).map(v => `${v}px`).join(', ')} with no clear body size`});
905
- // Leading: baselines of stacked lines at the same left edge and size.
906
- /** @type {number[]} */
907
- const pitches = [];
908
- // Only wrapped prose: a line at least 30 body sizes long, the next one
909
- // at its left edge, no further down than twice the text size.
910
- for (const l of bodyLines) {
911
- if (l.w < 30 * body * scale) continue;
912
- const next = bodyLines.find(o => o !== l && Math.abs(o.x - l.x) <= 2 * scale && o.baseline > l.baseline && o.baseline - l.baseline <= 2 * body * scale);
913
- if (next) pitches.push((next.baseline - l.baseline) / scale);
914
- }
915
- const p = agreed(pitches, 1);
916
- // The line height as a ratio, at most four decimals, no trailing dot.
917
- const leading = String(Number((p.value / body).toFixed(4)));
918
- if (p.n >= 1 && p.n >= pitches.length * 0.6 && p.value >= body)
919
- traits.push({
920
- trait: 'leading',
921
- on: 'theme',
922
- value: leading,
923
- read: `${p.n + 1} stacked lines of body text, ${p.value}px apart`,
924
- sure: p.n >= 2 ? 'sure' : 'about',
925
- step: {verb: 'set', thing: '--text-body-leading', value: leading},
926
- });
927
- else unsure.push({trait: 'leading', on: 'theme', why: 'no wrapped paragraph to measure line spacing from; a single line shows no leading'});
928
- // Headings: weight from stroke against height.
929
- const heads = sized.filter(l => l.size >= body * 1.25 && l.tall >= 12 * scale && l.size <= 72 && l.xHeight < l.tall);
930
- if (heads.length) {
931
- // Stroke width (area against outline, which reads thin on curves)
932
- // as a share of the text size: regular text reads under 0.085, bold
933
- // from 0.1. Semibold, bold and heavy read alike, so only a clearly
934
- // regular weight is written; a heavier one is named.
935
- const share = median(heads.map(l => l.stem / (l.tall / CAP_SHARE)));
936
- const sizes = [...new Set(heads.map(l => l.size))].sort((a, b) => b - a).map(v => `${v}px`).join(', ');
937
- if (heads.length >= 2 && share < 0.085)
938
- traits.push({trait: 'headings', on: 'theme', value: 'regular', read: `${heads.length} heading lines at ${sizes}, from stroke width against height`, sure: heads.length >= 3 ? 'sure' : 'about', step: {verb: 'set', thing: 'headings', value: 'regular'}});
939
- else
940
- unsure.push({
941
- trait: 'headings',
942
- on: 'theme',
943
- why: `${heads.length} heading line${heads.length > 1 ? 's' : ''} at ${sizes} read ${share >= 0.1 ? 'heavy (semibold to heavy, which a picture does not tell apart)' : 'between regular and semibold'}`,
944
- options: (share >= 0.1 ? ['semibold', 'bold', 'heavy'] : ['medium']).map(value => ({verb: /** @type {const} */ ('set'), thing: 'headings', value})),
945
- });
946
- }
947
- }
948
- // Controls are told apart from badges and tags only by seeing more than
949
- // one kind of them (buttons with labels and inputs with text at the
950
- // start); a picture with one kind (a crop of badges) writes no control
951
- // trait and says why.
952
- const kinds = ['button-like', 'inputs'].filter(g => groups[g].length).length;
953
- if (kinds < 2)
954
- for (const t of traits.filter(x => x.on === 'button-like' || x.on === 'inputs')) {
955
- traits.splice(traits.indexOf(t), 1);
956
- unsure.push({
957
- trait: t.trait,
958
- on: t.on,
959
- why: `${t.read} read ${t.value}, but only one kind of control shows, so this crop cannot tell controls from badges; send a full page`,
960
- options: [t.step],
961
- });
962
- }
963
- // Controls share one height at each size, so the picture's control height
964
- // is one token on the controls: the buttons' and the inputs' when they
965
- // agree, else the family read from more boxes, with the other family's
966
- // height named as a reading not written (written too, the two would
967
- // fight over the same token).
968
- const heightAt = (/** @type {string} */ on) =>
969
- traits.findIndex(t => t.trait === 'height' && t.on === on);
970
- const [bi, ii] = [heightAt('button-like'), heightAt('inputs')];
971
- if (bi >= 0 || ii >= 0) {
972
- const pick = (/** @type {number} */ i) => traits[i];
973
- const [b, n] = [bi >= 0 ? pick(bi) : null, ii >= 0 ? pick(ii) : null];
974
- const pxOf = (/** @type {any} */ t) => parseFloat(String(t.value));
975
- const main =
976
- b && n
977
- ? Math.abs(pxOf(b) - pxOf(n)) <= 2 ||
978
- groups['button-like'].length >= groups.inputs.length
979
- ? b
980
- : n
981
- : (b ?? n);
982
- const other = b && n && main === b ? n : b && n ? b : null;
983
- const agree = !!other && Math.abs(pxOf(b) - pxOf(n)) <= 2;
984
- for (const i of [bi, ii].filter(i => i >= 0).sort((x, y) => y - x))
985
- traits.splice(i, 1);
986
- const m = /** @type {any} */ (main);
987
- traits.push({
988
- ...m,
989
- on: 'controls',
990
- read: agree ? `${m.read}; ${/** @type {any} */ (other).read}` : m.read,
991
- step: {...m.step, on: 'controls'},
992
- });
993
- if (other && !agree)
994
- unsure.push({
995
- trait: 'height',
996
- on: other.on,
997
- why: `${other.read} read ${other.value}, where ${m.read} read ${m.value}: controls share one height at each size, so ${m.value} is written for every control`,
998
- });
999
- }
1000
- unsure.push({trait: 'font', on: 'theme', why: 'the typeface is not read from pictures; set the family you want with the font dial'});
1001
- // The same readings as trait slots per family, for the editor's traits
1002
- // (fill with gradient, edge with bevel, depth): measured looks no setting
1003
- // writes yet ride here, each with how many boxes agree.
1004
- /** @type {Record<string, Record<string, {value: unknown, source: 'picture', confidence: number}>>} */
1005
- const slots = {};
1006
- const slot = (/** @type {string} */ role, /** @type {string} */ name, /** @type {unknown} */ value, /** @type {number} */ n, /** @type {number} */ of) => {
1007
- (slots[role] ??= {})[name] = {value, source: 'picture', confidence: Number((n / of).toFixed(2))};
1008
- };
1009
- if (graded.length >= 2) {
1010
- const lead = graded.find(b => chroma(/** @type {NonNullable<Box['gradient']>} */ (b.gradient).stops[1]) >= 60) ?? graded[0];
1011
- const g = /** @type {NonNullable<Box['gradient']>} */ (lead.gradient);
1012
- slot('button-like', 'fill', {direction: g.direction === 'vertical' ? 'to bottom' : 'to right', stops: g.stops}, graded.length, controls.length);
1013
- }
1014
- const bevelled = controls.filter(b => b.bevelColors);
1015
- if (bevelled.length >= 2) {
1016
- const c = /** @type {NonNullable<Box['bevelColors']>} */ (bevelled[0].bevelColors);
1017
- slot('button-like', 'edge', {width: px(bevelled[0].edge), light: c.light, dark: c.dark, rings: Math.max(1, Math.round(bevelled[0].edge / scale))}, bevelled.length, controls.length);
1018
- }
1019
- if (shadowed.length >= 2) slot('button-like', 'depth', shadowed[0].shadow, shadowed.length, controls.length);
1020
- for (const t of traits) {
1021
- const name = {height: 'height', corners: 'radius'}[t.trait];
1022
- // (A reading of the controls together rides on the button-like slot, as it always has.)
1023
- const role = t.on === 'inputs' ? 'inset-fields' : t.on === 'controls' ? 'button-like' : t.on;
1024
- if (name && role !== 'checks' && role !== 'containers') {
1025
- const [n, of] = (/(\d+) of (\d+)/.exec(t.read) ?? [0, 1, 1]).slice(1).map(Number);
1026
- slot(role, name, t.value === 'pill' ? 'pill' : parseFloat(t.value), n, of);
1027
- }
1028
- }
1029
- return {traits, unsure, slots, scale, boxes: read.boxes.length, lines: lines.length};
1030
- }