forked from Blendman974/faceclaw
Use TTF fonts throughout the UI; also add some more functions to app extension API
This commit is contained in:
1 parent
5abbe2a405
commit
96842ceb23
82 files changed
+2666
-288
No files matched your search
+42
-45
@@ -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<UiFontChoice, EmbeddedFontName> = {
|
||||
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.
|
||||
Reference in new issue
Block a user