From 3800b5e4639b3c6154ee5321891a820b7ddad8fa Mon Sep 17 00:00:00 2001 From: Tim van der Meij Date: Tue, 6 Dec 2016 01:00:12 +0100 Subject: [PATCH] Document: extract `CropBox` fetching and validation into a getter This patch refactors the `CropBox` code to combine fetching and validation code in a getter, like we already did for the `MediaBox` property. Combined with variable name changes, this improves readability of the code and makes the `view` getter simpler as well. --- src/core/document.js | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/core/document.js b/src/core/document.js index b535cb9fe..ed0a8eff0 100644 --- a/src/core/document.js +++ b/src/core/document.js @@ -133,12 +133,21 @@ var Page = (function PageClosure() { }, get mediaBox() { - var obj = this.getInheritedPageProp('MediaBox', true); + var mediaBox = this.getInheritedPageProp('MediaBox', true); // Reset invalid media box to letter size. - if (!isArray(obj) || obj.length !== 4) { - obj = LETTER_SIZE_MEDIABOX; + if (!isArray(mediaBox) || mediaBox.length !== 4) { + return shadow(this, 'mediaBox', LETTER_SIZE_MEDIABOX); } - return shadow(this, 'mediaBox', obj); + return shadow(this, 'mediaBox', mediaBox); + }, + + get cropBox() { + var cropBox = this.getInheritedPageProp('CropBox', true); + // Reset invalid crop box to media box. + if (!isArray(cropBox) || cropBox.length !== 4) { + return shadow(this, 'cropBox', this.mediaBox); + } + return shadow(this, 'cropBox', cropBox); }, get userUnit() { @@ -150,21 +159,16 @@ var Page = (function PageClosure() { }, get view() { - var mediaBox = this.mediaBox; - var cropBox = this.getInheritedPageProp('CropBox', true); - if (!isArray(cropBox) || cropBox.length !== 4) { - return shadow(this, 'view', mediaBox); - } - // From the spec, 6th ed., p.963: // "The crop, bleed, trim, and art boxes should not ordinarily // extend beyond the boundaries of the media box. If they do, they are // effectively reduced to their intersection with the media box." - cropBox = Util.intersect(cropBox, mediaBox); - if (!cropBox) { + var mediaBox = this.mediaBox, cropBox = this.cropBox; + if (mediaBox === cropBox) { return shadow(this, 'view', mediaBox); } - return shadow(this, 'view', cropBox); + var intersection = Util.intersect(cropBox, mediaBox); + return shadow(this, 'view', intersection || mediaBox); }, get rotate() {