react-native-nitro-input 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (164) hide show
  1. package/CHANGELOG.md +38 -0
  2. package/README.md +43 -1
  3. package/android/CMakeLists.txt +4 -0
  4. package/android/gradle.properties +1 -1
  5. package/android/src/main/cpp/JRollingEngine.cpp +3 -2
  6. package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroInputView.kt +20 -0
  7. package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroNumberFormatPlatform.kt +209 -0
  8. package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroNumberView.kt +25 -0
  9. package/android/src/main/java/com/margelo/nitro/nitroinput/NitroInputView.kt +69 -10
  10. package/android/src/main/java/com/margelo/nitro/nitroinput/NitroNumberView.kt +164 -24
  11. package/android/src/main/java/com/margelo/nitro/nitroinput/RollingEngine.kt +2 -1
  12. package/cpp/RollingEngine.cpp +12 -2
  13. package/cpp/RollingEngine.hpp +8 -1
  14. package/cpp/numberformat/HybridNitroNumberFormat.cpp +498 -0
  15. package/cpp/numberformat/HybridNitroNumberFormat.hpp +74 -0
  16. package/cpp/numberformat/HybridNitroNumberFormatFactory.hpp +11 -0
  17. package/cpp/numberformat/NumberFormatCore.cpp +629 -0
  18. package/cpp/numberformat/NumberFormatCore.hpp +154 -0
  19. package/cpp/numberformat/NumberFormatProbe.cpp +354 -0
  20. package/cpp/numberformat/NumberFormatProbe.hpp +63 -0
  21. package/ios/HybridNitroInputView.swift +15 -0
  22. package/ios/HybridNitroNumberFormatPlatform.swift +348 -0
  23. package/ios/HybridNitroNumberView.swift +19 -0
  24. package/ios/NitroInputView.swift +79 -19
  25. package/ios/NitroNumberView.swift +171 -31
  26. package/lib/commonjs/nitrogen/generated/shared/json/NitroInputViewConfig.json +5 -0
  27. package/lib/commonjs/nitrogen/generated/shared/json/NitroNumberViewConfig.json +6 -0
  28. package/lib/commonjs/src/NitroInput.js +8 -6
  29. package/lib/commonjs/src/NitroNumber.js +4 -2
  30. package/lib/commonjs/src/NumberFormat.js +525 -0
  31. package/lib/commonjs/src/formatProps.js +48 -0
  32. package/lib/commonjs/src/index.js +3 -1
  33. package/lib/commonjs/src/specs/NumberFormat.nitro.js +2 -0
  34. package/lib/module/nitrogen/generated/shared/json/NitroInputViewConfig.json +5 -0
  35. package/lib/module/nitrogen/generated/shared/json/NitroNumberViewConfig.json +6 -0
  36. package/lib/module/src/NitroInput.js +8 -6
  37. package/lib/module/src/NitroNumber.js +4 -2
  38. package/lib/module/src/NumberFormat.js +521 -0
  39. package/lib/module/src/formatProps.js +45 -0
  40. package/lib/module/src/index.js +1 -0
  41. package/lib/module/src/specs/NumberFormat.nitro.js +1 -0
  42. package/lib/typescript/src/NitroInput.d.ts +22 -0
  43. package/lib/typescript/src/NitroNumber.d.ts +36 -1
  44. package/lib/typescript/src/NumberFormat.d.ts +47 -0
  45. package/lib/typescript/src/formatProps.d.ts +21 -0
  46. package/lib/typescript/src/index.d.ts +2 -0
  47. package/lib/typescript/src/specs/NitroInput.nitro.d.ts +10 -0
  48. package/lib/typescript/src/specs/NitroNumber.nitro.d.ts +13 -1
  49. package/lib/typescript/src/specs/NumberFormat.nitro.d.ts +139 -0
  50. package/nitro.json +16 -0
  51. package/nitrogen/generated/android/NitroInput+autolinking.cmake +6 -0
  52. package/nitrogen/generated/android/NitroInputOnLoad.cpp +28 -0
  53. package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.cpp +45 -0
  54. package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.hpp +10 -0
  55. package/nitrogen/generated/android/c++/JHybridNitroNumberFormatPlatformSpec.cpp +118 -0
  56. package/nitrogen/generated/android/c++/JHybridNitroNumberFormatPlatformSpec.hpp +64 -0
  57. package/nitrogen/generated/android/c++/JHybridNitroNumberViewSpec.cpp +54 -0
  58. package/nitrogen/generated/android/c++/JHybridNitroNumberViewSpec.hpp +12 -0
  59. package/nitrogen/generated/android/c++/JHybridNitroPlatformNumberFormatterSpec.cpp +65 -0
  60. package/nitrogen/generated/android/c++/JHybridNitroPlatformNumberFormatterSpec.hpp +64 -0
  61. package/nitrogen/generated/android/c++/JNitroNumberDirection.hpp +3 -0
  62. package/nitrogen/generated/android/c++/JNumberFormatCompactDisplay.hpp +58 -0
  63. package/nitrogen/generated/android/c++/JNumberFormatCurrencyDisplay.hpp +64 -0
  64. package/nitrogen/generated/android/c++/JNumberFormatCurrencySign.hpp +58 -0
  65. package/nitrogen/generated/android/c++/JNumberFormatNotation.hpp +64 -0
  66. package/nitrogen/generated/android/c++/JNumberFormatPlatformOptions.hpp +161 -0
  67. package/nitrogen/generated/android/c++/JNumberFormatPlatformSymbols.hpp +89 -0
  68. package/nitrogen/generated/android/c++/JNumberFormatRoundingMode.hpp +79 -0
  69. package/nitrogen/generated/android/c++/JNumberFormatSignDisplay.hpp +67 -0
  70. package/nitrogen/generated/android/c++/JNumberFormatStyle.hpp +64 -0
  71. package/nitrogen/generated/android/c++/JNumberFormatUnitDisplay.hpp +61 -0
  72. package/nitrogen/generated/android/c++/views/JHybridNitroInputViewStateUpdater.cpp +25 -0
  73. package/nitrogen/generated/android/c++/views/JHybridNitroNumberViewStateUpdater.cpp +30 -0
  74. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroInputViewSpec.kt +30 -0
  75. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroNumberFormatPlatformSpec.kt +60 -0
  76. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroNumberViewSpec.kt +36 -0
  77. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroPlatformNumberFormatterSpec.kt +62 -0
  78. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroNumberDirection.kt +2 -1
  79. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatCompactDisplay.kt +23 -0
  80. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatCurrencyDisplay.kt +25 -0
  81. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatCurrencySign.kt +23 -0
  82. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatNotation.kt +25 -0
  83. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatPlatformOptions.kt +136 -0
  84. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatPlatformSymbols.kt +91 -0
  85. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatRoundingMode.kt +30 -0
  86. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatSignDisplay.kt +26 -0
  87. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatStyle.kt +25 -0
  88. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatUnitDisplay.kt +24 -0
  89. package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Bridge.cpp +34 -0
  90. package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Bridge.hpp +63 -0
  91. package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Umbrella.hpp +40 -0
  92. package/nitrogen/generated/ios/NitroInputAutolinking.mm +18 -0
  93. package/nitrogen/generated/ios/NitroInputAutolinking.swift +12 -0
  94. package/nitrogen/generated/ios/c++/HybridNitroInputViewSpecSwift.hpp +30 -0
  95. package/nitrogen/generated/ios/c++/HybridNitroNumberFormatPlatformSpecSwift.cpp +11 -0
  96. package/nitrogen/generated/ios/c++/HybridNitroNumberFormatPlatformSpecSwift.hpp +122 -0
  97. package/nitrogen/generated/ios/c++/HybridNitroNumberViewSpecSwift.hpp +42 -0
  98. package/nitrogen/generated/ios/c++/HybridNitroPlatformNumberFormatterSpecSwift.cpp +11 -0
  99. package/nitrogen/generated/ios/c++/HybridNitroPlatformNumberFormatterSpecSwift.hpp +94 -0
  100. package/nitrogen/generated/ios/c++/views/HybridNitroInputViewComponent.mm +30 -0
  101. package/nitrogen/generated/ios/c++/views/HybridNitroNumberViewComponent.mm +36 -0
  102. package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec.swift +5 -0
  103. package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec_cxx.swift +55 -0
  104. package/nitrogen/generated/ios/swift/HybridNitroNumberFormatPlatformSpec.swift +56 -0
  105. package/nitrogen/generated/ios/swift/HybridNitroNumberFormatPlatformSpec_cxx.swift +159 -0
  106. package/nitrogen/generated/ios/swift/HybridNitroNumberViewSpec.swift +6 -0
  107. package/nitrogen/generated/ios/swift/HybridNitroNumberViewSpec_cxx.swift +144 -0
  108. package/nitrogen/generated/ios/swift/HybridNitroPlatformNumberFormatterSpec.swift +56 -0
  109. package/nitrogen/generated/ios/swift/HybridNitroPlatformNumberFormatterSpec_cxx.swift +155 -0
  110. package/nitrogen/generated/ios/swift/NitroNumberDirection.swift +4 -0
  111. package/nitrogen/generated/ios/swift/NumberFormatCompactDisplay.swift +40 -0
  112. package/nitrogen/generated/ios/swift/NumberFormatCurrencyDisplay.swift +48 -0
  113. package/nitrogen/generated/ios/swift/NumberFormatCurrencySign.swift +40 -0
  114. package/nitrogen/generated/ios/swift/NumberFormatNotation.swift +48 -0
  115. package/nitrogen/generated/ios/swift/NumberFormatPlatformOptions.swift +159 -0
  116. package/nitrogen/generated/ios/swift/NumberFormatPlatformSymbols.swift +69 -0
  117. package/nitrogen/generated/ios/swift/NumberFormatRoundingMode.swift +68 -0
  118. package/nitrogen/generated/ios/swift/NumberFormatSignDisplay.swift +52 -0
  119. package/nitrogen/generated/ios/swift/NumberFormatStyle.swift +48 -0
  120. package/nitrogen/generated/ios/swift/NumberFormatUnitDisplay.swift +44 -0
  121. package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.cpp +10 -0
  122. package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.hpp +10 -0
  123. package/nitrogen/generated/shared/c++/HybridNitroNumberFormatFactorySpec.cpp +22 -0
  124. package/nitrogen/generated/shared/c++/HybridNitroNumberFormatFactorySpec.hpp +70 -0
  125. package/nitrogen/generated/shared/c++/HybridNitroNumberFormatPlatformSpec.cpp +22 -0
  126. package/nitrogen/generated/shared/c++/HybridNitroNumberFormatPlatformSpec.hpp +70 -0
  127. package/nitrogen/generated/shared/c++/HybridNitroNumberFormatSpec.cpp +23 -0
  128. package/nitrogen/generated/shared/c++/HybridNitroNumberFormatSpec.hpp +71 -0
  129. package/nitrogen/generated/shared/c++/HybridNitroNumberViewSpec.cpp +12 -0
  130. package/nitrogen/generated/shared/c++/HybridNitroNumberViewSpec.hpp +12 -0
  131. package/nitrogen/generated/shared/c++/HybridNitroPlatformNumberFormatterSpec.cpp +23 -0
  132. package/nitrogen/generated/shared/c++/HybridNitroPlatformNumberFormatterSpec.hpp +65 -0
  133. package/nitrogen/generated/shared/c++/NitroNumberDirection.hpp +4 -0
  134. package/nitrogen/generated/shared/c++/NumberFormatCompactDisplay.hpp +76 -0
  135. package/nitrogen/generated/shared/c++/NumberFormatCurrencyDisplay.hpp +84 -0
  136. package/nitrogen/generated/shared/c++/NumberFormatCurrencySign.hpp +76 -0
  137. package/nitrogen/generated/shared/c++/NumberFormatGrouping.hpp +80 -0
  138. package/nitrogen/generated/shared/c++/NumberFormatNotation.hpp +84 -0
  139. package/nitrogen/generated/shared/c++/NumberFormatOptions.hpp +193 -0
  140. package/nitrogen/generated/shared/c++/NumberFormatPart.hpp +87 -0
  141. package/nitrogen/generated/shared/c++/NumberFormatPlatformOptions.hpp +176 -0
  142. package/nitrogen/generated/shared/c++/NumberFormatPlatformSymbols.hpp +115 -0
  143. package/nitrogen/generated/shared/c++/NumberFormatRoundingMode.hpp +104 -0
  144. package/nitrogen/generated/shared/c++/NumberFormatRoundingPriority.hpp +80 -0
  145. package/nitrogen/generated/shared/c++/NumberFormatSignDisplay.hpp +88 -0
  146. package/nitrogen/generated/shared/c++/NumberFormatStyle.hpp +84 -0
  147. package/nitrogen/generated/shared/c++/NumberFormatTrailingZeroDisplay.hpp +76 -0
  148. package/nitrogen/generated/shared/c++/NumberFormatUnitDisplay.hpp +80 -0
  149. package/nitrogen/generated/shared/c++/ResolvedNumberFormatOptions.hpp +197 -0
  150. package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.cpp +10 -0
  151. package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.hpp +15 -0
  152. package/nitrogen/generated/shared/c++/views/HybridNitroNumberViewComponent.cpp +12 -0
  153. package/nitrogen/generated/shared/c++/views/HybridNitroNumberViewComponent.hpp +18 -0
  154. package/nitrogen/generated/shared/json/NitroInputViewConfig.json +5 -0
  155. package/nitrogen/generated/shared/json/NitroNumberViewConfig.json +6 -0
  156. package/package.json +2 -2
  157. package/src/NitroInput.tsx +42 -7
  158. package/src/NitroNumber.tsx +57 -7
  159. package/src/NumberFormat.ts +563 -0
  160. package/src/formatProps.ts +59 -0
  161. package/src/index.ts +18 -0
  162. package/src/specs/NitroInput.nitro.ts +10 -0
  163. package/src/specs/NitroNumber.nitro.ts +13 -1
  164. package/src/specs/NumberFormat.nitro.ts +145 -0
package/CHANGELOG.md CHANGED
@@ -1,5 +1,43 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.3.0
4
+
5
+ - **`NumberFormat`**: `Intl.NumberFormat`'s API, formatted natively.
6
+ `new NumberFormat(locales, options)` learns the locale's format once from
7
+ the platform's formatter (Foundation / ICU, the data Hermes' `Intl` uses)
8
+ and formats in C++ from then on: `format`, `formatToParts` (on iOS too),
9
+ `formatRange`, `formatRangeToParts`, `resolvedOptions` and
10
+ `supportedLocalesOf`, every option of `Intl.NumberFormatOptions` (compact
11
+ notation, units and currency names printed by the platform formatter),
12
+ numbers, bigints and decimal strings. It passes 242 of test262's 251
13
+ `intl402/NumberFormat` tests on iOS and 244 on Android (Hermes' own `Intl`:
14
+ 121 and 101). On a Galaxy A22 a formatter builds in 9 µs instead of 3.3 ms
15
+ and formats in 2.8 µs instead of 10 µs. Where Hermes departs from ECMA-402 (iOS rounds ties to even on
16
+ the binary double, and prints NaN and infinities without the currency;
17
+ Android shows narrow currency symbols in zh-CN) it follows the
18
+ specification, as V8 does.
19
+ - **`format`** on `NitroNumber` and `NitroInput`: pass a `NumberFormat`
20
+ and the component takes its prefix and suffix, separators, fraction digits
21
+ (and, for the input, where the sign goes) from it; the individual props
22
+ still override.
23
+ - **`letterSpacing`**, **`prefixSpacing` / `suffixSpacing`** and
24
+ **`prefixOffset` / `suffixOffset`** on both components: tracking like
25
+ `Text`'s (affixes in proportion to their size), an exact gap between an
26
+ affix and the digits, and a nudge of an affix after its alignment.
27
+ - **`tabularNums={false}`** on `NitroNumber`: each digit at its own width
28
+ (the font's proportional figures) instead of the widest digit's. A
29
+ changing column's width eases once from the old digit's to the new one's,
30
+ on the roll's own easing, and a reveal keeps its target's widths
31
+ throughout. Tabular stays the default.
32
+ - **`direction="shortest"`** on `NitroNumber`: each digit rolls its own
33
+ shorter way round (1 → 2 is one step up even when the value falls), which
34
+ keeps a big jump calm; `auto` still follows the sign of the change.
35
+ - An `accessibilityLabel` given to `NitroNumber` is what VoiceOver and
36
+ TalkBack read; the figure was read instead.
37
+ - **Breaking:** the Android `minSdkVersion` default is 24 (was 23), as
38
+ React Native's: `NumberFormat` reads the platform's formats through
39
+ `android.icu`.
40
+
3
41
  ## 0.2.0
4
42
 
5
43
  - `react-native-nitro-rolling-number` is part of this package now, and its
package/README.md CHANGED
@@ -11,6 +11,9 @@ one native module.
11
11
  - **[`NitroNumber`](#nitronumber)**: a number that animates its changes.
12
12
  Every digit is a wheel that rolls to its new glyph, or swaps in place the way
13
13
  SwiftUI's `.contentTransition(.numericText())` does.
14
+ - **[`NumberFormat`](#numberformat)**: `Intl.NumberFormat`, formatted
15
+ natively. It learns a locale's format once from the platform and formats in
16
+ C++, with Hermes' output and a fraction of its cost.
14
17
 
15
18
  Fabric only (new architecture), React Native ≥ 0.78, Nitro Modules ≥ 0.37.
16
19
 
@@ -416,6 +419,10 @@ rather than renumbering the columns.
416
419
  | `prefixFontSize` / `suffixFontSize` | `number` | `fontSize` | Their own sizes. |
417
420
  | `affixAlign` | `'baseline' \| 'center' \| 'top' \| 'bottom'` | `'baseline'` | How they line up with the text: `top` pins glyph tops, `bottom` the bottom of the ink (a currency code sits on the digits' baseline). |
418
421
  | `prefixAlign` / `suffixAlign` | same | `affixAlign` | Per-affix override. |
422
+ | `letterSpacing` | number | `0` | Points added after every glyph, like `Text`'s; a smaller affix gets it in proportion to its size. |
423
+ | `prefixSpacing` / `suffixSpacing` | number | the letter spacing | Points between the prefix and the text, and between the text and the suffix. |
424
+ | `prefixOffset` / `suffixOffset` | number | `0` | Points an affix is moved down after its alignment (negative: up). |
425
+ | `format` | `NumberFormat` | none | The amount follows it: prefix and suffix, separators, fraction digits, where the sign goes. Sets `mode="number"`; the individual props override it. |
419
426
  | `placeholder` | `string` | `''` | Shown while empty; the first character reflows it away. `'0'` reads well for amounts. |
420
427
  | `placeholderTextColor` | `ColorValue` | platform | Placeholder color. |
421
428
  | `transition` | `'none' \| 'reflow'` | `'none'` | `'reflow'` runs the glyph engine, so characters glide, slide and fade as the text changes. |
@@ -737,7 +744,7 @@ busy and replays them afterwards.
737
744
  | `flashUpColor`, `flashDownColor` | `ColorValue` | unset | The change flash: digits whose glyph changes light up in the up colour when the value grew, the down colour when it shrank, stay lit while they move, and fade back over `flashDuration` once they have landed. Unset: no flash. |
738
745
  | `flashDuration` | `number` | `600` | ms a change flash takes to fade, once the digit has landed. |
739
746
  | `popOnChange` | `number` | `0` | A punch of the whole figure on every change, peak overshoot 0–1, rung out like the reveal's landing pop. |
740
- | `direction` | `'auto' \| 'up' \| 'down'` | `'auto'` | Roll direction; `auto` follows the sign of the change. |
747
+ | `direction` | `'auto' \| 'up' \| 'down' \| 'shortest'` | `'auto'` | Roll direction; `auto` follows the sign of the change. `shortest` rolls each digit its own shorter way round. |
741
748
  | `reveal` | `boolean` | – | `false` holds the opening frame (`$0.00` in the final layout); `true` plays the reveal to `value`. Unset = a normal NitroNumber. |
742
749
  | `revealStyle` | `'count' \| 'spin'` | `'count'` | The win-meter rollup, or slot reels locking from the left. |
743
750
  | `revealDuration` | `number` | `2200` | ms of the count, or until the last reel locks (holds and the pop come on top). |
@@ -755,6 +762,11 @@ busy and replays them afterwards.
755
762
  | `prefixFontSize` / `suffixFontSize` | `number` | `fontSize` | Smaller (or larger) prefix/suffix, e.g. a currency symbol or code. |
756
763
  | `affixAlign` | `'baseline' \| 'center' \| 'top' \| 'bottom'` | `'baseline'` | How prefix/suffix line up with the digits: `top` pins the glyph tops (cap height), `bottom` the bottom of the glyphs' ink (a currency code sits on the digits' baseline, not down where a comma's tail reaches). |
757
764
  | `prefixAlign` / `suffixAlign` | same | `affixAlign` | Per-affix override, e.g. `$` pinned top and `USD` pinned bottom. |
765
+ | `letterSpacing` | number | `0` | Points added after every glyph, like `Text`'s; a smaller affix gets it in proportion to its size. |
766
+ | `prefixSpacing` / `suffixSpacing` | number | the letter spacing | Points between the prefix and the digits, and between the digits and the suffix. |
767
+ | `prefixOffset` / `suffixOffset` | number | `0` | Points an affix is moved down after its alignment (negative: up). |
768
+ | `format` | `NumberFormat` | none | The number follows it: prefix and suffix, separators, fraction and minimum integer digits. The individual props override it. |
769
+ | `tabularNums` | boolean | `true` | `false` lays each digit out at its own width (proportional figures); a changing column eases once from the old digit's width to the new one's. |
758
770
  | `adjustsFontSizeToFit` | `boolean` | `false` | Shrink the whole number to fit the view's fixed `width`; the view keeps its full height. |
759
771
  | `minimumFontScale` | `number` | `0.5` | Lower bound for `adjustsFontSizeToFit`. |
760
772
  | `allowFontScaling` | `boolean` | `false` | Follow the system text size like `Text` (off by default so amounts keep their design size). |
@@ -839,6 +851,36 @@ to reflow while digits appear (e.g. a counter that grows past `999`).
839
851
  - Props are parsed by Nitro from JSI (no Fabric codegen); the view is a Fabric
840
852
  component, so `style`, `opacity`, `transform` and friends work as usual.
841
853
 
854
+ ## NumberFormat
855
+
856
+ `Intl.NumberFormat`'s API, formatted natively. The first formatter for a
857
+ locale and currency learns its format from the platform's own formatter
858
+ (Foundation on iOS, ICU on Android, the data Hermes' `Intl` uses); every number
859
+ after that is formatted in C++. It behaves as ECMA-402 specifies (callable
860
+ without `new`, a bound `format`, options read and checked in the specified
861
+ order) and passes test262's `intl402/NumberFormat` suite as far as the
862
+ platform's data goes: 242 of 251 tests on iOS and 244 on Android, where Hermes'
863
+ own `Intl` passes 121 and 101.
864
+
865
+ ```ts
866
+ import { NumberFormat, NitroNumber } from 'react-native-nitro-input'
867
+
868
+ const php = new NumberFormat('en-PH', { style: 'currency', currency: 'PHP' })
869
+ php.format(1234.5) // "₱1,234.50"
870
+ php.formatToParts(-12) // on iOS too
871
+ php.formatRange(3, 5) // "₱3.00 – ₱5.00"
872
+ [1, 2].map(php.format) // format is bound
873
+ php.format('12345678901234567890.125') // exact
874
+
875
+ <NitroNumber value={balance} format={php} />
876
+ ```
877
+
878
+ Building a formatter takes 9 µs on a Galaxy A22 against Hermes' 3.3 ms, and
879
+ `format()` 2.8 µs against 10 µs. Every option of `Intl.NumberFormatOptions` is
880
+ supported; compact notation, units and currency names are printed by the
881
+ platform formatter (iOS has no long compact form). Full guide:
882
+ https://ronickg.github.io/react-native-nitro-input/docs/number-format
883
+
842
884
  ## Credits
843
885
 
844
886
  The reflow is based on [Torph](https://torph.lochie.me) by
@@ -21,6 +21,9 @@ add_library(${PACKAGE_NAME} SHARED
21
21
  ../cpp/RollingEngine.cpp
22
22
  ../cpp/worklets/NitroInputWorkletsBridge.cpp
23
23
  ../cpp/worklets/HybridNitroInputWorklets.cpp
24
+ ../cpp/numberformat/NumberFormatCore.cpp
25
+ ../cpp/numberformat/NumberFormatProbe.cpp
26
+ ../cpp/numberformat/HybridNitroNumberFormat.cpp
24
27
  )
25
28
 
26
29
  # Add Nitrogen specs :)
@@ -31,6 +34,7 @@ include_directories(
31
34
  "src/main/cpp"
32
35
  "../cpp"
33
36
  "../cpp/worklets"
37
+ "../cpp/numberformat"
34
38
  )
35
39
 
36
40
  # Worklets are optional: with react-native-worklets installed the views can
@@ -1,4 +1,4 @@
1
- NitroInput_minSdkVersion=23
1
+ NitroInput_minSdkVersion=24
2
2
  NitroInput_targetSdkVersion=36
3
3
  NitroInput_compileSdkVersion=36
4
4
  NitroInput_ndkVersion=29.0.14206865
@@ -157,8 +157,8 @@ int JRollingEngine::frameInto(jni::alias_ref<jni::JArrayDouble> out) {
157
157
  // blurOut, active.
158
158
  // Then the decimal columns laid out and the decimal separator's factor.
159
159
  constexpr size_t kText = RollingEngine::kTextSlots * 4 + 2;
160
- double data[4 + kMaxWheels * 13 + kText];
161
- const size_t needed = 4 + count * 13 + kText;
160
+ double data[4 + kMaxWheels * 14 + kText];
161
+ const size_t needed = 4 + count * 14 + kText;
162
162
  if (count > kMaxWheels || static_cast<size_t>(out->size()) < needed) {
163
163
  return -1;
164
164
  }
@@ -181,6 +181,7 @@ int JRollingEngine::frameInto(jni::alias_ref<jni::JArrayDouble> out) {
181
181
  data[i++] = w.focus;
182
182
  data[i++] = w.grow;
183
183
  data[i++] = w.blurOut;
184
+ data[i++] = w.progress;
184
185
  }
185
186
  for (int slot = 0; slot < RollingEngine::kTextSlots; slot++) {
186
187
  const RollingEngine::TextChange t = engine_.textChange(slot);
@@ -115,6 +115,16 @@ class HybridNitroInputView(private val context: ThemedReactContext) : HybridNitr
115
115
  set(v) { field = v; markConfigDirty() }
116
116
  override var suffixAlign: NitroInputAffixAlign = NitroInputAffixAlign.BASELINE
117
117
  set(v) { field = v; markConfigDirty() }
118
+ override var letterSpacing: Double = 0.0
119
+ set(v) { field = v; markConfigDirty() }
120
+ override var prefixSpacing: Double = Double.POSITIVE_INFINITY
121
+ set(v) { field = v; markConfigDirty() }
122
+ override var suffixSpacing: Double = Double.POSITIVE_INFINITY
123
+ set(v) { field = v; markConfigDirty() }
124
+ override var prefixOffset: Double = 0.0
125
+ set(v) { field = v; markConfigDirty() }
126
+ override var suffixOffset: Double = 0.0
127
+ set(v) { field = v; markConfigDirty() }
118
128
  override var placeholder: String = ""
119
129
  set(v) { field = v; markConfigDirty() }
120
130
  override var placeholderColor: Double = Double.NaN
@@ -373,6 +383,11 @@ class HybridNitroInputView(private val context: ThemedReactContext) : HybridNitr
373
383
  prefixFontSize = Double.NaN
374
384
  suffixFontSize = Double.NaN
375
385
  signPlacement = NitroInputSignPlacement.BEFOREAFFIX
386
+ letterSpacing = 0.0
387
+ prefixSpacing = Double.POSITIVE_INFINITY
388
+ suffixSpacing = Double.POSITIVE_INFINITY
389
+ prefixOffset = 0.0
390
+ suffixOffset = 0.0
376
391
  prefixAlign = NitroInputAffixAlign.BASELINE
377
392
  suffixAlign = NitroInputAffixAlign.BASELINE
378
393
  placeholder = ""
@@ -575,6 +590,11 @@ class HybridNitroInputView(private val context: ThemedReactContext) : HybridNitr
575
590
  placeholderColor = colorFromARGB(placeholderColor),
576
591
  prefixAlign = mapAffixAlign(prefixAlign),
577
592
  suffixAlign = mapAffixAlign(suffixAlign),
593
+ letterSpacing = if (letterSpacing.isFinite()) letterSpacing.toFloat() else 0f,
594
+ prefixSpacing = prefixSpacing.takeIf { it.isFinite() }?.toFloat(),
595
+ suffixSpacing = suffixSpacing.takeIf { it.isFinite() }?.toFloat(),
596
+ prefixOffset = if (prefixOffset.isFinite()) prefixOffset.toFloat() else 0f,
597
+ suffixOffset = if (suffixOffset.isFinite()) suffixOffset.toFloat() else 0f,
578
598
  adjustsFontSizeToFit = adjustsFontSizeToFit,
579
599
  minimumFontScale = finite(minimumFontScale, 0.5).coerceIn(0.05, 1.0).toFloat(),
580
600
  allowFontScaling = allowFontScaling,
@@ -0,0 +1,209 @@
1
+ package com.margelo.nitro.nitroinput
2
+
3
+ import android.icu.text.CompactDecimalFormat
4
+ import android.icu.text.DecimalFormat
5
+ import android.icu.text.DecimalFormatSymbols
6
+ import android.icu.text.MeasureFormat
7
+ import android.icu.text.NumberFormat
8
+ import android.icu.text.NumberingSystem
9
+ import android.icu.util.Currency
10
+ import android.icu.util.Measure
11
+ import android.icu.util.MeasureUnit
12
+ import android.icu.util.ULocale
13
+ import android.os.Build
14
+ import androidx.annotation.Keep
15
+ import com.facebook.proguard.annotations.DoNotStrip
16
+ import java.math.BigDecimal
17
+
18
+ /**
19
+ * ICU's `NumberFormat`, for the C++ `NumberFormat`: C++ learns a locale's
20
+ * format from it once (see NumberFormatProbe) and formats with it only for
21
+ * what C++ does not draw itself (compact and scientific notation, units,
22
+ * currency names). Hermes' `Intl.NumberFormat` uses the same classes on
23
+ * Android, so the two agree.
24
+ */
25
+ @Keep
26
+ @DoNotStrip
27
+ class HybridNitroNumberFormatPlatform : HybridNitroNumberFormatPlatformSpec() {
28
+ override fun create(options: NumberFormatPlatformOptions): HybridNitroPlatformNumberFormatterSpec =
29
+ HybridNitroPlatformNumberFormatter(options)
30
+
31
+ override fun supportedLocalesOf(locales: Array<String>): Array<String> =
32
+ locales.mapNotNull { NumberFormatLocales.match(it)?.tag }.toTypedArray()
33
+ }
34
+
35
+ /** ECMA-402's lookup matcher over the locales ICU has number data for. */
36
+ internal object NumberFormatLocales {
37
+ private val available: Set<String> by lazy { NumberFormat.getAvailableLocales().map { it.toLanguageTag() }.toSet() }
38
+
39
+ class Match(val tag: String, val locale: ULocale, val numberingSystem: String?)
40
+
41
+ fun match(requested: String): Match? {
42
+ val locale = try {
43
+ ULocale.forLanguageTag(requested.replace('_', '-'))
44
+ } catch (e: Exception) {
45
+ return null
46
+ }
47
+ val numberingSystem = locale.getKeywordValue("numbers")
48
+ var tag = locale.toLanguageTag().substringBefore("-u-").substringBefore("-x-")
49
+ // ICU lists some locales with their script only ("zh-Hant-TW" for zh-TW): try the likely form too.
50
+ if (!available.contains(tag)) {
51
+ val likely = ULocale.addLikelySubtags(ULocale.forLanguageTag(tag)).toLanguageTag()
52
+ if (available.contains(likely)) return Match(tag, ULocale.forLanguageTag(likely), numberingSystem)
53
+ }
54
+ while (tag.isNotEmpty() && tag != "und") {
55
+ if (available.contains(tag)) return Match(tag, ULocale.forLanguageTag(tag), numberingSystem)
56
+ tag = tag.substringBeforeLast('-', "")
57
+ }
58
+ return null
59
+ }
60
+
61
+ /** The first requested locale ICU knows, or the device's. */
62
+ fun resolve(requested: Array<String>): Match =
63
+ requested.firstNotNullOfOrNull { match(it) } ?: ULocale.getDefault().let { Match(it.toLanguageTag(), it, null) }
64
+ }
65
+
66
+ @Keep
67
+ @DoNotStrip
68
+ class HybridNitroPlatformNumberFormatter(o: NumberFormatPlatformOptions) : HybridNitroPlatformNumberFormatterSpec() {
69
+ private val format: NumberFormat
70
+ private val unit: MeasureUnit?
71
+ private val measureFormat: MeasureFormat?
72
+ /** `unit: 'percent'`, which MeasureFormat prints without its sign: a percent formatter that does not multiply. */
73
+ private val percentUnit: NumberFormat?
74
+ override val symbols: NumberFormatPlatformSymbols
75
+
76
+ init {
77
+ val match = NumberFormatLocales.resolve(o.locales)
78
+ val numberingSystem = o.numberingSystem ?: match.numberingSystem
79
+ val locale = if (numberingSystem != null) match.locale.setKeywordValue("numbers", numberingSystem) else match.locale
80
+ val scientific = o.notation == NumberFormatNotation.SCIENTIFIC || o.notation == NumberFormatNotation.ENGINEERING
81
+ val currencyStyle = when (o.currencyDisplay) {
82
+ NumberFormatCurrencyDisplay.CODE -> NumberFormat.ISOCURRENCYSTYLE
83
+ NumberFormatCurrencyDisplay.NAME -> NumberFormat.PLURALCURRENCYSTYLE
84
+ else -> if (o.currencySign == NumberFormatCurrencySign.ACCOUNTING) NumberFormat.ACCOUNTINGCURRENCYSTYLE else NumberFormat.CURRENCYSTYLE
85
+ }
86
+ format = when {
87
+ o.notation == NumberFormatNotation.COMPACT && o.style != NumberFormatStyle.CURRENCY -> CompactDecimalFormat.getInstance(
88
+ locale,
89
+ if (o.compactDisplay == NumberFormatCompactDisplay.LONG) CompactDecimalFormat.CompactStyle.LONG else CompactDecimalFormat.CompactStyle.SHORT,
90
+ )
91
+ scientific -> NumberFormat.getInstance(locale, NumberFormat.SCIENTIFICSTYLE)
92
+ o.style == NumberFormatStyle.PERCENT -> NumberFormat.getInstance(locale, NumberFormat.PERCENTSTYLE)
93
+ o.style == NumberFormatStyle.CURRENCY -> NumberFormat.getInstance(locale, currencyStyle)
94
+ else -> NumberFormat.getInstance(locale, NumberFormat.NUMBERSTYLE)
95
+ }
96
+
97
+ var currencyShown = ""
98
+ val code = o.currency
99
+ if (o.style == NumberFormatStyle.CURRENCY && code != null) {
100
+ val currency = Currency.getInstance(code)
101
+ format.currency = currency
102
+ val decimal = format as? DecimalFormat
103
+ if (o.currencyDisplay == NumberFormatCurrencyDisplay.NARROWSYMBOL && decimal != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
104
+ val narrow = currency.getName(locale, Currency.NARROW_SYMBOL_NAME, null)
105
+ decimal.decimalFormatSymbols = decimal.decimalFormatSymbols.apply { currencySymbol = narrow }
106
+ }
107
+ currencyShown = if (o.currencyDisplay == NumberFormatCurrencyDisplay.CODE) code else decimal?.decimalFormatSymbols?.currencySymbol ?: currency.getSymbol(locale)
108
+ }
109
+
110
+ format.isGroupingUsed = o.useGrouping
111
+ format.minimumIntegerDigits = o.minimumIntegerDigits.toInt()
112
+ val decimal = format as? DecimalFormat
113
+ if (o.minimumSignificantDigits > 0 && decimal != null) {
114
+ decimal.setSignificantDigitsUsed(true)
115
+ decimal.minimumSignificantDigits = o.minimumSignificantDigits.toInt()
116
+ decimal.maximumSignificantDigits = o.maximumSignificantDigits.toInt()
117
+ } else {
118
+ format.minimumFractionDigits = o.minimumFractionDigits.toInt()
119
+ format.maximumFractionDigits = o.maximumFractionDigits.toInt()
120
+ }
121
+ if (o.notation == NumberFormatNotation.ENGINEERING && decimal != null) decimal.maximumIntegerDigits = 3
122
+ format.roundingMode = when (o.roundingMode) {
123
+ NumberFormatRoundingMode.CEIL -> BigDecimal.ROUND_CEILING
124
+ NumberFormatRoundingMode.FLOOR -> BigDecimal.ROUND_FLOOR
125
+ NumberFormatRoundingMode.EXPAND -> BigDecimal.ROUND_UP
126
+ NumberFormatRoundingMode.TRUNC -> BigDecimal.ROUND_DOWN
127
+ NumberFormatRoundingMode.HALFTRUNC -> BigDecimal.ROUND_HALF_DOWN
128
+ NumberFormatRoundingMode.HALFEVEN -> BigDecimal.ROUND_HALF_EVEN
129
+ else -> BigDecimal.ROUND_HALF_UP
130
+ }
131
+ if (decimal != null) {
132
+ when (o.signDisplay) {
133
+ NumberFormatSignDisplay.ALWAYS, NumberFormatSignDisplay.EXCEPTZERO ->
134
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) decimal.isSignAlwaysShown = true
135
+ else decimal.positivePrefix = decimal.decimalFormatSymbols.plusSign + decimal.positivePrefix
136
+ NumberFormatSignDisplay.NEVER -> {
137
+ decimal.negativePrefix = decimal.positivePrefix
138
+ decimal.negativeSuffix = decimal.positiveSuffix
139
+ }
140
+ else -> Unit
141
+ }
142
+ }
143
+
144
+ if (o.style == NumberFormatStyle.UNIT && o.unit == "percent") {
145
+ percentUnit = (NumberFormat.getInstance(locale, NumberFormat.PERCENTSTYLE) as DecimalFormat).also { p ->
146
+ p.multiplier = 1
147
+ p.isGroupingUsed = format.isGroupingUsed
148
+ p.minimumIntegerDigits = format.minimumIntegerDigits
149
+ p.minimumFractionDigits = format.minimumFractionDigits
150
+ p.maximumFractionDigits = format.maximumFractionDigits
151
+ p.roundingMode = format.roundingMode
152
+ if (decimal != null && decimal.areSignificantDigitsUsed()) {
153
+ p.setSignificantDigitsUsed(true)
154
+ p.minimumSignificantDigits = decimal.minimumSignificantDigits
155
+ p.maximumSignificantDigits = decimal.maximumSignificantDigits
156
+ }
157
+ }
158
+ } else {
159
+ percentUnit = null
160
+ }
161
+ if (o.style == NumberFormatStyle.UNIT && percentUnit == null) {
162
+ val id = o.unit ?: throw IllegalArgumentException("NumberFormat: a unit is required with style 'unit'.")
163
+ unit = MeasureUnit.getAvailable().firstOrNull { it.subtype == id }
164
+ // A compound unit ("acre-per-day") is built from its identifier (Android 11+).
165
+ ?: (if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) try { MeasureUnit.forIdentifier(id) } catch (e: Exception) { null } else null)
166
+ ?: throw IllegalArgumentException("NumberFormat: unknown unit '$id'.")
167
+ val width = when (o.unitDisplay) {
168
+ NumberFormatUnitDisplay.LONG -> MeasureFormat.FormatWidth.WIDE
169
+ NumberFormatUnitDisplay.NARROW -> MeasureFormat.FormatWidth.NARROW
170
+ else -> MeasureFormat.FormatWidth.SHORT
171
+ }
172
+ measureFormat = MeasureFormat.getInstance(locale, width, format)
173
+ } else {
174
+ unit = null
175
+ measureFormat = null
176
+ }
177
+
178
+ val dfs = decimal?.decimalFormatSymbols ?: DecimalFormatSymbols.getInstance(locale)
179
+ val modern = Build.VERSION.SDK_INT >= Build.VERSION_CODES.P
180
+ symbols = NumberFormatPlatformSymbols(
181
+ locale = match.tag,
182
+ numberingSystem = NumberingSystem.getInstance(locale).name,
183
+ minusSign = if (modern) dfs.minusSignString else dfs.minusSign.toString(),
184
+ plusSign = if (modern) dfs.plusSignString else dfs.plusSign.toString(),
185
+ percentSign = if (modern) dfs.percentString else dfs.percent.toString(),
186
+ currency = currencyShown,
187
+ nan = dfs.naN,
188
+ infinity = dfs.infinity,
189
+ exponentSeparator = dfs.exponentSeparator,
190
+ )
191
+ }
192
+
193
+ @Synchronized
194
+ override fun format(value: Double): String {
195
+ percentUnit?.let { return it.format(value) }
196
+ val measure = measureFormat
197
+ return if (measure != null) measure.format(Measure(value, unit)) else format.format(value)
198
+ }
199
+
200
+ @Synchronized
201
+ override fun formatDecimal(value: String): String {
202
+ if (value == "NaN") return format(Double.NaN)
203
+ if (value.endsWith("Infinity")) return format(if (value.startsWith("-")) Double.NEGATIVE_INFINITY else Double.POSITIVE_INFINITY)
204
+ val number = BigDecimal(value)
205
+ percentUnit?.let { return it.format(number) }
206
+ val measure = measureFormat
207
+ return if (measure != null) measure.format(Measure(number, unit)) else format.format(number)
208
+ }
209
+ }
@@ -141,6 +141,18 @@ class HybridNitroNumberView(private val context: ThemedReactContext) : HybridNit
141
141
  set(v) { field = v; markConfigDirty() }
142
142
  override var suffixAlign: NitroNumberAffixAlign? = null
143
143
  set(v) { field = v; markConfigDirty() }
144
+ override var letterSpacing: Double? = null
145
+ set(v) { field = v; markConfigDirty() }
146
+ override var prefixSpacing: Double? = null
147
+ set(v) { field = v; markConfigDirty() }
148
+ override var suffixSpacing: Double? = null
149
+ set(v) { field = v; markConfigDirty() }
150
+ override var prefixOffset: Double? = null
151
+ set(v) { field = v; markConfigDirty() }
152
+ override var suffixOffset: Double? = null
153
+ set(v) { field = v; markConfigDirty() }
154
+ override var tabularNums: Boolean? = null
155
+ set(v) { field = v; markConfigDirty() }
144
156
  override var adjustsFontSizeToFit: Boolean? = null
145
157
  set(v) { field = v; markConfigDirty() }
146
158
  override var minimumFontScale: Double? = null
@@ -309,6 +321,12 @@ class HybridNitroNumberView(private val context: ThemedReactContext) : HybridNit
309
321
  affixAlign = null
310
322
  prefixAlign = null
311
323
  suffixAlign = null
324
+ letterSpacing = null
325
+ prefixSpacing = null
326
+ suffixSpacing = null
327
+ prefixOffset = null
328
+ suffixOffset = null
329
+ tabularNums = null
312
330
  adjustsFontSizeToFit = null
313
331
  minimumFontScale = null
314
332
  allowFontScaling = null
@@ -377,6 +395,12 @@ class HybridNitroNumberView(private val context: ThemedReactContext) : HybridNit
377
395
  color = color?.let { colorFromARGB(it) },
378
396
  prefixAlign = mapAffixAlign(prefixAlign ?: affixAlign),
379
397
  suffixAlign = mapAffixAlign(suffixAlign ?: affixAlign),
398
+ letterSpacing = (letterSpacing ?: 0.0).takeIf { it.isFinite() }?.toFloat() ?: 0f,
399
+ prefixSpacing = prefixSpacing?.takeIf { it.isFinite() }?.toFloat(),
400
+ suffixSpacing = suffixSpacing?.takeIf { it.isFinite() }?.toFloat(),
401
+ prefixOffset = (prefixOffset ?: 0.0).takeIf { it.isFinite() }?.toFloat() ?: 0f,
402
+ suffixOffset = (suffixOffset ?: 0.0).takeIf { it.isFinite() }?.toFloat() ?: 0f,
403
+ tabularNums = tabularNums ?: true,
380
404
  adjustsFontSizeToFit = adjustsFontSizeToFit ?: false,
381
405
  minimumFontScale = (minimumFontScale ?: 0.5).coerceIn(0.05, 1.0).toFloat(),
382
406
  allowFontScaling = allowFontScaling ?: false,
@@ -410,6 +434,7 @@ class HybridNitroNumberView(private val context: ThemedReactContext) : HybridNit
410
434
  direction = when (rollDirection) {
411
435
  NitroNumberDirection.UP -> NitroNumberView.Direction.UP
412
436
  NitroNumberDirection.DOWN -> NitroNumberView.Direction.DOWN
437
+ NitroNumberDirection.SHORTEST -> NitroNumberView.Direction.SHORTEST
413
438
  NitroNumberDirection.AUTO, null -> NitroNumberView.Direction.AUTO
414
439
  },
415
440
  revealDurationMs = Math.max(0.0, revealDuration ?: 2200.0).toLong(),
@@ -131,6 +131,14 @@ class NitroInputView(context: Context) : FrameLayout(context) {
131
131
  val placeholderColor: Int? = null,
132
132
  val prefixAlign: AffixAlign = AffixAlign.BASELINE,
133
133
  val suffixAlign: AffixAlign = AffixAlign.BASELINE,
134
+ /** Dp added after every glyph, as `Text`'s letterSpacing; an affix gets it in proportion to its size. */
135
+ val letterSpacing: Float = 0f,
136
+ /** Dp between the prefix and the text, and between the text and the suffix, in place of the letter spacing there. */
137
+ val prefixSpacing: Float? = null,
138
+ val suffixSpacing: Float? = null,
139
+ /** Dp an affix is moved down (negative: up) after its alignment. */
140
+ val prefixOffset: Float = 0f,
141
+ val suffixOffset: Float = 0f,
134
142
  val adjustsFontSizeToFit: Boolean = false,
135
143
  val minimumFontScale: Float = 0.5f,
136
144
  val allowFontScaling: Boolean = false,
@@ -613,6 +621,14 @@ class NitroInputView(context: Context) : FrameLayout(context) {
613
621
  val suffix = makePaint(t, t.suffixFontSize * scale)
614
622
  private val prefixAlign = t.prefixAlign
615
623
  private val suffixAlign = t.suffixAlign
624
+ /** Letter spacing after a body / prefix / suffix glyph, the seams (null: the letter spacing) and the affix offsets, in px. */
625
+ val bodySpacing = t.letterSpacing * scale * density
626
+ val prefixLetterSpacing = bodySpacing * (t.prefixFontSize / t.fontSize)
627
+ val suffixLetterSpacing = bodySpacing * (t.suffixFontSize / t.fontSize)
628
+ val prefixSeam = t.prefixSpacing?.let { it * scale * density }
629
+ val suffixSeam = t.suffixSpacing?.let { it * scale * density }
630
+ val prefixOffset = t.prefixOffset * scale * density
631
+ val suffixOffset = t.suffixOffset * scale * density
616
632
  private val bodyMetrics: Paint.FontMetrics = body.fontMetrics
617
633
  /** The font's own line box, before any `lineHeight` override. */
618
634
  val fontLineHeight: Float = ceil(bodyMetrics.descent - bodyMetrics.ascent)
@@ -666,8 +682,34 @@ class NitroInputView(context: Context) : FrameLayout(context) {
666
682
  return total
667
683
  }
668
684
 
685
+ /** Letter spacing after a glyph of [role], in px. */
686
+ fun spacing(role: Role): Float = when (role) {
687
+ Role.BODY -> bodySpacing
688
+ Role.PREFIX -> prefixLetterSpacing
689
+ Role.SUFFIX -> suffixLetterSpacing
690
+ }
691
+
692
+ /** The room an affix takes beside the text: its glyphs, their spacing and the seam, in px. */
693
+ fun affixRoom(affix: String, role: Role): Float {
694
+ if (affix.isEmpty()) return 0f
695
+ val count = affix.codePointCount(0, affix.length)
696
+ val glyphs = width(affix, role)
697
+ if (role == Role.PREFIX) return glyphs + prefixLetterSpacing * (count - 1) + (prefixSeam ?: prefixLetterSpacing)
698
+ // The edit text's own letter spacing already follows its last glyph.
699
+ return glyphs + suffixLetterSpacing * count + ((suffixSeam ?: bodySpacing) - bodySpacing)
700
+ }
701
+
669
702
  /** Baseline y for `role` drawing `text`, given the top of the body line box. */
670
703
  fun baseline(role: Role, text: String, lineTop: Float): Float {
704
+ val aligned = alignedBaseline(role, text, lineTop)
705
+ return when (role) {
706
+ Role.BODY -> aligned
707
+ Role.PREFIX -> aligned + prefixOffset
708
+ Role.SUFFIX -> aligned + suffixOffset
709
+ }
710
+ }
711
+
712
+ private fun alignedBaseline(role: Role, text: String, lineTop: Float): Float {
671
713
  // `baselineNudge` re-centres the run when `lineHeight` differs from the
672
714
  // font's: the extra goes above the line, so the body sits that far lower.
673
715
  // Everything pinned to the body follows it; `CENTER` does not, because it
@@ -789,6 +831,8 @@ class NitroInputView(context: Context) : FrameLayout(context) {
789
831
  private fun applyEditTextLayout() {
790
832
  val f = fonts
791
833
  editText.typeface = f.body.typeface
834
+ // Spaced like the overlay, so the selection highlight lines up with the drawn glyphs.
835
+ editText.letterSpacing = if (f.body.textSize > 0f) f.bodySpacing / f.body.textSize else 0f
792
836
  // Not `setTextSize` directly: the platform ignores it while auto-sizing is
793
837
  // on, which would leave the auto-size bounds stale after a font change.
794
838
  applyAutoSize()
@@ -829,9 +873,9 @@ class NitroInputView(context: Context) : FrameLayout(context) {
829
873
  // Relative: the prefix sits at the start edge and the suffix at the end,
830
874
  // so under a right-to-left layout the room for them swaps sides too.
831
875
  editText.setPaddingRelative(
832
- side + f.width(format.prefix, Role.PREFIX).roundToInt(),
876
+ side + f.affixRoom(format.prefix, Role.PREFIX).roundToInt(),
833
877
  frameTopInsetPx.roundToInt() + framePadding + labelOverhang,
834
- side + f.width(format.suffix, Role.SUFFIX).roundToInt(),
878
+ side + f.affixRoom(format.suffix, Role.SUFFIX).roundToInt(),
835
879
  framePadding,
836
880
  )
837
881
  applyAffixes()
@@ -1006,6 +1050,13 @@ class NitroInputView(context: Context) : FrameLayout(context) {
1006
1050
  view.gravity = Gravity.CENTER_VERTICAL
1007
1051
  view.typeface = paint.typeface
1008
1052
  view.setTextSize(TypedValue.COMPLEX_UNIT_PX, paint.textSize)
1053
+ view.letterSpacing = if (paint.textSize > 0f) fonts.spacing(role) / paint.textSize else 0f
1054
+ // The seam against the text, and the affix's own offset.
1055
+ val seam = fonts.affixRoom(affix, role) - fonts.width(affix, role) - fonts.spacing(role) * affix.codePointCount(0, affix.length)
1056
+ val extra = seam.roundToInt().coerceAtLeast(0)
1057
+ view.setPaddingRelative(view.paddingStart + if (edge == Gravity.END) extra else 0, view.paddingTop,
1058
+ view.paddingEnd + if (edge == Gravity.START) extra else 0, view.paddingBottom)
1059
+ view.translationY = if (role == Role.PREFIX) fonts.prefixOffset else fonts.suffixOffset
1009
1060
  view.setTextColor(resolvedTextColor)
1010
1061
  if (view.text?.toString() != affix) view.text = affix
1011
1062
  return view
@@ -1494,9 +1545,12 @@ class NitroInputView(context: Context) : FrameLayout(context) {
1494
1545
  // order they are added - and is only laid out ahead of the prefix.
1495
1546
  val signed = format.signBeforeAffix && !showPlaceholder && format.prefix.isNotEmpty() &&
1496
1547
  shown.isNotEmpty() && isSign(shown[0])
1548
+ // Advances carry the letter spacing; the last prefix glyph and the last
1549
+ // body glyph before a suffix carry the seams instead.
1497
1550
  if (signed) addRun(shown.substring(0, 1), Role.BODY, f, placeholder = false)
1498
- addRun(format.prefix, Role.PREFIX, f, placeholder = false)
1499
- addRun(if (signed) shown.substring(1) else shown, Role.BODY, f, placeholder = showPlaceholder)
1551
+ addRun(format.prefix, Role.PREFIX, f, placeholder = false, lastSpacing = f.prefixSeam)
1552
+ addRun(if (signed) shown.substring(1) else shown, Role.BODY, f, placeholder = showPlaceholder,
1553
+ lastSpacing = if (format.suffix.isNotEmpty()) f.suffixSeam else null)
1500
1554
  addRun(format.suffix, Role.SUFFIX, f, placeholder = false)
1501
1555
  engine.commitText(caret, now())
1502
1556
  fed = true
@@ -1508,14 +1562,17 @@ class NitroInputView(context: Context) : FrameLayout(context) {
1508
1562
  /** A leading minus, in either the keyboard's spelling or the typographic one. */
1509
1563
  private fun isSign(c: Char): Boolean = c == '-' || c == '\u2212'
1510
1564
 
1511
- private fun addRun(s: String, role: Role, f: FontSet, placeholder: Boolean) {
1565
+ private fun addRun(s: String, role: Role, f: FontSet, placeholder: Boolean, lastSpacing: Float? = null) {
1512
1566
  val numberKinds = role == Role.BODY && format.mode == Mode.NUMBER
1567
+ val spacing = f.spacing(role)
1513
1568
  var i = 0
1514
1569
  while (i < s.length) {
1515
1570
  val cp = s.codePointAt(i)
1516
1571
  val kind = if (numberKinds) formatter.kindOf(cp) else 0
1517
- engine.addGlyph(cp, role.ordinal, kind, f.width(cp, role).toDouble(), placeholder)
1518
- i += Character.charCount(cp)
1572
+ val next = i + Character.charCount(cp)
1573
+ val after = if (next >= s.length && lastSpacing != null) lastSpacing else spacing
1574
+ engine.addGlyph(cp, role.ordinal, kind, (f.width(cp, role) + after).toDouble(), placeholder)
1575
+ i = next
1519
1576
  }
1520
1577
  }
1521
1578
 
@@ -1665,9 +1722,11 @@ class NitroInputView(context: Context) : FrameLayout(context) {
1665
1722
  keyboard.secureTextEntry -> "\u2022".repeat(text.codePointCount(0, text.length))
1666
1723
  else -> text
1667
1724
  }
1668
- return fonts.paint(Role.PREFIX).measureText(format.prefix) +
1669
- fonts.paint(Role.BODY).measureText(body) +
1670
- fonts.paint(Role.SUFFIX).measureText(format.suffix)
1725
+ // The edit text's own letter spacing follows every glyph; the affixes take their room with their seams.
1726
+ val f = fonts
1727
+ return f.affixRoom(format.prefix, Role.PREFIX) +
1728
+ f.paint(Role.BODY).measureText(body) + f.bodySpacing * body.codePointCount(0, body.length) +
1729
+ f.affixRoom(format.suffix, Role.SUFFIX)
1671
1730
  }
1672
1731
 
1673
1732
  private fun reportIntrinsicSize() {