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.
This commit is contained in:
Jonas Jenwald 2018-02-13 15:01:55 +01:00
parent 3a6f6d23d6
commit a1cfa5f4d7
7 changed files with 46 additions and 41 deletions

View File

@ -22,7 +22,7 @@ if (typeof PDFJS === 'undefined' || !PDFJS.PDFViewer || !PDFJS.getDocument) {
} }
var USE_ONLY_CSS_ZOOM = true; var USE_ONLY_CSS_ZOOM = true;
PDFJS.disableTextLayer = true; var TEXT_LAYER_MODE = 0; // DISABLE
PDFJS.maxImageSize = 1024 * 1024; PDFJS.maxImageSize = 1024 * 1024;
PDFJS.workerSrc = '../../node_modules/pdfjs-dist/build/pdf.worker.js'; PDFJS.workerSrc = '../../node_modules/pdfjs-dist/build/pdf.worker.js';
PDFJS.cMapUrl = '../../node_modules/pdfjs-dist/cmaps/'; PDFJS.cMapUrl = '../../node_modules/pdfjs-dist/cmaps/';
@ -299,6 +299,7 @@ var PDFViewerApplication = {
linkService: linkService, linkService: linkService,
l10n: this.l10n, l10n: this.l10n,
useOnlyCssZoom: USE_ONLY_CSS_ZOOM, useOnlyCssZoom: USE_ONLY_CSS_ZOOM,
textLayerMode: TEXT_LAYER_MODE,
}); });
this.pdfViewer = pdfViewer; this.pdfViewer = pdfViewer;
linkService.setViewer(pdfViewer); linkService.setViewer(pdfViewer);

View File

@ -75,11 +75,16 @@
"type": "boolean", "type": "boolean",
"default": false "default": false
}, },
"disableTextLayer": { "textLayerMode": {
"title": "Disable text selection layer", "title": "Text layer mode",
"description": "Whether to disable the text selection layer.", "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": "boolean", "type": "integer",
"default": false "enum": [
0,
1,
2
],
"default": 1
}, },
"useOnlyCssZoom": { "useOnlyCssZoom": {
"type": "boolean", "type": "boolean",
@ -112,10 +117,6 @@
"description": "Whether to prevent the extension from reporting the extension and browser version to the extension developers.", "description": "Whether to prevent the extension from reporting the extension and browser version to the extension developers.",
"default": false "default": false
}, },
"enhanceTextSelection": {
"type": "boolean",
"default": false
},
"renderer": { "renderer": {
"type": "string", "type": "string",
"enum": [ "enum": [

View File

@ -18,7 +18,7 @@ import {
animationStarted, DEFAULT_SCALE_VALUE, getPDFFileNameFromURL, isFileSchema, animationStarted, DEFAULT_SCALE_VALUE, getPDFFileNameFromURL, isFileSchema,
isValidRotation, MAX_SCALE, MIN_SCALE, noContextMenuHandler, isValidRotation, MAX_SCALE, MIN_SCALE, noContextMenuHandler,
normalizeWheelEventDelta, parseQueryString, PresentationModeState, normalizeWheelEventDelta, parseQueryString, PresentationModeState,
ProgressBar, RendererType ProgressBar, RendererType, TextLayerMode
} from './ui_utils'; } from './ui_utils';
import { import {
build, createBlob, getDocument, getFilenameFromUrl, InvalidPDFException, build, createBlob, getDocument, getFilenameFromUrl, InvalidPDFException,
@ -146,7 +146,7 @@ let PDFViewerApplication = {
disablePageMode: false, disablePageMode: false,
disablePageLabels: false, disablePageLabels: false,
renderer: 'canvas', renderer: 'canvas',
enhanceTextSelection: false, textLayerMode: TextLayerMode.ENABLE,
renderInteractiveForms: false, renderInteractiveForms: false,
enablePrintAutoRotate: false, enablePrintAutoRotate: false,
}, },
@ -217,14 +217,11 @@ let PDFViewerApplication = {
preferences.get('defaultZoomValue').then(function resolved(value) { preferences.get('defaultZoomValue').then(function resolved(value) {
viewerPrefs['defaultZoomValue'] = value; viewerPrefs['defaultZoomValue'] = value;
}), }),
preferences.get('enhanceTextSelection').then(function resolved(value) { preferences.get('textLayerMode').then(function resolved(value) {
viewerPrefs['enhanceTextSelection'] = value; if (viewerPrefs['textLayerMode'] === TextLayerMode.DISABLE) {
}),
preferences.get('disableTextLayer').then(function resolved(value) {
if (PDFJS.disableTextLayer === true) {
return; return;
} }
PDFJS.disableTextLayer = value; viewerPrefs['textLayerMode'] = value;
}), }),
preferences.get('disableRange').then(function resolved(value) { preferences.get('disableRange').then(function resolved(value) {
if (PDFJS.disableRange === true) { if (PDFJS.disableRange === true) {
@ -323,7 +320,7 @@ let PDFViewerApplication = {
if ('textlayer' in hashParams) { if ('textlayer' in hashParams) {
switch (hashParams['textlayer']) { switch (hashParams['textlayer']) {
case 'off': case 'off':
PDFJS.disableTextLayer = true; viewerPrefs['textLayerMode'] = TextLayerMode.DISABLE;
break; break;
case 'visible': case 'visible':
case 'shadow': case 'shadow':
@ -398,7 +395,7 @@ let PDFViewerApplication = {
downloadManager, downloadManager,
renderer: viewerPrefs['renderer'], renderer: viewerPrefs['renderer'],
l10n: this.l10n, l10n: this.l10n,
enhanceTextSelection: viewerPrefs['enhanceTextSelection'], textLayerMode: viewerPrefs['textLayerMode'],
imageResourcesPath: PDFJS.imageResourcesPath, imageResourcesPath: PDFJS.imageResourcesPath,
renderInteractiveForms: viewerPrefs['renderInteractiveForms'], renderInteractiveForms: viewerPrefs['renderInteractiveForms'],
enablePrintAutoRotate: viewerPrefs['enablePrintAutoRotate'], enablePrintAutoRotate: viewerPrefs['enablePrintAutoRotate'],

View File

@ -17,7 +17,7 @@ import { createPromiseCapability, PDFJS } from 'pdfjs-lib';
import { import {
CSS_UNITS, DEFAULT_SCALE, DEFAULT_SCALE_VALUE, isValidRotation, CSS_UNITS, DEFAULT_SCALE, DEFAULT_SCALE_VALUE, isValidRotation,
MAX_AUTO_SCALE, NullL10n, PresentationModeState, RendererType, MAX_AUTO_SCALE, NullL10n, PresentationModeState, RendererType,
SCROLLBAR_PADDING, UNKNOWN_SCALE, VERTICAL_PADDING, watchScroll SCROLLBAR_PADDING, TextLayerMode, UNKNOWN_SCALE, VERTICAL_PADDING, watchScroll
} from './ui_utils'; } from './ui_utils';
import { PDFRenderingQueue, RenderingStates } from './pdf_rendering_queue'; import { PDFRenderingQueue, RenderingStates } from './pdf_rendering_queue';
import { AnnotationLayerBuilder } from './annotation_layer_builder'; import { AnnotationLayerBuilder } from './annotation_layer_builder';
@ -39,9 +39,11 @@ const DEFAULT_CACHE_SIZE = 10;
* @property {PDFRenderingQueue} renderingQueue - (optional) The rendering * @property {PDFRenderingQueue} renderingQueue - (optional) The rendering
* queue object. * queue object.
* @property {boolean} removePageBorders - (optional) Removes the border shadow * @property {boolean} removePageBorders - (optional) Removes the border shadow
* around the pages. The default is false. * around the pages. The default value is `false`.
* @property {boolean} enhanceTextSelection - (optional) Enables the improved * @property {number} textLayerMode - (optional) Controls if the text layer used
* text selection behaviour. The default is `false`. * 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, * @property {string} imageResourcesPath - (optional) Path for image resources,
* mainly for annotation icons. Include trailing slash. * mainly for annotation icons. Include trailing slash.
* @property {boolean} renderInteractiveForms - (optional) Enables rendering of * @property {boolean} renderInteractiveForms - (optional) Enables rendering of
@ -114,6 +116,8 @@ class BaseViewer {
this.linkService = options.linkService || new SimpleLinkService(); this.linkService = options.linkService || new SimpleLinkService();
this.downloadManager = options.downloadManager || null; this.downloadManager = options.downloadManager || null;
this.removePageBorders = options.removePageBorders || false; this.removePageBorders = options.removePageBorders || false;
this.textLayerMode = Number.isInteger(options.textLayerMode) ?
options.textLayerMode : TextLayerMode.ENABLE;
this.enhanceTextSelection = options.enhanceTextSelection || false; this.enhanceTextSelection = options.enhanceTextSelection || false;
this.imageResourcesPath = options.imageResourcesPath || ''; this.imageResourcesPath = options.imageResourcesPath || '';
this.renderInteractiveForms = options.renderInteractiveForms || false; this.renderInteractiveForms = options.renderInteractiveForms || false;
@ -372,7 +376,7 @@ class BaseViewer {
let viewport = pdfPage.getViewport(scale * CSS_UNITS); let viewport = pdfPage.getViewport(scale * CSS_UNITS);
for (let pageNum = 1; pageNum <= pagesCount; ++pageNum) { for (let pageNum = 1; pageNum <= pagesCount; ++pageNum) {
let textLayerFactory = null; let textLayerFactory = null;
if (!PDFJS.disableTextLayer) { if (this.textLayerMode !== TextLayerMode.DISABLE) {
textLayerFactory = this; textLayerFactory = this;
} }
let pageView = new PDFPageView({ let pageView = new PDFPageView({
@ -383,8 +387,8 @@ class BaseViewer {
defaultViewport: viewport.clone(), defaultViewport: viewport.clone(),
renderingQueue: this.renderingQueue, renderingQueue: this.renderingQueue,
textLayerFactory, textLayerFactory,
textLayerMode: this.textLayerMode,
annotationLayerFactory: this, annotationLayerFactory: this,
enhanceTextSelection: this.enhanceTextSelection,
imageResourcesPath: this.imageResourcesPath, imageResourcesPath: this.imageResourcesPath,
renderInteractiveForms: this.renderInteractiveForms, renderInteractiveForms: this.renderInteractiveForms,
renderer: this.renderer, renderer: this.renderer,

View File

@ -10,10 +10,9 @@
"disableStream": false, "disableStream": false,
"disableAutoFetch": false, "disableAutoFetch": false,
"disableFontFace": false, "disableFontFace": false,
"disableTextLayer": false, "textLayerMode": 1,
"useOnlyCssZoom": false, "useOnlyCssZoom": false,
"externalLinkTarget": 0, "externalLinkTarget": 0,
"enhanceTextSelection": false,
"renderer": "canvas", "renderer": "canvas",
"renderInteractiveForms": false, "renderInteractiveForms": false,
"enablePrintAutoRotate": false, "enablePrintAutoRotate": false,

View File

@ -15,7 +15,7 @@
import { import {
approximateFraction, CSS_UNITS, DEFAULT_SCALE, getOutputScale, NullL10n, approximateFraction, CSS_UNITS, DEFAULT_SCALE, getOutputScale, NullL10n,
RendererType, roundToDivide RendererType, roundToDivide, TextLayerMode
} from './ui_utils'; } from './ui_utils';
import { import {
createPromiseCapability, RenderingCancelledException, SVGGraphics createPromiseCapability, RenderingCancelledException, SVGGraphics
@ -33,9 +33,11 @@ import { viewerCompatibilityParams } from './viewer_compatibility';
* @property {PageViewport} defaultViewport - The page viewport. * @property {PageViewport} defaultViewport - The page viewport.
* @property {PDFRenderingQueue} renderingQueue - The rendering queue object. * @property {PDFRenderingQueue} renderingQueue - The rendering queue object.
* @property {IPDFTextLayerFactory} textLayerFactory * @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 {IPDFAnnotationLayerFactory} annotationLayerFactory
* @property {boolean} enhanceTextSelection - Turns on the text selection
* enhancement. The default is `false`.
* @property {string} imageResourcesPath - (optional) Path for image resources, * @property {string} imageResourcesPath - (optional) Path for image resources,
* mainly for annotation icons. Include trailing slash. * mainly for annotation icons. Include trailing slash.
* @property {boolean} renderInteractiveForms - Turns on rendering of * @property {boolean} renderInteractiveForms - Turns on rendering of
@ -72,7 +74,8 @@ class PDFPageView {
this.viewport = defaultViewport; this.viewport = defaultViewport;
this.pdfPageRotate = defaultViewport.rotation; this.pdfPageRotate = defaultViewport.rotation;
this.hasRestrictedScaling = false; this.hasRestrictedScaling = false;
this.enhanceTextSelection = options.enhanceTextSelection || false; this.textLayerMode = Number.isInteger(options.textLayerMode) ?
options.textLayerMode : TextLayerMode.ENABLE;
this.imageResourcesPath = options.imageResourcesPath || ''; this.imageResourcesPath = options.imageResourcesPath || '';
this.renderInteractiveForms = options.renderInteractiveForms || false; this.renderInteractiveForms = options.renderInteractiveForms || false;
this.useOnlyCssZoom = options.useOnlyCssZoom || false; this.useOnlyCssZoom = options.useOnlyCssZoom || false;
@ -384,7 +387,7 @@ class PDFPageView {
} }
let textLayer = null; let textLayer = null;
if (this.textLayerFactory) { if (this.textLayerMode !== TextLayerMode.DISABLE && this.textLayerFactory) {
let textLayerDiv = document.createElement('div'); let textLayerDiv = document.createElement('div');
textLayerDiv.className = 'textLayer'; textLayerDiv.className = 'textLayer';
textLayerDiv.style.width = canvasWrapper.style.width; textLayerDiv.style.width = canvasWrapper.style.width;
@ -398,7 +401,7 @@ class PDFPageView {
textLayer = this.textLayerFactory. textLayer = this.textLayerFactory.
createTextLayerBuilder(textLayerDiv, this.id - 1, this.viewport, createTextLayerBuilder(textLayerDiv, this.id - 1, this.viewport,
this.enhanceTextSelection); this.textLayerMode === TextLayerMode.ENABLE_ENHANCE);
} }
this.textLayer = textLayer; this.textLayer = textLayer;

View File

@ -37,6 +37,12 @@ const RendererType = {
SVG: 'svg', SVG: 'svg',
}; };
const TextLayerMode = {
DISABLE: 0,
ENABLE: 1,
ENABLE_ENHANCE: 2,
};
// Replaces {{arguments}} with their values. // Replaces {{arguments}} with their values.
function formatL10nValue(text, args) { function formatL10nValue(text, args) {
if (!args) { if (!args) {
@ -87,13 +93,6 @@ PDFJS.maxCanvasPixels = (PDFJS.maxCanvasPixels === undefined ?
PDFJS.disableHistory = (PDFJS.disableHistory === undefined ? PDFJS.disableHistory = (PDFJS.disableHistory === undefined ?
false : PDFJS.disableHistory); 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' || if (typeof PDFJSDev === 'undefined' ||
!PDFJSDev.test('FIREFOX || MOZCENTRAL')) { !PDFJSDev.test('FIREFOX || MOZCENTRAL')) {
/** /**
@ -669,6 +668,7 @@ export {
cloneObj, cloneObj,
PresentationModeState, PresentationModeState,
RendererType, RendererType,
TextLayerMode,
mozL10n, mozL10n,
NullL10n, NullL10n,
EventBus, EventBus,