Coverage Report

Created: 2026-08-31 06:45

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