Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"build": "node scripts/build.mjs",
"check": "node scripts/check-extension.mjs",
"test": "node --test tests/enhancer-setup.test.mjs tests/document-title.test.mjs tests/metadata-doi.test.mjs tests/local-file-favicon.test.mjs tests/page-tab-labels.test.mjs tests/minimap.test.mjs tests/clickable-reference-links.test.mjs tests/printed-page-jump.test.mjs tests/markdown-copy.test.mjs tests/terminal-punctuation-selection.test.mjs tests/footnote-click-copy.test.mjs tests/chatgpt-summary-popup.test.mjs tests/chatgpt-pdf-relay.test.mjs tests/selection-gaps.test.mjs"
"test": "node --test tests/enhancer-setup.test.mjs tests/document-title.test.mjs tests/metadata-doi.test.mjs tests/local-file-favicon.test.mjs tests/page-tab-labels.test.mjs tests/minimap.test.mjs tests/clickable-reference-links.test.mjs tests/printed-page-jump.test.mjs tests/markdown-copy.test.mjs tests/terminal-punctuation-selection.test.mjs tests/footnote-click-copy.test.mjs tests/chatgpt-summary-popup.test.mjs tests/chatgpt-pdf-relay.test.mjs tests/selection-gaps.test.mjs tests/image-gallery.test.mjs"
},
"dependencies": {
"pdfjs-dist": "6.3.289",
Expand Down
2 changes: 2 additions & 0 deletions src/viewer.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<link rel="stylesheet" href="viewer/theme/image-color-toggle.css" />
<link rel="stylesheet" href="viewer/sharing/qr-file-url.css" />
<link rel="stylesheet" href="viewer/annotation-layer.css" />
<link rel="stylesheet" href="viewer/images/image-gallery.css" />
<link rel="stylesheet" href="viewer/navigation/section-cross-reference-panel.css" />
<link rel="stylesheet" href="viewer/navigation/footnote-cross-reference-panel.css" />
</head>
Expand Down Expand Up @@ -327,6 +328,7 @@ <h2 id="qr-file-url-title">Scan file URL</h2>
<script type="module" src="viewer/document-identity.js"></script>
<script type="module" src="viewer/enhance-pdf.js"></script>
<script type="module" src="viewer/viewer.js"></script>
<script type="module" src="viewer/images/image-gallery.js"></script>
<script type="module" src="viewer/navigation/rapid-page-navigation.js"></script>
<script type="module" src="viewer/selection/text-selection.js"></script>
<script type="module" src="viewer/search/search-highlight-cues.js"></script>
Expand Down
38 changes: 38 additions & 0 deletions src/viewer/images/image-gallery-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Coordinates are normalized to the unrotated, rendered PDF page. Each
// image contributes three corners: top-left, bottom-left, and top-right.
const clampUnit = (value) => Math.max(0, Math.min(1, value));

export function imageRegionsFromCoordinates(coordinates, pageWidth, pageHeight, {
minimumSide = 24,
maximumPageCoverage = 0.8,
} = {}) {
if (!coordinates?.length || !pageWidth || !pageHeight) return [];
const regions = [];
for (let offset = 0; offset + 5 < coordinates.length; offset += 6) {
const corners = Array.from(coordinates.slice(offset, offset + 6));
if (!corners.every(Number.isFinite)) continue;
const [ax, ay, bx, by, cx, cy] = corners;
const xs = [ax, bx, cx, bx + cx - ax];
const ys = [ay, by, cy, by + cy - ay];
const x = clampUnit(Math.min(...xs));
const y = clampUnit(Math.min(...ys));
const width = clampUnit(Math.max(...xs)) - x;
const height = clampUnit(Math.max(...ys)) - y;
if (width <= 0 || height <= 0) continue;
if (width * height >= maximumPageCoverage) continue; // scanned page backgrounds
if (width * pageWidth < minimumSide || height * pageHeight < minimumSide) continue;
regions.push({ x, y, width, height });
}
return regions;
}

export function imageCropPixels(region, canvasWidth, canvasHeight) {
const x = Math.max(0, Math.floor(region.x * canvasWidth));
const y = Math.max(0, Math.floor(region.y * canvasHeight));
return {
x,
y,
width: Math.min(canvasWidth - x, Math.max(1, Math.ceil((region.x + region.width) * canvasWidth) - x)),
height: Math.min(canvasHeight - y, Math.max(1, Math.ceil((region.y + region.height) * canvasHeight) - y)),
};
}
99 changes: 99 additions & 0 deletions src/viewer/images/image-gallery.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
.image-gallery-dialog {
width: min(1000px, calc(100vw - 30px));
max-width: none;
max-height: min(92dvh, 1000px);
margin: auto;
padding: 0;
border: 1px solid var(--border);
border-radius: 16px;
color: var(--text);
background: var(--toolbar-bg);
box-shadow: 0 28px 100px rgb(0 0 0 / 40%);
}
.image-gallery-dialog::backdrop { background: rgb(0 0 0 / 76%); backdrop-filter: blur(6px); }
.image-gallery-panel { display: flex; flex-direction: column; height: min(820px, 90dvh); min-height: 320px; }
.image-gallery-header, .image-gallery-footer {
display: flex;
justify-content: space-between;
align-items: center;
gap: 12px;
padding: 12px 18px;
}
.image-gallery-header { border-bottom: 1px solid var(--border); }
.image-gallery-header h2 { font-size: 16px; font-weight: 650; line-height: 1.4; margin: 0; }
.image-gallery-status { color: var(--muted); font-size: 12px; margin: 4px 0 0; }
.image-gallery-close, .image-gallery-previous, .image-gallery-next {
flex: none;
display: grid;
place-items: center;
padding: 0;
border: 0;
border-radius: 9px;
background: var(--control-bg);
color: var(--text);
cursor: pointer;
}
.image-gallery-close { width: 32px; height: 32px; font-size: 25px; }
.image-gallery-previous, .image-gallery-next { width: 38px; height: 56px; font-size: 36px; }
.image-gallery-close:hover, .image-gallery-previous:hover:not(:disabled), .image-gallery-next:hover:not(:disabled), .image-gallery-jump:hover { background: var(--control-hover); }
.image-gallery-dialog button:focus-visible { outline: 2px solid var(--search-focus-border); outline-offset: 2px; }
.image-gallery-dialog button:disabled { opacity: 0.25; cursor: default; }
.image-gallery-stage {
min-height: 0;
flex: 1;
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding: 14px;
}
.image-gallery-picture {
min-height: 0;
flex: 1;
display: grid;
place-items: center;
height: 100%;
overflow: hidden;
background: rgb(120 120 125 / 8%);
border-radius: 10px;
}
.image-gallery-picture img { display: block; width: auto; height: auto; max-width: 100%; max-height: 100%; object-fit: contain; filter: none; }
.image-gallery-empty { max-width: 35ch; color: var(--muted); font-size: 13px; text-align: center; line-height: 1.6; }
.image-gallery-footer { border-top: 1px solid var(--border); font-size: 13px; }
.image-gallery-jump { padding: 8px 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--control-bg); color: var(--text); cursor: pointer; white-space: nowrap; }
.image-gallery-thumbnails {
display: flex;
flex: none;
gap: 8px;
padding: 10px 14px 14px;
min-height: 105px;
max-height: 145px;
overflow-x: auto;
overflow-y: hidden;
border-top: 1px solid var(--border);
overscroll-behavior-inline: contain;
}
.image-gallery-thumb {
display: flex;
flex-direction: column;
justify-content: space-between;
align-items: center;
flex: 0 0 90px;
height: 100px;
gap: 5px;
padding: 5px;
border: 2px solid transparent;
border-radius: 9px;
background: var(--control-bg);
color: var(--muted);
font-size: 11px;
cursor: pointer;
}
.image-gallery-thumb[aria-pressed="true"] { border-color: var(--page-border); color: var(--text); }
.image-gallery-thumb img { max-width: 100%; max-height: 68px; min-height: 0; object-fit: contain; border-radius: 3px; filter: none; }
@media (max-width: 540px) {
.image-gallery-stage { gap: 5px; padding: 6px; }
.image-gallery-previous, .image-gallery-next { width: 29px; height: 45px; font-size: 29px; }
.image-gallery-header, .image-gallery-footer { padding: 10px; }
.image-gallery-dialog { width: calc(100vw - 12px); }
}
Loading
Loading