ShtukbaseStandard
файл пакета→ .shtukbase/packages/shtukbase-icons/lib/style-check.ts

lib/style-check.ts

45 строк, 3.3 КБ.

Файл как есть в пакете: код и комментарии в нём не переводятся.

  1. // 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.
  2. // Text in, findings out. What it holds: the flat form (drawing elements only, paint as attributes, no groups), the canvas
  3. // (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),
  4. // no outline of its own (the style draws the outline: no filled shape stroked in ink or in its hue's dark step),
  5. // lines that survive at 16 px (a line at least 2 wide), and a size that leaves room for the style (1500 bytes flat).
  6. import { R } from './sbicons.ts';
  7. export const MIN_STROKE = 2;
  8. export const MAX_FLAT_BYTES = 1500;
  9. export function checkFlat(svg: string, palette: Record<string, unknown>): string[] {
  10. const out: string[] = [];
  11. let doc: ReturnType<typeof R.parse>;
  12. if (/<g[\s>]/.test(svg)) out.push('a group (<g>): the flat form has none → shtuk run shtukbase-icons import <file> dissolves it');
  13. if (/\sstyle\s*=/.test(svg)) out.push('a style attribute: paint goes in attributes (fill, stroke) → import the file');
  14. try { doc = R.parse(svg); } catch (e) { return [...out, (e as Error).message]; }
  15. const vb = doc.root.find(([k]) => k === 'viewBox')?.[1]?.trim().replace(/\s+/g, ' ');
  16. if (vb !== '0 0 96 96') out.push(`viewBox "${vb ?? ''}": the set draws on "0 0 96 96"`);
  17. 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`);
  18. if (!doc.els.length) out.push('nothing is drawn');
  19. const allowed = new Set(R.flatColors(palette).map(R.norm));
  20. const off = new Set<string>();
  21. doc.els.forEach((el, i) => {
  22. const a = (k: string) => el.attrs.find(([n]) => n === k)?.[1] ?? null;
  23. for (const k of ['fill', 'stroke']) {
  24. const v = a(k);
  25. if (v === null || v === 'none') continue;
  26. const c = R.norm(v);
  27. 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})` : ''}`); }
  28. }
  29. 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');
  30. const fill = a('fill'), stroke = a('stroke');
  31. const filled = !!fill && fill !== 'none', stroked = !!stroke && stroke !== 'none';
  32. // 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.
  33. // A white ring or a coloured frame around a white card is a detail of the drawing, not an outline.
  34. if (filled && stroked && R.norm(fill!) !== R.norm(stroke!)) {
  35. const f = R.toneOf(fill!, palette), s = R.toneOf(stroke!, palette);
  36. const ink = s && s.tone === 'ink' && (s.hue === null || (f && f.hue === s.hue));
  37. if (ink) out.push(`<${el.tag}> has an outline of its own (fill ${fill}, stroke ${stroke}): the style draws the outline → drop the stroke`);
  38. }
  39. const sw = Number(a('stroke-width') ?? (stroked ? 1 : 0));
  40. if (stroked && !filled && sw < MIN_STROKE) out.push(`<${el.tag}> a line ${sw} wide: lines of less than ${MIN_STROKE} vanish at 16 px`);
  41. });
  42. return out;
  43. }