devmandan's picture
Claude Opus 5.5
Slants and cans, what a phone sees, a test sheet, SVG files and more
378f397 verified
Raw History Blame Contribute Delete
9.27 kB
// A file's code as made, for a picture marked The original graphic, under its width's lines: its facts (version,
// error correction, margin), how much of its error correction it already uses, whether its content fits a smaller
// version, and behind "+", where these come from: its squares, its mask, where zxing-cpp read it and the QR
// standard's capacities. From the reading, the capacities (capacity.js) and the plan (plan.js); the words from
// copy.json. Nothing is sent or stored.
import { count, el, pctText } from './util.js';
import { t } from './copy.js';
import { choice, codeText, codeWidth, framePpm, needFor, numText, planWidth, widthText } from './plan.js';
import { cmText } from './units.js';
import { smallerVersion } from './capacity.js';
import { itemEl, recordItems, recordMore } from './record_view.js';
const EC = ['L', 'M', 'Q', 'H']; // the four error corrections, as zxing-cpp names them
const FULL = 4; // squares: the margin check's cap, the margin the QR standard asks for
/** As for the width: a picture marked The original graphic whose width is planned from a scanner's version (not the
* checks' estimate: these are the facts zxing-cpp read). */
const shows = (p, plan) => p.kind === 'graphic' && plan != null && plan.mode !== 'cant' && !plan.estimated;
/** The frame zxing-cpp read the content's error correction on ([width, height]), and the code's pixels per square
* there; null when that isn't known. */
function readOn(r) {
const i = r.order?.indexOf(r.uec_frame);
const frame = i >= 0 ? r.frames?.[i] : null;
const ppm = framePpm(r, frame?.[0]);
return ppm == null ? null : { frame, ppm };
}
/** The code's pixels per square in the frame the checks measure, the largest (frames[0]: the server's _checks measures
* its margin there too); null when that isn't known. */
const checksPpm = (r) => framePpm(r, r.frames?.[0]?.[0]);
/** The pixels per square from which zxing-cpp's figure is judged: uec_min_ppm, or uec_min_ppm_v1 for version 1, whose
* plain codes read some of it as used at about 2.4. */
const uecMin = (r) => choice(r.checks?.version === 1 ? 'uec_min_ppm_v1' : 'uec_min_ppm');
/** The share of its error correction a code uses, from zxing-cpp's unused figure (0 to 1). Under uecMin pixels per
* square in the frame it read the code on, where plain codes read with some used too, it isn't judged: 'small'
* when the file's own pixels per square are under it too; 'shrunk' when they aren't and the file is bigger than the
* largest frame (frames[0] narrower than the file): files are read at no more than 1,920 pixels on their long side,
* so a crop that holds the code and its margin is read at its own; 'unjudged' otherwise (a crop can't help a file
* read at its own size) and when zxing-cpp didn't read it. null from a server that sends no figure. pair.js compares
* files by it. */
export function ecUse(r) {
if (!r || !('uec' in r)) return null;
const at = r.uec == null ? null : readOn(r);
if (!at) return 'unjudged';
const min = uecMin(r);
if (at.ppm < min) {
if (r.checks.ppm.native < min) return 'small';
return r.frames[0]?.[0] < r.size[0] ? 'shrunk' : 'unjudged';
}
return 1 - r.uec;
}
/** The one line on the error correction a file uses: all of it, how much, none, or why that can't be judged
* ('shrunk' names the frame read). */
function ecLine(r) {
const u = ecUse(r);
if (u == null) return '';
if (typeof u === 'number') return u >= 1 ? t('made.ec.all') : u > 0 ? t('made.ec.used', { u: pctText(u) }) : t('made.ec.none');
const frame = readOn(r)?.frame;
return t(`made.ec.${u}`, frame ? { fw: count(frame[0]), fh: count(frame[1]) } : {});
}
/** The margin as the margin check measured it, up to its cap; '' when it wasn't measured, was measured at under
* margin_min_ppm pixels per square (sizes.json), or was capped there (quiet.capped: under 4 pixels per square, the
* check reads a margin the file's edge ends from 3.5 squares out as 4), so margin_min_ppm must stay at or above
* that 4 (the server's QZ_EDGE_PPM). On plain files with margins of 0, 1 and 4 squares the check read every one
* exactly at 4 and 8 pixels per square, and all but 4 of 4,968 from 2 to 3.5, where a 1-square margin can read
* 0.75; margins of 2 to 3.5 squares on nearest-scaled files under 4 pixels per square read a quarter square short
* on 254 of 576. In a file, a margin the file's edge ends is the file's own. */
function marginClause(r) {
const q = r.checks.quiet;
const ppm = checksPpm(r);
if (q?.modules == null || q.capped || ppm == null || !(ppm >= choice('margin_min_ppm'))) return '';
if (q.under) return t('made.margin.under', { m: numText(q.modules) }); // known only to be less
if (q.modules >= FULL) return t('made.margin.full', { m: FULL });
return q.modules === 0 ? t('made.margin.none') : t('made.margin.some', { m: numText(q.modules) });
}
/** "As made: version 8 at error correction L, with at least a 4-square margin." */
function factsLine(r, plan) {
const ec = EC.includes(r.ec_level) ? r.ec_level : null;
const margin = marginClause(r);
return t(`made.facts.${ec ? 'ec' : 'plain'}${margin ? '_margin' : ''}`, { v: plan.v, ec, margin });
}
/** The smaller version the whole content fits, {v2}, with its need at the same distance, {w2}, and the code's width
* as the answer gives it, {w}; null when the two widths round the same in cm, with a printed width typed (that
* answer is a distance), or for a code that uses some of its error correction (its styling on a smaller version is
* another design, which nothing has tested). */
function smaller(r, plan) {
const u = ecUse(r);
const v2 = plan.mode === 'width' || plan.shape?.cant || (typeof u === 'number' && u > 0) ? null : smallerVersion(r);
if (v2 == null) return null;
if (cmText(planWidth(v2, plan)) === cmText(codeWidth(plan))) return null; // the inch figures don't decide it
return { v2, w2: widthText(planWidth(v2, plan)), w: codeText(plan) }; // w as the answer gives it, with its shape
}
/** "Its content fits version 2, which needs printing at least 2.7 cm (1⅛ in) wide instead of 3.6 cm (1⅜ in)." */
function smallerLine(r, plan) {
const s = smaller(r, plan);
return s ? t(needFor(s.v2).floor ? 'made.smaller_floor' : 'made.smaller', s) : '';
}
/** The as-made lines, for The original graphic: the facts, the error correction used and a smaller version, each
* only with something to say; [] for any other picture. plan_view.js shows them, and says them when what the
* picture shows changes. */
export function madeLines(p, plan) {
return madeItems(p, plan).map((x) => x.text);
}
/** The same lines for the block, as items: with what a file's verified record adds (record_view.js), and a
* control's name after a line whose advice it serves; [] for any other picture. */
function madeItems(p, plan) {
if (!shows(p, plan)) return [];
const r = p.result;
const u = ecUse(r);
const ec = typeof u === 'number' && u > 0 ? 'ec_used' : 'ec'; // some or all used: its advice has a control
return recordItems(p, plan, [['facts', factsLine(r, plan)], [ec, ecLine(r)], ['smaller', smallerLine(r, plan)]], u);
}
/** Behind "+", where these come from: the squares across, the mask, the error correction's figure with the frame
* zxing-cpp read it on, and the capacities behind a smaller version. [] when zxing-cpp gave no fact (a mask, or a
* figure with its frame): the squares across alone repeat "How this is worked out". */
function moreLines(r, plan) {
const mask = Number.isInteger(r.mask) && r.mask >= 0 && r.mask <= 7;
const at = r.uec == null ? null : readOn(r);
if (!mask && !at) return [];
const own = at && at.frame[0] === r.size[0] && at.frame[1] === r.size[1];
const s = smaller(r, plan);
return [
t('plan.how.version', { v: plan.v, n: plan.n }),
mask ? t('made.more.mask', { mask: r.mask }) : '',
at ? t(own ? 'made.more.uec_own' : 'made.more.uec', { left: pctText(r.uec), fw: count(at.frame[0]), fh: count(at.frame[1]) }) : '',
at ? t('made.more.rule', { min: uecMin(r) }) : '',
s ? t('made.more.capacity', { v2: s.v2, ec: r.ec_level }) : '',
].filter(Boolean);
}
/** The section's as-made block, hidden until refreshMade fills it: its lines, then "+", made once per section so an
* open "+" stays open as the distance or width changes. */
export const madeHead = () => el('div', { class: 'made', hidden: '' }, [
el('div', { class: 'made-lines' }),
el('details', { class: 'more made-more' }, [el('summary', { text: t('made.more_summary') }), el('div', { class: 'more-body method' })]),
]);
/** The as-made block in place, with the plan's other lines (plan_view.js refreshPlan); "+" only with one of
* zxing-cpp's facts to give. */
export function refreshMade(box, p, plan) {
const node = box.querySelector('.made');
if (!node) return;
const items = madeItems(p, plan);
const more = items.length ? [...moreLines(p.result, plan), ...recordMore(p)] : [];
node.hidden = !items.length;
node.querySelector('.made-lines').replaceChildren(...items.map(itemEl));
node.querySelector('.made-more').hidden = !more.length;
node.querySelector('.made-more .more-body').replaceChildren(...more.map((s) => el('p', { text: s })));
}