Coverage Report

Created: 2025-07-12 06:54

/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
13.8k
void PDFRectangle::clipTo(PDFRectangle *rect) {
37
13.8k
  if (x1 < rect->x1) {
38
0
    x1 = rect->x1;
39
13.8k
  } else if (x1 > rect->x2) {
40
24
    x1 = rect->x2;
41
24
  }
42
13.8k
  if (x2 < rect->x1) {
43
0
    x2 = rect->x1;
44
13.8k
  } else if (x2 > rect->x2) {
45
28
    x2 = rect->x2;
46
28
  }
47
13.8k
  if (y1 < rect->y1) {
48
24
    y1 = rect->y1;
49
13.7k
  } else if (y1 > rect->y2) {
50
0
    y1 = rect->y2;
51
0
  }
52
13.8k
  if (y2 < rect->y1) {
53
0
    y2 = rect->y1;
54
13.8k
  } else if (y2 > rect->y2) {
55
2.38k
    y2 = rect->y2;
56
2.38k
  }
57
13.8k
}
58
59
//------------------------------------------------------------------------
60
// PageAttrs
61
//------------------------------------------------------------------------
62
63
4.34k
PageAttrs::PageAttrs(PageAttrs *attrs, Dict *dict, XRef *xref) {
64
4.34k
  Object obj1;
65
66
  // get old/default values
67
4.34k
  if (attrs) {
68
2.28k
    mediaBox = attrs->mediaBox;
69
2.28k
    cropBox = attrs->cropBox;
70
2.28k
    haveCropBox = attrs->haveCropBox;
71
2.28k
    rotate = attrs->rotate;
72
2.28k
  } 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
2.05k
    mediaBox.x1 = 0;
76
2.05k
    mediaBox.y1 = 0;
77
2.05k
    mediaBox.x2 = 612;
78
2.05k
    mediaBox.y2 = 792;
79
2.05k
    cropBox.x1 = cropBox.y1 = cropBox.x2 = cropBox.y2 = 0;
80
2.05k
    haveCropBox = gFalse;
81
2.05k
    rotate = 0;
82
2.05k
  }
83
84
  // media box
85
4.34k
  readBox(dict, "MediaBox", &mediaBox);
86
87
  // crop box
88
4.34k
  if (readBox(dict, "CropBox", &cropBox)) {
89
1.16k
    haveCropBox = gTrue;
90
1.16k
  }
91
4.34k
  if (!haveCropBox) {
92
3.17k
    cropBox = mediaBox;
93
3.17k
  }
94
95
  // other boxes
96
4.34k
  bleedBox = cropBox;
97
4.34k
  readBox(dict, "BleedBox", &bleedBox);
98
4.34k
  trimBox = cropBox;
99
4.34k
  readBox(dict, "TrimBox", &trimBox);
100
4.34k
  artBox = cropBox;
101
4.34k
  readBox(dict, "ArtBox", &artBox);
102
103
  // rotate
104
4.34k
  dict->lookup("Rotate", &obj1);
105
4.34k
  if (obj1.isInt()) {
106
1.31k
    rotate = obj1.getInt();
107
1.31k
  }
108
4.34k
  obj1.free();
109
5.24k
  while (rotate < 0) {
110
905
    rotate += 360;
111
905
  }
112
4.37k
  while (rotate >= 360) {
113
32
    rotate -= 360;
114
32
  }
115
116
  // misc attributes
117
4.34k
  dict->lookup("LastModified", &lastModified);
118
4.34k
  dict->lookup("BoxColorInfo", &boxColorInfo);
119
4.34k
  dict->lookup("Group", &group);
120
4.34k
  dict->lookup("Metadata", &metadata);
121
4.34k
  dict->lookup("PieceInfo", &pieceInfo);
122
4.34k
  dict->lookup("SeparationInfo", &separationInfo);
123
4.34k
  if (dict->lookup("UserUnit", &obj1)->isNum()) {
124
734
    userUnit = obj1.getNum();
125
734
    if (userUnit < 1) {
126
734
      userUnit = 1;
127
734
    }
128
3.60k
  } else {
129
3.60k
    userUnit = 1;
130
3.60k
  }
131
4.34k
  obj1.free();
132
133
  // resource dictionary
134
4.34k
  Object childResDictObj;
135
4.34k
  dict->lookup("Resources", &childResDictObj);
136
4.34k
  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
451
    resources.initDict(xref);
140
451
    Dict *resDict = resources.getDict();
141
451
    Dict *parentResDict = attrs->resources.getDict();
142
3.56k
    for (int i = 0; i < parentResDict->getLength(); ++i) {
143
3.11k
      char *resType = parentResDict->getKey(i);
144
3.11k
      Object subdictObj1;
145
3.11k
      if (parentResDict->getVal(i, &subdictObj1)->isDict()) {
146
827
  Dict *subdict1 = subdictObj1.getDict();
147
827
  Object subdictObj2;
148
827
  subdictObj2.initDict(xref);
149
827
  Dict *subdict2 = subdictObj2.getDict();
150
4.55k
  for (int j = 0; j < subdict1->getLength(); ++j) {
151
3.72k
    subdict1->getValNF(j, &obj1);
152
3.72k
    subdict2->add(copyString(subdict1->getKey(j)), &obj1);
153
3.72k
  }
154
827
  resDict->add(copyString(resType), &subdictObj2);
155
827
      }
156
3.11k
      subdictObj1.free();
157
3.11k
    }
158
451
    Dict *childResDict = childResDictObj.getDict();
159
2.30k
    for (int i = 0; i < childResDict->getLength(); ++i) {
160
1.84k
      char *resType = childResDict->getKey(i);
161
1.84k
      Object subdictObj1;
162
1.84k
      if (childResDict->getVal(i, &subdictObj1)->isDict()) {
163
676
  Object subdictObj2;
164
676
  if (resDict->lookup(resType, &subdictObj2)->isDict()) {
165
104
    Dict *subdict1 = subdictObj1.getDict();
166
104
    Dict *subdict2 = subdictObj2.getDict();
167
460
    for (int j = 0; j < subdict1->getLength(); ++j) {
168
356
      subdict1->getValNF(j, &obj1);
169
356
      subdict2->add(copyString(subdict1->getKey(j)), &obj1);
170
356
    }
171
104
    subdictObj2.free();
172
572
  } else {
173
572
    subdictObj2.free();
174
572
    resDict->add(copyString(resType), subdictObj1.copy(&subdictObj2));
175
572
  }
176
676
      }
177
1.84k
      subdictObj1.free();
178
1.84k
    }
179
3.89k
  } else if (attrs && attrs->resources.isDict()) {
180
125
    attrs->resources.copy(&resources);
181
3.76k
  } else if (childResDictObj.isDict()) {
182
1.16k
    childResDictObj.copy(&resources);
183
2.60k
  } else {
184
2.60k
    resources.initNull();
185
2.60k
  }
186
4.34k
  childResDictObj.free();
187
4.34k
}
188
189
20.4k
PageAttrs::PageAttrs() {
190
20.4k
  mediaBox.x1 = mediaBox.y1 = 0;
191
20.4k
  mediaBox.x2 = mediaBox.y2 = 50;
192
20.4k
  cropBox = mediaBox;
193
20.4k
  haveCropBox = gFalse;
194
20.4k
  bleedBox = cropBox;
195
20.4k
  trimBox = cropBox;
196
20.4k
  artBox = cropBox;
197
20.4k
  rotate = 0;
198
20.4k
  lastModified.initNull();
199
20.4k
  boxColorInfo.initNull();
200
20.4k
  group.initNull();
201
20.4k
  metadata.initNull();
202
20.4k
  pieceInfo.initNull();
203
20.4k
  separationInfo.initNull();
204
20.4k
  userUnit = 1;
205
20.4k
  resources.initNull();
206
20.4k
}
207
208
24.8k
PageAttrs::~PageAttrs() {
209
24.8k
  lastModified.free();
210
24.8k
  boxColorInfo.free();
211
24.8k
  group.free();
212
24.8k
  metadata.free();
213
24.8k
  pieceInfo.free();
214
24.8k
  separationInfo.free();
215
24.8k
  resources.free();
216
24.8k
}
217
218
3.45k
void PageAttrs::clipBoxes() {
219
3.45k
  cropBox.clipTo(&mediaBox);
220
3.45k
  bleedBox.clipTo(&mediaBox);
221
3.45k
  trimBox.clipTo(&mediaBox);
222
3.45k
  artBox.clipTo(&mediaBox);
223
3.45k
}
224
225
21.7k
GBool PageAttrs::readBox(Dict *dict, const char *key, PDFRectangle *box) {
226
21.7k
  PDFRectangle tmp;
227
21.7k
  double t;
228
21.7k
  Object obj1, obj2;
229
21.7k
  GBool ok;
230
231
21.7k
  dict->lookup(key, &obj1);
232
21.7k
  if (obj1.isArray() && obj1.arrayGetLength() == 4) {
233
2.13k
    ok = gTrue;
234
2.13k
    obj1.arrayGet(0, &obj2);
235
2.13k
    if (obj2.isNum()) {
236
2.13k
      tmp.x1 = obj2.getNum();
237
2.13k
    } else {
238
0
      ok = gFalse;
239
0
    }
240
2.13k
    obj2.free();
241
2.13k
    obj1.arrayGet(1, &obj2);
242
2.13k
    if (obj2.isNum()) {
243
2.06k
      tmp.y1 = obj2.getNum();
244
2.06k
    } else {
245
71
      ok = gFalse;
246
71
    }
247
2.13k
    obj2.free();
248
2.13k
    obj1.arrayGet(2, &obj2);
249
2.13k
    if (obj2.isNum()) {
250
2.03k
      tmp.x2 = obj2.getNum();
251
2.03k
    } else {
252
105
      ok = gFalse;
253
105
    }
254
2.13k
    obj2.free();
255
2.13k
    obj1.arrayGet(3, &obj2);
256
2.13k
    if (obj2.isNum()) {
257
2.13k
      tmp.y2 = obj2.getNum();
258
2.13k
    } else {
259
3
      ok = gFalse;
260
3
    }
261
2.13k
    obj2.free();
262
2.13k
    if (ok) {
263
1.96k
      if (tmp.x1 > tmp.x2) {
264
3
  t = tmp.x1; tmp.x1 = tmp.x2; tmp.x2 = t;
265
3
      }
266
1.96k
      if (tmp.y1 > tmp.y2) {
267
6
  t = tmp.y1; tmp.y1 = tmp.y2; tmp.y2 = t;
268
6
      }
269
1.96k
      *box = tmp;
270
1.96k
    }
271
19.5k
  } else {
272
19.5k
    ok = gFalse;
273
19.5k
  }
274
21.7k
  obj1.free();
275
21.7k
  return ok;
276
21.7k
}
277
278
//------------------------------------------------------------------------
279
// Page
280
//------------------------------------------------------------------------
281
282
3.45k
Page::Page(PDFDoc *docA, int numA, Dict *pageDict, PageAttrs *attrsA) {
283
3.45k
  ok = gTrue;
284
3.45k
  doc = docA;
285
3.45k
  xref = doc->getXRef();
286
3.45k
  num = numA;
287
288
  // get attributes
289
3.45k
  attrs = attrsA;
290
3.45k
  attrs->clipBoxes();
291
292
  // annotations
293
3.45k
  pageDict->lookupNF("Annots", &annots);
294
3.45k
  if (!(annots.isRef() || annots.isArray() || annots.isNull())) {
295
838
    error(errSyntaxError, -1,
296
838
    "Page annotations object (page {0:d}) is wrong type ({1:s})",
297
838
    num, annots.getTypeName());
298
838
    annots.free();
299
838
    goto err2;
300
838
  }
301
302
  // contents
303
2.61k
  pageDict->lookupNF("Contents", &contents);
304
2.61k
  if (!(contents.isRef() || contents.isArray() ||
305
2.61k
  contents.isNull())) {
306
3
    error(errSyntaxError, -1,
307
3
    "Page contents object (page {0:d}) is wrong type ({1:s})",
308
3
    num, contents.getTypeName());
309
3
    contents.free();
310
3
    goto err1;
311
3
  }
312
313
  // thumbnail
314
2.61k
  pageDict->lookupNF("Thumb", &thumbnail);
315
2.61k
  if (!thumbnail.isRef()) {
316
2.60k
    if (!thumbnail.isNull()) {
317
40
      thumbnail.free();
318
40
      thumbnail.initNull();
319
40
    }
320
2.60k
  }
321
322
2.61k
  return;
323
324
838
 err2:
325
838
  annots.initNull();
326
841
 err1:
327
841
  contents.initNull();
328
841
  thumbnail.initNull();
329
841
  ok = gFalse;
330
841
}
331
332
20.4k
Page::Page(PDFDoc *docA, int numA) {
333
20.4k
  doc = docA;
334
20.4k
  xref = doc->getXRef();
335
20.4k
  num = numA;
336
20.4k
  attrs = new PageAttrs();
337
20.4k
  annots.initNull();
338
20.4k
  contents.initNull();
339
20.4k
  thumbnail.initNull();
340
20.4k
  ok = gTrue;
341
20.4k
}
342
343
23.9k
Page::~Page() {
344
23.9k
  delete attrs;
345
23.9k
  annots.free();
346
23.9k
  contents.free();
347
23.9k
  thumbnail.free();
348
23.9k
}
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