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,16 @@
|
|
|
1
|
+
// A canvas the capture draws into and reads back itself (image checks,
|
|
2
|
+
// font samples), through the getContext taken before any deck code ran.
|
|
3
|
+
import { fail } from './errors.js';
|
|
4
|
+
import { natives } from './instrument.js';
|
|
5
|
+
export function scratchCanvas(width, height, what) {
|
|
6
|
+
const canvas = document.createElement('canvas');
|
|
7
|
+
canvas.width = width;
|
|
8
|
+
canvas.height = height;
|
|
9
|
+
const context = Reflect.apply(natives.getContext, canvas, [
|
|
10
|
+
'2d',
|
|
11
|
+
{ willReadFrequently: true },
|
|
12
|
+
]);
|
|
13
|
+
if (!context)
|
|
14
|
+
fail('RASTERIZE_FAILED', `no 2D context for ${what}`);
|
|
15
|
+
return { canvas, context };
|
|
16
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/** One row of the check strip: card characters drawn in one embedded font. */
|
|
2
|
+
export interface TextSample {
|
|
3
|
+
family: string;
|
|
4
|
+
weight: string;
|
|
5
|
+
style: string;
|
|
6
|
+
/** A font-stretch keyword (canvas takes nothing else), nearest to the card's. */
|
|
7
|
+
stretch: string;
|
|
8
|
+
text: string;
|
|
9
|
+
/** How many distinct loaded faces the row's characters fall in. */
|
|
10
|
+
faces: number;
|
|
11
|
+
}
|
|
12
|
+
type RowFont = Omit<TextSample, 'text' | 'faces'>;
|
|
13
|
+
export declare const SAMPLE_FONT_PX = 20;
|
|
14
|
+
export declare const MAX_SAMPLE_ROWS = 120;
|
|
15
|
+
/** A face's unicode-range as code point intervals ("U+4??" wildcards included). */
|
|
16
|
+
export declare function unicodeRanges(value: string): Array<[number, number]>;
|
|
17
|
+
/** The canvas/CSS font for a row: the face's family first, or serif alone. */
|
|
18
|
+
export declare function sampleFont(sample: RowFont, px: number, face: boolean): string;
|
|
19
|
+
/**
|
|
20
|
+
* The check rows for the card's text in embedded web fonts (see the top of
|
|
21
|
+
* this file). Refuses (TOO_MANY_FONT_FACES) rather than leave a face out.
|
|
22
|
+
*/
|
|
23
|
+
export declare function collectTextSamples(root: Element, families: string[], rowWidth?: number): TextSample[];
|
|
24
|
+
export interface PageInk {
|
|
25
|
+
/** Whether the face draws the row differently from serif (by shape). */
|
|
26
|
+
distinct: boolean;
|
|
27
|
+
/** The face's ink extent, device px from the text origin; null when blank. */
|
|
28
|
+
left: number | null;
|
|
29
|
+
right: number | null;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* How the live page draws a row at the output scale: its shape against serif
|
|
33
|
+
* and its ink extent, with the strip's own notion of ink. Cached per scale.
|
|
34
|
+
*/
|
|
35
|
+
export declare function pageInk(sample: TextSample, scale: number): PageInk;
|
|
36
|
+
export {};
|
|
@@ -0,0 +1,412 @@
|
|
|
1
|
+
// The card's text, sampled for the rasterizer's font check (verify.ts): rows
|
|
2
|
+
// of the card's own characters, each drawn in one embedded web font at a
|
|
3
|
+
// weight, style and stretch the card uses, so every loaded face the card
|
|
4
|
+
// draws with is shown to draw in the copy too.
|
|
5
|
+
//
|
|
6
|
+
// How a character is assigned: for each run of text (an element's own text,
|
|
7
|
+
// and ::before/::after content, each with its own font and text-transform),
|
|
8
|
+
// walk its font-family list and take the first family that actually draws
|
|
9
|
+
// it (the stack from that family draws the cluster differently from the
|
|
10
|
+
// stack after it) -- that is the font the page draws it in. A web font there
|
|
11
|
+
// is sampled with its loaded face whose unicode-range holds the character; a
|
|
12
|
+
// system font there ends the walk (nothing to check); a family without the
|
|
13
|
+
// glyph -- a system font this machine lacks, a web face missing it, a
|
|
14
|
+
// generic with no font behind it -- is passed over, and a generic that draws
|
|
15
|
+
// it ends the walk. Characters are grapheme clusters:
|
|
16
|
+
// emoji sequences whole, others by their base (combining marks removed).
|
|
17
|
+
//
|
|
18
|
+
// Rows: one per loaded face and (weight, style, stretch) the card draws it
|
|
19
|
+
// at -- a face's failure must not hide behind another face's characters in
|
|
20
|
+
// the same row -- with as many of its characters as fit the card's width.
|
|
21
|
+
// Up to 120 rows (the strip grows); only more than 200 loaded faces refuse
|
|
22
|
+
// outright. The page's own rendering of a
|
|
23
|
+
// row (its shape against serif, and its ink extent) is measured when the
|
|
24
|
+
// check runs, at the output scale (pageInk).
|
|
25
|
+
import { trim } from './builtins.js';
|
|
26
|
+
import { fail } from './errors.js';
|
|
27
|
+
import { natives } from './instrument.js';
|
|
28
|
+
import { scratchCanvas } from './scratchCanvas.js';
|
|
29
|
+
import { decodePseudoContent, familyNames, listMarkerText, unquoteFamily, visibleSnapshotElements, } from './validate.js';
|
|
30
|
+
export const SAMPLE_FONT_PX = 20;
|
|
31
|
+
// Rows the strip may hold, and faces past which a card is refused outright.
|
|
32
|
+
export const MAX_SAMPLE_ROWS = 120;
|
|
33
|
+
const MAX_FACES = 200;
|
|
34
|
+
const MAX_ROW_CHARS = 24;
|
|
35
|
+
const STRETCHES = [
|
|
36
|
+
['ultra-condensed', 50],
|
|
37
|
+
['extra-condensed', 62.5],
|
|
38
|
+
['condensed', 75],
|
|
39
|
+
['semi-condensed', 87.5],
|
|
40
|
+
['normal', 100],
|
|
41
|
+
['semi-expanded', 112.5],
|
|
42
|
+
['expanded', 125],
|
|
43
|
+
['extra-expanded', 150],
|
|
44
|
+
['ultra-expanded', 200],
|
|
45
|
+
];
|
|
46
|
+
function stretchKeyword(value) {
|
|
47
|
+
const percent = Number.parseFloat(value);
|
|
48
|
+
if (!Number.isFinite(percent)) {
|
|
49
|
+
return STRETCHES.some(([name]) => name === value) ? value : 'normal';
|
|
50
|
+
}
|
|
51
|
+
let best = STRETCHES[0];
|
|
52
|
+
for (const entry of STRETCHES) {
|
|
53
|
+
if (Math.abs(entry[1] - percent) < Math.abs(best[1] - percent))
|
|
54
|
+
best = entry;
|
|
55
|
+
}
|
|
56
|
+
return best[0];
|
|
57
|
+
}
|
|
58
|
+
/** A face's unicode-range as code point intervals ("U+4??" wildcards included). */
|
|
59
|
+
export function unicodeRanges(value) {
|
|
60
|
+
const ranges = [];
|
|
61
|
+
for (const part of value.split(',')) {
|
|
62
|
+
const token = trim(part).replace(/^u\+/i, '');
|
|
63
|
+
if (!token)
|
|
64
|
+
continue;
|
|
65
|
+
const [low, high] = token.includes('?')
|
|
66
|
+
? [token.replace(/\?/g, '0'), token.replace(/\?/g, 'F')]
|
|
67
|
+
: token.split('-');
|
|
68
|
+
const from = Number.parseInt(low, 16);
|
|
69
|
+
const to = Number.parseInt(high ?? low, 16);
|
|
70
|
+
if (Number.isFinite(from) && Number.isFinite(to))
|
|
71
|
+
ranges.push([from, to]);
|
|
72
|
+
}
|
|
73
|
+
return ranges;
|
|
74
|
+
}
|
|
75
|
+
function inRanges(code, ranges) {
|
|
76
|
+
return ranges.some(([from, to]) => code >= from && code <= to);
|
|
77
|
+
}
|
|
78
|
+
// The text as the page draws it: after text-transform, in the element's
|
|
79
|
+
// language (Turkish i upper-cases to İ).
|
|
80
|
+
function transformed(text, transform, lang) {
|
|
81
|
+
const locale = lang || undefined;
|
|
82
|
+
if (/\buppercase\b/.test(transform))
|
|
83
|
+
text = text.toLocaleUpperCase(locale);
|
|
84
|
+
else if (/\blowercase\b/.test(transform))
|
|
85
|
+
text = text.toLocaleLowerCase(locale);
|
|
86
|
+
else if (/\bcapitalize\b/.test(transform)) {
|
|
87
|
+
text = text.replace(/(^|[\s\p{P}])(\p{L})/gu, (_all, before, letter) => before + letter.toLocaleUpperCase(locale));
|
|
88
|
+
}
|
|
89
|
+
if (/\bfull-width\b/.test(transform)) {
|
|
90
|
+
text = [...text]
|
|
91
|
+
.map((char) => {
|
|
92
|
+
const code = char.codePointAt(0) ?? 0;
|
|
93
|
+
if (code === 0x20)
|
|
94
|
+
return '\u3000';
|
|
95
|
+
return code >= 0x21 && code <= 0x7e ? String.fromCodePoint(code + 0xfee0) : char;
|
|
96
|
+
})
|
|
97
|
+
.join('');
|
|
98
|
+
}
|
|
99
|
+
return text;
|
|
100
|
+
}
|
|
101
|
+
function runOf(element, style, text) {
|
|
102
|
+
const lang = element.closest('[lang]')?.getAttribute('lang') ?? document.documentElement.lang;
|
|
103
|
+
return {
|
|
104
|
+
families: familyNames(style.fontFamily),
|
|
105
|
+
weight: style.fontWeight,
|
|
106
|
+
style: style.fontStyle,
|
|
107
|
+
stretch: stretchKeyword(style.fontStretch),
|
|
108
|
+
text: transformed(text, style.textTransform, lang),
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
// Runs whose list marker type this cannot spell out (checked in
|
|
112
|
+
// collectTextSamples: fine unless the marker would draw in a web font).
|
|
113
|
+
let unknownMarkers = [];
|
|
114
|
+
function textRuns(root) {
|
|
115
|
+
const runs = [];
|
|
116
|
+
unknownMarkers = [];
|
|
117
|
+
for (const element of visibleSnapshotElements(root)) {
|
|
118
|
+
const style = getComputedStyle(element);
|
|
119
|
+
let own = '';
|
|
120
|
+
for (const child of element.childNodes) {
|
|
121
|
+
if (child.nodeType === Node.TEXT_NODE)
|
|
122
|
+
own += child.textContent ?? '';
|
|
123
|
+
}
|
|
124
|
+
// A button input draws its value as text.
|
|
125
|
+
if (element instanceof HTMLInputElement &&
|
|
126
|
+
['button', 'submit', 'reset'].includes(element.type)) {
|
|
127
|
+
own += element.value;
|
|
128
|
+
}
|
|
129
|
+
if (trim(own))
|
|
130
|
+
runs.push(runOf(element, style, own));
|
|
131
|
+
const marker = listMarkerText(element, style);
|
|
132
|
+
if (marker === undefined) {
|
|
133
|
+
unknownMarkers.push(runOf(element, style, ''));
|
|
134
|
+
}
|
|
135
|
+
else if (marker && trim(marker)) {
|
|
136
|
+
runs.push(runOf(element, style, marker));
|
|
137
|
+
}
|
|
138
|
+
for (const pseudo of ['::before', '::after']) {
|
|
139
|
+
const pseudoStyle = getComputedStyle(element, pseudo);
|
|
140
|
+
if (pseudoStyle.display === 'none')
|
|
141
|
+
continue;
|
|
142
|
+
const content = decodePseudoContent(pseudoStyle.content);
|
|
143
|
+
if (content && trim(content))
|
|
144
|
+
runs.push(runOf(element, pseudoStyle, content));
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return runs;
|
|
148
|
+
}
|
|
149
|
+
function loadedFaces(embedded) {
|
|
150
|
+
const faces = new Map();
|
|
151
|
+
let index = 0;
|
|
152
|
+
for (const face of document.fonts) {
|
|
153
|
+
if (face.status !== 'loaded')
|
|
154
|
+
continue;
|
|
155
|
+
const family = unquoteFamily(face.family);
|
|
156
|
+
if (!embedded.has(family))
|
|
157
|
+
continue;
|
|
158
|
+
const list = faces.get(family) ?? [];
|
|
159
|
+
list.push({ ranges: unicodeRanges(face.unicodeRange), index: index++ });
|
|
160
|
+
faces.set(family, list);
|
|
161
|
+
}
|
|
162
|
+
if (index > MAX_FACES) {
|
|
163
|
+
fail('TOO_MANY_FONT_FACES', `the card's fonts have ${index} loaded faces`);
|
|
164
|
+
}
|
|
165
|
+
return faces;
|
|
166
|
+
}
|
|
167
|
+
// Families with any face in the document: a web font whose faces here are
|
|
168
|
+
// all unloaded is passed over by the browser for this character.
|
|
169
|
+
function webFamilies() {
|
|
170
|
+
const names = new Set();
|
|
171
|
+
for (const face of document.fonts)
|
|
172
|
+
names.add(unquoteFamily(face.family));
|
|
173
|
+
return names;
|
|
174
|
+
}
|
|
175
|
+
const COMBINING_MARKS = /\p{M}/gu;
|
|
176
|
+
// Emoji sequences are one glyph: variation selectors, the keycap mark, ZWJ,
|
|
177
|
+
// skin-tone modifiers, tag characters and regional indicators stay with
|
|
178
|
+
// their cluster.
|
|
179
|
+
function isEmojiSequencePart(code) {
|
|
180
|
+
return (code === 0xfe0e ||
|
|
181
|
+
code === 0xfe0f ||
|
|
182
|
+
code === 0x20e3 ||
|
|
183
|
+
code === 0x200d ||
|
|
184
|
+
(code >= 0x1f3fb && code <= 0x1f3ff) ||
|
|
185
|
+
(code >= 0xe0020 && code <= 0xe007f) ||
|
|
186
|
+
(code >= 0x1f1e6 && code <= 0x1f1ff));
|
|
187
|
+
}
|
|
188
|
+
// What a cluster is sampled as: an emoji sequence whole; otherwise its base
|
|
189
|
+
// alone -- a font lacking a combining mark sends the whole cluster to the
|
|
190
|
+
// fallback in page text but not in a canvas, so a cluster with ordinary
|
|
191
|
+
// marks cannot be measured the same way on both sides.
|
|
192
|
+
function sampledCluster(cluster) {
|
|
193
|
+
const emoji = [...cluster].some((char) => isEmojiSequencePart(char.codePointAt(0) ?? 0));
|
|
194
|
+
return emoji ? cluster : cluster.replace(COMBINING_MARKS, '');
|
|
195
|
+
}
|
|
196
|
+
const GENERIC_FAMILIES = new Set([
|
|
197
|
+
'serif',
|
|
198
|
+
'sans-serif',
|
|
199
|
+
'monospace',
|
|
200
|
+
'cursive',
|
|
201
|
+
'fantasy',
|
|
202
|
+
'system-ui',
|
|
203
|
+
'ui-serif',
|
|
204
|
+
'ui-sans-serif',
|
|
205
|
+
'ui-monospace',
|
|
206
|
+
'ui-rounded',
|
|
207
|
+
'emoji',
|
|
208
|
+
'math',
|
|
209
|
+
'fangsong',
|
|
210
|
+
]);
|
|
211
|
+
const drawCache = new Map();
|
|
212
|
+
// The cluster drawn in a font stack (a canvas falls back per cluster just as
|
|
213
|
+
// the page's text does).
|
|
214
|
+
function drawn(stack, run, cluster) {
|
|
215
|
+
const list = stack.length
|
|
216
|
+
? stack
|
|
217
|
+
.map((family) => (GENERIC_FAMILIES.has(family) ? family : JSON.stringify(family)))
|
|
218
|
+
.join(', ')
|
|
219
|
+
: 'serif';
|
|
220
|
+
const key = `${list}|${run.weight}|${run.style}|${cluster}`;
|
|
221
|
+
const cached = drawCache.get(key);
|
|
222
|
+
if (cached)
|
|
223
|
+
return cached;
|
|
224
|
+
const { canvas, context } = scratchCanvas(96, 48, 'a glyph test');
|
|
225
|
+
context.font = `${run.style} ${run.weight} 24px ${list}`;
|
|
226
|
+
context.fillStyle = '#000';
|
|
227
|
+
context.fillText(cluster, 8, 32);
|
|
228
|
+
const data = context.getImageData(0, 0, 96, 48).data;
|
|
229
|
+
canvas.width = 0;
|
|
230
|
+
canvas.height = 0;
|
|
231
|
+
drawCache.set(key, data);
|
|
232
|
+
return data;
|
|
233
|
+
}
|
|
234
|
+
// Whether family `index` of the stack draws this cluster: the stack from it
|
|
235
|
+
// draws differently from the stack after it. A family without the glyph --
|
|
236
|
+
// a system font this machine lacks, a web face missing it though its
|
|
237
|
+
// unicode-range holds it -- changes nothing, and the browser passes it over.
|
|
238
|
+
function drawsCluster(families, index, run, cluster) {
|
|
239
|
+
const from = drawn(families.slice(index), run, cluster);
|
|
240
|
+
const after = drawn(families.slice(index + 1), run, cluster);
|
|
241
|
+
for (let i = 0; i < from.length; i++)
|
|
242
|
+
if (from[i] !== after[i])
|
|
243
|
+
return true;
|
|
244
|
+
return false;
|
|
245
|
+
}
|
|
246
|
+
function graphemes(text) {
|
|
247
|
+
const segmenter = new Intl.Segmenter(undefined, { granularity: 'grapheme' });
|
|
248
|
+
return [...segmenter.segment(text)].map((part) => part.segment);
|
|
249
|
+
}
|
|
250
|
+
let measuring = null;
|
|
251
|
+
function measureWidth(sample, text) {
|
|
252
|
+
measuring ??= Reflect.apply(natives.getContext, document.createElement('canvas'), [
|
|
253
|
+
'2d',
|
|
254
|
+
]);
|
|
255
|
+
if (!measuring)
|
|
256
|
+
return text.length * SAMPLE_FONT_PX;
|
|
257
|
+
measuring.font = sampleFont(sample, SAMPLE_FONT_PX, true);
|
|
258
|
+
measuring.fontStretch = sample.stretch;
|
|
259
|
+
return measuring.measureText(text).width;
|
|
260
|
+
}
|
|
261
|
+
/** The canvas/CSS font for a row: the face's family first, or serif alone. */
|
|
262
|
+
export function sampleFont(sample, px, face) {
|
|
263
|
+
const family = face ? `${JSON.stringify(sample.family)}, serif` : 'serif';
|
|
264
|
+
return `${sample.style} ${sample.weight} ${px}px ${family}`;
|
|
265
|
+
}
|
|
266
|
+
// One row per face: a face's failure must not hide behind another face's
|
|
267
|
+
// characters in the same row. As many of its clusters as fit the width.
|
|
268
|
+
function rowOf(group, rowWidth) {
|
|
269
|
+
let row = '';
|
|
270
|
+
let count = 0;
|
|
271
|
+
for (const cluster of group.clusters.keys()) {
|
|
272
|
+
if (row && (count >= MAX_ROW_CHARS || measureWidth(group.sample, row + cluster) > rowWidth))
|
|
273
|
+
break;
|
|
274
|
+
row += cluster;
|
|
275
|
+
count++;
|
|
276
|
+
}
|
|
277
|
+
return row;
|
|
278
|
+
}
|
|
279
|
+
const SAFE_VALUE = /^[a-z0-9.%+\s-]+$/i;
|
|
280
|
+
/**
|
|
281
|
+
* The check rows for the card's text in embedded web fonts (see the top of
|
|
282
|
+
* this file). Refuses (TOO_MANY_FONT_FACES) rather than leave a face out.
|
|
283
|
+
*/
|
|
284
|
+
export function collectTextSamples(root, families, rowWidth = root.getBoundingClientRect().width - 16) {
|
|
285
|
+
if (!families.length || !document.fonts)
|
|
286
|
+
return [];
|
|
287
|
+
const embedded = new Set(families);
|
|
288
|
+
const faces = loadedFaces(embedded);
|
|
289
|
+
const web = webFamilies();
|
|
290
|
+
const groups = new Map();
|
|
291
|
+
for (const run of textRuns(root)) {
|
|
292
|
+
for (const whole of graphemes(run.text)) {
|
|
293
|
+
const cluster = sampledCluster(whole);
|
|
294
|
+
if (!trim(cluster))
|
|
295
|
+
continue;
|
|
296
|
+
const code = cluster.codePointAt(0) ?? 0;
|
|
297
|
+
for (const [index, family] of run.families.entries()) {
|
|
298
|
+
if (!drawsCluster(run.families, index, run, cluster))
|
|
299
|
+
continue;
|
|
300
|
+
// A generic family that draws it (this machine has a font for it):
|
|
301
|
+
// no web face to check. One it has no font for (ui-rounded, fantasy
|
|
302
|
+
// and the like on many systems) was passed over just above.
|
|
303
|
+
if (GENERIC_FAMILIES.has(family))
|
|
304
|
+
break;
|
|
305
|
+
const face = faces.get(family)?.find((candidate) => inRanges(code, candidate.ranges));
|
|
306
|
+
if (face) {
|
|
307
|
+
const sample = { family, weight: run.weight, style: run.style, stretch: run.stretch };
|
|
308
|
+
if (![sample.weight, sample.style, sample.stretch].every((value) => SAFE_VALUE.test(value))) {
|
|
309
|
+
fail('UNSUPPORTED_FONT_DESCRIPTOR', `text in ${family} has an unusual font setting`);
|
|
310
|
+
}
|
|
311
|
+
// Per face: each loaded face the card draws with gets its own row.
|
|
312
|
+
const key = JSON.stringify([sample, face.index]);
|
|
313
|
+
const group = groups.get(key) ?? { sample, clusters: new Map() };
|
|
314
|
+
if (!group.clusters.has(cluster))
|
|
315
|
+
group.clusters.set(cluster, face.index);
|
|
316
|
+
groups.set(key, group);
|
|
317
|
+
break;
|
|
318
|
+
}
|
|
319
|
+
// A system font that draws it: nothing to check.
|
|
320
|
+
if (!web.has(family))
|
|
321
|
+
break;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
// A marker of a type this cannot spell out, in a stack with an embedded
|
|
326
|
+
// font, might draw in that font unchecked.
|
|
327
|
+
for (const run of unknownMarkers) {
|
|
328
|
+
if (run.families.some((family) => faces.has(family))) {
|
|
329
|
+
fail('UNSUPPORTED_MARKER', `a list marker in ${run.families.join(', ')} has a type the check cannot sample`);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
const samples = [];
|
|
333
|
+
for (const group of groups.values()) {
|
|
334
|
+
samples.push({ ...group.sample, text: rowOf(group, Math.max(40, rowWidth)), faces: 1 });
|
|
335
|
+
}
|
|
336
|
+
if (samples.length > MAX_SAMPLE_ROWS) {
|
|
337
|
+
fail('TOO_MANY_FONT_STYLES', `the card's text uses ${samples.length} face and style combinations; ${MAX_SAMPLE_ROWS} can be checked`);
|
|
338
|
+
}
|
|
339
|
+
return samples;
|
|
340
|
+
}
|
|
341
|
+
// ------------------------------------------------------------ page side
|
|
342
|
+
const INK_ALPHA = 24; // what reads as ink on the strip's green: > ~24/255 cover
|
|
343
|
+
const pageInks = new Map();
|
|
344
|
+
// Laid out at the strip's own font size and drawn scaled, as the copy is
|
|
345
|
+
// (an SVG image scaled up): advances, hinting and optical size come from the
|
|
346
|
+
// 20px layout, not from a font set at the output size.
|
|
347
|
+
function render(sample, scale, face) {
|
|
348
|
+
const px = SAMPLE_FONT_PX;
|
|
349
|
+
const font = sampleFont(sample, px, face);
|
|
350
|
+
const measureCanvas = document.createElement('canvas');
|
|
351
|
+
const measure = Reflect.apply(natives.getContext, measureCanvas, [
|
|
352
|
+
'2d',
|
|
353
|
+
]);
|
|
354
|
+
if (!measure)
|
|
355
|
+
fail('RASTERIZE_FAILED', 'no 2D context for a font sample');
|
|
356
|
+
measure.font = font;
|
|
357
|
+
measure.fontStretch = sample.stretch;
|
|
358
|
+
const metrics = measure.measureText(sample.text);
|
|
359
|
+
const cssWidth = px * 3 +
|
|
360
|
+
Math.max(metrics.width, metrics.actualBoundingBoxRight + metrics.actualBoundingBoxLeft);
|
|
361
|
+
const origin = px; // CSS px
|
|
362
|
+
const width = Math.ceil(cssWidth * scale);
|
|
363
|
+
const height = Math.ceil(px * 3 * scale);
|
|
364
|
+
const { canvas, context } = scratchCanvas(width, height, 'a font sample');
|
|
365
|
+
context.scale(scale, scale);
|
|
366
|
+
context.font = font;
|
|
367
|
+
context.fontStretch = sample.stretch;
|
|
368
|
+
context.fillStyle = '#000';
|
|
369
|
+
context.fillText(sample.text, origin, px * 2);
|
|
370
|
+
const data = context.getImageData(0, 0, width, height).data;
|
|
371
|
+
canvas.width = 0;
|
|
372
|
+
canvas.height = 0;
|
|
373
|
+
const alpha = new Uint8ClampedArray(width * height);
|
|
374
|
+
let left = null;
|
|
375
|
+
let right = null;
|
|
376
|
+
for (let i = 0; i < alpha.length; i++) {
|
|
377
|
+
alpha[i] = data[i * 4 + 3];
|
|
378
|
+
if (alpha[i] <= INK_ALPHA)
|
|
379
|
+
continue;
|
|
380
|
+
const x = i % width;
|
|
381
|
+
if (left === null || x < left)
|
|
382
|
+
left = x;
|
|
383
|
+
if (right === null || x > right)
|
|
384
|
+
right = x;
|
|
385
|
+
}
|
|
386
|
+
const start = origin * scale;
|
|
387
|
+
return {
|
|
388
|
+
alpha,
|
|
389
|
+
left: left === null ? null : left - start,
|
|
390
|
+
right: right === null ? null : right + 1 - start,
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
/**
|
|
394
|
+
* How the live page draws a row at the output scale: its shape against serif
|
|
395
|
+
* and its ink extent, with the strip's own notion of ink. Cached per scale.
|
|
396
|
+
*/
|
|
397
|
+
export function pageInk(sample, scale) {
|
|
398
|
+
const key = `${scale}|${JSON.stringify(sample)}`;
|
|
399
|
+
const cached = pageInks.get(key);
|
|
400
|
+
if (cached)
|
|
401
|
+
return cached;
|
|
402
|
+
const drawn = render(sample, scale, true);
|
|
403
|
+
const fallback = render(sample, scale, false);
|
|
404
|
+
let differing = 0;
|
|
405
|
+
for (let i = 0; i < drawn.alpha.length; i++) {
|
|
406
|
+
if (drawn.alpha[i] > INK_ALPHA !== fallback.alpha[i] > INK_ALPHA)
|
|
407
|
+
differing++;
|
|
408
|
+
}
|
|
409
|
+
const ink = { distinct: differing >= 6 * scale, left: drawn.left, right: drawn.right };
|
|
410
|
+
pageInks.set(key, ink);
|
|
411
|
+
return ink;
|
|
412
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
export declare const HTML_NAMESPACE = "http://www.w3.org/1999/xhtml";
|
|
2
|
+
export declare const SVG_NAMESPACE = "http://www.w3.org/2000/svg";
|
|
3
|
+
export interface UrlToken {
|
|
4
|
+
/** Where the whole url(...) token starts and ends in the value. */
|
|
5
|
+
start: number;
|
|
6
|
+
end: number;
|
|
7
|
+
/** The URL as written, quotes removed. */
|
|
8
|
+
url: string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* The url() tokens in a CSS value, read the way CSS reads them: text inside a
|
|
12
|
+
* string (a local("...") name, a content string) is never taken for a URL, and
|
|
13
|
+
* neither is a function whose name merely ends in "url".
|
|
14
|
+
*/
|
|
15
|
+
export declare function urlTokens(value: string): UrlToken[];
|
|
16
|
+
export declare function parseUrlFunctions(value: string): string[];
|
|
17
|
+
/** The value with url() tokens (from urlTokens, in order) replaced. */
|
|
18
|
+
export declare function replaceUrlTokens(value: string, replacements: Array<[UrlToken, string]>): string;
|
|
19
|
+
/** A comma-separated CSS list, split at its top-level commas only. */
|
|
20
|
+
export declare function splitCssList(value: string): string[];
|
|
21
|
+
export declare function familyNames(value: string): string[];
|
|
22
|
+
/**
|
|
23
|
+
* One family name, unquoted and lower-cased (a FontFace family, an @font-face
|
|
24
|
+
* descriptor). A single name, not a list: Safari reports FontFace.family raw
|
|
25
|
+
* (`It's Font`, no quotes), so only a name wrapped in matching quotes is
|
|
26
|
+
* unquoted, and anything else is taken literally -- an apostrophe inside a name
|
|
27
|
+
* must never fail every capture on the page.
|
|
28
|
+
*/
|
|
29
|
+
export declare function unquoteFamily(name: string): string;
|
|
30
|
+
/** The text a ::before/::after renders, or null when it renders none. */
|
|
31
|
+
export declare function decodePseudoContent(content: string): string | null;
|
|
32
|
+
export declare function elementHasRenderedText(element: Element, style: CSSStyleDeclaration): boolean;
|
|
33
|
+
/** Every element of `root` that paints: subtrees with display:none are skipped. */
|
|
34
|
+
export declare function visibleSnapshotElements(root: Element): Element[];
|
|
35
|
+
/** Whether the card, or any ancestor up to <html>, styles a first letter or line apart. */
|
|
36
|
+
export declare function refuseStyledFirstLines(root: Element): void;
|
|
37
|
+
/**
|
|
38
|
+
* The characters a list item's marker is drawn with (in the item's own
|
|
39
|
+
* font), for the font check: null when it shows no text marker, undefined
|
|
40
|
+
* when its type is one this does not know.
|
|
41
|
+
*/
|
|
42
|
+
export declare function listMarkerText(element: Element, style: CSSStyleDeclaration): string | null | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* A live selection or a CSS Custom Highlight over the card paints on the
|
|
45
|
+
* page, and a copy of the card's markup carries neither: refuse.
|
|
46
|
+
*
|
|
47
|
+
* Known limit: a visited link's :visited style is also not copied, and
|
|
48
|
+
* cannot be detected -- getComputedStyle reports the unvisited style by
|
|
49
|
+
* design, so the cover shows links as unvisited.
|
|
50
|
+
*/
|
|
51
|
+
export declare function refuseHighlights(root: Element): void;
|
|
52
|
+
/** Forgets every copy (each capture fetches its own). */
|
|
53
|
+
export declare function clearSheetCopies(): void;
|
|
54
|
+
export declare function setSheetCopy(href: string, text: string): void;
|
|
55
|
+
/**
|
|
56
|
+
* What display a line-clamped element needs in the copy. Chromium clamps
|
|
57
|
+
* (with an ellipsis) only a vertical -webkit-box, and reports that box's
|
|
58
|
+
* display as flow-root (inline-block when inline): written back as
|
|
59
|
+
* reported, the copy loses the clamp's ellipsis. The reported display
|
|
60
|
+
* cannot tell such a box from an author's own flow-root or inline-block, so
|
|
61
|
+
* the element's declared display decides, read only where it is plain:
|
|
62
|
+
* every declaration a -webkit-box -> restore it; every one a plain other
|
|
63
|
+
* keyword (or none at all) -> keep the reported display; anything else --
|
|
64
|
+
* a mix, var() or a global keyword, nested CSS, @container or @scope, an
|
|
65
|
+
* animation of display, a sheet that cannot be read -> refused, rather than
|
|
66
|
+
* interpreted.
|
|
67
|
+
*/
|
|
68
|
+
export declare function lineClampDisplay(element: Element, style: CSSStyleDeclaration): string | null;
|
|
69
|
+
/** The element's parent in the flattened tree: its slot, its parent, or its shadow host. */
|
|
70
|
+
export declare function flatParent(element: Element): Element | null;
|
|
71
|
+
/** Refuses a card whose ancestors change how it looks as a whole. */
|
|
72
|
+
export declare function refuseAncestorEffects(root: Element): void;
|
|
73
|
+
/**
|
|
74
|
+
* A position:fixed element whose containing block is the viewport is placed
|
|
75
|
+
* relative to it; the copy places it relative to the card. The two agree
|
|
76
|
+
* only when the card fills the viewport from its origin.
|
|
77
|
+
*/
|
|
78
|
+
export declare function refuseViewportFixed(root: Element, elements: Iterable<Element>): void;
|
|
79
|
+
export declare function validateElement(source: Element, label: string): void;
|