Open History to see the last commit
1const PDF_PAGE_GAP = 16;
2export const PDF_MIN_ZOOM = 0.25;
3export const PDF_MAX_ZOOM = 3;
4const PDF_ZOOM_STEP = 0.25;
5
6export function pdfPageWidth(viewportWidth: number, pageWidth: number, zoom: number | null) {
7 const fitWidth = Math.max(1, viewportWidth - 2 * PDF_PAGE_GAP);
8 return {
9 scale: zoom ?? fitWidth / pageWidth,
10 width: zoom === null ? fitWidth : pageWidth * zoom,
11 };
12}
13
14export function stepPdfZoom(scale: number, direction: 1 | -1) {
15 return direction > 0
16 ? Math.min(PDF_MAX_ZOOM, scale + PDF_ZOOM_STEP)
17 : Math.max(PDF_MIN_ZOOM, scale - PDF_ZOOM_STEP);
18}
19
20/** Top offset of each page plus the end of the last one; ratios are height / width. */
21export function pdfPageOffsets(ratios: readonly number[], width: number) {
22 const result = [PDF_PAGE_GAP];
23 for (const ratio of ratios)
24 result.push(result[result.length - 1]! + width * ratio + PDF_PAGE_GAP);
25 return result;
26}
27
28export function currentPdfPage(offsets: readonly number[], pageCount: number, scrollTop: number) {
29 for (let index = 0; index < pageCount; index++)
30 if (offsets[index + 1]! > scrollTop + PDF_PAGE_GAP + 1) return index;
31 return 0;
32}
33
34/** Pages within one viewport above and two below the visible area stay mounted. */
35export function visiblePdfPages(
36 ratios: readonly number[],
37 offsets: readonly number[],
38 scrollTop: number,
39 viewportHeight: number,
40) {
41 return ratios.flatMap((ratio, index) =>
42 offsets[index + 1]! >= scrollTop - viewportHeight &&
43 offsets[index]! <= scrollTop + viewportHeight * 2
44 ? [{ index, ratio }]
45 : [],
46 );
47}
48
49export function pdfPageScrollTop(offsets: readonly number[], index: number) {
50 return Math.max(0, (offsets[index] ?? PDF_PAGE_GAP) - PDF_PAGE_GAP);
51}
52
53export function pdfPageText(
54 items: readonly ({ str: string; hasEOL: boolean } | { type: string })[],
55) {
56 return items.map((item) => ("str" in item ? item.str + (item.hasEOL ? "\n" : "") : "")).join("");
57}
58
59/**
60 * Lowercased search text with each whitespace run, line ends included, read as one space, and a
61 * hyphen that ends a line between letters dropped, as pdf.js's own find does. `owner` maps each
62 * character back to the run it came from.
63 */
64function pdfFindText(runs: readonly string[]) {
65 const chars = runs.flatMap((run, index) =>
66 Array.from(run.toLowerCase(), (char) => ({ char, run: index })),
67 );
68 let text = "";
69 let last = "";
70 const owner: number[] = [];
71 for (let index = 0; index < chars.length; index++) {
72 const { char, run } = chars[index]!;
73 if (/\s/.test(char)) {
74 if (!text.endsWith(" ")) {
75 text += " ";
76 last = " ";
77 owner.push(run);
78 }
79 } else if (
80 char === "-" &&
81 chars[index + 1]?.char === "\n" &&
82 /\p{L}/u.test(last) &&
83 /\p{L}/u.test(chars[index + 2]?.char ?? "")
84 ) {
85 index++;
86 } else {
87 text += char;
88 last = char;
89 for (let unit = 0; unit < char.length; unit++) owner.push(run);
90 }
91 }
92 return { text, owner };
93}
94
95/** Whether a page's text holds the query, under the same rules as `pdfFindMatchRuns`, without
96 * mapping characters back to runs: the page scan checks every page on each query. */
97export function pdfFindIncludes(text: string, query: string) {
98 const needle = query.toLowerCase().replace(/\s+/g, " ");
99 if (!needle.trim()) return false;
100 return text
101 .toLowerCase()
102 .replace(/(\p{L})-\n(?=\p{L})/gu, "$1")
103 .replace(/\s+/g, " ")
104 .includes(needle);
105}
106
107/** Which text runs hold part of a case-insensitive match, when pdf.js splits words across spans. */
108export function pdfFindMatchRuns(runs: readonly string[], query: string) {
109 const { text, owner } = pdfFindText(runs);
110 const needle = query.toLowerCase().replace(/\s+/g, " ");
111 const hits = runs.map(() => false);
112 if (!needle.trim()) return hits;
113 for (
114 let match = text.indexOf(needle);
115 match !== -1;
116 match = text.indexOf(needle, match + needle.length)
117 )
118 for (let index = match; index < match + needle.length; index++) hits[owner[index]!] = true;
119 return hits;
120}