-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpages.js
More file actions
151 lines (128 loc) · 6.24 KB
/
pages.js
File metadata and controls
151 lines (128 loc) · 6.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
(function () {
var VERSION = 1.1;
var MODULE_PATH = "pages";
var thisModule = Sky.getUtil(MODULE_PATH);
if( thisModule && thisModule.version >= VERSION) {
return;
};
//--------------------------
// Start pages class
function moduleClass() {
var pages = this;
pages.version = VERSION;
pages.description = "Some page tools for InDesign";
pages.getBounds = function( pageSpread ) {
// This functions returns the bounds of the spread or page in current measure units
if(pageSpread.constructor.name == "Spread") {
var bounds = [0,0,0,0]; //[y1, x1, y2, x2]
for (var i = 0; i < pageSpread.pages.length; i++) {
var pBounds = pageSpread.pages[i].bounds;
if (bounds[0] > pBounds[0]) bounds[0] = pBounds[0];
if (bounds[1] > pBounds[1]) bounds[1] = pBounds[1];
if (bounds[2] < pBounds[2]) bounds[2] = pBounds[2];
if (bounds[3] < pBounds[3]) bounds[3] = pBounds[3];
};
return bounds;
} else if(pageSpread.constructor.name == "Page") {
return pageSpread.bounds;
} else {
return new TypeError("pages.getBounds: Expected typeof Page or Spread but received " + pageSpread.constructor.name);
};
};
pages.getInfo = function( pageSpread, units ) {
// Creates and returned the page info object
var infoPage = new Object();
// Param: pageSpread: Object : page or spread
// Param: units: String, Number or MeasureUnits
// defaults to points
infoPage.units = ( units === undefined) ? "pt" : units;
var RulerUtil = Sky.getUtil("rulers");
var BoundsUtil = Sky.getUtil("bounds");
infoPage.kind = pageSpread.constructor.name;
infoPage.units = units;
var spread = (infoPage.kind === "Page") ? pageSpread.parent : pageSpread;
var parentDoc = spread.parent;
// set-n-safe original rulers
var prevRulers = RulerUtil.set(parentDoc, units);
var pageSpreadBounds = pages.getBounds( pageSpread );
// measure the page
var boundsInfo = BoundsUtil.getInfo( pageSpreadBounds );
// Update infoPage with measures
infoPage.bounds = boundsInfo.bounds;
infoPage.width = boundsInfo.width;
infoPage.height = boundsInfo.height;
// Get page bleed and slug settings [y1, x1, y2, x2]
var docPref = parentDoc.documentPreferences;
var growBounds = function( boundsArr1, boundsArr2 ){
return [ boundsArr1[0]-boundsArr2[0], boundsArr1[1]-boundsArr2[1],
boundsArr1[2]+boundsArr2[2], boundsArr1[3]+boundsArr2[3] ];
};
if (infoPage.kind === "Page" && docPref.facingPages && pageSpread.side == PageSideOptions.LEFT_HAND ) {
// LEFT_HAND Facing pages...
infoPage.bleedBounds = growBounds(infoPage.bounds,
[ docPref.documentBleedTopOffset,
docPref.documentBleedOutsideOrRightOffset,
docPref.documentBleedInsideOrLeftOffset,
docPref.documentBleedBottomOffset ]);
infoPage.slugsBounds = growBounds(infoPage.bleedBounds,
[ docPref.slugTopOffset,
docPref.slugRightOrOutsideOffset,
docPref.slugInsideOrLeftOffset,
docPref.slugBottomOffset ]);
} else { // RIGHT_HAND or SINGLE_SIDED
infoPage.bleedBounds = growBounds(infoPage.bounds,
[ docPref.documentBleedTopOffset,
docPref.documentBleedInsideOrLeftOffset,
docPref.documentBleedOutsideOrRightOffset,
docPref.documentBleedBottomOffset ]);
infoPage.slugsBounds = growBounds(infoPage.bleedBounds,
[ docPref.slugTopOffset,
docPref.slugInsideOrLeftOffset,
docPref.slugRightOrOutsideOffset,
docPref.slugBottomOffset ]);
};
// reset original rulers
RulerUtil.set(parentDoc, prevRulers);
return infoPage;
};
pages.getByLabel = function( pagesArr, labelStr, keyOption ) {
// Param pagesArr : Array : of pages (e.g. doc.pages or doc.masterSpreads[0].pages )
// Param labelStr : String : the page label
// Param keyOption : String or Object : key or object with key
// key : String, the key to the label, Default undefined
// any : Boolean, return both key-label matches and unkeyed label matches
// Returns : Array with matches
var options = {key: undefined, any: false};
switch (typeof keyOption) {
case "string":
options.key = keyOption;
break;
case "object":
if( keyOption.hasOwnProperty('key') && typeof keyOption.key === 'string' ) options.key = keyOption.key;
if( keyOption.hasOwnProperty('any') && typeof keyOption.any === 'boolean') options.any = keyOption.any;
break;
default:
break;
};
var fffound = new Array();
for ( i=0; pagesArr.length > i; i++ ) {
var page = pagesArr.item(i);
if( options.key ) {
if( page.extractLabel(options.key) === labelStr ) {
fffound.push( page );
} else if( options.any && page.label === labelStr ) {
fffound.push( page );
}
} else { // No key defined
if( page.label === labelStr ){
fffound.push( page );
};
};
};
return fffound;
};
};
//--------------------------
// End pages class
Sky.setUtil(MODULE_PATH, new moduleClass() );
})();