forked from Blendman974/faceclaw
Add animations to launcher and Files app
This commit is contained in:
1 parent
7ad9fe10ad
commit
b0a98f5942
24 files changed
+1608
-738
No files matched your search
@@ -24,7 +24,7 @@ const list: DisplayList = {
|
||||
frame.drawDisplayList(list, 20, 30, 140, 20, 0);
|
||||
```
|
||||
|
||||
Supported calls in this first pass:
|
||||
Supported calls:
|
||||
|
||||
- `ROUNDED_RECT`: literal or expression `x`/`y`, literal dimensions/radius, and
|
||||
4-bit colors (0–15). Border 16 means no border. Fill takes the maximum of the
|
||||
@@ -38,6 +38,24 @@ Supported calls in this first pass:
|
||||
rejects the list. `SCREEN` reads the uncomposed frame; its source coordinates
|
||||
are also relative to this list's placement. Each call may add its own `depth`
|
||||
to the list's depth. A screen restore normally uses the negative of the list depth.
|
||||
- `CLEAR` (revision 35 with a clip): fill with a 4-bit `color`. Under a clip it
|
||||
fills just the clip rect, which is how to blank a region; without one it
|
||||
fills the whole screen.
|
||||
- `DRAWS` (bridge-only): replay glyph and icon draws offset by `x`/`y`, which
|
||||
may be expressions. `records`/`count` come from glyph-wire's
|
||||
`encodeReplayDraws(image.draws)`, which registers the rasters and returns null
|
||||
unless every draw is a replayable glyph (atlas font, ASCII, ink inside the
|
||||
line box) or plain image. Kotlin compiles them to firmware IMAGE calls on
|
||||
ImageAtlas resources and TEXT runs on FontResourceAtlas fonts (one run per
|
||||
line and color). `source` keeps the draws for software painting. If Kotlin
|
||||
cannot resolve a record, the whole list draws nothing, so the frame's own
|
||||
pixels show rather than content with a hole in it.
|
||||
|
||||
Any call may carry a `clip` (revision 35): a rect in list coordinates outside
|
||||
which it draws nothing. It moves with the call's depth, like the call itself.
|
||||
The firmware flag also nests (a clipped play-list call clips everything inside
|
||||
it, intersected with the calls' own clips), but TS lists are inlined into the
|
||||
root, so each call carries its own.
|
||||
|
||||
Kotlin allocates/cache-pins all image resources, resolves the local indices, applies
|
||||
surface placement, and includes the calls in the root firmware display list. The
|
||||
@@ -68,7 +86,9 @@ exclude the changing elapsed duration. Without a timeline, raw `time()` expressi
|
||||
start at each PRESENT. Native compilation strips the bridge-only elapsed opcode.
|
||||
|
||||
`menu-selection-list.ts` authors both the rounded highlight and the transparent row
|
||||
image using this API. `menu-scroll-list.ts` authors a menu scroll or bounce: a
|
||||
image using this API. `IconGrid` (app/ui/icon-grid.ts) animates a whole grid
|
||||
this way: a clipped CLEAR of the grid box, the sliding highlight, then a DRAWS
|
||||
of every row visible during the scroll, offset by the scroll expression. `menu-scroll-list.ts` authors a menu scroll or bounce: a
|
||||
viewport-sized copy from a strip resource whose source y animates, then the
|
||||
highlight. The bounce curve (`app/ui/menu-scroll-motion.ts`) needs no branches:
|
||||
the easing ops clamp their input to 0..1, so `ease(t / p)` holds at 1 after the
|
||||
@@ -80,9 +100,13 @@ TypeScript. There is no animated-menu tag or menu animation policy in Kotlin.
|
||||
Tag 7 is followed by a little-endian u32 byte length and a bounded body:
|
||||
placement (i16 x/y, u16 width/height, i8 depth), timeline (u32 token/elapsed),
|
||||
u16 resource count, gray8 images (u16 width/height followed by pixels), u16 call
|
||||
count, then calls. Calls start with the firmware opcode and an i16 relative depth;
|
||||
operands otherwise use their fixed-width fields, except rounded x/y and rect-copy
|
||||
x/y/dx/dy which use extended varints. Expressions use the firmware bytecode plus bridge-only
|
||||
count, then calls. Calls start with the opcode (bit 7 set when a clip follows)
|
||||
and an i16 relative depth, then the clip if any (i16 x/y, u16 width/height);
|
||||
operands otherwise use their fixed-width fields, except rounded x/y, rect-copy
|
||||
x/y/dx/dy and DRAWS x/y which use extended varints. CLEAR is a u8 color. DRAWS
|
||||
(opcode 32) is followed by a u16 record count and the records, in the frame
|
||||
draw-buffer formats: glyph `[0][fontId u16][encoding u32][penX s16][lineY s16][value u8]`
|
||||
and image `[1][imageId u32][x s16][y s16]`. Expressions use the firmware bytecode plus bridge-only
|
||||
`ELAPSED = 128`. Resource indices are local to this record, never firmware IDs.
|
||||
Rect-copy source x/y are signed here to permit relative SCREEN coordinates.
|
||||
|
||||
|
||||
@@ -1,15 +1,31 @@
|
||||
import { DrawBytes, encodeValue, evaluate, integer, readValue, type DrawValue } from './draw-expression';
|
||||
import type { GrayImage } from './image';
|
||||
|
||||
/** Local resources are assigned firmware cache IDs by Kotlin. SCREEN reads the uncomposed frame. */
|
||||
export const SCREEN = 65535;
|
||||
export const DrawOp = { RECT_COPY: 2, IMAGE: 4, ROUNDED_RECT: 8 } as const;
|
||||
/**
|
||||
* Firmware opcodes, plus DRAWS: a bridge-only call that replays glyph and icon
|
||||
* draws (from glyph-wire's encodeReplayDraws) as firmware image/text calls.
|
||||
*/
|
||||
export const DrawOp = { RECT_COPY: 2, IMAGE: 4, ROUNDED_RECT: 8, CLEAR: 9, DRAWS: 32 } as const;
|
||||
export const DISPLAY_LIST_RECORD = 7;
|
||||
/** Bridge opcode bit: a clip rect follows the call's depth. */
|
||||
const CLIPPED = 128;
|
||||
export type ListImage = { readonly width: number; readonly height: number; readonly pixels: Uint8Array };
|
||||
/** Revision 35: a call draws only inside its clip, in list coordinates (and moves with its depth). */
|
||||
export type ListClip = { readonly x: number; readonly y: number; readonly width: number; readonly height: number };
|
||||
export type ListCall = (
|
||||
| { op: typeof DrawOp.ROUNDED_RECT; x: DrawValue; y: DrawValue; width: number; height: number; radius: number; background: number; border: number }
|
||||
| { op: typeof DrawOp.IMAGE; resource: number; x: number; y: number; transparent: boolean }
|
||||
| { op: typeof DrawOp.RECT_COPY; resource: number; x: DrawValue; y: DrawValue; width: number; height: number; dx: DrawValue; dy: DrawValue }
|
||||
) & { depth?: number };
|
||||
/** Fill with a 4-bit color: the clip rect when there is one, else the whole screen. */
|
||||
| { op: typeof DrawOp.CLEAR; color: number }
|
||||
/**
|
||||
* Glyph and icon draws offset by (x, y). `records`/`count` are the wire form;
|
||||
* `source` holds the same draws for software painting (a decoded list has none).
|
||||
*/
|
||||
| { op: typeof DrawOp.DRAWS; x: DrawValue; y: DrawValue; records: Uint8Array; count: number; source?: GrayImage }
|
||||
) & { depth?: number; clip?: ListClip };
|
||||
export type DisplayList = {
|
||||
readonly resources: readonly ListImage[];
|
||||
readonly calls: readonly ListCall[];
|
||||
@@ -45,12 +61,18 @@ export function encodeDisplayList(placed: PlacedDisplayList, now = Date.now()):
|
||||
}
|
||||
integer(list.calls.length, 0, 4096); out.u16(list.calls.length);
|
||||
for (const call of list.calls) {
|
||||
out.u8(call.op); out.i16(call.depth ?? 0);
|
||||
out.u8(call.op | (call.clip ? CLIPPED : 0)); out.i16(call.depth ?? 0);
|
||||
integer(placed.depth + (call.depth ?? 0), -128, 127);
|
||||
if (call.clip) { out.i16(call.clip.x); out.i16(call.clip.y); out.u16(call.clip.width); out.u16(call.clip.height); }
|
||||
if (call.op === DrawOp.ROUNDED_RECT) {
|
||||
out.raw(encodeValue(call.x)); out.raw(encodeValue(call.y));
|
||||
out.u16(integer(call.width, 1, 640)); out.u16(integer(call.height, 1, 480)); out.u16(call.radius);
|
||||
out.u8(integer(call.background, 0, 15)); out.u8(integer(call.border, 0, 16));
|
||||
} else if (call.op === DrawOp.CLEAR) {
|
||||
out.u8(integer(call.color, 0, 15));
|
||||
} else if (call.op === DrawOp.DRAWS) {
|
||||
out.raw(encodeValue(call.x)); out.raw(encodeValue(call.y));
|
||||
out.u16(call.count); out.raw(call.records);
|
||||
} else {
|
||||
if (call.resource !== SCREEN) integer(call.resource, 0, list.resources.length - 1);
|
||||
if (call.op === DrawOp.RECT_COPY && call.resource !== SCREEN && [call.x, call.y].some(v => typeof v === 'number' && v < 0)) throw new Error('Negative image source coordinate');
|
||||
@@ -68,6 +90,9 @@ export function encodeDisplayList(placed: PlacedDisplayList, now = Date.now()):
|
||||
return Uint8Array.from(header.bytes);
|
||||
}
|
||||
|
||||
/** Replay-record sizes by tag, as in glyph-wire's frame draw buffer. */
|
||||
const RECORD_BYTES: Record<number, number> = { 0: 12, 1: 9 };
|
||||
|
||||
export function readDisplayList(bytes: Uint8Array, offset: number, now = Date.now()): { placed: PlacedDisplayList; end: number } {
|
||||
const envelope = new DrawBytes(bytes.subarray(offset));
|
||||
if (envelope.u8() !== DISPLAY_LIST_RECORD) throw new Error('Invalid display-list record');
|
||||
@@ -79,11 +104,22 @@ export function readDisplayList(bytes: Uint8Array, offset: number, now = Date.no
|
||||
for (let i = 0; i < count; i++) { const width = r.u16(), height = r.u16(); resources.push({ width, height, pixels: r.take(width * height) }); }
|
||||
const calls: ListCall[] = [], callCount = r.u16(); integer(callCount, 0, 4096);
|
||||
for (let i = 0; i < callCount; i++) {
|
||||
const op = r.u8(), depth = r.i16();
|
||||
if (op === DrawOp.ROUNDED_RECT) calls.push({ op, depth, x: readValue(r), y: readValue(r), width: r.u16(), height: r.u16(), radius: r.u16(), background: r.u8(), border: r.u8() });
|
||||
else if (op === DrawOp.IMAGE) calls.push({ op, depth, resource: r.u16(), x: r.i16(), y: r.i16(), transparent: (r.u8() & 16) !== 0 });
|
||||
else if (op === DrawOp.RECT_COPY) calls.push({ op, depth, resource: r.u16(), x: readValue(r), y: readValue(r), width: r.u16(), height: r.u16(), dx: readValue(r), dy: readValue(r) });
|
||||
else throw new Error('Unsupported display-list call');
|
||||
const header = r.u8(), op = header & ~CLIPPED, depth = r.i16();
|
||||
const clip = header & CLIPPED ? { x: r.i16(), y: r.i16(), width: r.u16(), height: r.u16() } : undefined;
|
||||
const common = clip ? { depth, clip } : { depth };
|
||||
if (op === DrawOp.ROUNDED_RECT) calls.push({ op, ...common, x: readValue(r), y: readValue(r), width: r.u16(), height: r.u16(), radius: r.u16(), background: r.u8(), border: r.u8() });
|
||||
else if (op === DrawOp.IMAGE) calls.push({ op, ...common, resource: r.u16(), x: r.i16(), y: r.i16(), transparent: (r.u8() & 16) !== 0 });
|
||||
else if (op === DrawOp.RECT_COPY) calls.push({ op, ...common, resource: r.u16(), x: readValue(r), y: readValue(r), width: r.u16(), height: r.u16(), dx: readValue(r), dy: readValue(r) });
|
||||
else if (op === DrawOp.CLEAR) calls.push({ op, ...common, color: r.u8() });
|
||||
else if (op === DrawOp.DRAWS) {
|
||||
const x = readValue(r), y = readValue(r), records = r.u16(), start = r.offset;
|
||||
for (let n = 0; n < records; n++) {
|
||||
const size = RECORD_BYTES[r.bytes[r.offset]!];
|
||||
if (!size) throw new Error('Unsupported display-list draw record');
|
||||
r.take(size);
|
||||
}
|
||||
calls.push({ op, ...common, x, y, count: records, records: r.bytes.slice(start, r.offset) });
|
||||
} else throw new Error('Unsupported display-list call');
|
||||
}
|
||||
if (r.offset !== length) throw new Error('Trailing display-list bytes');
|
||||
const placed = { x, y, width, height, depth, displayList: { resources, calls, presentedAt: now, timeline: { token, startedAt: now - elapsed } } };
|
||||
@@ -92,13 +128,23 @@ export function readDisplayList(bytes: Uint8Array, offset: number, now = Date.no
|
||||
return { placed, end: offset + envelope.offset };
|
||||
}
|
||||
|
||||
/**
|
||||
* The list at `factor` brightness. Replayed icons cannot be dimmed on the
|
||||
* glasses, so a list with DRAWS calls becomes empty instead: the dimmed frame
|
||||
* underneath then shows its final state.
|
||||
*/
|
||||
export function dimDisplayList(list: DisplayList, factor: number): DisplayList {
|
||||
if (list.calls.some(c => c.op === DrawOp.DRAWS)) return { ...list, calls: [] };
|
||||
const dim = (v: number) => v === 0 ? 0 : Math.max(1, Math.round(v * factor));
|
||||
return { ...list, resources: list.resources.map(r => ({ ...r, pixels: r.pixels.map(dim) })),
|
||||
calls: list.calls.map(c => c.op === DrawOp.ROUNDED_RECT ? { ...c, background: Math.min(15, Math.round(c.background * factor)), border: c.border === 16 ? 16 : Math.min(15, Math.round(c.border * factor)) } : c) };
|
||||
calls: list.calls.map(c => c.op === DrawOp.ROUNDED_RECT ? { ...c, background: Math.min(15, Math.round(c.background * factor)), border: c.border === 16 ? 16 : Math.min(15, Math.round(c.border * factor)) }
|
||||
: c.op === DrawOp.CLEAR ? { ...c, color: Math.min(15, Math.round(c.color * factor)) } : c) };
|
||||
}
|
||||
|
||||
/** Fallback compositor; native preview and glasses replay the compiled list with their existing timers. */
|
||||
/**
|
||||
* Fallback compositor; native preview and glasses replay the compiled list with their existing timers.
|
||||
* A DRAWS call paints from its `source`, so a decoded list skips them.
|
||||
*/
|
||||
export function paintDisplayList(output: Uint8Array, screen: Uint8Array, width: number, height: number, placed: PlacedDisplayList, right = false, now = Date.now()): void {
|
||||
const q = (n: number) => Math.min(15, (n + 8) >> 4) * 16;
|
||||
const list = placed.displayList;
|
||||
@@ -112,10 +158,20 @@ export function paintDisplayList(output: Uint8Array, screen: Uint8Array, width:
|
||||
};
|
||||
for (const c of placed.displayList.calls) {
|
||||
const depth = placed.depth + (c.depth ?? 0), shift = right ? -Math.floor((depth + 1) / 2) : Math.floor(depth / 2);
|
||||
if (c.op === DrawOp.ROUNDED_RECT) {
|
||||
// Writable region in screen pixels: the clip moves with the call's depth, like its content.
|
||||
const clip = c.clip ? { x: placed.x + c.clip.x + shift, y: placed.y + c.clip.y, width: c.clip.width, height: c.clip.height }
|
||||
: { x: 0, y: 0, width, height };
|
||||
const writable = (x: number, y: number) => x >= 0 && y >= 0 && x < width && y < height &&
|
||||
x >= clip.x && y >= clip.y && x < clip.x + clip.width && y < clip.y + clip.height;
|
||||
if (c.op === DrawOp.CLEAR) {
|
||||
for (let y = 0; y < height; y++) for (let x = 0; x < width; x++) if (writable(x, y)) output[y * width + x] = c.color * 16;
|
||||
} else if (c.op === DrawOp.DRAWS) {
|
||||
const at = (v: DrawValue) => evaluate(v, elapsed, presentTime).value;
|
||||
c.source?.paintReplayDraws(output, width, height, placed.x + at(c.x) + shift, placed.y + at(c.y), clip);
|
||||
} else if (c.op === DrawOp.ROUNDED_RECT) {
|
||||
const x = placed.x + evaluate(c.x, elapsed, presentTime).value + shift, y = placed.y + evaluate(c.y, elapsed, presentTime).value;
|
||||
for (let yy = 0; yy < c.height; yy++) for (let xx = 0; xx < c.width; xx++) {
|
||||
if (x + xx < 0 || y + yy < 0 || x + xx >= width || y + yy >= height || !inside(xx, yy, c.width, c.height, c.radius)) continue;
|
||||
if (!writable(x + xx, y + yy) || !inside(xx, yy, c.width, c.height, c.radius)) continue;
|
||||
const p = (y + yy) * width + x + xx;
|
||||
output[p] = c.border !== 16 && !inside(xx - 1, yy - 1, c.width - 2, c.height - 2, Math.max(0, c.radius - 1)) ? c.border * 16 : Math.max(output[p], c.background * 16);
|
||||
}
|
||||
@@ -126,7 +182,7 @@ export function paintDisplayList(output: Uint8Array, screen: Uint8Array, width:
|
||||
const sx = c.op === DrawOp.IMAGE ? 0 : at(c.x) + (c.resource === SCREEN ? placed.x : 0), sy = c.op === DrawOp.IMAGE ? 0 : at(c.y) + (c.resource === SCREEN ? placed.y : 0);
|
||||
const x = placed.x + (c.op === DrawOp.IMAGE ? c.x : at(c.dx)) + shift, y = placed.y + (c.op === DrawOp.IMAGE ? c.y : at(c.dy));
|
||||
for (let yy = 0; yy < h; yy++) for (let xx = 0; xx < w; xx++) {
|
||||
if (x + xx < 0 || y + yy < 0 || x + xx >= width || y + yy >= height || sx + xx < 0 || sy + yy < 0 || sx + xx >= source.width || sy + yy >= source.height) continue;
|
||||
if (!writable(x + xx, y + yy) || sx + xx < 0 || sy + yy < 0 || sx + xx >= source.width || sy + yy >= source.height) continue;
|
||||
const v = q(source.pixels[(sy + yy) * source.width + sx + xx]);
|
||||
if (c.op !== DrawOp.IMAGE || !c.transparent || v !== 0) output[(y + yy) * width + x + xx] = v;
|
||||
}
|
||||
|
||||
+87
-28
@@ -21,7 +21,7 @@ import { DrawRecordKind, encodePresentation } from "./presentation-wire";
|
||||
* only means "no wire savings for this draw".
|
||||
*/
|
||||
import { Glyph } from "./bdffont";
|
||||
import { GrayImage, type DeferredDraw, type GlyphFont, type PlacedFwText, type PlacedImage } from "./image";
|
||||
import { GrayImage, type DeferredDraw, type GlyphFont, type PlacedFwText, type PlacedGlyph, type PlacedImage } from "./image";
|
||||
|
||||
import { textureAtlasAvailable, textureFontId, registerTextureGlyphs, registerFirmwareGlyphs, textureImageId } from "../native/texture-atlas";
|
||||
|
||||
@@ -167,15 +167,7 @@ export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer |
|
||||
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: [], aaGlyphs: [] };
|
||||
registration.set(state, group);
|
||||
}
|
||||
(placed.glyph.coverage ? group.aaGlyphs : group.glyphs).push(placed.glyph);
|
||||
registration = queueRegistration(registration, state, placed);
|
||||
} else if (placed.kind === "image") {
|
||||
if (!inRange16(placed.x) || !inRange16(placed.y)) continue;
|
||||
if (placed.presentation) { const record = encodePresentation(placed); presentations.set(placed, record); bytes += record.length; }
|
||||
@@ -186,12 +178,7 @@ export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer |
|
||||
if (ok) bytes += FWTEXT_HEADER_BYTES + placed.glyphs.length * FWTEXT_MEMBER_BYTES;
|
||||
}
|
||||
}
|
||||
if (registration) {
|
||||
const plainBuffer = buildRegistrationBuffer(registration);
|
||||
if (plainBuffer) registerTextureGlyphs(plainBuffer, false);
|
||||
const aaBuffer = buildAaRegistrationBuffer(registration);
|
||||
if (aaBuffer) registerTextureGlyphs(aaBuffer, true);
|
||||
}
|
||||
registerQueued(registration);
|
||||
if (bytes === 0) return null;
|
||||
|
||||
// Pass 2: the frame's draw records.
|
||||
@@ -202,23 +189,13 @@ export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer |
|
||||
if (!inRange16(placed.x) || !inRange16(placed.y)) continue;
|
||||
const state = fontWireState(placed.font);
|
||||
if (!state || !representableGlyph(placed.font, placed.glyph)) continue;
|
||||
out.setUint8(offset, DrawRecordKind.GLYPH);
|
||||
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;
|
||||
offset = writeGlyphRecord(out, offset, state, placed);
|
||||
} else if (placed.kind === "image") {
|
||||
if (!inRange16(placed.x) || !inRange16(placed.y)) continue;
|
||||
if (placed.presentation) { const bytes = presentations.get(placed)!; new Uint8Array(out.buffer).set(bytes, offset); offset += bytes.length; continue; }
|
||||
const id = imageId(placed);
|
||||
if (id === null) continue;
|
||||
out.setUint8(offset, DrawRecordKind.TEXTURE_IMAGE);
|
||||
out.setUint32(offset + 1, id, true);
|
||||
out.setInt16(offset + 5, placed.x, true);
|
||||
out.setInt16(offset + 7, placed.y, true);
|
||||
offset += IMAGE_RECORD_BYTES;
|
||||
offset = writeImageRecord(out, offset, id, placed);
|
||||
} else {
|
||||
if (!fwRunOk.get(placed)) continue;
|
||||
out.setUint8(offset, DrawRecordKind.FIRMWARE_TEXT);
|
||||
@@ -238,6 +215,88 @@ export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer |
|
||||
return out.buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode draws for replay inside a display list (DrawOp.DRAWS), registering
|
||||
* unseen glyph rasters and images as prepareFrameDraws does, in its glyph and
|
||||
* image record formats. Null unless every draw is a replayable glyph or plain
|
||||
* image: a list must reproduce all of its content or none of it.
|
||||
*/
|
||||
export function encodeReplayDraws(draws: readonly DeferredDraw[]): { records: Uint8Array; count: number } | null {
|
||||
if (!textureAtlasAvailable()) return null;
|
||||
let bytes = 0;
|
||||
for (const placed of draws) {
|
||||
if (!inRange16(placed.x) || !inRange16(placed.y)) return null;
|
||||
if (placed.kind === "glyph") {
|
||||
if (!fontWireState(placed.font) || !representableGlyph(placed.font, placed.glyph)) return null;
|
||||
bytes += GLYPH_RECORD_BYTES;
|
||||
} else if (placed.kind === "image" && !placed.presentation && imageId(placed) !== null) {
|
||||
bytes += IMAGE_RECORD_BYTES;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
let registration: Map<FontWireState, RegistrationGroup> | null = null;
|
||||
const out = new DataView(new ArrayBuffer(bytes));
|
||||
let offset = 0;
|
||||
for (const placed of draws) {
|
||||
if (placed.kind === "glyph") {
|
||||
const state = fontWireState(placed.font)!;
|
||||
registration = queueRegistration(registration, state, placed);
|
||||
offset = writeGlyphRecord(out, offset, state, placed);
|
||||
} else if (placed.kind === "image") {
|
||||
offset = writeImageRecord(out, offset, imageId(placed)!, placed);
|
||||
}
|
||||
}
|
||||
registerQueued(registration);
|
||||
return { records: new Uint8Array(out.buffer), count: draws.length };
|
||||
}
|
||||
|
||||
/** [0][fontId u16][encoding u32][penX s16][lineY s16][value u8] */
|
||||
function writeGlyphRecord(out: DataView, offset: number, state: FontWireState, placed: PlacedGlyph): number {
|
||||
out.setUint8(offset, DrawRecordKind.GLYPH);
|
||||
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);
|
||||
return offset + GLYPH_RECORD_BYTES;
|
||||
}
|
||||
|
||||
/** [1][imageId u32][x s16][y s16] */
|
||||
function writeImageRecord(out: DataView, offset: number, id: number, placed: PlacedImage): number {
|
||||
out.setUint8(offset, DrawRecordKind.TEXTURE_IMAGE);
|
||||
out.setUint32(offset + 1, id, true);
|
||||
out.setInt16(offset + 5, placed.x, true);
|
||||
out.setInt16(offset + 7, placed.y, true);
|
||||
return offset + IMAGE_RECORD_BYTES;
|
||||
}
|
||||
|
||||
/** Queue a glyph raster for registerQueued unless this JS context already registered it. */
|
||||
function queueRegistration(
|
||||
registration: Map<FontWireState, RegistrationGroup> | null,
|
||||
state: FontWireState,
|
||||
placed: PlacedGlyph,
|
||||
): Map<FontWireState, RegistrationGroup> | null {
|
||||
if (state.registered.has(placed.glyph.encoding)) return registration;
|
||||
state.registered.add(placed.glyph.encoding);
|
||||
registration ??= new Map();
|
||||
let group = registration.get(state);
|
||||
if (!group) {
|
||||
group = { font: placed.font, glyphs: [], aaGlyphs: [] };
|
||||
registration.set(state, group);
|
||||
}
|
||||
(placed.glyph.coverage ? group.aaGlyphs : group.glyphs).push(placed.glyph);
|
||||
return registration;
|
||||
}
|
||||
|
||||
function registerQueued(registration: Map<FontWireState, RegistrationGroup> | null): void {
|
||||
if (!registration) return;
|
||||
const plainBuffer = buildRegistrationBuffer(registration);
|
||||
if (plainBuffer) registerTextureGlyphs(plainBuffer, false);
|
||||
const aaBuffer = buildAaRegistrationBuffer(registration);
|
||||
if (aaBuffer) registerTextureGlyphs(aaBuffer, true);
|
||||
}
|
||||
|
||||
type RegistrationGroup = {
|
||||
font: GlyphFont;
|
||||
/** 1bpp (BDF) glyphs for GlyphAtlas.register. */
|
||||
|
||||
+32
-1
@@ -517,6 +517,34 @@ export class GrayImage {
|
||||
presentation: { radius, background, border, depth, displayList: menuSelectionList(baked, background, border, radius, animation) } });
|
||||
}
|
||||
|
||||
/**
|
||||
* Paint this image's glyph and image draws onto a gray8 buffer at (dx, dy),
|
||||
* inside `clip`, the way the glasses replay them from a display list (see
|
||||
* DrawOp.DRAWS): glyphs firmware-exact, images skipping pixels whose 4-bit
|
||||
* level is 0. Raster pixels and other draw kinds are not painted.
|
||||
*/
|
||||
paintReplayDraws(pixels: Uint8Array, width: number, height: number, dx: number, dy: number,
|
||||
clip: { x: number; y: number; width: number; height: number }): void {
|
||||
const left = Math.max(0, clip.x), top = Math.max(0, clip.y);
|
||||
const right = Math.min(width, clip.x + clip.width), bottom = Math.min(height, clip.y + clip.height);
|
||||
const sink: PixelSink = {
|
||||
setPixel: (x, y, value) => {
|
||||
if (x >= left && y >= top && x < right && y < bottom) pixels[y * width + x] = clampByte(value);
|
||||
},
|
||||
};
|
||||
for (const placed of this.drawList) {
|
||||
if (placed.kind === "glyph") {
|
||||
rasterizeGlyph(sink, placed.font, placed.glyph, placed.x + dx, placed.y + dy, placed.value);
|
||||
} else if (placed.kind === "image" && !placed.presentation) {
|
||||
const source = placed.source;
|
||||
for (let y = 0; y < source.height; y++) for (let x = 0; x < source.width; x++) {
|
||||
const level = grayToNibble(source.pixels[y * source.width + x]!);
|
||||
if (level) sink.setPixel(placed.x + dx + x, placed.y + dy + y, level * 16);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Submit a retained list alongside this frame; coordinates are relative to x/y. */
|
||||
drawDisplayList(displayList: DisplayList, x: number, y: number, width: number, height: number, depth = 0): void {
|
||||
this.drawList.push({ kind: "image", source: new GrayImage(width, height), x, y,
|
||||
@@ -661,9 +689,12 @@ export function grayToNibble(value: number): number {
|
||||
return value <= 0 ? 0 : Math.min(15, (value + 8) >> 4);
|
||||
}
|
||||
|
||||
/** Where rasterizeGlyph writes: an image, or a clipped buffer (paintReplayDraws). */
|
||||
type PixelSink = { setPixel(x: number, y: number, value: number): void };
|
||||
|
||||
/** Bake one glyph into an image's pixel buffer (y is the top of the line). */
|
||||
function rasterizeGlyph(
|
||||
target: GrayImage,
|
||||
target: PixelSink,
|
||||
font: GlyphFont,
|
||||
glyph: Glyph,
|
||||
x: number,
|
||||
|
||||
@@ -18,9 +18,14 @@ export type MenuScrollHighlight = {
|
||||
* started at `timelineStart` (the slide may have started before or after it).
|
||||
*/
|
||||
export function slidingHighlightY(y: number, motion: MenuHighlightAnimation | undefined, timelineStart: number): E<'i32'> {
|
||||
if (!motion?.dy) return E.i32(y);
|
||||
return slidingCoordinate(y, motion?.dy ?? 0, motion, timelineStart);
|
||||
}
|
||||
|
||||
/** One coordinate of a highlight sliding by `delta` to rest at `value`; see slidingHighlightY. */
|
||||
export function slidingCoordinate(value: number, delta: number, motion: MenuHighlightAnimation | undefined, timelineStart: number): E<'i32'> {
|
||||
if (!motion || !delta) return E.i32(value);
|
||||
return E.progress(motion.durationMs, motion.startedAt - timelineStart).ease()
|
||||
.lerp(E.i32(y + motion.dy).toFloat(), E.i32(y).toFloat()).toInt();
|
||||
.lerp(E.i32(value + delta).toFloat(), E.i32(value).toFloat()).toInt();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user