Skip to content

Commit

Permalink
[api-minor] Add support for toggling of Optional Content in the viewe…
Browse files Browse the repository at this point in the history
…r (issue 12096)

*Besides, obviously, adding viewer support:* This patch attempts to improve the general API for Optional Content Groups slightly, by adding a couple of new methods for interacting with the (more complex) data structures of `OptionalContentConfig`-instances. (Thus allowing us to mark some of the data as "private", given that it probably shouldn't be manipulated directly.)

By utilizing not just the "raw" Optional Content Groups, but the data from the `/Order` array when available, we can thus display the Layers in a proper tree-structure with collapsible headings for PDF documents that utilizes that feature.

Note that it's possible to reset all Optional Content Groups to their default visibility state, simply by double-clicking on the Layers-button in the sidebar.
(Currently that's indicated in the Layers-button tooltip, which is obviously easy to overlook, however it's probably the best we can do for now without adding more buttons, or even a dropdown-toolbar, to the sidebar.)

Also, the current Layers-button icons are a little rough around the edges, quite literally, but given that the viewer will soon have its UI modernized anyway they hopefully suffice in the meantime.

To give users *full* control of the visibility of the various Optional Content Groups, even those which according to the `/Order` array should not (by default) be toggleable in the UI, this patch will place those under a *custom* heading which:
 - Is collapsed by default, and placed at the bottom of the Layers-tree, to be a bit less obtrusive.
 - Uses a slightly different formatting, compared to the "regular" headings.
 - Is localizable.
  • Loading branch information
Snuffleupagus committed Aug 7, 2020
1 parent 0d933be commit d5b1668
Show file tree
Hide file tree
Showing 16 changed files with 455 additions and 34 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
59 changes: 44 additions & 15 deletions src/display/optional_content_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class OptionalContentConfig {
this.name = null;
this.creator = null;
this._order = null;
this.groups = new Map();
this._groups = new Map();

if (data === null) {
return;
Expand All @@ -36,34 +36,34 @@ class OptionalContentConfig {
this.creator = data.creator;
this._order = data.order;
for (const group of data.groups) {
this.groups.set(
this._groups.set(
group.id,
new OptionalContentGroup(group.name, group.intent)
);
}

if (data.baseState === "OFF") {
for (const group of this.groups) {
for (const group of this._groups) {
group.visible = false;
}
}

for (const on of data.on) {
this.groups.get(on).visible = true;
this._groups.get(on).visible = true;
}

for (const off of data.off) {
this.groups.get(off).visible = false;
this._groups.get(off).visible = false;
}
}

isVisible(group) {
if (group.type === "OCG") {
if (!this.groups.has(group.id)) {
if (!this._groups.has(group.id)) {
warn(`Optional content group not found: ${group.id}`);
return true;
}
return this.groups.get(group.id).visible;
return this._groups.get(group.id).visible;
} else if (group.type === "OCMD") {
// Per the spec, the expression should be preferred if available. Until
// we implement this, just fallback to using the group policy for now.
Expand All @@ -73,44 +73,44 @@ class OptionalContentConfig {
if (!group.policy || group.policy === "AnyOn") {
// Default
for (const id of group.ids) {
if (!this.groups.has(id)) {
if (!this._groups.has(id)) {
warn(`Optional content group not found: ${id}`);
return true;
}
if (this.groups.get(id).visible) {
if (this._groups.get(id).visible) {
return true;
}
}
return false;
} else if (group.policy === "AllOn") {
for (const id of group.ids) {
if (!this.groups.has(id)) {
if (!this._groups.has(id)) {
warn(`Optional content group not found: ${id}`);
return true;
}
if (!this.groups.get(id).visible) {
if (!this._groups.get(id).visible) {
return false;
}
}
return true;
} else if (group.policy === "AnyOff") {
for (const id of group.ids) {
if (!this.groups.has(id)) {
if (!this._groups.has(id)) {
warn(`Optional content group not found: ${id}`);
return true;
}
if (!this.groups.get(id).visible) {
if (!this._groups.get(id).visible) {
return true;
}
}
return false;
} else if (group.policy === "AllOff") {
for (const id of group.ids) {
if (!this.groups.has(id)) {
if (!this._groups.has(id)) {
warn(`Optional content group not found: ${id}`);
return true;
}
if (this.groups.get(id).visible) {
if (this._groups.get(id).visible) {
return false;
}
}
Expand All @@ -122,6 +122,35 @@ 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;
}

getOrder() {
if (!this._groups.size) {
return null;
}
if (this._order) {
return this._order.slice();
}
return Array.from(this._groups.keys());
}

getGroups() {
if (!this._groups.size) {
return null;
}
return Object.fromEntries(this._groups);
}

getGroup(id) {
return this._groups.get(id) || null;
}
}

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
32 changes: 29 additions & 3 deletions 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 @@ -1610,12 +1625,14 @@ const PDFViewerApplication = {
const pagesOverview = this.pdfViewer.getPagesOverview();
const printContainer = this.appConfig.printContainer;
const printResolution = AppOptions.get("printResolution");
const optionalContentConfigPromise = this.pdfViewer.getOptionalContentConfig();

const printService = PDFPrintServiceFactory.instance.createPrintService(
this.pdfDocument,
pagesOverview,
printContainer,
printResolution,
optionalContentConfigPromise,
this.l10n
);
this.printService = printService;
Expand Down Expand Up @@ -1686,6 +1703,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 @@ -1761,6 +1779,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 @@ -2084,12 +2103,15 @@ function webViewerPageMode({ mode }) {
view = SidebarView.THUMBS;
break;
case "bookmarks":
case "outline":
case "outline": // non-standard
view = SidebarView.OUTLINE;
break;
case "attachments":
case "attachments": // non-standard
view = SidebarView.ATTACHMENTS;
break;
case "layers": // non-standard
view = SidebarView.LAYERS;
break;
case "none":
view = SidebarView.NONE;
break;
Expand Down Expand Up @@ -2322,6 +2344,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 @@ -2868,7 +2894,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
Loading

0 comments on commit d5b1668

Please sign in to comment.