// What each check says: its measured value, and for a Borderline or Problem check why, and what to do for what the // picture shows, in copy.json's words. The checks are diagnostics: nothing here changes the level or the confidence. import { BASE_FRAME, count, pctText } from './util.js'; import { t } from './copy.js'; import { both, inchText } from './units.js'; const SHADOW_SHOWN = 0.3; // a shadow this deep is shown beside Fine: the shadow test's 30% step const FACING = 5; // degrees: a head-on code measures 0 to 4.3 const longSide = (r) => Math.max(...r.size); const small = (r) => longSide(r) <= BASE_FRAME; // read at its own size, never enlarged const tenths = (x, round) => round(Number((10 * x).toFixed(9))) / 10; /** Pixels per square: one decimal under 10, whole from 10, never on the wrong side of a limit as shown. A Fine value * is rounded up, so it never shows under the target the advice gives (itself rounded up); a Borderline or Problem * one is cut down, so it never reaches that target; and a Borderline one never shows under the Problem limit rounded * up, which every Problem value stays under. p: the ppm check (its status and limits), or none for a plain value. */ export function ppmText(x, p = {}) { if (x == null) return null; if (x >= 10) return String(Math.round(x)); // whole: far above every limit let v = tenths(x, p.status === 'ok' ? Math.ceil : p.status ? Math.floor : Math.round); if (p.status === 'watch' && p.problem_below != null) v = Math.max(v, tenths(p.problem_below, Math.ceil)); return v >= 10 ? '10' : v.toFixed(1); } const pct = (v) => (v == null ? null : pctText(v)); /** The margin with its unit: up to two decimals (it is measured in quarter squares). */ function marginText(m) { if (m == null) return null; return t(m === 1 ? 'checks.quiet.margin_one' : 'checks.quiet.margin_many', { amount: String(Number(m.toFixed(2))) }); } /** The values every check's strings may name. */ function vars(r) { const c = r.checks; const p = c.ppm || {}; const target = p.ok_from != null ? tenths(p.ok_from, Math.ceil) : null; const grow = (to) => (to != null && p.at1280 ? String(tenths(to / p.at1280, Math.ceil)) : null); const { size = {}, quiet = {}, blur = {}, contrast = {}, lighting = {}, perspective = {} } = c; return { ppm: ppmText(p.at1280, p), ppm_native: p.native === p.at1280 ? ppmText(p.native, p) : ppmText(p.native), // a file read at its own size: shown the same long: count(longSide(r)), ok_ppm: target != null ? target.toFixed(1) : null, grow_ok: grow(target), grow_watch: grow(p.problem_below), version: c.version, squares: c.modules, px: size.native != null ? count(Math.trunc(size.native)) : null, share: pct(size.share), // GS1's packaging width with the margin, at 0.495 mm a square, with its inch twin mm_margin: c.modules ? both(t('plan.unit.mm', { n: ((c.modules + 8) * 0.495).toFixed(1) }), inchText((c.modules + 8) * 0.0495)) : null, margin: marginText(quiet.modules), blur_pct: pct(blur.modules), contrast_pct: contrast.value != null ? pct(Math.min(1, contrast.value)) : null, shadow_pct: lighting.ratio != null ? pct(1 - lighting.ratio) : null, glare_pct: pct(lighting.clipped), tilt: perspective.tilt != null ? Math.round(perspective.tilt) : null, keystone_pct: perspective.keystone ? pct(1 - 1 / perspective.keystone) : null, }; } /** The Measured cell: '' where nothing can be said (the table shows a dash). */ export function valueText(key, r, kind) { const x = r.checks[key] || {}; const v = vars(r); const why = x.why || []; if (x.status == null) { if (key === 'quiet' && why.includes('unpinned')) return t('checks.quiet.value_unpinned'); if (key === 'size' && x.share != null) return t('checks.size.value_unknown', v); if (key === 'perspective' && x.keystone != null && x.tilt == null) return t('checks.perspective.value_keystone', v); return ''; } switch (key) { case 'ppm': if (kind === 'graphic') return t(small(r) ? 'checks.ppm.value_graphic_small' : 'checks.ppm.value_graphic', v); return t(small(r) ? 'checks.ppm.value_small' : 'checks.ppm.value', v); case 'size': return t(r.checks.modules_from === 'estimate' ? 'checks.size.value_estimate' : 'checks.size.value', v); case 'quiet': if (x.edge) return t(x.modules === 0 ? 'checks.quiet.value_cut_none' : 'checks.quiet.value_cut', v); if (x.under) return t('checks.quiet.value_under', v); if (x.modules >= 4) return t('checks.quiet.value_full'); return t(x.modules === 0 ? 'checks.quiet.value_none' : 'checks.quiet.value', v); case 'blur': return t('checks.blur.value', v); case 'contrast': return t(x.inverted ? 'checks.contrast.value_inverted' : 'checks.contrast.value', v); case 'lighting': { // in a file, a darker part is its own colours, not a shadow const g = kind === 'graphic' ? '_graphic' : ''; if (why.includes('glare')) return t(why.includes('shadow') ? `checks.lighting.value_both${g}` : 'checks.lighting.value_glare', v); if (why.includes('shadow') || (x.status === 'ok' && x.ratio != null && 1 - x.ratio >= SHADOW_SHOWN)) return t(`checks.lighting.value_shadow${g}`, v); return t('checks.lighting.value_even'); } case 'perspective': return x.tilt < FACING ? t('checks.perspective.value_facing') : t('checks.perspective.value', v); default: return ''; } } /** The Measured cells whose words follow what the picture shows, in place in the section box (yours.js, when the * choice changes): pixels per square and lighting. */ export function refreshValues(box, r, kind) { for (const k of ['ppm', 'lighting']) { const cell = box.querySelector(`tr[data-key="${k}"] .value`); if (cell && r.checks[k]?.status != null) cell.textContent = valueText(k, r, kind) || '—'; } } /** For each reason, in the server's order: why it matters, and what to do for this kind of picture (an action * already given in the cell is not repeated). A missing string leaves its place empty. */ export function adviceParts(key, r, kind) { const c = r.checks; const x = c[key]; const v = vars(r); const seen = new Set(); return (x.why || []).map((w) => { // in a file, a margin its own edge ends is the file's own margin (as made.js has it), not a close crop const because = key === 'quiet' && w === 'edge' && kind === 'graphic' ? 'margin' : w; let ex = `checks.${key}.explain.${because}.${x.status}`; if (key === 'ppm' && c.version >= 5) ex += c.version >= 9 ? '_big' : '_mid'; // more squares need more pixels each if (key === 'size' && c.version > 21) ex = 'checks.size.explain.dense.beyond'; if (key === 'quiet' && because === 'margin' && x.under) ex += '_under'; if (key === 'lighting' && w === 'shadow' && kind === 'graphic') ex += '_graphic'; const as = key === 'ppm' && kind === 'graphic' && small(r) ? 'graphic_small' : kind; const act = t(`checks.${key}.advice.${w}.${x.status}.${as}`, v); const out = { why: t(ex, v), act: seen.has(act) ? '' : act }; seen.add(act); return out; }); } /** Notes, never statuses: a close or tight framing, and a picture small enough to be read at its own size. */ export function noteLines(r, kind, levelKey, drawn) { const out = drawn ? [t('checks.size.note.svg', { long: count(drawn) })] : []; // an SVG: its pixels are the page's drawing if (r.checks?.size?.tight) out.push(t(`checks.size.note.tight.${kind}`)); if (small(r) && levelKey !== 'easy') out.push(t(`checks.size.note.small.${kind}`, { long: count(longSide(r)) })); return out.filter(Boolean); }