From 2d51bce941c631c4aaf531711f3ff9c465c96e78 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Tue, 24 Jul 2018 14:31:06 +0200 Subject: [PATCH 1/4] Remove unnecessary `stream.length` check from `PDFDocument.linearization` Note first of all that `PDFDocument` will be initialized with either a `Stream` or a `ChunkedStream`, and that both of these have `length` getters. Secondly, the `PDFDocument` constructor will assert that the `stream` has a non-zero (and positive) length. Hence there's no point in checking `stream.length` in the `linearization` getter. --- src/core/document.js | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/core/document.js b/src/core/document.js index 675e9c1a6d5ea..6832ef2d6d0a6 100644 --- a/src/core/document.js +++ b/src/core/document.js @@ -424,16 +424,14 @@ var PDFDocument = (function PDFDocumentClosure() { }, get linearization() { - var linearization = null; - if (this.stream.length) { - try { - linearization = Linearization.create(this.stream); - } catch (err) { - if (err instanceof MissingDataException) { - throw err; - } - info(err); + let linearization = null; + try { + linearization = Linearization.create(this.stream); + } catch (err) { + if (err instanceof MissingDataException) { + throw err; } + info(err); } // shadow the prototype getter with a data property return shadow(this, 'linearization', linearization); From 8a4466139bdc592cbabf0b9a4963eb4b11295bb8 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Tue, 24 Jul 2018 14:37:43 +0200 Subject: [PATCH 2/4] Simplify the `DocumentInfoValidators` definition With this file now being a proper (ES6) module, it's no longer (technically) necessary for this structure to be lazily initialized. Considering its size, and simplicity, I therefore cannot see the harm in letting `DocumentInfoValidators` just be simple Object instead. While I'm not aware of any bugs caused by the current code, it cannot hurt to add an `isDict` check in `PDFDocument.documentInfo` (since the current code assumes that `infoDict` being defined implies it also being a Dictionary). Finally, the patch also converts a couple of `var` to `let`/`const`. --- src/core/document.js | 35 ++++++++++++++--------------------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/src/core/document.js b/src/core/document.js index 6832ef2d6d0a6..dfc46df98d0cd 100644 --- a/src/core/document.js +++ b/src/core/document.js @@ -377,22 +377,16 @@ var PDFDocument = (function PDFDocumentClosure() { return true; /* found */ } - var DocumentInfoValidators = { - get entries() { - // Lazily build this since all the validation functions below are not - // defined until after this file loads. - return shadow(this, 'entries', { - Title: isString, - Author: isString, - Subject: isString, - Keywords: isString, - Creator: isString, - Producer: isString, - CreationDate: isString, - ModDate: isString, - Trapped: isName, - }); - }, + const DocumentInfoValidators = { + Title: isString, + Author: isString, + Subject: isString, + Keywords: isString, + Creator: isString, + Producer: isString, + CreationDate: isString, + ModDate: isString, + Trapped: isName, }; PDFDocument.prototype = { @@ -555,14 +549,13 @@ var PDFDocument = (function PDFDocumentClosure() { } info('The document information dictionary is invalid.'); } - if (infoDict) { - var validEntries = DocumentInfoValidators.entries; + if (isDict(infoDict)) { // Only fill the document info with valid entries from the spec. - for (var key in validEntries) { + for (let key in DocumentInfoValidators) { if (infoDict.has(key)) { - var value = infoDict.get(key); + const value = infoDict.get(key); // Make sure the value conforms to the spec. - if (validEntries[key](value)) { + if (DocumentInfoValidators[key](value)) { docInfo[key] = (typeof value !== 'string' ? value : stringToPDFString(value)); } else { From 928b89382e6b2350e740c481e7d6f44e8405e43c Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Tue, 24 Jul 2018 15:02:14 +0200 Subject: [PATCH 3/4] [api-minor] Add an `IsLinearized` property to the `PDFDocument.documentInfo` getter, to allow accessing the linearization status through the API (via `PDFDocumentProxy.getMetadata`) There was a (somewhat) recent question on IRC about accessing the linearization status of a PDF document, and this patch contains a simple way to expose that through already existing API methods. Please note that during setup/parsing in `PDFDocument` the linearization data is already being fetched and parsed, provided of course that it exists. Hence this patch will *not* cause any additional data to be loaded. --- src/core/document.js | 5 +++-- test/unit/api_spec.js | 18 +++++++++++++----- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/core/document.js b/src/core/document.js index dfc46df98d0cd..df243e1bba250 100644 --- a/src/core/document.js +++ b/src/core/document.js @@ -535,12 +535,13 @@ var PDFDocument = (function PDFDocumentClosure() { return shadow(this, 'numPages', num); }, get documentInfo() { - var docInfo = { + const docInfo = { PDFFormatVersion: this.pdfFormatVersion, + IsLinearized: !!this.linearization, IsAcroFormPresent: !!this.acroForm, IsXFAPresent: !!this.xfa, }; - var infoDict; + let infoDict; try { infoDict = this.xref.trailer.get('Info'); } catch (err) { diff --git a/test/unit/api_spec.js b/test/unit/api_spec.js index 2f79d4cc35151..1a643e0ae5188 100644 --- a/test/unit/api_spec.js +++ b/test/unit/api_spec.js @@ -28,6 +28,7 @@ import { } from '../../src/display/api'; import { GlobalWorkerOptions } from '../../src/display/worker_options'; import isNodeJS from '../../src/shared/is_node'; +import { Metadata } from '../../src/display/metadata'; describe('api', function() { let basicApiFileName = 'basicapi.pdf'; @@ -802,11 +803,18 @@ describe('api', function() { }); it('gets metadata', function(done) { var promise = doc.getMetadata(); - promise.then(function(metadata) { - expect(metadata.info['Title']).toEqual('Basic API Test'); - expect(metadata.info['PDFFormatVersion']).toEqual('1.7'); - expect(metadata.metadata.get('dc:title')).toEqual('Basic API Test'); - expect(metadata.contentDispositionFilename).toEqual(null); + promise.then(function({ info, metadata, contentDispositionFilename, }) { + expect(info['Title']).toEqual('Basic API Test'); + // The following are PDF.js specific, non-standard, properties. + expect(info['PDFFormatVersion']).toEqual('1.7'); + expect(info['IsLinearized']).toEqual(false); + expect(info['IsAcroFormPresent']).toEqual(false); + expect(info['IsXFAPresent']).toEqual(false); + + expect(metadata instanceof Metadata).toEqual(true); + expect(metadata.get('dc:title')).toEqual('Basic API Test'); + + expect(contentDispositionFilename).toEqual(null); done(); }).catch(function (reason) { done.fail(reason); From 522040d130441a41e985fc86d9202e17eed91d4f Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Tue, 24 Jul 2018 16:24:30 +0200 Subject: [PATCH 4/4] Expose the Linearization status in the document properties dialog This uses the same terminology, i.e. "Fast Web View", as is used by Adobe software. --- l10n/en-US/viewer.properties | 5 +++++ l10n/nl/viewer.properties | 5 +++++ l10n/sv-SE/viewer.properties | 5 +++++ web/pdf_document_properties.js | 13 ++++++++++++- web/viewer.html | 4 ++++ web/viewer.js | 1 + 6 files changed, 32 insertions(+), 1 deletion(-) diff --git a/l10n/en-US/viewer.properties b/l10n/en-US/viewer.properties index af1765fc1348e..3929482459c42 100644 --- a/l10n/en-US/viewer.properties +++ b/l10n/en-US/viewer.properties @@ -120,6 +120,11 @@ document_properties_page_size_dimension_string={{width}} × {{height}} {{unit}} # "{{width}}", "{{height}}", {{unit}}, {{name}}, and {{orientation}} will be replaced by # the size, respectively their unit of measurement, name, and orientation, of the (current) page. document_properties_page_size_dimension_name_string={{width}} × {{height}} {{unit}} ({{name}}, {{orientation}}) +# LOCALIZATION NOTE (document_properties_linearized): The linearization status of +# the document; usually called "Fast Web View" in English locales of Adobe software. +document_properties_linearized=Fast Web View: +document_properties_linearized_yes=Yes +document_properties_linearized_no=No document_properties_close=Close print_progress_message=Preparing document for printing… diff --git a/l10n/nl/viewer.properties b/l10n/nl/viewer.properties index 4a6e985f816b6..0ac2acac8c759 100644 --- a/l10n/nl/viewer.properties +++ b/l10n/nl/viewer.properties @@ -120,6 +120,11 @@ document_properties_page_size_dimension_string={{width}} × {{height}} {{unit}} # "{{width}}", "{{height}}", {{unit}}, {{name}}, and {{orientation}} will be replaced by # the size, respectively their unit of measurement, name, and orientation, of the (current) page. document_properties_page_size_dimension_name_string={{width}} × {{height}} {{unit}} ({{name}}, {{orientation}}) +# LOCALIZATION NOTE (document_properties_linearized): The linearization status of +# the document; usually called "Fast Web View" in English locales of Adobe software. +document_properties_linearized=Snelle webweergave: +document_properties_linearized_yes=Ja +document_properties_linearized_no=Nee document_properties_close=Sluiten print_progress_message=Document voorbereiden voor afdrukken… diff --git a/l10n/sv-SE/viewer.properties b/l10n/sv-SE/viewer.properties index 9de857dacb157..7e5c685af6ae4 100644 --- a/l10n/sv-SE/viewer.properties +++ b/l10n/sv-SE/viewer.properties @@ -120,6 +120,11 @@ document_properties_page_size_dimension_string={{width}} × {{height}} {{unit}} # "{{width}}", "{{height}}", {{unit}}, {{name}}, and {{orientation}} will be replaced by # the size, respectively their unit of measurement, name, and orientation, of the (current) page. document_properties_page_size_dimension_name_string={{width}} × {{height}} {{unit}} ({{name}}, {{orientation}}) +# LOCALIZATION NOTE (document_properties_linearized): The linearization status of +# the document; usually called "Fast Web View" in English locales of Adobe software. +document_properties_linearized=Snabb webbvisning: +document_properties_linearized_yes=Ja +document_properties_linearized_no=Nej document_properties_close=Stäng print_progress_message=Förbereder sidor för utskrift… diff --git a/web/pdf_document_properties.js b/web/pdf_document_properties.js index b5c543d758741..25711af5d2477 100644 --- a/web/pdf_document_properties.js +++ b/web/pdf_document_properties.js @@ -128,9 +128,10 @@ class PDFDocumentProperties { return this._parsePageSize(getPageSizeInches(pdfPage), pagesRotation); }), + this._parseLinearization(info.IsLinearized), ]); }).then(([info, metadata, fileName, fileSize, creationDate, modDate, - pageSize]) => { + pageSize, isLinearized]) => { freezeFieldData({ 'fileName': fileName, 'fileSize': fileSize, @@ -145,6 +146,7 @@ class PDFDocumentProperties { 'version': info.PDFFormatVersion, 'pageCount': this.pdfDocument.numPages, 'pageSize': pageSize, + 'linearized': isLinearized, '_currentPageNumber': currentPageNumber, '_pagesRotation': pagesRotation, }); @@ -406,6 +408,15 @@ class PDFDocumentProperties { { date: dateString, time: timeString, }, '{{date}}, {{time}}'); } + + /** + * @private + */ + _parseLinearization(isLinearized) { + return this.l10n.get('document_properties_linearized_' + + (isLinearized ? 'yes' : 'no'), null, + (isLinearized ? 'Yes' : 'No')); + } } export { diff --git a/web/viewer.html b/web/viewer.html index 5d64c1ed1b834..8f1b9e1f49c44 100644 --- a/web/viewer.html +++ b/web/viewer.html @@ -378,6 +378,10 @@
Page Size:

-

+
+
+ Fast Web View:

-

+
diff --git a/web/viewer.js b/web/viewer.js index 42088f314596d..7a9ae61c0f6b2 100644 --- a/web/viewer.js +++ b/web/viewer.js @@ -166,6 +166,7 @@ function getViewerConfiguration() { 'version': document.getElementById('versionField'), 'pageCount': document.getElementById('pageCountField'), 'pageSize': document.getElementById('pageSizeField'), + 'linearized': document.getElementById('linearizedField'), }, }, errorWrapper: {