From f3b74c5028a9ee24fa5fbe3db2595390af959347 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Tue, 20 Mar 2018 13:51:56 +0100 Subject: [PATCH] Display the pageSize, in the document properties dialog, with locale dependent units This uses a whitelist, based on the locale, to determine where non-metric units should be used. Note that the behaviour implemented here seem consistent with desktop PDF viewers (e.g. Adobe Reader), where the pageSizes are *always* displayed with locale dependent units rather than pageSize dependent ones (since the latter would probably be quite confusing). --- web/pdf_document_properties.js | 55 +++++++++++++++++++--------------- web/viewer.html | 6 +--- web/viewer.js | 3 +- 3 files changed, 33 insertions(+), 31 deletions(-) diff --git a/web/pdf_document_properties.js b/web/pdf_document_properties.js index 731dd5c9e..f65a10656 100644 --- a/web/pdf_document_properties.js +++ b/web/pdf_document_properties.js @@ -20,6 +20,9 @@ import { createPromiseCapability } from 'pdfjs-lib'; const DEFAULT_FIELD_CONTENT = '-'; +// See https://en.wikibooks.org/wiki/Lentis/Conversion_to_the_Metric_Standard_in_the_United_States +const NON_METRIC_LOCALES = ['en-us', 'en-lr', 'my']; + /** * @typedef {Object} PDFDocumentPropertiesOptions * @property {string} overlayName - Name/identifier for the overlay. @@ -59,6 +62,11 @@ class PDFDocumentProperties { this._pagesRotation = evt.pagesRotation; }); } + + this._isNonMetricLocale = true; // The default viewer locale is 'en-us'. + l10n.getLanguage().then((locale) => { + this._isNonMetricLocale = NON_METRIC_LOCALES.includes(locale); + }); } /** @@ -104,7 +112,7 @@ class PDFDocumentProperties { }), ]); }).then(([info, metadata, fileName, fileSize, creationDate, modDate, - pageSizes]) => { + pageSize]) => { freezeFieldData({ 'fileName': fileName, 'fileSize': fileSize, @@ -118,8 +126,7 @@ class PDFDocumentProperties { 'producer': info.Producer, 'version': info.PDFFormatVersion, 'pageCount': this.pdfDocument.numPages, - 'pageSizeInch': pageSizes.inch, - 'pageSizeMM': pageSizes.mm, + 'pageSize': pageSize, '_currentPageNumber': currentPageNumber, '_pagesRotation': pagesRotation, }); @@ -250,7 +257,7 @@ class PDFDocumentProperties { */ _parsePageSize(pageSizeInches, pagesRotation) { if (!pageSizeInches) { - return Promise.resolve({ inch: undefined, mm: undefined, }); + return Promise.resolve(undefined); } // Take the viewer rotation into account as well; compare with Adobe Reader. if (pagesRotation % 180 !== 0) { @@ -259,28 +266,28 @@ class PDFDocumentProperties { height: pageSizeInches.width, }; } - const { width, height, } = pageSizeInches; + + const sizeInches = { + width: Math.round(pageSizeInches.width * 100) / 100, + height: Math.round(pageSizeInches.height * 100) / 100, + }; + // 1in == 25.4mm; no need to round to 2 decimals for millimeters. + const sizeMillimeters = { + width: Math.round(pageSizeInches.width * 25.4 * 10) / 10, + height: Math.round(pageSizeInches.height * 25.4 * 10) / 10, + }; return Promise.all([ - this.l10n.get('document_properties_page_size_unit_inches', null, 'in'), - this.l10n.get('document_properties_page_size_unit_millimeters', null, - 'mm'), - ]).then(([unitInches, unitMillimeters]) => { - return Promise.all([ - this.l10n.get('document_properties_page_size_dimension_string', { - width: (Math.round(width * 100) / 100).toLocaleString(), - height: (Math.round(height * 100) / 100).toLocaleString(), - unit: unitInches, - }, '{{width}} × {{height}} {{unit}}'), - // 1in == 25.4mm; no need to round to 2 decimals for millimeters. - this.l10n.get('document_properties_page_size_dimension_string', { - width: (Math.round(width * 25.4 * 10) / 10).toLocaleString(), - height: (Math.round(height * 25.4 * 10) / 10).toLocaleString(), - unit: unitMillimeters, - }, '{{width}} × {{height}} {{unit}}'), - ]); - }).then((sizes) => { - return { inch: sizes[0], mm: sizes[1], }; + (this._isNonMetricLocale ? sizeInches : sizeMillimeters), + this.l10n.get('document_properties_page_size_unit_' + + (this._isNonMetricLocale ? 'inches' : 'millimeters'), null, + this._isNonMetricLocale ? 'in' : 'mm'), + ]).then(([{ width, height, }, unit]) => { + return this.l10n.get('document_properties_page_size_dimension_string', { + width: width.toLocaleString(), + height: height.toLocaleString(), + unit, + }, '{{width}} × {{height}} {{unit}}'); }); } diff --git a/web/viewer.html b/web/viewer.html index 7baec33e2..2d460456c 100644 --- a/web/viewer.html +++ b/web/viewer.html @@ -352,11 +352,7 @@ See https://github.com/adobe-type-tools/cmap-resources Page Count:

-

- Page Size: -

- -
- - -

+ Page Size:

-

diff --git a/web/viewer.js b/web/viewer.js index 242bf1df6..632a217f7 100644 --- a/web/viewer.js +++ b/web/viewer.js @@ -159,8 +159,7 @@ function getViewerConfiguration() { 'producer': document.getElementById('producerField'), 'version': document.getElementById('versionField'), 'pageCount': document.getElementById('pageCountField'), - 'pageSizeInch': document.getElementById('pageSizeFieldInch'), - 'pageSizeMM': document.getElementById('pageSizeFieldMM'), + 'pageSize': document.getElementById('pageSizeField'), }, }, errorWrapper: {