@g9tm/lynxcontrols-native 2.6.19 → 2.7.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/README.md +40 -1
- package/android/build.gradle.kts +7 -0
- package/android/src/main/java/com/g9lynx/g9native/G9NativeImages.kt +16 -0
- package/android/src/main/java/com/g9lynx/g9native/canvas/G9CanvasModel.kt +2 -2
- package/android/src/main/java/com/g9lynx/g9native/canvas/G9CanvasPaths.kt +7 -26
- package/android/src/main/java/com/g9lynx/g9native/canvas/G9CanvasView.kt +9 -4
- package/android/src/main/java/com/g9lynx/g9native/generated/G9ImageModuleSpec.java +27 -0
- package/android/src/main/java/com/g9lynx/g9native/image/G9HostTypeface.kt +49 -0
- package/android/src/main/java/com/g9lynx/g9native/image/G9ImageDraw.kt +222 -0
- package/android/src/main/java/com/g9lynx/g9native/image/G9ImageModule.kt +54 -0
- package/android/src/main/java/com/g9lynx/g9native/image/G9ImageProcessor.kt +464 -0
- package/android/src/main/java/com/g9lynx/g9native/image/core/G9HostFont.kt +28 -0
- package/android/src/main/java/com/g9lynx/g9native/image/core/G9ImageBudget.kt +54 -0
- package/android/src/main/java/com/g9lynx/g9native/image/core/G9ImageColor.kt +48 -0
- package/android/src/main/java/com/g9lynx/g9native/image/core/G9ImageError.kt +19 -0
- package/android/src/main/java/com/g9lynx/g9native/image/core/G9ImageExifTags.kt +61 -0
- package/android/src/main/java/com/g9lynx/g9native/image/core/G9ImageFiles.kt +99 -0
- package/android/src/main/java/com/g9lynx/g9native/image/core/G9ImageGeometry.kt +150 -0
- package/android/src/main/java/com/g9lynx/g9native/image/core/G9ImageJson.kt +200 -0
- package/android/src/main/java/com/g9lynx/g9native/image/core/G9ImageQuality.kt +65 -0
- package/android/src/main/java/com/g9lynx/g9native/image/core/G9ImageRedaction.kt +77 -0
- package/android/src/main/java/com/g9lynx/g9native/image/core/G9ImageService.kt +102 -0
- package/android/src/main/java/com/g9lynx/g9native/image/core/G9ImageSniff.kt +63 -0
- package/android/src/main/java/com/g9lynx/g9native/image/core/G9ImageSpec.kt +237 -0
- package/catalog/catalog.json +161 -1
- package/catalog/index.md +1 -1
- package/dist/.tsbuildinfo +1 -1
- package/dist/image.d.ts +219 -0
- package/dist/image.d.ts.map +1 -0
- package/dist/image.js +116 -0
- package/dist/image.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/protocol.d.ts +12 -0
- package/dist/protocol.d.ts.map +1 -1
- package/generated/G9ImageModule.ts +64 -0
- package/ios/Tests/G9NativeTestSuite.h +8 -0
- package/ios/Tests/G9NativeTestSuite.m +350 -0
- package/ios/Tests/G9NativeTests.m +16 -0
- package/ios/g9lynx-native-swift.podspec +1 -1
- package/ios/g9lynx-native.podspec +5 -4
- package/ios/src/G9ImageModule.h +30 -0
- package/ios/src/G9ImageModule.m +986 -0
- package/ios/src/core/G9ImageCore.h +201 -0
- package/ios/src/core/G9ImageCore.m +1039 -0
- package/ios/src/generated/G9ImageModuleSpec.h +16 -0
- package/ios/src/generated/G9ImageModuleSpec.m +2 -0
- package/ios/swift/canvas/G9LCCanvasCore.swift +3 -2
- package/ios/swift/canvas/G9LCCanvasView.swift +15 -4
- package/ios/swift/shared/G9LCFonts.swift +18 -2
- package/package.json +4 -4
- package/types/platform-native-module.d.ts +18 -0
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
package com.g9lynx.g9native.image.core
|
|
2
|
+
|
|
3
|
+
import java.util.concurrent.ConcurrentHashMap
|
|
4
|
+
import java.util.concurrent.Executor
|
|
5
|
+
|
|
6
|
+
/** A process's cancellation flag: [check] between steps throws `cancelled` once `cancel({token})` arrived. */
|
|
7
|
+
class G9ImageTicket(val token: String?) {
|
|
8
|
+
@Volatile
|
|
9
|
+
var cancelled = false
|
|
10
|
+
|
|
11
|
+
fun check() {
|
|
12
|
+
if (cancelled) throw G9ImageError(G9ImageError.CANCELLED, "The process was cancelled.")
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** The platform work behind the service (Android: `G9ImageProcessor`). Values are the contract's result objects. */
|
|
17
|
+
interface G9ImageBackend {
|
|
18
|
+
fun probe(spec: G9ProbeSpec): Map<String, Any?>
|
|
19
|
+
|
|
20
|
+
fun preview(spec: G9PreviewSpec): Map<String, Any?>
|
|
21
|
+
|
|
22
|
+
fun process(spec: G9ProcessSpec, ticket: G9ImageTicket): Map<String, Any?>
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* `G9ImageModule`'s methods without Lynx: one JSON string in, one JSON envelope out (`{"ok":true,"value"}` or
|
|
27
|
+
* `{"ok":false,"error":{"code","message"}}`), the work on [executor] (one background thread: one picture in memory at a
|
|
28
|
+
* time). A process's token is registered before it is queued, so `cancel` reaches it queued or running.
|
|
29
|
+
*/
|
|
30
|
+
class G9ImageService(private val backend: G9ImageBackend, private val executor: Executor) {
|
|
31
|
+
private val tickets = ConcurrentHashMap<String, MutableSet<G9ImageTicket>>()
|
|
32
|
+
|
|
33
|
+
fun probe(args: String?, reply: (String) -> Unit) = queue(reply) { backend.probe(G9ImageSpec.probe(G9ImageJson.parseObject(args))) }
|
|
34
|
+
|
|
35
|
+
fun preview(args: String?, reply: (String) -> Unit) = queue(reply) { backend.preview(G9ImageSpec.preview(G9ImageJson.parseObject(args))) }
|
|
36
|
+
|
|
37
|
+
fun process(args: String?, reply: (String) -> Unit) {
|
|
38
|
+
val spec =
|
|
39
|
+
try {
|
|
40
|
+
G9ImageSpec.process(G9ImageJson.parseObject(args))
|
|
41
|
+
} catch (e: G9ImageError) {
|
|
42
|
+
return reply(fail(e.code, e.message))
|
|
43
|
+
}
|
|
44
|
+
val ticket = G9ImageTicket(spec.token)
|
|
45
|
+
spec.token?.let { token -> tickets.computeIfAbsent(token) { ConcurrentHashMap.newKeySet() }.add(ticket) }
|
|
46
|
+
queue(reply, { release(ticket) }) {
|
|
47
|
+
ticket.check()
|
|
48
|
+
backend.process(spec, ticket)
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Marks every queued or running process with the token cancelled; answers `{}` at once. */
|
|
53
|
+
fun cancel(args: String?, reply: (String) -> Unit) {
|
|
54
|
+
val token =
|
|
55
|
+
try {
|
|
56
|
+
G9ImageSpec.cancelToken(G9ImageJson.parseObject(args))
|
|
57
|
+
} catch (e: G9ImageError) {
|
|
58
|
+
return reply(fail(e.code, e.message))
|
|
59
|
+
}
|
|
60
|
+
token?.let { tickets[it] }?.forEach { it.cancelled = true }
|
|
61
|
+
reply(ok(emptyMap<String, Any?>()))
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Processes whose token is registered (queued or running). */
|
|
65
|
+
fun pending(token: String) = tickets[token]?.size ?: 0
|
|
66
|
+
|
|
67
|
+
private fun release(ticket: G9ImageTicket) {
|
|
68
|
+
val token = ticket.token ?: return
|
|
69
|
+
tickets.computeIfPresent(token) { _, set -> set.remove(ticket); if (set.isEmpty()) null else set }
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
private fun queue(reply: (String) -> Unit, done: () -> Unit = {}, work: () -> Map<String, Any?>) {
|
|
73
|
+
try {
|
|
74
|
+
executor.execute {
|
|
75
|
+
val envelope = guard(work)
|
|
76
|
+
done()
|
|
77
|
+
reply(envelope)
|
|
78
|
+
}
|
|
79
|
+
} catch (e: Throwable) {
|
|
80
|
+
done()
|
|
81
|
+
reply(fail(G9ImageError.IO, "The image queue refused the work: ${e.message}"))
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
companion object {
|
|
86
|
+
fun ok(value: Any?) = G9ImageJson.stringify(mapOf("ok" to true, "value" to value))
|
|
87
|
+
|
|
88
|
+
fun fail(code: String, message: String?) = G9ImageJson.stringify(mapOf("ok" to false, "error" to mapOf("code" to code, "message" to (message ?: code))))
|
|
89
|
+
|
|
90
|
+
/** Runs [work] into an envelope: typed errors keep their code, an OutOfMemoryError is `memory`, anything else `io`. */
|
|
91
|
+
fun guard(work: () -> Map<String, Any?>): String =
|
|
92
|
+
try {
|
|
93
|
+
ok(work())
|
|
94
|
+
} catch (e: G9ImageError) {
|
|
95
|
+
fail(e.code, e.message)
|
|
96
|
+
} catch (e: OutOfMemoryError) {
|
|
97
|
+
fail(G9ImageError.MEMORY, "Out of memory: ${e.message}")
|
|
98
|
+
} catch (e: Throwable) {
|
|
99
|
+
fail(G9ImageError.IO, e.message ?: e.javaClass.simpleName)
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
package com.g9lynx.g9native.image.core
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* What a file's first bytes say: its MIME type (when the platform decoder does not) and whether it carries transparency
|
|
5
|
+
* (`probe().hasAlpha`; a bounds-only decode cannot tell). Pass the first [HEAD] bytes or fewer.
|
|
6
|
+
*/
|
|
7
|
+
object G9ImageSniff {
|
|
8
|
+
const val HEAD = 65536
|
|
9
|
+
|
|
10
|
+
private fun u8(b: ByteArray, i: Int) = b[i].toInt() and 0xff
|
|
11
|
+
|
|
12
|
+
private fun ascii(b: ByteArray, at: Int, text: String) = b.size >= at + text.length && text.indices.all { b[at + it].toInt() == text[it].code }
|
|
13
|
+
|
|
14
|
+
private fun be32(b: ByteArray, i: Int) = (u8(b, i) shl 24) or (u8(b, i + 1) shl 16) or (u8(b, i + 2) shl 8) or u8(b, i + 3)
|
|
15
|
+
|
|
16
|
+
fun mime(b: ByteArray): String? =
|
|
17
|
+
when {
|
|
18
|
+
b.size >= 3 && u8(b, 0) == 0xff && u8(b, 1) == 0xd8 && u8(b, 2) == 0xff -> "image/jpeg"
|
|
19
|
+
b.size >= 8 && u8(b, 0) == 0x89 && ascii(b, 1, "PNG") -> "image/png"
|
|
20
|
+
ascii(b, 0, "RIFF") && ascii(b, 8, "WEBP") -> "image/webp"
|
|
21
|
+
ascii(b, 0, "GIF8") -> "image/gif"
|
|
22
|
+
ascii(b, 0, "BM") -> "image/bmp"
|
|
23
|
+
ascii(b, 4, "ftyp") && (listOf("heic", "heix", "hevc", "heim", "heis", "mif1", "msf1").any { ascii(b, 8, it) }) -> "image/heic"
|
|
24
|
+
else -> null
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Whether the image can hold transparent pixels: PNG colour type 4/6 or a tRNS chunk, WebP with alpha, GIF with a transparent index. */
|
|
28
|
+
fun hasAlpha(b: ByteArray): Boolean =
|
|
29
|
+
when (mime(b)) {
|
|
30
|
+
"image/png" -> pngAlpha(b)
|
|
31
|
+
"image/webp" -> webpAlpha(b)
|
|
32
|
+
"image/gif" -> gifAlpha(b)
|
|
33
|
+
else -> false
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
private fun pngAlpha(b: ByteArray): Boolean {
|
|
37
|
+
if (b.size > 25) {
|
|
38
|
+
val colorType = u8(b, 25)
|
|
39
|
+
if (colorType == 4 || colorType == 6) return true
|
|
40
|
+
}
|
|
41
|
+
var i = 8
|
|
42
|
+
while (i + 8 <= b.size) {
|
|
43
|
+
val length = be32(b, i)
|
|
44
|
+
if (ascii(b, i + 4, "tRNS")) return true
|
|
45
|
+
if (ascii(b, i + 4, "IDAT") || length < 0) return false
|
|
46
|
+
i += 12 + length
|
|
47
|
+
}
|
|
48
|
+
return false
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
private fun webpAlpha(b: ByteArray): Boolean {
|
|
52
|
+
if (ascii(b, 12, "VP8X")) return b.size > 20 && (u8(b, 20) and 0x10) != 0
|
|
53
|
+
if (ascii(b, 12, "VP8L")) return b.size > 24 && (u8(b, 24) and 0x10) != 0 // bit 28 of the 32-bit header after 0x2f
|
|
54
|
+
return false
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
private fun gifAlpha(b: ByteArray): Boolean {
|
|
58
|
+
for (i in 0 until b.size - 3) {
|
|
59
|
+
if (u8(b, i) == 0x21 && u8(b, i + 1) == 0xf9 && u8(b, i + 2) == 0x04) return (u8(b, i + 3) and 1) != 0
|
|
60
|
+
}
|
|
61
|
+
return false
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
package com.g9lynx.g9native.image.core
|
|
2
|
+
|
|
3
|
+
import kotlin.math.max
|
|
4
|
+
|
|
5
|
+
/** `probe({ path })`. */
|
|
6
|
+
data class G9ProbeSpec(val source: G9ImageSource)
|
|
7
|
+
|
|
8
|
+
/** `preview({ path, maxSide, dir? })`. */
|
|
9
|
+
data class G9PreviewSpec(val source: G9ImageSource, val maxSide: Int, val dir: String?)
|
|
10
|
+
|
|
11
|
+
/** One privacy region, in output px. */
|
|
12
|
+
data class G9Redaction(val x: Double, val y: Double, val width: Double, val height: Double, val angle: Double, val pixelate: Boolean, val strength: Double)
|
|
13
|
+
|
|
14
|
+
/** An overlay op, in output px. */
|
|
15
|
+
sealed class G9OverlayOp
|
|
16
|
+
|
|
17
|
+
/** `{"k":"path"}`: [stroke] / [fill] null = none. */
|
|
18
|
+
data class G9PathOp(
|
|
19
|
+
val d: String,
|
|
20
|
+
val stroke: Int?,
|
|
21
|
+
val width: Double,
|
|
22
|
+
val fill: Int?,
|
|
23
|
+
val cap: String,
|
|
24
|
+
val join: String,
|
|
25
|
+
val dash: List<Double>?,
|
|
26
|
+
val opacity: Double,
|
|
27
|
+
val transform: DoubleArray?,
|
|
28
|
+
) : G9OverlayOp()
|
|
29
|
+
|
|
30
|
+
/** `{"k":"text"}`: [x], [y] = the block's centre; [rtl] null = from the text. */
|
|
31
|
+
data class G9TextOp(
|
|
32
|
+
val text: String,
|
|
33
|
+
val x: Double,
|
|
34
|
+
val y: Double,
|
|
35
|
+
val angle: Double,
|
|
36
|
+
val size: Double,
|
|
37
|
+
val color: Int,
|
|
38
|
+
val background: Int?,
|
|
39
|
+
val padding: Double,
|
|
40
|
+
val radius: Double,
|
|
41
|
+
val font: String,
|
|
42
|
+
val weight: Int,
|
|
43
|
+
val align: String,
|
|
44
|
+
val rtl: Boolean?,
|
|
45
|
+
val maxWidth: Double?,
|
|
46
|
+
val opacity: Double,
|
|
47
|
+
) : G9OverlayOp()
|
|
48
|
+
|
|
49
|
+
/** `process(spec)`; see the native contract. */
|
|
50
|
+
data class G9ProcessSpec(
|
|
51
|
+
val token: String?,
|
|
52
|
+
val source: G9ImageSource,
|
|
53
|
+
val width: Int,
|
|
54
|
+
val height: Int,
|
|
55
|
+
val matrix: DoubleArray,
|
|
56
|
+
val background: Int?,
|
|
57
|
+
val circle: Boolean,
|
|
58
|
+
val redact: List<G9Redaction>,
|
|
59
|
+
val overlay: List<G9OverlayOp>,
|
|
60
|
+
val format: String,
|
|
61
|
+
val quality: Int,
|
|
62
|
+
val maxBytes: Long?,
|
|
63
|
+
val keepMetadata: Boolean,
|
|
64
|
+
val passthrough: Boolean,
|
|
65
|
+
val dir: String?,
|
|
66
|
+
val name: String?,
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
/** Reads and checks the methods' arguments; anything wrong is `invalid-argument`. */
|
|
70
|
+
object G9ImageSpec {
|
|
71
|
+
const val MAX_SIDE = 20000
|
|
72
|
+
|
|
73
|
+
/** The shared defaults (Android, iOS, web): text size 32 px, line height 1.25 × size. */
|
|
74
|
+
const val TEXT_SIZE = 32.0
|
|
75
|
+
const val LINE_HEIGHT = 1.25
|
|
76
|
+
val FORMATS = setOf("jpeg", "png", "webp", "source")
|
|
77
|
+
|
|
78
|
+
fun probe(m: Map<*, *>) = G9ProbeSpec(G9ImageSource.parse(str(m, "path")))
|
|
79
|
+
|
|
80
|
+
fun preview(m: Map<*, *>): G9PreviewSpec {
|
|
81
|
+
val maxSide = int(m, "maxSide") ?: throw G9ImageError.invalid("maxSide is required.")
|
|
82
|
+
if (maxSide !in 1..MAX_SIDE) throw G9ImageError.invalid("maxSide must be 1..$MAX_SIDE.")
|
|
83
|
+
return G9PreviewSpec(G9ImageSource.parse(str(m, "path")), maxSide, G9ImageSource.folder(str(m, "dir")))
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
fun cancelToken(m: Map<*, *>) = str(m, "token")
|
|
87
|
+
|
|
88
|
+
fun process(m: Map<*, *>): G9ProcessSpec {
|
|
89
|
+
val source = G9ImageSource.parse(str(m, "path"))
|
|
90
|
+
val passthrough = bool(m, "passthrough") ?: false
|
|
91
|
+
val format = str(m, "format") ?: "jpeg"
|
|
92
|
+
if (format !in FORMATS) throw G9ImageError.invalid("format must be jpeg, png, webp or source.")
|
|
93
|
+
val quality = (num(m, "quality") ?: 90.0).toInt().coerceIn(0, 100)
|
|
94
|
+
val maxBytes = num(m, "maxBytes")?.toLong()
|
|
95
|
+
if (maxBytes != null && maxBytes < 1) throw G9ImageError.invalid("maxBytes must be positive.")
|
|
96
|
+
val name = str(m, "name")?.let(G9ImageNames::checked)
|
|
97
|
+
val width: Int
|
|
98
|
+
val height: Int
|
|
99
|
+
val matrix: DoubleArray
|
|
100
|
+
if (passthrough) {
|
|
101
|
+
width = 0
|
|
102
|
+
height = 0
|
|
103
|
+
matrix = G9Affine.IDENTITY
|
|
104
|
+
} else {
|
|
105
|
+
width = int(m, "width") ?: throw G9ImageError.invalid("width is required.")
|
|
106
|
+
height = int(m, "height") ?: throw G9ImageError.invalid("height is required.")
|
|
107
|
+
if (width !in 1..MAX_SIDE || height !in 1..MAX_SIDE) throw G9ImageError.invalid("width and height must be 1..$MAX_SIDE.")
|
|
108
|
+
matrix = numbers(m["matrix"], 6, "matrix") ?: G9Affine.IDENTITY
|
|
109
|
+
if (G9Affine.invert(matrix) == null) throw G9ImageError.invalid("matrix is not invertible.")
|
|
110
|
+
}
|
|
111
|
+
return G9ProcessSpec(
|
|
112
|
+
token = str(m, "token"),
|
|
113
|
+
source = source,
|
|
114
|
+
width = width,
|
|
115
|
+
height = height,
|
|
116
|
+
matrix = matrix,
|
|
117
|
+
background = color(m["background"], "background"),
|
|
118
|
+
circle = bool(m, "circle") ?: false,
|
|
119
|
+
redact = list(m["redact"], "redact").map(::redaction),
|
|
120
|
+
overlay = list(m["overlay"], "overlay").map(::overlay),
|
|
121
|
+
format = format,
|
|
122
|
+
quality = quality,
|
|
123
|
+
maxBytes = maxBytes,
|
|
124
|
+
keepMetadata = bool(m, "keepMetadata") ?: false,
|
|
125
|
+
passthrough = passthrough,
|
|
126
|
+
dir = G9ImageSource.folder(str(m, "dir")),
|
|
127
|
+
name = name,
|
|
128
|
+
)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
private fun redaction(v: Any?): G9Redaction {
|
|
132
|
+
val m = v as? Map<*, *> ?: throw G9ImageError.invalid("Each redact entry must be an object.")
|
|
133
|
+
val mode = str(m, "mode") ?: "pixelate"
|
|
134
|
+
if (mode != "pixelate" && mode != "blur") throw G9ImageError.invalid("redact mode must be blur or pixelate.")
|
|
135
|
+
val w = need(m, "width", "redact")
|
|
136
|
+
val h = need(m, "height", "redact")
|
|
137
|
+
if (w < 0 || h < 0) throw G9ImageError.invalid("redact width and height must not be negative.")
|
|
138
|
+
return G9Redaction(need(m, "x", "redact"), need(m, "y", "redact"), w, h, num(m, "angle") ?: 0.0, mode == "pixelate", (num(m, "strength") ?: 0.5).coerceIn(0.0, 1.0))
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
private fun overlay(v: Any?): G9OverlayOp {
|
|
142
|
+
val m = v as? Map<*, *> ?: throw G9ImageError.invalid("Each overlay entry must be an object.")
|
|
143
|
+
return when (str(m, "k")) {
|
|
144
|
+
"path" ->
|
|
145
|
+
G9PathOp(
|
|
146
|
+
d = str(m, "d") ?: throw G9ImageError.invalid("A path op needs d."),
|
|
147
|
+
stroke = color(m["stroke"], "stroke"),
|
|
148
|
+
width = max(0.0, num(m, "width") ?: 1.0),
|
|
149
|
+
fill = color(m["fill"], "fill"),
|
|
150
|
+
cap = choice(m, "cap", setOf("butt", "round", "square"), "butt"),
|
|
151
|
+
join = choice(m, "join", setOf("miter", "round", "bevel"), "miter"),
|
|
152
|
+
dash = numbers(m["dash"], -1, "dash")?.toList()?.takeIf { d -> d.isNotEmpty() && d.all { it >= 0 } && d.any { it > 0 } },
|
|
153
|
+
opacity = opacity(m),
|
|
154
|
+
transform = numbers(m["transform"], 6, "transform"),
|
|
155
|
+
)
|
|
156
|
+
"text" ->
|
|
157
|
+
G9TextOp(
|
|
158
|
+
text = str(m, "text") ?: throw G9ImageError.invalid("A text op needs text."),
|
|
159
|
+
x = need(m, "x", "text"),
|
|
160
|
+
y = need(m, "y", "text"),
|
|
161
|
+
angle = num(m, "angle") ?: 0.0,
|
|
162
|
+
size = (num(m, "size") ?: TEXT_SIZE).also { if (it <= 0) throw G9ImageError.invalid("text size must be positive.") },
|
|
163
|
+
color = color(m["color"], "color") ?: 0xff000000.toInt(),
|
|
164
|
+
background = color(m["background"], "background"),
|
|
165
|
+
padding = max(0.0, num(m, "padding") ?: 0.0),
|
|
166
|
+
radius = max(0.0, num(m, "radius") ?: 0.0),
|
|
167
|
+
font = str(m, "font") ?: "",
|
|
168
|
+
weight = (num(m, "weight") ?: 400.0).toInt().coerceIn(1, 1000),
|
|
169
|
+
align = choice(m, "align", setOf("center", "left", "right", "start", "end"), "center"),
|
|
170
|
+
rtl = bool(m, "rtl"),
|
|
171
|
+
maxWidth = num(m, "maxWidth")?.takeIf { it > 0 },
|
|
172
|
+
opacity = opacity(m),
|
|
173
|
+
)
|
|
174
|
+
else -> throw G9ImageError.invalid("overlay k must be path or text.")
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
private fun opacity(m: Map<*, *>) = (num(m, "opacity") ?: 1.0).coerceIn(0.0, 1.0)
|
|
179
|
+
|
|
180
|
+
private fun choice(m: Map<*, *>, key: String, allowed: Set<String>, fallback: String): String {
|
|
181
|
+
val v = str(m, key) ?: return fallback
|
|
182
|
+
if (v !in allowed) throw G9ImageError.invalid("$key must be one of ${allowed.joinToString()}.")
|
|
183
|
+
return v
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
private fun need(m: Map<*, *>, key: String, what: String) = num(m, key) ?: throw G9ImageError.invalid("$what needs a number $key.")
|
|
187
|
+
|
|
188
|
+
private fun str(m: Map<*, *>, key: String): String? =
|
|
189
|
+
when (val v = m[key]) {
|
|
190
|
+
null -> null
|
|
191
|
+
is String -> v
|
|
192
|
+
else -> throw G9ImageError.invalid("$key must be a string.")
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
private fun bool(m: Map<*, *>, key: String): Boolean? =
|
|
196
|
+
when (val v = m[key]) {
|
|
197
|
+
null -> null
|
|
198
|
+
is Boolean -> v
|
|
199
|
+
else -> throw G9ImageError.invalid("$key must be true or false.")
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
private fun num(m: Map<*, *>, key: String): Double? =
|
|
203
|
+
when (val v = m[key]) {
|
|
204
|
+
null -> null
|
|
205
|
+
is Number -> v.toDouble().also { if (!it.isFinite()) throw G9ImageError.invalid("$key must be a finite number.") }
|
|
206
|
+
else -> throw G9ImageError.invalid("$key must be a number.")
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** A whole number (1600 or 1600.0); never rounded. */
|
|
210
|
+
private fun int(m: Map<*, *>, key: String): Int? {
|
|
211
|
+
val d = num(m, key) ?: return null
|
|
212
|
+
if (d != Math.rint(d) || d < Int.MIN_VALUE || d > Int.MAX_VALUE) throw G9ImageError.invalid("$key must be a whole number.")
|
|
213
|
+
return d.toInt()
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** [count] finite numbers (-1 = any count), or null when absent. */
|
|
217
|
+
private fun numbers(v: Any?, count: Int, key: String): DoubleArray? {
|
|
218
|
+
if (v == null) return null
|
|
219
|
+
val l = v as? List<*> ?: throw G9ImageError.invalid("$key must be an array of numbers.")
|
|
220
|
+
if (count >= 0 && l.size != count) throw G9ImageError.invalid("$key must hold $count numbers.")
|
|
221
|
+
return DoubleArray(l.size) { i -> (l[i] as? Number)?.toDouble()?.takeIf { it.isFinite() } ?: throw G9ImageError.invalid("$key must hold finite numbers.") }
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
private fun list(v: Any?, key: String): List<*> =
|
|
225
|
+
when (v) {
|
|
226
|
+
null -> emptyList<Any?>()
|
|
227
|
+
is List<*> -> v
|
|
228
|
+
else -> throw G9ImageError.invalid("$key must be an array.")
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
private fun color(v: Any?, key: String): Int? =
|
|
232
|
+
when (v) {
|
|
233
|
+
null -> null
|
|
234
|
+
is String -> G9ImageColor.parse(v) ?: throw G9ImageError.invalid("$key is not a colour: \"$v\".")
|
|
235
|
+
else -> throw G9ImageError.invalid("$key must be a colour string.")
|
|
236
|
+
}
|
|
237
|
+
}
|
package/catalog/catalog.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"catalogVersion": 1,
|
|
3
3
|
"package": "@g9tm/lynxcontrols-native",
|
|
4
|
-
"version": "2.
|
|
4
|
+
"version": "2.7.0",
|
|
5
5
|
"generatedBy": "node scripts/check-docs.mjs --write",
|
|
6
6
|
"components": [
|
|
7
7
|
{
|
|
@@ -397,6 +397,158 @@
|
|
|
397
397
|
"signature": "G9HostNativeModule",
|
|
398
398
|
"summary": "`NativeModules.G9HostModule`."
|
|
399
399
|
},
|
|
400
|
+
{
|
|
401
|
+
"id": "native.native.G9ImageCallOptions",
|
|
402
|
+
"export": "G9ImageCallOptions",
|
|
403
|
+
"from": "@g9tm/lynxcontrols-native",
|
|
404
|
+
"kind": "interface",
|
|
405
|
+
"signature": "G9ImageCallOptions",
|
|
406
|
+
"summary": "The module an image call goes to: by default `NativeModules.G9ImageModule`; tests pass a fake."
|
|
407
|
+
},
|
|
408
|
+
{
|
|
409
|
+
"id": "native.native.g9ImageCancel",
|
|
410
|
+
"export": "g9ImageCancel",
|
|
411
|
+
"from": "@g9tm/lynxcontrols-native",
|
|
412
|
+
"kind": "function",
|
|
413
|
+
"signature": "(token: string, options?: G9ImageCallOptions | undefined): Promise<void>",
|
|
414
|
+
"summary": "Stops the running or queued process whose spec carried `token` (it rejects with `cancelled` and leaves no file)."
|
|
415
|
+
},
|
|
416
|
+
{
|
|
417
|
+
"id": "native.native.G9ImageError",
|
|
418
|
+
"export": "G9ImageError",
|
|
419
|
+
"from": "@g9tm/lynxcontrols-native",
|
|
420
|
+
"kind": "class",
|
|
421
|
+
"signature": "typeof G9ImageError",
|
|
422
|
+
"summary": "A failed image call: `code` is one of `G9ImageErrorCode` (`unsupported` where no image module is linked, `cancelled` after `g9ImageCancel`); a code the module does not list arrives as `io` with the original code in the message."
|
|
423
|
+
},
|
|
424
|
+
{
|
|
425
|
+
"id": "native.native.G9ImageErrorCode",
|
|
426
|
+
"export": "G9ImageErrorCode",
|
|
427
|
+
"from": "@g9tm/lynxcontrols-native",
|
|
428
|
+
"kind": "type",
|
|
429
|
+
"signature": "G9ImageErrorCode",
|
|
430
|
+
"summary": "The failure codes of `G9ImageModule` (the native contract's list)."
|
|
431
|
+
},
|
|
432
|
+
{
|
|
433
|
+
"id": "native.native.G9ImageFormat",
|
|
434
|
+
"export": "G9ImageFormat",
|
|
435
|
+
"from": "@g9tm/lynxcontrols-native",
|
|
436
|
+
"kind": "type",
|
|
437
|
+
"signature": "G9ImageFormat",
|
|
438
|
+
"summary": "The encoder a process asks for: `source` keeps the source's format (a HEIC source becomes JPEG)."
|
|
439
|
+
},
|
|
440
|
+
{
|
|
441
|
+
"id": "native.native.G9ImageMatrix",
|
|
442
|
+
"export": "G9ImageMatrix",
|
|
443
|
+
"from": "@g9tm/lynxcontrols-native",
|
|
444
|
+
"kind": "type",
|
|
445
|
+
"signature": "G9ImageMatrix",
|
|
446
|
+
"summary": "A 2×3 affine matrix `[a, b, c, d, e, f]`: `x' = a·x + c·y + e`, `y' = b·x + d·y + f` (the canvas convention). In a process spec it maps OUTPUT px to ORIENTED source px, so one matrix covers crop, quarter turns, flips, a free straighten angle and scale."
|
|
447
|
+
},
|
|
448
|
+
{
|
|
449
|
+
"id": "native.native.G9ImageNativeModule",
|
|
450
|
+
"export": "G9ImageNativeModule",
|
|
451
|
+
"from": "@g9tm/lynxcontrols-native",
|
|
452
|
+
"kind": "interface",
|
|
453
|
+
"signature": "G9ImageNativeModule",
|
|
454
|
+
"summary": "`NativeModules.G9ImageModule` (2.7.0). Every method takes ONE JSON string and replies ONE JSON string, the envelope `{\"ok\":true,\"value\":{…}}` or `{\"ok\":false,\"error\":{\"code\",\"message\"}}` (a host that hands the envelope over as an object is read the same way)."
|
|
455
|
+
},
|
|
456
|
+
{
|
|
457
|
+
"id": "native.native.G9ImageOverlayOp",
|
|
458
|
+
"export": "G9ImageOverlayOp",
|
|
459
|
+
"from": "@g9tm/lynxcontrols-native",
|
|
460
|
+
"kind": "type",
|
|
461
|
+
"signature": "G9ImageOverlayOp",
|
|
462
|
+
"summary": "One overlay drawing operation, drawn in order after the redactions."
|
|
463
|
+
},
|
|
464
|
+
{
|
|
465
|
+
"id": "native.native.G9ImagePathOp",
|
|
466
|
+
"export": "G9ImagePathOp",
|
|
467
|
+
"from": "@g9tm/lynxcontrols-native",
|
|
468
|
+
"kind": "interface",
|
|
469
|
+
"signature": "G9ImagePathOp",
|
|
470
|
+
"summary": "An overlay path in OUTPUT px: SVG path syntax, absolute M L H V C S Q T Z."
|
|
471
|
+
},
|
|
472
|
+
{
|
|
473
|
+
"id": "native.native.g9ImagePreview",
|
|
474
|
+
"export": "g9ImagePreview",
|
|
475
|
+
"from": "@g9tm/lynxcontrols-native",
|
|
476
|
+
"kind": "function",
|
|
477
|
+
"signature": "(path: string, preview: G9ImagePreviewOptions, options?: G9ImageCallOptions | undefined): Promise<G9ImagePreviewResult>",
|
|
478
|
+
"summary": "Writes an oriented, screen-sized copy of a picture for the editor (longest side ≤ `maxSide`, never upscaled)."
|
|
479
|
+
},
|
|
480
|
+
{
|
|
481
|
+
"id": "native.native.G9ImagePreviewOptions",
|
|
482
|
+
"export": "G9ImagePreviewOptions",
|
|
483
|
+
"from": "@g9tm/lynxcontrols-native",
|
|
484
|
+
"kind": "interface",
|
|
485
|
+
"signature": "G9ImagePreviewOptions",
|
|
486
|
+
"summary": "Options of `g9ImagePreview`."
|
|
487
|
+
},
|
|
488
|
+
{
|
|
489
|
+
"id": "native.native.G9ImagePreviewResult",
|
|
490
|
+
"export": "G9ImagePreviewResult",
|
|
491
|
+
"from": "@g9tm/lynxcontrols-native",
|
|
492
|
+
"kind": "interface",
|
|
493
|
+
"signature": "G9ImagePreviewResult",
|
|
494
|
+
"summary": "What `g9ImagePreview` writes: an oriented sRGB copy (JPEG q 85, PNG when the source has alpha)."
|
|
495
|
+
},
|
|
496
|
+
{
|
|
497
|
+
"id": "native.native.g9ImageProbe",
|
|
498
|
+
"export": "g9ImageProbe",
|
|
499
|
+
"from": "@g9tm/lynxcontrols-native",
|
|
500
|
+
"kind": "function",
|
|
501
|
+
"signature": "(path: string, options?: G9ImageCallOptions | undefined): Promise<G9ImageProbeResult>",
|
|
502
|
+
"summary": "Reads a picture's oriented size, EXIF orientation, type, byte size and alpha without decoding it."
|
|
503
|
+
},
|
|
504
|
+
{
|
|
505
|
+
"id": "native.native.G9ImageProbeResult",
|
|
506
|
+
"export": "G9ImageProbeResult",
|
|
507
|
+
"from": "@g9tm/lynxcontrols-native",
|
|
508
|
+
"kind": "interface",
|
|
509
|
+
"signature": "G9ImageProbeResult",
|
|
510
|
+
"summary": "What `g9ImageProbe` reports: the picture as a person sees it, read without decoding it."
|
|
511
|
+
},
|
|
512
|
+
{
|
|
513
|
+
"id": "native.native.g9ImageProcess",
|
|
514
|
+
"export": "g9ImageProcess",
|
|
515
|
+
"from": "@g9tm/lynxcontrols-native",
|
|
516
|
+
"kind": "function",
|
|
517
|
+
"signature": "(spec: G9ImageProcessSpec, options?: G9ImageCallOptions | undefined): Promise<G9ImageProcessResult>",
|
|
518
|
+
"summary": "Renders a picture into a new file: the source mapped through `matrix` into exactly `width × height`, background, redactions, overlay and circle mask, encoded as asked (and fitted to `maxBytes`). The source is never modified. Rejects with a `G9ImageError`: `cancelled` after `g9ImageCancel`, `unsupported` where no module exists."
|
|
519
|
+
},
|
|
520
|
+
{
|
|
521
|
+
"id": "native.native.G9ImageProcessResult",
|
|
522
|
+
"export": "G9ImageProcessResult",
|
|
523
|
+
"from": "@g9tm/lynxcontrols-native",
|
|
524
|
+
"kind": "interface",
|
|
525
|
+
"signature": "G9ImageProcessResult",
|
|
526
|
+
"summary": "What `g9ImageProcess` wrote."
|
|
527
|
+
},
|
|
528
|
+
{
|
|
529
|
+
"id": "native.native.G9ImageProcessSpec",
|
|
530
|
+
"export": "G9ImageProcessSpec",
|
|
531
|
+
"from": "@g9tm/lynxcontrols-native",
|
|
532
|
+
"kind": "interface",
|
|
533
|
+
"signature": "G9ImageProcessSpec",
|
|
534
|
+
"summary": "What `g9ImageProcess` makes: the output's exact size, how the source maps into it, and how it is written."
|
|
535
|
+
},
|
|
536
|
+
{
|
|
537
|
+
"id": "native.native.G9ImageRedaction",
|
|
538
|
+
"export": "G9ImageRedaction",
|
|
539
|
+
"from": "@g9tm/lynxcontrols-native",
|
|
540
|
+
"kind": "interface",
|
|
541
|
+
"signature": "G9ImageRedaction",
|
|
542
|
+
"summary": "A privacy region in OUTPUT px, applied after the picture and before the overlay."
|
|
543
|
+
},
|
|
544
|
+
{
|
|
545
|
+
"id": "native.native.G9ImageTextOp",
|
|
546
|
+
"export": "G9ImageTextOp",
|
|
547
|
+
"from": "@g9tm/lynxcontrols-native",
|
|
548
|
+
"kind": "interface",
|
|
549
|
+
"signature": "G9ImageTextOp",
|
|
550
|
+
"summary": "An overlay text block in OUTPUT px, shaped and ordered for Persian and Arabic as well as Latin."
|
|
551
|
+
},
|
|
400
552
|
{
|
|
401
553
|
"id": "native.native.G9InsetsSubscriber",
|
|
402
554
|
"export": "G9InsetsSubscriber",
|
|
@@ -589,6 +741,14 @@
|
|
|
589
741
|
"signature": "(): G9HostNativeModule | null",
|
|
590
742
|
"summary": "`NativeModules.G9HostModule`, or `null` where the native library is not linked."
|
|
591
743
|
},
|
|
744
|
+
{
|
|
745
|
+
"id": "native.native.nativeImageModule",
|
|
746
|
+
"export": "nativeImageModule",
|
|
747
|
+
"from": "@g9tm/lynxcontrols-native",
|
|
748
|
+
"kind": "function",
|
|
749
|
+
"signature": "(): G9ImageNativeModule | null",
|
|
750
|
+
"summary": "`NativeModules.G9ImageModule`, or `null` where neither the native library nor the web host provides it."
|
|
751
|
+
},
|
|
592
752
|
{
|
|
593
753
|
"id": "native.native.nativeSpeechModule",
|
|
594
754
|
"export": "nativeSpeechModule",
|
package/catalog/index.md
CHANGED