0de11320bb
Инструмент в tools/covergen: SVG 1200x630, детерминированный от слага (FNV-1a -> mulberry32 -> палитра), 5 стилей (poster, bauhaus, constructivist, aurora, paper), типографика с переносом и подбором кегля, аудит геометрии и WCAG-контраста, CLI для одной обложки и для манифеста. Без зависимостей, node >= 18, 12 тестов (node --test) — проходят в этой раскладке. Документация и объём работ по встраиванию в сайт: docs/covergen.md, задача #1.
117 lines
4.1 KiB
JavaScript
117 lines
4.1 KiB
JavaScript
'use strict';
|
|
|
|
const { measure } = require('./typeset');
|
|
|
|
/** "hsl(212 62% 16%)" -> [r,g,b] 0..255 */
|
|
function hslToRgb(value) {
|
|
const m = /hsl\(\s*([\d.]+)\s+([\d.]+)%\s+([\d.]+)%\s*\)/i.exec(String(value));
|
|
if (!m) {
|
|
const hex = /^#([0-9a-f]{6})$/i.exec(String(value));
|
|
if (hex) {
|
|
const n = parseInt(hex[1], 16);
|
|
return [(n >> 16) & 255, (n >> 8) & 255, n & 255];
|
|
}
|
|
throw new Error(`unsupported color: ${value}`);
|
|
}
|
|
const h = Number(m[1]) / 360;
|
|
const s = Number(m[2]) / 100;
|
|
const l = Number(m[3]) / 100;
|
|
if (s === 0) return [l * 255, l * 255, l * 255];
|
|
const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
|
|
const p = 2 * l - q;
|
|
const hue = (t) => {
|
|
let x = t;
|
|
if (x < 0) x += 1;
|
|
if (x > 1) x -= 1;
|
|
if (x < 1 / 6) return p + (q - p) * 6 * x;
|
|
if (x < 1 / 2) return q;
|
|
if (x < 2 / 3) return p + (q - p) * (2 / 3 - x) * 6;
|
|
return p;
|
|
};
|
|
return [hue(h + 1 / 3) * 255, hue(h) * 255, hue(h - 1 / 3) * 255];
|
|
}
|
|
|
|
function luminance(color) {
|
|
const [r, g, b] = hslToRgb(color).map((channel) => {
|
|
const c = channel / 255;
|
|
return c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
|
|
});
|
|
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
|
|
}
|
|
|
|
/** WCAG-контраст: 1..21 */
|
|
function contrastRatio(a, b) {
|
|
const la = luminance(a);
|
|
const lb = luminance(b);
|
|
return (Math.max(la, lb) + 0.05) / (Math.min(la, lb) + 0.05);
|
|
}
|
|
|
|
function attr(attrs, name) {
|
|
const m = new RegExp(`${name}="([^"]*)"`).exec(attrs);
|
|
return m ? m[1] : null;
|
|
}
|
|
|
|
/** Все <text> элементы с координатами и содержимым. */
|
|
function textNodes(svg) {
|
|
const nodes = [];
|
|
const re = /<text([^>]*)>([\s\S]*?)<\/text>/g;
|
|
let match;
|
|
while ((match = re.exec(svg)) !== null) {
|
|
const [, attrs, rawContent] = match;
|
|
nodes.push({
|
|
x: Number(attr(attrs, 'x') ?? 0),
|
|
y: Number(attr(attrs, 'y') ?? 0),
|
|
size: Number(attr(attrs, 'font-size') ?? 16),
|
|
anchor: attr(attrs, 'text-anchor') ?? 'start',
|
|
fill: attr(attrs, 'fill') ?? '#000000',
|
|
fillOpacity: Number(attr(attrs, 'fill-opacity') ?? 1),
|
|
text: rawContent.replace(/<[^>]*>/g, '').replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, "'"),
|
|
});
|
|
}
|
|
return nodes;
|
|
}
|
|
|
|
/**
|
|
* Геометрический и контрастный аудит готового SVG.
|
|
* @returns {{issues: {type: string, detail: string}[], stats: {texts: number, minContrast: number}}}
|
|
*/
|
|
function audit(svg, { width, height, palette, margin = 4, minContrast = 4.5, minFont = 12 }) {
|
|
const issues = [];
|
|
const nodes = textNodes(svg);
|
|
|
|
const bgContrast = contrastRatio(palette.ink, palette.bg);
|
|
if (bgContrast < minContrast) {
|
|
issues.push({ type: 'contrast', detail: `ink/bg = ${bgContrast.toFixed(2)} < ${minContrast}` });
|
|
}
|
|
|
|
for (const node of nodes) {
|
|
const textWidth = measure(node.text, node.size);
|
|
const half = node.anchor === 'middle' ? textWidth / 2 : node.anchor === 'end' ? textWidth : 0;
|
|
const left = node.x - half;
|
|
const right = node.x + (node.anchor === 'middle' ? textWidth / 2 : node.anchor === 'end' ? 0 : textWidth);
|
|
const baseline = node.y;
|
|
if (baseline - node.size * 0.85 < margin) {
|
|
issues.push({ type: 'overflow-top', detail: `"${node.text.slice(0, 24)}" y=${baseline} size=${node.size}` });
|
|
}
|
|
if (baseline > height - margin) {
|
|
issues.push({ type: 'overflow-bottom', detail: `"${node.text.slice(0, 24)}" y=${baseline}` });
|
|
}
|
|
if (left < -margin || right > width + margin) {
|
|
issues.push({
|
|
type: 'overflow-x',
|
|
detail: `"${node.text.slice(0, 24)}" box=[${Math.round(left)},${Math.round(right)}] canvas=${width}`,
|
|
});
|
|
}
|
|
if (node.size < minFont) {
|
|
issues.push({ type: 'font-too-small', detail: `"${node.text.slice(0, 24)}" size=${node.size}` });
|
|
}
|
|
if (node.fillOpacity < 0.45) {
|
|
issues.push({ type: 'faint-text', detail: `"${node.text.slice(0, 24)}" fill-opacity=${node.fillOpacity}` });
|
|
}
|
|
}
|
|
|
|
return { issues, stats: { texts: nodes.length, minContrast: Number(bgContrast.toFixed(2)) } };
|
|
}
|
|
|
|
module.exports = { audit, contrastRatio, textNodes, hslToRgb };
|