Spaces:
Sleeping
Sleeping
File size: 8,145 Bytes
de7cd1e 378f397 de7cd1e 378f397 de7cd1e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | // What a file's own record adds under its as-made facts (made.js), for a picture marked The original graphic, once
// the record checked out (record.js): its kind of art, the square check, the fill, capitals, dots and repeats, and the
// maker's own name for a control after the advice it serves, as plain text in <bdi>; or the one line saying why a
// record is set aside. The words come from copy.json, every number from record.json, sizes.json or the reading.
import { BASE_FRAME, count, el, pctText } from './util.js';
import { t } from './copy.js';
import { needFor, planFor, planWidth, widthText } from './plan.js';
import { smallestVersion } from './capacity.js';
import { ppmText } from './checks.js';
import { ascii, recordSettings, verifiedRecord } from './record.js';
const ASIDE = ['newer', 'unreadable', 'saved', 'unchecked', 'mismatch']; // a record set aside: one line saying why
const NAMED = ['margin', 'contrast', 'error_correction', 'size', 'version']; // the names a record may give
const SERVES = { ec_used: 'error_correction', smaller: 'version', fill: 'version' }; // the control a line's advice names
const FILL = '{control}'; // where the name goes in made.record.control
/** A control's name as the page shows it: white space made spaces; control, format (bidirectional among them) and
* lone surrogate characters removed; runs of spaces made one, and the ends trimmed. '' when that leaves nothing, or
* more than record.json's max_code_points: the advice then stays general. Never anything but text. */
export function cleanName(s) {
const max = recordSettings()?.names.max_code_points;
if (typeof s !== 'string' || !max) return '';
const name = s.replace(/\s/gu, ' ').replace(/[\p{Cc}\p{Cf}\p{Cs}]/gu, '').replace(/ {2,}/g, ' ').trim();
return name && [...name].length <= max ? name : '';
}
/** The record's control names, by control, each cleaned; a name that isn't text, or cleans to nothing, is left out. */
function namesOf(rec) {
const c = rec.controls;
const out = {};
if (c === null || typeof c !== 'object' || Array.isArray(c)) return out;
for (const key of NAMED) {
const name = Object.hasOwn(c, key) ? cleanName(c[key]) : '';
if (name) out[key] = name;
}
return out;
}
/** The line naming a control, split round the name, which goes in as the text of a <bdi>. */
function controlParts(name) {
const [before, after = ''] = t('made.record.control', { control: FILL }).split(FILL);
return [before, el('bdi', { text: name }), after].filter((x) => x !== '');
}
/** made.js: the as-made block's items, [{ text, control? }]. own: the code's facts as [kind, text] ('facts', 'ec' or
* 'ec_used', 'smaller'); u: made.js ecUse(r). With a record that checked out: those lines, each followed by the
* name of the control its advice serves (each control once, after the first line it serves), then the record's own
* lines. With a record set aside: those lines, then one saying why. Otherwise those lines as they are. */
export function recordItems(p, plan, own, u) {
const rec = verifiedRecord(p);
const names = rec ? namesOf(rec) : {};
const used = new Set();
const items = [];
const add = (text, serves) => {
if (!text) return;
items.push({ text });
const name = names[serves];
if (!name || used.has(serves)) return;
used.add(serves);
items.push({ text: t('made.record.control', { control: name }), control: name });
};
for (const [kind, text] of own) add(text, SERVES[kind]);
if (rec) for (const [kind, text] of recordLines(p, plan, rec, u)) add(text, SERVES[kind]);
else if (ASIDE.includes(p.made?.state)) add(t(`made.record.aside.${p.made.state}`));
return items;
}
/** The record's lines, in order: its art (with how many squares read the wrong way, for art drawn in the spare room
* of a code that uses some of its error correction), the square check, the fill, capitals, dots and repeats. */
function recordLines(p, plan, rec, u) {
const r = p.result;
const sq = p.made.squares;
const art = rec.art === 'none' ? '' : t(`made.record.art.${rec.art}`);
const yet = rec.art === 'spare-data' && typeof u === 'number' && u > 0 && sq.wrong >= 1 ? t('made.record.yet', { k: count(sq.wrong) }) : '';
const dots = rec.code.dots;
const size = dots === 'varies' ? 'varies' : dots >= recordSettings().dots.small_below ? 'large' : 'small';
return [
['art', [art, yet].filter(Boolean).join(' ')],
...squareLines(sq).map((s) => ['squares', s]),
['fill', fillLine(r, plan, rec)],
['capitals', rec.content.capitals === 'whole-link' ? t('made.record.capitals.whole-link') : ''],
['dots', dots == null ? '' : t(`made.record.dots.${size}`)],
['repeats', rec.code.grid ? t('made.record.repeats', { p: ppmText(r.checks?.ppm?.native) }) : ''],
];
}
/** The square check: a mirrored code says so first (and then counts only squares that read the wrong way); else all
* the squares read as made, or how many didn't, and how many of those are in the corner patterns. */
function squareLines(sq) {
const vars = { N: count(sq.N), k: count(sq.wrong), pct: pctText(sq.wrong / sq.N), f: count(sq.f) };
const tally = sq.wrong === 0 ? t('made.squares.all', vars) : t(sq.f > 0 ? 'made.squares.wrong' : 'made.squares.wrong_clear', vars);
return sq.mirrored ? [t('made.squares.mirrored'), sq.wrong > 0 ? tally : ''] : [tally];
}
/** The fill: the link alone, whole in an ASCII text the page holds, fits a smaller version at the same error
* correction (capacity.js), with that version's width at the chosen distance; with a printed width typed, the
* version alone, since that answer is a distance. '' otherwise. */
function fillLine(r, plan, rec) {
const lb = rec.content.link_bytes;
const text = r.content?.text;
if (lb == null || !ascii(text) || lb > text.length) return '';
const lv = smallestVersion(text.slice(0, lb), r.ec_level);
if (lv == null || lv >= plan.v) return '';
const lw = plan.mode === 'width' || plan.shape?.cant ? null : planWidth(lv, plan); // with its shape (tilt)
if (lw == null) return t('made.record.fill_width', { lv, v: plan.v });
return t(needFor(lv).floor ? 'made.record.fill_floor' : 'made.record.fill', { lv, lw: widthText(lw), v: plan.v });
}
/** made.js, behind "+": where the dots line comes from, then how the squares are counted and how many of the wrong
* ones are in the other fixed patterns, when some read the wrong way. */
export function recordMore(p) {
const rec = verifiedRecord(p);
if (!rec) return [];
const sq = p.made.squares;
return [
rec.code.dots == null ? '' : t('made.record.more.dots'),
sq.wrong > 0 ? t('made.squares.more.how') : '',
sq.wrong > 0 ? t(sq.g > 1 ? 'made.squares.more.fixed' : sq.g === 1 ? 'made.squares.more.fixed_one' : 'made.squares.more.fixed_none', { g: count(sq.g) }) : '',
].filter(Boolean);
}
/** made.js: an item as the block shows it, a <p> of text, with a control's name as the text of a <bdi>. */
export const itemEl = (item) => (item.control ? el('p', {}, controlParts(item.control)) : el('p', { text: item.text }));
/** yours.js: under a Borderline or Problem check's advice, the file's own name for the control it serves, for a
* picture marked The original graphic whose record checked out: the margin's for the margin check, the contrast's
* for low contrast (not for a light-on-dark code alone), the pixel size's for too few pixels per square in a file read
* at its own size (where the advice is a bigger file), and the version's for a dense code. */
export function checkControl(p, key) {
const rec = verifiedRecord(p);
const plan = rec && p.kind === 'graphic' ? planFor(p) : null;
if (!plan || plan.mode === 'cant' || plan.estimated) return [];
const r = p.result;
const why = r.checks?.[key]?.why || [];
const serves = {
quiet: 'margin', contrast: why.includes('contrast') ? 'contrast' : '', ppm: Math.max(...r.size) <= BASE_FRAME ? 'size' : '',
size: why.includes('dense') ? 'version' : '',
}[key];
const name = serves ? namesOf(rec)[serves] : '';
return name ? [el('span', { class: 'do file-control' }, controlParts(name))] : [];
}
|