Browse Source

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.
Tim van der Meij 8 years ago
parent
commit
3800b5e463
  1. 30
      src/core/document.js

30
src/core/document.js

@ -133,12 +133,21 @@ var Page = (function PageClosure() {
}, },
get mediaBox() { get mediaBox() {
var obj = this.getInheritedPageProp('MediaBox', true); var mediaBox = this.getInheritedPageProp('MediaBox', true);
// Reset invalid media box to letter size. // Reset invalid media box to letter size.
if (!isArray(obj) || obj.length !== 4) { if (!isArray(mediaBox) || mediaBox.length !== 4) {
obj = LETTER_SIZE_MEDIABOX; 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() { get userUnit() {
@ -150,21 +159,16 @@ var Page = (function PageClosure() {
}, },
get view() { 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: // From the spec, 6th ed., p.963:
// "The crop, bleed, trim, and art boxes should not ordinarily // "The crop, bleed, trim, and art boxes should not ordinarily
// extend beyond the boundaries of the media box. If they do, they are // extend beyond the boundaries of the media box. If they do, they are
// effectively reduced to their intersection with the media box." // effectively reduced to their intersection with the media box."
cropBox = Util.intersect(cropBox, mediaBox); var mediaBox = this.mediaBox, cropBox = this.cropBox;
if (!cropBox) { if (mediaBox === cropBox) {
return shadow(this, 'view', mediaBox); return shadow(this, 'view', mediaBox);
} }
return shadow(this, 'view', cropBox); var intersection = Util.intersect(cropBox, mediaBox);
return shadow(this, 'view', intersection || mediaBox);
}, },
get rotate() { get rotate() {

Loading…
Cancel
Save