Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

pdfLayout.ts

136 lines4.4 KB

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 };
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);
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;
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;
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 );
48​
49export function pdfPageScrollTop(offsets: readonly number[], index: number) {
50 return Math.max(0, (offsets[index] ?? PDF_PAGE_GAP) - PDF_PAGE_GAP);
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 );
68​
69export function pdfPageText(
70 items: readonly ({ str: string; hasEOL: boolean } | { type: string })[],
71) {
72 return items.map((item) => ("str" in item ? item.str + (item.hasEOL ? "\n" : "") : "")).join("");
74​
75/**
76 * Lowercased search text with each whitespace run, line ends included, read as one space, and a
77 * hyphen that ends a line between letters dropped, as pdf.js's own find does. `owner` maps each
78 * character back to the run it came from.
79 */
80function pdfFindText(runs: readonly string[]) {
81 const chars = runs.flatMap((run, index) =>
82 Array.from(run.toLowerCase(), (char) => ({ char, run: index })),
83 );
84 let text = "";
85 let last = "";
86 const owner: number[] = [];
87 for (let index = 0; index < chars.length; index++) {
88 const { char, run } = chars[index]!;
89 if (/\s/.test(char)) {
90 if (!text.endsWith(" ")) {
91 text += " ";
92 last = " ";
93 owner.push(run);
94 }
95 } else if (
96 char === "-" &&
97 chars[index + 1]?.char === "\n" &&
98 /\p{L}/u.test(last) &&
99 /\p{L}/u.test(chars[index + 2]?.char ?? "")
100 ) {
101 index++;
102 } else {
103 text += char;
104 last = char;
105 for (let unit = 0; unit < char.length; unit++) owner.push(run);
106 }
107 }
108 return { text, owner };
110​
111/** Whether a page's text holds the query, under the same rules as `pdfFindMatchRuns`, without
112 * mapping characters back to runs: the page scan checks every page on each query. */
113export function pdfFindIncludes(text: string, query: string) {
114 const needle = query.toLowerCase().replace(/\s+/g, " ");
115 if (!needle.trim()) return false;
116 return text
117 .toLowerCase()
118 .replace(/(\p{L})-\n(?=\p{L})/gu, "$1")
119 .replace(/\s+/g, " ")
120 .includes(needle);
122​
123/** Which text runs hold part of a case-insensitive match, when pdf.js splits words across spans. */
124export function pdfFindMatchRuns(runs: readonly string[], query: string) {
125 const { text, owner } = pdfFindText(runs);
126 const needle = query.toLowerCase().replace(/\s+/g, " ");
127 const hits = runs.map(() => false);
128 if (!needle.trim()) return hits;
129 for (
130 let match = text.indexOf(needle);
131 match !== -1;
132 match = text.indexOf(needle, match + needle.length)
133 )
134 for (let index = match; index < match + needle.length; index++) hits[owner[index]!] = true;
135 return hits;