Coverage Report

Created: 2025-07-18 07:17

/src/xpdf-4.05/xpdf/Page.cc
Line
Count
Source (jump to first uncovered line)
1
//========================================================================
2
//
3
// Page.cc
4
//
5
// Copyright 1996-2007 Glyph & Cog, LLC
6
//
7
//========================================================================
8
9
#include <aconf.h>
10
11
#include <stddef.h>
12
#include "gmempp.h"
13
#include "Trace.h"
14
#include "GlobalParams.h"
15
#include "Object.h"
16
#include "Array.h"
17
#include "Dict.h"
18
#include "PDFDoc.h"
19
#include "XRef.h"
20
#include "Link.h"
21
#include "OutputDev.h"
22
#ifndef PDF_PARSER_ONLY
23
#include "Gfx.h"
24
#include "GfxState.h"
25
#include "Annot.h"
26
#include "AcroForm.h"
27
#endif
28
#include "Error.h"
29
#include "Catalog.h"
30
#include "Page.h"
31
32
//------------------------------------------------------------------------
33
// PDFRectangle
34
//------------------------------------------------------------------------
35
36
437k
void PDFRectangle::clipTo(PDFRectangle *rect) {
37
437k
  if (x1 < rect->x1) {
38
0
    x1 = rect->x1;
39
437k
  } else if (x1 > rect->x2) {
40
8
    x1 = rect->x2;
41
8
  }
42
437k
  if (x2 < rect->x1) {
43
0
    x2 = rect->x1;
44
437k
  } else if (x2 > rect->x2) {
45
8
    x2 = rect->x2;
46
8
  }
47
437k
  if (y1 < rect->y1) {
48
8
    y1 = rect->y1;
49
437k
  } else if (y1 > rect->y2) {
50
0
    y1 = rect->y2;
51
0
  }
52
437k
  if (y2 < rect->y1) {
53
0
    y2 = rect->y1;
54
437k
  } else if (y2 > rect->y2) {
55
1.60k
    y2 = rect->y2;
56
1.60k
  }
57
437k
}
58
59
//------------------------------------------------------------------------
60
// PageAttrs
61
//------------------------------------------------------------------------
62
63
110k
PageAttrs::PageAttrs(PageAttrs *attrs, Dict *dict, XRef *xref) {
64
110k
  Object obj1;
65
66
  // get old/default values
67
110k
  if (attrs) {
68
2.56k
    mediaBox = attrs->mediaBox;
69
2.56k
    cropBox = attrs->cropBox;
70
2.56k
    haveCropBox = attrs->haveCropBox;
71
2.56k
    rotate = attrs->rotate;
72
107k
  } else {
73
    // set default MediaBox to 8.5" x 11" -- this shouldn't be necessary
74
    // but some (non-compliant) PDF files don't specify a MediaBox
75
107k
    mediaBox.x1 = 0;
76
107k
    mediaBox.y1 = 0;
77
107k
    mediaBox.x2 = 612;
78
107k
    mediaBox.y2 = 792;
79
107k
    cropBox.x1 = cropBox.y1 = cropBox.x2 = cropBox.y2 = 0;
80
107k
    haveCropBox = gFalse;
81
107k
    rotate = 0;
82
107k
  }
83
84
  // media box
85
110k
  readBox(dict, "MediaBox", &mediaBox);
86
87
  // crop box
88
110k
  if (readBox(dict, "CropBox", &cropBox)) {
89
1.08k
    haveCropBox = gTrue;
90
1.08k
  }
91
110k
  if (!haveCropBox) {
92
109k
    cropBox = mediaBox;
93
109k
  }
94
95
  // other boxes
96
110k
  bleedBox = cropBox;
97
110k
  readBox(dict, "BleedBox", &bleedBox);
98
110k
  trimBox = cropBox;
99
110k
  readBox(dict, "TrimBox", &trimBox);
100
110k
  artBox = cropBox;
101
110k
  readBox(dict, "ArtBox", &artBox);
102
103
  // rotate
104
110k
  dict->lookup("Rotate", &obj1);
105
110k
  if (obj1.isInt()) {
106
1.18k
    rotate = obj1.getInt();
107
1.18k
  }
108
110k
  obj1.free();
109
111k
  while (rotate < 0) {
110
855
    rotate += 360;
111
855
  }
112
110k
  while (rotate >= 360) {
113
16
    rotate -= 360;
114
16
  }
115
116
  // misc attributes
117
110k
  dict->lookup("LastModified", &lastModified);
118
110k
  dict->lookup("BoxColorInfo", &boxColorInfo);
119
110k
  dict->lookup("Group", &group);
120
110k
  dict->lookup("Metadata", &metadata);
121
110k
  dict->lookup("PieceInfo", &pieceInfo);
122
110k
  dict->lookup("SeparationInfo", &separationInfo);
123
110k
  if (dict->lookup("UserUnit", &obj1)->isNum()) {
124
732
    userUnit = obj1.getNum();
125
732
    if (userUnit < 1) {
126
732
      userUnit = 1;
127
732
    }
128
109k
  } else {
129
109k
    userUnit = 1;
130
109k
  }
131
110k
  obj1.free();
132
133
  // resource dictionary
134
110k
  Object childResDictObj;
135
110k
  dict->lookup("Resources", &childResDictObj);
136
110k
  if (attrs && attrs->resources.isDict() && childResDictObj.isDict()) {
137
    // merge this node's resources into the parent's resources
138
    // (some PDF files violate the PDF spec and expect this merging)
139
692
    resources.initDict(xref);
140
692
    Dict *resDict = resources.getDict();
141
692
    Dict *parentResDict = attrs->resources.getDict();
142
6.31k
    for (int i = 0; i < parentResDict->getLength(); ++i) {
143
5.62k
      char *resType = parentResDict->getKey(i);
144
5.62k
      Object subdictObj1;
145
5.62k
      if (parentResDict->getVal(i, &subdictObj1)->isDict()) {
146
1.55k
  Dict *subdict1 = subdictObj1.getDict();
147
1.55k
  Object subdictObj2;
148
1.55k
  subdictObj2.initDict(xref);
149
1.55k
  Dict *subdict2 = subdictObj2.getDict();
150
9.16k
  for (int j = 0; j < subdict1->getLength(); ++j) {
151
7.61k
    subdict1->getValNF(j, &obj1);
152
7.61k
    subdict2->add(copyString(subdict1->getKey(j)), &obj1);
153
7.61k
  }
154
1.55k
  resDict->add(copyString(resType), &subdictObj2);
155
1.55k
      }
156
5.62k
      subdictObj1.free();
157
5.62k
    }
158
692
    Dict *childResDict = childResDictObj.getDict();
159
4.61k
    for (int i = 0; i < childResDict->getLength(); ++i) {
160
3.92k
      char *resType = childResDict->getKey(i);
161
3.92k
      Object subdictObj1;
162
3.92k
      if (childResDict->getVal(i, &subdictObj1)->isDict()) {
163
1.30k
  Object subdictObj2;
164
1.30k
  if (resDict->lookup(resType, &subdictObj2)->isDict()) {
165
824
    Dict *subdict1 = subdictObj1.getDict();
166
824
    Dict *subdict2 = subdictObj2.getDict();
167
4.92k
    for (int j = 0; j < subdict1->getLength(); ++j) {
168
4.10k
      subdict1->getValNF(j, &obj1);
169
4.10k
      subdict2->add(copyString(subdict1->getKey(j)), &obj1);
170
4.10k
    }
171
824
    subdictObj2.free();
172
824
  } else {
173
477
    subdictObj2.free();
174
477
    resDict->add(copyString(resType), subdictObj1.copy(&subdictObj2));
175
477
  }
176
1.30k
      }
177
3.92k
      subdictObj1.free();
178
3.92k
    }
179
109k
  } else if (attrs && attrs->resources.isDict()) {
180
64
    attrs->resources.copy(&resources);
181
109k
  } else if (childResDictObj.isDict()) {
182
1.09k
    childResDictObj.copy(&resources);
183
108k
  } else {
184
108k
    resources.initNull();
185
108k
  }
186
110k
  childResDictObj.free();
187
110k
}
188
189
15.7k
PageAttrs::PageAttrs() {
190
15.7k
  mediaBox.x1 = mediaBox.y1 = 0;
191
15.7k
  mediaBox.x2 = mediaBox.y2 = 50;
192
15.7k
  cropBox = mediaBox;
193
15.7k
  haveCropBox = gFalse;
194
15.7k
  bleedBox = cropBox;
195
15.7k
  trimBox = cropBox;
196
15.7k
  artBox = cropBox;
197
15.7k
  rotate = 0;
198
15.7k
  lastModified.initNull();
199
15.7k
  boxColorInfo.initNull();
200
15.7k
  group.initNull();
201
15.7k
  metadata.initNull();
202
15.7k
  pieceInfo.initNull();
203
15.7k
  separationInfo.initNull();
204
15.7k
  userUnit = 1;
205
15.7k
  resources.initNull();
206
15.7k
}
207
208
126k
PageAttrs::~PageAttrs() {
209
126k
  lastModified.free();
210
126k
  boxColorInfo.free();
211
126k
  group.free();
212
126k
  metadata.free();
213
126k
  pieceInfo.free();
214
126k
  separationInfo.free();
215
126k
  resources.free();
216
126k
}
217
218
109k
void PageAttrs::clipBoxes() {
219
109k
  cropBox.clipTo(&mediaBox);
220
109k
  bleedBox.clipTo(&mediaBox);
221
109k
  trimBox.clipTo(&mediaBox);
222
109k
  artBox.clipTo(&mediaBox);
223
109k
}
224
225
552k
GBool PageAttrs::readBox(Dict *dict, const char *key, PDFRectangle *box) {
226
552k
  PDFRectangle tmp;
227
552k
  double t;
228
552k
  Object obj1, obj2;
229
552k
  GBool ok;
230
231
552k
  dict->lookup(key, &obj1);
232
552k
  if (obj1.isArray() && obj1.arrayGetLength() == 4) {
233
2.11k
    ok = gTrue;
234
2.11k
    obj1.arrayGet(0, &obj2);
235
2.11k
    if (obj2.isNum()) {
236
2.11k
      tmp.x1 = obj2.getNum();
237
2.11k
    } else {
238
0
      ok = gFalse;
239
0
    }
240
2.11k
    obj2.free();
241
2.11k
    obj1.arrayGet(1, &obj2);
242
2.11k
    if (obj2.isNum()) {
243
2.03k
      tmp.y1 = obj2.getNum();
244
2.03k
    } else {
245
85
      ok = gFalse;
246
85
    }
247
2.11k
    obj2.free();
248
2.11k
    obj1.arrayGet(2, &obj2);
249
2.11k
    if (obj2.isNum()) {
250
2.01k
      tmp.x2 = obj2.getNum();
251
2.01k
    } else {
252
99
      ok = gFalse;
253
99
    }
254
2.11k
    obj2.free();
255
2.11k
    obj1.arrayGet(3, &obj2);
256
2.11k
    if (obj2.isNum()) {
257
2.11k
      tmp.y2 = obj2.getNum();
258
2.11k
    } else {
259
1
      ok = gFalse;
260
1
    }
261
2.11k
    obj2.free();
262
2.11k
    if (ok) {
263
1.93k
      if (tmp.x1 > tmp.x2) {
264
3
  t = tmp.x1; tmp.x1 = tmp.x2; tmp.x2 = t;
265
3
      }
266
1.93k
      if (tmp.y1 > tmp.y2) {
267
3
  t = tmp.y1; tmp.y1 = tmp.y2; tmp.y2 = t;
268
3
      }
269
1.93k
      *box = tmp;
270
1.93k
    }
271
550k
  } else {
272
550k
    ok = gFalse;
273
550k
  }
274
552k
  obj1.free();
275
552k
  return ok;
276
552k
}
277
278
//------------------------------------------------------------------------
279
// Page
280
//------------------------------------------------------------------------
281
282
109k
Page::Page(PDFDoc *docA, int numA, Dict *pageDict, PageAttrs *attrsA) {
283
109k
  ok = gTrue;
284
109k
  doc = docA;
285
109k
  xref = doc->getXRef();
286
109k
  num = numA;
287
288
  // get attributes
289
109k
  attrs = attrsA;
290
109k
  attrs->clipBoxes();
291
292
  // annotations
293
109k
  pageDict->lookupNF("Annots", &annots);
294
109k
  if (!(annots.isRef() || annots.isArray() || annots.isNull())) {
295
1.05k
    error(errSyntaxError, -1,
296
1.05k
    "Page annotations object (page {0:d}) is wrong type ({1:s})",
297
1.05k
    num, annots.getTypeName());
298
1.05k
    annots.free();
299
1.05k
    goto err2;
300
1.05k
  }
301
302
  // contents
303
108k
  pageDict->lookupNF("Contents", &contents);
304
108k
  if (!(contents.isRef() || contents.isArray() ||
305
108k
  contents.isNull())) {
306
5
    error(errSyntaxError, -1,
307
5
    "Page contents object (page {0:d}) is wrong type ({1:s})",
308
5
    num, contents.getTypeName());
309
5
    contents.free();
310
5
    goto err1;
311
5
  }
312
313
  // thumbnail
314
108k
  pageDict->lookupNF("Thumb", &thumbnail);
315
108k
  if (!thumbnail.isRef()) {
316
108k
    if (!thumbnail.isNull()) {
317
11.1k
      thumbnail.free();
318
11.1k
      thumbnail.initNull();
319
11.1k
    }
320
108k
  }
321
322
108k
  return;
323
324
1.05k
 err2:
325
1.05k
  annots.initNull();
326
1.06k
 err1:
327
1.06k
  contents.initNull();
328
1.06k
  thumbnail.initNull();
329
1.06k
  ok = gFalse;
330
1.06k
}
331
332
15.7k
Page::Page(PDFDoc *docA, int numA) {
333
15.7k
  doc = docA;
334
15.7k
  xref = doc->getXRef();
335
15.7k
  num = numA;
336
15.7k
  attrs = new PageAttrs();
337
15.7k
  annots.initNull();
338
15.7k
  contents.initNull();
339
15.7k
  thumbnail.initNull();
340
15.7k
  ok = gTrue;
341
15.7k
}
342
343
125k
Page::~Page() {
344
125k
  delete attrs;
345
125k
  annots.free();
346
125k
  contents.free();
347
125k
  thumbnail.free();
348
125k
}
349
350
0
Links *Page::getLinks() {
351
0
  Links *links;
352
0
  Object obj;
353
354
0
  links = new Links(getAnnots(&obj), doc->getCatalog()->getBaseURI());
355
0
  obj.free();
356
0
  return links;
357
0
}
358
359
void Page::display(OutputDev *out, double hDPI, double vDPI,
360
       int rotate, GBool useMediaBox, GBool crop,
361
       GBool printing,
362
       GBool (*abortCheckCbk)(void *data),
363
0
       void *abortCheckCbkData) {
364
0
  displaySlice(out, hDPI, vDPI, rotate, useMediaBox, crop,
365
0
         -1, -1, -1, -1, printing,
366
0
         abortCheckCbk, abortCheckCbkData);
367
0
}
368
369
void Page::displaySlice(OutputDev *out, double hDPI, double vDPI,
370
      int rotate, GBool useMediaBox, GBool crop,
371
      int sliceX, int sliceY, int sliceW, int sliceH,
372
      GBool printing,
373
      GBool (*abortCheckCbk)(void *data),
374
0
      void *abortCheckCbkData) {
375
0
#ifndef PDF_PARSER_ONLY
376
0
  PDFRectangle *mediaBox, *cropBox;
377
0
  PDFRectangle box;
378
0
  Gfx *gfx;
379
0
  Object obj;
380
0
  AcroForm *form;
381
0
  int i;
382
383
0
  if (!out->checkPageSlice(this, hDPI, vDPI, rotate, useMediaBox, crop,
384
0
         sliceX, sliceY, sliceW, sliceH,
385
0
         printing, abortCheckCbk, abortCheckCbkData)) {
386
0
    return;
387
0
  }
388
389
0
  traceBegin(this, "begin page");
390
391
0
  rotate += getRotate();
392
0
  if (rotate >= 360) {
393
0
    rotate -= 360;
394
0
  } else if (rotate < 0) {
395
0
    rotate += 360;
396
0
  }
397
398
0
  makeBox(hDPI, vDPI, rotate, useMediaBox, out->upsideDown(),
399
0
    sliceX, sliceY, sliceW, sliceH, &box, &crop);
400
0
  cropBox = getCropBox();
401
402
0
  if (globalParams->getPrintCommands()) {
403
0
    mediaBox = getMediaBox();
404
0
    printf("***** MediaBox = ll:%g,%g ur:%g,%g\n",
405
0
     mediaBox->x1, mediaBox->y1, mediaBox->x2, mediaBox->y2);
406
0
    printf("***** CropBox = ll:%g,%g ur:%g,%g\n",
407
0
     cropBox->x1, cropBox->y1, cropBox->x2, cropBox->y2);
408
0
    printf("***** Rotate = %d\n", attrs->getRotate());
409
0
  }
410
411
0
  gfx = new Gfx(doc, out, num, attrs->getResourceDict(),
412
0
    hDPI, vDPI, &box, crop ? cropBox : (PDFRectangle *)NULL,
413
0
    rotate, abortCheckCbk, abortCheckCbkData);
414
0
  contents.fetch(xref, &obj);
415
0
  if (!obj.isNull()) {
416
0
    gfx->saveState();
417
0
    gfx->display(&contents);
418
0
    gfx->endOfPage();
419
0
  }
420
0
  obj.free();
421
422
  // draw (non-form) annotations
423
0
  if (globalParams->getDrawAnnotations()) {
424
0
    Annots *annots2 = doc->getAnnots();
425
0
    annots2->generateAnnotAppearances(num);
426
0
    int n = annots2->getNumAnnots(num);
427
0
    if (n > 0) {
428
0
      if (globalParams->getPrintCommands()) {
429
0
  printf("***** Annotations\n");
430
0
      }
431
0
      for (i = 0; i < n; ++i) {
432
0
  if (abortCheckCbk && (*abortCheckCbk)(abortCheckCbkData)) {
433
0
    break;
434
0
  }
435
0
  annots2->getAnnot(num, i)->draw(gfx, printing);
436
0
      }
437
0
    }
438
0
  }
439
440
  // draw form fields
441
0
  if (globalParams->getDrawFormFields()) {
442
0
    if ((form = doc->getCatalog()->getForm())) {
443
0
      if (!(abortCheckCbk && (*abortCheckCbk)(abortCheckCbkData))) {
444
0
  form->draw(num, gfx, printing);
445
0
      }
446
0
    }
447
0
  }
448
449
0
  delete gfx;
450
0
#endif // PDF_PARSER_ONLY
451
452
0
  traceEnd(this, "end page");
453
0
}
454
455
void Page::makeBox(double hDPI, double vDPI, int rotate,
456
       GBool useMediaBox, GBool upsideDown,
457
       double sliceX, double sliceY, double sliceW, double sliceH,
458
0
       PDFRectangle *box, GBool *crop) {
459
0
  PDFRectangle *mediaBox, *cropBox, *baseBox;
460
0
  double kx, ky;
461
462
0
  mediaBox = getMediaBox();
463
0
  cropBox = getCropBox();
464
0
  if (sliceW >= 0 && sliceH >= 0) {
465
0
    baseBox = useMediaBox ? mediaBox : cropBox;
466
0
    kx = 72.0 / hDPI;
467
0
    ky = 72.0 / vDPI;
468
0
    if (rotate == 90) {
469
0
      if (upsideDown) {
470
0
  box->x1 = baseBox->x1 + ky * sliceY;
471
0
  box->x2 = baseBox->x1 + ky * (sliceY + sliceH);
472
0
      } else {
473
0
  box->x1 = baseBox->x2 - ky * (sliceY + sliceH);
474
0
  box->x2 = baseBox->x2 - ky * sliceY;
475
0
      }
476
0
      box->y1 = baseBox->y1 + kx * sliceX;
477
0
      box->y2 = baseBox->y1 + kx * (sliceX + sliceW);
478
0
    } else if (rotate == 180) {
479
0
      box->x1 = baseBox->x2 - kx * (sliceX + sliceW);
480
0
      box->x2 = baseBox->x2 - kx * sliceX;
481
0
      if (upsideDown) {
482
0
  box->y1 = baseBox->y1 + ky * sliceY;
483
0
  box->y2 = baseBox->y1 + ky * (sliceY + sliceH);
484
0
      } else {
485
0
  box->y1 = baseBox->y2 - ky * (sliceY + sliceH);
486
0
  box->y2 = baseBox->y2 - ky * sliceY;
487
0
      }
488
0
    } else if (rotate == 270) {
489
0
      if (upsideDown) {
490
0
  box->x1 = baseBox->x2 - ky * (sliceY + sliceH);
491
0
  box->x2 = baseBox->x2 - ky * sliceY;
492
0
      } else {
493
0
  box->x1 = baseBox->x1 + ky * sliceY;
494
0
  box->x2 = baseBox->x1 + ky * (sliceY + sliceH);
495
0
      }
496
0
      box->y1 = baseBox->y2 - kx * (sliceX + sliceW);
497
0
      box->y2 = baseBox->y2 - kx * sliceX;
498
0
    } else {
499
0
      box->x1 = baseBox->x1 + kx * sliceX;
500
0
      box->x2 = baseBox->x1 + kx * (sliceX + sliceW);
501
0
      if (upsideDown) {
502
0
  box->y1 = baseBox->y2 - ky * (sliceY + sliceH);
503
0
  box->y2 = baseBox->y2 - ky * sliceY;
504
0
      } else {
505
0
  box->y1 = baseBox->y1 + ky * sliceY;
506
0
  box->y2 = baseBox->y1 + ky * (sliceY + sliceH);
507
0
      }
508
0
    }
509
0
  } else if (useMediaBox) {
510
0
    *box = *mediaBox;
511
0
  } else {
512
0
    *box = *cropBox;
513
0
    *crop = gFalse;
514
0
  }
515
0
}
516
517
0
void Page::processLinks(OutputDev *out) {
518
0
  Links *links;
519
0
  int i;
520
521
0
  links = getLinks();
522
0
  for (i = 0; i < links->getNumLinks(); ++i) {
523
0
    out->processLink(links->getLink(i));
524
0
  }
525
0
  delete links;
526
0
}
527
528
#ifndef PDF_PARSER_ONLY
529
void Page::getDefaultCTM(double *ctm, double hDPI, double vDPI,
530
0
       int rotate, GBool useMediaBox, GBool upsideDown) {
531
0
  GfxState *state;
532
0
  int i;
533
534
0
  rotate += getRotate();
535
0
  if (rotate >= 360) {
536
0
    rotate -= 360;
537
0
  } else if (rotate < 0) {
538
0
    rotate += 360;
539
0
  }
540
0
  state = new GfxState(hDPI, vDPI,
541
0
           useMediaBox ? getMediaBox() : getCropBox(),
542
0
           rotate, upsideDown);
543
0
  for (i = 0; i < 6; ++i) {
544
0
    ctm[i] = state->getCTM()[i];
545
0
  }
546
0
  delete state;
547
0
}
548
#endif
549