@uselazuli/cli 0.0.0-stage → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +98 -0
- package/LICENSE +21 -0
- package/README.md +202 -2
- package/agents/lazuli-design-reviewer.md +30 -0
- package/agents/lazuli-first-look.md +22 -0
- package/agents/lazuli-task-tester.md +25 -0
- package/bin/lazuli.mjs +699 -0
- package/package.json +11 -4
- package/rules/accessibility/HYPOTHESES.md +8 -0
- package/rules/accessibility/_area.yaml +7 -0
- package/rules/accessibility/operable.yaml +100 -0
- package/rules/accessibility/perceivable.yaml +153 -0
- package/rules/accessibility/robust.yaml +24 -0
- package/rules/accessibility/understandable.yaml +72 -0
- package/rules/color/HYPOTHESES.md +27 -0
- package/rules/color/_area.yaml +10 -0
- package/rules/color/adapt.yaml +183 -0
- package/rules/color/create.yaml +259 -0
- package/rules/color/review.yaml +215 -0
- package/rules/color/signal.yaml +196 -0
- package/rules/content/HYPOTHESES.md +18 -0
- package/rules/content/_area.yaml +8 -0
- package/rules/content/review.yaml +347 -0
- package/rules/content/voice.yaml +144 -0
- package/rules/content/writing.yaml +202 -0
- package/rules/data/HYPOTHESES.md +11 -0
- package/rules/data/_area.yaml +7 -0
- package/rules/data/charts.yaml +189 -0
- package/rules/data/dashboards.yaml +141 -0
- package/rules/data/review.yaml +159 -0
- package/rules/graphic/HYPOTHESES.md +17 -0
- package/rules/graphic/_area.yaml +8 -0
- package/rules/graphic/identity.yaml +141 -0
- package/rules/graphic/pieces.yaml +164 -0
- package/rules/graphic/review.yaml +242 -0
- package/rules/graphic/symbol.yaml +141 -0
- package/rules/motion/HYPOTHESES.md +19 -0
- package/rules/motion/_area.yaml +7 -0
- package/rules/motion/micro.yaml +143 -0
- package/rules/motion/review.yaml +347 -0
- package/rules/motion/transitions.yaml +169 -0
- package/rules/typography/HYPOTHESES.md +12 -0
- package/rules/typography/_area.yaml +8 -0
- package/rules/typography/adapt.yaml +45 -0
- package/rules/typography/create.yaml +126 -0
- package/rules/typography/review.yaml +127 -0
- package/rules/ui/HYPOTHESES.md +23 -0
- package/rules/ui/_area.yaml +9 -0
- package/rules/ui/compose.yaml +219 -0
- package/rules/ui/grid.yaml +75 -0
- package/rules/ui/review.yaml +399 -0
- package/rules/ui/states.yaml +257 -0
- package/rules/ux/HYPOTHESES.md +13 -0
- package/rules/ux/_area.yaml +8 -0
- package/rules/ux/flows.yaml +164 -0
- package/rules/ux/forms.yaml +142 -0
- package/rules/ux/review.yaml +186 -0
- package/skills/lazuli/SKILL.md +180 -0
- package/skills/lazuli/procedures/create.md +29 -0
- package/skills/lazuli/procedures/explain-decisions.md +22 -0
- package/skills/lazuli/procedures/improve.md +30 -0
- package/skills/lazuli/procedures/remember-this.md +41 -0
- package/skills/lazuli/procedures/review-design.md +35 -0
- package/skills/lazuli/procedures/show-options.md +32 -0
- package/skills/lazuli-accessibility-check/SKILL.md +68 -0
- package/skills/lazuli-color-context-and-culture/SKILL.md +53 -0
- package/skills/lazuli-color-context-and-culture/references/sectors.md +26 -0
- package/skills/lazuli-color-palette-by-intent/SKILL.md +54 -0
- package/skills/lazuli-color-palette-by-intent/references/intent-chords.md +51 -0
- package/skills/lazuli-color-screen-review/SKILL.md +51 -0
- package/skills/lazuli-color-states-and-signals/SKILL.md +53 -0
- package/skills/lazuli-content-interface-writing/SKILL.md +53 -0
- package/skills/lazuli-content-voice/SKILL.md +39 -0
- package/skills/lazuli-data-charts-and-dashboards/SKILL.md +59 -0
- package/skills/lazuli-graphic-derived-pieces/SKILL.md +40 -0
- package/skills/lazuli-graphic-derived-pieces/references/piece-template.html +24 -0
- package/skills/lazuli-graphic-identity/SKILL.md +43 -0
- package/skills/lazuli-motion-micro-and-transitions/SKILL.md +49 -0
- package/skills/lazuli-typography-choose-and-pair/SKILL.md +50 -0
- package/skills/lazuli-typography-screen-review/SKILL.md +51 -0
- package/skills/lazuli-ui-composition/SKILL.md +42 -0
- package/skills/lazuli-ui-composition/references/option-template.html +34 -0
- package/skills/lazuli-ui-layout-review/SKILL.md +47 -0
- package/skills/lazuli-ui-states-and-system/SKILL.md +54 -0
- package/skills/lazuli-ux-flows-and-forms/SKILL.md +51 -0
- package/skills/lazuli-ux-task-test/SKILL.md +44 -0
- package/src/browser.mjs +161 -0
- package/src/commands/capture.mjs +313 -0
- package/src/commands/catalog.mjs +37 -0
- package/src/commands/check-design.mjs +428 -0
- package/src/commands/check-goals.mjs +57 -0
- package/src/commands/compare-designs.mjs +97 -0
- package/src/commands/draft-memo.mjs +161 -0
- package/src/commands/hooks.mjs +343 -0
- package/src/commands/install-hooks.mjs +120 -0
- package/src/commands/install.mjs +132 -0
- package/src/commands/list-rules.mjs +49 -0
- package/src/commands/map-page.mjs +594 -0
- package/src/commands/record-answers.mjs +79 -0
- package/src/commands/record-log.mjs +296 -0
- package/src/commands/record-task.mjs +250 -0
- package/src/commands/render-options.mjs +164 -0
- package/src/commands/start-project.mjs +32 -0
- package/src/measure/a11y.mjs +383 -0
- package/src/measure/asset.mjs +206 -0
- package/src/measure/at-rest.mjs +49 -0
- package/src/measure/cases.mjs +549 -0
- package/src/measure/chart-read.mjs +401 -0
- package/src/measure/chart.mjs +264 -0
- package/src/measure/coherence.mjs +145 -0
- package/src/measure/color.mjs +79 -0
- package/src/measure/content.mjs +82 -0
- package/src/measure/cvd.mjs +24 -0
- package/src/measure/extreme.mjs +241 -0
- package/src/measure/forms.mjs +150 -0
- package/src/measure/in-page/a11y.js +489 -0
- package/src/measure/in-page/color.js +346 -0
- package/src/measure/in-page/composition.js +1496 -0
- package/src/measure/in-page/contrast.js +15 -0
- package/src/measure/in-page/cvd.js +173 -0
- package/src/measure/in-page/extreme.js +355 -0
- package/src/measure/in-page/frames.js +781 -0
- package/src/measure/in-page/look.js +28 -0
- package/src/measure/in-page/motion-probe.js +169 -0
- package/src/measure/in-page/style.js +107 -0
- package/src/measure/in-page/svg-strokes.js +112 -0
- package/src/measure/in-page/svg-uses.js +50 -0
- package/src/measure/in-page/symbol.js +123 -0
- package/src/measure/in-page/type.js +266 -0
- package/src/measure/in-page/weight.js +207 -0
- package/src/measure/layout.mjs +91 -0
- package/src/measure/motion.mjs +508 -0
- package/src/measure/piece.mjs +195 -0
- package/src/measure/registry.mjs +252 -0
- package/src/measure/rhythm.mjs +81 -0
- package/src/measure/signature.mjs +65 -0
- package/src/measure/states.mjs +240 -0
- package/src/measure/style.mjs +13 -0
- package/src/measure/symbol.mjs +268 -0
- package/src/measure/terms.mjs +153 -0
- package/src/measure/tiles.mjs +20 -0
- package/src/measure/timeline.mjs +621 -0
- package/src/measure/type.mjs +167 -0
- package/src/measure/weight.mjs +18 -0
- package/src/private-strings.mjs +81 -0
- package/src/project/blind.mjs +70 -0
- package/src/project/edits.mjs +154 -0
- package/src/project/gate.mjs +159 -0
- package/src/project/lazuli-md.mjs +497 -0
- package/src/project/log.mjs +329 -0
- package/src/project/match.mjs +112 -0
- package/src/project/records.mjs +380 -0
- package/src/project/tasks.mjs +153 -0
- package/src/report/evaluate.mjs +27 -0
- package/src/report/evaluators/a11y.mjs +206 -0
- package/src/report/evaluators/chart.mjs +151 -0
- package/src/report/evaluators/coherence.mjs +103 -0
- package/src/report/evaluators/color.mjs +86 -0
- package/src/report/evaluators/content.mjs +112 -0
- package/src/report/evaluators/extreme.mjs +53 -0
- package/src/report/evaluators/forms.mjs +116 -0
- package/src/report/evaluators/layout.mjs +94 -0
- package/src/report/evaluators/motion.mjs +273 -0
- package/src/report/evaluators/piece.mjs +69 -0
- package/src/report/evaluators/rhythm.mjs +99 -0
- package/src/report/evaluators/states.mjs +39 -0
- package/src/report/evaluators/style.mjs +35 -0
- package/src/report/evaluators/symbol.mjs +110 -0
- package/src/report/evaluators/terms.mjs +49 -0
- package/src/report/evaluators/type.mjs +163 -0
- package/src/report/evaluators/weight.mjs +51 -0
- package/src/report/evidence.mjs +4 -0
- package/src/report/finding.mjs +11 -0
- package/src/report/format.mjs +215 -0
- package/src/rules/hypotheses.mjs +28 -0
- package/src/rules/load.mjs +256 -0
- package/src/rules/public-text.mjs +56 -0
- package/src/rules/schema.mjs +212 -0
- package/templates/LAZULI.md +86 -0
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
// Chart geometry: pure functions, no browser. The composition reader `read: "chart"`
|
|
2
|
+
// (in-page/composition.js) returns RawChart data; readChart turns one into a chart reading: series by paint,
|
|
3
|
+
// bars and their shared base, the value axis fitted from numeric tick labels, legend entries and direct labels.
|
|
4
|
+
// Every constant below is a definition or a measurement tolerance fixed before any chart was
|
|
5
|
+
// measured, never a rule cutoff and never edited after a reading. Rule cutoffs live in rules/data only.
|
|
6
|
+
|
|
7
|
+
// An svg or canvas smaller than this on either side is not a chart (icons, sparklines in table cells).
|
|
8
|
+
export const MIN_CHART_SIDE_PX = 120;
|
|
9
|
+
// An svg with no chart word is a chart only with at least this many data marks (and 2 numeric texts).
|
|
10
|
+
export const MIN_MARKS = 3;
|
|
11
|
+
// The numeric texts an svg with no chart word needs to be a chart.
|
|
12
|
+
const MIN_NUMERIC_TEXTS = 2;
|
|
13
|
+
// A chart word, matched against whole tokens of the role, aria-roledescription, aria-label,
|
|
14
|
+
// class and id of the element or its ancestors up to CONTAINER_LEVELS: the tokens are the lowercase value
|
|
15
|
+
// split on [^a-z0-9áà]+. A token equal to chart, graph, plot or grafico (singular or plural, with or without
|
|
16
|
+
// the accent), or a chart library name as a whole token (`vega-embed` splits into the
|
|
17
|
+
// tokens vega and embed and matches; `vegan`, `vegano` and `vegas` do not). `graphic`, `typography` or
|
|
18
|
+
// `paragraph` do not match.
|
|
19
|
+
export const CHART_WORDS = /^(?:charts?|graphs?|plots?|gr[aá]ficos?|recharts|highcharts|apexcharts|echarts|chartjs|plotly|vega)$/;
|
|
20
|
+
export const CONTAINER_LEVELS = 3;
|
|
21
|
+
// A legend (class, id or aria-label).
|
|
22
|
+
export const LEGEND_WORD = /legend/i;
|
|
23
|
+
// An axis, grid or tick shape (class or id on the shape or an ancestor inside the svg): not a data mark.
|
|
24
|
+
export const AXIS_WORDS = /axis|grid|tick/i;
|
|
25
|
+
// The composition tolerance: two edges within 2 px are one edge.
|
|
26
|
+
export const ALIGN_PX = 2;
|
|
27
|
+
// A legend swatch drawn by an HTML element is at most this many px on both sides.
|
|
28
|
+
export const SWATCH_PX = 24;
|
|
29
|
+
// A text this close to a mark labels it.
|
|
30
|
+
export const LABEL_GAP_PX = 8;
|
|
31
|
+
export const MIN_BARS = 2;
|
|
32
|
+
// Measurement tolerances: a base value within 1 percent of the tick range is zero; ticks are linear when
|
|
33
|
+
// every tick lies within 1 percent of the tick range of the fitted line.
|
|
34
|
+
export const ZERO_SHARE = 0.01;
|
|
35
|
+
export const LINEAR_SHARE = 0.01;
|
|
36
|
+
// Trailing number suffixes and their scale; currency signs are stripped.
|
|
37
|
+
export const NUMBER_SUFFIXES = { "%": 1, k: 1e3, K: 1e3, mil: 1e3, M: 1e6, mi: 1e6, B: 1e9, bi: 1e9 };
|
|
38
|
+
const CURRENCY = /^(?:R\$|\$|€|£)/;
|
|
39
|
+
// Sampling limits, noted when they cut.
|
|
40
|
+
export const MAX_CHARTS = 12;
|
|
41
|
+
export const MAX_MARKS = 2000;
|
|
42
|
+
export const MAX_TEXTS = 500;
|
|
43
|
+
export const MAX_SERIES = 20;
|
|
44
|
+
export const MAX_LISTED = 10;
|
|
45
|
+
|
|
46
|
+
// The options of the composition reader, regexes passed as { source, flags }.
|
|
47
|
+
const rx = (r) => ({ source: r.source, flags: r.flags });
|
|
48
|
+
export const READER_OPTS = {
|
|
49
|
+
read: "chart",
|
|
50
|
+
keepPinned: true,
|
|
51
|
+
minSide: MIN_CHART_SIDE_PX,
|
|
52
|
+
chartWords: rx(CHART_WORDS),
|
|
53
|
+
legendWord: rx(LEGEND_WORD),
|
|
54
|
+
axisWords: rx(AXIS_WORDS),
|
|
55
|
+
containerLevels: CONTAINER_LEVELS,
|
|
56
|
+
maxCharts: MAX_CHARTS,
|
|
57
|
+
maxMarks: MAX_MARKS,
|
|
58
|
+
maxTexts: MAX_TEXTS,
|
|
59
|
+
swatchPx: SWATCH_PX,
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const plural = (n, one, many) => (n === 1 ? one : many);
|
|
63
|
+
|
|
64
|
+
// ---- numbers ----
|
|
65
|
+
const SUFFIX = new RegExp(`\\s*(${Object.keys(NUMBER_SUFFIXES).sort((a, b) => b.length - a.length).map((s) => s.replace(/[%$]/g, "\\$&")).join("|")})$`);
|
|
66
|
+
// Digits with group separators of exactly three digits (one separator kind) and an optional decimal part
|
|
67
|
+
// with the other separator; or plain digits with one decimal separator.
|
|
68
|
+
const GROUPED = /^(\d{1,3})((?:([.,])\d{3})(?:\3\d{3})*)(?:([.,])(\d+))?$/;
|
|
69
|
+
const PLAIN = /^(\d+)(?:[.,](\d+))?$/;
|
|
70
|
+
|
|
71
|
+
// A tick label as a number: optional sign (- or U+2212), currency signs stripped, one decimal separator,
|
|
72
|
+
// group separators of exactly three digits (1.000 and 1,000 are 1000; 2,5 and 2.5 are 2.5; 1.000,5 is
|
|
73
|
+
// 1000.5), a trailing NUMBER_SUFFIXES entry scales. No-break and thin spaces are spaces. Anything else
|
|
74
|
+
// (a date, a word) is null. Known limit: 0,125 and 1.000 always read as thousands.
|
|
75
|
+
export function parseNumber(text) {
|
|
76
|
+
let s = String(text ?? "").replace(/[ ]/g, " ").trim();
|
|
77
|
+
let sign = 1;
|
|
78
|
+
const takeSign = () => {
|
|
79
|
+
if (/^[-−+]/.test(s)) {
|
|
80
|
+
if (s[0] !== "+") sign = -sign;
|
|
81
|
+
s = s.slice(1).trim();
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
takeSign();
|
|
85
|
+
if (CURRENCY.test(s)) {
|
|
86
|
+
s = s.replace(CURRENCY, "").trim();
|
|
87
|
+
takeSign();
|
|
88
|
+
}
|
|
89
|
+
let scale = 1;
|
|
90
|
+
const suffix = SUFFIX.exec(s);
|
|
91
|
+
if (suffix) {
|
|
92
|
+
scale = NUMBER_SUFFIXES[suffix[1]];
|
|
93
|
+
s = s.slice(0, suffix.index);
|
|
94
|
+
}
|
|
95
|
+
let value;
|
|
96
|
+
const g = GROUPED.exec(s);
|
|
97
|
+
if (g && (!g[4] || g[4] !== g[3])) {
|
|
98
|
+
value = Number(`${g[1]}${g[2].replace(/[.,]/g, "")}${g[5] ? `.${g[5]}` : ""}`);
|
|
99
|
+
} else {
|
|
100
|
+
const p = PLAIN.exec(s);
|
|
101
|
+
if (!p) return null;
|
|
102
|
+
value = Number(`${p[1]}${p[2] ? `.${p[2]}` : ""}`);
|
|
103
|
+
}
|
|
104
|
+
return sign * value * scale || 0;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// ---- boxes ----
|
|
108
|
+
const right = (b) => b.x + b.w;
|
|
109
|
+
const bottom = (b) => b.y + b.h;
|
|
110
|
+
const cx = (b) => b.x + b.w / 2;
|
|
111
|
+
const cy = (b) => b.y + b.h / 2;
|
|
112
|
+
const near = (a, b) => Math.abs(a - b) <= ALIGN_PX;
|
|
113
|
+
const unionBox = (boxes) => {
|
|
114
|
+
const x = Math.min(...boxes.map((b) => b.x));
|
|
115
|
+
const y = Math.min(...boxes.map((b) => b.y));
|
|
116
|
+
return { x, y, w: Math.max(...boxes.map(right)) - x, h: Math.max(...boxes.map(bottom)) - y };
|
|
117
|
+
};
|
|
118
|
+
const contains = (outer, inner) =>
|
|
119
|
+
inner.x >= outer.x - ALIGN_PX && inner.y >= outer.y - ALIGN_PX && right(inner) <= right(outer) + ALIGN_PX && bottom(inner) <= bottom(outer) + ALIGN_PX;
|
|
120
|
+
const overlaps = (a, b) => Math.min(right(a), right(b)) > Math.max(a.x, b.x) && Math.min(bottom(a), bottom(b)) > Math.max(a.y, b.y);
|
|
121
|
+
// Distance between two boxes (0 when they touch or overlap).
|
|
122
|
+
const gap = (a, b) => {
|
|
123
|
+
const dx = Math.max(0, a.x - right(b), b.x - right(a));
|
|
124
|
+
const dy = Math.max(0, a.y - bottom(b), b.y - bottom(a));
|
|
125
|
+
return Math.hypot(dx, dy);
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
// ---- series ----
|
|
129
|
+
// The data marks: not axis-like, not in a legend, not straight, color read, and not a background (a rect
|
|
130
|
+
// whose box holds every other candidate's box).
|
|
131
|
+
function dataMarks(marks) {
|
|
132
|
+
const list = marks.filter((m) => !m.axisLike && !m.inLegend && !m.straight && m.color !== null);
|
|
133
|
+
return list.filter((m) => !(m.tag === "rect" && list.length > 1 && list.every((o) => o === m || contains(m.box, o.box))));
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function seriesAll(marks) {
|
|
137
|
+
const groups = new Map();
|
|
138
|
+
for (const m of dataMarks(marks)) {
|
|
139
|
+
const g = groups.get(m.color) ?? [];
|
|
140
|
+
g.push(m);
|
|
141
|
+
groups.set(m.color, g);
|
|
142
|
+
}
|
|
143
|
+
return [...groups].map(([color, list]) => {
|
|
144
|
+
const fills = list.filter((m) => m.shape === "fill").length;
|
|
145
|
+
return { color, shape: fills * 2 >= list.length ? "fill" : "stroke", marks: list.length, box: unionBox(list.map((m) => m.box)) };
|
|
146
|
+
}).sort((a, b) => b.marks - a.marks || (a.color < b.color ? -1 : a.color > b.color ? 1 : 0));
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// The series of a chart: data marks grouped by color, the first MAX_SERIES by mark count, ties by color.
|
|
150
|
+
export function seriesOf(marks) {
|
|
151
|
+
return seriesAll(marks).slice(0, MAX_SERIES);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// ---- bars ----
|
|
155
|
+
// A filled rect, or a filled path or polygon of 4 vertices (5 when it closes on its first point).
|
|
156
|
+
const rectLike = (m) => m.shape === "fill" && (m.tag === "rect" || ((m.tag === "path" || m.tag === "polygon") && (m.points === 4 || m.points === 5)));
|
|
157
|
+
const spread = (values) => Math.max(...values) - Math.min(...values);
|
|
158
|
+
|
|
159
|
+
// The bar candidates of a chart (every series together) and the orientation their sizes allow.
|
|
160
|
+
function barShape(marks) {
|
|
161
|
+
const bars = dataMarks(marks).filter(rectLike).map((m) => m.box);
|
|
162
|
+
if (bars.length < MIN_BARS) return null;
|
|
163
|
+
const vertical = spread(bars.map((b) => b.w)) <= ALIGN_PX;
|
|
164
|
+
const horizontal = spread(bars.map((b) => b.h)) <= ALIGN_PX;
|
|
165
|
+
return vertical || horizontal ? { bars, vertical, horizontal } : null;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// One base line every bar ends on, within ALIGN_PX. Vertical bars prefer the bottom line, horizontal the left.
|
|
169
|
+
function baseOf(bars, lo, hi, preferHi) {
|
|
170
|
+
const candidates = bars.flatMap((b) => (preferHi ? [hi(b), lo(b)] : [lo(b), hi(b)]));
|
|
171
|
+
for (const line of candidates) {
|
|
172
|
+
if (bars.every((b) => near(lo(b), line) || near(hi(b), line))) {
|
|
173
|
+
const before = bars.filter((b) => near(hi(b), line) && !near(lo(b), line)).length;
|
|
174
|
+
const after = bars.filter((b) => near(lo(b), line) && !near(hi(b), line)).length;
|
|
175
|
+
return { base: line, sides: before && after ? "both" : "one" };
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// The bars of a chart: filled rect or rect-like data marks, at least MIN_BARS, of one width (vertical) or
|
|
182
|
+
// height (horizontal) within ALIGN_PX, all ending on one base line within ALIGN_PX ("both" when bars lie
|
|
183
|
+
// on both sides of it). null otherwise (stacked or floating bars share no base).
|
|
184
|
+
export function barsOf(marks) {
|
|
185
|
+
const shape = barShape(marks);
|
|
186
|
+
if (!shape) return null;
|
|
187
|
+
const { bars } = shape;
|
|
188
|
+
if (shape.vertical) {
|
|
189
|
+
const b = baseOf(bars, (x) => x.y, bottom, true);
|
|
190
|
+
if (b) return { orientation: "vertical", bars, ...b };
|
|
191
|
+
}
|
|
192
|
+
if (shape.horizontal) {
|
|
193
|
+
const b = baseOf(bars, (x) => x.x, right, false);
|
|
194
|
+
if (b) return { orientation: "horizontal", bars, ...b };
|
|
195
|
+
}
|
|
196
|
+
return null;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// ---- value axis ----
|
|
200
|
+
// Display precision of a base value, not a cutoff: the fitted base is rounded to the power of ten at or
|
|
201
|
+
// under the value one px stands for (60 units over 480 px gives tenths), so sub-pixel offsets of the tick
|
|
202
|
+
// text boxes do not print. The zero test uses the raw value.
|
|
203
|
+
const roundToPx = (v, perPx) => {
|
|
204
|
+
const step = 10 ** Math.floor(Math.log10(perPx));
|
|
205
|
+
return Math.round(v / step) * step;
|
|
206
|
+
};
|
|
207
|
+
const clean = (v) => Number(v.toPrecision(12)) || 0;
|
|
208
|
+
|
|
209
|
+
// The value axis of a bar chart: the numeric texts on the value side of the bars (left or right for
|
|
210
|
+
// vertical bars, below or above for horizontal, the side with more), one per distinct position; "none"
|
|
211
|
+
// under two; a least squares line of value against position; "not-linear" when a tick lies more than
|
|
212
|
+
// LINEAR_SHARE of the tick range from the line; else baseValue is the value of the tick whose text box
|
|
213
|
+
// holds the base line (within ALIGN_PX on the position axis; the nearest such tick by center), since a label
|
|
214
|
+
// can sit on its tick by its baseline, center or top; with no such tick, the line at
|
|
215
|
+
// the base, 0 when within ZERO_SHARE of the tick range.
|
|
216
|
+
export function valueAxis(texts, bars) {
|
|
217
|
+
const plot = unionBox(bars.bars);
|
|
218
|
+
const vertical = bars.orientation === "vertical";
|
|
219
|
+
const numeric = texts.filter((t) => !t.inLegend).map((t) => ({ t, value: parseNumber(t.text) })).filter((n) => n.value !== null);
|
|
220
|
+
const sides = vertical
|
|
221
|
+
? [numeric.filter((n) => right(n.t.box) <= plot.x + ALIGN_PX), numeric.filter((n) => n.t.box.x >= right(plot) - ALIGN_PX)]
|
|
222
|
+
: [numeric.filter((n) => n.t.box.y >= bottom(plot) - ALIGN_PX), numeric.filter((n) => bottom(n.t.box) <= plot.y + ALIGN_PX)];
|
|
223
|
+
const side = sides[1].length > sides[0].length ? sides[1] : sides[0];
|
|
224
|
+
const ticks = [];
|
|
225
|
+
for (const n of side) {
|
|
226
|
+
const at = vertical ? cy(n.t.box) : cx(n.t.box);
|
|
227
|
+
if (!ticks.some((k) => near(k.at, at))) ticks.push({ value: n.value, at, box: n.t.box });
|
|
228
|
+
}
|
|
229
|
+
const out = ticks.map(({ value, at }) => ({ value, at }));
|
|
230
|
+
if (ticks.length < 2) return { kind: "none", ticks: out, baseValue: null };
|
|
231
|
+
const n = ticks.length;
|
|
232
|
+
const mx = ticks.reduce((s, k) => s + k.at, 0) / n;
|
|
233
|
+
const my = ticks.reduce((s, k) => s + k.value, 0) / n;
|
|
234
|
+
const sxx = ticks.reduce((s, k) => s + (k.at - mx) ** 2, 0);
|
|
235
|
+
const sxy = ticks.reduce((s, k) => s + (k.at - mx) * (k.value - my), 0);
|
|
236
|
+
const slope = sxy / sxx;
|
|
237
|
+
const fit = (at) => my + slope * (at - mx);
|
|
238
|
+
const range = spread(ticks.map((k) => k.value));
|
|
239
|
+
if (!(range > 0) || ticks.some((k) => Math.abs(k.value - fit(k.at)) > LINEAR_SHARE * range)) {
|
|
240
|
+
return { kind: "not-linear", ticks: out, baseValue: null };
|
|
241
|
+
}
|
|
242
|
+
const lo = (b) => (vertical ? b.y : b.x);
|
|
243
|
+
const hi = (b) => (vertical ? bottom(b) : right(b));
|
|
244
|
+
const holding = ticks
|
|
245
|
+
.filter((k) => bars.base >= lo(k.box) - ALIGN_PX && bars.base <= hi(k.box) + ALIGN_PX)
|
|
246
|
+
.sort((a, b) => Math.abs(a.at - bars.base) - Math.abs(b.at - bars.base));
|
|
247
|
+
if (holding.length) return { kind: "linear", ticks: out, baseValue: holding[0].value };
|
|
248
|
+
const base = fit(bars.base);
|
|
249
|
+
return { kind: "linear", ticks: out, baseValue: Math.abs(base) <= ZERO_SHARE * range ? 0 : clean(roundToPx(base, Math.abs(slope))) };
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// ---- texts ----
|
|
253
|
+
// Texts outside the plot box that share a center line (row or column) within ALIGN_PX with at least two
|
|
254
|
+
// other such texts: tick and category rows and columns.
|
|
255
|
+
export function axisTexts(texts, plot) {
|
|
256
|
+
const outside = texts.filter((t) => !overlaps(t.box, plot));
|
|
257
|
+
const found = new Set();
|
|
258
|
+
for (const t of outside) {
|
|
259
|
+
const row = outside.filter((o) => o !== t && near(cy(o.box), cy(t.box))).length;
|
|
260
|
+
const col = outside.filter((o) => o !== t && near(cx(o.box), cx(t.box))).length;
|
|
261
|
+
if (row >= 2 || col >= 2) found.add(t);
|
|
262
|
+
}
|
|
263
|
+
return found;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// Each legend swatch whose color is a series color, paired with the nearest legend text on its row (center
|
|
267
|
+
// within ALIGN_PX vertically, the text to its right). One entry per color, in swatch order.
|
|
268
|
+
export function legendEntries(legend, series) {
|
|
269
|
+
if (!legend) return [];
|
|
270
|
+
const colors = new Set(series.map((s) => s.color));
|
|
271
|
+
const out = [];
|
|
272
|
+
for (const sw of legend.swatches) {
|
|
273
|
+
if (!colors.has(sw.color) || out.some((e) => e.color === sw.color)) continue;
|
|
274
|
+
const onRow = legend.texts
|
|
275
|
+
.filter((t) => near(cy(t.box), cy(sw.box)) && t.box.x >= right(sw.box) - ALIGN_PX)
|
|
276
|
+
.sort((a, b) => a.box.x - b.box.x);
|
|
277
|
+
if (onRow.length) out.push({ color: sw.color, name: onRow[0].text });
|
|
278
|
+
}
|
|
279
|
+
return out;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// Direct labels: non-numeric texts, not in the legend and not axis texts. A text in a series color labels
|
|
283
|
+
// that series; otherwise it labels at most one series: the one whose nearest data mark box
|
|
284
|
+
// is closest, within LABEL_GAP_PX (a tie labels none).
|
|
285
|
+
export function directLabels(series, marks, texts, axis, legend) {
|
|
286
|
+
const out = new Map(series.map((s) => [s.color, []]));
|
|
287
|
+
const data = dataMarks(marks).filter((m) => out.has(m.color));
|
|
288
|
+
const legendTexts = new Set(legend?.texts ?? []);
|
|
289
|
+
for (const t of texts) {
|
|
290
|
+
if (t.inLegend || legendTexts.has(t) || axis.has(t) || parseNumber(t.text) !== null) continue;
|
|
291
|
+
if (out.has(t.color)) {
|
|
292
|
+
out.get(t.color).push(t.text);
|
|
293
|
+
continue;
|
|
294
|
+
}
|
|
295
|
+
let best = null;
|
|
296
|
+
let bestGap = Infinity;
|
|
297
|
+
let tie = false;
|
|
298
|
+
for (const s of series) {
|
|
299
|
+
const d = Math.min(...data.filter((m) => m.color === s.color).map((m) => gap(t.box, m.box)));
|
|
300
|
+
if (d < bestGap) {
|
|
301
|
+
best = s.color;
|
|
302
|
+
bestGap = d;
|
|
303
|
+
tie = false;
|
|
304
|
+
} else if (d === bestGap) {
|
|
305
|
+
tie = true;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
if (best !== null && !tie && bestGap <= LABEL_GAP_PX) out.get(best).push(t.text);
|
|
309
|
+
}
|
|
310
|
+
return out;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// ---- the axis of a word-less svg ----
|
|
314
|
+
// Definition, not a cutoff: texts on one line share an edge or
|
|
315
|
+
// center within this many px.
|
|
316
|
+
export const AXIS_LINE_PX = 1;
|
|
317
|
+
|
|
318
|
+
// Whether the texts hold an axis for the marks: at least 2 numeric texts (not in a legend) on one line, lying
|
|
319
|
+
// along one edge of the marks' box: left or right of it with the same left, right or center x within
|
|
320
|
+
// AXIS_LINE_PX (a vertical axis), or below or above it with the same bottom, top or center y (one text
|
|
321
|
+
// baseline: a horizontal axis).
|
|
322
|
+
export function hasAxis(texts, marks) {
|
|
323
|
+
if (!marks.length) return false;
|
|
324
|
+
const plot = unionBox(marks.map((m) => m.box));
|
|
325
|
+
const boxes = texts.filter((t) => !t.inLegend && parseNumber(t.text) !== null).map((t) => t.box);
|
|
326
|
+
const lined = (list, keys) => keys.some((key) => list.some((a) => list.filter((b) => Math.abs(key(a) - key(b)) <= AXIS_LINE_PX).length >= 2));
|
|
327
|
+
const xs = [(b) => b.x, right, cx];
|
|
328
|
+
const ys = [(b) => b.y, bottom, cy];
|
|
329
|
+
return (
|
|
330
|
+
lined(boxes.filter((b) => right(b) <= plot.x + ALIGN_PX), xs) ||
|
|
331
|
+
lined(boxes.filter((b) => b.x >= right(plot) - ALIGN_PX), xs) ||
|
|
332
|
+
lined(boxes.filter((b) => b.y >= bottom(plot) - ALIGN_PX), ys) ||
|
|
333
|
+
lined(boxes.filter((b) => bottom(b) <= plot.y + ALIGN_PX), ys)
|
|
334
|
+
);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// ---- the chart ----
|
|
338
|
+
const keyOf = (t) => `${t.selector}|${t.text}|${t.box.x},${t.box.y}`;
|
|
339
|
+
|
|
340
|
+
// A chart reading from a RawChart, or null when it is not a chart: an svg is one when a chart word names it
|
|
341
|
+
// or it holds at least MIN_MARKS data marks and 2 numeric texts that form an axis along their edge (hasAxis);
|
|
342
|
+
// a canvas only when a chart word names it.
|
|
343
|
+
export function readChart(raw) {
|
|
344
|
+
const head = { index: raw.index, kind: raw.kind, selector: raw.selector, box: raw.box, name: raw.name };
|
|
345
|
+
if (raw.kind === "canvas") {
|
|
346
|
+
if (!raw.chartWord) return null;
|
|
347
|
+
return {
|
|
348
|
+
...head, series: null, bars: null, axis: null, labels: null, legend: null, direct: null,
|
|
349
|
+
notes: [`${raw.selector} is a canvas: its baseline, labels and legend are drawn as pixels and were not read.`],
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
const data = dataMarks(raw.marks);
|
|
353
|
+
const numeric = raw.texts.filter((t) => parseNumber(t.text) !== null).length;
|
|
354
|
+
// A candidate with no marks is not a chart, whatever word names it.
|
|
355
|
+
if (!raw.marks.length || (!raw.chartWord && !(data.length >= MIN_MARKS && numeric >= MIN_NUMERIC_TEXTS && hasAxis(raw.texts, data)))) return null;
|
|
356
|
+
const notes = [];
|
|
357
|
+
const all = seriesAll(raw.marks);
|
|
358
|
+
const series = all.slice(0, MAX_SERIES);
|
|
359
|
+
if (all.length > MAX_SERIES) notes.push(`${raw.selector}: the first ${MAX_SERIES} of ${all.length} series were read.`);
|
|
360
|
+
const bars = barsOf(raw.marks);
|
|
361
|
+
let axis = null;
|
|
362
|
+
if (bars) {
|
|
363
|
+
axis = valueAxis(raw.texts, bars);
|
|
364
|
+
if (axis.kind === "none") notes.push(`${raw.selector}: the bars have no value axis labels; the baseline was not read.`);
|
|
365
|
+
if (axis.kind === "not-linear") notes.push(`${raw.selector}: the value axis is not linear (ticks ${axis.ticks.map((k) => k.value).join(", ")}); the baseline was not read.`);
|
|
366
|
+
} else if (barShape(raw.marks)) {
|
|
367
|
+
notes.push(`${raw.selector}: the bars do not share one base (stacked or floating); the baseline was not read.`);
|
|
368
|
+
}
|
|
369
|
+
const own = new Set(raw.texts.map(keyOf));
|
|
370
|
+
const labelTexts = [...raw.texts, ...(raw.legend?.texts ?? []).filter((t) => !own.has(keyOf(t)))];
|
|
371
|
+
const labels = labelTexts.length
|
|
372
|
+
? {
|
|
373
|
+
count: labelTexts.length,
|
|
374
|
+
minPx: Math.min(...labelTexts.map((t) => t.px)),
|
|
375
|
+
// Every label size, ascending (the label-size evaluator counts past the listed ones).
|
|
376
|
+
sizes: labelTexts.map((t) => t.px).sort((a, b) => a - b),
|
|
377
|
+
smallest: labelTexts
|
|
378
|
+
.map((t, i) => ({ t, i }))
|
|
379
|
+
.sort((a, b) => a.t.px - b.t.px || a.i - b.i)
|
|
380
|
+
.slice(0, MAX_LISTED)
|
|
381
|
+
.map(({ t }) => ({ text: t.text, px: t.px, selector: t.selector })),
|
|
382
|
+
}
|
|
383
|
+
: null;
|
|
384
|
+
const legend = raw.legend ? legendEntries(raw.legend, series) : null;
|
|
385
|
+
const plot = data.length ? unionBox(data.map((m) => m.box)) : null;
|
|
386
|
+
const axisSet = plot ? axisTexts(raw.texts, plot) : new Set();
|
|
387
|
+
const direct = [...directLabels(series, raw.marks, raw.texts, axisSet, raw.legend)]
|
|
388
|
+
.filter(([, names]) => names.length)
|
|
389
|
+
.map(([color, names]) => ({ color, names }));
|
|
390
|
+
if (raw.unread.paint) {
|
|
391
|
+
const n = raw.unread.paint;
|
|
392
|
+
notes.push(`${raw.selector}: ${n} ${plural(n, "mark paints with a gradient or pattern; its color was not read.", "marks paint with a gradient or pattern; their colors were not read.")}`);
|
|
393
|
+
}
|
|
394
|
+
if (raw.unread.clip) {
|
|
395
|
+
const n = raw.unread.clip;
|
|
396
|
+
notes.push(`${raw.selector}: ${n} ${plural(n, "mark sits", "marks sit")} under a clip path that is not one rect; it was not applied.`);
|
|
397
|
+
}
|
|
398
|
+
if (raw.cut.marks) notes.push(`${raw.selector}: the first ${MAX_MARKS} marks were read.`);
|
|
399
|
+
if (raw.cut.texts) notes.push(`${raw.selector}: the first ${MAX_TEXTS} texts were read.`);
|
|
400
|
+
return { ...head, series, bars, axis, labels, legend, direct, notes };
|
|
401
|
+
}
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { targetKey } from "../project/match.mjs";
|
|
4
|
+
import { restOn, restOff } from "./at-rest.mjs";
|
|
5
|
+
import { bounded } from "./states.mjs";
|
|
6
|
+
import { DECODE } from "./motion.mjs";
|
|
7
|
+
import { cvdSource } from "./cvd.mjs";
|
|
8
|
+
import { READER_OPTS, readChart } from "./chart-read.mjs";
|
|
9
|
+
|
|
10
|
+
// The chart measurer. A page measurer that reads the shared page and never acts on it: with
|
|
11
|
+
// the page at rest, the composition reader `read: "chart"` lists the charts (chart-read.mjs turns each into a
|
|
12
|
+
// reading), the part of each chart in the first view is clipped losslessly with page.screenshot (a canvas only once two clips are
|
|
13
|
+
// byte-equal), and the clips go to a blank analysis context (every request aborted) where in-page/cvd.js
|
|
14
|
+
// reads canvas colors in buckets and simulates each view for the review sheet. The evaluators
|
|
15
|
+
// (src/report/evaluators/chart.mjs) compare the series colors with the same library in Node.
|
|
16
|
+
|
|
17
|
+
// Definitions and measurement constants fixed before any chart was measured, never rule
|
|
18
|
+
// cutoffs and never edited after a reading.
|
|
19
|
+
// A canvas color bucket covering less than this share of the clip is antialiasing, text or grid, not a series.
|
|
20
|
+
export const MIN_SERIES_SHARE = 0.005;
|
|
21
|
+
// A canvas is read once two clips this far apart are byte-equal, in at most this many rounds (every
|
|
22
|
+
// unsettled canvas is clipped in each round, so the wait does not grow with the canvas count).
|
|
23
|
+
export const CANVAS_GAP_MS = 250;
|
|
24
|
+
export const CANVAS_TRIES = 8;
|
|
25
|
+
// Sheet display width per tile, in px (layout of the review image, not a reading).
|
|
26
|
+
export const TILE_PX = 480;
|
|
27
|
+
// Sheet label strips, in px: one per row (the chart) and one above each tile (the view).
|
|
28
|
+
export const ROW_LABEL_PX = 24;
|
|
29
|
+
export const TILE_LABEL_PX = 20;
|
|
30
|
+
// Time cap of the whole measure at one width (a cap, not a cutoff).
|
|
31
|
+
export const CHART_CAP_MS = 30000;
|
|
32
|
+
// After the cap, the stray work is awaited this long before cleanup (a cap, not a cutoff).
|
|
33
|
+
export const CLEANUP_CAP_MS = 5000;
|
|
34
|
+
|
|
35
|
+
export const CHART_IDS = ["chart.baseline", "chart.label-size", "chart.direct-labels", "chart.categories", "chart.cvd"];
|
|
36
|
+
// With ctx.sheets false (the post-edit check) nothing is
|
|
37
|
+
// written into the project: svg charts are not clipped, canvases are clipped only to read their colors, and the
|
|
38
|
+
// report carries this note instead of the sheet.
|
|
39
|
+
export const NO_SHEET_NOTE = "No chart sheet was written: the post-edit check writes none; a check-design run that includes the data area writes it.";
|
|
40
|
+
// The tiles of a sheet row: the capture as it is, then the simulated views.
|
|
41
|
+
export const SHEET_VIEWS = ["normal", "protanopia", "deuteranopia", "tritanopia", "grayscale"];
|
|
42
|
+
|
|
43
|
+
let script;
|
|
44
|
+
|
|
45
|
+
const plural = (n, one, many) => (n === 1 ? one : many);
|
|
46
|
+
const joined = (list) => (list.length < 2 ? list.join("") : `${list.slice(0, -1).join(", ")} and ${list[list.length - 1]}`);
|
|
47
|
+
const firstLine = (err) => String(err?.message ?? err).split("\n")[0];
|
|
48
|
+
const wait = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
49
|
+
|
|
50
|
+
// "<names> was not checked" or "were not checked": the selected rules' names, else the measure ids.
|
|
51
|
+
function notChecked(rules) {
|
|
52
|
+
const list = (rules ?? []).filter((r) => CHART_IDS.includes(r.measure)).map((r) => r.name);
|
|
53
|
+
const names = list.length > 0 ? list : CHART_IDS;
|
|
54
|
+
return `${joined(names)} ${plural(names.length, "was", "were")} not checked`;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// The labels of the review sheet: one per row (the chart number and its selector) and the view name of each
|
|
58
|
+
// tile. Nothing else is written on the sheet: no file name, host or path.
|
|
59
|
+
export function sheetLabels(charts) {
|
|
60
|
+
return { rows: charts.map((c, i) => `chart ${i + 1}: ${c.selector}`), tiles: [...SHEET_VIEWS] };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// The clip of a chart (images only of what lies in the first view, never fullPage): its
|
|
64
|
+
// painted part (the reader's visible box, page coordinates) cut by the first view and moved to viewport
|
|
65
|
+
// coordinates, rounded inward to whole px; null when nothing of it is in the first view.
|
|
66
|
+
function clipOf(visible, view) {
|
|
67
|
+
if (!visible) return null;
|
|
68
|
+
const left = Math.max(visible.x, view.x);
|
|
69
|
+
const top = Math.max(visible.y, view.y);
|
|
70
|
+
const x = Math.ceil(left - view.x);
|
|
71
|
+
const y = Math.ceil(top - view.y);
|
|
72
|
+
const w = Math.floor(Math.min(visible.x + visible.w, view.x + view.w) - view.x) - x;
|
|
73
|
+
const h = Math.floor(Math.min(visible.y + visible.h, view.y + view.h) - view.y) - y;
|
|
74
|
+
return w >= 1 && h >= 1 ? { x, y, width: w, height: h } : null;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// The work of one measure. Every page call checks state.cancelled first; restOff is never run
|
|
78
|
+
// here (only the outer finally runs it).
|
|
79
|
+
async function work(page, ctx, state) {
|
|
80
|
+
const guard = () => {
|
|
81
|
+
if (state.cancelled) throw new Error("cancelled");
|
|
82
|
+
};
|
|
83
|
+
const notes = [];
|
|
84
|
+
script ??= await fs.readFile(new URL("./in-page/composition.js", import.meta.url), "utf8");
|
|
85
|
+
guard();
|
|
86
|
+
await page.evaluate(restOn());
|
|
87
|
+
guard();
|
|
88
|
+
const raw = await page.evaluate(`(${script})(${JSON.stringify(READER_OPTS)})`);
|
|
89
|
+
notes.push(...raw.notes);
|
|
90
|
+
const found = [];
|
|
91
|
+
for (const r of raw.charts) {
|
|
92
|
+
const reading = readChart(r);
|
|
93
|
+
if (reading) found.push({ raw: r, reading });
|
|
94
|
+
}
|
|
95
|
+
if (found.length === 0) return { charts: [], notes, written: [] };
|
|
96
|
+
// What each reading could not read (the canvas note, an axis that is not linear, cut samples).
|
|
97
|
+
for (const f of found) notes.push(...f.reading.notes);
|
|
98
|
+
|
|
99
|
+
// Captures: svg charts once, canvases in rounds until two clips are byte-equal. Only the part in the first
|
|
100
|
+
// view is captured, without fullPage (which captures beyond the viewport and fires resize on the page);
|
|
101
|
+
// a chart outside the first view is read from its markup only.
|
|
102
|
+
guard();
|
|
103
|
+
const view = await page.evaluate("({ x: scrollX, y: scrollY, w: document.documentElement.clientWidth, h: document.documentElement.clientHeight })");
|
|
104
|
+
const shoot = async (clip) => {
|
|
105
|
+
guard();
|
|
106
|
+
return page.screenshot({ type: "png", clip, caret: "initial" });
|
|
107
|
+
};
|
|
108
|
+
const items = found.map(({ raw: r, reading }) => {
|
|
109
|
+
const clip = clipOf(r.visible, view);
|
|
110
|
+
if (!r.visible) {
|
|
111
|
+
notes.push(`${r.selector}: nothing of the chart paints inside its scroll box; it was not captured.`);
|
|
112
|
+
} else if (!clip) {
|
|
113
|
+
const where = r.visible.y >= view.y + view.h ? "below the first view" : "outside the first view";
|
|
114
|
+
notes.push(reading.kind === "canvas"
|
|
115
|
+
? `${r.selector}: ${where}; its pixels were not read.`
|
|
116
|
+
: `${r.selector}: ${where}; it was read from its markup and is not on the sheet.`);
|
|
117
|
+
} else {
|
|
118
|
+
// The larger of the two cuts, across or down.
|
|
119
|
+
const cut = Math.round(Math.max(r.box.w - r.visible.w, r.box.h - r.visible.h));
|
|
120
|
+
if (cut >= 1) notes.push(`${r.selector}: ${cut} px of the chart lie outside its scroll box; the capture shows the visible part.`);
|
|
121
|
+
// From the unrounded geometry against the view: the inward rounding of the clip is
|
|
122
|
+
// silent; the note shows only when more than 1 px lies outside, across or down.
|
|
123
|
+
const v = r.visible;
|
|
124
|
+
const across = Math.max(0, view.x - v.x) + Math.max(0, v.x + v.w - (view.x + view.w));
|
|
125
|
+
const down = Math.max(0, view.y - v.y) + Math.max(0, v.y + v.h - (view.y + view.h));
|
|
126
|
+
const outside = Math.max(across, down);
|
|
127
|
+
if (outside > 1) notes.push(`${r.selector}: ${Math.round(outside)} px of the chart lie outside the first view; the capture shows the part in view.`);
|
|
128
|
+
}
|
|
129
|
+
return { reading, clip, png: null, settled: reading.kind === "svg" };
|
|
130
|
+
});
|
|
131
|
+
const sheet = ctx.sheets !== false;
|
|
132
|
+
if (!sheet) notes.push(NO_SHEET_NOTE);
|
|
133
|
+
if (sheet) for (const it of items) if (it.clip && it.reading.kind === "svg") it.png = await shoot(it.clip);
|
|
134
|
+
const canvases = items.filter((it) => it.clip && it.reading.kind === "canvas");
|
|
135
|
+
for (const it of canvases) it.png = await shoot(it.clip);
|
|
136
|
+
for (let round = 1; round < CANVAS_TRIES && canvases.some((it) => !it.settled); round++) {
|
|
137
|
+
await wait(CANVAS_GAP_MS);
|
|
138
|
+
for (const it of canvases.filter((x) => !x.settled)) {
|
|
139
|
+
const next = await shoot(it.clip);
|
|
140
|
+
it.settled = next.equals(it.png);
|
|
141
|
+
it.png = next;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
for (const it of canvases) {
|
|
145
|
+
if (!it.settled) notes.push(`${it.reading.selector}: the canvas was still changing after ${(CANVAS_TRIES - 1) * CANVAS_GAP_MS} ms; its colors were not read.`);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const charts = items.map((it) => ({
|
|
149
|
+
...it.reading,
|
|
150
|
+
colors: it.reading.kind === "svg" ? { source: "dom", list: it.reading.series.map((s) => ({ hex: s.color, share: null })) } : null,
|
|
151
|
+
}));
|
|
152
|
+
// A canvas that never settled is left out of the sheet (its last clip is mid-redraw).
|
|
153
|
+
const shot = items.filter((it) => it.png && it.settled);
|
|
154
|
+
if (shot.length === 0) return { charts, notes, written: [] };
|
|
155
|
+
|
|
156
|
+
// The analysis context (the browser may be unknown).
|
|
157
|
+
guard();
|
|
158
|
+
const browser = page.context().browser();
|
|
159
|
+
if (!browser) {
|
|
160
|
+
notes.push("No browser was found to open the analysis page; canvas colors were not read and no sheet was written.");
|
|
161
|
+
return { charts, notes, written: [] };
|
|
162
|
+
}
|
|
163
|
+
state.context = await browser.newContext();
|
|
164
|
+
if (state.cancelled) {
|
|
165
|
+
// Opened after the cap: the outer cleanup may have run already.
|
|
166
|
+
await state.context.close().catch(() => {});
|
|
167
|
+
throw new Error("cancelled");
|
|
168
|
+
}
|
|
169
|
+
await state.context.route("**/*", (route) => route.abort());
|
|
170
|
+
const analysis = await state.context.newPage();
|
|
171
|
+
await analysis.evaluate(`window.__lazuliCvd = ${cvdSource}; window.__lazuliDecode = ${DECODE};`);
|
|
172
|
+
for (const [i, it] of items.entries()) {
|
|
173
|
+
if (it.reading.kind !== "canvas" || !it.settled || !it.png) continue;
|
|
174
|
+
guard();
|
|
175
|
+
const list = await analysis.evaluate(
|
|
176
|
+
async ([b64, minShare]) => window.__lazuliCvd.bucketsOf(await window.__lazuliDecode(b64), { minShare }),
|
|
177
|
+
[it.png.toString("base64"), MIN_SERIES_SHARE],
|
|
178
|
+
);
|
|
179
|
+
charts[i].colors = { source: "pixels", list };
|
|
180
|
+
}
|
|
181
|
+
if (!sheet) return { charts, notes, written: [] };
|
|
182
|
+
guard();
|
|
183
|
+
const labels = sheetLabels(shot.map((it) => it.reading));
|
|
184
|
+
const b64 = await analysis.evaluate(
|
|
185
|
+
async ([pngs, labels, o]) => {
|
|
186
|
+
const cvd = window.__lazuliCvd;
|
|
187
|
+
const rows = [];
|
|
188
|
+
for (const p of pngs) {
|
|
189
|
+
const img = await window.__lazuliDecode(p);
|
|
190
|
+
const h = Math.max(1, Math.round((img.height * o.tile) / img.width));
|
|
191
|
+
rows.push({ img, h });
|
|
192
|
+
}
|
|
193
|
+
const width = o.views.length * o.tile;
|
|
194
|
+
const height = rows.reduce((s, r) => s + o.rowLabel + o.tileLabel + r.h, 0);
|
|
195
|
+
const cv = new OffscreenCanvas(width, height);
|
|
196
|
+
const g = cv.getContext("2d");
|
|
197
|
+
g.fillStyle = "#ffffff";
|
|
198
|
+
g.fillRect(0, 0, width, height);
|
|
199
|
+
g.textBaseline = "middle";
|
|
200
|
+
let y = 0;
|
|
201
|
+
rows.forEach((r, k) => {
|
|
202
|
+
g.fillStyle = "#000000";
|
|
203
|
+
g.font = `${o.rowLabel - 8}px sans-serif`;
|
|
204
|
+
g.fillText(labels.rows[k], 4, y + o.rowLabel / 2);
|
|
205
|
+
y += o.rowLabel;
|
|
206
|
+
o.views.forEach((view, j) => {
|
|
207
|
+
const x = j * o.tile;
|
|
208
|
+
g.fillStyle = "#000000";
|
|
209
|
+
g.font = `${o.tileLabel - 6}px sans-serif`;
|
|
210
|
+
g.fillText(labels.tiles[j], x + 4, y + o.tileLabel / 2);
|
|
211
|
+
const sim = view === "normal" ? r.img : cvd.simulateImage(r.img, view);
|
|
212
|
+
const src = new OffscreenCanvas(sim.width, sim.height);
|
|
213
|
+
src.getContext("2d").putImageData(new ImageData(sim.data, sim.width, sim.height), 0, 0);
|
|
214
|
+
g.drawImage(src, x, y + o.tileLabel, o.tile, r.h);
|
|
215
|
+
});
|
|
216
|
+
y += o.tileLabel + r.h;
|
|
217
|
+
});
|
|
218
|
+
const bytes = new Uint8Array(await (await cv.convertToBlob({ type: "image/png" })).arrayBuffer());
|
|
219
|
+
let s = "";
|
|
220
|
+
for (let i = 0; i < bytes.length; i += 0x8000) s += String.fromCharCode(...bytes.subarray(i, i + 0x8000));
|
|
221
|
+
return btoa(s);
|
|
222
|
+
},
|
|
223
|
+
[shot.map((it) => it.png.toString("base64")), labels, { tile: TILE_PX, rowLabel: ROW_LABEL_PX, tileLabel: TILE_LABEL_PX, views: SHEET_VIEWS }],
|
|
224
|
+
);
|
|
225
|
+
guard();
|
|
226
|
+
const file = path.join(ctx.cwd, ".lazuli", "charts", targetKey(ctx.target, ctx.cwd), `charts-${ctx.width}.png`);
|
|
227
|
+
await fs.mkdir(path.dirname(file), { recursive: true });
|
|
228
|
+
await fs.writeFile(file, Buffer.from(b64, "base64"));
|
|
229
|
+
notes.push(`Charts for review: ${file}`);
|
|
230
|
+
return { charts, notes, written: [file] };
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// ChartReport = { width, charts, written, notes, elapsedMs }: charts [] on a page with no chart, null when the
|
|
234
|
+
// measure failed or passed its cap.
|
|
235
|
+
export async function measureChart(page, ctx, { capMs = CHART_CAP_MS } = {}) {
|
|
236
|
+
const started = Date.now();
|
|
237
|
+
const report = { width: ctx.width, charts: null, written: [], notes: [], elapsedMs: null };
|
|
238
|
+
const state = { cancelled: false, context: null };
|
|
239
|
+
// The work never rejects: its error is carried, so a rejection of bounded is the cap alone.
|
|
240
|
+
const running = work(page, ctx, state).then((out) => ({ out }), (err) => ({ err }));
|
|
241
|
+
try {
|
|
242
|
+
let done;
|
|
243
|
+
try {
|
|
244
|
+
done = await bounded(running, capMs, "the chart measure");
|
|
245
|
+
} catch {
|
|
246
|
+
state.cancelled = true;
|
|
247
|
+
await bounded(running, CLEANUP_CAP_MS, "the chart measure cleanup").catch(() => {});
|
|
248
|
+
report.notes.push(`The chart measure did not finish within ${capMs} ms; ${notChecked(ctx.rules)}.`);
|
|
249
|
+
return report;
|
|
250
|
+
}
|
|
251
|
+
if (done.err) {
|
|
252
|
+
report.notes.push(`The chart measure failed: ${firstLine(done.err)}; ${notChecked(ctx.rules)}.`);
|
|
253
|
+
return report;
|
|
254
|
+
}
|
|
255
|
+
report.charts = done.out.charts;
|
|
256
|
+
report.written = done.out.written;
|
|
257
|
+
report.notes = done.out.notes;
|
|
258
|
+
return report;
|
|
259
|
+
} finally {
|
|
260
|
+
if (state.context) await state.context.close().catch(() => {});
|
|
261
|
+
await page.evaluate(restOff()).catch(() => {});
|
|
262
|
+
report.elapsedMs = Date.now() - started;
|
|
263
|
+
}
|
|
264
|
+
}
|