Refactor font handling; add image-compression estimation debug option

This commit is contained in:
James Babcock committed 2026-06-01 11:36:46 -07:00
1 parent 5a92637537
commit 964cd11785
15 files changed
+389 -130

No files matched your search

+4 -2
View File
@@ -1,3 +1,4 @@
import { getDefaultSmallFont } from "../../graphics/bdffont";
import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../../graphics/image";
import { Layer, type DashboardInputEvent, type LayerContext } from "../layers";
@@ -18,6 +19,7 @@ const SWATCH_HEIGHT = 16;
export class ScreenTestLayer implements Layer {
paint(ctx: LayerContext): GrayImage {
const image = new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0);
const font = getDefaultSmallFont();
for (let value = 0; value < COLOR_VALUE_COUNT; value += COLOR_VALUE_STEP) {
const slot = value / COLOR_VALUE_STEP;
@@ -26,10 +28,10 @@ export class ScreenTestLayer implements Layer {
const cellX = GRID_X + col * CELL_WIDTH;
const cellY = row * CELL_HEIGHT;
const label = String(value);
const labelX = cellX + Math.max(0, Math.floor((CELL_WIDTH - ctx.font.measureText(label)) / 2));
const labelX = cellX + Math.max(0, Math.floor((CELL_WIDTH - font.measureText(label)) / 2));
const swatchX = cellX + Math.floor((CELL_WIDTH - SWATCH_WIDTH) / 2);
image.drawText(ctx.font, labelX, cellY + LABEL_Y_OFFSET, label, 185);
image.drawText(font, labelX, cellY + LABEL_Y_OFFSET, label, 185);
fillSwatch(image, swatchX, cellY + SWATCH_Y_OFFSET, SWATCH_WIDTH, SWATCH_HEIGHT, value);
}
+7 -6
View File
@@ -1,8 +1,8 @@
import { loadEmbeddedTerminus32 } from "../../graphics/bdffont";
import { getDefaultSmallFont, getFont } from "../../graphics/bdffont";
import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../../graphics/image";
import { Layer, type DashboardInputEvent, type LayerContext } from "../layers";
const stopwatchFont = loadEmbeddedTerminus32();
const stopwatchFont = getFont("terminus32");
export class StopwatchLayer implements Layer {
private readonly startedAtMs = Date.now();
@@ -14,17 +14,18 @@ export class StopwatchLayer implements Layer {
const elapsedMs = this.elapsedMs();
const timeLabel = formatStopwatchElapsed(elapsedMs);
const stateLabel = this.pausedAtMs === null ? "Running" : "Paused";
const smallFont = getDefaultSmallFont();
image.drawText(ctx.font, 24, 24, "Stopwatch", 180);
image.drawText(smallFont, 24, 24, "Stopwatch", 180);
const quadrantWidth = G2_LENS_WIDTH / 2;
const quadrantHeight = G2_LENS_HEIGHT / 2;
const timeX = Math.max(0, Math.round((quadrantWidth - stopwatchFont.measureText(timeLabel)) / 2));
const timeY = Math.max(0, Math.round((quadrantHeight - stopwatchFont.lineHeight) / 2));
image.drawText(stopwatchFont, timeX, timeY, timeLabel, 245);
image.drawText(ctx.font, 90, 142, stateLabel, 150);
image.drawText(ctx.font, 90, 160, "Click: pause / resume", 150);
image.drawText(ctx.font, 90, 178, "Double-click: back", 150);
image.drawText(smallFont, 90, 142, stateLabel, 150);
image.drawText(smallFont, 90, 160, "Click: pause / resume", 150);
image.drawText(smallFont, 90, 178, "Double-click: back", 150);
return image;
}
+7 -6
View File
@@ -1,5 +1,5 @@
import { wrapText } from "../../graphics/textwrap";
import { type BdfFont } from "../../graphics/bdffont";
import { getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont";
import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../../graphics/image";
import { Layer, type DashboardInputEvent, type LayerContext } from "../layers";
@@ -20,29 +20,30 @@ export class TelepromptLayer implements Layer {
constructor(private readonly documentText: string | null) {}
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
const image = new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0);
image.drawRect(12, 12, G2_LENS_WIDTH - 24, G2_LENS_HEIGHT - 24, 52);
image.drawText(ctx.font, MARGIN_X + 4, TITLE_Y, "Teleprompt", 220);
image.drawText(font, MARGIN_X + 4, TITLE_Y, "Teleprompt", 220);
if (this.documentText === null) {
const message = [
"Open a plain text document on your phone and Share it to Faceclaw.",
];
for (let index = 0; index < message.length; index++) {
image.drawText(ctx.font, BODY_X, BODY_Y + index * LINE_STEP, message[index]!, 200);
image.drawText(font, BODY_X, BODY_Y + index * LINE_STEP, message[index]!, 200);
}
return image;
}
const lines = this.getLines(ctx.font);
const lines = this.getLines(font);
const visibleLines = lines.slice(this.firstLine, this.firstLine + BODY_LINE_COUNT);
for (let index = 0; index < visibleLines.length; index++) {
image.drawText(ctx.font, BODY_X, BODY_Y + index * LINE_STEP, visibleLines[index]!, 230);
image.drawText(font, BODY_X, BODY_Y + index * LINE_STEP, visibleLines[index]!, 230);
}
const currentPage = Math.floor(this.firstLine / PAGE_STEP) + 1;
const totalPages = totalPageCount(lines.length);
image.drawText(ctx.font, BODY_X, FOOTER_Y, `Page ${currentPage}/${totalPages}`, 110);
image.drawText(font, BODY_X, FOOTER_Y, `Page ${currentPage}/${totalPages}`, 110);
return image;
}
+9 -8
View File
@@ -1,5 +1,5 @@
import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../../graphics/image";
import { type BdfFont } from "../../graphics/bdffont";
import { getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont";
import { voiceControlBridge, type VoiceTranscriptEvent } from "../../native/voice-control";
import { Layer, type DashboardInputEvent, type LayerContext } from "../layers";
@@ -22,24 +22,25 @@ export class TranscribeLayer implements Layer {
}
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
const image = new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0);
const modeLabel = this.mode === "full" ? "Full" : this.mode === "wakeword" ? "Wakeword" : "Off";
const text = this.transcript || "Listening...";
const wrapped = wrapTranscribeText(ctx.font, text, G2_LENS_WIDTH - 64);
const wrapped = wrapTranscribeText(font, text, G2_LENS_WIDTH - 64);
image.drawRect(12, 12, G2_LENS_WIDTH - 24, G2_LENS_HEIGHT - 24, 52);
image.drawText(ctx.font, 24, 24, "Transcribe", 200);
image.drawText(ctx.font, G2_LENS_WIDTH - 108, 24, modeLabel, this.mode === "full" ? 220 : 130);
//image.drawText(ctx.font, 24, 46, truncateTranscribeLine(this.status, 74), 110);
image.drawText(ctx.font, 24, 46, this.status, 110);
image.drawText(font, 24, 24, "Transcribe", 200);
image.drawText(font, G2_LENS_WIDTH - 108, 24, modeLabel, this.mode === "full" ? 220 : 130);
//image.drawText(font, 24, 46, truncateTranscribeLine(this.status, 74), 110);
image.drawText(font, 24, 46, this.status, 110);
const firstLine = Math.max(0, wrapped.length - 10);
for (let index = firstLine; index < wrapped.length; index++) {
const y = 72 + (index - firstLine) * 16;
image.drawText(ctx.font, 32, y, wrapped[index]!, 230);
image.drawText(font, 32, y, wrapped[index]!, 230);
}
image.drawText(ctx.font, 24, 252, "Click: mode / Double-click: back", 110);
image.drawText(font, 24, 252, "Click: mode / Double-click: back", 110);
return image;
}