Skip to content

Commit

Permalink
Replace the disableTextLayer and enhanceTextSelection options/pre…
Browse files Browse the repository at this point in the history
…ferences 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.
  • Loading branch information
Snuffleupagus committed Feb 13, 2018
1 parent 3a6f6d2 commit a1cfa5f
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 41 deletions.
3 changes: 2 additions & 1 deletion examples/mobile-viewer/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/';
Expand Down Expand Up @@ -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);
Expand Down
19 changes: 10 additions & 9 deletions extensions/chromium/preferences_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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": [
Expand Down
17 changes: 7 additions & 10 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -146,7 +146,7 @@ let PDFViewerApplication = {
disablePageMode: false,
disablePageLabels: false,
renderer: 'canvas',
enhanceTextSelection: false,
textLayerMode: TextLayerMode.ENABLE,
renderInteractiveForms: false,
enablePrintAutoRotate: false,
},
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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':
Expand Down Expand Up @@ -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'],
Expand Down
16 changes: 10 additions & 6 deletions web/base_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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({
Expand All @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions web/default_preferences.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 9 additions & 6 deletions web/pdf_page_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import {
approximateFraction, CSS_UNITS, DEFAULT_SCALE, getOutputScale, NullL10n,
RendererType, roundToDivide
RendererType, roundToDivide, TextLayerMode
} from './ui_utils';
import {
createPromiseCapability, RenderingCancelledException, SVGGraphics
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;

Expand Down
14 changes: 7 additions & 7 deletions web/ui_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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')) {
/**
Expand Down Expand Up @@ -669,6 +668,7 @@ export {
cloneObj,
PresentationModeState,
RendererType,
TextLayerMode,
mozL10n,
NullL10n,
EventBus,
Expand Down

0 comments on commit a1cfa5f

Please sign in to comment.