forked from Blendman974/faceclaw
Change default font to Roboto Light 14
This commit is contained in:
1 parent
eef9fbaab4
commit
695d6fb8d3
4 files changed
+31
-27
No files matched your search
@@ -1,5 +1,5 @@
|
|||||||
import { wrapText, truncateText } from "../../graphics/textwrap";
|
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 { TtfFont } from "../../graphics/ttf-font";
|
||||||
import { GrayImage, type UiFont } from "../../graphics/image";
|
import { GrayImage, type UiFont } from "../../graphics/image";
|
||||||
import { Layer, type DashboardInputEvent, type LayerContext } from "../../ui/layers";
|
import { Layer, type DashboardInputEvent, type LayerContext } from "../../ui/layers";
|
||||||
@@ -10,18 +10,12 @@ const BODY_X = 18;
|
|||||||
const BODY_Y = 44;
|
const BODY_Y = 44;
|
||||||
const FOOTER_MARGIN = 36;
|
const FOOTER_MARGIN = 36;
|
||||||
|
|
||||||
/**
|
/** Line step for a bitmap face (the 12px font plus leading, as before). */
|
||||||
* 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;
|
const BDF_LINE_STEP = 14;
|
||||||
|
|
||||||
|
/** Body text follows the UI font (a TTF by default; see ui-fonts.ts). */
|
||||||
function getViewerFont(): UiFont {
|
function getViewerFont(): UiFont {
|
||||||
if (getUiFontSelection().kind === "ttf") return getDefaultSmallFont();
|
return getDefaultSmallFont();
|
||||||
return TtfFont.bundled("inter-light", FALLBACK_BODY_FONT_SIZE_PX) ?? getDefaultSmallFont();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function lineStepOf(font: UiFont): number {
|
function lineStepOf(font: UiFont): number {
|
||||||
|
|||||||
@@ -211,10 +211,16 @@ function isMonospaceFont(path: string): boolean {
|
|||||||
// Plain stream/channel copy: java.nio.file.Paths.get is a varargs overload
|
// 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
|
// 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.
|
// (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 {
|
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);
|
const input = new java.io.FileInputStream(source);
|
||||||
try {
|
try {
|
||||||
const output = new java.io.FileOutputStream(target);
|
const output = new java.io.FileOutputStream(temp);
|
||||||
try {
|
try {
|
||||||
const inChannel = input.getChannel();
|
const inChannel = input.getChannel();
|
||||||
output.getChannel().transferFrom(inChannel, 0, inChannel.size());
|
output.getChannel().transferFrom(inChannel, 0, inChannel.size());
|
||||||
@@ -224,4 +230,8 @@ function copyFileBytes(source: string, target: string): void {
|
|||||||
} finally {
|
} finally {
|
||||||
input.close();
|
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`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -23,7 +23,6 @@
|
|||||||
* rasters as immutable — safe because renderGlyphCell is deterministic for a
|
* rasters as immutable — safe because renderGlyphCell is deterministic for a
|
||||||
* given (path, size, gamma), all of which are baked into the atlasKey.
|
* given (path, size, gamma), all of which are baked into the atlasKey.
|
||||||
*/
|
*/
|
||||||
import { knownFolders } from "@nativescript/core";
|
|
||||||
import { allocateFontFingerprintId, type Glyph } from "./bdffont";
|
import { allocateFontFingerprintId, type Glyph } from "./bdffont";
|
||||||
import { GrayImage, grayToNibble, type GlyphFont } from "./image";
|
import { GrayImage, grayToNibble, type GlyphFont } from "./image";
|
||||||
import { toUint8Array } from "../util/array-util";
|
import { toUint8Array } from "../util/array-util";
|
||||||
@@ -38,12 +37,6 @@ declare const global: any;
|
|||||||
*/
|
*/
|
||||||
const DEFAULT_TTF_GAMMA = 1.0;
|
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<string, TtfFont | null>();
|
const loadedFonts = new Map<string, TtfFont | null>();
|
||||||
|
|
||||||
export class TtfFont implements GlyphFont {
|
export class TtfFont implements GlyphFont {
|
||||||
@@ -98,12 +91,6 @@ export class TtfFont implements GlyphFont {
|
|||||||
return 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
|
* The codepoint's glyph (rendered and memoized on first use), or undefined
|
||||||
* when the render fails outright. Android renders SOMETHING for any
|
* when the render fails outright. Android renders SOMETHING for any
|
||||||
|
|||||||
@@ -11,7 +11,7 @@
|
|||||||
import { getFont, type BdfFont } from "./bdffont";
|
import { getFont, type BdfFont } from "./bdffont";
|
||||||
import type { UiFont } from "./image";
|
import type { UiFont } from "./image";
|
||||||
import { TtfFont } from "./ttf-font";
|
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";
|
import { getStringSetting, onSettingsStoreChanged, setStringSetting } from "../native/settings-store";
|
||||||
|
|
||||||
export type BitmapFace = "terminus" | "terminusv";
|
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. */
|
/** Whether a small-font selection at this path+size satisfies the UI bound. */
|
||||||
export function uiFontSizeAllowed(path: string, size: number): boolean {
|
export function uiFontSizeAllowed(path: string, size: number): boolean {
|
||||||
|
ensurePreinstalledFonts(); // the default selection may precede any picker use
|
||||||
const font = TtfFont.load(path, size);
|
const font = TtfFont.load(path, size);
|
||||||
return (
|
return (
|
||||||
font !== null &&
|
font !== null &&
|
||||||
@@ -71,11 +72,19 @@ export function parseFontSelection(raw: string): UiFontSelection | null {
|
|||||||
return 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 {
|
export function getUiFontSelection(): UiFontSelection {
|
||||||
const parsed = parseFontSelection(getStringSetting(UI_FONT_SELECTION_KEY, ""));
|
const parsed = parseFontSelection(getStringSetting(UI_FONT_SELECTION_KEY, ""));
|
||||||
if (parsed) return parsed;
|
if (parsed) return parsed;
|
||||||
const legacy = getStringSetting(LEGACY_UI_FONT_KEY, "terminus");
|
// A pre-picker-era explicit bitmap choice is honored; fresh states get the
|
||||||
return { kind: "bitmap", face: legacy === "terminusv" ? "terminusv" : "terminus" };
|
// 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 {
|
export function setUiFontSelection(selection: UiFontSelection): void {
|
||||||
@@ -126,6 +135,10 @@ function installInvalidation(): void {
|
|||||||
*/
|
*/
|
||||||
function resolveTtf(selection: UiFontSelection, minLineHeight = 0): TtfFont | null {
|
function resolveTtf(selection: UiFontSelection, minLineHeight = 0): TtfFont | null {
|
||||||
if (selection.kind !== "ttf") return 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);
|
const path = installedFontPath(selection.file);
|
||||||
for (let size = selection.size; size <= selection.size + MAX_SIZE_GROWTH; size++) {
|
for (let size = selection.size; size <= selection.size + MAX_SIZE_GROWTH; size++) {
|
||||||
const font = TtfFont.load(path, size);
|
const font = TtfFont.load(path, size);
|
||||||
|
|||||||
Reference in new issue
Block a user