// Copyright (C) 2026 Danilo M. // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License version 2 as // published by the Free Software Foundation. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // Run: node desktop/modules/fonts/test_fonts.js // Fonts.js is a QML library, so its .pragma line is stripped before node // evaluates it. const fs = require("fs"); const assert = require("assert"); const src = fs.readFileSync(__dirname + "/Fonts.js", "utf8").replace(/^\.pragma library$/m, ""); const F = new Function(src + "\nreturn { size, entryPx, withOverride, parseModel, seed, qtPt, gtkPt, kittyPt, " + "rofiEntries, hyprlockEntries, setQt, setGtk, gtkFamily, kittyFile, rofiFile, hyprlockFile };")(); const qt = '[Fonts]\nfixed="Inconsolata Nerd Font Mono,14,-1,5,400"\ngeneral="Noto Sans,14,-1,5,400,Regular"\n'; const gtk3 = "[Settings]\ngtk-font-name=Noto Sans, 10\ngtk-theme-name=udt\n"; const gtk2 = 'gtk-font-name="Noto Sans, 10"\n'; assert.strictEqual(F.qtPt(qt), 14); assert.strictEqual(F.gtkPt(gtk3), 10); assert.strictEqual(F.gtkPt(gtk2), 10); assert.strictEqual(F.kittyPt("font_size 15.0\n"), 15); assert.strictEqual(F.gtkFamily(gtk3), "Noto Sans"); assert.strictEqual(F.setQt(qt, 13.5), '[Fonts]\nfixed="Inconsolata Nerd Font Mono,13.5,-1,5,400"\ngeneral="Noto Sans,13.5,-1,5,400,Regular"\n'); assert.strictEqual(F.setQt("[Appearance]\n", 12), null); assert.strictEqual(F.setGtk(gtk3, 12), "[Settings]\ngtk-font-name=Noto Sans, 12\ngtk-theme-name=udt\n"); assert.strictEqual(F.setGtk(gtk2, 12), 'gtk-font-name="Noto Sans, 12"\n'); // The first save changes nothing: what differs from 16px is an override. const m = F.seed({ quickshell: 16, qt: 14 * 4 / 3, gtk: 10 * 4 / 3, kitty: 20 }); assert.deepStrictEqual(m, { base: 16, override: { qt: 18.67, gtk: 13.33, kitty: 20 } }); assert.strictEqual(F.size(m, "quickshell"), 16); assert.strictEqual(F.size(F.withOverride(m, "qt", null), "qt"), 16); assert.strictEqual(F.parseModel("nope"), null); assert.deepStrictEqual(F.parseModel(JSON.stringify(m)), m); const rasi = ['* { font: var(kf-common-font, "Noto Sans 11"); }', 'entry { font: var(kf-launcher-entry, "Noto Sans Bold 15"); }', 'x { font: var(kf-common-font, "Noto Sans 11"); }']; const re = F.rofiEntries(rasi); assert.deepStrictEqual(re.map(e => [e.name, e.family, e.pt]), [["kf-common-font", "Noto Sans", 11], ["kf-launcher-entry", "Noto Sans Bold", 15]]); // rofi at 22px: the 11pt body becomes 16.5pt, the 15pt entry keeps 15/11. const r = { base: 16, override: { rofi: 22 } }; assert.strictEqual(F.rofiFile(r, re).split("\n").slice(2, 4).join("|"), ' kf-common-font: "Noto Sans 16.5";| kf-launcher-entry: "Noto Sans Bold 22.5";'); const r2 = F.withOverride(r, "rofi:kf-launcher-entry", 40); assert.ok(F.rofiFile(r2, re).includes('kf-launcher-entry: "Noto Sans Bold 30"')); const hl = F.hyprlockEntries("$font = Noto Sans\n$kf_clock = 120\n$kf_body = 16\n"); assert.deepStrictEqual(hl, [{ name: "kf_clock", pt: 120 }, { name: "kf_body", pt: 16 }]); // Following the base 16px: body 12pt, clock 120 * 12 / 16 = 90. assert.strictEqual(F.hyprlockFile({ base: 16, override: {} }, hl).split("\n").slice(1).join("|"), "$kf_clock = 90|$kf_body = 12|"); // A seeded rofi keeps its designed sizes exactly. const sr = F.seed({ rofi: 11 * 4 / 3 }); assert.ok(F.rofiFile(sr, [{ name: "kf-a", family: "M", pt: 11 }, { name: "kf-b", family: "M", pt: 72 }]).includes('kf-b: "M 72"')); assert.strictEqual(F.kittyFile(m).split("\n")[1], "font_size 15"); console.log("ok");