@open-artifact/export-kit 0.0.0-stage → 0.2.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.
@@ -0,0 +1,55 @@
1
+ /**
2
+ * The PDF text layer, pure part: words measured on a rendered page
3
+ * (dom/measure.ts) joined into the lines they sit on. A PDF draws each line
4
+ * as an invisible, selectable run over the page's picture, so text can be
5
+ * searched and copied.
6
+ *
7
+ * Coordinates are whatever the words were measured in (slide px, document
8
+ * px); placing lines on a PDF page is up to each editor.
9
+ */
10
+ /** One word as drawn. */
11
+ export interface Word {
12
+ text: string;
13
+ left: number;
14
+ right: number;
15
+ top: number;
16
+ bottom: number;
17
+ /** Font size, px. */
18
+ size: number;
19
+ /** A space follows it in the source. */
20
+ space: boolean;
21
+ }
22
+ /** A line of words. */
23
+ export interface TextLine {
24
+ text: string;
25
+ /** Left edge. */
26
+ x: number;
27
+ /** Top of the line box (its first word's top). */
28
+ top: number;
29
+ /** Bottom of the line box (its lowest word's bottom). */
30
+ bottom: number;
31
+ /** Baseline, from the top. */
32
+ baseline: number;
33
+ /** Font size (the largest of its words). */
34
+ size: number;
35
+ /** Drawn width. */
36
+ width: number;
37
+ }
38
+ /** A link's clickable area. */
39
+ export interface LinkArea {
40
+ href: string;
41
+ x: number;
42
+ y: number;
43
+ w: number;
44
+ h: number;
45
+ }
46
+ /**
47
+ * Joins words in reading order into the lines they sit on. A word continues
48
+ * the current line when its top is within half its font size of the line's
49
+ * top and it starts at most 0.3 em before, and less than 2.5 em after, the
50
+ * line's right edge; a space goes between when the source has one or the
51
+ * gap is wider than 0.15 em. Empty words are skipped.
52
+ */
53
+ export declare function groupLines(words: Iterable<Word>): TextLine[];
54
+ /** True for a web link a PDF keeps: http, https or mailto. */
55
+ export declare function isWebLink(href: string): boolean;
@@ -0,0 +1,48 @@
1
+ /**
2
+ * The PDF text layer, pure part: words measured on a rendered page
3
+ * (dom/measure.ts) joined into the lines they sit on. A PDF draws each line
4
+ * as an invisible, selectable run over the page's picture, so text can be
5
+ * searched and copied.
6
+ *
7
+ * Coordinates are whatever the words were measured in (slide px, document
8
+ * px); placing lines on a PDF page is up to each editor.
9
+ */
10
+ /**
11
+ * Joins words in reading order into the lines they sit on. A word continues
12
+ * the current line when its top is within half its font size of the line's
13
+ * top and it starts at most 0.3 em before, and less than 2.5 em after, the
14
+ * line's right edge; a space goes between when the source has one or the
15
+ * gap is wider than 0.15 em. Empty words are skipped.
16
+ */
17
+ export function groupLines(words) {
18
+ const lines = [];
19
+ let current = null;
20
+ const flush = () => {
21
+ if (current)
22
+ lines.push({ text: current.text, x: current.x, top: current.top, bottom: current.bottom, baseline: current.baseline, size: current.size, width: current.right - current.x });
23
+ current = null;
24
+ };
25
+ for (const word of words) {
26
+ if (!word.text)
27
+ continue;
28
+ const line = current;
29
+ if (line && Math.abs(word.top - line.top) < word.size * 0.5 && word.left >= line.right - word.size * 0.3 && word.left - line.right < word.size * 2.5) {
30
+ const gap = word.left - line.right > word.size * 0.15;
31
+ line.text += (line.space || gap ? ' ' : '') + word.text;
32
+ line.right = Math.max(line.right, word.right);
33
+ line.bottom = Math.max(line.bottom, word.bottom);
34
+ line.space = word.space;
35
+ line.size = Math.max(line.size, word.size);
36
+ continue;
37
+ }
38
+ flush();
39
+ const baseline = word.bottom - (word.bottom - word.top) * 0.2;
40
+ current = { text: word.text, x: word.left, top: word.top, bottom: word.bottom, baseline, size: word.size, width: 0, right: word.right, space: word.space };
41
+ }
42
+ flush();
43
+ return lines;
44
+ }
45
+ /** True for a web link a PDF keeps: http, https or mailto. */
46
+ export function isWebLink(href) {
47
+ return /^(https?:|mailto:)/i.test(href);
48
+ }