[api-minor] Fix various issues related to the pageSize information

The `getPageSizeInches` method was implemented on `PDFDocumentProxy`, which seems conceptually wrong since the size property isn't global to the document but rather specific to each page. Hence the method is moved into `PDFPageProxy`, as `get pageSizeInches` instead to address this.

Despite the fact that new API functionality was implemented, no unit-tests were added. To prevent issues later on, we should *always* ensure that new functionality has at least some test-coverage; something that this patch also takes care of.

The new `PDFDocumentProperties._parsePageSize` method seemed unnecessary convoluted. Furthermore, in the "no data provided"-case it even returned incorrect data (an array, rather than the expected object).
Finally, the fallback strings didn't actually agree with the `en-US` locale. This inconsistency doesn't look too great, and it's thus addressed here as well.
This commit is contained in:
Jonas Jenwald 2018-03-17 17:10:37 +01:00
parent db6e316efd
commit e0ae157582
3 changed files with 45 additions and 41 deletions

View File

@ -692,23 +692,6 @@ var PDFDocumentProxy = (function PDFDocumentProxyClosure() {
getMetadata: function PDFDocumentProxy_getMetadata() { getMetadata: function PDFDocumentProxy_getMetadata() {
return this.transport.getMetadata(); return this.transport.getMetadata();
}, },
/**
* @param {number} pageNumber The page number to get the page size from.
* The first page is 1, which is also the default page used.
* @return {Promise} A promise that is resolved with an dict containing the
* width and height in inches.
*/
getPageSizeInches(pageNumber) {
pageNumber = pageNumber || 1;
return this.getPage(pageNumber).then((page) => {
const [x1, y1, x2, y2] = page.view;
// convert values from user units to inches
return {
width: (x2 - x1) / 72 * page.userUnit,
height: (y2 - y1) / 72 * page.userUnit,
};
});
},
/** /**
* @return {Promise} A promise that is resolved with a TypedArray that has * @return {Promise} A promise that is resolved with a TypedArray that has
* the raw data from the PDF. * the raw data from the PDF.
@ -890,6 +873,20 @@ var PDFPageProxy = (function PDFPageProxyClosure() {
get view() { get view() {
return this.pageInfo.view; return this.pageInfo.view;
}, },
/**
* The size of the current page, converted from PDF units to inches.
* @return {Object} An Object containing the properties: {number} `width`
* and {number} `height`, given in inches.
*/
get pageSizeInches() {
const [x1, y1, x2, y2] = this.view, userUnit = this.userUnit;
return {
width: (x2 - x1) / 72 * userUnit,
height: (y2 - y1) / 72 * userUnit,
};
},
/** /**
* @param {number} scale The desired scale of the viewport. * @param {number} scale The desired scale of the viewport.
* @param {number} rotate Degrees to rotate the viewport. If omitted this * @param {number} rotate Degrees to rotate the viewport. If omitted this

View File

@ -964,6 +964,14 @@ describe('api', function() {
it('gets view', function () { it('gets view', function () {
expect(page.view).toEqual([0, 0, 595.28, 841.89]); expect(page.view).toEqual([0, 0, 595.28, 841.89]);
}); });
it('gets page size (in inches)', function() {
const { width, height, } = page.pageSizeInches;
expect(+width.toPrecision(3)).toEqual(8.27);
expect(+height.toPrecision(4)).toEqual(11.69);
});
it('gets viewport', function () { it('gets viewport', function () {
var viewport = page.getViewport(1.5, 90); var viewport = page.getViewport(1.5, 90);
expect(viewport.viewBox).toEqual(page.view); expect(viewport.viewBox).toEqual(page.view);

View File

@ -80,13 +80,12 @@ class PDFDocumentProperties {
this._parseFileSize(this.maybeFileSize), this._parseFileSize(this.maybeFileSize),
this._parseDate(info.CreationDate), this._parseDate(info.CreationDate),
this._parseDate(info.ModDate), this._parseDate(info.ModDate),
this.pdfDocument.getPageSizeInches().then((pageSizeInches) => { this.pdfDocument.getPage(1).then((pdfPage) => {
return this._parsePageSize(pageSizeInches); return this._parsePageSize(pdfPage.pageSizeInches);
}), }),
]); ]);
}).then(([info, metadata, fileName, fileSize, }).then(([info, metadata, fileName, fileSize, creationDate, modDate,
creationDate, modDate, pageSize]) => { pageSizes]) => {
freezeFieldData({ freezeFieldData({
'fileName': fileName, 'fileName': fileName,
'fileSize': fileSize, 'fileSize': fileSize,
@ -100,8 +99,8 @@ class PDFDocumentProperties {
'producer': info.Producer, 'producer': info.Producer,
'version': info.PDFFormatVersion, 'version': info.PDFFormatVersion,
'pageCount': this.pdfDocument.numPages, 'pageCount': this.pdfDocument.numPages,
'pageSizeInch': pageSize.inch, 'pageSizeInch': pageSizes.inch,
'pageSizeMM': pageSize.mm, 'pageSizeMM': pageSizes.mm,
}); });
this._updateUI(); this._updateUI();
@ -224,25 +223,25 @@ class PDFDocumentProperties {
*/ */
_parsePageSize(pageSizeInches) { _parsePageSize(pageSizeInches) {
if (!pageSizeInches) { if (!pageSizeInches) {
return Promise.resolve([undefined, undefined]); return Promise.resolve({ inch: undefined, mm: undefined, });
} }
const sizes_two_units = { const { width, height, } = pageSizeInches;
'width_in': Math.round(pageSizeInches.width * 100) / 100,
'height_in': Math.round(pageSizeInches.height * 100) / 100,
// 1in = 25.4mm; no need to round to 2 decimals for mm
'width_mm': Math.round(pageSizeInches.width * 25.4 * 10) / 10,
'height_mm': Math.round(pageSizeInches.height * 25.4 * 10) / 10,
};
return Promise.all([ return Promise.all([
this.l10n.get('document_properties_page_size_in', this.l10n.get('document_properties_page_size_in', {
sizes_two_units, '{{width_in}} in × {{height_in}} in'), width_in: Math.round(width * 100) / 100,
this.l10n.get('document_properties_page_size_mm', height_in: Math.round(height * 100) / 100,
sizes_two_units, '{{width_mm}} mm × {{height_mm}} mm'), }, '{{width_in}}in × {{height_in}}in'),
]).then(([parsedPageSizeInches, parsedPageSizeMM]) => { // 1in = 25.4mm; no need to round to 2 decimals for millimeters.
return Promise.resolve({ this.l10n.get('document_properties_page_size_mm', {
inch: parsedPageSizeInches, width_mm: Math.round(width * 25.4 * 10) / 10,
mm: parsedPageSizeMM, height_mm: Math.round(height * 25.4 * 10) / 10,
}); }, '{{width_mm}}mm × {{height_mm}}mm'),
]).then((sizes) => {
return {
inch: sizes[0],
mm: sizes[1],
};
}); });
} }