Files
faceclaw/app/apps/evenhub/compositor.ts
T
James Babcock c713815694 EvenHub: Fix an issue where indentation was incorrectly dropped in text containers
This caused text to overlap with icons in Iris Pro and Ocuclaw
2026-11-07 20:57:55 -08:00

122 lines
5.0 KiB
TypeScript

/**
* 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. Extended layouts may pick other fonts per container
* (fonts.ts), which wrap with Faceclaw's own text wrapping.
*
* Known deviations from stock, acceptable for now:
* - List rendering is a plain vertical list. A non-zero itemWidth sets the
* selection outline's width and truncates long items to fit, as on stock;
* whether stock also lays narrow items out side by side is unknown.
* - Lists scroll with our menus' animation and bounce (see list-menu.ts).
*/
import { GrayImage } from "../../graphics/image";
import { containerFace } from "./fonts";
import {
paintOrder,
type EvenHubImageContainer,
type EvenHubListContainer,
type EvenHubPage,
type EvenHubTextContainer,
} from "./containers";
import { paintListItems } from "./list-menu";
export const EVENHUB_SCREEN_WIDTH = 576;
export const EVENHUB_SCREEN_HEIGHT = 288;
const TEXT_WHITE = 255;
/**
* SDK 0.0.14 textColor levels 0..4 as grey values. Level 4 is the default and
* the only one apps got before 0.0.14, so it must stay exactly TEXT_WHITE.
* Level 0 is the firmware's "fully dim" step, which over the black page
* background means the text is effectively not shown.
*/
const TEXT_BRIGHTNESS_GREYS = [0, 64, 128, 191, 255];
function textGrey(level: number | undefined): number {
return level === undefined ? TEXT_WHITE : (TEXT_BRIGHTNESS_GREYS[level] ?? TEXT_WHITE);
}
/** Chars the font lacks are skipped silently on stock (no tofu); EvenHubFont
* skips unknown glyphs, matching that. */
function paintTextContainer(image: GrayImage, container: EvenHubTextContainer): void {
// Only the text dims: textColor is a text-brightness level, and stock draws
// the container's border at full brightness regardless.
paintBorder(image, container);
const inset = container.borderWidth + container.paddingLength;
const face = containerFace(container.font);
const lines = face.wrap(container.content, Math.max(1, container.width - 2 * inset));
for (let i = 0; i < lines.length; i++) {
face.drawText(image, container.x + inset, container.y + inset + i * face.lineHeight, lines[i]!, textGrey(container.textColor));
}
}
function paintBorder(
image: GrayImage,
container: { x: number; y: number; width: number; height: number; borderWidth: number; borderRadius: number },
): void {
for (let i = 0; i < container.borderWidth; i++) {
if (container.borderRadius > 0) {
image.drawRoundedRect(
container.x + i,
container.y + i,
container.width - 2 * i,
container.height - 2 * i,
TEXT_WHITE,
Math.max(0, container.borderRadius - i),
);
} else {
image.drawRect(container.x + i, container.y + i, container.width - 2 * i, container.height - 2 * i, TEXT_WHITE);
}
}
}
function paintImageContainer(image: GrayImage, container: EvenHubImageContainer): void {
if (!container.pixels || container.pixelsWidth <= 0 || container.pixelsHeight <= 0) return;
const source = new GrayImage(container.pixelsWidth, container.pixelsHeight, 0);
source.pixels.set(container.pixels);
// Stock quirk: raw data smaller than the container tiles/repeats to fill it.
for (let ty = 0; ty < container.height; ty += container.pixelsHeight) {
for (let tx = 0; tx < container.width; tx += container.pixelsWidth) {
image.bitBlt(source, container.x + tx, container.y + ty, {
width: Math.min(container.pixelsWidth, container.width - tx),
height: Math.min(container.pixelsHeight, container.height - ty),
});
}
}
}
function paintListContainer(image: GrayImage, container: EvenHubListContainer, focused: boolean): void {
paintBorder(image, container);
paintListItems(image, container, containerFace(container.font), focused);
}
/** 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);
if (!page) return image;
for (const container of paintOrder(page.containers)) {
switch (container.kind) {
case "image":
// Text painted so far is deferred draws (firmware-text runs), which
// would otherwise render above this container's raster. Stock puts a
// later image container OVER earlier text, so bake the pending draws
// first; text painted after this container stays deferred (and above).
image.bakeDeferredDrawsInPlace();
paintImageContainer(image, container);
break;
case "text":
paintTextContainer(image, container);
break;
case "list":
paintListContainer(image, container, focused);
break;
}
}
return image;
}