diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.java index 994e9231..d6d6ae06 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.java @@ -2248,6 +2248,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { + " fw=" + tex.fwGlyphs + "/" + tex.fwRuns + " baked=" + tex.bakedCandidates + (tex.uploadBytes > 0 ? " upload=" + tex.uploadBytes + "B" : "") + + " cache=" + textureCache.usedBytes() + "B" + " payload=" + tex.payload.length + "B"; FrameTimings.getInstance().log(frameId, texLog); finishEnqueueDesiredImageLocked(plan, fingerprint, paintMs, frameId); diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FontFileRenderer.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FontFileRenderer.java index 6338fc2d..0eca05e5 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FontFileRenderer.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FontFileRenderer.java @@ -174,6 +174,132 @@ public final class FontFileRenderer { } } + /** + * Render one codepoint as a tight-ink-box antialiased glyph cell for the + * texture-cache text pipeline (see app/graphics/ttf-font.ts). Ligatures + * are disabled so a per-codepoint raster plus pairwise kerning (measured + * via measureTextExact) fully describes a line. Little-endian packet: + * [advance u16, 26.6 fixed point] + * [bearingX s16] ink left relative to the pen position + * [inkTop s16] ink top relative to the line top (ascent above baseline) + * [width u16][height u16] + * [width*height coverage bytes, gamma-mapped] + * An ink-free glyph (space) has width = height = 0 but a valid advance. + * Empty array when the font cannot be loaded or the render fails. + */ + public static byte[] renderGlyphCell(String path, float sizePx, int codePoint, double gamma) { + Typeface typeface = loadTypeface(path); + if (typeface == null || sizePx <= 0 || codePoint < 0 || codePoint > 0x10ffff) { + return new byte[0]; + } + try { + Paint paint = glyphPaint(typeface, sizePx); + String text = new String(Character.toChars(codePoint)); + float advance = paint.measureText(text); + int advanceFixed = Math.max(0, Math.min(0xffff, Math.round(advance * 64f))); + + android.graphics.Rect bounds = new android.graphics.Rect(); + paint.getTextBounds(text, 0, text.length(), bounds); + if (bounds.isEmpty()) { + return glyphCellPacket(advanceFixed, 0, 0, 0, 0, null); + } + // getTextBounds can be off by a hair on AA edges; render with + // padding, then trim to the actually painted box. + int pad = 2; + int bw = bounds.width() + 2 * pad; + int bh = bounds.height() + 2 * pad; + if (bw <= 0 || bh <= 0 || bw * bh > 1_000_000) { + return new byte[0]; + } + int penX = pad - bounds.left; + int baselineY = pad - bounds.top; + Bitmap bitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ALPHA_8); + Canvas canvas = new Canvas(bitmap); + canvas.drawText(text, penX, baselineY, paint); + byte[] pixels = alpha8Pixels(bitmap, bw, bh); + bitmap.recycle(); + + int x0 = bw, x1 = -1, y0 = bh, y1 = -1; + for (int y = 0; y < bh; y++) { + int row = y * bw; + for (int x = 0; x < bw; x++) { + if (pixels[row + x] != 0) { + if (x < x0) x0 = x; + if (x > x1) x1 = x; + if (y < y0) y0 = y; + if (y > y1) y1 = y; + } + } + } + if (x1 < x0) { + return glyphCellPacket(advanceFixed, 0, 0, 0, 0, null); + } + int width = x1 - x0 + 1; + int height = y1 - y0 + 1; + Paint.FontMetricsInt fm = paint.getFontMetricsInt(); + int bearingX = x0 - penX; + int inkTop = (-fm.ascent) + (y0 - baselineY); + + byte[] lut = gammaLut(gamma); + byte[] coverage = new byte[width * height]; + for (int y = 0; y < height; y++) { + int src = (y0 + y) * bw + x0; + int dst = y * width; + for (int x = 0; x < width; x++) { + coverage[dst + x] = lut[pixels[src + x] & 0xff]; + } + } + return glyphCellPacket(advanceFixed, bearingX, inkTop, width, height, coverage); + } catch (Exception | OutOfMemoryError e) { + Log.w(TAG, "glyph render failed: " + path + " U+" + Integer.toHexString(codePoint), e); + return new byte[0]; + } + } + + /** + * Exact (float) advance width of a text run, with the same paint setup as + * renderGlyphCell (subpixel advances, ligatures off), so pair kerning can + * be derived as measure(ab) - measure(a) - measure(b). + */ + public static double measureTextExact(String path, String text, float sizePx) { + Typeface typeface = loadTypeface(path); + if (typeface == null || text == null || sizePx <= 0) { + return 0; + } + return glyphPaint(typeface, sizePx).measureText(text); + } + + /** Paint used by both the glyph-cell renderer and its measurements. */ + private static Paint glyphPaint(Typeface typeface, float sizePx) { + Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG); + paint.setTypeface(typeface); + paint.setTextSize(sizePx); + paint.setColor(Color.WHITE); + // Ligatures merge codepoints into one glyph, which the per-codepoint + // cache model cannot represent; kerning still applies. + paint.setFontFeatureSettings("'liga' off, 'clig' off"); + return paint; + } + + private static byte[] glyphCellPacket( + int advanceFixed, int bearingX, int inkTop, int width, int height, byte[] coverage) { + byte[] out = new byte[10 + (coverage == null ? 0 : coverage.length)]; + out[0] = (byte) (advanceFixed & 0xff); + out[1] = (byte) ((advanceFixed >> 8) & 0xff); + out[2] = (byte) (bearingX & 0xff); + out[3] = (byte) ((bearingX >> 8) & 0xff); + out[4] = (byte) (inkTop & 0xff); + out[5] = (byte) ((inkTop >> 8) & 0xff); + out[6] = (byte) (width & 0xff); + out[7] = (byte) ((width >> 8) & 0xff); + out[8] = (byte) (height & 0xff); + out[9] = (byte) ((height >> 8) & 0xff); + if (coverage != null) { + System.arraycopy(coverage, 0, out, 10, coverage.length); + } + return out; + } + /** * Line metrics for a font at a pixel size, as "ascent descent lineGap" * (integers, pixels). Empty string if the font cannot be loaded. diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/GlyphAtlas.java b/App_Resources/Android/src/main/java/com/faceclaw/app/GlyphAtlas.java index 0bbd5be1..c075c230 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/GlyphAtlas.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/GlyphAtlas.java @@ -15,8 +15,10 @@ import java.util.Map; * - as the ink mask for the "would this draw land correctly" check against * the composited frame, * - to punch the ink pixels out of the baked delta rect it replaces, - * - pre-encoded as the CFW cached-image bytes ([w][h][4bpp RLE], ink = 15) - * uploaded via mode 12 and drawn via mode 14 with a top-color LUT. + * - pre-encoded as the CFW cached-image bytes ([w][h][4bpp RLE]) uploaded + * via mode 12 and drawn via mode 14 with a top-color LUT. 1bpp (BDF) + * glyphs store ink at 15 so the LUT maps them to exactly the requested + * level; AA (TTF) glyphs store true coverage nibbles the LUT scales. * * Fonts are identified by a stable string key (the embedded font name), NOT a * per-JS-context counter: worker threads register independently and must agree @@ -44,36 +46,50 @@ public final class GlyphAtlas { public final int inkHeight; /** Horizontal bearing: draw x = pen x + bbxX. */ public final int bbxX; - /** Ink bitmap rows (inkHeight entries), bit (rowBitWidth-1-col) = ink. */ + /** 1bpp ink rows (inkHeight entries), bit (rowBitWidth-1-col) = ink; null for AA glyphs. */ final int[] rows; final int rowBitWidth; - /** CFW cached-image bytes: [w][cellHeight][RLE(w*cellHeight px, ink=15)]. */ + /** AA coverage, width*inkHeight nibble values 0..15; null for 1bpp glyphs. */ + final byte[] coverage; + /** CFW cached-image bytes: [w][cellHeight][RLE(w*cellHeight px)]. */ public final byte[] cachedBytes; - Glyph(int width, int cellHeight, int inkTop, int inkHeight, int bbxX, int[] rows) { + Glyph(int width, int cellHeight, int inkTop, int inkHeight, int bbxX, + int[] rows, byte[] coverage) { this.width = width; this.cellHeight = cellHeight; this.inkTop = inkTop; this.inkHeight = inkHeight; this.bbxX = bbxX; this.rows = rows; + this.coverage = coverage; this.rowBitWidth = ((width + 7) >> 3) << 3; this.cachedBytes = encodeCachedImage(); } - /** Whether the cell pixel at (col, row) is ink (row is cell-relative). */ - public boolean inkAt(int col, int row) { - if (col < 0 || col >= width) return false; + /** + * The cell pixel's 4bpp source value at (col, row), row cell-relative. + * 1bpp glyphs are stored at 15 so the draw-time LUT (source*top/15) + * maps them to exactly the requested top color; AA glyphs carry their + * true coverage level. + */ + public int nibbleAt(int col, int row) { + if (col < 0 || col >= width) return 0; int inkRow = row - inkTop; - if (inkRow < 0 || inkRow >= inkHeight) return false; - return ((rows[inkRow] >>> (rowBitWidth - 1 - col)) & 1) != 0; + if (inkRow < 0 || inkRow >= inkHeight) return 0; + if (coverage != null) return coverage[inkRow * width + col]; + return ((rows[inkRow] >>> (rowBitWidth - 1 - col)) & 1) != 0 ? 15 : 0; + } + + /** Whether the cell pixel at (col, row) has any ink (row is cell-relative). */ + public boolean inkAt(int col, int row) { + return nibbleAt(col, row) != 0; } /** * The firmware cached-image encoding: [width][height][RLE tokens] - * covering exactly width*cellHeight pixels (no row padding), ink pixels - * as color 15 so the draw-time LUT (top-color scaling) maps them to any - * requested 4bpp level. + * covering exactly width*cellHeight pixels (no row padding), each + * pixel's stored color being its nibbleAt value. */ private byte[] encodeCachedImage() { int total = width * cellHeight; @@ -84,9 +100,9 @@ public final class GlyphAtlas { int o = 2; int i = 0; while (i < total) { - int color = inkAt(i % width, i / width) ? 15 : 0; + int color = nibbleAt(i % width, i / width); int j = i + 1; - while (j < total && (inkAt(j % width, j / width) ? 15 : 0) == color) j++; + while (j < total && nibbleAt(j % width, j / width) == color) j++; int run = j - i; while (run > 0) { int c = Math.min(run, 0xffff); @@ -183,7 +199,66 @@ public final class GlyphAtlas { long k = key(id, encoding); if (!glyphs.containsKey(k) && width > 0 && cellHeight > 0 && inkTop + inkHeight <= cellHeight) { - glyphs.put(k, new Glyph(width, cellHeight, inkTop, inkHeight, bbxX, rows)); + glyphs.put(k, new Glyph(width, cellHeight, inkTop, inkHeight, bbxX, + rows, null)); + } + } + } + } + } + + /** + * Register a batch of antialiased (4bpp) glyph rasters, e.g. TTF renders. + * Same font-group framing as register(), but each glyph record carries + * packed coverage instead of 1bpp rows: + * [encoding u32][bbxX s8][inkTop u8][width u8][inkHeight u8] + * [inkHeight x ceil(width/2) packed bytes, high nibble = left pixel] + * Nibble values are the final 4bpp source levels (the draw-time LUT + * scales them by the top color). Already-registered (font, encoding) + * records are skipped, as in register(). + */ + public static void registerAa(ByteBuffer buffer) { + if (buffer == null) return; + ByteBuffer in = buffer.order(ByteOrder.LITTLE_ENDIAN); + synchronized (lock) { + while (in.remaining() > 0) { + int keyLen = in.get() & 0xff; + byte[] keyBytes = new byte[keyLen]; + in.get(keyBytes); + String fontKey = new String(keyBytes, StandardCharsets.UTF_8); + int cellHeight = in.get() & 0xff; + int count = in.getShort() & 0xffff; + Integer idBoxed = fontIds.get(fontKey); + int id; + if (idBoxed == null) { + id = nextFontId++; + fontIds.put(fontKey, id); + } else { + id = idBoxed; + } + for (int g = 0; g < count; g++) { + int encoding = in.getInt(); + int bbxX = in.get(); + int inkTop = in.get() & 0xff; + int width = in.get() & 0xff; + int inkHeight = in.get() & 0xff; + int stride = (width + 1) >> 1; + byte[] coverage = new byte[width * inkHeight]; + for (int r = 0; r < inkHeight; r++) { + for (int b = 0; b < stride; b++) { + int packed = in.get() & 0xff; + int col = b * 2; + coverage[r * width + col] = (byte) (packed >> 4); + if (col + 1 < width) { + coverage[r * width + col + 1] = (byte) (packed & 0x0f); + } + } + } + long k = key(id, encoding); + if (!glyphs.containsKey(k) && width > 0 && cellHeight > 0 + && inkTop + inkHeight <= cellHeight) { + glyphs.put(k, new Glyph(width, cellHeight, inkTop, inkHeight, bbxX, + null, coverage)); } } } diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/TextureCacheState.java b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/TextureCacheState.java index ecb951cc..0f0da3ee 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/TextureCacheState.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/TextureCacheState.java @@ -61,6 +61,15 @@ public final class TextureCacheState { return generation; } + /** + * Bytes currently allocated out of CACHE_SIZE, for logging/tuning (a + * sudden drop in the frame log means a reset-on-full re-upload cycle — + * the signal that the 64 KiB budget is being outgrown). + */ + public int usedBytes() { + return allocEnd; + } + /** The font's 96-entry table offset in the cache, or -1 when it exists but this glyph can't fit. */ public int fontTableOffset(int fontId) { Integer existing = fontTableOffsets.get(fontId); diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/TexturePlanner.java b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/TexturePlanner.java index eb9a1525..ba128478 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/TexturePlanner.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/TexturePlanner.java @@ -457,8 +457,10 @@ public final class TexturePlanner { } /** - * Whether every in-panel ink pixel of the glyph equals `top` in the packed - * new frame — i.e. the draw's writes all land correct. Ink outside the + * Whether every in-panel ink pixel of the glyph lands correct in the + * packed new frame: the composite's 4bpp value must equal the firmware's + * LUT output source*top/15 (integer). For 1bpp glyphs (source 15) this is + * exactly `== top`; AA glyphs get the general check. Ink outside the * panel is clipped by the firmware and irrelevant. */ private static boolean glyphMatchesComposite(byte[] packed, int stride, int width, int height, @@ -467,10 +469,11 @@ public final class TexturePlanner { int y = lineY + row; if (y < 0 || y >= height) continue; for (int col = 0; col < atlas.width; col++) { - if (!atlas.inkAt(col, row)) continue; + int source = atlas.nibbleAt(col, row); + if (source == 0) continue; int x = gx + col; if (x < 0 || x >= width) continue; - if (nibbleAt(packed, stride, x, y) != top) { + if (nibbleAt(packed, stride, x, y) != (source * top) / 15) { return false; } } diff --git a/app/apps/blocks/blocks-app.worker.ts b/app/apps/blocks/blocks-app.worker.ts index 293c6151..d8a7d6bf 100644 --- a/app/apps/blocks/blocks-app.worker.ts +++ b/app/apps/blocks/blocks-app.worker.ts @@ -11,7 +11,8 @@ import "@nativescript/core/globals"; import { GrayImage } from "../../graphics/image"; import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane"; import { prepareFrameDraws } from "../../graphics/glyph-wire"; -import { getDefaultSmallFont, getFont } from "../../graphics/bdffont"; +import { getFont } from "../../graphics/bdffont"; +import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import * as frameTimings from "../../native/frame-timings"; import type { DashboardInputEvent } from "../../ui/layers"; import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects"; diff --git a/app/apps/calendar/calendar.ts b/app/apps/calendar/calendar.ts index 47d76748..37ec3cba 100644 --- a/app/apps/calendar/calendar.ts +++ b/app/apps/calendar/calendar.ts @@ -1,5 +1,5 @@ -import { getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont"; -import { GrayImage } from "../../graphics/image"; +import { getDefaultSmallFont } from "../../graphics/ui-fonts"; +import { GrayImage, type UiFont } from "../../graphics/image"; import { wrapText, truncateText } from "../../graphics/textwrap"; import { clamp } from "../../util/numeric-util"; import { readUpcomingEvents, type CalendarEvent } from "../../native/calendar"; @@ -90,7 +90,7 @@ export class CalendarLayer implements Layer { } } - private paintPermissionPrompt(image: GrayImage, font: BdfFont, width: number, height: number): void { + private paintPermissionPrompt(image: GrayImage, font: UiFont, width: number, height: number): void { const message = "Grant calendar permission on your phone to see your events on the glasses."; const lines = wrapText(font, message, width - 48); for (let index = 0; index < lines.length; index++) { @@ -127,7 +127,7 @@ function buildEventRows(events: CalendarEvent[]): EventRow[] { function drawEventRow( image: GrayImage, - font: BdfFont, + font: UiFont, row: EventRow, x: number, y: number, diff --git a/app/apps/compass/compass-app.ts b/app/apps/compass/compass-app.ts index 08f796b4..a6885d7f 100644 --- a/app/apps/compass/compass-app.ts +++ b/app/apps/compass/compass-app.ts @@ -1,4 +1,4 @@ -import { getDefaultLargeFont, getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/bdffont"; +import { getDefaultLargeFont, getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/ui-fonts"; import { GrayImage } from "../../graphics/image"; import { COMPASS_CALIBRATION_COMPLETE, diff --git a/app/apps/debug-tests/accelerometer-demo.ts b/app/apps/debug-tests/accelerometer-demo.ts index f80a4955..3d5c205d 100644 --- a/app/apps/debug-tests/accelerometer-demo.ts +++ b/app/apps/debug-tests/accelerometer-demo.ts @@ -1,4 +1,4 @@ -import { getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/bdffont"; +import { getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/ui-fonts"; import { GrayImage } from "../../graphics/image"; import { clamp } from "../../util/numeric-util"; import { addImuListener, imuSourceLabel, setImuReportEnabled, type ImuReading } from "../../native/imu"; diff --git a/app/apps/debug-tests/buzzer-demo.ts b/app/apps/debug-tests/buzzer-demo.ts index 5ba6f560..8e030da2 100644 --- a/app/apps/debug-tests/buzzer-demo.ts +++ b/app/apps/debug-tests/buzzer-demo.ts @@ -1,4 +1,4 @@ -import { getDefaultSmallFont } from "../../graphics/bdffont"; +import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import { GrayImage } from "../../graphics/image"; import { clamp } from "../../util/numeric-util"; import { Layer, type DashboardInputEvent, type LayerActions, type LayerContext } from "../../ui/layers"; diff --git a/app/apps/debug-tests/resource-usage.ts b/app/apps/debug-tests/resource-usage.ts index 5150a8b1..27df5ea1 100644 --- a/app/apps/debug-tests/resource-usage.ts +++ b/app/apps/debug-tests/resource-usage.ts @@ -1,4 +1,4 @@ -import { getDefaultSmallFont } from "../../graphics/bdffont"; +import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import { GrayImage } from "../../graphics/image"; import { clamp } from "../../util/numeric-util"; import { GESTURE_DOUBLE_CLICK } from "../../ui/gestures"; diff --git a/app/apps/debug-tests/screen-test.ts b/app/apps/debug-tests/screen-test.ts index 37ec6e48..20ecf0f0 100644 --- a/app/apps/debug-tests/screen-test.ts +++ b/app/apps/debug-tests/screen-test.ts @@ -1,4 +1,4 @@ -import { getDefaultSmallFont } from "../../graphics/bdffont"; +import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import { GrayImage } from "../../graphics/image"; import { Layer, type DashboardInputEvent, type LayerContext } from "../../ui/layers"; import { drawListScrollbar } from "../../ui/menu"; diff --git a/app/apps/evenhub/api-key-dialog.ts b/app/apps/evenhub/api-key-dialog.ts index b5e3916a..1bd3c13e 100644 --- a/app/apps/evenhub/api-key-dialog.ts +++ b/app/apps/evenhub/api-key-dialog.ts @@ -1,4 +1,4 @@ -import { getDefaultSmallFont } from "../../graphics/bdffont"; +import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import { GrayImage } from "../../graphics/image"; import { truncateText } from "../../graphics/textwrap"; import { Layer, type DashboardInputEvent, type LayerContext, type PaintBelow } from "../../ui/layers"; diff --git a/app/apps/evenhub/compass-router.ts b/app/apps/evenhub/compass-router.ts new file mode 100644 index 00000000..854e67a6 --- /dev/null +++ b/app/apps/evenhub/compass-router.ts @@ -0,0 +1,69 @@ +/** + * Arbitrates the glasses' magnetometer compass (CFW mode-10 stream) among + * running EvenHub apps, mirroring the IMU router. Only the foreground app + * receives headings, so a backgrounded app can't keep the sensor running. + * + * The compass is NOT pre-calibrated — the magnetometer is in the right arm, + * which rests on the head with a wearer-dependent curl (see compass-app memory); + * headings are relative, not true north. + */ +import { addCompassListener, setCompassEnabled, COMPASS_CHANGED, type CompassEvent } from "../../native/compass"; + +export type EvenHubCompassClient = { + readonly windowId: string; + isForeground(): boolean; + deliverCompass(headingDegrees: number): void; +}; + +class EvenHubCompassRouter { + private readonly requesting = new Set(); + private active: EvenHubCompassClient | null = null; + private unsubscribe: (() => void) | null = null; + private enabled = false; + + requestCompass(client: EvenHubCompassClient): void { + this.requesting.add(client); + this.evaluate(); + } + + releaseCompass(client: EvenHubCompassClient): void { + if (!this.requesting.delete(client)) return; + this.evaluate(); + } + + notifyEligibilityChanged(): void { + this.evaluate(); + } + + private eligible(): EvenHubCompassClient | null { + return Array.from(this.requesting).find((client) => client.isForeground()) ?? null; + } + + private evaluate(): void { + const next = this.eligible(); + this.active = next; + if (next) { + if (!this.unsubscribe) { + this.unsubscribe = addCompassListener((event: CompassEvent) => { + // Only heading updates carry a real heading; calibration events use -1. + if (event.command === COMPASS_CHANGED && event.headingDegrees >= 0) { + this.active?.deliverCompass(event.headingDegrees); + } + }); + } + if (!this.enabled) { + setCompassEnabled(true); + this.enabled = true; + } + } else { + if (this.enabled) { + setCompassEnabled(false); + this.enabled = false; + } + this.unsubscribe?.(); + this.unsubscribe = null; + } + } +} + +export const evenHubCompassRouter = new EvenHubCompassRouter(); diff --git a/app/apps/evenhub/containers.ts b/app/apps/evenhub/containers.ts index 5578b865..95954fa6 100644 --- a/app/apps/evenhub/containers.ts +++ b/app/apps/evenhub/containers.ts @@ -19,6 +19,8 @@ export type EvenHubTextContainer = { paddingLength: number; isEventCapture: boolean; zOrderIndex: number | undefined; + /** Extended-layout: inherit content from a same-named container on replace. */ + preserve: boolean; content: string; }; @@ -31,6 +33,7 @@ export type EvenHubImageContainer = { width: number; height: number; zOrderIndex: number | undefined; + preserve: boolean; /** Decoded 8bpp grayscale, set by updateImageRawData; null until first update. */ pixels: Uint8Array | null; pixelsWidth: number; @@ -50,6 +53,7 @@ export type EvenHubListContainer = { paddingLength: number; isEventCapture: boolean; zOrderIndex: number | undefined; + preserve: boolean; itemNames: string[]; itemWidth: number; selectBorder: boolean; @@ -117,6 +121,7 @@ function parseTextContainer(json: Record): EvenHubTextContainer paddingLength: readNumber(json, "paddingLength", 0), isEventCapture: readNumber(json, "isEventCapture", 0) !== 0, zOrderIndex: readOptionalNumber(json, "zOrderIndex"), + preserve: readNumber(json, "preserve", 0) !== 0, content: readString(json, "content", ""), }; } @@ -131,6 +136,7 @@ function parseImageContainer(json: Record): EvenHubImageContain width: readNumber(json, "width", 0), height: readNumber(json, "height", 0), zOrderIndex: readOptionalNumber(json, "zOrderIndex"), + preserve: readNumber(json, "preserve", 0) !== 0, pixels: null, pixelsWidth: 0, pixelsHeight: 0, @@ -154,6 +160,7 @@ function parseListContainer(json: Record): EvenHubListContainer paddingLength: readNumber(json, "paddingLength", 0), isEventCapture: readNumber(json, "isEventCapture", 0) !== 0, zOrderIndex: readOptionalNumber(json, "zOrderIndex"), + preserve: readNumber(json, "preserve", 0) !== 0, itemNames, itemWidth: readNumber(itemContainer, "itemWidth", 0), selectBorder: readNumber(itemContainer, "isItemSelectBorderEn", 0) !== 0, diff --git a/app/apps/evenhub/evenhub-window.ts b/app/apps/evenhub/evenhub-window.ts index 15173ed7..3b549980 100644 --- a/app/apps/evenhub/evenhub-window.ts +++ b/app/apps/evenhub/evenhub-window.ts @@ -58,6 +58,7 @@ export function createEvenHubWindow( submitFrame: options.submitFrame, setSurfaceVisible: options.setSurfaceVisible, removeSurface: options.removeSurface, + reconfigureSurface: options.reconfigureSurface, onClosed: () => { options.onClosed(); session.windowClosed(); @@ -69,6 +70,10 @@ export function createEvenHubWindow( closeWindow: () => shell.closeWindow(windowId), focusSwitcher: () => shell.yieldFocusToSidebar(), pushOverlay: (layer) => created.stack.push(layer), + // The extended layout uses the full 576x452 app area ("max"); stock apps + // stay in the 576x288 band ("medium"). + setTallCanvas: (tall) => created.setHeightMode(tall ? "max" : "medium"), + windowId, }); // FOREGROUND_ENTER/EXIT for the app on shell focus changes. diff --git a/app/apps/evenhub/permission-dialog.ts b/app/apps/evenhub/permission-dialog.ts index a196caf6..2e7c944f 100644 --- a/app/apps/evenhub/permission-dialog.ts +++ b/app/apps/evenhub/permission-dialog.ts @@ -1,4 +1,4 @@ -import { getDefaultSmallFont } from "../../graphics/bdffont"; +import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import { GrayImage } from "../../graphics/image"; import { truncateText } from "../../graphics/textwrap"; import { Layer, type DashboardInputEvent, type LayerContext, type PaintBelow } from "../../ui/layers"; diff --git a/app/apps/evenhub/session.ts b/app/apps/evenhub/session.ts index c8b7b3fb..a6ba5f8c 100644 --- a/app/apps/evenhub/session.ts +++ b/app/apps/evenhub/session.ts @@ -44,6 +44,7 @@ import { readString, type EvenHubImageContainer, type EvenHubListContainer, + type EvenHubContainer, type EvenHubPage, } from "./containers"; import { compositePage } from "./compositor"; @@ -51,7 +52,9 @@ import { type EvenHubManifest } from "./ehpk"; import { permissionsIncludeMicrophone, permissionsInclude } from "./permissions"; import { evenHubMicRouter, type EvenHubMicClient } from "./mic-router"; import { evenHubImuRouter, type EvenHubImuClient } from "./imu-router"; +import { evenHubCompassRouter, type EvenHubCompassClient } from "./compass-router"; import { type ImuReading } from "../../native/imu"; +import { toolRegistry, type ToolResult, type ToolSpec } from "../../assistant/tool-registry"; import { getCurrentLocation } from "../../native/location"; import { LocationTracker, type TrackedLocation } from "../../native/location-tracker"; import { ensureFineLocationPermission } from "../../g2/android-permissions"; @@ -225,6 +228,48 @@ export function buildFaceclawExtensionsScript(versionString: string): string { if (i >= 0) arr.splice(i, 1); }; } + // Compass: enable on first listener, disable when the last one leaves. + var compassListeners = []; + function addCompass(cb) { + compassListeners.push(cb); + if (compassListeners.length === 1) send("setCompass", [true]); + var off = on("compass", cb); + return function () { + off(); + var i = compassListeners.indexOf(cb); + if (i >= 0) compassListeners.splice(i, 1); + if (compassListeners.length === 0) send("setCompass", [false]); + }; + } + // Voice-assistant tools: specs go to the host; handlers stay here and run when + // the host invokes a tool via __fcExtInvokeTool. + var toolHandlers = {}; + window.__fcExtInvokeTool = function (id, name, args) { + var handler = toolHandlers[name]; + function reply(ok, value) { + window.__faceclawEvenHub.postMessage("faceclawExtToolResult", JSON.stringify([id, ok, value]), 0); + } + if (!handler) { reply(false, "unknown tool " + name); return; } + Promise.resolve().then(function () { return handler(args); }).then( + function (res) { reply(true, res == null ? "" : res); }, + function (err) { reply(false, String((err && err.message) || err)); } + ); + }; + function setAssistantTools(tools) { + toolHandlers = {}; + var specs = []; + (tools || []).forEach(function (t) { + if (!t || !t.name || typeof t.handler !== "function") return; + toolHandlers[t.name] = t.handler; + specs.push({ + name: t.name, + description: t.description || "", + parameters: t.parameters || { type: "object", properties: {} }, + availability: t.availability === "open" ? "open" : "foreground" + }); + }); + send("setAssistantTools", [specs]); + } var extensions = { getVersion: function () { return VERSION; }, returnToAppSwitcher: function () { send("returnToAppSwitcher"); }, @@ -232,7 +277,11 @@ export function buildFaceclawExtensionsScript(versionString: string): string { addWindowLifecycleListener: function (cb) { return on("windowLifecycle", cb); }, getConfiguredApiKeys: function () { return call("getConfiguredApiKeys"); }, requestApiKeyAccess: function (services) { return call("requestApiKeyAccess", [services || []]); }, - playBuzzer: function (steps) { return call("playBuzzer", [steps || []]); } + playBuzzer: function (steps) { return call("playBuzzer", [steps || []]); }, + addCompassListener: addCompass, + createLayout: function (layout) { return call("createLayout", [layout || {}]); }, + replaceLayout: function (layout) { return call("replaceLayout", [layout || {}]); }, + setAssistantTools: setAssistantTools }; window.getFaceclawExtensions = function () { return extensions; }; })(); @@ -256,9 +305,13 @@ export type EvenHubWindowHooks = { focusSwitcher: () => void; /** Push a modal overlay layer onto the app window (e.g. a consent prompt). */ pushOverlay: (layer: Layer) => void; + /** Switch the window between the stock 576x288 band and the full 576x452 canvas. */ + setTallCanvas: (tall: boolean) => void; + /** The shell window id (unique per running instance; used for tool registration). */ + windowId: string; }; -export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient { +export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenHubCompassClient { readonly manifest: EvenHubManifest; readonly distDir: string; @@ -269,6 +322,8 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient { private closed = false; private launchContextPushed = false; private systemExitSent = false; + /** The one-shot launch FOREGROUND_ENTER has been delivered (page exists). */ + private launchEnterSent = false; /** Shell focus state; true from launch (apps open focused). */ private foreground = true; /** Phone screen state; the mic goes silent while the screen is off. */ @@ -277,8 +332,17 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient { private micRequested = false; /** The app has enabled the accelerometer via imuControl(true). */ private imuRequested = false; + /** The app has activated the compass (extension addCompassListener). */ + private compassRequested = false; /** Non-null while the app has an active location subscription. */ private locationTracker: LocationTracker | null = null; + /** The shell window id, for tool registration (falls back to packageId). */ + private shellWindowId = ""; + /** Whether this app has contributed assistant tools (for cleanup). */ + private hasAppTools = false; + /** Pending host→app tool invocations, keyed by call id. */ + private readonly pendingToolCalls = new Map void>(); + private nextToolCallId = 1; // Window-lifecycle tracking for the extension API (apps launch visible+focused). private lastVisible = true; private lastFocused = true; @@ -321,6 +385,7 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient { attachWindow(hooks: EvenHubWindowHooks): void { this.windowHooks = hooks; + this.shellWindowId = hooks.windowId; } windowClosed(): void { @@ -402,6 +467,9 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient { // reads the accelerometer). if (this.micRequested) evenHubMicRouter.notifyEligibilityChanged(); if (this.imuRequested) evenHubImuRouter.notifyEligibilityChanged(); + if (this.compassRequested) evenHubCompassRouter.notifyEligibilityChanged(); + // Foreground gates which app tools are live; re-list on change. + if (this.hasAppTools) toolRegistry.fireToolsChanged(); // Focus is unknown until the next paint (a re-shown window repaints and // re-asserts it; a hidden one won't, so treat it as unfocused meanwhile). this.shellFocused = false; @@ -450,7 +518,7 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient { // ----- microphone (EvenHubMicClient) ----- get windowId(): string { - return this.manifest.packageId; + return this.shellWindowId || this.manifest.packageId; } isForeground(): boolean { @@ -593,6 +661,10 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient { this.handleExtensionCall(argsJson, callId); return; } + if (handlerName === "faceclawExtToolResult") { + this.handleToolResult(argsJson); + return; + } void (async () => { let result: unknown = null; try { @@ -676,7 +748,10 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient { this.page = page; this.pageCreated = true; // The launch focus arrived before the page existed; deliver the ENTER now. - if (this.foreground) this.emitSysEvent(FOREGROUND_ENTER_EVENT, 0); + if (this.foreground && !this.launchEnterSent) { + this.launchEnterSent = true; + this.emitSysEvent(FOREGROUND_ENTER_EVENT, 0); + } this.windowHooks?.requestRender(); return 0; } @@ -842,12 +917,126 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient { case "playBuzzer": await this.playBuzzer(Array.isArray(params[0]) ? params[0] : []); return null; + case "setCompass": + this.setCompass(params[0] === true); + return null; + case "createLayout": + case "replaceLayout": + return this.applyExtensionLayout(asRecord(params[0])); + case "setAssistantTools": + this.setAssistantTools(Array.isArray(params[0]) ? params[0] : []); + return null; default: this.log(`evenhub: unknown extension method ${method}`); return null; } } + // ----- compass (EvenHubCompassClient) ----- + + deliverCompass(headingDegrees: number): void { + this.pushExtEvent("compass", { headingDegrees }); + } + + private setCompass(enable: boolean): void { + if (enable) { + if (!this.compassRequested) { + this.compassRequested = true; + evenHubCompassRouter.requestCompass(this); + } + } else if (this.compassRequested) { + this.compassRequested = false; + evenHubCompassRouter.releaseCompass(this); + } + } + + // ----- extended layout ----- + + /** + * Build/replace the page from an extended layout: full 576x452 canvas, no + * container-count limit, and per-container `preserve` (inherit content from a + * same-named container in the outgoing layout). Returns true on success. + */ + private applyExtensionLayout(data: Record): boolean { + const previous = this.page; + const page = parsePage(data); + for (const container of page.containers) { + if (container.preserve) preserveContent(container, previous); + } + this.page = page; + this.pageCreated = true; + // The extended layout uses the taller app area. + this.windowHooks?.setTallCanvas(true); + if (this.foreground && !this.launchEnterSent) { + this.launchEnterSent = true; + this.emitSysEvent(FOREGROUND_ENTER_EVENT, 0); + } + this.windowHooks?.requestRender(); + return true; + } + + // ----- assistant tools ----- + + private setAssistantTools(rawSpecs: unknown[]): void { + const specs: ToolSpec[] = []; + for (const raw of rawSpecs) { + const record = asRecord(raw); + const name = readString(record, "name", ""); + if (!name) continue; + const availability = record.availability === "open" ? "open" : "foreground"; + specs.push({ + name, + description: readString(record, "description", ""), + inputSchema: asRecord(record.parameters), + availability, + }); + } + if (specs.length === 0 && !this.hasAppTools) return; + toolRegistry.setAppTools({ + windowId: this.windowId, + // Prefix tools with the app's package name, per the extension contract. + appId: this.manifest.packageId, + specs, + invoke: (toolName, args) => this.invokeAppTool(toolName, args), + isForeground: () => this.foreground, + }); + this.hasAppTools = specs.length > 0; + } + + /** Invoke one of the app's tools by dispatching into the webview and awaiting its reply. */ + private invokeAppTool(toolName: string, args: unknown): Promise { + return new Promise((resolve) => { + if (!this.webViewHandle || this.closed) { + resolve({ ok: false, error: "app not available" }); + return; + } + const id = this.nextToolCallId++; + this.pendingToolCalls.set(id, resolve); + this.webViewHandle.evaluateJs( + `window.__fcExtInvokeTool && window.__fcExtInvokeTool(${id}, ${JSON.stringify(toolName)}, ${JSON.stringify(args ?? {})})`, + ); + }); + } + + private handleToolResult(argsJson: string): void { + try { + const args = JSON.parse(argsJson) as unknown[]; + const id = Number(args[0]); + const ok = args[1] === true; + const value = args[2]; + const resolve = this.pendingToolCalls.get(id); + if (!resolve) return; + this.pendingToolCalls.delete(id); + if (ok) { + resolve({ ok: true, content: typeof value === "string" ? value : JSON.stringify(value ?? null) }); + } else { + resolve({ ok: false, error: typeof value === "string" ? value : "tool failed" }); + } + } catch (error) { + this.log(`evenhub: bad tool result: ${error}`); + } + } + /** * Prompt the user (on the glasses) to share configured API keys with the app, * resolving with the granted values. Services already granted this session @@ -916,6 +1105,16 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient { this.imuRequested = false; evenHubImuRouter.releaseImu(this); } + if (this.compassRequested) { + this.compassRequested = false; + evenHubCompassRouter.releaseCompass(this); + } + if (this.hasAppTools) { + this.hasAppTools = false; + toolRegistry.removeAppTools(this.windowId); + } + for (const resolve of this.pendingToolCalls.values()) resolve({ ok: false, error: "app closed" }); + this.pendingToolCalls.clear(); this.locationTracker?.stop(); this.locationTracker = null; if (!this.systemExitSent) { @@ -996,6 +1195,26 @@ function activeCommunicator(): any { } } +/** + * Copy content into a `preserve` container from the same-named, same-kind + * container in the previous page (if any), so a replaceLayout keeps its pixels / + * text / list state instead of blanking. + */ +function preserveContent(target: EvenHubContainer, previous: EvenHubPage | null): void { + const source = previous?.containers.find((c) => c.kind === target.kind && c.name === target.name); + if (!source) return; + if (target.kind === "text" && source.kind === "text") { + target.content = source.content; + } else if (target.kind === "image" && source.kind === "image") { + target.pixels = source.pixels; + target.pixelsWidth = source.pixelsWidth; + target.pixelsHeight = source.pixelsHeight; + } else if (target.kind === "list" && source.kind === "list") { + target.itemNames = source.itemNames; + target.selectedIndex = Math.min(source.selectedIndex, Math.max(0, target.itemNames.length - 1)); + } +} + /** The API-key services the extension API can surface, and their settings. */ const API_KEY_SERVICES = [ { id: "openai", setting: openAiApiKeySetting }, diff --git a/app/apps/evenhub/store-detail-layer.ts b/app/apps/evenhub/store-detail-layer.ts index e17de28e..ebf3acbb 100644 --- a/app/apps/evenhub/store-detail-layer.ts +++ b/app/apps/evenhub/store-detail-layer.ts @@ -1,4 +1,4 @@ -import { getDefaultSmallFont } from "../../graphics/bdffont"; +import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import { GrayImage } from "../../graphics/image"; import { truncateText, wrapText } from "../../graphics/textwrap"; import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL } from "../../ui/gestures"; diff --git a/app/apps/evenhub/store-layer.ts b/app/apps/evenhub/store-layer.ts index 5cc11e3e..5a88d227 100644 --- a/app/apps/evenhub/store-layer.ts +++ b/app/apps/evenhub/store-layer.ts @@ -1,4 +1,4 @@ -import { getDefaultSmallFont } from "../../graphics/bdffont"; +import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import { GrayImage } from "../../graphics/image"; import { truncateText, wrapText } from "../../graphics/textwrap"; import { clamp } from "../../util/numeric-util"; diff --git a/app/apps/files/file-browser.ts b/app/apps/files/file-browser.ts index 88ca2f17..78e83e7b 100644 --- a/app/apps/files/file-browser.ts +++ b/app/apps/files/file-browser.ts @@ -1,6 +1,6 @@ -import { getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont"; +import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import { truncateText, truncateLeft } from "../../graphics/textwrap"; -import { GrayImage } from "../../graphics/image"; +import { GrayImage, type UiFont } from "../../graphics/image"; import { renderIcon, type IconName } from "../../graphics/icons"; import { clamp } from "../../util/numeric-util"; import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL } from "../../ui/gestures"; @@ -149,7 +149,7 @@ export class FileBrowserLayer implements Layer { return image; } - private paintList(image: GrayImage, font: BdfFont, rows: BrowserItem[], ctx: LayerContext): void { + private paintList(image: GrayImage, font: UiFont, rows: BrowserItem[], ctx: LayerContext): void { const { width, height } = ctx.stack.getBaseSize(); this.listIndex = clamp(this.listIndex, 0, Math.max(0, rows.length - 1)); @@ -172,7 +172,7 @@ export class FileBrowserLayer implements Layer { image.drawText(font, 20, height - 16, `${GESTURE_CLICK} open ${GESTURE_DOUBLE_CLICK} up / back`, 110); } - private paintIcons(image: GrayImage, font: BdfFont, rows: BrowserItem[], ctx: LayerContext): void { + private paintIcons(image: GrayImage, font: UiFont, rows: BrowserItem[], ctx: LayerContext): void { const { width, height } = ctx.stack.getBaseSize(); const focused = ctx.stack.isFocused(); const iconRows = buildIconRows(rows); diff --git a/app/apps/files/file-info-dialog.ts b/app/apps/files/file-info-dialog.ts index 834341af..329588e0 100644 --- a/app/apps/files/file-info-dialog.ts +++ b/app/apps/files/file-info-dialog.ts @@ -1,4 +1,4 @@ -import { getDefaultSmallFont } from "../../graphics/bdffont"; +import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import { GrayImage } from "../../graphics/image"; import { wrapText } from "../../graphics/textwrap"; import type { DirectoryEntry } from "../../native/file-access"; diff --git a/app/apps/files/files-app.ts b/app/apps/files/files-app.ts index e1c60e57..c3c3ae23 100644 --- a/app/apps/files/files-app.ts +++ b/app/apps/files/files-app.ts @@ -1,5 +1,6 @@ import { readTextFile, type DirectoryEntry } from "../../native/file-access"; import { isFontFile } from "../../native/font-files"; +import { installFontFile, isFontInstalled } from "../../graphics/installed-fonts"; import { isDecodableImageFile } from "../../native/image-files"; import { readEvenHubPackageManifest } from "../evenhub/installed-apps"; import { EvenHubPermissionDialogLayer } from "../evenhub/permission-dialog"; @@ -227,6 +228,16 @@ function fileOpenActions(entry: DirectoryEntry, options: FilesAppOptions): FileI ]; } if (isFontFile(entry.name)) { + // Install copies into the app-internal fonts directory, making the face + // available in the Settings font pickers. The row relabels itself with + // the result (the dialog re-reads labels each paint). + const install: FileInfoAction = { + label: isFontInstalled(entry.path) ? "Reinstall" : "Install", + onSelect: () => { + const error = installFontFile(entry.path); + install.label = error ?? "Installed"; + }, + }; return [ { label: "Preview here", @@ -242,6 +253,7 @@ function fileOpenActions(entry: DirectoryEntry, options: FilesAppOptions): FileI options.openFontWindow(entry.name, entry.path); }, }, + install, ]; } return []; diff --git a/app/apps/files/font-previewer.ts b/app/apps/files/font-previewer.ts index 08b902e4..8c2b9b4b 100644 --- a/app/apps/files/font-previewer.ts +++ b/app/apps/files/font-previewer.ts @@ -1,4 +1,4 @@ -import { getDefaultSmallFont } from "../../graphics/bdffont"; +import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import { GrayImage } from "../../graphics/image"; import { truncateText } from "../../graphics/textwrap"; import { diff --git a/app/apps/files/image-viewer.ts b/app/apps/files/image-viewer.ts index 6232e91b..4d13fb96 100644 --- a/app/apps/files/image-viewer.ts +++ b/app/apps/files/image-viewer.ts @@ -1,4 +1,5 @@ -import { getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont"; +import { type BdfFont } from "../../graphics/bdffont"; +import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import { truncateText } from "../../graphics/textwrap"; import { GrayImage } from "../../graphics/image"; import { loadImageFileAsGray } from "../../native/image-files"; diff --git a/app/apps/files/text-viewer.ts b/app/apps/files/text-viewer.ts index ce8328cc..2482794e 100644 --- a/app/apps/files/text-viewer.ts +++ b/app/apps/files/text-viewer.ts @@ -1,15 +1,33 @@ import { wrapText, truncateText } from "../../graphics/textwrap"; -import { getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont"; -import { GrayImage } from "../../graphics/image"; +import { getDefaultSmallFont, getUiFontSelection } from "../../graphics/ui-fonts"; +import { TtfFont } from "../../graphics/ttf-font"; +import { GrayImage, type UiFont } from "../../graphics/image"; import { Layer, type DashboardInputEvent, type LayerContext } from "../../ui/layers"; const MARGIN_X = 18; const TITLE_Y = 16; const BODY_X = 18; const BODY_Y = 44; -const LINE_STEP = 14; const FOOTER_MARGIN = 36; +/** + * Body text: the user's UI font when it's a TTF; otherwise bundled Inter + * Light (the document viewer stays antialiased even while the UI default is + * still a bitmap face), with the bitmap UI font as the last fallback. + */ +const FALLBACK_BODY_FONT_SIZE_PX = 16; +/** Line step for the BDF fallback (the 12px font plus leading, as before). */ +const BDF_LINE_STEP = 14; + +function getViewerFont(): UiFont { + if (getUiFontSelection().kind === "ttf") return getDefaultSmallFont(); + return TtfFont.bundled("inter-light", FALLBACK_BODY_FONT_SIZE_PX) ?? getDefaultSmallFont(); +} + +function lineStepOf(font: UiFont): number { + return font instanceof TtfFont ? font.lineHeight : BDF_LINE_STEP; +} + /** * Paged text viewer for a document. Sized to its hosting stack (a Files * document window, or pushed over the file browser for view-in-place). @@ -17,6 +35,7 @@ const FOOTER_MARGIN = 36; export class TextViewerLayer implements Layer { private lines: string[] | null = null; private wrappedForWidth = 0; + private wrappedWithFont = 0; private firstLine = 0; private bodyLineCount = 14; @@ -26,17 +45,18 @@ export class TextViewerLayer implements Layer { ) {} paint(ctx: LayerContext): GrayImage { - const font = getDefaultSmallFont(); + const font = getViewerFont(); + const lineStep = lineStepOf(font); const { width, height } = ctx.stack.getBaseSize(); const image = new GrayImage(width, height, 0); const footerY = height - FOOTER_MARGIN; - this.bodyLineCount = Math.max(1, Math.floor((footerY - BODY_Y) / LINE_STEP)); + this.bodyLineCount = Math.max(1, Math.floor((footerY - BODY_Y) / lineStep)); image.drawText(font, MARGIN_X + 4, TITLE_Y, truncateText(font, this.title, width - 2 * MARGIN_X - 8), 220); const lines = this.getLines(font, width); const visibleLines = lines.slice(this.firstLine, this.firstLine + this.bodyLineCount); for (let index = 0; index < visibleLines.length; index++) { - image.drawText(font, BODY_X, BODY_Y + index * LINE_STEP, visibleLines[index]!, 230); + image.drawText(font, BODY_X, BODY_Y + index * lineStep, visibleLines[index]!, 230); } const currentPage = Math.floor(this.firstLine / this.pageStep()) + 1; @@ -74,14 +94,15 @@ export class TextViewerLayer implements Layer { this.firstLine = Math.max(0, Math.min(maxFirstLine, this.firstLine + delta)); } - private getLines(font: BdfFont, width: number): string[] { - if (this.lines === null || this.wrappedForWidth !== width) { + private getLines(font: UiFont, width: number): string[] { + if (this.lines === null || this.wrappedForWidth !== width || this.wrappedWithFont !== font.fingerprintId) { const normalized = this.documentText.replace(/\t/g, " ").replace(/\r/g, ""); this.lines = wrapText(font, normalized, width - BODY_X - 12, { preserveLeadingWhitespace: true, breakLongWords: true, }); this.wrappedForWidth = width; + this.wrappedWithFont = font.fingerprintId; } return this.lines; } @@ -93,4 +114,3 @@ export class TextViewerLayer implements Layer { return Math.ceil((lineCount - this.bodyLineCount) / this.pageStep()) + 1; } } - diff --git a/app/apps/freecell/freecell-app.worker.ts b/app/apps/freecell/freecell-app.worker.ts index 35eb95d8..1f4da7d9 100644 --- a/app/apps/freecell/freecell-app.worker.ts +++ b/app/apps/freecell/freecell-app.worker.ts @@ -15,7 +15,8 @@ import "@nativescript/core/globals"; import { GrayImage } from "../../graphics/image"; import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane"; import { prepareFrameDraws } from "../../graphics/glyph-wire"; -import { getDefaultSmallFont, getFont } from "../../graphics/bdffont"; +import { getFont } from "../../graphics/bdffont"; +import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import * as frameTimings from "../../native/frame-timings"; import type { DashboardInputEvent } from "../../ui/layers"; import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects"; diff --git a/app/apps/launcher/launcher-app.ts b/app/apps/launcher/launcher-app.ts index 68ee541a..916730f7 100644 --- a/app/apps/launcher/launcher-app.ts +++ b/app/apps/launcher/launcher-app.ts @@ -1,4 +1,5 @@ -import { getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont"; +import { type BdfFont } from "../../graphics/bdffont"; +import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import { truncateText } from "../../graphics/textwrap"; import { GrayImage } from "../../graphics/image"; import { type Plane } from "../../graphics/plane"; diff --git a/app/apps/minesweeper/minesweeper-app.worker.ts b/app/apps/minesweeper/minesweeper-app.worker.ts index 1a060fc3..e8989419 100644 --- a/app/apps/minesweeper/minesweeper-app.worker.ts +++ b/app/apps/minesweeper/minesweeper-app.worker.ts @@ -15,7 +15,8 @@ import "@nativescript/core/globals"; import { GrayImage } from "../../graphics/image"; import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane"; import { prepareFrameDraws } from "../../graphics/glyph-wire"; -import { getDefaultSmallFont, getFont } from "../../graphics/bdffont"; +import { getFont } from "../../graphics/bdffont"; +import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import * as frameTimings from "../../native/frame-timings"; import type { DashboardInputEvent } from "../../ui/layers"; import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects"; diff --git a/app/apps/music/media-browse.ts b/app/apps/music/media-browse.ts index 930e345f..8180c864 100644 --- a/app/apps/music/media-browse.ts +++ b/app/apps/music/media-browse.ts @@ -1,5 +1,5 @@ -import { getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont"; -import { GrayImage } from "../../graphics/image"; +import { getDefaultSmallFont } from "../../graphics/ui-fonts"; +import { GrayImage, type UiFont } from "../../graphics/image"; import { clamp } from "../../util/numeric-util"; import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL } from "../../ui/gestures"; import { @@ -285,7 +285,7 @@ export class MediaBrowseLayer implements Layer { } } -function truncateRight(font: BdfFont, text: string, maxWidth: number): string { +function truncateRight(font: UiFont, text: string, maxWidth: number): string { if (font.measureText(text) <= maxWidth) return text; let out = text; while (out.length > 1 && font.measureText(`${out}...`) > maxWidth) { @@ -294,7 +294,7 @@ function truncateRight(font: BdfFont, text: string, maxWidth: number): string { return `${out}...`; } -function truncateLeft(font: BdfFont, text: string, maxWidth: number): string { +function truncateLeft(font: UiFont, text: string, maxWidth: number): string { if (font.measureText(text) <= maxWidth) return text; let out = text; while (out.length > 1 && font.measureText(`...${out}`) > maxWidth) { diff --git a/app/apps/music/music-app.ts b/app/apps/music/music-app.ts index 4fe5b962..92d211d1 100644 --- a/app/apps/music/music-app.ts +++ b/app/apps/music/music-app.ts @@ -1,4 +1,4 @@ -import { getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/bdffont"; +import { getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/ui-fonts"; import { GrayImage } from "../../graphics/image"; import { wrapText, truncateText } from "../../graphics/textwrap"; import { clamp } from "../../util/numeric-util"; diff --git a/app/apps/navigate/navigate-app.worker.ts b/app/apps/navigate/navigate-app.worker.ts index 878b0eeb..05d7c5f0 100644 --- a/app/apps/navigate/navigate-app.worker.ts +++ b/app/apps/navigate/navigate-app.worker.ts @@ -10,7 +10,8 @@ import "@nativescript/core/globals"; import { GrayImage } from "../../graphics/image"; import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane"; import { prepareFrameDraws } from "../../graphics/glyph-wire"; -import { getDefaultSmallFont, getFont } from "../../graphics/bdffont"; +import { getFont } from "../../graphics/bdffont"; +import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import * as frameTimings from "../../native/frame-timings"; import type { DashboardInputEvent } from "../../ui/layers"; import { defaultWindowMenuItems, WindowMenu } from "../../ui/window-menu"; diff --git a/app/apps/nightscout/nightscout-app.ts b/app/apps/nightscout/nightscout-app.ts index 8677f8e2..8a35174e 100644 --- a/app/apps/nightscout/nightscout-app.ts +++ b/app/apps/nightscout/nightscout-app.ts @@ -1,4 +1,4 @@ -import { getDefaultSmallFont } from "../../graphics/bdffont"; +import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import { GrayImage } from "../../graphics/image"; import { nightscoutBridge, type NightscoutState } from "../../native/nightscout-bridge"; import { NightscoutLayer } from "./nightscout"; diff --git a/app/apps/nightscout/nightscout.ts b/app/apps/nightscout/nightscout.ts index 7bd1fc2e..32a23d6f 100644 --- a/app/apps/nightscout/nightscout.ts +++ b/app/apps/nightscout/nightscout.ts @@ -1,5 +1,5 @@ -import { BdfFont, getDefaultLargeFont, getDefaultSmallFont } from "../../graphics/bdffont"; -import { GrayImage, imageFromAsciiArt } from "../../graphics/image"; +import { getDefaultLargeFont, getDefaultSmallFont } from "../../graphics/ui-fonts"; +import { GrayImage, imageFromAsciiArt, type UiFont } from "../../graphics/image"; import { DashboardInputEvent, Layer, LayerContext } from "../../ui/layers"; import { nightscoutBridge, type NightscoutState } from "../../native/nightscout-bridge"; import { @@ -20,7 +20,7 @@ const NIGHTSCOUT_GRAPH_TIME_QUANTUM_MS = 60 * 1000; function drawDirectionIndicator( image: GrayImage, - font: BdfFont, + font: UiFont, x: number, y: number, direction: string, @@ -43,7 +43,7 @@ function drawDirectionIndicator( } } -function directionGlyphLabel(font: BdfFont, direction: string): string { +function directionGlyphLabel(font: UiFont, direction: string): string { switch (direction) { case "DoubleUp": return font.hasGlyph("↑".codePointAt(0)!) ? "↑↑" : ""; @@ -98,7 +98,7 @@ function drawNightscoutGraph( bounds: { x: number; y: number; width: number; height: number }, nightscout: NightscoutState, nowMs: number, - font: BdfFont, + font: UiFont, ): void { if (bounds.width <= 2 || bounds.height <= 2) { return; @@ -195,7 +195,7 @@ function drawNightscoutBasalOverlay( function drawNightscoutCarbMarkers( image: GrayImage, bounds: { x: number; y: number; width: number; height: number }, - font: BdfFont, + font: UiFont, carbEvents: NightscoutState["carbs"], nowMs: number, ): void { @@ -216,7 +216,7 @@ function drawNightscoutCarbMarkers( function drawNightscoutBolusMarkers( image: GrayImage, bounds: { x: number; y: number; width: number; height: number }, - font: BdfFont, + font: UiFont, bolusEvents: NightscoutState["boluses"], nowMs: number, ): void { @@ -246,7 +246,7 @@ function drawNightscoutBolusMarkers( function drawTextCentered( image: GrayImage, - font: BdfFont, + font: UiFont, centerX: number, y: number, text: string, diff --git a/app/apps/pinball/pinball-app.worker.ts b/app/apps/pinball/pinball-app.worker.ts index 41c1cfdc..18166ded 100644 --- a/app/apps/pinball/pinball-app.worker.ts +++ b/app/apps/pinball/pinball-app.worker.ts @@ -21,7 +21,8 @@ import "@nativescript/core/globals"; import { GrayImage } from "../../graphics/image"; import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane"; import { prepareFrameDraws } from "../../graphics/glyph-wire"; -import { getDefaultSmallFont, getFont } from "../../graphics/bdffont"; +import { getFont } from "../../graphics/bdffont"; +import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import * as frameTimings from "../../native/frame-timings"; import { getStringSetting, setStringSetting } from "../../native/settings-store"; import type { DashboardInputEvent } from "../../ui/layers"; diff --git a/app/apps/roam/roam-app.worker.ts b/app/apps/roam/roam-app.worker.ts index 939abad4..1d9410b7 100644 --- a/app/apps/roam/roam-app.worker.ts +++ b/app/apps/roam/roam-app.worker.ts @@ -9,7 +9,7 @@ import "@nativescript/core/globals"; import { GrayImage } from "../../graphics/image"; import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane"; import { prepareFrameDraws } from "../../graphics/glyph-wire"; -import { getDefaultSmallFont } from "../../graphics/bdffont"; +import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import { truncateText, wrapText } from "../../graphics/textwrap"; import * as frameTimings from "../../native/frame-timings"; import type { DashboardInputEvent } from "../../ui/layers"; diff --git a/app/apps/terminal/terminal-app.worker.ts b/app/apps/terminal/terminal-app.worker.ts index caa0fc0e..d7dc9d25 100644 --- a/app/apps/terminal/terminal-app.worker.ts +++ b/app/apps/terminal/terminal-app.worker.ts @@ -31,10 +31,10 @@ * re-attach snapshot resyncs contents and scrollback only once it's needed. */ import "@nativescript/core/globals"; -import { GrayImage } from "../../graphics/image"; +import { GrayImage, type UiFont } from "../../graphics/image"; import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane"; import { prepareFrameDraws } from "../../graphics/glyph-wire"; -import { getFont } from "../../graphics/bdffont"; +import { getDefaultSmallFont, getTerminalFontConfig } from "../../graphics/ui-fonts"; import { TERMINAL_ICON_GLYPHS } from "../../graphics/icons"; import * as frameTimings from "../../native/frame-timings"; import { GESTURE_DOUBLE_CLICK } from "../../ui/gestures"; @@ -77,19 +77,25 @@ import type { ToolResult, ToolSpec } from "../../assistant/tool-registry"; declare const global: any; declare const com: any; -// Terminus-12 has a 6x12 cell; each window derives its grid from the -// viewport in its open-window message (the hub is min-height, session views -// full-height). The websocket init handshake declares the view grid. -const terminalFont = getFont("terminus12"); -const CELL_WIDTH = 6; -const CELL_HEIGHT = 12; +// Cell geometry comes from the terminal font setting (Settings > Terminal > +// Font; the default is Terminus-12's 6x12). Each window derives its grid from +// the viewport in its open-window message (the hub is min-height, session +// views full-height). Grid dimensions are baked into each session's websocket +// handshake, so windows and control connections capture the cell size at +// creation — a font change applies to ones (re)opened afterwards. +function chromeFont(): UiFont { + return getDefaultSmallFont(); +} // Grid of a session view window (full-height); also declared on the control // connections so sessions launched from presets come up at view size. const VIEW_VIEWPORT = appViewportSize("max"); -const VIEW_GRID = { - cols: Math.floor(VIEW_VIEWPORT.width / CELL_WIDTH), - rows: Math.floor(VIEW_VIEWPORT.height / CELL_HEIGHT), -}; +function viewGrid(): { cols: number; rows: number } { + const { cellWidth, cellHeight } = getTerminalFontConfig(); + return { + cols: Math.floor(VIEW_VIEWPORT.width / cellWidth), + rows: Math.floor(VIEW_VIEWPORT.height / cellHeight), + }; +} const DEVICE_NAME = "Faceclaw G2"; const HUB_ROW_HEIGHT = 20; const RENDER_COALESCE_MS = 33; @@ -148,6 +154,10 @@ type HubWindow = BaseWindow & { type ViewWindow = BaseWindow & { kind: "view"; + /** Grid font and integer cell pitch, captured at window creation. */ + font: UiFont; + cellWidth: number; + cellHeight: number; connectionId: string; socket: string; label: string; @@ -427,14 +437,15 @@ function openWindow(windowId: string, surfaceId: string, viewport: { width: numb renderHubWindows(); return; } + const hubCell = getTerminalFontConfig(); windows.set(windowId, { kind: "hub", windowId, surfaceId, viewportWidth: viewport.width, viewportHeight: viewport.height, - gridCols: Math.floor(viewport.width / CELL_WIDTH), - gridRows: Math.floor(viewport.height / CELL_HEIGHT), + gridCols: Math.floor(viewport.width / hubCell.cellWidth), + gridRows: Math.floor(viewport.height / hubCell.cellHeight), foreground: false, focused: false, renderScheduled: false, @@ -490,8 +501,7 @@ function clientOptionsFor(connection: TerminalConnection): G2MirrorClientOptions return { ...parsed, deviceName: DEVICE_NAME, - cols: VIEW_GRID.cols, - rows: VIEW_GRID.rows, + ...viewGrid(), }; } @@ -686,8 +696,12 @@ function createViewWindow( view: PendingView, ): ViewWindow { const { connectionId, socket, label, glyph } = view; - const gridCols = Math.floor(viewport.width / CELL_WIDTH); - const gridRows = Math.floor(viewport.height / CELL_HEIGHT); + // Capture the terminal font at open time: the grid dims go into the + // websocket handshake and the emulator, so they must not change under a + // live window (a font-setting change applies to the next window). + const cell = getTerminalFontConfig(); + const gridCols = Math.floor(viewport.width / cell.cellWidth); + const gridRows = Math.floor(viewport.height / cell.cellHeight); const client = new G2MirrorClient({ ...view.options, cols: gridCols, rows: gridRows }); const window: ViewWindow = { kind: "view", @@ -697,6 +711,9 @@ function createViewWindow( viewportHeight: viewport.height, gridCols, gridRows, + font: cell.font, + cellWidth: cell.cellWidth, + cellHeight: cell.cellHeight, foreground: false, focused: false, renderScheduled: false, @@ -1386,13 +1403,13 @@ function paintHub(window: HubWindow): GrayImage { } const image = new GrayImage(window.viewportWidth, window.viewportHeight, 0); // No border box: the shell chrome (top bar + sidebar) already frames the app. - image.drawText(terminalFont, 18, 10, window.mode === "connections" ? "Terminal - Connections" : "Terminal", 220); - image.drawText(terminalFont, 24, 30, hubStatusLine(window), 170); + image.drawText(chromeFont(), 18, 10, window.mode === "connections" ? "Terminal - Connections" : "Terminal", 220); + image.drawText(chromeFont(), 24, 30, hubStatusLine(window), 170); let listTop = 52; if (window.mode === "sessions" && controls.size === 0) { - image.drawText(terminalFont, 24, 46, "Add a g2mirror:// connection to get started, see:", 150); - image.drawText(terminalFont, 24, 60, "https://github.com/jimrandomh/g2mirror", 190); + image.drawText(chromeFont(), 24, 46, "Add a g2mirror:// connection to get started, see:", 150); + image.drawText(chromeFont(), 24, 60, "https://github.com/jimrandomh/g2mirror", 190); listTop += 28; } @@ -1405,7 +1422,7 @@ function paintHub(window: HubWindow): GrayImage { const y = listTop + (index - window.scrollRow) * HUB_ROW_HEIGHT; const item = items[index]!; if (item.heading) { - image.drawText(terminalFont, 20, y + 2, item.label, 140); + image.drawText(chromeFont(), 20, y + 2, item.label, 140); continue; } const selected = index === window.selectedIndex; @@ -1414,7 +1431,7 @@ function paintHub(window: HubWindow): GrayImage { // an outline-only selection signals the sidebar owns input. drawSelectionHighlight(image, 20, y - 2, window.viewportWidth - 40, HUB_ROW_HEIGHT - 1, window.focused, 8); } - image.drawText(terminalFont, 32, y + 2, item.label, selected ? 255 : 200); + image.drawText(chromeFont(), 32, y + 2, item.label, selected ? 255 : 200); } if (items.length > visibleRowCount) { drawListScrollbar( @@ -1428,21 +1445,21 @@ function paintHub(window: HubWindow): GrayImage { ); } - image.drawText(terminalFont, 24, window.viewportHeight - 24, `${GESTURE_DOUBLE_CLICK} back`, 110); + image.drawText(chromeFont(), 24, window.viewportHeight - 24, `${GESTURE_DOUBLE_CLICK} back`, 110); return image; } function paintAddConnection(window: HubWindow): GrayImage { const image = new GrayImage(window.viewportWidth, window.viewportHeight, 0); - image.drawText(terminalFont, 18, 10, "Add connection", 220); - image.drawText(terminalFont, 24, 34, "Type the g2mirror://token@host string in the", 170); - image.drawText(terminalFont, 24, 48, "phone app (or use voice input from the menu).", 170); + image.drawText(chromeFont(), 18, 10, "Add connection", 220); + image.drawText(chromeFont(), 24, 34, "Type the g2mirror://token@host string in the", 170); + image.drawText(chromeFont(), 24, 48, "phone app (or use voice input from the menu).", 170); const draft = terminalNewConnectionSetting.get(); - image.drawText(terminalFont, 24, 76, truncateLabel(draft || "(empty)", 52), 220); + image.drawText(chromeFont(), 24, 76, truncateLabel(draft || "(empty)", 52), 220); if (window.addError) { - image.drawText(terminalFont, 24, 96, window.addError, 150); + image.drawText(chromeFont(), 24, 96, window.addError, 150); } - image.drawText(terminalFont, 24, window.viewportHeight - 24, `click save ${GESTURE_DOUBLE_CLICK} cancel`, 110); + image.drawText(chromeFont(), 24, window.viewportHeight - 24, `click save ${GESTURE_DOUBLE_CLICK} cancel`, 110); return image; } @@ -1467,11 +1484,37 @@ function hubStatusLine(window: HubWindow): string { return anyRetrying ? `${summary} Retrying...` : summary; } +/** + * Draw one terminal row at a fixed integer cell pitch, so glyph columns stay + * aligned with the emulator's grid arithmetic even when a TTF face has + * fractional advances. Identical to drawText for the 6px bitmap default. + */ +function drawGridRow( + image: GrayImage, + font: UiFont, + cellWidth: number, + rowY: number, + text: string, + value: number, +): void { + let col = 0; + for (const char of text) { + const codePoint = char.codePointAt(0) ?? 32; + if (codePoint !== 32) { + const glyph = font.getGlyph(codePoint); + if (glyph && glyph.bbxWidth > 0) { + image.drawGlyph(font, glyph, col * cellWidth, rowY, value); + } + } + col++; + } +} + function paintView(window: ViewWindow): GrayImage { const image = new GrayImage(window.viewportWidth, window.viewportHeight, 0); if (!window.receivedData) { - image.drawText(terminalFont, 24, 110, window.status, 170); - image.drawText(terminalFont, 24, 130, `${GESTURE_DOUBLE_CLICK} back`, 110); + image.drawText(chromeFont(), 24, 110, window.status, 170); + image.drawText(chromeFont(), 24, 130, `${GESTURE_DOUBLE_CLICK} back`, 110); return image; } @@ -1485,7 +1528,7 @@ function paintView(window: ViewWindow): GrayImage { // Draw the cursor only if its line is within the visible window. const cursorScreenRow = historyNext + window.emulator.cursorRow() - top; if (cursorScreenRow >= 0 && cursorScreenRow < rows) { - image.fillRect(window.emulator.cursorCol() * CELL_WIDTH, cursorScreenRow * CELL_HEIGHT, CELL_WIDTH, CELL_HEIGHT, 70); + image.fillRect(window.emulator.cursorCol() * window.cellWidth, cursorScreenRow * window.cellHeight, window.cellWidth, window.cellHeight, 70); } // Stale content stays visible across a disconnect, so flag it: a status @@ -1493,12 +1536,12 @@ function paintView(window: ViewWindow): GrayImage { // Text is deferred glyphs (always on top of raster), so the covered rows // must be suppressed rather than painted over. const statusBannerY = window.client.state().phase !== "attached" - ? window.viewportHeight - CELL_HEIGHT + ? window.viewportHeight - window.cellHeight : null; for (let row = 0; row < rows; row++) { - const rowY = row * CELL_HEIGHT; - if (statusBannerY !== null && rowY + CELL_HEIGHT > statusBannerY) continue; + const rowY = row * window.cellHeight; + if (statusBannerY !== null && rowY + window.cellHeight > statusBannerY) continue; const absolute = top + row; let text = ""; if (absolute >= historyNext) { @@ -1507,15 +1550,15 @@ function paintView(window: ViewWindow): GrayImage { } else if (absolute >= window.archiveStart) { text = window.archive[absolute - window.archiveStart] ?? ""; } - if (text.length) image.drawText(terminalFont, 0, rowY, text, 200); + if (text.length) drawGridRow(image, window.font, window.cellWidth, rowY, text, 200); } if (!following) { drawScrollIndicator(image, top, window.archiveStart, bottomTop); } if (statusBannerY !== null) { - image.fillRect(0, statusBannerY, window.viewportWidth, CELL_HEIGHT, 0); - image.drawText(terminalFont, 0, statusBannerY, window.status, 170); + image.fillRect(0, statusBannerY, window.viewportWidth, window.cellHeight, 0); + image.drawText(chromeFont(), 0, statusBannerY, window.status, 170); } return image; } diff --git a/app/apps/timer/timer-app.worker.ts b/app/apps/timer/timer-app.worker.ts index e0b45888..cb56b9dd 100644 --- a/app/apps/timer/timer-app.worker.ts +++ b/app/apps/timer/timer-app.worker.ts @@ -7,7 +7,8 @@ import "@nativescript/core/globals"; import { GrayImage } from "../../graphics/image"; import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane"; import { prepareFrameDraws } from "../../graphics/glyph-wire"; -import { getDefaultSmallFont, getFont } from "../../graphics/bdffont"; +import { getFont } from "../../graphics/bdffont"; +import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import * as frameTimings from "../../native/frame-timings"; import { cancelTimerNotification, diff --git a/app/apps/transcribe/transcribe.ts b/app/apps/transcribe/transcribe.ts index 9a4d479f..60b49377 100644 --- a/app/apps/transcribe/transcribe.ts +++ b/app/apps/transcribe/transcribe.ts @@ -1,5 +1,5 @@ -import { GrayImage } from "../../graphics/image"; -import { getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont"; +import { GrayImage, type UiFont } from "../../graphics/image"; +import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import { writeTextToDownloads } from "../../native/file-access"; import { voiceControlBridge, type VoiceTranscriptEvent } from "../../native/voice-control"; import { Layer, type DashboardInputEvent, type LayerContext } from "../../ui/layers"; @@ -112,7 +112,7 @@ function transcriptTimestamp(): string { ); } -function wrapTranscribeText(font: BdfFont, text: string, maxWidth: number): string[] { +function wrapTranscribeText(font: UiFont, text: string, maxWidth: number): string[] { const words = text.split(/\s+/).filter(Boolean); const lines: string[] = []; let line = ""; diff --git a/app/apps/weather/weather.ts b/app/apps/weather/weather.ts index 50e98a0f..56aba72c 100644 --- a/app/apps/weather/weather.ts +++ b/app/apps/weather/weather.ts @@ -1,5 +1,5 @@ -import { getDefaultLargeFont, getDefaultMediumFont, getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont"; -import { GrayImage } from "../../graphics/image"; +import { getDefaultLargeFont, getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/ui-fonts"; +import { GrayImage, type UiFont } from "../../graphics/image"; import { wrapText, truncateText } from "../../graphics/textwrap"; import { clamp } from "../../util/numeric-util"; import { type ForecastPeriod, type WeatherState } from "../../native/weather"; @@ -167,7 +167,7 @@ export class WeatherLayer implements Layer { private drawMessage( image: GrayImage, - font: BdfFont, + font: UiFont, width: number, height: number, message: string, diff --git a/app/fonts/ttf/Inter_18pt-Bold.ttf b/app/fonts/ttf/Inter_18pt-Bold.ttf new file mode 100644 index 00000000..57704d10 Binary files /dev/null and b/app/fonts/ttf/Inter_18pt-Bold.ttf differ diff --git a/app/fonts/ttf/Inter_18pt-Light.ttf b/app/fonts/ttf/Inter_18pt-Light.ttf new file mode 100644 index 00000000..acae3612 Binary files /dev/null and b/app/fonts/ttf/Inter_18pt-Light.ttf differ diff --git a/app/fonts/ttf/Inter_18pt-Regular.ttf b/app/fonts/ttf/Inter_18pt-Regular.ttf new file mode 100644 index 00000000..ce097c82 Binary files /dev/null and b/app/fonts/ttf/Inter_18pt-Regular.ttf differ diff --git a/app/fonts/ttf/Montserrat-Bold.ttf b/app/fonts/ttf/Montserrat-Bold.ttf new file mode 100644 index 00000000..40335875 Binary files /dev/null and b/app/fonts/ttf/Montserrat-Bold.ttf differ diff --git a/app/fonts/ttf/Montserrat-Light.ttf b/app/fonts/ttf/Montserrat-Light.ttf new file mode 100644 index 00000000..881f12d0 Binary files /dev/null and b/app/fonts/ttf/Montserrat-Light.ttf differ diff --git a/app/fonts/ttf/Montserrat-Regular.ttf b/app/fonts/ttf/Montserrat-Regular.ttf new file mode 100644 index 00000000..895e220a Binary files /dev/null and b/app/fonts/ttf/Montserrat-Regular.ttf differ diff --git a/app/fonts/ttf/OFL-Inter.txt b/app/fonts/ttf/OFL-Inter.txt new file mode 100644 index 00000000..d05ec4b3 --- /dev/null +++ b/app/fonts/ttf/OFL-Inter.txt @@ -0,0 +1,93 @@ +Copyright 2020 The Inter Project Authors (https://github.com/rsms/inter) + +This Font Software is licensed under the SIL Open Font License, Version 1.1. +This license is copied below, and is also available with a FAQ at: +https://openfontlicense.org + + +----------------------------------------------------------- +SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 +----------------------------------------------------------- + +PREAMBLE +The goals of the Open Font License (OFL) are to stimulate worldwide +development of collaborative font projects, to support the font creation +efforts of academic and linguistic communities, and to provide a free and +open framework in which fonts may be shared and improved in partnership +with others. + +The OFL allows the licensed fonts to be used, studied, modified and +redistributed freely as long as they are not sold by themselves. The +fonts, including any derivative works, can be bundled, embedded, +redistributed and/or sold with any software provided that any reserved +names are not used by derivative works. The fonts and derivatives, +however, cannot be released under any other type of license. The +requirement for fonts to remain under this license does not apply +to any document created using the fonts or their derivatives. + +DEFINITIONS +"Font Software" refers to the set of files released by the Copyright +Holder(s) under this license and clearly marked as such. This may +include source files, build scripts and documentation. + +"Reserved Font Name" refers to any names specified as such after the +copyright statement(s). + +"Original Version" refers to the collection of Font Software components as +distributed by the Copyright Holder(s). + +"Modified Version" refers to any derivative made by adding to, deleting, +or substituting -- in part or in whole -- any of the components of the +Original Version, by changing formats or by porting the Font Software to a +new environment. + +"Author" refers to any designer, engineer, programmer, technical +writer or other person who contributed to the Font Software. + +PERMISSION & CONDITIONS +Permission is hereby granted, free of charge, to any person obtaining +a copy of the Font Software, to use, study, copy, merge, embed, modify, +redistribute, and sell modified and unmodified copies of the Font +Software, subject to the following conditions: + +1) Neither the Font Software nor any of its individual components, +in Original or Modified Versions, may be sold by itself. + +2) Original or Modified Versions of the Font Software may be bundled, +redistributed and/or sold with any software, provided that each copy +contains the above copyright notice and this license. These can be +included either as stand-alone text files, human-readable headers or +in the appropriate machine-readable metadata fields within text or +binary files as long as those fields can be easily viewed by the user. + +3) No Modified Version of the Font Software may use the Reserved Font +Name(s) unless explicit written permission is granted by the corresponding +Copyright Holder. This restriction only applies to the primary font name as +presented to the users. + +4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font +Software shall not be used to promote, endorse or advertise any +Modified Version, except to acknowledge the contribution(s) of the +Copyright Holder(s) and the Author(s) or with their explicit written +permission. + +5) The Font Software, modified or unmodified, in part or in whole, +must be distributed entirely under this license, and must not be +distributed under any other license. The requirement for fonts to +remain under this license does not apply to any document created +using the Font Software. + +TERMINATION +This license becomes null and void if any of the above conditions are +not met. + +DISCLAIMER +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT +OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE +COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL +DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM +OTHER DEALINGS IN THE FONT SOFTWARE. diff --git a/app/fonts/ttf/OFL-Montserrat.txt b/app/fonts/ttf/OFL-Montserrat.txt new file mode 100644 index 00000000..8e618c81 --- /dev/null +++ b/app/fonts/ttf/OFL-Montserrat.txt @@ -0,0 +1,93 @@ +Copyright 2024 The Montserrat.Git Project Authors (https://github.com/JulietaUla/Montserrat.git) + +This Font Software is licensed under the SIL Open Font License, Version 1.1. +This license is copied below, and is also available with a FAQ at: +https://openfontlicense.org + + +----------------------------------------------------------- +SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 +----------------------------------------------------------- + +PREAMBLE +The goals of the Open Font License (OFL) are to stimulate worldwide +development of collaborative font projects, to support the font creation +efforts of academic and linguistic communities, and to provide a free and +open framework in which fonts may be shared and improved in partnership +with others. + +The OFL allows the licensed fonts to be used, studied, modified and +redistributed freely as long as they are not sold by themselves. The +fonts, including any derivative works, can be bundled, embedded, +redistributed and/or sold with any software provided that any reserved +names are not used by derivative works. The fonts and derivatives, +however, cannot be released under any other type of license. The +requirement for fonts to remain under this license does not apply +to any document created using the fonts or their derivatives. + +DEFINITIONS +"Font Software" refers to the set of files released by the Copyright +Holder(s) under this license and clearly marked as such. This may +include source files, build scripts and documentation. + +"Reserved Font Name" refers to any names specified as such after the +copyright statement(s). + +"Original Version" refers to the collection of Font Software components as +distributed by the Copyright Holder(s). + +"Modified Version" refers to any derivative made by adding to, deleting, +or substituting -- in part or in whole -- any of the components of the +Original Version, by changing formats or by porting the Font Software to a +new environment. + +"Author" refers to any designer, engineer, programmer, technical +writer or other person who contributed to the Font Software. + +PERMISSION & CONDITIONS +Permission is hereby granted, free of charge, to any person obtaining +a copy of the Font Software, to use, study, copy, merge, embed, modify, +redistribute, and sell modified and unmodified copies of the Font +Software, subject to the following conditions: + +1) Neither the Font Software nor any of its individual components, +in Original or Modified Versions, may be sold by itself. + +2) Original or Modified Versions of the Font Software may be bundled, +redistributed and/or sold with any software, provided that each copy +contains the above copyright notice and this license. These can be +included either as stand-alone text files, human-readable headers or +in the appropriate machine-readable metadata fields within text or +binary files as long as those fields can be easily viewed by the user. + +3) No Modified Version of the Font Software may use the Reserved Font +Name(s) unless explicit written permission is granted by the corresponding +Copyright Holder. This restriction only applies to the primary font name as +presented to the users. + +4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font +Software shall not be used to promote, endorse or advertise any +Modified Version, except to acknowledge the contribution(s) of the +Copyright Holder(s) and the Author(s) or with their explicit written +permission. + +5) The Font Software, modified or unmodified, in part or in whole, +must be distributed entirely under this license, and must not be +distributed under any other license. The requirement for fonts to +remain under this license does not apply to any document created +using the Font Software. + +TERMINATION +This license becomes null and void if any of the above conditions are +not met. + +DISCLAIMER +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT +OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE +COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL +DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM +OTHER DEALINGS IN THE FONT SOFTWARE. diff --git a/app/fonts/ttf/OFL-Roboto.txt b/app/fonts/ttf/OFL-Roboto.txt new file mode 100644 index 00000000..a417551a --- /dev/null +++ b/app/fonts/ttf/OFL-Roboto.txt @@ -0,0 +1,93 @@ +Copyright 2011 The Roboto Project Authors (https://github.com/googlefonts/roboto-classic) + +This Font Software is licensed under the SIL Open Font License, Version 1.1. +This license is copied below, and is also available with a FAQ at: +https://openfontlicense.org + + +----------------------------------------------------------- +SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 +----------------------------------------------------------- + +PREAMBLE +The goals of the Open Font License (OFL) are to stimulate worldwide +development of collaborative font projects, to support the font creation +efforts of academic and linguistic communities, and to provide a free and +open framework in which fonts may be shared and improved in partnership +with others. + +The OFL allows the licensed fonts to be used, studied, modified and +redistributed freely as long as they are not sold by themselves. The +fonts, including any derivative works, can be bundled, embedded, +redistributed and/or sold with any software provided that any reserved +names are not used by derivative works. The fonts and derivatives, +however, cannot be released under any other type of license. The +requirement for fonts to remain under this license does not apply +to any document created using the fonts or their derivatives. + +DEFINITIONS +"Font Software" refers to the set of files released by the Copyright +Holder(s) under this license and clearly marked as such. This may +include source files, build scripts and documentation. + +"Reserved Font Name" refers to any names specified as such after the +copyright statement(s). + +"Original Version" refers to the collection of Font Software components as +distributed by the Copyright Holder(s). + +"Modified Version" refers to any derivative made by adding to, deleting, +or substituting -- in part or in whole -- any of the components of the +Original Version, by changing formats or by porting the Font Software to a +new environment. + +"Author" refers to any designer, engineer, programmer, technical +writer or other person who contributed to the Font Software. + +PERMISSION & CONDITIONS +Permission is hereby granted, free of charge, to any person obtaining +a copy of the Font Software, to use, study, copy, merge, embed, modify, +redistribute, and sell modified and unmodified copies of the Font +Software, subject to the following conditions: + +1) Neither the Font Software nor any of its individual components, +in Original or Modified Versions, may be sold by itself. + +2) Original or Modified Versions of the Font Software may be bundled, +redistributed and/or sold with any software, provided that each copy +contains the above copyright notice and this license. These can be +included either as stand-alone text files, human-readable headers or +in the appropriate machine-readable metadata fields within text or +binary files as long as those fields can be easily viewed by the user. + +3) No Modified Version of the Font Software may use the Reserved Font +Name(s) unless explicit written permission is granted by the corresponding +Copyright Holder. This restriction only applies to the primary font name as +presented to the users. + +4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font +Software shall not be used to promote, endorse or advertise any +Modified Version, except to acknowledge the contribution(s) of the +Copyright Holder(s) and the Author(s) or with their explicit written +permission. + +5) The Font Software, modified or unmodified, in part or in whole, +must be distributed entirely under this license, and must not be +distributed under any other license. The requirement for fonts to +remain under this license does not apply to any document created +using the Font Software. + +TERMINATION +This license becomes null and void if any of the above conditions are +not met. + +DISCLAIMER +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT +OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE +COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL +DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM +OTHER DEALINGS IN THE FONT SOFTWARE. diff --git a/app/fonts/ttf/OFL-RobotoMono.txt b/app/fonts/ttf/OFL-RobotoMono.txt new file mode 100644 index 00000000..e1651437 --- /dev/null +++ b/app/fonts/ttf/OFL-RobotoMono.txt @@ -0,0 +1,93 @@ +Copyright 2015 The Roboto Mono Project Authors (https://github.com/googlefonts/robotomono) + +This Font Software is licensed under the SIL Open Font License, Version 1.1. +This license is copied below, and is also available with a FAQ at: +https://openfontlicense.org + + +----------------------------------------------------------- +SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 +----------------------------------------------------------- + +PREAMBLE +The goals of the Open Font License (OFL) are to stimulate worldwide +development of collaborative font projects, to support the font creation +efforts of academic and linguistic communities, and to provide a free and +open framework in which fonts may be shared and improved in partnership +with others. + +The OFL allows the licensed fonts to be used, studied, modified and +redistributed freely as long as they are not sold by themselves. The +fonts, including any derivative works, can be bundled, embedded, +redistributed and/or sold with any software provided that any reserved +names are not used by derivative works. The fonts and derivatives, +however, cannot be released under any other type of license. The +requirement for fonts to remain under this license does not apply +to any document created using the fonts or their derivatives. + +DEFINITIONS +"Font Software" refers to the set of files released by the Copyright +Holder(s) under this license and clearly marked as such. This may +include source files, build scripts and documentation. + +"Reserved Font Name" refers to any names specified as such after the +copyright statement(s). + +"Original Version" refers to the collection of Font Software components as +distributed by the Copyright Holder(s). + +"Modified Version" refers to any derivative made by adding to, deleting, +or substituting -- in part or in whole -- any of the components of the +Original Version, by changing formats or by porting the Font Software to a +new environment. + +"Author" refers to any designer, engineer, programmer, technical +writer or other person who contributed to the Font Software. + +PERMISSION & CONDITIONS +Permission is hereby granted, free of charge, to any person obtaining +a copy of the Font Software, to use, study, copy, merge, embed, modify, +redistribute, and sell modified and unmodified copies of the Font +Software, subject to the following conditions: + +1) Neither the Font Software nor any of its individual components, +in Original or Modified Versions, may be sold by itself. + +2) Original or Modified Versions of the Font Software may be bundled, +redistributed and/or sold with any software, provided that each copy +contains the above copyright notice and this license. These can be +included either as stand-alone text files, human-readable headers or +in the appropriate machine-readable metadata fields within text or +binary files as long as those fields can be easily viewed by the user. + +3) No Modified Version of the Font Software may use the Reserved Font +Name(s) unless explicit written permission is granted by the corresponding +Copyright Holder. This restriction only applies to the primary font name as +presented to the users. + +4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font +Software shall not be used to promote, endorse or advertise any +Modified Version, except to acknowledge the contribution(s) of the +Copyright Holder(s) and the Author(s) or with their explicit written +permission. + +5) The Font Software, modified or unmodified, in part or in whole, +must be distributed entirely under this license, and must not be +distributed under any other license. The requirement for fonts to +remain under this license does not apply to any document created +using the Font Software. + +TERMINATION +This license becomes null and void if any of the above conditions are +not met. + +DISCLAIMER +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT +OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE +COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL +DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM +OTHER DEALINGS IN THE FONT SOFTWARE. diff --git a/app/fonts/ttf/Roboto-Bold.ttf b/app/fonts/ttf/Roboto-Bold.ttf new file mode 100644 index 00000000..65161856 Binary files /dev/null and b/app/fonts/ttf/Roboto-Bold.ttf differ diff --git a/app/fonts/ttf/Roboto-Light.ttf b/app/fonts/ttf/Roboto-Light.ttf new file mode 100644 index 00000000..ee7268fd Binary files /dev/null and b/app/fonts/ttf/Roboto-Light.ttf differ diff --git a/app/fonts/ttf/Roboto-Regular.ttf b/app/fonts/ttf/Roboto-Regular.ttf new file mode 100644 index 00000000..3db0d1fb Binary files /dev/null and b/app/fonts/ttf/Roboto-Regular.ttf differ diff --git a/app/fonts/ttf/RobotoMono-Bold.ttf b/app/fonts/ttf/RobotoMono-Bold.ttf new file mode 100644 index 00000000..bef439f3 Binary files /dev/null and b/app/fonts/ttf/RobotoMono-Bold.ttf differ diff --git a/app/fonts/ttf/RobotoMono-Light.ttf b/app/fonts/ttf/RobotoMono-Light.ttf new file mode 100644 index 00000000..b6fb475d Binary files /dev/null and b/app/fonts/ttf/RobotoMono-Light.ttf differ diff --git a/app/fonts/ttf/RobotoMono-Regular.ttf b/app/fonts/ttf/RobotoMono-Regular.ttf new file mode 100644 index 00000000..3806bfb1 Binary files /dev/null and b/app/fonts/ttf/RobotoMono-Regular.ttf differ diff --git a/app/g2/dashboard-controller.ts b/app/g2/dashboard-controller.ts index ec2f841b..8a1a2099 100644 --- a/app/g2/dashboard-controller.ts +++ b/app/g2/dashboard-controller.ts @@ -18,7 +18,7 @@ import { voiceControlBridge } from "../native/voice-control"; import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../graphics/image"; import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../graphics/plane"; import { prepareFrameDraws } from "../graphics/glyph-wire"; -import { getDefaultMediumFont } from "../graphics/bdffont"; +import { getDefaultMediumFont } from "../graphics/ui-fonts"; import { wrapText } from "../graphics/textwrap"; import { rawInputEventToInputEvent, shell, type ShellInputOutcome } from "../ui/shell/shell"; import { registerSystemTools } from "../assistant/system-tools"; @@ -1427,6 +1427,13 @@ class DashboardController { submitFrame: (planes, paintMs, frameId) => this.submitWindowFrame(surfaceId, planes, paintMs, frameId), setSurfaceVisible: (visible) => this.setWindowSurfaceVisible(surfaceId, visible), removeSurface: () => this.removeWindowSurface(surfaceId), + reconfigureSurface: (heightMode) => { + // Resize the surface rect to the new band; a foreground window stays + // visible. The shell re-renders so the chrome (top bar) follows. + const visible = shell.foregroundWindow()?.windowId === windowId; + void this.configureWindowSurface(surfaceId, visible, heightMode); + this.requestShellRender(); + }, onClosed: () => { this.inProcessApps.delete(windowId); }, diff --git a/app/graphics/bdffont.ts b/app/graphics/bdffont.ts index 563007eb..8bf9dfec 100644 --- a/app/graphics/bdffont.ts +++ b/app/graphics/bdffont.ts @@ -1,5 +1,5 @@ import { knownFolders } from "@nativescript/core"; -import { getStringSetting, onSettingsStoreChanged } from "~/native/settings-store"; +import type { GrayImage } from "./image"; export type Glyph = { encoding: number; @@ -9,17 +9,35 @@ export type Glyph = { bbxX: number; bbxY: number; bitmapRows: number[]; + /** + * Antialiased coverage, bbxWidth*bbxHeight 4bpp levels (0..15) row-major. + * When present the glyph is an AA raster (e.g. a TTF render; see + * ttf-font.ts) and bitmapRows is unused/empty; when absent the glyph is + * 1bpp via bitmapRows. + */ + coverage?: Uint8Array; + /** Exact (fractional) advance in pixels; dwidthX is its integer stand-in. */ + advance?: number; }; let nextFontFingerprintId = 1; +/** + * Allocate a process-local font fingerprint id. Shared by every font class + * that participates in PlacedGlyph fingerprints (BdfFont, TtfFont) so ids + * never collide across font kinds. + */ +export function allocateFontFingerprintId(): number { + return nextFontFingerprintId++; +} + export class BdfFont { /** * Process-local identity for content fingerprints (fonts are cached * singletons, so two draws with the same id + encoding are the same glyph * bitmap). Only meaningful within one JS context; never persist it. */ - readonly fingerprintId = nextFontFingerprintId++; + readonly fingerprintId = allocateFontFingerprintId(); /** * Stable cross-context identity for the on-glasses glyph atlas (the * embedded font name). Worker threads and the main thread must agree on @@ -165,6 +183,25 @@ export class BdfFont { } return width; } + + /** + * Draw text with integer advances, recording each glyph as a deferred draw + * on the image (UiFont contract; y is the top of the line). + */ + drawText(image: GrayImage, x: number, y: number, text: string, value: number): void { + let cursorX = x; + for (const char of text) { + if (char === "\n") { + cursorX = x; + y += this.lineHeight; + continue; + } + const glyph = this.getGlyph(char.codePointAt(0) ?? 32); + if (!glyph) continue; + image.drawGlyph(this, glyph, cursorX, y, value); + cursorX += glyph.dwidthX; + } + } } const embeddedFonts = { @@ -211,46 +248,6 @@ export function getFont(font: EmbeddedFontName): BdfFont { return loadEmbeddedFont(font); } -// User-selectable UI typeface. Only the small (12px) font has an alternative, -// since TerminusV ships no larger sizes; medium/large stay Terminus. The -// storage key and values are re-used by the settings UI in dashboard-settings. -export const UI_FONT_SETTING_KEY = "display.uiFont"; -export const UI_FONT_VALUES = ["terminus", "terminusv"] as const; -export type UiFontChoice = (typeof UI_FONT_VALUES)[number]; - -const SMALL_FONT_FOR_CHOICE: Record = { - terminus: "terminus12", - terminusv: "terminusv12", -}; - -// Reading the setting hits a JNI bridge, and getDefaultSmallFont() is called -// once per menu item per paint, so cache the resolved choice and invalidate it -// when the setting changes (in any isolate). -let cachedSmallFontChoice: UiFontChoice | null = null; -let smallFontListenerInstalled = false; - -function currentSmallFontName(): EmbeddedFontName { - if (!smallFontListenerInstalled) { - onSettingsStoreChanged((key) => { - if (key === UI_FONT_SETTING_KEY) cachedSmallFontChoice = null; - }); - smallFontListenerInstalled = true; - } - if (cachedSmallFontChoice === null) { - const raw = getStringSetting(UI_FONT_SETTING_KEY, "terminus"); - cachedSmallFontChoice = (UI_FONT_VALUES as readonly string[]).includes(raw) - ? (raw as UiFontChoice) - : "terminus"; - } - return SMALL_FONT_FOR_CHOICE[cachedSmallFontChoice]; -} - -export function getDefaultSmallFont(): BdfFont { - return getFont(currentSmallFontName()); -} -export function getDefaultMediumFont(): BdfFont { - return getFont("terminus16"); -} -export function getDefaultLargeFont(): BdfFont { - return getFont("terminus24"); -} +// NOTE: the default UI font getters (getDefaultSmallFont & co.) live in +// ./ui-fonts, which resolves the user's font settings to a BdfFont or +// TtfFont. This module stays a pure BDF loader. diff --git a/app/graphics/evenhub-font.ts b/app/graphics/evenhub-font.ts index 3ed00e48..fe2736db 100644 --- a/app/graphics/evenhub-font.ts +++ b/app/graphics/evenhub-font.ts @@ -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, type FwTextFont, type PlacedFwText, type PlacedFwTextGlyph } from "./image"; +import { GrayImage, grayToNibble, type FwTextFont, type PlacedFwText, type PlacedFwTextGlyph } from "./image"; declare const com: any; @@ -83,9 +83,7 @@ function inMode15UncertainRange(cp: number): boolean { } /** 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); -} +const quantNibble = grayToNibble; export class EvenHubFont implements FwTextFont { /** One builtin firmware font today; see FwTextFont. */ diff --git a/app/graphics/glyph-wire.ts b/app/graphics/glyph-wire.ts index 5e45d7c6..3842c499 100644 --- a/app/graphics/glyph-wire.ts +++ b/app/graphics/glyph-wire.ts @@ -19,8 +19,8 @@ * 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 PlacedFwText, type PlacedImage } from "./image"; +import { Glyph } from "./bdffont"; +import { GrayImage, type DeferredDraw, type GlyphFont, type PlacedFwText, type PlacedImage } from "./image"; declare const com: any; @@ -36,7 +36,7 @@ type FontWireState = { registered: Set; }; -const fontStates = new Map(); +const fontStates = new Map(); /** * Java-side image id per source image object, or null for images that can't @@ -46,7 +46,7 @@ const fontStates = new Map(); */ const imageIds = new WeakMap(); -function fontWireState(font: BdfFont): FontWireState | null { +function fontWireState(font: GlyphFont): FontWireState | null { if (!global.isAndroid || !font.atlasKey) return null; if (font.lineHeight <= 0 || font.lineHeight > 255) return null; let state = fontStates.get(font); @@ -67,7 +67,7 @@ function fontWireState(font: BdfFont): FontWireState | null { * 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 { +function representableGlyph(font: GlyphFont, 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; @@ -155,7 +155,7 @@ export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer | if (!global.isAndroid || draws.length === 0) return null; // Pass 1: resolve ids, register unseen rasters, size the buffer. - let registration: Map | null = null; + let registration: Map | null = null; const fwRunOk = new Map(); let bytes = 0; for (const placed of draws) { @@ -169,10 +169,10 @@ export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer | registration ??= new Map(); let group = registration.get(state); if (!group) { - group = { font: placed.font, glyphs: [] }; + group = { font: placed.font, glyphs: [], aaGlyphs: [] }; registration.set(state, group); } - group.glyphs.push(placed.glyph); + (placed.glyph.coverage ? group.aaGlyphs : group.glyphs).push(placed.glyph); } else if (placed.kind === "image") { if (!inRange16(placed.x) || !inRange16(placed.y)) continue; if (imageId(placed) !== null) bytes += IMAGE_RECORD_BYTES; @@ -183,7 +183,10 @@ export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer | } } if (registration) { - com.faceclaw.app.GlyphAtlas.register(buildRegistrationBuffer(registration)); + const plainBuffer = buildRegistrationBuffer(registration); + if (plainBuffer) com.faceclaw.app.GlyphAtlas.register(plainBuffer); + const aaBuffer = buildAaRegistrationBuffer(registration); + if (aaBuffer) com.faceclaw.app.GlyphAtlas.registerAa(aaBuffer); } if (bytes === 0) return null; @@ -230,51 +233,129 @@ export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer | return out.buffer; } +type RegistrationGroup = { + font: GlyphFont; + /** 1bpp (BDF) glyphs for GlyphAtlas.register. */ + glyphs: Glyph[]; + /** Antialiased (coverage) glyphs for GlyphAtlas.registerAa. */ + aaGlyphs: Glyph[]; +}; + /** - * Registration buffer: per font group + * 1bpp 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. + * mirroring GlyphAtlas.register on the Java side. Null when the groups hold + * no 1bpp glyphs. */ function buildRegistrationBuffer( - registration: Map, -): ArrayBuffer { - let total = 0; - const encodedKeys = new Map(); - 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); + registration: Map, +): ArrayBuffer | null { + return buildGroupedBuffer( + registration, + (group) => group.glyphs, + (glyph) => 8 + 4 * glyph.bbxHeight, + (view, _bytes, offset, group, glyph) => { + writeGlyphHeader(view, offset, group, glyph); offset += 8; for (let row = 0; row < glyph.bbxHeight; row++) { view.setUint32(offset, (glyph.bitmapRows[row] ?? 0) >>> 0, true); offset += 4; } + return offset; + }, + ); +} + +/** + * AA registration buffer: same group framing, but each glyph record carries + * packed 4bpp coverage instead of 1bpp rows: + * [encoding u32][bbxX s8][inkTop u8][width u8][inkHeight u8] + * [inkHeight x ceil(width/2) packed bytes, high nibble = left pixel] + * mirroring GlyphAtlas.registerAa. Null when the groups hold no AA glyphs. + */ +function buildAaRegistrationBuffer( + registration: Map, +): ArrayBuffer | null { + return buildGroupedBuffer( + registration, + (group) => group.aaGlyphs, + (glyph) => 8 + ((glyph.bbxWidth + 1) >> 1) * glyph.bbxHeight, + (view, bytes, offset, group, glyph) => { + writeGlyphHeader(view, offset, group, glyph); + offset += 8; + const coverage = glyph.coverage!; + const stride = (glyph.bbxWidth + 1) >> 1; + for (let row = 0; row < glyph.bbxHeight; row++) { + const rowStart = row * glyph.bbxWidth; + for (let b = 0; b < stride; b++) { + const col = b * 2; + const high = coverage[rowStart + col]! & 0x0f; + const low = col + 1 < glyph.bbxWidth ? coverage[rowStart + col + 1]! & 0x0f : 0; + bytes[offset++] = (high << 4) | low; + } + } + return offset; + }, + ); +} + +/** [encoding u32][bbxX s8][inkTop u8][width u8][inkHeight u8], shared by both formats. */ +function writeGlyphHeader(view: DataView, offset: number, group: RegistrationGroup, glyph: Glyph): void { + 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); +} + +/** Shared group-framing walk for the two registration formats. */ +function buildGroupedBuffer( + registration: Map, + glyphsOf: (group: RegistrationGroup) => Glyph[], + glyphBytes: (glyph: Glyph) => number, + writeGlyph: ( + view: DataView, + bytes: Uint8Array, + offset: number, + group: RegistrationGroup, + glyph: Glyph, + ) => number, +): ArrayBuffer | null { + let total = 0; + let glyphCount = 0; + const encodedKeys = new Map(); + registration.forEach((group, state) => { + const glyphs = glyphsOf(group); + if (glyphs.length === 0) return; + const keyBytes = utf8Encode(group.font.atlasKey ?? ""); + encodedKeys.set(state, keyBytes); + total += 1 + keyBytes.length + 1 + 2; + glyphCount += glyphs.length; + for (const glyph of glyphs) { + total += glyphBytes(glyph); + } + }); + if (glyphCount === 0) return null; + const buffer = new ArrayBuffer(total); + const view = new DataView(buffer); + const bytes = new Uint8Array(buffer); + let offset = 0; + registration.forEach((group, state) => { + const glyphs = glyphsOf(group); + if (glyphs.length === 0) return; + 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, glyphs.length, true); + offset += 3; + for (const glyph of glyphs) { + offset = writeGlyph(view, bytes, offset, group, glyph); } }); return buffer; diff --git a/app/graphics/image.ts b/app/graphics/image.ts index efcf8c03..85746e0e 100644 --- a/app/graphics/image.ts +++ b/app/graphics/image.ts @@ -1,4 +1,4 @@ -import { BdfFont, Glyph } from "./bdffont"; +import { Glyph } from "./bdffont"; import { wrapText } from "./textwrap"; export const G2_LENS_WIDTH = 640; @@ -21,6 +21,35 @@ export function imageFromAsciiArt(lines: readonly string[], value = 255): GrayIm return image; } +/** + * The font behind a deferred glyph draw: what rasterizeGlyph and the wire + * encoder (glyph-wire.ts) need from it. BdfFont satisfies this directly; + * TtfFont (ttf-font.ts) implements it for antialiased glyphs. Structural so + * image.ts needs no imports beyond BdfFont's Glyph shape. + */ +export interface GlyphFont { + /** Process-local identity for content fingerprints (see BdfFont). */ + readonly fingerprintId: number; + /** Stable cross-context identity for the on-glasses glyph atlas, or null. */ + readonly atlasKey: string | null; + readonly ascent: number; + readonly lineHeight: number; +} + +/** + * A general text-drawing font: what GrayImage.drawText and the shared list + * UI helpers need. BdfFont (bitmap) and TtfFont (antialiased) both implement + * it. The font owns text layout (drawText) because layout rules differ by + * kind — BDF advances are integers, TTF advances are fractional and kerned. + */ +export interface UiFont extends GlyphFont { + readonly descent: number; + measureText(text: string): number; + getGlyph(codePoint: number): Glyph | undefined; + hasGlyph(codePoint: number): boolean; + drawText(image: GrayImage, x: number, y: number, text: string, value: number): void; +} + /** * One deferred glyph draw. Text drawn through drawGlyph/drawText/ * drawTextWrapped is retained here rather than baked into the pixel buffer, @@ -30,7 +59,7 @@ export function imageFromAsciiArt(lines: readonly string[], value = 255): GrayIm */ export type PlacedGlyph = { kind: "glyph"; - font: BdfFont; + font: GlyphFont; glyph: Glyph; x: number; y: number; @@ -202,23 +231,12 @@ export class GrayImage { } } - drawText(font: BdfFont, x: number, y: number, text: string, value: number): void { - let cursorX = x; - for (const char of text) { - if (char === "\n") { - cursorX = x; - y += font.lineHeight; - continue; - } - const glyph = font.getGlyph(char.codePointAt(0) ?? 32); - if (!glyph) continue; - this.drawGlyph(font, glyph, cursorX, y, value); - cursorX += glyph.dwidthX; - } + drawText(font: UiFont, x: number, y: number, text: string, value: number): void { + font.drawText(this, x, y, text, value); } drawTextWrapped({font, x, y, width, text, value }: { - font: BdfFont; + font: UiFont; x: number; y: number; width: number; @@ -227,7 +245,7 @@ export class GrayImage { }): void { const lines = wrapText(font, text, width); for (let i = 0; i < lines.length; i++) { - this.drawText(font, x, y + i * font.lineHeight, lines[i]!, value); + font.drawText(this, x, y + i * font.lineHeight, lines[i]!, value); } } @@ -414,7 +432,7 @@ export class GrayImage { * (rendered above all raster pixels) instead of being baked into the * buffer. */ - drawGlyph(font: BdfFont, glyph: Glyph, x: number, y: number, value: number): void { + drawGlyph(font: GlyphFont, glyph: Glyph, x: number, y: number, value: number): void { this.drawList.push({ kind: "glyph", font, glyph, x, y, value: clampByte(value) }); } @@ -492,10 +510,19 @@ export class GrayImage { } } -/** Bake one BDF glyph into an image's pixel buffer (y is the top of the line). */ +/** + * The 4bpp level an 8-bit gray value packs to (BmpUtil's GRAY_TO_NIBBLE). + * Shared by every phone-side bake that must match the firmware's draw-time + * LUT math bit-for-bit (AA glyphs here, EvenHubFont's mode-15 bakes). + */ +export function grayToNibble(value: number): number { + return value <= 0 ? 0 : Math.min(15, (value + 8) >> 4); +} + +/** Bake one glyph into an image's pixel buffer (y is the top of the line). */ function rasterizeGlyph( target: GrayImage, - font: BdfFont, + font: GlyphFont, glyph: Glyph, x: number, y: number, @@ -504,6 +531,24 @@ function rasterizeGlyph( const baselineY = y + font.ascent; const top = baselineY - (glyph.bbxHeight + glyph.bbxY); const left = x + glyph.bbxX; + if (glyph.coverage) { + // Firmware-exact AA shading (see EvenHubFont.drawGlyph): the CFW draws + // cached glyphs through a LUT mapping source nibble n to n*top/15 + // (integer), skipping only n == 0. Writing 16*level makes the composite + // quantize (grayToNibble) back to exactly that level, so the + // texture-cache planner's pixel checks — and the shadow after an + // on-glasses mode-14 draw — match this bake bit-for-bit. + const top4 = grayToNibble(value); + for (let row = 0; row < glyph.bbxHeight; row++) { + const rowStart = row * glyph.bbxWidth; + for (let col = 0; col < glyph.bbxWidth; col++) { + const nibble = glyph.coverage[rowStart + col]!; + if (nibble === 0) continue; + target.setPixel(left + col, top + row, Math.floor((nibble * top4) / 15) * 16); + } + } + return; + } const rowBitWidth = ((glyph.bbxWidth + 7) >> 3) << 3; for (let row = 0; row < glyph.bbxHeight; row++) { const bits = glyph.bitmapRows[row] ?? 0; diff --git a/app/graphics/installed-fonts.ts b/app/graphics/installed-fonts.ts new file mode 100644 index 00000000..7a96b09d --- /dev/null +++ b/app/graphics/installed-fonts.ts @@ -0,0 +1,227 @@ +/** + * The app-internal installed-fonts directory: the set of TTF/OTF faces the + * font picker offers. Fonts arrive two ways — bundled faces copied out of the + * app assets on first listing (so the directory is the single source of + * truth), and user installs from the Files app ("Install" on a font file). + * + * Listing parses each file's name table for family/style (via the Java + * FontFileRenderer) and probes monospace-ness by comparing advances, both + * memoized per path for the session. + */ +import { File, Folder, knownFolders, path as nsPath } from "@nativescript/core"; +import { canLoadFontFile, fontFileDisplayName, isFontFile } from "../native/font-files"; + +declare const com: any; +declare const java: any; +declare const global: any; + +/** Bundled faces (under app assets fonts/ttf/) preinstalled on first listing. */ +const PREINSTALLED_FONT_FILES = [ + "Inter_18pt-Light.ttf", + "Inter_18pt-Regular.ttf", + "Inter_18pt-Bold.ttf", + "Montserrat-Light.ttf", + "Montserrat-Regular.ttf", + "Montserrat-Bold.ttf", + "Roboto-Light.ttf", + "Roboto-Regular.ttf", + "Roboto-Bold.ttf", + "RobotoMono-Light.ttf", + "RobotoMono-Regular.ttf", + "RobotoMono-Bold.ttf", +] as const; + +export type InstalledFont = { + /** Absolute path, loadable by TtfFont / FontFileRenderer. */ + path: string; + fileName: string; + /** Family name from the font's name table (filename stem as fallback). */ + family: string; + /** Style name from the name table ("Light", "Bold", ... ; "" = regular). */ + style: string; + /** Sort key for weight pickers (400 = regular; +1000 marks italics last). */ + weightOrder: number; + monospace: boolean; +}; + +/** The app-internal fonts directory (created on first use). */ +export function installedFontsDir(): string { + return knownFolders.documents().getFolder("fonts").path; +} + +/** Absolute path of an installed font by filename (existence not checked). */ +export function installedFontPath(fileName: string): string { + return nsPath.join(installedFontsDir(), fileName); +} + +let preinstalledEnsured = false; + +/** + * Copy any missing bundled faces into the installed-fonts directory. Cheap + * after the first run (existence checks only), so callers can invoke it + * before every listing. + */ +export function ensurePreinstalledFonts(): void { + if (preinstalledEnsured || !global.isAndroid) return; + const assetsDir = nsPath.join(knownFolders.currentApp().path, "fonts/ttf"); + for (const fileName of PREINSTALLED_FONT_FILES) { + const target = installedFontPath(fileName); + if (File.exists(target)) continue; + const source = nsPath.join(assetsDir, fileName); + if (!File.exists(source)) continue; + try { + copyFileBytes(source, target); + } catch (error) { + console.warn(`Could not preinstall font ${fileName}: ${error}`); + } + } + preinstalledEnsured = true; +} + +/** + * Install a font file into the installed-fonts directory (overwriting a + * same-named file). Returns an error message for the UI, or null on success. + */ +export function installFontFile(sourcePath: string): string | null { + if (!global.isAndroid) return "Not available on this platform."; + if (!isFontFile(sourcePath)) return "Not a font file."; + if (!canLoadFontFile(sourcePath)) return "This file could not be loaded as a font."; + const fileName = sourcePath.slice(sourcePath.lastIndexOf("/") + 1); + try { + copyFileBytes(sourcePath, installedFontPath(fileName)); + metadataCache.delete(installedFontPath(fileName)); + return null; + } catch (error) { + console.warn(`installFontFile failed for ${sourcePath}: ${error}`); + return "Could not copy the font file."; + } +} + +/** Whether a same-named font is already installed. */ +export function isFontInstalled(sourcePath: string): boolean { + const fileName = sourcePath.slice(sourcePath.lastIndexOf("/") + 1); + return File.exists(installedFontPath(fileName)); +} + +/** + * All installed fonts (preinstalling bundled faces first), sorted by family + * then weight. Unloadable files are skipped. + */ +export function listInstalledFonts(): InstalledFont[] { + if (!global.isAndroid) return []; + ensurePreinstalledFonts(); + const fonts: InstalledFont[] = []; + try { + for (const entity of Folder.fromPath(installedFontsDir()).getEntitiesSync()) { + if (!isFontFile(entity.name)) continue; + const meta = fontMetadata(entity.path, entity.name); + if (meta) fonts.push(meta); + } + } catch (error) { + console.warn(`listInstalledFonts failed: ${error}`); + } + fonts.sort((a, b) => + a.family.localeCompare(b.family) || a.weightOrder - b.weightOrder || a.style.localeCompare(b.style), + ); + return fonts; +} + +/** The installed font with this filename, or null. */ +export function getInstalledFont(fileName: string): InstalledFont | null { + const path = installedFontPath(fileName); + if (!File.exists(path)) return null; + return fontMetadata(path, fileName); +} + +const metadataCache = new Map(); + +function fontMetadata(path: string, fileName: string): InstalledFont | null { + const cached = metadataCache.get(path); + if (cached !== undefined) return cached; + let meta: InstalledFont | null = null; + if (canLoadFontFile(path)) { + const name = fontFileDisplayName(path); + const family = name?.family || fileName.replace(/\.[^.]+$/, ""); + const style = normalizeStyle(name?.style ?? ""); + meta = { + path, + fileName, + family, + style, + weightOrder: styleWeightOrder(style), + monospace: isMonospaceFont(path), + }; + } + metadataCache.set(path, meta); + return meta; +} + +function normalizeStyle(style: string): string { + const trimmed = style.trim(); + return trimmed === "Regular" ? "" : trimmed; +} + +/** OS/2-style weight for a style name; italics sort after all uprights. */ +function styleWeightOrder(style: string): number { + const withoutItalic = style.replace(/\bitalic\b/i, "").trim().toLowerCase(); + const weights: Record = { + "thin": 100, + "extralight": 200, + "extra light": 200, + "ultralight": 200, + "light": 300, + "": 400, + "regular": 400, + "normal": 400, + "medium": 500, + "semibold": 600, + "semi bold": 600, + "demibold": 600, + "bold": 700, + "extrabold": 800, + "extra bold": 800, + "black": 900, + "heavy": 900, + }; + const weight = weights[withoutItalic] ?? 400; + return /\bitalic\b/i.test(style) ? weight + 1000 : weight; +} + +/** + * Whether the face is monospace: narrow, wide, and punctuation glyphs all + * measure the same advance. Measured at a large size so fractional advance + * differences show up clearly. + */ +function isMonospaceFont(path: string): boolean { + try { + const measure = (text: string) => + Number(com.faceclaw.app.FontFileRenderer.measureTextExact(path, text, 48)); + const narrow = measure("i"); + if (!(narrow > 0)) return false; + return ( + Math.abs(measure("W") - narrow) < 0.5 && + Math.abs(measure("m") - narrow) < 0.5 && + Math.abs(measure(".") - narrow) < 0.5 + ); + } catch { + return false; + } +} + +// Plain stream/channel copy: java.nio.file.Paths.get is a varargs overload +// set that NativeScript's JNI overload resolution picks wrong (it tries the +// (URI) form for a JS string), so java.nio.file is avoided here. +function copyFileBytes(source: string, target: string): void { + const input = new java.io.FileInputStream(source); + try { + const output = new java.io.FileOutputStream(target); + try { + const inChannel = input.getChannel(); + output.getChannel().transferFrom(inChannel, 0, inChannel.size()); + } finally { + output.close(); + } + } finally { + input.close(); + } +} diff --git a/app/graphics/textwrap.ts b/app/graphics/textwrap.ts index 8a9df153..3d2d4ff5 100644 --- a/app/graphics/textwrap.ts +++ b/app/graphics/textwrap.ts @@ -1,4 +1,13 @@ -import { BdfFont } from "./bdffont"; +/** + * What wrapping and truncation need from a font. BdfFont and TtfFont both + * satisfy it structurally: getGlyph feeds per-codepoint advances for wrap + * measurement (integer and kerning-free, so slightly conservative for kerned + * faces), measureText feeds truncation. + */ +export interface WrapFont { + measureText(text: string): number; + getGlyph(codePoint: number): { dwidthX: number } | undefined; +} export type WrapTextOptions = { preserveLeadingWhitespace?: boolean; @@ -34,7 +43,7 @@ export function findWrapOpportunities(text: string): number[] { return offsets; } -export function wrapText(font: BdfFont, text: string, targetWidth: number, opts: WrapTextOptions = {}): string[] { +export function wrapText(font: WrapFont, text: string, targetWidth: number, opts: WrapTextOptions = {}): string[] { if (targetWidth <= 0) { return text.length ? text.split("\n") : [""]; } @@ -47,7 +56,7 @@ export function wrapText(font: BdfFont, text: string, targetWidth: number, opts: return lines.length ? lines : [""]; } -export function truncateText(font: BdfFont, text: string, maxWidth: number): string { +export function truncateText(font: WrapFont, text: string, maxWidth: number): string { if (font.measureText(text) <= maxWidth) return text; let out = text; while (out.length > 1 && font.measureText(`${out}...`) > maxWidth) { @@ -56,7 +65,7 @@ export function truncateText(font: BdfFont, text: string, maxWidth: number): str return `${out}...`; } -export function truncateLeft(font: BdfFont, text: string, maxWidth: number): string { +export function truncateLeft(font: WrapFont, text: string, maxWidth: number): string { if (font.measureText(text) <= maxWidth) return text; let out = text; while (out.length > 1 && font.measureText(`...${out}`) > maxWidth) { @@ -65,7 +74,7 @@ export function truncateLeft(font: BdfFont, text: string, maxWidth: number): str return `...${out}`; } -function wrapParagraph(font: BdfFont, paragraph: string, targetWidth: number, opts: WrapTextOptions): string[] { +function wrapParagraph(font: WrapFont, paragraph: string, targetWidth: number, opts: WrapTextOptions): string[] { if (paragraph.length === 0) { return [""]; } @@ -109,7 +118,7 @@ function wrapParagraph(font: BdfFont, paragraph: string, targetWidth: number, op return lines.length ? lines : [""]; } -function measureOffsets(font: BdfFont, text: string): Map { +function measureOffsets(font: WrapFont, text: string): Map { const widths = new Map(); let total = 0; widths.set(0, 0); diff --git a/app/graphics/ttf-font.ts b/app/graphics/ttf-font.ts new file mode 100644 index 00000000..55094d0a --- /dev/null +++ b/app/graphics/ttf-font.ts @@ -0,0 +1,285 @@ +/** + * TTF/OTF fonts for on-glasses UI text, rendered per-codepoint through the + * Java FontFileRenderer (Android minikin/HarfBuzz: real shaping, kerning, + * antialiasing) and cached as 4bpp coverage rasters that ride the + * texture-cache glyph pipeline (CFW mode 14; see + * notes/texture-cache-display-list-design.md and graphics/glyph-wire.ts). + * + * Layout model: the font owns drawText (like EvenHubFont) because TTF + * advances are fractional and kerned, which GrayImage.drawText's integer + * cursor walk can't express. Each glyph is recorded as a deferred + * PlacedGlyph at an integer pen x, so the wire path and the phone-side bake + * (rasterizeGlyph's coverage branch, firmware-LUT-exact) see identical + * positions; the fractional pen accumulates across the line so rounding + * never drifts. + * + * Kerning uses memoized per-pair measurements (measure("ab") - a - b); + * ligatures are disabled Java-side so per-codepoint rasters plus pairwise + * kerning fully describe a line. Glyph rasters and advances come from one + * JNI render per (font, size, codepoint) per JS context, memoized forever. + * + * Cross-context determinism: worker threads and the main thread each build + * their own TtfFont, but the Java GlyphAtlas dedupes by atlasKey and treats + * rasters as immutable — safe because renderGlyphCell is deterministic for a + * given (path, size, gamma), all of which are baked into the atlasKey. + */ +import { knownFolders } from "@nativescript/core"; +import { allocateFontFingerprintId, type Glyph } from "./bdffont"; +import { GrayImage, grayToNibble, type GlyphFont } from "./image"; +import { toUint8Array } from "../util/array-util"; + +declare const com: any; +declare const global: any; + +/** + * Coverage-to-shade gamma (out = coverage^gamma). The G2 display response + * looks roughly linear, so 1.0; part of the atlas identity, so changing it + * per-font is safe but changing it per-draw is not possible. + */ +const DEFAULT_TTF_GAMMA = 1.0; + +/** Fonts bundled as app assets (paths relative to the app folder). */ +const BUNDLED_FONTS = { + "inter-light": "fonts/ttf/Inter_18pt-Light.ttf", +} as const; +export type BundledTtfName = keyof typeof BUNDLED_FONTS; + +const loadedFonts = new Map(); + +export class TtfFont implements GlyphFont { + readonly fingerprintId = allocateFontFingerprintId(); + /** Stable cross-context atlas identity: face + size + gamma. */ + readonly atlasKey: string; + readonly ascent: number; + readonly descent: number; + readonly lineHeight: number; + private readonly glyphCache = new Map(); + private readonly kernCache = new Map(); + + private constructor( + readonly path: string, + readonly sizePx: number, + readonly gamma: number, + ascent: number, + descent: number, + ) { + this.ascent = ascent; + this.descent = descent; + this.lineHeight = ascent + descent; + const basename = path.slice(path.lastIndexOf("/") + 1).replace(/\.[^.]+$/, ""); + this.atlasKey = `ttf:${basename}:${fnv32(path).toString(36)}@${sizePx}g${gamma}`; + } + + /** + * Load (or return the memoized) font at a pixel size. Null when the font + * file is missing/unloadable or off-Android — callers fall back to a BDF + * face. + */ + static load(path: string, sizePx: number, gamma = DEFAULT_TTF_GAMMA): TtfFont | null { + const key = `${path}@${sizePx}g${gamma}`; + const cached = loadedFonts.get(key); + if (cached !== undefined) return cached; + let font: TtfFont | null = null; + if (global.isAndroid && sizePx > 0) { + try { + const raw = String(com.faceclaw.app.FontFileRenderer.getFontMetrics(path, sizePx) ?? ""); + const parts = raw.split(" ").map((part) => parseInt(part, 10)); + if (parts.length === 3 && parts.every((part) => Number.isFinite(part))) { + const [ascent, descent] = parts as [number, number, number]; + if (ascent + descent > 0 && ascent + descent <= 255) { + font = new TtfFont(path, sizePx, gamma, ascent, descent); + } + } + } catch (error) { + console.warn(`TtfFont.load failed for ${path}: ${error}`); + } + } + loadedFonts.set(key, font); + return font; + } + + /** A font bundled with the app, or null when it can't load. */ + static bundled(name: BundledTtfName, sizePx: number, gamma = DEFAULT_TTF_GAMMA): TtfFont | null { + if (!global.isAndroid) return null; + return TtfFont.load(`${knownFolders.currentApp().path}/${BUNDLED_FONTS[name]}`, sizePx, gamma); + } + + /** + * The codepoint's glyph (rendered and memoized on first use), or undefined + * when the render fails outright. Android renders SOMETHING for any + * codepoint the face covers or tofu otherwise, so undefined is rare. + * Ink-free glyphs (space) have bbxWidth 0 and no coverage. + */ + getGlyph(codePoint: number): Glyph | undefined { + let glyph = this.glyphCache.get(codePoint); + if (glyph === undefined) { + glyph = this.loadGlyph(codePoint); + this.glyphCache.set(codePoint, glyph); + } + return glyph ?? undefined; + } + + hasGlyph(codePoint: number): boolean { + return this.getGlyph(codePoint) !== undefined; + } + + /** + * Pixel width of the widest line in `text` (fractional; kerning included), + * for wrapping and truncation. + */ + measureText(text: string): number { + const cps = toCodePoints(text); + let width = 0; + let maxWidth = 0; + for (let i = 0; i < cps.length; i++) { + const cp = cps[i]!; + if (cp === 10) { + maxWidth = Math.max(maxWidth, width); + width = 0; + continue; + } + width += this.advanceOf(cp); + const next = cps[i + 1]; + if (next !== undefined && next !== 10) { + width += this.kernBetween(cp, next); + } + } + return Math.max(maxWidth, width); + } + + /** + * Draw text with the font's own (fractional, kerned) layout, recording + * each inked glyph as a deferred draw on the image. y is the top of the + * line (baseline at y + ascent), matching GrayImage.drawText. + */ + drawText(image: GrayImage, x: number, y: number, text: string, value: number): void { + const cps = toCodePoints(text); + let penX = x; + let lineY = y; + for (let i = 0; i < cps.length; i++) { + const cp = cps[i]!; + if (cp === 10) { + penX = x; + lineY += this.lineHeight; + continue; + } + const glyph = this.getGlyph(cp); + if (!glyph) continue; + if (glyph.coverage && glyph.bbxWidth > 0) { + image.drawGlyph(this, glyph, Math.round(penX), lineY, value); + } + penX += glyph.advance ?? glyph.dwidthX; + const next = cps[i + 1]; + if (next !== undefined && next !== 10) { + penX += this.kernBetween(cp, next); + } + } + } + + private advanceOf(codePoint: number): number { + const glyph = this.getGlyph(codePoint); + return glyph ? (glyph.advance ?? glyph.dwidthX) : 0; + } + + /** + * Kerning between two adjacent codepoints: measure(ab) - a - b, memoized. + * One JNI measurement per distinct pair per JS context. + */ + private kernBetween(a: number, b: number): number { + const key = a * 0x200000 + b; + const cached = this.kernCache.get(key); + if (cached !== undefined) return cached; + let kern = 0; + try { + const pair = Number( + com.faceclaw.app.FontFileRenderer.measureTextExact( + this.path, + String.fromCodePoint(a) + String.fromCodePoint(b), + this.sizePx, + ), + ); + if (pair > 0) { + kern = pair - this.advanceOf(a) - this.advanceOf(b); + // A kern larger than the em size means the decomposition failed + // (e.g. the pair shaped into something we can't represent). + if (!Number.isFinite(kern) || Math.abs(kern) > this.sizePx) kern = 0; + } + } catch { + kern = 0; + } + this.kernCache.set(key, kern); + return kern; + } + + /** Render + quantize one glyph via renderGlyphCell (see FontFileRenderer). */ + private loadGlyph(codePoint: number): Glyph | null { + let bytes: Uint8Array; + try { + bytes = toUint8Array( + com.faceclaw.app.FontFileRenderer.renderGlyphCell(this.path, this.sizePx, codePoint, this.gamma), + ); + } catch (error) { + console.warn(`renderGlyphCell failed for U+${codePoint.toString(16)}: ${error}`); + return null; + } + if (bytes.length < 10) return null; + const u16 = (offset: number) => bytes[offset]! | (bytes[offset + 1]! << 8); + const s16 = (offset: number) => { + const value = u16(offset); + return value & 0x8000 ? value - 0x10000 : value; + }; + const advance = u16(0) / 64; + const bearingX = s16(2); + const inkTop = s16(4); + const width = u16(6); + const height = u16(8); + const base: Glyph = { + encoding: codePoint, + dwidthX: Math.round(advance), + bbxWidth: 0, + bbxHeight: 0, + bbxX: 0, + bbxY: 0, + bitmapRows: [], + advance, + }; + if (width <= 0 || height <= 0 || bytes.length < 10 + width * height) { + return base; // ink-free (space) or malformed: advance-only + } + // Quantize 8-bit coverage to the 4bpp levels the composite packs to; + // faint fringe (< 8) quantizes to 0 = transparent. + const coverage = new Uint8Array(width * height); + let hasInk = false; + for (let i = 0; i < coverage.length; i++) { + const nibble = grayToNibble(bytes[10 + i]!); + coverage[i] = nibble; + if (nibble !== 0) hasInk = true; + } + if (!hasInk) return base; + return { + ...base, + bbxWidth: width, + bbxHeight: height, + bbxX: bearingX, + bbxY: this.ascent - inkTop - height, + coverage, + }; + } +} + +function toCodePoints(text: string): number[] { + const cps: number[] = []; + for (const char of text) { + cps.push(char.codePointAt(0)!); + } + return cps; +} + +function fnv32(text: string): number { + let hash = 2166136261; + for (let i = 0; i < text.length; i++) { + hash ^= text.charCodeAt(i); + hash = Math.imul(hash, 16777619); + } + return hash >>> 0; +} diff --git a/app/graphics/ui-fonts.ts b/app/graphics/ui-fonts.ts new file mode 100644 index 00000000..d18d2562 --- /dev/null +++ b/app/graphics/ui-fonts.ts @@ -0,0 +1,192 @@ +/** + * The user-selectable UI fonts: resolves the font settings (chosen in the + * Settings app's font picker) to concrete BdfFont/TtfFont instances for the + * three UI size roles, plus the terminal's separate monospace font. + * + * Selections are stored as one JSON value per setting so a font/size change + * is atomic. Resolution is cached per isolate and invalidated by the settings + * broadcast, matching the old getDefaultSmallFont pattern (these getters are + * called per menu row per paint). + */ +import { getFont, type BdfFont } from "./bdffont"; +import type { UiFont } from "./image"; +import { TtfFont } from "./ttf-font"; +import { getInstalledFont, installedFontPath } from "./installed-fonts"; +import { getStringSetting, onSettingsStoreChanged, setStringSetting } from "../native/settings-store"; + +export type BitmapFace = "terminus" | "terminusv"; + +export type UiFontSelection = + | { kind: "bitmap"; face: BitmapFace } + | { kind: "ttf"; file: string; size: number }; + +export const UI_FONT_SELECTION_KEY = "display.uiFont2"; +export const TERMINAL_FONT_SELECTION_KEY = "terminal.font"; +/** Pre-picker setting ("terminus" | "terminusv"), read as a migration default. */ +const LEGACY_UI_FONT_KEY = "display.uiFont"; + +/** Line-height floors for the medium/large UI font roles (bitmap: 16/24px faces). */ +const MEDIUM_MIN_LINE_HEIGHT = 20; +const LARGE_MIN_LINE_HEIGHT = 26; +/** How far size can grow hunting a line-height floor before giving up. */ +const MAX_SIZE_GROWTH = 48; + +export function parseFontSelection(raw: string): UiFontSelection | null { + try { + const parsed: unknown = JSON.parse(raw); + if (typeof parsed !== "object" || parsed === null) return null; + const sel = parsed as Record; + if (sel.kind === "bitmap" && (sel.face === "terminus" || sel.face === "terminusv")) { + return { kind: "bitmap", face: sel.face }; + } + if ( + sel.kind === "ttf" && + typeof sel.file === "string" && sel.file.length > 0 && + typeof sel.size === "number" && Number.isFinite(sel.size) && sel.size >= 6 && sel.size <= 64 + ) { + return { kind: "ttf", file: sel.file, size: Math.round(sel.size) }; + } + } catch { + // fall through + } + return null; +} + +export function getUiFontSelection(): UiFontSelection { + const parsed = parseFontSelection(getStringSetting(UI_FONT_SELECTION_KEY, "")); + if (parsed) return parsed; + const legacy = getStringSetting(LEGACY_UI_FONT_KEY, "terminus"); + return { kind: "bitmap", face: legacy === "terminusv" ? "terminusv" : "terminus" }; +} + +export function setUiFontSelection(selection: UiFontSelection): void { + setStringSetting(UI_FONT_SELECTION_KEY, JSON.stringify(selection)); +} + +export function getTerminalFontSelection(): UiFontSelection { + return parseFontSelection(getStringSetting(TERMINAL_FONT_SELECTION_KEY, "")) ?? { kind: "bitmap", face: "terminus" }; +} + +export function setTerminalFontSelection(selection: UiFontSelection): void { + setStringSetting(TERMINAL_FONT_SELECTION_KEY, JSON.stringify(selection)); +} + +/** Human label for a selection, e.g. "Inter Light 16" or "Terminus". */ +export function fontSelectionLabel(selection: UiFontSelection): string { + if (selection.kind === "bitmap") { + return selection.face === "terminusv" ? "TerminusV" : "Terminus"; + } + const installed = getInstalledFont(selection.file); + const face = installed ? `${installed.family}${installed.style ? ` ${installed.style}` : ""}` : selection.file; + return `${face} ${selection.size}`; +} + +// --- Resolution, cached per isolate --- + +const resolved = new Map(); +let invalidationInstalled = false; + +function cacheKey(role: string): string { + return role; +} + +function installInvalidation(): void { + if (invalidationInstalled) return; + invalidationInstalled = true; + onSettingsStoreChanged((key) => { + if (key === UI_FONT_SELECTION_KEY || key === LEGACY_UI_FONT_KEY || key === TERMINAL_FONT_SELECTION_KEY) { + resolved.clear(); + cachedTerminalConfig = null; + } + }); +} + +/** + * The selection's TTF at a size grown until lineHeight reaches minLineHeight + * (never shrunk below the picked size), or null when the file can't load. + */ +function resolveTtf(selection: UiFontSelection, minLineHeight = 0): TtfFont | null { + if (selection.kind !== "ttf") return null; + const path = installedFontPath(selection.file); + for (let size = selection.size; size <= selection.size + MAX_SIZE_GROWTH; size++) { + const font = TtfFont.load(path, size); + if (!font) return null; + if (font.lineHeight >= minLineHeight) return font; + } + return null; +} + +function resolveRole(role: "small" | "medium" | "large"): UiFont { + installInvalidation(); + const key = cacheKey(role); + const cached = resolved.get(key); + if (cached) return cached; + const selection = getUiFontSelection(); + let font: UiFont | null = null; + if (selection.kind === "ttf") { + font = + role === "small" + ? resolveTtf(selection) + : resolveTtf(selection, role === "medium" ? MEDIUM_MIN_LINE_HEIGHT : LARGE_MIN_LINE_HEIGHT); + } + if (!font) { + font = bitmapForRole(selection.kind === "bitmap" ? selection.face : "terminus", role); + } + resolved.set(key, font); + return font; +} + +// Only the small (12px) bitmap font has a TerminusV alternative (it ships no +// larger sizes); bitmap medium/large stay Terminus, as before the picker. +function bitmapForRole(face: BitmapFace, role: "small" | "medium" | "large"): BdfFont { + if (role === "medium") return getFont("terminus16"); + if (role === "large") return getFont("terminus24"); + return getFont(face === "terminusv" ? "terminusv12" : "terminus12"); +} + +export function getDefaultSmallFont(): UiFont { + return resolveRole("small"); +} +export function getDefaultMediumFont(): UiFont { + return resolveRole("medium"); +} +export function getDefaultLargeFont(): UiFont { + return resolveRole("large"); +} + +// --- Terminal font --- + +export type TerminalFontConfig = { + font: UiFont; + /** Integer cell pitch; TTF glyphs are drawn per-cell at col*cellWidth. */ + cellWidth: number; + cellHeight: number; +}; + +let cachedTerminalConfig: TerminalFontConfig | null = null; + +/** + * The terminal's font and cell geometry. Note grid dimensions are baked into + * each session's handshake, so callers capture this per window at open time; + * a settings change applies to windows opened afterwards. + */ +export function getTerminalFontConfig(): TerminalFontConfig { + installInvalidation(); + if (cachedTerminalConfig) return cachedTerminalConfig; + const selection = getTerminalFontSelection(); + let config: TerminalFontConfig | null = null; + if (selection.kind === "ttf") { + const font = resolveTtf(selection); + // Cell pitch from a digit's advance (all advances are equal in the + // monospace faces the picker offers), rounded to an integer grid. + const advance = font?.getGlyph(48)?.advance ?? 0; + if (font && advance > 0) { + config = { font, cellWidth: Math.max(1, Math.round(advance)), cellHeight: font.lineHeight }; + } + } + if (!config) { + config = { font: getFont("terminus12"), cellWidth: 6, cellHeight: 12 }; + } + cachedTerminalConfig = config; + return config; +} diff --git a/app/ui/dashboard-settings.ts b/app/ui/dashboard-settings.ts index bc360274..04474db6 100644 --- a/app/ui/dashboard-settings.ts +++ b/app/ui/dashboard-settings.ts @@ -6,12 +6,7 @@ import { setBooleanSetting, setStringSetting, } from "~/native/settings-store"; -import { - getDefaultSmallFont, - UI_FONT_SETTING_KEY, - UI_FONT_VALUES, - type UiFontChoice, -} from "~/graphics/bdffont"; +import { getDefaultSmallFont } from "~/graphics/ui-fonts"; import { wrapText } from "~/graphics/textwrap"; import { ASSISTANT_MODEL_VALUES, @@ -217,16 +212,6 @@ export const batteryDisplayModeSetting = new ConfigSettingEnum({ - id: "uiFont", - label: "Font", - storageKey: UI_FONT_SETTING_KEY, - defaultValue: "terminus", - values: UI_FONT_VALUES, - formatValue: uiFontLabel, - description: "Typeface for UI text on the glasses. Terminus is fixed-width; TerminusV is a proportional variant that fits more text per line.", -}); - export const timeFormatSetting = new ConfigSettingEnum({ id: "timeFormat", label: "Time format", @@ -631,10 +616,6 @@ export function timeFormatLabel(value: TimeFormat): string { return value === "12h" ? "12-hour" : "24-hour"; } -export function uiFontLabel(value: UiFontChoice): string { - return value === "terminusv" ? "TerminusV" : "Terminus"; -} - export function loadNightscoutSettings(): NightscoutSettings { return { siteUrl: nightscoutSiteUrlSetting.get(), diff --git a/app/ui/dashboard/settings-menus.ts b/app/ui/dashboard/settings-menus.ts index 3a6428b7..447e2494 100644 --- a/app/ui/dashboard/settings-menus.ts +++ b/app/ui/dashboard/settings-menus.ts @@ -1,5 +1,5 @@ import { knownFolders } from "@nativescript/core"; -import { getDefaultSmallFont } from "../../graphics/bdffont"; +import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import type { GrayImage } from "../../graphics/image"; import { getDashboardLogo } from "../../graphics/logo"; import { wrapText } from "../../graphics/textwrap"; @@ -44,13 +44,13 @@ import { textSettingMenuItem, timeFormatSetting, toggleSettingMenuItem, - uiFontSetting, verticalPositionSetting, voiceProviderSetting, screenTimeoutSetting, wakeWordActionSetting, } from "../dashboard-settings"; import { SettingsPanelLayer, type SettingsSection } from "./settings-panel"; +import { terminalFontPickerMenuItem, uiFontPickerMenuItem } from "../font-picker"; /** The Settings app's master-detail panel (sections on the left, contents on the right). */ export function createSettingsPanelLayer(): SettingsPanelLayer { @@ -78,8 +78,8 @@ function settingsSections(): SettingsSection[] { enumSettingMenuItem(batteryDisplayModeSetting), // Controls the top-bar clock (24-hour vs 12-hour). enumSettingMenuItem(timeFormatSetting), - // Selects the UI body typeface (Terminus vs proportional TerminusV). - enumSettingMenuItem(uiFontSetting), + // Opens the modal font picker (face, weight, size) for UI text. + uiFontPickerMenuItem(), ], }, { @@ -120,6 +120,7 @@ function settingsSections(): SettingsSection[] { // Connections (g2mirror:// strings) are managed inside the Terminal // app's Manage Connections section, not here. items: [ + terminalFontPickerMenuItem(), textSettingMenuItem(terminalLaunchPresetsSetting), toggleSettingMenuItem(terminalAutoReconnectSetting), toggleSettingMenuItem(terminalWakeOnBellSetting), diff --git a/app/ui/dashboard/settings-panel.ts b/app/ui/dashboard/settings-panel.ts index 45592613..9fc4500a 100644 --- a/app/ui/dashboard/settings-panel.ts +++ b/app/ui/dashboard/settings-panel.ts @@ -1,4 +1,4 @@ -import { getDefaultSmallFont } from "../../graphics/bdffont"; +import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import { GrayImage } from "../../graphics/image"; import { wrapText } from "../../graphics/textwrap"; import { clamp } from "../../util/numeric-util"; diff --git a/app/ui/document/document-view.ts b/app/ui/document/document-view.ts index 05e7bc06..e9300d62 100644 --- a/app/ui/document/document-view.ts +++ b/app/ui/document/document-view.ts @@ -5,8 +5,8 @@ * lines highlight as one unit) and the scroll position follows it, matching * the house list style in ui/menu.ts. */ -import { GrayImage } from "../../graphics/image"; -import { getDefaultMediumFont, getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont"; +import { GrayImage, type UiFont } from "../../graphics/image"; +import { getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/ui-fonts"; import { wrapText } from "../../graphics/textwrap"; import { clamp } from "../../util/numeric-util"; import { drawListScrollbar, drawSelectionHighlight } from "../menu"; @@ -35,7 +35,7 @@ type LayoutLine = { indentX: number; textX: number; height: number; - font: BdfFont; + font: UiFont; /** Line belongs to a code block (shaded background). */ codeBlock: boolean; segments: Segment[]; @@ -305,7 +305,7 @@ function inlineSpans(inlines: readonly DocInline[]): InlineSpan[] { /** Split one wrapped line (plainText[start..start+text.length)) at span boundaries. */ function segmentLine( - font: BdfFont, + font: UiFont, text: string, start: number, spans: readonly InlineSpan[], diff --git a/app/ui/font-picker.ts b/app/ui/font-picker.ts new file mode 100644 index 00000000..58c78f4a --- /dev/null +++ b/app/ui/font-picker.ts @@ -0,0 +1,320 @@ +/** + * Modal font picker for the Settings app: choose a face (bitmap Terminus + * variants or any installed TTF family), a weight (the family's installed + * styles), and a pixel size, with a live preview line rendered in the draft + * font. Selections only persist on Save; double-click cancels. + * + * Used for both the UI font (Display section) and the terminal font + * (Terminal section, filtered to monospace faces). + */ +import { getFont } from "../graphics/bdffont"; +import { GrayImage, type UiFont } from "../graphics/image"; +import { TtfFont } from "../graphics/ttf-font"; +import { listInstalledFonts, type InstalledFont } from "../graphics/installed-fonts"; +import { + fontSelectionLabel, + getDefaultSmallFont, + getTerminalFontSelection, + getUiFontSelection, + setTerminalFontSelection, + setUiFontSelection, + type BitmapFace, + type UiFontSelection, +} from "../graphics/ui-fonts"; +import { GESTURE_DOUBLE_CLICK } from "./gestures"; +import { drawRightValueMenuItem, drawSelectionHighlight, openModalMenu, type MenuItem } from "./menu"; +import type { DashboardInputEvent, Layer, LayerContext } from "./layers"; + +const SIZE_CHOICES = [12, 13, 14, 15, 16, 17, 18, 20, 22, 24, 26, 28] as const; +const DEFAULT_TTF_SIZE = 16; +const PREVIEW_TEXT = "The quick brown fox jumps over 0123456789"; + +type FontPickerOptions = { + title: string; + /** Only offer faces where every style is monospace (terminal picker). */ + monospaceOnly: boolean; + /** Bitmap faces offered ahead of the installed TTF families. */ + bitmapFaces: readonly { face: BitmapFace; label: string }[]; + get(): UiFontSelection; + set(selection: UiFontSelection): void; +}; + +type FontFamily = { + label: string; + fonts: InstalledFont[]; +}; + +/** The draft being edited: a bitmap face, or a TTF family + style + size. */ +type Draft = + | { kind: "bitmap"; face: BitmapFace } + | { kind: "ttf"; family: FontFamily; font: InstalledFont; size: number }; + +const ROWS = ["face", "weight", "size", "save"] as const; +type RowId = (typeof ROWS)[number]; + +export class FontPickerLayer implements Layer { + private readonly families: FontFamily[]; + private draft: Draft; + private selectedRow = 0; + + constructor(private readonly options: FontPickerOptions) { + this.families = collectFamilies(options.monospaceOnly); + this.draft = this.draftFromSelection(options.get()); + } + + private draftFromSelection(selection: UiFontSelection): Draft { + if (selection.kind === "ttf") { + for (const family of this.families) { + const font = family.fonts.find((f) => f.fileName === selection.file); + if (font) return { kind: "ttf", family, font, size: selection.size }; + } + } + const face = + selection.kind === "bitmap" && this.options.bitmapFaces.some((b) => b.face === selection.face) + ? selection.face + : this.options.bitmapFaces[0]?.face ?? "terminus"; + return { kind: "bitmap", face }; + } + + private draftSelection(): UiFontSelection { + return this.draft.kind === "bitmap" + ? { kind: "bitmap", face: this.draft.face } + : { kind: "ttf", file: this.draft.font.fileName, size: this.draft.size }; + } + + /** The draft's concrete font for the preview (bitmap fallback if unloadable). */ + private previewFont(): UiFont { + if (this.draft.kind === "ttf") { + const font = TtfFont.load(this.draft.font.path, this.draft.size); + if (font) return font; + } + return this.draft.kind === "bitmap" && this.draft.face === "terminusv" + ? getFont("terminusv12") + : getFont("terminus12"); + } + + paint(ctx: LayerContext): GrayImage { + const font = getDefaultSmallFont(); + const { width, height } = ctx.stack.getBaseSize(); + const image = new GrayImage(width, height, 0); + const focused = ctx.stack.isFocused(); + image.drawText(font, 22, 14, this.options.title, 220); + + const rowX = 22; + const rowWidth = width - 2 * rowX; + const rowHeight = 22; + const rowsTop = 40; + for (let index = 0; index < ROWS.length; index++) { + const row = ROWS[index]!; + const y = rowsTop + index * rowHeight; + const disabled = this.rowDisabled(row); + if (index === this.selectedRow) { + drawSelectionHighlight(image, rowX - 10, y - 2, rowWidth + 20, rowHeight - 2, focused, 8); + } + if (row === "save") { + image.drawText(font, rowX, y + 3, "Save", index === this.selectedRow ? 255 : 200); + continue; + } + const label = row === "face" ? "Font" : row === "weight" ? "Weight" : "Size"; + const value = this.rowValue(row); + if (disabled) { + image.drawText(font, rowX, y + 3, label, 70); + image.drawText(font, rowX + rowWidth - font.measureText(value) - 2, y + 3, value, 70); + } else { + drawRightValueMenuItem(image, font, rowX, y, rowWidth, label, value); + } + } + + // Preview: a separator, then the sample line in the draft font. + const previewTop = rowsTop + ROWS.length * rowHeight + 10; + image.drawLine(rowX - 10, previewTop, rowX + rowWidth + 10, previewTop, 60); + const preview = this.previewFont(); + const sample = + preview.measureText(PREVIEW_TEXT) <= rowWidth + 20 ? PREVIEW_TEXT : "The quick brown fox 0123"; + preview.drawText(image, rowX - 4, previewTop + 8, sample, 230); + const info = `Line height: ${preview.lineHeight}px`; + image.drawText(font, rowX - 4, previewTop + 8 + preview.lineHeight + 6, info, 110); + + image.drawText(font, rowX - 4, height - 30, `scroll select click change ${GESTURE_DOUBLE_CLICK} cancel`, 90); + return image; + } + + private rowDisabled(row: RowId): boolean { + return (row === "weight" || row === "size") && this.draft.kind === "bitmap"; + } + + private rowValue(row: RowId): string { + if (row === "face") { + if (this.draft.kind === "bitmap") { + return this.options.bitmapFaces.find((b) => b.face === (this.draft as { face: BitmapFace }).face)?.label + ?? "Terminus"; + } + return this.draft.family.label; + } + if (this.draft.kind === "bitmap") return "-"; + if (row === "weight") return this.draft.font.style || "Regular"; + return String(this.draft.size); + } + + handleInput(event: DashboardInputEvent, ctx: LayerContext): void { + switch (event.type) { + case "scroll-up": + this.selectedRow = (this.selectedRow + ROWS.length - 1) % ROWS.length; + return; + case "scroll-down": + this.selectedRow = (this.selectedRow + 1) % ROWS.length; + return; + case "click": { + const row = ROWS[this.selectedRow]!; + if (this.rowDisabled(row)) return; + if (row === "face") this.openFaceMenu(ctx); + else if (row === "weight") this.openWeightMenu(ctx); + else if (row === "size") this.openSizeMenu(ctx); + else { + this.options.set(this.draftSelection()); + ctx.stack.pop(); + } + return; + } + case "double-click": + ctx.stack.pop(); + return; + default: + return; + } + } + + private openFaceMenu(ctx: LayerContext): void { + const items: MenuItem[] = []; + for (const bitmap of this.options.bitmapFaces) { + items.push({ + label: `${bitmap.label} (bitmap)`, + onSelect: (innerCtx) => { + this.draft = { kind: "bitmap", face: bitmap.face }; + innerCtx.stack.pop(); + }, + }); + } + for (const family of this.families) { + items.push({ + label: family.label, + onSelect: (innerCtx) => { + this.setFamily(family); + innerCtx.stack.pop(); + }, + }); + } + const currentIndex = + this.draft.kind === "bitmap" + ? Math.max(0, this.options.bitmapFaces.findIndex((b) => b.face === (this.draft as { face: BitmapFace }).face)) + : this.options.bitmapFaces.length + this.families.indexOf(this.draft.family); + openModalMenu(ctx, "Font", items, Math.max(0, currentIndex)); + } + + /** Switch families, carrying the weight (nearest available) and size over. */ + private setFamily(family: FontFamily): void { + const previous = this.draft; + const targetOrder = previous.kind === "ttf" ? previous.font.weightOrder : 400; + const size = previous.kind === "ttf" ? previous.size : DEFAULT_TTF_SIZE; + let font = family.fonts[0]!; + for (const candidate of family.fonts) { + if (Math.abs(candidate.weightOrder - targetOrder) < Math.abs(font.weightOrder - targetOrder)) { + font = candidate; + } + } + this.draft = { kind: "ttf", family, font, size }; + } + + private openWeightMenu(ctx: LayerContext): void { + if (this.draft.kind !== "ttf") return; + const draft = this.draft; + const items = draft.family.fonts.map((font): MenuItem => ({ + label: font.style || "Regular", + onSelect: (innerCtx) => { + this.draft = { ...draft, font }; + innerCtx.stack.pop(); + }, + })); + openModalMenu(ctx, "Weight", items, Math.max(0, draft.family.fonts.indexOf(draft.font))); + } + + private openSizeMenu(ctx: LayerContext): void { + if (this.draft.kind !== "ttf") return; + const draft = this.draft; + const items = SIZE_CHOICES.map((size): MenuItem => ({ + label: String(size), + onSelect: (innerCtx) => { + this.draft = { ...draft, size }; + innerCtx.stack.pop(); + }, + })); + openModalMenu(ctx, "Size", items, Math.max(0, SIZE_CHOICES.indexOf(draft.size as (typeof SIZE_CHOICES)[number]))); + } +} + +/** Installed fonts grouped into families (styles sorted by weight). */ +function collectFamilies(monospaceOnly: boolean): FontFamily[] { + const byLabel = new Map(); + for (const font of listInstalledFonts()) { + if (monospaceOnly && !font.monospace) continue; + let family = byLabel.get(font.family); + if (!family) { + family = { label: font.family, fonts: [] }; + byLabel.set(font.family, family); + } + family.fonts.push(font); + } + return [...byLabel.values()]; +} + +/** Settings row for the UI font: label + current value, opens the picker. */ +export function uiFontPickerMenuItem(): MenuItem { + return fontPickerMenuItem({ + rowLabel: "Font", + description: + "Typeface for UI text on the glasses: a bitmap Terminus variant or any installed TTF face at a chosen weight and size. Install more fonts from the Files app.", + title: "UI font", + monospaceOnly: false, + bitmapFaces: [ + { face: "terminus", label: "Terminus" }, + { face: "terminusv", label: "TerminusV" }, + ], + get: getUiFontSelection, + set: setUiFontSelection, + }); +} + +/** Settings row for the terminal font (monospace faces only). */ +export function terminalFontPickerMenuItem(): MenuItem { + return fontPickerMenuItem({ + rowLabel: "Font", + description: + "Typeface for terminal windows, limited to fixed-width faces. Takes effect for newly opened terminal windows.", + title: "Terminal font", + monospaceOnly: true, + bitmapFaces: [{ face: "terminus", label: "Terminus" }], + get: getTerminalFontSelection, + set: setTerminalFontSelection, + }); +} + +function fontPickerMenuItem(options: FontPickerOptions & { rowLabel: string; description: string }): MenuItem { + return { + label: options.rowLabel, + description: options.description, + onSelect: (ctx) => { + ctx.stack.push(new FontPickerLayer(options)); + }, + render: ({ image, x, y, width }) => { + drawRightValueMenuItem( + image, + getDefaultSmallFont(), + x, + y, + width, + options.rowLabel, + fontSelectionLabel(options.get()), + ); + }, + }; +} diff --git a/app/ui/layers.ts b/app/ui/layers.ts index 4a46e2e2..d3a0255b 100644 --- a/app/ui/layers.ts +++ b/app/ui/layers.ts @@ -103,8 +103,8 @@ export interface Layer { export class LayerStack { private readonly layers: Layer[]; private readonly ctx: LayerContext; - private readonly baseWidth: number; - private readonly baseHeight: number; + private baseWidth: number; + private baseHeight: number; private readonly focusedFn: () => boolean; constructor( @@ -168,6 +168,12 @@ export class LayerStack { return { width: this.baseWidth, height: this.baseHeight }; } + /** Resize the base viewport (e.g. an EvenHub window switching to the tall canvas). */ + setBaseSize(size: { width: number; height: number }): void { + this.baseWidth = size.width; + this.baseHeight = size.height; + } + setActions(actions: Partial): void { Object.assign(this.ctx.actions, actions); } diff --git a/app/ui/menu.ts b/app/ui/menu.ts index 14a2e1df..fc0bb1e8 100644 --- a/app/ui/menu.ts +++ b/app/ui/menu.ts @@ -1,6 +1,6 @@ -import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../graphics/image"; +import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage, type UiFont } from "../graphics/image"; import { wrapText } from "../graphics/textwrap"; -import { getDefaultSmallFont, type BdfFont } from "../graphics/bdffont"; +import { getDefaultSmallFont } from "../graphics/ui-fonts"; import { clamp } from "../util/numeric-util"; import { DashboardInputEvent, Layer, LayerContext, PaintBelow } from "./layers"; @@ -121,7 +121,7 @@ export function drawListScrollbar( */ export function drawSubmenuIndicator( image: GrayImage, - font: BdfFont, + font: UiFont, highlightX: number, highlightY: number, highlightWidth: number, @@ -136,7 +136,7 @@ export function drawSubmenuIndicator( export function drawToggleMenuItem( image: GrayImage, - font: BdfFont, + font: UiFont, x: number, y: number, width: number, @@ -159,7 +159,7 @@ export function drawToggleMenuItem( export function drawRightValueMenuItem( image: GrayImage, - font: BdfFont, + font: UiFont, x: number, y: number, width: number, diff --git a/app/ui/notifications.ts b/app/ui/notifications.ts index dc28b604..a9f07bb0 100644 --- a/app/ui/notifications.ts +++ b/app/ui/notifications.ts @@ -1,8 +1,8 @@ import { clamp } from "~/util/numeric-util"; import { formatRelativeTime } from "~/util/date-util"; -import { BdfFont, getDefaultSmallFont } from "../graphics/bdffont"; +import { getDefaultSmallFont } from "../graphics/ui-fonts"; import { truncateText } from "../graphics/textwrap"; -import { GrayImage } from "../graphics/image"; +import { GrayImage, type UiFont } from "../graphics/image"; import { wrapText } from "../graphics/textwrap"; import { GESTURE_DOUBLE_CLICK } from "./gestures"; import { @@ -237,7 +237,7 @@ export class SingleNotificationLayer implements Layer { function buildNotificationCardLayout( - font: BdfFont, + font: UiFont, notification: AndroidNotification, selected: boolean, cardTextWidth: number, @@ -273,7 +273,7 @@ function buildNotificationCardLayout( function drawNotificationCard( image: GrayImage, - font: BdfFont, + font: UiFont, layout: CardLayout, x: number, y: number, @@ -313,7 +313,7 @@ function scrollForSelected(layouts: CardLayout[], selectedIndex: number, viewpor function drawDetailContent( image: GrayImage, - font: BdfFont, + font: UiFont, notification: AndroidNotification, icon: GrayImage | null, width: number, @@ -353,7 +353,7 @@ function drawDetailContent( } } -function drawDetailMenu(image: GrayImage, font: BdfFont, menu: DetailMenuItem[], selectedIndex: number, width: number): void { +function drawDetailMenu(image: GrayImage, font: UiFont, menu: DetailMenuItem[], selectedIndex: number, width: number): void { const menuX = width - DETAIL_MENU_WIDTH - 24; const menuY = 24; for (let index = 0; index < menu.length; index++) { diff --git a/app/ui/shell/assistant.ts b/app/ui/shell/assistant.ts index 3582f7db..845681db 100644 --- a/app/ui/shell/assistant.ts +++ b/app/ui/shell/assistant.ts @@ -1,5 +1,6 @@ import { G2_LENS_WIDTH, GrayImage } from "../../graphics/image"; -import { getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont"; +import { type BdfFont } from "../../graphics/bdffont"; +import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import { GESTURE_DOUBLE_CLICK } from "../gestures"; import { drawSelectionHighlight } from "../menu"; import { Layer, type DashboardInputEvent, type LayerActions, type LayerContext } from "../layers"; diff --git a/app/ui/shell/chrome-layer.ts b/app/ui/shell/chrome-layer.ts index 27a7e620..77da66af 100644 --- a/app/ui/shell/chrome-layer.ts +++ b/app/ui/shell/chrome-layer.ts @@ -1,5 +1,5 @@ import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../../graphics/image"; -import { getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/bdffont"; +import { getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/ui-fonts"; import { BATTERY_ICON_WIDTH, drawBattery } from "../../graphics/battery"; import { readActiveNotificationIcons } from "../../native/notification-icons"; import { readPhoneBatteryState } from "../../native/phone-battery"; diff --git a/app/ui/shell/in-process-window.ts b/app/ui/shell/in-process-window.ts index ccf95948..d659f231 100644 --- a/app/ui/shell/in-process-window.ts +++ b/app/ui/shell/in-process-window.ts @@ -41,6 +41,8 @@ export type InProcessWindowOptions = { submitFrame: (planes: Plane[], paintMs: number, frameId: number) => Promise; setSurfaceVisible: (visible: boolean) => void; removeSurface?: () => void; + /** Resize this window's compositor surface after a height-mode change. */ + reconfigureSurface?: (heightMode: WindowHeightMode) => void; onClosed?: () => void; }; @@ -48,6 +50,8 @@ export type InProcessWindow = { window: ShellWindow; stack: LayerStack; requestRender: () => void; + /** Change the window's height band at runtime (resizes the viewport + surface). */ + setHeightMode: (mode: WindowHeightMode) => void; }; /** The controller-provided plumbing common to every in-process app window. */ @@ -56,6 +60,7 @@ export type InProcessAppOptions = { submitFrame: (planes: Plane[], paintMs: number, frameId: number) => Promise; setSurfaceVisible: (visible: boolean) => void; removeSurface: () => void; + reconfigureSurface?: (heightMode: WindowHeightMode) => void; onClosed: () => void; }; @@ -65,7 +70,7 @@ export function createInProcessWindow(options: InProcessWindowOptions): InProces console.error(`${options.windowId} render failed: ${error}`); }); }; - const heightMode = options.heightMode ?? "min"; + let heightMode = options.heightMode ?? "min"; const stack = new LayerStack( options.baseLayer, { ...options.actions, requestRender }, @@ -168,7 +173,15 @@ export function createInProcessWindow(options: InProcessWindowOptions): InProces options.setSurfaceVisible(foreground); }, }; - return { window, stack, requestRender }; + const setHeightMode = (mode: WindowHeightMode) => { + if (heightMode === mode) return; + heightMode = mode; + window.heightMode = mode; + stack.setBaseSize(appViewportSize(mode)); + options.reconfigureSurface?.(mode); + requestRender(); + }; + return { window, stack, requestRender, setHeightMode }; } /** diff --git a/app/ui/shell/shell.ts b/app/ui/shell/shell.ts index de135a86..232cbb43 100644 --- a/app/ui/shell/shell.ts +++ b/app/ui/shell/shell.ts @@ -1,6 +1,6 @@ import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../../graphics/image"; import { singlePlane, type Plane } from "../../graphics/plane"; -import { getDefaultSmallFont } from "../../graphics/bdffont"; +import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import { EvenAIStatus, EventSourceType, OsEventTypeList } from "../../g2/events"; import type { RawInputEvent } from "../../native/faceclaw-communicator"; import { DashboardInputEvent, Layer, LayerActions, LayerContext, LayerStack, noopLayerActions } from "../layers"; diff --git a/app/ui/shell/tool-debug-layer.ts b/app/ui/shell/tool-debug-layer.ts index ec1b0e86..8f80c827 100644 --- a/app/ui/shell/tool-debug-layer.ts +++ b/app/ui/shell/tool-debug-layer.ts @@ -1,5 +1,5 @@ import { G2_LENS_WIDTH, GrayImage } from "../../graphics/image"; -import { getDefaultSmallFont } from "../../graphics/bdffont"; +import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import { wrapText } from "../../graphics/textwrap"; import { clamp } from "../../util/numeric-util"; import type { ToolDebugEntry } from "../../assistant/tool-registry"; diff --git a/app/ui/shell/voice-input.ts b/app/ui/shell/voice-input.ts index 4431af8b..c0df0a46 100644 --- a/app/ui/shell/voice-input.ts +++ b/app/ui/shell/voice-input.ts @@ -1,6 +1,7 @@ import { G2_LENS_WIDTH, GrayImage } from "../../graphics/image"; import { wrapText, truncateText } from "../../graphics/textwrap"; -import { getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont"; +import { type BdfFont } from "../../graphics/bdffont"; +import { getDefaultSmallFont } from "../../graphics/ui-fonts"; import { voiceControlBridge, type VoiceTranscriptEvent } from "../../native/voice-control"; import { refineDictation, type AnthropicStreamHandle } from "../../native/anthropic"; import { anthropicApiKeySetting } from "../dashboard-settings"; diff --git a/faceclaw-extensions/README.md b/faceclaw-extensions/README.md index aa35f064..3b33b520 100644 --- a/faceclaw-extensions/README.md +++ b/faceclaw-extensions/README.md @@ -79,9 +79,47 @@ brief: | `getConfiguredApiKeys(): Promise` | Which keys the user configured (names only). | | `requestApiKeyAccess(services): Promise>>` | Prompt for and return key values. | | `playBuzzer(steps): Promise` | Play a piezo tone sequence. | +| `addCompassListener(fn): () => void` | Activate the magnetometer; receive heading samples. Uncalibrated (see note). | +| `createLayout(layout): Promise` | Build the initial page (extended layout: full 576×452, no count limit, `preserve`). | +| `replaceLayout(layout): Promise` | Replace the page; `preserve` containers inherit content by name. | +| `setAssistantTools(tools): void` | Contribute voice-assistant tools (namespaced by package name). | `ApiKeyService` is one of `"openai" | "anthropic" | "soniox" | "elevenlabs" | "mapbox"`. +### Compass + +`addCompassListener` is **not pre-calibrated**: the magnetometer is inside the +right arm, which rests on the head with a slight curl that differs between +wearers and wears. Treat the heading as relative — let the user zero it. + +### Extended layout + +`createLayout` / `replaceLayout` accept the same `textObject` / `imageObject` / +`listObject` shape as the stock `createStartUpPageContainer` / +`rebuildPageContainer` (so you can keep using the SDK's container builders), +plus: the canvas is the full **576×452** app area (image containers may fill it), +there is **no container-count limit**, and any container may set `preserve: true` +to inherit its content from the same-named container in the previous layout. +`containerID` remains the identity for `updateImageRawData` / `textContainerUpgrade`. + +### Voice-assistant tools + +```ts +fc.setAssistantTools([ + { + name: "set_bpm", + description: "Set the metronome tempo.", + parameters: { type: "object", properties: { bpm: { type: "number" } }, required: ["bpm"] }, + availability: "open", // or "foreground" (default) + handler: ({ bpm }) => { setTempo(bpm); return `tempo is now ${bpm}`; }, + }, +]); +``` + +The assistant sees each tool namespaced by your package name +(`app..set_bpm`). The handler's return value is reported back to the +assistant; throwing reports an error. + ## Stability Pre-1.0: the surface may still change. Once it stabilizes, additive-only changes diff --git a/faceclaw-extensions/dist/index.d.ts b/faceclaw-extensions/dist/index.d.ts index 9e8297ec..72806cb3 100644 --- a/faceclaw-extensions/dist/index.d.ts +++ b/faceclaw-extensions/dist/index.d.ts @@ -62,6 +62,67 @@ export type WindowLifecycleEventType = "visible" | "hidden" | "focused" | "blurr export interface WindowLifecycleEvent { type: WindowLifecycleEventType; } +/** A magnetometer heading sample from {@link FaceclawExtensions.addCompassListener}. */ +export interface CompassReading { + /** Magnetic heading in degrees [0, 360). */ + headingDegrees: number; +} +export interface FaceclawContainerCommon { + containerID: number; + containerName: string; + xPosition: number; + yPosition: number; + width: number; + height: number; + zOrderIndex?: number; + isEventCapture?: 0 | 1 | boolean; + /** Inherit content from the same-named container in the previous layout. */ + preserve?: boolean; +} +export interface FaceclawTextContainer extends FaceclawContainerCommon { + content?: string; + borderWidth?: number; + borderRadius?: number; + paddingLength?: number; +} +/** Image containers may be up to 576x452 in the extended layout. */ +export type FaceclawImageContainer = FaceclawContainerCommon; +export interface FaceclawListContainer extends FaceclawContainerCommon { + itemContainer?: { + itemName?: string[]; + itemWidth?: number; + isItemSelectBorderEn?: 0 | 1 | boolean; + }; +} +export interface FaceclawLayout { + textObject?: FaceclawTextContainer[]; + imageObject?: FaceclawImageContainer[]; + listObject?: FaceclawListContainer[]; +} +/** + * A tool the app contributes to Faceclaw's voice assistant. The assistant sees + * it namespaced by the app's package name (`app..`); `name` + * here is the bare, un-prefixed name. + */ +export interface AssistantTool { + /** Bare tool name (no package prefix), e.g. `"set_color"`. */ + name: string; + /** What the tool does — the assistant reads this to decide when to call it. */ + description: string; + /** JSON Schema for the arguments object the assistant will pass to `handler`. */ + parameters?: object; + /** + * When the tool is callable: `"foreground"` (default) only while the app owns + * the visible window; `"open"` any time the app is running (even backgrounded). + */ + availability?: "open" | "foreground"; + /** + * Invoked when the assistant calls the tool. Return a string (or any + * JSON-serializable value) to report back to the assistant; throw to report an + * error. May be async. + */ + handler: (args: any) => unknown | Promise; +} /** * The Faceclaw-only capability surface. Obtained via {@link getFaceclawExtensions}. * Every method is additive to the standard EvenHub SDK. @@ -106,6 +167,35 @@ export interface FaceclawExtensions { * Resolves once every message has been queued. */ playBuzzer(steps: BuzzerStep[]): Promise; + /** + * Activate the glasses' magnetometer compass and receive heading samples. + * Returns an unsubscribe function; the sensor stays on while at least one + * listener is registered and the app is the foreground window. + * + * NOT pre-calibrated: the magnetometer sits inside the right arm, which rests + * against the wearer's head with a slight curl that differs between wearers + * (and between wears). Treat `headingDegrees` as relative — let the user zero + * it against a known direction rather than trusting it as true/magnetic north. + */ + addCompassListener(listener: (reading: CompassReading) => void): () => void; + /** + * Build the app's initial page from an extended layout (superset of the stock + * createStartUpPageContainer — full 576x452 canvas, no container-count limit, + * per-container `preserve`). Resolves true on success. + */ + createLayout(layout: FaceclawLayout): Promise; + /** + * Replace the current page with a new extended layout (superset of the stock + * rebuildPageContainer). Containers marked `preserve` inherit their content + * from the same-named container in the outgoing layout. Resolves true on success. + */ + replaceLayout(layout: FaceclawLayout): Promise; + /** + * Declare the app's voice-assistant tools, replacing any previously set. Each + * tool's `handler` runs in the app when the assistant calls it. The assistant + * sees the tools namespaced by the app's package name. Pass `[]` to remove all. + */ + setAssistantTools(tools: AssistantTool[]): void; } declare global { interface Window { diff --git a/faceclaw-extensions/src/index.ts b/faceclaw-extensions/src/index.ts index f6275b94..4e5c4dff 100644 --- a/faceclaw-extensions/src/index.ts +++ b/faceclaw-extensions/src/index.ts @@ -67,6 +67,90 @@ export interface WindowLifecycleEvent { type: WindowLifecycleEventType; } +/** A magnetometer heading sample from {@link FaceclawExtensions.addCompassListener}. */ +export interface CompassReading { + /** Magnetic heading in degrees [0, 360). */ + headingDegrees: number; +} + +// ---- Extended layout (a superset of the stock EvenHub page containers) ---- +// +// createLayout/replaceLayout take the same textObject/imageObject/listObject +// shape as the stock createStartUpPageContainer/rebuildPageContainer (so you can +// keep using the SDK's container-property builders), with three differences: +// - the canvas is the full 576x452 app area (vs the stock 576x288), so image +// containers may be up to 576x452; +// - there is no maximum container count; +// - each container may set `preserve: true` to inherit its content (image +// pixels / text / list items) from a same-named container in the previous +// layout — otherwise it starts blank. +// `containerID` is still the identity for content updates (the stock +// updateImageRawData / textContainerUpgrade address containers by it). + +export interface FaceclawContainerCommon { + containerID: number; + containerName: string; + xPosition: number; + yPosition: number; + width: number; + height: number; + zOrderIndex?: number; + isEventCapture?: 0 | 1 | boolean; + /** Inherit content from the same-named container in the previous layout. */ + preserve?: boolean; +} + +export interface FaceclawTextContainer extends FaceclawContainerCommon { + content?: string; + borderWidth?: number; + borderRadius?: number; + paddingLength?: number; +} + +/** Image containers may be up to 576x452 in the extended layout. */ +export type FaceclawImageContainer = FaceclawContainerCommon; + +export interface FaceclawListContainer extends FaceclawContainerCommon { + itemContainer?: { + itemName?: string[]; + itemWidth?: number; + isItemSelectBorderEn?: 0 | 1 | boolean; + }; +} + +export interface FaceclawLayout { + textObject?: FaceclawTextContainer[]; + imageObject?: FaceclawImageContainer[]; + listObject?: FaceclawListContainer[]; +} + +// ---- Voice-assistant tools ---- + +/** + * A tool the app contributes to Faceclaw's voice assistant. The assistant sees + * it namespaced by the app's package name (`app..`); `name` + * here is the bare, un-prefixed name. + */ +export interface AssistantTool { + /** Bare tool name (no package prefix), e.g. `"set_color"`. */ + name: string; + /** What the tool does — the assistant reads this to decide when to call it. */ + description: string; + /** JSON Schema for the arguments object the assistant will pass to `handler`. */ + parameters?: object; + /** + * When the tool is callable: `"foreground"` (default) only while the app owns + * the visible window; `"open"` any time the app is running (even backgrounded). + */ + availability?: "open" | "foreground"; + /** + * Invoked when the assistant calls the tool. Return a string (or any + * JSON-serializable value) to report back to the assistant; throw to report an + * error. May be async. + */ + handler: (args: any) => unknown | Promise; +} + /** * The Faceclaw-only capability surface. Obtained via {@link getFaceclawExtensions}. * Every method is additive to the standard EvenHub SDK. @@ -117,6 +201,39 @@ export interface FaceclawExtensions { * Resolves once every message has been queued. */ playBuzzer(steps: BuzzerStep[]): Promise; + + /** + * Activate the glasses' magnetometer compass and receive heading samples. + * Returns an unsubscribe function; the sensor stays on while at least one + * listener is registered and the app is the foreground window. + * + * NOT pre-calibrated: the magnetometer sits inside the right arm, which rests + * against the wearer's head with a slight curl that differs between wearers + * (and between wears). Treat `headingDegrees` as relative — let the user zero + * it against a known direction rather than trusting it as true/magnetic north. + */ + addCompassListener(listener: (reading: CompassReading) => void): () => void; + + /** + * Build the app's initial page from an extended layout (superset of the stock + * createStartUpPageContainer — full 576x452 canvas, no container-count limit, + * per-container `preserve`). Resolves true on success. + */ + createLayout(layout: FaceclawLayout): Promise; + + /** + * Replace the current page with a new extended layout (superset of the stock + * rebuildPageContainer). Containers marked `preserve` inherit their content + * from the same-named container in the outgoing layout. Resolves true on success. + */ + replaceLayout(layout: FaceclawLayout): Promise; + + /** + * Declare the app's voice-assistant tools, replacing any previously set. Each + * tool's `handler` runs in the app when the assistant calls it. The assistant + * sees the tools namespaced by the app's package name. Pass `[]` to remove all. + */ + setAssistantTools(tools: AssistantTool[]): void; } declare global {