Files
faceclaw/app/graphics/glyph-wire.ts
T

226 lines
8.8 KiB
TypeScript

/**
* Marshals deferred-draw identity (text glyphs and icon images) to the Java
* side for the texture-cache pipeline (CFW modes 12/13/14; see
* notes/texture-cache-display-list-design.md).
*
* Channels, all cheap ByteBuffer crossings:
* - glyph raster registration into the process-wide Java GlyphAtlas, once
* per (font, glyph) per JS context (main thread and each worker register
* independently; the atlas dedupes by the font's stable atlasKey);
* - image registration into the Java ImageAtlas, once per distinct image
* content per JS context (images are content-addressed: the key is a hash
* of dimensions + pixels, so the same icon rendered anywhere dedupes and
* a changed icon is simply a new image);
* - a per-frame draw list accompanying each submitted surface frame, which
* the Java planner may replay as on-glasses cached draws.
*
* Draws that can't participate (no atlasKey, outside the mode-14 ASCII
* table, ink outside the line cell, oversized images) are simply left out —
* their pixels are baked into the submitted frame either way, so exclusion
* only means "no wire savings for this draw".
*/
import { BdfFont, Glyph } from "./bdffont";
import { GrayImage, type DeferredDraw, 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]
type FontWireState = {
fontId: number;
cellHeight: number;
/** Encodings already registered with the Java atlas from this JS context. */
registered: Set<number>;
};
const fontStates = new Map<BdfFont, FontWireState>();
/**
* Java-side image id per source image object, or null for images that can't
* participate. Sources handed to drawImage are immutable by contract, so
* memoizing by object identity is safe; a re-rendered icon is a new object
* and re-resolves (usually to the same content-addressed id).
*/
const imageIds = new WeakMap<GrayImage, number | null>();
function fontWireState(font: BdfFont): FontWireState | null {
if (!global.isAndroid || !font.atlasKey) return null;
if (font.lineHeight <= 0 || font.lineHeight > 255) return null;
let state = fontStates.get(font);
if (!state) {
state = {
fontId: com.faceclaw.app.GlyphAtlas.fontId(font.atlasKey),
cellHeight: font.lineHeight,
registered: new Set(),
};
fontStates.set(font, state);
}
return state;
}
/**
* Whether this glyph can be expressed as an on-glasses cached draw: within
* the mode-14 char table (32..127), a nonempty raster that fits the font's
* line cell (the cached image is bbxWidth x lineHeight with the ink placed at
* inkTop), and metrics that fit the wire fields.
*/
function representableGlyph(font: BdfFont, glyph: Glyph): boolean {
if (glyph.encoding < 32 || glyph.encoding > 127) return false;
if (glyph.bbxWidth <= 0 || glyph.bbxWidth > 255) return false;
if (glyph.bbxHeight <= 0 || glyph.bbxHeight > 255) return false;
if (glyph.bbxX < -128 || glyph.bbxX > 127) return false;
const inkTop = font.ascent - (glyph.bbxHeight + glyph.bbxY);
return inkTop >= 0 && inkTop + glyph.bbxHeight <= font.lineHeight;
}
/** Resolve (registering on first sight) the Java image id for a drawImage source. */
function imageId(placed: PlacedImage): number | null {
if (!global.isAndroid) return null;
const source = placed.source;
const memo = imageIds.get(source);
if (memo !== undefined) return memo;
let id: number | null = null;
if (source.width > 0 && source.width <= 255 && source.height > 0 && source.height <= 255) {
const key = `img:${source.width}x${source.height}:${source.contentHash32().toString(16)}`;
id = com.faceclaw.app.ImageAtlas.ensure(key, source.width, source.height, source.pixels.buffer);
if (!(typeof id === "number") || id <= 0) id = null;
}
imageIds.set(source, id);
return id;
}
function inRange16(v: number): boolean {
return v >= -32768 && v <= 32767;
}
/**
* Register any not-yet-registered glyph rasters and image contents among
* `draws` with the Java atlases, then build the per-frame draw buffer to pass
* 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)
* 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.
let registration: Map<FontWireState, { font: BdfFont; glyphs: Glyph[] }> | null = null;
let bytes = 0;
for (const placed of draws) {
if (placed.kind === "glyph") {
const state = fontWireState(placed.font);
if (!state || !representableGlyph(placed.font, placed.glyph)) continue;
if (!inRange16(placed.x) || !inRange16(placed.y)) continue;
bytes += GLYPH_RECORD_BYTES;
if (state.registered.has(placed.glyph.encoding)) continue;
state.registered.add(placed.glyph.encoding);
registration ??= new Map();
let group = registration.get(state);
if (!group) {
group = { font: placed.font, glyphs: [] };
registration.set(state, group);
}
group.glyphs.push(placed.glyph);
} else {
if (!inRange16(placed.x) || !inRange16(placed.y)) continue;
if (imageId(placed) !== null) bytes += IMAGE_RECORD_BYTES;
}
}
if (registration) {
com.faceclaw.app.GlyphAtlas.register(buildRegistrationBuffer(registration));
}
if (bytes === 0) return null;
// Pass 2: the frame's draw records.
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") {
const state = fontWireState(placed.font);
if (!state || !representableGlyph(placed.font, placed.glyph)) continue;
out.setUint8(offset, 0);
out.setUint16(offset + 1, state.fontId, true);
out.setUint32(offset + 3, placed.glyph.encoding, true);
out.setInt16(offset + 7, placed.x, true);
out.setInt16(offset + 9, placed.y, true);
out.setUint8(offset + 11, placed.value);
offset += GLYPH_RECORD_BYTES;
} else {
const id = imageId(placed);
if (id === null) continue;
out.setUint8(offset, 1);
out.setUint32(offset + 1, id, true);
out.setInt16(offset + 5, placed.x, true);
out.setInt16(offset + 7, placed.y, true);
offset += IMAGE_RECORD_BYTES;
}
}
return out.buffer;
}
/**
* Registration buffer: per font group
* [keyLen u8][key utf8][cellHeight u8][count u16]
* then per glyph
* [encoding u32][bbxX s8][inkTop u8][width u8][inkHeight u8]
* [inkHeight x row u32] (bit (ceil(width/8)*8 - 1 - col) = ink)
* mirroring GlyphAtlas.register on the Java side.
*/
function buildRegistrationBuffer(
registration: Map<FontWireState, { font: BdfFont; glyphs: Glyph[] }>,
): ArrayBuffer {
let total = 0;
const encodedKeys = new Map<FontWireState, Uint8Array>();
registration.forEach((group, state) => {
const keyBytes = utf8Encode(group.font.atlasKey ?? "");
encodedKeys.set(state, keyBytes);
total += 1 + keyBytes.length + 1 + 2;
for (const glyph of group.glyphs) {
total += 8 + 4 * glyph.bbxHeight;
}
});
const buffer = new ArrayBuffer(total);
const view = new DataView(buffer);
const bytes = new Uint8Array(buffer);
let offset = 0;
registration.forEach((group, state) => {
const keyBytes = encodedKeys.get(state)!;
view.setUint8(offset, keyBytes.length);
bytes.set(keyBytes, offset + 1);
offset += 1 + keyBytes.length;
view.setUint8(offset, state.cellHeight);
view.setUint16(offset + 1, group.glyphs.length, true);
offset += 3;
for (const glyph of group.glyphs) {
const inkTop = group.font.ascent - (glyph.bbxHeight + glyph.bbxY);
view.setUint32(offset, glyph.encoding, true);
view.setInt8(offset + 4, glyph.bbxX);
view.setUint8(offset + 5, inkTop);
view.setUint8(offset + 6, glyph.bbxWidth);
view.setUint8(offset + 7, glyph.bbxHeight);
offset += 8;
for (let row = 0; row < glyph.bbxHeight; row++) {
view.setUint32(offset, (glyph.bitmapRows[row] ?? 0) >>> 0, true);
offset += 4;
}
}
});
return buffer;
}
function utf8Encode(text: string): Uint8Array {
// TextEncoder exists in NativeScript's JS runtime; fall back to code units
// (atlas keys are ASCII font names, so both paths agree).
if (typeof TextEncoder !== "undefined") {
return new TextEncoder().encode(text);
}
const out = new Uint8Array(text.length);
for (let i = 0; i < text.length; i++) {
out[i] = text.charCodeAt(i) & 0x7f;
}
return out;
}