// The four levels: their names and cuts from data/levels.json (the file the server rates with), what each means on // the measured codes, the row of level names under the bell's axis, and the confidence bars. import { count, css, el, join, pctText, sv } from './util.js'; import { t } from './copy.js'; const PHONES = { apple: 'apple-vision', google: 'google', huawei: 'hms-scankit' }; const GROUP = { // what a measured code is, by its set: {mix} in the levels' lines pristine: 'prints', everyday: 'prints', tough: 'prints', boofcv: 'photos', broken104: 'photos', qrdn: 'research', syn10k: 'research', }; let LV; let STATS; export function initLevels(json) { LV = json; } /** A d on a cut goes to the level above; the left pile (-6) is Easy and the right pile (106) Advanced. */ export const levelOf = (d) => LV.cuts.filter((c) => d >= c).length; export const levelName = (i) => LV.levels[i].name; export const levelKey = (i) => LV.levels[i].key; export const levelNames = () => LV.levels.map((lv) => lv.name); export const indexOfKey = (key) => LV.levels.findIndex((lv) => lv.key === key); /** The level across the cut nearest d; on a cut, the level below it. */ export function nearestOther(d) { const c = LV.cuts.reduce((a, b) => (Math.abs(b - d) < Math.abs(a - d) ? b : a)); return levelName(d < c ? levelOf(c) : levelOf(c) - 1); } /** The score, cut down (never rounded) so a Tough picture never shows 60; the piles have words of their own. */ export function scoreText(place) { if (place.pile === 'lo') return t('verdict.score_lo'); if (place.pile === 'hi') return t('verdict.score_hi'); return t('verdict.score', { s: Math.floor(place.d) }); } export function measuredScore(d) { if (d < 0) return t('panel.measured_level_lo'); if (d > 100) return t('panel.measured_level_hi'); return t('panel.measured_level', { s: Math.floor(d) }); } /** What the scanners did with the measured codes at each level (each code's level from its eight-scanner d), at * 1280 pixels, and what the codes are; read once from bell.json. */ export function levelStats({ n, d, read1280, set }) { const st = LV.levels.map(() => ({ n: 0, apple: 0, google: 0, huawei: 0, all3: 0, any3: 0, zxing: 0, mix: {}, sets: {} })); const totals = {}; for (let i = 0; i < n; i++) { const s = st[levelOf(d(i))]; const ph = Object.values(PHONES).map((k) => read1280(i, k)); s.n += 1; Object.keys(PHONES).forEach((k, j) => { s[k] += ph[j]; }); s.all3 += ph.every(Boolean); s.any3 += ph.some(Boolean); s.zxing += read1280(i, 'zxing-java'); const key = set(i); if (GROUP[key]) s.mix[GROUP[key]] = (s.mix[GROUP[key]] || 0) + 1; s.sets[key] = (s.sets[key] || 0) + 1; totals[key] = (totals[key] || 0) + 1; } for (const s of st) for (const k of [...Object.keys(PHONES), 'all3', 'any3', 'zxing']) s[k] /= s.n || 1; STATS = { st, totals }; } /** What the codes mostly are: the kinds making up a tenth or more, largest first; "mostly" when some were left out. */ function mixText(mix) { const all = Object.values(mix).reduce((a, b) => a + b, 0); if (!all) return ''; const parts = Object.entries(mix).filter(([, c]) => c / all >= 0.1).sort((a, b) => b[1] - a[1]); const names = join(parts.map(([k]) => t(`levels.mix.${k}`))); return Object.values(mix).some((c) => c > 0 && c / all < 0.1) ? t('levels.mix.mostly', { parts: names }) : names; } const num = (v) => pctText(v).replace('%', ''); function shares(m) { const each = [m.apple, m.google, m.huawei]; const [a, b] = [num(Math.min(...each)), num(Math.max(...each))]; return { each3: a === b ? `${a}%` : `${a}–${b}%`, all3: pctText(m.all3), any3: pctText(m.any3), zxing: pctText(m.zxing), google: pctText(m.google) }; } export const gist = (i) => t(`levels.${levelKey(i)}.gist`); /** The levels list: the measured codes all eight scanners place at level i. */ function listLine(i) { const s = STATS.st[i]; return s.n ? t(`levels.${levelKey(i)}.line`, { n: count(s.n), mix: mixText(s.mix), ...shares(s) }) : ''; } /** Beside a picture: the measured codes in pictures of its size class that the five place at its level. */ export function yoursLine(i, big) { const m = LV.meaning?.[big ? 'big' : 'small']?.[levelKey(i)]; if (!m || !m.n) return ''; return t(`levels.${levelKey(i)}.line_yours`, { class: t(big ? 'levels.class.big' : 'levels.class.small'), frame: t(big ? 'levels.frame.big' : 'levels.frame.small'), n: count(m.n), mix: mixText(m.mix || {}), ...shares(m), }); } /** Beside a picture on the right pile: the measured codes the five put there, over both size classes. */ export function pileLine() { const m = LV.meaning?.pile_hi; if (!m || !m.n) return ''; return t('levels.advanced.line_pile_hi', { n: count(m.n), mix: mixText(m.mix || {}), ...shares(m) }); } /** The score's integer band for level i: below 30, 30 to 49, 50 to 59, 60 and up. */ function band(i) { const c = LV.cuts; if (i === 0) return t('levels.band.below', { c: c[0] }); if (i === c.length) return t('levels.band.above', { c: c[c.length - 1] }); return t('levels.band.between', { a: c[i - 1], b: c[i] - 1 }); } /** The prints line-up: how many clean, everyday and tough prints sit at Easy, Everyday and Tough. */ function prints() { const at = (set, key) => count(STATS.st[indexOfKey(key)]?.sets[set] || 0); const of = (set) => count(STATS.totals[set] || 0); return { clean_easy: at('pristine', 'easy'), clean_n: of('pristine'), everyday_everyday: at('everyday', 'everyday'), everyday_n: of('everyday'), tough_tough: at('tough', 'tough'), tough_n: of('tough'), }; } /** "What the four levels mean", under the bell. */ export function renderLevelsList(box) { if (!box) return; // a page cached from before the levels: nothing to fill const summary = box.querySelector('summary'); summary.textContent = t('levels.more_summary'); box.querySelector('.levels-intro').textContent = t('levels.phones_defined'); box.querySelector('.levels-list').replaceChildren(...LV.levels.flatMap((lv, i) => [ el('dt', {}, [el('span', { class: 'lv-name', text: lv.name }), el('span', { class: 'lv-band', text: band(i) })]), el('dd', { text: [gist(i), listLine(i)].filter(Boolean).join(' ') }), ])); box.querySelector('.levels-relation').textContent = t('levels.relation', prints()); box.hidden = !summary.textContent; } /** The footer's sentences on levels, confidence, the checks and the selector, with their numbers from the data. */ export function fillHow() { const conf = LV.confidence; const hi = conf.cells.pile_hi; const [c1, c2, c3] = LV.cuts; const fill = (id, key, vars) => { const node = document.getElementById(id); if (!node) return; node.textContent = t(key, vars); node.hidden = !node.textContent; }; fill('howDashed', 'how.index_html.dashed'); fill('howPileHi', 'how.index_html.pile_hi_add', { hi_n: count(hi.n), hi_pct: pctText(hi.m != null ? hi.m / hi.n : hi.agree) }); fill('howLevels', 'how.index_html.levels', { c1, c2, c3, ...prints() }); fill('howConfidence', 'how.index_html.confidence', { agree_m: count(conf.overall.m), agree_n: count(conf.overall.n), high: pctText(conf.grades.high), medium: pctText(conf.grades.medium), }); fill('howChecks', 'how.index_html.checks'); fill('howDetails', 'how.index_html.details'); fill('howSelector', 'how.index_html.selector'); } /** Under the bell's axis: a notch at each cut, each level's name centred under its band (pushed apart where a band is * narrower than its name), and the band where the cursor is, underlined in ink with its name in bold. */ export function drawLevelAxis(svg, x, base, W, d, [lo, hi]) { const on = levelOf(d); const last = LV.levels.length - 1; const edges = [lo, ...LV.cuts, hi]; const g = sv('g', { class: 'levels', 'aria-hidden': 'true' }, svg); for (const c of LV.cuts) sv('line', { x1: x(c), x2: x(c), y1: base - 0.5, y2: base + 9, stroke: css('--ink-3') }, g); sv('line', { x1: x(edges[on]) + (on ? 1.5 : 0), x2: x(edges[on + 1]) - (on < last ? 1.5 : 0), y1: base + 1.5, y2: base + 1.5, stroke: css('--ink'), 'stroke-width': 2, }, g); const names = LV.levels.map((lv, i) => sv('text', { x: x((edges[i] + edges[i + 1]) / 2), y: base + 25, 'text-anchor': 'middle', class: `level-name${i === on ? ' on' : ''}${W < 520 ? ' narrow' : ''}`, text: lv.name, }, g)); const boxes = names.map((n) => n.getBBox()); let right = -Infinity; for (const b of boxes) { // left to right: no name closer than 8 px to the one before b.x += Math.max(0, right + 8 - b.x); right = b.x + b.width; } for (let i = boxes.length - 1; i >= 0 && right > W - 2; i--) { // then the last one back inside the drawing const limit = i < boxes.length - 1 ? boxes[i + 1].x - 8 - boxes[i].width : W - 2 - boxes[i].width; boxes[i].x = Math.min(boxes[i].x, limit); } names.forEach((n, i) => n.setAttribute('x', boxes[i].x + boxes[i].width / 2)); } /** Confidence as one to three filled bars; the word always stands beside them. */ export const bars = (grade) => el('span', { class: `conf conf-${grade}`, 'aria-hidden': 'true' }, [el('i'), el('i'), el('i')]);