Skip to content

Commit

Permalink
Add support for toggling of Optional Content in the viewer (issue 12096)
Browse files Browse the repository at this point in the history
  • Loading branch information
Snuffleupagus committed Aug 5, 2020
1 parent 0be8879 commit f2bd346
Show file tree
Hide file tree
Showing 17 changed files with 475 additions and 18 deletions.
5 changes: 4 additions & 1 deletion l10n/en-US/viewer.properties
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,20 @@ print_progress_close=Cancel
# (the _label strings are alt text for the buttons, the .title strings are
# tooltips)
toggle_sidebar.title=Toggle Sidebar
toggle_sidebar_notification.title=Toggle Sidebar (document contains outline/attachments)
toggle_sidebar_notification2.title=Toggle Sidebar (document contains outline/attachments/layers)
toggle_sidebar_label=Toggle Sidebar
document_outline.title=Show Document Outline (double-click to expand/collapse all items)
document_outline_label=Document Outline
attachments.title=Show Attachments
attachments_label=Attachments
layers.title=Show Layers (double-click to reset all layers to the default state)
layers_label=Layers
thumbs.title=Show Thumbnails
thumbs_label=Thumbnails
findbar.title=Find in Document
findbar_label=Find

additional_layers=Additional Layers
# LOCALIZATION NOTE (page_canvas): "{{page}}" will be replaced by the page number.
page_canvas=Page {{page}}
# Thumbnails panel item (tooltip and alt text for images)
Expand Down
5 changes: 4 additions & 1 deletion l10n/sv-SE/viewer.properties
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,20 @@ print_progress_close=Avbryt
# (the _label strings are alt text for the buttons, the .title strings are
# tooltips)
toggle_sidebar.title=Visa/dölj sidofält
toggle_sidebar_notification.title=Visa/dölj sidofält (dokument innehåller översikt/bilagor)
toggle_sidebar_notification2.title=Visa/dölj sidofält (dokument innehåller översikt/bilagor/lager)
toggle_sidebar_label=Visa/dölj sidofält
document_outline.title=Visa dokumentdisposition (dubbelklicka för att expandera/komprimera alla objekt)
document_outline_label=Dokumentöversikt
attachments.title=Visa Bilagor
attachments_label=Bilagor
layers.title=Visa lager (dubbelklicka för att återställa alla lager till ursrungligt läge)
layers_label=Lager
thumbs.title=Visa miniatyrer
thumbs_label=Miniatyrer
findbar.title=Sök i dokument
findbar_label=Sök

additional_layers=Ytterliggare lager
# LOCALIZATION NOTE (page_canvas): "{{page}}" will be replaced by the page number.
page_canvas=Sida {{page}}
# Thumbnails panel item (tooltip and alt text for images)
Expand Down
22 changes: 22 additions & 0 deletions src/display/optional_content_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,28 @@ class OptionalContentConfig {
warn(`Unknown group type ${group.type}.`);
return true;
}

toggleVisibility(id, visible = true) {
if (!this.groups.has(id)) {
warn(`Optional content group not found: ${id}`);
return;
}
this.groups.get(id).visible = !!visible;
}

getGroups() {
if (!this.groups.size) {
return null;
}
if (this.order) {
return this.order.slice();
}
return Array.from(this.groups.keys());
}

getGroup(id) {
return this.groups.get(id);
}
}

export { OptionalContentConfig };
3 changes: 3 additions & 0 deletions src/pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import {
import { AnnotationLayer } from "./display/annotation_layer.js";
import { apiCompatibilityParams } from "./display/api_compatibility.js";
import { GlobalWorkerOptions } from "./display/worker_options.js";
import { OptionalContentConfig } from "./display/optional_content_config.js";
import { renderTextLayer } from "./display/text_layer.js";
import { SVGGraphics } from "./display/svg.js";

Expand Down Expand Up @@ -160,6 +161,8 @@ export {
apiCompatibilityParams,
// From "./display/worker_options.js":
GlobalWorkerOptions,
// From "./display/optional_content_config.js":
OptionalContentConfig,
// From "./display/text_layer.js":
renderTextLayer,
// From "./display/svg.js":
Expand Down
25 changes: 24 additions & 1 deletion web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import { PDFDocumentProperties } from "./pdf_document_properties.js";
import { PDFFindBar } from "./pdf_find_bar.js";
import { PDFFindController } from "./pdf_find_controller.js";
import { PDFHistory } from "./pdf_history.js";
import { PDFLayerViewer } from "./pdf_layer_viewer.js";
import { PDFLinkService } from "./pdf_link_service.js";
import { PDFOutlineViewer } from "./pdf_outline_viewer.js";
import { PDFPresentationMode } from "./pdf_presentation_mode.js";
Expand Down Expand Up @@ -208,6 +209,8 @@ const PDFViewerApplication = {
pdfOutlineViewer: null,
/** @type {PDFAttachmentViewer} */
pdfAttachmentViewer: null,
/** @type {PDFLayerViewer} */
pdfLayerViewer: null,
/** @type {PDFCursorTools} */
pdfCursorTools: null,
/** @type {ViewHistory} */
Expand Down Expand Up @@ -508,6 +511,12 @@ const PDFViewerApplication = {
downloadManager,
});

this.pdfLayerViewer = new PDFLayerViewer({
container: appConfig.sidebar.layersView,
eventBus,
l10n: this.l10n,
});

this.pdfSidebar = new PDFSidebar({
elements: appConfig.sidebar,
pdfViewer: this.pdfViewer,
Expand Down Expand Up @@ -735,6 +744,7 @@ const PDFViewerApplication = {
this.pdfSidebar.reset();
this.pdfOutlineViewer.reset();
this.pdfAttachmentViewer.reset();
this.pdfLayerViewer.reset();

if (this.pdfHistory) {
this.pdfHistory.reset();
Expand Down Expand Up @@ -1269,6 +1279,11 @@ const PDFViewerApplication = {
pdfDocument.getAttachments().then(attachments => {
this.pdfAttachmentViewer.render({ attachments });
});
// Ensure that the layers accurately reflects the current state in the
// viewer itself, rather than the default state provided by the API.
pdfViewer.getOptionalContentConfig().then(optionalContentConfig => {
this.pdfLayerViewer.render({ optionalContentConfig, pdfDocument });
});
});

this._initializePageLabels(pdfDocument);
Expand Down Expand Up @@ -1609,10 +1624,12 @@ const PDFViewerApplication = {

const pagesOverview = this.pdfViewer.getPagesOverview();
const printContainer = this.appConfig.printContainer;
const optionalContentConfigPromise = this.pdfViewer.getOptionalContentConfig();
const printService = PDFPrintServiceFactory.instance.createPrintService(
this.pdfDocument,
pagesOverview,
printContainer,
optionalContentConfigPromise,
this.l10n
);
this.printService = printService;
Expand Down Expand Up @@ -1683,6 +1700,7 @@ const PDFViewerApplication = {
eventBus._on("scalechanged", webViewerScaleChanged);
eventBus._on("rotatecw", webViewerRotateCw);
eventBus._on("rotateccw", webViewerRotateCcw);
eventBus._on("optionalcontentconfig", webViewerOptionalContentConfig);
eventBus._on("switchscrollmode", webViewerSwitchScrollMode);
eventBus._on("scrollmodechanged", webViewerScrollModeChanged);
eventBus._on("switchspreadmode", webViewerSwitchSpreadMode);
Expand Down Expand Up @@ -1758,6 +1776,7 @@ const PDFViewerApplication = {
eventBus._off("scalechanged", webViewerScaleChanged);
eventBus._off("rotatecw", webViewerRotateCw);
eventBus._off("rotateccw", webViewerRotateCcw);
eventBus._off("optionalcontentconfig", webViewerOptionalContentConfig);
eventBus._off("switchscrollmode", webViewerSwitchScrollMode);
eventBus._off("scrollmodechanged", webViewerScrollModeChanged);
eventBus._off("switchspreadmode", webViewerSwitchSpreadMode);
Expand Down Expand Up @@ -2319,6 +2338,10 @@ function webViewerRotateCw() {
function webViewerRotateCcw() {
PDFViewerApplication.rotatePages(-90);
}
function webViewerOptionalContentConfig(evt) {
PDFViewerApplication.pdfViewer.optionalContentConfig =
evt.optionalContentConfig;
}
function webViewerSwitchScrollMode(evt) {
PDFViewerApplication.pdfViewer.scrollMode = evt.mode;
}
Expand Down Expand Up @@ -2865,7 +2888,7 @@ function apiPageModeToSidebarView(mode) {
case "UseAttachments":
return SidebarView.ATTACHMENTS;
case "UseOC":
// Not implemented, since we don't support Optional Content Groups yet.
return SidebarView.LAYERS;
}
return SidebarView.NONE; // Default value.
}
Expand Down
56 changes: 55 additions & 1 deletion web/base_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* limitations under the License.
*/

import { createPromiseCapability, OptionalContentConfig } from "pdfjs-lib";
import {
CSS_UNITS,
DEFAULT_SCALE,
Expand All @@ -38,7 +39,6 @@ import {
} from "./ui_utils.js";
import { PDFRenderingQueue, RenderingStates } from "./pdf_rendering_queue.js";
import { AnnotationLayerBuilder } from "./annotation_layer_builder.js";
import { createPromiseCapability } from "pdfjs-lib";
import { PDFPageView } from "./pdf_page_view.js";
import { SimpleLinkService } from "./pdf_link_service.js";
import { TextLayerBuilder } from "./text_layer_builder.js";
Expand Down Expand Up @@ -434,6 +434,11 @@ class BaseViewer {
}
const pagesCount = pdfDocument.numPages;
const firstPagePromise = pdfDocument.getPage(1);
const optionalContentConfigPromise = pdfDocument
.getOptionalContentConfig()
.then(optionalContentConfig => {
return (this._optionalContentConfig = optionalContentConfig);
});

this._pagesCapability.promise.then(() => {
this.eventBus.dispatch("pagesloaded", {
Expand Down Expand Up @@ -482,6 +487,7 @@ class BaseViewer {
id: pageNum,
scale,
defaultViewport: viewport.clone(),
optionalContentConfigPromise,
renderingQueue: this.renderingQueue,
textLayerFactory,
textLayerMode: this.textLayerMode,
Expand Down Expand Up @@ -599,6 +605,7 @@ class BaseViewer {
this._buffer = new PDFPageViewBuffer(DEFAULT_CACHE_SIZE);
this._location = null;
this._pagesRotation = 0;
this._optionalContentConfig = null;
this._pagesRequests = new WeakMap();
this._firstPageCapability = createPromiseCapability();
this._onePageRenderedCapability = createPromiseCapability();
Expand Down Expand Up @@ -1215,6 +1222,53 @@ class BaseViewer {
});
}

/**
* @type {OptionalContentConfig}
*/
get optionalContentConfig() {
return this._optionalContentConfig;
}

/**
* @param {OptionalContentConfig} val - An {OptionalContentConfig} instance.
*/
set optionalContentConfig(val) {
if (!(val instanceof OptionalContentConfig)) {
throw new Error(`Invalid optionalContentConfig: ${val}`);
}
if (!this.pdfDocument) {
return;
}
if (!this._optionalContentConfig) {
// Ignore the setter *before* the `onePageRendered` promise has resolved,
// since it'd be overwritten anyway; won't happen in the default viewer.
return;
}
this._optionalContentConfig = val;

const optionalContentConfigPromise = Promise.resolve(val);
for (const pageView of this._pages) {
pageView.update(
pageView.scale,
pageView.rotation,
optionalContentConfigPromise
);
}
this.update();
}

async getOptionalContentConfig() {
if (!this.pdfDocument) {
throw new Error("getOptionalContentConfig - setDocument was not called.");
}
if (!this._optionalContentConfig) {
// Prevent issues if the method is called *before* the `onePageRendered`
// promise has resolved; won't happen in the default viewer.
return this.pdfDocument.getOptionalContentConfig();
}
return this._optionalContentConfig;
}

/**
* @type {number} One of the values in {ScrollMode}.
*/
Expand Down
47 changes: 41 additions & 6 deletions web/firefox_print_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ import { PDFPrintServiceFactory } from "./app.js";
import { shadow } from "pdfjs-lib";

// Creates a placeholder with div and canvas with right size for the page.
function composePage(pdfDocument, pageNumber, size, printContainer) {
function composePage(
pdfDocument,
pageNumber,
size,
printContainer,
optionalContentConfigPromise
) {
const canvas = document.createElement("canvas");

// The size of the canvas in pixels for printing.
Expand Down Expand Up @@ -54,6 +60,7 @@ function composePage(pdfDocument, pageNumber, size, printContainer) {
viewport: pdfPage.getViewport({ scale: 1, rotation: size.rotation }),
intent: "print",
annotationStorage: pdfDocument.annotationStorage.getAll(),
optionalContentConfigPromise,
};
return pdfPage.render(renderContext).promise;
})
Expand All @@ -76,21 +83,39 @@ function composePage(pdfDocument, pageNumber, size, printContainer) {
};
}

function FirefoxPrintService(pdfDocument, pagesOverview, printContainer) {
function FirefoxPrintService(
pdfDocument,
pagesOverview,
printContainer,
optionalContentConfigPromise = null
) {
this.pdfDocument = pdfDocument;
this.pagesOverview = pagesOverview;
this.printContainer = printContainer;
this._optionalContentConfigPromise =
optionalContentConfigPromise || pdfDocument.getOptionalContentConfig();
}

FirefoxPrintService.prototype = {
layout() {
const { pdfDocument, pagesOverview, printContainer } = this;
const {
pdfDocument,
pagesOverview,
printContainer,
_optionalContentConfigPromise,
} = this;

const body = document.querySelector("body");
body.setAttribute("data-pdfjsprinting", true);

for (let i = 0, ii = pagesOverview.length; i < ii; ++i) {
composePage(pdfDocument, i + 1, pagesOverview[i], printContainer);
composePage(
pdfDocument,
i + 1,
pagesOverview[i],
printContainer,
_optionalContentConfigPromise
);
}
},

Expand All @@ -110,8 +135,18 @@ PDFPrintServiceFactory.instance = {
return shadow(this, "supportsPrinting", value);
},

createPrintService(pdfDocument, pagesOverview, printContainer) {
return new FirefoxPrintService(pdfDocument, pagesOverview, printContainer);
createPrintService(
pdfDocument,
pagesOverview,
printContainer,
optionalContentConfigPromise
) {
return new FirefoxPrintService(
pdfDocument,
pagesOverview,
printContainer,
optionalContentConfigPromise
);
},
};

Expand Down
Binary file added web/images/toolbarButton-viewLayers.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added web/images/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit f2bd346

Please sign in to comment.