castle-web-cli 0.4.212 → 0.4.214
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/dist/agent-prompts.d.ts +1 -0
- package/dist/agent-prompts.js +28 -6
- package/dist/agent.js +33 -2
- package/dist/cloudGit.d.ts +254 -0
- package/dist/cloudGit.js +1687 -0
- package/dist/cloudGitServe.d.ts +41 -0
- package/dist/cloudGitServe.js +318 -0
- package/dist/cover-bridge/play-cover-bridge.js +2 -0
- package/dist/externalSourcemaps.js +2 -1
- package/dist/gitRemote.d.ts +4 -1
- package/dist/gitRemote.js +4 -2
- package/dist/ide.js +52 -7
- package/dist/index.js +149 -2
- package/dist/playCover/bridge.d.ts +6 -0
- package/dist/playCover/bridge.js +159 -0
- package/dist/playCover/builtins.d.ts +29 -0
- package/dist/playCover/builtins.js +189 -0
- package/dist/playCover/capture.d.ts +53 -0
- package/dist/playCover/capture.js +419 -0
- package/dist/playCover/clone.d.ts +40 -0
- package/dist/playCover/clone.js +317 -0
- package/dist/playCover/engine.d.ts +37 -0
- package/dist/playCover/engine.js +76 -0
- package/dist/playCover/errors.d.ts +6 -0
- package/dist/playCover/errors.js +18 -0
- package/dist/playCover/images.d.ts +21 -0
- package/dist/playCover/images.js +180 -0
- package/dist/playCover/instrument.d.ts +49 -0
- package/dist/playCover/instrument.js +334 -0
- package/dist/playCover/protocol.d.ts +14 -0
- package/dist/playCover/protocol.js +139 -0
- package/dist/playCover/rasterize.d.ts +21 -0
- package/dist/playCover/rasterize.js +408 -0
- package/dist/playCover/resources.d.ts +31 -0
- package/dist/playCover/resources.js +823 -0
- package/dist/playCover/scratchCanvas.d.ts +4 -0
- package/dist/playCover/scratchCanvas.js +16 -0
- package/dist/playCover/textSamples.d.ts +36 -0
- package/dist/playCover/textSamples.js +412 -0
- package/dist/playCover/validate.d.ts +79 -0
- package/dist/playCover/validate.js +883 -0
- package/dist/playCover/verify.d.ts +56 -0
- package/dist/playCover/verify.js +578 -0
- package/dist/playCover/verifyRecord.d.ts +71 -0
- package/dist/playCover/verifyRecord.js +243 -0
- package/dist/playCoverGate.d.ts +4 -0
- package/dist/playCoverGate.js +57 -0
- package/dist/playCoverPlacement.d.ts +11 -0
- package/dist/playCoverPlacement.js +110 -0
- package/dist/serve.d.ts +1 -0
- package/dist/serve.js +35 -17
- package/dist/shell/assets/index-BPhS3Ysy.css +1 -0
- package/dist/shell/assets/{index-DzLbNBc7.js → index-BcslKMfS.js} +94 -92
- package/dist/shell/index.html +2 -2
- package/package.json +2 -2
- package/dist/shell/assets/index-BCrgYh76.css +0 -1
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { Effect, EmbeddedBitmap } from './verifyRecord.js';
|
|
2
|
+
import { type PageInk, type TextSample } from './textSamples.js';
|
|
3
|
+
export interface Raster {
|
|
4
|
+
data: Uint8ClampedArray;
|
|
5
|
+
width: number;
|
|
6
|
+
height: number;
|
|
7
|
+
}
|
|
8
|
+
export declare const STRIP_COLOR = "#00ff00";
|
|
9
|
+
/** How many bitmaps one capture can check: each needs its own green code. */
|
|
10
|
+
export declare const MAX_CHECKED_BITMAPS = 25;
|
|
11
|
+
/** The colour a bitmap's box takes in the identifying draw. */
|
|
12
|
+
export declare function bitmapCode(id: number): string;
|
|
13
|
+
export declare function stripHeight(samples: TextSample[], effects: Effect[], width: number): number;
|
|
14
|
+
/** The strip's markup, in the document the card is serialized into. */
|
|
15
|
+
export declare function stripElement(doc: Document, namespace: string, width: number, samples: TextSample[], effects: Effect[]): Element;
|
|
16
|
+
/** Frees a canvas's backing store now rather than at garbage collection. */
|
|
17
|
+
export declare function releaseCanvas(canvas: HTMLCanvasElement): void;
|
|
18
|
+
/** Reads a scratch canvas and frees it. */
|
|
19
|
+
export declare function readAndRelease(canvas: HTMLCanvasElement): Raster;
|
|
20
|
+
export declare function readRaster(canvas: HTMLCanvasElement): Raster;
|
|
21
|
+
/**
|
|
22
|
+
* The strip below the card, drawn at (0, cardHeight) in CSS pixels: its
|
|
23
|
+
* corners must be green at the output scale, and each font sample must paint
|
|
24
|
+
* (and differ from `serif` where the page said it would). Null when it passes.
|
|
25
|
+
*/
|
|
26
|
+
export declare function checkStrip(raster: Raster, cssWidth: number, cardHeight: number, scale: number, samples: TextSample[], effects?: Effect[], sentinels?: boolean,
|
|
27
|
+
/** How the live page draws a row (a seam for the unit tests). */
|
|
28
|
+
inkOf?: (sample: TextSample, scale: number) => PageInk): string | null;
|
|
29
|
+
/** A box to compare between the real drawing and one with its clip removed (CSS px). */
|
|
30
|
+
export interface CompareRegion {
|
|
31
|
+
x: number;
|
|
32
|
+
y: number;
|
|
33
|
+
width: number;
|
|
34
|
+
height: number;
|
|
35
|
+
radius: number;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Removing a clip may only change what lies outside it. The card is drawn a
|
|
39
|
+
* second time with the clips around its blur effects removed (and room around
|
|
40
|
+
* it so nothing is cut by the image's edge); inside each clip -- away from its
|
|
41
|
+
* edges and rounded corners -- the two drawings must agree. A browser that
|
|
42
|
+
* draws a clipped shadow wrong (WebKit drew one spilling past the card's edge
|
|
43
|
+
* as a hard rectangle down and to the right) disagrees with itself here.
|
|
44
|
+
*/
|
|
45
|
+
export declare function checkUnclipped(real: Raster, unclipped: Raster, regions: CompareRegion[], scale: number, label: string): string | null;
|
|
46
|
+
/** Decodes each bitmap's own data URL (top-level decoding, not inside SVG). */
|
|
47
|
+
export declare function loadBitmapRasters(bitmaps: EmbeddedBitmap[]): Promise<Map<number, Raster>>;
|
|
48
|
+
export interface Drawn {
|
|
49
|
+
real: Raster;
|
|
50
|
+
magenta: Raster;
|
|
51
|
+
black: Raster;
|
|
52
|
+
}
|
|
53
|
+
/** One bitmap shows its own pixels wherever it shows at all. Null when it passes. */
|
|
54
|
+
export declare function checkBitmap(drawn: Drawn, box: EmbeddedBitmap, bitmap: Raster, scale: number, requireChecked?: boolean): string | null;
|
|
55
|
+
/** Every checkable bitmap shows its own pixels wherever it shows at all. Null when it passes. */
|
|
56
|
+
export declare function checkBitmaps(drawn: Drawn, bitmaps: EmbeddedBitmap[], rasters: Map<number, Raster>, scale: number, requireChecked?: boolean): string | null;
|
|
@@ -0,0 +1,578 @@
|
|
|
1
|
+
// The rasterizer's check on its own output: the PNG it is about to return must
|
|
2
|
+
// show what the page showed, or the capture fails with RASTERIZE_MISMATCH.
|
|
3
|
+
// Nothing here depends on which browser is drawing -- it compares pixels.
|
|
4
|
+
//
|
|
5
|
+
// Three checks, all against things known independently of the drawing:
|
|
6
|
+
//
|
|
7
|
+
// - Scale. A green strip is laid out under the card in the same SVG image and
|
|
8
|
+
// its corners must land where the output scale puts them. (WebKit drew the
|
|
9
|
+
// whole image at 1x into the top-left quarter.)
|
|
10
|
+
// - Web fonts. The strip also draws a sample of the card's own text in each
|
|
11
|
+
// embedded web font, above the same text in `serif`. The font line must
|
|
12
|
+
// paint, and where the page measured the font as different from `serif`,
|
|
13
|
+
// the two lines must differ. (WebKit painted no text at all in its web font.)
|
|
14
|
+
// - Effects. For each blur-type effect the card uses (box-shadow, text-shadow,
|
|
15
|
+
// filter blur and drop-shadow, backdrop blur), the strip draws a probe of
|
|
16
|
+
// that effect around a known box: the blur must be there on every side,
|
|
17
|
+
// symmetric, soft and bounded. (WebKit drew a blurred box-shadow as a hard
|
|
18
|
+
// rectangle offset down and right under one geometry.)
|
|
19
|
+
// - Bitmaps. Every canvas snapshot and <img> whose painting is predictable is
|
|
20
|
+
// compared with its own pixels. The card is drawn twice more with each such
|
|
21
|
+
// bitmap replaced by a box of the same geometry: once in a colour that
|
|
22
|
+
// names it (magenta, with the green channel coding which bitmap), once in
|
|
23
|
+
// black. The difference between those two gives, at every point, which
|
|
24
|
+
// bitmap shows there and how much of it (v: 1 unobstructed, 0 covered, in
|
|
25
|
+
// between under a translucent overlay or opacity), and with source-over
|
|
26
|
+
// compositing the real draw must then equal black-draw + v x bitmap. Points
|
|
27
|
+
// the bitmap barely shows through, where another bitmap shows instead, or
|
|
28
|
+
// where the bitmap itself is transparent, are skipped. (WebKit painted none
|
|
29
|
+
// of the data: images.)
|
|
30
|
+
import { pageInk, SAMPLE_FONT_PX } from './textSamples.js';
|
|
31
|
+
import { fail } from './errors.js';
|
|
32
|
+
import { natives } from './instrument.js';
|
|
33
|
+
export const STRIP_COLOR = '#00ff00';
|
|
34
|
+
const ROW = 28;
|
|
35
|
+
const SAMPLE_LEFT = 8;
|
|
36
|
+
const SAMPLE_STRIDE = 2 * ROW;
|
|
37
|
+
const STRIP_PAD = 8;
|
|
38
|
+
const GRID = 10;
|
|
39
|
+
const TOLERANCE = 24;
|
|
40
|
+
const MIN_VISIBILITY = 0.25;
|
|
41
|
+
const MIN_MATCH = 0.9;
|
|
42
|
+
/** How many bitmaps one capture can check: each needs its own green code. */
|
|
43
|
+
export const MAX_CHECKED_BITMAPS = 25;
|
|
44
|
+
/** The colour a bitmap's box takes in the identifying draw. */
|
|
45
|
+
export function bitmapCode(id) {
|
|
46
|
+
return `rgb(255, ${codeGreen(id)}, 255)`;
|
|
47
|
+
}
|
|
48
|
+
function codeGreen(id) {
|
|
49
|
+
return id * 10;
|
|
50
|
+
}
|
|
51
|
+
const EFFECT_ROW = 48;
|
|
52
|
+
// The two sentinels sit one above the other at x=4, 20px square, or as wide as
|
|
53
|
+
// a narrow card leaves room for (down to 4px, whose quadrants are 2px).
|
|
54
|
+
const SENTINEL_LEFT = 4;
|
|
55
|
+
const SENTINEL_GAP = 4;
|
|
56
|
+
function sentinelSize(width) {
|
|
57
|
+
return Math.max(4, Math.min(20, Math.floor(width - 2 * SENTINEL_LEFT)));
|
|
58
|
+
}
|
|
59
|
+
function sentinelRow(width) {
|
|
60
|
+
return 2 * sentinelSize(width) + SENTINEL_GAP + 8;
|
|
61
|
+
}
|
|
62
|
+
// Quadrant colours of the sentinel image, none of them the strip's green.
|
|
63
|
+
const SENTINEL_COLORS = [
|
|
64
|
+
[255, 0, 0],
|
|
65
|
+
[0, 0, 255],
|
|
66
|
+
[255, 255, 0],
|
|
67
|
+
[255, 0, 255],
|
|
68
|
+
];
|
|
69
|
+
let sentinelUrl = null;
|
|
70
|
+
/**
|
|
71
|
+
* A 2x2 PNG, drawn in the strip as an <img> and as a CSS background-image on
|
|
72
|
+
* every draw: WebKit has been seen to paint a foreignObject's data: images
|
|
73
|
+
* only on a later draw, and nothing else checks backgrounds.
|
|
74
|
+
*/
|
|
75
|
+
function sentinelDataUrl() {
|
|
76
|
+
if (sentinelUrl)
|
|
77
|
+
return sentinelUrl;
|
|
78
|
+
const canvas = document.createElement('canvas');
|
|
79
|
+
canvas.width = 2;
|
|
80
|
+
canvas.height = 2;
|
|
81
|
+
const context = Reflect.apply(natives.getContext, canvas, [
|
|
82
|
+
'2d',
|
|
83
|
+
]);
|
|
84
|
+
if (!context)
|
|
85
|
+
fail('RASTERIZE_FAILED', 'no 2D context for the check image');
|
|
86
|
+
const pixels = new ImageData(2, 2);
|
|
87
|
+
SENTINEL_COLORS.forEach((color, index) => pixels.data.set([...color, 255], index * 4));
|
|
88
|
+
context.putImageData(pixels, 0, 0);
|
|
89
|
+
sentinelUrl = canvas.toDataURL('image/png');
|
|
90
|
+
canvas.width = 0;
|
|
91
|
+
canvas.height = 0;
|
|
92
|
+
return sentinelUrl;
|
|
93
|
+
}
|
|
94
|
+
export function stripHeight(samples, effects, width) {
|
|
95
|
+
return (STRIP_PAD * 2 +
|
|
96
|
+
samples.length * SAMPLE_STRIDE +
|
|
97
|
+
effects.length * EFFECT_ROW +
|
|
98
|
+
sentinelRow(width));
|
|
99
|
+
}
|
|
100
|
+
function sentinelTop(samples, effects) {
|
|
101
|
+
return STRIP_PAD + samples.length * SAMPLE_STRIDE + effects.length * EFFECT_ROW + 4;
|
|
102
|
+
}
|
|
103
|
+
const SENTINELS = ['img', 'background-image'];
|
|
104
|
+
function sentinelBox(index, top, width) {
|
|
105
|
+
const size = sentinelSize(width);
|
|
106
|
+
return { left: SENTINEL_LEFT, top: top + index * (size + SENTINEL_GAP), size };
|
|
107
|
+
}
|
|
108
|
+
function sentinelElements(doc, namespace, top, width) {
|
|
109
|
+
const url = sentinelDataUrl();
|
|
110
|
+
return SENTINELS.map((kind, index) => {
|
|
111
|
+
const place = sentinelBox(index, top, width);
|
|
112
|
+
const element = doc.createElementNS(namespace, kind === 'img' ? 'img' : 'div');
|
|
113
|
+
const box = `position:absolute;margin:0;left:${place.left}px;top:${place.top}px;width:${place.size}px;height:${place.size}px;image-rendering:pixelated`;
|
|
114
|
+
if (kind === 'img') {
|
|
115
|
+
element.setAttribute('src', url);
|
|
116
|
+
element.setAttribute('alt', '');
|
|
117
|
+
element.setAttribute('style', `${box};display:block`);
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
element.setAttribute('style', `${box};background-image:url("${url}");background-size:100% 100%`);
|
|
121
|
+
}
|
|
122
|
+
return element;
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
// Each quadrant of each sentinel must show its colour.
|
|
126
|
+
function checkSentinels(raster, top, scale, width) {
|
|
127
|
+
for (const [which, kind] of SENTINELS.entries()) {
|
|
128
|
+
const place = sentinelBox(which, top, width);
|
|
129
|
+
for (const [index, color] of SENTINEL_COLORS.entries()) {
|
|
130
|
+
const x = place.left + (place.size * (index % 2 ? 3 : 1)) / 4;
|
|
131
|
+
const y = place.top + (place.size * (index < 2 ? 1 : 3)) / 4;
|
|
132
|
+
const i = at(raster, Math.floor(x * scale), Math.floor(y * scale));
|
|
133
|
+
if (color.some((value, c) => Math.abs(raster.data[i + c] - value) > 40)) {
|
|
134
|
+
return `image: the strip's data: ${kind} was not painted`;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
function effectTop(samples, index) {
|
|
141
|
+
return STRIP_PAD + samples.length * SAMPLE_STRIDE + index * EFFECT_ROW;
|
|
142
|
+
}
|
|
143
|
+
// Each probe is a 40x20 box at (30, top + 14) in its row.
|
|
144
|
+
const PROBE = { left: 30, width: 40, top: 14, height: 20 };
|
|
145
|
+
const PROBE_STYLE = {
|
|
146
|
+
'box-shadow': 'background:#000;box-shadow:0 0 8px #000',
|
|
147
|
+
'drop-shadow': 'background:#000;filter:drop-shadow(0 0 8px #000)',
|
|
148
|
+
'filter-blur': 'background:#000;filter:blur(4px)',
|
|
149
|
+
'text-shadow': 'font:20px/20px monospace;color:#000;text-shadow:0 0 6px #000;text-align:center;white-space:pre;overflow:visible',
|
|
150
|
+
'backdrop-blur': 'background:transparent;backdrop-filter:blur(4px);-webkit-backdrop-filter:blur(4px)',
|
|
151
|
+
};
|
|
152
|
+
function probeElements(doc, namespace, effect, top) {
|
|
153
|
+
const place = (style, box) => {
|
|
154
|
+
const element = doc.createElementNS(namespace, 'div');
|
|
155
|
+
element.setAttribute('style', `position:absolute;margin:0;padding:0;box-sizing:border-box;left:${box.left}px;top:${top + box.top}px;` +
|
|
156
|
+
`width:${box.width}px;height:${box.height}px;${style}`);
|
|
157
|
+
return element;
|
|
158
|
+
};
|
|
159
|
+
const probe = place(PROBE_STYLE[effect], PROBE);
|
|
160
|
+
if (effect === 'text-shadow')
|
|
161
|
+
probe.textContent = '\u2588\u2588';
|
|
162
|
+
// A backdrop blur needs an edge behind it to soften: black up to x=50.
|
|
163
|
+
if (effect === 'backdrop-blur')
|
|
164
|
+
return [place('background:#000', { left: 10, top: 4, width: 40, height: 40 }), probe];
|
|
165
|
+
return [probe];
|
|
166
|
+
}
|
|
167
|
+
/** The strip's markup, in the document the card is serialized into. */
|
|
168
|
+
export function stripElement(doc, namespace, width, samples, effects) {
|
|
169
|
+
const strip = doc.createElementNS(namespace, 'div');
|
|
170
|
+
strip.setAttribute('style', `position:relative;margin:0;width:${width}px;height:${stripHeight(samples, effects, width)}px;overflow:hidden;background:${STRIP_COLOR}`);
|
|
171
|
+
effects.forEach((effect, index) => strip.append(...probeElements(doc, namespace, effect, effectTop(samples, index))));
|
|
172
|
+
strip.append(...sentinelElements(doc, namespace, sentinelTop(samples, effects), width));
|
|
173
|
+
samples.forEach((sample, index) => {
|
|
174
|
+
const faceFamily = `${JSON.stringify(sample.family)}, serif`;
|
|
175
|
+
for (const [line, family] of [
|
|
176
|
+
[0, faceFamily],
|
|
177
|
+
[1, 'serif'],
|
|
178
|
+
]) {
|
|
179
|
+
const row = doc.createElementNS(namespace, 'div');
|
|
180
|
+
const top = STRIP_PAD + index * SAMPLE_STRIDE + line * ROW;
|
|
181
|
+
// Both rows share one strut -- the face's family on the row itself --
|
|
182
|
+
// so the fallback row's glyphs sit on exactly the baseline an
|
|
183
|
+
// unloaded face's would: a face that drew as its fallback matches the
|
|
184
|
+
// fallback row pixel for pixel, not a few pixels off. At the face's own
|
|
185
|
+
// weight, style and stretch.
|
|
186
|
+
row.setAttribute('style', `position:absolute;left:0;top:${top}px;width:${width}px;height:${ROW}px;padding:0 0 0 ${SAMPLE_LEFT}px;box-sizing:border-box;` +
|
|
187
|
+
`font:${SAMPLE_FONT_PX}px/${ROW}px ${faceFamily};font-weight:${sample.weight};font-style:${sample.style};font-stretch:${sample.stretch};color:#000;white-space:pre;overflow:hidden`);
|
|
188
|
+
const text = doc.createElementNS(namespace, 'span');
|
|
189
|
+
text.setAttribute('style', `font-family:${family}`);
|
|
190
|
+
text.textContent = sample.text;
|
|
191
|
+
row.append(text);
|
|
192
|
+
strip.append(row);
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
return strip;
|
|
196
|
+
}
|
|
197
|
+
/** Frees a canvas's backing store now rather than at garbage collection. */
|
|
198
|
+
export function releaseCanvas(canvas) {
|
|
199
|
+
canvas.width = 0;
|
|
200
|
+
canvas.height = 0;
|
|
201
|
+
}
|
|
202
|
+
/** Reads a scratch canvas and frees it. */
|
|
203
|
+
export function readAndRelease(canvas) {
|
|
204
|
+
const raster = readRaster(canvas);
|
|
205
|
+
releaseCanvas(canvas);
|
|
206
|
+
return raster;
|
|
207
|
+
}
|
|
208
|
+
export function readRaster(canvas) {
|
|
209
|
+
const context = Reflect.apply(natives.getContext, canvas, [
|
|
210
|
+
'2d',
|
|
211
|
+
]);
|
|
212
|
+
if (!context)
|
|
213
|
+
fail('RASTERIZE_FAILED', 'no 2D context');
|
|
214
|
+
try {
|
|
215
|
+
return context.getImageData(0, 0, canvas.width, canvas.height);
|
|
216
|
+
}
|
|
217
|
+
catch (error) {
|
|
218
|
+
if (error instanceof DOMException && error.name === 'SecurityError') {
|
|
219
|
+
fail('RASTERIZE_BLOCKED', 'the browser tainted the rasterized capture');
|
|
220
|
+
}
|
|
221
|
+
throw error;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
function at(raster, x, y) {
|
|
225
|
+
const cx = Math.min(raster.width - 1, Math.max(0, x));
|
|
226
|
+
const cy = Math.min(raster.height - 1, Math.max(0, y));
|
|
227
|
+
return (cy * raster.width + cx) * 4;
|
|
228
|
+
}
|
|
229
|
+
function isStripGreen(raster, x, y) {
|
|
230
|
+
const i = at(raster, x, y);
|
|
231
|
+
return (raster.data[i + 1] > 200 &&
|
|
232
|
+
raster.data[i] < 60 &&
|
|
233
|
+
raster.data[i + 2] < 60 &&
|
|
234
|
+
raster.data[i + 3] > 200);
|
|
235
|
+
}
|
|
236
|
+
function inkMap(raster, x0, y0, width, height) {
|
|
237
|
+
const ink = [];
|
|
238
|
+
for (let y = y0; y < y0 + height; y++) {
|
|
239
|
+
for (let x = x0; x < x0 + width; x++)
|
|
240
|
+
ink.push(!isStripGreen(raster, x, y));
|
|
241
|
+
}
|
|
242
|
+
return ink;
|
|
243
|
+
}
|
|
244
|
+
// How many pixels of two rows' ink differ once each is moved to its own ink
|
|
245
|
+
// box's corner: a face drawn as its fallback is the fallback row's ink, only
|
|
246
|
+
// perhaps a few pixels lower (the face's line metrics still set the line),
|
|
247
|
+
// and must not pass as different for that offset alone.
|
|
248
|
+
function alignedDifference(a, b, width, height) {
|
|
249
|
+
const corner = (ink) => {
|
|
250
|
+
let left = width;
|
|
251
|
+
let top = height;
|
|
252
|
+
for (let i = 0; i < ink.length; i++) {
|
|
253
|
+
if (!ink[i])
|
|
254
|
+
continue;
|
|
255
|
+
left = Math.min(left, i % width);
|
|
256
|
+
top = Math.min(top, Math.floor(i / width));
|
|
257
|
+
}
|
|
258
|
+
return [left, top];
|
|
259
|
+
};
|
|
260
|
+
const [ax, ay] = corner(a);
|
|
261
|
+
const [bx, by] = corner(b);
|
|
262
|
+
const at = (ink, x, y) => x >= 0 && y >= 0 && x < width && y < height && ink[y * width + x];
|
|
263
|
+
let differing = 0;
|
|
264
|
+
for (let y = 0; y < height; y++) {
|
|
265
|
+
for (let x = 0; x < width; x++) {
|
|
266
|
+
if (at(a, x + ax, y + ay) !== at(b, x + bx, y + by))
|
|
267
|
+
differing++;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
return differing;
|
|
271
|
+
}
|
|
272
|
+
// The face row's ink must span what the live page measured for that face's
|
|
273
|
+
// sample (clipped to the row), within a pixel or two: a fallback glyph run
|
|
274
|
+
// that happens to differ from the fallback row still spans another width.
|
|
275
|
+
function inkExtentReason(ink, rowWidth, cssWidth, scale, page, sample) {
|
|
276
|
+
if (page.left === null || page.right === null)
|
|
277
|
+
return null;
|
|
278
|
+
let left = -1;
|
|
279
|
+
let right = -1;
|
|
280
|
+
for (let i = 0; i < ink.length; i++) {
|
|
281
|
+
if (!ink[i])
|
|
282
|
+
continue;
|
|
283
|
+
const x = i % rowWidth;
|
|
284
|
+
if (left < 0 || x < left)
|
|
285
|
+
left = x;
|
|
286
|
+
if (x > right)
|
|
287
|
+
right = x;
|
|
288
|
+
}
|
|
289
|
+
if (left < 0)
|
|
290
|
+
return null;
|
|
291
|
+
const expectedLeft = SAMPLE_LEFT * scale + page.left;
|
|
292
|
+
const expectedRight = Math.min(SAMPLE_LEFT * scale + page.right, cssWidth * scale);
|
|
293
|
+
const tolerance = Math.max(2 * scale, 0.03 * (expectedRight - expectedLeft));
|
|
294
|
+
if (Math.abs(left - expectedLeft) > tolerance ||
|
|
295
|
+
Math.abs(right + 1 - expectedRight) > tolerance) {
|
|
296
|
+
return `font: ${sample.family} ${sample.weight} spans ${left}-${right + 1}, the page ${Math.round(expectedLeft)}-${Math.round(expectedRight)}`;
|
|
297
|
+
}
|
|
298
|
+
return null;
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* The strip below the card, drawn at (0, cardHeight) in CSS pixels: its
|
|
302
|
+
* corners must be green at the output scale, and each font sample must paint
|
|
303
|
+
* (and differ from `serif` where the page said it would). Null when it passes.
|
|
304
|
+
*/
|
|
305
|
+
export function checkStrip(raster, cssWidth, cardHeight, scale, samples, effects = [], sentinels = false,
|
|
306
|
+
/** How the live page draws a row (a seam for the unit tests). */
|
|
307
|
+
inkOf = pageInk) {
|
|
308
|
+
const top = cardHeight;
|
|
309
|
+
const bottom = cardHeight + stripHeight(samples, effects, cssWidth);
|
|
310
|
+
const corners = [
|
|
311
|
+
[3, top + 3],
|
|
312
|
+
[cssWidth - 3, top + 3],
|
|
313
|
+
[3, bottom - 3],
|
|
314
|
+
[cssWidth - 3, bottom - 3],
|
|
315
|
+
];
|
|
316
|
+
for (const [x, y] of corners) {
|
|
317
|
+
if (!isStripGreen(raster, Math.floor(x * scale), Math.floor(y * scale))) {
|
|
318
|
+
return `geometry: the strip is not where a ${scale}x drawing puts it (${x},${y})`;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
for (const [index, sample] of samples.entries()) {
|
|
322
|
+
const y = Math.round((top + STRIP_PAD + index * SAMPLE_STRIDE) * scale);
|
|
323
|
+
const height = Math.floor(ROW * scale);
|
|
324
|
+
const width = Math.floor(cssWidth * scale);
|
|
325
|
+
const font = inkMap(raster, 0, y, width, height);
|
|
326
|
+
const fallback = inkMap(raster, 0, y + height, width, height);
|
|
327
|
+
const inked = font.filter(Boolean).length;
|
|
328
|
+
if (inked < 4)
|
|
329
|
+
return `font: nothing painted in ${sample.family}`;
|
|
330
|
+
if (inkOf(sample, scale).distinct) {
|
|
331
|
+
const differing = alignedDifference(font, fallback, width, height);
|
|
332
|
+
if (differing < Math.max(12, inked * 0.05))
|
|
333
|
+
return `font: ${sample.family} ${sample.weight} drew as its fallback`;
|
|
334
|
+
}
|
|
335
|
+
const extent = inkExtentReason(font, width, cssWidth, scale, inkOf(sample, scale), sample);
|
|
336
|
+
if (extent)
|
|
337
|
+
return extent;
|
|
338
|
+
}
|
|
339
|
+
if (sentinels) {
|
|
340
|
+
const reason = checkSentinels(raster, cardHeight + sentinelTop(samples, effects), scale, cssWidth);
|
|
341
|
+
if (reason)
|
|
342
|
+
return reason;
|
|
343
|
+
}
|
|
344
|
+
if (effects.length && cssWidth < 100)
|
|
345
|
+
return 'effect probes: the card is too narrow to probe its effects';
|
|
346
|
+
for (const [index, effect] of effects.entries()) {
|
|
347
|
+
const reason = checkEffect(raster, effect, cardHeight + effectTop(samples, index), scale);
|
|
348
|
+
if (reason)
|
|
349
|
+
return `effect ${effect}: ${reason}`;
|
|
350
|
+
}
|
|
351
|
+
return null;
|
|
352
|
+
}
|
|
353
|
+
// How dark the strip is at a CSS point: 0 on the green, 1 on black.
|
|
354
|
+
function darkness(raster, x, y, scale) {
|
|
355
|
+
const i = at(raster, Math.floor(x * scale), Math.floor(y * scale));
|
|
356
|
+
return (255 - raster.data[i + 1]) / 255;
|
|
357
|
+
}
|
|
358
|
+
function softCount(raster, y, from, to, scale) {
|
|
359
|
+
let soft = 0;
|
|
360
|
+
for (let x = Math.floor(from * scale); x < Math.floor(to * scale); x++) {
|
|
361
|
+
const d = (255 - raster.data[at(raster, x, Math.floor(y * scale)) + 1]) / 255;
|
|
362
|
+
if (d > 0.08 && d < 0.85)
|
|
363
|
+
soft++;
|
|
364
|
+
}
|
|
365
|
+
return soft;
|
|
366
|
+
}
|
|
367
|
+
// A blurred halo around the probe box: present on all four sides, the same on
|
|
368
|
+
// opposite sides (not shifted), fading outward, and gone well past the edge.
|
|
369
|
+
function checkHalo(raster, top, scale) {
|
|
370
|
+
const box = {
|
|
371
|
+
left: PROBE.left,
|
|
372
|
+
right: PROBE.left + PROBE.width,
|
|
373
|
+
top: top + PROBE.top,
|
|
374
|
+
bottom: top + PROBE.top + PROBE.height,
|
|
375
|
+
};
|
|
376
|
+
const cx = (box.left + box.right) / 2;
|
|
377
|
+
const cy = (box.top + box.bottom) / 2;
|
|
378
|
+
const d = (x, y) => darkness(raster, x, y, scale);
|
|
379
|
+
const near = {
|
|
380
|
+
left: d(box.left - 2, cy),
|
|
381
|
+
right: d(box.right + 2, cy),
|
|
382
|
+
top: d(cx, box.top - 2),
|
|
383
|
+
bottom: d(cx, box.bottom + 2),
|
|
384
|
+
};
|
|
385
|
+
if (Object.values(near).some((value) => value < 0.12))
|
|
386
|
+
return `no blur beside the box ${JSON.stringify(near)}`;
|
|
387
|
+
if (Math.abs(near.left - near.right) > 0.2 || Math.abs(near.top - near.bottom) > 0.2) {
|
|
388
|
+
return `blur is lopsided ${JSON.stringify(near)}`;
|
|
389
|
+
}
|
|
390
|
+
if (d(box.left - 6, cy) > near.left + 0.05 || d(box.right + 6, cy) > near.right + 0.05)
|
|
391
|
+
return 'blur does not fade outward';
|
|
392
|
+
if (d(box.left - 14, cy) > 0.1 || d(box.right + 14, cy) > 0.1)
|
|
393
|
+
return 'dark well past the blur';
|
|
394
|
+
return null;
|
|
395
|
+
}
|
|
396
|
+
function checkEffect(raster, effect, top, scale) {
|
|
397
|
+
const cy = top + PROBE.top + PROBE.height / 2;
|
|
398
|
+
if (effect === 'text-shadow') {
|
|
399
|
+
let darkest = 0;
|
|
400
|
+
for (let x = PROBE.left - 10; x < PROBE.left + PROBE.width + 10; x++)
|
|
401
|
+
darkest = Math.max(darkest, darkness(raster, x, cy, scale));
|
|
402
|
+
if (darkest < 0.3)
|
|
403
|
+
return 'no shadow drawn';
|
|
404
|
+
return softCount(raster, cy, PROBE.left - 10, PROBE.left + PROBE.width + 10, scale) < 4
|
|
405
|
+
? 'shadow is not blurred'
|
|
406
|
+
: null;
|
|
407
|
+
}
|
|
408
|
+
if (effect === 'backdrop-blur') {
|
|
409
|
+
// Under the probe the black edge at x=50 is softened; above it, it is not.
|
|
410
|
+
if (softCount(raster, cy, 40, 60, scale) < 3)
|
|
411
|
+
return 'the edge behind it is not blurred';
|
|
412
|
+
return softCount(raster, top + 8, 40, 60, scale) > 2 ? 'blurred outside the probe' : null;
|
|
413
|
+
}
|
|
414
|
+
return checkHalo(raster, top, scale);
|
|
415
|
+
}
|
|
416
|
+
const CLIP_INSET = 2;
|
|
417
|
+
const CLIP_STEP = 2;
|
|
418
|
+
function nearCorner(x, y, region, scale) {
|
|
419
|
+
const reach = (region.radius + CLIP_INSET) * scale;
|
|
420
|
+
const left = region.x * scale;
|
|
421
|
+
const top = region.y * scale;
|
|
422
|
+
const right = (region.x + region.width) * scale;
|
|
423
|
+
const bottom = (region.y + region.height) * scale;
|
|
424
|
+
return (x < left + reach || x > right - reach) && (y < top + reach || y > bottom - reach);
|
|
425
|
+
}
|
|
426
|
+
/**
|
|
427
|
+
* Removing a clip may only change what lies outside it. The card is drawn a
|
|
428
|
+
* second time with the clips around its blur effects removed (and room around
|
|
429
|
+
* it so nothing is cut by the image's edge); inside each clip -- away from its
|
|
430
|
+
* edges and rounded corners -- the two drawings must agree. A browser that
|
|
431
|
+
* draws a clipped shadow wrong (WebKit drew one spilling past the card's edge
|
|
432
|
+
* as a hard rectangle down and to the right) disagrees with itself here.
|
|
433
|
+
*/
|
|
434
|
+
export function checkUnclipped(real, unclipped, regions, scale, label) {
|
|
435
|
+
for (const region of regions) {
|
|
436
|
+
let compared = 0;
|
|
437
|
+
let changed = 0;
|
|
438
|
+
const x0 = Math.ceil((region.x + CLIP_INSET) * scale);
|
|
439
|
+
const x1 = Math.floor((region.x + region.width - CLIP_INSET) * scale);
|
|
440
|
+
const y0 = Math.ceil((region.y + CLIP_INSET) * scale);
|
|
441
|
+
const y1 = Math.floor((region.y + region.height - CLIP_INSET) * scale);
|
|
442
|
+
for (let y = y0; y < y1; y += CLIP_STEP) {
|
|
443
|
+
for (let x = x0; x < x1; x += CLIP_STEP) {
|
|
444
|
+
if (nearCorner(x, y, region, scale))
|
|
445
|
+
continue;
|
|
446
|
+
const i = at(real, x, y);
|
|
447
|
+
const j = at(unclipped, x, y);
|
|
448
|
+
compared++;
|
|
449
|
+
for (let c = 0; c < 3; c++) {
|
|
450
|
+
if (Math.abs(real.data[i + c] - unclipped.data[j + c]) > TOLERANCE) {
|
|
451
|
+
changed++;
|
|
452
|
+
break;
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
if (compared && changed > Math.max(16, compared * 0.003)) {
|
|
458
|
+
return `effect clip: ${label} changed ${changed} of ${compared} sampled pixels when its clip was removed`;
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
return null;
|
|
462
|
+
}
|
|
463
|
+
/** Decodes each bitmap's own data URL (top-level decoding, not inside SVG). */
|
|
464
|
+
export async function loadBitmapRasters(bitmaps) {
|
|
465
|
+
const rasters = new Map();
|
|
466
|
+
for (const bitmap of bitmaps) {
|
|
467
|
+
const image = new Image();
|
|
468
|
+
image.src = bitmap.dataUrl;
|
|
469
|
+
try {
|
|
470
|
+
await image.decode();
|
|
471
|
+
}
|
|
472
|
+
catch {
|
|
473
|
+
// A broken image paints nothing on the page either; nothing to compare.
|
|
474
|
+
continue;
|
|
475
|
+
}
|
|
476
|
+
const canvas = document.createElement('canvas');
|
|
477
|
+
canvas.width = image.naturalWidth;
|
|
478
|
+
canvas.height = image.naturalHeight;
|
|
479
|
+
const context = Reflect.apply(natives.getContext, canvas, [
|
|
480
|
+
'2d',
|
|
481
|
+
]);
|
|
482
|
+
if (!canvas.width || !canvas.height)
|
|
483
|
+
continue;
|
|
484
|
+
if (!context)
|
|
485
|
+
fail('RASTERIZE_FAILED', 'no 2D context to read a bitmap back');
|
|
486
|
+
context.drawImage(image, 0, 0);
|
|
487
|
+
rasters.set(bitmap.id, readAndRelease(canvas));
|
|
488
|
+
}
|
|
489
|
+
return rasters;
|
|
490
|
+
}
|
|
491
|
+
// The bitmap's own colour range in the 3x3 texels around a point, so a point
|
|
492
|
+
// the browser interpolated between neighbours still counts; null where any of
|
|
493
|
+
// them is not opaque (what shows through there is the page, not the bitmap).
|
|
494
|
+
function texelRange(bitmap, u, v) {
|
|
495
|
+
const min = [255, 255, 255];
|
|
496
|
+
const max = [0, 0, 0];
|
|
497
|
+
for (let dy = -1; dy <= 1; dy++) {
|
|
498
|
+
for (let dx = -1; dx <= 1; dx++) {
|
|
499
|
+
const i = at(bitmap, u + dx, v + dy);
|
|
500
|
+
if (bitmap.data[i + 3] < 250)
|
|
501
|
+
return null;
|
|
502
|
+
for (let c = 0; c < 3; c++) {
|
|
503
|
+
min[c] = Math.min(min[c], bitmap.data[i + c]);
|
|
504
|
+
max[c] = Math.max(max[c], bitmap.data[i + c]);
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
return { min, max };
|
|
509
|
+
}
|
|
510
|
+
function checkPoint(drawn, bitmap, box, x, y, scale) {
|
|
511
|
+
const i = at(drawn.real, x, y);
|
|
512
|
+
const visibility = Math.min((drawn.magenta.data[i] - drawn.black.data[i]) / 255, (drawn.magenta.data[i + 2] - drawn.black.data[i + 2]) / 255);
|
|
513
|
+
if (visibility < MIN_VISIBILITY)
|
|
514
|
+
return null;
|
|
515
|
+
const green = (drawn.magenta.data[i + 1] - drawn.black.data[i + 1]) / visibility;
|
|
516
|
+
if (Math.abs(green - codeGreen(box.id)) > 4)
|
|
517
|
+
return null;
|
|
518
|
+
const u = Math.floor((((x + 0.5) / scale - box.x) / box.width) * bitmap.width);
|
|
519
|
+
const v = Math.floor((((y + 0.5) / scale - box.y) / box.height) * bitmap.height);
|
|
520
|
+
const range = texelRange(bitmap, u, v);
|
|
521
|
+
if (!range)
|
|
522
|
+
return null;
|
|
523
|
+
for (let c = 0; c < 3; c++) {
|
|
524
|
+
const base = drawn.black.data[i + c];
|
|
525
|
+
const value = drawn.real.data[i + c];
|
|
526
|
+
if (value < base + visibility * range.min[c] - TOLERANCE ||
|
|
527
|
+
value > base + visibility * range.max[c] + TOLERANCE) {
|
|
528
|
+
return false;
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
return true;
|
|
532
|
+
}
|
|
533
|
+
/** One bitmap shows its own pixels wherever it shows at all. Null when it passes. */
|
|
534
|
+
export function checkBitmap(drawn, box, bitmap, scale, requireChecked = false) {
|
|
535
|
+
let considered = 0;
|
|
536
|
+
let matched = 0;
|
|
537
|
+
// Only where the bitmap is on the card: a canvas or image larger than the
|
|
538
|
+
// card (an overscanned background, a window-sized canvas) is checked over
|
|
539
|
+
// the part the cover shows, never against clamped edge pixels.
|
|
540
|
+
const left = Math.max(box.x, 0);
|
|
541
|
+
const top = Math.max(box.y, 0);
|
|
542
|
+
const width = Math.min(box.x + box.width, drawn.real.width / scale) - left;
|
|
543
|
+
const height = Math.min(box.y + box.height, drawn.real.height / scale) - top;
|
|
544
|
+
if (width <= 0 || height <= 0)
|
|
545
|
+
return null;
|
|
546
|
+
for (let row = 0; row < GRID; row++) {
|
|
547
|
+
for (let col = 0; col < GRID; col++) {
|
|
548
|
+
const x = Math.floor((left + (width * (col + 0.5)) / GRID) * scale);
|
|
549
|
+
const y = Math.floor((top + (height * (row + 0.5)) / GRID) * scale);
|
|
550
|
+
const result = checkPoint(drawn, bitmap, box, x, y, scale);
|
|
551
|
+
if (result === null)
|
|
552
|
+
continue;
|
|
553
|
+
considered++;
|
|
554
|
+
if (result)
|
|
555
|
+
matched++;
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
if (considered >= 4 && matched / considered < MIN_MATCH) {
|
|
559
|
+
return `bitmap ${box.id}: ${matched} of ${considered} visible points show its pixels`;
|
|
560
|
+
}
|
|
561
|
+
// Where an unchecked bitmap is not acceptable (WebKit), one too covered or
|
|
562
|
+
// too transparent to check is refused rather than trusted.
|
|
563
|
+
if (requireChecked && considered < 4)
|
|
564
|
+
return `unverifiable: bitmap ${box.id} shows too little to check`;
|
|
565
|
+
return null;
|
|
566
|
+
}
|
|
567
|
+
/** Every checkable bitmap shows its own pixels wherever it shows at all. Null when it passes. */
|
|
568
|
+
export function checkBitmaps(drawn, bitmaps, rasters, scale, requireChecked = false) {
|
|
569
|
+
for (const box of bitmaps) {
|
|
570
|
+
const bitmap = rasters.get(box.id);
|
|
571
|
+
if (!bitmap)
|
|
572
|
+
continue;
|
|
573
|
+
const reason = checkBitmap(drawn, box, bitmap, scale, requireChecked);
|
|
574
|
+
if (reason)
|
|
575
|
+
return reason;
|
|
576
|
+
}
|
|
577
|
+
return null;
|
|
578
|
+
}
|