Use the texture cache and builtin-font drawing firmware extensions

This commit is contained in:
James Babcock committed 2026-08-19 10:36:16 -07:00
1 parent deac81f98f
commit 90603b5aa4
9 files changed
+674 -60

No files matched your search

+127 -5
View File
@@ -21,7 +21,7 @@ import { File, knownFolders } from "@nativescript/core";
import { getTextWidth } from "@evenrealities/pretext";
import { EVENHUB_RUNTIME_FONT_FILENAME, isEvenHubFontAsset } from "../g2/firmware-fonts";
import { toUint8Array } from "../util/array-util";
import { GrayImage } from "./image";
import { GrayImage, type FwTextFont, type PlacedFwText, type PlacedFwTextGlyph } from "./image";
declare const com: any;
@@ -61,7 +61,35 @@ function base64Decode(text: string): Uint8Array {
return out.subarray(0, outIndex);
}
export class EvenHubFont {
/**
* Codepoint ranges whose glyphs the font asset merged from the flash CJK
* font with a baseline-shifted ofsY (see scripts/build-evenhub-font.mjs).
* The shift keeps the phone-side composite right, but the firmware's mode-15
* draw uses the root font's baseline with the glyph's TRUE ofsY, so the two
* may disagree by the shift — keep these baked until the fallback placement
* is hardware-verified. Mirrors the build script's SYMBOL_RANGES.
*/
const MODE15_UNCERTAIN_RANGES: ReadonlyArray<readonly [number, number]> = [
[0x2000, 0x206f], [0x2100, 0x214f], [0x2190, 0x21ff], [0x2200, 0x22ff],
[0x2500, 0x257f], [0x2580, 0x259f], [0x25a0, 0x25ff], [0x2600, 0x26ff],
[0x2700, 0x27bf], [0x3000, 0x303f], [0xff00, 0xffef],
];
function inMode15UncertainRange(cp: number): boolean {
for (const [start, end] of MODE15_UNCERTAIN_RANGES) {
if (cp >= start && cp <= end) return true;
}
return false;
}
/** The 4bpp level an 8-bit value packs to (BmpUtil's GRAY_TO_NIBBLE). */
function quantNibble(value: number): number {
return value <= 0 ? 0 : Math.min(15, (value + 8) >> 4);
}
export class EvenHubFont implements FwTextFont {
/** One builtin firmware font today; see FwTextFont. */
readonly atlasTag = 1;
readonly lineHeight: number;
readonly baseline: number;
private readonly glyphs: Map<number, GlyphRecord>;
@@ -195,22 +223,110 @@ export class EvenHubFont {
return lines;
}
/** Draw one line of text with its top-left at (x, y). Missing glyphs are skipped. */
/**
* Draw one line of text with its top-left at (x, y). Missing glyphs are
* skipped.
*
* Glyphs the firmware's mode-15 builtin-font draw renders identically to
* our raster (native-table entries outside MODE15_UNCERTAIN_RANGES) are
* recorded as deferred firmware-text runs: maximal stretches laid out with
* consecutive pretext advances, spaces included as inkless connectors so
* the firmware's own advance/kerning walk stays in lockstep. Anything else
* (CJK fallback, shifted symbols, unknown codepoints) rasters immediately
* and breaks the run — a break means the next run restarts at an explicit
* pen x, so no cursor prediction ever spans an uncertain glyph.
*/
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;
let run: PlacedFwTextGlyph[] = [];
let runX = 0;
const flushRun = () => {
if (run.length) {
this.emitRun(image, runX, y, value, run);
run = [];
}
};
for (let i = 0; i < cps.length; i++) {
const cp = cps[i]!;
const glyph = this.glyphs.get(cp);
if (glyph) {
if (glyph && !inMode15UncertainRange(cp)) {
if (run.length === 0) runX = penX;
run.push({
cp,
dx: penX - runX,
ink: glyph[0] > 0 && glyph[1] > 0 && glyph[5] > 0,
});
} else if (glyph) {
flushRun();
this.drawGlyph(image, glyph, this.bitmap, penX, baselineY, value);
} else {
flushRun();
const cjk = this.getCjkGlyph(cp);
if (cjk) this.drawGlyph(image, cjk.record, cjk.bitmap, penX, y + this.cjkBaseline, value);
}
penX += this.advanceOf(cp, cps[i + 1] ?? 0);
}
flushRun();
}
/** Record one run, trimmed of inkless leading/trailing connectors. */
private emitRun(
image: GrayImage,
runX: number,
lineY: number,
value: number,
members: PlacedFwTextGlyph[],
): void {
let first = 0;
let last = members.length - 1;
while (first <= last && !members[first]!.ink) first++;
while (last >= first && !members[last]!.ink) last--;
if (first > last) return; // nothing visible
const startX = runX + members[first]!.dx;
const trimmed = members.slice(first, last + 1).map((member) =>
first === 0 ? member : { ...member, dx: member.dx - members[first]!.dx },
);
image.drawFwText(this, startX, lineY, value, trimmed);
}
/** FwTextFont: bake a recorded run with firmware-exact pixel math. */
bakeFwTextRun(target: GrayImage, run: PlacedFwText, dx: number, dy: number): void {
const baselineY = run.y + dy + this.baseline;
for (const member of run.glyphs) {
if (!member.ink) continue;
const glyph = this.glyphs.get(member.cp);
if (glyph) this.drawGlyph(target, glyph, this.bitmap, run.x + member.dx + dx, baselineY, run.value);
}
}
/** FwTextFont: raster registration data for the Java-side planner. */
fwGlyphWireData(cp: number): {
ofsX: number;
inkTop: number;
boxW: number;
boxH: number;
nibbles: Uint8Array;
} | null {
const glyph = this.glyphs.get(cp);
if (!glyph) return null;
const [boxW, boxH, ofsX, ofsY, off, len] = glyph;
if (boxW <= 0 || boxH <= 0 || len <= 0 || boxW > 255 || boxH > 255) return null;
if (ofsX < -32768 || ofsX > 32767) return null;
const inkTop = this.baseline - boxH - ofsY;
if (inkTop < -32768 || inkTop > 32767) return null;
// Repack from the source stride (floor(boxW/2)+1, pad nibble at odd
// widths) to the wire stride ceil(boxW/2).
const srcStride = Math.floor(boxW / 2) + 1;
const outStride = (boxW + 1) >> 1;
const nibbles = new Uint8Array(outStride * boxH);
for (let row = 0; row < boxH; row++) {
for (let byte = 0; byte < outStride; byte++) {
nibbles[row * outStride + byte] = this.bitmap[off + row * srcStride + byte] ?? 0;
}
}
return { ofsX, inkTop, boxW, boxH, nibbles };
}
/** Draw wrapped text within a box; returns the number of lines drawn. */
@@ -257,13 +373,19 @@ export class EvenHubFont {
const stride = Math.floor(boxW / 2) + 1;
const left = penX + ofsX;
const top = baselineY - boxH - ofsY;
// Firmware-exact shading: the CFW draws through a LUT mapping source
// nibble n to n*top/15 (integer), skipping only n == 0. Writing 16*level
// makes the composite quantize (min(15, (v+8)>>4)) back to exactly that
// level, so the texture-cache planner's pixel checks — and the shadow
// after an on-glasses mode-15 draw — match this bake bit-for-bit.
const top4 = quantNibble(value);
for (let row = 0; row < boxH; row++) {
const rowStart = off + row * stride;
for (let col = 0; col < boxW; col++) {
const byte = 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));
image.setPixel(left + col, top + row, Math.floor((nibble * top4) / 15) * 16);
}
}
}
+76 -7
View File
@@ -20,12 +20,14 @@
* only means "no wire savings for this draw".
*/
import { BdfFont, Glyph } from "./bdffont";
import { GrayImage, type DeferredDraw, type PlacedImage } from "./image";
import { GrayImage, type DeferredDraw, type PlacedFwText, type PlacedImage } from "./image";
declare const com: any;
const GLYPH_RECORD_BYTES = 12; // [0][fontId u16][encoding u32][penX s16][lineY s16][value u8]
const IMAGE_RECORD_BYTES = 9; // [1][imageId u32][x s16][y s16]
const GLYPH_RECORD_BYTES = 12; // [0][fontId u16][encoding u32][penX s16][lineY s16][value u8]
const IMAGE_RECORD_BYTES = 9; // [1][imageId u32][x s16][y s16]
const FWTEXT_HEADER_BYTES = 7; // [2][x s16][y s16][value u8][count u8]
const FWTEXT_MEMBER_BYTES = 7; // [cp u32][dx s16][ink u8]
type FontWireState = {
fontId: number;
@@ -74,6 +76,51 @@ function representableGlyph(font: BdfFont, glyph: Glyph): boolean {
return inkTop >= 0 && inkTop + glyph.bbxHeight <= font.lineHeight;
}
/** Firmware-font codepoints already registered with the Java FwGlyphAtlas. */
const fwRegistered = new Set<string>();
/**
* Whether a firmware-text run is expressible on the wire, registering any
* unseen ink members' rasters with the Java FwGlyphAtlas as a side effect.
*/
function prepareFwTextRun(placed: PlacedFwText): boolean {
if (!inRange16(placed.x) || !inRange16(placed.y)) return false;
if (placed.glyphs.length === 0 || placed.glyphs.length > 255) return false;
let registration: Array<{ cp: number; data: NonNullable<ReturnType<PlacedFwText["font"]["fwGlyphWireData"]>> }> | null = null;
for (const member of placed.glyphs) {
if (member.cp < 0 || member.cp > 0x10ffff || !inRange16(member.dx)) return false;
if (!member.ink) continue;
const key = `${placed.font.atlasTag}:${member.cp}`;
if (fwRegistered.has(key)) continue;
const data = placed.font.fwGlyphWireData(member.cp);
if (!data) return false; // ink without raster data: keep the run baked
fwRegistered.add(key);
registration ??= [];
registration.push({ cp: member.cp, data });
}
if (registration) {
let total = 0;
for (const entry of registration) {
total += 10 + entry.data.nibbles.length;
}
const buffer = new ArrayBuffer(total);
const view = new DataView(buffer);
const bytes = new Uint8Array(buffer);
let offset = 0;
for (const entry of registration) {
view.setUint32(offset, entry.cp, true);
view.setInt16(offset + 4, entry.data.ofsX, true);
view.setInt16(offset + 6, entry.data.inkTop, true);
view.setUint8(offset + 8, entry.data.boxW);
view.setUint8(offset + 9, entry.data.boxH);
bytes.set(entry.data.nibbles, offset + 10);
offset += 10 + entry.data.nibbles.length;
}
com.faceclaw.app.FwGlyphAtlas.register(buffer);
}
return true;
}
/** Resolve (registering on first sight) the Java image id for a drawImage source. */
function imageId(placed: PlacedImage): number | null {
if (!global.isAndroid) return null;
@@ -100,13 +147,16 @@ function inRange16(v: number): boolean {
* alongside the frame's pixels: little-endian tagged records
* [0][fontId u16][encoding u32][penX s16][lineY s16][value u8] (glyph)
* [1][imageId u32][x s16][y s16] (image)
* [2][x s16][y s16][value u8][count u8] (fw text run)
* then count x [cp u32][dx s16][ink u8]
* in draw order. Returns null when nothing is expressible (or off-Android).
*/
export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer | null {
if (!global.isAndroid || draws.length === 0) return null;
// Pass 1: resolve ids, collect unregistered glyph rasters, size the buffer.
// Pass 1: resolve ids, register unseen rasters, size the buffer.
let registration: Map<FontWireState, { font: BdfFont; glyphs: Glyph[] }> | null = null;
const fwRunOk = new Map<PlacedFwText, boolean>();
let bytes = 0;
for (const placed of draws) {
if (placed.kind === "glyph") {
@@ -123,9 +173,13 @@ export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer |
registration.set(state, group);
}
group.glyphs.push(placed.glyph);
} else {
} else if (placed.kind === "image") {
if (!inRange16(placed.x) || !inRange16(placed.y)) continue;
if (imageId(placed) !== null) bytes += IMAGE_RECORD_BYTES;
} else {
const ok = prepareFwTextRun(placed);
fwRunOk.set(placed, ok);
if (ok) bytes += FWTEXT_HEADER_BYTES + placed.glyphs.length * FWTEXT_MEMBER_BYTES;
}
}
if (registration) {
@@ -137,8 +191,8 @@ export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer |
const out = new DataView(new ArrayBuffer(bytes));
let offset = 0;
for (const placed of draws) {
if (!inRange16(placed.x) || !inRange16(placed.y)) continue;
if (placed.kind === "glyph") {
if (!inRange16(placed.x) || !inRange16(placed.y)) continue;
const state = fontWireState(placed.font);
if (!state || !representableGlyph(placed.font, placed.glyph)) continue;
out.setUint8(offset, 0);
@@ -148,7 +202,8 @@ export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer |
out.setInt16(offset + 9, placed.y, true);
out.setUint8(offset + 11, placed.value);
offset += GLYPH_RECORD_BYTES;
} else {
} else if (placed.kind === "image") {
if (!inRange16(placed.x) || !inRange16(placed.y)) continue;
const id = imageId(placed);
if (id === null) continue;
out.setUint8(offset, 1);
@@ -156,6 +211,20 @@ export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer |
out.setInt16(offset + 5, placed.x, true);
out.setInt16(offset + 7, placed.y, true);
offset += IMAGE_RECORD_BYTES;
} else {
if (!fwRunOk.get(placed)) continue;
out.setUint8(offset, 2);
out.setInt16(offset + 1, placed.x, true);
out.setInt16(offset + 3, placed.y, true);
out.setUint8(offset + 5, placed.value);
out.setUint8(offset + 6, placed.glyphs.length);
offset += FWTEXT_HEADER_BYTES;
for (const member of placed.glyphs) {
out.setUint32(offset, member.cp, true);
out.setInt16(offset + 4, member.dx, true);
out.setUint8(offset + 6, member.ink ? 1 : 0);
offset += FWTEXT_MEMBER_BYTES;
}
}
}
return out.buffer;
+97 -3
View File
@@ -50,13 +50,62 @@ export type PlacedImage = {
y: number;
};
/** One member of a firmware-font text run: codepoint at run.x + dx; ink =
* whether it has visible pixels (spaces are inkless connectors kept for the
* firmware's own advance/kerning continuity). */
export type PlacedFwTextGlyph = {
cp: number;
dx: number;
ink: boolean;
};
/**
* The provider behind firmware-font text runs (implemented by EvenHubFont).
* Structural interface so image.ts needs no import: the font both bakes runs
* phone-side (with firmware-exact pixel math) and serves per-glyph raster
* data for the Java-side eligibility checks.
*/
export interface FwTextFont {
/** Stable tag for fingerprints/registration (one builtin font today). */
readonly atlasTag: number;
bakeFwTextRun(target: GrayImage, run: PlacedFwText, dx: number, dy: number): void;
/** Wire-registration raster for a run member, or null when unavailable. */
fwGlyphWireData(cp: number): {
ofsX: number;
/** First ink row relative to the run's line-top y. */
inkTop: number;
boxW: number;
boxH: number;
/** 4bpp rows, stride ceil(boxW/2), high nibble = left pixel. */
nibbles: Uint8Array;
} | null;
}
/**
* One deferred firmware-font text run (CFW mode 15): a maximal stretch of
* text that EvenHubFont laid out with consecutive pretext advances, drawn by
* the glasses' builtin 20px font chain. Members' dx are relative to x so
* translation only touches x/y. Runs must only be built over glyphs whose
* on-glasses rendering the phone predicts exactly (the font enforces this).
*/
export type PlacedFwText = {
kind: "fwtext";
font: FwTextFont;
/** Pen x of the first member; the wire draw starts exactly here. */
x: number;
/** Line top y (the mode-15 y argument). */
y: number;
value: number;
glyphs: readonly PlacedFwTextGlyph[];
};
/**
* A retained draw, always rendered on top of the raster pixels regardless of
* draw order (deferred draws keep their order among themselves). An overlay
* that must cover deferred draws below it needs its own plane (see plane.ts)
* or must suppress the covered draws.
*/
export type DeferredDraw = PlacedGlyph | PlacedImage;
export type DeferredDraw = PlacedGlyph | PlacedImage | PlacedFwText;
export class GrayImage {
readonly width: number;
@@ -321,11 +370,19 @@ export class GrayImage {
hash = mixInt(hash, placed.font.fingerprintId);
hash = mixInt(hash, placed.glyph.encoding);
hash = mixInt(hash, placed.value);
} else {
} else if (placed.kind === "image") {
hash = mixInt(hash, 0x1c0e5);
hash = mixInt(hash, placed.source.width);
hash = mixInt(hash, placed.source.height);
hash = mixInt(hash, placed.source.contentHash32());
} else {
hash = mixInt(hash, 0xf17e);
hash = mixInt(hash, placed.font.atlasTag);
hash = mixInt(hash, placed.value);
for (const member of placed.glyphs) {
hash = mixInt(hash, member.cp);
hash = mixInt(hash, member.dx);
}
}
hash = mixInt(hash, placed.x);
hash = mixInt(hash, placed.y);
@@ -373,6 +430,41 @@ export class GrayImage {
this.drawList.push({ kind: "image", source: source.withDrawsBaked(), x, y });
}
/**
* Record a firmware-font text run (see PlacedFwText). Called by
* EvenHubFont, which owns run construction; the members array must be
* immutable from here on.
*/
drawFwText(
font: FwTextFont,
x: number,
y: number,
value: number,
glyphs: readonly PlacedFwTextGlyph[],
): void {
if (glyphs.length === 0) return;
this.drawList.push({ kind: "fwtext", font, x, y, value: clampByte(value), glyphs });
}
/**
* Bake this image's deferred draws into its own pixels and clear the list.
* Used where later raster must be able to cover earlier draws within the
* same image (e.g. the EvenHub compositor's image-over-text z-order).
*/
bakeDeferredDrawsInPlace(): void {
if (!this.drawList.length) return;
const draws = this.drawList.splice(0, this.drawList.length);
for (const placed of draws) {
if (placed.kind === "glyph") {
rasterizeGlyph(this, placed.font, placed.glyph, placed.x, placed.y, placed.value);
} else if (placed.kind === "image") {
this.bitBlt(placed.source, placed.x, placed.y, { transparentZero: true });
} else {
placed.font.bakeFwTextRun(this, placed, 0, 0);
}
}
}
/**
* This image with its deferred draws rasterized into the pixel buffer.
* Returns this image unchanged when there are none, otherwise a baked copy
@@ -391,8 +483,10 @@ export class GrayImage {
for (const placed of this.drawList) {
if (placed.kind === "glyph") {
rasterizeGlyph(target, placed.font, placed.glyph, placed.x + dx, placed.y + dy, placed.value);
} else {
} else if (placed.kind === "image") {
target.bitBlt(placed.source, placed.x + dx, placed.y + dy, { transparentZero: true });
} else {
placed.font.bakeFwTextRun(target, placed, dx, dy);
}
}
}