@blotch/angular-lynx-native 22.3.1 → 22.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (100) hide show
  1. package/README.md +41 -15
  2. package/dist/android/project.d.ts +90 -1
  3. package/dist/android/project.d.ts.map +1 -1
  4. package/dist/android/project.js +369 -5
  5. package/dist/eject.d.ts +15 -1
  6. package/dist/eject.d.ts.map +1 -1
  7. package/dist/eject.js +13 -2
  8. package/dist/features.d.ts +70 -0
  9. package/dist/features.d.ts.map +1 -0
  10. package/dist/features.js +115 -0
  11. package/dist/generate.d.ts +17 -1
  12. package/dist/generate.d.ts.map +1 -1
  13. package/dist/generate.js +12 -4
  14. package/dist/index.d.ts +1 -0
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/index.js +8 -1
  17. package/dist/ios/build.d.ts.map +1 -1
  18. package/dist/ios/build.js +8 -2
  19. package/dist/ios/frameworks.d.ts +6 -5
  20. package/dist/ios/frameworks.d.ts.map +1 -1
  21. package/dist/ios/frameworks.js +46 -15
  22. package/dist/ios/project.d.ts +173 -11
  23. package/dist/ios/project.d.ts.map +1 -1
  24. package/dist/ios/project.js +767 -33
  25. package/dist/options.d.ts +140 -1
  26. package/dist/options.d.ts.map +1 -1
  27. package/dist/options.js +127 -0
  28. package/dist/permissions.d.ts +18 -1
  29. package/dist/permissions.d.ts.map +1 -1
  30. package/dist/permissions.js +63 -3
  31. package/package.json +1 -1
  32. package/templates/android/README.md +35 -2
  33. package/templates/android/angular-lynx.properties +2 -0
  34. package/templates/android/app/build.gradle.kts +51 -2
  35. package/templates/android/app/src/main/AndroidManifest.xml +14 -3
  36. package/templates/android/app/src/main/java/dev/angularlynx/shell/BarcodeScannerModule.kt +144 -0
  37. package/templates/android/app/src/main/java/dev/angularlynx/shell/BiometricsModule.kt +433 -0
  38. package/templates/android/app/src/main/java/dev/angularlynx/shell/BitmapPresenter.kt +98 -0
  39. package/templates/android/app/src/main/java/dev/angularlynx/shell/BitmapRendererContext.kt +175 -0
  40. package/templates/android/app/src/main/java/dev/angularlynx/shell/BluetoothModule.kt +1288 -0
  41. package/templates/android/app/src/main/java/dev/angularlynx/shell/CalendarModule.kt +127 -0
  42. package/templates/android/app/src/main/java/dev/angularlynx/shell/Canvas2DContext.kt +1557 -0
  43. package/templates/android/app/src/main/java/dev/angularlynx/shell/Canvas2DOps.kt +63 -0
  44. package/templates/android/app/src/main/java/dev/angularlynx/shell/CanvasFilter.kt +357 -0
  45. package/templates/android/app/src/main/java/dev/angularlynx/shell/CanvasUI.kt +4 -3
  46. package/templates/android/app/src/main/java/dev/angularlynx/shell/FilePickerModule.kt +263 -0
  47. package/templates/android/app/src/main/java/dev/angularlynx/shell/HapticsModule.kt +82 -0
  48. package/templates/android/app/src/main/java/dev/angularlynx/shell/MainActivity.kt +376 -6
  49. package/templates/android/app/src/main/java/dev/angularlynx/shell/ModuleBytes.kt +58 -0
  50. package/templates/android/app/src/main/java/dev/angularlynx/shell/MotionModule.kt +332 -0
  51. package/templates/android/app/src/main/java/dev/angularlynx/shell/NfcModule.kt +274 -0
  52. package/templates/android/app/src/main/java/dev/angularlynx/shell/PermissionRequests.kt +25 -0
  53. package/templates/android/app/src/main/java/dev/angularlynx/shell/PhotoLibraryModule.kt +168 -0
  54. package/templates/android/app/src/main/java/dev/angularlynx/shell/ShareTargetModule.kt +234 -0
  55. package/templates/android/app/src/main/java/dev/angularlynx/shell/ShellApplication.kt +6 -0
  56. package/templates/android/app/src/main/java/dev/angularlynx/shell/ShellModule.kt +47 -3
  57. package/templates/android/app/src/main/java/dev/angularlynx/shell/StoreReviewModule.kt +66 -0
  58. package/templates/android/app/src/main/java/dev/angularlynx/shell/SystemFontFamilies.kt +68 -0
  59. package/templates/android/app/src/main/java/dev/angularlynx/shell/TrackingModule.kt +74 -0
  60. package/templates/android/app/src/main/java/dev/angularlynx/shell/VideoFrameTarget.kt +429 -0
  61. package/templates/android/app/src/main/java/dev/angularlynx/shell/VideoUI.kt +676 -0
  62. package/templates/android/app/src/main/java/dev/angularlynx/shell/WebGLContext.kt +128 -13
  63. package/templates/android/app/src/main/java/dev/angularlynx/shell/WebGLModule.kt +493 -65
  64. package/templates/android/app/src/main/java/dev/angularlynx/shell/WifiModule.kt +429 -0
  65. package/templates/android/app/src/main/res/values/biometrics.xml +9 -0
  66. package/templates/android/app/src/main/res/values/share_target.xml +11 -0
  67. package/templates/android/app/src/main/res/xml/backup_rules.xml +5 -3
  68. package/templates/android/app/src/main/res/xml/data_extraction_rules.xml +6 -3
  69. package/templates/android/build.gradle.kts +1 -1
  70. package/templates/android/gradle/wrapper/gradle-wrapper.properties +2 -2
  71. package/templates/ios/App/AngularLynxBitmapRendererContext.m +188 -0
  72. package/templates/ios/App/AngularLynxCanvas2DContext.m +2467 -0
  73. package/templates/ios/App/AngularLynxCanvas2DOps.h +61 -0
  74. package/templates/ios/App/AngularLynxVideo.h +37 -0
  75. package/templates/ios/App/AngularLynxVideo.m +435 -0
  76. package/templates/ios/App/AngularLynxWebGL.h +69 -7
  77. package/templates/ios/App/AngularLynxWebGL.m +538 -29
  78. package/templates/ios/App/AngularLynxWebGLContext.m +148 -46
  79. package/templates/ios/App/AppDelegate.swift +34 -2
  80. package/templates/ios/App/AppleSignInButton.swift +106 -0
  81. package/templates/ios/App/AppleSignInModule.swift +222 -0
  82. package/templates/ios/App/BarcodeScannerModule.swift +284 -0
  83. package/templates/ios/App/BiometricsModule.swift +256 -0
  84. package/templates/ios/App/BluetoothModule.swift +1086 -0
  85. package/templates/ios/App/CalendarModule.swift +158 -0
  86. package/templates/ios/App/FilePickerModule.swift +240 -0
  87. package/templates/ios/App/HapticsModule.swift +90 -0
  88. package/templates/ios/App/Info.plist +2 -0
  89. package/templates/ios/App/LynxViewController.swift +210 -6
  90. package/templates/ios/App/MotionModule.swift +430 -0
  91. package/templates/ios/App/NfcModule.swift +346 -0
  92. package/templates/ios/App/PhotoLibraryModule.swift +169 -0
  93. package/templates/ios/App/ShareTargetModule.swift +150 -0
  94. package/templates/ios/App/ShellModule.swift +17 -0
  95. package/templates/ios/App/StoreReviewModule.swift +61 -0
  96. package/templates/ios/App/TrackingModule.swift +121 -0
  97. package/templates/ios/App/WifiModule.swift +237 -0
  98. package/templates/ios/App/XElementRegistry.swift +13 -3
  99. package/templates/ios/ShareExtension/Info.plist +42 -0
  100. package/templates/ios/ShareExtension/ShareViewController.swift +387 -0
@@ -0,0 +1,63 @@
1
+ // Generated from packages/runtime/src/lib/canvas/canvas-2d-ops.ts by
2
+ // packages/native/scripts/webgl-ops.ts: run `npm run generate:webgl-ops -w
3
+ // packages/native` rather than editing it.
4
+
5
+ package dev.angularlynx.shell
6
+
7
+ /**
8
+ * The commands of the 2D context's bridge, as `<canvas>`'s 2D context in
9
+ * the runtime records them: below 200 they return nothing, from 200 each
10
+ * answers one query.
11
+ */
12
+ internal object Canvas2DOps {
13
+ const val SAVE = 1
14
+ const val RESTORE = 2
15
+ const val RESET = 3
16
+ const val RESIZE = 4
17
+ const val PRESENT = 5
18
+ const val SET_TRANSFORM = 10
19
+ const val SET_GLOBAL_ALPHA = 11
20
+ const val SET_COMPOSITE_OPERATION = 12
21
+ const val SET_FILL_COLOR = 13
22
+ const val SET_FILL_GRADIENT = 14
23
+ const val SET_FILL_PATTERN = 15
24
+ const val SET_STROKE_COLOR = 16
25
+ const val SET_STROKE_GRADIENT = 17
26
+ const val SET_STROKE_PATTERN = 18
27
+ const val SET_LINE_WIDTH = 19
28
+ const val SET_LINE_CAP = 20
29
+ const val SET_LINE_JOIN = 21
30
+ const val SET_MITER_LIMIT = 22
31
+ const val SET_LINE_DASH = 23
32
+ const val SET_SHADOW = 24
33
+ const val SET_FONT = 25
34
+ const val SET_TEXT_ALIGN = 26
35
+ const val SET_TEXT_BASELINE = 27
36
+ const val SET_DIRECTION = 28
37
+ const val SET_LETTER_SPACING = 29
38
+ const val SET_IMAGE_SMOOTHING = 30
39
+ const val SET_FILTER = 31
40
+ const val CREATE_PATTERN = 44
41
+ const val RELEASE_OBJECT = 46
42
+ const val CLEAR_PATH = 50
43
+ const val FILL = 51
44
+ const val STROKE = 52
45
+ const val CLIP = 53
46
+ const val FILL_TEXT = 54
47
+ const val STROKE_TEXT = 55
48
+ const val DRAW_IMAGE = 56
49
+ const val PUT_IMAGE_DATA = 57
50
+ const val MEASURE_TEXT = 200
51
+ const val GET_IMAGE_DATA = 201
52
+ const val IS_POINT_IN_PATH = 202
53
+ const val IS_POINT_IN_STROKE = 203
54
+ }
55
+
56
+ /** The segments of a path, each followed by its points. */
57
+ internal object Canvas2DPathOps {
58
+ const val MOVE_TO = 0
59
+ const val LINE_TO = 1
60
+ const val QUAD_TO = 2
61
+ const val CUBIC_TO = 3
62
+ const val CLOSE = 4
63
+ }
@@ -0,0 +1,357 @@
1
+ package dev.angularlynx.shell
2
+
3
+ import kotlin.math.PI
4
+ import kotlin.math.abs
5
+ import kotlin.math.ceil
6
+ import kotlin.math.exp
7
+ import kotlin.math.floor
8
+ import kotlin.math.min
9
+ import kotlin.math.roundToInt
10
+ import kotlin.math.sqrt
11
+
12
+ /**
13
+ * The 2D context's `filter`, which the runtime parses into steps
14
+ * (packages/runtime/src/lib/canvas/css-filter.ts): a Gaussian blur, a color
15
+ * matrix, or a drop shadow, applied in order to the layer what's drawn went
16
+ * into, after its alpha and before its shadow and compositing, as the canvas
17
+ * spec has it.
18
+ *
19
+ * They work on the layer's bytes as `copyPixelsToBuffer` gives them,
20
+ * premultiplied RGBA, in sRGB, which is where the spec has filter functions
21
+ * work. Android's own effects don't fit: `RenderEffect` needs Android 12 and
22
+ * a hardware canvas, and `BlurMaskFilter` blurs only a shape's coverage, not
23
+ * its colors. The iOS shell runs the same steps the same way
24
+ * (AngularLynxCanvas2DContext.m), so the two draw alike.
25
+ */
26
+ internal object CanvasFilter {
27
+ /** The steps' kinds, as css-filter.ts's FILTER_STEP numbers them. */
28
+ private const val BLUR = 0
29
+ private const val MATRIX = 1
30
+ private const val DROP_SHADOW = 2
31
+
32
+ /**
33
+ * The most drop shadows in one filter that can be cast from a layer of
34
+ * their own (see Canvas2DContext.lastFar): the last ones moved by an
35
+ * offset. Each works out the steps before it twice, over the layer it
36
+ * goes under and over that moved back by its offset, so the draws double
37
+ * with each one; past this, the earlier ones are planned as any step is,
38
+ * within the layers' cap, as before issue #824. Three keep a fill to
39
+ * eight draws.
40
+ */
41
+ private const val MAX_OWN_LAYERS = 3
42
+
43
+ /** A step of the filter: its kind (as css-filter.ts's FILTER_STEP numbers them) and its numbers. */
44
+ class Step(val kind: Int, val values: DoubleArray) {
45
+ val isBlur: Boolean get() = kind == BLUR
46
+
47
+ /** Whether it's a drop shadow moved by an offset. */
48
+ val isMovedShadow: Boolean get() = kind == DROP_SHADOW && (values[0] != 0.0 || values[1] != 0.0)
49
+
50
+ /**
51
+ * Whether, when the filter reaches past the layers' cap, it's cast
52
+ * from a layer of its own: one of the filter's last MAX_OWN_LAYERS
53
+ * drop shadows moved by an offset, which [parse] marks.
54
+ */
55
+ var castsAlone: Boolean = false
56
+
57
+ /** Its deviation, for a blur or a drop shadow; 0 for a color matrix. */
58
+ val sigma: Double get() = when (kind) {
59
+ BLUR -> values[0]
60
+ DROP_SHADOW -> values[2]
61
+ else -> 0.0
62
+ }
63
+
64
+ /**
65
+ * How far it can move what's drawn, in the canvas's pixels: three
66
+ * deviations, and a drop shadow's offset. Uncapped: planFor caps
67
+ * the layer it sizes (MAX_REACH).
68
+ */
69
+ val reach: Double get() = when (kind) {
70
+ BLUR -> ceil(values[0] * 3)
71
+ DROP_SHADOW -> ceil(values[2] * 3) + ceil(maxOf(abs(values[0]), abs(values[1])))
72
+ else -> 0.0
73
+ }
74
+
75
+ companion object {
76
+ /** A blur of deviation `sigma`, as blur() makes. */
77
+ fun blur(sigma: Double): Step = Step(BLUR, doubleArrayOf(sigma))
78
+ }
79
+ }
80
+
81
+ /** The filter's steps, as the runtime sends them. */
82
+ fun parse(steps: DoubleArray): List<Step> {
83
+ val parsed = ArrayList<Step>()
84
+ var i = 0
85
+ while (i < steps.size) {
86
+ val kind = steps[i++].toInt()
87
+ val arity = when (kind) {
88
+ BLUR -> 1
89
+ MATRIX -> 20
90
+ DROP_SHADOW -> 7
91
+ else -> 0
92
+ }
93
+ // A step it doesn't know, or one cut short, ends the list: what's
94
+ // after it would be read as the wrong numbers.
95
+ if (arity == 0 || i + arity > steps.size) break
96
+ parsed.add(Step(kind, steps.copyOfRange(i, i + arity)))
97
+ i += arity
98
+ }
99
+ for (step in parsed.filter { it.isMovedShadow }.takeLast(MAX_OWN_LAYERS)) step.castsAlone = true
100
+ return parsed
101
+ }
102
+
103
+ /**
104
+ * One step on a layer's bytes, in a layer `scale` of whose pixels make a
105
+ * canvas pixel: its lengths (a deviation, an offset) are in the canvas's
106
+ * pixels, so they're scaled to the layer's.
107
+ */
108
+ fun applyStep(bytes: ByteArray, width: Int, height: Int, step: Step, scale: Double) {
109
+ val v = step.values
110
+ when (step.kind) {
111
+ BLUR -> gaussianBlur(bytes, width, height, v[0] * scale)
112
+ MATRIX -> colorMatrix(bytes, v)
113
+ else -> {
114
+ val shadow = shadowOf(bytes, width, height, v[0] * scale, v[1] * scale, v[2] * scale, v[3], v[4], v[5], v[6])
115
+ under(bytes, shadow)
116
+ }
117
+ }
118
+ }
119
+
120
+ /** globalAlpha on a layer's bytes, which are premultiplied, so every channel scales. */
121
+ fun scaleAlpha(bytes: ByteArray, alpha: Double) {
122
+ if (alpha >= 1) return
123
+ for (i in bytes.indices) bytes[i] = clampByte(bytes.at(i) * alpha)
124
+ }
125
+
126
+ private fun clampByte(value: Double): Byte = when {
127
+ value <= 0 -> 0.toByte()
128
+ value >= 255 -> 255.toByte()
129
+ else -> (value + 0.5).toInt().toByte()
130
+ }
131
+
132
+ private fun clampUnit(value: Double): Double = value.coerceIn(0.0, 1.0)
133
+
134
+ private fun ByteArray.at(index: Int): Int = this[index].toInt() and 0xff
135
+
136
+ /**
137
+ * A color matrix (row by row: red, green, blue, alpha; each row's factors
138
+ * and offset) on the colors not premultiplied, clamped, as feColorMatrix is.
139
+ */
140
+ private fun colorMatrix(bytes: ByteArray, m: DoubleArray) {
141
+ for (p in 0 until bytes.size step 4) {
142
+ val a = bytes.at(p + 3) / 255.0
143
+ var r = 0.0
144
+ var g = 0.0
145
+ var b = 0.0
146
+ if (a > 0) {
147
+ r = bytes.at(p) / 255.0 / a
148
+ g = bytes.at(p + 1) / 255.0 / a
149
+ b = bytes.at(p + 2) / 255.0 / a
150
+ }
151
+ val r2 = clampUnit(m[0] * r + m[1] * g + m[2] * b + m[3] * a + m[4])
152
+ val g2 = clampUnit(m[5] * r + m[6] * g + m[7] * b + m[8] * a + m[9])
153
+ val b2 = clampUnit(m[10] * r + m[11] * g + m[12] * b + m[13] * a + m[14])
154
+ val a2 = clampUnit(m[15] * r + m[16] * g + m[17] * b + m[18] * a + m[19])
155
+ bytes[p] = clampByte(r2 * a2 * 255)
156
+ bytes[p + 1] = clampByte(g2 * a2 * 255)
157
+ bytes[p + 2] = clampByte(b2 * a2 * 255)
158
+ bytes[p + 3] = clampByte(a2 * 255)
159
+ }
160
+ }
161
+
162
+ /**
163
+ * One box blur along each row (or each column), each output the mean of
164
+ * the `lo` bytes before it, itself and the `hi` after; transparent past
165
+ * the edges.
166
+ */
167
+ private fun boxPass(src: ByteArray, dst: ByteArray, width: Int, height: Int, lo: Int, hi: Int, horizontal: Boolean) {
168
+ val lines = if (horizontal) height else width
169
+ val length = if (horizontal) width else height
170
+ val step = if (horizontal) 4 else width * 4
171
+ val lineStride = if (horizontal) width * 4 else 4
172
+ val size = lo + hi + 1
173
+ for (line in 0 until lines) {
174
+ val base = line * lineStride
175
+ for (channel in 0 until 4) {
176
+ var sum = 0
177
+ for (i in 0..min(hi, length - 1)) sum += src.at(base + i * step + channel)
178
+ for (x in 0 until length) {
179
+ dst[base + x * step + channel] = ((sum * 2 + size) / (size * 2)).toByte()
180
+ if (x + hi + 1 < length) sum += src.at(base + (x + hi + 1) * step + channel)
181
+ if (x >= lo) sum -= src.at(base + (x - lo) * step + channel)
182
+ }
183
+ }
184
+ }
185
+ }
186
+
187
+ /** One pass of a Gaussian kernel along each row (or each column). */
188
+ private fun kernelPass(src: ByteArray, dst: ByteArray, width: Int, height: Int, weights: DoubleArray, radius: Int, horizontal: Boolean) {
189
+ val lines = if (horizontal) height else width
190
+ val length = if (horizontal) width else height
191
+ val step = if (horizontal) 4 else width * 4
192
+ val lineStride = if (horizontal) width * 4 else 4
193
+ val sum = DoubleArray(4)
194
+ for (line in 0 until lines) {
195
+ val base = line * lineStride
196
+ for (x in 0 until length) {
197
+ sum.fill(0.0)
198
+ for (i in maxOf(0, x - radius)..min(length - 1, x + radius)) {
199
+ val weight = weights[i + radius - x]
200
+ val p = base + i * step
201
+ for (channel in 0 until 4) sum[channel] += src.at(p + channel) * weight
202
+ }
203
+ for (channel in 0 until 4) dst[base + x * step + channel] = clampByte(sum[channel])
204
+ }
205
+ }
206
+ }
207
+
208
+ /**
209
+ * A Gaussian blur of deviation `sigma`, as feGaussianBlur draws it: from a
210
+ * deviation of 2, three box blurs, which the spec gives as within 3% of
211
+ * the Gaussian (and which cost the same whatever the deviation); below
212
+ * it, the Gaussian's own kernel, which is small there.
213
+ */
214
+ private fun gaussianBlur(bytes: ByteArray, width: Int, height: Int, sigma: Double) {
215
+ if (!(sigma > 0) || width == 0 || height == 0) return
216
+ val scratch = ByteArray(bytes.size)
217
+ for (horizontal in booleanArrayOf(true, false)) {
218
+ if (sigma >= 2) {
219
+ val d = floor(sigma * 3 * sqrt(2 * PI) / 4 + 0.5).toInt()
220
+ if (d % 2 == 1) {
221
+ boxPass(bytes, scratch, width, height, d / 2, d / 2, horizontal)
222
+ boxPass(scratch, bytes, width, height, d / 2, d / 2, horizontal)
223
+ boxPass(bytes, scratch, width, height, d / 2, d / 2, horizontal)
224
+ } else {
225
+ // An even box is centered between two pixels: one leans
226
+ // left, one right, and the third is a pixel wider and
227
+ // centered.
228
+ boxPass(bytes, scratch, width, height, d / 2, d / 2 - 1, horizontal)
229
+ boxPass(scratch, bytes, width, height, d / 2 - 1, d / 2, horizontal)
230
+ boxPass(bytes, scratch, width, height, d / 2, d / 2, horizontal)
231
+ }
232
+ } else {
233
+ val radius = ceil(sigma * 3).toInt()
234
+ val weights = DoubleArray(radius * 2 + 1) { i ->
235
+ val k = (i - radius).toDouble()
236
+ exp(-(k * k) / (2 * sigma * sigma))
237
+ }
238
+ val total = weights.sum()
239
+ for (i in weights.indices) weights[i] /= total
240
+ kernelPass(bytes, scratch, width, height, weights, radius, horizontal)
241
+ }
242
+ scratch.copyInto(bytes)
243
+ }
244
+ }
245
+
246
+ /**
247
+ * The shadow of what's in `bytes`: its alpha, moved by the offset (to the
248
+ * nearest pixel), blurred by the deviation and in the color (red, green
249
+ * and blue 0 to 255, alpha 0 to 1), as new premultiplied bytes.
250
+ */
251
+ fun shadowOf(
252
+ bytes: ByteArray,
253
+ width: Int,
254
+ height: Int,
255
+ dx: Double,
256
+ dy: Double,
257
+ sigma: Double,
258
+ r: Double,
259
+ g: Double,
260
+ b: Double,
261
+ a: Double,
262
+ ): ByteArray {
263
+ val offsetX = dx.roundToInt()
264
+ val offsetY = dy.roundToInt()
265
+ val shadow = ByteArray(bytes.size)
266
+ if (abs(offsetX) < width && abs(offsetY) < height) {
267
+ for (y in maxOf(0, offsetY) until min(height, height + offsetY)) {
268
+ for (x in maxOf(0, offsetX) until min(width, width + offsetX)) {
269
+ shadow[(y * width + x) * 4 + 3] = bytes[((y - offsetY) * width + (x - offsetX)) * 4 + 3]
270
+ }
271
+ }
272
+ }
273
+ gaussianBlur(shadow, width, height, sigma)
274
+ for (p in 0 until shadow.size step 4) {
275
+ val shadowAlpha = shadow.at(p + 3) / 255.0 * a
276
+ shadow[p] = clampByte(r * shadowAlpha)
277
+ shadow[p + 1] = clampByte(g * shadowAlpha)
278
+ shadow[p + 2] = clampByte(b * shadowAlpha)
279
+ shadow[p + 3] = clampByte(255 * shadowAlpha)
280
+ }
281
+ return shadow
282
+ }
283
+
284
+ /**
285
+ * `under` drawn under `bytes`, source-over, as drop-shadow() puts its
286
+ * shadow under what's drawn. Both are premultiplied, the same size.
287
+ */
288
+ fun under(bytes: ByteArray, under: ByteArray) {
289
+ for (p in 0 until bytes.size) {
290
+ val uncovered = 1 - bytes.at(p - p % 4 + 3) / 255.0
291
+ bytes[p] = clampByte(bytes.at(p) + under.at(p) * uncovered)
292
+ }
293
+ }
294
+
295
+ /**
296
+ * The bytes of a layer resampled onto another: `src`, over the canvas's
297
+ * pixels from (srcLeft, srcTop) at `srcScale` of its pixels to a canvas
298
+ * pixel, read with bilinear filtering at the center of each of the
299
+ * `dstWidth` by `dstHeight` pixels of a layer from (dstLeft, dstTop) at
300
+ * `dstScale`, moved by (shiftX, shiftY) canvas pixels. Transparent past
301
+ * `src`'s edges. It's how a blur worked out in a smaller layer is scaled
302
+ * up, and how a drop shadow's offset lands between two pixels, which a
303
+ * blur that wide has no edge to show.
304
+ */
305
+ fun resample(
306
+ src: ByteArray,
307
+ srcWidth: Int,
308
+ srcHeight: Int,
309
+ srcLeft: Double,
310
+ srcTop: Double,
311
+ srcScale: Double,
312
+ dstWidth: Int,
313
+ dstHeight: Int,
314
+ dstLeft: Double,
315
+ dstTop: Double,
316
+ dstScale: Double,
317
+ shiftX: Double,
318
+ shiftY: Double,
319
+ ): ByteArray {
320
+ val dst = ByteArray(dstWidth * dstHeight * 4)
321
+ // Where each column's and row's center falls in src, as the pixel
322
+ // before it and how far past that pixel's center it is. Clamped, so a
323
+ // far shift stays an Int (and reads nothing).
324
+ fun axis(count: Int, from: Double, shift: Double, srcFrom: Double, size: Int): Pair<IntArray, DoubleArray> {
325
+ val index = IntArray(count)
326
+ val fraction = DoubleArray(count)
327
+ for (i in 0 until count) {
328
+ val u = ((from + (i + 0.5) / dstScale - shift - srcFrom) * srcScale - 0.5).coerceIn(-2.0, size + 1.0)
329
+ val whole = floor(u)
330
+ index[i] = whole.toInt()
331
+ fraction[i] = u - whole
332
+ }
333
+ return index to fraction
334
+ }
335
+ val (xs, fx) = axis(dstWidth, dstLeft, shiftX, srcLeft, srcWidth)
336
+ val (ys, fy) = axis(dstHeight, dstTop, shiftY, srcTop, srcHeight)
337
+ fun sample(x: Int, y: Int, channel: Int): Int =
338
+ if (x < 0 || y < 0 || x >= srcWidth || y >= srcHeight) 0 else src.at((y * srcWidth + x) * 4 + channel)
339
+ for (j in 0 until dstHeight) {
340
+ val y = ys[j]
341
+ if (y < -1 || y >= srcHeight) continue
342
+ val wy = fy[j]
343
+ for (i in 0 until dstWidth) {
344
+ val x = xs[i]
345
+ if (x < -1 || x >= srcWidth) continue
346
+ val wx = fx[i]
347
+ val p = (j * dstWidth + i) * 4
348
+ for (channel in 0 until 4) {
349
+ val top = sample(x, y, channel) * (1 - wx) + sample(x + 1, y, channel) * wx
350
+ val bottom = sample(x, y + 1, channel) * (1 - wx) + sample(x + 1, y + 1, channel) * wx
351
+ dst[p + channel] = clampByte(top * (1 - wy) + bottom * wy)
352
+ }
353
+ }
354
+ }
355
+ return dst
356
+ }
357
+ }
@@ -10,8 +10,9 @@ import com.lynx.tasm.behavior.LynxProp
10
10
  import com.lynx.tasm.behavior.ui.LynxUI
11
11
 
12
12
  /**
13
- * The `canvas` element: a TextureView its WebGL context (WebGLContext) copies
14
- * each finished frame into. Its `canvas-id` attribute, which the runtime
13
+ * The `canvas` element: a TextureView its context copies each finished frame
14
+ * into: a WebGL context's drawing buffer (WebGLContext), or a 2D context's
15
+ * bitmap (Canvas2DContext). Its `canvas-id` attribute, which the runtime
15
16
  * sets, pairs it with the context the runtime made for it.
16
17
  *
17
18
  * A TextureView composites with the views around it, so a canvas can sit
@@ -52,7 +53,7 @@ class CanvasUI(context: LynxContext) : LynxUI<TextureView>(context) {
52
53
  }
53
54
  }
54
55
 
55
- private fun contextOrNull(): WebGLContext? = canvasId?.let { CanvasRegistry.context(it) }
56
+ private fun contextOrNull(): CanvasContext? = canvasId?.let { CanvasRegistry.context(it) }
56
57
 
57
58
  /**
58
59
  * Whether the frames can be shown see-through, as the context's `alpha`