3
Copyright (c) 2006-2007, BBR Inc. All rights reserved.
5
Permission is hereby granted, free of charge, to any person obtaining
6
a copy of this software and associated documentation files (the
7
"Software"), to deal in the Software without restriction, including
8
without limitation the rights to use, copy, modify, merge, publish,
9
distribute, sublicense, and/or sell copies of the Software, and to
10
permit persons to whom the Software is furnished to do so, subject to
11
the following conditions:
13
The above copyright notice and this permission notice shall be included
14
in all copies or substantial portions of the Software.
16
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32
#include "P2PPageTree.h"
36
GBool P2PPageTree::checkPageRange(int no, const char *pageSet,
37
const char *pageRanges)
43
if (!strcasecmp(pageSet,"even") && (no % 2) == 1) {
45
} else if (!strcasecmp(pageSet, "odd") && (no % 2) == 0) {
50
if (pageRanges == 0) {
54
for (range = pageRanges; *range != '\0';) {
58
upper = strtol(range, (char **)&range, 10);
60
lower = strtol(range, (char **)&range, 10);
63
if (!isdigit(*range)) {
66
upper = strtol(range, (char **)&range, 10);
72
if (no >= lower && no <= upper) {
86
P2PPageTree::P2PPageTree(Catalog *orgCatalogA, XRef *xrefA)
91
numPages = orgCatalogA->getNumPages();
92
pages = new P2PPage *[numPages];
93
for (i = 0;i < numPages;i++) {
94
pages[i] = new P2PPage(orgCatalogA->getPage(i+1),xref);
98
void P2PPageTree::cleanPages(P2PPage **pagesA, int size)
102
for (i = 0;i < size;i++) {
103
if (pagesA[i] != 0) {
110
P2PPageTree::~P2PPageTree()
112
cleanPages(pages,numPages);
115
void P2PPageTree::outputSelf(P2POutputStream *str, P2PObject **pageObjects,
121
str->puts("<< /Type /Pages "
123
for (i = 0;i < len;i++) {
124
P2PXRef::put(pageObjects[i]);
125
pageObjects[i]->outputRef(str);
128
str->printf(" ] /Count %d >>\n",len);
132
void P2PPageTree::output(P2POutputStream *str, int copies, GBool collate)
137
P2PObject **pageObjects;
140
if (P2PDoc::options.even && (numPages % 2) != 0) {
141
/* make number of pages even.
142
append empty page to the last */
143
/* only increment allocate size here */
146
outPages = new P2PPage *[n];
147
for (i = 0;i < numPages;i++) {
148
outPages[i] = pages[i];
150
if (P2PDoc::options.even && (numPages % 2) != 0) {
151
/* make number of pages even.
152
append a empty page to the last. */
154
/* empty page's mediaSize is same as a preceding page's media Size */
155
outPages[n-1] = new P2PPage(outPages[n-2]->getMediaBox(),xref);
157
if (P2PDoc::options.reverse) {
158
/* reverse output pages */
159
for (i = 0;i < (n+1)/2;i++) {
163
outPages[i] = outPages[n-i-1];
169
pageObjects = new P2PObject * [len];
172
for (i = 0;i < n;i++) {
173
pageObjects[i] = static_cast<P2PObject *>(outPages[i]);
175
for (i = n;i < len;i++) {
176
pageObjects[i] = new P2PObject();
178
outputSelf(str,pageObjects,len);
179
for (i = 0;i < n;i++) {
180
outPages[i]->output(str,this);
181
if (P2PDoc::options.pdfPrinter)
182
fprintf(stderr, "PAGE: %d %d\n", i+1, 1);
184
for (i = 1;i < copies;i++) {
186
for (j = 0;j < n;j++) {
187
outPages[j]->output(str,this,pageObjects[k+j]);
188
if (P2PDoc::options.pdfPrinter)
189
fprintf(stderr, "PAGE: %d %d\n", j+1, 1);
193
for (i = 0;i < n;i++) {
195
pageObjects[k] = static_cast<P2PObject *>(outPages[i]);
196
for (j = 1;j < copies;j++) {
197
pageObjects[k+j] = new P2PObject();
200
outputSelf(str,pageObjects,len);
201
for (i = 0;i < n;i++) {
203
outPages[i]->output(str,this);
204
if (P2PDoc::options.pdfPrinter)
205
fprintf(stderr, "PAGE: %d %d\n", i+1, 1);
206
for (j = 1;j < copies;j++) {
207
outPages[i]->output(str,this,pageObjects[k+j]);
208
if (P2PDoc::options.pdfPrinter)
209
fprintf(stderr, "PAGE: %d %d\n", i+1, 1);
213
delete[] pageObjects;
217
int P2PPageTree::nup(int n, PDFRectangle *box,
218
unsigned int borderFlag, unsigned int layout,
225
size = (numPages+(n-1))/n;
226
newPages = new P2PPage *[size];
227
for (i = 0,j = 0;i < numPages;j++, i += n) {
228
int len = numPages - i;
234
/* check original pages */
235
for (k = 0;k < len;k++) {
236
if (pages[i+k]->getNumOrgPages() != 1) {
237
/* nup twice error */
241
newPages[j] = new P2PPage(n,pages+i,len,box,borderFlag,layout,
244
cleanPages(pages,numPages);
250
void P2PPageTree::select(const char *pageSet, const char *pageRanges)
255
newPages = new P2PPage *[numPages];
256
for (i = 0,j = 0;i < numPages;i++) {
257
if (checkPageRange(i+1, pageSet, pageRanges)) {
258
newPages[j ++] = pages[i];
268
void P2PPageTree::fit(PDFRectangle *box, double zoom)
272
for (i = 0;i < numPages;i++) {
273
pages[i]->fit(box,zoom);
277
void P2PPageTree::mirror()
281
for (i = 0;i < numPages;i++) {
286
void P2PPageTree::rotate(int orientation)
290
for (i = 0;i < numPages;i++) {
291
pages[i]->rotate(orientation);
295
void P2PPageTree::position(PDFRectangle *box, int xpos, int ypos)
299
for (i = 0;i < numPages;i++) {
300
pages[i]->position(box,xpos,ypos);
304
void P2PPageTree::scale(double zoom)
308
for (i = 0;i < numPages;i++) {
309
pages[i]->scale(zoom);
313
void P2PPageTree::autoRotate(PDFRectangle *box)
317
for (i = 0;i < numPages;i++) {
318
pages[i]->autoRotate(box);
322
void P2PPageTree::setMediaBox(PDFRectangle *mediaBoxA)
326
for (i = 0;i < numPages;i++) {
327
pages[i]->setMediaBox(mediaBoxA);