Merge pull request #15215 from Snuffleupagus/optional-content-initial

[api-minor] Improve how we disable `PDFThumbnailView.setImage` for documents with Optional Content
This commit is contained in:
Tim van der Meij 2022-07-30 12:04:23 +02:00 committed by GitHub
commit c7b71a3376
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 140 additions and 62 deletions

View File

@ -12,52 +12,85 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
import { objectFromMap, warn } from "../shared/util.js";
import { objectFromMap, unreachable, warn } from "../shared/util.js";
const INTERNAL = Symbol("INTERNAL");
class OptionalContentGroup { class OptionalContentGroup {
#visible = true;
constructor(name, intent) { constructor(name, intent) {
this.visible = true;
this.name = name; this.name = name;
this.intent = intent; this.intent = intent;
} }
/**
* @type {boolean}
*/
get visible() {
return this.#visible;
}
/**
* @ignore
*/
_setVisible(internal, visible) {
if (internal !== INTERNAL) {
unreachable("Internal method `_setVisible` called.");
}
this.#visible = visible;
}
} }
class OptionalContentConfig { class OptionalContentConfig {
#cachedHasInitialVisibility = true;
#groups = new Map();
#initialVisibility = null;
#order = null;
constructor(data) { constructor(data) {
this.name = null; this.name = null;
this.creator = null; this.creator = null;
this._order = null;
this._groups = new Map();
if (data === null) { if (data === null) {
return; return;
} }
this.name = data.name; this.name = data.name;
this.creator = data.creator; this.creator = data.creator;
this._order = data.order; this.#order = data.order;
for (const group of data.groups) { for (const group of data.groups) {
this._groups.set( this.#groups.set(
group.id, group.id,
new OptionalContentGroup(group.name, group.intent) new OptionalContentGroup(group.name, group.intent)
); );
} }
if (data.baseState === "OFF") { if (data.baseState === "OFF") {
for (const group of this._groups) { for (const group of this.#groups.values()) {
group.visible = false; group._setVisible(INTERNAL, false);
} }
} }
for (const on of data.on) { for (const on of data.on) {
this._groups.get(on).visible = true; this.#groups.get(on)._setVisible(INTERNAL, true);
} }
for (const off of data.off) { for (const off of data.off) {
this._groups.get(off).visible = false; this.#groups.get(off)._setVisible(INTERNAL, false);
}
// The following code must always run *last* in the constructor.
this.#initialVisibility = new Map();
for (const [id, group] of this.#groups) {
this.#initialVisibility.set(id, group.visible);
} }
} }
_evaluateVisibilityExpression(array) { #evaluateVisibilityExpression(array) {
const length = array.length; const length = array.length;
if (length < 2) { if (length < 2) {
return true; return true;
@ -67,9 +100,9 @@ class OptionalContentConfig {
const element = array[i]; const element = array[i];
let state; let state;
if (Array.isArray(element)) { if (Array.isArray(element)) {
state = this._evaluateVisibilityExpression(element); state = this.#evaluateVisibilityExpression(element);
} else if (this._groups.has(element)) { } else if (this.#groups.has(element)) {
state = this._groups.get(element).visible; state = this.#groups.get(element).visible;
} else { } else {
warn(`Optional content group not found: ${element}`); warn(`Optional content group not found: ${element}`);
return true; return true;
@ -95,7 +128,7 @@ class OptionalContentConfig {
} }
isVisible(group) { isVisible(group) {
if (this._groups.size === 0) { if (this.#groups.size === 0) {
return true; return true;
} }
if (!group) { if (!group) {
@ -103,57 +136,57 @@ class OptionalContentConfig {
return true; return true;
} }
if (group.type === "OCG") { if (group.type === "OCG") {
if (!this._groups.has(group.id)) { if (!this.#groups.has(group.id)) {
warn(`Optional content group not found: ${group.id}`); warn(`Optional content group not found: ${group.id}`);
return true; return true;
} }
return this._groups.get(group.id).visible; return this.#groups.get(group.id).visible;
} else if (group.type === "OCMD") { } else if (group.type === "OCMD") {
// Per the spec, the expression should be preferred if available. // Per the spec, the expression should be preferred if available.
if (group.expression) { if (group.expression) {
return this._evaluateVisibilityExpression(group.expression); return this.#evaluateVisibilityExpression(group.expression);
} }
if (!group.policy || group.policy === "AnyOn") { if (!group.policy || group.policy === "AnyOn") {
// Default // Default
for (const id of group.ids) { for (const id of group.ids) {
if (!this._groups.has(id)) { if (!this.#groups.has(id)) {
warn(`Optional content group not found: ${id}`); warn(`Optional content group not found: ${id}`);
return true; return true;
} }
if (this._groups.get(id).visible) { if (this.#groups.get(id).visible) {
return true; return true;
} }
} }
return false; return false;
} else if (group.policy === "AllOn") { } else if (group.policy === "AllOn") {
for (const id of group.ids) { for (const id of group.ids) {
if (!this._groups.has(id)) { if (!this.#groups.has(id)) {
warn(`Optional content group not found: ${id}`); warn(`Optional content group not found: ${id}`);
return true; return true;
} }
if (!this._groups.get(id).visible) { if (!this.#groups.get(id).visible) {
return false; return false;
} }
} }
return true; return true;
} else if (group.policy === "AnyOff") { } else if (group.policy === "AnyOff") {
for (const id of group.ids) { for (const id of group.ids) {
if (!this._groups.has(id)) { if (!this.#groups.has(id)) {
warn(`Optional content group not found: ${id}`); warn(`Optional content group not found: ${id}`);
return true; return true;
} }
if (!this._groups.get(id).visible) { if (!this.#groups.get(id).visible) {
return true; return true;
} }
} }
return false; return false;
} else if (group.policy === "AllOff") { } else if (group.policy === "AllOff") {
for (const id of group.ids) { for (const id of group.ids) {
if (!this._groups.has(id)) { if (!this.#groups.has(id)) {
warn(`Optional content group not found: ${id}`); warn(`Optional content group not found: ${id}`);
return true; return true;
} }
if (this._groups.get(id).visible) { if (this.#groups.get(id).visible) {
return false; return false;
} }
} }
@ -167,29 +200,44 @@ class OptionalContentConfig {
} }
setVisibility(id, visible = true) { setVisibility(id, visible = true) {
if (!this._groups.has(id)) { if (!this.#groups.has(id)) {
warn(`Optional content group not found: ${id}`); warn(`Optional content group not found: ${id}`);
return; return;
} }
this._groups.get(id).visible = !!visible; this.#groups.get(id)._setVisible(INTERNAL, !!visible);
this.#cachedHasInitialVisibility = null;
}
get hasInitialVisibility() {
if (this.#cachedHasInitialVisibility !== null) {
return this.#cachedHasInitialVisibility;
}
for (const [id, group] of this.#groups) {
const visible = this.#initialVisibility.get(id);
if (group.visible !== visible) {
return (this.#cachedHasInitialVisibility = false);
}
}
return (this.#cachedHasInitialVisibility = true);
} }
getOrder() { getOrder() {
if (!this._groups.size) { if (!this.#groups.size) {
return null; return null;
} }
if (this._order) { if (this.#order) {
return this._order.slice(); return this.#order.slice();
} }
return Array.from(this._groups.keys()); return [...this.#groups.keys()];
} }
getGroups() { getGroups() {
return this._groups.size > 0 ? objectFromMap(this._groups) : null; return this.#groups.size > 0 ? objectFromMap(this.#groups) : null;
} }
getGroup(id) { getGroup(id) {
return this._groups.get(id) || null; return this.#groups.get(id) || null;
} }
} }

View File

@ -1856,6 +1856,7 @@ class BaseViewer {
return Promise.resolve(null); return Promise.resolve(null);
} }
if (!this._optionalContentConfigPromise) { if (!this._optionalContentConfigPromise) {
console.error("optionalContentConfigPromise: Not initialized yet.");
// Prevent issues if the getter is accessed *before* the `onePageRendered` // Prevent issues if the getter is accessed *before* the `onePageRendered`
// promise has resolved; won't (normally) happen in the default viewer. // promise has resolved; won't (normally) happen in the default viewer.
return this.pdfDocument.getOptionalContentConfig(); return this.pdfDocument.getOptionalContentConfig();

View File

@ -99,6 +99,8 @@ const MAX_CANVAS_PIXELS = compatibilityParams.maxCanvasPixels || 16777216;
class PDFPageView { class PDFPageView {
#annotationMode = AnnotationMode.ENABLE_FORMS; #annotationMode = AnnotationMode.ENABLE_FORMS;
#useThumbnailCanvas = true;
/** /**
* @param {PDFPageViewOptions} options * @param {PDFPageViewOptions} options
*/ */
@ -151,7 +153,12 @@ class PDFPageView {
this.renderingState = RenderingStates.INITIAL; this.renderingState = RenderingStates.INITIAL;
this.resume = null; this.resume = null;
this._renderError = null; this._renderError = null;
this._isStandalone = !this.renderingQueue?.hasViewer(); if (
typeof PDFJSDev === "undefined" ||
PDFJSDev.test("!PRODUCTION || GENERIC")
) {
this._isStandalone = !this.renderingQueue?.hasViewer();
}
this._annotationCanvasMap = null; this._annotationCanvasMap = null;
@ -174,6 +181,26 @@ class PDFPageView {
this.div = div; this.div = div;
container?.append(div); container?.append(div);
if (
(typeof PDFJSDev === "undefined" ||
PDFJSDev.test("!PRODUCTION || GENERIC")) &&
this._isStandalone
) {
const { optionalContentConfigPromise } = options;
if (optionalContentConfigPromise) {
// Ensure that the thumbnails always display the *initial* document
// state.
optionalContentConfigPromise.then(optionalContentConfig => {
if (
optionalContentConfigPromise !== this._optionalContentConfigPromise
) {
return;
}
this.#useThumbnailCanvas = optionalContentConfig.hasInitialVisibility;
});
}
}
} }
setPdfPage(pdfPage) { setPdfPage(pdfPage) {
@ -359,7 +386,11 @@ class PDFPageView {
this.loadingIconDiv = document.createElement("div"); this.loadingIconDiv = document.createElement("div");
this.loadingIconDiv.className = "loadingIcon notVisible"; this.loadingIconDiv.className = "loadingIcon notVisible";
if (this._isStandalone) { if (
(typeof PDFJSDev === "undefined" ||
PDFJSDev.test("!PRODUCTION || GENERIC")) &&
this._isStandalone
) {
this.toggleLoadingIconSpinner(/* viewVisible = */ true); this.toggleLoadingIconSpinner(/* viewVisible = */ true);
} }
this.loadingIconDiv.setAttribute("role", "img"); this.loadingIconDiv.setAttribute("role", "img");
@ -376,6 +407,16 @@ class PDFPageView {
} }
if (optionalContentConfigPromise instanceof Promise) { if (optionalContentConfigPromise instanceof Promise) {
this._optionalContentConfigPromise = optionalContentConfigPromise; this._optionalContentConfigPromise = optionalContentConfigPromise;
// Ensure that the thumbnails always display the *initial* document state.
optionalContentConfigPromise.then(optionalContentConfig => {
if (
optionalContentConfigPromise !== this._optionalContentConfigPromise
) {
return;
}
this.#useThumbnailCanvas = optionalContentConfig.hasInitialVisibility;
});
} }
const totalRotation = (this.rotation + this.pdfPageRotate) % 360; const totalRotation = (this.rotation + this.pdfPageRotate) % 360;
@ -384,7 +425,11 @@ class PDFPageView {
rotation: totalRotation, rotation: totalRotation,
}); });
if (this._isStandalone) { if (
(typeof PDFJSDev === "undefined" ||
PDFJSDev.test("!PRODUCTION || GENERIC")) &&
this._isStandalone
) {
docStyle.setProperty("--scale-factor", this.viewport.scale); docStyle.setProperty("--scale-factor", this.viewport.scale);
} }
@ -999,6 +1044,14 @@ class PDFPageView {
this.div.removeAttribute("data-page-label"); this.div.removeAttribute("data-page-label");
} }
} }
/**
* For use by the `PDFThumbnailView.setImage`-method.
* @ignore
*/
get thumbnailCanvas() {
return this.#useThumbnailCanvas ? this.canvas : null;
}
} }
export { PDFPageView }; export { PDFPageView };

View File

@ -37,7 +37,6 @@ const THUMBNAIL_WIDTH = 98; // px
* The default value is `null`. * The default value is `null`.
* @property {IPDFLinkService} linkService - The navigation/linking service. * @property {IPDFLinkService} linkService - The navigation/linking service.
* @property {PDFRenderingQueue} renderingQueue - The rendering queue object. * @property {PDFRenderingQueue} renderingQueue - The rendering queue object.
* @property {function} checkSetImageDisabled
* @property {IL10n} l10n - Localization service. * @property {IL10n} l10n - Localization service.
* @property {Object} [pageColors] - Overwrites background and foreground colors * @property {Object} [pageColors] - Overwrites background and foreground colors
* with user defined ones in order to improve readability in high contrast * with user defined ones in order to improve readability in high contrast
@ -88,7 +87,6 @@ class PDFThumbnailView {
optionalContentConfigPromise, optionalContentConfigPromise,
linkService, linkService,
renderingQueue, renderingQueue,
checkSetImageDisabled,
l10n, l10n,
pageColors, pageColors,
}) { }) {
@ -109,11 +107,6 @@ class PDFThumbnailView {
this.renderTask = null; this.renderTask = null;
this.renderingState = RenderingStates.INITIAL; this.renderingState = RenderingStates.INITIAL;
this.resume = null; this.resume = null;
this._checkSetImageDisabled =
checkSetImageDisabled ||
function () {
return false;
};
const pageWidth = this.viewport.width, const pageWidth = this.viewport.width,
pageHeight = this.viewport.height, pageHeight = this.viewport.height,
@ -356,13 +349,10 @@ class PDFThumbnailView {
} }
setImage(pageView) { setImage(pageView) {
if (this._checkSetImageDisabled()) {
return;
}
if (this.renderingState !== RenderingStates.INITIAL) { if (this.renderingState !== RenderingStates.INITIAL) {
return; return;
} }
const { canvas, pdfPage } = pageView; const { thumbnailCanvas: canvas, pdfPage } = pageView;
if (!canvas) { if (!canvas) {
return; return;
} }

View File

@ -85,12 +85,6 @@ class PDFThumbnailViewer {
this.scroll = watchScroll(this.container, this._scrollUpdated.bind(this)); this.scroll = watchScroll(this.container, this._scrollUpdated.bind(this));
this._resetView(); this._resetView();
eventBus._on("optionalcontentconfigchanged", () => {
// Ensure that the thumbnails always render with the *default* optional
// content configuration.
this._setImageDisabled = true;
});
} }
/** /**
@ -195,8 +189,6 @@ class PDFThumbnailViewer {
this._currentPageNumber = 1; this._currentPageNumber = 1;
this._pageLabels = null; this._pageLabels = null;
this._pagesRotation = 0; this._pagesRotation = 0;
this._optionalContentConfigPromise = null;
this._setImageDisabled = false;
// Remove the thumbnails from the DOM. // Remove the thumbnails from the DOM.
this.container.textContent = ""; this.container.textContent = "";
@ -220,13 +212,8 @@ class PDFThumbnailViewer {
firstPagePromise firstPagePromise
.then(firstPdfPage => { .then(firstPdfPage => {
this._optionalContentConfigPromise = optionalContentConfigPromise;
const pagesCount = pdfDocument.numPages; const pagesCount = pdfDocument.numPages;
const viewport = firstPdfPage.getViewport({ scale: 1 }); const viewport = firstPdfPage.getViewport({ scale: 1 });
const checkSetImageDisabled = () => {
return this._setImageDisabled;
};
for (let pageNum = 1; pageNum <= pagesCount; ++pageNum) { for (let pageNum = 1; pageNum <= pagesCount; ++pageNum) {
const thumbnail = new PDFThumbnailView({ const thumbnail = new PDFThumbnailView({
@ -236,7 +223,6 @@ class PDFThumbnailViewer {
optionalContentConfigPromise, optionalContentConfigPromise,
linkService: this.linkService, linkService: this.linkService,
renderingQueue: this.renderingQueue, renderingQueue: this.renderingQueue,
checkSetImageDisabled,
l10n: this.l10n, l10n: this.l10n,
pageColors: this.pageColors, pageColors: this.pageColors,
}); });