From c713815694654e3000ab9c0fd711554dc6f3ecea Mon Sep 17 00:00:00 2001 From: James Babcock Date: Sat, 7 Nov 2026 20:57:55 -0800 Subject: [PATCH] EvenHub: Fix an issue where indentation was incorrectly dropped in text containers This caused text to overlap with icons in Iris Pro and Ocuclaw --- app/apps/evenhub/compositor.ts | 18 +- app/apps/evenhub/containers.ts | 22 +++ app/apps/evenhub/layout-debug.ts | 271 ++++++++++++++++++++++++++++ app/apps/evenhub/session.ts | 63 ++++++- app/graphics/evenhub-font.ts | 67 +------ app/graphics/lvgl-wrap.ts | 86 +++++++++ tests/evenhub-layout-debug.test.cjs | 122 +++++++++++++ tests/lvgl-wrap.test.cjs | 45 +++++ tests/tsconfig.json | 2 + 9 files changed, 610 insertions(+), 86 deletions(-) create mode 100644 app/apps/evenhub/layout-debug.ts create mode 100644 app/graphics/lvgl-wrap.ts create mode 100644 tests/evenhub-layout-debug.test.cjs create mode 100644 tests/lvgl-wrap.test.cjs diff --git a/app/apps/evenhub/compositor.ts b/app/apps/evenhub/compositor.ts index 8518facf..0d86f8e0 100644 --- a/app/apps/evenhub/compositor.ts +++ b/app/apps/evenhub/compositor.ts @@ -16,7 +16,7 @@ import { GrayImage } from "../../graphics/image"; import { containerFace } from "./fonts"; import { - type EvenHubContainer, + paintOrder, type EvenHubImageContainer, type EvenHubListContainer, type EvenHubPage, @@ -95,22 +95,6 @@ function paintListContainer(image: GrayImage, container: EvenHubListContainer, f paintListItems(image, container, containerFace(container.font), focused); } -/** Bottom-to-top paint order for a page's containers. */ -function paintOrder(containers: EvenHubContainer[]): EvenHubContainer[] { - const anyZ = containers.some((c) => c.zOrderIndex !== undefined); - if (anyZ) { - // Explicit z-order: larger zOrderIndex renders closer to the front, i.e. - // painted later. (Stock requires all-or-nothing + unique values.) - return [...containers].sort((a, b) => (a.zOrderIndex ?? 0) - (b.zOrderIndex ?? 0)); - } - // Default (no z-order): image containers sit below lists and text. Cross-type - // order isn't defined by the wire format (separate object arrays), so this is - // our choice, matching stock. Order within each group is preserved. - const images = containers.filter((c) => c.kind === "image"); - const rest = containers.filter((c) => c.kind !== "image"); - return [...images, ...rest]; -} - /** Render a page into a fresh app-viewport image (positioned by the shell). */ export function compositePage(page: EvenHubPage | null, size: { width: number; height: number }, focused: boolean): GrayImage { const image = new GrayImage(size.width, size.height, 0); diff --git a/app/apps/evenhub/containers.ts b/app/apps/evenhub/containers.ts index 6cb7f629..4096391a 100644 --- a/app/apps/evenhub/containers.ts +++ b/app/apps/evenhub/containers.ts @@ -303,6 +303,28 @@ export function parsePage(data: Record): EvenHubPage { return { containers, menuItems: parseMenuItems(data) }; } +/** + * Bottom-to-top paint order for a page's containers. + * + * With zOrderIndex, larger values render closer to the front (painted later). + * Stock honors it only when every container has one (the SDK enforces that + * app-side); we sort whenever any container has one, treating a missing index + * as 0. + * + * Without it, image containers sit below lists and text, keeping declaration + * order within each group. That is our choice, and it may not match stock: + * the evenRealities-openCFW firmware reconstruction builds the container list + * as lists, then texts, then images, which would put images on top. Not yet + * checked on hardware; layout-debug.ts logs where the two orders differ. + */ +export function paintOrder(containers: readonly EvenHubContainer[]): EvenHubContainer[] { + const anyZ = containers.some((c) => c.zOrderIndex !== undefined); + if (anyZ) return [...containers].sort((a, b) => (a.zOrderIndex ?? 0) - (b.zOrderIndex ?? 0)); + const images = containers.filter((c) => c.kind === "image"); + const rest = containers.filter((c) => c.kind !== "image"); + return [...images, ...rest]; +} + /** The container that owns gesture events (exactly one per valid page). */ export function eventCaptureContainer(page: EvenHubPage): EvenHubContainer | undefined { return page.containers.find( diff --git a/app/apps/evenhub/layout-debug.ts b/app/apps/evenhub/layout-debug.ts new file mode 100644 index 00000000..5c228780 --- /dev/null +++ b/app/apps/evenhub/layout-debug.ts @@ -0,0 +1,271 @@ +/** + * Debug-log descriptions of EvenHub page layouts and container updates. + * session.ts sends them to logcat at debug level (tag FaceclawEvenHubLayout). + * + * Lines starting "stock:" flag places where our emulation may draw something + * stock would not, or the reverse. The stock rules come from the + * evenRealities-openCFW reconstruction of firmware 2.2.6.10 + * (evenhub_ui_containers.c, evenhub_ui_page.c, common_image_container.c) and + * the community simulator's hardware notes. Nobody has checked them against + * captures of our own, so they are leads, not established facts. They also + * describe the glasses firmware only: the Even phone app sits between the SDK + * and the firmware and may validate or convert payloads first. + */ +import { + paintOrder, + readOptionalNumber, + type EvenHubContainer, + type EvenHubImageContainer, + type EvenHubListContainer, + type EvenHubPage, + type EvenHubTextContainer, +} from "./containers"; + +/** Stock image-container size limits; outside them the container, and so the whole page, fails to create. */ +const STOCK_IMAGE_MIN_SIZE = 20; +const STOCK_IMAGE_MAX_WIDTH = 288; +const STOCK_IMAGE_MAX_HEIGHT = 144; + +const CONTENT_PREVIEW_CHARS = 40; +const RAW_STRING_CHARS = 120; +const RAW_ARRAY_ITEMS = 32; + +/** + * Stock's bottom-to-top container order: lists, then texts, then images (each + * in declaration order). The firmware sorts by zOrderIndex (stably) only when + * every container has one and there are at least two. + */ +export function stockPaintOrder(containers: readonly EvenHubContainer[]): EvenHubContainer[] { + const legacy = [ + ...containers.filter((c) => c.kind === "list"), + ...containers.filter((c) => c.kind === "text"), + ...containers.filter((c) => c.kind === "image"), + ]; + const allHaveZ = legacy.every((c) => c.zOrderIndex !== undefined); + if (!allHaveZ || legacy.length < 2) return legacy; + return legacy.sort((a, b) => (a.zOrderIndex ?? 0) - (b.zOrderIndex ?? 0)); +} + +function label(c: EvenHubContainer): string { + return `${c.kind}#${c.id} ${JSON.stringify(c.name)}`; +} + +function preview(text: string): string { + return JSON.stringify(text.length > CONTENT_PREVIEW_CHARS ? `${text.slice(0, CONTENT_PREVIEW_CHARS)}…` : text); +} + +function decoration(c: EvenHubTextContainer | EvenHubListContainer): string { + let out = ""; + if (c.borderWidth > 0) out += ` border=${c.borderWidth}${c.borderRadius > 0 ? `r${c.borderRadius}` : ""}`; + if (c.paddingLength > 0) out += ` pad=${c.paddingLength}`; + if (c.isEventCapture) out += " capture"; + if (c.font) out += " font"; + return out; +} + +function describeContainer(c: EvenHubContainer): string { + const z = c.zOrderIndex === undefined ? "" : ` z=${c.zOrderIndex}`; + const head = `${label(c)} @${c.x},${c.y} ${c.width}x${c.height}${z}`; + switch (c.kind) { + case "image": + return `${head} ${c.pixels ? `pixels=${c.pixelsWidth}x${c.pixelsHeight}` : "no pixels yet"}`; + case "text": { + const color = c.textColor === undefined ? "" : ` color=${c.textColor}`; + return `${head}${decoration(c)}${color} len=${c.content.length} ${preview(c.content)}`; + } + case "list": + return `${head}${decoration(c)} items=${c.itemNames.length} itemWidth=${c.itemWidth}${c.selectBorder ? " selectBorder" : ""} selected=${c.selectedIndex}`; + } +} + +function overlaps(a: EvenHubContainer, b: EvenHubContainer): boolean { + if (a.width <= 0 || a.height <= 0 || b.width <= 0 || b.height <= 0) return false; + return a.x < b.x + b.width && b.x < a.x + a.width && a.y < b.y + b.height && b.y < a.y + a.height; +} + +/** Ways a page would lay out or behave differently on stock firmware. */ +export function stockDifferences(page: EvenHubPage, declaredTotal?: number): string[] { + const notes: string[] = []; + const containers = page.containers; + const ours = paintOrder(containers); + const stock = stockPaintOrder(containers); + + const withZ = containers.filter((c) => c.zOrderIndex !== undefined).length; + if (withZ > 0 && withZ < containers.length) { + notes.push(`zOrderIndex is set on ${withZ} of ${containers.length} containers: we sort by it (missing = 0), stock ignores it`); + } + if (ours.some((c, i) => c !== stock[i])) { + notes.push(`paint order differs; stock bottom to top is ${stock.map(label).join(", ")}`); + for (let i = 0; i < ours.length; i++) { + for (let j = i + 1; j < ours.length; j++) { + const below = ours[i]!; + const above = ours[j]!; + if (overlaps(below, above) && stock.indexOf(below) > stock.indexOf(above)) { + notes.push(`${label(below)} overlaps ${label(above)}: we draw ${label(above)} on top, stock draws ${label(below)} on top`); + } + } + } + } + + for (const c of containers) { + if (c.kind !== "image") continue; + if (c.width < STOCK_IMAGE_MIN_SIZE || c.height < STOCK_IMAGE_MIN_SIZE || c.width > STOCK_IMAGE_MAX_WIDTH || c.height > STOCK_IMAGE_MAX_HEIGHT) { + notes.push( + `${label(c)} is ${c.width}x${c.height}; stock fails the whole page for an image container outside ` + + `${STOCK_IMAGE_MIN_SIZE}-${STOCK_IMAGE_MAX_WIDTH} x ${STOCK_IMAGE_MIN_SIZE}-${STOCK_IMAGE_MAX_HEIGHT}`, + ); + } + } + + // The firmware finds a container by id with a first-match walk of its + // (stock-ordered) list, for image updates and events alike. + const byId = new Map(); + for (const c of stock) byId.set(c.id, [...(byId.get(c.id) ?? []), c]); + byId.forEach((shared, id) => { + if (shared.length > 1) { + notes.push(`id ${id} is shared by ${shared.map(label).join(", ")}; stock resolves it to ${label(shared[0]!)} only`); + } + }); + + const captures = stock.filter((c) => c.kind !== "image" && c.isEventCapture); + if (captures.length !== 1) { + notes.push( + captures.length === 0 + ? "no event-capture container (stock requires exactly one)" + : `${captures.length} event-capture containers; stock binds only the first, ${label(captures[0]!)}`, + ); + } + + if (declaredTotal !== undefined && declaredTotal !== containers.length) { + notes.push(`containerTotalNum is ${declaredTotal} but ${containers.length} containers were sent`); + } + return notes; +} + +/** The payload, as compact JSON with long strings and arrays cut short. */ +function rawJson(data: Record): string { + try { + return JSON.stringify(data, (_key, value: unknown) => { + if (typeof value === "string" && value.length > RAW_STRING_CHARS) { + return `${value.slice(0, RAW_STRING_CHARS)}…(${value.length} chars)`; + } + if (Array.isArray(value) && value.length > RAW_ARRAY_ITEMS) { + return [...value.slice(0, RAW_ARRAY_ITEMS), `…(${value.length} items)`]; + } + return value; + }); + } catch (error) { + return `(unserializable: ${String(error)})`; + } +} + +/** A create/rebuild/extension layout: one line per container in our paint order, then stock differences, then the raw payload. */ +export function describePage(method: string, page: EvenHubPage, data: Record): string[] { + const containers = page.containers; + const count = (kind: EvenHubContainer["kind"]) => containers.filter((c) => c.kind === kind).length; + const declaredTotal = readOptionalNumber(data, "containerTotalNum"); + const total = declaredTotal === undefined ? "" : `, containerTotalNum=${declaredTotal}`; + const menu = page.menuItems.length > 0 ? `, ${page.menuItems.length} menu items` : ""; + const lines = [ + `${method}: ${containers.length} containers (${count("list")} list, ${count("text")} text, ${count("image")} image)` + + `${total}${menu}; we paint bottom to top:`, + ]; + paintOrder(containers).forEach((c, i) => lines.push(` ${i}: ${describeContainer(c)}`)); + for (const note of stockDifferences(page, declaredTotal)) lines.push(` stock: ${note}`); + lines.push(` raw: ${rawJson(data)}`); + return lines; +} + +function payloadKind(raw: unknown): string { + if (raw === undefined || raw === null) return "missing"; + if (Array.isArray(raw)) return `number[${raw.length}]`; + if (typeof raw === "string") return `string(${raw.length} chars)`; + if (typeof raw === "object") return `object(${Object.keys(raw).length} keys)`; + return typeof raw; +} + +function imageFormat(bytes: Uint8Array, container: EvenHubImageContainer): string { + if (bytes.length > 4 && bytes[0] === 0x89 && bytes[1] === 0x50 && bytes[2] === 0x4e && bytes[3] === 0x47) return "PNG"; + // BITMAPINFOHEADER biBitCount: 14-byte file header + 14. + if (bytes.length > 30 && bytes[0] === 0x42 && bytes[1] === 0x4d) return `BMP ${bytes[28]! | (bytes[29]! << 8)}bpp`; + const area = container.width * container.height; + if (bytes.length === area) return "raw8"; + if (bytes.length === Math.ceil(area / 2)) return "raw4"; + const head = Array.from(bytes.subarray(0, 4), (b) => b.toString(16).padStart(2, "0")).join(" "); + return `unrecognized (${bytes.length} bytes, starts ${head})`; +} + +/** Lit-pixel count and bounding box, so a blank "hide this" image is recognizable. */ +function litSummary(decoded: { pixels: Uint8Array; width: number; height: number }): string { + let lit = 0; + let max = 0; + let x0 = decoded.width; + let y0 = decoded.height; + let x1 = -1; + let y1 = -1; + for (let i = 0; i < decoded.pixels.length; i++) { + const value = decoded.pixels[i]!; + if (value === 0) continue; + lit++; + if (value > max) max = value; + const x = i % decoded.width; + const y = Math.floor(i / decoded.width); + if (x < x0) x0 = x; + if (x > x1) x1 = x; + if (y < y0) y0 = y; + if (y > y1) y1 = y; + } + return lit === 0 ? "blank" : `lit=${lit}px max=${max} bbox=${x0},${y0}-${x1},${y1}`; +} + +export type ImageUpdateDebug = { + page: EvenHubPage | null; + sentId: number; + sentName: string; + container: EvenHubImageContainer | null; + raw: unknown; + bytes: Uint8Array | null; + decoded: { pixels: Uint8Array; width: number; height: number } | null; + /** BleEhSendImageResult: 0 success, 1 imageException, 2 sizeInvalid, 3 sendFailed. */ + result: number; +}; + +export function describeImageUpdate(u: ImageUpdateDebug): string[] { + const parts = [`updateImageRawData ${u.container ? label(u.container) : `id=${u.sentId} ${JSON.stringify(u.sentName)}`}`]; + if (u.container && u.sentName !== "" && u.sentName !== u.container.name) parts.push(`(sent name ${JSON.stringify(u.sentName)})`); + parts.push(`payload=${payloadKind(u.raw)}`); + if (u.bytes && u.container) parts.push(`format=${imageFormat(u.bytes, u.container)}`); + if (u.decoded) parts.push(`decoded=${u.decoded.width}x${u.decoded.height} ${litSummary(u.decoded)}`); + parts.push(`result=${u.result}`); + const lines = [parts.join(" ")]; + + if (!u.container) lines.push(` no image container with id ${u.sentId} on this page`); + else if (u.result !== 0) lines.push(" rejected; the container keeps its previous pixels"); + if (typeof u.raw === "string") { + lines.push(" stock: real hardware reportedly ignores a base64-string imageData (it needs bytes); we decode and draw it"); + } + if (u.container && u.decoded && (u.decoded.width !== u.container.width || u.decoded.height !== u.container.height)) { + lines.push( + ` stock: the firmware only accepts a bitmap exactly the container's size (${u.container.width}x${u.container.height}); ` + + "we tile or crop it to fit", + ); + } + const stockTarget = u.page ? stockPaintOrder(u.page.containers).find((c) => c.id === u.sentId) : undefined; + if (stockTarget && stockTarget.kind !== "image") { + lines.push(` stock: id ${u.sentId} resolves to ${label(stockTarget)} first, so stock drops this update`); + } + return lines; +} + +export function describeTextUpgrade( + sentId: number, + sentName: string, + container: EvenHubTextContainer | null, + offset: number | undefined, + sentLength: number, +): string { + if (!container) return `textContainerUpgrade id=${sentId} ${JSON.stringify(sentName)}: no matching text container`; + const at = offset === undefined ? "" : ` offset=${offset}`; + const color = container.textColor === undefined ? "" : ` color=${container.textColor}`; + return `textContainerUpgrade ${label(container)}${at} sent=${sentLength} chars -> len=${container.content.length}${color} ${preview(container.content)}`; +} diff --git a/app/apps/evenhub/session.ts b/app/apps/evenhub/session.ts index bdbdd92d..71191a6b 100644 --- a/app/apps/evenhub/session.ts +++ b/app/apps/evenhub/session.ts @@ -52,6 +52,7 @@ import { type EvenHubPage, } from "./containers"; import { compositePage } from "./compositor"; +import { describeImageUpdate, describePage, describeTextUpgrade } from "./layout-debug"; import { stepListSelection } from "./list-menu"; import { type EvenHubManifest } from "./ehpk"; import { permissionsIncludeMicrophone, permissionsInclude } from "./permissions"; @@ -76,6 +77,15 @@ import { parseFontSpec } from "./font-spec"; const UPNG = require("upng-js"); +/** + * Page layouts and container updates go to logcat at debug level (see + * layout-debug.ts): `adb logcat -s FaceclawEvenHubLayout:D`. Android only. + */ +const LAYOUT_LOG_TAG = "FaceclawEvenHubLayout"; +const LAYOUT_LOG_ENABLED = !!global.isAndroid; +/** Logcat truncates entries around 4 KB; split longer lines below that. */ +const LOGCAT_CHUNK_CHARS = 3000; + /** OsEventTypeList values (PB ordinals). */ const CLICK_EVENT = 0; const SCROLL_TOP_EVENT = 1; @@ -916,11 +926,13 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH private async createStartUpPage(data: Record): Promise { if (this.pageCreated) { // One-shot per session on stock: a second call blocks ~2s, then fails. + this.layoutLog(() => ["createStartUpPageContainer: repeated call, rejected as stock does"]); await delay(DUPLICATE_CREATE_DELAY_MS); return 1; } const page = parsePage(data); this.warnOnInvalidPage(page, data); + this.layoutLog(() => describePage("createStartUpPageContainer", page, data)); this.page = page; this.pageCreated = true; // The launch focus arrived before the page existed; deliver the ENTER now. @@ -936,6 +948,7 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH // Also serves as create for apps that fall back to rebuild-on-error. const page = parsePage(data); this.warnOnInvalidPage(page, data); + this.layoutLog(() => describePage("rebuildPageContainer", page, data)); this.page = page; this.pageCreated = true; this.windowHooks?.requestRender(); @@ -968,9 +981,12 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH const container = this.page?.containers.find( (c) => c.kind === "text" && c.id === id && (name === "" || c.name === name), ); - if (!container || container.kind !== "text") return false; const content = readString(data, "content", ""); const offset = readOptionalNumber(data, "contentOffset"); + if (!container || container.kind !== "text") { + this.layoutLog(() => [describeTextUpgrade(id, name, null, offset, content.length)]); + return false; + } // (contentLength is intentionally ignored — see below.) // Stock semantics are "write `content` at contentOffset and truncate the // rest" (a null-terminating strcpy(buf+offset, content)), NOT a splice that @@ -987,6 +1003,7 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH // brightness; omitting it keeps whatever level the container has. const brightness = readTextBrightness(data); if (brightness !== undefined) container.textColor = brightness; + this.layoutLog(() => [describeTextUpgrade(id, name, container, offset, content.length)]); this.windowHooks?.requestRender(); return true; } @@ -994,20 +1011,49 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH /** BleEhSendImageResult: 0 success, 1 imageException, 2 sizeInvalid, 3 sendFailed. */ private updateImage(data: Record): number { const id = readNumber(data, "containerID", -1); - const container = this.page?.containers.find((c) => c.kind === "image" && c.id === id); - if (!container || container.kind !== "image") return 3; - const bytes = imagePayloadBytes(data.imageData ?? data.rawData ?? asRecord(data).mapRawData); - if (!bytes || bytes.length === 0) return 1; - const decoded = decodeImageBytes(bytes, container); - if (!decoded) return 1; + const found = this.page?.containers.find((c) => c.kind === "image" && c.id === id); + const container = found?.kind === "image" ? found : null; + const raw = data.imageData ?? data.rawData ?? data.mapRawData; + const bytes = container ? imagePayloadBytes(raw) : null; + const decoded = container && bytes && bytes.length > 0 ? decodeImageBytes(bytes, container) : null; + const result = !container ? 3 : !decoded ? 1 : 0; + this.layoutLog(() => + describeImageUpdate({ + page: this.page, + sentId: id, + sentName: readString(data, "containerName", ""), + container, + raw, + bytes, + decoded, + result, + }), + ); + if (!container || !decoded) return result; container.pixels = decoded.pixels; container.pixelsWidth = decoded.width; container.pixelsHeight = decoded.height; this.windowHooks?.requestRender(); - return 0; + return result; + } + + /** Debug-level logcat for layouts and updates; `describe` runs only when logging is on. */ + private layoutLog(describe: () => string[]): void { + if (!LAYOUT_LOG_ENABLED) return; + try { + const prefix = `[${this.manifest.packageId}] `; + for (const line of describe()) { + for (let i = 0; i < line.length; i += LOGCAT_CHUNK_CHARS) { + android.util.Log.d(LAYOUT_LOG_TAG, prefix + line.slice(i, i + LOGCAT_CHUNK_CHARS)); + } + } + } catch (error) { + console.warn(`evenhub: layout log failed: ${error}`); + } } private shutDown(exitMode: number): boolean { + this.layoutLog(() => [`shutDownPageContainer exitMode=${exitMode}`]); if (exitMode === 1) { // The app asked to quit (stock would pop an on-glasses confirm). We // decline and hand focus to the app switcher instead, leaving the app @@ -1252,6 +1298,7 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH for (const container of page.containers) { if (container.preserve) preserveContent(container, previous); } + this.layoutLog(() => describePage("extension layout", page, data)); this.page = page; this.pageCreated = true; // The extended layout uses the taller app area. diff --git a/app/graphics/evenhub-font.ts b/app/graphics/evenhub-font.ts index d80ca2bd..bd612844 100644 --- a/app/graphics/evenhub-font.ts +++ b/app/graphics/evenhub-font.ts @@ -12,7 +12,8 @@ * - Text SIZING / KERNING / WRAPPING: @evenrealities/pretext, whose advance * tables the bitmaps were validated against. Using pretext for measurement * means our line breaks match exactly where EvenHub apps (which measure - * with the same library) expect them. + * with the same library) expect them. The one exception is leading spaces + * after a '\n', which stock keeps and pretext drops (see lvgl-wrap.ts). * * Bitmaps are 4bpp, high-nibble-first, row stride floor(boxW/2)+1. The UI * fonts are effectively 1-bit (nibbles 0 or 0xF); we keep the nibble value so @@ -23,6 +24,7 @@ import { File, knownFolders } from "@nativescript/core"; import { getTextWidth } from "@evenrealities/pretext"; import { EVENHUB_RUNTIME_FONT_FILENAME, isEvenHubFontAsset } from "../g2/firmware-fonts"; import { lvglMetrics, lvglGlyph } from "../native/lvgl-font"; +import { wrapLvglText } from "./lvgl-wrap"; import { GrayImage, grayToNibble, type FwTextFont, type PlacedFwText, type PlacedFwTextGlyph } from "./image"; import { TtfFont } from "./ttf-font"; import { decodeBase64 } from "../util/base64"; @@ -200,63 +202,11 @@ export class EvenHubFont implements FwTextFont { } /** - * Wrap text to a pixel width, returning the actual line strings. Ports - * pretext's measureTextWrap (LVGL lv_text_get_next_line semantics) so the - * breaks land exactly where the measurement library reports them. + * Wrap text to a pixel width, returning the actual line strings: pretext's + * breaks, but keeping leading spaces after a '\n' (see lvgl-wrap.ts). */ wrap(text: string, maxWidth: number): string[] { - if (!text) return [""]; - const cps = Array.from(text).map((c) => c.codePointAt(0)!); - const lines: string[] = []; - let line: number[] = []; - let lineWidth = 0; - let lastBreak = -1; // index within `line` after which we may wrap - - for (let i = 0; i < cps.length; i++) { - const cp = cps[i]!; - if (cp === 10) { - lines.push(String.fromCodePoint(...line)); - line = []; - lineWidth = 0; - lastBreak = -1; - continue; - } - // LVGL drops leading spaces at the start of a wrapped line. - if (line.length === 0 && cp === 32) continue; - const adv = this.advanceOf(cp, cps[i + 1] ?? 0); - if (lineWidth + adv > maxWidth) { - if (cp === 32) { - // The overflowing char is a space: break here, discard it. - lines.push(String.fromCodePoint(...line)); - line = []; - lineWidth = 0; - lastBreak = -1; - } else if (lastBreak !== -1) { - // Wrap at the last break opportunity; carry the remainder forward. - const carry = line.slice(lastBreak + 1); - line = line.slice(0, lastBreak + 1); - const trimmed = line[line.length - 1] === 32 ? line.slice(0, -1) : line; - lines.push(String.fromCodePoint(...trimmed)); - line = carry; - lineWidth = carry.reduce((sum, c, j) => sum + this.advanceOf(c, carry[j + 1] ?? 0), 0); - lastBreak = -1; - line.push(cp); - lineWidth += adv; - } else { - // No break opportunity: hard break before the overflowing char. - lines.push(String.fromCodePoint(...line)); - line = [cp]; - lineWidth = adv; - lastBreak = -1; - } - } else { - line.push(cp); - lineWidth += adv; - if (isBreakable(cp)) lastBreak = line.length - 1; - } - } - lines.push(String.fromCodePoint(...line)); - return lines; + return wrapLvglText(text, maxWidth, kernedAdvance); } /** @@ -446,8 +396,3 @@ export class EvenHubFont implements FwTextFont { } } -function isBreakable(cp: number): boolean { - if (cp === 32 || cp === 45) return true; // space, hyphen - // CJK ranges (pretext parity) — breakable between any two ideographs. - return (cp >= 0x2e80 && cp <= 0x9fff) || (cp >= 0xf900 && cp <= 0xfaff) || (cp >= 0xac00 && cp <= 0xd7af); -} diff --git a/app/graphics/lvgl-wrap.ts b/app/graphics/lvgl-wrap.ts new file mode 100644 index 00000000..3ad87ec3 --- /dev/null +++ b/app/graphics/lvgl-wrap.ts @@ -0,0 +1,86 @@ +/** + * Word wrap for EvenHub text containers, after LVGL's lv_text_get_next_line + * as ported by @evenrealities/pretext's measureTextWrap, so soft breaks land + * where apps that measure with pretext expect them. The loop mirrors + * measureTextWrap's, including rescanning from the break point when it wraps + * at an earlier opportunity. + * + * One deliberate difference from pretext: pretext skips leading spaces at the + * start of every line, but stock keeps them at the start of the text and after + * a '\n'; only a soft-wrapped continuation line loses them. Apps indent with + * spaces to clear image containers placed beside their text: Iris Pro indents a + * line 11 spaces (44px) past a 40px glyph. Stripping that indent slid the text + * under the icon. + */ + +/** Wrap opportunities: space, hyphen, and between CJK ideographs (pretext parity). */ +export function isLvglBreakable(cp: number): boolean { + if (cp === 32 || cp === 45) return true; + return (cp >= 0x2e80 && cp <= 0x9fff) || (cp >= 0xf900 && cp <= 0xfaff) || (cp >= 0xac00 && cp <= 0xd7af); +} + +/** + * Split text into display lines no wider than maxWidth. + * `advanceOf(cp, next)` is the kerned pixel advance of cp before next (0 at the end). + */ +export function wrapLvglText(text: string, maxWidth: number, advanceOf: (cp: number, next: number) => number): string[] { + if (!text) return [""]; + const cps = Array.from(text).map((c) => c.codePointAt(0)!); + const lines: string[] = []; + let line: number[] = []; + let lineWidth = 0; + // The last break opportunity on this line, as an index into cps, and how + // many of `line`'s codepoints a wrap there keeps (a breaking space is dropped). + let lastBreak = -1; + let lastBreakKeep = 0; + // Whether `line` continues a soft-wrapped line, whose leading spaces are dropped. + let continuation = false; + + const endLine = (keep: number[], next: number[], nextWidth: number) => { + lines.push(String.fromCodePoint(...keep)); + line = next; + lineWidth = nextWidth; + lastBreak = -1; + continuation = true; + }; + + let i = 0; + while (i < cps.length) { + const cp = cps[i]!; + if (cp === 10) { + endLine(line, [], 0); + continuation = false; + i++; + continue; + } + if (continuation && line.length === 0 && cp === 32) { + i++; + continue; + } + const adv = advanceOf(cp, cps[i + 1] ?? 0); + if (lineWidth + adv <= maxWidth) { + line.push(cp); + lineWidth += adv; + if (isLvglBreakable(cp)) { + lastBreak = i; + lastBreakKeep = cp === 32 ? line.length - 1 : line.length; + } + i++; + } else if (cp === 32) { + // The overflowing char is a space: break here and discard it. + endLine(line, [], 0); + i++; + } else if (lastBreak !== -1) { + // Wrap at the last break opportunity and rescan from just after it. + const restart = lastBreak + 1; + endLine(line.slice(0, lastBreakKeep), [], 0); + i = restart; + } else { + // No break opportunity: hard break before the overflowing char. + endLine(line, [cp], adv); + i++; + } + } + lines.push(String.fromCodePoint(...line)); + return lines; +} diff --git a/tests/evenhub-layout-debug.test.cjs b/tests/evenhub-layout-debug.test.cjs new file mode 100644 index 00000000..7e134ae7 --- /dev/null +++ b/tests/evenhub-layout-debug.test.cjs @@ -0,0 +1,122 @@ +// The EvenHub layout debug log: the stock container-order rules it compares +// our paint order against (from the evenRealities-openCFW reconstruction of +// evenhub_ui_containers.c), and the stock notes it attaches to updates. +const test = require("node:test"); +const assert = require("node:assert/strict"); + +const { parsePage, paintOrder } = require("../.test-build/app/apps/evenhub/containers.js"); +const { + describeImageUpdate, + describePage, + stockDifferences, + stockPaintOrder, +} = require("../.test-build/app/apps/evenhub/layout-debug.js"); + +const names = (containers) => containers.map((c) => c.name); + +const iconOverBody = (extra = {}) => + parsePage({ + containerTotalNum: 2, + textObject: [{ containerID: 1, containerName: "body", xPosition: 0, yPosition: 0, width: 576, height: 288, isEventCapture: 1, ...extra.text }], + imageObject: [{ containerID: 2, containerName: "icon", xPosition: 8, yPosition: 8, width: 24, height: 24, ...extra.image }], + }); + +test("without zOrderIndex, stock orders lists, then texts, then images", () => { + const page = parsePage({ + imageObject: [{ containerID: 1, containerName: "img" }], + textObject: [{ containerID: 2, containerName: "txt" }], + listObject: [{ containerID: 3, containerName: "lst" }], + }); + assert.deepEqual(names(stockPaintOrder(page.containers)), ["lst", "txt", "img"]); + // ...where we put images at the bottom. + assert.deepEqual(names(paintOrder(page.containers)), ["img", "lst", "txt"]); +}); + +test("stock sorts by zOrderIndex only when every container has one", () => { + const all = iconOverBody({ text: { zOrderIndex: 2 }, image: { zOrderIndex: 1 } }); + assert.deepEqual(names(stockPaintOrder(all.containers)), ["icon", "body"]); + assert.deepEqual(names(paintOrder(all.containers)), ["icon", "body"]); + + const some = iconOverBody({ text: { zOrderIndex: 2 } }); + assert.deepEqual(names(stockPaintOrder(some.containers)), ["body", "icon"]); + assert.deepEqual(names(paintOrder(some.containers)), ["icon", "body"]); + assert.ok(stockDifferences(some).some((note) => note.includes("zOrderIndex is set on 1 of 2"))); +}); + +test("an image overlapping text is flagged when the two orders disagree", () => { + const notes = stockDifferences(iconOverBody()); + assert.ok(notes.some((note) => note.startsWith("paint order differs"))); + assert.ok(notes.includes('image#2 "icon" overlaps text#1 "body": we draw text#1 "body" on top, stock draws image#2 "icon" on top')); +}); + +test("non-overlapping containers in a different order raise no overlap note", () => { + const notes = stockDifferences(iconOverBody({ text: { yPosition: 100, height: 100 } })); + assert.ok(!notes.some((note) => note.includes("overlaps"))); +}); + +test("image containers outside stock's size limits are flagged", () => { + const notes = stockDifferences(iconOverBody({ image: { width: 16, height: 16 } })); + assert.ok(notes.some((note) => note.includes("16x16") && note.includes("fails the whole page"))); +}); + +test("a shared container id resolves to the first container in stock order", () => { + const page = iconOverBody({ image: { containerID: 1 } }); + assert.ok(stockDifferences(page).includes('id 1 is shared by text#1 "body", image#1 "icon"; stock resolves it to text#1 "body" only')); + const lines = describeImageUpdate({ + page, + sentId: 1, + sentName: "", + container: page.containers.find((c) => c.kind === "image"), + raw: [], + bytes: null, + decoded: null, + result: 1, + }); + assert.ok(lines.some((line) => line.includes("stock drops this update"))); +}); + +test("image updates note base64 payloads, size mismatches and blank images", () => { + const page = iconOverBody(); + const icon = page.containers.find((c) => c.kind === "image"); + const lines = describeImageUpdate({ + page, + sentId: 2, + sentName: "icon", + container: icon, + raw: "AAAA", + bytes: new Uint8Array(3), + decoded: { pixels: new Uint8Array(4), width: 2, height: 2 }, + result: 0, + }); + assert.match(lines[0], /payload=string\(4 chars\)/); + assert.match(lines[0], /decoded=2x2 blank/); + assert.ok(lines.some((line) => line.includes("base64-string"))); + assert.ok(lines.some((line) => line.includes("exactly the container's size (24x24)"))); +}); + +test("lit pixels are summarized with a bounding box", () => { + const page = iconOverBody(); + const icon = page.containers.find((c) => c.kind === "image"); + const pixels = new Uint8Array(24 * 24); + pixels[24 * 3 + 5] = 200; + pixels[24 * 10 + 7] = 255; + const [first] = describeImageUpdate({ + page, + sentId: 2, + sentName: "", + container: icon, + raw: Array.from(pixels), + bytes: pixels, + decoded: { pixels, width: 24, height: 24 }, + result: 0, + }); + assert.match(first, /format=raw8 decoded=24x24 lit=2px max=255 bbox=5,3-7,10 result=0$/); +}); + +test("describePage lists containers bottom to top and ends with the raw payload", () => { + const lines = describePage("rebuildPageContainer", iconOverBody(), { containerTotalNum: 2 }); + assert.match(lines[0], /^rebuildPageContainer: 2 containers \(0 list, 1 text, 1 image\), containerTotalNum=2/); + assert.match(lines[1], /^ {2}0: image#2 "icon" @8,8 24x24 no pixels yet$/); + assert.match(lines[2], /^ {2}1: text#1 "body" @0,0 576x288 capture len=0 ""$/); + assert.equal(lines.at(-1), ' raw: {"containerTotalNum":2}'); +}); diff --git a/tests/lvgl-wrap.test.cjs b/tests/lvgl-wrap.test.cjs new file mode 100644 index 00000000..09821955 --- /dev/null +++ b/tests/lvgl-wrap.test.cjs @@ -0,0 +1,45 @@ +// EvenHub text-container wrapping (app/graphics/lvgl-wrap.ts): pretext's +// breaks, except that leading spaces survive at the start of the text and +// after '\n' — apps indent with them to clear icons placed beside the text. +const test = require("node:test"); +const assert = require("node:assert/strict"); + +const { wrapLvglText } = require("../.test-build/app/graphics/lvgl-wrap.js"); + +// Spaces are 4px like the stock 20px font; everything else 10px. +const fixed = (cp) => (cp === 32 ? 4 : 10); + +test("indentation after a newline is kept (Iris Pro's icon-clearing indent)", () => { + const content = " \n R E C O R D\n Conversation"; + const lines = wrapLvglText(content, 248, fixed); + assert.deepEqual(lines, [" ", " R E C O R D", " Conversation"]); +}); + +test("indentation at the very start of the text is kept", () => { + assert.deepEqual(wrapLvglText(" 55°", 248, fixed), [" 55°"]); +}); + +test("a soft-wrapped continuation line drops its leading spaces", () => { + // "aaaa" (40px) fits; the space after it overflows at width 40 and is + // discarded, and the next space would lead the continuation line. + assert.deepEqual(wrapLvglText("aaaa bbbb", 40, fixed), ["aaaa", "bbbb"]); + // Wrapping at an earlier break opportunity. + assert.deepEqual(wrapLvglText("aa bb cc", 54, fixed), ["aa bb", "cc"]); +}); + +test("unindented text breaks exactly where pretext's measureTextWrap says", async () => { + const pretext = await import("@evenrealities/pretext"); + const advance = (a, b) => (b ? pretext.getTextWidth(String.fromCodePoint(a, b)) - pretext.getTextWidth(String.fromCodePoint(b)) : pretext.getTextWidth(String.fromCodePoint(a))); + const samples = [ + "Market moving from feature flex to everyday integration", + "No one owns human, calm intelligence\nConversation likely becomes next differentiation layer", + "Supercalifragilisticexpialidocious-and-then-some words that will not fit on one line at all", + "short\n\nlines\nhere", + ]; + for (const text of samples) { + for (let width = 40; width <= 560; width += 7) { + const ours = wrapLvglText(text, width, advance); + assert.equal(ours.length, pretext.measureTextWrap(text, width).lineCount, `${JSON.stringify(text)} at ${width}px`); + } + } +}); diff --git a/tests/tsconfig.json b/tests/tsconfig.json index 7f5af242..1de19ea5 100644 --- a/tests/tsconfig.json +++ b/tests/tsconfig.json @@ -21,6 +21,7 @@ "../app/g2/ios-peripheral-identity.ts", "../app/graphics/image.ts", "../app/graphics/textwrap.ts", + "../app/graphics/lvgl-wrap.ts", "../app/graphics/battery.ts", "../app/graphics/plane.ts", "../app/graphics/shell-scene.ts", @@ -55,6 +56,7 @@ "../app/apps/microphones/caption-clock.ts", "../app/apps/microphones/name-guess.ts", "../app/apps/evenhub/containers.ts", + "../app/apps/evenhub/layout-debug.ts", "../app/apps/evenhub/list-menu.ts", "../app/apps/evenhub/device-status.ts", "../app/apps/evenhub/font-spec.ts",