Download web/plan.js from devmandan/qr-scan-difficulty-check: direct link, hf CLI and curl.
- Browser
- Download file 17.8 kB
-
https://huggingface.co/spaces/devmandan/qr-scan-difficulty-check/resolve/main/web/plan.js
- Command line
-
hf download hf://spaces/devmandan/qr-scan-difficulty-check/web/plan.js
-
curl -L -o plan.js https://huggingface.co/spaces/devmandan/qr-scan-difficulty-check/resolve/main/web/plan.js
17.8 kB
| // How wide to print a read picture's code, or the picture itself, to scan from a distance; or, given the picture's | |
| // printed width, how far its code scans from. The needs, the camera, the distances and the named choices come from | |
| // data/sizes.json, written from the measured grid; the words from copy.json, and lengths as shown from units.js. All | |
| // of it runs in the browser: no distance, width or choice is sent or stored. | |
| import { BASE_FRAME, count, join, lower, sayNames } from './util.js'; | |
| import { getJson, t } from './copy.js'; | |
| import { CHECKS } from './verdict.js'; | |
| import { INCH, both, clean, cmText, distCmText, distText, distUpText, feetText, inchText, inches, numText, passes, rounded, typedText, widthText } from './units.js'; | |
| import { useSizes as hintSizes } from './hints.js'; | |
| import { shapeAnswer, shapeFar, shapeFor, shapeHow, shapeLines, shapeNear, shapeOf, shapeRow, useSizes as shapeSizes } from './shape.js'; | |
| export { distText, numText, typedText, widthText } from './units.js'; // where made.js, plan_view.js and the kits get them | |
| export { framePpm, looksLikeFile, looksLikePhoto, turnOf } from './hints.js'; // the suggestions, moved: their old address | |
| let S; // data/sizes.json | |
| // the scanning distance: one of the four by key, or 'exact' with its cm; and the last of the four chosen | |
| const where = { key: null, cm: null, last: null }; | |
| /** Load sizes.json, and hand it to the suggestions and the shapes; it is returned too, for the test sheet (sheet.js). */ | |
| export async function loadPlan(url) { | |
| S = await getJson(url); | |
| [hintSizes, shapeSizes].forEach((use) => use(S)); | |
| where.key = where.last = S.default_distance; | |
| return S; | |
| } | |
| export const squares = (v) => 17 + 4 * v; | |
| /** The need behind version v: the measured version it takes, its camera pixels per square and the scanners that set | |
| * it. A version between measured ones takes the next one up, version 1 version 2's, and past the last measured one, | |
| * the last one's as a floor. */ | |
| export function needFor(v) { | |
| const last = S.needs[S.needs.length - 1]; | |
| return { ...(S.needs.find((n) => n.version >= v) || last), floor: v > last.version }; | |
| } | |
| /** The code width version v needs to scan from d cm: squares across × need × distance ÷ the camera's pixels. */ | |
| export const needWidth = (v, d) => (squares(v) * needFor(v).ppm * d) / S.camera.pixels; | |
| // ---- the distance (the page's, for every picture) --------------------------------------------------------------- | |
| export const distances = () => S.distances; | |
| export const choice = (name) => S.choices[name]; // a named choice in sizes.json | |
| export const distanceKey = () => where.key; | |
| export const distanceCm = () => (where.key === 'exact' ? where.cm : S.distances.find((d) => d.key === where.key).cm); | |
| export function setDistance(key, cm = null) { | |
| where.key = key; | |
| where.cm = key === 'exact' ? cm : null; | |
| if (key !== 'exact') where.last = key; | |
| } | |
| /** The last of the four distances chosen (the default until one is): emptying an exact distance goes back to it. */ | |
| export const lastPreset = () => where.last; | |
| const nearest = () => S.choices.distance_cm[0]; // the closest distance planned for | |
| /** A typed distance or width in cm, or null outside its stated range (choices in sizes.json). */ | |
| export function inRange(name, cm) { | |
| const [lo, hi] = S.choices[name]; | |
| return Number.isFinite(cm) && cm > 0 && clean(cm) >= lo && clean(cm) <= hi ? cm : null; | |
| } | |
| /** A range in inches: its cm ÷ 2.54, rounded inward to two decimals, so a typed width in inches is checked against | |
| * exactly what the hint states (both ends fall inside the cm range). */ | |
| export function inchRange(name) { | |
| const [lo, hi] = S.choices[name].map((cm) => (100 * cm) / 2.54); | |
| return [Math.ceil(clean(lo)) / 100, Math.floor(clean(hi)) / 100]; | |
| } | |
| /** A range's ends in its field's unit ('cm', 'mm' or 'in'): the low end as a number, the high end with its unit. */ | |
| export function rangeIn(name, unit = 'cm') { | |
| const ends = unit === 'in' ? inchRange(name) : S.choices[name].map((cm) => (unit === 'mm' ? cm * 10 : cm)); | |
| const [lo, hi] = ends.map(numText); | |
| return [lo, t(`plan.unit.${unit}`, { n: hi })]; | |
| } | |
| /** The page's distance as shown: one of the four in both units, rounded down; a typed one as typed, in cm. */ | |
| export const placeText = (cm) => (where.key === 'exact' ? t('plan.unit.cm', { n: typedText(cm) }) : distText(cm)); | |
| /** The same in cm alone: an input to the working behind "+". */ | |
| const placeCm = (cm) => (where.key === 'exact' ? t('plan.unit.cm', { n: typedText(cm) }) : distCmText(cm)); | |
| const toText = (short = false) => t(`plan.distance.${where.key}.${short ? 'short' : 'to'}`, { d: placeText(distanceCm()) }); | |
| const tenths = (x) => String(Number(x.toFixed(1))); | |
| const names = (keys) => join(keys.map((k) => t(`plan.owner.${k}`))); | |
| // ---- the plan for one picture --------------------------------------------------------------------------------------- | |
| /** What the plan says for a read picture, or null when it says nothing (no checks, or the right pile). mode: 'cant' | |
| * (no version, or the checks' estimate overriding one zxing-cpp decoded: then estimate), 'code' (the code's width), | |
| * 'picture' (the original graphic: the picture's own width, from its code's share of the picture's width) or 'width' | |
| * (the same with its printed width given: how far). estimated: the checks' estimate for a code no scanner decoded, | |
| * which every answer says; copies: zxing-cpp's copies, over one; grid: a verified record's (each answer: one copy); | |
| * shape: a slant or a pack the reader gave (shape.js), whose width w then is, or which can't be planned (cant). */ | |
| export function planFor(p) { | |
| const r = p.status === 'done' ? p.result : null; | |
| if (!r?.checks || r.place.pile === 'hi') return null; | |
| const c = r.checks; | |
| const est = c.modules_from === 'estimate'; | |
| if (c.version == null || !(est || c.modules_from === 'scanner') || (est && r.ec_level != null)) return { mode: 'cant', estimate: est ? c.version : null }; | |
| const v = c.version; | |
| const n = squares(v); | |
| const need = needFor(v); | |
| const d = distanceCm(); | |
| const shape = shapeFor(shapeOf(p), n, need.ppm, d); | |
| const w0 = needWidth(v, d); // the width facing the phone, flat; w is it with the shape, null when it can't be | |
| const w = shape?.cant ? null : w0 * (shape?.k ?? 1); | |
| const share = c.size?.native > 0 && r.size?.[0] > 0 ? c.size.native / r.size[0] : null; | |
| const copies = Number.isInteger(r.copies) && r.copies > 1 ? r.copies : 0; | |
| const base = { mode: 'code', v, n, need, d, w0, w, share, screen: p.kind === 'screen', estimated: est, copies, grid: p.made?.state === 'verified' && Boolean(p.made.rec?.code?.grid), shape }; | |
| if (p.kind !== 'graphic' || !share) return base; | |
| if (p.width) { // as typed: { cm, text } | |
| const at = p.width.cm * share; | |
| const sf = shape && shapeFar(shape, at, n, need.ppm); | |
| return { ...base, mode: 'width', W: p.width.cm, typed: p.width.text, at, shape: sf, far: sf ? sf.far ?? 0 : (at * S.camera.pixels) / (n * need.ppm) }; | |
| } | |
| if (shape?.cant) return { ...base, mode: 'picture' }; | |
| const Wx = w / share; // the exact picture width: W and its inch figure are both rounded up from it | |
| const W = rounded(Wx, true); | |
| return { ...base, mode: 'picture', W, Wx, at: W * share, atIn: inches(Wx, 'up') * INCH * share }; | |
| } | |
| export const fileFirst = (plan) => plan?.mode === 'picture' || plan?.mode === 'width'; | |
| /** The code's width the plan stands on: as printed for a file, else at its need. */ | |
| export const codeWidth = (plan) => (fileFirst(plan) ? plan.at : plan.w); | |
| /** The code's width as the answer gives it, in both units: for a picture planned by its width, in cm from W in cm and | |
| * in inches from W in inches (atIn), so each sentence agrees with itself; and the margin, 4 squares of it, the same | |
| * way, with its inch figure rounded up, as a least clear space. */ | |
| export const codeText = (plan) => both(cmText(codeWidth(plan)), inchText(plan.atIn ?? codeWidth(plan))); | |
| const marginText = (plan, k = S.margin_squares / plan.n) => both(cmText(k * codeWidth(plan)), inchText(k * (plan.atIn ?? codeWidth(plan)), 'up')); | |
| /** Versions 22–40 take version 21's need as a floor: their answers say "likely more" (or "likely less"). */ | |
| const floored = (plan, key) => (plan.need.floor ? `${key}_floor` : key); | |
| /** A picture that repeats its code, as zxing-cpp read it or as a verified record says: each answer is for one copy. */ | |
| const each = (plan, key) => (plan.copies || plan.grid ? `${key}_each` : key); | |
| /** A printed width whose code would scan only from closer than the nearest distance planned for. */ | |
| export const tooNear = (plan) => plan.mode === 'width' && clean(plan.far) < nearest(); | |
| /** The answer, in one sentence: the same beside the bell, in "Your picture", on the curve and when it is read. A | |
| * version the checks estimated is said first. */ | |
| export function answerText(plan) { | |
| if (!plan) return ''; | |
| if (plan.mode === 'cant') return t(plan.estimate != null ? 'plan.cant_override' : 'plan.cant'); // estimate: the checks overrode zxing-cpp | |
| const s = shapeAnswer(plan, plan.shape?.cant ? '' : answerOf(plan), { to: toText(), w: plan.at ? codeText(plan) : '' }); | |
| return plan.estimated ? t('plan.estimated', { answer: lower(s) }) : s; | |
| } | |
| function answerOf(plan) { | |
| if (plan.mode === 'width') { | |
| const vars = { W: plan.typed, w: codeText(plan), d: distText(plan.far) }; | |
| if (tooNear(plan)) return t(each(plan, 'plan.width_near'), { ...vars, lo: distText(nearest(), 'up') }); | |
| return t(floored(plan, each(plan, 'plan.width')), vars); | |
| } | |
| if (plan.mode === 'picture') return t(floored(plan, each(plan, 'plan.picture')), { W: widthText(plan.Wx, true), w: codeText(plan), to: toText() }); | |
| return t(floored(plan, each(plan, plan.screen ? 'plan.code_screen' : 'plan.code')), { w: codeText(plan), to: toText() }); | |
| } | |
| const ZXING_MOST = 255; // zxing-cpp returned at most this many codes from one picture in tests (512 copies gave 255) | |
| /** The lines under the answer: the screen note, an estimated version, a code the picture repeats, a short margin, a | |
| * file's Problem checks, who may still miss it, and the assumption the width rests on. */ | |
| export function answerLines(p, plan) { | |
| if (!plan) return []; | |
| if (plan.mode === 'cant') return plan.estimate != null ? [t('plan.cant_estimate', { v: plan.estimate })] : []; | |
| const c = p.result.checks; | |
| const { need } = plan; | |
| const out = [plan.screen ? t('plan.screen') : '', plan.estimated ? t('plan.estimate_line', { v: plan.v }) : '', | |
| plan.copies ? t(plan.copies >= ZXING_MOST ? 'plan.repeats_most' : 'plan.repeats', { k: count(plan.copies) }) : '', ...shapeLines(plan)]; | |
| if (!tooNear(plan) && !plan.shape?.cant && (c.quiet?.edge || (c.quiet?.modules != null && c.quiet.modules < S.margin_squares))) { // 4 squares of the code | |
| out.push(t(each(plan, 'plan.margin'), { m: marginText(plan) })); | |
| } | |
| if (fileFirst(plan)) { // a file read shrunk to 1280 pixels: its print width, not the file, sets its pixels per square | |
| const shrunk = Math.max(...p.result.size) > BASE_FRAME; | |
| const bad = CHECKS.filter((k) => c[k]?.status === 'problem' && !(k === 'ppm' && shrunk)).map((k) => lower(t(`checks.${k}.name`))); | |
| if (bad.length) out.push(t(bad.length === 1 ? 'plan.problem_one' : 'plan.problem_many', { names: join(bad) })); | |
| } | |
| if (need.left_out && !need.floor) out.push(missedText(need.left_out)); | |
| out.push(t(need.floor ? 'plan.assume_floor' : plan.shape ? 'plan.shape.assume' : 'plan.assume', { v: need.version, names: names(need.scanners) })); | |
| return out.filter(Boolean); | |
| } | |
| /** "Huawei's scanner missed about 1 scan in 10 at 5–6 camera pixels per square, the most measured": its misses at | |
| * the two largest steps measured, as 1 in N with N to one significant figure. */ | |
| function missedText({ scanner, reads }) { | |
| const steps = Object.keys(reads).map(Number).sort((a, b) => a - b).slice(-2); | |
| const missed = steps.reduce((s, k) => s + S.rule.of - reads[k.toFixed(1)], 0) / (steps.length * S.rule.of); | |
| if (!missed) return ''; | |
| return t('plan.missed', { who: sayNames([scanner])[0], n: Number((1 / missed).toPrecision(1)), lo: tenths(steps[0]), hi: tenths(steps[steps.length - 1]) }); | |
| } | |
| /** A row's two short lines: the number, and what it is for (with "by an estimated version" when it is), and the | |
| * shape after it (one line when it can't be planned). */ | |
| export const rowLines = (plan) => (plan?.shape ? shapeRow(plan, plan.shape.cant ? [] : rowOf(plan)) : rowOf(plan)); | |
| function rowOf(plan) { | |
| if (!plan) return []; | |
| if (plan.mode === 'cant') return [t('plan.row.cant')]; | |
| const why = (s) => (plan.estimated ? t('plan.row.estimated', { why: s }) : s); | |
| if (plan.mode === 'width') { | |
| const at = why(t('plan.row.width_at', { W: plan.typed })); | |
| if (tooNear(plan)) return [t('plan.row.width_near', { lo: distText(nearest(), 'up') }), at]; | |
| return [t(floored(plan, 'plan.row.width'), { d: distText(plan.far) }), at]; | |
| } | |
| if (plan.mode === 'picture') return [t(floored(plan, 'plan.row.picture'), { W: widthText(plan.Wx, true) }), why(toText(true))]; | |
| return [t(floored(plan, 'plan.row.code'), { w: widthText(plan.w) }), why(t(each(plan, plan.screen ? 'plan.row.screen_to' : 'plan.row.code_to'), { to: toText(true) }))]; | |
| } | |
| // ---- behind "+": how it is worked out ------------------------------------------------------------------------------ | |
| const pctNear5 = (x) => 5 * Math.round(clean(20 * x)); // a reach gain to the nearest 5%: a named rounding | |
| function gainText(gains) { | |
| const v = Object.values(gains).map(pctNear5).sort((a, b) => a - b); | |
| return v[0] === v[v.length - 1] ? t('plan.how.about_pct', { p: v[0] }) : t('plan.how.pct_range', { lo: v[0], hi: v[v.length - 1] }); | |
| } | |
| /** The sentences behind "+", for a planned picture: a formula's inputs and the camera line in metric units alone, | |
| * every result with its inch twin. */ | |
| export function howLines(plan) { | |
| if (!plan || plan.mode === 'cant') return []; | |
| const { v, n, need, d } = plan; | |
| const px = S.camera.pixels; | |
| const measured = S.needs.map((m) => m.version); | |
| let from = ''; | |
| if (need.floor) from = t('plan.how.from_floor', { at: need.version }); | |
| else if (v === 1 && need.version !== 1) from = t('plan.how.from_one', { at: need.version }); | |
| else if (!measured.includes(v)) from = t('plan.how.from_next', { v, at: need.version }); | |
| const width = plan.mode === 'width'; | |
| const near = tooNear(plan); | |
| const small = width && near && plan.at < 1; // then the working shows the small numbers themselves, not "under 1 mm" | |
| const at = small ? t('plan.unit.mm', { n: numText(10 * plan.at) }) : cmText(plan.at ?? 0); | |
| const formula = width | |
| ? t('plan.how.formula_far', { px, n, p: need.ppm.toFixed(1), w: at, d: flatFar(plan) }) | |
| : t('plan.how.formula', { px, n, p: need.ppm.toFixed(1), d: placeCm(d), w: widthText(plan.w0) }); | |
| const share = `${Math.round(100 * plan.share)}%`; | |
| const dc = width ? Math.max(plan.far, nearest()) : d; // the distance the answer is for | |
| const nearCm = plan.shape?.cant ? 0 : shapeNear(plan, (codeWidth(plan) * (n + 2 * S.margin_squares)) / n); | |
| const b = S.bigger; | |
| return [ | |
| t(plan.estimated ? 'plan.how.version_estimate' : 'plan.how.version', { v, n }), | |
| t('plan.how.need', { p: need.ppm.toFixed(1), names: names(need.scanners), pass: S.rule.pass, of: S.rule.of }), | |
| from, | |
| t('plan.how.step'), | |
| formula, | |
| width ? t(each(plan, 'plan.how.share_width'), { share, w: small ? at : widthText(plan.at), W: plan.typed }) | |
| : fileFirst(plan) ? t(each(plan, 'plan.how.share'), { share }) : '', | |
| ...shapeHow(plan, plan.shape?.cant ? '' : codeText(plan), plan.far > 0 ? distText(plan.far) : ''), | |
| t('plan.how.camera', { | |
| px, lo: S.camera.lens_mm[0], hi: S.camera.lens_mm[1], fw: S.camera.frame[0], fh: S.camera.frame[1], | |
| ppmm: px / (10 * dc) >= 0.1 ? (px / (10 * dc)).toFixed(1) : String(Number((px / (10 * dc)).toPrecision(2))), | |
| d: width ? distCmText(dc) : placeCm(dc), | |
| }), | |
| t('plan.how.scene', { ...S.scene, contrast: `${Math.round(100 * S.scene.contrast)}%` }), | |
| t('plan.how.bigger', { | |
| v: b.version, names: names(Object.keys(b.gain.everyday)), frame: b.frame, everyday: gainText(b.gain.everyday), | |
| good: gainText(b.gain.good), sw: S.smaller.frame[0], sh: S.smaller.frame[1], small: `${Math.round(100 * S.smaller.reach)}%`, | |
| }), | |
| // the near limit, rounded up as a least distance; left out where it can't be kept apart from the distance, in either unit | |
| nearCm <= 0 || (width && near) || passes(nearCm, dc) ? '' : t('plan.how.near', { near: distUpText(nearCm) }), | |
| ].filter(Boolean); | |
| } | |
| /** The farthest distance facing the phone, flat, as the formula behind "+" gives it before any shape. */ | |
| const flatFar = (plan) => { const f = (plan.at * S.camera.pixels) / (plan.n * plan.need.ppm); return clean(f) < nearest() ? t('plan.unit.cm', { n: numText(f) }) : distText(f); }; | |
| /** The width version v needs at the plan's distance with its shape (made.js's smaller version, the record's fill); | |
| * null when the shape can't be planned for it. */ | |
| export function planWidth(v, plan) { | |
| const s = shapeFor(plan.shape, squares(v), needFor(v).ppm, plan.d); | |
| return s?.cant ? null : needWidth(v, plan.d) * (s?.k ?? 1); | |
| } | |
| /** The table behind "+": each measured version's need and its code widths at the four distances, each width as | |
| * [metric, inches], which plan_view.js shows on two lines. */ | |
| export function tableRows() { | |
| return S.needs.map((m) => ({ | |
| version: m.version, label: t('plan.how.table_row', { v: m.version, n: squares(m.version) }), need: m.ppm.toFixed(1), | |
| widths: S.distances.map((dd) => { const w = needWidth(m.version, dd.cm); return [cmText(w), inchText(w)]; }), | |
| })); | |
| } | |
| export const tableHeads = () => S.distances.map((dd) => [t(`plan.distance.${dd.key}.label`, { d: distCmText(dd.cm) }), feetText(dd.cm)]); | |