Norcleeh (Discord #general): magpie in Docker, used through the web GUI,
Settings' font menu doesn't list the fonts he installed on his own
computer; the app does. Cause, confirmed in the code: a tab's list is
/api/fonts, the server's families (none in a nogui build such as Docker)
plus commonFamilies, filtered by what the browser draws. A family that
is only on the browser's computer was never offered, and a choice the
server doesn't list was dropped by desktopFonts.available.
Now (webfonts.js, which only a browser tab loads; the app's windows are
unchanged):
- Where the Local Font Access API is (Chromium on https or localhost),
the press that opens a family menu calls queryLocalFonts() once a
page, and at load when the local-fonts permission is already given.
The families join the list with their real styles ("SemiBold
Condensed Italic" -> weight 600, 75%, italic).
- Elsewhere (Safari, Firefox, a plain-http LAN address, a refused
permission) the menu's search says "Search or type a font name…" and
offers Use "name" for a family the list lacks, enabled when the canvas
probe says this browser draws it, greyed with "This browser can't find
this font" when not; Enter takes it.
- A chosen family the server doesn't list stays in the tab's list on
later loads while the browser draws it, so it keeps being applied.
- The app's menu (openProtoMenu), never a <select>. Strings in zh,
zh-TW, ja and de. docs/subsystems/gui-shell.md says so.
Budget: no raise. here() moved out of fonts.js into webfonts.js, which
isn't a core script: loadFonts import()s it in a tab only (the page has
no CSP). The core is 106 bytes smaller than origin/main's.
Run (mote-run). At this head on e8c7dddcb: gofmt, go build ./... and
-tags nogui, go test -run 'Font|Budget|Boot|Asset' ./internal/gui
./internal/fonts ok; fonts, i18n-keys, web-page, settings-general,
inner-scroll-contain 114/114. Earlier, on 6af83172d (with a <script>
loader in place of the import):
- fonts, font-cascade, win-fonts, i18n-keys, settings-* (10 files),
text-size, web-page, web-app-standalone, mobile-web, gui-de, gui-ja,
gui-zh-tw, menu-scroll, omarchy, proto-menu-overflow: 311 tests
before the rebase, 300 ok. Red: mobile-web's 8 "desktop
matches origin/main" subtests (they read origin/main through git, and
a mote-run copy has no .git), and settings-lines-wrap webkit zh 440px
(a Models tab press timing out at 8s under load). At the rebased
head: fonts, i18n-keys, settings-general, web-page,
settings-lines-wrap: fonts' WebKit half and a lines-wrap zh-TW press
timed out at mote load ~210 (the box then rebooted); rerun at load
~14, fonts WebKit 21/21 and settings-lines-wrap 20/20, the rest of the
first run ok. The 8s press is filed as #4026.
- New tests in fonts.test.cjs, Chromium and WebKit, en and zh, 1000px
and 440px: "a browser tab lists the fonts of the computer it runs on"
(queryLocalFonts stubbed: not asked before the press, asked once in
it, families and real styles offered, applied, no settings POST) and
"a browser tab takes a typed font when the API is missing/refused".
All 16 red on origin/main's assets (MAGPIE_FONT_ASSETS): this
computer's families not offered, the search not taking a name.
- Broken by hand: merge() without the tab's own chosen families: the
typed tests go red at "kept across a reload, though the server lists
no such font" in all 8 cases.
- go build ./... and -tags nogui, gofmt -l, GOOS=linux go vet -tags
nogui ./internal/gui, GOOS=windows go vet ./internal/gui ok; go test
-tags nogui -run 'Font|Budget|Boot|Asset' ./internal/gui
./internal/fonts ok.
- Real use on mote: a nogui `magpie web` in a sandbox HOME (server list
57 common families). Chromium with local-fonts granted: 204 families,
185 only on this Mac; Baskerville picked, drawn, kept across a reload.
WebKit (no API): typed Didot, offered and taken, kept across a reload.
Not tried: Chromium's real permission prompt (Playwright grants it),
Firefox, a browser on another computer than magpie's, a Docker image
run, the full Go suite (no Go change).
118 lines
6.0 KiB
JavaScript
118 lines
6.0 KiB
JavaScript
// A browser tab's fonts: the ones on the computer the browser runs on,
|
|
// which may not be the one magpie runs on (Norcleeh on Discord: magpie in
|
|
// Docker, its web GUI listed the container's fonts, never his own). Only
|
|
// a tab loads this (app.js loadFonts imports it); the app's windows list the
|
|
// installed fonts magpie reads itself.
|
|
//
|
|
// A tab's list is the server's families it draws, plus:
|
|
// - its computer's own, from the Local Font Access API (Chromium, on
|
|
// https or localhost), asked the first time the family menu is opened,
|
|
// since it needs a press and asks the user's permission once; read at
|
|
// once when that permission is already given;
|
|
// - the families chosen in this tab (kept in localStorage by fonts.js);
|
|
// - any family typed into the menu's search that this browser draws, for
|
|
// Safari, Firefox, a plain-http address or a refused permission.
|
|
// "Draws" is a canvas probe: text in the family measures apart from
|
|
// monospace's or serif's.
|
|
window.webFonts = (() => {
|
|
let ctx, local = null, asked = false, base = [];
|
|
const extra = [], seen = new Map();
|
|
const quoted = window.desktopFonts.quoted;
|
|
function drawn(name) {
|
|
if (!seen.has(name)) {
|
|
ctx ||= document.createElement("canvas").getContext("2d");
|
|
const w = (f) => (ctx.font = "72px " + f, ctx.measureText("mmmlli WwQ@ 0123 中文").width);
|
|
seen.set(name, !!name && ["monospace", "serif"].some((b) => w(quoted(name) + "," + b) !== w(b)));
|
|
}
|
|
return seen.get(name);
|
|
}
|
|
const face = (family, name = "Regular", weight = 400, style = "normal", stretch = 100) => ({ family, name, weight, style, stretch });
|
|
const regular = (family) => [face(family), face(family, "Bold", 700)];
|
|
const same = window.desktopFonts.same;
|
|
// a face's CSS traits from the style name the system gives it
|
|
// ("SemiBold Condensed Italic")
|
|
const first = (rules, s, d) => (rules.find(([re]) => re.test(s)) || [, d])[1];
|
|
const WEIGHTS = [[/thin|hairline/, 100], [/(extra|ultra)-?light/, 200], [/light/, 300], [/(semi|demi)-?bold/, 600],
|
|
[/(extra|ultra)-?bold/, 800], [/black|heavy/, 900], [/medium/, 500], [/bold/, 700]];
|
|
const STRETCHES = [[/ultra-?condensed/, 50], [/extra-?condensed/, 62.5], [/semi-?condensed/, 87.5], [/condensed|narrow/, 75],
|
|
[/semi-?expanded/, 112.5], [/extra-?expanded/, 150], [/ultra-?expanded/, 200], [/expanded|wide/, 125]];
|
|
function group(data) {
|
|
const fams = new Map();
|
|
for (const d of data) {
|
|
if (!d.family) continue;
|
|
const s = String(d.style || "").toLowerCase().replace(/\s+/g, "");
|
|
const f = face(d.family, d.style || "Regular", first(WEIGHTS, s, 400), /italic/.test(s) ? "italic" : /oblique/.test(s) ? "oblique" : "normal", first(STRETCHES, s, 100));
|
|
const styles = fams.get(d.family) || [];
|
|
if (!styles.some((o) => o.weight === f.weight && o.style === f.style && o.stretch === f.stretch)) styles.push(f);
|
|
fams.set(d.family, styles);
|
|
}
|
|
return [...fams].map(([name, styles]) => ({ name, styles: styles.sort((a, b) => a.weight - b.weight || (a.style > b.style) - (a.style < b.style) || a.stretch - b.stretch) }));
|
|
}
|
|
function merge() {
|
|
const out = new Map();
|
|
for (const f of [...base, ...(local || []), ...extra]) out.set(f.name, f);
|
|
const own = window.desktopFonts.own() || {};
|
|
for (const c of [own.uiFont, own.codeFont]) {
|
|
if (typeof c?.family !== "string" || !drawn(c.family)) continue;
|
|
const f = out.get(c.family) || { name: c.family, styles: regular(c.family) };
|
|
out.set(c.family, f.styles.some((s) => same(s, c)) ? f : { ...f, styles: [...f.styles, c] });
|
|
}
|
|
return [...out.values()].sort((a, b) => a.name.toLowerCase().localeCompare(b.name.toLowerCase()));
|
|
}
|
|
// called in the press that opens the menu: queryLocalFonts needs it
|
|
async function read() {
|
|
local = group(await window.queryLocalFonts());
|
|
}
|
|
// list: the tab's families, from the server's (/api/fonts)
|
|
async function list(server) {
|
|
base = server.filter((f) => drawn(f.name));
|
|
if (!asked && window.queryLocalFonts) try {
|
|
if ((await navigator.permissions.query({ name: "local-fonts" })).state === "granted") await read(), asked = true;
|
|
} catch {}
|
|
return merge();
|
|
}
|
|
// ask: the first time a family menu opens where the browser can tell
|
|
// this computer's fonts, they join the list (app.js fontList)
|
|
const catalogue = () => window.desktopFonts.catalogue(fontList = merge());
|
|
function ask() {
|
|
if (asked || !window.queryLocalFonts) return;
|
|
asked = true; // a refusal isn't asked again on this page
|
|
return read().then(catalogue, () => {});
|
|
}
|
|
// typing: the open family menu's search also takes a family the list
|
|
// lacks; it is offered when this browser draws it, and chosen for key
|
|
function typing(key) {
|
|
const box = protoMenu?.box, q = box?.querySelector(".sc-q");
|
|
if (!q) return;
|
|
q.placeholder = t("Search or type a font name…");
|
|
q.setAttribute("aria-label", q.placeholder);
|
|
const b = el("button", "pm-item typed-font"), words = el("span", "pm-words"), name = el("span", "pm-name"), note = el("span", "pm-note");
|
|
b.type = "button";
|
|
b.hidden = true;
|
|
words.append(name, note);
|
|
b.append(el("span", "pm-tick"), words);
|
|
box.lastChild.before(b); // above Refresh fonts
|
|
hoverFocus(b);
|
|
let family = "";
|
|
q.addEventListener("input", () => {
|
|
family = q.value.trim();
|
|
const low = family.toLowerCase(), ok = drawn(family);
|
|
b.hidden = !family || (fontList || []).some((f) => f.name.toLowerCase() === low);
|
|
b.disabled = !ok;
|
|
name.textContent = t("Use “{name}”", { name: family });
|
|
note.textContent = ok ? "" : t("This browser can't find this font");
|
|
});
|
|
// Enter takes it when no listed family matched (app.js took that one)
|
|
q.addEventListener("keydown", (e) => { if (e.key === "Enter" && !e.defaultPrevented && !b.hidden && !b.disabled) b.click(); });
|
|
b.onclick = (e) => {
|
|
e.stopPropagation();
|
|
closeProtoMenu();
|
|
const styles = regular(family);
|
|
extra.push({ name: family, styles });
|
|
catalogue();
|
|
chooseFont(key, styles[0]);
|
|
};
|
|
}
|
|
return { list, ask, typing, drawn };
|
|
})();
|