react-native-nitro-input 0.3.7 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +133 -0
- package/README.md +9 -4
- package/android/CMakeLists.txt +1 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroInputMeasure.kt +52 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroInputView.kt +74 -80
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroNumberView.kt +18 -18
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroTextView.kt +14 -14
- package/android/src/main/java/com/margelo/nitro/nitroinput/InputFonts.kt +199 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/InputSizing.kt +398 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/NitroInputView.kt +112 -333
- package/android/src/main/java/com/margelo/nitro/nitroinput/NitroNumberView.kt +178 -30
- package/cpp/RollingEngine.cpp +68 -5
- package/cpp/RollingEngine.hpp +13 -0
- package/cpp/numberformat/FormatFigure.cpp +134 -0
- package/cpp/numberformat/FormatFigure.hpp +65 -0
- package/cpp/numberformat/HybridNitroNumberFormat.cpp +32 -10
- package/cpp/numberformat/HybridNitroNumberFormat.hpp +9 -1
- package/ios/HybridNitroInputView.swift +69 -75
- package/ios/HybridNitroNumberView.swift +18 -18
- package/ios/HybridNitroTextView.swift +14 -14
- package/ios/InputSizing.swift +534 -0
- package/ios/NitroInputView.swift +102 -198
- package/ios/NitroNumberView.swift +98 -23
- package/lib/commonjs/nitrogen/generated/shared/json/NitroInputViewConfig.json +12 -8
- package/lib/commonjs/nitrogen/generated/shared/json/NitroNumberViewConfig.json +6 -6
- package/lib/commonjs/nitrogen/generated/shared/json/NitroTextViewConfig.json +6 -6
- package/lib/commonjs/src/NitroInput.js +164 -23
- package/lib/commonjs/src/NitroNumber.js +19 -11
- package/lib/commonjs/src/NitroText.js +6 -6
- package/lib/commonjs/src/NumberFormat.js +20 -0
- package/lib/commonjs/src/formatProps.js +57 -81
- package/lib/commonjs/src/specs/NitroInputMeasure.nitro.js +2 -0
- package/lib/module/nitrogen/generated/shared/json/NitroInputViewConfig.json +12 -8
- package/lib/module/nitrogen/generated/shared/json/NitroNumberViewConfig.json +6 -6
- package/lib/module/nitrogen/generated/shared/json/NitroTextViewConfig.json +6 -6
- package/lib/module/src/NitroInput.js +165 -24
- package/lib/module/src/NitroNumber.js +20 -12
- package/lib/module/src/NitroText.js +6 -6
- package/lib/module/src/NumberFormat.js +18 -0
- package/lib/module/src/formatProps.js +54 -81
- package/lib/module/src/specs/NitroInputMeasure.nitro.js +1 -0
- package/lib/typescript/src/NitroInput.d.ts +9 -16
- package/lib/typescript/src/NitroNumber.d.ts +25 -7
- package/lib/typescript/src/NumberFormat.d.ts +15 -1
- package/lib/typescript/src/formatProps.d.ts +23 -18
- package/lib/typescript/src/specs/NitroInput.nitro.d.ts +17 -8
- package/lib/typescript/src/specs/NitroInputMeasure.nitro.d.ts +67 -0
- package/lib/typescript/src/specs/NitroNumber.nitro.d.ts +6 -6
- package/lib/typescript/src/specs/NitroText.nitro.d.ts +6 -6
- package/lib/typescript/src/specs/NumberFormat.nitro.d.ts +33 -0
- package/nitro.json +10 -0
- package/nitrogen/generated/android/NitroInput+autolinking.cmake +2 -0
- package/nitrogen/generated/android/NitroInputOnLoad.cpp +16 -0
- package/nitrogen/generated/android/c++/JHybridNitroInputMeasureSpec.cpp +93 -0
- package/nitrogen/generated/android/c++/JHybridNitroInputMeasureSpec.hpp +63 -0
- package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.cpp +76 -40
- package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.hpp +24 -16
- package/nitrogen/generated/android/c++/JHybridNitroNumberViewSpec.cpp +30 -30
- package/nitrogen/generated/android/c++/JHybridNitroNumberViewSpec.hpp +12 -12
- package/nitrogen/generated/android/c++/JHybridNitroTextViewSpec.cpp +30 -30
- package/nitrogen/generated/android/c++/JHybridNitroTextViewSpec.hpp +12 -12
- package/nitrogen/generated/android/c++/JNitroInputSize.hpp +61 -0
- package/nitrogen/generated/android/c++/JNitroInputSizing.hpp +284 -0
- package/nitrogen/generated/android/c++/views/JHybridNitroInputViewStateUpdater.cpp +44 -24
- package/nitrogen/generated/android/c++/views/JHybridNitroNumberViewStateUpdater.cpp +18 -18
- package/nitrogen/generated/android/c++/views/JHybridNitroTextViewStateUpdater.cpp +18 -18
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroInputMeasureSpec.kt +56 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroInputViewSpec.kt +32 -8
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroNumberViewSpec.kt +6 -6
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroTextViewSpec.kt +6 -6
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputSize.kt +56 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputSizing.kt +246 -0
- package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Bridge.cpp +17 -0
- package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Bridge.hpp +29 -0
- package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Umbrella.hpp +11 -0
- package/nitrogen/generated/ios/NitroInputAutolinking.mm +8 -0
- package/nitrogen/generated/ios/NitroInputAutolinking.swift +12 -0
- package/nitrogen/generated/ios/c++/HybridNitroInputMeasureSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridNitroInputMeasureSpecSwift.hpp +112 -0
- package/nitrogen/generated/ios/c++/HybridNitroInputViewSpecSwift.hpp +56 -32
- package/nitrogen/generated/ios/c++/HybridNitroNumberViewSpecSwift.hpp +24 -24
- package/nitrogen/generated/ios/c++/HybridNitroTextViewSpecSwift.hpp +24 -24
- package/nitrogen/generated/ios/c++/views/HybridNitroInputViewComponent.mm +56 -32
- package/nitrogen/generated/ios/c++/views/HybridNitroNumberViewComponent.mm +24 -24
- package/nitrogen/generated/ios/c++/views/HybridNitroTextViewComponent.mm +24 -24
- package/nitrogen/generated/ios/swift/HybridNitroInputMeasureSpec.swift +55 -0
- package/nitrogen/generated/ios/swift/HybridNitroInputMeasureSpec_cxx.swift +138 -0
- package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec.swift +12 -8
- package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec_cxx.swift +68 -24
- package/nitrogen/generated/ios/swift/HybridNitroNumberViewSpec.swift +6 -6
- package/nitrogen/generated/ios/swift/HybridNitroNumberViewSpec_cxx.swift +18 -18
- package/nitrogen/generated/ios/swift/HybridNitroTextViewSpec.swift +6 -6
- package/nitrogen/generated/ios/swift/HybridNitroTextViewSpec_cxx.swift +18 -18
- package/nitrogen/generated/ios/swift/NitroInputSize.swift +34 -0
- package/nitrogen/generated/ios/swift/NitroInputSizing.swift +242 -0
- package/nitrogen/generated/shared/c++/HybridNitroInputMeasureSpec.cpp +21 -0
- package/nitrogen/generated/shared/c++/HybridNitroInputMeasureSpec.hpp +66 -0
- package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.cpp +24 -16
- package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.hpp +24 -16
- package/nitrogen/generated/shared/c++/HybridNitroNumberFormatSpec.cpp +2 -0
- package/nitrogen/generated/shared/c++/HybridNitroNumberFormatSpec.hpp +8 -0
- package/nitrogen/generated/shared/c++/HybridNitroNumberViewSpec.cpp +12 -12
- package/nitrogen/generated/shared/c++/HybridNitroNumberViewSpec.hpp +12 -12
- package/nitrogen/generated/shared/c++/HybridNitroTextViewSpec.cpp +12 -12
- package/nitrogen/generated/shared/c++/HybridNitroTextViewSpec.hpp +12 -12
- package/nitrogen/generated/shared/c++/NitroInputSize.hpp +87 -0
- package/nitrogen/generated/shared/c++/NitroInputSizing.hpp +263 -0
- package/nitrogen/generated/shared/c++/NumberFigure.hpp +95 -0
- package/nitrogen/generated/shared/c++/NumberFormatLayout.hpp +112 -0
- package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.cpp +24 -16
- package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.hpp +36 -24
- package/nitrogen/generated/shared/c++/views/HybridNitroNumberViewComponent.cpp +12 -12
- package/nitrogen/generated/shared/c++/views/HybridNitroNumberViewComponent.hpp +18 -18
- package/nitrogen/generated/shared/c++/views/HybridNitroTextViewComponent.cpp +12 -12
- package/nitrogen/generated/shared/c++/views/HybridNitroTextViewComponent.hpp +18 -18
- package/nitrogen/generated/shared/json/NitroInputViewConfig.json +12 -8
- package/nitrogen/generated/shared/json/NitroNumberViewConfig.json +6 -6
- package/nitrogen/generated/shared/json/NitroTextViewConfig.json +6 -6
- package/package.json +5 -3
- package/src/NitroInput.tsx +201 -64
- package/src/NitroNumber.tsx +54 -25
- package/src/NitroText.tsx +6 -6
- package/src/NumberFormat.ts +22 -0
- package/src/formatProps.ts +59 -99
- package/src/specs/NitroInput.nitro.ts +17 -8
- package/src/specs/NitroInputMeasure.nitro.ts +76 -0
- package/src/specs/NitroNumber.nitro.ts +6 -6
- package/src/specs/NitroText.nitro.ts +6 -6
- package/src/specs/NumberFormat.nitro.ts +35 -0
|
@@ -0,0 +1,199 @@
|
|
|
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
|
+
/** Advance of one character, in px. */
|
|
90
|
+
fun width(codePoint: Int, role: InputRole): Float =
|
|
91
|
+
widthCaches[role.ordinal].getOrPut(codePoint) { paint(role).measureText(charString(codePoint)) }
|
|
92
|
+
|
|
93
|
+
/** Sum of the per-character advances of [text] (what the engine lays out). */
|
|
94
|
+
fun width(text: String, role: InputRole): Float {
|
|
95
|
+
var total = 0f
|
|
96
|
+
var i = 0
|
|
97
|
+
while (i < text.length) {
|
|
98
|
+
val cp = text.codePointAt(i)
|
|
99
|
+
total += width(cp, role)
|
|
100
|
+
i += Character.charCount(cp)
|
|
101
|
+
}
|
|
102
|
+
return total
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Letter spacing after a glyph of [role], in px. */
|
|
106
|
+
fun spacing(role: InputRole): Float = when (role) {
|
|
107
|
+
InputRole.BODY -> bodySpacing
|
|
108
|
+
InputRole.PREFIX -> prefixLetterSpacing
|
|
109
|
+
InputRole.SUFFIX -> suffixLetterSpacing
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** The room an affix takes beside the text: its glyphs, their spacing and the seam, in px. */
|
|
113
|
+
fun affixRoom(affix: String, role: InputRole): Float {
|
|
114
|
+
if (affix.isEmpty()) return 0f
|
|
115
|
+
val count = affix.codePointCount(0, affix.length)
|
|
116
|
+
val glyphs = width(affix, role)
|
|
117
|
+
if (role == InputRole.PREFIX) return glyphs + prefixLetterSpacing * (count - 1) + (prefixSeam ?: prefixLetterSpacing)
|
|
118
|
+
// The edit text's own letter spacing already follows its last glyph.
|
|
119
|
+
return glyphs + suffixLetterSpacing * count + ((suffixSeam ?: bodySpacing) - bodySpacing)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Baseline y for `role` drawing `text`, given the top of the body line box. */
|
|
123
|
+
fun baseline(role: InputRole, text: String, lineTop: Float): Float {
|
|
124
|
+
val aligned = alignedBaseline(role, text, lineTop)
|
|
125
|
+
return when (role) {
|
|
126
|
+
InputRole.BODY -> aligned
|
|
127
|
+
InputRole.PREFIX -> aligned + prefixOffset
|
|
128
|
+
InputRole.SUFFIX -> aligned + suffixOffset
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
private fun alignedBaseline(role: InputRole, text: String, lineTop: Float): Float {
|
|
133
|
+
// `baselineNudge` re-centres the run when `lineHeight` differs from the
|
|
134
|
+
// font's: the extra goes above the line, so the body sits that far lower.
|
|
135
|
+
// Everything pinned to the body follows it; `CENTER` does not, because it
|
|
136
|
+
// centres in the line box itself, which is already the new height.
|
|
137
|
+
val bodyBaseline = lineTop + baselineNudge - bodyMetrics.ascent
|
|
138
|
+
if (role == InputRole.BODY) return bodyBaseline
|
|
139
|
+
val m = metrics[role.ordinal]
|
|
140
|
+
return when (if (role == InputRole.PREFIX) prefixAlign else suffixAlign) {
|
|
141
|
+
AffixAlign.BASELINE -> bodyBaseline
|
|
142
|
+
AffixAlign.CENTER -> lineTop + (lineHeight - (m.descent - m.ascent)) / 2f - m.ascent
|
|
143
|
+
AffixAlign.TOP -> bodyBaseline - capHeight(InputRole.BODY) + capHeight(role)
|
|
144
|
+
// Pin the bottom of the ink, not of the line boxes: the digits' ink ends
|
|
145
|
+
// on the baseline, so "USD" sits on it too instead of hanging down to
|
|
146
|
+
// where a comma's tail reaches.
|
|
147
|
+
AffixAlign.BOTTOM -> bodyBaseline + inkDescent(ALL_DIGITS, InputRole.BODY) - inkDescent(text, role)
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** How far `text`'s ink hangs below the baseline (0 for digits and capitals). */
|
|
152
|
+
private fun inkDescent(text: String, role: InputRole): Float = inkDescentCaches[role.ordinal].getOrPut(text) {
|
|
153
|
+
val bounds = Rect()
|
|
154
|
+
paint(role).getTextBounds(text, 0, text.length, bounds)
|
|
155
|
+
max(0, bounds.bottom).toFloat()
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
private fun capHeight(role: InputRole): Float = capHeightCache.getOrPut(role) {
|
|
159
|
+
val bounds = Rect()
|
|
160
|
+
paint(role).getTextBounds("0", 0, 1, bounds)
|
|
161
|
+
-bounds.top.toFloat()
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
private fun charString(codePoint: Int): String = charCache.getOrPut(codePoint) { String(Character.toChars(codePoint)) }
|
|
165
|
+
|
|
166
|
+
/** System font scale for `allowFontScaling`, capped by `maxFontSizeMultiplier`. */
|
|
167
|
+
private fun systemFontMultiplier(t: Typography, multiplier: Float): Float {
|
|
168
|
+
if (!t.allowFontScaling) return 1f
|
|
169
|
+
return if (t.maxFontSizeMultiplier > 0f) min(multiplier, t.maxFontSizeMultiplier) else multiplier
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
private fun makePaint(t: Typography, sizeDp: Float): TextPaint {
|
|
173
|
+
val paint = TextPaint(Paint.ANTI_ALIAS_FLAG or Paint.SUBPIXEL_TEXT_FLAG)
|
|
174
|
+
paint.typeface = makeTypeface(t)
|
|
175
|
+
paint.textSize = sizeDp * density
|
|
176
|
+
paint.color = t.color ?: defaultColor
|
|
177
|
+
// Tabular figures in every mode, as on iOS: the engine slides digit runs
|
|
178
|
+
// in text mode too, and proportional digits would jitter as they change.
|
|
179
|
+
paint.fontFeatureSettings = "tnum"
|
|
180
|
+
return paint
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
private fun makeTypeface(t: Typography): Typeface {
|
|
184
|
+
val family = t.fontFamily
|
|
185
|
+
return try {
|
|
186
|
+
if (!family.isNullOrEmpty()) {
|
|
187
|
+
ReactFontManager.getInstance().getTypeface(family, Typeface.NORMAL, t.fontWeight, assets)
|
|
188
|
+
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
|
189
|
+
Typeface.create(Typeface.DEFAULT, t.fontWeight, false)
|
|
190
|
+
} else if (t.fontWeight >= 600) {
|
|
191
|
+
Typeface.DEFAULT_BOLD
|
|
192
|
+
} else {
|
|
193
|
+
Typeface.DEFAULT
|
|
194
|
+
}
|
|
195
|
+
} catch (e: Throwable) {
|
|
196
|
+
if (t.fontWeight >= 600) Typeface.DEFAULT_BOLD else Typeface.DEFAULT
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
@@ -0,0 +1,398 @@
|
|
|
1
|
+
package com.margelo.nitro.nitroinput
|
|
2
|
+
|
|
3
|
+
import android.graphics.Paint
|
|
4
|
+
import android.text.TextPaint
|
|
5
|
+
import com.margelo.nitro.nitroinput.NitroInputView.Format
|
|
6
|
+
import com.margelo.nitro.nitroinput.NitroInputView.Frame
|
|
7
|
+
import com.margelo.nitro.nitroinput.NitroInputView.Mode
|
|
8
|
+
import com.margelo.nitro.nitroinput.NitroInputView.Typography
|
|
9
|
+
import kotlin.math.ceil
|
|
10
|
+
import kotlin.math.max
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The props a field's size follows, under the names both [HybridNitroInputView]
|
|
14
|
+
* and [NitroInputSizing] give them, so one mapping serves the view and the
|
|
15
|
+
* measure that runs before it exists.
|
|
16
|
+
*/
|
|
17
|
+
internal interface InputSizingProps {
|
|
18
|
+
val mode: NitroInputMode
|
|
19
|
+
val mask: String
|
|
20
|
+
val maskNotations: Array<NitroInputNotation>
|
|
21
|
+
val maskAutocomplete: Boolean
|
|
22
|
+
val maskAutoSkip: Boolean
|
|
23
|
+
val maskAffinityFormats: Array<String>
|
|
24
|
+
val maskAffinityStrategy: NitroInputMaskAffinity
|
|
25
|
+
val maskTextCase: NitroInputTextCase
|
|
26
|
+
val maskCharacterMap: Array<NitroInputCharacterMapping>
|
|
27
|
+
val maskSlotPlaceholder: String
|
|
28
|
+
val keepPlaceholder: Boolean
|
|
29
|
+
val fractionDigits: Double
|
|
30
|
+
val maxIntegerDigits: Double
|
|
31
|
+
val groupingSeparator: String
|
|
32
|
+
val decimalSeparator: String
|
|
33
|
+
val prefix: String
|
|
34
|
+
val suffix: String
|
|
35
|
+
val signPlacement: NitroInputSignPlacement
|
|
36
|
+
val placeholder: String
|
|
37
|
+
val textSize: Double
|
|
38
|
+
val textLineHeight: Double
|
|
39
|
+
val prefixFontSize: Double
|
|
40
|
+
val suffixFontSize: Double
|
|
41
|
+
val textWeight: Double
|
|
42
|
+
val textFamily: String
|
|
43
|
+
val textLetterSpacing: Double
|
|
44
|
+
val prefixSpacing: Double
|
|
45
|
+
val suffixSpacing: Double
|
|
46
|
+
val allowFontScaling: Boolean
|
|
47
|
+
val maxFontSizeMultiplier: Double
|
|
48
|
+
val variant: NitroInputVariant
|
|
49
|
+
val label: String
|
|
50
|
+
val labelBehavior: NitroInputLabelBehavior
|
|
51
|
+
val labelFontSize: Double
|
|
52
|
+
val cornerRadius: Double
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
internal fun finiteOr(value: Double, fallback: Double): Double = if (value.isFinite()) value else fallback
|
|
56
|
+
|
|
57
|
+
internal fun clampedInt(value: Double, lower: Int, upper: Int, fallback: Int): Int {
|
|
58
|
+
if (!value.isFinite()) return fallback
|
|
59
|
+
return Math.rint(value).coerceIn(lower.toDouble(), upper.toDouble()).toInt()
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
internal fun formatOf(p: InputSizingProps) = Format(
|
|
63
|
+
mode = when (p.mode) {
|
|
64
|
+
NitroInputMode.NUMBER -> Mode.NUMBER
|
|
65
|
+
NitroInputMode.MASK -> Mode.MASK
|
|
66
|
+
else -> Mode.TEXT
|
|
67
|
+
},
|
|
68
|
+
mask = p.mask,
|
|
69
|
+
maskNotations = p.maskNotations.map { NitroInputView.MaskNotation(it.character, it.characterSet, it.isOptional) },
|
|
70
|
+
maskAutocomplete = p.maskAutocomplete,
|
|
71
|
+
maskAutoSkip = p.maskAutoSkip,
|
|
72
|
+
maskAffinityFormats = p.maskAffinityFormats.toList(),
|
|
73
|
+
maskAffinityStrategy = p.maskAffinityStrategy.value,
|
|
74
|
+
maskTextCase = p.maskTextCase.value,
|
|
75
|
+
maskCharacterMap = p.maskCharacterMap.map { NitroInputView.MaskMapping(it.from, it.to) },
|
|
76
|
+
maskSlotPlaceholder = p.maskSlotPlaceholder,
|
|
77
|
+
keepPlaceholder = p.keepPlaceholder,
|
|
78
|
+
fractionDigits = clampedInt(p.fractionDigits, 0, 9, 2),
|
|
79
|
+
maxIntegerDigits = clampedInt(p.maxIntegerDigits, 1, 30, 15),
|
|
80
|
+
groupingSeparator = p.groupingSeparator,
|
|
81
|
+
decimalSeparator = p.decimalSeparator,
|
|
82
|
+
prefix = p.prefix,
|
|
83
|
+
suffix = p.suffix,
|
|
84
|
+
signBeforeAffix = p.signPlacement != NitroInputSignPlacement.AFTERAFFIX,
|
|
85
|
+
placeholder = p.placeholder,
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
/** The typography's sizing half; the view adds the colours, alignment and offsets. */
|
|
89
|
+
internal fun sizingTypographyOf(p: InputSizingProps, density: Float): Typography {
|
|
90
|
+
val size = finiteOr(p.textSize, 32.0).toFloat()
|
|
91
|
+
return Typography(
|
|
92
|
+
fontSize = size,
|
|
93
|
+
lineHeightPx = (max(0.0, finiteOr(p.textLineHeight, 0.0)) * density).toFloat(),
|
|
94
|
+
prefixFontSize = finiteOr(p.prefixFontSize, size.toDouble()).toFloat(),
|
|
95
|
+
suffixFontSize = finiteOr(p.suffixFontSize, size.toDouble()).toFloat(),
|
|
96
|
+
fontWeight = clampedInt(p.textWeight, 100, 900, 400),
|
|
97
|
+
fontFamily = p.textFamily.ifEmpty { null },
|
|
98
|
+
letterSpacing = if (p.textLetterSpacing.isFinite()) p.textLetterSpacing.toFloat() else 0f,
|
|
99
|
+
prefixSpacing = p.prefixSpacing.takeIf { it.isFinite() }?.toFloat(),
|
|
100
|
+
suffixSpacing = p.suffixSpacing.takeIf { it.isFinite() }?.toFloat(),
|
|
101
|
+
allowFontScaling = p.allowFontScaling,
|
|
102
|
+
maxFontSizeMultiplier = max(0.0, finiteOr(p.maxFontSizeMultiplier, 0.0)).toFloat(),
|
|
103
|
+
)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** The frame's sizing half; the view adds the colours and the stroke width. */
|
|
107
|
+
internal fun sizingFrameOf(p: InputSizingProps) = Frame(
|
|
108
|
+
variant = when (p.variant) {
|
|
109
|
+
NitroInputVariant.OUTLINED -> NitroInputView.Variant.OUTLINED
|
|
110
|
+
NitroInputVariant.FILLED -> NitroInputView.Variant.FILLED
|
|
111
|
+
NitroInputVariant.NONE -> NitroInputView.Variant.NONE
|
|
112
|
+
},
|
|
113
|
+
label = p.label,
|
|
114
|
+
labelBehavior = when (p.labelBehavior) {
|
|
115
|
+
NitroInputLabelBehavior.ALWAYS -> NitroInputView.LabelBehavior.ALWAYS
|
|
116
|
+
NitroInputLabelBehavior.FLOAT -> NitroInputView.LabelBehavior.FLOAT
|
|
117
|
+
},
|
|
118
|
+
labelFontSize = max(0.0, finiteOr(p.labelFontSize, 0.0)).toFloat(),
|
|
119
|
+
cornerRadius = max(0.0, finiteOr(p.cornerRadius, 8.0)).toFloat(),
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* The room an outlined or filled frame keeps around its text and for its
|
|
124
|
+
* label, in px. The view lays itself out with these; [InputSizing] adds them
|
|
125
|
+
* to the size it reports. Mirrors `InputFrameMetrics` in InputSizing.swift.
|
|
126
|
+
*/
|
|
127
|
+
internal object InputFrame {
|
|
128
|
+
/** How far the label starts from the frame's edge, at a minimum. */
|
|
129
|
+
const val LABEL_INSET_DP = 16f
|
|
130
|
+
/** Past the corner's curve, so the notch never opens onto the arc. */
|
|
131
|
+
const val LABEL_CORNER_GAP_DP = 8f
|
|
132
|
+
/**
|
|
133
|
+
* The room a framed field keeps above and below its text. Flat, unlike the
|
|
134
|
+
* side inset: that one grows with `cornerRadius` because the *label* has to
|
|
135
|
+
* clear the corner curve, which is a horizontal problem.
|
|
136
|
+
*/
|
|
137
|
+
const val FRAME_PADDING_DP = 16f
|
|
138
|
+
const val FLOATED_LABEL_RATIO = 0.75f
|
|
139
|
+
const val MIN_FLOATED_LABEL_DP = 9f
|
|
140
|
+
/** A filled field's floated label line, as a multiple of its size (a fudge, not a Material metric). */
|
|
141
|
+
const val FILLED_LABEL_LINE = 1.4f
|
|
142
|
+
|
|
143
|
+
/** How far in the label - and so the text under it - starts. */
|
|
144
|
+
fun labelInsetPx(frame: Frame, density: Float): Float =
|
|
145
|
+
max(LABEL_INSET_DP * density, frame.cornerRadius * density + LABEL_CORNER_GAP_DP * density)
|
|
146
|
+
|
|
147
|
+
fun sideInsetPx(frame: Frame, density: Float): Float = if (frame.draws) labelInsetPx(frame, density) else 0f
|
|
148
|
+
|
|
149
|
+
fun floatedLabelSizePx(frame: Frame, restingSizePx: Float, density: Float): Float =
|
|
150
|
+
if (frame.labelFontSize > 0f) frame.labelFontSize * density
|
|
151
|
+
else max(MIN_FLOATED_LABEL_DP * density, restingSizePx * FLOATED_LABEL_RATIO)
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Room at the top for the floated label - which only the filled variant
|
|
155
|
+
* needs. Its label floats inside the box, on its own line above the text. An
|
|
156
|
+
* outlined field's label sits *on* the stroke, outside the content box: it
|
|
157
|
+
* intrudes into the top of the frame but never onto the text's line.
|
|
158
|
+
*/
|
|
159
|
+
fun topInsetPx(frame: Frame, restingSizePx: Float, density: Float): Float =
|
|
160
|
+
if (!frame.hasLabel || frame.variant != NitroInputView.Variant.FILLED) 0f
|
|
161
|
+
else floatedLabelSizePx(frame, restingSizePx, density) * FILLED_LABEL_LINE
|
|
162
|
+
|
|
163
|
+
/** The label at rest, in the body face and size: the widest it gets. */
|
|
164
|
+
fun restingLabelWidthPx(frame: Frame, fonts: InputFonts): Float {
|
|
165
|
+
if (!frame.hasLabel) return 0f
|
|
166
|
+
val body = fonts.paint(InputRole.BODY)
|
|
167
|
+
val paint = TextPaint(Paint.ANTI_ALIAS_FLAG)
|
|
168
|
+
paint.typeface = body.typeface
|
|
169
|
+
paint.textSize = body.textSize
|
|
170
|
+
return paint.measureText(frame.label)
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** A framed field's width: its content, or its label if wider, between the side insets. */
|
|
174
|
+
fun widthPx(contentPx: Float, frame: Frame, fonts: InputFonts, density: Float): Float {
|
|
175
|
+
if (!frame.draws) return contentPx
|
|
176
|
+
return 2f * sideInsetPx(frame, density) + max(contentPx, restingLabelWidthPx(frame, fonts))
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* A framed single line's height: the line with the frame's padding above and
|
|
181
|
+
* below it, and a filled field's label line. The line is centred in it, as it
|
|
182
|
+
* is in any height the caller gives. (A wrapping field adds its own.)
|
|
183
|
+
*/
|
|
184
|
+
fun singleLineHeightPx(linePx: Float, frame: Frame, fonts: InputFonts, density: Float): Float {
|
|
185
|
+
if (!frame.draws) return linePx
|
|
186
|
+
return linePx + topInsetPx(frame, fonts.paint(InputRole.BODY).textSize, density) + 2f * FRAME_PADDING_DP * density
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* What a field shows for its format: the text conformed to it, the placeholder
|
|
192
|
+
* and `keepPlaceholder`'s tail. Pure functions of the format and the shared C++
|
|
193
|
+
* engines, which the view holds for its life and a measure configures afresh.
|
|
194
|
+
*/
|
|
195
|
+
internal object InputText {
|
|
196
|
+
/** Points [formatter] and [maskEngine] at [value]; [old] skips a mask that did not change. */
|
|
197
|
+
fun configure(formatter: AmountFormatter, maskEngine: MaskEngine, old: Format?, value: Format) {
|
|
198
|
+
formatter.setFormat(value.fractionDigits, value.maxIntegerDigits, value.groupingSeparator, value.decimalSeparator)
|
|
199
|
+
val maskChanged = old == null || old.mask != value.mask || old.maskNotations != value.maskNotations ||
|
|
200
|
+
old.maskAffinityFormats != value.maskAffinityFormats || old.maskAffinityStrategy != value.maskAffinityStrategy ||
|
|
201
|
+
old.maskTextCase != value.maskTextCase || old.maskCharacterMap != value.maskCharacterMap ||
|
|
202
|
+
old.maskSlotPlaceholder != value.maskSlotPlaceholder
|
|
203
|
+
if (value.mode == Mode.MASK && (old == null || old.mode != Mode.MASK || maskChanged)) {
|
|
204
|
+
maskEngine.clearNotations()
|
|
205
|
+
value.maskNotations.forEach { maskEngine.addNotation(it.character, it.characterSet, it.isOptional) }
|
|
206
|
+
maskEngine.clearAffinityFormats()
|
|
207
|
+
value.maskAffinityFormats.forEach { maskEngine.addAffinityFormat(it) }
|
|
208
|
+
maskEngine.setAffinityStrategy(value.maskAffinityStrategy)
|
|
209
|
+
maskEngine.setTextCase(value.maskTextCase)
|
|
210
|
+
maskEngine.clearCharacterMap()
|
|
211
|
+
value.maskCharacterMap.forEach { maskEngine.addCharacterMapping(it.from, it.to) }
|
|
212
|
+
maskEngine.setSlotPlaceholder(value.maskSlotPlaceholder)
|
|
213
|
+
// A bad pattern leaves the engine inactive; the field then behaves as
|
|
214
|
+
// plain text rather than refusing every keystroke.
|
|
215
|
+
maskEngine.setFormat(value.mask)
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** [value] as the format holds it: formatted, masked, or cut to `maxLength` code points. */
|
|
220
|
+
fun conform(value: String, format: Format, formatter: AmountFormatter, maskEngine: MaskEngine, maxLength: Int): String {
|
|
221
|
+
if (format.mode == Mode.NUMBER) return formatter.normalize(value)
|
|
222
|
+
if (format.mode == Mode.MASK) {
|
|
223
|
+
return maskEngine.applyAll(value, value.codePointCount(0, value.length), true, format.maskAutocomplete, false)
|
|
224
|
+
}
|
|
225
|
+
if (maxLength <= 0) return value
|
|
226
|
+
val total = value.codePointCount(0, value.length)
|
|
227
|
+
return value.substring(0, value.offsetByCodePoints(0, maxLength.coerceIn(0, total)))
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* `keepPlaceholder` without a `placeholder` shows the mask itself, empty
|
|
232
|
+
* slots as `maskSlotPlaceholder`: "+1 (___) ___-____".
|
|
233
|
+
*/
|
|
234
|
+
fun basePlaceholder(format: Format, maskEngine: MaskEngine): String {
|
|
235
|
+
if (format.placeholder.isEmpty() && format.mode == Mode.MASK && format.keepPlaceholder && maskEngine.isActive()) {
|
|
236
|
+
maskEngine.applyAll("", 0, true, false, false)
|
|
237
|
+
return maskEngine.lastTailPlaceholder()
|
|
238
|
+
}
|
|
239
|
+
return format.placeholder
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* `keepPlaceholder`: what is still to come after the typed text, drawn in the
|
|
244
|
+
* placeholder colour. The caller's placeholder from where the text ends -
|
|
245
|
+
* "1234 5" leaves "678 9012" of "1234 5678 9012" - or, without one, the rest
|
|
246
|
+
* of the mask. The placeholder is written for `mask`, so when one of
|
|
247
|
+
* `maskAffinityFormats` has taken over (a 4-6-5 Amex under a 4-4-4-4
|
|
248
|
+
* placeholder) its characters no longer line up, and that format's own tail
|
|
249
|
+
* is shown instead. A complete text has nothing to come.
|
|
250
|
+
*/
|
|
251
|
+
fun ghostTail(text: String, format: Format, maskEngine: MaskEngine, secure: Boolean): String {
|
|
252
|
+
if (format.mode != Mode.MASK || !format.keepPlaceholder || text.isEmpty() || secure || !maskEngine.isActive()) {
|
|
253
|
+
return ""
|
|
254
|
+
}
|
|
255
|
+
val typed = text.codePointCount(0, text.length)
|
|
256
|
+
maskEngine.applyAll(text, typed, true, false, false)
|
|
257
|
+
if (maskEngine.lastComplete()) return ""
|
|
258
|
+
val placeholder = format.placeholder
|
|
259
|
+
if (placeholder.isNotEmpty() && maskEngine.lastFormatIndex() == 0) {
|
|
260
|
+
val total = placeholder.codePointCount(0, placeholder.length)
|
|
261
|
+
return if (typed < total) placeholder.substring(placeholder.offsetByCodePoints(0, typed)) else ""
|
|
262
|
+
}
|
|
263
|
+
return maskEngine.lastTailPlaceholder()
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/** `secureTextEntry` draws a bullet for every code point. */
|
|
267
|
+
fun bullets(text: String): String = "•".repeat(text.codePointCount(0, text.length))
|
|
268
|
+
|
|
269
|
+
/** A leading minus, in either the keyboard's spelling or the typographic one. */
|
|
270
|
+
fun isSign(c: Char): Boolean = c == '-' || c == '−'
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/** Receives one glyph of [InputGlyphs.walk]: its code point, role, whether it is placeholder, and its advance in px. */
|
|
274
|
+
internal fun interface GlyphSink {
|
|
275
|
+
fun glyph(codePoint: Int, role: InputRole, placeholder: Boolean, advance: Float)
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* The glyphs a reflowing field lays out, in order, with the advance each takes:
|
|
280
|
+
* the prefix, the text (or the placeholder), `keepPlaceholder`'s tail and the
|
|
281
|
+
* suffix. The view hands them to the engine; [InputSizing] adds them up, which
|
|
282
|
+
* is the engine's width at rest.
|
|
283
|
+
*/
|
|
284
|
+
internal object InputGlyphs {
|
|
285
|
+
/**
|
|
286
|
+
* [shown] is the text or, when [showPlaceholder], the placeholder; [tail]
|
|
287
|
+
* is ignored then. Advances carry the letter spacing; the last prefix glyph
|
|
288
|
+
* and the last body glyph before a suffix carry the seams instead.
|
|
289
|
+
*/
|
|
290
|
+
fun walk(f: InputFonts, format: Format, shown: String, showPlaceholder: Boolean, tail: String, sink: GlyphSink) {
|
|
291
|
+
// A negative amount reads "-$1,234.56", not "$-1,234.56": the sign belongs
|
|
292
|
+
// to the amount, not to the digits after the symbol. It stays a *body*
|
|
293
|
+
// glyph - it is part of the text, and the caret counts body glyphs in the
|
|
294
|
+
// order they are added - and is only laid out ahead of the prefix.
|
|
295
|
+
val signed = format.signBeforeAffix && !showPlaceholder && format.prefix.isNotEmpty() &&
|
|
296
|
+
shown.isNotEmpty() && InputText.isSign(shown[0])
|
|
297
|
+
if (signed) run(shown.substring(0, 1), InputRole.BODY, f, false, null, sink)
|
|
298
|
+
run(format.prefix, InputRole.PREFIX, f, false, f.prefixSeam, sink)
|
|
299
|
+
val kept = if (showPlaceholder) "" else tail
|
|
300
|
+
run(if (signed) shown.substring(1) else shown, InputRole.BODY, f, showPlaceholder,
|
|
301
|
+
if (format.suffix.isNotEmpty() && kept.isEmpty()) f.suffixSeam else null, sink)
|
|
302
|
+
// `keepPlaceholder`: the rest of the mask trails the text as placeholder
|
|
303
|
+
// glyphs. They come after every typed glyph, so the caret - which counts
|
|
304
|
+
// body glyphs in order - never lands among them.
|
|
305
|
+
run(kept, InputRole.BODY, f, true, if (format.suffix.isNotEmpty()) f.suffixSeam else null, sink)
|
|
306
|
+
run(format.suffix, InputRole.SUFFIX, f, false, null, sink)
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
private fun run(s: String, role: InputRole, f: InputFonts, placeholder: Boolean, lastSpacing: Float?, sink: GlyphSink) {
|
|
310
|
+
val spacing = f.spacing(role)
|
|
311
|
+
var i = 0
|
|
312
|
+
while (i < s.length) {
|
|
313
|
+
val cp = s.codePointAt(i)
|
|
314
|
+
val next = i + Character.charCount(cp)
|
|
315
|
+
val after = if (next >= s.length && lastSpacing != null) lastSpacing else spacing
|
|
316
|
+
sink.glyph(cp, role, placeholder, f.width(cp, role) + after)
|
|
317
|
+
i = next
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/** The size a single-line field reports through `onSizeChange`, worked out the same way before and after it mounts. */
|
|
323
|
+
internal object InputSizing {
|
|
324
|
+
/**
|
|
325
|
+
* A plain field's content width in px: the text (or the placeholder) and the
|
|
326
|
+
* affixes measured with the paints the field draws them in. Its own letter
|
|
327
|
+
* spacing follows every glyph; the affixes take their room with their seams.
|
|
328
|
+
*/
|
|
329
|
+
fun plainWidthPx(f: InputFonts, format: Format, body: String): Float =
|
|
330
|
+
f.affixRoom(format.prefix, InputRole.PREFIX) +
|
|
331
|
+
f.paint(InputRole.BODY).measureText(body) + f.bodySpacing * body.codePointCount(0, body.length) +
|
|
332
|
+
f.affixRoom(format.suffix, InputRole.SUFFIX)
|
|
333
|
+
|
|
334
|
+
/** What a plain field measures: the placeholder when empty, bullets when secure, else the text and its tail. */
|
|
335
|
+
fun plainBody(text: String, hint: String, tail: String, secure: Boolean): String = when {
|
|
336
|
+
text.isEmpty() -> hint
|
|
337
|
+
secure -> InputText.bullets(text)
|
|
338
|
+
else -> text + tail
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/** A reflowing field's width in px at rest: the engine's target, the sum of its glyphs' advances. */
|
|
342
|
+
fun reflowWidthPx(f: InputFonts, format: Format, text: String, hint: String, tail: String, secure: Boolean): Float {
|
|
343
|
+
val body = if (secure) InputText.bullets(text) else text
|
|
344
|
+
val showPlaceholder = text.isEmpty() && hint.isNotEmpty()
|
|
345
|
+
var width = 0.0
|
|
346
|
+
// The engine clamps every advance at 0 (a negative letter spacing).
|
|
347
|
+
InputGlyphs.walk(f, format, if (showPlaceholder) hint else body, showPlaceholder, tail) { _, _, _, advance ->
|
|
348
|
+
width += max(0.0, advance.toDouble())
|
|
349
|
+
}
|
|
350
|
+
return width.toFloat()
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/** Content width and line height in px to the reported size in dp. */
|
|
354
|
+
fun reported(widthPx: Float, heightPx: Float, density: Float): NitroInputSize =
|
|
355
|
+
NitroInputSize(ceil(widthPx / density + 2f).toDouble(), ceil(heightPx / density).toDouble())
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* [NitroInputSizing] read through [InputSizingProps], so [formatOf] and
|
|
360
|
+
* [sizingTypographyOf] serve the measure as they do the view.
|
|
361
|
+
*/
|
|
362
|
+
internal class SizingProps(s: NitroInputSizing) : InputSizingProps {
|
|
363
|
+
override val mode = s.mode
|
|
364
|
+
override val mask = s.mask
|
|
365
|
+
override val maskNotations = s.maskNotations
|
|
366
|
+
override val maskAutocomplete = s.maskAutocomplete
|
|
367
|
+
override val maskAutoSkip = s.maskAutoSkip
|
|
368
|
+
override val maskAffinityFormats = s.maskAffinityFormats
|
|
369
|
+
override val maskAffinityStrategy = s.maskAffinityStrategy
|
|
370
|
+
override val maskTextCase = s.maskTextCase
|
|
371
|
+
override val maskCharacterMap = s.maskCharacterMap
|
|
372
|
+
override val maskSlotPlaceholder = s.maskSlotPlaceholder
|
|
373
|
+
override val keepPlaceholder = s.keepPlaceholder
|
|
374
|
+
override val fractionDigits = s.fractionDigits
|
|
375
|
+
override val maxIntegerDigits = s.maxIntegerDigits
|
|
376
|
+
override val groupingSeparator = s.groupingSeparator
|
|
377
|
+
override val decimalSeparator = s.decimalSeparator
|
|
378
|
+
override val prefix = s.prefix
|
|
379
|
+
override val suffix = s.suffix
|
|
380
|
+
override val signPlacement = s.signPlacement
|
|
381
|
+
override val placeholder = s.placeholder
|
|
382
|
+
override val textSize = s.textSize
|
|
383
|
+
override val textLineHeight = s.textLineHeight
|
|
384
|
+
override val prefixFontSize = s.prefixFontSize
|
|
385
|
+
override val suffixFontSize = s.suffixFontSize
|
|
386
|
+
override val textWeight = s.textWeight
|
|
387
|
+
override val textFamily = s.textFamily
|
|
388
|
+
override val textLetterSpacing = s.textLetterSpacing
|
|
389
|
+
override val prefixSpacing = s.prefixSpacing
|
|
390
|
+
override val suffixSpacing = s.suffixSpacing
|
|
391
|
+
override val allowFontScaling = s.allowFontScaling
|
|
392
|
+
override val maxFontSizeMultiplier = s.maxFontSizeMultiplier
|
|
393
|
+
override val variant = s.variant
|
|
394
|
+
override val label = s.label
|
|
395
|
+
override val labelBehavior = s.labelBehavior
|
|
396
|
+
override val labelFontSize = s.labelFontSize
|
|
397
|
+
override val cornerRadius = s.cornerRadius
|
|
398
|
+
}
|