@bloxwap/hig 0.6.0 → 0.8.1

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 (286) hide show
  1. package/README.md +2 -2
  2. package/chart/README.md +13 -2
  3. package/chart/dist/altimeter-burst.d.ts +4 -0
  4. package/chart/dist/altimeter-burst.js +43 -0
  5. package/chart/dist/altimeter-chart.d.ts +52 -0
  6. package/chart/dist/altimeter-chart.js +275 -0
  7. package/chart/dist/altimeter-model.d.ts +43 -0
  8. package/chart/dist/altimeter-model.js +148 -0
  9. package/chart/dist/altimeter-targets.d.ts +33 -0
  10. package/chart/dist/altimeter-targets.js +88 -0
  11. package/chart/dist/bitcoin-price.d.ts +8 -0
  12. package/chart/dist/bitcoin-price.js +41 -0
  13. package/chart/dist/champions-league.d.ts +19 -0
  14. package/chart/dist/champions-league.js +104 -0
  15. package/chart/dist/index.d.ts +6 -0
  16. package/chart/dist/index.js +5 -0
  17. package/chart/dist/outcome-chart.d.ts +37 -0
  18. package/chart/dist/outcome-chart.js +212 -0
  19. package/chart/dist/outcome-model.d.ts +43 -0
  20. package/chart/dist/outcome-model.js +71 -0
  21. package/chart/dist/plus-grid.d.ts +6 -0
  22. package/chart/dist/plus-grid.js +29 -0
  23. package/contracts/README.md +1 -1
  24. package/contracts/dist/avatar.d.ts +68 -0
  25. package/contracts/dist/avatar.js +93 -0
  26. package/contracts/dist/confetti.d.ts +174 -0
  27. package/contracts/dist/confetti.js +138 -0
  28. package/contracts/dist/decimal.d.ts +7 -0
  29. package/contracts/dist/decimal.js +35 -0
  30. package/contracts/dist/index.d.ts +45 -2
  31. package/contracts/dist/index.js +145 -4
  32. package/contracts/dist/invariants.d.ts +17 -0
  33. package/contracts/dist/invariants.js +24 -0
  34. package/contracts/dist/messages.d.ts +62 -2
  35. package/contracts/dist/messages.js +22 -2
  36. package/contracts/dist/number-flow.d.ts +90 -0
  37. package/contracts/dist/number-flow.js +150 -0
  38. package/contracts/dist/qr.d.ts +151 -0
  39. package/contracts/dist/qr.js +581 -0
  40. package/contracts/dist/toast.d.ts +168 -0
  41. package/contracts/dist/toast.js +171 -0
  42. package/feedback/README.md +1 -1
  43. package/feedback/dist/contract.js +1 -1
  44. package/feedback/dist/react.js +2 -4
  45. package/native/README.md +6 -1
  46. package/native/dist/account-block.d.ts +62 -7
  47. package/native/dist/account-block.js +54 -11
  48. package/native/dist/animated-number-model.d.ts +47 -1
  49. package/native/dist/animated-number-model.js +248 -21
  50. package/native/dist/animated-number.d.ts +41 -1
  51. package/native/dist/animated-number.js +165 -17
  52. package/native/dist/announce.d.ts +2 -0
  53. package/native/dist/announce.js +10 -0
  54. package/native/dist/asset-balance-row.d.ts +19 -1
  55. package/native/dist/asset-balance-row.js +24 -8
  56. package/native/dist/assets-block.d.ts +115 -0
  57. package/native/dist/assets-block.js +87 -0
  58. package/native/dist/async-result.d.ts +2 -0
  59. package/native/dist/async-result.js +11 -0
  60. package/native/dist/avatar-frame.d.ts +29 -0
  61. package/native/dist/avatar-frame.js +56 -0
  62. package/native/dist/avatar.d.ts +21 -0
  63. package/native/dist/avatar.js +31 -0
  64. package/native/dist/bar-mark.d.ts +16 -2
  65. package/native/dist/bar-mark.js +23 -7
  66. package/native/dist/bar-pnl.js +7 -5
  67. package/native/dist/blocks.d.ts +13 -14
  68. package/native/dist/blocks.js +10 -6
  69. package/native/dist/brand.js +2 -1
  70. package/native/dist/button.d.ts +7 -2
  71. package/native/dist/button.js +16 -4
  72. package/native/dist/checkbox.js +2 -1
  73. package/native/dist/confetti.d.ts +33 -0
  74. package/native/dist/confetti.js +143 -0
  75. package/native/dist/confirm-block.d.ts +98 -0
  76. package/native/dist/confirm-block.js +48 -0
  77. package/native/dist/data.d.ts +1 -1
  78. package/native/dist/data.js +8 -4
  79. package/native/dist/explainer-block.d.ts +91 -0
  80. package/native/dist/explainer-block.js +34 -0
  81. package/native/dist/field.d.ts +19 -5
  82. package/native/dist/field.js +56 -9
  83. package/native/dist/figure.d.ts +23 -0
  84. package/native/dist/figure.js +10 -0
  85. package/native/dist/gesture-sheet-model.d.ts +50 -0
  86. package/native/dist/gesture-sheet-model.js +105 -0
  87. package/native/dist/gesture-sheet-next.d.ts +82 -0
  88. package/native/dist/gesture-sheet-next.js +315 -0
  89. package/native/dist/gesture-sheet.d.ts +5 -1
  90. package/native/dist/gesture-sheet.js +13 -6
  91. package/native/dist/icons.d.ts +9 -6
  92. package/native/dist/icons.js +25 -24
  93. package/native/dist/language-block.d.ts +14 -1
  94. package/native/dist/language-block.js +11 -4
  95. package/native/dist/list-group.d.ts +44 -8
  96. package/native/dist/list-group.js +84 -13
  97. package/native/dist/manage-position-block.d.ts +35 -2
  98. package/native/dist/manage-position-block.js +41 -11
  99. package/native/dist/market-row.d.ts +46 -0
  100. package/native/dist/market-row.js +41 -0
  101. package/native/dist/navigation.js +5 -3
  102. package/native/dist/notifications-block.d.ts +30 -2
  103. package/native/dist/notifications-block.js +16 -8
  104. package/native/dist/number-format.d.ts +6 -0
  105. package/native/dist/number-format.js +9 -0
  106. package/native/dist/pickers.d.ts +32 -2
  107. package/native/dist/pickers.js +17 -9
  108. package/native/dist/position-block.d.ts +66 -10
  109. package/native/dist/position-block.js +45 -11
  110. package/native/dist/preset-group.d.ts +31 -0
  111. package/native/dist/preset-group.js +39 -0
  112. package/native/dist/preset-picker-block.d.ts +107 -0
  113. package/native/dist/preset-picker-block.js +43 -0
  114. package/native/dist/profile-block.d.ts +38 -5
  115. package/native/dist/profile-block.js +31 -6
  116. package/native/dist/provider.d.ts +14 -5
  117. package/native/dist/provider.js +32 -6
  118. package/native/dist/qr-code.d.ts +33 -0
  119. package/native/dist/qr-code.js +29 -0
  120. package/native/dist/radio-group.d.ts +23 -5
  121. package/native/dist/radio-group.js +40 -12
  122. package/native/dist/recipes.d.ts +6 -0
  123. package/native/dist/recipes.js +6 -0
  124. package/native/dist/reward-block.d.ts +55 -13
  125. package/native/dist/reward-block.js +37 -14
  126. package/native/dist/screen-header.d.ts +59 -0
  127. package/native/dist/screen-header.js +37 -0
  128. package/native/dist/select.js +4 -2
  129. package/native/dist/selection.d.ts +3 -2
  130. package/native/dist/selection.js +1 -0
  131. package/native/dist/setting-header.d.ts +2 -8
  132. package/native/dist/setting-header.js +2 -11
  133. package/native/dist/setting-switch.d.ts +15 -2
  134. package/native/dist/setting-switch.js +11 -4
  135. package/native/dist/signals-block.d.ts +26 -2
  136. package/native/dist/signals-block.js +14 -4
  137. package/native/dist/slider.js +5 -1
  138. package/native/dist/sound-haptics-block.d.ts +29 -2
  139. package/native/dist/sound-haptics-block.js +19 -8
  140. package/native/dist/status.d.ts +47 -14
  141. package/native/dist/status.js +39 -22
  142. package/native/dist/sticky-header.d.ts +28 -0
  143. package/native/dist/sticky-header.js +29 -0
  144. package/native/dist/surface.d.ts +3 -1
  145. package/native/dist/surface.js +2 -1
  146. package/native/dist/swap-amount-card.d.ts +57 -0
  147. package/native/dist/swap-amount-card.js +31 -0
  148. package/native/dist/swap-panel-block.d.ts +11 -4
  149. package/native/dist/swap-panel-block.js +67 -38
  150. package/native/dist/swap-parts.d.ts +5 -10
  151. package/native/dist/swap-parts.js +22 -24
  152. package/native/dist/swap-shared.d.ts +8 -2
  153. package/native/dist/swap-shared.js +10 -6
  154. package/native/dist/swap-views.js +6 -4
  155. package/native/dist/switch-row.d.ts +3 -2
  156. package/native/dist/switch-row.js +5 -3
  157. package/native/dist/switch.d.ts +4 -2
  158. package/native/dist/switch.js +3 -3
  159. package/native/dist/target-block.d.ts +40 -7
  160. package/native/dist/target-block.js +31 -11
  161. package/native/dist/toast.d.ts +47 -0
  162. package/native/dist/toast.js +386 -0
  163. package/native/dist/token-action-block.d.ts +26 -9
  164. package/native/dist/token-action-block.js +21 -8
  165. package/native/dist/trade-history-detail-block.d.ts +26 -1
  166. package/native/dist/trade-history-detail-block.js +17 -9
  167. package/native/dist/trade-history-list-block.d.ts +71 -5
  168. package/native/dist/trade-history-list-block.js +55 -17
  169. package/native/dist/wallet-address-core.d.ts +25 -0
  170. package/native/dist/wallet-address-core.js +34 -0
  171. package/native/dist/wallet-address.d.ts +6 -10
  172. package/native/dist/wallet-address.js +16 -20
  173. package/native-fonts/README.md +1 -1
  174. package/og-fonts/README.md +1 -1
  175. package/package.json +131 -2
  176. package/react/README.md +3 -1
  177. package/react/dist/animated-number-flow.d.ts +72 -0
  178. package/react/dist/animated-number-flow.js +316 -0
  179. package/react/dist/animated-number-model.d.ts +24 -0
  180. package/react/dist/animated-number-model.js +106 -3
  181. package/react/dist/animated-number.d.ts +49 -1
  182. package/react/dist/animated-number.js +146 -11
  183. package/react/dist/asset-balance-row.d.ts +19 -1
  184. package/react/dist/asset-balance-row.js +17 -3
  185. package/react/dist/async-result.d.ts +2 -0
  186. package/react/dist/async-result.js +11 -0
  187. package/react/dist/avatar.d.ts +25 -0
  188. package/react/dist/avatar.js +50 -0
  189. package/react/dist/bar-mark.d.ts +5 -2
  190. package/react/dist/bar-mark.js +5 -3
  191. package/react/dist/blocks/account-block.d.ts +57 -7
  192. package/react/dist/blocks/account-block.js +51 -11
  193. package/react/dist/blocks/assets-block.d.ts +106 -0
  194. package/react/dist/blocks/assets-block.js +135 -0
  195. package/react/dist/blocks/confirm-block.d.ts +91 -0
  196. package/react/dist/blocks/confirm-block.js +31 -0
  197. package/react/dist/blocks/explainer-block.d.ts +80 -0
  198. package/react/dist/blocks/explainer-block.js +20 -0
  199. package/react/dist/blocks/install-command.js +3 -1
  200. package/react/dist/blocks/language-block.d.ts +9 -1
  201. package/react/dist/blocks/language-block.js +8 -6
  202. package/react/dist/blocks/manage-position-block.d.ts +30 -2
  203. package/react/dist/blocks/manage-position-block.js +23 -4
  204. package/react/dist/blocks/market-row.d.ts +20 -9
  205. package/react/dist/blocks/market-row.js +17 -4
  206. package/react/dist/blocks/notifications-block.d.ts +27 -2
  207. package/react/dist/blocks/notifications-block.js +11 -2
  208. package/react/dist/blocks/pickers.d.ts +30 -1
  209. package/react/dist/blocks/pickers.js +32 -10
  210. package/react/dist/blocks/position-block.d.ts +34 -11
  211. package/react/dist/blocks/position-block.js +16 -5
  212. package/react/dist/blocks/preset-picker-block.d.ts +85 -0
  213. package/react/dist/blocks/preset-picker-block.js +19 -0
  214. package/react/dist/blocks/profile-block.d.ts +33 -6
  215. package/react/dist/blocks/profile-block.js +31 -6
  216. package/react/dist/blocks/reward-block.d.ts +65 -21
  217. package/react/dist/blocks/reward-block.js +24 -11
  218. package/react/dist/blocks/screen-header.d.ts +34 -2
  219. package/react/dist/blocks/screen-header.js +17 -6
  220. package/react/dist/blocks/setting-switch.d.ts +14 -1
  221. package/react/dist/blocks/setting-switch.js +11 -4
  222. package/react/dist/blocks/signals-block.d.ts +23 -2
  223. package/react/dist/blocks/signals-block.js +6 -5
  224. package/react/dist/blocks/sound-haptics-block.d.ts +27 -3
  225. package/react/dist/blocks/sound-haptics-block.js +11 -7
  226. package/react/dist/blocks/sticky-header.d.ts +19 -0
  227. package/react/dist/blocks/sticky-header.js +103 -0
  228. package/react/dist/blocks/swap/amount-card.js +2 -1
  229. package/react/dist/blocks/swap/settings.js +4 -3
  230. package/react/dist/blocks/swap/shared.d.ts +29 -23
  231. package/react/dist/blocks/swap/shared.js +36 -32
  232. package/react/dist/blocks/swap-panel-block.d.ts +12 -3
  233. package/react/dist/blocks/swap-panel-block.js +72 -37
  234. package/react/dist/blocks/target-block.d.ts +35 -7
  235. package/react/dist/blocks/target-block.js +26 -12
  236. package/react/dist/blocks/token-action-block.d.ts +21 -8
  237. package/react/dist/blocks/token-action-block.js +9 -6
  238. package/react/dist/blocks/trade-history-detail-block.d.ts +23 -1
  239. package/react/dist/blocks/trade-history-detail-block.js +5 -4
  240. package/react/dist/blocks/trade-history-list-block.d.ts +31 -5
  241. package/react/dist/blocks/trade-history-list-block.js +20 -10
  242. package/react/dist/blocks.d.ts +12 -8
  243. package/react/dist/blocks.js +6 -2
  244. package/react/dist/brand.js +2 -1
  245. package/react/dist/button.d.ts +9 -3
  246. package/react/dist/button.js +12 -5
  247. package/react/dist/confetti.d.ts +27 -0
  248. package/react/dist/confetti.js +240 -0
  249. package/react/dist/data.js +11 -4
  250. package/react/dist/field.d.ts +20 -5
  251. package/react/dist/field.js +30 -8
  252. package/react/dist/figure.d.ts +23 -0
  253. package/react/dist/figure.js +12 -0
  254. package/react/dist/gesture-sheet.d.ts +15 -1
  255. package/react/dist/gesture-sheet.js +53 -8
  256. package/react/dist/list-group.d.ts +41 -8
  257. package/react/dist/list-group.js +49 -13
  258. package/react/dist/navigation.js +11 -3
  259. package/react/dist/overlay.d.ts +28 -3
  260. package/react/dist/overlay.js +29 -3
  261. package/react/dist/preset-group.d.ts +30 -0
  262. package/react/dist/preset-group.js +13 -0
  263. package/react/dist/provider.d.ts +42 -5
  264. package/react/dist/provider.js +84 -10
  265. package/react/dist/qr-code.d.ts +32 -0
  266. package/react/dist/qr-code.js +28 -0
  267. package/react/dist/select.js +10 -3
  268. package/react/dist/selection.d.ts +18 -4
  269. package/react/dist/selection.js +20 -10
  270. package/react/dist/slider.js +5 -1
  271. package/react/dist/status.d.ts +14 -10
  272. package/react/dist/status.js +17 -9
  273. package/react/dist/styles.css +380 -150
  274. package/react/dist/surface.d.ts +5 -1
  275. package/react/dist/surface.js +1 -1
  276. package/react/dist/toast.d.ts +19 -12
  277. package/react/dist/toast.js +29 -7
  278. package/react/dist/wallet-address-core.d.ts +27 -0
  279. package/react/dist/wallet-address-core.js +29 -0
  280. package/react/dist/wallet-address.d.ts +6 -12
  281. package/react/dist/wallet-address.js +5 -16
  282. package/tokens/README.md +2 -2
  283. package/tokens/dist/index.d.ts +16 -0
  284. package/tokens/dist/index.js +16 -0
  285. package/tokens/dist/manifest.json +8 -0
  286. package/tokens/dist/tokens.css +6 -0
@@ -0,0 +1,581 @@
1
+ const LEVELS = ['low', 'medium', 'quartile', 'high'];
2
+ // ISO/IEC 18004 Table 9, versions 1 to 40: error correction codewords in each block, then the number of blocks.
3
+ const ECC_WORDS = {
4
+ low: [7, 10, 15, 20, 26, 18, 20, 24, 30, 18, 20, 24, 26, 30, 22, 24, 28, 30, 28, 28, 28, 28, 30, 30, 26, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30],
5
+ medium: [10, 16, 26, 18, 24, 16, 18, 22, 22, 26, 30, 22, 22, 24, 24, 28, 28, 26, 26, 26, 26, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28],
6
+ quartile: [13, 22, 18, 26, 18, 24, 18, 22, 20, 24, 28, 26, 24, 20, 30, 24, 28, 28, 26, 30, 28, 30, 30, 30, 30, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30],
7
+ high: [17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, 24, 24, 30, 28, 28, 26, 28, 30, 24, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30],
8
+ };
9
+ const BLOCKS = {
10
+ low: [1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, 4, 6, 6, 6, 6, 7, 8, 8, 9, 9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25],
11
+ medium: [1, 1, 1, 2, 2, 4, 4, 4, 5, 5, 5, 8, 9, 9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21, 23, 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49],
12
+ quartile: [1, 1, 2, 2, 4, 4, 6, 6, 8, 8, 8, 10, 12, 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68],
13
+ high: [1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81],
14
+ };
15
+ // Table 12: the two format bits that name each level.
16
+ const LEVEL_BITS = { low: 1, medium: 0, quartile: 3, high: 2 };
17
+ const MODE_BITS = { numeric: 1, alphanumeric: 2, byte: 4 };
18
+ // Table 3: bits in the character count, for versions 1–9, 10–26 and 27–40.
19
+ const COUNT_BITS = { numeric: [10, 12, 14], alphanumeric: [9, 11, 13], byte: [8, 16, 16] };
20
+ /** Table 5: the alphanumeric alphabet in value order. */
21
+ export const QR_ALPHANUMERIC = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:';
22
+ const alphanumericValue = (code) => code < 128 ? QR_ALPHANUMERIC.indexOf(String.fromCharCode(code)) : -1;
23
+ /** Modules that carry data and error correction in a version: the symbol less its function patterns. */
24
+ function rawModules(version) {
25
+ let bits = (16 * version + 128) * version + 64;
26
+ if (version >= 2) {
27
+ const align = Math.floor(version / 7) + 2;
28
+ bits -= (25 * align - 10) * align - 55;
29
+ if (version >= 7)
30
+ bits -= 36;
31
+ }
32
+ return bits;
33
+ }
34
+ /** Data capacity and block layout of a version at a level. */
35
+ function layout(version, level) {
36
+ const total = rawModules(version) >>> 3, ecc = ECC_WORDS[level][version - 1], blocks = BLOCKS[level][version - 1];
37
+ return { total, ecc, blocks, short: blocks - total % blocks, shortData: Math.floor(total / blocks) - ecc, data: total - ecc * blocks };
38
+ }
39
+ /** Centres of the alignment patterns along one axis (Annex E); empty for version 1. */
40
+ export function qrAlignmentCentres(version) {
41
+ if (version === 1)
42
+ return [];
43
+ const count = Math.floor(version / 7) + 2, size = 17 + 4 * version, step = Math.floor((version * 8 + count * 3 + 5) / (count * 4 - 4)) * 2, centres = [6];
44
+ for (let position = size - 7; centres.length < count; position -= step)
45
+ centres.splice(1, 0, position);
46
+ return centres;
47
+ }
48
+ /** The 15 format bits for a level and mask: five data bits, ten BCH(15,5) bits, masked with 101010000010010. */
49
+ export function qrFormatBits(level, mask) {
50
+ const data = LEVEL_BITS[level] << 3 | mask;
51
+ let remainder = data;
52
+ for (let i = 0; i < 10; i++)
53
+ remainder = remainder << 1 ^ (remainder >>> 9) * 0x537;
54
+ return (data << 10 | remainder) ^ 0x5412;
55
+ }
56
+ /** The 18 version bits for versions 7 and up: six data bits and twelve Golay(18,6) bits. */
57
+ export function qrVersionBits(version) {
58
+ let remainder = version;
59
+ for (let i = 0; i < 12; i++)
60
+ remainder = remainder << 1 ^ (remainder >>> 11) * 0x1f25;
61
+ return version << 12 | remainder;
62
+ }
63
+ // GF(2^8) over x^8 + x^4 + x^3 + x^2 + 1. The exponent table is doubled so a product needs no modulo.
64
+ const EXP = new Uint8Array(510), LOG = new Uint8Array(256);
65
+ for (let i = 0, x = 1; i < 255; i++) {
66
+ EXP[i] = x;
67
+ EXP[i + 255] = x;
68
+ LOG[x] = i;
69
+ x <<= 1;
70
+ if (x & 0x100)
71
+ x ^= 0x11d;
72
+ }
73
+ const generators = new Map();
74
+ /** Coefficients of the Reed–Solomon generator polynomial of a degree, highest power first, without the leading 1. */
75
+ function generator(degree) {
76
+ const known = generators.get(degree);
77
+ if (known)
78
+ return known;
79
+ const poly = new Uint8Array(degree);
80
+ poly[degree - 1] = 1;
81
+ for (let i = 0; i < degree; i++) {
82
+ const root = EXP[i];
83
+ for (let j = 0; j < degree; j++) {
84
+ const c = poly[j];
85
+ poly[j] = (c ? EXP[LOG[c] + LOG[root]] : 0) ^ (j + 1 < degree ? poly[j + 1] : 0);
86
+ }
87
+ }
88
+ generators.set(degree, poly);
89
+ return poly;
90
+ }
91
+ /** Error correction codewords of one block: the remainder of the data polynomial divided by the generator. */
92
+ function remainder(data, gen) {
93
+ const degree = gen.length, result = new Uint8Array(degree);
94
+ for (let i = 0; i < data.length; i++) {
95
+ const factor = data[i] ^ result[0];
96
+ result.copyWithin(0, 1);
97
+ result[degree - 1] = 0;
98
+ if (factor) {
99
+ const log = LOG[factor];
100
+ for (let j = 0; j < degree; j++) {
101
+ const g = gen[j];
102
+ if (g)
103
+ result[j] = result[j] ^ EXP[LOG[g] + log];
104
+ }
105
+ }
106
+ }
107
+ return result;
108
+ }
109
+ /** The Reed–Solomon error correction codewords of one block: `count` codewords for the data codewords given. */
110
+ export function qrErrorCorrectionCodewords(data, count) { return remainder(data, generator(count)); }
111
+ /** UTF-8 bytes of a string; an unpaired surrogate becomes U+FFFD, as `TextEncoder` writes it. */
112
+ export function qrUtf8(text) {
113
+ const bytes = [];
114
+ for (let i = 0; i < text.length; i++) {
115
+ let code = text.charCodeAt(i);
116
+ if (code >= 0xd800 && code <= 0xdbff && i + 1 < text.length) {
117
+ const low = text.charCodeAt(i + 1);
118
+ if (low >= 0xdc00 && low <= 0xdfff) {
119
+ code = 0x10000 + (code - 0xd800 << 10) + (low - 0xdc00);
120
+ i++;
121
+ }
122
+ }
123
+ if (code >= 0xd800 && code <= 0xdfff)
124
+ code = 0xfffd;
125
+ if (code < 0x80)
126
+ bytes.push(code);
127
+ else if (code < 0x800)
128
+ bytes.push(0xc0 | code >> 6, 0x80 | code & 63);
129
+ else if (code < 0x10000)
130
+ bytes.push(0xe0 | code >> 12, 0x80 | code >> 6 & 63, 0x80 | code & 63);
131
+ else
132
+ bytes.push(0xf0 | code >> 18, 0x80 | code >> 12 & 63, 0x80 | code >> 6 & 63, 0x80 | code & 63);
133
+ }
134
+ return Uint8Array.from(bytes);
135
+ }
136
+ /** The densest mode that holds every character of a value: digits are `numeric`, the Table 5 alphabet `alphanumeric`, anything else `byte`. */
137
+ export function qrMode(value) {
138
+ let mode = 'numeric';
139
+ for (let i = 0; i < value.length; i++) {
140
+ const v = alphanumericValue(value.charCodeAt(i));
141
+ if (v < 0)
142
+ return 'byte';
143
+ if (v > 9)
144
+ mode = 'alphanumeric';
145
+ }
146
+ return mode;
147
+ }
148
+ const countBits = (mode, version) => COUNT_BITS[mode][Math.floor((version + 7) / 17)];
149
+ /** Payload bits after the mode and count fields. */
150
+ function payloadBits(mode, length) { return mode === 'numeric' ? Math.floor(length / 3) * 10 + [0, 4, 7][length % 3] : mode === 'alphanumeric' ? Math.floor(length / 2) * 11 + length % 2 * 6 : length * 8; }
151
+ /** The interleaved data and error correction codewords of a symbol. */
152
+ function codewords(value, bytes, mode, version, level) {
153
+ const plan = layout(version, level), data = new Uint8Array(plan.data), length = bytes ? bytes.length : value.length;
154
+ let position = 0, held = 0, heldBits = 0;
155
+ // Most significant bit first; at most 16 bits a push, so the accumulator stays well inside 32 bits.
156
+ const push = (bits, count) => { held = held << count | bits; for (heldBits += count; heldBits >= 8;)
157
+ data[position++] = held >>> (heldBits -= 8) & 0xff; };
158
+ push(MODE_BITS[mode], 4);
159
+ push(length, countBits(mode, version));
160
+ if (bytes)
161
+ for (let i = 0; i < length; i++)
162
+ push(bytes[i], 8);
163
+ else if (mode === 'numeric')
164
+ for (let i = 0; i < length; i += 3) {
165
+ const digits = Math.min(3, length - i);
166
+ push(Number(value.slice(i, i + digits)), [0, 4, 7, 10][digits]);
167
+ }
168
+ else {
169
+ for (let i = 0; i + 1 < length; i += 2)
170
+ push(alphanumericValue(value.charCodeAt(i)) * 45 + alphanumericValue(value.charCodeAt(i + 1)), 11);
171
+ if (length & 1)
172
+ push(alphanumericValue(value.charCodeAt(length - 1)), 6);
173
+ }
174
+ // Terminator (up to four zero bits), zero bits to the next codeword, then the two pad codewords in turn.
175
+ let used = position * 8 + heldBits;
176
+ if (heldBits)
177
+ data[position] = held << 8 - heldBits & 0xff;
178
+ used += Math.min(4, plan.data * 8 - used);
179
+ for (let i = Math.ceil(used / 8), pad = 0; i < plan.data; i++, pad ^= 1)
180
+ data[i] = pad ? 0x11 : 0xec;
181
+ // Split into blocks (the short ones first), then interleave data codewords and error correction codewords.
182
+ const gen = generator(plan.ecc), blocks = [], parity = [];
183
+ for (let i = 0, start = 0; i < plan.blocks; i++) {
184
+ const size = plan.shortData + (i < plan.short ? 0 : 1), block = data.subarray(start, start + size);
185
+ blocks.push(block);
186
+ parity.push(remainder(block, gen));
187
+ start += size;
188
+ }
189
+ const result = new Uint8Array(plan.total);
190
+ let out = 0;
191
+ for (let i = 0; i <= plan.shortData; i++)
192
+ for (const block of blocks)
193
+ if (i < block.length)
194
+ result[out++] = block[i];
195
+ for (let i = 0; i < plan.ecc; i++)
196
+ for (const block of parity)
197
+ result[out++] = block[i];
198
+ return result;
199
+ }
200
+ /** Whether mask pattern `mask` inverts the module at a column and row (Table 10). */
201
+ export function qrMaskAt(mask, column, row) {
202
+ switch (mask) {
203
+ case 0: return (column + row) % 2 === 0;
204
+ case 1: return row % 2 === 0;
205
+ case 2: return column % 3 === 0;
206
+ case 3: return (column + row) % 3 === 0;
207
+ case 4: return (Math.floor(column / 3) + Math.floor(row / 2)) % 2 === 0;
208
+ case 5: return column * row % 2 + column * row % 3 === 0;
209
+ case 6: return (column * row % 2 + column * row % 3) % 2 === 0;
210
+ default: return ((column + row) % 2 + column * row % 3) % 2 === 0;
211
+ }
212
+ }
213
+ /**
214
+ * The mask search reads a symbol as bits, 32 cells a word, so a mask is scored in a few operations a word, not many a cell.
215
+ * A line (a row or a column) is cut into words that start every `SPAN` cells and overlap: word k holds cells 21k−1 to 21k+30,
216
+ * the cell 21k−1+j at bit j. A word answers for the runs and sequences that start in its own cells, bits 1 to 21 (`OWN`):
217
+ * the bit before them says whether a run began earlier, and the ten after them complete an eleven-cell sequence.
218
+ */
219
+ const SPAN = 21, OWN = 0x3ffffe;
220
+ const MASK_COLUMNS = 8 * 12 * 2;
221
+ let maskWords;
222
+ /**
223
+ * Each mask pattern as words, built when a symbol is first encoded: every pattern repeats each 6 columns and 12 rows. For a
224
+ * row, by `row % 12` and the word's parity (a word starts 21 columns after the last); then, from `MASK_COLUMNS`, for a column,
225
+ * by `column % 6` and the word's index modulo 4.
226
+ */
227
+ function maskPatterns() {
228
+ if (maskWords)
229
+ return maskWords;
230
+ const words = maskWords = new Int32Array(MASK_COLUMNS + 8 * 6 * 4), tile = new Uint8Array(12);
231
+ for (let mask = 0; mask < 8; mask++) {
232
+ for (let row = 0; row < 12; row++) {
233
+ let six = 0;
234
+ for (let column = 0; column < 6; column++)
235
+ if (qrMaskAt(mask, column, row))
236
+ six |= 1 << column;
237
+ tile[row] = six;
238
+ // Bit j of a row's word is column 21k−1+j: column j+5 of the tile in an even word, j+2 in an odd one. Six bits repeat to fill the word.
239
+ for (let phase = 0; phase < 2; phase++) {
240
+ const turn = 5 - 3 * phase;
241
+ words[(mask * 12 + row) * 2 + phase] = Math.imul((six >>> turn | six << 6 - turn) & 63, 0x41041041);
242
+ }
243
+ }
244
+ for (let column = 0; column < 6; column++) {
245
+ let twelve = 0;
246
+ for (let row = 0; row < 12; row++)
247
+ twelve |= (tile[row] >>> column & 1) << row;
248
+ // Bit j of a column's word is row 21k−1+j: row j+11, j+8, j+5 and j+2 of the tile in words 0 to 3.
249
+ for (let phase = 0; phase < 4; phase++) {
250
+ const turn = 11 - 3 * phase;
251
+ words[MASK_COLUMNS + (mask * 6 + column) * 4 + phase] = Math.imul((twelve >>> turn | twelve << 12 - turn) & 4095, 0x1001001);
252
+ }
253
+ }
254
+ }
255
+ return words;
256
+ }
257
+ /** A symbol as words, its rows and then its columns: `lines` holds the modules and `free` the cells that are not reserved, which a mask may invert. */
258
+ function pack(modules, reserved, size) {
259
+ const words = Math.ceil(size / SPAN), lines = new Int32Array(2 * size * words), free = new Int32Array(2 * size * words);
260
+ for (let pass = 0, at = 0; pass < 2; pass++) {
261
+ const step = pass ? size : 1, stride = pass ? 1 : size;
262
+ for (let line = 0; line < size; line++, at += words) {
263
+ // Each cell is read once, into its word's own bits; a word then takes the cell before them and the ten after from its neighbours.
264
+ for (let word = 0, cell = 0, index = line * stride; word < words; word++) {
265
+ let dark = 0, open = 0;
266
+ for (let bit = 1, end = Math.min(cell + SPAN, size); cell < end; cell++, bit++, index += step) {
267
+ dark |= modules[index] << bit;
268
+ open |= (reserved[index] ^ 1) << bit;
269
+ }
270
+ lines[at + word] = dark;
271
+ free[at + word] = open;
272
+ }
273
+ for (let word = at, last = at + words - 1; word <= last; word++) {
274
+ if (word > at) {
275
+ lines[word] = lines[word] | lines[word - 1] >>> SPAN & 1;
276
+ free[word] = free[word] | free[word - 1] >>> SPAN & 1;
277
+ }
278
+ if (word < last) {
279
+ lines[word] = lines[word] | (lines[word + 1] & 0x7fe) << SPAN;
280
+ free[word] = free[word] | (free[word + 1] & 0x7fe) << SPAN;
281
+ }
282
+ }
283
+ }
284
+ }
285
+ return { lines, free };
286
+ }
287
+ /** How many bits of a word are set. */
288
+ function count(word) { word -= word >>> 1 & 0x55555555; word = (word & 0x33333333) + (word >>> 2 & 0x33333333); return Math.imul(word + (word >>> 4) & 0x0f0f0f0f, 0x01010101) >>> 24; }
289
+ /**
290
+ * Points of one word of a line for the runs (rule 1) and finder-like sequences (rule 3) that start in its own cells. `equal` has a
291
+ * bit for each cell that matches the next, `inside` one for each cell of the word that is in the symbol.
292
+ */
293
+ function wordPenalty(word, equal, inside) {
294
+ let score = 0;
295
+ // A run of n ≥ 5 modules holds n−4 windows of five equal modules and scores n−2: one a window, and two more where the run starts.
296
+ const five = equal & equal >>> 1 & equal >>> 2 & equal >>> 3 & OWN;
297
+ if (five)
298
+ score = count(five) + 2 * count(five & ~(equal << 1));
299
+ // 1011101 with four light modules after it or before it, all eleven inside the symbol.
300
+ const core = word & ~(word >>> 1) & word >>> 2 & word >>> 3 & word >>> 4 & ~(word >>> 5) & word >>> 6;
301
+ if (core) {
302
+ const light = ~(word | word >>> 1 | word >>> 2 | word >>> 3), found = (core & light >>> 7 | light & core >>> 4) & inside >>> 10 & OWN;
303
+ if (found)
304
+ score += 40 * count(found);
305
+ }
306
+ return score;
307
+ }
308
+ /** The function patterns of a version and the cells they reserve: finders, separators, alignment and timing patterns, with the format and version cells left light. */
309
+ function template(version) {
310
+ const size = 17 + 4 * version, modules = new Uint8Array(size * size), reserved = new Uint8Array(size * size);
311
+ const set = (column, row, dark) => { modules[row * size + column] = dark ? 1 : 0; reserved[row * size + column] = 1; };
312
+ for (const [left, top] of [[0, 0], [size - 7, 0], [0, size - 7]])
313
+ for (let dy = -1; dy < 8; dy++)
314
+ for (let dx = -1; dx < 8; dx++) {
315
+ const column = left + dx, row = top + dy;
316
+ if (column < 0 || row < 0 || column >= size || row >= size)
317
+ continue;
318
+ const ring = Math.max(Math.abs(dx - 3), Math.abs(dy - 3));
319
+ set(column, row, ring !== 2 && ring < 4);
320
+ }
321
+ const centres = qrAlignmentCentres(version);
322
+ for (const row of centres)
323
+ for (const column of centres) {
324
+ if (reserved[row * size + column])
325
+ continue;
326
+ for (let dy = -2; dy <= 2; dy++)
327
+ for (let dx = -2; dx <= 2; dx++)
328
+ set(column + dx, row + dy, Math.max(Math.abs(dx), Math.abs(dy)) !== 1);
329
+ }
330
+ for (let i = 0; i < size; i++) {
331
+ if (!reserved[6 * size + i])
332
+ set(i, 6, i % 2 === 0);
333
+ if (!reserved[i * size + 6])
334
+ set(6, i, i % 2 === 0);
335
+ }
336
+ for (let i = 0; i < 9; i++) {
337
+ if (i !== 6) {
338
+ set(8, i, 0);
339
+ set(i, 8, 0);
340
+ }
341
+ if (i < 8) {
342
+ set(size - 1 - i, 8, 0);
343
+ set(8, size - 1 - i, 0);
344
+ }
345
+ }
346
+ if (version >= 7)
347
+ for (let i = 0; i < 18; i++) {
348
+ const a = size - 11 + i % 3, b = Math.floor(i / 3);
349
+ set(a, b, 0);
350
+ set(b, a, 0);
351
+ }
352
+ return { modules, reserved };
353
+ }
354
+ /**
355
+ * The penalty of a symbol under a mask pattern. `lines` is the symbol before masking and `free` the cells the mask may invert,
356
+ * both as `pack` writes them. No rule takes points away, so a candidate is given up, and `limit` returned, as soon as its
357
+ * score reaches `limit`: it cannot be lower than a score already found.
358
+ */
359
+ function maskPenalty(lines, free, size, mask, limit) {
360
+ const words = Math.ceil(size / SPAN), inside = new Int32Array(words), above = new Int32Array(2 * words), patterns = maskPatterns();
361
+ for (let word = 0; word < words; word++) {
362
+ const cells = size + 1 - word * SPAN;
363
+ inside[word] = (cells < 32 ? (1 << cells) - 1 : -1) & (word ? -1 : -2);
364
+ }
365
+ let score = 0, dark = 0, at = 0;
366
+ // Along the rows: runs, finder-like sequences, 2×2 blocks against the row above, and the dark count.
367
+ for (let row = 0; row < size; row++) {
368
+ const pattern = (mask * 12 + row % 12) * 2;
369
+ for (let word = 0; word < words; word++, at++) {
370
+ const cells = lines[at] ^ patterns[pattern + (word & 1)] & free[at], valid = inside[word], equal = ~(cells ^ cells >>> 1) & valid & valid >>> 1;
371
+ const blocks = equal & above[words + word] & ~(cells ^ above[word]) & OWN;
372
+ if (blocks)
373
+ score += 3 * count(blocks);
374
+ above[word] = cells;
375
+ above[words + word] = equal;
376
+ dark += count(cells & OWN);
377
+ score += wordPenalty(cells, equal, valid);
378
+ }
379
+ if (score >= limit)
380
+ return limit;
381
+ }
382
+ // Down the columns: runs and finder-like sequences.
383
+ for (let column = 0; column < size; column++) {
384
+ const pattern = MASK_COLUMNS + (mask * 6 + column % 6) * 4;
385
+ for (let word = 0; word < words; word++, at++) {
386
+ const cells = lines[at] ^ patterns[pattern + (word & 3)] & free[at], valid = inside[word];
387
+ score += wordPenalty(cells, ~(cells ^ cells >>> 1) & valid & valid >>> 1, valid);
388
+ }
389
+ if (score >= limit)
390
+ return limit;
391
+ }
392
+ const total = size * size;
393
+ return score + 10 * Math.ceil(Math.max(0, Math.abs(dark * 100 - total * 50) - total * 5) / (total * 5));
394
+ }
395
+ /**
396
+ * The mask penalty of a symbol (ISO/IEC 18004 §7.8.3): runs of five or more, 2×2 blocks, finder-like sequences and the share of
397
+ * dark modules. `encodeQR` scores each candidate with its format and version cells still light, as the `qr` encoder behind Cuer
398
+ * and python-qrcode do, so HIG picks the same mask they pick.
399
+ */
400
+ export function qrPenalty(modules, size) { const { lines, free } = pack(modules, new Uint8Array(size * size).fill(1), size); return maskPenalty(lines, free, size, 0, Infinity); }
401
+ /** Why a value cannot be encoded: it is longer than a version 40 symbol (or the requested version) holds at the level. */
402
+ export class QRCapacityError extends RangeError {
403
+ constructor(message) { super(message); this.name = 'QRCapacityError'; }
404
+ }
405
+ /**
406
+ * Encodes a string as a QR code symbol. Throws `QRCapacityError` when the value does not fit, and `RangeError` or `TypeError`
407
+ * for an option outside its range. The same value and options always give the same modules.
408
+ */
409
+ export function encodeQR(value, options = {}) {
410
+ if (typeof value !== 'string')
411
+ throw new TypeError('QR value must be a string');
412
+ const level = options.errorCorrection ?? 'medium';
413
+ if (!LEVELS.includes(level))
414
+ throw new RangeError(`Unknown QR error correction: ${String(level)}`);
415
+ const mode = options.mode ?? qrMode(value);
416
+ if (!(mode in MODE_BITS))
417
+ throw new RangeError(`Unknown QR mode: ${String(mode)}`);
418
+ if (mode !== 'byte') {
419
+ const limit = mode === 'numeric' ? 10 : 45;
420
+ for (let i = 0; i < value.length; i++) {
421
+ const v = alphanumericValue(value.charCodeAt(i));
422
+ if (v < 0 || v >= limit)
423
+ throw new RangeError(`QR ${mode} mode cannot hold ${JSON.stringify(value[i])}`);
424
+ }
425
+ }
426
+ if (options.version !== undefined && (!Number.isInteger(options.version) || options.version < 1 || options.version > 40))
427
+ throw new RangeError('QR version must be an integer from 1 to 40');
428
+ if (options.mask !== undefined && (!Number.isInteger(options.mask) || options.mask < 0 || options.mask > 7))
429
+ throw new RangeError('QR mask must be an integer from 0 to 7');
430
+ const bytes = mode === 'byte' ? qrUtf8(value) : null, length = bytes ? bytes.length : value.length, payload = payloadBits(mode, length);
431
+ const fits = (version) => length < 2 ** countBits(mode, version) && 4 + countBits(mode, version) + payload <= layout(version, level).data * 8;
432
+ let version = options.version ?? 1;
433
+ if (options.version === undefined)
434
+ while (version <= 40 && !fits(version))
435
+ version++;
436
+ if (version > 40 || !fits(version))
437
+ throw new QRCapacityError(`QR value of ${length} ${mode === 'byte' ? 'bytes' : 'characters'} does not fit ${options.version === undefined ? 'a version 40 symbol' : `version ${version}`} at ${level} error correction`);
438
+ const size = 17 + 4 * version, { modules, reserved } = template(version), stream = codewords(value, bytes, mode, version, level);
439
+ // Two-module columns from the right edge, alternately upward and downward, skipping the vertical timing column. Each cell
440
+ // that is not reserved takes the next bit of the stream; the remainder bits past the last codeword stay light.
441
+ for (let right = size - 1, step = -size, bit = 0, bits = stream.length * 8; right >= 1; right -= 2, step = -step) {
442
+ if (right === 6)
443
+ right = 5;
444
+ for (let i = 0, at = (step < 0 ? size - 1 : 0) * size + right; i < size; i++, at += step)
445
+ for (let cell = at; cell > at - 2; cell--)
446
+ if (!reserved[cell]) {
447
+ if (bit < bits)
448
+ modules[cell] = stream[bit >>> 3] >>> (7 - (bit & 7)) & 1;
449
+ bit++;
450
+ }
451
+ }
452
+ // The candidates are scored from the symbol as bits; only the mask that wins is applied to the modules.
453
+ let mask = options.mask;
454
+ if (mask === undefined) {
455
+ const { lines, free } = pack(modules, reserved, size);
456
+ let lowest = Infinity;
457
+ mask = 0;
458
+ for (let candidate = 0; candidate < 8; candidate++) {
459
+ const score = maskPenalty(lines, free, size, candidate, lowest);
460
+ if (score < lowest) {
461
+ lowest = score;
462
+ mask = candidate;
463
+ }
464
+ }
465
+ }
466
+ // A row's pattern words hold its columns 21 a word, from bit 1: the mask inverts each cell that is not reserved.
467
+ const patterns = maskPatterns();
468
+ for (let row = 0, at = 0, end = size; row < size; row++, end += size) {
469
+ const pattern = (mask * 12 + row % 12) * 2;
470
+ for (let word = 0; at < end; word++)
471
+ for (let inverts = patterns[pattern + (word & 1)] >>> 1, stop = Math.min(at + SPAN, end); at < stop; at++, inverts >>>= 1)
472
+ modules[at] = modules[at] ^ inverts & (reserved[at] ^ 1);
473
+ }
474
+ const result = modules, format = qrFormatBits(level, mask);
475
+ const set = (column, row, bit) => { result[row * size + column] = bit; };
476
+ for (let i = 0; i < 15; i++) {
477
+ const bit = format >>> i & 1;
478
+ if (i < 6)
479
+ set(8, i, bit);
480
+ else if (i < 8)
481
+ set(8, i + 1, bit);
482
+ else if (i === 8)
483
+ set(7, 8, bit);
484
+ else
485
+ set(14 - i, 8, bit);
486
+ if (i < 8)
487
+ set(size - 1 - i, 8, bit);
488
+ else
489
+ set(8, size - 15 + i, bit);
490
+ }
491
+ set(8, size - 8, 1);
492
+ if (version >= 7) {
493
+ const bits = qrVersionBits(version);
494
+ for (let i = 0; i < 18; i++) {
495
+ const bit = bits >>> i & 1, a = size - 11 + i % 3, b = Math.floor(i / 3);
496
+ set(a, b, bit);
497
+ set(b, a, bit);
498
+ }
499
+ }
500
+ return { size, version, errorCorrection: level, mask, mode, modules: result };
501
+ }
502
+ /** The modules of a symbol as rows of booleans (`true` is dark), the shape other encoders return. */
503
+ export function qrRows(matrix) {
504
+ return Array.from({ length: matrix.size }, (_, row) => Array.from({ length: matrix.size }, (_, column) => matrix.modules[row * matrix.size + column] === 1));
505
+ }
506
+ /** Modules a finder pattern spans on each side. */
507
+ export const QR_FINDER = 7;
508
+ /** Quiet zone ISO/IEC 18004 asks for around a symbol, in modules. */
509
+ export const QR_QUIET_ZONE = 4;
510
+ /** Radius of a data module's dot, in modules: Cuer's cell with its 10% inset, fully rounded. */
511
+ export const QR_DOT_RADIUS = 0.4;
512
+ /** Edge of the cleared centre square for a symbol: a quarter of the side, made odd so it is centred on the centre module. */
513
+ export function qrArenaSize(size) { const quarter = Math.floor(size / 4); return quarter % 2 === 1 ? quarter : quarter + 1; }
514
+ // A rounded square as one closed sub-path, clockwise or anticlockwise; a ring is a clockwise square around an anticlockwise one.
515
+ function roundedSquare(x, y, edge, radius, clockwise) {
516
+ const side = edge - 2 * radius, r = `${radius} ${radius}`;
517
+ return clockwise
518
+ ? `M${x + radius} ${y}h${side}a${r} 0 0 1 ${radius} ${radius}v${side}a${r} 0 0 1 ${-radius} ${radius}h${-side}a${r} 0 0 1 ${-radius} ${-radius}v${-side}a${r} 0 0 1 ${radius} ${-radius}z`
519
+ : `M${x + radius} ${y}a${r} 0 0 0 ${-radius} ${radius}v${side}a${r} 0 0 0 ${radius} ${radius}h${side}a${r} 0 0 0 ${radius} ${-radius}v${-side}a${r} 0 0 0 ${-radius} ${-radius}z`;
520
+ }
521
+ /**
522
+ * The Cuer look as one path: round dots for the data modules and rounded finder patterns, with the centre left clear when an
523
+ * arena is drawn. The ring is Cuer's one-module stroke around a 6×6 square of radius 1.25, written as a filled outline (7×7,
524
+ * radius 1.75) with a hole (5×5, radius 0.75); the core is 3×3 with radius 0.5.
525
+ */
526
+ export function qrGeometry(matrix, options = {}) {
527
+ const { size, modules } = matrix, far = size - QR_FINDER, cleared = options.arena ? qrArenaSize(size) : 0, origin = (size - cleared) / 2, diameter = QR_DOT_RADIUS * 2;
528
+ const arc = `a${QR_DOT_RADIUS} ${QR_DOT_RADIUS} 0 1 0 `;
529
+ // One circle is two half arcs. Every dot of a column starts with the same text and every dot of a row ends with the same text, so each
530
+ // is written once: no number is formatted a dot. Tenths of a module are exact enough in a double to print without rounding noise.
531
+ const starts = [];
532
+ for (let column = 0; column < size; column++)
533
+ starts.push(`M${(column * 10 + 1) / 10} `);
534
+ let path = '', dots = 0;
535
+ for (let row = 0; row < size; row++) {
536
+ const tail = `${row + 0.5}${arc}${diameter} 0${arc}${-diameter} 0`, hole = cleared > 0 && row >= origin && row < origin + cleared;
537
+ // The columns of a finder pattern are skipped in the rows it spans, and the arena's in the rows it clears.
538
+ for (let column = row < QR_FINDER || row >= far ? QR_FINDER : 0, end = row < QR_FINDER ? far : size, at = row * size; column < end; column++) {
539
+ if (hole && column === origin)
540
+ column += cleared;
541
+ if (modules[at + column]) {
542
+ path += starts[column] + tail;
543
+ dots++;
544
+ }
545
+ }
546
+ }
547
+ const finders = [{ x: 0, y: 0 }, { x: far, y: 0 }, { x: 0, y: far }];
548
+ for (const { x, y } of finders)
549
+ path += roundedSquare(x, y, 7, 1.75, true) + roundedSquare(x + 1, y + 1, 5, 0.75, false) + roundedSquare(x + 2, y + 2, 3, 0.5, true);
550
+ return { size, path, dots, finders, arena: cleared ? { origin, size: cleared } : null };
551
+ }
552
+ /**
553
+ * The error correction a code is drawn with. Without an arena it is the host's level, or `medium`. An arena covers the centre
554
+ * of the code, so there the default is `high` and a host's `low` or `medium` is raised to `quartile`: with the centre covered,
555
+ * `quartile` and `high` codes decoded in every case tested (tests/qr-model.test.ts), while some `medium` and most `low` ones did not.
556
+ */
557
+ export function qrErrorCorrection(errorCorrection, arena) {
558
+ if (!arena)
559
+ return errorCorrection ?? 'medium';
560
+ return errorCorrection === undefined ? 'high' : errorCorrection === 'low' || errorCorrection === 'medium' ? 'quartile' : errorCorrection;
561
+ }
562
+ /**
563
+ * Encodes a value and lays it out, for a renderer: `null` when the value does not fit a QR code, so a component can draw
564
+ * nothing instead of throwing while it renders.
565
+ */
566
+ export function qrCodeGeometry(value, options = {}) {
567
+ const arena = options.arena === true;
568
+ try {
569
+ return qrGeometry(encodeQR(value, { errorCorrection: qrErrorCorrection(options.errorCorrection, arena) }), { arena });
570
+ }
571
+ catch (error) {
572
+ if (error instanceof QRCapacityError)
573
+ return null;
574
+ throw error;
575
+ }
576
+ }
577
+ /** Lays a geometry out in a square: the view box, the module size and the arena's box. A `quietZone` below zero or not finite counts as zero. */
578
+ export function qrFrame(geometry, edge, quietZone = QR_QUIET_ZONE) {
579
+ const quiet = Number.isFinite(quietZone) && quietZone > 0 ? quietZone : 0, span = geometry.size + 2 * quiet, unit = edge / span, arena = geometry.arena;
580
+ return { quietZone: quiet, span, viewBox: `${-quiet} ${-quiet} ${span} ${span}`, unit, tileRadius: quiet / 2, arena: arena ? { x: arena.origin + 0.5, size: arena.size - 1, radius: 1, left: (quiet + arena.origin + 0.5) * unit, edge: (arena.size - 1) * unit, corner: unit, inset: (quiet + arena.origin + 0.5) / span, extent: (arena.size - 1) / span } : null };
581
+ }