Repository navigation
Expand file tree
/
Copy pathreader-layout.mjs
More file actions
142 lines (142 loc) · 4.74 KB
/
Copy pathreader-layout.mjs
File metadata and controls
142 lines (142 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const firstAfter = (items, value, end) => {
let low = 0,
high = items.length;
while (low < high) {
const middle = (low + high) >>> 1;
if (end(items[middle]) <= value) low = middle + 1;
else high = middle;
}
return low;
};
export function buildReaderLayout(
pages,
scale = 1,
direction = 'vertical',
columns = 1,
gap = 24,
border = 2,
) {
const count = direction === 'horizontal' ? 1 : columns,
rows = [],
frames = [];
const rowWidths = [];
if (direction === 'vertical')
for (let start = 0; start < pages.length; start += count) {
const group = pages.slice(start, start + count);
rowWidths.push(
group.reduce((sum, p) => sum + p.width * scale + border, 0) +
Math.max(0, group.length - 1) * gap,
);
}
const layoutWidth = Math.max(0, ...rowWidths);
let offset = 0;
const height =
direction === 'horizontal'
? pages.reduce((max, p) => Math.max(max, p.height * scale + border), 0)
: 0;
for (let start = 0; start < pages.length; start += count) {
const group = pages.slice(start, start + count),
extent =
direction === 'horizontal'
? group[0].width * scale + border
: Math.max(...group.map((p) => p.height * scale + border));
const row = { start, end: start + group.length, offset, extent };
rows.push(row);
let rowX = (layoutWidth - (rowWidths[rows.length - 1] || 0)) / 2;
for (let i = 0; i < group.length; i++) {
const p = group[i],
width = p.width * scale + border,
h = p.height * scale + border;
frames.push({
number: p.number,
row: rows.length - 1,
x: direction === 'horizontal' ? offset : rowX,
y: direction === 'horizontal' ? (height - h) / 2 : offset,
width,
height: h,
});
rowX += width + gap;
}
offset += extent + gap;
}
const length = rows.length ? offset - gap : 0;
return {
direction,
columns: count,
rows,
frames,
width: direction === 'horizontal' ? length : layoutWidth,
height: direction === 'horizontal' ? height : length,
};
}
export function visibleReaderWindow(layout, bounds, active = 1, buffer = 4) {
const horizontal = layout.direction === 'horizontal',
start = horizontal ? bounds.left : bounds.top,
end = horizontal ? bounds.right : bounds.bottom;
const first = firstAfter(layout.rows, start, (row) => row.offset + row.extent),
visible = [];
let last = first;
for (let i = first; i < layout.rows.length && layout.rows[i].offset < end; i++) {
last = i;
for (let n = layout.rows[i].start; n < layout.rows[i].end; n++) {
const f = layout.frames[n];
const w = Math.min(f.x + f.width, bounds.right) - Math.max(f.x, bounds.left),
h = Math.min(f.y + f.height, bounds.bottom) - Math.max(f.y, bounds.top);
if (w > 0 && h > 0)
visible.push({
number: f.number,
ratio: (w * h) / (f.width * f.height),
area: w * h,
row: i,
});
}
}
const fallback = Math.min(
Math.max(0, Math.floor((active - 1) / layout.columns)),
Math.max(0, layout.rows.length - 1),
);
const min = visible.length ? visible[0].row : fallback,
max = visible.length ? visible.at(-1).row : fallback;
const from = Math.max(0, min - buffer),
to = Math.min(layout.rows.length - 1, max + buffer),
numbers = [];
for (let i = from; i <= to; i++)
for (let n = layout.rows[i].start; n < layout.rows[i].end; n++)
numbers.push(layout.frames[n].number);
return { visible, numbers, firstRow: from, lastRow: to };
}
// Thumbnails fit a 128x160 box at the sidebar's default width and grow or
// shrink with a resized sidebar while each page keeps its own aspect ratio.
export function thumbnailFactor(sidebarWidth, defaultWidth) {
return sidebarWidth && defaultWidth ? sidebarWidth / defaultWidth : 1;
}
export function thumbnailScale(page, factor = 1) {
return Math.min((128 * factor) / page.width, (160 * factor) / page.height);
}
export function buildThumbnailLayout(pages, factor) {
let height = 0;
const frames = pages.map((p) => {
const scale = thumbnailScale(p, factor),
item = {
number: p.number,
offset: height,
height: p.height * scale + 40.2,
width: p.width * scale,
imageHeight: p.height * scale,
};
height += item.height + 14;
return item;
});
return { frames, height: Math.max(0, height - 14) };
}
export function visibleThumbnailWindow(layout, top, height, buffer = 200) {
const first = firstAfter(layout.frames, top - buffer, (p) => p.offset + p.height),
items = [];
for (
let i = first;
i < layout.frames.length && layout.frames[i].offset < top + height + buffer;
i++
)
items.push(layout.frames[i]);
return items;
}