react-native-nitro-input 0.3.8 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. package/CHANGELOG.md +105 -0
  2. package/README.md +6 -1
  3. package/android/src/main/java/com/margelo/nitro/nitroinput/DigitSlots.kt +143 -0
  4. package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroInputMeasure.kt +52 -0
  5. package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroInputView.kt +80 -80
  6. package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroNumberView.kt +18 -18
  7. package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroTextView.kt +14 -14
  8. package/android/src/main/java/com/margelo/nitro/nitroinput/InputFonts.kt +244 -0
  9. package/android/src/main/java/com/margelo/nitro/nitroinput/InputSizing.kt +404 -0
  10. package/android/src/main/java/com/margelo/nitro/nitroinput/NitroInputView.kt +168 -338
  11. package/android/src/main/java/com/margelo/nitro/nitroinput/NitroNumberView.kt +178 -30
  12. package/ios/HybridNitroInputView.swift +73 -75
  13. package/ios/HybridNitroNumberView.swift +18 -18
  14. package/ios/HybridNitroTextView.swift +14 -14
  15. package/ios/InputSizing.swift +628 -0
  16. package/ios/NitroInputView.swift +180 -209
  17. package/ios/NitroNumberView.swift +98 -23
  18. package/lib/commonjs/nitrogen/generated/shared/json/NitroInputViewConfig.json +14 -8
  19. package/lib/commonjs/nitrogen/generated/shared/json/NitroNumberViewConfig.json +6 -6
  20. package/lib/commonjs/nitrogen/generated/shared/json/NitroTextViewConfig.json +6 -6
  21. package/lib/commonjs/src/NitroInput.js +166 -23
  22. package/lib/commonjs/src/NitroNumber.js +1 -1
  23. package/lib/commonjs/src/NitroText.js +6 -6
  24. package/lib/commonjs/src/specs/NitroInputMeasure.nitro.js +2 -0
  25. package/lib/module/nitrogen/generated/shared/json/NitroInputViewConfig.json +14 -8
  26. package/lib/module/nitrogen/generated/shared/json/NitroNumberViewConfig.json +6 -6
  27. package/lib/module/nitrogen/generated/shared/json/NitroTextViewConfig.json +6 -6
  28. package/lib/module/src/NitroInput.js +167 -24
  29. package/lib/module/src/NitroNumber.js +1 -1
  30. package/lib/module/src/NitroText.js +6 -6
  31. package/lib/module/src/specs/NitroInputMeasure.nitro.js +1 -0
  32. package/lib/typescript/src/NitroInput.d.ts +20 -16
  33. package/lib/typescript/src/specs/NitroInput.nitro.d.ts +21 -8
  34. package/lib/typescript/src/specs/NitroInputMeasure.nitro.d.ts +69 -0
  35. package/lib/typescript/src/specs/NitroNumber.nitro.d.ts +6 -6
  36. package/lib/typescript/src/specs/NitroText.nitro.d.ts +6 -6
  37. package/nitro.json +10 -0
  38. package/nitrogen/generated/android/NitroInput+autolinking.cmake +2 -0
  39. package/nitrogen/generated/android/NitroInputOnLoad.cpp +16 -0
  40. package/nitrogen/generated/android/c++/JHybridNitroInputMeasureSpec.cpp +93 -0
  41. package/nitrogen/generated/android/c++/JHybridNitroInputMeasureSpec.hpp +63 -0
  42. package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.cpp +94 -40
  43. package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.hpp +28 -16
  44. package/nitrogen/generated/android/c++/JHybridNitroNumberViewSpec.cpp +30 -30
  45. package/nitrogen/generated/android/c++/JHybridNitroNumberViewSpec.hpp +12 -12
  46. package/nitrogen/generated/android/c++/JHybridNitroTextViewSpec.cpp +30 -30
  47. package/nitrogen/generated/android/c++/JHybridNitroTextViewSpec.hpp +12 -12
  48. package/nitrogen/generated/android/c++/JNitroInputSize.hpp +61 -0
  49. package/nitrogen/generated/android/c++/JNitroInputSizing.hpp +292 -0
  50. package/nitrogen/generated/android/c++/views/JHybridNitroInputViewStateUpdater.cpp +54 -24
  51. package/nitrogen/generated/android/c++/views/JHybridNitroNumberViewStateUpdater.cpp +18 -18
  52. package/nitrogen/generated/android/c++/views/JHybridNitroTextViewStateUpdater.cpp +18 -18
  53. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroInputMeasureSpec.kt +56 -0
  54. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroInputViewSpec.kt +44 -8
  55. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroNumberViewSpec.kt +6 -6
  56. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroTextViewSpec.kt +6 -6
  57. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputSize.kt +56 -0
  58. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputSizing.kt +256 -0
  59. package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Bridge.cpp +17 -0
  60. package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Bridge.hpp +29 -0
  61. package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Umbrella.hpp +11 -0
  62. package/nitrogen/generated/ios/NitroInputAutolinking.mm +8 -0
  63. package/nitrogen/generated/ios/NitroInputAutolinking.swift +12 -0
  64. package/nitrogen/generated/ios/c++/HybridNitroInputMeasureSpecSwift.cpp +11 -0
  65. package/nitrogen/generated/ios/c++/HybridNitroInputMeasureSpecSwift.hpp +112 -0
  66. package/nitrogen/generated/ios/c++/HybridNitroInputViewSpecSwift.hpp +68 -32
  67. package/nitrogen/generated/ios/c++/HybridNitroNumberViewSpecSwift.hpp +24 -24
  68. package/nitrogen/generated/ios/c++/HybridNitroTextViewSpecSwift.hpp +24 -24
  69. package/nitrogen/generated/ios/c++/views/HybridNitroInputViewComponent.mm +68 -32
  70. package/nitrogen/generated/ios/c++/views/HybridNitroNumberViewComponent.mm +24 -24
  71. package/nitrogen/generated/ios/c++/views/HybridNitroTextViewComponent.mm +24 -24
  72. package/nitrogen/generated/ios/swift/HybridNitroInputMeasureSpec.swift +55 -0
  73. package/nitrogen/generated/ios/swift/HybridNitroInputMeasureSpec_cxx.swift +138 -0
  74. package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec.swift +14 -8
  75. package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec_cxx.swift +90 -24
  76. package/nitrogen/generated/ios/swift/HybridNitroNumberViewSpec.swift +6 -6
  77. package/nitrogen/generated/ios/swift/HybridNitroNumberViewSpec_cxx.swift +18 -18
  78. package/nitrogen/generated/ios/swift/HybridNitroTextViewSpec.swift +6 -6
  79. package/nitrogen/generated/ios/swift/HybridNitroTextViewSpec_cxx.swift +18 -18
  80. package/nitrogen/generated/ios/swift/NitroInputSize.swift +34 -0
  81. package/nitrogen/generated/ios/swift/NitroInputSizing.swift +252 -0
  82. package/nitrogen/generated/shared/c++/HybridNitroInputMeasureSpec.cpp +21 -0
  83. package/nitrogen/generated/shared/c++/HybridNitroInputMeasureSpec.hpp +66 -0
  84. package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.cpp +28 -16
  85. package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.hpp +28 -16
  86. package/nitrogen/generated/shared/c++/HybridNitroNumberViewSpec.cpp +12 -12
  87. package/nitrogen/generated/shared/c++/HybridNitroNumberViewSpec.hpp +12 -12
  88. package/nitrogen/generated/shared/c++/HybridNitroTextViewSpec.cpp +12 -12
  89. package/nitrogen/generated/shared/c++/HybridNitroTextViewSpec.hpp +12 -12
  90. package/nitrogen/generated/shared/c++/NitroInputSize.hpp +87 -0
  91. package/nitrogen/generated/shared/c++/NitroInputSizing.hpp +271 -0
  92. package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.cpp +28 -16
  93. package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.hpp +42 -24
  94. package/nitrogen/generated/shared/c++/views/HybridNitroNumberViewComponent.cpp +12 -12
  95. package/nitrogen/generated/shared/c++/views/HybridNitroNumberViewComponent.hpp +18 -18
  96. package/nitrogen/generated/shared/c++/views/HybridNitroTextViewComponent.cpp +12 -12
  97. package/nitrogen/generated/shared/c++/views/HybridNitroTextViewComponent.hpp +18 -18
  98. package/nitrogen/generated/shared/json/NitroInputViewConfig.json +14 -8
  99. package/nitrogen/generated/shared/json/NitroNumberViewConfig.json +6 -6
  100. package/nitrogen/generated/shared/json/NitroTextViewConfig.json +6 -6
  101. package/package.json +4 -2
  102. package/src/NitroInput.tsx +215 -64
  103. package/src/NitroNumber.tsx +6 -6
  104. package/src/NitroText.tsx +6 -6
  105. package/src/specs/NitroInput.nitro.ts +21 -8
  106. package/src/specs/NitroInputMeasure.nitro.ts +78 -0
  107. package/src/specs/NitroNumber.nitro.ts +6 -6
  108. package/src/specs/NitroText.nitro.ts +6 -6
@@ -141,7 +141,7 @@ class HybridNitroNumberView(private val context: ThemedReactContext) : HybridNit
141
141
  set(v) { field = v; markConfigDirty() }
142
142
  override var shimmerDelay: Double? = null
143
143
  set(v) { field = v; markConfigDirty() }
144
- override var fontSize: Double? = null
144
+ override var textSize: Double? = null
145
145
  set(v) { field = v; markConfigDirty() }
146
146
  override var prefixFontSize: Double? = null
147
147
  set(v) { field = v; markConfigDirty() }
@@ -153,7 +153,7 @@ class HybridNitroNumberView(private val context: ThemedReactContext) : HybridNit
153
153
  set(v) { field = v; markConfigDirty() }
154
154
  override var suffixAlign: NitroNumberAffixAlign? = null
155
155
  set(v) { field = v; markConfigDirty() }
156
- override var letterSpacing: Double? = null
156
+ override var textLetterSpacing: Double? = null
157
157
  set(v) { field = v; markConfigDirty() }
158
158
  override var prefixSpacing: Double? = null
159
159
  set(v) { field = v; markConfigDirty() }
@@ -201,13 +201,13 @@ class HybridNitroNumberView(private val context: ThemedReactContext) : HybridNit
201
201
  set(v) { field = v; markConfigDirty() }
202
202
  override var maxFontSizeMultiplier: Double? = null
203
203
  set(v) { field = v; markConfigDirty() }
204
- override var fontWeight: Double? = null
204
+ override var textWeight: Double? = null
205
205
  set(v) { field = v; markConfigDirty() }
206
- override var fontFamily: String? = null
206
+ override var textFamily: String? = null
207
207
  set(v) { field = v; markConfigDirty() }
208
- override var color: Double? = null
208
+ override var textColor: Double? = null
209
209
  set(v) { field = v; markConfigDirty() }
210
- override var textAlign: NitroNumberTextAlign? = null
210
+ override var textAlignment: NitroNumberTextAlign? = null
211
211
  set(v) { field = v; markConfigDirty() }
212
212
  override var rightToLeft: Boolean? = null
213
213
  set(v) { field = v; markConfigDirty() }
@@ -360,13 +360,13 @@ class HybridNitroNumberView(private val context: ThemedReactContext) : HybridNit
360
360
  shimmerBaseColor = null
361
361
  shimmerDirection = null
362
362
  shimmerDelay = null
363
- fontSize = null
363
+ textSize = null
364
364
  prefixFontSize = null
365
365
  suffixFontSize = null
366
366
  affixAlign = null
367
367
  prefixAlign = null
368
368
  suffixAlign = null
369
- letterSpacing = null
369
+ textLetterSpacing = null
370
370
  prefixSpacing = null
371
371
  suffixSpacing = null
372
372
  prefixOffset = null
@@ -391,10 +391,10 @@ class HybridNitroNumberView(private val context: ThemedReactContext) : HybridNit
391
391
  minimumFontScale = null
392
392
  allowFontScaling = null
393
393
  maxFontSizeMultiplier = null
394
- fontWeight = null
395
- fontFamily = null
396
- color = null
397
- textAlign = null
394
+ textWeight = null
395
+ textFamily = null
396
+ textColor = null
397
+ textAlignment = null
398
398
  rightToLeft = null
399
399
  onSizeChange = null
400
400
  configDirty = true
@@ -447,15 +447,15 @@ class HybridNitroNumberView(private val context: ThemedReactContext) : HybridNit
447
447
  configDirty = false
448
448
 
449
449
  rollingView.typography = NitroNumberView.Typography(
450
- fontSize = (fontSize ?: 32.0).toFloat(),
450
+ fontSize = (textSize ?: 32.0).toFloat(),
451
451
  prefixFontSize = prefixFontSize?.toFloat(),
452
452
  suffixFontSize = suffixFontSize?.toFloat(),
453
- fontWeight = clampInt(fontWeight, 100, 900, 400),
454
- fontFamily = fontFamily,
455
- color = color?.let { colorFromARGB(it) },
453
+ fontWeight = clampInt(textWeight, 100, 900, 400),
454
+ fontFamily = textFamily,
455
+ color = textColor?.let { colorFromARGB(it) },
456
456
  prefixAlign = mapAffixAlign(prefixAlign ?: affixAlign),
457
457
  suffixAlign = mapAffixAlign(suffixAlign ?: affixAlign),
458
- letterSpacing = (letterSpacing ?: 0.0).takeIf { it.isFinite() }?.toFloat() ?: 0f,
458
+ letterSpacing = (textLetterSpacing ?: 0.0).takeIf { it.isFinite() }?.toFloat() ?: 0f,
459
459
  prefixSpacing = prefixSpacing?.takeIf { it.isFinite() }?.toFloat(),
460
460
  suffixSpacing = suffixSpacing?.takeIf { it.isFinite() }?.toFloat(),
461
461
  prefixOffset = (prefixOffset ?: 0.0).takeIf { it.isFinite() }?.toFloat() ?: 0f,
@@ -545,7 +545,7 @@ class HybridNitroNumberView(private val context: ThemedReactContext) : HybridNit
545
545
  downColor = flashDownColor?.let { colorFromARGB(it) },
546
546
  durationMs = Math.max(50.0, flashDuration ?: 600.0).toLong(),
547
547
  )
548
- rollingView.alignment = when (textAlign) {
548
+ rollingView.alignment = when (textAlignment) {
549
549
  NitroNumberTextAlign.CENTER -> NitroNumberView.Alignment.CENTER
550
550
  NitroNumberTextAlign.RIGHT -> NitroNumberView.Alignment.RIGHT
551
551
  NitroNumberTextAlign.LEFT -> NitroNumberView.Alignment.LEFT
@@ -26,12 +26,12 @@ class HybridNitroTextView(private val context: ThemedReactContext) : HybridNitro
26
26
 
27
27
  override var text: String = ""
28
28
  // Unset unless the element set it: the wrapper sends only those.
29
- override var fontSize: Double? = null
30
- override var fontWeight: Double? = null
31
- override var fontFamily: String? = null
32
- override var color: Double? = null
33
- override var letterSpacing: Double? = null
34
- override var textAlign: NitroNumberTextAlign? = null
29
+ override var textSize: Double? = null
30
+ override var textWeight: Double? = null
31
+ override var textFamily: String? = null
32
+ override var textColor: Double? = null
33
+ override var textLetterSpacing: Double? = null
34
+ override var textAlignment: NitroNumberTextAlign? = null
35
35
  override var rightToLeft: Boolean? = null
36
36
  override var duration: Double? = null
37
37
  override var easing: NitroInputEasing? = null
@@ -57,12 +57,12 @@ class HybridNitroTextView(private val context: ThemedReactContext) : HybridNitro
57
57
 
58
58
  private fun apply() {
59
59
  textView.style = NitroTextView.Style(
60
- fontSize = fontSize?.takeIf { it.isFinite() && it > 0 } ?: 17.0,
61
- fontWeight = fontWeight?.takeIf { it.isFinite() } ?: 400.0,
62
- fontFamily = fontFamily ?: "",
63
- color = color?.let(::colorFromARGB),
64
- letterSpacing = (letterSpacing?.takeIf { it.isFinite() } ?: 0.0).toFloat(),
65
- textAlign = when (textAlign) {
60
+ fontSize = textSize?.takeIf { it.isFinite() && it > 0 } ?: 17.0,
61
+ fontWeight = textWeight?.takeIf { it.isFinite() } ?: 400.0,
62
+ fontFamily = textFamily ?: "",
63
+ color = textColor?.let(::colorFromARGB),
64
+ letterSpacing = (textLetterSpacing?.takeIf { it.isFinite() } ?: 0.0).toFloat(),
65
+ textAlign = when (textAlignment) {
66
66
  NitroNumberTextAlign.LEFT -> NitroNumberView.Alignment.LEFT
67
67
  NitroNumberTextAlign.CENTER -> NitroNumberView.Alignment.CENTER
68
68
  NitroNumberTextAlign.RIGHT -> NitroNumberView.Alignment.RIGHT
@@ -116,8 +116,8 @@ class HybridNitroTextView(private val context: ThemedReactContext) : HybridNitro
116
116
  override fun prepareForRecycle() {
117
117
  // The next element's props arrive as a first mount's do, only the ones it set.
118
118
  text = ""
119
- fontSize = null; fontWeight = null; fontFamily = null; color = null; letterSpacing = null
120
- textAlign = null; rightToLeft = null
119
+ textSize = null; textWeight = null; textFamily = null; textColor = null; textLetterSpacing = null
120
+ textAlignment = null; rightToLeft = null
121
121
  duration = null; easing = null; bounce = null; effect = null; respectReduceMotion = null
122
122
  loading = null
123
123
  shimmerColor = null; shimmerDuration = null; shimmerAngle = null; shimmerWidth = null
@@ -0,0 +1,244 @@
1
+ package com.margelo.nitro.nitroinput
2
+
3
+ import android.content.res.AssetManager
4
+ import android.graphics.Paint
5
+ import android.graphics.Rect
6
+ import android.graphics.Typeface
7
+ import android.os.Build
8
+ import android.text.TextPaint
9
+ import com.facebook.react.common.assets.ReactFontManager
10
+ import com.margelo.nitro.nitroinput.NitroInputView.AffixAlign
11
+ import com.margelo.nitro.nitroinput.NitroInputView.Typography
12
+ import kotlin.math.ceil
13
+ import kotlin.math.max
14
+ import kotlin.math.min
15
+
16
+ internal enum class InputRole { PREFIX, BODY, SUFFIX }
17
+
18
+ private const val ALL_DIGITS = "0123456789"
19
+
20
+ /**
21
+ * A field's body / prefix / suffix paints with per-character advance caches.
22
+ * The view draws with it and [InputSizing] measures with it, so what a field
23
+ * is measured at before its view exists is what the view then reports.
24
+ *
25
+ * Not thread-safe: one instance per thread. The view's lives on the main
26
+ * thread; a measure on the JS thread builds its own.
27
+ */
28
+ internal class InputFonts(
29
+ t: Typography,
30
+ private val density: Float,
31
+ /** The system's font scale (`Configuration.fontScale`), applied under `allowFontScaling`. */
32
+ fontScale: Float,
33
+ private val assets: AssetManager?,
34
+ /** The text colour when the typography sets none. */
35
+ private val defaultColor: Int,
36
+ ) {
37
+ private val scale = systemFontMultiplier(t, fontScale)
38
+ val body = makePaint(t, t.fontSize * scale)
39
+ val prefix = makePaint(t, t.prefixFontSize * scale)
40
+ val suffix = makePaint(t, t.suffixFontSize * scale)
41
+ private val prefixAlign = t.prefixAlign
42
+ private val suffixAlign = t.suffixAlign
43
+ /** Letter spacing after a body / prefix / suffix glyph, the seams (null: the letter spacing) and the affix offsets, in px. */
44
+ val bodySpacing = t.letterSpacing * scale * density
45
+ val prefixLetterSpacing = bodySpacing * (t.prefixFontSize / t.fontSize)
46
+ val suffixLetterSpacing = bodySpacing * (t.suffixFontSize / t.fontSize)
47
+ val prefixSeam = t.prefixSpacing?.let { it * scale * density }
48
+ val suffixSeam = t.suffixSpacing?.let { it * scale * density }
49
+ val prefixOffset = t.prefixOffset * scale * density
50
+ val suffixOffset = t.suffixOffset * scale * density
51
+ private val bodyMetrics: Paint.FontMetrics = body.fontMetrics
52
+ /** The font's own line box, before any `lineHeight` override. */
53
+ val fontLineHeight: Float = ceil(bodyMetrics.descent - bodyMetrics.ascent)
54
+ /** Height of the line box in px: `lineHeight` when given, else the font's. */
55
+ val lineHeight: Float = if (t.lineHeightPx > 0f) t.lineHeightPx else fontLineHeight
56
+ /**
57
+ * How far to push the glyphs down inside the line box. The platform stacks
58
+ * the extra leading above the line, so without this the run rides high;
59
+ * with a line height *tighter* than the font it rides low, and the same
60
+ * halving corrects both. React Native skips the tighter case.
61
+ */
62
+ val baselineNudge: Float = (lineHeight - fontLineHeight) / 2f
63
+ private val widthCaches = Array(3) { HashMap<Int, Float>() }
64
+ private val capHeightCache = HashMap<InputRole, Float>()
65
+ private val charCache = HashMap<Int, String>()
66
+ // By role, then text: a key built from both was a string allocated for
67
+ // every affix glyph on every frame of a reflow.
68
+ private val inkDescentCaches = Array(3) { HashMap<String, Float>() }
69
+ // Each role's metrics, read once: `Paint.fontMetrics` allocates, and the
70
+ // affixes' baselines are asked for on every frame of a reflow.
71
+ private val metrics = arrayOf(prefix.fontMetrics, bodyMetrics, suffix.fontMetrics)
72
+ private val shapedCaches = Array(3) { HashMap<Int, ShapedText>() }
73
+
74
+ /**
75
+ * One character of [role] shaped once, for [Canvas.drawGlyphs]: `drawText`
76
+ * runs the text shaper on every call, and a reflow draws every glyph on
77
+ * every frame of its animation (see NitroNumberView's `shaped`).
78
+ */
79
+ @androidx.annotation.RequiresApi(Build.VERSION_CODES.S)
80
+ fun shaped(codePoint: Int, role: InputRole): ShapedText =
81
+ shapedCaches[role.ordinal].getOrPut(codePoint) { ShapedText.of(charString(codePoint), paint(role)) }
82
+
83
+ fun paint(role: InputRole): TextPaint = when (role) {
84
+ InputRole.BODY -> body
85
+ InputRole.PREFIX -> prefix
86
+ InputRole.SUFFIX -> suffix
87
+ }
88
+
89
+ /**
90
+ * The body's digits when they are slotted (`tabularNums` set) and the font
91
+ * has no tabular figures: each is then laid out in a slot as wide as the
92
+ * widest ([DigitSlots]); null otherwise. The affixes keep their own: they
93
+ * never change under the typing.
94
+ */
95
+ private val bodyDigits: DigitSlots.Digits? = if (t.slotDigits) DigitSlots.measure(body) else null
96
+
97
+ /** A slotted body digit's advance, its letter spacing aside: the edit text's slot less that spacing. */
98
+ private val bodySlot: Float = bodyDigits?.let { it.slot(bodySpacing) - bodySpacing } ?: 0f
99
+
100
+ private fun slotted(codePoint: Int, role: InputRole): Boolean =
101
+ role == InputRole.BODY && bodyDigits != null && DigitSlots.isDigit(codePoint)
102
+
103
+ /** Advance of one character, in px: a slotted digit's is the slot's. */
104
+ fun width(codePoint: Int, role: InputRole): Float =
105
+ widthCaches[role.ordinal].getOrPut(codePoint) {
106
+ if (slotted(codePoint, role)) bodySlot else paint(role).measureText(charString(codePoint))
107
+ }
108
+
109
+ /** How far to draw a character in from the left of its advance: a slotted digit is centred in it. */
110
+ fun inset(codePoint: Int, role: InputRole): Float {
111
+ if (!slotted(codePoint, role)) return 0f
112
+ return (bodySlot - bodyDigits!!.widths[codePoint - '0'.code]) / 2f
113
+ }
114
+
115
+ /**
116
+ * [text] in the body paint as the edit text lays it out, without letter
117
+ * spacing: whole, or, when its digits are slotted, in runs split at each
118
+ * digit, which is where the slots' spans split it.
119
+ */
120
+ fun plainWidth(text: String): Float {
121
+ if (bodyDigits == null) return body.measureText(text)
122
+ var total = 0f
123
+ var start = 0
124
+ while (start < text.length) {
125
+ if (DigitSlots.isDigit(text[start].code)) {
126
+ total += width(text[start].code, InputRole.BODY)
127
+ start++
128
+ continue
129
+ }
130
+ var end = start + 1
131
+ while (end < text.length && !DigitSlots.isDigit(text[end].code)) end++
132
+ total += body.measureText(text, start, end)
133
+ start = end
134
+ }
135
+ return total
136
+ }
137
+
138
+ /** Sum of the per-character advances of [text] (what the engine lays out). */
139
+ fun width(text: String, role: InputRole): Float {
140
+ var total = 0f
141
+ var i = 0
142
+ while (i < text.length) {
143
+ val cp = text.codePointAt(i)
144
+ total += width(cp, role)
145
+ i += Character.charCount(cp)
146
+ }
147
+ return total
148
+ }
149
+
150
+ /** Letter spacing after a glyph of [role], in px. */
151
+ fun spacing(role: InputRole): Float = when (role) {
152
+ InputRole.BODY -> bodySpacing
153
+ InputRole.PREFIX -> prefixLetterSpacing
154
+ InputRole.SUFFIX -> suffixLetterSpacing
155
+ }
156
+
157
+ /** The room an affix takes beside the text: its glyphs, their spacing and the seam, in px. */
158
+ fun affixRoom(affix: String, role: InputRole): Float {
159
+ if (affix.isEmpty()) return 0f
160
+ val count = affix.codePointCount(0, affix.length)
161
+ val glyphs = width(affix, role)
162
+ if (role == InputRole.PREFIX) return glyphs + prefixLetterSpacing * (count - 1) + (prefixSeam ?: prefixLetterSpacing)
163
+ // The edit text's own letter spacing already follows its last glyph.
164
+ return glyphs + suffixLetterSpacing * count + ((suffixSeam ?: bodySpacing) - bodySpacing)
165
+ }
166
+
167
+ /** Baseline y for `role` drawing `text`, given the top of the body line box. */
168
+ fun baseline(role: InputRole, text: String, lineTop: Float): Float {
169
+ val aligned = alignedBaseline(role, text, lineTop)
170
+ return when (role) {
171
+ InputRole.BODY -> aligned
172
+ InputRole.PREFIX -> aligned + prefixOffset
173
+ InputRole.SUFFIX -> aligned + suffixOffset
174
+ }
175
+ }
176
+
177
+ private fun alignedBaseline(role: InputRole, text: String, lineTop: Float): Float {
178
+ // `baselineNudge` re-centres the run when `lineHeight` differs from the
179
+ // font's: the extra goes above the line, so the body sits that far lower.
180
+ // Everything pinned to the body follows it; `CENTER` does not, because it
181
+ // centres in the line box itself, which is already the new height.
182
+ val bodyBaseline = lineTop + baselineNudge - bodyMetrics.ascent
183
+ if (role == InputRole.BODY) return bodyBaseline
184
+ val m = metrics[role.ordinal]
185
+ return when (if (role == InputRole.PREFIX) prefixAlign else suffixAlign) {
186
+ AffixAlign.BASELINE -> bodyBaseline
187
+ AffixAlign.CENTER -> lineTop + (lineHeight - (m.descent - m.ascent)) / 2f - m.ascent
188
+ AffixAlign.TOP -> bodyBaseline - capHeight(InputRole.BODY) + capHeight(role)
189
+ // Pin the bottom of the ink, not of the line boxes: the digits' ink ends
190
+ // on the baseline, so "USD" sits on it too instead of hanging down to
191
+ // where a comma's tail reaches.
192
+ AffixAlign.BOTTOM -> bodyBaseline + inkDescent(ALL_DIGITS, InputRole.BODY) - inkDescent(text, role)
193
+ }
194
+ }
195
+
196
+ /** How far `text`'s ink hangs below the baseline (0 for digits and capitals). */
197
+ private fun inkDescent(text: String, role: InputRole): Float = inkDescentCaches[role.ordinal].getOrPut(text) {
198
+ val bounds = Rect()
199
+ paint(role).getTextBounds(text, 0, text.length, bounds)
200
+ max(0, bounds.bottom).toFloat()
201
+ }
202
+
203
+ private fun capHeight(role: InputRole): Float = capHeightCache.getOrPut(role) {
204
+ val bounds = Rect()
205
+ paint(role).getTextBounds("0", 0, 1, bounds)
206
+ -bounds.top.toFloat()
207
+ }
208
+
209
+ private fun charString(codePoint: Int): String = charCache.getOrPut(codePoint) { String(Character.toChars(codePoint)) }
210
+
211
+ /** System font scale for `allowFontScaling`, capped by `maxFontSizeMultiplier`. */
212
+ private fun systemFontMultiplier(t: Typography, multiplier: Float): Float {
213
+ if (!t.allowFontScaling) return 1f
214
+ return if (t.maxFontSizeMultiplier > 0f) min(multiplier, t.maxFontSizeMultiplier) else multiplier
215
+ }
216
+
217
+ private fun makePaint(t: Typography, sizeDp: Float): TextPaint {
218
+ val paint = TextPaint(Paint.ANTI_ALIAS_FLAG or Paint.SUBPIXEL_TEXT_FLAG)
219
+ paint.typeface = makeTypeface(t)
220
+ paint.textSize = sizeDp * density
221
+ paint.color = t.color ?: defaultColor
222
+ // Tabular figures unless asked not to, as on iOS: the engine slides digit
223
+ // runs in text mode too, and proportional digits would jitter as they change.
224
+ paint.fontFeatureSettings = if (t.tabularNums) "tnum" else "pnum"
225
+ return paint
226
+ }
227
+
228
+ private fun makeTypeface(t: Typography): Typeface {
229
+ val family = t.fontFamily
230
+ return try {
231
+ if (!family.isNullOrEmpty()) {
232
+ ReactFontManager.getInstance().getTypeface(family, Typeface.NORMAL, t.fontWeight, assets)
233
+ } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
234
+ Typeface.create(Typeface.DEFAULT, t.fontWeight, false)
235
+ } else if (t.fontWeight >= 600) {
236
+ Typeface.DEFAULT_BOLD
237
+ } else {
238
+ Typeface.DEFAULT
239
+ }
240
+ } catch (e: Throwable) {
241
+ if (t.fontWeight >= 600) Typeface.DEFAULT_BOLD else Typeface.DEFAULT
242
+ }
243
+ }
244
+ }