mirror of
https://github.com/D-Modem/app.git
synced 2026-11-28 17:38:05 -08:00
Shared Kotlin: fax (T.4's MH, T.30, class 1, the PDF of the pages)
T4, T30, Class1 and FaxPdf move to shared/'s common Kotlin (the PDF written to a Sink); the documents' rendering (Android's PdfRenderer) and the fax manager stay the app's. T4Test in common Kotlin: the JVM and native. The app's fax tests pass on the shared code, efax's calls over two modems among them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0172Hejd12i4aYikYW9vgpZs
This commit is contained in:
8 files changed
+71
-52
No files matched your search
@@ -2,7 +2,6 @@
|
||||
// Copyright (C) 2026 Neo-Desktop
|
||||
package com.neodesktop.dmodem.fax
|
||||
|
||||
import com.neodesktop.dmodem.io.asOutputStream
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import com.neodesktop.dmodem.bridge.Bridges
|
||||
@@ -244,7 +243,7 @@ object Fax {
|
||||
val into = DownloadsIncoming(context)
|
||||
val (out, actual) = into.create(name, -1)
|
||||
try {
|
||||
out.asOutputStream().use { FaxPdf.write(pages, it) }
|
||||
out.use { FaxPdf.write(pages, it) }
|
||||
into.done(actual, System.currentTimeMillis() / 1000)
|
||||
} catch (e: IOException) {
|
||||
into.abandon(actual)
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// Copyright (C) 2026 Neo-Desktop
|
||||
package com.neodesktop.dmodem.fax
|
||||
|
||||
import com.neodesktop.dmodem.io.asSink
|
||||
import org.junit.Assert.assertArrayEquals
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertTrue
|
||||
@@ -49,7 +50,7 @@ class FaxPdfTest {
|
||||
assumeTrue("poppler-utils isn't installed", File("/usr/bin/pdfimages").canExecute())
|
||||
val pages = listOf(T4Test.testPage(true, rows = 700, seed = 1), T4Test.testPage(false, rows = 350, seed = 2))
|
||||
val f = tmp.newFile("fax.pdf")
|
||||
f.outputStream().use { FaxPdf.write(pages, it) }
|
||||
f.outputStream().asSink().use { FaxPdf.write(pages, it) }
|
||||
// small: the fax's own coding
|
||||
assertTrue("${f.length()} bytes", f.length() < 200_000)
|
||||
val back = images(f, tmp.newFolder("img"))
|
||||
|
||||
+12
-12
@@ -2,9 +2,9 @@
|
||||
// Copyright (C) 2026 Neo-Desktop
|
||||
package com.neodesktop.dmodem.fax
|
||||
|
||||
import com.neodesktop.dmodem.io.*
|
||||
|
||||
import com.neodesktop.dmodem.transfer.TransferIo
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.IOException
|
||||
|
||||
/**
|
||||
* A class 1 fax modem (T.31) from its DTE: AT commands and their
|
||||
@@ -23,9 +23,9 @@ class Class1(private val io: TransferIo, private val log: (String) -> Unit = {})
|
||||
/** a line from the modem, its CR LF off; null when none came in [ms] */
|
||||
fun line(ms: Long): String? {
|
||||
val sb = StringBuilder()
|
||||
val end = System.currentTimeMillis() + ms
|
||||
val end = monotonicMs() + ms
|
||||
while (true) {
|
||||
val left = end - System.currentTimeMillis()
|
||||
val left = end - monotonicMs()
|
||||
if (left <= 0) return null
|
||||
val c = io.read(left)
|
||||
if (c < 0) return null
|
||||
@@ -40,21 +40,21 @@ class Class1(private val io: TransferIo, private val log: (String) -> Unit = {})
|
||||
/** a command (without its AT): its result line (OK, CONNECT, ERROR, NO CARRIER, +FCERROR, …), null on a timeout */
|
||||
fun command(cmd: String, ms: Long = 5000): String? {
|
||||
log("-> AT$cmd")
|
||||
io.write("AT$cmd\r".toByteArray())
|
||||
io.write("AT$cmd\r".encodeToByteArray())
|
||||
return result(ms)
|
||||
}
|
||||
|
||||
/** a command out (without its AT), its result left for [result] */
|
||||
fun send(cmd: String) {
|
||||
log("-> AT$cmd")
|
||||
io.write("AT$cmd\r".toByteArray())
|
||||
io.write("AT$cmd\r".encodeToByteArray())
|
||||
}
|
||||
|
||||
/** the next result line (the command's echo, and anything else that isn't one, passed over) */
|
||||
fun result(ms: Long): String? {
|
||||
val end = System.currentTimeMillis() + ms
|
||||
val end = monotonicMs() + ms
|
||||
while (true) {
|
||||
val l = line(end - System.currentTimeMillis()) ?: return null
|
||||
val l = line(end - monotonicMs()) ?: return null
|
||||
if (l.startsWith("AT", ignoreCase = true)) continue
|
||||
if (l in RESULTS || l.startsWith("CONNECT") || l.startsWith("+F") && !l.startsWith("+FTM:")) return l
|
||||
lastInfo = l
|
||||
@@ -67,7 +67,7 @@ class Class1(private val io: TransferIo, private val log: (String) -> Unit = {})
|
||||
|
||||
/** octets out, <DLE> doubled, <DLE><ETX> after */
|
||||
fun sendData(data: ByteArray, end: Boolean = true) {
|
||||
val o = ByteArrayOutputStream(data.size + data.size / 64 + 4)
|
||||
val o = Bytes(data.size + data.size / 64 + 4)
|
||||
for (b in data) {
|
||||
o.write(b.toInt())
|
||||
if (b == DLE.toByte()) o.write(DLE)
|
||||
@@ -81,7 +81,7 @@ class Class1(private val io: TransferIo, private val log: (String) -> Unit = {})
|
||||
* timeout of [ms] with nothing coming
|
||||
*/
|
||||
fun readData(ms: Long, max: Int = 4 * 1024 * 1024): ByteArray? {
|
||||
val o = ByteArrayOutputStream()
|
||||
val o = Bytes()
|
||||
var dle = false
|
||||
while (true) {
|
||||
val c = io.read(ms)
|
||||
@@ -99,7 +99,7 @@ class Class1(private val io: TransferIo, private val log: (String) -> Unit = {})
|
||||
} else {
|
||||
o.write(c)
|
||||
}
|
||||
if (o.size() > max) throw IOException("too much data")
|
||||
if (o.size > max) throw IOException("too much data")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -169,7 +169,7 @@ class Class1(private val io: TransferIo, private val log: (String) -> Unit = {})
|
||||
const val T2_MS = 6000L
|
||||
val RESULTS = setOf("OK", "ERROR", "NO CARRIER", "NO DIALTONE", "NO DIAL TONE", "BUSY", "NO ANSWER", "RING", "FCERROR")
|
||||
|
||||
fun hex(b: ByteArray) = b.joinToString(" ") { "%02x".format(it) }
|
||||
fun hex(b: ByteArray) = b.joinToString(" ") { hex2(it.toInt() and 0xff) }
|
||||
|
||||
/** a frame: address, control (set when sent), FCF, FIF */
|
||||
fun frame(fcf: Int, fif: ByteArray = ByteArray(0)) = byteArrayOf(ADDR.toByte(), CTRL.toByte(), fcf.toByte()) + fif
|
||||
+17
-17
@@ -2,8 +2,8 @@
|
||||
// Copyright (C) 2026 Neo-Desktop
|
||||
package com.neodesktop.dmodem.fax
|
||||
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.OutputStream
|
||||
import com.neodesktop.dmodem.io.*
|
||||
|
||||
|
||||
/**
|
||||
* Fax pages as a PDF, as fax software has them: each page an image in
|
||||
@@ -12,30 +12,30 @@ import java.io.OutputStream
|
||||
* Small, and the pels exactly those that came.
|
||||
*/
|
||||
object FaxPdf {
|
||||
fun write(pages: List<FaxPage>, out: OutputStream) {
|
||||
val o = ByteArrayOutputStream()
|
||||
fun write(pages: List<FaxPage>, out: Sink) {
|
||||
val o = Bytes()
|
||||
val offsets = ArrayList<Int>()
|
||||
fun obj(body: ByteArray) {
|
||||
offsets.add(o.size())
|
||||
o.write("${offsets.size} 0 obj\n".toByteArray())
|
||||
offsets.add(o.size)
|
||||
o.write("${offsets.size} 0 obj\n".encodeToByteArray())
|
||||
o.write(body)
|
||||
o.write("\nendobj\n".toByteArray())
|
||||
o.write("\nendobj\n".encodeToByteArray())
|
||||
}
|
||||
fun obj(s: String) = obj(s.toByteArray(Charsets.ISO_8859_1))
|
||||
fun obj(s: String) = obj(latin1(s))
|
||||
fun stream(dict: String, data: ByteArray) =
|
||||
obj("<< $dict /Length ${data.size} >>\nstream\n".toByteArray() + data + "\nendstream".toByteArray())
|
||||
obj("<< $dict /Length ${data.size} >>\nstream\n".encodeToByteArray() + data + "\nendstream".encodeToByteArray())
|
||||
|
||||
o.write("%PDF-1.4\n%âãÏÓ\n".toByteArray(Charsets.ISO_8859_1))
|
||||
o.write(latin1("%PDF-1.4\n%âãÏÓ\n"))
|
||||
// 1: the catalogue, 2: the page tree; then each page's three: the page, its contents, its image
|
||||
val kids = pages.indices.joinToString(" ") { "${3 + it * 3} 0 R" }
|
||||
obj("<< /Type /Catalog /Pages 2 0 R >>")
|
||||
obj("<< /Type /Pages /Kids [$kids] /Count ${pages.size} >>")
|
||||
for ((i, p) in pages.withIndex()) {
|
||||
val page = 3 + i * 3
|
||||
val w = "%.2f".format(java.util.Locale.ROOT, p.width / 204.0 * 72)
|
||||
val h = "%.2f".format(java.util.Locale.ROOT, p.rows.coerceAtLeast(1).toDouble() / (if (p.fine) 196 else 98) * 72)
|
||||
val w = fixed2(p.width / 204.0 * 72)
|
||||
val h = fixed2(p.rows.coerceAtLeast(1).toDouble() / (if (p.fine) 196 else 98) * 72)
|
||||
obj("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 $w $h] /Resources << /XObject << /Im0 ${page + 2} 0 R >> >> /Contents ${page + 1} 0 R >>")
|
||||
stream("", "q $w 0 0 $h 0 0 cm /Im0 Do Q".toByteArray())
|
||||
stream("", "q $w 0 0 $h 0 0 cm /Im0 Do Q".encodeToByteArray())
|
||||
// (the fax's bits, the first in the bottom bit, the other way round: PDF's first in the top bit)
|
||||
val data = T4.encode(p).also { d -> for (k in d.indices) d[k] = reverse(d[k]) }
|
||||
stream(
|
||||
@@ -44,10 +44,10 @@ object FaxPdf {
|
||||
data,
|
||||
)
|
||||
}
|
||||
val xref = o.size()
|
||||
o.write("xref\n0 ${offsets.size + 1}\n0000000000 65535 f \n".toByteArray())
|
||||
for (off in offsets) o.write("%010d 00000 n \n".format(off).toByteArray())
|
||||
o.write("trailer\n<< /Size ${offsets.size + 1} /Root 1 0 R >>\nstartxref\n$xref\n%%EOF\n".toByteArray())
|
||||
val xref = o.size
|
||||
o.write("xref\n0 ${offsets.size + 1}\n0000000000 65535 f \n".encodeToByteArray())
|
||||
for (off in offsets) o.write("${off.toString().padStart(10, '0')} 00000 n \n".encodeToByteArray())
|
||||
o.write("trailer\n<< /Size ${offsets.size + 1} /Root 1 0 R >>\nstartxref\n$xref\n%%EOF\n".encodeToByteArray())
|
||||
out.write(o.toByteArray())
|
||||
}
|
||||
|
||||
+6
-5
@@ -2,7 +2,8 @@
|
||||
// Copyright (C) 2026 Neo-Desktop
|
||||
package com.neodesktop.dmodem.fax
|
||||
|
||||
import java.io.IOException
|
||||
import com.neodesktop.dmodem.io.*
|
||||
|
||||
|
||||
/** how a fax call went, as it goes */
|
||||
data class FaxProgress(
|
||||
@@ -184,8 +185,8 @@ object T30 {
|
||||
var dis: ByteArray? = null
|
||||
var remote = ""
|
||||
var first = true
|
||||
val t1 = System.currentTimeMillis() + 35_000
|
||||
while (dis == null && System.currentTimeMillis() < t1) {
|
||||
val t1 = monotonicMs() + 35_000
|
||||
while (dis == null && monotonicMs() < t1) {
|
||||
val f = if (first) c.readFrame(10_000) else c.receiveFrame()
|
||||
first = false
|
||||
if (f == null) continue
|
||||
@@ -268,7 +269,7 @@ object T30 {
|
||||
}
|
||||
DCN -> throw FaxException("the fax machine hung up after page ${i + 1}")
|
||||
null -> throw FaxException("no answer to page ${i + 1}")
|
||||
else -> throw FaxException("the fax machine answered page ${i + 1} with %02x".format(answer))
|
||||
else -> throw FaxException("the fax machine answered page ${i + 1} with ${hex2(answer)}")
|
||||
}
|
||||
}
|
||||
// phase E
|
||||
@@ -397,7 +398,7 @@ object T30 {
|
||||
c.receiveFrame()
|
||||
return p.copy(phase = "Received")
|
||||
}
|
||||
else -> throw FaxException("the sender ended page $pages with %02x".format(post))
|
||||
else -> throw FaxException("the sender ended page $pages with ${hex2(post)}")
|
||||
}
|
||||
}
|
||||
}
|
||||
+9
-8
@@ -2,7 +2,8 @@
|
||||
// Copyright (C) 2026 Neo-Desktop
|
||||
package com.neodesktop.dmodem.fax
|
||||
|
||||
import java.io.ByteArrayOutputStream
|
||||
import com.neodesktop.dmodem.io.*
|
||||
|
||||
|
||||
/**
|
||||
* A fax page: [rows] rows of [width] pels, 1 = black, packed eight to a
|
||||
@@ -90,8 +91,8 @@ object T4 {
|
||||
var n = 1
|
||||
|
||||
init {
|
||||
java.util.Arrays.fill(zero, -1)
|
||||
java.util.Arrays.fill(one, -1)
|
||||
zero.fill(-1)
|
||||
one.fill(-1)
|
||||
for (c in table) {
|
||||
var at = 0
|
||||
for (ch in c.bits) {
|
||||
@@ -108,9 +109,9 @@ object T4 {
|
||||
|
||||
private fun grow() {
|
||||
val s = zero.size * 2
|
||||
zero = zero.copyOf(s).also { java.util.Arrays.fill(it, s / 2, s, -1) }
|
||||
one = one.copyOf(s).also { java.util.Arrays.fill(it, s / 2, s, -1) }
|
||||
leaf = leaf.copyOf(s).also { java.util.Arrays.fill(it, s / 2, s, -1) }
|
||||
zero = zero.copyOf(s).also { it.fill(-1, s / 2, s) }
|
||||
one = one.copyOf(s).also { it.fill(-1, s / 2, s) }
|
||||
leaf = leaf.copyOf(s).also { it.fill(-1, s / 2, s) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,7 +120,7 @@ object T4 {
|
||||
|
||||
/** bits out, the first in the bottom bit of each byte */
|
||||
private class BitWriter {
|
||||
val out = ByteArrayOutputStream()
|
||||
val out = Bytes()
|
||||
private var cur = 0
|
||||
private var n = 0
|
||||
var count = 0L
|
||||
@@ -243,7 +244,7 @@ object T4 {
|
||||
if (rows.size > 20_000) break
|
||||
}
|
||||
val page = FaxPage(width, rows.size, fine)
|
||||
for ((y, r) in rows.withIndex()) System.arraycopy(r, 0, page.bits, y * stride, stride)
|
||||
for ((y, r) in rows.withIndex()) r.copyInto(page.bits, y * stride, 0, stride)
|
||||
return Decoded(page, bad)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
// Copyright (C) 2026 Neo-Desktop
|
||||
package com.neodesktop.dmodem.fax
|
||||
|
||||
/** [v] as two hex digits (%02x) */
|
||||
internal fun hex2(v: Int): String = (v and 0xff).toString(16).padStart(2, '0')
|
||||
|
||||
/** ISO 8859-1's octets of the text */
|
||||
internal fun latin1(s: String): ByteArray = ByteArray(s.length) { s[it].code.toByte() }
|
||||
|
||||
/** [d] with two decimals, a point between (%.2f in the root locale) */
|
||||
internal fun fixed2(d: Double): String {
|
||||
val c = kotlin.math.round(d * 100).toLong()
|
||||
val sign = if (c < 0) "-" else ""
|
||||
val a = kotlin.math.abs(c)
|
||||
return "$sign${a / 100}.${(a % 100).toString().padStart(2, '0')}"
|
||||
}
|
||||
+7
-7
@@ -2,11 +2,11 @@
|
||||
// Copyright (C) 2026 Neo-Desktop
|
||||
package com.neodesktop.dmodem.fax
|
||||
|
||||
import org.junit.Assert.assertArrayEquals
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import kotlin.test.assertContentEquals
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertTrue
|
||||
import kotlin.test.Test
|
||||
import kotlin.random.Random
|
||||
|
||||
/** T.4's MH coding both ways; T.30's DIS/DCS bits and identities. */
|
||||
@@ -40,7 +40,7 @@ class T4Test {
|
||||
val back = T4.decode(data, fine = fine)
|
||||
assertEquals(0, back.badRows)
|
||||
assertEquals(page.rows, back.page.rows)
|
||||
assertArrayEquals(page.bits, back.page.bits)
|
||||
assertContentEquals(page.bits, back.page.bits)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ class T4Test {
|
||||
// a byte in the middle spoilt: a row or two lost, the rest still there
|
||||
data[data.size / 2] = (data[data.size / 2].toInt() xor 0xff).toByte()
|
||||
val back = T4.decode(data, fine = false)
|
||||
assertTrue("bad rows: ${back.badRows}", back.badRows in 1..3)
|
||||
assertTrue(back.badRows in 1..3, "bad rows: ${back.badRows}")
|
||||
assertEquals(page.rows, back.page.rows)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user