mirror of
https://github.com/mozilla/pdf.js.git
synced 2026-11-30 16:37:18 +01:00
Group glyphs with the same baseline, font and transform into runs. Infer word spaces from the positions and tracking within each run, and split runs into spans at wide gaps. Compute span advances in the worker from the generated font metrics so the text layer can lay out simple spans without measuring them. Find uses the same extracted items as the text layer.
1343 lines
36 KiB
JavaScript
1343 lines
36 KiB
JavaScript
/* Copyright 2020 Mozilla Foundation
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
import { createCanvas, loadImage } from "@napi-rs/canvas";
|
|
import { mergeCoverageIntoGlobal } from "../coverage_utils.js";
|
|
import os from "os";
|
|
|
|
const isMac = os.platform() === "darwin";
|
|
|
|
/**
|
|
* Decode PNG data into RGBA pixels.
|
|
* @param {Uint8Array} data
|
|
* @returns {Promise<{width: number, height: number, data: Uint8ClampedArray}>}
|
|
*/
|
|
async function decodePNG(data) {
|
|
const image = await loadImage(data);
|
|
const { width, height } = image;
|
|
const ctx = createCanvas(width, height).getContext("2d");
|
|
ctx.drawImage(image, 0, 0);
|
|
return { width, height, data: ctx.getImageData(0, 0, width, height).data };
|
|
}
|
|
|
|
function loadAndWait(filename, selector, zoom, setups, options, viewport) {
|
|
return Promise.all(
|
|
global.integrationSessions.map(async session => {
|
|
const page = await session.browser.newPage();
|
|
settleEditorBeforeInput(page);
|
|
|
|
if (viewport) {
|
|
await page.setViewport(viewport);
|
|
}
|
|
|
|
// In order to avoid errors because of checks which depend on
|
|
// a locale.
|
|
await page.evaluateOnNewDocument(() => {
|
|
Object.defineProperty(navigator, "language", {
|
|
get: () => "en-US",
|
|
});
|
|
Object.defineProperty(navigator, "languages", {
|
|
get: () => ["en-US", "en"],
|
|
});
|
|
});
|
|
|
|
let app_options = "";
|
|
if (options) {
|
|
const optionsObject =
|
|
typeof options === "function"
|
|
? await options(page, session.name)
|
|
: options;
|
|
|
|
// Options must be handled in app.js::_parseHashParams.
|
|
for (const [key, value] of Object.entries(optionsObject || {})) {
|
|
app_options += `&${key}=${encodeURIComponent(value)}`;
|
|
}
|
|
}
|
|
|
|
const fileParam = filename.startsWith("http")
|
|
? filename
|
|
: `/test/pdfs/${filename}`;
|
|
const url = `${global.integrationBaseUrl}?file=${fileParam}#zoom=${zoom ?? "page-fit"}${app_options}`;
|
|
|
|
if (setups) {
|
|
// page.evaluateOnNewDocument allows us to run code before the
|
|
// first js script is executed.
|
|
// The idea here is to set up some setters for PDFViewerApplication
|
|
// and EventBus, so we can inject some code to do whatever we want
|
|
// soon enough especially before the first event in the eventBus is
|
|
// dispatched.
|
|
const { prePageSetup, appSetup, earlySetup, eventBusSetup } = setups;
|
|
await prePageSetup?.(page);
|
|
if (earlySetup || appSetup || eventBusSetup) {
|
|
await page.evaluateOnNewDocument(
|
|
(eaSetup, aSetup, evSetup) => {
|
|
if (eaSetup) {
|
|
// eslint-disable-next-line no-eval
|
|
eval(`(${eaSetup})`)();
|
|
}
|
|
let app;
|
|
let eventBus;
|
|
Object.defineProperty(window, "PDFViewerApplication", {
|
|
get: () => app,
|
|
set(newValue) {
|
|
app = newValue;
|
|
if (aSetup) {
|
|
// eslint-disable-next-line no-eval
|
|
eval(`(${aSetup})`)(app);
|
|
}
|
|
Object.defineProperty(app, "eventBus", {
|
|
get: () => eventBus,
|
|
set(newV) {
|
|
eventBus = newV;
|
|
if (evSetup) {
|
|
// eslint-disable-next-line no-eval
|
|
eval(`(${evSetup})`)(eventBus);
|
|
}
|
|
},
|
|
});
|
|
},
|
|
});
|
|
},
|
|
earlySetup?.toString(),
|
|
appSetup?.toString(),
|
|
eventBusSetup?.toString()
|
|
);
|
|
}
|
|
}
|
|
|
|
await page.goto(url);
|
|
await setups?.postPageSetup?.(page);
|
|
|
|
await page.bringToFront();
|
|
if (selector) {
|
|
await page.waitForSelector(selector, {
|
|
timeout: 0,
|
|
});
|
|
}
|
|
return [session.name, page];
|
|
})
|
|
);
|
|
}
|
|
|
|
function createPromise(page, callback) {
|
|
return page.evaluateHandle(
|
|
// eslint-disable-next-line no-eval
|
|
cb => [new Promise(eval(`(${cb})`))],
|
|
callback.toString()
|
|
);
|
|
}
|
|
|
|
function createPromiseWithArgs(page, callback, args) {
|
|
return page.evaluateHandle(
|
|
// eslint-disable-next-line no-eval, no-shadow
|
|
(cb, args) => [new Promise(eval(`(${cb})`))],
|
|
callback.toString(),
|
|
args
|
|
);
|
|
}
|
|
|
|
function awaitPromise(promise) {
|
|
return promise.evaluate(([p]) => p);
|
|
}
|
|
|
|
function closePages(pages) {
|
|
return Promise.all(pages.map(([_, page]) => closeSinglePage(page)));
|
|
}
|
|
|
|
/**
|
|
* @param {Page} page
|
|
* @returns {Promise<object>} The names of the loaded text layer fonts and of
|
|
* the ones used by the text layer spans.
|
|
*/
|
|
function getTextLayerFonts(page) {
|
|
return page.evaluate(() => {
|
|
const isTextLayerFont = name => /^g_d\d+_tf\d+$/.test(name);
|
|
const loaded = [...document.fonts]
|
|
.map(({ family }) => family.replaceAll('"', ""))
|
|
.filter(isTextLayerFont);
|
|
const used = new Set();
|
|
for (const span of document.querySelectorAll(".textLayer span")) {
|
|
const name = span.style.fontFamily.replaceAll('"', "");
|
|
if (isTextLayerFont(name)) {
|
|
used.add(name);
|
|
}
|
|
}
|
|
return { loaded: loaded.sort(), used: [...used].sort() };
|
|
});
|
|
}
|
|
|
|
async function closeSinglePage(page) {
|
|
const coverage = await page.evaluate(async () => {
|
|
// Close the viewer gracefully, and clear local storage to avoid state
|
|
// leaking from one test to another.
|
|
await window.PDFViewerApplication.testingClose();
|
|
window.localStorage.clear();
|
|
|
|
// Serialize the coverage data to a JSON string because that is a lot
|
|
// faster/cheaper to transfer from the browser to Node.js over the WebDriver
|
|
// BiDi protocol, otherwise Puppeteer's (significantly slower) serialization
|
|
// logic kicks in (see https://github.com/puppeteer/puppeteer/issues/2427).
|
|
return {
|
|
page: window.__coverage__ ? JSON.stringify(window.__coverage__) : null,
|
|
workers: window.__worker_coverage__?.map(c => JSON.stringify(c)) ?? null,
|
|
};
|
|
});
|
|
|
|
if (coverage.page) {
|
|
mergeCoverageIntoGlobal(JSON.parse(coverage.page));
|
|
}
|
|
coverage.workers?.map(c => mergeCoverageIntoGlobal(JSON.parse(c)));
|
|
|
|
await page.close({ runBeforeUnload: false });
|
|
}
|
|
|
|
async function waitForScripting(page) {
|
|
await page.waitForFunction(
|
|
"window.PDFViewerApplication.scriptingReady === true"
|
|
);
|
|
}
|
|
|
|
async function waitForSandboxTrip(page) {
|
|
const handle = await page.evaluateHandle(() => [
|
|
new Promise(resolve => {
|
|
window.addEventListener("sandboxtripend", resolve, { once: true });
|
|
window.PDFViewerApplication.pdfScriptingManager.sandboxTrip();
|
|
}),
|
|
]);
|
|
await awaitPromise(handle);
|
|
}
|
|
|
|
async function waitForDOMMutation(page, callback) {
|
|
return page.evaluateHandle(
|
|
cb => [
|
|
new Promise(resolve => {
|
|
const mutationObserver = new MutationObserver(mutationList => {
|
|
// eslint-disable-next-line no-eval
|
|
if (eval(`(${cb})`)(mutationList)) {
|
|
mutationObserver.disconnect();
|
|
resolve();
|
|
}
|
|
});
|
|
mutationObserver.observe(document, { childList: true, subtree: true });
|
|
}),
|
|
],
|
|
callback.toString()
|
|
);
|
|
}
|
|
|
|
function waitForTimeout(milliseconds) {
|
|
/**
|
|
* Wait for the given number of milliseconds.
|
|
*
|
|
* Note that waiting for an arbitrary time in tests is discouraged because it
|
|
* can easily cause intermittent failures, which is why this functionality is
|
|
* no longer provided by Puppeteer 22+ and we have to implement it ourselves
|
|
* for the remaining callers in the integration tests. We should avoid
|
|
* creating new usages of this function; instead please refer to the better
|
|
* alternatives at https://github.com/puppeteer/puppeteer/pull/11780.
|
|
*/
|
|
return new Promise(resolve => {
|
|
setTimeout(resolve, milliseconds);
|
|
});
|
|
}
|
|
|
|
async function clearInput(page, selector, waitForInputEvent = false) {
|
|
const action = async () => {
|
|
await page.click(selector);
|
|
await kbSelectAll(page);
|
|
await page.keyboard.press("Backspace");
|
|
await page.waitForFunction(
|
|
`document.querySelector('${selector}').value === ""`
|
|
);
|
|
};
|
|
return waitForInputEvent
|
|
? waitForEvent({
|
|
page,
|
|
eventName: "input",
|
|
action,
|
|
selector,
|
|
})
|
|
: action();
|
|
}
|
|
|
|
async function waitAndClick(page, selector, clickOptions = {}) {
|
|
await page.waitForSelector(selector, { visible: true });
|
|
await page.click(selector, clickOptions);
|
|
}
|
|
|
|
function waitForPointerUp(page) {
|
|
return createPromise(page, resolve => {
|
|
window.addEventListener("pointerup", resolve, { once: true });
|
|
});
|
|
}
|
|
|
|
function getSelector(id) {
|
|
return `[data-element-id="${id}"]`;
|
|
}
|
|
|
|
async function getRect(page, selector) {
|
|
await page.waitForSelector(selector, { visible: true });
|
|
return (await page.$(selector)).boundingBox();
|
|
}
|
|
|
|
function getQuerySelector(id) {
|
|
return `document.querySelector('${getSelector(id)}')`;
|
|
}
|
|
|
|
function getComputedStyleSelector(id) {
|
|
return `getComputedStyle(${getQuerySelector(id)})`;
|
|
}
|
|
|
|
function getEditorSelector(n) {
|
|
return `#pdfjs_internal_editor_${n}`;
|
|
}
|
|
|
|
function getAnnotationSelector(id) {
|
|
return `[data-annotation-id="${id}"]`;
|
|
}
|
|
|
|
function getThumbnailSelector(pageNumber) {
|
|
return `.thumbnailImageContainer[data-l10n-args^='{"page":${pageNumber}']`;
|
|
}
|
|
|
|
async function getSpanRectFromText(page, pageNumber, text) {
|
|
await page.waitForSelector(
|
|
`.page[data-page-number="${pageNumber}"] > .textLayer .endOfContent`
|
|
);
|
|
return page.evaluate(
|
|
(number, content) => {
|
|
for (const el of document.querySelectorAll(
|
|
`.page[data-page-number="${number}"] > .textLayer span:not(:has(> span))`
|
|
)) {
|
|
if (el.textContent === content) {
|
|
const { x, y, width, height } = el.getBoundingClientRect();
|
|
return { x, y, width, height };
|
|
}
|
|
}
|
|
return null;
|
|
},
|
|
pageNumber,
|
|
text
|
|
);
|
|
}
|
|
|
|
async function waitForEvent({
|
|
page,
|
|
eventName,
|
|
action,
|
|
selector = null,
|
|
validator = null,
|
|
timeout = 5000,
|
|
}) {
|
|
const handle = await page.evaluateHandle(
|
|
(name, sel, validate, timeOut) => {
|
|
let callback = null,
|
|
timeoutId = null;
|
|
const element = sel ? document.querySelector(sel) : document;
|
|
return [
|
|
Promise.race([
|
|
new Promise(resolve => {
|
|
// The promise is resolved if the event fired in the context of the
|
|
// selector and, if a validator is defined, the event data satisfies
|
|
// the conditions of the validator function.
|
|
callback = e => {
|
|
if (timeoutId) {
|
|
clearTimeout(timeoutId);
|
|
}
|
|
// eslint-disable-next-line no-eval
|
|
resolve(validate ? eval(`(${validate})`)(e) : true);
|
|
};
|
|
element.addEventListener(name, callback, { once: true });
|
|
}),
|
|
new Promise(resolve => {
|
|
timeoutId = setTimeout(() => {
|
|
element.removeEventListener(name, callback);
|
|
resolve(null);
|
|
}, timeOut);
|
|
}),
|
|
]),
|
|
];
|
|
},
|
|
eventName,
|
|
selector,
|
|
validator ? validator.toString() : null,
|
|
timeout
|
|
);
|
|
|
|
await action();
|
|
|
|
const success = await awaitPromise(handle);
|
|
if (success === null) {
|
|
console.warn(
|
|
`waitForEvent: ${eventName} didn't trigger within the timeout`
|
|
);
|
|
} else if (!success) {
|
|
console.warn(`waitForEvent: ${eventName} triggered, but validation failed`);
|
|
}
|
|
}
|
|
|
|
async function countStorageEntries(page) {
|
|
return page.evaluate(
|
|
() => window.PDFViewerApplication.pdfDocument.annotationStorage.size
|
|
);
|
|
}
|
|
|
|
async function waitForStorageEntries(page, nEntries) {
|
|
return page.waitForFunction(
|
|
n => window.PDFViewerApplication.pdfDocument.annotationStorage.size === n,
|
|
{},
|
|
nEntries
|
|
);
|
|
}
|
|
|
|
async function countSerialized(page) {
|
|
return page.evaluate(
|
|
() =>
|
|
window.PDFViewerApplication.pdfDocument.annotationStorage.serializable.map
|
|
?.size ?? 0
|
|
);
|
|
}
|
|
|
|
async function waitForSerialized(page, nEntries) {
|
|
return page.waitForFunction(
|
|
n => {
|
|
try {
|
|
return (
|
|
(window.PDFViewerApplication.pdfDocument.annotationStorage
|
|
.serializable.map?.size ?? 0) === n
|
|
);
|
|
} catch {
|
|
// When serializing a stamp annotation with a SVG, the transfer
|
|
// can fail because of the SVG, so we just retry.
|
|
return false;
|
|
}
|
|
},
|
|
{},
|
|
nEntries
|
|
);
|
|
}
|
|
|
|
async function applyFunctionToEditor(page, editorId, func) {
|
|
return page.evaluate(
|
|
(id, f) => {
|
|
const editor =
|
|
window.PDFViewerApplication.pdfDocument.annotationStorage.getRawValue(
|
|
id
|
|
);
|
|
// eslint-disable-next-line no-eval
|
|
eval(`(${f})`)(editor);
|
|
},
|
|
editorId,
|
|
func.toString()
|
|
);
|
|
}
|
|
|
|
async function selectEditor(page, selector, count = 1) {
|
|
const editorRect = await getRect(page, selector);
|
|
await page.mouse.click(
|
|
editorRect.x + editorRect.width / 2,
|
|
editorRect.y + editorRect.height / 2,
|
|
{ count }
|
|
);
|
|
await waitForSelectedEditor(page, selector);
|
|
}
|
|
|
|
async function waitForSelectedEditor(page, selector) {
|
|
return page.waitForSelector(`${selector}.selectedEditor`);
|
|
}
|
|
|
|
async function unselectEditor(page, selector) {
|
|
await page.keyboard.press("Escape");
|
|
await waitForUnselectedEditor(page, selector);
|
|
}
|
|
|
|
async function waitForUnselectedEditor(page, selector) {
|
|
return page.waitForSelector(`${selector}:not(.selectedEditor)`);
|
|
}
|
|
|
|
async function mockClipboard(pages) {
|
|
return Promise.all(
|
|
pages.map(async ([_, page]) => {
|
|
await page.evaluate(() => {
|
|
let data = null;
|
|
const clipboard = {
|
|
writeText: async text => (data = text),
|
|
readText: async () => data,
|
|
};
|
|
Object.defineProperty(navigator, "clipboard", { value: clipboard });
|
|
});
|
|
})
|
|
);
|
|
}
|
|
|
|
async function copy(page) {
|
|
await waitForEvent({
|
|
page,
|
|
eventName: "copy",
|
|
action: () => kbCopy(page),
|
|
});
|
|
}
|
|
|
|
async function copyToClipboard(page, data) {
|
|
await page.evaluate(async dat => {
|
|
const items = Object.create(null);
|
|
for (const [type, value] of Object.entries(dat)) {
|
|
if (value.startsWith("data:")) {
|
|
const resp = await fetch(value);
|
|
items[type] = await resp.blob();
|
|
} else {
|
|
items[type] = new Blob([value], { type });
|
|
}
|
|
}
|
|
await navigator.clipboard.write([new ClipboardItem(items)]);
|
|
}, data);
|
|
}
|
|
|
|
async function paste(page) {
|
|
await waitForEvent({
|
|
page,
|
|
eventName: "paste",
|
|
action: () => kbPaste(page),
|
|
});
|
|
}
|
|
|
|
async function pasteFromClipboard(page, selector = null) {
|
|
const validator = e => e.clipboardData.items.length !== 0;
|
|
await waitForEvent({
|
|
page,
|
|
eventName: "paste",
|
|
action: () => kbPaste(page),
|
|
selector,
|
|
validator,
|
|
});
|
|
}
|
|
|
|
async function getSerialized(page, filter = undefined) {
|
|
const values = await page.evaluate(() => {
|
|
const { map } =
|
|
window.PDFViewerApplication.pdfDocument.annotationStorage.serializable;
|
|
if (!map) {
|
|
return [];
|
|
}
|
|
const vals = Array.from(map.values());
|
|
for (const value of vals) {
|
|
for (const [k, v] of Object.entries(value)) {
|
|
// Puppeteer don't serialize typed array correctly, so we convert them
|
|
// to arrays.
|
|
if (ArrayBuffer.isView(v)) {
|
|
value[k] = Array.from(v);
|
|
}
|
|
}
|
|
}
|
|
return vals;
|
|
});
|
|
return filter ? values.map(filter) : values;
|
|
}
|
|
|
|
async function getFirstSerialized(page, filter = undefined) {
|
|
return (await getSerialized(page, filter))[0];
|
|
}
|
|
|
|
function getAnnotationStorage(page) {
|
|
return page.evaluate(() =>
|
|
Object.fromEntries(
|
|
window.PDFViewerApplication.pdfDocument.annotationStorage.serializable
|
|
.map || []
|
|
)
|
|
);
|
|
}
|
|
|
|
function waitForEntryInStorage(page, key, value, checker = (x, y) => x === y) {
|
|
return page.waitForFunction(
|
|
(k, v, c) => {
|
|
const { map } =
|
|
window.PDFViewerApplication.pdfDocument.annotationStorage.serializable;
|
|
// eslint-disable-next-line no-eval
|
|
return map && eval(`(${c})`)(JSON.stringify(map.get(k)), v);
|
|
},
|
|
{},
|
|
key,
|
|
JSON.stringify(value),
|
|
checker.toString()
|
|
);
|
|
}
|
|
|
|
function getEditors(page, kind) {
|
|
return page.evaluate(aKind => {
|
|
const elements = document.querySelectorAll(`.${aKind}Editor`);
|
|
const results = [];
|
|
for (const { id } of elements) {
|
|
results.push(parseInt(id.split("_").at(-1), 10));
|
|
}
|
|
results.sort();
|
|
return results;
|
|
}, kind);
|
|
}
|
|
|
|
function getEditorDimensions(page, selector) {
|
|
return page.evaluate(sel => {
|
|
const { style } = document.querySelector(sel);
|
|
return {
|
|
left: style.left,
|
|
top: style.top,
|
|
width: style.width,
|
|
height: style.height,
|
|
};
|
|
}, selector);
|
|
}
|
|
|
|
async function serializeBitmapDimensions(page) {
|
|
await page.waitForFunction(() => {
|
|
try {
|
|
const map =
|
|
window.PDFViewerApplication.pdfDocument.annotationStorage.serializable
|
|
.map;
|
|
return !!map;
|
|
} catch {
|
|
return false;
|
|
}
|
|
});
|
|
|
|
return page.evaluate(() => {
|
|
const { map } =
|
|
window.PDFViewerApplication.pdfDocument.annotationStorage.serializable;
|
|
return map
|
|
? Array.from(map.values(), x => ({
|
|
width: x.bitmap.width,
|
|
height: x.bitmap.height,
|
|
}))
|
|
: [];
|
|
});
|
|
}
|
|
|
|
async function dragAndDrop(page, selector, translations, steps = 1) {
|
|
const rect = await getRect(page, selector);
|
|
const startX = rect.x + rect.width / 2;
|
|
const startY = rect.y + rect.height / 2;
|
|
await page.mouse.move(startX, startY);
|
|
await page.mouse.down();
|
|
for (const [tX, tY] of translations) {
|
|
await page.mouse.move(startX + tX, startY + tY, { steps });
|
|
}
|
|
await page.mouse.up();
|
|
await page.waitForSelector("#viewer:not(.noUserSelect)");
|
|
}
|
|
|
|
// Move two fingers, horizontally centered on (centerX, centerY), from startGap
|
|
// to endGap: it's a pinch out when endGap is larger than startGap.
|
|
// Keep in mind that `TouchManager` starts to pinch only once the distance
|
|
// between the two fingers changed by more than `MIN_TOUCH_DISTANCE_TO_PINCH`
|
|
// (35 CSS pixels), hence the first moves are swallowed and the resulting zoom
|
|
// factor is smaller than endGap / startGap.
|
|
// Explicit start/end points can be used for tests which need an asymmetric
|
|
// gesture, or hooks around each touch lifetime.
|
|
async function pinch(
|
|
page,
|
|
{
|
|
afterEnd = null,
|
|
afterFirstEnd = null,
|
|
afterFirstStart = null,
|
|
afterStart = null,
|
|
beforeEnd = null,
|
|
centerX = 0,
|
|
centerY = 0,
|
|
centerDeltaX = 0,
|
|
centerDeltaY = 0,
|
|
startGap = 0,
|
|
endGap = startGap,
|
|
endPoints = null,
|
|
startPoints = null,
|
|
steps = 12,
|
|
}
|
|
) {
|
|
const normalizePoint = point =>
|
|
Array.isArray(point) ? { x: point[0], y: point[1] } : point;
|
|
const start = (
|
|
startPoints || [
|
|
{ x: centerX - startGap, y: centerY },
|
|
{ x: centerX + startGap, y: centerY },
|
|
]
|
|
).map(normalizePoint);
|
|
let end;
|
|
if (endPoints) {
|
|
end = endPoints.map(normalizePoint);
|
|
} else if (startPoints) {
|
|
end = start;
|
|
} else {
|
|
end = [
|
|
{ x: centerX + centerDeltaX - endGap, y: centerY + centerDeltaY },
|
|
{ x: centerX + centerDeltaX + endGap, y: centerY + centerDeltaY },
|
|
];
|
|
}
|
|
|
|
const finger0 = await page.touchscreen.touchStart(start[0].x, start[0].y);
|
|
await afterFirstStart?.(finger0);
|
|
const finger1 = await page.touchscreen.touchStart(start[1].x, start[1].y);
|
|
await afterStart?.([finger0, finger1]);
|
|
|
|
for (let i = 1; i <= steps; i++) {
|
|
const t = i / steps;
|
|
await finger0.move(
|
|
start[0].x + (end[0].x - start[0].x) * t,
|
|
start[0].y + (end[0].y - start[0].y) * t
|
|
);
|
|
await finger1.move(
|
|
start[1].x + (end[1].x - start[1].x) * t,
|
|
start[1].y + (end[1].y - start[1].y) * t
|
|
);
|
|
}
|
|
|
|
await beforeEnd?.([finger0, finger1]);
|
|
await finger0.end();
|
|
await afterFirstEnd?.([finger0, finger1]);
|
|
await finger1.end();
|
|
await afterEnd?.([finger0, finger1]);
|
|
}
|
|
|
|
function waitForPageChanging(page) {
|
|
return createPromise(page, resolve => {
|
|
window.PDFViewerApplication.eventBus.on("pagechanging", resolve, {
|
|
once: true,
|
|
});
|
|
});
|
|
}
|
|
|
|
function waitForAnnotationEditorLayer(page) {
|
|
return createPromise(page, resolve => {
|
|
window.PDFViewerApplication.eventBus.on(
|
|
"annotationeditorlayerrendered",
|
|
resolve,
|
|
{ once: true }
|
|
);
|
|
});
|
|
}
|
|
|
|
function waitForAnnotationModeChanged(page) {
|
|
return createPromise(page, resolve => {
|
|
window.PDFViewerApplication.eventBus.on(
|
|
"annotationeditormodechanged",
|
|
resolve,
|
|
{ once: true }
|
|
);
|
|
});
|
|
}
|
|
|
|
function waitForPageRendered(page, pageNumber) {
|
|
return page.evaluateHandle(
|
|
number => [
|
|
new Promise(resolve => {
|
|
const { eventBus } = window.PDFViewerApplication;
|
|
eventBus.on("pagerendered", function handler(e) {
|
|
if (
|
|
!e.isDetailView &&
|
|
(number === undefined || e.pageNumber === number)
|
|
) {
|
|
resolve();
|
|
eventBus.off("pagerendered", handler);
|
|
}
|
|
});
|
|
}),
|
|
],
|
|
pageNumber
|
|
);
|
|
}
|
|
|
|
function waitForEditorMovedInDOM(page) {
|
|
return createPromise(page, resolve => {
|
|
window.PDFViewerApplication.eventBus.on("editormovedindom", resolve, {
|
|
once: true,
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Wait two timer turns for deferred editor DOM and focus updates.
|
|
*/
|
|
function waitForEditorFocusSettled(page) {
|
|
return page.evaluate(
|
|
() =>
|
|
new Promise(resolve => {
|
|
setTimeout(() => setTimeout(resolve, 0), 0);
|
|
})
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Wait for deferred editor updates before input or focus.
|
|
*/
|
|
function settleEditorBeforeInput(page) {
|
|
for (const [target, names] of [
|
|
[page.keyboard, ["down", "up", "press", "type", "sendCharacter"]],
|
|
[page.mouse, ["down", "click"]],
|
|
[page, ["focus"]],
|
|
]) {
|
|
for (const name of names) {
|
|
const method = target[name].bind(target);
|
|
target[name] = async (...args) => {
|
|
await waitForEditorFocusSettled(page);
|
|
return method(...args);
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
async function scrollIntoView(page, selector) {
|
|
await page.waitForSelector(selector, { visible: true });
|
|
const handle = await page.evaluateHandle(
|
|
sel => [
|
|
new Promise(resolve => {
|
|
const container = document.getElementById("viewerContainer");
|
|
const element = document.querySelector(sel);
|
|
if (!container || !element) {
|
|
resolve();
|
|
return;
|
|
}
|
|
if (
|
|
container.scrollHeight <= container.clientHeight &&
|
|
container.scrollWidth <= container.clientWidth
|
|
) {
|
|
resolve();
|
|
return;
|
|
}
|
|
|
|
const beforeTop = container.scrollTop;
|
|
const beforeLeft = container.scrollLeft;
|
|
let settled = false;
|
|
let timeoutId = null;
|
|
|
|
const finish = () => {
|
|
if (settled) {
|
|
return;
|
|
}
|
|
settled = true;
|
|
if (timeoutId !== null) {
|
|
clearTimeout(timeoutId);
|
|
}
|
|
container.removeEventListener("scrollend", finish);
|
|
resolve();
|
|
};
|
|
|
|
container.addEventListener("scrollend", finish, { once: true });
|
|
element.scrollIntoView({ behavior: "instant", block: "start" });
|
|
|
|
if (
|
|
container.scrollTop === beforeTop &&
|
|
container.scrollLeft === beforeLeft
|
|
) {
|
|
finish();
|
|
return;
|
|
}
|
|
|
|
// Some browsers occasionally miss `scrollend`, so keep a short
|
|
// fallback to avoid hanging.
|
|
timeoutId = setTimeout(finish, 250);
|
|
}),
|
|
],
|
|
selector
|
|
);
|
|
return awaitPromise(handle);
|
|
}
|
|
|
|
async function firstPageOnTop(page) {
|
|
const handle = await page.evaluateHandle(() => [
|
|
new Promise(resolve => {
|
|
const container = document.getElementById("viewerContainer");
|
|
if (container.scrollTop === 0 && container.scrollLeft === 0) {
|
|
resolve();
|
|
return;
|
|
}
|
|
container.addEventListener("scrollend", resolve, { once: true });
|
|
container.scrollTo(0, 0);
|
|
}),
|
|
]);
|
|
return awaitPromise(handle);
|
|
}
|
|
|
|
async function setCaretAt(page, pageNumber, text, position) {
|
|
// Wait for the text layer to finish rendering before trying to find the span.
|
|
await page.waitForSelector(
|
|
`.page[data-page-number="${pageNumber}"] .textLayer .endOfContent`
|
|
);
|
|
await page.evaluate(
|
|
(pageN, string, pos) => {
|
|
for (const el of document.querySelectorAll(
|
|
`.page[data-page-number="${pageN}"] > .textLayer > span`
|
|
)) {
|
|
if (el.textContent === string) {
|
|
window.getSelection().setPosition(el.firstChild, pos);
|
|
break;
|
|
}
|
|
}
|
|
},
|
|
pageNumber,
|
|
text,
|
|
position
|
|
);
|
|
}
|
|
|
|
const modifier = isMac ? "Meta" : "Control";
|
|
async function kbCopy(page) {
|
|
await page.keyboard.down(modifier);
|
|
await page.keyboard.press("c", { commands: ["Copy"] });
|
|
await page.keyboard.up(modifier);
|
|
}
|
|
async function kbCut(page) {
|
|
await page.keyboard.down(modifier);
|
|
await page.keyboard.press("x", { commands: ["Cut"] });
|
|
await page.keyboard.up(modifier);
|
|
}
|
|
async function kbDelete(page) {
|
|
await page.keyboard.press("Delete");
|
|
}
|
|
async function kbPaste(page) {
|
|
await page.keyboard.down(modifier);
|
|
await page.keyboard.press("v", { commands: ["Paste"] });
|
|
await page.keyboard.up(modifier);
|
|
}
|
|
async function kbUndo(page) {
|
|
await page.keyboard.down(modifier);
|
|
await page.keyboard.press("z");
|
|
await page.keyboard.up(modifier);
|
|
}
|
|
async function kbRedo(page) {
|
|
if (isMac) {
|
|
await page.keyboard.down("Meta");
|
|
await page.keyboard.down("Shift");
|
|
await page.keyboard.press("z");
|
|
await page.keyboard.up("Shift");
|
|
await page.keyboard.up("Meta");
|
|
} else {
|
|
await page.keyboard.down("Control");
|
|
await page.keyboard.press("y");
|
|
await page.keyboard.up("Control");
|
|
}
|
|
}
|
|
async function kbSelectAll(page) {
|
|
await page.keyboard.down(modifier);
|
|
await page.keyboard.press("a", { commands: ["SelectAll"] });
|
|
await page.keyboard.up(modifier);
|
|
}
|
|
async function kbModifierDown(page) {
|
|
await page.keyboard.down(modifier);
|
|
}
|
|
async function kbModifierUp(page) {
|
|
await page.keyboard.up(modifier);
|
|
}
|
|
async function kbGoToEnd(page) {
|
|
if (isMac) {
|
|
await page.keyboard.down("Meta");
|
|
await page.keyboard.press("ArrowDown", {
|
|
commands: ["MoveToEndOfDocument"],
|
|
});
|
|
await page.keyboard.up("Meta");
|
|
} else {
|
|
await page.keyboard.down("Control");
|
|
await page.keyboard.press("End");
|
|
await page.keyboard.up("Control");
|
|
}
|
|
}
|
|
async function kbGoToBegin(page) {
|
|
if (isMac) {
|
|
await page.keyboard.down("Meta");
|
|
await page.keyboard.press("ArrowUp", {
|
|
commands: ["MoveToBeginningOfDocument"],
|
|
});
|
|
await page.keyboard.up("Meta");
|
|
} else {
|
|
await page.keyboard.down("Control");
|
|
await page.keyboard.press("Home");
|
|
await page.keyboard.up("Control");
|
|
}
|
|
}
|
|
async function kbBigMoveLeft(page) {
|
|
if (isMac) {
|
|
await page.keyboard.down("Shift");
|
|
await page.keyboard.press("ArrowLeft");
|
|
await page.keyboard.up("Shift");
|
|
} else {
|
|
await page.keyboard.down("Control");
|
|
await page.keyboard.press("ArrowLeft");
|
|
await page.keyboard.up("Control");
|
|
}
|
|
}
|
|
async function kbBigMoveRight(page) {
|
|
if (isMac) {
|
|
await page.keyboard.down("Shift");
|
|
await page.keyboard.press("ArrowRight");
|
|
await page.keyboard.up("Shift");
|
|
} else {
|
|
await page.keyboard.down("Control");
|
|
await page.keyboard.press("ArrowRight");
|
|
await page.keyboard.up("Control");
|
|
}
|
|
}
|
|
async function kbBigMoveUp(page) {
|
|
if (isMac) {
|
|
await page.keyboard.down("Shift");
|
|
await page.keyboard.press("ArrowUp");
|
|
await page.keyboard.up("Shift");
|
|
} else {
|
|
await page.keyboard.down("Control");
|
|
await page.keyboard.press("ArrowUp");
|
|
await page.keyboard.up("Control");
|
|
}
|
|
}
|
|
async function kbBigMoveDown(page) {
|
|
if (isMac) {
|
|
await page.keyboard.down("Shift");
|
|
await page.keyboard.press("ArrowDown");
|
|
await page.keyboard.up("Shift");
|
|
} else {
|
|
await page.keyboard.down("Control");
|
|
await page.keyboard.press("ArrowDown");
|
|
await page.keyboard.up("Control");
|
|
}
|
|
}
|
|
|
|
async function kbDeleteLastWord(page) {
|
|
if (isMac) {
|
|
await page.keyboard.down("Alt");
|
|
await page.keyboard.press("Backspace");
|
|
await page.keyboard.up("Alt");
|
|
} else {
|
|
await page.keyboard.down("Control");
|
|
await page.keyboard.press("Backspace");
|
|
await page.keyboard.up("Control");
|
|
}
|
|
}
|
|
|
|
async function kbFocusNext(page, selector = null) {
|
|
if (selector) {
|
|
await page.waitForSelector(selector, { visible: true });
|
|
}
|
|
while (true) {
|
|
const handle = await page.evaluateHandle(
|
|
sel => [
|
|
new Promise(resolve => {
|
|
const cb = e => {
|
|
if (!sel || document.querySelector(sel)?.contains(e.target)) {
|
|
window.removeEventListener("focusin", cb);
|
|
resolve(true);
|
|
} else {
|
|
resolve(false);
|
|
}
|
|
};
|
|
window.addEventListener("focusin", cb);
|
|
}),
|
|
],
|
|
selector
|
|
);
|
|
|
|
await page.keyboard.press("Tab");
|
|
const result = await awaitPromise(handle);
|
|
if (result) {
|
|
break;
|
|
}
|
|
}
|
|
if (selector) {
|
|
await page.waitForSelector(`${selector}:focus-within`, { visible: true });
|
|
}
|
|
}
|
|
|
|
async function kbFocusPrevious(page) {
|
|
const handle = await createPromise(page, resolve => {
|
|
window.addEventListener("focusin", resolve, { once: true });
|
|
});
|
|
await page.keyboard.down("Shift");
|
|
await page.keyboard.press("Tab");
|
|
await page.keyboard.up("Shift");
|
|
await awaitPromise(handle);
|
|
}
|
|
|
|
async function kbSave(page) {
|
|
await page.keyboard.down(modifier);
|
|
await page.keyboard.press("s");
|
|
await page.keyboard.up(modifier);
|
|
}
|
|
|
|
async function switchToEditor(name, page, disable = false) {
|
|
const modeChangedHandle = await createPromise(page, resolve => {
|
|
window.PDFViewerApplication.eventBus.on(
|
|
"annotationeditormodechanged",
|
|
resolve,
|
|
{ once: true }
|
|
);
|
|
});
|
|
await page.click(`#editor${name}Button`);
|
|
name = name.toLowerCase();
|
|
await page.waitForSelector(
|
|
".annotationEditorLayer" +
|
|
(disable ? `:not(.${name}Editing)` : `.${name}Editing`)
|
|
);
|
|
await awaitPromise(modeChangedHandle);
|
|
}
|
|
|
|
async function selectEditors(name, page) {
|
|
await kbSelectAll(page);
|
|
await page.waitForFunction(
|
|
() => !document.querySelector(`.${name}Editor:not(.selectedEditor)`)
|
|
);
|
|
}
|
|
|
|
async function clearEditors(name, page) {
|
|
await selectEditors(name, page);
|
|
await page.keyboard.press("Backspace");
|
|
await waitForStorageEntries(page, 0);
|
|
}
|
|
|
|
function waitForNoElement(page, selector) {
|
|
return page.waitForFunction(
|
|
sel => !document.querySelector(sel),
|
|
{},
|
|
selector
|
|
);
|
|
}
|
|
|
|
function waitForTextToBe(page, selector, text) {
|
|
return page.waitForFunction(
|
|
(sel, str) => document.querySelector(sel)?.textContent.trim() === str,
|
|
{},
|
|
selector,
|
|
text
|
|
);
|
|
}
|
|
|
|
function waitForTooltipToBe(page, selector, text) {
|
|
return page.waitForFunction(
|
|
(sel, str) => document.querySelector(sel)?.title === str,
|
|
{},
|
|
selector,
|
|
text
|
|
);
|
|
}
|
|
|
|
function isCanvasMonochrome(page, pageNumber, rectangle, color) {
|
|
return page.evaluate(
|
|
(rect, pageN, col) => {
|
|
const canvas = document.querySelector(
|
|
`.page[data-page-number = "${pageN}"] .canvasWrapper canvas`
|
|
);
|
|
const canvasRect = canvas.getBoundingClientRect();
|
|
const ctx = canvas.getContext("2d");
|
|
rect ||= canvasRect;
|
|
// The canvas is scaled by the devicePixelRatio: convert from CSS pixels.
|
|
const scale = canvas.width / canvasRect.width;
|
|
const { data } = ctx.getImageData(
|
|
Math.round((rect.x - canvasRect.x) * scale),
|
|
Math.round((rect.y - canvasRect.y) * scale),
|
|
Math.round(rect.width * scale),
|
|
Math.round(rect.height * scale)
|
|
);
|
|
return new Uint32Array(data.buffer).every(x => x === col);
|
|
},
|
|
rectangle,
|
|
pageNumber,
|
|
color
|
|
);
|
|
}
|
|
|
|
async function getXY(page, selector) {
|
|
const rect = await getRect(page, selector);
|
|
return `${rect.x}::${rect.y}`;
|
|
}
|
|
|
|
function waitForPositionChange(page, selector, xy) {
|
|
return page.waitForFunction(
|
|
(sel, currentXY) => {
|
|
const bbox = document.querySelector(sel).getBoundingClientRect();
|
|
return `${bbox.x}::${bbox.y}` !== currentXY;
|
|
},
|
|
{},
|
|
selector,
|
|
xy
|
|
);
|
|
}
|
|
|
|
async function moveEditor(page, selector, n, pressKey) {
|
|
let xy = await getXY(page, selector);
|
|
for (let i = 0; i < n; i++) {
|
|
const handle = await waitForEditorMovedInDOM(page);
|
|
await pressKey();
|
|
await awaitPromise(handle);
|
|
await waitForPositionChange(page, selector, xy);
|
|
xy = await getXY(page, selector);
|
|
}
|
|
}
|
|
|
|
async function getNextEditorId(page) {
|
|
return page.evaluate(() =>
|
|
window.PDFViewerApplication.pdfViewer._layerProperties.annotationEditorUIManager.getNextEditorId()
|
|
);
|
|
}
|
|
|
|
async function highlightSpan(
|
|
page,
|
|
pageIndex,
|
|
text,
|
|
xRatio = 0.5,
|
|
yRatio = 0.5
|
|
) {
|
|
const nextId = await getNextEditorId(page);
|
|
const rect = await getSpanRectFromText(page, pageIndex, text);
|
|
const x = rect.x + rect.width * xRatio;
|
|
const y = rect.y + rect.height * yRatio;
|
|
// We add a small delay between press and release to make sure that a
|
|
// pointerup event is triggered after selectionchange.
|
|
// It works with a value of 1ms, but we use 100ms to be sure.
|
|
await page.mouse.click(x, y, { count: 2, delay: 100 });
|
|
await page.waitForSelector(getEditorSelector(nextId));
|
|
}
|
|
|
|
async function showViewsManager(page) {
|
|
// Opening dispatches this event synchronously when animations are disabled,
|
|
// so install the listener before clicking the toggle button. With animations,
|
|
// it's dispatched once the transition has ended and the moving class has
|
|
// been removed.
|
|
const openedHandle = await createPromise(page, resolve => {
|
|
const { eventBus, viewsManager } = window.PDFViewerApplication;
|
|
const onResize = ({ source }) => {
|
|
if (source !== viewsManager) {
|
|
return;
|
|
}
|
|
eventBus.off("resize", onResize);
|
|
resolve();
|
|
};
|
|
eventBus.on("resize", onResize);
|
|
});
|
|
await page.click("#viewsManagerToggleButton");
|
|
await awaitPromise(openedHandle);
|
|
await page.waitForSelector("#viewsManager", { visible: true });
|
|
await page.waitForSelector(
|
|
"#outerContainer:not(.viewsManagerMoving).viewsManagerOpen",
|
|
{ visible: true }
|
|
);
|
|
await page.waitForSelector("#viewsManagerStatusActionButton:not(:disabled)", {
|
|
visible: true,
|
|
});
|
|
}
|
|
|
|
async function waitForBrowserTrip(page) {
|
|
const handle = await page.evaluateHandle(() => [
|
|
new Promise(resolve => {
|
|
window.requestAnimationFrame(resolve);
|
|
}),
|
|
]);
|
|
await awaitPromise(handle);
|
|
}
|
|
|
|
function waitForSelectionChange(page, selection) {
|
|
return page.waitForFunction(
|
|
// We need to replace EOL on Windows to make the test pass.
|
|
sel => document.getSelection().toString().replaceAll("\r\n", "\n") === sel,
|
|
{},
|
|
selection
|
|
);
|
|
}
|
|
|
|
// Unicode bidi isolation characters, Fluent adds these markers to the text.
|
|
const FSI = "\u2068";
|
|
const PDI = "\u2069";
|
|
|
|
export {
|
|
applyFunctionToEditor,
|
|
awaitPromise,
|
|
clearEditors,
|
|
clearInput,
|
|
closePages,
|
|
closeSinglePage,
|
|
copy,
|
|
copyToClipboard,
|
|
countSerialized,
|
|
countStorageEntries,
|
|
createPromise,
|
|
createPromiseWithArgs,
|
|
decodePNG,
|
|
dragAndDrop,
|
|
firstPageOnTop,
|
|
FSI,
|
|
getAnnotationSelector,
|
|
getAnnotationStorage,
|
|
getComputedStyleSelector,
|
|
getEditorDimensions,
|
|
getEditors,
|
|
getEditorSelector,
|
|
getFirstSerialized,
|
|
getNextEditorId,
|
|
getQuerySelector,
|
|
getRect,
|
|
getSelector,
|
|
getSerialized,
|
|
getSpanRectFromText,
|
|
getTextLayerFonts,
|
|
getThumbnailSelector,
|
|
getXY,
|
|
highlightSpan,
|
|
isCanvasMonochrome,
|
|
kbBigMoveDown,
|
|
kbBigMoveLeft,
|
|
kbBigMoveRight,
|
|
kbBigMoveUp,
|
|
kbCopy,
|
|
kbCut,
|
|
kbDelete,
|
|
kbDeleteLastWord,
|
|
kbFocusNext,
|
|
kbFocusPrevious,
|
|
kbGoToBegin,
|
|
kbGoToEnd,
|
|
kbModifierDown,
|
|
kbModifierUp,
|
|
kbRedo,
|
|
kbSave,
|
|
kbSelectAll,
|
|
kbUndo,
|
|
loadAndWait,
|
|
mockClipboard,
|
|
moveEditor,
|
|
paste,
|
|
pasteFromClipboard,
|
|
PDI,
|
|
pinch,
|
|
scrollIntoView,
|
|
selectEditor,
|
|
selectEditors,
|
|
serializeBitmapDimensions,
|
|
setCaretAt,
|
|
showViewsManager,
|
|
switchToEditor,
|
|
unselectEditor,
|
|
waitAndClick,
|
|
waitForAnnotationEditorLayer,
|
|
waitForAnnotationModeChanged,
|
|
waitForBrowserTrip,
|
|
waitForDOMMutation,
|
|
waitForEntryInStorage,
|
|
waitForEvent,
|
|
waitForNoElement,
|
|
waitForPageChanging,
|
|
waitForPageRendered,
|
|
waitForPointerUp,
|
|
waitForSandboxTrip,
|
|
waitForScripting,
|
|
waitForSelectedEditor,
|
|
waitForSelectionChange,
|
|
waitForSerialized,
|
|
waitForStorageEntries,
|
|
waitForTextToBe,
|
|
waitForTimeout,
|
|
waitForTooltipToBe,
|
|
waitForUnselectedEditor,
|
|
};
|