react-native-nitro-input 0.4.0 → 0.4.3
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 +44 -0
- package/README.md +2 -1
- package/android/src/main/java/com/margelo/nitro/nitroinput/DigitSlots.kt +143 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroInputView.kt +6 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroNumberView.kt +24 -1
- package/android/src/main/java/com/margelo/nitro/nitroinput/InputFonts.kt +50 -5
- package/android/src/main/java/com/margelo/nitro/nitroinput/InputSizing.kt +7 -1
- package/android/src/main/java/com/margelo/nitro/nitroinput/NitroInputView.kt +117 -6
- package/android/src/main/java/com/margelo/nitro/nitroinput/NitroNumberView.kt +290 -137
- package/ios/HybridNitroInputView.swift +4 -0
- package/ios/InputSizing.swift +98 -4
- package/ios/NitroInputView.swift +79 -12
- package/lib/commonjs/nitrogen/generated/shared/json/NitroInputViewConfig.json +2 -0
- package/lib/commonjs/src/NitroInput.js +3 -1
- package/lib/commonjs/src/NitroNumber.js +94 -29
- package/lib/module/nitrogen/generated/shared/json/NitroInputViewConfig.json +2 -0
- package/lib/module/src/NitroInput.js +3 -1
- package/lib/module/src/NitroNumber.js +94 -29
- package/lib/typescript/src/NitroInput.d.ts +11 -0
- package/lib/typescript/src/specs/NitroInput.nitro.d.ts +4 -0
- package/lib/typescript/src/specs/NitroInputMeasure.nitro.d.ts +2 -0
- package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.cpp +18 -0
- package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.hpp +4 -0
- package/nitrogen/generated/android/c++/JNitroInputSizing.hpp +9 -1
- package/nitrogen/generated/android/c++/views/JHybridNitroInputViewStateUpdater.cpp +10 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroInputViewSpec.kt +12 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputSizing.kt +12 -2
- package/nitrogen/generated/ios/c++/HybridNitroInputViewSpecSwift.hpp +12 -0
- package/nitrogen/generated/ios/c++/views/HybridNitroInputViewComponent.mm +12 -0
- package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec.swift +2 -0
- package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec_cxx.swift +22 -0
- package/nitrogen/generated/ios/swift/NitroInputSizing.swift +12 -2
- package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.cpp +4 -0
- package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.hpp +4 -0
- package/nitrogen/generated/shared/c++/NitroInputSizing.hpp +9 -1
- package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.cpp +4 -0
- package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.hpp +6 -0
- package/nitrogen/generated/shared/json/NitroInputViewConfig.json +2 -0
- package/package.json +1 -1
- package/src/NitroInput.tsx +14 -0
- package/src/NitroNumber.tsx +121 -54
- package/src/specs/NitroInput.nitro.ts +4 -0
- package/src/specs/NitroInputMeasure.nitro.ts +2 -0
|
@@ -394,11 +394,14 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
394
394
|
/**
|
|
395
395
|
* This font set's digit masks by role (digit, fraction), digit and level,
|
|
396
396
|
* filled from the shared cache: a swapping glyph looks two up every frame,
|
|
397
|
-
* and a lookup there built a key string each time.
|
|
398
|
-
* thread
|
|
397
|
+
* and a lookup there built a key string each time. Filled by the UI
|
|
398
|
+
* thread only: a queued mask lands in the shared cache, never here.
|
|
399
399
|
*/
|
|
400
400
|
private val digitMasks = AtomicReferenceArray<Bitmap>(2 * 10 * (NUMERIC_BLUR_LEVELS + 1))
|
|
401
401
|
|
|
402
|
+
/** Stands for this font set in the warm queue ([WarmExecutor.cancel]), without being it. */
|
|
403
|
+
val warmToken = Any()
|
|
404
|
+
|
|
402
405
|
fun strip(role: GlyphRole, blankZero: Boolean, modulus: Int): Strip? {
|
|
403
406
|
val cell = digitWidth(role)
|
|
404
407
|
if (cell <= 0f || lineHeight <= 0f) return null
|
|
@@ -440,8 +443,6 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
440
443
|
* at its role's height in the line box.
|
|
441
444
|
*/
|
|
442
445
|
fun blurred(role: GlyphRole, digitIndex: Int, level: Int): Bitmap? {
|
|
443
|
-
val cell = digitWidth(role)
|
|
444
|
-
if (cell <= 0f || lineHeight <= 0f) return null
|
|
445
446
|
// Level 0 (or no blur at all) is the glyph unblurred, still a mask: a
|
|
446
447
|
// swapping glyph's two copies must be the same kind of thing, drawn the
|
|
447
448
|
// same way at the same sub-pixel position. Hardware text snaps to whole
|
|
@@ -450,48 +451,68 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
450
451
|
val slot = (if (role == GlyphRole.FRACTION) 10 * (NUMERIC_BLUR_LEVELS + 1) else 0) +
|
|
451
452
|
digitIndex * (NUMERIC_BLUR_LEVELS + 1) + level.coerceIn(0, NUMERIC_BLUR_LEVELS)
|
|
452
453
|
digitMasks.get(slot)?.let { return it }
|
|
453
|
-
|
|
454
|
-
val
|
|
455
|
-
|
|
454
|
+
// A blur the UI thread wants goes to the warm thread, with the warm thread's paint.
|
|
455
|
+
val queued = level > 0 && isMainThread()
|
|
456
|
+
val spec = digitMaskSpec(role, digitIndex, level, if (queued) maskPaints[role.ordinal] else threadPaint(role)) ?: return null
|
|
457
|
+
if (queued && spec.radius > 0) {
|
|
456
458
|
// Not warmed yet (a font or size that just changed): blur it on the
|
|
457
459
|
// warm thread and draw the sharp mask this frame. A blur in onDraw was
|
|
458
460
|
// ANRing low-end phones, see [BlurredGlyphCache].
|
|
459
|
-
val ready = BlurredGlyphCache.peek(cacheKey)
|
|
461
|
+
val ready = BlurredGlyphCache.peek(spec.cacheKey)
|
|
460
462
|
if (ready == null) {
|
|
461
|
-
WarmExecutor.request(
|
|
463
|
+
WarmExecutor.request(spec)
|
|
462
464
|
return blurred(role, digitIndex, 0)
|
|
463
465
|
}
|
|
464
466
|
digitMasks.set(slot, ready)
|
|
465
467
|
return ready
|
|
466
468
|
}
|
|
467
|
-
val mask =
|
|
468
|
-
val text = glyphs[digitIndex]
|
|
469
|
-
val pad = blurPad
|
|
470
|
-
val w = ceil(cell).toInt() + 2 * pad
|
|
471
|
-
val h = ceil(lineHeight).toInt() + 2 * pad
|
|
472
|
-
// An alpha mask, not a coloured bitmap: the glyph's coverage, blurred,
|
|
473
|
-
// that a draw fills with the digit paint's colour. A coloured
|
|
474
|
-
// ARGB_8888 bitmap round-tripped through get/setPixels came back with
|
|
475
|
-
// its whole rectangle faintly inked on the GPU; a mask has no colour
|
|
476
|
-
// to get wrong, and is a quarter of the memory.
|
|
477
|
-
val mask = Bitmap.createBitmap(w, h, Bitmap.Config.ALPHA_8)
|
|
478
|
-
val canvas = Canvas(mask)
|
|
479
|
-
// A paint of its own: the masks are warmed off the main thread, where
|
|
480
|
-
// the digit paint's alpha is being changed by the draws.
|
|
481
|
-
val paint = TextPaint(threadPaint(role)).apply { alpha = 255 }
|
|
482
|
-
canvas.drawText(text, pad + (cell - width(text, role)) / 2f, pad + digitBaseline(role), paint)
|
|
483
|
-
if (radius > 0) boxBlurMask(mask, radius)
|
|
484
|
-
// Immutable and uploaded ahead of its first draw: HWUI re-pins a mutable
|
|
485
|
-
// bitmap at every sync, and the first frame that drew a fresh mask paid
|
|
486
|
-
// for its texture upload.
|
|
487
|
-
val done = mask.copy(Bitmap.Config.ALPHA_8, false) ?: mask
|
|
488
|
-
if (done !== mask) mask.recycle()
|
|
489
|
-
done
|
|
490
|
-
}
|
|
469
|
+
val mask = spec.make()
|
|
491
470
|
digitMasks.set(slot, mask)
|
|
492
471
|
return mask
|
|
493
472
|
}
|
|
494
473
|
|
|
474
|
+
/**
|
|
475
|
+
* What [blurred] draws for [role]'s digit [digitIndex] at [level], as a
|
|
476
|
+
* [MaskSpec]: a paint and a few numbers, so a mask queued for the warm
|
|
477
|
+
* thread holds neither this font set nor the view. Measured on the calling
|
|
478
|
+
* thread; [paint] is the one the mask will be drawn with (the warm
|
|
479
|
+
* thread's own copy, [maskPaints], for a queued one).
|
|
480
|
+
*/
|
|
481
|
+
private fun digitMaskSpec(role: GlyphRole, digitIndex: Int, level: Int, paint: TextPaint): MaskSpec? {
|
|
482
|
+
val cell = digitWidth(role)
|
|
483
|
+
if (cell <= 0f || lineHeight <= 0f) return null
|
|
484
|
+
val radius = blurRadius(level)
|
|
485
|
+
val pad = blurPad
|
|
486
|
+
val text = glyphs[digitIndex]
|
|
487
|
+
return MaskSpec(
|
|
488
|
+
cacheKey = "${key(role)}|$digitIndex|$radius|$pad",
|
|
489
|
+
paint = paint,
|
|
490
|
+
text = text,
|
|
491
|
+
left = pad + (cell - width(text, role)) / 2f,
|
|
492
|
+
baseline = pad + digitBaseline(role),
|
|
493
|
+
width = ceil(cell).toInt() + 2 * pad,
|
|
494
|
+
height = ceil(lineHeight).toInt() + 2 * pad,
|
|
495
|
+
radius = radius,
|
|
496
|
+
)
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
/**
|
|
500
|
+
* Every digit mask of the numeric transition (ten digits at every blur
|
|
501
|
+
* level, and the fraction's when it has one), for the warm thread.
|
|
502
|
+
*/
|
|
503
|
+
fun swapMaskSpecs(fraction: Boolean): List<MaskSpec> {
|
|
504
|
+
val roles = if (fraction) listOf(GlyphRole.DIGIT, GlyphRole.FRACTION) else listOf(GlyphRole.DIGIT)
|
|
505
|
+
val specs = ArrayList<MaskSpec>(roles.size * 10 * (NUMERIC_BLUR_LEVELS + 1))
|
|
506
|
+
for (role in roles) for (digit in 0..9) for (level in 0..NUMERIC_BLUR_LEVELS) {
|
|
507
|
+
digitMaskSpec(role, digit, level, maskPaints[role.ordinal])?.let { specs.add(it) }
|
|
508
|
+
}
|
|
509
|
+
return specs
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
/** The blur radius of [level], in px (0: sharp). */
|
|
513
|
+
private fun blurRadius(level: Int): Int =
|
|
514
|
+
if (level <= 0 || numericBlur <= 0f) 0 else Math.round(lineHeight * numericBlur * level / NUMERIC_BLUR_LEVELS).coerceAtLeast(1)
|
|
515
|
+
|
|
495
516
|
/**
|
|
496
517
|
* [text] of [role] as an alpha mask blurred to [level] (of
|
|
497
518
|
* [NUMERIC_BLUR_LEVELS]; 0 sharp), `width + 2 pad` by `lineHeight + 2 pad`
|
|
@@ -500,28 +521,31 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
500
521
|
*/
|
|
501
522
|
fun blurredText(text: String, role: GlyphRole, level: Int): Bitmap? {
|
|
502
523
|
if (text.isEmpty() || lineHeight <= 0f) return null
|
|
503
|
-
val
|
|
504
|
-
val
|
|
505
|
-
if (radius > 0
|
|
524
|
+
val queued = level > 0 && isMainThread()
|
|
525
|
+
val spec = textMaskSpec(text, role, level, if (queued) maskPaints[role.ordinal] else threadPaint(role))
|
|
526
|
+
if (queued && spec.radius > 0) {
|
|
506
527
|
// Never warmed ahead (any text can change into any other): blurred on
|
|
507
528
|
// the warm thread, sharp until it lands, as [blurred] does.
|
|
508
|
-
BlurredGlyphCache.peek(cacheKey)?.let { return it }
|
|
509
|
-
WarmExecutor.request(
|
|
529
|
+
BlurredGlyphCache.peek(spec.cacheKey)?.let { return it }
|
|
530
|
+
WarmExecutor.request(spec)
|
|
510
531
|
return blurredText(text, role, 0)
|
|
511
532
|
}
|
|
512
|
-
return
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
533
|
+
return spec.make()
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
private fun textMaskSpec(text: String, role: GlyphRole, level: Int, paint: TextPaint): MaskSpec {
|
|
537
|
+
val radius = blurRadius(level)
|
|
538
|
+
val pad = blurPad
|
|
539
|
+
return MaskSpec(
|
|
540
|
+
cacheKey = "${key(role)}|t|$text|$radius|$pad",
|
|
541
|
+
paint = paint,
|
|
542
|
+
text = text,
|
|
543
|
+
left = pad.toFloat(),
|
|
544
|
+
baseline = pad + baseline(role, text, 0f),
|
|
545
|
+
width = (ceil(width(text, role)).toInt() + 2 * pad).coerceAtLeast(1),
|
|
546
|
+
height = ceil(lineHeight).toInt() + 2 * pad,
|
|
547
|
+
radius = radius,
|
|
548
|
+
)
|
|
525
549
|
}
|
|
526
550
|
|
|
527
551
|
/** Padding around a blurred glyph's bitmap, enough for the blur's tail. */
|
|
@@ -700,56 +724,6 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
700
724
|
}
|
|
701
725
|
}
|
|
702
726
|
|
|
703
|
-
/**
|
|
704
|
-
* Blurs a single-colour glyph in place: the alpha channel gets three box
|
|
705
|
-
* passes of [radius] in each axis, and every pixel is put back as [color]
|
|
706
|
-
* at its blurred coverage.
|
|
707
|
-
*/
|
|
708
|
-
/**
|
|
709
|
-
* Blurs an ALPHA_8 [mask] in place: three box passes of [radius], which is a
|
|
710
|
-
* Gaussian to the eye with a standard deviation of about the radius.
|
|
711
|
-
*/
|
|
712
|
-
private fun boxBlurMask(mask: Bitmap, radius: Int) {
|
|
713
|
-
val w = mask.width
|
|
714
|
-
val h = mask.height
|
|
715
|
-
val stride = mask.rowBytes
|
|
716
|
-
// Reused, not allocated per mask: two float copies of a large glyph went
|
|
717
|
-
// to the large-object heap, and a screenful of masks collected mid-frame.
|
|
718
|
-
val buffers = BlurBuffers.get(stride * h, w * h)
|
|
719
|
-
val bytes = buffers.bytes
|
|
720
|
-
bytes.clear()
|
|
721
|
-
bytes.limit(stride * h)
|
|
722
|
-
mask.copyPixelsToBuffer(bytes)
|
|
723
|
-
val alpha = buffers.alpha
|
|
724
|
-
val scratch = buffers.scratch
|
|
725
|
-
for (i in 0 until w * h) alpha[i] = (bytes.get((i / w) * stride + i % w).toInt() and 0xff).toFloat()
|
|
726
|
-
val window = (2 * radius + 1).toFloat()
|
|
727
|
-
repeat(3) {
|
|
728
|
-
// horizontal
|
|
729
|
-
for (y in 0 until h) {
|
|
730
|
-
val row = y * w
|
|
731
|
-
var sum = 0f
|
|
732
|
-
for (x in -radius..radius) sum += alpha[row + x.coerceIn(0, w - 1)]
|
|
733
|
-
for (x in 0 until w) {
|
|
734
|
-
scratch[row + x] = sum / window
|
|
735
|
-
sum += alpha[row + (x + radius + 1).coerceIn(0, w - 1)] - alpha[row + (x - radius).coerceIn(0, w - 1)]
|
|
736
|
-
}
|
|
737
|
-
}
|
|
738
|
-
// vertical
|
|
739
|
-
for (x in 0 until w) {
|
|
740
|
-
var sum = 0f
|
|
741
|
-
for (y in -radius..radius) sum += scratch[y.coerceIn(0, h - 1) * w + x]
|
|
742
|
-
for (y in 0 until h) {
|
|
743
|
-
alpha[y * w + x] = sum / window
|
|
744
|
-
sum += scratch[(y + radius + 1).coerceIn(0, h - 1) * w + x] - scratch[(y - radius).coerceIn(0, h - 1) * w + x]
|
|
745
|
-
}
|
|
746
|
-
}
|
|
747
|
-
}
|
|
748
|
-
for (i in 0 until w * h) bytes.put((i / w) * stride + i % w, Math.round(alpha[i]).coerceIn(0, 255).toByte())
|
|
749
|
-
bytes.rewind()
|
|
750
|
-
mask.copyPixelsFromBuffer(bytes)
|
|
751
|
-
}
|
|
752
|
-
|
|
753
727
|
/** System font scale for `allowFontScaling`, capped by `maxFontSizeMultiplier`. */
|
|
754
728
|
private fun systemFontMultiplier(t: Typography): Float {
|
|
755
729
|
if (!t.allowFontScaling) return 1f
|
|
@@ -1171,30 +1145,101 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
1171
1145
|
invalidate()
|
|
1172
1146
|
}
|
|
1173
1147
|
|
|
1148
|
+
/** The font set whose swap masks were last queued, and whether with the fraction's. */
|
|
1149
|
+
private var warmedFonts: FontSet? = null
|
|
1150
|
+
private var warmedFraction = false
|
|
1151
|
+
|
|
1174
1152
|
/**
|
|
1175
1153
|
* Renders the numeric transition's glyph masks, every digit at every blur
|
|
1176
1154
|
* level, on a background thread before they are needed. Made on demand, a
|
|
1177
1155
|
* change's first frames each paid for a few software blurs on the UI
|
|
1178
1156
|
* thread and ran late; iOS warms its images the same way.
|
|
1157
|
+
*
|
|
1158
|
+
* Once per font set: the `timing` and the font setters both land here when
|
|
1159
|
+
* a view mounts. A new font set (a font, size or colour change) takes the
|
|
1160
|
+
* old one's warm-ups out of the queue first, unless another view still
|
|
1161
|
+
* wants them: on a colour animation they were never going to be drawn.
|
|
1179
1162
|
*/
|
|
1180
1163
|
private fun warmSwapMasks() {
|
|
1181
|
-
if (timing.transition != Transition.NUMERIC)
|
|
1164
|
+
if (timing.transition != Transition.NUMERIC) {
|
|
1165
|
+
cancelWarmUps()
|
|
1166
|
+
return
|
|
1167
|
+
}
|
|
1182
1168
|
val set = fonts
|
|
1183
1169
|
val fraction = format.fractionDigits > 0
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1170
|
+
if (set === warmedFonts && (warmedFraction || !fraction)) return
|
|
1171
|
+
if (set !== warmedFonts) cancelWarmUps()
|
|
1172
|
+
warmedFonts = set
|
|
1173
|
+
warmedFraction = fraction
|
|
1174
|
+
WarmExecutor.warm(set.warmToken, set.swapMaskSpecs(fraction))
|
|
1175
|
+
}
|
|
1176
|
+
|
|
1177
|
+
/** Takes this view's queued warm-ups out of the queue (those no other view asked for). */
|
|
1178
|
+
private fun cancelWarmUps() {
|
|
1179
|
+
val set = warmedFonts ?: return
|
|
1180
|
+
warmedFonts = null
|
|
1181
|
+
warmedFraction = false
|
|
1182
|
+
WarmExecutor.cancel(set.warmToken)
|
|
1183
|
+
}
|
|
1184
|
+
|
|
1185
|
+
/**
|
|
1186
|
+
* A glyph mask to make: the text, the paint to draw it with and where it
|
|
1187
|
+
* goes in the bitmap, measured on the UI thread. A nested class and plain
|
|
1188
|
+
* values, so a mask waiting in [WarmExecutor]'s queue holds a paint and a
|
|
1189
|
+
* few numbers, never a font set or the view: when the masks were lambdas
|
|
1190
|
+
* over the (inner) font set, every queued one kept its whole view alive,
|
|
1191
|
+
* strips and layers included, and a backlog of them ran low-end phones out
|
|
1192
|
+
* of memory.
|
|
1193
|
+
*/
|
|
1194
|
+
private class MaskSpec(
|
|
1195
|
+
val cacheKey: String,
|
|
1196
|
+
/** Never drawn with or changed by the UI thread once queued (a font set's mask paint). */
|
|
1197
|
+
val paint: TextPaint,
|
|
1198
|
+
val text: String,
|
|
1199
|
+
/** The text's left edge and baseline in the bitmap, padding included. */
|
|
1200
|
+
val left: Float,
|
|
1201
|
+
val baseline: Float,
|
|
1202
|
+
val width: Int,
|
|
1203
|
+
val height: Int,
|
|
1204
|
+
val radius: Int,
|
|
1205
|
+
) {
|
|
1206
|
+
/** The mask, from the shared cache or drawn (and blurred) now on the calling thread. */
|
|
1207
|
+
fun make(): Bitmap = BlurredGlyphCache.get(cacheKey) {
|
|
1208
|
+
// An alpha mask, not a coloured bitmap: the glyph's coverage, blurred,
|
|
1209
|
+
// that a draw fills with the digit paint's colour. A coloured
|
|
1210
|
+
// ARGB_8888 bitmap round-tripped through get/setPixels came back with
|
|
1211
|
+
// its whole rectangle faintly inked on the GPU; a mask has no colour
|
|
1212
|
+
// to get wrong, and is a quarter of the memory.
|
|
1213
|
+
val mask = Bitmap.createBitmap(width, height, Bitmap.Config.ALPHA_8)
|
|
1214
|
+
val canvas = Canvas(mask)
|
|
1215
|
+
// A paint of its own: the UI thread changes its paints' alpha as it draws.
|
|
1216
|
+
val paint = TextPaint(paint).apply { alpha = 255 }
|
|
1217
|
+
canvas.drawText(text, left, baseline, paint)
|
|
1218
|
+
if (radius > 0) boxBlurMask(mask, radius)
|
|
1219
|
+
// Immutable and uploaded ahead of its first draw: HWUI re-pins a mutable
|
|
1220
|
+
// bitmap at every sync, and the first frame that drew a fresh mask paid
|
|
1221
|
+
// for its texture upload.
|
|
1222
|
+
val done = mask.copy(Bitmap.Config.ALPHA_8, false) ?: mask
|
|
1223
|
+
if (done !== mask) mask.recycle()
|
|
1224
|
+
done
|
|
1189
1225
|
}
|
|
1190
1226
|
}
|
|
1191
1227
|
|
|
1192
1228
|
/**
|
|
1193
1229
|
* One background thread for glyph masks, shared by every view. A mask the UI
|
|
1194
1230
|
* thread is waiting on ([request]) goes to the front of the queue, ahead of
|
|
1195
|
-
* the ten-digits-at-every-level warm-ups ([
|
|
1231
|
+
* the ten-digits-at-every-level warm-ups ([warm]): behind a font change's
|
|
1196
1232
|
* warm-up, the glyph on screen stayed sharp for its whole swap.
|
|
1197
1233
|
*
|
|
1234
|
+
* Bounded both ways, and keyed by mask, so it cannot outgrow its one thread
|
|
1235
|
+
* however many fonts, sizes and colours come and go: a mask is queued once
|
|
1236
|
+
* however many views ask for it, a dropped view's or an old font set's
|
|
1237
|
+
* warm-ups are taken out ([cancel]), and past [MAX_WARM] (or [MAX_URGENT])
|
|
1238
|
+
* the oldest are dropped. A dropped warm-up costs at most a sharp glyph
|
|
1239
|
+
* for a swap's first frames, then [request] makes it. An unbounded queue
|
|
1240
|
+
* of masks for colours already gone grew to hundreds of thousands and
|
|
1241
|
+
* took the app down on low-end phones (0.4.1).
|
|
1242
|
+
*
|
|
1198
1243
|
* At background priority plus one step (Glide's choice): below the UI
|
|
1199
1244
|
* thread, not starved under load (Thread.MIN_PRIORITY is the lowest nice
|
|
1200
1245
|
* level there is). A mask that cannot be made (out of memory, say) is
|
|
@@ -1203,13 +1248,25 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
1203
1248
|
* on Android, and a failure retried every frame logged every frame. Any
|
|
1204
1249
|
* other Error is a bug and is not swallowed.
|
|
1205
1250
|
*/
|
|
1206
|
-
private object WarmExecutor
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1251
|
+
private object WarmExecutor {
|
|
1252
|
+
/** Warm-ups kept at most: four numeric typographies with fractions, ten digits at seven levels in two roles. */
|
|
1253
|
+
const val MAX_WARM = 4 * 2 * 10 * (NUMERIC_BLUR_LEVELS + 1)
|
|
1254
|
+
/** Masks frames are waiting on, kept at most (a screenful of swapping glyphs and texts is far fewer). */
|
|
1255
|
+
const val MAX_URGENT = 256
|
|
1256
|
+
private const val RETRY_MS = 2000L
|
|
1257
|
+
|
|
1258
|
+
private val lock = Object()
|
|
1259
|
+
/** Masks the UI thread is waiting on, newest first, and their keys (also while one is being made). */
|
|
1260
|
+
private val urgent = ArrayDeque<MaskSpec>()
|
|
1261
|
+
private val urgentKeys = HashSet<String>()
|
|
1262
|
+
/** Warm-ups by mask key, oldest first, each with the font sets ([FontSet.warmToken]) that want it. */
|
|
1263
|
+
private val warm = LinkedHashMap<String, WarmUp>()
|
|
1210
1264
|
/** When a mask last failed, by key: not asked for again until [RETRY_MS] later. */
|
|
1211
1265
|
private val failed = java.util.concurrent.ConcurrentHashMap<String, Long>()
|
|
1212
|
-
|
|
1266
|
+
|
|
1267
|
+
private class WarmUp(val spec: MaskSpec) {
|
|
1268
|
+
val owners = HashSet<Any>(2)
|
|
1269
|
+
}
|
|
1213
1270
|
|
|
1214
1271
|
init {
|
|
1215
1272
|
Thread({
|
|
@@ -1217,17 +1274,22 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
1217
1274
|
android.os.Process.THREAD_PRIORITY_BACKGROUND + android.os.Process.THREAD_PRIORITY_MORE_FAVORABLE
|
|
1218
1275
|
)
|
|
1219
1276
|
while (true) {
|
|
1220
|
-
val
|
|
1221
|
-
|
|
1277
|
+
val spec = try {
|
|
1278
|
+
next()
|
|
1222
1279
|
} catch (_: InterruptedException) {
|
|
1223
1280
|
continue
|
|
1224
1281
|
}
|
|
1225
1282
|
try {
|
|
1226
|
-
|
|
1283
|
+
if (BlurredGlyphCache.peek(spec.cacheKey) == null) spec.make()
|
|
1284
|
+
failed.remove(spec.cacheKey)
|
|
1227
1285
|
} catch (e: Exception) {
|
|
1286
|
+
failed[spec.cacheKey] = SystemClock.uptimeMillis()
|
|
1228
1287
|
android.util.Log.w("NitroNumber", "A glyph mask could not be made; the glyph is drawn sharp", e)
|
|
1229
1288
|
} catch (e: OutOfMemoryError) {
|
|
1289
|
+
failed[spec.cacheKey] = SystemClock.uptimeMillis()
|
|
1230
1290
|
android.util.Log.w("NitroNumber", "Out of memory making a glyph mask; the glyph is drawn sharp", e)
|
|
1291
|
+
} finally {
|
|
1292
|
+
synchronized(lock) { urgentKeys.remove(spec.cacheKey) }
|
|
1231
1293
|
}
|
|
1232
1294
|
}
|
|
1233
1295
|
}, "NitroNumberWarm").apply {
|
|
@@ -1236,31 +1298,68 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
1236
1298
|
}
|
|
1237
1299
|
}
|
|
1238
1300
|
|
|
1239
|
-
|
|
1240
|
-
|
|
1301
|
+
/** The next mask to make, waiting for one: a frame's before any warm-up. */
|
|
1302
|
+
private fun next(): MaskSpec = synchronized(lock) {
|
|
1303
|
+
while (urgent.isEmpty() && warm.isEmpty()) lock.wait()
|
|
1304
|
+
if (urgent.isNotEmpty()) return urgent.removeFirst()
|
|
1305
|
+
val oldest = warm.values.iterator()
|
|
1306
|
+
val warmUp = oldest.next()
|
|
1307
|
+
oldest.remove()
|
|
1308
|
+
// Kept in [urgentKeys] while it is made, so a frame asking meanwhile doesn't queue it again.
|
|
1309
|
+
urgentKeys.add(warmUp.spec.cacheKey)
|
|
1310
|
+
warmUp.spec
|
|
1311
|
+
}
|
|
1312
|
+
|
|
1313
|
+
private fun recentlyFailed(key: String): Boolean {
|
|
1314
|
+
val at = failed[key] ?: return false
|
|
1315
|
+
return SystemClock.uptimeMillis() - at < RETRY_MS
|
|
1316
|
+
}
|
|
1317
|
+
|
|
1318
|
+
/** Makes [spec]'s mask on the warm thread next, once however many frames ask for it while it is queued. */
|
|
1319
|
+
fun request(spec: MaskSpec) {
|
|
1320
|
+
val key = spec.cacheKey
|
|
1321
|
+
if (recentlyFailed(key)) return
|
|
1322
|
+
synchronized(lock) {
|
|
1323
|
+
if (!urgentKeys.add(key)) return
|
|
1324
|
+
warm.remove(key)
|
|
1325
|
+
urgent.addFirst(spec)
|
|
1326
|
+
while (urgent.size > MAX_URGENT) urgentKeys.remove(urgent.removeLast().cacheKey)
|
|
1327
|
+
lock.notifyAll()
|
|
1328
|
+
}
|
|
1241
1329
|
}
|
|
1242
1330
|
|
|
1243
|
-
/**
|
|
1244
|
-
fun
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
} catch (e: Exception) {
|
|
1253
|
-
failed[key] = SystemClock.uptimeMillis()
|
|
1254
|
-
throw e
|
|
1255
|
-
} catch (e: OutOfMemoryError) {
|
|
1256
|
-
failed[key] = SystemClock.uptimeMillis()
|
|
1257
|
-
throw e
|
|
1258
|
-
} finally {
|
|
1259
|
-
pending.remove(key)
|
|
1260
|
-
}
|
|
1331
|
+
/** Queues [specs] behind every frame's masks, for the font set [owner] stands for. */
|
|
1332
|
+
fun warm(owner: Any, specs: List<MaskSpec>) {
|
|
1333
|
+
// Outside the lock: the cache has a lock of its own.
|
|
1334
|
+
val missing = specs.filter { BlurredGlyphCache.peek(it.cacheKey) == null && !recentlyFailed(it.cacheKey) }
|
|
1335
|
+
if (missing.isEmpty()) return
|
|
1336
|
+
synchronized(lock) {
|
|
1337
|
+
for (spec in missing) {
|
|
1338
|
+
if (spec.cacheKey in urgentKeys) continue
|
|
1339
|
+
warm.getOrPut(spec.cacheKey) { WarmUp(spec) }.owners.add(owner)
|
|
1261
1340
|
}
|
|
1262
|
-
|
|
1341
|
+
val oldest = warm.values.iterator()
|
|
1342
|
+
while (warm.size > MAX_WARM && oldest.hasNext()) {
|
|
1343
|
+
oldest.next()
|
|
1344
|
+
oldest.remove()
|
|
1345
|
+
}
|
|
1346
|
+
lock.notifyAll()
|
|
1347
|
+
}
|
|
1263
1348
|
}
|
|
1349
|
+
|
|
1350
|
+
/** Takes out the warm-ups only [owner] wanted; the ones another font set wants too stay. */
|
|
1351
|
+
fun cancel(owner: Any) {
|
|
1352
|
+
synchronized(lock) {
|
|
1353
|
+
val each = warm.values.iterator()
|
|
1354
|
+
while (each.hasNext()) {
|
|
1355
|
+
val warmUp = each.next()
|
|
1356
|
+
if (warmUp.owners.remove(owner) && warmUp.owners.isEmpty()) each.remove()
|
|
1357
|
+
}
|
|
1358
|
+
}
|
|
1359
|
+
}
|
|
1360
|
+
|
|
1361
|
+
/** Warm-ups and frame requests waiting, for tests and diagnostics. */
|
|
1362
|
+
fun queued(): Int = synchronized(lock) { warm.size + urgent.size }
|
|
1264
1363
|
}
|
|
1265
1364
|
|
|
1266
1365
|
/** Per-thread buffers for [boxBlurMask], grown to the largest mask that thread has blurred. */
|
|
@@ -2112,6 +2211,7 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
2112
2211
|
*/
|
|
2113
2212
|
fun release() {
|
|
2114
2213
|
stopAnimation()
|
|
2214
|
+
cancelWarmUps()
|
|
2115
2215
|
engine.reset()
|
|
2116
2216
|
moving = false
|
|
2117
2217
|
wheels.clear()
|
|
@@ -2211,6 +2311,59 @@ class NitroNumberView(context: Context) : View(context) {
|
|
|
2211
2311
|
}
|
|
2212
2312
|
|
|
2213
2313
|
companion object {
|
|
2314
|
+
/**
|
|
2315
|
+
* Blurs an ALPHA_8 [mask] in place: three box passes of [radius], which is a
|
|
2316
|
+
* Gaussian to the eye with a standard deviation of about the radius.
|
|
2317
|
+
*/
|
|
2318
|
+
private fun boxBlurMask(mask: Bitmap, radius: Int) {
|
|
2319
|
+
val w = mask.width
|
|
2320
|
+
val h = mask.height
|
|
2321
|
+
val stride = mask.rowBytes
|
|
2322
|
+
// Reused, not allocated per mask: two float copies of a large glyph went
|
|
2323
|
+
// to the large-object heap, and a screenful of masks collected mid-frame.
|
|
2324
|
+
val buffers = BlurBuffers.get(stride * h, w * h)
|
|
2325
|
+
val bytes = buffers.bytes
|
|
2326
|
+
bytes.clear()
|
|
2327
|
+
bytes.limit(stride * h)
|
|
2328
|
+
mask.copyPixelsToBuffer(bytes)
|
|
2329
|
+
val alpha = buffers.alpha
|
|
2330
|
+
val scratch = buffers.scratch
|
|
2331
|
+
for (i in 0 until w * h) alpha[i] = (bytes.get((i / w) * stride + i % w).toInt() and 0xff).toFloat()
|
|
2332
|
+
val window = (2 * radius + 1).toFloat()
|
|
2333
|
+
repeat(3) {
|
|
2334
|
+
// horizontal
|
|
2335
|
+
for (y in 0 until h) {
|
|
2336
|
+
val row = y * w
|
|
2337
|
+
var sum = 0f
|
|
2338
|
+
for (x in -radius..radius) sum += alpha[row + x.coerceIn(0, w - 1)]
|
|
2339
|
+
for (x in 0 until w) {
|
|
2340
|
+
scratch[row + x] = sum / window
|
|
2341
|
+
sum += alpha[row + (x + radius + 1).coerceIn(0, w - 1)] - alpha[row + (x - radius).coerceIn(0, w - 1)]
|
|
2342
|
+
}
|
|
2343
|
+
}
|
|
2344
|
+
// vertical
|
|
2345
|
+
for (x in 0 until w) {
|
|
2346
|
+
var sum = 0f
|
|
2347
|
+
for (y in -radius..radius) sum += scratch[y.coerceIn(0, h - 1) * w + x]
|
|
2348
|
+
for (y in 0 until h) {
|
|
2349
|
+
alpha[y * w + x] = sum / window
|
|
2350
|
+
sum += scratch[(y + radius + 1).coerceIn(0, h - 1) * w + x] - scratch[(y - radius).coerceIn(0, h - 1) * w + x]
|
|
2351
|
+
}
|
|
2352
|
+
}
|
|
2353
|
+
}
|
|
2354
|
+
for (i in 0 until w * h) bytes.put((i / w) * stride + i % w, Math.round(alpha[i]).coerceIn(0, 255).toByte())
|
|
2355
|
+
bytes.rewind()
|
|
2356
|
+
mask.copyPixelsFromBuffer(bytes)
|
|
2357
|
+
}
|
|
2358
|
+
|
|
2359
|
+
|
|
2360
|
+
/**
|
|
2361
|
+
* Glyph masks waiting for the warm thread, warm-ups and frame requests
|
|
2362
|
+
* together; bounded (see WarmExecutor). For tests and diagnostics.
|
|
2363
|
+
*/
|
|
2364
|
+
@JvmStatic
|
|
2365
|
+
fun queuedGlyphMasks(): Int = WarmExecutor.queued()
|
|
2366
|
+
|
|
2214
2367
|
/** The glyphs drawn for 0…9 unless `digitGlyphs` gives ten others. */
|
|
2215
2368
|
private val DIGITS = Array(10) { it.toString() }
|
|
2216
2369
|
/** Flash paints a font set keeps per role (a change flash has an up and a down colour). */
|
|
@@ -118,6 +118,8 @@ final class HybridNitroInputView: HybridNitroInputViewSpec, RecyclableView {
|
|
|
118
118
|
var prefixAlign: NitroInputAffixAlign = .baseline { didSet { markConfigDirty() } }
|
|
119
119
|
var suffixAlign: NitroInputAffixAlign = .baseline { didSet { markConfigDirty() } }
|
|
120
120
|
var textLetterSpacing: Double = 0 { didSet { markConfigDirty() } }
|
|
121
|
+
var tabularNums: Bool = true { didSet { markConfigDirty() } }
|
|
122
|
+
var slotDigits: Bool = false { didSet { markConfigDirty() } }
|
|
121
123
|
var prefixSpacing: Double = .infinity { didSet { markConfigDirty() } }
|
|
122
124
|
var suffixSpacing: Double = .infinity { didSet { markConfigDirty() } }
|
|
123
125
|
var prefixOffset: Double = 0 { didSet { markConfigDirty() } }
|
|
@@ -337,6 +339,8 @@ final class HybridNitroInputView: HybridNitroInputViewSpec, RecyclableView {
|
|
|
337
339
|
prefixAlign = .baseline
|
|
338
340
|
suffixAlign = .baseline
|
|
339
341
|
textLetterSpacing = 0
|
|
342
|
+
tabularNums = true
|
|
343
|
+
slotDigits = false
|
|
340
344
|
prefixSpacing = .infinity
|
|
341
345
|
suffixSpacing = .infinity
|
|
342
346
|
prefixOffset = 0
|