forked from Blendman974/faceclaw
Add font viewer
This commit is contained in:
1 parent
495cf0363a
commit
812c3f9b4a
7 files changed
+775
-1
No files matched your search
@@ -65,12 +65,14 @@ const TEXT_EXT = /\.(txt|md|markdown|log|json|xml|csv|ini|conf|cfg|yaml|yml|ts|j
|
||||
const IMAGE_EXT = /\.(png|jpe?g|gif|bmp|webp|heic|heif|svg)$/i;
|
||||
const VIDEO_EXT = /\.(mp4|mkv|webm|avi|mov|3gp|m4v)$/i;
|
||||
const AUDIO_EXT = /\.(mp3|m4a|aac|ogg|oga|opus|flac|wav|mid)$/i;
|
||||
const FONT_EXT = /\.(ttf|otf|ttc|bdf|woff2?)$/i;
|
||||
|
||||
function fileIconName(name: string): IconName {
|
||||
if (TEXT_EXT.test(name)) return "file-text";
|
||||
if (IMAGE_EXT.test(name)) return "image";
|
||||
if (VIDEO_EXT.test(name)) return "film";
|
||||
if (AUDIO_EXT.test(name)) return "music";
|
||||
if (FONT_EXT.test(name)) return "type";
|
||||
return "file";
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import { readTextFile, type DirectoryEntry } from "../../native/file-access";
|
||||
import { isFontFile } from "../../native/font-files";
|
||||
import { isDecodableImageFile } from "../../native/image-files";
|
||||
import { FileBrowserLayer } from "./file-browser";
|
||||
import { FileInfoDialogLayer, type FileInfoAction } from "./file-info-dialog";
|
||||
import { FontPreviewerLayer } from "./font-previewer";
|
||||
import { ImageViewerLayer } from "./image-viewer";
|
||||
import { TextViewerLayer } from "./text-viewer";
|
||||
import {
|
||||
@@ -22,6 +24,8 @@ export type FilesAppOptions = InProcessAppOptions & {
|
||||
openDocumentWindow: (title: string, text: string) => void;
|
||||
/** Open an image file as its own shell window. */
|
||||
openImageWindow: (title: string, path: string) => void;
|
||||
/** Open a font file's previewer as its own shell window. */
|
||||
openFontWindow: (title: string, path: string) => void;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -32,7 +36,7 @@ export type FilesAppOptions = InProcessAppOptions & {
|
||||
export function createFilesAppWindow(options: FilesAppOptions): InProcessWindow {
|
||||
let created: InProcessWindow | null = null;
|
||||
const browser = new FileBrowserLayer({
|
||||
isSupportedFile: (name) => TEXT_FILE.test(name) || isDecodableImageFile(name),
|
||||
isSupportedFile: (name) => TEXT_FILE.test(name) || isDecodableImageFile(name) || isFontFile(name),
|
||||
// The browser handles double-click itself (up a level), so it is not
|
||||
// wrapped in YieldAtRootLayer; it yields explicitly from the top level.
|
||||
onLeave: () => shell.yieldFocusToSidebar(),
|
||||
@@ -83,6 +87,29 @@ export function createTextDocumentWindow(
|
||||
});
|
||||
}
|
||||
|
||||
/** One opened font file's previewer as its own window. */
|
||||
export function createFontDocumentWindow(
|
||||
windowId: string,
|
||||
title: string,
|
||||
path: string,
|
||||
options: InProcessAppOptions,
|
||||
): InProcessWindow {
|
||||
return createInProcessWindow({
|
||||
appId: "files",
|
||||
windowId,
|
||||
title,
|
||||
iconLetter: "F",
|
||||
icon: "type",
|
||||
closeable: true,
|
||||
actions: options.actions,
|
||||
baseLayer: new YieldAtRootLayer(new FontPreviewerLayer(path, title)),
|
||||
submitFrame: options.submitFrame,
|
||||
setSurfaceVisible: options.setSurfaceVisible,
|
||||
removeSurface: options.removeSurface,
|
||||
onClosed: options.onClosed,
|
||||
});
|
||||
}
|
||||
|
||||
/** One opened image file as its own window. */
|
||||
export function createImageDocumentWindow(
|
||||
windowId: string,
|
||||
@@ -149,5 +176,23 @@ function fileOpenActions(entry: DirectoryEntry, options: FilesAppOptions): FileI
|
||||
},
|
||||
];
|
||||
}
|
||||
if (isFontFile(entry.name)) {
|
||||
return [
|
||||
{
|
||||
label: "Preview here",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
ctx.stack.push(new FontPreviewerLayer(entry.path, entry.name));
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "Open in new window",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
options.openFontWindow(entry.name, entry.path);
|
||||
},
|
||||
},
|
||||
];
|
||||
}
|
||||
return [];
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
import { getDefaultSmallFont } from "../../graphics/bdffont";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { truncateText } from "../../graphics/textwrap";
|
||||
import {
|
||||
canLoadFontFile,
|
||||
fontFileDisplayName,
|
||||
fontFileMetrics,
|
||||
renderFontFileText,
|
||||
renderFontFileWrapped,
|
||||
} from "../../native/font-files";
|
||||
import {
|
||||
GESTURE_CLICK,
|
||||
GESTURE_DOUBLE_CLICK,
|
||||
GESTURE_SCROLL,
|
||||
gestureHints,
|
||||
} from "../../ui/gestures";
|
||||
import { Layer, type DashboardInputEvent, type LayerContext } from "../../ui/layers";
|
||||
|
||||
const MARGIN_X = 18;
|
||||
const TITLE_Y = 8;
|
||||
const BODY_TOP = 26;
|
||||
const FOOTER_MARGIN = 20;
|
||||
const SAMPLE_GAP = 4;
|
||||
|
||||
const SIZES = [8,9,10,11, 12, 13, 14, 16, 18, 20, 24, 28, 32, 40, 48] as const;
|
||||
const DEFAULT_SIZE_INDEX = SIZES.indexOf(16);
|
||||
// 1.0 = linear coverage->shade mapping, which should match the G2's
|
||||
// roughly-linear display response; the darker/brighter options are for
|
||||
// on-hardware comparison. Cycled with click.
|
||||
const GAMMAS = [1.0, 1.3, 1.6, 0.7, 0.5] as const;
|
||||
|
||||
const PANGRAM = "The quick brown fox jumps over the lazy dog.";
|
||||
const CHARSET = "AaBbGgRr 0123456789 ?!&%().,;:'\"";
|
||||
const PARAGRAPH =
|
||||
"Weather for today: partly cloudy, high of 23°C with a light breeze. " +
|
||||
"Meeting with Sam moved to 14:30 — they asked if you can review the " +
|
||||
"draft first. Battery 78%.";
|
||||
|
||||
/**
|
||||
* Font previewer for a font file on disk (TTF/OTF/TTC), rendered through the
|
||||
* Android text stack: a single-line pangram, a character-set sample, and a
|
||||
* wrapped paragraph, all at an adjustable pixel size. Scroll changes size,
|
||||
* click cycles the coverage gamma (for judging antialiasing on the lens),
|
||||
* double-click closes. Sized to its hosting stack (a Files document window,
|
||||
* or pushed over the file browser for view-in-place).
|
||||
*/
|
||||
export class FontPreviewerLayer implements Layer {
|
||||
private sizeIndex = DEFAULT_SIZE_INDEX >= 0 ? DEFAULT_SIZE_INDEX : 2;
|
||||
private gammaIndex = 0;
|
||||
private loadable: boolean | null = null;
|
||||
private displayName: string | null = null;
|
||||
/** Rendered samples for the current size+gamma, keyed by sample id. */
|
||||
private readonly cache = new Map<string, GrayImage | null>();
|
||||
|
||||
constructor(
|
||||
private readonly path: string,
|
||||
private readonly fileName: string,
|
||||
) {}
|
||||
|
||||
paint(ctx: LayerContext): GrayImage {
|
||||
const font = getDefaultSmallFont();
|
||||
const { width, height } = ctx.stack.getBaseSize();
|
||||
const out = new GrayImage(width, height, 0);
|
||||
const size = SIZES[this.sizeIndex]!;
|
||||
const gamma = GAMMAS[this.gammaIndex]!;
|
||||
|
||||
const status = `${size}px γ ${gamma.toFixed(1)}`;
|
||||
const statusWidth = font.measureText(status);
|
||||
const title = truncateText(font, this.getDisplayName(), width - 2 * MARGIN_X - statusWidth - 12);
|
||||
out.drawText(font, MARGIN_X, TITLE_Y, title, 220);
|
||||
out.drawText(font, width - MARGIN_X - statusWidth, TITLE_Y, status, 150);
|
||||
|
||||
if (this.loadable === null) {
|
||||
this.loadable = canLoadFontFile(this.path);
|
||||
}
|
||||
const footerY = height - FOOTER_MARGIN;
|
||||
if (!this.loadable) {
|
||||
out.drawText(font, MARGIN_X, BODY_TOP + 16, "(could not load font)", 160);
|
||||
out.drawText(font, MARGIN_X, footerY, gestureHints([[GESTURE_DOUBLE_CLICK, "back"]]), 110);
|
||||
return out;
|
||||
}
|
||||
|
||||
const bodyWidth = width - 2 * MARGIN_X;
|
||||
let y = BODY_TOP;
|
||||
for (const [id, text] of [
|
||||
["pangram", PANGRAM],
|
||||
["charset", CHARSET],
|
||||
] as const) {
|
||||
const line = this.getCached(id, () => renderFontFileText(this.path, text, size, gamma));
|
||||
if (!line) continue;
|
||||
out.bitBlt(line, MARGIN_X, y, { width: bodyWidth });
|
||||
y += line.height + SAMPLE_GAP;
|
||||
}
|
||||
|
||||
// Fill the rest with a wrapped paragraph, however many lines fit.
|
||||
const metrics = fontFileMetrics(this.path, size);
|
||||
const lineHeight = metrics ? metrics.ascent + metrics.descent + metrics.lineGap : size;
|
||||
const paragraphLines = Math.floor((footerY - SAMPLE_GAP - y) / Math.max(1, lineHeight));
|
||||
if (paragraphLines >= 1) {
|
||||
const paragraph = this.getCached(`para:${paragraphLines}:${bodyWidth}`, () =>
|
||||
renderFontFileWrapped({
|
||||
path: this.path,
|
||||
text: PARAGRAPH,
|
||||
sizePx: size,
|
||||
maxWidth: bodyWidth,
|
||||
maxLines: paragraphLines,
|
||||
gamma,
|
||||
}),
|
||||
);
|
||||
if (paragraph) {
|
||||
out.bitBlt(paragraph, MARGIN_X, y + SAMPLE_GAP);
|
||||
}
|
||||
}
|
||||
|
||||
out.drawText(
|
||||
font,
|
||||
MARGIN_X,
|
||||
footerY,
|
||||
gestureHints([
|
||||
[GESTURE_SCROLL, "size"],
|
||||
[GESTURE_CLICK, "gamma"],
|
||||
[GESTURE_DOUBLE_CLICK, "back"],
|
||||
]),
|
||||
110,
|
||||
);
|
||||
return out;
|
||||
}
|
||||
|
||||
handleInput(event: DashboardInputEvent, ctx: LayerContext): void {
|
||||
switch (event.type) {
|
||||
case "scroll-up":
|
||||
this.setSizeIndex(this.sizeIndex + 1);
|
||||
return;
|
||||
case "scroll-down":
|
||||
this.setSizeIndex(this.sizeIndex - 1);
|
||||
return;
|
||||
case "click":
|
||||
this.gammaIndex = (this.gammaIndex + 1) % GAMMAS.length;
|
||||
this.cache.clear();
|
||||
return;
|
||||
case "double-click":
|
||||
ctx.stack.pop();
|
||||
return;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private setSizeIndex(index: number): void {
|
||||
const clamped = Math.max(0, Math.min(SIZES.length - 1, index));
|
||||
if (clamped !== this.sizeIndex) {
|
||||
this.sizeIndex = clamped;
|
||||
this.cache.clear();
|
||||
}
|
||||
}
|
||||
|
||||
private getCached(key: string, render: () => GrayImage | null): GrayImage | null {
|
||||
if (!this.cache.has(key)) {
|
||||
this.cache.set(key, render());
|
||||
}
|
||||
return this.cache.get(key) ?? null;
|
||||
}
|
||||
|
||||
private getDisplayName(): string {
|
||||
if (this.displayName === null) {
|
||||
const name = fontFileDisplayName(this.path);
|
||||
this.displayName = name
|
||||
? name.style && !/^regular$/i.test(name.style)
|
||||
? `${name.family} ${name.style}`
|
||||
: name.family
|
||||
: this.fileName;
|
||||
}
|
||||
return this.displayName;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { type AppContext, type AppDefinition } from "../app-definition";
|
||||
import {
|
||||
createFilesAppWindow,
|
||||
createFontDocumentWindow,
|
||||
createImageDocumentWindow,
|
||||
createTextDocumentWindow,
|
||||
FILES_SURFACE_ID,
|
||||
@@ -33,6 +34,17 @@ function openImageDocumentWindow(ctx: AppContext, title: string, path: string):
|
||||
});
|
||||
}
|
||||
|
||||
function openFontDocumentWindow(ctx: AppContext, title: string, path: string): void {
|
||||
const windowId = `files:doc:${nextDocumentSerial++}`;
|
||||
void ctx
|
||||
.launchInProcessApp(windowId, `window:${windowId}`, (options) =>
|
||||
createFontDocumentWindow(windowId, title, path, options),
|
||||
)
|
||||
.catch((error) => {
|
||||
ctx.appendLog(`font preview window failed: ${error}`);
|
||||
});
|
||||
}
|
||||
|
||||
const filesApp: AppDefinition = {
|
||||
appId: "files",
|
||||
title: "Files",
|
||||
@@ -43,6 +55,7 @@ const filesApp: AppDefinition = {
|
||||
...options,
|
||||
openDocumentWindow: (title, text) => openTextDocumentWindow(ctx, title, text),
|
||||
openImageWindow: (title, path) => openImageDocumentWindow(ctx, title, path),
|
||||
openFontWindow: (title, path) => openFontDocumentWindow(ctx, title, path),
|
||||
}),
|
||||
),
|
||||
// A document arriving via Android's Share intent opens as its own window.
|
||||
|
||||
Reference in new issue
Block a user