Skip to content
Merged
1 change: 1 addition & 0 deletions l10n/en-US/viewer.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,7 @@ pdfjs-editor-undo-bar-message-freetext = Text removed
pdfjs-editor-undo-bar-message-ink = Drawing removed
pdfjs-editor-undo-bar-message-stamp = Image removed
pdfjs-editor-undo-bar-message-signature = Signature removed
pdfjs-editor-undo-bar-message-comment = Comment removed
# Variables:
# $count (Number) - the number of removed annotations.
pdfjs-editor-undo-bar-message-multiple =
Expand Down
5 changes: 3 additions & 2 deletions src/core/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,8 @@ class Page {
task,
intent,
cacheKey,
pageId = this.pageIndex,
pageIndex = this.pageIndex,
annotationStorage = null,
modifiedIds = null,
}) {
Expand Down Expand Up @@ -549,13 +551,12 @@ class Page {
RESOURCES_KEYS_OPERATOR_LIST
);
const opList = new OperatorList(intent, sink);

handler.send("StartRenderPage", {
transparency: partialEvaluator.hasBlendModes(
resources,
this.nonBlendModesSet
),
pageIndex: this.pageIndex,
pageIndex,
cacheKey,
});

Expand Down
10 changes: 6 additions & 4 deletions src/core/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -853,8 +853,8 @@ class WorkerMessageHandler {
);

handler.on("GetOperatorList", function (data, sink) {
const pageIndex = data.pageIndex;
pdfManager.getPage(pageIndex).then(function (page) {
const { pageId, pageIndex } = data;
pdfManager.getPage(pageId).then(function (page) {
const task = new WorkerTask(`GetOperatorList: page ${pageIndex}`);
startWorkerTask(task);

Expand All @@ -871,6 +871,7 @@ class WorkerMessageHandler {
cacheKey: data.cacheKey,
annotationStorage: data.annotationStorage,
modifiedIds: data.modifiedIds,
pageIndex,
})
.then(
function (operatorListInfo) {
Expand Down Expand Up @@ -899,9 +900,10 @@ class WorkerMessageHandler {
});

handler.on("GetTextContent", function (data, sink) {
const { pageIndex, includeMarkedContent, disableNormalization } = data;
const { pageId, pageIndex, includeMarkedContent, disableNormalization } =
data;

pdfManager.getPage(pageIndex).then(function (page) {
pdfManager.getPage(pageId).then(function (page) {
const task = new WorkerTask("GetTextContent: page " + pageIndex);
startWorkerTask(task);

Expand Down
2 changes: 1 addition & 1 deletion src/display/annotation_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ class AnnotationElement {
this.annotationStorage.setValue(`${AnnotationEditorPrefix}${data.id}`, {
id: data.id,
annotationType: data.annotationType,
pageIndex: this.parent.page._pageIndex,
page: this.parent.page,
popup,
popupRef: data.popupRef,
modificationDate: new Date(),
Expand Down
4 changes: 4 additions & 0 deletions src/display/annotation_storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ class AnnotationStorage {
val instanceof AnnotationEditor
? val.serialize(/* isForCopying = */ false, context)
: val;
if (val.page) {
val.pageIndex = val.page._pageIndex;
delete val.page;
}
if (serialized) {
map.set(key, serialized);

Expand Down
79 changes: 59 additions & 20 deletions src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
deprecated,
isDataScheme,
isValidFetchUrl,
PagesMapper,
PageViewport,
RenderingCancelledException,
StatTimer,
Expand Down Expand Up @@ -1328,6 +1329,8 @@ class PDFDocumentProxy {
class PDFPageProxy {
#pendingCleanup = false;

#pagesMapper = PagesMapper.instance;

constructor(pageIndex, pageInfo, transport, pdfBug = false) {
this._pageIndex = pageIndex;
this._pageInfo = pageInfo;
Expand All @@ -1350,6 +1353,13 @@ class PDFPageProxy {
return this._pageIndex + 1;
}

/**
* @param {number} value - The page number to set. First page is 1.
*/
set pageNumber(value) {
this._pageIndex = value - 1;
}

/**
* @type {number} The number of degrees the page is rotated clockwise.
*/
Expand Down Expand Up @@ -1699,6 +1709,7 @@ class PDFPageProxy {
return this._transport.messageHandler.sendWithStream(
"GetTextContent",
{
pageId: this.#pagesMapper.getPageId(this._pageIndex + 1) - 1,
pageIndex: this._pageIndex,
includeMarkedContent: includeMarkedContent === true,
disableNormalization: disableNormalization === true,
Expand Down Expand Up @@ -1884,6 +1895,7 @@ class PDFPageProxy {
const readableStream = this._transport.messageHandler.sendWithStream(
"GetOperatorList",
{
pageId: this.#pagesMapper.getPageId(this._pageIndex + 1) - 1,
pageIndex: this._pageIndex,
intent: renderingIntent,
cacheKey,
Expand Down Expand Up @@ -2389,6 +2401,8 @@ class WorkerTransport {

#passwordCapability = null;

#pagesMapper = PagesMapper.instance;

constructor(
messageHandler,
loadingTask,
Expand Down Expand Up @@ -2424,6 +2438,8 @@ class WorkerTransport {

this.setupMessageHandler();

this.#pagesMapper.addListener(this.#updateCaches.bind(this));

if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
// For testing purposes.
Object.defineProperty(this, "getNetworkStreamName", {
Expand All @@ -2448,6 +2464,24 @@ class WorkerTransport {
}
}

#updateCaches() {
const newPageCache = new Map();
const newPromiseCache = new Map();
for (let i = 0, ii = this.#pagesMapper.pagesNumber; i < ii; i++) {
const prevPageIndex = this.#pagesMapper.getPrevPageNumber(i + 1) - 1;
const page = this.#pageCache.get(prevPageIndex);
if (page) {
newPageCache.set(i, page);
}
const promise = this.#pagePromises.get(prevPageIndex);
if (promise) {
newPromiseCache.set(i, promise);
}
}
this.#pageCache = newPageCache;
this.#pagePromises = newPromiseCache;
}

#cacheSimpleMethod(name, data = null) {
const cachedPromise = this.#methodPromises.get(name);
if (cachedPromise) {
Expand Down Expand Up @@ -2710,6 +2744,7 @@ class WorkerTransport {
});

messageHandler.on("GetDoc", ({ pdfInfo }) => {
this.#pagesMapper.pagesNumber = pdfInfo.numPages;
this._numPages = pdfInfo.numPages;
this._htmlForXfa = pdfInfo.htmlForXfa;
delete pdfInfo.htmlForXfa;
Expand Down Expand Up @@ -2932,26 +2967,27 @@ class WorkerTransport {
if (
!Number.isInteger(pageNumber) ||
pageNumber <= 0 ||
pageNumber > this._numPages
pageNumber > this.#pagesMapper.pagesNumber
) {
return Promise.reject(new Error("Invalid page request."));
}
const pageIndex = pageNumber - 1;
const newPageIndex = this.#pagesMapper.getPageId(pageNumber) - 1;

const pageIndex = pageNumber - 1,
cachedPromise = this.#pagePromises.get(pageIndex);
const cachedPromise = this.#pagePromises.get(pageIndex);
if (cachedPromise) {
return cachedPromise;
}
const promise = this.messageHandler
.sendWithPromise("GetPage", {
pageIndex,
pageIndex: newPageIndex,
})
.then(pageInfo => {
if (this.destroyed) {
throw new Error("Transport destroyed");
}
if (pageInfo.refStr) {
this.#pageRefCache.set(pageInfo.refStr, pageNumber);
this.#pageRefCache.set(pageInfo.refStr, newPageIndex);
}

const page = new PDFPageProxy(
Expand All @@ -2967,19 +3003,20 @@ class WorkerTransport {
return promise;
}

getPageIndex(ref) {
async getPageIndex(ref) {
if (!isRefProxy(ref)) {
return Promise.reject(new Error("Invalid pageIndex request."));
throw new Error("Invalid pageIndex request.");
}
return this.messageHandler.sendWithPromise("GetPageIndex", {
const index = await this.messageHandler.sendWithPromise("GetPageIndex", {
num: ref.num,
gen: ref.gen,
});
return this.#pagesMapper.getPageNumber(index + 1) - 1;
}

getAnnotations(pageIndex, intent) {
return this.messageHandler.sendWithPromise("GetAnnotations", {
pageIndex,
pageIndex: this.#pagesMapper.getPageId(pageIndex + 1) - 1,
intent,
});
}
Expand Down Expand Up @@ -3046,13 +3083,13 @@ class WorkerTransport {

getPageJSActions(pageIndex) {
return this.messageHandler.sendWithPromise("GetPageJSActions", {
pageIndex,
pageIndex: this.#pagesMapper.getPageId(pageIndex + 1) - 1,
});
}

getStructTree(pageIndex) {
return this.messageHandler.sendWithPromise("GetStructTree", {
pageIndex,
pageIndex: this.#pagesMapper.getPageId(pageIndex + 1) - 1,
});
}

Expand Down Expand Up @@ -3122,15 +3159,18 @@ class WorkerTransport {
return null;
}
const refStr = ref.gen === 0 ? `${ref.num}R` : `${ref.num}R${ref.gen}`;
return this.#pageRefCache.get(refStr) ?? null;
const pageIndex = this.#pageRefCache.get(refStr);
return pageIndex >= 0
? this.#pagesMapper.getPageNumber(pageIndex + 1)
: null;
}
}

/**
* Allows controlling of the rendering tasks.
*/
class RenderTask {
#internalRenderTask = null;
_internalRenderTask = null;

/**
* Callback for incremental rendering -- a function that will be called
Expand All @@ -3151,12 +3191,12 @@ class RenderTask {
onError = null;

constructor(internalRenderTask) {
this.#internalRenderTask = internalRenderTask;
this._internalRenderTask = internalRenderTask;

if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
// For testing purposes.
Object.defineProperty(this, "getOperatorList", {
value: () => this.#internalRenderTask.operatorList,
value: () => this._internalRenderTask.operatorList,
});
}
}
Expand All @@ -3166,7 +3206,7 @@ class RenderTask {
* @type {Promise<void>}
*/
get promise() {
return this.#internalRenderTask.capability.promise;
return this._internalRenderTask.capability.promise;
}

/**
Expand All @@ -3177,19 +3217,19 @@ class RenderTask {
* @param {number} [extraDelay]
*/
cancel(extraDelay = 0) {
this.#internalRenderTask.cancel(/* error = */ null, extraDelay);
this._internalRenderTask.cancel(/* error = */ null, extraDelay);
}

/**
* Whether form fields are rendered separately from the main operatorList.
* @type {boolean}
*/
get separateAnnots() {
const { separateAnnots } = this.#internalRenderTask.operatorList;
const { separateAnnots } = this._internalRenderTask.operatorList;
if (!separateAnnots) {
return false;
}
const { annotationCanvasMap } = this.#internalRenderTask;
const { annotationCanvasMap } = this._internalRenderTask;
return (
separateAnnots.form ||
(separateAnnots.canvas && annotationCanvasMap?.size > 0)
Expand Down Expand Up @@ -3389,7 +3429,6 @@ class InternalRenderTask {
if (this.operatorList.lastChunk) {
this.gfx.endDrawing();
InternalRenderTask.#canvasInUse.delete(this._canvas);

this.callback();
}
}
Expand Down
Loading
Loading