74 lignes
4.0 KiB
JavaScript
74 lignes
4.0 KiB
JavaScript
/**
|
|
* Icône adaptative de l'application Android Loksi, rendue par Chromium depuis brand/.
|
|
*
|
|
* node render.mjs <dossier de sortie>
|
|
*
|
|
* Deux couches de 108 dp par densité (mdpi à xxxhdpi), tirées de l'icône sculptée
|
|
* (brand/icones.mjs, export `sculpte`) :
|
|
* - loksi_launcher_background.png : la face, dégradé et lumière, pleine page. Le
|
|
* liseré et le reflet d'angle suivent l'arrondi de la charte et sont laissés : le
|
|
* lanceur pose sa propre forme ;
|
|
* - loksi_launcher_foreground.png : le symbole avec son ombre portée (feDropShadow),
|
|
* ce qu'un VectorDrawable ne sait pas dessiner.
|
|
* Le carré 100 de la charte occupe les 72 dp visibles du centre, le symbole tient dans
|
|
* le cercle sûr de 66 dp.
|
|
*
|
|
* Chaque retouche du maître doit tomber exactement une fois : sinon la charte a
|
|
* changé de forme et le rendu s'arrête. Lancé par loksi.py depuis mobile/.cache/node,
|
|
* où playwright est installé à la version de brand/package-lock.json.
|
|
*/
|
|
import { mkdirSync } from 'fs';
|
|
import { join, dirname, resolve } from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
import { createRequire } from 'module';
|
|
|
|
const here = dirname(fileURLToPath(import.meta.url));
|
|
const brand = resolve(here, '../../brand');
|
|
const out = process.argv[2];
|
|
if (!out) throw new Error('dossier de sortie manquant');
|
|
|
|
const { chromium } = createRequire(join(process.cwd(), 'package.json'))('playwright');
|
|
const { sculpte } = await import(join(brand, 'icones.mjs'));
|
|
|
|
const une = (s, motif, quoi) => {
|
|
const trouves = s.match(new RegExp(motif, 'g')) ?? [];
|
|
if (trouves.length !== 1) throw new Error(`icône Android : « ${quoi} » trouvé ${trouves.length} fois, 1 attendu`);
|
|
return trouves[0];
|
|
};
|
|
|
|
const defs = une(sculpte, '<defs>[\\s\\S]*?</defs>', 'defs');
|
|
// La face et sa lumière passent en coordonnées utilisateur, sur le carré 0 à 100 :
|
|
// le dégradé reste celui du carré et se prolonge (pad) jusqu'aux bords du calque.
|
|
let fondDefs = defs;
|
|
for (const id of ['facestandalone', 'lightstandalone']) {
|
|
const balise = une(fondDefs, `<(linear|radial)Gradient id="${id}"[^>]*>`, id);
|
|
fondDefs = fondDefs.replace(balise, balise.replace(/="(-?[\d.]+)%"/g, '="$1"').replace('>', ' gradientUnits="userSpaceOnUse">'));
|
|
}
|
|
une(sculpte, '<rect width="100" height="100" rx="24" fill="url\\(#facestandalone\\)"/>', 'face');
|
|
une(sculpte, '<rect width="100" height="100" rx="24" fill="url\\(#lightstandalone\\)"/>', 'lumière');
|
|
const debut = sculpte.indexOf('<g transform="translate(3 0) scale(.94)" filter="url(#caststandalone)">');
|
|
if (debut < 0 || sculpte.indexOf('<g transform="translate(3 0) scale(.94)"', debut + 1) >= 0) throw new Error('icône Android : symbole introuvable');
|
|
const symbole = sculpte.slice(debut, sculpte.lastIndexOf('</g></svg>'));
|
|
if ((symbole.match(/<g[ >]/g) ?? []).length !== (symbole.match(/<\/g>/g) ?? []).length) throw new Error('icône Android : symbole mal découpé');
|
|
|
|
const cadre = 'viewBox="-25 -25 150 150" xmlns="http://www.w3.org/2000/svg"';
|
|
const couches = {
|
|
loksi_launcher_background: `<svg ${cadre}>${fondDefs}<rect x="-25" y="-25" width="150" height="150" fill="url(#facestandalone)"/><rect x="-25" y="-25" width="150" height="150" fill="url(#lightstandalone)"/></svg>`,
|
|
loksi_launcher_foreground: `<svg ${cadre}>${defs}${symbole}</svg>`,
|
|
};
|
|
const densites = { mdpi: 1, hdpi: 1.5, xhdpi: 2, xxhdpi: 3, xxxhdpi: 4 };
|
|
|
|
const browser = await chromium.launch();
|
|
const page = await browser.newPage();
|
|
for (const [densite, echelle] of Object.entries(densites)) {
|
|
const cote = 108 * echelle;
|
|
mkdirSync(join(out, `drawable-${densite}`), { recursive: true });
|
|
for (const [nom, svg] of Object.entries(couches)) {
|
|
await page.setViewportSize({ width: cote, height: cote });
|
|
await page.setContent(`<style>html,body{margin:0;width:100%;height:100%;background:transparent}svg{width:100%;height:100%;display:block}</style>${svg}`);
|
|
await page.screenshot({ path: join(out, `drawable-${densite}`, `${nom}.png`), omitBackground: true });
|
|
}
|
|
}
|
|
await browser.close();
|
|
console.log('Icône adaptative rendue dans', out);
|