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).
This commit is contained in:
Jonas Jenwald 2018-03-20 13:51:56 +01:00
parent 513412c92e
commit f3b74c5028
3 changed files with 33 additions and 31 deletions

View File

@ -20,6 +20,9 @@ import { createPromiseCapability } from 'pdfjs-lib';
const DEFAULT_FIELD_CONTENT = '-'; 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 * @typedef {Object} PDFDocumentPropertiesOptions
* @property {string} overlayName - Name/identifier for the overlay. * @property {string} overlayName - Name/identifier for the overlay.
@ -59,6 +62,11 @@ class PDFDocumentProperties {
this._pagesRotation = evt.pagesRotation; 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, }).then(([info, metadata, fileName, fileSize, creationDate, modDate,
pageSizes]) => { pageSize]) => {
freezeFieldData({ freezeFieldData({
'fileName': fileName, 'fileName': fileName,
'fileSize': fileSize, 'fileSize': fileSize,
@ -118,8 +126,7 @@ class PDFDocumentProperties {
'producer': info.Producer, 'producer': info.Producer,
'version': info.PDFFormatVersion, 'version': info.PDFFormatVersion,
'pageCount': this.pdfDocument.numPages, 'pageCount': this.pdfDocument.numPages,
'pageSizeInch': pageSizes.inch, 'pageSize': pageSize,
'pageSizeMM': pageSizes.mm,
'_currentPageNumber': currentPageNumber, '_currentPageNumber': currentPageNumber,
'_pagesRotation': pagesRotation, '_pagesRotation': pagesRotation,
}); });
@ -250,7 +257,7 @@ class PDFDocumentProperties {
*/ */
_parsePageSize(pageSizeInches, pagesRotation) { _parsePageSize(pageSizeInches, pagesRotation) {
if (!pageSizeInches) { 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. // Take the viewer rotation into account as well; compare with Adobe Reader.
if (pagesRotation % 180 !== 0) { if (pagesRotation % 180 !== 0) {
@ -259,28 +266,28 @@ class PDFDocumentProperties {
height: pageSizeInches.width, 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([ return Promise.all([
this.l10n.get('document_properties_page_size_unit_inches', null, 'in'), (this._isNonMetricLocale ? sizeInches : sizeMillimeters),
this.l10n.get('document_properties_page_size_unit_millimeters', null, this.l10n.get('document_properties_page_size_unit_' +
'mm'), (this._isNonMetricLocale ? 'inches' : 'millimeters'), null,
]).then(([unitInches, unitMillimeters]) => { this._isNonMetricLocale ? 'in' : 'mm'),
return Promise.all([ ]).then(([{ width, height, }, unit]) => {
this.l10n.get('document_properties_page_size_dimension_string', { return this.l10n.get('document_properties_page_size_dimension_string', {
width: (Math.round(width * 100) / 100).toLocaleString(), width: width.toLocaleString(),
height: (Math.round(height * 100) / 100).toLocaleString(), height: height.toLocaleString(),
unit: unitInches, unit,
}, '{{width}} × {{height}} {{unit}}'), }, '{{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], };
}); });
} }

View File

@ -352,11 +352,7 @@ See https://github.com/adobe-type-tools/cmap-resources
<span data-l10n-id="document_properties_page_count">Page Count:</span> <p id="pageCountField">-</p> <span data-l10n-id="document_properties_page_count">Page Count:</span> <p id="pageCountField">-</p>
</div> </div>
<div class="row"> <div class="row">
<span data-l10n-id="document_properties_page_size">Page Size:</span> <span data-l10n-id="document_properties_page_size">Page Size:</span> <p id="pageSizeField">-</p>
<p>
<span id="pageSizeFieldMM">-</span><br>
<span id="pageSizeFieldInch">-</span>
</p>
</div> </div>
<div class="buttonRow"> <div class="buttonRow">
<button id="documentPropertiesClose" class="overlayButton"><span data-l10n-id="document_properties_close">Close</span></button> <button id="documentPropertiesClose" class="overlayButton"><span data-l10n-id="document_properties_close">Close</span></button>

View File

@ -159,8 +159,7 @@ function getViewerConfiguration() {
'producer': document.getElementById('producerField'), 'producer': document.getElementById('producerField'),
'version': document.getElementById('versionField'), 'version': document.getElementById('versionField'),
'pageCount': document.getElementById('pageCountField'), 'pageCount': document.getElementById('pageCountField'),
'pageSizeInch': document.getElementById('pageSizeFieldInch'), 'pageSize': document.getElementById('pageSizeField'),
'pageSizeMM': document.getElementById('pageSizeFieldMM'),
}, },
}, },
errorWrapper: { errorWrapper: {