react-native-nitro-input 0.2.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +87 -0
- package/README.md +132 -2
- package/android/CMakeLists.txt +4 -0
- package/android/gradle.properties +1 -1
- package/android/src/main/cpp/JReflowEngine.cpp +32 -0
- package/android/src/main/cpp/JReflowEngine.hpp +13 -0
- package/android/src/main/cpp/JRollingEngine.cpp +43 -2
- package/android/src/main/cpp/JRollingEngine.hpp +9 -1
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroInputView.kt +20 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroNumberFormatPlatform.kt +284 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroNumberView.kt +113 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroTextMeasure.kt +25 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroTextView.kt +140 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/NitroInputPackage.kt +2 -1
- package/android/src/main/java/com/margelo/nitro/nitroinput/NitroInputView.kt +69 -10
- package/android/src/main/java/com/margelo/nitro/nitroinput/NitroNumberView.kt +522 -130
- package/android/src/main/java/com/margelo/nitro/nitroinput/NitroTextView.kt +868 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/ReflowEngine.kt +21 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/RollingEngine.kt +14 -2
- package/cpp/RollingEngine.cpp +102 -8
- package/cpp/RollingEngine.hpp +44 -3
- package/cpp/numberformat/HybridNitroNumberFormat.cpp +498 -0
- package/cpp/numberformat/HybridNitroNumberFormat.hpp +74 -0
- package/cpp/numberformat/HybridNitroNumberFormatFactory.hpp +11 -0
- package/cpp/numberformat/NumberFormatCore.cpp +629 -0
- package/cpp/numberformat/NumberFormatCore.hpp +154 -0
- package/cpp/numberformat/NumberFormatProbe.cpp +354 -0
- package/cpp/numberformat/NumberFormatProbe.hpp +63 -0
- package/ios/HybridNitroInputView.swift +15 -0
- package/ios/HybridNitroNumberFormatPlatform.swift +426 -0
- package/ios/HybridNitroNumberView.swift +101 -1
- package/ios/HybridNitroTextView.swift +134 -0
- package/ios/NitroInputView.swift +79 -19
- package/ios/NitroNumberView.swift +520 -147
- package/ios/NitroTextView.swift +583 -0
- package/lib/commonjs/nitrogen/generated/shared/json/NitroInputViewConfig.json +5 -0
- package/lib/commonjs/nitrogen/generated/shared/json/NitroNumberViewConfig.json +26 -0
- package/lib/commonjs/nitrogen/generated/shared/json/NitroTextViewConfig.json +30 -0
- package/lib/commonjs/src/NitroInput.js +8 -6
- package/lib/commonjs/src/NitroNumber.js +54 -3
- package/lib/commonjs/src/NitroText.js +138 -0
- package/lib/commonjs/src/NitroTime.js +65 -0
- package/lib/commonjs/src/NumberFormat.js +525 -0
- package/lib/commonjs/src/formatProps.js +108 -0
- package/lib/commonjs/src/index.js +8 -1
- package/lib/commonjs/src/specs/NitroText.nitro.js +2 -0
- package/lib/commonjs/src/specs/NumberFormat.nitro.js +2 -0
- package/lib/module/nitrogen/generated/shared/json/NitroInputViewConfig.json +5 -0
- package/lib/module/nitrogen/generated/shared/json/NitroNumberViewConfig.json +26 -0
- package/lib/module/nitrogen/generated/shared/json/NitroTextViewConfig.json +30 -0
- package/lib/module/src/NitroInput.js +8 -6
- package/lib/module/src/NitroNumber.js +54 -3
- package/lib/module/src/NitroText.js +98 -0
- package/lib/module/src/NitroTime.js +29 -0
- package/lib/module/src/NumberFormat.js +521 -0
- package/lib/module/src/formatProps.js +104 -0
- package/lib/module/src/index.js +3 -0
- package/lib/module/src/specs/NitroText.nitro.js +1 -0
- package/lib/module/src/specs/NumberFormat.nitro.js +1 -0
- package/lib/typescript/src/NitroInput.d.ts +22 -0
- package/lib/typescript/src/NitroNumber.d.ts +115 -2
- package/lib/typescript/src/NitroText.d.ts +67 -0
- package/lib/typescript/src/NitroTime.d.ts +19 -0
- package/lib/typescript/src/NumberFormat.d.ts +47 -0
- package/lib/typescript/src/formatProps.d.ts +47 -0
- package/lib/typescript/src/index.d.ts +6 -1
- package/lib/typescript/src/specs/NitroInput.nitro.d.ts +10 -0
- package/lib/typescript/src/specs/NitroNumber.nitro.d.ts +80 -1
- package/lib/typescript/src/specs/NitroText.nitro.d.ts +60 -0
- package/lib/typescript/src/specs/NumberFormat.nitro.d.ts +139 -0
- package/nitro.json +36 -0
- package/nitrogen/generated/android/NitroInput+autolinking.cmake +12 -0
- package/nitrogen/generated/android/NitroInputOnLoad.cpp +62 -0
- package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.cpp +45 -0
- package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.hpp +10 -0
- package/nitrogen/generated/android/c++/JHybridNitroNumberFormatPlatformSpec.cpp +118 -0
- package/nitrogen/generated/android/c++/JHybridNitroNumberFormatPlatformSpec.hpp +64 -0
- package/nitrogen/generated/android/c++/JHybridNitroNumberViewSpec.cpp +297 -0
- package/nitrogen/generated/android/c++/JHybridNitroNumberViewSpec.hpp +52 -0
- package/nitrogen/generated/android/c++/JHybridNitroPlatformNumberFormatterSpec.cpp +65 -0
- package/nitrogen/generated/android/c++/JHybridNitroPlatformNumberFormatterSpec.hpp +64 -0
- package/nitrogen/generated/android/c++/JHybridNitroTextMeasureSpec.cpp +58 -0
- package/nitrogen/generated/android/c++/JHybridNitroTextMeasureSpec.hpp +64 -0
- package/nitrogen/generated/android/c++/JHybridNitroTextViewSpec.cpp +253 -0
- package/nitrogen/generated/android/c++/JHybridNitroTextViewSpec.hpp +104 -0
- package/nitrogen/generated/android/c++/JNitroNumberDirection.hpp +3 -0
- package/nitrogen/generated/android/c++/JNitroNumberShimmerDirection.hpp +61 -0
- package/nitrogen/generated/android/c++/JNitroNumberSignDisplay.hpp +67 -0
- package/nitrogen/generated/android/c++/JNumberFormatCompactDisplay.hpp +58 -0
- package/nitrogen/generated/android/c++/JNumberFormatCurrencyDisplay.hpp +64 -0
- package/nitrogen/generated/android/c++/JNumberFormatCurrencySign.hpp +58 -0
- package/nitrogen/generated/android/c++/JNumberFormatNotation.hpp +64 -0
- package/nitrogen/generated/android/c++/JNumberFormatPlatformOptions.hpp +161 -0
- package/nitrogen/generated/android/c++/JNumberFormatPlatformSymbols.hpp +89 -0
- package/nitrogen/generated/android/c++/JNumberFormatRoundingMode.hpp +79 -0
- package/nitrogen/generated/android/c++/JNumberFormatSignDisplay.hpp +67 -0
- package/nitrogen/generated/android/c++/JNumberFormatStyle.hpp +64 -0
- package/nitrogen/generated/android/c++/JNumberFormatUnitDisplay.hpp +61 -0
- package/nitrogen/generated/android/c++/views/JHybridNitroInputViewStateUpdater.cpp +25 -0
- package/nitrogen/generated/android/c++/views/JHybridNitroNumberViewStateUpdater.cpp +130 -0
- package/nitrogen/generated/android/c++/views/JHybridNitroTextViewStateUpdater.cpp +174 -0
- package/nitrogen/generated/android/c++/views/JHybridNitroTextViewStateUpdater.hpp +54 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroInputViewSpec.kt +30 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroNumberFormatPlatformSpec.kt +60 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroNumberViewSpec.kt +172 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroPlatformNumberFormatterSpec.kt +62 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroTextMeasureSpec.kt +60 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroTextViewSpec.kt +179 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroNumberDirection.kt +2 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroNumberShimmerDirection.kt +24 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroNumberSignDisplay.kt +26 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatCompactDisplay.kt +23 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatCurrencyDisplay.kt +25 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatCurrencySign.kt +23 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatNotation.kt +25 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatPlatformOptions.kt +136 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatPlatformSymbols.kt +91 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatRoundingMode.kt +30 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatSignDisplay.kt +26 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatStyle.kt +25 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatUnitDisplay.kt +24 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/views/HybridNitroTextViewManager.kt +96 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/views/HybridNitroTextViewStateUpdater.kt +23 -0
- package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Bridge.cpp +68 -0
- package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Bridge.hpp +184 -0
- package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Umbrella.hpp +56 -0
- package/nitrogen/generated/ios/NitroInputAutolinking.mm +34 -0
- package/nitrogen/generated/ios/NitroInputAutolinking.swift +36 -0
- package/nitrogen/generated/ios/c++/HybridNitroInputViewSpecSwift.hpp +30 -0
- package/nitrogen/generated/ios/c++/HybridNitroNumberFormatPlatformSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridNitroNumberFormatPlatformSpecSwift.hpp +122 -0
- package/nitrogen/generated/ios/c++/HybridNitroNumberViewSpecSwift.hpp +188 -0
- package/nitrogen/generated/ios/c++/HybridNitroPlatformNumberFormatterSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridNitroPlatformNumberFormatterSpecSwift.hpp +94 -0
- package/nitrogen/generated/ios/c++/HybridNitroTextMeasureSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridNitroTextMeasureSpecSwift.hpp +90 -0
- package/nitrogen/generated/ios/c++/HybridNitroTextViewSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridNitroTextViewSpecSwift.hpp +233 -0
- package/nitrogen/generated/ios/c++/views/HybridNitroInputViewComponent.mm +30 -0
- package/nitrogen/generated/ios/c++/views/HybridNitroNumberViewComponent.mm +156 -0
- package/nitrogen/generated/ios/c++/views/HybridNitroTextViewComponent.mm +264 -0
- package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec.swift +5 -0
- package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec_cxx.swift +55 -0
- package/nitrogen/generated/ios/swift/HybridNitroNumberFormatPlatformSpec.swift +56 -0
- package/nitrogen/generated/ios/swift/HybridNitroNumberFormatPlatformSpec_cxx.swift +159 -0
- package/nitrogen/generated/ios/swift/HybridNitroNumberViewSpec.swift +26 -0
- package/nitrogen/generated/ios/swift/HybridNitroNumberViewSpec_cxx.swift +658 -21
- package/nitrogen/generated/ios/swift/HybridNitroPlatformNumberFormatterSpec.swift +56 -0
- package/nitrogen/generated/ios/swift/HybridNitroPlatformNumberFormatterSpec_cxx.swift +155 -0
- package/nitrogen/generated/ios/swift/HybridNitroTextMeasureSpec.swift +56 -0
- package/nitrogen/generated/ios/swift/HybridNitroTextMeasureSpec_cxx.swift +150 -0
- package/nitrogen/generated/ios/swift/HybridNitroTextViewSpec.swift +75 -0
- package/nitrogen/generated/ios/swift/HybridNitroTextViewSpec_cxx.swift +608 -0
- package/nitrogen/generated/ios/swift/NitroNumberDirection.swift +4 -0
- package/nitrogen/generated/ios/swift/NitroNumberShimmerDirection.swift +44 -0
- package/nitrogen/generated/ios/swift/NitroNumberSignDisplay.swift +52 -0
- package/nitrogen/generated/ios/swift/NumberFormatCompactDisplay.swift +40 -0
- package/nitrogen/generated/ios/swift/NumberFormatCurrencyDisplay.swift +48 -0
- package/nitrogen/generated/ios/swift/NumberFormatCurrencySign.swift +40 -0
- package/nitrogen/generated/ios/swift/NumberFormatNotation.swift +48 -0
- package/nitrogen/generated/ios/swift/NumberFormatPlatformOptions.swift +159 -0
- package/nitrogen/generated/ios/swift/NumberFormatPlatformSymbols.swift +69 -0
- package/nitrogen/generated/ios/swift/NumberFormatRoundingMode.swift +68 -0
- package/nitrogen/generated/ios/swift/NumberFormatSignDisplay.swift +52 -0
- package/nitrogen/generated/ios/swift/NumberFormatStyle.swift +48 -0
- package/nitrogen/generated/ios/swift/NumberFormatUnitDisplay.swift +44 -0
- package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.cpp +10 -0
- package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.hpp +10 -0
- package/nitrogen/generated/shared/c++/HybridNitroNumberFormatFactorySpec.cpp +22 -0
- package/nitrogen/generated/shared/c++/HybridNitroNumberFormatFactorySpec.hpp +70 -0
- package/nitrogen/generated/shared/c++/HybridNitroNumberFormatPlatformSpec.cpp +22 -0
- package/nitrogen/generated/shared/c++/HybridNitroNumberFormatPlatformSpec.hpp +70 -0
- package/nitrogen/generated/shared/c++/HybridNitroNumberFormatSpec.cpp +23 -0
- package/nitrogen/generated/shared/c++/HybridNitroNumberFormatSpec.hpp +71 -0
- package/nitrogen/generated/shared/c++/HybridNitroNumberViewSpec.cpp +52 -0
- package/nitrogen/generated/shared/c++/HybridNitroNumberViewSpec.hpp +58 -0
- package/nitrogen/generated/shared/c++/HybridNitroPlatformNumberFormatterSpec.cpp +23 -0
- package/nitrogen/generated/shared/c++/HybridNitroPlatformNumberFormatterSpec.hpp +65 -0
- package/nitrogen/generated/shared/c++/HybridNitroTextMeasureSpec.cpp +22 -0
- package/nitrogen/generated/shared/c++/HybridNitroTextMeasureSpec.hpp +63 -0
- package/nitrogen/generated/shared/c++/HybridNitroTextViewSpec.cpp +62 -0
- package/nitrogen/generated/shared/c++/HybridNitroTextViewSpec.hpp +115 -0
- package/nitrogen/generated/shared/c++/NitroNumberDirection.hpp +4 -0
- package/nitrogen/generated/shared/c++/NitroNumberShimmerDirection.hpp +80 -0
- package/nitrogen/generated/shared/c++/NitroNumberSignDisplay.hpp +88 -0
- package/nitrogen/generated/shared/c++/NumberFormatCompactDisplay.hpp +76 -0
- package/nitrogen/generated/shared/c++/NumberFormatCurrencyDisplay.hpp +84 -0
- package/nitrogen/generated/shared/c++/NumberFormatCurrencySign.hpp +76 -0
- package/nitrogen/generated/shared/c++/NumberFormatGrouping.hpp +80 -0
- package/nitrogen/generated/shared/c++/NumberFormatNotation.hpp +84 -0
- package/nitrogen/generated/shared/c++/NumberFormatOptions.hpp +193 -0
- package/nitrogen/generated/shared/c++/NumberFormatPart.hpp +87 -0
- package/nitrogen/generated/shared/c++/NumberFormatPlatformOptions.hpp +176 -0
- package/nitrogen/generated/shared/c++/NumberFormatPlatformSymbols.hpp +115 -0
- package/nitrogen/generated/shared/c++/NumberFormatRoundingMode.hpp +104 -0
- package/nitrogen/generated/shared/c++/NumberFormatRoundingPriority.hpp +80 -0
- package/nitrogen/generated/shared/c++/NumberFormatSignDisplay.hpp +88 -0
- package/nitrogen/generated/shared/c++/NumberFormatStyle.hpp +84 -0
- package/nitrogen/generated/shared/c++/NumberFormatTrailingZeroDisplay.hpp +76 -0
- package/nitrogen/generated/shared/c++/NumberFormatUnitDisplay.hpp +80 -0
- package/nitrogen/generated/shared/c++/ResolvedNumberFormatOptions.hpp +197 -0
- package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.cpp +10 -0
- package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.hpp +15 -0
- package/nitrogen/generated/shared/c++/views/HybridNitroNumberViewComponent.cpp +52 -0
- package/nitrogen/generated/shared/c++/views/HybridNitroNumberViewComponent.hpp +80 -0
- package/nitrogen/generated/shared/c++/views/HybridNitroTextViewComponent.cpp +74 -0
- package/nitrogen/generated/shared/c++/views/HybridNitroTextViewComponent.hpp +149 -0
- package/nitrogen/generated/shared/json/NitroInputViewConfig.json +5 -0
- package/nitrogen/generated/shared/json/NitroNumberViewConfig.json +26 -0
- package/nitrogen/generated/shared/json/NitroTextViewConfig.json +30 -0
- package/package.json +2 -2
- package/src/NitroInput.tsx +42 -7
- package/src/NitroNumber.tsx +236 -12
- package/src/NitroText.tsx +166 -0
- package/src/NitroTime.tsx +68 -0
- package/src/NumberFormat.ts +563 -0
- package/src/formatProps.ts +149 -0
- package/src/index.ts +23 -0
- package/src/specs/NitroInput.nitro.ts +10 -0
- package/src/specs/NitroNumber.nitro.ts +82 -1
- package/src/specs/NitroText.nitro.ts +65 -0
- package/src/specs/NumberFormat.nitro.ts +145 -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 {
|
|
@@ -50,6 +59,25 @@ final class NitroNumberView: UIView {
|
|
|
50
59
|
var color: UIColor = .label
|
|
51
60
|
var prefixAlign: AffixAlign = .baseline
|
|
52
61
|
var suffixAlign: AffixAlign = .baseline
|
|
62
|
+
/// Points added after every glyph, as `Text`'s letterSpacing; an affix gets it in proportion to its size.
|
|
63
|
+
var letterSpacing: CGFloat = 0
|
|
64
|
+
/// Points between the prefix and the digits, and between the digits and the suffix, in place of the letter spacing there.
|
|
65
|
+
var prefixSpacing: CGFloat? = nil
|
|
66
|
+
var suffixSpacing: CGFloat? = nil
|
|
67
|
+
/// Points an affix is moved down (negative: up) after its alignment.
|
|
68
|
+
var prefixOffset: CGFloat = 0
|
|
69
|
+
var suffixOffset: CGFloat = 0
|
|
70
|
+
/// Every digit as wide as the widest (tabular figures, the default), or each at its own width.
|
|
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] = []
|
|
53
81
|
var adjustsFontSizeToFit: Bool = false
|
|
54
82
|
var minimumFontScale: CGFloat = 0.5
|
|
55
83
|
var allowFontScaling: Bool = false
|
|
@@ -61,7 +89,7 @@ final class NitroNumberView: UIView {
|
|
|
61
89
|
}
|
|
62
90
|
|
|
63
91
|
enum Direction: Int32 {
|
|
64
|
-
case auto = 0, up = 1, down = 2
|
|
92
|
+
case auto = 0, up = 1, down = 2, shortest = 3
|
|
65
93
|
}
|
|
66
94
|
|
|
67
95
|
struct Timing: Equatable {
|
|
@@ -87,6 +115,10 @@ final class NitroNumberView: UIView {
|
|
|
87
115
|
var revealStagger: TimeInterval = 0.2
|
|
88
116
|
/// Count style: how long the count pauses on each milestone.
|
|
89
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
|
|
90
122
|
}
|
|
91
123
|
|
|
92
124
|
enum RevealStyle: Int32 {
|
|
@@ -109,6 +141,16 @@ final class NitroNumberView: UIView {
|
|
|
109
141
|
var color: UIColor? = nil
|
|
110
142
|
/// Duration of one sweep across the number.
|
|
111
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
|
|
112
154
|
}
|
|
113
155
|
|
|
114
156
|
enum Alignment {
|
|
@@ -138,6 +180,15 @@ final class NitroNumberView: UIView {
|
|
|
138
180
|
change(Self.suffixText, suffixInk(oldValue.suffix), suffixInk(format.suffix))
|
|
139
181
|
change(Self.groupingText, oldValue.groupingSeparator, format.groupingSeparator)
|
|
140
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
|
+
}
|
|
141
192
|
}
|
|
142
193
|
// Played in a glyph-swap transition; snaps otherwise (see the engine).
|
|
143
194
|
engine.changeFormat(Int32(format.fractionDigits), Int32(format.minimumIntegerDigits), CACurrentMediaTime())
|
|
@@ -154,7 +205,8 @@ final class NitroNumberView: UIView {
|
|
|
154
205
|
static let suffixText = 1
|
|
155
206
|
static let groupingText = 2
|
|
156
207
|
static let decimalText = 3
|
|
157
|
-
|
|
208
|
+
static let signText = 4
|
|
209
|
+
private var leavingText: [String?] = [nil, nil, nil, nil, nil]
|
|
158
210
|
|
|
159
211
|
var typography = Typography() {
|
|
160
212
|
didSet {
|
|
@@ -172,6 +224,7 @@ final class NitroNumberView: UIView {
|
|
|
172
224
|
engine.setRevealTiming(timing.revealDuration, timing.revealBounce, timing.revealStyle.rawValue, timing.revealStagger)
|
|
173
225
|
engine.setRevealGrow(timing.revealGrow)
|
|
174
226
|
engine.setRevealMilestoneHold(timing.revealMilestoneHold)
|
|
227
|
+
engine.setContinuous(timing.continuous)
|
|
175
228
|
}
|
|
176
229
|
}
|
|
177
230
|
|
|
@@ -206,7 +259,7 @@ final class NitroNumberView: UIView {
|
|
|
206
259
|
var loading = false {
|
|
207
260
|
didSet {
|
|
208
261
|
guard loading != oldValue else { return }
|
|
209
|
-
|
|
262
|
+
applyReduceMotion()
|
|
210
263
|
engine.setLoading(loading, CACurrentMediaTime())
|
|
211
264
|
updateDisplayLinkNeed()
|
|
212
265
|
render()
|
|
@@ -223,6 +276,11 @@ final class NitroNumberView: UIView {
|
|
|
223
276
|
var onRevealEnd: (() -> Void)?
|
|
224
277
|
/// Called when a count-style reveal reaches a milestone (its index and value).
|
|
225
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
|
|
226
284
|
|
|
227
285
|
/// The value currently shown or being rolled towards.
|
|
228
286
|
var targetValue: Double { engine.targetValue() }
|
|
@@ -238,13 +296,15 @@ final class NitroNumberView: UIView {
|
|
|
238
296
|
}
|
|
239
297
|
/// The core starts at the glyphs' left edge instead of parked off-screen.
|
|
240
298
|
private static let shimmerSeed: CGFloat = 0.25
|
|
241
|
-
/// How far the top of the band leads the bottom, as a fraction of the height ("/" slant).
|
|
242
|
-
private static let shimmerSlant: CGFloat = 0.6
|
|
243
299
|
|
|
244
300
|
// MARK: - Fonts
|
|
245
301
|
|
|
246
302
|
private enum GlyphRole {
|
|
247
|
-
|
|
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
|
|
248
308
|
}
|
|
249
309
|
|
|
250
310
|
private struct GlyphKey: Hashable {
|
|
@@ -313,17 +373,42 @@ final class NitroNumberView: UIView {
|
|
|
313
373
|
let digit: UIFont
|
|
314
374
|
let prefix: UIFont
|
|
315
375
|
let suffix: UIFont
|
|
376
|
+
let fraction: UIFont
|
|
316
377
|
/// The text color resolved for the view's traits: the glyph images bake it
|
|
317
378
|
/// in, and `.label` is dynamic (see `traitCollectionDidChange`).
|
|
318
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
|
|
319
384
|
let prefixAlign: AffixAlign
|
|
320
385
|
let suffixAlign: AffixAlign
|
|
386
|
+
let fractionAlign: AffixAlign
|
|
387
|
+
/// The glyphs drawn for 0…9.
|
|
388
|
+
let glyphs: [String]
|
|
389
|
+
private let glyphsKey: String
|
|
390
|
+
/// Letter spacing after a digit / prefix / suffix glyph, and the spacing at the affix seams (nil: the letter spacing), scaled with the fonts.
|
|
391
|
+
let digitSpacing: CGFloat
|
|
392
|
+
let prefixLetterSpacing: CGFloat
|
|
393
|
+
let suffixLetterSpacing: CGFloat
|
|
394
|
+
let fractionLetterSpacing: CGFloat
|
|
395
|
+
let prefixSeam: CGFloat?
|
|
396
|
+
let suffixSeam: CGFloat?
|
|
397
|
+
let prefixOffset: CGFloat
|
|
398
|
+
let suffixOffset: CGFloat
|
|
321
399
|
/// Height of the line box (the digit font's line height).
|
|
322
400
|
let lineHeight: CGFloat
|
|
323
401
|
/// The numeric transition's blur radius at full blur, in line heights.
|
|
324
402
|
let numericBlur: CGFloat = NitroNumberView.numericBlur
|
|
325
403
|
/// Width of the widest digit glyph.
|
|
326
404
|
private(set) var digitWidth: CGFloat = 0
|
|
405
|
+
/// Each digit at its own advance instead of the widest's (`tabularNums={false}`).
|
|
406
|
+
let proportional: Bool
|
|
407
|
+
/// The advance of each digit 0…9.
|
|
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] = []
|
|
327
412
|
// Keyed by a value type rather than a concatenated string: a lookup on the
|
|
328
413
|
// frame path must not allocate.
|
|
329
414
|
private var glyphCache: [GlyphKey: NSAttributedString] = [:]
|
|
@@ -348,6 +433,11 @@ final class NitroNumberView: UIView {
|
|
|
348
433
|
let color: UInt32
|
|
349
434
|
let scale: CGFloat
|
|
350
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
|
|
351
441
|
}
|
|
352
442
|
private static var sharedImages: [ImageKey: UIImage] = [:]
|
|
353
443
|
/// Blurred glyphs, the digit's own colour (tint 0) and the flash's alike.
|
|
@@ -365,30 +455,82 @@ final class NitroNumberView: UIView {
|
|
|
365
455
|
private static var sharedStrips: [StripKey: UIImage] = [:]
|
|
366
456
|
private static let sharedCapacity = 512
|
|
367
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
|
+
}
|
|
368
477
|
private func fontTag(_ role: GlyphRole) -> String {
|
|
369
478
|
let font = self.font(for: role)
|
|
370
|
-
return "\(font.fontName)|\(font.pointSize)"
|
|
479
|
+
return "\(font.fontName)|\(font.pointSize)\(proportional ? "|p" : "")"
|
|
371
480
|
}
|
|
372
481
|
/// Pixel density the glyph images are rendered at.
|
|
373
482
|
let renderScale: CGFloat
|
|
374
|
-
/// Slots in a wheel strip: index -1 (blank) through 10 (the 0 after 9).
|
|
375
|
-
static let stripSlots = 12
|
|
376
483
|
|
|
377
484
|
init(_ t: Typography, traits: UITraitCollection) {
|
|
378
485
|
// The view's own display, not the main screen's (deprecated, and absent on visionOS).
|
|
379
486
|
renderScale = max(1, traits.displayScale)
|
|
380
487
|
let scale = NitroNumberView.systemFontMultiplier(t)
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
488
|
+
proportional = !t.tabularNums
|
|
489
|
+
digit = NitroNumberView.makeFont(size: t.fontSize * scale, weight: t.fontWeight, family: t.fontFamily, tabular: t.tabularNums)
|
|
490
|
+
prefix = NitroNumberView.makeFont(size: (t.prefixFontSize ?? t.fontSize) * scale, weight: t.fontWeight, family: t.fontFamily, tabular: t.tabularNums)
|
|
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)
|
|
384
493
|
color = t.color.resolvedColor(with: traits)
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
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)
|
|
388
501
|
prefixAlign = t.prefixAlign
|
|
389
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: "|")
|
|
506
|
+
digitSpacing = t.letterSpacing * scale
|
|
507
|
+
prefixLetterSpacing = t.letterSpacing * scale * (prefix.pointSize / digit.pointSize)
|
|
508
|
+
suffixLetterSpacing = t.letterSpacing * scale * (suffix.pointSize / digit.pointSize)
|
|
509
|
+
fractionLetterSpacing = t.letterSpacing * scale * (fraction.pointSize / digit.pointSize)
|
|
510
|
+
prefixSeam = t.prefixSpacing.map { $0 * scale }
|
|
511
|
+
suffixSeam = t.suffixSpacing.map { $0 * scale }
|
|
512
|
+
prefixOffset = t.prefixOffset * scale
|
|
513
|
+
suffixOffset = t.suffixOffset * scale
|
|
390
514
|
lineHeight = ceil(digit.lineHeight)
|
|
391
|
-
|
|
515
|
+
digitWidths = (0...9).map { width(of: glyphs[$0], role: .digit) }
|
|
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
|
|
392
534
|
}
|
|
393
535
|
|
|
394
536
|
func font(for role: GlyphRole) -> UIFont {
|
|
@@ -396,6 +538,7 @@ final class NitroNumberView: UIView {
|
|
|
396
538
|
case .digit: return digit
|
|
397
539
|
case .prefix: return prefix
|
|
398
540
|
case .suffix: return suffix
|
|
541
|
+
case .fraction: return fraction
|
|
399
542
|
}
|
|
400
543
|
}
|
|
401
544
|
|
|
@@ -404,7 +547,7 @@ final class NitroNumberView: UIView {
|
|
|
404
547
|
if let cached = glyphCache[key] { return cached }
|
|
405
548
|
let string = NSAttributedString(string: text, attributes: [
|
|
406
549
|
.font: font(for: role),
|
|
407
|
-
.foregroundColor: color,
|
|
550
|
+
.foregroundColor: color(for: role),
|
|
408
551
|
])
|
|
409
552
|
glyphCache[key] = string
|
|
410
553
|
return string
|
|
@@ -423,7 +566,7 @@ final class NitroNumberView: UIView {
|
|
|
423
566
|
/// rasterization pass per glyph. Profiling 24 rolling views showed the
|
|
424
567
|
/// per-glyph `NSAttributedString.draw` dominating the main thread.
|
|
425
568
|
func image(_ text: String, role: GlyphRole) -> UIImage? {
|
|
426
|
-
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)
|
|
427
570
|
if let cached = Self.sharedImages[key] { return cached }
|
|
428
571
|
let string = attributed(text, role: role)
|
|
429
572
|
let size = string.size()
|
|
@@ -451,7 +594,7 @@ final class NitroNumberView: UIView {
|
|
|
451
594
|
/// blur cross-faded, so it never shows a sharp core.
|
|
452
595
|
func blurredImage(_ text: String, role: GlyphRole = .digit, level: Int) -> UIImage? {
|
|
453
596
|
if level <= 0 || numericBlur <= 0 { return image(text, role: role) }
|
|
454
|
-
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)
|
|
455
598
|
if let cached = Self.sharedBlurred[key] { return cached }
|
|
456
599
|
guard let sharp = image(text, role: role), let image = blurred(sharp, level: level) else { return nil }
|
|
457
600
|
Self.sharedBlurred.insert(image, for: key)
|
|
@@ -460,19 +603,19 @@ final class NitroNumberView: UIView {
|
|
|
460
603
|
|
|
461
604
|
/// `blurredImage`'s twin for `tintedImage`, so a swapping glyph's change
|
|
462
605
|
/// flash blurs as the glyph does.
|
|
463
|
-
func tintedBlurredImage(_ text: String, tint: UIColor, level: Int) -> UIImage? {
|
|
464
|
-
if level <= 0 || numericBlur <= 0 { return tintedImage(text, tint: tint) }
|
|
465
|
-
let key = TintKey(image: ImageKey(font: fontTag(
|
|
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)
|
|
466
609
|
if let cached = Self.sharedBlurred[key] { return cached }
|
|
467
|
-
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 }
|
|
468
611
|
Self.sharedBlurred.insert(image, for: key)
|
|
469
612
|
return image
|
|
470
613
|
}
|
|
471
614
|
|
|
472
615
|
/// A swapping glyph's image at a blur level, in its own colour or the flash's.
|
|
473
|
-
func swapImage(_ text: String, level: Int, tint: UIColor?) -> UIImage? {
|
|
474
|
-
if let tint { return tintedBlurredImage(text, tint: tint, level: level) }
|
|
475
|
-
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)
|
|
476
619
|
}
|
|
477
620
|
|
|
478
621
|
private var blurKey: Int { Int((numericBlur * 1000).rounded()) }
|
|
@@ -535,10 +678,10 @@ final class NitroNumberView: UIView {
|
|
|
535
678
|
/// The glyph in another colour, for the change flash: the same raster as
|
|
536
679
|
/// `image` with the tint baked in, composited over the glyph at the flash's
|
|
537
680
|
/// opacity. Made once per font, density and tint.
|
|
538
|
-
func tintedImage(_ text: String, tint: UIColor) -> UIImage? {
|
|
539
|
-
let key = TintKey(image: ImageKey(font: fontTag(
|
|
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))
|
|
540
683
|
if let cached = Self.sharedTinted[key] { return cached }
|
|
541
|
-
let string = NSAttributedString(string: text, attributes: [.font:
|
|
684
|
+
let string = NSAttributedString(string: text, attributes: [.font: font(for: role), .foregroundColor: tint])
|
|
542
685
|
let size = string.size()
|
|
543
686
|
guard size.width > 0, size.height > 0 else { return nil }
|
|
544
687
|
let format = UIGraphicsImageRendererFormat()
|
|
@@ -560,53 +703,60 @@ final class NitroNumberView: UIView {
|
|
|
560
703
|
let tint: UInt32
|
|
561
704
|
}
|
|
562
705
|
private static var sharedTintedStrips: [TintedStripKey: UIImage] = [:]
|
|
563
|
-
func tintedStrip(blankZero: Bool, tint: UIColor) -> UIImage? {
|
|
564
|
-
let key = TintedStripKey(strip:
|
|
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))
|
|
565
708
|
if let cached = Self.sharedTintedStrips[key] { return cached }
|
|
566
|
-
guard
|
|
567
|
-
let format = UIGraphicsImageRendererFormat()
|
|
568
|
-
format.scale = renderScale
|
|
569
|
-
format.opaque = false
|
|
570
|
-
let size = CGSize(width: ceil(digitWidth), height: lineHeight * CGFloat(Self.stripSlots))
|
|
571
|
-
let image = UIGraphicsImageRenderer(size: size, format: format).image { _ in
|
|
572
|
-
for slot in 0..<Self.stripSlots {
|
|
573
|
-
let index = slot - 1
|
|
574
|
-
if index < 0 || (blankZero && index == 0) { continue }
|
|
575
|
-
let text = String(index % 10)
|
|
576
|
-
let w = width(of: text, role: .digit)
|
|
577
|
-
NSAttributedString(string: text, attributes: [.font: digit, .foregroundColor: tint])
|
|
578
|
-
.draw(at: CGPoint(x: (digitWidth - w) / 2, y: CGFloat(slot) * lineHeight))
|
|
579
|
-
}
|
|
580
|
-
}
|
|
709
|
+
guard let image = drawStrip(role: role, blankZero: blankZero, modulus: modulus, color: tint) else { return nil }
|
|
581
710
|
if Self.sharedTintedStrips.count >= 8 { Self.sharedTintedStrips.removeAll(keepingCapacity: true) }
|
|
582
711
|
Self.sharedTintedStrips[key] = image
|
|
583
712
|
return image
|
|
584
713
|
}
|
|
585
714
|
|
|
586
715
|
/// A wheel's whole digit strip as one image: slots for index -1 (blank) to
|
|
587
|
-
///
|
|
588
|
-
///
|
|
589
|
-
///
|
|
590
|
-
|
|
591
|
-
|
|
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)
|
|
592
723
|
if let cached = Self.sharedStrips[key] { return cached }
|
|
593
|
-
guard
|
|
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 }
|
|
594
744
|
let format = UIGraphicsImageRendererFormat()
|
|
595
745
|
format.scale = renderScale
|
|
596
746
|
format.opaque = false
|
|
597
|
-
let
|
|
598
|
-
let
|
|
599
|
-
|
|
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 {
|
|
600
752
|
let index = slot - 1
|
|
601
753
|
if index < 0 || (blankZero && index == 0) { continue }
|
|
602
|
-
let text =
|
|
603
|
-
let w = width(of: text, role:
|
|
604
|
-
|
|
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))
|
|
605
758
|
}
|
|
606
759
|
}
|
|
607
|
-
if Self.sharedStrips.count >= 64 { Self.sharedStrips.removeAll(keepingCapacity: true) }
|
|
608
|
-
Self.sharedStrips[key] = image
|
|
609
|
-
return image
|
|
610
760
|
}
|
|
611
761
|
|
|
612
762
|
/// How far `text`'s ink hangs below the baseline (0 for digits and capitals).
|
|
@@ -622,9 +772,34 @@ final class NitroNumberView: UIView {
|
|
|
622
772
|
|
|
623
773
|
/// Top of the glyph's line box for `role` drawing `text`, given the top of the digit line box.
|
|
624
774
|
func top(for role: GlyphRole, text: String, lineTop: CGFloat) -> CGFloat {
|
|
625
|
-
|
|
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
|
+
}
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
/// Letter spacing after a glyph of `role`.
|
|
784
|
+
func spacing(for role: GlyphRole) -> CGFloat {
|
|
785
|
+
switch role {
|
|
786
|
+
case .digit: return digitSpacing
|
|
787
|
+
case .prefix: return prefixLetterSpacing
|
|
788
|
+
case .suffix: return suffixLetterSpacing
|
|
789
|
+
case .fraction: return fractionLetterSpacing
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
private func alignedTop(for role: GlyphRole, text: String, lineTop: CGFloat) -> CGFloat {
|
|
626
794
|
let f = font(for: role)
|
|
627
|
-
|
|
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 {
|
|
628
803
|
case .baseline:
|
|
629
804
|
return lineTop + (digit.ascender - f.ascender)
|
|
630
805
|
case .center:
|
|
@@ -635,7 +810,7 @@ final class NitroNumberView: UIView {
|
|
|
635
810
|
// Pin the bottom of the ink, not of the line boxes: the digits' ink ends
|
|
636
811
|
// on the baseline, so "USD" sits on it too instead of hanging down to
|
|
637
812
|
// where a comma's tail reaches.
|
|
638
|
-
let affixBaseline = lineTop + digit.ascender + inkDescent(
|
|
813
|
+
let affixBaseline = lineTop + digit.ascender + inkDescent(glyphs.joined(), role: .digit) - inkDescent(text, role: role)
|
|
639
814
|
return affixBaseline - f.ascender
|
|
640
815
|
}
|
|
641
816
|
}
|
|
@@ -731,8 +906,8 @@ final class NitroNumberView: UIView {
|
|
|
731
906
|
var layer: CALayer
|
|
732
907
|
/// The strip (wheels) or the glyph's image (glyphs), inside `layer`.
|
|
733
908
|
var inner: CALayer
|
|
734
|
-
/// Wheels only: which strip
|
|
735
|
-
var
|
|
909
|
+
/// Wheels only: which strip the layer currently shows (`stripVariant`).
|
|
910
|
+
var stripVariant: Int
|
|
736
911
|
/// Wheels only: the numeric transition's layers, once the wheel has swapped,
|
|
737
912
|
/// on an unclipped layer of their own over the containers.
|
|
738
913
|
var swap: SwapLayers?
|
|
@@ -754,11 +929,11 @@ final class NitroNumberView: UIView {
|
|
|
754
929
|
var innerFrame = CGRect.null
|
|
755
930
|
var opacity: Float = -1
|
|
756
931
|
|
|
757
|
-
init(kind: LayerKind, layer: CALayer, inner: CALayer,
|
|
932
|
+
init(kind: LayerKind, layer: CALayer, inner: CALayer, stripVariant: Int, glyphTop: CGFloat) {
|
|
758
933
|
self.kind = kind
|
|
759
934
|
self.layer = layer
|
|
760
935
|
self.inner = inner
|
|
761
|
-
self.
|
|
936
|
+
self.stripVariant = stripVariant
|
|
762
937
|
self.glyphTop = glyphTop
|
|
763
938
|
}
|
|
764
939
|
}
|
|
@@ -854,6 +1029,7 @@ final class NitroNumberView: UIView {
|
|
|
854
1029
|
func release() {
|
|
855
1030
|
stopDisplayLink()
|
|
856
1031
|
engine.reset()
|
|
1032
|
+
moving = false
|
|
857
1033
|
clearSlots()
|
|
858
1034
|
wheelBuffer = []
|
|
859
1035
|
elementBuffer = []
|
|
@@ -882,7 +1058,8 @@ final class NitroNumberView: UIView {
|
|
|
882
1058
|
engine.reset()
|
|
883
1059
|
fontScale = 1
|
|
884
1060
|
lastReportedSize = .zero
|
|
885
|
-
leavingText = [nil, nil, nil, nil]
|
|
1061
|
+
leavingText = [nil, nil, nil, nil, nil]
|
|
1062
|
+
moving = false
|
|
886
1063
|
autoAnchor = nil
|
|
887
1064
|
awaitingLayout = false
|
|
888
1065
|
laidFrame = nil
|
|
@@ -893,6 +1070,7 @@ final class NitroNumberView: UIView {
|
|
|
893
1070
|
shimmer = Shimmer()
|
|
894
1071
|
alignment = .auto
|
|
895
1072
|
revealMilestones = []
|
|
1073
|
+
cellWidths.removeAll()
|
|
896
1074
|
// `onIntrinsicSizeChange`, `onRevealEnd` and `onRevealMilestone` are the
|
|
897
1075
|
// hybrid's wiring, not the element's props: they stay across recycling
|
|
898
1076
|
// (the hybrid clears its own callback props).
|
|
@@ -907,18 +1085,24 @@ final class NitroNumberView: UIView {
|
|
|
907
1085
|
/// Shows `value` immediately with continuously positioned wheels (odometer
|
|
908
1086
|
/// style). Cancels any running roll. Intended to be called every frame.
|
|
909
1087
|
func setValue(_ value: Double) {
|
|
1088
|
+
let sign = signBefore()
|
|
910
1089
|
engine.setValue(value)
|
|
1090
|
+
swapSignIfChanged(sign)
|
|
911
1091
|
reportIntrinsicSize()
|
|
912
1092
|
updateDisplayLinkNeed()
|
|
913
1093
|
render()
|
|
1094
|
+
updateMoving()
|
|
914
1095
|
}
|
|
915
1096
|
|
|
916
1097
|
/// Rolls every wheel to `value` (snaps on first show, duration 0 or Reduce Motion).
|
|
917
1098
|
func animate(to value: Double) {
|
|
918
|
-
|
|
1099
|
+
applyReduceMotion()
|
|
1100
|
+
let sign = signBefore()
|
|
919
1101
|
engine.animateTo(value, CACurrentMediaTime())
|
|
1102
|
+
swapSignIfChanged(sign)
|
|
920
1103
|
reportIntrinsicSize()
|
|
921
1104
|
updateDisplayLinkNeed()
|
|
1105
|
+
updateMoving()
|
|
922
1106
|
// A roll is rendered by the display link's next tick, within a frame; a
|
|
923
1107
|
// value that snapped (duration 0, Reduce Motion, nothing shown yet) has
|
|
924
1108
|
// no tick coming and is rendered now. Rendering here as well doubled the
|
|
@@ -930,7 +1114,9 @@ final class NitroNumberView: UIView {
|
|
|
930
1114
|
/// Shows the opening frame of a jackpot reveal for `value` ("$0.00" in the
|
|
931
1115
|
/// target's layout), waiting for `reveal(to:)`.
|
|
932
1116
|
func holdReveal(_ value: Double) {
|
|
1117
|
+
let sign = signBefore()
|
|
933
1118
|
engine.holdReveal(value)
|
|
1119
|
+
swapSignIfChanged(sign)
|
|
934
1120
|
reportIntrinsicSize()
|
|
935
1121
|
updateDisplayLinkNeed()
|
|
936
1122
|
render()
|
|
@@ -938,11 +1124,14 @@ final class NitroNumberView: UIView {
|
|
|
938
1124
|
|
|
939
1125
|
/// Counts up from 0 to `value` and lands with a pop (snaps under Reduce Motion).
|
|
940
1126
|
func reveal(to value: Double) {
|
|
941
|
-
|
|
1127
|
+
applyReduceMotion()
|
|
1128
|
+
let sign = signBefore()
|
|
942
1129
|
engine.reveal(value, CACurrentMediaTime())
|
|
1130
|
+
swapSignIfChanged(sign)
|
|
943
1131
|
reportIntrinsicSize()
|
|
944
1132
|
updateDisplayLinkNeed()
|
|
945
1133
|
render()
|
|
1134
|
+
updateMoving()
|
|
946
1135
|
reportMilestones(reachedBefore: 0)
|
|
947
1136
|
if !engine.isRevealing() {
|
|
948
1137
|
// Snapped (Reduce Motion / duration 0): the reveal is over before it began.
|
|
@@ -950,6 +1139,40 @@ final class NitroNumberView: UIView {
|
|
|
950
1139
|
}
|
|
951
1140
|
}
|
|
952
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
|
+
|
|
953
1176
|
/// Fires `onRevealMilestone` for every milestone reached since `reachedBefore`.
|
|
954
1177
|
private func reportMilestones(reachedBefore: Int) {
|
|
955
1178
|
let reached = Int(engine.revealMilestonesReached())
|
|
@@ -982,6 +1205,7 @@ final class NitroNumberView: UIView {
|
|
|
982
1205
|
onRevealEnd?()
|
|
983
1206
|
}
|
|
984
1207
|
}
|
|
1208
|
+
updateMoving()
|
|
985
1209
|
}
|
|
986
1210
|
|
|
987
1211
|
/// Keeps the display link alive only while the engine says something moves.
|
|
@@ -1026,6 +1250,7 @@ final class NitroNumberView: UIView {
|
|
|
1026
1250
|
|
|
1027
1251
|
private func rebuildFonts() {
|
|
1028
1252
|
fonts = FontSet(typography, traits: traitCollection)
|
|
1253
|
+
cellWidths.removeAll()
|
|
1029
1254
|
contentLayer.contentsScale = fonts.renderScale
|
|
1030
1255
|
fontScale = 1
|
|
1031
1256
|
// Every cached glyph image belongs to the old font set.
|
|
@@ -1047,11 +1272,14 @@ final class NitroNumberView: UIView {
|
|
|
1047
1272
|
DispatchQueue.main.async { [weak self] in
|
|
1048
1273
|
guard let self, self.timing.transition == .numeric else { return }
|
|
1049
1274
|
let tints = [self.flash.upColor, self.flash.downColor].compactMap { $0 }
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
for
|
|
1054
|
-
_ = self.fonts.
|
|
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
|
+
}
|
|
1055
1283
|
}
|
|
1056
1284
|
}
|
|
1057
1285
|
}
|
|
@@ -1079,7 +1307,7 @@ final class NitroNumberView: UIView {
|
|
|
1079
1307
|
return multiplier
|
|
1080
1308
|
}
|
|
1081
1309
|
|
|
1082
|
-
private static func makeFont(size: CGFloat, weight: CGFloat, family: String
|
|
1310
|
+
private static func makeFont(size: CGFloat, weight: CGFloat, family: String?, tabular: Bool) -> UIFont {
|
|
1083
1311
|
let uiWeight = uiWeight(weight)
|
|
1084
1312
|
let traits: [UIFontDescriptor.TraitKey: Any] = [.weight: uiWeight]
|
|
1085
1313
|
var base: UIFont
|
|
@@ -1094,12 +1322,14 @@ final class NitroNumberView: UIView {
|
|
|
1094
1322
|
} else {
|
|
1095
1323
|
base = UIFont.systemFont(ofSize: size, weight: uiWeight)
|
|
1096
1324
|
}
|
|
1097
|
-
// Tabular (monospaced) digits so every column has the same width
|
|
1325
|
+
// Tabular (monospaced) digits so every column has the same width, or the
|
|
1326
|
+
// font's proportional ones when each digit is laid out at its own width.
|
|
1327
|
+
let selector = tabular ? kMonospacedNumbersSelector : kProportionalNumbersSelector
|
|
1098
1328
|
let feature: [UIFontDescriptor.FeatureKey: Int]
|
|
1099
1329
|
if #available(iOS 15.0, *) {
|
|
1100
|
-
feature = [.type: kNumberSpacingType, .selector:
|
|
1330
|
+
feature = [.type: kNumberSpacingType, .selector: selector]
|
|
1101
1331
|
} else {
|
|
1102
|
-
feature = [.featureIdentifier: kNumberSpacingType, .typeIdentifier:
|
|
1332
|
+
feature = [.featureIdentifier: kNumberSpacingType, .typeIdentifier: selector]
|
|
1103
1333
|
}
|
|
1104
1334
|
let descriptor = base.fontDescriptor.addingAttributes([.featureSettings: [feature]])
|
|
1105
1335
|
return UIFont(descriptor: descriptor, size: size)
|
|
@@ -1135,6 +1365,11 @@ final class NitroNumberView: UIView {
|
|
|
1135
1365
|
var slot = -1
|
|
1136
1366
|
var fromText: String?
|
|
1137
1367
|
var anchor = 1
|
|
1368
|
+
/// Space after the cell (letter spacing, or an affix seam), scaled with it; not part of the glyph's box.
|
|
1369
|
+
var gap: CGFloat = 0
|
|
1370
|
+
/// Wheels: whose font draws it, the integer digits' or the fraction's.
|
|
1371
|
+
var role: GlyphRole = .digit
|
|
1372
|
+
var advance: CGFloat { width + gap }
|
|
1138
1373
|
}
|
|
1139
1374
|
|
|
1140
1375
|
/// Pulls the engine's wheels into `wheelBuffer`.
|
|
@@ -1191,36 +1426,132 @@ final class NitroNumberView: UIView {
|
|
|
1191
1426
|
affixBlocks = AffixBlocks(prefix: format.prefix, suffix: format.suffix, rtl: rtl)
|
|
1192
1427
|
}
|
|
1193
1428
|
let affixes = affixBlocks
|
|
1429
|
+
let signGlyph = engine.signPositive() ? format.plusSign : format.minusSign
|
|
1194
1430
|
// A swapping affix keeps to the digits' side; a separator is centred.
|
|
1195
1431
|
if rtl {
|
|
1196
1432
|
addGlyph(affixes.suffixInk, role: .suffix, factor: 1, slot: Self.suffixText, anchor: 1)
|
|
1197
1433
|
addGlyph(affixes.suffixGap, role: .suffix, factor: 1)
|
|
1198
1434
|
} else {
|
|
1199
1435
|
// Sign first, then the currency prefix: "-$1,234.50".
|
|
1200
|
-
addGlyph(
|
|
1436
|
+
addGlyph(signGlyph, role: .digit, factor: signFactor, slot: Self.signText, anchor: 1)
|
|
1201
1437
|
addGlyph(affixes.prefixInk, role: .prefix, factor: 1, slot: Self.prefixText, anchor: 1)
|
|
1202
1438
|
}
|
|
1203
1439
|
var power = wheels.count - 1
|
|
1204
1440
|
while power >= 0 {
|
|
1205
1441
|
let wheel = wheels[power]
|
|
1206
1442
|
if wheel.width > 0 {
|
|
1207
|
-
|
|
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))
|
|
1208
1446
|
}
|
|
1209
|
-
if power > fd, (power - fd
|
|
1447
|
+
if power > fd, Self.isGroupBoundary(power - fd, sizes: format.groupingSizes) {
|
|
1210
1448
|
addGlyph(format.groupingSeparator, role: .digit, factor: wheel.width, slot: Self.groupingText, anchor: 0)
|
|
1211
1449
|
}
|
|
1212
1450
|
if fd > 0, power == fd {
|
|
1213
|
-
addGlyph(format.decimalSeparator, role: .
|
|
1451
|
+
addGlyph(format.decimalSeparator, role: .fraction, factor: decimal, slot: Self.decimalText, anchor: 0)
|
|
1214
1452
|
}
|
|
1215
1453
|
power -= 1
|
|
1216
1454
|
}
|
|
1217
1455
|
if rtl {
|
|
1218
1456
|
addGlyph(affixes.prefixGap, role: .prefix, factor: 1)
|
|
1219
1457
|
addGlyph(affixes.prefixInk, role: .prefix, factor: 1, slot: Self.prefixText, anchor: -1)
|
|
1220
|
-
addGlyph(
|
|
1458
|
+
addGlyph(signGlyph, role: .digit, factor: signFactor, slot: Self.signText, anchor: -1)
|
|
1221
1459
|
} else {
|
|
1222
1460
|
addGlyph(affixes.suffixInk, role: .suffix, factor: 1, slot: Self.suffixText, anchor: -1)
|
|
1223
1461
|
}
|
|
1462
|
+
applySpacing(to: &elements, fonts: fonts)
|
|
1463
|
+
}
|
|
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
|
+
|
|
1476
|
+
/// A wheel's cell width at full size: the widest digit's with tabular
|
|
1477
|
+
/// figures; otherwise its target digit's, eased there once from where the
|
|
1478
|
+
/// column was on the wheel's own progress. Following the digits a wheel
|
|
1479
|
+
/// rolled past made every column pulse as a narrow 1 went by. Settled, and
|
|
1480
|
+
/// throughout a reveal (whose layout is the target's from the first frame),
|
|
1481
|
+
/// it is the target digit's.
|
|
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)
|
|
1485
|
+
let digit = max(0, min(9, Int(engine.targetDigit(Int32(power)))))
|
|
1486
|
+
let goal = widths[digit]
|
|
1487
|
+
if settled || engine.isRevealing() { return goal }
|
|
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
|
+
}
|
|
1494
|
+
if cell.digit != digit || wheel.progress < cell.progress {
|
|
1495
|
+
// Interrupted mid-change, it carries on from what it showed; from rest,
|
|
1496
|
+
// from the digit the wheel is leaving.
|
|
1497
|
+
cell.from = cell.digit >= 0 && cell.progress < 1 ? cell.shown : restingWidth(wheel, modulus: Int(engine.wheelModulus(Int32(power))), widths: widths, goal: goal)
|
|
1498
|
+
cell.digit = digit
|
|
1499
|
+
}
|
|
1500
|
+
cell.progress = wheel.progress
|
|
1501
|
+
cell.shown = cell.from + (goal - cell.from) * CGFloat(max(0, min(1, wheel.progress)))
|
|
1502
|
+
cellWidths[power] = cell
|
|
1503
|
+
return cell.shown
|
|
1504
|
+
}
|
|
1505
|
+
|
|
1506
|
+
/// The width of the digit a wheel shows at the start of a change (blank takes the target's).
|
|
1507
|
+
private func restingWidth(_ wheel: Engine.Wheel, modulus: Int, widths: [CGFloat], goal: CGFloat) -> CGFloat {
|
|
1508
|
+
let glyph: Int
|
|
1509
|
+
if wheel.blend < 1 {
|
|
1510
|
+
glyph = Int(wheel.fromGlyph)
|
|
1511
|
+
} else {
|
|
1512
|
+
glyph = Int((wheel.linear ? wheel.position : Self.wrap(wheel.position, modulus)).rounded()) % modulus
|
|
1513
|
+
}
|
|
1514
|
+
return glyph >= 0 ? widths[glyph % 10] : goal
|
|
1515
|
+
}
|
|
1516
|
+
|
|
1517
|
+
/// A proportional column's width through a change, by place value.
|
|
1518
|
+
private struct CellWidth {
|
|
1519
|
+
var from: CGFloat = 0
|
|
1520
|
+
var shown: CGFloat = 0
|
|
1521
|
+
var digit = -1
|
|
1522
|
+
var progress: Double = 1
|
|
1523
|
+
var role: GlyphRole = .digit
|
|
1524
|
+
}
|
|
1525
|
+
|
|
1526
|
+
private var cellWidths: [Int: CellWidth] = [:]
|
|
1527
|
+
|
|
1528
|
+
/// Letter spacing after every cell, and the affix seams: the space between
|
|
1529
|
+
/// the prefix and the digits and between the digits and the suffix.
|
|
1530
|
+
private func applySpacing(to elements: inout [Element], fonts: FontSet) {
|
|
1531
|
+
guard fonts.digitSpacing != 0 || fonts.prefixSeam != nil || fonts.suffixSeam != nil else { return }
|
|
1532
|
+
func role(_ e: Element) -> GlyphRole {
|
|
1533
|
+
if case .glyph(_, let r) = e.kind { return r }
|
|
1534
|
+
return .digit
|
|
1535
|
+
}
|
|
1536
|
+
for i in elements.indices {
|
|
1537
|
+
elements[i].gap = fonts.spacing(for: role(elements[i])) * CGFloat(elements[i].factor)
|
|
1538
|
+
}
|
|
1539
|
+
// The seam is the gap at the affix's edge that faces the digits (the far side in RTL).
|
|
1540
|
+
let rtl = isRTL
|
|
1541
|
+
if let seam = fonts.prefixSeam {
|
|
1542
|
+
if !rtl, let last = elements.lastIndex(where: { role($0) == .prefix }) {
|
|
1543
|
+
elements[last].gap = seam
|
|
1544
|
+
} else if rtl, let first = elements.firstIndex(where: { role($0) == .prefix }), first > 0 {
|
|
1545
|
+
elements[first - 1].gap = seam
|
|
1546
|
+
}
|
|
1547
|
+
}
|
|
1548
|
+
if let seam = fonts.suffixSeam {
|
|
1549
|
+
if !rtl, let first = elements.firstIndex(where: { role($0) == .suffix }), first > 0 {
|
|
1550
|
+
elements[first - 1].gap = seam
|
|
1551
|
+
} else if rtl, let last = elements.lastIndex(where: { role($0) == .suffix }) {
|
|
1552
|
+
elements[last].gap = seam
|
|
1553
|
+
}
|
|
1554
|
+
}
|
|
1224
1555
|
}
|
|
1225
1556
|
|
|
1226
1557
|
/// The layout direction this view is in. The hybrid sets
|
|
@@ -1297,10 +1628,10 @@ final class NitroNumberView: UIView {
|
|
|
1297
1628
|
let count = Int(engine.settledPowerCount())
|
|
1298
1629
|
settledWheels.removeAll(keepingCapacity: true)
|
|
1299
1630
|
for _ in 0..<count {
|
|
1300
|
-
settledWheels.append(Engine.Wheel(position: 0, width: 1, linear: false, blankZero: false, fromGlyph: -1, toGlyph: -1, blend: 1, fromAbove: true, focus: 1, grow: 1, blurOut: 1, flash: 0, flashUp: true))
|
|
1631
|
+
settledWheels.append(Engine.Wheel(position: 0, width: 1, linear: false, blankZero: false, fromGlyph: -1, toGlyph: -1, blend: 1, fromAbove: true, focus: 1, grow: 1, blurOut: 1, flash: 0, flashUp: true, progress: 1))
|
|
1301
1632
|
}
|
|
1302
1633
|
buildElements(into: &settledBuffer, wheels: settledWheels, signFactor: engine.settledNegative() ? 1 : 0, animated: false)
|
|
1303
|
-
return settledBuffer.reduce(CGFloat(0)) { $0 + $1.
|
|
1634
|
+
return settledBuffer.reduce(CGFloat(0)) { $0 + $1.advance }
|
|
1304
1635
|
}
|
|
1305
1636
|
|
|
1306
1637
|
/// Formats the target the way it is displayed, for VoiceOver.
|
|
@@ -1310,7 +1641,7 @@ final class NitroNumberView: UIView {
|
|
|
1310
1641
|
var power = Int(engine.settledPowerCount()) - 1
|
|
1311
1642
|
while power >= 0 {
|
|
1312
1643
|
digits += String(engine.targetDigit(Int32(power)))
|
|
1313
|
-
if power > fd, (power - fd
|
|
1644
|
+
if power > fd, Self.isGroupBoundary(power - fd, sizes: format.groupingSizes) {
|
|
1314
1645
|
digits += format.groupingSeparator
|
|
1315
1646
|
}
|
|
1316
1647
|
if fd > 0, power == fd {
|
|
@@ -1318,14 +1649,22 @@ final class NitroNumberView: UIView {
|
|
|
1318
1649
|
}
|
|
1319
1650
|
power -= 1
|
|
1320
1651
|
}
|
|
1321
|
-
|
|
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
|
|
1322
1655
|
}
|
|
1323
1656
|
|
|
1657
|
+
/// An `accessibilityLabel` the app set, which VoiceOver reads instead of the figure.
|
|
1658
|
+
private var explicitAccessibilityLabel: String?
|
|
1659
|
+
|
|
1324
1660
|
// VoiceOver reads the settled figure when it asks for it, so a value update
|
|
1325
1661
|
// (which can come every frame) formats nothing.
|
|
1326
1662
|
override var accessibilityLabel: String? {
|
|
1327
|
-
get {
|
|
1328
|
-
|
|
1663
|
+
get {
|
|
1664
|
+
if let explicit = explicitAccessibilityLabel, !explicit.isEmpty { return explicit }
|
|
1665
|
+
return engine.hasShownValue() ? accessibleText() : nil
|
|
1666
|
+
}
|
|
1667
|
+
set { explicitAccessibilityLabel = newValue }
|
|
1329
1668
|
}
|
|
1330
1669
|
|
|
1331
1670
|
override var accessibilityValue: String? {
|
|
@@ -1401,7 +1740,7 @@ final class NitroNumberView: UIView {
|
|
|
1401
1740
|
buildElements(into: &elementBuffer, wheels: wheelBuffer, signFactor: engine.signFactor())
|
|
1402
1741
|
let wheels = wheelBuffer
|
|
1403
1742
|
let elements = elementBuffer
|
|
1404
|
-
let total = elements.reduce(CGFloat(0)) { $0 + $1.
|
|
1743
|
+
let total = elements.reduce(CGFloat(0)) { $0 + $1.advance }
|
|
1405
1744
|
updateFontScale(contentWidth: total)
|
|
1406
1745
|
let placement = contentPlacement(total: total, lineHeight: fonts.lineHeight)
|
|
1407
1746
|
|
|
@@ -1421,7 +1760,13 @@ final class NitroNumberView: UIView {
|
|
|
1421
1760
|
// still opening or closing is not cut to its width (the glyph read as a
|
|
1422
1761
|
// sliver of its right edge, a ")" of a 0 rolling past); the glyph
|
|
1423
1762
|
// overhangs, faded (`openingOpacity`). Vertically it is the roll's window.
|
|
1424
|
-
|
|
1763
|
+
var mask = CGRect(x: x + element.width - element.fullWidth, y: 0, width: element.fullWidth, height: fonts.lineHeight)
|
|
1764
|
+
if case .wheel = element.kind {
|
|
1765
|
+
// A digit column is the widest digit wide, centred on the digit's own
|
|
1766
|
+
// cell (with tabular figures the two are the same, right-aligned).
|
|
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)
|
|
1769
|
+
}
|
|
1425
1770
|
if slot.frame != mask {
|
|
1426
1771
|
slot.layer.frame = mask
|
|
1427
1772
|
slot.overlay?.frame = frame
|
|
@@ -1455,20 +1800,26 @@ final class NitroNumberView: UIView {
|
|
|
1455
1800
|
slots[i].overlay = overlay
|
|
1456
1801
|
slots[i].swap = swap
|
|
1457
1802
|
}
|
|
1458
|
-
layoutSwap(swap, wheel: wheel, fonts: fonts, cellWidth: element.width, 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))
|
|
1459
1804
|
} else if let swap = slot.swap, !swap.hidden {
|
|
1460
1805
|
for layer in swap.all { layer.isHidden = true }
|
|
1461
1806
|
swap.hidden = true
|
|
1462
1807
|
}
|
|
1463
1808
|
if strip.isHidden != swapping { strip.isHidden = swapping }
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
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
|
|
1467
1818
|
}
|
|
1468
|
-
// Linear strips run from -1 (blank) to 9; a roll can be any real, so wrap it onto
|
|
1469
|
-
let position = wheel.linear ? wheel.position : Self.
|
|
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)
|
|
1470
1821
|
let stripFrame = CGRect(
|
|
1471
|
-
x:
|
|
1822
|
+
x: 0,
|
|
1472
1823
|
y: -(position + 1) * fonts.lineHeight,
|
|
1473
1824
|
width: strip.bounds.width,
|
|
1474
1825
|
height: strip.bounds.height
|
|
@@ -1477,7 +1828,7 @@ final class NitroNumberView: UIView {
|
|
|
1477
1828
|
strip.frame = stripFrame
|
|
1478
1829
|
slots[i].innerFrame = stripFrame
|
|
1479
1830
|
}
|
|
1480
|
-
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)
|
|
1481
1832
|
case .glyph(let text, let role):
|
|
1482
1833
|
if let from = element.fromText {
|
|
1483
1834
|
// Swapping its text: the overlay's pair of pairs instead of the image.
|
|
@@ -1495,7 +1846,7 @@ final class NitroNumberView: UIView {
|
|
|
1495
1846
|
slots[i].textSwap = swap
|
|
1496
1847
|
}
|
|
1497
1848
|
layoutTextSwap(swap, element: element, from: from, to: text, role: role, fonts: fonts)
|
|
1498
|
-
x += element.
|
|
1849
|
+
x += element.advance
|
|
1499
1850
|
continue
|
|
1500
1851
|
}
|
|
1501
1852
|
if slot.layer.isHidden { slot.layer.isHidden = false }
|
|
@@ -1520,7 +1871,7 @@ final class NitroNumberView: UIView {
|
|
|
1520
1871
|
slots[i].innerFrame = imageFrame
|
|
1521
1872
|
}
|
|
1522
1873
|
}
|
|
1523
|
-
x += element.
|
|
1874
|
+
x += element.advance
|
|
1524
1875
|
}
|
|
1525
1876
|
}
|
|
1526
1877
|
|
|
@@ -1545,15 +1896,15 @@ final class NitroNumberView: UIView {
|
|
|
1545
1896
|
/// cross-faded with its blurred image as it goes out of, or comes into,
|
|
1546
1897
|
/// focus. With a change flash on (`tint`), the same two again in the tint
|
|
1547
1898
|
/// at `flash` of their opacity, over them: the ink mixed towards the tint.
|
|
1548
|
-
private func layoutSwap(_ swap: SwapLayers, wheel: Engine.Wheel, fonts: FontSet, cellWidth: 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) {
|
|
1549
1900
|
// The position clock overshoots 1 (a spring); only the offsets follow it there.
|
|
1550
1901
|
let b = CGFloat(wheel.blend)
|
|
1551
1902
|
let g = max(0, min(1, CGFloat(wheel.grow)))
|
|
1552
1903
|
let f = max(0, min(1, CGFloat(wheel.focus)))
|
|
1553
1904
|
let d: CGFloat = wheel.fromAbove ? 1 : -1
|
|
1554
1905
|
let offset = fonts.lineHeight * Self.numericOffset
|
|
1555
|
-
// The digit
|
|
1556
|
-
let center = CGPoint(x: cellWidth -
|
|
1906
|
+
// The digit sits right-aligned in its cell (the cell shrinks and grows from the left).
|
|
1907
|
+
let center = CGPoint(x: cellWidth - advance / 2, y: fonts.digitCenterY(for: role))
|
|
1557
1908
|
let outGlyph = Int(wheel.fromGlyph)
|
|
1558
1909
|
let outCenter = CGPoint(x: center.x, y: center.y + d * offset * b)
|
|
1559
1910
|
let outScale = 1 - (1 - Self.numericScale) * g
|
|
@@ -1564,11 +1915,11 @@ final class NitroNumberView: UIView {
|
|
|
1564
1915
|
let inScale = Self.numericScale + (1 - Self.numericScale) * g
|
|
1565
1916
|
let inAlpha = g
|
|
1566
1917
|
let inBlur = 1 - f
|
|
1567
|
-
placeSwapGlyph(swap.leaving, glyph: outGlyph, fonts: fonts, tint: nil, center: outCenter, scale: outScale, alpha: outAlpha, blur: outBlur)
|
|
1568
|
-
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)
|
|
1569
1920
|
let tintAlpha = tint == nil ? 0 : flash
|
|
1570
|
-
placeSwapGlyph(swap.leavingTint, glyph: outGlyph, fonts: fonts, tint: tint, center: outCenter, scale: outScale, alpha: outAlpha * tintAlpha, blur: outBlur)
|
|
1571
|
-
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)
|
|
1572
1923
|
swap.hidden = false
|
|
1573
1924
|
}
|
|
1574
1925
|
|
|
@@ -1635,7 +1986,7 @@ final class NitroNumberView: UIView {
|
|
|
1635
1986
|
/// `numericBlurLevels`, level 0 sharp), cross-faded by where between them
|
|
1636
1987
|
/// it falls, so a glyph half out of focus is half-blurred rather than a
|
|
1637
1988
|
/// sharp glyph half-hidden in a fully blurred one.
|
|
1638
|
-
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) {
|
|
1639
1990
|
guard glyph >= 0, alpha > 0.002 else {
|
|
1640
1991
|
if !pair.0.layer.isHidden { pair.0.layer.isHidden = true }
|
|
1641
1992
|
if !pair.1.layer.isHidden { pair.1.layer.isHidden = true }
|
|
@@ -1654,7 +2005,7 @@ final class NitroNumberView: UIView {
|
|
|
1654
2005
|
let hiAlpha = alpha * w
|
|
1655
2006
|
let loAlpha = hiAlpha >= 0.999 ? 0 : alpha * (1 - w) / (1 - hiAlpha)
|
|
1656
2007
|
let tintKey = tint.map { FontSet.key(of: $0) } ?? 0
|
|
1657
|
-
let text =
|
|
2008
|
+
let text = fonts.glyphs[glyph % 10]
|
|
1658
2009
|
let transform = CATransform3DMakeScale(scale, scale, 1)
|
|
1659
2010
|
// A local function, not a loop over an array literal: that allocated an array per call.
|
|
1660
2011
|
func place(_ slot: GlyphLayer, _ level: Int, _ layerAlpha: CGFloat) {
|
|
@@ -1663,7 +2014,7 @@ final class NitroNumberView: UIView {
|
|
|
1663
2014
|
if layer.isHidden != hidden { layer.isHidden = hidden }
|
|
1664
2015
|
if hidden { return }
|
|
1665
2016
|
if slot.glyph != glyph || slot.level != level || slot.tint != tintKey {
|
|
1666
|
-
if let image = fonts.swapImage(text, level: level, tint: tint) {
|
|
2017
|
+
if let image = fonts.swapImage(text, role: role, level: level, tint: tint) {
|
|
1667
2018
|
layer.contents = image.cgImage
|
|
1668
2019
|
layer.bounds = CGRect(origin: .zero, size: image.size)
|
|
1669
2020
|
}
|
|
@@ -1684,7 +2035,7 @@ final class NitroNumberView: UIView {
|
|
|
1684
2035
|
/// down colour at the flash's opacity, so the tint rides the roll instead
|
|
1685
2036
|
/// of hovering over it. A swapping wheel's flash is its swap layers' tinted
|
|
1686
2037
|
/// twins (`layoutSwap`), so `tint` is nil for it.
|
|
1687
|
-
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) {
|
|
1688
2039
|
guard let tint, !stripFrame.isNull else {
|
|
1689
2040
|
if let layer = slots[i].flashLayer, !layer.isHidden { layer.isHidden = true }
|
|
1690
2041
|
return
|
|
@@ -1700,9 +2051,9 @@ final class NitroNumberView: UIView {
|
|
|
1700
2051
|
}
|
|
1701
2052
|
let tintKey = FontSet.key(of: tint)
|
|
1702
2053
|
// The strip's own tinted twin, keyed as a glyph the digits can never be.
|
|
1703
|
-
let stripGlyph = 100 + (wheel.blankZero
|
|
2054
|
+
let stripGlyph = 100 + Self.stripVariant(role: role, blankZero: wheel.blankZero, modulus: modulus)
|
|
1704
2055
|
if slots[i].flashGlyph != stripGlyph || slots[i].flashTint != tintKey {
|
|
1705
|
-
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 }
|
|
1706
2057
|
layer.contents = image.cgImage
|
|
1707
2058
|
slots[i].flashGlyph = stripGlyph
|
|
1708
2059
|
slots[i].flashTint = tintKey
|
|
@@ -1726,13 +2077,14 @@ final class NitroNumberView: UIView {
|
|
|
1726
2077
|
switch element.kind {
|
|
1727
2078
|
case .wheel(let index):
|
|
1728
2079
|
let blankZero = wheels[index].blankZero
|
|
1729
|
-
|
|
2080
|
+
let modulus = Int(engine.wheelModulus(Int32(index)))
|
|
2081
|
+
if let strip = fonts.strip(role: element.role, blankZero: blankZero, modulus: modulus) {
|
|
1730
2082
|
inner.contents = strip.cgImage
|
|
1731
2083
|
inner.bounds = CGRect(origin: .zero, size: strip.size)
|
|
1732
2084
|
}
|
|
1733
2085
|
container.addSublayer(inner)
|
|
1734
2086
|
contentLayer.addSublayer(container)
|
|
1735
|
-
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))
|
|
1736
2088
|
case .glyph(let text, let role):
|
|
1737
2089
|
if let image = fonts.image(text, role: role) {
|
|
1738
2090
|
inner.contents = image.cgImage
|
|
@@ -1740,7 +2092,7 @@ final class NitroNumberView: UIView {
|
|
|
1740
2092
|
}
|
|
1741
2093
|
container.addSublayer(inner)
|
|
1742
2094
|
contentLayer.addSublayer(container)
|
|
1743
|
-
slots.append(ElementLayer(kind: .glyph(text, role), layer: container, inner: inner,
|
|
2095
|
+
slots.append(ElementLayer(kind: .glyph(text, role), layer: container, inner: inner, stripVariant: -1, glyphTop: fonts.top(for: role, text: text, lineTop: 0)))
|
|
1744
2096
|
}
|
|
1745
2097
|
}
|
|
1746
2098
|
}
|
|
@@ -1762,9 +2114,20 @@ final class NitroNumberView: UIView {
|
|
|
1762
2114
|
return true
|
|
1763
2115
|
}
|
|
1764
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
|
+
|
|
1765
2122
|
private static func wrap10(_ position: Double) -> Double {
|
|
1766
|
-
|
|
1767
|
-
|
|
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
|
|
1768
2131
|
}
|
|
1769
2132
|
|
|
1770
2133
|
/// Where the content box (`total` × `lineHeight`, in font space) goes and how
|
|
@@ -1796,7 +2159,7 @@ final class NitroNumberView: UIView {
|
|
|
1796
2159
|
buildElements(into: &elementBuffer, wheels: wheelBuffer, signFactor: engine.signFactor())
|
|
1797
2160
|
let wheels = wheelBuffer
|
|
1798
2161
|
let elements = elementBuffer
|
|
1799
|
-
let total = elements.reduce(CGFloat(0)) { $0 + $1.
|
|
2162
|
+
let total = elements.reduce(CGFloat(0)) { $0 + $1.advance }
|
|
1800
2163
|
updateFontScale(contentWidth: total)
|
|
1801
2164
|
let placement = contentPlacement(total: total, lineHeight: fonts.lineHeight)
|
|
1802
2165
|
|
|
@@ -1814,11 +2177,11 @@ final class NitroNumberView: UIView {
|
|
|
1814
2177
|
for element in elements {
|
|
1815
2178
|
switch element.kind {
|
|
1816
2179
|
case .wheel(let index):
|
|
1817
|
-
drawWheel(wheels[index], fonts: fonts, x: x, width: element.width, 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)
|
|
1818
2181
|
case .glyph(let text, let role):
|
|
1819
2182
|
drawGlyph(text, role: role, fonts: fonts, x: x, width: element.width, fullWidth: element.fullWidth, alpha: element.factor, ctx: ctx)
|
|
1820
2183
|
}
|
|
1821
|
-
x += element.
|
|
2184
|
+
x += element.advance
|
|
1822
2185
|
}
|
|
1823
2186
|
if dim > 0 {
|
|
1824
2187
|
drawShimmer(contentWidth: total, dim: dim, ctx: ctx)
|
|
@@ -1833,23 +2196,30 @@ final class NitroNumberView: UIView {
|
|
|
1833
2196
|
private func drawShimmer(contentWidth: CGFloat, dim: CGFloat, ctx: CGContext) {
|
|
1834
2197
|
guard contentWidth > 0 else { return }
|
|
1835
2198
|
var br: CGFloat = 0, bg: CGFloat = 0, bb: CGFloat = 0, ba: CGFloat = 1
|
|
1836
|
-
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)
|
|
1837
2200
|
var hr: CGFloat = 0, hg: CGFloat = 0, hb: CGFloat = 0, ha: CGFloat = 1
|
|
1838
|
-
(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)
|
|
1839
2202
|
let base = CGColor(red: br, green: bg, blue: bb, alpha: ba)
|
|
1840
2203
|
let colors = [base, CGColor(red: hr, green: hg, blue: hb, alpha: ha), base] as CFArray
|
|
1841
2204
|
guard let gradient = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: colors, locations: [0.1, 0.5, 0.9]) else { return }
|
|
1842
|
-
|
|
1843
|
-
let
|
|
1844
|
-
|
|
1845
|
-
let
|
|
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)
|
|
1846
2216
|
ctx.saveGState()
|
|
1847
2217
|
ctx.setBlendMode(.sourceAtop)
|
|
1848
2218
|
ctx.setAlpha(dim)
|
|
1849
2219
|
ctx.drawLinearGradient(
|
|
1850
2220
|
gradient,
|
|
1851
2221
|
start: CGPoint(x: startX, y: 0),
|
|
1852
|
-
end: CGPoint(x: startX +
|
|
2222
|
+
end: CGPoint(x: startX + length, y: slant * length),
|
|
1853
2223
|
options: [.drawsBeforeStartLocation, .drawsAfterEndLocation]
|
|
1854
2224
|
)
|
|
1855
2225
|
ctx.restoreGState()
|
|
@@ -1865,12 +2235,16 @@ final class NitroNumberView: UIView {
|
|
|
1865
2235
|
ctx.restoreGState()
|
|
1866
2236
|
}
|
|
1867
2237
|
|
|
1868
|
-
private func drawWheel(_ wheel: Engine.Wheel, fonts: FontSet, x: CGFloat, width: CGFloat, ctx: CGContext) {
|
|
2238
|
+
private func drawWheel(_ wheel: Engine.Wheel, role: GlyphRole, modulus: Int, fonts: FontSet, x: CGFloat, width: CGFloat, advance: CGFloat, ctx: CGContext) {
|
|
1869
2239
|
guard width > 0 else { return }
|
|
1870
2240
|
let lineHeight = fonts.lineHeight
|
|
2241
|
+
let column = fonts.digitWidth(for: role)
|
|
2242
|
+
// The digit column, the widest digit wide, centred on the digit's own cell.
|
|
2243
|
+
let columnLeft = x + width - (advance + column) / 2
|
|
2244
|
+
let top = role == .fraction ? fonts.top(for: .fraction, text: fonts.glyphs[0], lineTop: 0) : 0
|
|
1871
2245
|
ctx.saveGState()
|
|
1872
2246
|
// The roll's window, the whole digit wide (see `renderLayers`).
|
|
1873
|
-
ctx.clip(to: CGRect(x:
|
|
2247
|
+
ctx.clip(to: CGRect(x: columnLeft, y: 0, width: column, height: lineHeight))
|
|
1874
2248
|
ctx.setAlpha(CGFloat(Self.openingOpacity(wheel.width)))
|
|
1875
2249
|
if wheel.blend < 1 || wheel.focus < 1 || wheel.grow < 1 {
|
|
1876
2250
|
// The numeric transition under the glint: the pair, without the blur.
|
|
@@ -1878,16 +2252,17 @@ final class NitroNumberView: UIView {
|
|
|
1878
2252
|
let g = max(0, min(1, CGFloat(wheel.grow)))
|
|
1879
2253
|
let d: CGFloat = wheel.fromAbove ? 1 : -1
|
|
1880
2254
|
let offset = lineHeight * Self.numericOffset
|
|
1881
|
-
let cx =
|
|
2255
|
+
let cx = columnLeft + column / 2
|
|
2256
|
+
let cy = fonts.digitCenterY(for: role)
|
|
1882
2257
|
let pair: [(glyph: Int, dy: CGFloat, scale: CGFloat, alpha: CGFloat)] = [
|
|
1883
2258
|
(Int(wheel.fromGlyph), d * offset * b, 1 - (1 - Self.numericScale) * g, 1 - g),
|
|
1884
2259
|
(Int(wheel.toGlyph), -d * offset * (1 - b), Self.numericScale + (1 - Self.numericScale) * g, g),
|
|
1885
2260
|
]
|
|
1886
2261
|
for item in pair where item.glyph >= 0 && item.alpha > 0.002 {
|
|
1887
|
-
guard let image = fonts.image(
|
|
2262
|
+
guard let image = fonts.image(fonts.glyphs[item.glyph % 10], role: role) else { continue }
|
|
1888
2263
|
ctx.saveGState()
|
|
1889
2264
|
ctx.setAlpha(CGFloat(wheel.width) * item.alpha)
|
|
1890
|
-
ctx.translateBy(x: cx, y:
|
|
2265
|
+
ctx.translateBy(x: cx, y: cy + item.dy)
|
|
1891
2266
|
ctx.scaleBy(x: item.scale, y: item.scale)
|
|
1892
2267
|
image.draw(at: CGPoint(x: -image.size.width / 2, y: -image.size.height / 2))
|
|
1893
2268
|
ctx.restoreGState()
|
|
@@ -1895,25 +2270,23 @@ final class NitroNumberView: UIView {
|
|
|
1895
2270
|
ctx.restoreGState()
|
|
1896
2271
|
return
|
|
1897
2272
|
}
|
|
1898
|
-
let
|
|
1899
|
-
let
|
|
2273
|
+
let position = wheel.linear ? wheel.position : Self.wrap(wheel.position, modulus)
|
|
2274
|
+
let base = position.rounded(.down)
|
|
2275
|
+
let fraction = CGFloat(position - base)
|
|
1900
2276
|
let index = Int(base)
|
|
1901
|
-
let
|
|
1902
|
-
|
|
1903
|
-
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))
|
|
1904
2279
|
}
|
|
1905
|
-
if fraction > 0.0001, let text = digitText(at: index + 1, in: wheel), let image = fonts.image(text, role:
|
|
1906
|
-
image.draw(at: CGPoint(x: columnLeft + (
|
|
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))
|
|
1907
2282
|
}
|
|
1908
2283
|
ctx.restoreGState()
|
|
1909
2284
|
}
|
|
1910
2285
|
|
|
1911
|
-
private
|
|
1912
|
-
|
|
1913
|
-
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? {
|
|
1914
2287
|
if wheel.linear && index < 0 { return nil }
|
|
1915
2288
|
if wheel.blankZero && index == 0 { return nil }
|
|
1916
|
-
return
|
|
2289
|
+
return fonts.glyphs[((index % modulus) + modulus) % modulus]
|
|
1917
2290
|
}
|
|
1918
2291
|
}
|
|
1919
2292
|
|