From a1cfa5f4d7c8fcf55e9f3b51a23885dca8782915 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Tue, 13 Feb 2018 15:01:55 +0100 Subject: [PATCH] Replace the `disableTextLayer` and `enhanceTextSelection` options/preferences with a single `textLayerMode` option/preference Rather than having two different (but connected) options for the textLayer, I think that it makes sense to try and unify this. For example: currently if `disableTextLayer === true`, then the value of `enhanceTextSelection` is simply ignored. Since PDF.js version `2.0` already won't be backwards compatible in lots of ways, I don't think that we need to worry about migrating existing preferences here. --- examples/mobile-viewer/viewer.js | 3 ++- extensions/chromium/preferences_schema.json | 19 ++++++++++--------- web/app.js | 17 +++++++---------- web/base_viewer.js | 16 ++++++++++------ web/default_preferences.json | 3 +-- web/pdf_page_view.js | 15 +++++++++------ web/ui_utils.js | 14 +++++++------- 7 files changed, 46 insertions(+), 41 deletions(-) diff --git a/examples/mobile-viewer/viewer.js b/examples/mobile-viewer/viewer.js index 01d2a097e..dc3276ce4 100644 --- a/examples/mobile-viewer/viewer.js +++ b/examples/mobile-viewer/viewer.js @@ -22,7 +22,7 @@ if (typeof PDFJS === 'undefined' || !PDFJS.PDFViewer || !PDFJS.getDocument) { } var USE_ONLY_CSS_ZOOM = true; -PDFJS.disableTextLayer = true; +var TEXT_LAYER_MODE = 0; // DISABLE PDFJS.maxImageSize = 1024 * 1024; PDFJS.workerSrc = '../../node_modules/pdfjs-dist/build/pdf.worker.js'; PDFJS.cMapUrl = '../../node_modules/pdfjs-dist/cmaps/'; @@ -299,6 +299,7 @@ var PDFViewerApplication = { linkService: linkService, l10n: this.l10n, useOnlyCssZoom: USE_ONLY_CSS_ZOOM, + textLayerMode: TEXT_LAYER_MODE, }); this.pdfViewer = pdfViewer; linkService.setViewer(pdfViewer); diff --git a/extensions/chromium/preferences_schema.json b/extensions/chromium/preferences_schema.json index 16cf475eb..1ccf98db7 100644 --- a/extensions/chromium/preferences_schema.json +++ b/extensions/chromium/preferences_schema.json @@ -75,11 +75,16 @@ "type": "boolean", "default": false }, - "disableTextLayer": { - "title": "Disable text selection layer", - "description": "Whether to disable the text selection layer.", - "type": "boolean", - "default": false + "textLayerMode": { + "title": "Text layer mode", + "description": "Controls if the text layer is enabled, and the selection mode that is used.\n 0 = Disabled.\n 1 = Enabled.\n 2 = (Experimental) Enabled, with enhanced text selection.", + "type": "integer", + "enum": [ + 0, + 1, + 2 + ], + "default": 1 }, "useOnlyCssZoom": { "type": "boolean", @@ -112,10 +117,6 @@ "description": "Whether to prevent the extension from reporting the extension and browser version to the extension developers.", "default": false }, - "enhanceTextSelection": { - "type": "boolean", - "default": false - }, "renderer": { "type": "string", "enum": [ diff --git a/web/app.js b/web/app.js index df983f181..cd4cc1495 100644 --- a/web/app.js +++ b/web/app.js @@ -18,7 +18,7 @@ import { animationStarted, DEFAULT_SCALE_VALUE, getPDFFileNameFromURL, isFileSchema, isValidRotation, MAX_SCALE, MIN_SCALE, noContextMenuHandler, normalizeWheelEventDelta, parseQueryString, PresentationModeState, - ProgressBar, RendererType + ProgressBar, RendererType, TextLayerMode } from './ui_utils'; import { build, createBlob, getDocument, getFilenameFromUrl, InvalidPDFException, @@ -146,7 +146,7 @@ let PDFViewerApplication = { disablePageMode: false, disablePageLabels: false, renderer: 'canvas', - enhanceTextSelection: false, + textLayerMode: TextLayerMode.ENABLE, renderInteractiveForms: false, enablePrintAutoRotate: false, }, @@ -217,14 +217,11 @@ let PDFViewerApplication = { preferences.get('defaultZoomValue').then(function resolved(value) { viewerPrefs['defaultZoomValue'] = value; }), - preferences.get('enhanceTextSelection').then(function resolved(value) { - viewerPrefs['enhanceTextSelection'] = value; - }), - preferences.get('disableTextLayer').then(function resolved(value) { - if (PDFJS.disableTextLayer === true) { + preferences.get('textLayerMode').then(function resolved(value) { + if (viewerPrefs['textLayerMode'] === TextLayerMode.DISABLE) { return; } - PDFJS.disableTextLayer = value; + viewerPrefs['textLayerMode'] = value; }), preferences.get('disableRange').then(function resolved(value) { if (PDFJS.disableRange === true) { @@ -323,7 +320,7 @@ let PDFViewerApplication = { if ('textlayer' in hashParams) { switch (hashParams['textlayer']) { case 'off': - PDFJS.disableTextLayer = true; + viewerPrefs['textLayerMode'] = TextLayerMode.DISABLE; break; case 'visible': case 'shadow': @@ -398,7 +395,7 @@ let PDFViewerApplication = { downloadManager, renderer: viewerPrefs['renderer'], l10n: this.l10n, - enhanceTextSelection: viewerPrefs['enhanceTextSelection'], + textLayerMode: viewerPrefs['textLayerMode'], imageResourcesPath: PDFJS.imageResourcesPath, renderInteractiveForms: viewerPrefs['renderInteractiveForms'], enablePrintAutoRotate: viewerPrefs['enablePrintAutoRotate'], diff --git a/web/base_viewer.js b/web/base_viewer.js index de63579e3..b9e2e2591 100644 --- a/web/base_viewer.js +++ b/web/base_viewer.js @@ -17,7 +17,7 @@ import { createPromiseCapability, PDFJS } from 'pdfjs-lib'; import { CSS_UNITS, DEFAULT_SCALE, DEFAULT_SCALE_VALUE, isValidRotation, MAX_AUTO_SCALE, NullL10n, PresentationModeState, RendererType, - SCROLLBAR_PADDING, UNKNOWN_SCALE, VERTICAL_PADDING, watchScroll + SCROLLBAR_PADDING, TextLayerMode, UNKNOWN_SCALE, VERTICAL_PADDING, watchScroll } from './ui_utils'; import { PDFRenderingQueue, RenderingStates } from './pdf_rendering_queue'; import { AnnotationLayerBuilder } from './annotation_layer_builder'; @@ -39,9 +39,11 @@ const DEFAULT_CACHE_SIZE = 10; * @property {PDFRenderingQueue} renderingQueue - (optional) The rendering * queue object. * @property {boolean} removePageBorders - (optional) Removes the border shadow - * around the pages. The default is false. - * @property {boolean} enhanceTextSelection - (optional) Enables the improved - * text selection behaviour. The default is `false`. + * around the pages. The default value is `false`. + * @property {number} textLayerMode - (optional) Controls if the text layer used + * for selection and searching is created, and if the improved text selection + * behaviour is enabled. The constants from {TextLayerMode} should be used. + * The default value is `TextLayerMode.ENABLE`. * @property {string} imageResourcesPath - (optional) Path for image resources, * mainly for annotation icons. Include trailing slash. * @property {boolean} renderInteractiveForms - (optional) Enables rendering of @@ -114,6 +116,8 @@ class BaseViewer { this.linkService = options.linkService || new SimpleLinkService(); this.downloadManager = options.downloadManager || null; this.removePageBorders = options.removePageBorders || false; + this.textLayerMode = Number.isInteger(options.textLayerMode) ? + options.textLayerMode : TextLayerMode.ENABLE; this.enhanceTextSelection = options.enhanceTextSelection || false; this.imageResourcesPath = options.imageResourcesPath || ''; this.renderInteractiveForms = options.renderInteractiveForms || false; @@ -372,7 +376,7 @@ class BaseViewer { let viewport = pdfPage.getViewport(scale * CSS_UNITS); for (let pageNum = 1; pageNum <= pagesCount; ++pageNum) { let textLayerFactory = null; - if (!PDFJS.disableTextLayer) { + if (this.textLayerMode !== TextLayerMode.DISABLE) { textLayerFactory = this; } let pageView = new PDFPageView({ @@ -383,8 +387,8 @@ class BaseViewer { defaultViewport: viewport.clone(), renderingQueue: this.renderingQueue, textLayerFactory, + textLayerMode: this.textLayerMode, annotationLayerFactory: this, - enhanceTextSelection: this.enhanceTextSelection, imageResourcesPath: this.imageResourcesPath, renderInteractiveForms: this.renderInteractiveForms, renderer: this.renderer, diff --git a/web/default_preferences.json b/web/default_preferences.json index 425de07c3..9aa83132a 100644 --- a/web/default_preferences.json +++ b/web/default_preferences.json @@ -10,10 +10,9 @@ "disableStream": false, "disableAutoFetch": false, "disableFontFace": false, - "disableTextLayer": false, + "textLayerMode": 1, "useOnlyCssZoom": false, "externalLinkTarget": 0, - "enhanceTextSelection": false, "renderer": "canvas", "renderInteractiveForms": false, "enablePrintAutoRotate": false, diff --git a/web/pdf_page_view.js b/web/pdf_page_view.js index 644857b21..65e1b46af 100644 --- a/web/pdf_page_view.js +++ b/web/pdf_page_view.js @@ -15,7 +15,7 @@ import { approximateFraction, CSS_UNITS, DEFAULT_SCALE, getOutputScale, NullL10n, - RendererType, roundToDivide + RendererType, roundToDivide, TextLayerMode } from './ui_utils'; import { createPromiseCapability, RenderingCancelledException, SVGGraphics @@ -33,9 +33,11 @@ import { viewerCompatibilityParams } from './viewer_compatibility'; * @property {PageViewport} defaultViewport - The page viewport. * @property {PDFRenderingQueue} renderingQueue - The rendering queue object. * @property {IPDFTextLayerFactory} textLayerFactory + * @property {number} textLayerMode - (optional) Controls if the text layer used + * for selection and searching is created, and if the improved text selection + * behaviour is enabled. The constants from {TextLayerMode} should be used. + * The default value is `TextLayerMode.ENABLE`. * @property {IPDFAnnotationLayerFactory} annotationLayerFactory - * @property {boolean} enhanceTextSelection - Turns on the text selection - * enhancement. The default is `false`. * @property {string} imageResourcesPath - (optional) Path for image resources, * mainly for annotation icons. Include trailing slash. * @property {boolean} renderInteractiveForms - Turns on rendering of @@ -72,7 +74,8 @@ class PDFPageView { this.viewport = defaultViewport; this.pdfPageRotate = defaultViewport.rotation; this.hasRestrictedScaling = false; - this.enhanceTextSelection = options.enhanceTextSelection || false; + this.textLayerMode = Number.isInteger(options.textLayerMode) ? + options.textLayerMode : TextLayerMode.ENABLE; this.imageResourcesPath = options.imageResourcesPath || ''; this.renderInteractiveForms = options.renderInteractiveForms || false; this.useOnlyCssZoom = options.useOnlyCssZoom || false; @@ -384,7 +387,7 @@ class PDFPageView { } let textLayer = null; - if (this.textLayerFactory) { + if (this.textLayerMode !== TextLayerMode.DISABLE && this.textLayerFactory) { let textLayerDiv = document.createElement('div'); textLayerDiv.className = 'textLayer'; textLayerDiv.style.width = canvasWrapper.style.width; @@ -398,7 +401,7 @@ class PDFPageView { textLayer = this.textLayerFactory. createTextLayerBuilder(textLayerDiv, this.id - 1, this.viewport, - this.enhanceTextSelection); + this.textLayerMode === TextLayerMode.ENABLE_ENHANCE); } this.textLayer = textLayer; diff --git a/web/ui_utils.js b/web/ui_utils.js index 9119147cb..ec45ac04c 100644 --- a/web/ui_utils.js +++ b/web/ui_utils.js @@ -37,6 +37,12 @@ const RendererType = { SVG: 'svg', }; +const TextLayerMode = { + DISABLE: 0, + ENABLE: 1, + ENABLE_ENHANCE: 2, +}; + // Replaces {{arguments}} with their values. function formatL10nValue(text, args) { if (!args) { @@ -87,13 +93,6 @@ PDFJS.maxCanvasPixels = (PDFJS.maxCanvasPixels === undefined ? PDFJS.disableHistory = (PDFJS.disableHistory === undefined ? false : PDFJS.disableHistory); -/** - * Disables creation of the text layer that used for text selection and search. - * @var {boolean} - */ -PDFJS.disableTextLayer = (PDFJS.disableTextLayer === undefined ? - false : PDFJS.disableTextLayer); - if (typeof PDFJSDev === 'undefined' || !PDFJSDev.test('FIREFOX || MOZCENTRAL')) { /** @@ -669,6 +668,7 @@ export { cloneObj, PresentationModeState, RendererType, + TextLayerMode, mozL10n, NullL10n, EventBus,