Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add page label details support #1

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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
97 changes: 96 additions & 1 deletion src/core/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,19 @@ class Catalog {
return shadow(this, "pageLabels", obj);
}

get pageLabelDetails() {
let obj = null;
// try {
obj = this._readPageLabelDetails();
// } catch (ex) {
// if (ex instanceof MissingDataException) {
// throw ex;
// }
// warn("Unable to read page label details.");
// }
return shadow(this, "pageLabelDetails", obj);
}

/**
* @private
*/
Expand Down Expand Up @@ -732,7 +745,89 @@ class Catalog {
currentLabel = "";
}

pageLabels[i] = prefix + currentLabel;
pageLabels[i] = {
value: prefix + currentLabel,
labelDict,
};
currentIndex++;
}
return pageLabels;
}

/**
* @private
*/
_readPageLabelDetails() {
const obj = this._catDict.getRaw("PageLabels");

if (!obj) {
return null;
}

const pageLabels = new Array(this.numPages);
let style = null,
prefix = "";

const numberTree = new NumberTree(obj, this.xref);
const numberTreeMap = numberTree.getAll();
let currentIndex = 1;

for (let i = 0, ii = this.numPages; i < ii; i++) {
if (numberTreeMap.has(i)) {
const labelDict = numberTreeMap.get(i);
if (!(labelDict instanceof Dict)) {
throw new FormatError("PageLabel is not a dictionary.");
}

if (
labelDict.has("Type") &&
!isName(labelDict.get("Type"), "PageLabel")
) {
throw new FormatError("Invalid type in PageLabel dictionary.");
}

if (labelDict.has("S")) {
const s = labelDict.get("S");
if (!(s instanceof Name)) {
throw new FormatError("Invalid style in PageLabel dictionary.");
}
style = {
D: "decimal_arabic",
R: "uppercase_roman",
r: "lowercase_roman",
A: "uppercase_latin",
a: "lowercase_latin",
}[s.name];
} else {
style = "no_style";
}

if (labelDict.has("P")) {
const p = labelDict.get("P");
if (!(p instanceof String)) {
throw new FormatError("Invalid prefix in PageLabel dictionary.");
}
prefix = stringToPDFString(p);
} else {
prefix = "";
}

if (labelDict.has("St")) {
const st = labelDict.get("St");
if (!(Number.isInteger(st) && st >= 1)) {
throw new FormatError("Invalid start in PageLabel dictionary.");
}
currentIndex = st;
} else {
currentIndex = 1;
}
}

pageLabels[i] = {
firstPageNum: currentIndex,
prefix,
style,
};
currentIndex++;
}
return pageLabels;
Expand Down
7 changes: 7 additions & 0 deletions src/core/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,13 @@ class WorkerMessageHandler {
return pdfManager.ensureCatalog("pageLabels");
});

handler.on(
"GetPageLabelDetails",
function wphSetupGetPageLabelDetails(data) {
return pdfManager.ensureCatalog("pageLabelDetails");
}
);

handler.on("GetPageLayout", function wphSetupGetPageLayout(data) {
return pdfManager.ensureCatalog("pageLayout");
});
Expand Down
8 changes: 8 additions & 0 deletions src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,10 @@ class PDFDocumentProxy {
return this._transport.getPageLabels();
}

getPageLabelDetails() {
return this._transport.getPageLabelDetails();
}

/**
* @returns {Promise<string>} A promise that is resolved with a {string}
* containing the page layout name.
Expand Down Expand Up @@ -2948,6 +2952,10 @@ class WorkerTransport {
return this.messageHandler.sendWithPromise("GetPageLabels", null);
}

getPageLabelDetails() {
return this.messageHandler.sendWithPromise("GetPageLabelDetails", null);
}

getPageLayout() {
return this.messageHandler.sendWithPromise("GetPageLayout", null);
}
Expand Down