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 * 3
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
53/** A saved offset was taken at `savedWidth`; a reloaded document can have fewer or shorter pages. */
54export function pdfRestoredScrollTop(
55 ratios: readonly number[],
56 width: number,
57 view: { page: number; offset: number },
58 savedWidth = width,
59) {
60 const page = Math.min(view.page, ratios.length - 1);
61 const offsets = pdfPageOffsets(ratios, width);
62 return (
63 offsets[page]! -
64 PDF_PAGE_GAP +
65 Math.min((view.offset * width) / savedWidth, Math.max(0, width * ratios[page]! - 1))
66 );
67}
68
69/** Where the place at `from.top` lands after the page width changes from `from.width`. */
70export function pdfResizedScrollTop(
71 ratios: readonly number[],
72 from: { top: number; width: number },
73 width: number,
74) {
75 const offsets = pdfPageOffsets(ratios, from.width);
76 const page = currentPdfPage(offsets, ratios.length, from.top);
77 const offset = from.top - (offsets[page]! - PDF_PAGE_GAP);
78 return pdfRestoredScrollTop(ratios, width, { page, offset }, from.width);
79}
80
81export function pdfPageText(
82 items: readonly ({ str: string; hasEOL: boolean } | { type: string })[],
83) {
84 return items.map((item) => ("str" in item ? item.str + (item.hasEOL ? "\n" : "") : "")).join("");
85}
86
87// A typed straight quote finds a typeset curly one, as pdf.js's own find does. Final sigma folds
88// to σ because lowercasing a whole page turns a word-final Σ into ς, but a lone run's into σ.
89function foldPdfFindCharacters(text: string) {
90 return text
91 .toLowerCase()
92 .replace(/ς/g, "σ")
93 .replace(/[\u2018-\u201b]/g, "'")
94 .replace(/[\u201c-\u201f]/g, '"');
95}
96
97/**
98 * Lowercased search text with each whitespace run, line ends included, read as one space, and a
99 * hyphen that ends a line between letters dropped, as pdf.js's own find does. `owner` maps each
100 * character back to the run it came from.
101 */
102function pdfFindText(runs: readonly string[]) {
103 const chars = runs.flatMap((run, index) =>
104 Array.from(foldPdfFindCharacters(run), (char) => ({ char, run: index })),
105 );
106 let text = "";
107 let last = "";
108 const owner: number[] = [];
109 for (let index = 0; index < chars.length; index++) {
110 const { char, run } = chars[index]!;
111 if (/\s/.test(char)) {
112 if (!text.endsWith(" ")) {
113 text += " ";
114 last = " ";
115 owner.push(run);
116 }
117 } else if (
118 char === "-" &&
119 chars[index + 1]?.char === "\n" &&
120 /\p{L}/u.test(last) &&
121 /\p{L}/u.test(chars[index + 2]?.char ?? "")
122 ) {
123 index++;
124 } else {
125 text += char;
126 last = char;
127 for (let unit = 0; unit < char.length; unit++) owner.push(run);
128 }
129 }
130 return { text, owner };
131}
132
133/** Whether a page's text holds the query, under the same rules as `pdfFindMatchRuns`, without
134 * mapping characters back to runs: the page scan checks every page on each query. */
135export function pdfFindIncludes(text: string, query: string) {
136 const needle = foldPdfFindCharacters(query).replace(/\s+/g, " ");
137 if (!needle.trim()) return false;
138 return foldPdfFindCharacters(text)
139 .replace(/(\p{L})-\n(?=\p{L})/gu, "$1")
140 .replace(/\s+/g, " ")
141 .includes(needle);
142}
143
144/** Which text runs hold part of a case-insensitive match, when pdf.js splits words across spans. */
145export function pdfFindMatchRuns(runs: readonly string[], query: string) {
146 const { text, owner } = pdfFindText(runs);
147 const needle = foldPdfFindCharacters(query).replace(/\s+/g, " ");
148 const hits = runs.map(() => false);
149 if (!needle.trim()) return hits;
150 for (
151 let match = text.indexOf(needle);
152 match !== -1;
153 match = text.indexOf(needle, match + needle.length)
154 )
155 for (let index = match; index < match + needle.length; index++) hits[owner[index]!] = true;
156 return hits;
157}