diff --git a/app/apps/evenhub/compositor.ts b/app/apps/evenhub/compositor.ts index 2c870b66..5bd29711 100644 --- a/app/apps/evenhub/compositor.ts +++ b/app/apps/evenhub/compositor.ts @@ -2,15 +2,16 @@ * Phone-side reimplementation of the on-glasses EvenHub page compositor: * containers render into the 576x288 canvas the stock firmware gives apps. * + * Text uses Even's extracted 20px firmware font (via EvenHubFont), measured + * and wrapped with @evenrealities/pretext, so glyph widths and line breaks + * match what apps expect. + * * Known deviations from stock, acceptable for now: - * - Text uses Terminus 24 rather than the firmware's proportional LVGL font, - * so grid-aligned/padded layouts won't line up exactly (see - * notes/evenhub_compatibility.txt section 6 for the extraction plan). * - List rendering is a plain vertical list; itemWidth-based horizontal * layouts are not implemented yet. */ import { GrayImage } from "../../graphics/image"; -import { getFont } from "../../graphics/bdffont"; +import { EvenHubFont } from "../../graphics/evenhub-font"; import { type EvenHubContainer, type EvenHubImageContainer, @@ -25,24 +26,19 @@ export const EVENHUB_SCREEN_HEIGHT = 288; const TEXT_WHITE = 255; const LIST_ROW_PADDING = 4; -function containerFont() { - return getFont("terminus24"); -} - -/** Chars the font lacks are skipped silently on stock (no tofu); drawText - * already skips unknown glyphs, matching that. */ +/** Chars the font lacks are skipped silently on stock (no tofu); EvenHubFont + * skips unknown glyphs, matching that. */ function paintTextContainer(image: GrayImage, container: EvenHubTextContainer): void { paintBorder(image, container); const inset = container.borderWidth + container.paddingLength; - const font = containerFont(); - image.drawTextWrapped({ - font, - x: container.x + inset, - y: container.y + inset, - width: Math.max(1, container.width - 2 * inset), - text: container.content, - value: TEXT_WHITE, - }); + EvenHubFont.get().drawTextWrapped( + image, + container.x + inset, + container.y + inset, + Math.max(1, container.width - 2 * inset), + container.content, + TEXT_WHITE, + ); } function paintBorder( @@ -82,7 +78,7 @@ function paintImageContainer(image: GrayImage, container: EvenHubImageContainer) function paintListContainer(image: GrayImage, container: EvenHubListContainer, focused: boolean): void { paintBorder(image, container); - const font = containerFont(); + const font = EvenHubFont.get(); const inset = container.borderWidth + container.paddingLength; const rowHeight = font.lineHeight + LIST_ROW_PADDING; const left = container.x + inset; @@ -103,7 +99,7 @@ function paintListContainer(image: GrayImage, container: EvenHubListContainer, f if (selected && container.selectBorder) { image.drawRect(left, y, width, rowHeight - 1, TEXT_WHITE); } - image.drawText(font, left + 2, y + Math.floor(LIST_ROW_PADDING / 2), container.itemNames[index]!, TEXT_WHITE); + font.drawText(image, left + 2, y + Math.floor(LIST_ROW_PADDING / 2), container.itemNames[index]!, TEXT_WHITE); } } diff --git a/app/apps/evenhub/session.ts b/app/apps/evenhub/session.ts index 95ab8762..1419cfe5 100644 --- a/app/apps/evenhub/session.ts +++ b/app/apps/evenhub/session.ts @@ -19,7 +19,7 @@ */ import { ApplicationSettings } from "@nativescript/core"; import { GrayImage } from "../../graphics/image"; -import { getFont } from "../../graphics/bdffont"; +import { EvenHubFont } from "../../graphics/evenhub-font"; import { type DashboardInputEvent } from "../../ui/layers"; import { asRecord, @@ -158,9 +158,9 @@ export class EvenHubSession { paint(size: { width: number; height: number }, focused: boolean): GrayImage { if (!this.pageCreated || !this.page) { const image = new GrayImage(size.width, size.height, 0); - const font = getFont("terminus24"); - image.drawText(font, 16, 16, `Loading ${this.manifest.name}...`, 255); - image.drawText(font, 16, 16 + 2 * font.lineHeight, "(waiting for the app to build its page)", 120); + const font = EvenHubFont.get(); + font.drawText(image, 16, 16, `Loading ${this.manifest.name}...`, 255); + font.drawText(image, 16, 16 + 2 * font.lineHeight, "(waiting for the app to build its page)", 120); return image; } return compositePage(this.page, size, focused); diff --git a/app/graphics/evenhub-font.ts b/app/graphics/evenhub-font.ts new file mode 100644 index 00000000..2046d4d7 --- /dev/null +++ b/app/graphics/evenhub-font.ts @@ -0,0 +1,205 @@ +/** + * Even's on-glasses 20px UI font, for the EvenHub compatibility layer. + * + * Two halves, from two sources: + * - Glyph BITMAPS + boxes: preprocessed from the firmware's extracted LVGL + * fonts (latin + greek/cyrillic + emoji, merged in the EvenHub fallback + * order) into fonts/evenhub/evenhub-20.json by scripts/build-evenhub-font.mjs. + * - 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. + * + * 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 + * any anti-aliasing survives. Codepoints with no glyph are skipped silently, + * matching stock firmware (no tofu boxes). + */ +import { knownFolders } from "@nativescript/core"; +import { getTextWidth } from "@evenrealities/pretext"; +import { GrayImage } from "./image"; + +const ASSET_PATH = "fonts/evenhub/evenhub-20.json"; + +/** [boxW, boxH, ofsX, ofsY, bitmapOffset, bitmapLen] */ +type GlyphRecord = [number, number, number, number, number, number]; + +type FontAsset = { + lineHeight: number; + baseline: number; + glyphs: Record; + bitmapBase64: string; +}; + +const BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +function base64Decode(text: string): Uint8Array { + const clean = text.replace(/[^A-Za-z0-9+/]/g, ""); + const out = new Uint8Array(Math.floor((clean.length * 3) / 4)); + let bits = 0; + let bitCount = 0; + let outIndex = 0; + for (let i = 0; i < clean.length; i++) { + bits = (bits << 6) | BASE64_ALPHABET.indexOf(clean[i]!); + bitCount += 6; + if (bitCount >= 8) { + bitCount -= 8; + out[outIndex++] = (bits >> bitCount) & 0xff; + } + } + return out.subarray(0, outIndex); +} + +export class EvenHubFont { + readonly lineHeight: number; + readonly baseline: number; + private readonly glyphs: Map; + private readonly bitmap: Uint8Array; + + private constructor(asset: FontAsset) { + this.lineHeight = asset.lineHeight; + this.baseline = asset.baseline; + this.bitmap = base64Decode(asset.bitmapBase64); + this.glyphs = new Map(); + for (const key of Object.keys(asset.glyphs)) { + this.glyphs.set(Number(key), asset.glyphs[key]!); + } + } + + private static cached: EvenHubFont | null = null; + + static get(): EvenHubFont { + if (!EvenHubFont.cached) { + const text = knownFolders.currentApp().getFile(ASSET_PATH).readTextSync(); + EvenHubFont.cached = new EvenHubFont(JSON.parse(text) as FontAsset); + } + return EvenHubFont.cached; + } + + hasGlyph(codePoint: number): boolean { + return this.glyphs.has(codePoint); + } + + /** + * Pixel advance of a codepoint, including kerning against the following one + * (pass 0/undefined for the last glyph). Derived from pretext's public + * getTextWidth so it carries pretext's kerning and missing-glyph placeholder + * exactly: width(a+b) - width(b) isolates a's kerned advance. + */ + advanceOf(codePoint: number, nextCodePoint = 0): number { + const a = String.fromCodePoint(codePoint); + if (!nextCodePoint) return getTextWidth(a); + const b = String.fromCodePoint(nextCodePoint); + return getTextWidth(a + b) - getTextWidth(b); + } + + /** Single-line pixel width including kerning (pretext-exact). */ + measureLine(text: string): number { + return getTextWidth(text); + } + + /** + * 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: 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; + } + + /** Draw one line of text with its top-left at (x, y). Missing glyphs are skipped. */ + drawText(image: GrayImage, x: number, y: number, text: string, value = 255): void { + const cps = Array.from(text).map((c) => c.codePointAt(0)!); + const baselineY = y + this.baseline; + let penX = x; + for (let i = 0; i < cps.length; i++) { + const cp = cps[i]!; + const glyph = this.glyphs.get(cp); + if (glyph) this.drawGlyph(image, glyph, penX, baselineY, value); + penX += this.advanceOf(cp, cps[i + 1] ?? 0); + } + } + + /** Draw wrapped text within a box; returns the number of lines drawn. */ + drawTextWrapped(image: GrayImage, x: number, y: number, maxWidth: number, text: string, value = 255): number { + const lines = this.wrap(text, maxWidth); + for (let i = 0; i < lines.length; i++) { + this.drawText(image, x, y + i * this.lineHeight, lines[i]!, value); + } + return lines.length; + } + + private drawGlyph(image: GrayImage, glyph: GlyphRecord, penX: number, baselineY: number, value: number): void { + const [boxW, boxH, ofsX, ofsY, off, len] = glyph; + if (boxW <= 0 || boxH <= 0 || len <= 0) return; + const stride = Math.floor(boxW / 2) + 1; + const left = penX + ofsX; + const top = baselineY - boxH - ofsY; + for (let row = 0; row < boxH; row++) { + const rowStart = off + row * stride; + for (let col = 0; col < boxW; col++) { + const byte = this.bitmap[rowStart + (col >> 1)] ?? 0; + const nibble = col % 2 === 0 ? byte >> 4 : byte & 0x0f; + if (nibble === 0) continue; + image.setPixel(left + col, top + row, Math.round((nibble / 15) * value)); + } + } + } +} + +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/package-lock.json b/package-lock.json index 5c4bd8ab..783099da 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,6 +8,7 @@ "name": "faceclaw", "version": "1.0.0", "dependencies": { + "@evenrealities/pretext": "^0.1.4", "@nativescript/core": "~9.0.0", "@nativescript/theme": "^3.1.0", "@xterm/headless": "^6.0.0", @@ -404,6 +405,12 @@ "node": ">=10.0.0" } }, + "node_modules/@evenrealities/pretext": { + "version": "0.1.4", + "resolved": "https://registry.npmjs.org/@evenrealities/pretext/-/pretext-0.1.4.tgz", + "integrity": "sha512-gSZk/uxIhZW3geOM5bP1IS0s1sg7cA7KLLpBDgH9T1+ZNHMIFTMcgQN1qw4+/Xi/rcxsDsWZ7UZndIS/s4fRcA==", + "license": "MIT" + }, "node_modules/@jridgewell/gen-mapping": { "version": "0.3.13", "resolved": "https://registry.npmjs.org/@jridgewell/gen-mapping/-/gen-mapping-0.3.13.tgz", diff --git a/package.json b/package.json index 67528064..26cbed6d 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "build": "nativescript build android" }, "dependencies": { + "@evenrealities/pretext": "^0.1.4", "@nativescript/core": "~9.0.0", "@nativescript/theme": "^3.1.0", "@xterm/headless": "^6.0.0",