From 47ad7a7fd220ba56d6df3bec749ce467b234b476 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 18 Nov 2026 13:03:59 +0000 Subject: [PATCH] App: label rich text with headings, sub- and superscripts, images, tables and pre A label's text now draws what Qt's rich text draws of these, as Qt 6's QTextDocument does: h1 to h5 bold at 2, 1.5, 1.2, 1 and 0.8 times the label's own size, over any size a style sets in them (h6 plain, as Qt leaves it); a font size 1 to 7 at Qt's 0.7 to 2.4 times the label's size, over any size set in it, read as Qt reads it, a whole number or else 0, so one with a unit is the smallest; sub and sup at 2/3 size, shifted; an img in the line through the engine's getImage, or a data: URL's image in base64, percent-escapes decoded first as Qt does, decoded once for all labels and kept apart from what the engine is asked for, with the engine's images, which are let go of by bytes as well as by count, out of memory caught and one that fails forgotten, at its width and height or its own size, each side rounded as Qt rounds it, a side Qt reads no number off at the file's own, -1 none given, and a 0, given or rounded to, hiding it but keeping its room along the other side, each side 4096 pixels at most, in a font of any size; an image read as nothing as Mudlet reads it, but a label of only images read by its tool tip, as Mudlet reads it, else, where they are links, by an image's alt or title, else its link's target, so TalkBack reaches the links; a table a row to a line with its cells apart, a paragraph in a cell kept in its row, a cell or row left open ended at the next; pre with its spaces in monospace. These and the rest of what differs from Qt, among them the table's lost grid, the room headings and tables do not get, an SVG data: image or a file over 4096 pixels a side not drawn and the file icon Qt draws for an image that does not load, are in docs/divergences.md. A log line no longer names an image's path, which a script chose. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016ujaeGdi2MHnkoicGsDqhg --- .../org/mudlet/android/ui/ScriptLabelsTest.kt | 74 +++++- .../mudlet/android/ui/ScriptWindowsClient.kt | 73 +++++- .../org/mudlet/android/ui/game/ScriptLabel.kt | 81 +++++- docs/architecture.md | 5 +- docs/divergences.md | 18 ++ docs/phase-f.md | 60 +++-- .../org/mudlet/android/protocol/RichText.kt | 246 ++++++++++++++++-- .../mudlet/android/protocol/RichTextTest.kt | 161 +++++++++++- 8 files changed, 645 insertions(+), 73 deletions(-) diff --git a/app/src/androidTest/kotlin/org/mudlet/android/ui/ScriptLabelsTest.kt b/app/src/androidTest/kotlin/org/mudlet/android/ui/ScriptLabelsTest.kt index 6f0ca4bad..505b2af26 100644 --- a/app/src/androidTest/kotlin/org/mudlet/android/ui/ScriptLabelsTest.kt +++ b/app/src/androidTest/kotlin/org/mudlet/android/ui/ScriptLabelsTest.kt @@ -73,8 +73,9 @@ import kotlin.math.abs * one as its new text said, a live label as one live node, a link followed by the engine, by its action or a tap, with * its href as Qt reads it, a `prompt:` one giving the input line the focus, but a web link opened in the browser, a * click-through label's links neither tapped nor actions, also once a label already drawn is made click-through, and - * both again once it no longer is, a label's own colour drawn only where its sheet draws no box of its own, and the - * images the engine is asked for. + * both again once it no longer is, a label's own colour drawn only where its sheet draws no box of its own, the + * images the engine is asked for, an image and a heading in a label's text, and a label of only a linked image read + * by its alt, and one of only images, white space between them or not, by its tool tip. */ @RunWith(AndroidJUnit4::class) class ScriptLabelsTest { @@ -586,6 +587,71 @@ class ScriptLabelsTest { compose.onRoot().tryPerformAccessibilityChecks() } + @Test + fun aLabelsTextDrawsItsImagesAndHeadings() { + // Below the main console's line, on black, so only the text and the image are drawn in a colour + val sheet = "QLabel { background-color: black; color: white; font-size: 10pt }" + val labels = listOf(label("Heading", 0, 300, 300, 100), label("Body", 400, 300, 300, 100), label("Pictured", 0, 450, 300, 100), label("Small", 400, 450, 300, 100)) + session.windows(labels, full = true) + session.label("Heading", text = "

HH

", accessibleName = "Heading", styleSheet = sheet) + session.label("Body", text = "HH", accessibleName = "Body", styleSheet = sheet) + session.label("Pictured", text = "ab", styleSheet = sheet) + // The same image in the smallest font size, as Qt reads a `font size` with a unit: drawn at the same size + session.label("Small", text = "cd", styleSheet = sheet) + // Each read as its text without the image; the test's clock recomposes only while the test waits on Compose, so + // the labels are drawn, and ask for their image, first + compose.waitUntil(WAIT_MS) { listOf("ab", "cd").all { compose.onAllNodes(hasText(it)).fetchSemanticsNodes().isNotEmpty() } } + // The image is asked of the engine at its own size + val request = session.receive("getImage") + assertEquals("/data/dot.png", request.getString("path")) + assertFalse(request.toString(), request.has("w") || request.has("frame")) + session.image("/data/dot.png", png(4, 2, android.graphics.Color.RED), 4, 2, request.get("id")) + compose.waitUntil(WAIT_MS) { EngineClient.front.windows.images[ScriptWindowsClient.ImageRequest("/data/dot.png")] != null } + compose.waitForIdle() + + // Drawn in the line at the size its attributes give, 40 by 20 of the label's 300 by 100 pixels, in both fonts + for (shown in listOf("ab", "cd")) { + val pictured = compose.onNode(hasText(shown)).captureToImage().toPixelMap() + val red = (0 until pictured.height).flatMap { y -> (0 until pictured.width).map { x -> x to y } }.filter { (x, y) -> + val c = pictured[x, y] + c.red > 0.8f && c.green < 0.2f && c.blue < 0.2f + } + assertTrue("$shown: no image drawn", red.isNotEmpty()) + // Scaled as the label's text is, by the less of its window's two scales + val scale = minOf(pictured.width / 300f, pictured.height / 100f) + assertNear("$shown: image width", 40 * scale, (red.maxOf { it.first } - red.minOf { it.first } + 1).toFloat()) + assertNear("$shown: image height", 20 * scale, (red.maxOf { it.second } - red.minOf { it.second } + 1).toFloat()) + } + + // A heading's text is taller than the same text in the body, as Qt draws an h1 at twice the label's size + fun inkRows(name: String): Int { + val pixels = compose.onNode(hasContentDescription(name)).captureToImage().toPixelMap() + return (0 until pixels.height).count { y -> (0 until pixels.width).any { x -> pixels[x, y].red > 0.5f } } + } + val heading = inkRows("Heading") + val body = inkRows("Body") + assertTrue("heading $heading rows, body $body", body > 0 && heading > body * 3 / 2) + compose.onRoot().tryPerformAccessibilityChecks() + } + + @Test + fun aLabelOfOnlyALinkedImageIsReadByItsAltWithItsLinkAnAction() { + session.windows(listOf(label("eye", w = 100, h = 40), label("lid", y = 50, w = 100, h = 40)), full = true) + session.label("eye", text = """Look""") + // Not left out as decoration: read by the image's alt, its link an action that has the engine follow it + compose.waitUntil(WAIT_MS) { compose.onAllNodes(hasText("Look")).fetchSemanticsNodes().isNotEmpty() } + val follow = string(R.string.label_follow_link, "send:look") + val action = compose.onNode(hasText("Look")).fetchSemanticsNode().config[SemanticsActions.CustomActions].single { it.label == follow } + compose.runOnIdle { action.action() } + assertEquals("send:look", session.receive("followLabelLink").getString("href")) + // One of only an image that is no link, read by its tool tip, as Mudlet reads it, not left out as decoration + session.label("lid", text = """""", toolTip = "Peek") + compose.waitUntil(WAIT_MS) { compose.onAllNodes(hasText("Peek")).fetchSemanticsNodes().isNotEmpty() } + // So too with white space between its images, which Mudlet simplifies away before it reads the text + session.label("lid", text = """ """, toolTip = "Both") + compose.waitUntil(WAIT_MS) { compose.onAllNodes(hasText("Both")).fetchSemanticsNodes().isNotEmpty() } + } + // The pointer events of one tap, up to its release private fun tap(): List { val events = ArrayList() @@ -606,9 +672,9 @@ class ScriptLabelsTest { private fun label(name: String, x: Int = 0, y: Int = 0, w: Int, h: Int) = FakeEngine.window(name, kind = "label", x = x, y = y, w = w, h = h) - private fun png(w: Int, h: Int): ByteArray { + private fun png(w: Int, h: Int, colour: Int = 0): ByteArray { val out = ByteArrayOutputStream() - Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888).compress(Bitmap.CompressFormat.PNG, 100, out) + Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888).apply { eraseColor(colour) }.compress(Bitmap.CompressFormat.PNG, 100, out) return out.toByteArray() } diff --git a/app/src/main/kotlin/org/mudlet/android/ui/ScriptWindowsClient.kt b/app/src/main/kotlin/org/mudlet/android/ui/ScriptWindowsClient.kt index b75aabf27..19c596026 100644 --- a/app/src/main/kotlin/org/mudlet/android/ui/ScriptWindowsClient.kt +++ b/app/src/main/kotlin/org/mudlet/android/ui/ScriptWindowsClient.kt @@ -12,12 +12,16 @@ import androidx.compose.runtime.setValue import androidx.compose.ui.graphics.ImageBitmap import androidx.compose.ui.graphics.asImageBitmap import androidx.core.net.toUri +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.NonCancellable +import kotlinx.coroutines.withContext import org.mudlet.android.protocol.AddonCommand import org.mudlet.android.protocol.ButtonBar import org.mudlet.android.protocol.EngineMessage import org.mudlet.android.protocol.KeyBinding import org.mudlet.android.protocol.KeyPressResult import org.mudlet.android.protocol.LabelLink +import org.mudlet.android.protocol.LabelStyle import org.mudlet.android.protocol.LayoutMode import org.mudlet.android.protocol.MapInfo import org.mudlet.android.protocol.MapPoint @@ -28,6 +32,7 @@ import org.mudlet.android.protocol.POINTER_LEFT import org.mudlet.android.protocol.Placement import org.mudlet.android.protocol.PointerType import org.mudlet.android.protocol.Requests +import org.mudlet.android.protocol.RichText import org.mudlet.android.protocol.ScriptWindows import org.mudlet.android.protocol.TextBoxContent @@ -93,8 +98,12 @@ class ScriptWindowsClient internal constructor(private val session: ProfileSessi // Where this engine instance was last told each user window is private val sentPlacements = HashMap() - /** An image file of the engine's, as a label shows it: `getImage`'s fields. */ - data class ImageRequest(val path: String, val w: Int? = null, val h: Int? = null, val frame: Int? = null) { + /** + * An image file of the engine's, as a label shows it: `getImage`'s fields; or, [decoded], the image of the `data:` + * URL [path] in a label's text, which the app decodes itself ([decodeImage]) and the engine is never asked for, so + * a style sheet's `url()` of the same text, which the engine refuses as Qt's style sheet draws none, is apart. + */ + data class ImageRequest(val path: String, val w: Int? = null, val h: Int? = null, val frame: Int? = null, val decoded: Boolean = false) { init { require((w == null) == (h == null)) { "an image is asked for at a size with both w and h, or neither" } } @@ -107,10 +116,14 @@ class ScriptWindowsClient internal constructor(private val session: ProfileSessi */ class MapImage(val bitmap: ImageBitmap?, val w: Int, val h: Int, val viewOnly: Boolean, val info: MapInfo? = null) - /** The images the engine sent, by what was asked; one not there yet is asked for with [requestImage]. */ + /** + * The images the engine sent, and those the app decoded of `data:` URLs ([decodeImage]), by what was asked; one not + * there yet is asked for with [requestImage]. + */ val images = mutableStateMapOf() - // What was asked and not yet answered, by request id, and what of this engine instance's is kept or awaited + // What was asked and not yet answered, by request id, and what of this engine instance's is kept or awaited, or + // being decoded private val pendingImages = HashMap() private val askedImages = HashSet() @@ -192,9 +205,27 @@ class ScriptWindowsClient internal constructor(private val session: ProfileSessi val request = pendingImages.remove((message.replyTo as? Number)?.toLong()) ?: return if (bitmap == null) { // Not asked again: the same file would not decode better - Log.w(TAG, "the engine's image ${request.path} did not decode") + Log.w(TAG, "an image of the engine's did not decode") return } + keep(request, bitmap) + } + + /** + * Decodes [request]'s `data:` URL, [ImageRequest.decoded], off the main thread into [images], where the engine's + * files are kept and let go: once, as [requestImage] asks once, and to its end, so each label that shows it gets it. + * One that does not decode is forgotten, so script-made URLs that fail do not pile up; it is decoded again only when + * a label shows it afresh. + */ + suspend fun decodeImage(request: ImageRequest) { + if (request in images || !askedImages.add(request)) return + withContext(NonCancellable) { + val bitmap = withContext(Dispatchers.Default) { decodeData(request.path) } + if (bitmap != null) keep(request, bitmap) else askedImages.remove(request) + } + } + + private fun keep(request: ImageRequest, bitmap: ImageBitmap) { images[request] = bitmap imageOrder.add(request) trimImages() @@ -211,15 +242,16 @@ class ScriptWindowsClient internal constructor(private val session: ProfileSessi trimImages() } - // Lets go of the oldest images no label shows while there are more than MAX_IMAGES; one let go is asked again - // when a label shows it + // Lets go of the oldest images no label shows while there are more than MAX_IMAGES, or those no label shows hold + // more than MAX_IMAGE_BYTES; one let go is asked again when a label shows it private fun trimImages() { + var bytes = images.entries.sumOf { (request, image) -> if (request in shownImages) 0L else image.width * image.height * 4L } val unshown = imageOrder.iterator() - while (images.size > MAX_IMAGES && unshown.hasNext()) { + while ((images.size > MAX_IMAGES || bytes > MAX_IMAGE_BYTES) && unshown.hasNext()) { val request = unshown.next() if (request in shownImages) continue unshown.remove() - images.remove(request) + images.remove(request)?.let { bytes -= it.width * it.height * 4L } askedImages.remove(request) } } @@ -451,7 +483,24 @@ class ScriptWindowsClient internal constructor(private val session: ProfileSessi /** [message]'s PNG, decoded off the main thread; null for one that does not decode. */ internal fun decode(message: EngineMessage.Map): ImageBitmap? = decodePng(message.png) - private fun decodePng(png: ByteArray): ImageBitmap? = BitmapFactory.decodeByteArray(png, 0, png.size)?.asImageBitmap() + // An image's bytes decoded, null for bytes that do not decode or an image too large for the memory left + private fun decodePng(png: ByteArray): ImageBitmap? = try { + BitmapFactory.decodeByteArray(png, 0, png.size)?.asImageBitmap() + } catch (_: OutOfMemoryError) { + null + } + + // A `data:` URL's image, null as for the engine's, and for one wider or higher than the engine sends one + // (`PngImage::maxSide`), found from its size alone first; its base64 read too within the memory left + private fun decodeData(path: String): ImageBitmap? = try { + RichText.Image(path, null, null).data()?.let { bytes -> + val bounds = BitmapFactory.Options().apply { inJustDecodeBounds = true } + BitmapFactory.decodeByteArray(bytes, 0, bytes.size, bounds) + if (maxOf(bounds.outWidth, bounds.outHeight) > LabelStyle.MAX_LENGTH) null else decodePng(bytes) + } + } catch (_: OutOfMemoryError) { + null + } private const val TAG = "ScriptWindows" @@ -460,5 +509,9 @@ class ScriptWindowsClient internal constructor(private val session: ProfileSessi // The images kept beyond those shown: few enough that sized ones asked again as labels resize do not pile up private const val MAX_IMAGES = 64 + + // And the bytes those hold at most, as images in a label's text are kept at their own size: one of the + // largest the engine sends, 4096 pixels a side + private const val MAX_IMAGE_BYTES = 64L shl 20 } } diff --git a/app/src/main/kotlin/org/mudlet/android/ui/game/ScriptLabel.kt b/app/src/main/kotlin/org/mudlet/android/ui/game/ScriptLabel.kt index 1d5de74a8..782e4ad86 100644 --- a/app/src/main/kotlin/org/mudlet/android/ui/game/ScriptLabel.kt +++ b/app/src/main/kotlin/org/mudlet/android/ui/game/ScriptLabel.kt @@ -14,6 +14,8 @@ import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.wrapContentSize +import androidx.compose.foundation.text.InlineTextContent +import androidx.compose.foundation.text.appendInlineContent import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Surface import androidx.compose.material3.Text @@ -21,6 +23,7 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.DisposableEffect import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue +import androidx.compose.runtime.key import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.rememberUpdatedState @@ -75,12 +78,15 @@ import androidx.compose.ui.semantics.traversalIndex import androidx.compose.ui.text.AnnotatedString import androidx.compose.ui.text.LinkAnnotation import androidx.compose.ui.text.ParagraphStyle +import androidx.compose.ui.text.Placeholder +import androidx.compose.ui.text.PlaceholderVerticalAlign import androidx.compose.ui.text.SpanStyle import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.buildAnnotatedString import androidx.compose.ui.text.font.FontFamily import androidx.compose.ui.text.font.FontStyle import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.text.style.BaselineShift import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.text.style.TextDecoration import androidx.compose.ui.text.style.TextOverflow @@ -93,6 +99,7 @@ import androidx.compose.ui.unit.IntSize import androidx.compose.ui.unit.LayoutDirection import androidx.compose.ui.unit.TextUnit import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.em import androidx.compose.ui.window.Popup import androidx.compose.ui.window.PopupPositionProvider import kotlinx.coroutines.delay @@ -268,6 +275,7 @@ internal fun fontPx(style: LabelStyle, window: ScriptWindow): Float = style.font /** A label's style sheet, its text if it has any, the scale it is drawn at, and whether its links are followed. */ private class LabelText(val style: LabelStyle, val rich: RichText?, val scale: Float, val linksFollowable: Boolean) { val plain: String = rich?.plain.orEmpty() + val images: List = rich?.images.orEmpty() /** The links a screen reader is offered as actions. */ val links: List> = if (linksFollowable) rich?.links.orEmpty() else emptyList() @@ -381,18 +389,27 @@ private fun labelReading( } } // A live label keeps one node, blank or not, so its first text is a change TalkBack hears; its links are then - // actions, as a named label's are. Blank, it holds no text, so it is not a stop that says nothing - if (live != null && accessibleName.isEmpty()) { + // actions, as a named label's are. Blank, it holds no text, so it is not a stop that says nothing. One with images + // and text is read so too, by its text without the object replacement characters the images are drawn in; one + // whose only text is images (and white space, which Mudlet's TLabelModel simplifies away), by its tool tip, as it + // reads it, else, where they are links, an image's `alt` or `title`, else its link's target, so its links are + // reached + val read = when { + plain.isBlank() && linkActions.isNotEmpty() -> tooltip.ifEmpty { text.images.firstOrNull { it.alt.isNotEmpty() }?.alt ?: text.links.first().second } + plain.isBlank() && text.images.isNotEmpty() -> tooltip + else -> plain + } + if ((live != null || (text.images.isNotEmpty() && read.isNotEmpty())) && accessibleName.isEmpty()) { return { - if (plain.isNotEmpty()) this.text = AnnotatedString(plain) + if (read.isNotEmpty()) this.text = AnnotatedString(read) if (linkActions.isNotEmpty()) customActions = linkActions liveAs(live) inOrder() } } return when { - // Its text, links and all, read as the text draws it - accessibleName.isEmpty() && plain.isNotEmpty() -> null + // Its text, links and all, read as the text draws it; images with only white space between them are no text + accessibleName.isEmpty() && plain.isNotEmpty() && (text.images.isEmpty() || plain.isNotBlank()) -> null accessibleName.isNotEmpty() -> { { @@ -656,7 +673,13 @@ internal fun Modifier.edgePadding(edges: Edges, sx: Float, sy: Float, density: D ) } -/** The engine's image for [request], asked for once of each engine instance; null until it comes, or for no request. */ +/** + * The engine's image for [request], asked for once of each engine instance; null until it comes, or for no request. + * Or, for a [ImageRequest.decoded] one, the image its `data:` URL holds in base64, as Qt's QTextDocument decodes one + * itself, decoded once off the main thread and kept with the engine's images (`ScriptWindowsClient.decodeImage`); null + * until then, and for one that does not decode, is no image Android reads (an SVG), or is wider or higher than the + * engine sends one. + */ @Composable internal fun engineImage(request: ImageRequest?): ImageBitmap? { val session = EngineClient.front @@ -666,8 +689,8 @@ internal fun engineImage(request: ImageRequest?): ImageBitmap? { request?.let(client::imageShown) onDispose { request?.let(client::imageHidden) } } - // A new engine instance is asked again - LaunchedEffect(request, client, session.attaches) { request?.let(client::requestImage) } + // A new engine instance is asked again, and a `data:` URL's image, which it lets go of, decoded again + LaunchedEffect(request, client, session.attaches) { request?.let { if (it.decoded) client.decodeImage(it) else client.requestImage(it) } } return request?.let { client.images[it] } } @@ -764,7 +787,10 @@ private fun LabelSvg(content: LabelContent) { } } -/** The label's rich text, unwrapped as QLabel draws it, with its links followed when tapped, where they are followed. */ +/** + * The label's rich text, unwrapped as QLabel draws it, with its links followed when tapped, where they are followed, + * and its images, the engine's files or a `data:` URL's, in the line on its baseline as Qt draws them. + */ @Composable private fun LabelTextView(label: String, text: LabelText, modifier: Modifier = Modifier) { val rich = text.rich ?: return @@ -773,6 +799,31 @@ private fun LabelTextView(label: String, text: LabelText, modifier: Modifier = M val density = LocalDensity.current val scale = text.scale val toSp: (Float) -> TextUnit = { px -> with(density) { (px * scale).toSp() } } + // Each image is a placeholder of its size, which may be the file's own, so known once the engine sends the file, + // or the app has decoded a `data:` URL's in base64, as Qt's QTextDocument decodes one itself (but an SVG, which + // Android's BitmapFactory does not read). The engine reads only files: a web URL, which QTextDocument does not + // fetch either, is not asked + val images = remember(rich) { rich.blocks.flatMap { block -> block.spans.mapNotNull { span -> span.style.image?.let { it to span.style.drawnSizePx } } }.distinct() } + val files = images.map { it.first.path }.distinct().filterNot { "://" in it }.associateWith { path -> + key(path) { engineImage(ImageRequest(path, decoded = path.startsWith("data:", ignoreCase = true))) } + } + // An image's span's font in device pixels, whole as Android draws a font's and one at least, whatever size the + // script gives it, so a placeholder in ems of it is drawn at its size + val imageFontPx = { px: Float -> (px * scale).roundToInt().coerceAtLeast(1) } + // Each placeholder named by where the image and its span's size are in [images], so a second of the same is the + // same placeholder, and not by the image, whose `data:` URL may be long. In ems of its span's font as drawn, so + // exact whatever the font scale, as Compose may not draw a placeholder's sp (Android 14's nonlinear font scaling) + val inline = images.withIndex().associate { (i, placed) -> + val (image, fontPx) = placed + val file = files[image.path] + val (w, h) = image.size(file?.width, file?.height) ?: (0f to 0f) + val drawnPx = imageFontPx(fontPx) + // Under 32767 device pixels a side, past which Compose cannot measure what is in a placeholder + val ems = { side: Float -> (minOf(side * scale, 32766f) / drawnPx).em } + "$i" to InlineTextContent(Placeholder(ems(w), ems(h), PlaceholderVerticalAlign.AboveBaseline)) { + if (file != null) Image(file, contentDescription = null, modifier = Modifier.fillMaxSize(), contentScale = ContentScale.FillBounds) + } + } // Keyed on the whole of what the label's text is drawn from, so a change to any part of it, as to whether its links // are followed, redraws it val annotated = remember(label, text, density) { @@ -784,7 +835,11 @@ private fun LabelTextView(label: String, text: LabelText, modifier: Modifier = M val link = if (href != null && text.linksFollowable) LinkAnnotation.Clickable(href, linkInteractionListener = { EngineClient.front.windows.followLabelLink(context, label, href) }) else null if (link != null) pushLink(link) // Drawn as a link whether or not it is followed, as QLabel draws one whatever its text interaction - withStyle(spanStyle(span.style, toSp, link = href != null)) { append(span.text) } + val image = span.style.image + val drawn = spanStyle(span.style, toSp, link = href != null) + withStyle(if (image == null) drawn else drawn.copy(fontSize = with(density) { imageFontPx(span.style.drawnSizePx).toSp() })) { + if (image != null) appendInlineContent("${images.indexOf(image to span.style.drawnSizePx)}", span.text) else append(span.text) + } if (link != null) pop() } } @@ -799,7 +854,7 @@ private fun LabelTextView(label: String, text: LabelText, modifier: Modifier = M fontFamily = style.fontFamily?.let(::fontFamily), textAlign = textAlign(style.hAlign), ) - Text(annotated, modifier, style = base, softWrap = false, overflow = TextOverflow.Clip) + Text(annotated, modifier, style = base, softWrap = false, overflow = TextOverflow.Clip, inlineContent = inline) } private fun textAlign(align: HAlign?): TextAlign = when (align) { @@ -817,11 +872,13 @@ private fun spanStyle(style: RichText.Style, toSp: (Float) -> TextUnit, link: Bo return SpanStyle( color = style.color?.let { Color(it) } ?: if (link) LINK_COLOUR else Color.Unspecified, background = style.background?.let { Color(it) } ?: Color.Unspecified, - fontSize = toSp(style.sizePx), + fontSize = toSp(style.drawnSizePx), fontWeight = if (style.bold) FontWeight.Bold else null, fontStyle = if (style.italic) FontStyle.Italic else null, fontFamily = style.family?.let(::fontFamily), textDecoration = if (decorations.isEmpty()) null else TextDecoration.combine(decorations), + // Compose shifts by the font's ascent, Qt by its whole height: near enough for a sub- or superscript + baselineShift = if (style.raise != 0f) BaselineShift(style.raise) else null, ) } diff --git a/docs/architecture.md b/docs/architecture.md index 6378eb4fb..5e8b5b057 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -1469,7 +1469,10 @@ device shows, below the UI's debug hook: link, and the link as Qt reads it from the text, a `prompt:` link giving the input line the focus and a `send:` one not, and neither sent again nor run by the app on a refusal, a web link opened with an intent and not sent, a click-through label's links neither tapped nor actions, also once a label already drawn is made click-through, and - both again once it no longer is, and a label's image asked of the engine; the button bars: a bar's buttons + both again once it no longer is, a label's image asked of the engine, an image in a label's text asked of it and + drawn in the line at its `img`'s size, in its font and in the smallest too, a label of only a linked image read by its + `alt` with its link an action and one of only an image by its tool tip, and a heading taller than the label's other + text; the button bars: a bar's buttons as 48 dp buttons above or beside the console and read after the windows, whose taps send `pressButton`, a push-down button as a switch, a folder's menu, an icon button's name and icon, a bar's and a button's style sheets drawn, with a sheet's own frame, its padding, border and margin diff --git a/docs/divergences.md b/docs/divergences.md index 646004faf..a9e99130f 100644 --- a/docs/divergences.md +++ b/docs/divergences.md @@ -318,6 +318,24 @@ reach whatever server it named, or log in to one of the player's games with its label's rich text black for its text and no change to its background; Qt takes the colour from the widget's palette (`docs/phase-f.md`, "Labels"). The app's widgets have no Qt palette, and the desktop's palette, which differs with its style and theme, does not reach it. +- A table in a label's text is drawn a row to a line, with its cells four spaces apart and a cell's paragraphs a space + apart; Qt lays the cells out in a grid, each column as wide as its widest cell, so a column's cells line up, and a + cell's paragraphs one under another in it. Nor does the app leave the room Qt leaves above and below a heading, a + paragraph or a table (`docs/phase-f.md`, "Labels"). A label is drawn as one text, whose lines Compose lays out as a + paragraph each, which keeps a label's links, taps and reading in one place; a grid of its own would take a layout of + cells the size of a table, for the few labels that hold one. +- An image in a label's text that the engine cannot read is not drawn; Qt draws a small file icon in its place, 16 + pixels a side or the size the `img` gives (`docs/phase-f.md`, "Labels"). The app has no such icon of Qt's. Nor is a + file drawn that is over 4096 pixels a side, which the engine does not send, nor the image of a `data:` URL that is not + in base64, is an SVG or is over 4096 pixels a side, which Qt draws (an SVG where Qt's SVG image plugin is loaded, as + Mudlet's builds load it): the app decodes only base64, as scripts write such an image, with Android's own decoder, + which reads no SVG, and no image larger than the engine sends. Along a side whose `width` or `height` is no number or + a negative one, Qt draws the file's own size but leaves it no room along it: with such a width the text after it is + drawn over it, and with such a height, or one worked out from such a width, it hangs below the baseline over the line + below. The app leaves it its room, its bottom on the baseline, as a placeholder in the line is as big as what is drawn + in it. A label whose only text is linked images and that has no tool tip TalkBack reads by an image's `alt` or + `title`, else its link's target, so its links can be reached; Mudlet, which reads a label of only images by its tool + tip as the app does, reads it as nothing. - A border whose colour the sheet does not name (`border: 2px solid`, or `border-width` and `border-style` alone) is drawn in the text's colour, the sheet's `color` or else the app's; Qt draws it black whatever the `color`. One it names but Qt reads no colour off (`border: 2px solid none`) is drawn as Qt draws it, in the sheet's `color`, or clear diff --git a/docs/phase-f.md b/docs/phase-f.md index dc48bf011..f0a553614 100644 --- a/docs/phase-f.md +++ b/docs/phase-f.md @@ -313,8 +313,9 @@ A `label` is a `Box` at its geometry, drawn in this order: ([`architecture.md`](architecture.md), "Gaps"). - **Text**: Qt's rich text converted to an `AnnotatedString` by the app's own parser for the tags Geyser and scripts write: `b`, `i`, `u`, `s`, `font` (`color`, `size`, `face`), `span` with `style` (`color`, `background-color`, - `font-size`, `font-weight`), `br`, `p`/`div` with `align`, `center`, `a`. Anything else keeps its text without its - formatting. A link's `href` is read as Qt's `QTextHtmlParser` reads it, as the engine follows only that: a quoted + `font-size`, `font-weight`), `br`, `p`/`div` with `align`, `center`, `a`, and the headings, `sub`, `sup`, `img`, + `pre` and tables below. Anything else keeps its text without its formatting. A link's `href` is read as Qt's + `QTextHtmlParser` reads it, as the engine follows only that: a quoted value up to its own closing quote, `<`, `>` and the other quote included, and entities decoded as Qt decodes them (numeric ones, and the named ones of Qt's table, `HtmlEntities.kt`: HTML 4's, with Qt's own `∧` and `∨`, and `&`, `>`, `<`, `"`, `'` and `%`), but in a single-quoted value, which Qt keeps as written. @@ -322,6 +323,31 @@ A `label` is a `Box` at its geometry, drawn in this order: black and a background it reads none off stays as it was, as `QTextHtmlParser` draws them; `font`'s `color` takes only a name or `#` and hex digits, and anything else is black, but an empty value, which Qt passes over as it does any attribute's. +- **Headings, scripts, images and tables**, as Qt 6's `QTextDocument` draws them: `h1` to `h5` are bold at 2, 1.5, 1.2, + 1 and 0.8 times the label's own size (Qt's `FontSizeAdjustment` 3, 2, 1, 0 and -1, which + `QTextFormatPrivate::resolveFont()` takes of the document's font), whatever size is around them or set by a `style` on + or in them, but a `font size` in them, which is such an adjustment too and so also against the label's own size; `h6` + is plain text, as Qt leaves it. A `font size` is 0.7, 0.8, 1, 1.2, 1.5, 2 or 2.4 times the label's size for 1 to 7, + from 3 with a `+` or `-` first, and over any size set in it; one that is not a whole number Qt reads as 0, so + `size="12px"` is the smallest. `sub` and `sup` are drawn at 2/3 of their size, a sixth down or a half up (Qt's + `subScriptBaseline()` and `superScriptBaseline()`, of the font's height; Compose's `BaselineShift` takes them of its + ascent, a little less). An `img` is drawn in the line, its bottom on the baseline: the engine's file at its `src` + (`getImage`; `qrc:` read as `:`, and a `file:` URL as its path), or the image of a `data:` URL in base64, which the + app decodes itself as Qt's `QTextDocument` does, its percent-escapes first, once for all the labels that show it and + kept with the engine's images, but for an SVG, which Android's decoder does not read, or one over 4096 pixels a side; + at the `width` and `height` it gives, at one of them with its shape kept, or else at its own size, each side drawn + 4096 pixels at most (`LabelStyle.MAX_LENGTH`). A size is a number alone, as Qt reads it, rounded to a whole pixel as + `QTextImageHandler` rounds it, and -1 is Qt's for none given: a 0 hides the image, as does a side worked out from the + other that rounds to 0, which keeps its room along the side given, as Qt's layout does, and along a side given as + anything else (`80px`, `50%`) or as another negative number Qt draws the file's own size, as `QPainter::drawPixmap()` + draws a negative width. One the engine cannot read or does not send (a file over 4096 pixels a side) is not drawn, and + takes room only where it gives both sides, neither negative; a `:/` resource is drawn only where the engine has it, + and a web URL, which `QTextDocument` does not fetch either, is not asked for. An image is read as nothing, as Mudlet's + `TLabelModel` reads it. A table's row is a line with its cells four spaces apart, a `th` bold, a paragraph in a cell a + space from the rest of it, as Qt's `toHtml()` writes a paragraph in every cell, and a cell or row left open ends at + the next, as Qt's `QTextHtmlParser` ends it; `pre` keeps its spaces, tabs and line breaks, but for a line break right + after its start or before its end, in a monospace font. A heading, paragraph or table has no room above or below it + ([`divergences.md`](divergences.md), "Labels and TalkBack"). Taps reach the label with `pointer`, in its own Mudlet pixels: a tap is `enter`, `press` and `release`, a double tap adds `doubleClick`, a long press is the right button's `press` and `release` (Geyser's right-click menus); `leave` @@ -415,21 +441,23 @@ as the screen. Phase E's requirements apply to every pull request here; in short, and what they mean for script windows: -- **Labels.** A label with `accessibleButton` is a button (`Role.Button`) whose label is its `accessibleName`, else - its text without formatting, else its tool tip; its click is the tap's `pointer` sequence, and a right-click menu - is a custom accessibility action. A label with text and no callbacks is text. One with only an image and no +- **Labels.** A label with `accessibleButton` is a button (`Role.Button`) whose label is its `accessibleName`, else its + text without formatting, else its tool tip; its click is the tap's `pointer` sequence, and a right-click menu is a + custom accessibility action. A label with text and no callbacks is text. One with only an image and no `accessibleName` is decorative and left out of the tree, unless it is a button, which then gets its name from its - window name, and a lint-style warning in the log. A label with a live region (gap 3) is one node with - `liveRegion`, blank or not, so TalkBack says its new text as the desktop does, its first text too. An unnamed one - reads its text, its links as actions as a named label's are; a named one, or a named button, has its text as its - `stateDescription` unless its name says the same, so its new text is said after its name. A click-through label's - links are not actions, as the desktop's screen readers cannot reach them ("Labels"). Unlike the desktop, - TalkBack also reads a live label when a script makes it live or changes its region, when its name changes, and - (its name alone) when a named one's text goes blank; and the app does not wait for the profile's "Make script - buttons reachable with the keyboard" setting (`accessibleLabels`), which the desktop needs too. A label that is not - a button and has an accessible value (`setLabelAccessibleValue()`, gap 2) is a progress bar, as on the desktop: - `progressBarRangeInfo` with its value and range, its name, else its text, as its content description, and its - percentage as its `stateDescription`, after its text where its name does not say it ("Level 3, 40%"), so the value + window name, and a lint-style warning in the log. An unnamed one whose text has images and words is one node, read by + its text without the images, its links as actions; one whose only text is images, by its tool tip, as Mudlet reads it, + else, where they are links, an image's `alt` or `title`, else its link's target, so its links stay within reach. A + label with a live region (gap 3) is one node with `liveRegion`, blank or not, so TalkBack says its new text as the + desktop does, its first text too. An unnamed one reads its text, its links as actions as a named label's are; a named + one, or a named button, has its text as its `stateDescription` unless its name says the same, so its new text is said + after its name. A click-through label's links are not actions, as the desktop's screen readers cannot reach them + ("Labels"). Unlike the desktop, TalkBack also reads a live label when a script makes it live or changes its region, + when its name changes, and (its name alone) when a named one's text goes blank; and the app does not wait for the + profile's "Make script buttons reachable with the keyboard" setting (`accessibleLabels`), which the desktop needs too. + A label that is not a button and has an accessible value (`setLabelAccessibleValue()`, gap 2) is a progress bar, as on + the desktop: `progressBarRangeInfo` with its value and range, its name, else its text, as its content description, and + its percentage as its `stateDescription`, after its text where its name does not say it ("Level 3, 40%"), so the value is always said; not live, for the reason a gauge is not, so the `Announcer` says a live one's new text. - **Gauges** are one node: `progressBarRangeInfo` with the value and range (the `_back` label's accessible value, else 0 to 100 from the front label's width), the `_text` label's text, else the percentage, as diff --git a/protocol/src/main/kotlin/org/mudlet/android/protocol/RichText.kt b/protocol/src/main/kotlin/org/mudlet/android/protocol/RichText.kt index 24ffec73d..55b69e29a 100644 --- a/protocol/src/main/kotlin/org/mudlet/android/protocol/RichText.kt +++ b/protocol/src/main/kotlin/org/mudlet/android/protocol/RichText.kt @@ -1,16 +1,68 @@ package org.mudlet.android.protocol +import java.util.Base64 + /** * A label's text as the app draws it (docs/phase-f.md, "Labels"): Qt's rich text, of which [parse] reads the tags * Geyser and scripts write, `b`, `i`, `u`, `s`, `font`, `span` and any tag's `style`, `br`, `p`, `div`, `center` and - * `a`. Any other tag keeps its text without its formatting; one that holds no text to show (`head`, `style`, + * `a`, and also `h1` to `h5`, `sub`, `sup`, `img`, `pre` and a table's rows (`tr`), each a line with its cells (`td`, + * `th`) apart. Any other tag keeps its text without its formatting; one that holds no text to show (`head`, `style`, * `script`, `title`) is dropped with it. */ data class RichText(val blocks: List) { /** A paragraph: [align] across the label, null for the label's own. */ data class Block(val align: HAlign?, val spans: List) - data class Span(val text: String, val style: Style) + data class Span(val text: String, val style: Style) { + /** Its text as a screen reader is given it: none for an image, as Mudlet's `TLabelModel` reads a label. */ + val readText: String get() = if (style.image == null) text else "" + } + + /** + * An `img`: the engine's file at [path], or the image a `data:` URL holds, its `width` and `height` in Mudlet's + * pixels where they are given, a number alone as Qt reads them, rounded to a whole pixel as `QTextImageHandler` + * rounds it, and -2 for one that is no number, as Qt keeps it (but -1, Qt's own for a side not given, is null), and + * its `alt` or else its `title`, for a screen reader. Its span's text is Qt's object replacement character. + */ + data class Image(val path: String, val widthPx: Float?, val heightPx: Float?, val alt: String = "") { + /** + * Its size in Mudlet's pixels, as Qt's `QTextImageHandler` sizes it, of an image [w] by [h] pixels, or null + * while it has not loaded: both attributes as given, one with the other kept in proportion and rounded, + * neither the image's own size; and along a side whose size is negative the image's own, as + * `QPainter::drawPixmap()` draws a negative width or height, which draws nothing for a side of 0, so a size + * of 0, given or rounded to, hides it but keeps its room along the other side, as Qt's layout does. Null to + * draw nothing: an image that does not load takes room only where + * both are given and neither is negative. Each side [LabelStyle.MAX_LENGTH] at most, so a script's size is + * never one the app cannot lay out. + */ + fun size(w: Int?, h: Int?): Pair? { + if (widthPx != null && heightPx != null && widthPx >= 0f && heightPx >= 0f) return bounded(widthPx, heightPx) + if (w == null || h == null || w <= 0 || h <= 0) return null + val across = widthPx ?: heightPx?.let { qRound(it * (w.toDouble() / h)) } ?: w.toFloat() + val down = heightPx ?: widthPx?.let { qRound(it * (h.toDouble() / w)) } ?: h.toFloat() + if (across == 0f || down == 0f) return bounded(across.coerceAtLeast(0f), down.coerceAtLeast(0f)) + return bounded(if (across < 0f) w.toFloat() else across, if (down < 0f) h.toFloat() else down) + } + + /** + * The bytes of the image a `data:` URL in base64 at [path] holds, as Qt's `qDecodeDataUrl()` reads them, its + * percent-escapes decoded first and then past what is not base64; null for any other path, as for one that is + * not base64, which the app does not read. + */ + fun data(): ByteArray? { + if (!path.startsWith("data:", ignoreCase = true)) return null + val url = PERCENT_ESCAPE.replace(path.substring(5)) { it.groupValues[1].toInt(16).toChar().toString() } + val comma = url.indexOf(',') + if (comma < 0 || !url.substring(0, comma).trim().endsWith(";base64", ignoreCase = true)) return null + return try { + Base64.getMimeDecoder().decode(url.substring(comma + 1)) + } catch (_: IllegalArgumentException) { + null + } + } + + private fun bounded(across: Float, down: Float) = across.coerceAtMost(LabelStyle.MAX_LENGTH) to down.coerceAtMost(LabelStyle.MAX_LENGTH) + } /** A span's formatting; colours are 0xAARRGGBB, null for what is inherited from the label. */ data class Style( @@ -25,10 +77,24 @@ data class RichText(val blocks: List) { val family: String? = null, /** The link's target, for a span in an `a` with an `href`. */ val href: String? = null, - ) + /** + * How far its baseline is raised, in heights of its font as drawn: in a `sup` a half up and in a `sub` a sixth + * down, Qt's `QTextCharFormat::superScriptBaseline()` and `subScriptBaseline()` (50 and 16.67 percent). + */ + val raise: Float = 0f, + /** The image this span is, for an `img`; never an open element's, and part of [equals] so no text joins it. */ + val image: Image? = null, + ) { + /** The size it is drawn at, in Mudlet's pixels: Qt's `QTextEngine` draws sub- and superscripts at 2/3 of theirs. */ + val drawnSizePx: Float get() = if (raise == 0f) sizePx else sizePx * 2 / 3 + } + + /** The text without its formatting, a line for each block but one of only images, as a screen reader is given it. */ + val plain: String + get() = blocks.map { block -> block.spans.joinToString("") { it.readText } }.filter { it.isNotEmpty() }.joinToString("\n") - /** The text without its formatting, a line for each block, as a screen reader is given it. */ - val plain: String get() = blocks.joinToString("\n") { block -> block.spans.joinToString("") { it.text } } + /** Its images, in order; their text as drawn, Qt's object replacement character, [plain] leaves out. */ + val images: List get() = blocks.flatMap { block -> block.spans.mapNotNull { it.style.image } } /** The links, in order, each its text and its target. */ val links: List> @@ -43,7 +109,7 @@ data class RichText(val blocks: List) { text.clear() href = span.style.href } - text.append(span.text) + text.append(span.readText) } } return out @@ -53,8 +119,9 @@ data class RichText(val blocks: List) { /** [html] with text [baseSizePx] Mudlet pixels high where it sets no size. */ fun parse(html: String, baseSizePx: Float): RichText = Parser(html, baseSizePx).parse() - // HTML's font sizes 1 to 7 against the base size, 3 - private val FONT_SIZE_SCALE = floatArrayOf(0.6f, 0.8f, 1f, 1.2f, 1.5f, 2f, 3f) + // HTML's font sizes 1 to 7 against the base size, 3, by Qt's factors (QTextFormatPrivate::resolveFont()); Qt's + // headings h1 to h5 are 6, 5, 4, 3 and 2 of them + private val FONT_SIZE_SCALE = floatArrayOf(0.7f, 0.8f, 1f, 1.2f, 1.5f, 2f, 2.4f) private val BLOCK_TAGS = setOf( "p", "div", "center", "h1", "h2", "h3", "h4", "h5", "h6", "li", "ul", "ol", "tr", "table", "blockquote", "pre", "hr", "dl", "dt", "dd", "body", "html", @@ -62,15 +129,27 @@ data class RichText(val blocks: List) { private val VOID_TAGS = setOf("br", "hr", "img", "meta", "link", "input", "col", "area", "base", "wbr") private val HIDDEN_TAGS = setOf("head", "style", "script", "title") + // Between a row's cells, which Qt draws in a grid, so they read apart + private const val CELL_GAP = " " + + // What Qt's text holds for an image + private const val OBJECT = "\uFFFC" + // A value in double quotes, in single quotes or unquoted, as Qt's QTextHtmlParser::parseWord() reads it private val ATTRIBUTE = Regex("([^\\s=/]+)(?:\\s*(=)\\s*(\"[^\"]*\"|'[^']*'|[^\\s<>=]+)?)?") private val WHITESPACE = Regex("[ \\t\\r\\n\\u000C]+") + // As QByteArray::fromPercentEncoding() decodes one: not URLDecoder's, which reads a +, base64's, as a space + private val PERCENT_ESCAPE = Regex("%([0-9A-Fa-f]{2})") + // Qt reads € to Ÿ as Windows-1252's characters private const val WINDOWS_1252 = "\u20AC\u0081\u201A\u0192\u201E\u2026\u2020\u2021\u02C6\u2030\u0160\u2039\u0152\u008D\u017D\u008F" + "\u0090\u2018\u2019\u201C\u201D\u2022\u2013\u2014\u02DC\u2122\u0161\u203A\u0153\u009D\u017E\u0178" + // Qt's qRound(): half away from 0, and a size past an Int's one past what a label can draw + private fun qRound(v: Double) = (if (v >= 0) v + 0.5 else v - 0.5).toInt().toFloat() + // The longest name between & and ; that Qt reads private const val MAX_ENTITY = 8 @@ -130,18 +209,35 @@ data class RichText(val blocks: List) { } } - // One element open around the text: its style for the text in it, and its alignment if it is a block - private class Open(val tag: String, val style: Style, val align: HAlign?) + // One element open around the text: its style for the text in it, its alignment if it is a block, whether it or + // one around it is a heading or a `font size`, whose size Qt gives all in it, whether it is in a table's cell, and + // whether it is a block that ends the line, as a block in a cell does not + private class Open(val tag: String, val style: Style, val align: HAlign?, val sized: Boolean, val inCell: Boolean, val breaks: Boolean) - private class Parser(private val html: String, baseSizePx: Float) { + private class Parser(private val html: String, private val baseSizePx: Float) { private val blocks = ArrayList() private val spans = ArrayList() - private val open = ArrayList().apply { add(Open("", Style(sizePx = baseSizePx), null)) } + private val open = ArrayList().apply { add(Open("", Style(sizePx = baseSizePx), null, false, false, false)) } private var hidden = 0 + // The `pre` elements open, so a text's spaces are kept without a walk of [open] + private var pres = 0 + + // Where in [open] each table, row and cell open is, innermost last + private val tables = ArrayList() + // At a block's start, and after a space or a line break, a space is dropped private var atSpace = true + // The cells begun in the table row being written + private var cells = 0 + + // Right after a `pre`'s start, where Qt drops a line break + private var preStart = false + + // After a paragraph's start or end in a table's cell, which keeps the row a line: a space before the next text + private var spaceBefore = false + fun parse(): RichText { var i = 0 while (i < html.length) { @@ -227,12 +323,32 @@ data class RichText(val blocks: List) { atSpace = true return } + val cell = name == "td" || name == "th" + val grid = name == "tr" || name == "table" + // A cell ends the cell open in its row, and a row the row open in its table, as Qt's + // QTextHtmlParser::resolveParent() gives them a row and a table for their parent: looked for among the + // tables, rows and cells open only, so many cells after many tags left open stay cheap + if (cell || name == "tr") { + val at = tables.lastOrNull { cell || (open[it].tag != "td" && open[it].tag != "th") } + val tag = at?.let { open[it].tag } + if (at != null && if (cell) tag == "td" || tag == "th" else tag == "tr") closeFrom(at) + } val block = name in BLOCK_TAGS - if (block) flush() + // A paragraph in a cell, as Qt's toHtml() writes every cell's, keeps its row a line, but a table in it + val breaks = block && (!current().inCell || grid) + if (breaks) flush() + if (block && !breaks) spaceBefore = true + if (grid) cells = 0 + if (cell) { + if (cells++ > 0 && hidden == 0) append(CELL_GAP, current().style) + atSpace = true + } + if (name == "img") return image(attributes) if (name in VOID_TAGS || selfClosing) return val parent = current() var style = parent.style var align = parent.align + var adjusted: Float? = null when (name) { "b", "strong" -> style = style.copy(bold = true) @@ -242,6 +358,22 @@ data class RichText(val blocks: List) { "s", "strike", "del" -> style = style.copy(strike = true) + // A heading is sized against the label's own font, as below + "th", "h1", "h2", "h3", "h4", "h5" -> { + style = style.copy(bold = true) + if (name != "th") adjusted = baseSizePx * FONT_SIZE_SCALE['6' - name[1]] + } + + "sup" -> style = style.copy(raise = 0.5f) + + "sub" -> style = style.copy(raise = -1f / 6) + + "pre" -> { + style = style.copy(family = "monospace") + preStart = true + pres++ + } + // An empty href is no link, as in Qt "a" -> attributes["href"]?.takeIf { it.isNotEmpty() }?.let { style = style.copy(href = it) } @@ -250,7 +382,7 @@ data class RichText(val blocks: List) { "font" -> { attributes["color"]?.let { style = style.copy(color = CssColor.named(it)) } attributes["face"]?.let { style = style.copy(family = it.split(',').first().trim()) } - attributes["size"]?.let { fontSize(it, parent.style.sizePx) }?.let { style = style.copy(sizePx = it) } + adjusted = attributes["size"]?.let(::fontSize) } } if (block) attributes["align"]?.let(::hAlign)?.let { align = it } @@ -260,7 +392,12 @@ data class RichText(val blocks: List) { if (block && property == "text-align") hAlign(value)?.let { align = it } } } - open.add(Open(name, style, align)) + // Qt sizes a heading and a `font size` against the label's own font, and all in them, over a size a style + // gives: their FontSizeAdjustment, h1 to h5's 3, 2, 1, 0 and -1, QTextFormatPrivate::resolveFont() applies + // to the document's font, and over any size a format has; h6 Qt leaves as text + (adjusted ?: parent.style.sizePx.takeIf { parent.sized })?.let { style = style.copy(sizePx = it) } + if (cell || grid) tables.add(open.size) + open.add(Open(name, style, align, adjusted != null || parent.sized, cell || (parent.inCell && !grid), breaks)) } private fun close(name: String) { @@ -271,20 +408,72 @@ data class RichText(val blocks: List) { // Closes the innermost element of that name, and any left open inside it val at = open.indexOfLast { it.tag == name } if (at <= 0) return - if (name in BLOCK_TAGS || open.subList(at, open.size).any { it.tag in BLOCK_TAGS }) flush() - while (open.size > at) open.removeAt(open.size - 1) + // Qt drops a line break right before a `pre`'s end + if (name == "pre" && spans.lastOrNull()?.text?.endsWith('\n') == true) { + val last = spans.removeAt(spans.size - 1) + if (last.text.length > 1) spans.add(last.copy(text = last.text.dropLast(1))) + } + // After a cell, as at its start, Qt drops the spaces before the next + if (name == "td" || name == "th") atSpace = true + closeFrom(at) + } + + // Closes the element open at [at] and any left open inside it + private fun closeFrom(at: Int) { + val closed = open.subList(at, open.size) + when { + closed.any { it.breaks } -> flush() + closed.any { it.tag in BLOCK_TAGS } -> spaceBefore = true + } + while (open.size > at) if (open.removeAt(open.size - 1).tag == "pre") pres-- + while (tables.lastOrNull()?.let { it >= at } == true) tables.removeAt(tables.size - 1) } private fun current() = open.last() + // An `img` with a `src`, as its own span; Qt takes `qrc:` for its resources and a `file:` URL for its file + private fun image(attributes: Map) { + val src = attributes["src"]?.takeIf { it.isNotEmpty() } ?: return + if (hidden > 0) return + val path = when { + src.startsWith("qrc:") -> ":" + src.substring(4) + src.startsWith("file://") -> src.substring(7) + src.startsWith("file:/") -> src.substring(5) + else -> src + } + + // A number alone, as Qt's QString::toDouble() reads one, else -2, as Qt keeps it: "8px", "50%" and "80f" are + // none to Qt; and -1 is Qt's own for a side not given, as QTextHtmlImporter passes over it + fun length(key: String): Float? { + val v = attributes[key]?.trim() ?: return null + val n = v.takeIf { t -> t.all { it.isDigit() || it in "+-.eE" } }?.toDoubleOrNull() ?: return -2f + return if (n == -1.0) null else qRound(n) + } + val alt = attributes["alt"] ?: attributes["title"] ?: "" + separate() + spans.add(Span(OBJECT, current().style.copy(image = Image(path, length("width"), length("height"), alt)))) + atSpace = false + preStart = false + } + private fun text(raw: String, atEnd: Boolean = false) { if (raw.isEmpty() || hidden > 0) return - var text = WHITESPACE.replace(raw, " ") - if (atSpace) text = text.trimStart(' ') + val pre = pres > 0 + // In a `pre`, spaces and line breaks as written, but the line break right after its start + var text = if (pre) raw.replace("\r\n", "\n").let { if (preStart) it.removePrefix("\n") else it } else WHITESPACE.replace(raw, " ") + preStart = false + if ((atSpace || spaceBefore) && !pre) text = text.trimStart(' ') if (text.isEmpty()) return text = decodeEntities(text, atEnd) + separate() append(text, current().style) - atSpace = text.endsWith(' ') + atSpace = !pre && text.endsWith(' ') + } + + // The space a paragraph's start or end in a cell leaves before what follows it in the row + private fun separate() { + if (spaceBefore && spans.lastOrNull()?.text?.lastOrNull()?.isWhitespace() == false) append(" ", current().style) + spaceBefore = false } private fun append(text: String, style: Style) { @@ -292,9 +481,9 @@ data class RichText(val blocks: List) { if (last != null && last.style == style) spans[spans.size - 1] = Span(last.text + text, style) else spans.add(Span(text, style)) } - // Ends the block being written, without its trailing spaces + // Ends the block being written, without its trailing spaces but in a `pre` private fun flush() { - while (spans.isNotEmpty()) { + while (spans.isNotEmpty() && pres == 0) { val last = spans.last() val trimmed = last.text.trimEnd(' ') if (trimmed.isNotEmpty()) { @@ -306,6 +495,7 @@ data class RichText(val blocks: List) { if (spans.isNotEmpty()) blocks.add(Block(current().align, spans.toList())) spans.clear() atSpace = true + spaceBefore = false } private fun hAlign(value: String): HAlign? = when (value.trim().lowercase()) { @@ -316,12 +506,12 @@ data class RichText(val blocks: List) { else -> null } - // HTML's size: 1 to 7, or relative as +1 or -2 from 3 - private fun fontSize(value: String, base: Float): Float? { - val v = value.trim() - val n = v.toIntOrNull() ?: return LabelStyle.length(v) - val logical = if (v.startsWith("+") || v.startsWith("-")) 3 + n else n - return base * FONT_SIZE_SCALE[logical.coerceIn(1, 7) - 1] + // HTML's size, 1 to 7, or from 3 as +1 or -2, as Qt's QTextHtmlParser reads it: a whole number or else 0, so + // "12px" is 1's size, and from 3 only for a + or - written first + private fun fontSize(value: String): Float { + val n = value.trim().toIntOrNull() ?: 0 + val logical = if (value.startsWith("+") || value.startsWith("-")) 3 + n else n + return baseSizePx * FONT_SIZE_SCALE[logical.coerceIn(1, 7) - 1] } private fun styled(style: Style, property: String, value: String): Style = when (property) { diff --git a/protocol/src/test/kotlin/org/mudlet/android/protocol/RichTextTest.kt b/protocol/src/test/kotlin/org/mudlet/android/protocol/RichTextTest.kt index 8545b9c1c..f60dc4089 100644 --- a/protocol/src/test/kotlin/org/mudlet/android/protocol/RichTextTest.kt +++ b/protocol/src/test/kotlin/org/mudlet/android/protocol/RichTextTest.kt @@ -1,5 +1,6 @@ package org.mudlet.android.protocol +import org.junit.Assert.assertArrayEquals import org.junit.Assert.assertEquals import org.junit.Assert.assertTrue import org.junit.Test @@ -42,7 +43,7 @@ class RichTextTest { RichText.Span("s", base.copy(strike = true)), RichText.Span("big", base.copy(background = 0xFFFF0000.toInt(), bold = true, sizePx = 6f)), RichText.Span("green", base.copy(color = 0xFF00FF00.toInt(), sizePx = 18f)), - RichText.Span("small", base.copy(sizePx = 12f * 0.6f)), + RichText.Span("small", base.copy(sizePx = 12f * 0.7f)), RichText.Span("x", base.copy(strike = true, italic = true)), ), text.blocks.single().spans, @@ -62,6 +63,10 @@ class RichTextTest { assertEquals(none, listOf(RichText.Span("a", base)), parse("a").blocks.single().spans) } assertEquals(listOf(RichText.Span("a", base.copy(sizePx = 13f))), parse("a").blocks.single().spans) + // A `font`'s `size` Qt reads as a whole number, else 0, the smallest size, 0.7 of the label's, from 3 only with a + // + or - first, and over a size set in it: as Qt 6.4.2 sizes them in a label + fun font(size: String) = parse("ab").blocks.single().spans.single().style.sizePx + assertEquals(listOf(0.7f, 0.7f, 0.7f, 1.5f, 1f, 2.4f).map { 12f * it }, listOf("0px", "12px", " +1", " 5", "-0px", "7").map(::font)) } @Test @@ -167,8 +172,160 @@ class RichTextTest { @Test fun otherTagsKeepTheirTextAndUnclosedOnesEndWithTheText() { val text = parse("
celltwo
onbold") - assertEquals("celltwo\nonbold", text.plain) + assertEquals("cell two\nonbold", text.plain) assertTrue(text.blocks.last().spans.last().style.bold) assertEquals(RichText(emptyList()), parse("

")) } + + @Test + fun headingsAsQtSizesThem() { + // As Qt 6.4.2's QTextDocument sizes them against a 10 pt default font: 20, 15, 12, 10 and 8 pt, bold, and h6 as text + val text = parse("body

a

b

c

d

e
f
") + assertEquals( + listOf(base, base.copy(bold = true, sizePx = 24f), base.copy(bold = true, sizePx = 18f), base.copy(bold = true, sizePx = 12f * 1.2f), base.copy(bold = true), base.copy(bold = true, sizePx = 12f * 0.8f), base), + text.blocks.map { it.spans.single().style }, + ) + assertEquals("body\na\nb\nc\nd\ne\nf", text.plain) + // Against the label's own size, over a size around it or in the heading's style, h4 too (Qt 6.4.2, 12 px font) + val sized = parse("

x

y

z

") + assertEquals(listOf(24f, 12f, 18f), sized.blocks.map { it.spans.single().style.sizePx }) + assertTrue(sized.blocks.all { it.spans.single().style.bold }) + // And over a size a style sets in it, but a `font size`'s, which Qt also takes against the label's own size + val inside = parse( + "

abcd

" + + "

gh

" + + "

ef

", + ) + assertEquals(listOf("ab" to 24f, "cd" to 18f, "gh" to 24f, "ef" to 12f * 1.2f), inside.blocks.flatMap { b -> b.spans.map { it.text to it.style.sizePx } }) + } + + @Test + fun subscriptsAndSuperscriptsAreSmallerAndShifted() { + val spans = parse("E = mc2 H2O q").blocks.single().spans + assertEquals(listOf("E = mc", "2", " H", "2", "O ", "q"), spans.map { it.text }) + assertEquals(listOf(0f, 0.5f, 0f, -1f / 6, 0f, -1f / 6), spans.map { it.style.raise }) + // At 2/3 of the size around them, once, as Qt draws them + assertEquals(listOf(12f, 8f, 12f, 8f, 12f, 8f), spans.map { it.style.drawnSizePx }) + assertEquals(12f, spans[1].style.sizePx) + } + + @Test + fun imagesAreSpansOfTheirOwn() { + val text = parse( + "ab" + + "\"none\"", + ) + val images = text.images + assertEquals( + listOf( + RichText.Image("/data/key.png", 20f, 10f), + RichText.Image(":/icons/x.png", null, null), + RichText.Image(":/icons/x.png", null, null), + RichText.Image("/sdcard/i.png", 30f, null), + RichText.Image("/sdcard/i.png", null, null), + RichText.Image("eye.png", null, 8f), + ), + images, + ) + assertEquals(listOf("\uFFFC"), text.blocks.single().spans.filter { it.style.image != null }.map { it.text }.distinct()) + // Read as nothing, as Mudlet reads a label, but a link of one is still a link + assertEquals("ab", text.plain) + assertEquals("x\ny", parse("

x

y").plain) + assertEquals(emptyList(), parse("\"nox").images) + assertEquals(listOf("" to "send:look"), text.links) + assertEquals("send:look", text.blocks.single().spans.last().style.href) + // Sized as Qt sizes them, of an image 8 by 4 pixels + assertEquals(20f to 10f, images[0].size(8, 4)) + assertEquals(20f to 10f, images[0].size(null, null)) + assertEquals(8f to 4f, images[1].size(8, 4)) + assertEquals(null, images[1].size(null, null)) + assertEquals(30f to 15f, images[3].size(8, 4)) + assertEquals(null, images[3].size(null, null)) + assertEquals(16f to 8f, images[5].size(8, 4)) + assertEquals(" b", parse(" b").plain) + // Qt reads a number alone; along a side it reads none or a negative one off it draws the file's own size, as + // QPainter::drawPixmap() draws a negative width, and a 0 alone hides it: as a QLabel of Qt 6.4.2 draws a 40 by + // 20 file + val odd = parse( + "" + + "", + ).images + assertEquals(listOf(-2f, -5f, 10f, 0f, 0f, 10f, -2f), odd.map { it.widthPx }) + assertEquals(listOf(null, 10f, -2f, null, 10f, 2f, null), odd.map { it.heightPx }) + assertEquals(listOf(40f to 20f, 40f to 10f, 10f to 20f, 0f to 0f, 0f to 10f, 10f to 2f, 40f to 20f), odd.map { it.size(40, 20) }) + // Before the file comes, room only for both sides given and drawn + assertEquals(listOf(null, null, null, null, 0f to 10f, 10f to 2f, null), odd.map { it.size(null, null) }) + // Qt rounds a side, a side it works out from the other that rounds to 0 hides the image but keeps its room along + // the side given, and -1 is Qt's own for a side not given: as a QLabel of Qt 6.4.2 draws a 200 by 20 file + val wide = parse( + "" + + "" + + "", + ).images + assertEquals( + listOf(0f to 0f, 4f to 0f, 5f to 1f, 200f to 20f, 0f to 0f, 100f to 10f, 200f to 20f, 1f to 0f, 11f to 5f, 0f to 0f, 25f to 20f), + wide.map { it.size(200, 20) }, + ) + assertEquals(1f to 1f, wide[7].size(40, 20)) + assertEquals(50f to 0f, parse("").images.single().size(200, 1)) + assertEquals(11f to 6f, parse("").images.single().size(40, 20)) + // A side at most a sheet's longest length, whatever the script or the file gives, so the app can lay it out + assertEquals(4096f to 4096f, parse("").images.single().size(null, null)) + assertEquals(4000f to 4096f, parse("").images.single().size(1, 4096)) + assertEquals(4096f to 4096f, parse("").images.single().size(100000, 5000)) + // Its `alt`, else its `title`, for a screen reader + assertEquals(listOf("Look", "eye", ""), parse("Look").images.map { it.alt }) + } + + @Test + fun aDataUrlsImageIsReadFromItsBase64() { + // As Qt's qDecodeDataUrl() reads one: its percent-escapes first, its base64 past what is not base64, and `data:` + // and `;base64` in any case + val png = byteArrayOf(0x89.toByte(), 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A) + val images = parse( + "" + + "" + + "", + ).images + assertArrayEquals(png, images[0].data()) + assertArrayEquals(png, images[1].data()) + assertArrayEquals(png, images[2].data()) + assertArrayEquals(byteArrayOf(0xFB.toByte(), 0xFF.toByte()), images[3].data()) + // Nor bad base64, which does not throw + assertEquals(listOf(null, null, null, null), images.drop(4).map { it.data() }) + } + + @Test + fun aTablesRowsAreLinesWithTheirCellsApart() { + val text = parse("
Name HP
bc
after") + assertEquals("Name HP\n b c\nafter", text.plain) + assertEquals(listOf(true, false), text.blocks.first().spans.map { it.style.bold }) + // A cell or row left open ends at the next cell, row or the table's end, so a `th`'s bold goes no further, as + // in Qt + val unclosed = parse("
ab
cd
e") + assertEquals("a b\nc d\ne", unclosed.plain) + assertEquals(listOf("a" to true, " b" to false, "c" to true, " d" to false, "e" to false), unclosed.blocks.flatMap { b -> b.spans.map { it.text to it.style.bold } }) + // A row ends a cell left open in the row before, so its formatting stays in that row + assertEquals(listOf("a" to true, "b" to false), parse("
a
b
").blocks.flatMap { b -> b.spans.map { it.text to it.style.bold } }) + // A paragraph in a cell, as Qt's toHtml() writes every cell's, keeps its row a line, a space from its cell's text + val paragraphs = parse("

a

b

c
d
") + assertEquals("a b c\nd", paragraphs.plain) + // A table in a cell keeps the cell's `th` bold in it and after it, as Qt's QTextHtmlParser + val nested = parse("
a
b
c
") + assertEquals(listOf("a" to true, "b" to true, "c" to true), nested.blocks.flatMap { b -> b.spans.map { it.text to it.style.bold } }) + // The spaces between cells are dropped, and a table without rows starts its own + assertEquals("a b\nc", parse("\n \n \n \n \n
ab
c
").plain) + } + + @Test + fun preformattedTextKeepsItsSpacesInMonospace() { + val text = parse("
\n  a   b\r\n\tc & d  \n
after it") + assertEquals(" a b\n\tc & d \nafter it", text.plain) + val mono = base.copy(family = "monospace") + assertEquals( + listOf(RichText.Span(" a b\n\tc & ", mono), RichText.Span("d", mono.copy(bold = true)), RichText.Span(" ", mono)), + text.blocks.first().spans, + ) + assertEquals(listOf(RichText.Span("after it", base)), text.blocks.last().spans) + } }