@openartifactsai/export-kit 0.0.0-stage → 0.23.0-rc.0
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/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +5 -2
- package/package.json +36 -4
- package/src/canvas-area.d.ts +29 -0
- package/src/canvas-area.js +37 -0
- package/src/capture-safe.d.ts +32 -0
- package/src/capture-safe.js +190 -0
- package/src/capture.d.ts +120 -0
- package/src/capture.js +301 -0
- package/src/content-bottom.d.ts +50 -0
- package/src/content-bottom.js +131 -0
- package/src/document-measure.d.ts +36 -0
- package/src/document-measure.js +240 -0
- package/src/font-embed.d.ts +71 -0
- package/src/font-embed.js +366 -0
- package/src/font-subset.d.ts +65 -0
- package/src/font-subset.js +627 -0
- package/src/fonts.d.ts +57 -0
- package/src/fonts.js +156 -0
- package/src/index.d.ts +40 -0
- package/src/index.js +40 -0
- package/src/measure.d.ts +53 -0
- package/src/measure.js +116 -0
- package/src/outline.d.ts +73 -0
- package/src/outline.js +160 -0
- package/src/page-image.d.ts +104 -0
- package/src/page-image.js +507 -0
- package/src/pagination.d.ts +79 -0
- package/src/pagination.js +193 -0
- package/src/pdf-text.d.ts +31 -0
- package/src/pdf-text.js +45 -0
- package/src/pictures.d.ts +45 -0
- package/src/pictures.js +125 -0
- package/src/text-layer.d.ts +55 -0
- package/src/text-layer.js +48 -0
package/src/fonts.d.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reading font stylesheets for exports that inline their fonts. Pictures
|
|
3
|
+
* drawn from serialized markup cannot load fonts from the network, and an
|
|
4
|
+
* exported page should open offline, so editors inline `@font-face` rules
|
|
5
|
+
* with `data:` URLs. Stylesheets such as Google Fonts' are split by
|
|
6
|
+
* `unicode-range`; these helpers find the blocks that cover the characters
|
|
7
|
+
* a document uses. Pure.
|
|
8
|
+
*/
|
|
9
|
+
/** The `@font-face` blocks of a stylesheet. */
|
|
10
|
+
export declare function fontFaceBlocks(css: string): string[];
|
|
11
|
+
/** The `unicode-range` value of a `@font-face` block, if it has one. */
|
|
12
|
+
export declare function blockUnicodeRange(block: string): string | undefined;
|
|
13
|
+
/** A `unicode-range` as inclusive code point ranges (null: everything). */
|
|
14
|
+
export declare function parseUnicodeRange(value: string | undefined): [number, number][] | null;
|
|
15
|
+
/** True when any of the code points falls in the ranges (always, for null ranges). */
|
|
16
|
+
export declare function rangeCovers(ranges: [number, number][] | null, points: Iterable<number>): boolean;
|
|
17
|
+
/** The descriptors of a `@font-face` block by lower-case name (`font-family`, `src`, …), values as written; a later one replaces an earlier one. */
|
|
18
|
+
export declare function fontFaceDescriptors(block: string): Map<string, string>;
|
|
19
|
+
/** A family name as it compares: no quotes, lower case, single spaces. */
|
|
20
|
+
export declare function familyName(value: string | undefined): string;
|
|
21
|
+
/** What tells one face of a family from another. */
|
|
22
|
+
export interface FaceTraits {
|
|
23
|
+
family?: string;
|
|
24
|
+
weight?: string;
|
|
25
|
+
style?: string;
|
|
26
|
+
unicodeRange?: string;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* One string for a face, the same whether its traits were read from a
|
|
30
|
+
* `@font-face` block or from a `FontFace` object (which spell defaults and
|
|
31
|
+
* keywords differently: `normal` for `400`, `U+0-10FFFF` for no range).
|
|
32
|
+
*/
|
|
33
|
+
export declare function faceKey(traits: FaceTraits): string;
|
|
34
|
+
/** One entry of a `src` descriptor. */
|
|
35
|
+
export interface FontSource {
|
|
36
|
+
/** The entry as written. */
|
|
37
|
+
text: string;
|
|
38
|
+
/** Its `url(…)` as written (`match`) and the address; none for `local(…)`. */
|
|
39
|
+
url?: {
|
|
40
|
+
match: string;
|
|
41
|
+
url: string;
|
|
42
|
+
};
|
|
43
|
+
/** Its `format(…)` hint, lower case, without quotes. */
|
|
44
|
+
format?: string;
|
|
45
|
+
}
|
|
46
|
+
/** The entries of a `src` descriptor, in order. */
|
|
47
|
+
export declare function fontSources(src: string): FontSource[];
|
|
48
|
+
/**
|
|
49
|
+
* Every `url(…)` in a CSS text: the whole match and the address, as written
|
|
50
|
+
* (backslash escapes are kept). A quoted address runs to its closing quote,
|
|
51
|
+
* so it may hold `)`, the other quote and escaped characters; an unquoted one
|
|
52
|
+
* ends at the first quote or `)`.
|
|
53
|
+
*/
|
|
54
|
+
export declare function cssUrls(css: string): {
|
|
55
|
+
match: string;
|
|
56
|
+
url: string;
|
|
57
|
+
}[];
|
package/src/fonts.js
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reading font stylesheets for exports that inline their fonts. Pictures
|
|
3
|
+
* drawn from serialized markup cannot load fonts from the network, and an
|
|
4
|
+
* exported page should open offline, so editors inline `@font-face` rules
|
|
5
|
+
* with `data:` URLs. Stylesheets such as Google Fonts' are split by
|
|
6
|
+
* `unicode-range`; these helpers find the blocks that cover the characters
|
|
7
|
+
* a document uses. Pure.
|
|
8
|
+
*/
|
|
9
|
+
/** The `@font-face` blocks of a stylesheet. */
|
|
10
|
+
export function fontFaceBlocks(css) {
|
|
11
|
+
return css.match(/@font-face\s*\{[^}]*\}/g) ?? [];
|
|
12
|
+
}
|
|
13
|
+
/** The `unicode-range` value of a `@font-face` block, if it has one. */
|
|
14
|
+
export function blockUnicodeRange(block) {
|
|
15
|
+
return /unicode-range\s*:\s*([^;}]+)/i.exec(block)?.[1];
|
|
16
|
+
}
|
|
17
|
+
/** A `unicode-range` as inclusive code point ranges (null: everything). */
|
|
18
|
+
export function parseUnicodeRange(value) {
|
|
19
|
+
if (!value)
|
|
20
|
+
return null;
|
|
21
|
+
const ranges = [];
|
|
22
|
+
for (const part of value.split(',')) {
|
|
23
|
+
const text = part.trim().replace(/^u\+/i, '');
|
|
24
|
+
if (!text)
|
|
25
|
+
continue;
|
|
26
|
+
if (text.includes('?')) {
|
|
27
|
+
ranges.push([parseInt(text.replace(/\?/g, '0'), 16), parseInt(text.replace(/\?/g, 'f'), 16)]);
|
|
28
|
+
}
|
|
29
|
+
else if (text.includes('-')) {
|
|
30
|
+
const [from, to] = text.split('-');
|
|
31
|
+
ranges.push([parseInt(from, 16), parseInt(to, 16)]);
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
const point = parseInt(text, 16);
|
|
35
|
+
ranges.push([point, point]);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return ranges.filter(([a, b]) => Number.isFinite(a) && Number.isFinite(b));
|
|
39
|
+
}
|
|
40
|
+
/** True when any of the code points falls in the ranges (always, for null ranges). */
|
|
41
|
+
export function rangeCovers(ranges, points) {
|
|
42
|
+
if (!ranges)
|
|
43
|
+
return true;
|
|
44
|
+
for (const point of points)
|
|
45
|
+
for (const [from, to] of ranges)
|
|
46
|
+
if (point >= from && point <= to)
|
|
47
|
+
return true;
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
/** Splits `text` at `separator`s that are outside quotes and parentheses (a `data:` URL holds both). */
|
|
51
|
+
function splitOutside(text, separator) {
|
|
52
|
+
const parts = [];
|
|
53
|
+
let depth = 0;
|
|
54
|
+
let quote = '';
|
|
55
|
+
let start = 0;
|
|
56
|
+
for (let i = 0; i < text.length; i++) {
|
|
57
|
+
const c = text[i];
|
|
58
|
+
if (quote) {
|
|
59
|
+
if (c === '\\')
|
|
60
|
+
i++;
|
|
61
|
+
else if (c === quote)
|
|
62
|
+
quote = '';
|
|
63
|
+
}
|
|
64
|
+
else if (c === '"' || c === "'")
|
|
65
|
+
quote = c;
|
|
66
|
+
else if (c === '(')
|
|
67
|
+
depth++;
|
|
68
|
+
else if (c === ')')
|
|
69
|
+
depth = Math.max(0, depth - 1);
|
|
70
|
+
else if (c === separator && depth === 0) {
|
|
71
|
+
parts.push(text.slice(start, i));
|
|
72
|
+
start = i + 1;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
parts.push(text.slice(start));
|
|
76
|
+
return parts.map((part) => part.trim()).filter(Boolean);
|
|
77
|
+
}
|
|
78
|
+
/** The descriptors of a `@font-face` block by lower-case name (`font-family`, `src`, …), values as written; a later one replaces an earlier one. */
|
|
79
|
+
export function fontFaceDescriptors(block) {
|
|
80
|
+
const open = block.indexOf('{');
|
|
81
|
+
const close = block.lastIndexOf('}');
|
|
82
|
+
const descriptors = new Map();
|
|
83
|
+
for (const declaration of splitOutside(block.slice(open + 1, close < 0 ? undefined : close), ';')) {
|
|
84
|
+
const colon = declaration.indexOf(':');
|
|
85
|
+
if (colon <= 0)
|
|
86
|
+
continue;
|
|
87
|
+
const name = declaration.slice(0, colon).trim().toLowerCase();
|
|
88
|
+
descriptors.delete(name);
|
|
89
|
+
descriptors.set(name, declaration.slice(colon + 1).trim());
|
|
90
|
+
}
|
|
91
|
+
return descriptors;
|
|
92
|
+
}
|
|
93
|
+
/** A family name as it compares: no quotes, lower case, single spaces. */
|
|
94
|
+
export function familyName(value) {
|
|
95
|
+
return (value ?? '').trim().replace(/^(['"])(.*)\1$/, '$2').replace(/\s+/g, ' ').toLowerCase();
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* One string for a face, the same whether its traits were read from a
|
|
99
|
+
* `@font-face` block or from a `FontFace` object (which spell defaults and
|
|
100
|
+
* keywords differently: `normal` for `400`, `U+0-10FFFF` for no range).
|
|
101
|
+
*/
|
|
102
|
+
export function faceKey(traits) {
|
|
103
|
+
const weight = (traits.weight ?? '').trim().toLowerCase().split(/\s+/).filter(Boolean).map((token) => (token === 'normal' ? '400' : token === 'bold' ? '700' : token)).join(' ') || '400';
|
|
104
|
+
const style = (traits.style ?? '').trim().toLowerCase().replace(/\s+/g, ' ') || 'normal';
|
|
105
|
+
const ranges = parseUnicodeRange(traits.unicodeRange?.trim() || undefined);
|
|
106
|
+
const range = !ranges || ranges.some(([from, to]) => from <= 0 && to >= 0x10ffff) ? '*' : ranges.map(([from, to]) => `${from}-${to}`).sort().join(',');
|
|
107
|
+
return `${familyName(traits.family)}|${weight}|${style}|${range}`;
|
|
108
|
+
}
|
|
109
|
+
/** The entries of a `src` descriptor, in order. */
|
|
110
|
+
export function fontSources(src) {
|
|
111
|
+
return splitOutside(src, ',').map((text) => {
|
|
112
|
+
const url = cssUrls(text)[0];
|
|
113
|
+
const format = /format\(\s*(['"]?)([^'")]+)\1\s*\)/i.exec(text)?.[2]?.trim().toLowerCase();
|
|
114
|
+
return { text, ...(url ? { url } : {}), ...(format ? { format } : {}) };
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Every `url(…)` in a CSS text: the whole match and the address, as written
|
|
119
|
+
* (backslash escapes are kept). A quoted address runs to its closing quote,
|
|
120
|
+
* so it may hold `)`, the other quote and escaped characters; an unquoted one
|
|
121
|
+
* ends at the first quote or `)`.
|
|
122
|
+
*/
|
|
123
|
+
export function cssUrls(css) {
|
|
124
|
+
const found = [];
|
|
125
|
+
let from = 0;
|
|
126
|
+
for (;;) {
|
|
127
|
+
const start = css.indexOf('url(', from);
|
|
128
|
+
if (start < 0)
|
|
129
|
+
return found;
|
|
130
|
+
const address = urlArgument(css, start + 'url('.length);
|
|
131
|
+
if (address) {
|
|
132
|
+
found.push({ match: css.slice(start, address.end), url: address.url });
|
|
133
|
+
from = address.end;
|
|
134
|
+
}
|
|
135
|
+
else
|
|
136
|
+
from = start + 1;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
/** The address of a `url(` whose argument starts at `at`, and where the call ends; null when it is not well formed. */
|
|
140
|
+
function urlArgument(css, at) {
|
|
141
|
+
const quote = css[at];
|
|
142
|
+
if (quote === '"' || quote === "'") {
|
|
143
|
+
let close = at + 1;
|
|
144
|
+
while (close < css.length && css[close] !== quote)
|
|
145
|
+
close += css[close] === '\\' ? 2 : 1;
|
|
146
|
+
if (close === at + 1 || close >= css.length || css[close + 1] !== ')')
|
|
147
|
+
return null;
|
|
148
|
+
return { url: css.slice(at + 1, close), end: close + 2 };
|
|
149
|
+
}
|
|
150
|
+
let close = at;
|
|
151
|
+
while (close < css.length && css[close] !== ')' && css[close] !== '"' && css[close] !== "'")
|
|
152
|
+
close++;
|
|
153
|
+
if (close === at || css[close] !== ')')
|
|
154
|
+
return null;
|
|
155
|
+
return { url: css.slice(at, close), end: close + 1 };
|
|
156
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @openartifactsai/export-kit: the capture and export code the Slides, Design,
|
|
3
|
+
* Whiteboard and Data editors share in the browser.
|
|
4
|
+
*
|
|
5
|
+
* - capture.ts: DOM to pictures (inlined copies drawn through
|
|
6
|
+
* `<foreignObject>`, or dom-to-svg) and capture size limits;
|
|
7
|
+
* - page-image.ts: a rendered document's viewport as a picture, and an
|
|
8
|
+
* element as standalone HTML with its computed styles inlined;
|
|
9
|
+
* - measure.ts and text-layer.ts: the PDF text layer (words and links
|
|
10
|
+
* measured on a rendered page, words grouped into lines);
|
|
11
|
+
* - document-measure.ts, pagination.ts and pdf-text.ts: a long document
|
|
12
|
+
* measured, cut into paper pages, and its text and links placed on each;
|
|
13
|
+
* - content-bottom.ts: where a rendered page's content ends, above any blank
|
|
14
|
+
* space its frame leaves;
|
|
15
|
+
* - pictures.ts: `data:` URLs, canvas encoding, decoding and shrinking pictures;
|
|
16
|
+
* - outline.ts: CSS outlines, which dom-to-svg leaves out, drawn into its SVG;
|
|
17
|
+
* - canvas-area.ts: how large a canvas captures draw on, lowered once the browser would not make one;
|
|
18
|
+
* - fonts.ts: reading font stylesheets for inlined `@font-face` rules;
|
|
19
|
+
* - font-embed.ts: the web fonts a rendered document uses, inlined for pictures of it;
|
|
20
|
+
* - font-subset.ts: a TrueType font cut down to the ideographs and Hangul a document uses.
|
|
21
|
+
*
|
|
22
|
+
* It never imports an editor or a document model; each editor passes its
|
|
23
|
+
* own limits and policies. The pure modules (text-layer.ts, pagination.ts,
|
|
24
|
+
* pdf-text.ts, fonts.ts, font-subset.ts, canvas-area.ts, outline.ts's
|
|
25
|
+
* geometry and content-bottom.ts's test of one element) load in `node --test`.
|
|
26
|
+
*/
|
|
27
|
+
export * from './canvas-area.js';
|
|
28
|
+
export * from './capture.js';
|
|
29
|
+
export * from './content-bottom.js';
|
|
30
|
+
export * from './document-measure.js';
|
|
31
|
+
export * from './font-embed.js';
|
|
32
|
+
export * from './font-subset.js';
|
|
33
|
+
export * from './fonts.js';
|
|
34
|
+
export * from './measure.js';
|
|
35
|
+
export * from './outline.js';
|
|
36
|
+
export * from './page-image.js';
|
|
37
|
+
export * from './pagination.js';
|
|
38
|
+
export * from './pdf-text.js';
|
|
39
|
+
export * from './pictures.js';
|
|
40
|
+
export * from './text-layer.js';
|
package/src/index.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @openartifactsai/export-kit: the capture and export code the Slides, Design,
|
|
3
|
+
* Whiteboard and Data editors share in the browser.
|
|
4
|
+
*
|
|
5
|
+
* - capture.ts: DOM to pictures (inlined copies drawn through
|
|
6
|
+
* `<foreignObject>`, or dom-to-svg) and capture size limits;
|
|
7
|
+
* - page-image.ts: a rendered document's viewport as a picture, and an
|
|
8
|
+
* element as standalone HTML with its computed styles inlined;
|
|
9
|
+
* - measure.ts and text-layer.ts: the PDF text layer (words and links
|
|
10
|
+
* measured on a rendered page, words grouped into lines);
|
|
11
|
+
* - document-measure.ts, pagination.ts and pdf-text.ts: a long document
|
|
12
|
+
* measured, cut into paper pages, and its text and links placed on each;
|
|
13
|
+
* - content-bottom.ts: where a rendered page's content ends, above any blank
|
|
14
|
+
* space its frame leaves;
|
|
15
|
+
* - pictures.ts: `data:` URLs, canvas encoding, decoding and shrinking pictures;
|
|
16
|
+
* - outline.ts: CSS outlines, which dom-to-svg leaves out, drawn into its SVG;
|
|
17
|
+
* - canvas-area.ts: how large a canvas captures draw on, lowered once the browser would not make one;
|
|
18
|
+
* - fonts.ts: reading font stylesheets for inlined `@font-face` rules;
|
|
19
|
+
* - font-embed.ts: the web fonts a rendered document uses, inlined for pictures of it;
|
|
20
|
+
* - font-subset.ts: a TrueType font cut down to the ideographs and Hangul a document uses.
|
|
21
|
+
*
|
|
22
|
+
* It never imports an editor or a document model; each editor passes its
|
|
23
|
+
* own limits and policies. The pure modules (text-layer.ts, pagination.ts,
|
|
24
|
+
* pdf-text.ts, fonts.ts, font-subset.ts, canvas-area.ts, outline.ts's
|
|
25
|
+
* geometry and content-bottom.ts's test of one element) load in `node --test`.
|
|
26
|
+
*/
|
|
27
|
+
export * from "./canvas-area.js";
|
|
28
|
+
export * from "./capture.js";
|
|
29
|
+
export * from "./content-bottom.js";
|
|
30
|
+
export * from "./document-measure.js";
|
|
31
|
+
export * from "./font-embed.js";
|
|
32
|
+
export * from "./font-subset.js";
|
|
33
|
+
export * from "./fonts.js";
|
|
34
|
+
export * from "./measure.js";
|
|
35
|
+
export * from "./outline.js";
|
|
36
|
+
export * from "./page-image.js";
|
|
37
|
+
export * from "./pagination.js";
|
|
38
|
+
export * from "./pdf-text.js";
|
|
39
|
+
export * from "./pictures.js";
|
|
40
|
+
export * from "./text-layer.js";
|
package/src/measure.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Measuring a rendered page for the PDF text layer: the words drawn, in
|
|
3
|
+
* reading (document) order, and the clickable areas of web links. Lines are
|
|
4
|
+
* grouped from the words by text-layer.ts.
|
|
5
|
+
*
|
|
6
|
+
* The editors measure differently framed pages (a slide offscreen on the
|
|
7
|
+
* editor's own page, an artboard alone in a hidden frame), so where to
|
|
8
|
+
* measure from, how far up "drawn" is decided, which elements hold no text
|
|
9
|
+
* and how a word broken across lines is boxed are options, not guesses.
|
|
10
|
+
*/
|
|
11
|
+
import { type LinkArea, type Word } from './text-layer.js';
|
|
12
|
+
/** Where measures are taken from, and what decides whether something is drawn. */
|
|
13
|
+
export interface MeasureOptions {
|
|
14
|
+
/**
|
|
15
|
+
* `root`: from the root element's top left (a slide offscreen); `client`:
|
|
16
|
+
* client rects as they are (an unscrolled frame, where they are document
|
|
17
|
+
* coordinates).
|
|
18
|
+
*/
|
|
19
|
+
origin: 'root' | 'client';
|
|
20
|
+
/**
|
|
21
|
+
* The topmost element whose styles can hide what it holds (the root, for
|
|
22
|
+
* a slide inside the editor's page); null checks every ancestor.
|
|
23
|
+
*/
|
|
24
|
+
drawnWithin: Element | null;
|
|
25
|
+
}
|
|
26
|
+
/** How words are measured. */
|
|
27
|
+
export interface WordOptions extends MeasureOptions {
|
|
28
|
+
/** Text whose parent element's local name matches is skipped (scripts, styles…). */
|
|
29
|
+
skip?: RegExp;
|
|
30
|
+
/**
|
|
31
|
+
* The box of a word broken across lines, for each piece on one line:
|
|
32
|
+
* `domrect` is a `DOMRect` from the first character's top left to the
|
|
33
|
+
* last character's right, as tall as the tallest character; `extent` runs
|
|
34
|
+
* from the first character's left and top to the last character's right
|
|
35
|
+
* and the lowest character's bottom.
|
|
36
|
+
*/
|
|
37
|
+
brokenWord: 'domrect' | 'extent';
|
|
38
|
+
}
|
|
39
|
+
/** How links are measured. */
|
|
40
|
+
export interface LinkOptions extends MeasureOptions {
|
|
41
|
+
/** Leave out links that are not drawn. */
|
|
42
|
+
drawnOnly: boolean;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Is this element (and every ancestor up to `within`, or to the document
|
|
46
|
+
* root when null) drawn: not `display: none`, `visibility: hidden` or
|
|
47
|
+
* `opacity: 0`? Answers are kept in `cache`.
|
|
48
|
+
*/
|
|
49
|
+
export declare function isDrawn(element: Element, within: Element | null, cache?: Map<Element, boolean>): boolean;
|
|
50
|
+
/** The words drawn under `root`, in reading (document) order. */
|
|
51
|
+
export declare function measureWords(root: Element, options: WordOptions): Word[];
|
|
52
|
+
/** The clickable areas of web links (http, https, mailto) under `root`. */
|
|
53
|
+
export declare function measureLinks(root: Element, options: LinkOptions): LinkArea[];
|
package/src/measure.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Measuring a rendered page for the PDF text layer: the words drawn, in
|
|
3
|
+
* reading (document) order, and the clickable areas of web links. Lines are
|
|
4
|
+
* grouped from the words by text-layer.ts.
|
|
5
|
+
*
|
|
6
|
+
* The editors measure differently framed pages (a slide offscreen on the
|
|
7
|
+
* editor's own page, an artboard alone in a hidden frame), so where to
|
|
8
|
+
* measure from, how far up "drawn" is decided, which elements hold no text
|
|
9
|
+
* and how a word broken across lines is boxed are options, not guesses.
|
|
10
|
+
*/
|
|
11
|
+
import { isWebLink } from "./text-layer.js";
|
|
12
|
+
const styleOf = (element) => element.ownerDocument.defaultView.getComputedStyle(element);
|
|
13
|
+
/**
|
|
14
|
+
* Is this element (and every ancestor up to `within`, or to the document
|
|
15
|
+
* root when null) drawn: not `display: none`, `visibility: hidden` or
|
|
16
|
+
* `opacity: 0`? Answers are kept in `cache`.
|
|
17
|
+
*/
|
|
18
|
+
export function isDrawn(element, within, cache = new Map()) {
|
|
19
|
+
const known = cache.get(element);
|
|
20
|
+
if (known !== undefined)
|
|
21
|
+
return known;
|
|
22
|
+
const style = styleOf(element);
|
|
23
|
+
const own = style.display !== 'none' && style.visibility !== 'hidden' && style.opacity !== '0';
|
|
24
|
+
const result = own && (element === within || !element.parentElement ? true : isDrawn(element.parentElement, within, cache));
|
|
25
|
+
cache.set(element, result);
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
/** The words drawn under `root`, in reading (document) order. */
|
|
29
|
+
export function measureWords(root, options) {
|
|
30
|
+
const doc = root.ownerDocument;
|
|
31
|
+
const view = doc.defaultView;
|
|
32
|
+
const origin = options.origin === 'root' ? root.getBoundingClientRect() : { left: 0, top: 0 };
|
|
33
|
+
const out = [];
|
|
34
|
+
const range = doc.createRange();
|
|
35
|
+
const walker = doc.createTreeWalker(root, NodeFilter.SHOW_TEXT);
|
|
36
|
+
const shown = new Map();
|
|
37
|
+
const info = new Map();
|
|
38
|
+
const grow = options.brokenWord === 'domrect'
|
|
39
|
+
? (box, rect) => new view.DOMRect(box.left, box.top, rect.right - box.left, Math.max(box.bottom - box.top, rect.height))
|
|
40
|
+
: (box, rect) => ({ left: box.left, top: box.top, right: rect.right, bottom: Math.max(box.bottom, rect.bottom) });
|
|
41
|
+
const start = (rect) => (options.brokenWord === 'domrect' ? rect : { left: rect.left, right: rect.right, top: rect.top, bottom: rect.bottom });
|
|
42
|
+
for (let node = walker.nextNode(); node; node = walker.nextNode()) {
|
|
43
|
+
const text = node.nodeValue ?? '';
|
|
44
|
+
if (!text.trim())
|
|
45
|
+
continue;
|
|
46
|
+
const parent = node.parentElement;
|
|
47
|
+
if (!parent || options.skip?.test(parent.localName) || !isDrawn(parent, options.drawnWithin, shown))
|
|
48
|
+
continue;
|
|
49
|
+
let meta = info.get(parent);
|
|
50
|
+
if (!meta) {
|
|
51
|
+
const style = styleOf(parent);
|
|
52
|
+
meta = { size: parseFloat(style.fontSize) || 16, upper: style.textTransform === 'uppercase' };
|
|
53
|
+
info.set(parent, meta);
|
|
54
|
+
}
|
|
55
|
+
const { size, upper } = meta;
|
|
56
|
+
for (const match of text.matchAll(/\S+/g)) {
|
|
57
|
+
const from = match.index;
|
|
58
|
+
const end = from + match[0].length;
|
|
59
|
+
const space = end < text.length;
|
|
60
|
+
range.setStart(node, from);
|
|
61
|
+
range.setEnd(node, end);
|
|
62
|
+
const rects = Array.from(range.getClientRects()).filter((r) => r.width > 0 && r.height > 0);
|
|
63
|
+
const push = (value, rect, trailing) => {
|
|
64
|
+
out.push({
|
|
65
|
+
text: upper ? value.toUpperCase() : value,
|
|
66
|
+
left: rect.left - origin.left,
|
|
67
|
+
right: rect.right - origin.left,
|
|
68
|
+
top: rect.top - origin.top,
|
|
69
|
+
bottom: rect.bottom - origin.top,
|
|
70
|
+
size,
|
|
71
|
+
space: trailing,
|
|
72
|
+
});
|
|
73
|
+
};
|
|
74
|
+
if (rects.length === 1)
|
|
75
|
+
push(match[0], rects[0], space);
|
|
76
|
+
else if (rects.length > 1) {
|
|
77
|
+
// A word broken across lines: split it where its characters land.
|
|
78
|
+
let piece = '';
|
|
79
|
+
let box = null;
|
|
80
|
+
let offset = from;
|
|
81
|
+
for (const char of match[0]) {
|
|
82
|
+
range.setStart(node, offset);
|
|
83
|
+
range.setEnd(node, offset + char.length);
|
|
84
|
+
offset += char.length;
|
|
85
|
+
const rect = range.getBoundingClientRect();
|
|
86
|
+
if (box && Math.abs(rect.top - box.top) > size * 0.5) {
|
|
87
|
+
push(piece, box, false);
|
|
88
|
+
piece = '';
|
|
89
|
+
box = null;
|
|
90
|
+
}
|
|
91
|
+
piece += char;
|
|
92
|
+
box = box ? grow(box, rect) : start(rect);
|
|
93
|
+
}
|
|
94
|
+
if (box)
|
|
95
|
+
push(piece, box, space);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return out;
|
|
100
|
+
}
|
|
101
|
+
/** The clickable areas of web links (http, https, mailto) under `root`. */
|
|
102
|
+
export function measureLinks(root, options) {
|
|
103
|
+
const origin = options.origin === 'root' ? root.getBoundingClientRect() : { left: 0, top: 0 };
|
|
104
|
+
const areas = [];
|
|
105
|
+
const shown = new Map();
|
|
106
|
+
for (const link of Array.from(root.querySelectorAll('a[href]'))) {
|
|
107
|
+
const href = link.getAttribute('href') ?? '';
|
|
108
|
+
if (!isWebLink(href) || (options.drawnOnly && !isDrawn(link, options.drawnWithin, shown)))
|
|
109
|
+
continue;
|
|
110
|
+
for (const rect of Array.from(link.getClientRects())) {
|
|
111
|
+
if (rect.width > 0 && rect.height > 0)
|
|
112
|
+
areas.push({ href, x: rect.left - origin.left, y: rect.top - origin.top, w: rect.width, h: rect.height });
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return areas;
|
|
116
|
+
}
|
package/src/outline.d.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CSS outlines in captures. dom-to-svg draws an element's background and
|
|
3
|
+
* border but not its `outline`, which sits outside the box and takes no room
|
|
4
|
+
* in the layout. {@link drawOutlines} adds each one to the drawing as a
|
|
5
|
+
* stroked `<rect>`, and {@link outlineReach} says how far one sticks out, so
|
|
6
|
+
* the capture of a single element can grow to hold it.
|
|
7
|
+
*
|
|
8
|
+
* The geometry ({@link visibleOutline}, {@link outlineRect}) is pure and
|
|
9
|
+
* loads in `node --test`.
|
|
10
|
+
*/
|
|
11
|
+
/** The computed style properties an outline is read from. */
|
|
12
|
+
export interface OutlineStyle {
|
|
13
|
+
outlineStyle: string;
|
|
14
|
+
outlineWidth: string;
|
|
15
|
+
outlineOffset: string;
|
|
16
|
+
outlineColor: string;
|
|
17
|
+
visibility: string;
|
|
18
|
+
opacity: string;
|
|
19
|
+
borderTopLeftRadius: string;
|
|
20
|
+
}
|
|
21
|
+
/** An outline that draws something. */
|
|
22
|
+
export interface VisibleOutline {
|
|
23
|
+
/** Line width, px. */
|
|
24
|
+
width: number;
|
|
25
|
+
/** Gap between the border box and the line's inner edge, px (negative: inside the box). */
|
|
26
|
+
offset: number;
|
|
27
|
+
color: string;
|
|
28
|
+
dash: 'solid' | 'dashed' | 'dotted';
|
|
29
|
+
}
|
|
30
|
+
/** A box in the drawing's coordinates. */
|
|
31
|
+
export interface OutlineBox {
|
|
32
|
+
x: number;
|
|
33
|
+
y: number;
|
|
34
|
+
width: number;
|
|
35
|
+
height: number;
|
|
36
|
+
}
|
|
37
|
+
/** The outline a computed style draws, or null when it draws none (no style, `auto`, no width, no colour, hidden). */
|
|
38
|
+
export declare function visibleOutline(style: OutlineStyle): VisibleOutline | null;
|
|
39
|
+
/** How far past the border box an outline reaches on each side, px. */
|
|
40
|
+
export declare const outlineReach: (outline: VisibleOutline | null) => number;
|
|
41
|
+
/**
|
|
42
|
+
* The attributes of a `<rect>` that strokes `outline` around `box`: the
|
|
43
|
+
* stroke is centred on the rect's edge, so the rect is the border box grown
|
|
44
|
+
* by the offset and half the width. Its corners follow the box's top left
|
|
45
|
+
* radius, grown as much. Null when the rect would have no area.
|
|
46
|
+
*/
|
|
47
|
+
export declare function outlineRect(box: OutlineBox, outline: VisibleOutline, style: Pick<OutlineStyle, 'borderTopLeftRadius' | 'opacity'>): Record<string, string> | null;
|
|
48
|
+
/** The outline an element draws (none for a zero-sized one). */
|
|
49
|
+
export declare function elementOutline(element: Element): VisibleOutline | null;
|
|
50
|
+
/** An element under capture that draws an outline, and the name its group gets in dom-to-svg's drawing. */
|
|
51
|
+
interface Outlined {
|
|
52
|
+
element: Element;
|
|
53
|
+
id: string;
|
|
54
|
+
/** The id was lent for the capture and is taken back after it. */
|
|
55
|
+
lent: boolean;
|
|
56
|
+
outline: VisibleOutline;
|
|
57
|
+
box: OutlineBox;
|
|
58
|
+
style: Pick<OutlineStyle, 'borderTopLeftRadius' | 'opacity'>;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Finds the elements under `root` (itself included, shapes inside an `<svg>`
|
|
62
|
+
* left out) that draw an outline, and gives each one without an id a
|
|
63
|
+
* temporary one: dom-to-svg names an element's group by its id, which is how
|
|
64
|
+
* the outline finds its place in the drawing. Call `release` when the
|
|
65
|
+
* drawing is made; it takes the lent ids back.
|
|
66
|
+
*/
|
|
67
|
+
export declare function markOutlines(root: Element): {
|
|
68
|
+
outlined: Outlined[];
|
|
69
|
+
release: () => void;
|
|
70
|
+
};
|
|
71
|
+
/** Draws each outline right after its element's group in an SVG drawing made while they were marked. */
|
|
72
|
+
export declare function drawOutlines(svg: Element, outlined: readonly Outlined[]): void;
|
|
73
|
+
export {};
|