react-native-nitro-input 0.3.0 → 0.3.2

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 (150) hide show
  1. package/CHANGELOG.md +80 -0
  2. package/README.md +120 -5
  3. package/android/src/main/cpp/JMaskEngine.cpp +45 -0
  4. package/android/src/main/cpp/JMaskEngine.hpp +9 -0
  5. package/android/src/main/cpp/JReflowEngine.cpp +32 -0
  6. package/android/src/main/cpp/JReflowEngine.hpp +13 -0
  7. package/android/src/main/cpp/JRollingEngine.cpp +42 -2
  8. package/android/src/main/cpp/JRollingEngine.hpp +9 -1
  9. package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroInputView.kt +24 -0
  10. package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroNumberFormatPlatform.kt +76 -1
  11. package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroNumberView.kt +88 -0
  12. package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroTextMeasure.kt +25 -0
  13. package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroTextView.kt +140 -0
  14. package/android/src/main/java/com/margelo/nitro/nitroinput/MaskEngine.kt +16 -0
  15. package/android/src/main/java/com/margelo/nitro/nitroinput/NitroInputPackage.kt +2 -1
  16. package/android/src/main/java/com/margelo/nitro/nitroinput/NitroInputView.kt +96 -6
  17. package/android/src/main/java/com/margelo/nitro/nitroinput/NitroNumberView.kt +388 -136
  18. package/android/src/main/java/com/margelo/nitro/nitroinput/NitroTextView.kt +868 -0
  19. package/android/src/main/java/com/margelo/nitro/nitroinput/ReflowEngine.kt +21 -0
  20. package/android/src/main/java/com/margelo/nitro/nitroinput/RollingEngine.kt +13 -2
  21. package/cpp/MaskEngine.cpp +294 -33
  22. package/cpp/MaskEngine.hpp +68 -0
  23. package/cpp/RollingEngine.cpp +92 -8
  24. package/cpp/RollingEngine.hpp +36 -2
  25. package/ios/HybridNitroInputView.swift +18 -0
  26. package/ios/HybridNitroNumberFormatPlatform.swift +87 -9
  27. package/ios/HybridNitroNumberView.swift +82 -1
  28. package/ios/HybridNitroTextView.swift +134 -0
  29. package/ios/NitroInputView.swift +125 -7
  30. package/ios/NitroNumberView.swift +368 -135
  31. package/ios/NitroTextView.swift +583 -0
  32. package/lib/commonjs/nitrogen/generated/shared/json/NitroInputViewConfig.json +6 -0
  33. package/lib/commonjs/nitrogen/generated/shared/json/NitroNumberViewConfig.json +20 -0
  34. package/lib/commonjs/nitrogen/generated/shared/json/NitroTextViewConfig.json +30 -0
  35. package/lib/commonjs/src/NitroInput.js +15 -2
  36. package/lib/commonjs/src/NitroNumber.js +52 -3
  37. package/lib/commonjs/src/NitroText.js +138 -0
  38. package/lib/commonjs/src/NitroTime.js +65 -0
  39. package/lib/commonjs/src/formatProps.js +76 -16
  40. package/lib/commonjs/src/index.js +6 -1
  41. package/lib/commonjs/src/specs/NitroText.nitro.js +2 -0
  42. package/lib/module/nitrogen/generated/shared/json/NitroInputViewConfig.json +6 -0
  43. package/lib/module/nitrogen/generated/shared/json/NitroNumberViewConfig.json +20 -0
  44. package/lib/module/nitrogen/generated/shared/json/NitroTextViewConfig.json +30 -0
  45. package/lib/module/src/NitroInput.js +15 -2
  46. package/lib/module/src/NitroNumber.js +53 -4
  47. package/lib/module/src/NitroText.js +98 -0
  48. package/lib/module/src/NitroTime.js +29 -0
  49. package/lib/module/src/formatProps.js +76 -17
  50. package/lib/module/src/index.js +2 -0
  51. package/lib/module/src/specs/NitroText.nitro.js +1 -0
  52. package/lib/typescript/src/NitroInput.d.ts +39 -1
  53. package/lib/typescript/src/NitroNumber.d.ts +79 -1
  54. package/lib/typescript/src/NitroText.d.ts +67 -0
  55. package/lib/typescript/src/NitroTime.d.ts +19 -0
  56. package/lib/typescript/src/formatProps.d.ts +30 -4
  57. package/lib/typescript/src/index.d.ts +5 -2
  58. package/lib/typescript/src/specs/NitroInput.nitro.d.ts +43 -0
  59. package/lib/typescript/src/specs/NitroNumber.nitro.d.ts +67 -0
  60. package/lib/typescript/src/specs/NitroText.nitro.d.ts +60 -0
  61. package/nitro.json +20 -0
  62. package/nitrogen/generated/android/NitroInput+autolinking.cmake +6 -0
  63. package/nitrogen/generated/android/NitroInputOnLoad.cpp +34 -0
  64. package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.cpp +102 -0
  65. package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.hpp +12 -0
  66. package/nitrogen/generated/android/c++/JHybridNitroNumberViewSpec.cpp +243 -0
  67. package/nitrogen/generated/android/c++/JHybridNitroNumberViewSpec.hpp +40 -0
  68. package/nitrogen/generated/android/c++/JHybridNitroTextMeasureSpec.cpp +58 -0
  69. package/nitrogen/generated/android/c++/JHybridNitroTextMeasureSpec.hpp +64 -0
  70. package/nitrogen/generated/android/c++/JHybridNitroTextViewSpec.cpp +253 -0
  71. package/nitrogen/generated/android/c++/JHybridNitroTextViewSpec.hpp +104 -0
  72. package/nitrogen/generated/android/c++/JNitroInputCharacterMapping.hpp +61 -0
  73. package/nitrogen/generated/android/c++/JNitroInputMaskAffinity.hpp +64 -0
  74. package/nitrogen/generated/android/c++/JNitroInputTextCase.hpp +61 -0
  75. package/nitrogen/generated/android/c++/JNitroNumberShimmerDirection.hpp +61 -0
  76. package/nitrogen/generated/android/c++/JNitroNumberSignDisplay.hpp +67 -0
  77. package/nitrogen/generated/android/c++/views/JHybridNitroInputViewStateUpdater.cpp +30 -0
  78. package/nitrogen/generated/android/c++/views/JHybridNitroNumberViewStateUpdater.cpp +100 -0
  79. package/nitrogen/generated/android/c++/views/JHybridNitroTextViewStateUpdater.cpp +174 -0
  80. package/nitrogen/generated/android/c++/views/JHybridNitroTextViewStateUpdater.hpp +54 -0
  81. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroInputViewSpec.kt +36 -0
  82. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroNumberViewSpec.kt +136 -0
  83. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroTextMeasureSpec.kt +60 -0
  84. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroTextViewSpec.kt +179 -0
  85. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputCharacterMapping.kt +56 -0
  86. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputMaskAffinity.kt +25 -0
  87. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputTextCase.kt +24 -0
  88. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroNumberShimmerDirection.kt +24 -0
  89. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroNumberSignDisplay.kt +26 -0
  90. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/views/HybridNitroTextViewManager.kt +96 -0
  91. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/views/HybridNitroTextViewStateUpdater.kt +23 -0
  92. package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Bridge.cpp +34 -0
  93. package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Bridge.hpp +146 -11
  94. package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Umbrella.hpp +25 -0
  95. package/nitrogen/generated/ios/NitroInputAutolinking.mm +16 -0
  96. package/nitrogen/generated/ios/NitroInputAutolinking.swift +24 -0
  97. package/nitrogen/generated/ios/c++/HybridNitroInputViewSpecSwift.hpp +50 -0
  98. package/nitrogen/generated/ios/c++/HybridNitroNumberViewSpecSwift.hpp +146 -0
  99. package/nitrogen/generated/ios/c++/HybridNitroTextMeasureSpecSwift.cpp +11 -0
  100. package/nitrogen/generated/ios/c++/HybridNitroTextMeasureSpecSwift.hpp +90 -0
  101. package/nitrogen/generated/ios/c++/HybridNitroTextViewSpecSwift.cpp +11 -0
  102. package/nitrogen/generated/ios/c++/HybridNitroTextViewSpecSwift.hpp +233 -0
  103. package/nitrogen/generated/ios/c++/views/HybridNitroInputViewComponent.mm +36 -0
  104. package/nitrogen/generated/ios/c++/views/HybridNitroNumberViewComponent.mm +120 -0
  105. package/nitrogen/generated/ios/c++/views/HybridNitroTextViewComponent.mm +264 -0
  106. package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec.swift +6 -0
  107. package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec_cxx.swift +78 -0
  108. package/nitrogen/generated/ios/swift/HybridNitroNumberViewSpec.swift +20 -0
  109. package/nitrogen/generated/ios/swift/HybridNitroNumberViewSpec_cxx.swift +493 -0
  110. package/nitrogen/generated/ios/swift/HybridNitroTextMeasureSpec.swift +56 -0
  111. package/nitrogen/generated/ios/swift/HybridNitroTextMeasureSpec_cxx.swift +150 -0
  112. package/nitrogen/generated/ios/swift/HybridNitroTextViewSpec.swift +75 -0
  113. package/nitrogen/generated/ios/swift/HybridNitroTextViewSpec_cxx.swift +608 -0
  114. package/nitrogen/generated/ios/swift/NitroInputCharacterMapping.swift +34 -0
  115. package/nitrogen/generated/ios/swift/NitroInputMaskAffinity.swift +48 -0
  116. package/nitrogen/generated/ios/swift/NitroInputTextCase.swift +44 -0
  117. package/nitrogen/generated/ios/swift/NitroNumberShimmerDirection.swift +44 -0
  118. package/nitrogen/generated/ios/swift/NitroNumberSignDisplay.swift +52 -0
  119. package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.cpp +12 -0
  120. package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.hpp +21 -0
  121. package/nitrogen/generated/shared/c++/HybridNitroNumberViewSpec.cpp +40 -0
  122. package/nitrogen/generated/shared/c++/HybridNitroNumberViewSpec.hpp +46 -0
  123. package/nitrogen/generated/shared/c++/HybridNitroTextMeasureSpec.cpp +22 -0
  124. package/nitrogen/generated/shared/c++/HybridNitroTextMeasureSpec.hpp +63 -0
  125. package/nitrogen/generated/shared/c++/HybridNitroTextViewSpec.cpp +62 -0
  126. package/nitrogen/generated/shared/c++/HybridNitroTextViewSpec.hpp +115 -0
  127. package/nitrogen/generated/shared/c++/NitroInputCharacterMapping.hpp +87 -0
  128. package/nitrogen/generated/shared/c++/NitroInputMaskAffinity.hpp +84 -0
  129. package/nitrogen/generated/shared/c++/NitroInputTextCase.hpp +80 -0
  130. package/nitrogen/generated/shared/c++/NitroNumberShimmerDirection.hpp +80 -0
  131. package/nitrogen/generated/shared/c++/NitroNumberSignDisplay.hpp +88 -0
  132. package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.cpp +12 -0
  133. package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.hpp +21 -0
  134. package/nitrogen/generated/shared/c++/views/HybridNitroNumberViewComponent.cpp +40 -0
  135. package/nitrogen/generated/shared/c++/views/HybridNitroNumberViewComponent.hpp +62 -0
  136. package/nitrogen/generated/shared/c++/views/HybridNitroTextViewComponent.cpp +74 -0
  137. package/nitrogen/generated/shared/c++/views/HybridNitroTextViewComponent.hpp +149 -0
  138. package/nitrogen/generated/shared/json/NitroInputViewConfig.json +6 -0
  139. package/nitrogen/generated/shared/json/NitroNumberViewConfig.json +20 -0
  140. package/nitrogen/generated/shared/json/NitroTextViewConfig.json +30 -0
  141. package/package.json +2 -2
  142. package/src/NitroInput.tsx +73 -0
  143. package/src/NitroNumber.tsx +183 -9
  144. package/src/NitroText.tsx +166 -0
  145. package/src/NitroTime.tsx +68 -0
  146. package/src/formatProps.ts +108 -18
  147. package/src/index.ts +8 -0
  148. package/src/specs/NitroInput.nitro.ts +46 -0
  149. package/src/specs/NitroNumber.nitro.ts +69 -0
  150. package/src/specs/NitroText.nitro.ts +65 -0
@@ -35,6 +35,15 @@ final class NitroNumberView: UIView {
35
35
  var decimalSeparator: String = "."
36
36
  var prefix: String = ""
37
37
  var suffix: String = ""
38
+ /// ECMA-402's signDisplay (`Engine.setSignDisplay`), and the glyphs drawn for the two signs.
39
+ var signDisplay: Int32 = 0
40
+ var plusSign: String = "+"
41
+ var minusSign: String = "-"
42
+ /// Digit group sizes counted from the decimal point, the first then every
43
+ /// later one (empty: threes). [3, 2] is Indian grouping, [2] a clock's.
44
+ var groupingSizes: [Int] = []
45
+ /// Per integer position, the highest digit its wheel shows before it wraps (a clock's 5).
46
+ var digitMax: [Int] = []
38
47
  }
39
48
 
40
49
  enum AffixAlign {
@@ -60,6 +69,15 @@ final class NitroNumberView: UIView {
60
69
  var suffixOffset: CGFloat = 0
61
70
  /// Every digit as wide as the widest (tabular figures, the default), or each at its own width.
62
71
  var tabularNums: Bool = true
72
+ /// The prefix's, the suffix's and the fraction's colours (nil: `color`).
73
+ var prefixColor: UIColor? = nil
74
+ var suffixColor: UIColor? = nil
75
+ var fractionColor: UIColor? = nil
76
+ /// The fraction digits' and decimal separator's size (nil: `fontSize`) and how they line up.
77
+ var fractionFontSize: CGFloat? = nil
78
+ var fractionAlign: AffixAlign = .baseline
79
+ /// The glyphs drawn for 0…9 (empty: "0"…"9").
80
+ var digitGlyphs: [String] = []
63
81
  var adjustsFontSizeToFit: Bool = false
64
82
  var minimumFontScale: CGFloat = 0.5
65
83
  var allowFontScaling: Bool = false
@@ -97,6 +115,10 @@ final class NitroNumberView: UIView {
97
115
  var revealStagger: TimeInterval = 0.2
98
116
  /// Count style: how long the count pauses on each milestone.
99
117
  var revealMilestoneHold: TimeInterval = 0
118
+ /// Rolls turn the lower wheels a full turn too (`Engine.setContinuous`).
119
+ var continuous = false
120
+ /// Snap while the system's Reduce Motion is on.
121
+ var respectReduceMotion = true
100
122
  }
101
123
 
102
124
  enum RevealStyle: Int32 {
@@ -119,6 +141,16 @@ final class NitroNumberView: UIView {
119
141
  var color: UIColor? = nil
120
142
  /// Duration of one sweep across the number.
121
143
  var duration: TimeInterval = 0.95
144
+ /// The band's slant in degrees (0 upright, positive leans like "/").
145
+ var angle: CGFloat = 31
146
+ /// The band's width, a fraction of the number's.
147
+ var width: CGFloat = 1
148
+ /// The glyphs' colour outside the band while loading (nil: the text colour).
149
+ var baseColor: UIColor? = nil
150
+ /// Sweep direction: nil follows the layout direction, true left to right.
151
+ var leftToRight: Bool? = nil
152
+ /// A pause after each sweep.
153
+ var delay: TimeInterval = 0
122
154
  }
123
155
 
124
156
  enum Alignment {
@@ -148,6 +180,15 @@ final class NitroNumberView: UIView {
148
180
  change(Self.suffixText, suffixInk(oldValue.suffix), suffixInk(format.suffix))
149
181
  change(Self.groupingText, oldValue.groupingSeparator, format.groupingSeparator)
150
182
  change(Self.decimalText, oldValue.decimalSeparator, format.decimalSeparator)
183
+ let positive = engine.signPositive()
184
+ change(Self.signText, positive ? oldValue.plusSign : oldValue.minusSign, positive ? format.plusSign : format.minusSign)
185
+ }
186
+ engine.setSignDisplay(format.signDisplay)
187
+ if format.digitMax != oldValue.digitMax {
188
+ engine.clearDigitMax()
189
+ for (power, max) in format.digitMax.enumerated() where (0...8).contains(max) {
190
+ engine.setDigitMax(Int32(power), Int32(max))
191
+ }
151
192
  }
152
193
  // Played in a glyph-swap transition; snaps otherwise (see the engine).
153
194
  engine.changeFormat(Int32(format.fractionDigits), Int32(format.minimumIntegerDigits), CACurrentMediaTime())
@@ -164,7 +205,8 @@ final class NitroNumberView: UIView {
164
205
  static let suffixText = 1
165
206
  static let groupingText = 2
166
207
  static let decimalText = 3
167
- private var leavingText: [String?] = [nil, nil, nil, nil]
208
+ static let signText = 4
209
+ private var leavingText: [String?] = [nil, nil, nil, nil, nil]
168
210
 
169
211
  var typography = Typography() {
170
212
  didSet {
@@ -182,6 +224,7 @@ final class NitroNumberView: UIView {
182
224
  engine.setRevealTiming(timing.revealDuration, timing.revealBounce, timing.revealStyle.rawValue, timing.revealStagger)
183
225
  engine.setRevealGrow(timing.revealGrow)
184
226
  engine.setRevealMilestoneHold(timing.revealMilestoneHold)
227
+ engine.setContinuous(timing.continuous)
185
228
  }
186
229
  }
187
230
 
@@ -216,7 +259,7 @@ final class NitroNumberView: UIView {
216
259
  var loading = false {
217
260
  didSet {
218
261
  guard loading != oldValue else { return }
219
- engine.setReduceMotion(UIAccessibility.isReduceMotionEnabled)
262
+ applyReduceMotion()
220
263
  engine.setLoading(loading, CACurrentMediaTime())
221
264
  updateDisplayLinkNeed()
222
265
  render()
@@ -233,6 +276,11 @@ final class NitroNumberView: UIView {
233
276
  var onRevealEnd: (() -> Void)?
234
277
  /// Called when a count-style reveal reaches a milestone (its index and value).
235
278
  var onRevealMilestone: ((Int, Double) -> Void)?
279
+ /// Called when the figure starts moving from rest, and when it comes to rest (with its value).
280
+ var onAnimationStart: (() -> Void)?
281
+ var onAnimationEnd: ((Double) -> Void)?
282
+ /// A roll or a reveal is under way (`onAnimationStart` has fired, `onAnimationEnd` not yet).
283
+ private var moving = false
236
284
 
237
285
  /// The value currently shown or being rolled towards.
238
286
  var targetValue: Double { engine.targetValue() }
@@ -248,13 +296,15 @@ final class NitroNumberView: UIView {
248
296
  }
249
297
  /// The core starts at the glyphs' left edge instead of parked off-screen.
250
298
  private static let shimmerSeed: CGFloat = 0.25
251
- /// How far the top of the band leads the bottom, as a fraction of the height ("/" slant).
252
- private static let shimmerSlant: CGFloat = 0.6
253
299
 
254
300
  // MARK: - Fonts
255
301
 
256
302
  private enum GlyphRole {
257
- case digit, prefix, suffix
303
+ /// The integer digits, the sign and the grouping separators.
304
+ case digit
305
+ case prefix, suffix
306
+ /// The fraction digits and the decimal separator (`fractionFontSize`, `fractionColor`).
307
+ case fraction
258
308
  }
259
309
 
260
310
  private struct GlyphKey: Hashable {
@@ -323,15 +373,25 @@ final class NitroNumberView: UIView {
323
373
  let digit: UIFont
324
374
  let prefix: UIFont
325
375
  let suffix: UIFont
376
+ let fraction: UIFont
326
377
  /// The text color resolved for the view's traits: the glyph images bake it
327
378
  /// in, and `.label` is dynamic (see `traitCollectionDidChange`).
328
379
  let color: UIColor
380
+ /// The prefix's, the suffix's and the fraction's colours, resolved (`color` when unset).
381
+ let prefixColor: UIColor
382
+ let suffixColor: UIColor
383
+ let fractionColor: UIColor
329
384
  let prefixAlign: AffixAlign
330
385
  let suffixAlign: AffixAlign
386
+ let fractionAlign: AffixAlign
387
+ /// The glyphs drawn for 0…9.
388
+ let glyphs: [String]
389
+ private let glyphsKey: String
331
390
  /// Letter spacing after a digit / prefix / suffix glyph, and the spacing at the affix seams (nil: the letter spacing), scaled with the fonts.
332
391
  let digitSpacing: CGFloat
333
392
  let prefixLetterSpacing: CGFloat
334
393
  let suffixLetterSpacing: CGFloat
394
+ let fractionLetterSpacing: CGFloat
335
395
  let prefixSeam: CGFloat?
336
396
  let suffixSeam: CGFloat?
337
397
  let prefixOffset: CGFloat
@@ -346,6 +406,9 @@ final class NitroNumberView: UIView {
346
406
  let proportional: Bool
347
407
  /// The advance of each digit 0…9.
348
408
  private(set) var digitWidths: [CGFloat] = []
409
+ /// The same for the fraction digits, in their own font.
410
+ private(set) var fractionDigitWidth: CGFloat = 0
411
+ private(set) var fractionDigitWidths: [CGFloat] = []
349
412
  // Keyed by a value type rather than a concatenated string: a lookup on the
350
413
  // frame path must not allocate.
351
414
  private var glyphCache: [GlyphKey: NSAttributedString] = [:]
@@ -370,6 +433,11 @@ final class NitroNumberView: UIView {
370
433
  let color: UInt32
371
434
  let scale: CGFloat
372
435
  let blankZero: Bool
436
+ /// Places before the wheel wraps: 10, or fewer on a clock's wheel.
437
+ let modulus: Int
438
+ /// The glyphs' offset in their slot (a smaller fraction aligned to the digits).
439
+ let top: CGFloat
440
+ let glyphs: String
373
441
  }
374
442
  private static var sharedImages: [ImageKey: UIImage] = [:]
375
443
  /// Blurred glyphs, the digit's own colour (tint 0) and the flash's alike.
@@ -387,14 +455,31 @@ final class NitroNumberView: UIView {
387
455
  private static var sharedStrips: [StripKey: UIImage] = [:]
388
456
  private static let sharedCapacity = 512
389
457
  private let colorKey: UInt32
458
+ private let prefixColorKey: UInt32
459
+ private let suffixColorKey: UInt32
460
+ private let fractionColorKey: UInt32
461
+ private func colorKey(for role: GlyphRole) -> UInt32 {
462
+ switch role {
463
+ case .digit: return colorKey
464
+ case .prefix: return prefixColorKey
465
+ case .suffix: return suffixColorKey
466
+ case .fraction: return fractionColorKey
467
+ }
468
+ }
469
+ func color(for role: GlyphRole) -> UIColor {
470
+ switch role {
471
+ case .digit: return color
472
+ case .prefix: return prefixColor
473
+ case .suffix: return suffixColor
474
+ case .fraction: return fractionColor
475
+ }
476
+ }
390
477
  private func fontTag(_ role: GlyphRole) -> String {
391
478
  let font = self.font(for: role)
392
479
  return "\(font.fontName)|\(font.pointSize)\(proportional ? "|p" : "")"
393
480
  }
394
481
  /// Pixel density the glyph images are rendered at.
395
482
  let renderScale: CGFloat
396
- /// Slots in a wheel strip: index -1 (blank) through 10 (the 0 after 9).
397
- static let stripSlots = 12
398
483
 
399
484
  init(_ t: Typography, traits: UITraitCollection) {
400
485
  // The view's own display, not the main screen's (deprecated, and absent on visionOS).
@@ -404,22 +489,48 @@ final class NitroNumberView: UIView {
404
489
  digit = NitroNumberView.makeFont(size: t.fontSize * scale, weight: t.fontWeight, family: t.fontFamily, tabular: t.tabularNums)
405
490
  prefix = NitroNumberView.makeFont(size: (t.prefixFontSize ?? t.fontSize) * scale, weight: t.fontWeight, family: t.fontFamily, tabular: t.tabularNums)
406
491
  suffix = NitroNumberView.makeFont(size: (t.suffixFontSize ?? t.fontSize) * scale, weight: t.fontWeight, family: t.fontFamily, tabular: t.tabularNums)
492
+ fraction = NitroNumberView.makeFont(size: (t.fractionFontSize ?? t.fontSize) * scale, weight: t.fontWeight, family: t.fontFamily, tabular: t.tabularNums)
407
493
  color = t.color.resolvedColor(with: traits)
408
- var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0
409
- color.getRed(&r, green: &g, blue: &b, alpha: &a)
410
- colorKey = (UInt32(max(0, min(1, a)) * 255) << 24) | (UInt32(max(0, min(1, r)) * 255) << 16) | (UInt32(max(0, min(1, g)) * 255) << 8) | UInt32(max(0, min(1, b)) * 255)
494
+ prefixColor = (t.prefixColor ?? t.color).resolvedColor(with: traits)
495
+ suffixColor = (t.suffixColor ?? t.color).resolvedColor(with: traits)
496
+ fractionColor = (t.fractionColor ?? t.color).resolvedColor(with: traits)
497
+ colorKey = Self.key(of: color)
498
+ prefixColorKey = Self.key(of: prefixColor)
499
+ suffixColorKey = Self.key(of: suffixColor)
500
+ fractionColorKey = Self.key(of: fractionColor)
411
501
  prefixAlign = t.prefixAlign
412
502
  suffixAlign = t.suffixAlign
503
+ fractionAlign = t.fractionAlign
504
+ glyphs = t.digitGlyphs.count == 10 && !t.digitGlyphs.contains(where: \.isEmpty) ? t.digitGlyphs : (0...9).map { String($0) }
505
+ glyphsKey = glyphs.joined(separator: "|")
413
506
  digitSpacing = t.letterSpacing * scale
414
507
  prefixLetterSpacing = t.letterSpacing * scale * (prefix.pointSize / digit.pointSize)
415
508
  suffixLetterSpacing = t.letterSpacing * scale * (suffix.pointSize / digit.pointSize)
509
+ fractionLetterSpacing = t.letterSpacing * scale * (fraction.pointSize / digit.pointSize)
416
510
  prefixSeam = t.prefixSpacing.map { $0 * scale }
417
511
  suffixSeam = t.suffixSpacing.map { $0 * scale }
418
512
  prefixOffset = t.prefixOffset * scale
419
513
  suffixOffset = t.suffixOffset * scale
420
514
  lineHeight = ceil(digit.lineHeight)
421
- digitWidths = (0...9).map { width(of: String($0), role: .digit) }
515
+ digitWidths = (0...9).map { width(of: glyphs[$0], role: .digit) }
422
516
  digitWidth = digitWidths.max() ?? 0
517
+ fractionDigitWidths = (0...9).map { width(of: glyphs[$0], role: .fraction) }
518
+ fractionDigitWidth = fractionDigitWidths.max() ?? 0
519
+ }
520
+
521
+ /// The widest digit of `role`'s font (the digits' or the fraction's).
522
+ func digitWidth(for role: GlyphRole) -> CGFloat {
523
+ role == .fraction ? fractionDigitWidth : digitWidth
524
+ }
525
+
526
+ func digitWidths(for role: GlyphRole) -> [CGFloat] {
527
+ role == .fraction ? fractionDigitWidths : digitWidths
528
+ }
529
+
530
+ /// The vertical centre of a `role` digit in the line box (a swapping glyph's).
531
+ func digitCenterY(for role: GlyphRole) -> CGFloat {
532
+ guard role == .fraction else { return lineHeight / 2 }
533
+ return top(for: .fraction, text: glyphs[0], lineTop: 0) + fraction.lineHeight / 2
423
534
  }
424
535
 
425
536
  func font(for role: GlyphRole) -> UIFont {
@@ -427,6 +538,7 @@ final class NitroNumberView: UIView {
427
538
  case .digit: return digit
428
539
  case .prefix: return prefix
429
540
  case .suffix: return suffix
541
+ case .fraction: return fraction
430
542
  }
431
543
  }
432
544
 
@@ -435,7 +547,7 @@ final class NitroNumberView: UIView {
435
547
  if let cached = glyphCache[key] { return cached }
436
548
  let string = NSAttributedString(string: text, attributes: [
437
549
  .font: font(for: role),
438
- .foregroundColor: color,
550
+ .foregroundColor: color(for: role),
439
551
  ])
440
552
  glyphCache[key] = string
441
553
  return string
@@ -454,7 +566,7 @@ final class NitroNumberView: UIView {
454
566
  /// rasterization pass per glyph. Profiling 24 rolling views showed the
455
567
  /// per-glyph `NSAttributedString.draw` dominating the main thread.
456
568
  func image(_ text: String, role: GlyphRole) -> UIImage? {
457
- let key = ImageKey(font: fontTag(role), color: colorKey, scale: renderScale, text: text, role: role)
569
+ let key = ImageKey(font: fontTag(role), color: colorKey(for: role), scale: renderScale, text: text, role: role)
458
570
  if let cached = Self.sharedImages[key] { return cached }
459
571
  let string = attributed(text, role: role)
460
572
  let size = string.size()
@@ -482,7 +594,7 @@ final class NitroNumberView: UIView {
482
594
  /// blur cross-faded, so it never shows a sharp core.
483
595
  func blurredImage(_ text: String, role: GlyphRole = .digit, level: Int) -> UIImage? {
484
596
  if level <= 0 || numericBlur <= 0 { return image(text, role: role) }
485
- let key = TintKey(image: ImageKey(font: fontTag(role), color: colorKey, scale: renderScale, text: text, role: role), tint: 0, level: level, blur: blurKey)
597
+ let key = TintKey(image: ImageKey(font: fontTag(role), color: colorKey(for: role), scale: renderScale, text: text, role: role), tint: 0, level: level, blur: blurKey)
486
598
  if let cached = Self.sharedBlurred[key] { return cached }
487
599
  guard let sharp = image(text, role: role), let image = blurred(sharp, level: level) else { return nil }
488
600
  Self.sharedBlurred.insert(image, for: key)
@@ -491,19 +603,19 @@ final class NitroNumberView: UIView {
491
603
 
492
604
  /// `blurredImage`'s twin for `tintedImage`, so a swapping glyph's change
493
605
  /// flash blurs as the glyph does.
494
- func tintedBlurredImage(_ text: String, tint: UIColor, level: Int) -> UIImage? {
495
- if level <= 0 || numericBlur <= 0 { return tintedImage(text, tint: tint) }
496
- let key = TintKey(image: ImageKey(font: fontTag(.digit), color: colorKey, scale: renderScale, text: text, role: .digit), tint: Self.key(of: tint), level: level, blur: blurKey)
606
+ func tintedBlurredImage(_ text: String, role: GlyphRole = .digit, tint: UIColor, level: Int) -> UIImage? {
607
+ if level <= 0 || numericBlur <= 0 { return tintedImage(text, role: role, tint: tint) }
608
+ let key = TintKey(image: ImageKey(font: fontTag(role), color: colorKey(for: role), scale: renderScale, text: text, role: role), tint: Self.key(of: tint), level: level, blur: blurKey)
497
609
  if let cached = Self.sharedBlurred[key] { return cached }
498
- guard let sharp = tintedImage(text, tint: tint), let image = blurred(sharp, level: level) else { return nil }
610
+ guard let sharp = tintedImage(text, role: role, tint: tint), let image = blurred(sharp, level: level) else { return nil }
499
611
  Self.sharedBlurred.insert(image, for: key)
500
612
  return image
501
613
  }
502
614
 
503
615
  /// A swapping glyph's image at a blur level, in its own colour or the flash's.
504
- func swapImage(_ text: String, level: Int, tint: UIColor?) -> UIImage? {
505
- if let tint { return tintedBlurredImage(text, tint: tint, level: level) }
506
- return blurredImage(text, level: level)
616
+ func swapImage(_ text: String, role: GlyphRole = .digit, level: Int, tint: UIColor?) -> UIImage? {
617
+ if let tint { return tintedBlurredImage(text, role: role, tint: tint, level: level) }
618
+ return blurredImage(text, role: role, level: level)
507
619
  }
508
620
 
509
621
  private var blurKey: Int { Int((numericBlur * 1000).rounded()) }
@@ -566,10 +678,10 @@ final class NitroNumberView: UIView {
566
678
  /// The glyph in another colour, for the change flash: the same raster as
567
679
  /// `image` with the tint baked in, composited over the glyph at the flash's
568
680
  /// opacity. Made once per font, density and tint.
569
- func tintedImage(_ text: String, tint: UIColor) -> UIImage? {
570
- let key = TintKey(image: ImageKey(font: fontTag(.digit), color: colorKey, scale: renderScale, text: text, role: .digit), tint: Self.key(of: tint))
681
+ func tintedImage(_ text: String, role: GlyphRole = .digit, tint: UIColor) -> UIImage? {
682
+ let key = TintKey(image: ImageKey(font: fontTag(role), color: colorKey(for: role), scale: renderScale, text: text, role: role), tint: Self.key(of: tint))
571
683
  if let cached = Self.sharedTinted[key] { return cached }
572
- let string = NSAttributedString(string: text, attributes: [.font: digit, .foregroundColor: tint])
684
+ let string = NSAttributedString(string: text, attributes: [.font: font(for: role), .foregroundColor: tint])
573
685
  let size = string.size()
574
686
  guard size.width > 0, size.height > 0 else { return nil }
575
687
  let format = UIGraphicsImageRendererFormat()
@@ -591,53 +703,60 @@ final class NitroNumberView: UIView {
591
703
  let tint: UInt32
592
704
  }
593
705
  private static var sharedTintedStrips: [TintedStripKey: UIImage] = [:]
594
- func tintedStrip(blankZero: Bool, tint: UIColor) -> UIImage? {
595
- let key = TintedStripKey(strip: StripKey(font: fontTag(.digit), color: colorKey, scale: renderScale, blankZero: blankZero), tint: Self.key(of: tint))
706
+ func tintedStrip(role: GlyphRole = .digit, blankZero: Bool, modulus: Int = 10, tint: UIColor) -> UIImage? {
707
+ let key = TintedStripKey(strip: stripKey(role: role, blankZero: blankZero, modulus: modulus), tint: Self.key(of: tint))
596
708
  if let cached = Self.sharedTintedStrips[key] { return cached }
597
- guard digitWidth > 0, lineHeight > 0 else { return nil }
598
- let format = UIGraphicsImageRendererFormat()
599
- format.scale = renderScale
600
- format.opaque = false
601
- let size = CGSize(width: ceil(digitWidth), height: lineHeight * CGFloat(Self.stripSlots))
602
- let image = UIGraphicsImageRenderer(size: size, format: format).image { _ in
603
- for slot in 0..<Self.stripSlots {
604
- let index = slot - 1
605
- if index < 0 || (blankZero && index == 0) { continue }
606
- let text = String(index % 10)
607
- let w = width(of: text, role: .digit)
608
- NSAttributedString(string: text, attributes: [.font: digit, .foregroundColor: tint])
609
- .draw(at: CGPoint(x: (digitWidth - w) / 2, y: CGFloat(slot) * lineHeight))
610
- }
611
- }
709
+ guard let image = drawStrip(role: role, blankZero: blankZero, modulus: modulus, color: tint) else { return nil }
612
710
  if Self.sharedTintedStrips.count >= 8 { Self.sharedTintedStrips.removeAll(keepingCapacity: true) }
613
711
  Self.sharedTintedStrips[key] = image
614
712
  return image
615
713
  }
616
714
 
617
715
  /// A wheel's whole digit strip as one image: slots for index -1 (blank) to
618
- /// 10 (the 0 that follows 9 on a wrap), each `lineHeight` tall with the
619
- /// digit centred in `digitWidth`. A wheel layer shows one slot-high window
620
- /// of it and just moves the strip, so a roll is a position change.
621
- func strip(blankZero: Bool) -> UIImage? {
622
- let key = StripKey(font: fontTag(.digit), color: colorKey, scale: renderScale, blankZero: blankZero)
716
+ /// `modulus` (the 0 that follows the last digit on a wrap: 10 on a plain
717
+ /// wheel, 6 on a clock's tens), each `lineHeight` tall with the digit
718
+ /// centred in the role's widest digit. A wheel layer shows one slot-high
719
+ /// window of it and just moves the strip, so a roll is a position change.
720
+ /// A fraction digit set smaller is drawn at its aligned height in the slot.
721
+ func strip(role: GlyphRole = .digit, blankZero: Bool, modulus: Int = 10) -> UIImage? {
722
+ let key = stripKey(role: role, blankZero: blankZero, modulus: modulus)
623
723
  if let cached = Self.sharedStrips[key] { return cached }
624
- guard digitWidth > 0, lineHeight > 0 else { return nil }
724
+ guard let image = drawStrip(role: role, blankZero: blankZero, modulus: modulus, color: nil) else { return nil }
725
+ if Self.sharedStrips.count >= 64 { Self.sharedStrips.removeAll(keepingCapacity: true) }
726
+ Self.sharedStrips[key] = image
727
+ return image
728
+ }
729
+
730
+ /// Slots in a strip for `modulus`: -1 (blank) through `modulus`.
731
+ static func stripSlots(modulus: Int) -> Int { modulus + 2 }
732
+
733
+ private func stripKey(role: GlyphRole, blankZero: Bool, modulus: Int) -> StripKey {
734
+ StripKey(font: fontTag(role), color: colorKey(for: role), scale: renderScale, blankZero: blankZero, modulus: modulus, top: stripTop(role), glyphs: glyphsKey)
735
+ }
736
+
737
+ private func stripTop(_ role: GlyphRole) -> CGFloat {
738
+ role == .fraction ? top(for: .fraction, text: glyphs[0], lineTop: 0) : 0
739
+ }
740
+
741
+ private func drawStrip(role: GlyphRole, blankZero: Bool, modulus: Int, color tint: UIColor?) -> UIImage? {
742
+ let cell = digitWidth(for: role)
743
+ guard cell > 0, lineHeight > 0 else { return nil }
625
744
  let format = UIGraphicsImageRendererFormat()
626
745
  format.scale = renderScale
627
746
  format.opaque = false
628
- let size = CGSize(width: ceil(digitWidth), height: lineHeight * CGFloat(Self.stripSlots))
629
- let image = UIGraphicsImageRenderer(size: size, format: format).image { _ in
630
- for slot in 0..<Self.stripSlots {
747
+ let slots = Self.stripSlots(modulus: modulus)
748
+ let top = stripTop(role)
749
+ let size = CGSize(width: ceil(cell), height: lineHeight * CGFloat(slots))
750
+ return UIGraphicsImageRenderer(size: size, format: format).image { _ in
751
+ for slot in 0..<slots {
631
752
  let index = slot - 1
632
753
  if index < 0 || (blankZero && index == 0) { continue }
633
- let text = String(index % 10)
634
- let w = width(of: text, role: .digit)
635
- attributed(text, role: .digit).draw(at: CGPoint(x: (digitWidth - w) / 2, y: CGFloat(slot) * lineHeight))
754
+ let text = glyphs[index % modulus]
755
+ let w = width(of: text, role: role)
756
+ let string = tint.map { NSAttributedString(string: text, attributes: [.font: font(for: role), .foregroundColor: $0]) } ?? attributed(text, role: role)
757
+ string.draw(at: CGPoint(x: (cell - w) / 2, y: CGFloat(slot) * lineHeight + top))
636
758
  }
637
759
  }
638
- if Self.sharedStrips.count >= 64 { Self.sharedStrips.removeAll(keepingCapacity: true) }
639
- Self.sharedStrips[key] = image
640
- return image
641
760
  }
642
761
 
643
762
  /// How far `text`'s ink hangs below the baseline (0 for digits and capitals).
@@ -653,8 +772,12 @@ final class NitroNumberView: UIView {
653
772
 
654
773
  /// Top of the glyph's line box for `role` drawing `text`, given the top of the digit line box.
655
774
  func top(for role: GlyphRole, text: String, lineTop: CGFloat) -> CGFloat {
656
- guard role != .digit else { return lineTop }
657
- return alignedTop(for: role, text: text, lineTop: lineTop) + (role == .prefix ? prefixOffset : suffixOffset)
775
+ switch role {
776
+ case .digit: return lineTop
777
+ case .fraction: return alignedTop(for: role, text: text, lineTop: lineTop)
778
+ case .prefix: return alignedTop(for: role, text: text, lineTop: lineTop) + prefixOffset
779
+ case .suffix: return alignedTop(for: role, text: text, lineTop: lineTop) + suffixOffset
780
+ }
658
781
  }
659
782
 
660
783
  /// Letter spacing after a glyph of `role`.
@@ -663,12 +786,20 @@ final class NitroNumberView: UIView {
663
786
  case .digit: return digitSpacing
664
787
  case .prefix: return prefixLetterSpacing
665
788
  case .suffix: return suffixLetterSpacing
789
+ case .fraction: return fractionLetterSpacing
666
790
  }
667
791
  }
668
792
 
669
793
  private func alignedTop(for role: GlyphRole, text: String, lineTop: CGFloat) -> CGFloat {
670
794
  let f = font(for: role)
671
- switch role == .prefix ? prefixAlign : suffixAlign {
795
+ let align: AffixAlign
796
+ switch role {
797
+ case .prefix: align = prefixAlign
798
+ case .suffix: align = suffixAlign
799
+ case .fraction: align = fractionAlign
800
+ case .digit: align = .baseline
801
+ }
802
+ switch align {
672
803
  case .baseline:
673
804
  return lineTop + (digit.ascender - f.ascender)
674
805
  case .center:
@@ -679,7 +810,7 @@ final class NitroNumberView: UIView {
679
810
  // Pin the bottom of the ink, not of the line boxes: the digits' ink ends
680
811
  // on the baseline, so "USD" sits on it too instead of hanging down to
681
812
  // where a comma's tail reaches.
682
- let affixBaseline = lineTop + digit.ascender + inkDescent("0123456789", role: .digit) - inkDescent(text, role: role)
813
+ let affixBaseline = lineTop + digit.ascender + inkDescent(glyphs.joined(), role: .digit) - inkDescent(text, role: role)
683
814
  return affixBaseline - f.ascender
684
815
  }
685
816
  }
@@ -775,8 +906,8 @@ final class NitroNumberView: UIView {
775
906
  var layer: CALayer
776
907
  /// The strip (wheels) or the glyph's image (glyphs), inside `layer`.
777
908
  var inner: CALayer
778
- /// Wheels only: which strip variant the layer currently shows.
779
- var blankZero: Bool
909
+ /// Wheels only: which strip the layer currently shows (`stripVariant`).
910
+ var stripVariant: Int
780
911
  /// Wheels only: the numeric transition's layers, once the wheel has swapped,
781
912
  /// on an unclipped layer of their own over the containers.
782
913
  var swap: SwapLayers?
@@ -798,11 +929,11 @@ final class NitroNumberView: UIView {
798
929
  var innerFrame = CGRect.null
799
930
  var opacity: Float = -1
800
931
 
801
- init(kind: LayerKind, layer: CALayer, inner: CALayer, blankZero: Bool, glyphTop: CGFloat) {
932
+ init(kind: LayerKind, layer: CALayer, inner: CALayer, stripVariant: Int, glyphTop: CGFloat) {
802
933
  self.kind = kind
803
934
  self.layer = layer
804
935
  self.inner = inner
805
- self.blankZero = blankZero
936
+ self.stripVariant = stripVariant
806
937
  self.glyphTop = glyphTop
807
938
  }
808
939
  }
@@ -898,6 +1029,7 @@ final class NitroNumberView: UIView {
898
1029
  func release() {
899
1030
  stopDisplayLink()
900
1031
  engine.reset()
1032
+ moving = false
901
1033
  clearSlots()
902
1034
  wheelBuffer = []
903
1035
  elementBuffer = []
@@ -926,7 +1058,8 @@ final class NitroNumberView: UIView {
926
1058
  engine.reset()
927
1059
  fontScale = 1
928
1060
  lastReportedSize = .zero
929
- leavingText = [nil, nil, nil, nil]
1061
+ leavingText = [nil, nil, nil, nil, nil]
1062
+ moving = false
930
1063
  autoAnchor = nil
931
1064
  awaitingLayout = false
932
1065
  laidFrame = nil
@@ -952,18 +1085,24 @@ final class NitroNumberView: UIView {
952
1085
  /// Shows `value` immediately with continuously positioned wheels (odometer
953
1086
  /// style). Cancels any running roll. Intended to be called every frame.
954
1087
  func setValue(_ value: Double) {
1088
+ let sign = signBefore()
955
1089
  engine.setValue(value)
1090
+ swapSignIfChanged(sign)
956
1091
  reportIntrinsicSize()
957
1092
  updateDisplayLinkNeed()
958
1093
  render()
1094
+ updateMoving()
959
1095
  }
960
1096
 
961
1097
  /// Rolls every wheel to `value` (snaps on first show, duration 0 or Reduce Motion).
962
1098
  func animate(to value: Double) {
963
- engine.setReduceMotion(UIAccessibility.isReduceMotionEnabled)
1099
+ applyReduceMotion()
1100
+ let sign = signBefore()
964
1101
  engine.animateTo(value, CACurrentMediaTime())
1102
+ swapSignIfChanged(sign)
965
1103
  reportIntrinsicSize()
966
1104
  updateDisplayLinkNeed()
1105
+ updateMoving()
967
1106
  // A roll is rendered by the display link's next tick, within a frame; a
968
1107
  // value that snapped (duration 0, Reduce Motion, nothing shown yet) has
969
1108
  // no tick coming and is rendered now. Rendering here as well doubled the
@@ -975,7 +1114,9 @@ final class NitroNumberView: UIView {
975
1114
  /// Shows the opening frame of a jackpot reveal for `value` ("$0.00" in the
976
1115
  /// target's layout), waiting for `reveal(to:)`.
977
1116
  func holdReveal(_ value: Double) {
1117
+ let sign = signBefore()
978
1118
  engine.holdReveal(value)
1119
+ swapSignIfChanged(sign)
979
1120
  reportIntrinsicSize()
980
1121
  updateDisplayLinkNeed()
981
1122
  render()
@@ -983,11 +1124,14 @@ final class NitroNumberView: UIView {
983
1124
 
984
1125
  /// Counts up from 0 to `value` and lands with a pop (snaps under Reduce Motion).
985
1126
  func reveal(to value: Double) {
986
- engine.setReduceMotion(UIAccessibility.isReduceMotionEnabled)
1127
+ applyReduceMotion()
1128
+ let sign = signBefore()
987
1129
  engine.reveal(value, CACurrentMediaTime())
1130
+ swapSignIfChanged(sign)
988
1131
  reportIntrinsicSize()
989
1132
  updateDisplayLinkNeed()
990
1133
  render()
1134
+ updateMoving()
991
1135
  reportMilestones(reachedBefore: 0)
992
1136
  if !engine.isRevealing() {
993
1137
  // Snapped (Reduce Motion / duration 0): the reveal is over before it began.
@@ -995,6 +1139,40 @@ final class NitroNumberView: UIView {
995
1139
  }
996
1140
  }
997
1141
 
1142
+ /// Snaps under Reduce Motion unless told not to (`respectReduceMotion`).
1143
+ private func applyReduceMotion() {
1144
+ engine.setReduceMotion(timing.respectReduceMotion && UIAccessibility.isReduceMotionEnabled)
1145
+ }
1146
+
1147
+ /// The sign glyph on screen before a change, if one is.
1148
+ private func signBefore() -> String? {
1149
+ guard engine.hasShownValue(), engine.signFactor() > 0 else { return nil }
1150
+ return engine.signPositive() ? format.plusSign : format.minusSign
1151
+ }
1152
+
1153
+ /// A plus that turns into a minus (or back) swaps like any text: the old
1154
+ /// one softens away as the new one comes into focus.
1155
+ private func swapSignIfChanged(_ before: String?) {
1156
+ guard let before else { return }
1157
+ let after = engine.signPositive() ? format.plusSign : format.minusSign
1158
+ guard after != before else { return }
1159
+ leavingText[Self.signText] = before
1160
+ engine.changeText(Int32(Self.signText), CACurrentMediaTime())
1161
+ }
1162
+
1163
+ /// Fires `onAnimationStart` when a roll or a reveal sets off from rest and
1164
+ /// `onAnimationEnd` when the figure is still again, once for a run of changes.
1165
+ private func updateMoving() {
1166
+ let now = engine.isRolling() || engine.isRevealing()
1167
+ guard now != moving else { return }
1168
+ moving = now
1169
+ if now {
1170
+ onAnimationStart?()
1171
+ } else {
1172
+ onAnimationEnd?(engine.targetValue())
1173
+ }
1174
+ }
1175
+
998
1176
  /// Fires `onRevealMilestone` for every milestone reached since `reachedBefore`.
999
1177
  private func reportMilestones(reachedBefore: Int) {
1000
1178
  let reached = Int(engine.revealMilestonesReached())
@@ -1027,6 +1205,7 @@ final class NitroNumberView: UIView {
1027
1205
  onRevealEnd?()
1028
1206
  }
1029
1207
  }
1208
+ updateMoving()
1030
1209
  }
1031
1210
 
1032
1211
  /// Keeps the display link alive only while the engine says something moves.
@@ -1093,11 +1272,14 @@ final class NitroNumberView: UIView {
1093
1272
  DispatchQueue.main.async { [weak self] in
1094
1273
  guard let self, self.timing.transition == .numeric else { return }
1095
1274
  let tints = [self.flash.upColor, self.flash.downColor].compactMap { $0 }
1096
- for digit in Self.digitStrings {
1097
- for level in 0...Self.numericBlurLevels {
1098
- _ = self.fonts.blurredImage(digit, level: level)
1099
- for tint in tints {
1100
- _ = self.fonts.tintedBlurredImage(digit, tint: tint, level: level)
1275
+ let roles: [GlyphRole] = self.format.fractionDigits > 0 ? [.digit, .fraction] : [.digit]
1276
+ for role in roles {
1277
+ for digit in self.fonts.glyphs {
1278
+ for level in 0...Self.numericBlurLevels {
1279
+ _ = self.fonts.blurredImage(digit, role: role, level: level)
1280
+ for tint in tints {
1281
+ _ = self.fonts.tintedBlurredImage(digit, role: role, tint: tint, level: level)
1282
+ }
1101
1283
  }
1102
1284
  }
1103
1285
  }
@@ -1185,6 +1367,8 @@ final class NitroNumberView: UIView {
1185
1367
  var anchor = 1
1186
1368
  /// Space after the cell (letter spacing, or an affix seam), scaled with it; not part of the glyph's box.
1187
1369
  var gap: CGFloat = 0
1370
+ /// Wheels: whose font draws it, the integer digits' or the fraction's.
1371
+ var role: GlyphRole = .digit
1188
1372
  var advance: CGFloat { width + gap }
1189
1373
  }
1190
1374
 
@@ -1242,57 +1426,75 @@ final class NitroNumberView: UIView {
1242
1426
  affixBlocks = AffixBlocks(prefix: format.prefix, suffix: format.suffix, rtl: rtl)
1243
1427
  }
1244
1428
  let affixes = affixBlocks
1429
+ let signGlyph = engine.signPositive() ? format.plusSign : format.minusSign
1245
1430
  // A swapping affix keeps to the digits' side; a separator is centred.
1246
1431
  if rtl {
1247
1432
  addGlyph(affixes.suffixInk, role: .suffix, factor: 1, slot: Self.suffixText, anchor: 1)
1248
1433
  addGlyph(affixes.suffixGap, role: .suffix, factor: 1)
1249
1434
  } else {
1250
1435
  // Sign first, then the currency prefix: "-$1,234.50".
1251
- addGlyph("-", role: .digit, factor: signFactor)
1436
+ addGlyph(signGlyph, role: .digit, factor: signFactor, slot: Self.signText, anchor: 1)
1252
1437
  addGlyph(affixes.prefixInk, role: .prefix, factor: 1, slot: Self.prefixText, anchor: 1)
1253
1438
  }
1254
1439
  var power = wheels.count - 1
1255
1440
  while power >= 0 {
1256
1441
  let wheel = wheels[power]
1257
1442
  if wheel.width > 0 {
1258
- let advance = digitAdvance(wheel, power: power, fonts: fonts, settled: !animated)
1259
- elements.append(Element(kind: .wheel(power), width: advance * CGFloat(wheel.width), fullWidth: advance, factor: wheel.width))
1443
+ let role: GlyphRole = power < fd ? .fraction : .digit
1444
+ let advance = digitAdvance(wheel, power: power, role: role, fonts: fonts, settled: !animated)
1445
+ elements.append(Element(kind: .wheel(power), width: advance * CGFloat(wheel.width), fullWidth: advance, factor: wheel.width, role: role))
1260
1446
  }
1261
- if power > fd, (power - fd) % 3 == 0 {
1447
+ if power > fd, Self.isGroupBoundary(power - fd, sizes: format.groupingSizes) {
1262
1448
  addGlyph(format.groupingSeparator, role: .digit, factor: wheel.width, slot: Self.groupingText, anchor: 0)
1263
1449
  }
1264
1450
  if fd > 0, power == fd {
1265
- addGlyph(format.decimalSeparator, role: .digit, factor: decimal, slot: Self.decimalText, anchor: 0)
1451
+ addGlyph(format.decimalSeparator, role: .fraction, factor: decimal, slot: Self.decimalText, anchor: 0)
1266
1452
  }
1267
1453
  power -= 1
1268
1454
  }
1269
1455
  if rtl {
1270
1456
  addGlyph(affixes.prefixGap, role: .prefix, factor: 1)
1271
1457
  addGlyph(affixes.prefixInk, role: .prefix, factor: 1, slot: Self.prefixText, anchor: -1)
1272
- addGlyph("-", role: .digit, factor: signFactor)
1458
+ addGlyph(signGlyph, role: .digit, factor: signFactor, slot: Self.signText, anchor: -1)
1273
1459
  } else {
1274
1460
  addGlyph(affixes.suffixInk, role: .suffix, factor: 1, slot: Self.suffixText, anchor: -1)
1275
1461
  }
1276
1462
  applySpacing(to: &elements, fonts: fonts)
1277
1463
  }
1278
1464
 
1465
+ /// Whether a grouping separator follows the `k`th integer digit counted
1466
+ /// from the decimal point: every three by default, or `sizes` (the first
1467
+ /// group, then each later one: [3, 2] is 12,34,567). A size of 0 is none.
1468
+ private static func isGroupBoundary(_ k: Int, sizes: [Int]) -> Bool {
1469
+ let primary = sizes.first ?? 3
1470
+ guard primary > 0, k >= primary else { return false }
1471
+ let secondary = sizes.count > 1 ? sizes[1] : primary
1472
+ guard secondary > 0 else { return k == primary }
1473
+ return (k - primary) % secondary == 0
1474
+ }
1475
+
1279
1476
  /// A wheel's cell width at full size: the widest digit's with tabular
1280
1477
  /// figures; otherwise its target digit's, eased there once from where the
1281
1478
  /// column was on the wheel's own progress. Following the digits a wheel
1282
1479
  /// rolled past made every column pulse as a narrow 1 went by. Settled, and
1283
1480
  /// throughout a reveal (whose layout is the target's from the first frame),
1284
1481
  /// it is the target digit's.
1285
- private func digitAdvance(_ wheel: Engine.Wheel, power: Int, fonts: FontSet, settled: Bool) -> CGFloat {
1286
- guard fonts.proportional else { return fonts.digitWidth }
1287
- let widths = fonts.digitWidths
1482
+ private func digitAdvance(_ wheel: Engine.Wheel, power: Int, role: GlyphRole, fonts: FontSet, settled: Bool) -> CGFloat {
1483
+ guard fonts.proportional else { return fonts.digitWidth(for: role) }
1484
+ let widths = fonts.digitWidths(for: role)
1288
1485
  let digit = max(0, min(9, Int(engine.targetDigit(Int32(power)))))
1289
1486
  let goal = widths[digit]
1290
1487
  if settled || engine.isRevealing() { return goal }
1291
1488
  var cell = cellWidths[power] ?? CellWidth()
1489
+ if cell.role != role {
1490
+ // A column that became a fraction digit (or stopped being one) starts over in its new font.
1491
+ cell = CellWidth()
1492
+ cell.role = role
1493
+ }
1292
1494
  if cell.digit != digit || wheel.progress < cell.progress {
1293
1495
  // Interrupted mid-change, it carries on from what it showed; from rest,
1294
1496
  // from the digit the wheel is leaving.
1295
- cell.from = cell.digit >= 0 && cell.progress < 1 ? cell.shown : restingWidth(wheel, widths: widths, goal: goal)
1497
+ cell.from = cell.digit >= 0 && cell.progress < 1 ? cell.shown : restingWidth(wheel, modulus: Int(engine.wheelModulus(Int32(power))), widths: widths, goal: goal)
1296
1498
  cell.digit = digit
1297
1499
  }
1298
1500
  cell.progress = wheel.progress
@@ -1302,12 +1504,12 @@ final class NitroNumberView: UIView {
1302
1504
  }
1303
1505
 
1304
1506
  /// The width of the digit a wheel shows at the start of a change (blank takes the target's).
1305
- private func restingWidth(_ wheel: Engine.Wheel, widths: [CGFloat], goal: CGFloat) -> CGFloat {
1507
+ private func restingWidth(_ wheel: Engine.Wheel, modulus: Int, widths: [CGFloat], goal: CGFloat) -> CGFloat {
1306
1508
  let glyph: Int
1307
1509
  if wheel.blend < 1 {
1308
1510
  glyph = Int(wheel.fromGlyph)
1309
1511
  } else {
1310
- glyph = Int((wheel.linear ? wheel.position : Self.wrap10(wheel.position)).rounded())
1512
+ glyph = Int((wheel.linear ? wheel.position : Self.wrap(wheel.position, modulus)).rounded()) % modulus
1311
1513
  }
1312
1514
  return glyph >= 0 ? widths[glyph % 10] : goal
1313
1515
  }
@@ -1318,6 +1520,7 @@ final class NitroNumberView: UIView {
1318
1520
  var shown: CGFloat = 0
1319
1521
  var digit = -1
1320
1522
  var progress: Double = 1
1523
+ var role: GlyphRole = .digit
1321
1524
  }
1322
1525
 
1323
1526
  private var cellWidths: [Int: CellWidth] = [:]
@@ -1438,7 +1641,7 @@ final class NitroNumberView: UIView {
1438
1641
  var power = Int(engine.settledPowerCount()) - 1
1439
1642
  while power >= 0 {
1440
1643
  digits += String(engine.targetDigit(Int32(power)))
1441
- if power > fd, (power - fd) % 3 == 0 {
1644
+ if power > fd, Self.isGroupBoundary(power - fd, sizes: format.groupingSizes) {
1442
1645
  digits += format.groupingSeparator
1443
1646
  }
1444
1647
  if fd > 0, power == fd {
@@ -1446,7 +1649,9 @@ final class NitroNumberView: UIView {
1446
1649
  }
1447
1650
  power -= 1
1448
1651
  }
1449
- return (engine.settledNegative() ? "-" : "") + format.prefix + digits + format.suffix
1652
+ // `settledNegative` is whether the settled figure carries a sign, a plus included.
1653
+ let sign = engine.settledNegative() ? (engine.signPositive() ? format.plusSign : format.minusSign) : ""
1654
+ return sign + format.prefix + digits + format.suffix
1450
1655
  }
1451
1656
 
1452
1657
  /// An `accessibilityLabel` the app set, which VoiceOver reads instead of the figure.
@@ -1559,7 +1764,8 @@ final class NitroNumberView: UIView {
1559
1764
  if case .wheel = element.kind {
1560
1765
  // A digit column is the widest digit wide, centred on the digit's own
1561
1766
  // cell (with tabular figures the two are the same, right-aligned).
1562
- mask = CGRect(x: x + element.width - (element.fullWidth + fonts.digitWidth) / 2, y: 0, width: fonts.digitWidth, height: fonts.lineHeight)
1767
+ let column = fonts.digitWidth(for: element.role)
1768
+ mask = CGRect(x: x + element.width - (element.fullWidth + column) / 2, y: 0, width: column, height: fonts.lineHeight)
1563
1769
  }
1564
1770
  if slot.frame != mask {
1565
1771
  slot.layer.frame = mask
@@ -1594,18 +1800,24 @@ final class NitroNumberView: UIView {
1594
1800
  slots[i].overlay = overlay
1595
1801
  slots[i].swap = swap
1596
1802
  }
1597
- layoutSwap(swap, wheel: wheel, fonts: fonts, cellWidth: element.width, advance: element.fullWidth, tint: tint, flash: CGFloat(wheel.flash))
1803
+ layoutSwap(swap, wheel: wheel, role: element.role, fonts: fonts, cellWidth: element.width, advance: element.fullWidth, tint: tint, flash: CGFloat(wheel.flash))
1598
1804
  } else if let swap = slot.swap, !swap.hidden {
1599
1805
  for layer in swap.all { layer.isHidden = true }
1600
1806
  swap.hidden = true
1601
1807
  }
1602
1808
  if strip.isHidden != swapping { strip.isHidden = swapping }
1603
- if slot.blankZero != wheel.blankZero {
1604
- strip.contents = fonts.strip(blankZero: wheel.blankZero)?.cgImage
1605
- slots[i].blankZero = wheel.blankZero
1809
+ let modulus = Int(engine.wheelModulus(Int32(index)))
1810
+ let variant = Self.stripVariant(role: element.role, blankZero: wheel.blankZero, modulus: modulus)
1811
+ if slot.stripVariant != variant {
1812
+ if let image = fonts.strip(role: element.role, blankZero: wheel.blankZero, modulus: modulus) {
1813
+ strip.contents = image.cgImage
1814
+ strip.bounds = CGRect(origin: .zero, size: image.size)
1815
+ }
1816
+ slots[i].stripVariant = variant
1817
+ slots[i].innerFrame = .null
1606
1818
  }
1607
- // Linear strips run from -1 (blank) to 9; a roll can be any real, so wrap it onto 0..<10.
1608
- let position = wheel.linear ? wheel.position : Self.wrap10(wheel.position)
1819
+ // Linear strips run from -1 (blank) to 9; a roll can be any real, so wrap it onto the wheel's places.
1820
+ let position = wheel.linear ? wheel.position : Self.wrap(wheel.position, modulus)
1609
1821
  let stripFrame = CGRect(
1610
1822
  x: 0,
1611
1823
  y: -(position + 1) * fonts.lineHeight,
@@ -1616,7 +1828,7 @@ final class NitroNumberView: UIView {
1616
1828
  strip.frame = stripFrame
1617
1829
  slots[i].innerFrame = stripFrame
1618
1830
  }
1619
- layoutFlash(slotIndex: i, wheel: wheel, tint: swapping ? nil : tint, fonts: fonts, stripFrame: stripFrame)
1831
+ layoutFlash(slotIndex: i, wheel: wheel, role: element.role, modulus: modulus, tint: swapping ? nil : tint, fonts: fonts, stripFrame: stripFrame)
1620
1832
  case .glyph(let text, let role):
1621
1833
  if let from = element.fromText {
1622
1834
  // Swapping its text: the overlay's pair of pairs instead of the image.
@@ -1684,7 +1896,7 @@ final class NitroNumberView: UIView {
1684
1896
  /// cross-faded with its blurred image as it goes out of, or comes into,
1685
1897
  /// focus. With a change flash on (`tint`), the same two again in the tint
1686
1898
  /// at `flash` of their opacity, over them: the ink mixed towards the tint.
1687
- private func layoutSwap(_ swap: SwapLayers, wheel: Engine.Wheel, fonts: FontSet, cellWidth: CGFloat, advance: CGFloat, tint: UIColor?, flash: CGFloat) {
1899
+ private func layoutSwap(_ swap: SwapLayers, wheel: Engine.Wheel, role: GlyphRole, fonts: FontSet, cellWidth: CGFloat, advance: CGFloat, tint: UIColor?, flash: CGFloat) {
1688
1900
  // The position clock overshoots 1 (a spring); only the offsets follow it there.
1689
1901
  let b = CGFloat(wheel.blend)
1690
1902
  let g = max(0, min(1, CGFloat(wheel.grow)))
@@ -1692,7 +1904,7 @@ final class NitroNumberView: UIView {
1692
1904
  let d: CGFloat = wheel.fromAbove ? 1 : -1
1693
1905
  let offset = fonts.lineHeight * Self.numericOffset
1694
1906
  // The digit sits right-aligned in its cell (the cell shrinks and grows from the left).
1695
- let center = CGPoint(x: cellWidth - advance / 2, y: fonts.lineHeight / 2)
1907
+ let center = CGPoint(x: cellWidth - advance / 2, y: fonts.digitCenterY(for: role))
1696
1908
  let outGlyph = Int(wheel.fromGlyph)
1697
1909
  let outCenter = CGPoint(x: center.x, y: center.y + d * offset * b)
1698
1910
  let outScale = 1 - (1 - Self.numericScale) * g
@@ -1703,11 +1915,11 @@ final class NitroNumberView: UIView {
1703
1915
  let inScale = Self.numericScale + (1 - Self.numericScale) * g
1704
1916
  let inAlpha = g
1705
1917
  let inBlur = 1 - f
1706
- placeSwapGlyph(swap.leaving, glyph: outGlyph, fonts: fonts, tint: nil, center: outCenter, scale: outScale, alpha: outAlpha, blur: outBlur)
1707
- placeSwapGlyph(swap.arriving, glyph: inGlyph, fonts: fonts, tint: nil, center: inCenter, scale: inScale, alpha: inAlpha, blur: inBlur)
1918
+ placeSwapGlyph(swap.leaving, glyph: outGlyph, role: role, fonts: fonts, tint: nil, center: outCenter, scale: outScale, alpha: outAlpha, blur: outBlur)
1919
+ placeSwapGlyph(swap.arriving, glyph: inGlyph, role: role, fonts: fonts, tint: nil, center: inCenter, scale: inScale, alpha: inAlpha, blur: inBlur)
1708
1920
  let tintAlpha = tint == nil ? 0 : flash
1709
- placeSwapGlyph(swap.leavingTint, glyph: outGlyph, fonts: fonts, tint: tint, center: outCenter, scale: outScale, alpha: outAlpha * tintAlpha, blur: outBlur)
1710
- placeSwapGlyph(swap.arrivingTint, glyph: inGlyph, fonts: fonts, tint: tint, center: inCenter, scale: inScale, alpha: inAlpha * tintAlpha, blur: inBlur)
1921
+ placeSwapGlyph(swap.leavingTint, glyph: outGlyph, role: role, fonts: fonts, tint: tint, center: outCenter, scale: outScale, alpha: outAlpha * tintAlpha, blur: outBlur)
1922
+ placeSwapGlyph(swap.arrivingTint, glyph: inGlyph, role: role, fonts: fonts, tint: tint, center: inCenter, scale: inScale, alpha: inAlpha * tintAlpha, blur: inBlur)
1711
1923
  swap.hidden = false
1712
1924
  }
1713
1925
 
@@ -1774,7 +1986,7 @@ final class NitroNumberView: UIView {
1774
1986
  /// `numericBlurLevels`, level 0 sharp), cross-faded by where between them
1775
1987
  /// it falls, so a glyph half out of focus is half-blurred rather than a
1776
1988
  /// sharp glyph half-hidden in a fully blurred one.
1777
- private func placeSwapGlyph(_ pair: (GlyphLayer, GlyphLayer), glyph: Int, fonts: FontSet, tint: UIColor?, center: CGPoint, scale: CGFloat, alpha: CGFloat, blur amount: CGFloat) {
1989
+ private func placeSwapGlyph(_ pair: (GlyphLayer, GlyphLayer), glyph: Int, role: GlyphRole, fonts: FontSet, tint: UIColor?, center: CGPoint, scale: CGFloat, alpha: CGFloat, blur amount: CGFloat) {
1778
1990
  guard glyph >= 0, alpha > 0.002 else {
1779
1991
  if !pair.0.layer.isHidden { pair.0.layer.isHidden = true }
1780
1992
  if !pair.1.layer.isHidden { pair.1.layer.isHidden = true }
@@ -1793,7 +2005,7 @@ final class NitroNumberView: UIView {
1793
2005
  let hiAlpha = alpha * w
1794
2006
  let loAlpha = hiAlpha >= 0.999 ? 0 : alpha * (1 - w) / (1 - hiAlpha)
1795
2007
  let tintKey = tint.map { FontSet.key(of: $0) } ?? 0
1796
- let text = Self.digitStrings[glyph % 10]
2008
+ let text = fonts.glyphs[glyph % 10]
1797
2009
  let transform = CATransform3DMakeScale(scale, scale, 1)
1798
2010
  // A local function, not a loop over an array literal: that allocated an array per call.
1799
2011
  func place(_ slot: GlyphLayer, _ level: Int, _ layerAlpha: CGFloat) {
@@ -1802,7 +2014,7 @@ final class NitroNumberView: UIView {
1802
2014
  if layer.isHidden != hidden { layer.isHidden = hidden }
1803
2015
  if hidden { return }
1804
2016
  if slot.glyph != glyph || slot.level != level || slot.tint != tintKey {
1805
- if let image = fonts.swapImage(text, level: level, tint: tint) {
2017
+ if let image = fonts.swapImage(text, role: role, level: level, tint: tint) {
1806
2018
  layer.contents = image.cgImage
1807
2019
  layer.bounds = CGRect(origin: .zero, size: image.size)
1808
2020
  }
@@ -1823,7 +2035,7 @@ final class NitroNumberView: UIView {
1823
2035
  /// down colour at the flash's opacity, so the tint rides the roll instead
1824
2036
  /// of hovering over it. A swapping wheel's flash is its swap layers' tinted
1825
2037
  /// twins (`layoutSwap`), so `tint` is nil for it.
1826
- private func layoutFlash(slotIndex i: Int, wheel: Engine.Wheel, tint: UIColor?, fonts: FontSet, stripFrame: CGRect) {
2038
+ private func layoutFlash(slotIndex i: Int, wheel: Engine.Wheel, role: GlyphRole, modulus: Int, tint: UIColor?, fonts: FontSet, stripFrame: CGRect) {
1827
2039
  guard let tint, !stripFrame.isNull else {
1828
2040
  if let layer = slots[i].flashLayer, !layer.isHidden { layer.isHidden = true }
1829
2041
  return
@@ -1839,9 +2051,9 @@ final class NitroNumberView: UIView {
1839
2051
  }
1840
2052
  let tintKey = FontSet.key(of: tint)
1841
2053
  // The strip's own tinted twin, keyed as a glyph the digits can never be.
1842
- let stripGlyph = 100 + (wheel.blankZero ? 1 : 0)
2054
+ let stripGlyph = 100 + Self.stripVariant(role: role, blankZero: wheel.blankZero, modulus: modulus)
1843
2055
  if slots[i].flashGlyph != stripGlyph || slots[i].flashTint != tintKey {
1844
- guard let image = fonts.tintedStrip(blankZero: wheel.blankZero, tint: tint) else { return }
2056
+ guard let image = fonts.tintedStrip(role: role, blankZero: wheel.blankZero, modulus: modulus, tint: tint) else { return }
1845
2057
  layer.contents = image.cgImage
1846
2058
  slots[i].flashGlyph = stripGlyph
1847
2059
  slots[i].flashTint = tintKey
@@ -1865,13 +2077,14 @@ final class NitroNumberView: UIView {
1865
2077
  switch element.kind {
1866
2078
  case .wheel(let index):
1867
2079
  let blankZero = wheels[index].blankZero
1868
- if let strip = fonts.strip(blankZero: blankZero) {
2080
+ let modulus = Int(engine.wheelModulus(Int32(index)))
2081
+ if let strip = fonts.strip(role: element.role, blankZero: blankZero, modulus: modulus) {
1869
2082
  inner.contents = strip.cgImage
1870
2083
  inner.bounds = CGRect(origin: .zero, size: strip.size)
1871
2084
  }
1872
2085
  container.addSublayer(inner)
1873
2086
  contentLayer.addSublayer(container)
1874
- slots.append(ElementLayer(kind: .wheel, layer: container, inner: inner, blankZero: blankZero, glyphTop: 0))
2087
+ slots.append(ElementLayer(kind: .wheel, layer: container, inner: inner, stripVariant: Self.stripVariant(role: element.role, blankZero: blankZero, modulus: modulus), glyphTop: 0))
1875
2088
  case .glyph(let text, let role):
1876
2089
  if let image = fonts.image(text, role: role) {
1877
2090
  inner.contents = image.cgImage
@@ -1879,7 +2092,7 @@ final class NitroNumberView: UIView {
1879
2092
  }
1880
2093
  container.addSublayer(inner)
1881
2094
  contentLayer.addSublayer(container)
1882
- slots.append(ElementLayer(kind: .glyph(text, role), layer: container, inner: inner, blankZero: false, glyphTop: fonts.top(for: role, text: text, lineTop: 0)))
2095
+ slots.append(ElementLayer(kind: .glyph(text, role), layer: container, inner: inner, stripVariant: -1, glyphTop: fonts.top(for: role, text: text, lineTop: 0)))
1883
2096
  }
1884
2097
  }
1885
2098
  }
@@ -1901,9 +2114,20 @@ final class NitroNumberView: UIView {
1901
2114
  return true
1902
2115
  }
1903
2116
 
2117
+ /// Which strip a wheel draws from, as one comparable number.
2118
+ private static func stripVariant(role: GlyphRole, blankZero: Bool, modulus: Int) -> Int {
2119
+ (blankZero ? 1 : 0) | (modulus << 1) | (role == .fraction ? 1 << 8 : 0)
2120
+ }
2121
+
1904
2122
  private static func wrap10(_ position: Double) -> Double {
1905
- let r = fmod(position, 10)
1906
- return r < 0 ? r + 10 : r
2123
+ wrap(position, 10)
2124
+ }
2125
+
2126
+ /// `position` wrapped onto 0..<`modulus` (a wheel's places, 10 or a clock's 6).
2127
+ private static func wrap(_ position: Double, _ modulus: Int) -> Double {
2128
+ let m = Double(modulus)
2129
+ let r = fmod(position, m)
2130
+ return r < 0 ? r + m : r
1907
2131
  }
1908
2132
 
1909
2133
  /// Where the content box (`total` × `lineHeight`, in font space) goes and how
@@ -1953,7 +2177,7 @@ final class NitroNumberView: UIView {
1953
2177
  for element in elements {
1954
2178
  switch element.kind {
1955
2179
  case .wheel(let index):
1956
- drawWheel(wheels[index], fonts: fonts, x: x, width: element.width, advance: element.fullWidth, ctx: ctx)
2180
+ drawWheel(wheels[index], role: element.role, modulus: Int(engine.wheelModulus(Int32(index))), fonts: fonts, x: x, width: element.width, advance: element.fullWidth, ctx: ctx)
1957
2181
  case .glyph(let text, let role):
1958
2182
  drawGlyph(text, role: role, fonts: fonts, x: x, width: element.width, fullWidth: element.fullWidth, alpha: element.factor, ctx: ctx)
1959
2183
  }
@@ -1972,23 +2196,30 @@ final class NitroNumberView: UIView {
1972
2196
  private func drawShimmer(contentWidth: CGFloat, dim: CGFloat, ctx: CGContext) {
1973
2197
  guard contentWidth > 0 else { return }
1974
2198
  var br: CGFloat = 0, bg: CGFloat = 0, bb: CGFloat = 0, ba: CGFloat = 1
1975
- typography.color.getRed(&br, green: &bg, blue: &bb, alpha: &ba)
2199
+ (shimmer.baseColor ?? typography.color).resolvedColor(with: traitCollection).getRed(&br, green: &bg, blue: &bb, alpha: &ba)
1976
2200
  var hr: CGFloat = 0, hg: CGFloat = 0, hb: CGFloat = 0, ha: CGFloat = 1
1977
- (shimmer.color ?? Self.defaultShimmerColor).getRed(&hr, green: &hg, blue: &hb, alpha: &ha)
2201
+ (shimmer.color ?? Self.defaultShimmerColor).resolvedColor(with: traitCollection).getRed(&hr, green: &hg, blue: &hb, alpha: &ha)
1978
2202
  let base = CGColor(red: br, green: bg, blue: bb, alpha: ba)
1979
2203
  let colors = [base, CGColor(red: hr, green: hg, blue: hb, alpha: ha), base] as CFArray
1980
2204
  guard let gradient = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: colors, locations: [0.1, 0.5, 0.9]) else { return }
1981
- let phase = CGFloat(engine.shimmerPhase(CACurrentMediaTime(), shimmer.duration))
1982
- let progress = Self.shimmerSeed + (1 - Self.shimmerSeed) * phase
1983
- // Core at width * (2p - 0.5): enters at the left edge, exits past the right.
1984
- let startX = contentWidth * (2 * progress - 1)
2205
+ // One sweep takes `duration`; the band then waits off the far edge for `delay`.
2206
+ let cycle = shimmer.duration + shimmer.delay
2207
+ let phase = min(1, CGFloat(engine.shimmerPhase(CACurrentMediaTime(), cycle)) * CGFloat(cycle / shimmer.duration))
2208
+ let seeded = Self.shimmerSeed + (1 - Self.shimmerSeed) * phase
2209
+ let ltr = shimmer.leftToRight ?? !isRTL
2210
+ let progress = ltr ? seeded : 1 - seeded
2211
+ // The gradient runs `length` along x (its slant on top): it enters at the
2212
+ // left edge and leaves past the right, or the mirror image.
2213
+ let length = contentWidth * max(0.05, shimmer.width)
2214
+ let startX = (contentWidth + length) * progress - length
2215
+ let slant = tan(max(-75, min(75, shimmer.angle)) * .pi / 180) * (ltr ? 1 : -1)
1985
2216
  ctx.saveGState()
1986
2217
  ctx.setBlendMode(.sourceAtop)
1987
2218
  ctx.setAlpha(dim)
1988
2219
  ctx.drawLinearGradient(
1989
2220
  gradient,
1990
2221
  start: CGPoint(x: startX, y: 0),
1991
- end: CGPoint(x: startX + contentWidth, y: Self.shimmerSlant * contentWidth),
2222
+ end: CGPoint(x: startX + length, y: slant * length),
1992
2223
  options: [.drawsBeforeStartLocation, .drawsAfterEndLocation]
1993
2224
  )
1994
2225
  ctx.restoreGState()
@@ -2004,14 +2235,16 @@ final class NitroNumberView: UIView {
2004
2235
  ctx.restoreGState()
2005
2236
  }
2006
2237
 
2007
- private func drawWheel(_ wheel: Engine.Wheel, fonts: FontSet, x: CGFloat, width: CGFloat, advance: CGFloat, ctx: CGContext) {
2238
+ private func drawWheel(_ wheel: Engine.Wheel, role: GlyphRole, modulus: Int, fonts: FontSet, x: CGFloat, width: CGFloat, advance: CGFloat, ctx: CGContext) {
2008
2239
  guard width > 0 else { return }
2009
2240
  let lineHeight = fonts.lineHeight
2241
+ let column = fonts.digitWidth(for: role)
2010
2242
  // The digit column, the widest digit wide, centred on the digit's own cell.
2011
- let columnLeft = x + width - (advance + fonts.digitWidth) / 2
2243
+ let columnLeft = x + width - (advance + column) / 2
2244
+ let top = role == .fraction ? fonts.top(for: .fraction, text: fonts.glyphs[0], lineTop: 0) : 0
2012
2245
  ctx.saveGState()
2013
2246
  // The roll's window, the whole digit wide (see `renderLayers`).
2014
- ctx.clip(to: CGRect(x: columnLeft, y: 0, width: fonts.digitWidth, height: lineHeight))
2247
+ ctx.clip(to: CGRect(x: columnLeft, y: 0, width: column, height: lineHeight))
2015
2248
  ctx.setAlpha(CGFloat(Self.openingOpacity(wheel.width)))
2016
2249
  if wheel.blend < 1 || wheel.focus < 1 || wheel.grow < 1 {
2017
2250
  // The numeric transition under the glint: the pair, without the blur.
@@ -2019,16 +2252,17 @@ final class NitroNumberView: UIView {
2019
2252
  let g = max(0, min(1, CGFloat(wheel.grow)))
2020
2253
  let d: CGFloat = wheel.fromAbove ? 1 : -1
2021
2254
  let offset = lineHeight * Self.numericOffset
2022
- let cx = columnLeft + fonts.digitWidth / 2
2255
+ let cx = columnLeft + column / 2
2256
+ let cy = fonts.digitCenterY(for: role)
2023
2257
  let pair: [(glyph: Int, dy: CGFloat, scale: CGFloat, alpha: CGFloat)] = [
2024
2258
  (Int(wheel.fromGlyph), d * offset * b, 1 - (1 - Self.numericScale) * g, 1 - g),
2025
2259
  (Int(wheel.toGlyph), -d * offset * (1 - b), Self.numericScale + (1 - Self.numericScale) * g, g),
2026
2260
  ]
2027
2261
  for item in pair where item.glyph >= 0 && item.alpha > 0.002 {
2028
- guard let image = fonts.image(Self.digitStrings[item.glyph % 10], role: .digit) else { continue }
2262
+ guard let image = fonts.image(fonts.glyphs[item.glyph % 10], role: role) else { continue }
2029
2263
  ctx.saveGState()
2030
2264
  ctx.setAlpha(CGFloat(wheel.width) * item.alpha)
2031
- ctx.translateBy(x: cx, y: lineHeight / 2 + item.dy)
2265
+ ctx.translateBy(x: cx, y: cy + item.dy)
2032
2266
  ctx.scaleBy(x: item.scale, y: item.scale)
2033
2267
  image.draw(at: CGPoint(x: -image.size.width / 2, y: -image.size.height / 2))
2034
2268
  ctx.restoreGState()
@@ -2036,24 +2270,23 @@ final class NitroNumberView: UIView {
2036
2270
  ctx.restoreGState()
2037
2271
  return
2038
2272
  }
2039
- let base = wheel.position.rounded(.down)
2040
- let fraction = CGFloat(wheel.position - base)
2273
+ let position = wheel.linear ? wheel.position : Self.wrap(wheel.position, modulus)
2274
+ let base = position.rounded(.down)
2275
+ let fraction = CGFloat(position - base)
2041
2276
  let index = Int(base)
2042
- if let text = digitText(at: index, in: wheel), let image = fonts.image(text, role: .digit) {
2043
- image.draw(at: CGPoint(x: columnLeft + (fonts.digitWidth - fonts.width(of: text, role: .digit)) / 2, y: -fraction * lineHeight))
2277
+ if let text = digitText(at: index, in: wheel, modulus: modulus, fonts: fonts), let image = fonts.image(text, role: role) {
2278
+ image.draw(at: CGPoint(x: columnLeft + (column - fonts.width(of: text, role: role)) / 2, y: top - fraction * lineHeight))
2044
2279
  }
2045
- if fraction > 0.0001, let text = digitText(at: index + 1, in: wheel), let image = fonts.image(text, role: .digit) {
2046
- image.draw(at: CGPoint(x: columnLeft + (fonts.digitWidth - fonts.width(of: text, role: .digit)) / 2, y: (1 - fraction) * lineHeight))
2280
+ if fraction > 0.0001, let text = digitText(at: index + 1, in: wheel, modulus: modulus, fonts: fonts), let image = fonts.image(text, role: role) {
2281
+ image.draw(at: CGPoint(x: columnLeft + (column - fonts.width(of: text, role: role)) / 2, y: top + (1 - fraction) * lineHeight))
2047
2282
  }
2048
2283
  ctx.restoreGState()
2049
2284
  }
2050
2285
 
2051
- private static let digitStrings = (0...9).map { String($0) }
2052
-
2053
- private func digitText(at index: Int, in wheel: Engine.Wheel) -> String? {
2286
+ private func digitText(at index: Int, in wheel: Engine.Wheel, modulus: Int, fonts: FontSet) -> String? {
2054
2287
  if wheel.linear && index < 0 { return nil }
2055
2288
  if wheel.blankZero && index == 0 { return nil }
2056
- return Self.digitStrings[((index % 10) + 10) % 10]
2289
+ return fonts.glyphs[((index % modulus) + modulus) % modulus]
2057
2290
  }
2058
2291
  }
2059
2292