archivo del paquete→ .shtukbase/packages/shtukbase-icons/lib/style-check.ts
lib/style-check.ts
45 líneas, 3.3 KB.
El archivo tal como está en el paquete: el código y sus comentarios no se traducen.
- // SBI-R01, the pure part: a flat drawing keeps the set's style, so the build can turn it into style 79 and its reserves.
- // Text in, findings out. What it holds: the flat form (drawing elements only, paint as attributes, no groups), the canvas
- // (viewBox 0 0 96 96), the palette (every colour a step of a hue, ink, a grey or white), no plate under the thing (SBS-R06),
- // no outline of its own (the style draws the outline: no filled shape stroked in ink or in its hue's dark step),
- // lines that survive at 16 px (a line at least 2 wide), and a size that leaves room for the style (1500 bytes flat).
- import { R } from './sbicons.ts';
- export const MIN_STROKE = 2;
- export const MAX_FLAT_BYTES = 1500;
- export function checkFlat(svg: string, palette: Record<string, unknown>): string[] {
- const out: string[] = [];
- let doc: ReturnType<typeof R.parse>;
- if (/<g[\s>]/.test(svg)) out.push('a group (<g>): the flat form has none → shtuk run shtukbase-icons import <file> dissolves it');
- if (/\sstyle\s*=/.test(svg)) out.push('a style attribute: paint goes in attributes (fill, stroke) → import the file');
- try { doc = R.parse(svg); } catch (e) { return [...out, (e as Error).message]; }
- const vb = doc.root.find(([k]) => k === 'viewBox')?.[1]?.trim().replace(/\s+/g, ' ');
- if (vb !== '0 0 96 96') out.push(`viewBox "${vb ?? ''}": the set draws on "0 0 96 96"`);
- if (svg.length > MAX_FLAT_BYTES) out.push(`${svg.length} bytes flat: keep a drawing under ${MAX_FLAT_BYTES} so the styled icon fits the 4096 budget`);
- if (!doc.els.length) out.push('nothing is drawn');
- const allowed = new Set(R.flatColors(palette).map(R.norm));
- const off = new Set<string>();
- doc.els.forEach((el, i) => {
- const a = (k: string) => el.attrs.find(([n]) => n === k)?.[1] ?? null;
- for (const k of ['fill', 'stroke']) {
- const v = a(k);
- if (v === null || v === 'none') continue;
- const c = R.norm(v);
- if (!allowed.has(c) && !off.has(c)) { off.add(c); const s = R.snap(v, palette); out.push(`colour ${v} is not in the palette${s ? ` → ${s.fill} (${s.hue ?? s.tone})` : ''}`); }
- }
- if (i === 0 && el.tag === 'rect' && Number(a('width')) >= 96 * 0.7 && Number(a('height')) >= 96 * 0.7) out.push('a plate: the first shape is a rect over 70% of the canvas behind the thing (SBS-R06) → draw on the surface, without the tile');
- const fill = a('fill'), stroke = a('stroke');
- const filled = !!fill && fill !== 'none', stroked = !!stroke && stroke !== 'none';
- // an outline of its own: a filled shape stroked in ink or in the dark step of its own hue, which is what the style adds.
- // A white ring or a coloured frame around a white card is a detail of the drawing, not an outline.
- if (filled && stroked && R.norm(fill!) !== R.norm(stroke!)) {
- const f = R.toneOf(fill!, palette), s = R.toneOf(stroke!, palette);
- const ink = s && s.tone === 'ink' && (s.hue === null || (f && f.hue === s.hue));
- if (ink) out.push(`<${el.tag}> has an outline of its own (fill ${fill}, stroke ${stroke}): the style draws the outline → drop the stroke`);
- }
- const sw = Number(a('stroke-width') ?? (stroked ? 1 : 0));
- if (stroked && !filled && sw < MIN_STROKE) out.push(`<${el.tag}> a line ${sw} wide: lines of less than ${MIN_STROKE} vanish at 16 px`);
- });
- return out;
- }