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
@@ -60,6 +60,8 @@ private const val TEXT_SUFFIX = 1
60
60
  private const val TEXT_GROUPING = 2
61
61
  private const val TEXT_DECIMAL = 3
62
62
  private const val TEXT_SLOTS = 4
63
+ /** Doubles per wheel in the engine's frame buffer (`RollingEngine.frameInto`). */
64
+ private const val WHEEL_FIELDS = 14
63
65
 
64
66
  class NitroNumberView(context: Context) : View(context) {
65
67
 
@@ -85,6 +87,16 @@ class NitroNumberView(context: Context) : View(context) {
85
87
  val color: Int? = null,
86
88
  val prefixAlign: AffixAlign = AffixAlign.BASELINE,
87
89
  val suffixAlign: AffixAlign = AffixAlign.BASELINE,
90
+ /** Dp added after every glyph, as `Text`'s letterSpacing; an affix gets it in proportion to its size. */
91
+ val letterSpacing: Float = 0f,
92
+ /** Dp between the prefix and the digits, and between the digits and the suffix, in place of the letter spacing there. */
93
+ val prefixSpacing: Float? = null,
94
+ val suffixSpacing: Float? = null,
95
+ /** Dp an affix is moved down (negative: up) after its alignment. */
96
+ val prefixOffset: Float = 0f,
97
+ val suffixOffset: Float = 0f,
98
+ /** Every digit as wide as the widest (tabular figures, the default), or each at its own width. */
99
+ val tabularNums: Boolean = true,
88
100
  val adjustsFontSizeToFit: Boolean = false,
89
101
  val minimumFontScale: Float = 0.5f,
90
102
  val allowFontScaling: Boolean = false,
@@ -93,7 +105,7 @@ class NitroNumberView(context: Context) : View(context) {
93
105
 
94
106
  enum class Easing(val raw: Int) { LINEAR(0), EASE_IN(1), EASE_OUT(2), EASE_IN_OUT(3), SPRING(4) }
95
107
 
96
- enum class Direction(val raw: Int) { AUTO(0), UP(1), DOWN(2) }
108
+ enum class Direction(val raw: Int) { AUTO(0), UP(1), DOWN(2), SHORTEST(3) }
97
109
 
98
110
  data class Timing(
99
111
  /** The odometer roll, or one of the glyph-swap transitions. */
@@ -261,6 +273,14 @@ class NitroNumberView(context: Context) : View(context) {
261
273
  val suffix = makePaint(t, (t.suffixFontSize ?: t.fontSize) * scale)
262
274
  private val prefixAlign = t.prefixAlign
263
275
  private val suffixAlign = t.suffixAlign
276
+ /** Letter spacing after a digit / prefix / suffix glyph, the seams (null: the letter spacing) and the affix offsets, in px. */
277
+ val digitSpacing = t.letterSpacing * scale * density
278
+ val prefixLetterSpacing = digitSpacing * ((t.prefixFontSize ?: t.fontSize) / t.fontSize)
279
+ val suffixLetterSpacing = digitSpacing * ((t.suffixFontSize ?: t.fontSize) / t.fontSize)
280
+ val prefixSeam = t.prefixSpacing?.let { it * scale * density }
281
+ val suffixSeam = t.suffixSpacing?.let { it * scale * density }
282
+ private val prefixOffset = t.prefixOffset * scale * density
283
+ private val suffixOffset = t.suffixOffset * scale * density
264
284
  private val digitMetrics: Paint.FontMetrics = digit.fontMetrics
265
285
  /** Height of the line box (the digit paint's line height), in px. */
266
286
  val lineHeight: Float = ceil(digitMetrics.descent - digitMetrics.ascent)
@@ -268,6 +288,10 @@ class NitroNumberView(context: Context) : View(context) {
268
288
  val numericBlur: Float = NUMERIC_BLUR
269
289
  /** Width of the widest digit glyph, in px. */
270
290
  val digitWidth: Float
291
+ /** Each digit at its own advance instead of the widest's (`tabularNums={false}`). */
292
+ val proportional = !t.tabularNums
293
+ /** The advance of each digit 0…9, in px. */
294
+ val digitWidths = FloatArray(10)
271
295
  private val widthCache = Array(GlyphRole.entries.size) { java.util.concurrent.ConcurrentHashMap<String, Float>() }
272
296
  /**
273
297
  * A wheel's whole digit strip, per blank-zero variant: 12 slots for index
@@ -290,7 +314,7 @@ class NitroNumberView(context: Context) : View(context) {
290
314
  * typography (see [StripCache]).
291
315
  */
292
316
  private val stripKey: String =
293
- "${t.fontFamily}|${t.fontWeight}|${digit.textSize}|${digit.color}|${digit.typeface?.hashCode()}"
317
+ "${t.fontFamily}|${t.fontWeight}|${digit.textSize}|${digit.color}|${digit.typeface?.hashCode()}|${digit.fontFeatureSettings}"
294
318
 
295
319
  /**
296
320
  * This font set's digit masks by digit and level, filled from the shared
@@ -423,7 +447,8 @@ class NitroNumberView(context: Context) : View(context) {
423
447
  private val inkDescentCache = HashMap<String, Float>()
424
448
 
425
449
  init {
426
- digitWidth = (0..9).maxOf { width(it.toString(), GlyphRole.DIGIT) }
450
+ for (d in 0..9) digitWidths[d] = width(d.toString(), GlyphRole.DIGIT)
451
+ digitWidth = digitWidths.max()
427
452
  }
428
453
 
429
454
  fun paint(role: GlyphRole): TextPaint = when (role) {
@@ -453,8 +478,19 @@ class NitroNumberView(context: Context) : View(context) {
453
478
 
454
479
  /** Baseline y for `role` drawing `text`, given the top of the digit line box. */
455
480
  fun baseline(role: GlyphRole, text: String, lineTop: Float): Float {
481
+ if (role == GlyphRole.DIGIT) return lineTop - digitMetrics.ascent
482
+ return alignedBaseline(role, text, lineTop) + if (role == GlyphRole.PREFIX) prefixOffset else suffixOffset
483
+ }
484
+
485
+ /** Letter spacing after a glyph of [role], in px. */
486
+ fun spacing(role: GlyphRole): Float = when (role) {
487
+ GlyphRole.DIGIT -> digitSpacing
488
+ GlyphRole.PREFIX -> prefixLetterSpacing
489
+ GlyphRole.SUFFIX -> suffixLetterSpacing
490
+ }
491
+
492
+ private fun alignedBaseline(role: GlyphRole, text: String, lineTop: Float): Float {
456
493
  val digitBaseline = lineTop - digitMetrics.ascent
457
- if (role == GlyphRole.DIGIT) return digitBaseline
458
494
  val p = paint(role)
459
495
  val m = p.fontMetrics
460
496
  return when (if (role == GlyphRole.PREFIX) prefixAlign else suffixAlign) {
@@ -538,7 +574,8 @@ class NitroNumberView(context: Context) : View(context) {
538
574
  paint.typeface = makeTypeface(t)
539
575
  paint.textSize = sizeDp * density
540
576
  paint.color = t.color ?: defaultTextColor()
541
- paint.fontFeatureSettings = "tnum"
577
+ // Tabular figures unless each digit is laid out at its own width.
578
+ paint.fontFeatureSettings = if (t.tabularNums) "tnum" else "pnum"
542
579
  return paint
543
580
  }
544
581
 
@@ -591,6 +628,7 @@ class NitroNumberView(context: Context) : View(context) {
591
628
  var focus: Double = 1.0,
592
629
  var grow: Double = 1.0,
593
630
  var blurOut: Double = 1.0,
631
+ var progress: Double = 1.0,
594
632
  )
595
633
 
596
634
  private val engine = RollingEngine()
@@ -697,6 +735,7 @@ class NitroNumberView(context: Context) : View(context) {
697
735
  shimmer = Shimmer()
698
736
  alignment = Alignment.AUTO
699
737
  revealMilestones = DoubleArray(0)
738
+ cellWidths.clear()
700
739
  // onIntrinsicSizeChange, onRevealEnd and onRevealMilestone are the hybrid's
701
740
  // wiring, not the element's props: they stay across recycling (the hybrid
702
741
  // clears its own callback props).
@@ -823,7 +862,7 @@ class NitroNumberView(context: Context) : View(context) {
823
862
  }
824
863
 
825
864
  /** Reused per frame so the JNI hop never allocates (room for far more wheels than the engine's 18), then the text slots. */
826
- private val frameBuffer = DoubleArray(4 + 13 * 32 + 4 * TEXT_SLOTS + 2)
865
+ private val frameBuffer = DoubleArray(4 + WHEEL_FIELDS * 32 + 4 * TEXT_SLOTS + 2)
827
866
 
828
867
  // The decimal columns laid out now (more than the format's while dropped
829
868
  // ones close) and the decimal separator's factor.
@@ -849,7 +888,7 @@ class NitroNumberView(context: Context) : View(context) {
849
888
  while (wheels.size < count) wheels.add(Wheel())
850
889
  while (wheels.size > count) wheels.removeAt(wheels.size - 1)
851
890
  for (i in 0 until count) {
852
- val base = 4 + i * 13
891
+ val base = 4 + i * WHEEL_FIELDS
853
892
  val w = wheels[i]
854
893
  w.position = f[base]
855
894
  w.width = f[base + 1]
@@ -864,8 +903,9 @@ class NitroNumberView(context: Context) : View(context) {
864
903
  w.focus = f[base + 10]
865
904
  w.grow = f[base + 11]
866
905
  w.blurOut = f[base + 12]
906
+ w.progress = f[base + 13]
867
907
  }
868
- val text = 4 + count * 13
908
+ val text = 4 + count * WHEEL_FIELDS
869
909
  for (slot in 0 until TEXT_SLOTS) {
870
910
  textGrow[slot] = f[text + slot * 4]
871
911
  textFocus[slot] = f[text + slot * 4 + 1]
@@ -883,6 +923,7 @@ class NitroNumberView(context: Context) : View(context) {
883
923
 
884
924
  private fun rebuildFonts() {
885
925
  fonts = FontSet(typography)
926
+ cellWidths.clear()
886
927
  warmSwapMasks()
887
928
  fontScale = 1f
888
929
  if (engine.hasShownValue()) reportIntrinsicSize()
@@ -937,6 +978,8 @@ class NitroNumberView(context: Context) : View(context) {
937
978
  var slot = -1
938
979
  var fromText: String? = null
939
980
  var anchor = 1
981
+ /** Space after the cell (letter spacing, or an affix seam), scaled with it; not part of the glyph's box. */
982
+ var gap = 0f
940
983
  }
941
984
 
942
985
  /** A list of pooled [Element]s: filling it allocates nothing once it has grown to the run's length. */
@@ -958,7 +1001,7 @@ class NitroNumberView(context: Context) : View(context) {
958
1001
 
959
1002
  fun totalWidth(): Float {
960
1003
  var total = 0f
961
- for (i in 0 until size) total += items[i].width
1004
+ for (i in 0 until size) total += items[i].width + items[i].gap
962
1005
  return total
963
1006
  }
964
1007
  }
@@ -1020,15 +1063,19 @@ class NitroNumberView(context: Context) : View(context) {
1020
1063
  addGlyph("-", GlyphRole.DIGIT, signFactor)
1021
1064
  addGlyph(prefixInk, GlyphRole.PREFIX, 1.0, TEXT_PREFIX, anchor = 1)
1022
1065
  }
1066
+ // Settled, and throughout a reveal (whose layout is the target's from the
1067
+ // first frame), a proportional digit takes its target digit's width.
1068
+ val targetWidths = fonts.proportional && (!animated || engine.isRevealing())
1023
1069
  for (power in wheels.indices.reversed()) {
1024
1070
  val wheel = wheels[power]
1025
1071
  if (wheel.width > 0.0) {
1026
1072
  val e = out.next()
1073
+ val advance = digitAdvance(wheel, power, fonts, targetWidths)
1027
1074
  e.wheelIndex = power
1028
1075
  e.text = null
1029
1076
  e.role = GlyphRole.DIGIT
1030
- e.width = (fonts.digitWidth * wheel.width).toFloat()
1031
- e.fullWidth = fonts.digitWidth
1077
+ e.width = (advance * wheel.width).toFloat()
1078
+ e.fullWidth = advance
1032
1079
  e.factor = wheel.width
1033
1080
  e.slot = -1
1034
1081
  e.fromText = null
@@ -1043,6 +1090,88 @@ class NitroNumberView(context: Context) : View(context) {
1043
1090
  } else {
1044
1091
  addGlyph(suffixInk, GlyphRole.SUFFIX, 1.0, TEXT_SUFFIX, anchor = -1)
1045
1092
  }
1093
+ applySpacing(fonts, out)
1094
+ }
1095
+
1096
+ /**
1097
+ * A wheel's cell width at full size: the widest digit's with tabular
1098
+ * figures; otherwise its digit's own advance, blended between the two digits
1099
+ * it is passing through (a roll between them, or a swap), or the target
1100
+ * digit's when [target] is set.
1101
+ */
1102
+ private fun digitAdvance(wheel: Wheel, power: Int, fonts: FontSet, target: Boolean): Float {
1103
+ if (!fonts.proportional) return fonts.digitWidth
1104
+ val widths = fonts.digitWidths
1105
+ val digit = engine.targetDigit(power).coerceIn(0, 9)
1106
+ val goal = widths[digit]
1107
+ if (target) return goal
1108
+ // The width eases once, from where the column was to its target digit's,
1109
+ // on the wheel's own progress: following the digits it rolled past made
1110
+ // every column pulse as a narrow 1 went by.
1111
+ var cell = cellWidths[power]
1112
+ if (cell == null) {
1113
+ cell = CellWidth()
1114
+ cellWidths[power] = cell
1115
+ cell.progress = 1.0
1116
+ cell.digit = -1
1117
+ }
1118
+ if (cell.digit != digit || wheel.progress < cell.progress) {
1119
+ // Interrupted mid-change, it carries on from what it showed; from rest,
1120
+ // from the digit the wheel is leaving.
1121
+ cell.from = if (cell.digit >= 0 && cell.progress < 1.0) cell.shown else restingWidth(wheel, widths, goal)
1122
+ cell.digit = digit
1123
+ }
1124
+ cell.progress = wheel.progress
1125
+ cell.shown = cell.from + (goal - cell.from) * wheel.progress.toFloat().coerceIn(0f, 1f)
1126
+ return cell.shown
1127
+ }
1128
+
1129
+ /** The width of the digit a wheel shows at the start of a change (blank takes the target's). */
1130
+ private fun restingWidth(wheel: Wheel, widths: FloatArray, goal: Float): Float {
1131
+ val glyph = if (wheel.blend < 1.0) {
1132
+ wheel.fromGlyph.toInt()
1133
+ } else {
1134
+ val position = if (wheel.linear) wheel.position else wrap10(wheel.position)
1135
+ Math.round(position).toInt()
1136
+ }
1137
+ return if (glyph >= 0) widths[glyph % 10] else goal
1138
+ }
1139
+
1140
+ /** A proportional column's width through a change, by place value. */
1141
+ private class CellWidth(var from: Float = 0f, var shown: Float = 0f, var digit: Int = -1, var progress: Double = 1.0)
1142
+
1143
+ private val cellWidths = HashMap<Int, CellWidth>()
1144
+
1145
+ /**
1146
+ * Letter spacing after every cell, and the affix seams: the space between
1147
+ * the prefix and the digits and between the digits and the suffix.
1148
+ */
1149
+ private fun applySpacing(fonts: FontSet, out: ElementList) {
1150
+ for (i in 0 until out.size) out[i].gap = 0f
1151
+ if (fonts.digitSpacing == 0f && fonts.prefixSeam == null && fonts.suffixSeam == null) return
1152
+ fun roleOf(e: Element) = if (e.wheelIndex >= 0) GlyphRole.DIGIT else e.role
1153
+ for (i in 0 until out.size) out[i].gap = fonts.spacing(roleOf(out[i])) * out[i].factor.toFloat()
1154
+ // The seam is the gap at the affix's edge that faces the digits (the far side in RTL).
1155
+ var firstPrefix = -1
1156
+ var lastPrefix = -1
1157
+ var firstSuffix = -1
1158
+ var lastSuffix = -1
1159
+ for (i in 0 until out.size) {
1160
+ when (roleOf(out[i])) {
1161
+ GlyphRole.PREFIX -> { if (firstPrefix < 0) firstPrefix = i; lastPrefix = i }
1162
+ GlyphRole.SUFFIX -> { if (firstSuffix < 0) firstSuffix = i; lastSuffix = i }
1163
+ GlyphRole.DIGIT -> Unit
1164
+ }
1165
+ }
1166
+ val rtl = isRtl
1167
+ fonts.prefixSeam?.let { seam ->
1168
+ if (!rtl && lastPrefix >= 0) out[lastPrefix].gap = seam
1169
+ else if (rtl && firstPrefix > 0) out[firstPrefix - 1].gap = seam
1170
+ }
1171
+ fonts.suffixSeam?.let { seam ->
1172
+ if (!rtl && firstSuffix > 0) out[firstSuffix - 1].gap = seam
1173
+ else if (rtl && lastSuffix >= 0) out[lastSuffix].gap = seam
1174
+ }
1046
1175
  }
1047
1176
 
1048
1177
  private fun updateAffixBlocks(rtl: Boolean) {
@@ -1161,9 +1290,18 @@ class NitroNumberView(context: Context) : View(context) {
1161
1290
  return sb.toString()
1162
1291
  }
1163
1292
 
1293
+ /** An `accessibilityLabel` the app set, which TalkBack reads instead of the figure. */
1294
+ private var explicitDescription: CharSequence? = null
1295
+
1296
+ override fun setContentDescription(contentDescription: CharSequence?) {
1297
+ explicitDescription = contentDescription
1298
+ super.setContentDescription(contentDescription)
1299
+ }
1300
+
1164
1301
  // TalkBack reads the settled figure when it asks for it, so a value update
1165
1302
  // (which can come every frame) formats nothing.
1166
1303
  override fun getContentDescription(): CharSequence? {
1304
+ explicitDescription?.let { if (it.isNotEmpty()) return it }
1167
1305
  if (!engine.hasShownValue()) return null
1168
1306
  return if (loading) accessibleText() + ", loading" else accessibleText()
1169
1307
  }
@@ -1240,13 +1378,13 @@ class NitroNumberView(context: Context) : View(context) {
1240
1378
  val element = elements[i]
1241
1379
  val text = element.text
1242
1380
  if (element.wheelIndex >= 0) {
1243
- drawWheel(canvas, fonts, wheels[element.wheelIndex], x, element.width, originX, originY, scale)
1381
+ drawWheel(canvas, fonts, wheels[element.wheelIndex], x, element.width, element.fullWidth, originX, originY, scale)
1244
1382
  } else if (element.fromText != null) {
1245
1383
  drawTextSwap(canvas, fonts, element, x)
1246
1384
  } else if (text != null) {
1247
1385
  drawGlyph(canvas, fonts, text, element.role, x, element.width, element.fullWidth, element.factor)
1248
1386
  }
1249
- x += element.width
1387
+ x += element.width + element.gap
1250
1388
  }
1251
1389
  if (dim > 0f) {
1252
1390
  drawShimmer(canvas, fonts, total, dim)
@@ -1328,11 +1466,14 @@ class NitroNumberView(context: Context) : View(context) {
1328
1466
  * [originX], [originY] and [scale] are the content transform already on the
1329
1467
  * canvas, so the strip window can be landed on whole device pixels.
1330
1468
  */
1331
- private fun drawWheel(canvas: Canvas, fonts: FontSet, wheel: Wheel, x: Float, width: Float, originX: Float, originY: Float, scale: Float) {
1469
+ private fun drawWheel(canvas: Canvas, fonts: FontSet, wheel: Wheel, x: Float, width: Float, advance: Float, originX: Float, originY: Float, scale: Float) {
1332
1470
  if (width <= 0f) return
1333
1471
  val lineHeight = fonts.lineHeight
1472
+ // The digit column, the widest digit wide, centred on the digit's own cell
1473
+ // (with tabular figures the two are the same, right-aligned in the cell).
1474
+ val columnLeft = x + width - (advance + fonts.digitWidth) / 2f
1334
1475
  if (wheel.blend < 1.0 || wheel.focus < 1.0 || wheel.grow < 1.0) {
1335
- drawSwap(canvas, fonts, wheel, x, width, originX, originY, scale)
1476
+ drawSwap(canvas, fonts, wheel, columnLeft, originX, originY, scale)
1336
1477
  return
1337
1478
  }
1338
1479
  // A settled-width wheel is a window onto the shared strip (the strip has
@@ -1346,7 +1487,7 @@ class NitroNumberView(context: Context) : View(context) {
1346
1487
  // through 0 shows 9); a linear wheel never wraps and uses -1 for the
1347
1488
  // blank slot. Either way the window lands inside the strip.
1348
1489
  val position = if (wheel.linear) wheel.position.coerceIn(-1.0, 10.0) else wrap10(wheel.position)
1349
- var tx = x + width - fonts.digitWidth
1490
+ var tx = columnLeft
1350
1491
  var ty = (-(position + 1) * lineHeight).toFloat()
1351
1492
  if (scale == 1f && position == floor(position)) {
1352
1493
  // At rest, land the window on whole device pixels: composited at a
@@ -1380,7 +1521,7 @@ class NitroNumberView(context: Context) : View(context) {
1380
1521
  return
1381
1522
  }
1382
1523
  canvas.save()
1383
- canvas.clipRect(x, 0f, x + width, lineHeight)
1524
+ canvas.clipRect(columnLeft, 0f, columnLeft + fonts.digitWidth, lineHeight)
1384
1525
  canvas.translate(tx, ty)
1385
1526
  if (strip.node != null) canvas.drawRenderNode(strip.node) else canvas.drawBitmap(strip.bitmap, 0f, 0f, stripPaint)
1386
1527
  // The change flash rides the roll: the strip once more, in the tint, at the same offset.
@@ -1401,11 +1542,10 @@ class NitroNumberView(context: Context) : View(context) {
1401
1542
  // The roll's window, the whole digit wide: a column still opening or
1402
1543
  // closing is not cut to its width (the glyph read as a sliver of its right
1403
1544
  // edge, a ")" of a 0 rolling past), it overhangs the cell's far side, faded.
1404
- canvas.clipRect(x + width - fonts.digitWidth, 0f, x + width, lineHeight)
1545
+ canvas.clipRect(columnLeft, 0f, columnLeft + fonts.digitWidth, lineHeight)
1405
1546
  val base = floor(wheel.position)
1406
1547
  val fraction = (wheel.position - base).toFloat()
1407
1548
  val index = base.toInt()
1408
- val columnLeft = x + width - fonts.digitWidth
1409
1549
  fun glyphs(paint: TextPaint, alpha: Float) {
1410
1550
  // Scaled from the ink colour's alpha and put back (see [drawGlyph]).
1411
1551
  val ink = paint.alpha
@@ -1436,9 +1576,9 @@ class NitroNumberView(context: Context) : View(context) {
1436
1576
  * around the digit. With a change flash on, the pair is drawn again in
1437
1577
  * the tint at the flash's opacity: the ink mixed towards the tint.
1438
1578
  */
1439
- private fun drawSwap(canvas: Canvas, fonts: FontSet, wheel: Wheel, x: Float, width: Float, originX: Float, originY: Float, scale: Float) {
1440
- drawSwapPair(canvas, fonts, wheel, x, width, fonts.digit, 1f, originX, originY, scale)
1441
- flashColor(wheel)?.let { color -> drawSwapPair(canvas, fonts, wheel, x, width, fonts.tinted(color), wheel.flash.toFloat(), originX, originY, scale) }
1579
+ private fun drawSwap(canvas: Canvas, fonts: FontSet, wheel: Wheel, columnLeft: Float, originX: Float, originY: Float, scale: Float) {
1580
+ drawSwapPair(canvas, fonts, wheel, columnLeft, fonts.digit, 1f, originX, originY, scale)
1581
+ flashColor(wheel)?.let { color -> drawSwapPair(canvas, fonts, wheel, columnLeft, fonts.tinted(color), wheel.flash.toFloat(), originX, originY, scale) }
1442
1582
  }
1443
1583
 
1444
1584
  /**
@@ -1449,7 +1589,7 @@ class NitroNumberView(context: Context) : View(context) {
1449
1589
  * blurred copy hides and a lightly blurred one does not. Landed, the last
1450
1590
  * swap frame is pixel for pixel the strip that takes over from it.
1451
1591
  */
1452
- private fun drawSwapPair(canvas: Canvas, fonts: FontSet, wheel: Wheel, x: Float, width: Float, paint: TextPaint, opacity: Float, originX: Float, originY: Float, scale: Float) {
1592
+ private fun drawSwapPair(canvas: Canvas, fonts: FontSet, wheel: Wheel, columnLeft: Float, paint: TextPaint, opacity: Float, originX: Float, originY: Float, scale: Float) {
1453
1593
  val lineHeight = fonts.lineHeight
1454
1594
  // The position clock overshoots 1 (a spring); only the offsets follow it there.
1455
1595
  val b = wheel.blend.toFloat()
@@ -1460,7 +1600,7 @@ class NitroNumberView(context: Context) : View(context) {
1460
1600
  val f = wheel.focus.toFloat().coerceIn(0f, 1f)
1461
1601
  val d = if (wheel.fromAbove) 1f else -1f
1462
1602
  val offset = lineHeight * NUMERIC_OFFSET
1463
- val cx = x + width - fonts.digitWidth / 2f
1603
+ val cx = columnLeft + fonts.digitWidth / 2f
1464
1604
  val cy = lineHeight / 2f
1465
1605
  val column = wheel.width.toFloat().coerceIn(0f, 1f) * opacity
1466
1606
  val from = wheel.fromGlyph.toInt()
@@ -46,7 +46,8 @@ class RollingEngine {
46
46
  external fun revealMilestoneValue(index: Int): Double
47
47
  /**
48
48
  * Fills [out] with the render state without allocating:
49
- * `[signFactor, loadingProgress, revealScale, wheelCount, (position, width, linear, blankZero, fromGlyph, toGlyph, blend, fromAbove, flash, flashUp)…]`.
49
+ * `[signFactor, loadingProgress, revealScale, wheelCount, (position, width, linear, blankZero, fromGlyph, toGlyph, blend, fromAbove, flash, flashUp, focus, grow, blurOut, progress)…]`,
50
+ * then each text slot's swap and the decimal columns.
50
51
  * Returns the number of doubles written, or -1 if [out] is too small.
51
52
  */
52
53
  external fun frameInto(out: DoubleArray): Int
@@ -430,11 +430,16 @@ void RollingEngine::planRoll(Transition& next, const Target& target, bool increa
430
430
  from.linear = true;
431
431
  to = Wheel{digit, 1.0, true, current.blankZero};
432
432
  } else {
433
- // Interior wheel: shortest roll in the direction of the change.
433
+ // Interior wheel: shortest roll in the direction of the change, or
434
+ // with `shortest` the shorter way round for this wheel alone (8 → 2
435
+ // rolls back through 5), a tie (five apart) going with the change.
434
436
  const double base = wrap(from.position);
435
437
  from.position = base;
436
438
  from.linear = false;
437
- const double delta = increasing ? wrap(digit - base) : -wrap(base - digit);
439
+ const double up = wrap(digit - base);
440
+ const double down = wrap(base - digit);
441
+ double delta = increasing ? up : -down;
442
+ if (direction_ == 3 && up != down) delta = up < down ? up : -down;
438
443
  to = Wheel{base + delta, 1.0, false, false};
439
444
  }
440
445
  next.finals.push_back(Wheel{digit, 1.0, false, false});
@@ -897,6 +902,7 @@ void RollingEngine::apply(double elapsed) {
897
902
  w.blend = 1;
898
903
  w.focus = 1;
899
904
  w.grow = 1;
905
+ w.progress = 1;
900
906
  } else if (tr.style == 2) {
901
907
  // Scramble: a different digit every step until the wheel locks.
902
908
  const int from = static_cast<int>(wt.from.fromGlyph);
@@ -922,6 +928,7 @@ void RollingEngine::apply(double elapsed) {
922
928
  w.blend = 1;
923
929
  w.focus = 1;
924
930
  w.grow = 1;
931
+ w.progress = clamp01(t);
925
932
  } else if (tr.numeric) {
926
933
  // Numeric: the glyphs swap in place; the position is the digit
927
934
  // arriving and the three clocks say how far the swap is (see the header).
@@ -943,6 +950,8 @@ void RollingEngine::apply(double elapsed) {
943
950
  w.focus = damped(local, kNumericFocusZeta, kNumericFocusSettle * d);
944
951
  w.blurOut = damped(local, 1.0, kNumericBlurOutSettle * d);
945
952
  }
953
+ // The grow clock is critically damped, so a column never rings.
954
+ w.progress = clamp01(w.grow);
946
955
  } else {
947
956
  w.position = wt.from.position + (wt.to.position - wt.from.position) * t;
948
957
  w.fromGlyph = -1;
@@ -950,6 +959,7 @@ void RollingEngine::apply(double elapsed) {
950
959
  w.blend = 1;
951
960
  w.focus = 1;
952
961
  w.grow = 1;
962
+ w.progress = clamp01(t);
953
963
  }
954
964
  }
955
965
  const double signRaw = tr.duration > 0 ? clamp01(elapsed / tr.duration) : 1.0;
@@ -64,6 +64,12 @@ public:
64
64
  /// or the down colour by this much. `flashUp`: the value grew.
65
65
  double flash = 0;
66
66
  bool flashUp = true;
67
+ /// How far this wheel is through its change, 0 → 1: the roll's easing, or
68
+ /// a swap's `grow` clock. 1 while settled or not part of the change. A
69
+ /// renderer that sizes a column by its digit (proportional figures) eases
70
+ /// the width from where it was to the target digit's on this, rather than
71
+ /// following the digits the wheel passes (which made the number pulse).
72
+ double progress = 1;
67
73
  };
68
74
 
69
75
  // The numeric transition as the renderers draw it, in line heights, so all
@@ -111,7 +117,8 @@ public:
111
117
  static constexpr double kScrambleStepSeconds = 0.045;
112
118
 
113
119
  // Easing: 0 linear, 1 easeIn, 2 easeOut, 3 easeInOut, 4 spring.
114
- // Direction: 0 auto (sign of the change), 1 up, 2 down.
120
+ // Direction: 0 auto (sign of the change), 1 up, 2 down, 3 shortest (each
121
+ // interior wheel its own shorter way round; a tie goes with the change).
115
122
  // Transition: 0 roll (the odometer), 1 numeric (glyphs swap in place),
116
123
  // 2 scramble.
117
124