diff --git a/app/apps/files/text-viewer.ts b/app/apps/files/text-viewer.ts index 2482794e..5d0ac4c3 100644 --- a/app/apps/files/text-viewer.ts +++ b/app/apps/files/text-viewer.ts @@ -1,5 +1,5 @@ import { wrapText, truncateText } from "../../graphics/textwrap"; -import { getDefaultSmallFont, getUiFontSelection } from "../../graphics/ui-fonts"; +import { getDefaultSmallFont } 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"; @@ -10,18 +10,12 @@ const BODY_X = 18; const BODY_Y = 44; 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). */ +/** Line step for a bitmap face (the 12px font plus leading, as before). */ const BDF_LINE_STEP = 14; +/** Body text follows the UI font (a TTF by default; see ui-fonts.ts). */ function getViewerFont(): UiFont { - if (getUiFontSelection().kind === "ttf") return getDefaultSmallFont(); - return TtfFont.bundled("inter-light", FALLBACK_BODY_FONT_SIZE_PX) ?? getDefaultSmallFont(); + return getDefaultSmallFont(); } function lineStepOf(font: UiFont): number { diff --git a/app/graphics/installed-fonts.ts b/app/graphics/installed-fonts.ts index 7a96b09d..14d7259a 100644 --- a/app/graphics/installed-fonts.ts +++ b/app/graphics/installed-fonts.ts @@ -211,10 +211,16 @@ function isMonospaceFont(path: string): boolean { // 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. +// +// Written to a temp name and renamed into place (atomic on the same +// filesystem): preinstallation can be triggered concurrently from several JS +// isolates on first launch (each resolving the default UI font), and readers +// must never see a half-written font file. function copyFileBytes(source: string, target: string): void { + const temp = `${target}.tmp-${Math.floor(Math.random() * 0x7fffffff).toString(36)}`; const input = new java.io.FileInputStream(source); try { - const output = new java.io.FileOutputStream(target); + const output = new java.io.FileOutputStream(temp); try { const inChannel = input.getChannel(); output.getChannel().transferFrom(inChannel, 0, inChannel.size()); @@ -224,4 +230,8 @@ function copyFileBytes(source: string, target: string): void { } finally { input.close(); } + if (!new java.io.File(temp).renameTo(new java.io.File(target))) { + new java.io.File(temp).delete(); + throw new Error(`rename to ${target} failed`); + } } diff --git a/app/graphics/ttf-font.ts b/app/graphics/ttf-font.ts index 55094d0a..3118235e 100644 --- a/app/graphics/ttf-font.ts +++ b/app/graphics/ttf-font.ts @@ -23,7 +23,6 @@ * 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"; @@ -38,12 +37,6 @@ declare const global: any; */ 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 { @@ -98,12 +91,6 @@ export class TtfFont implements GlyphFont { 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 diff --git a/app/graphics/ui-fonts.ts b/app/graphics/ui-fonts.ts index c15526f3..bcca0c53 100644 --- a/app/graphics/ui-fonts.ts +++ b/app/graphics/ui-fonts.ts @@ -11,7 +11,7 @@ import { getFont, type BdfFont } from "./bdffont"; import type { UiFont } from "./image"; import { TtfFont } from "./ttf-font"; -import { getInstalledFont, installedFontPath } from "./installed-fonts"; +import { ensurePreinstalledFonts, getInstalledFont, installedFontPath } from "./installed-fonts"; import { getStringSetting, onSettingsStoreChanged, setStringSetting } from "../native/settings-store"; export type BitmapFace = "terminus" | "terminusv"; @@ -42,6 +42,7 @@ export const SMALL_FONT_MAX_LINE_HEIGHT = 21; /** Whether a small-font selection at this path+size satisfies the UI bound. */ export function uiFontSizeAllowed(path: string, size: number): boolean { + ensurePreinstalledFonts(); // the default selection may precede any picker use const font = TtfFont.load(path, size); return ( font !== null && @@ -71,11 +72,19 @@ export function parseFontSelection(raw: string): UiFontSelection | null { return null; } +/** Default UI font when the user has never picked one. */ +const DEFAULT_UI_FONT: UiFontSelection = { kind: "ttf", file: "Roboto-Light.ttf", size: 14 }; + 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" }; + // A pre-picker-era explicit bitmap choice is honored; fresh states get the + // bundled default. + const legacy = getStringSetting(LEGACY_UI_FONT_KEY, ""); + if (legacy === "terminus" || legacy === "terminusv") { + return { kind: "bitmap", face: legacy }; + } + return DEFAULT_UI_FONT; } export function setUiFontSelection(selection: UiFontSelection): void { @@ -126,6 +135,10 @@ function installInvalidation(): void { */ function resolveTtf(selection: UiFontSelection, minLineHeight = 0): TtfFont | null { if (selection.kind !== "ttf") return null; + // The default selection can be resolved before the picker (whose listing + // preinstalls bundled faces) has ever been opened, so make sure the + // bundled files exist first. Cheap after the first call per isolate. + ensurePreinstalledFonts(); const path = installedFontPath(selection.file); for (let size = selection.size; size <= selection.size + MAX_SIZE_GROWTH; size++) { const font = TtfFont.load(path, size);