Coverage Report

Created: 2026-04-04 06:05

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
754k
void PDFRectangle::clipTo(PDFRectangle *rect) {
37
754k
  if (x1 < rect->x1) {
38
6.59k
    x1 = rect->x1;
39
747k
  } else if (x1 > rect->x2) {
40
293k
    x1 = rect->x2;
41
293k
  }
42
754k
  if (x2 < rect->x1) {
43
2.15k
    x2 = rect->x1;
44
752k
  } else if (x2 > rect->x2) {
45
294k
    x2 = rect->x2;
46
294k
  }
47
754k
  if (y1 < rect->y1) {
48
3.21k
    y1 = rect->y1;
49
751k
  } else if (y1 > rect->y2) {
50
2.13k
    y1 = rect->y2;
51
2.13k
  }
52
754k
  if (y2 < rect->y1) {
53
3.17k
    y2 = rect->y1;
54
751k
  } else if (y2 > rect->y2) {
55
296k
    y2 = rect->y2;
56
296k
  }
57
754k
}
58
59
//------------------------------------------------------------------------
60
// PageAttrs
61
//------------------------------------------------------------------------
62
63
190k
PageAttrs::PageAttrs(PageAttrs *attrs, Dict *dict, XRef *xref) {
64
190k
  Object obj1;
65
66
  // get old/default values
67
190k
  if (attrs) {
68
115k
    mediaBox = attrs->mediaBox;
69
115k
    cropBox = attrs->cropBox;
70
115k
    haveCropBox = attrs->haveCropBox;
71
115k
    rotate = attrs->rotate;
72
115k
  } 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
75.0k
    mediaBox.x1 = 0;
76
75.0k
    mediaBox.y1 = 0;
77
75.0k
    mediaBox.x2 = 612;
78
75.0k
    mediaBox.y2 = 792;
79
75.0k
    cropBox.x1 = cropBox.y1 = cropBox.x2 = cropBox.y2 = 0;
80
75.0k
    haveCropBox = gFalse;
81
75.0k
    rotate = 0;
82
75.0k
  }
83
84
  // media box
85
190k
  readBox(dict, "MediaBox", &mediaBox);
86
87
  // crop box
88
190k
  if (readBox(dict, "CropBox", &cropBox)) {
89
76.5k
    haveCropBox = gTrue;
90
76.5k
  }
91
190k
  if (!haveCropBox) {
92
114k
    cropBox = mediaBox;
93
114k
  }
94
95
  // other boxes
96
190k
  bleedBox = cropBox;
97
190k
  readBox(dict, "BleedBox", &bleedBox);
98
190k
  trimBox = cropBox;
99
190k
  readBox(dict, "TrimBox", &trimBox);
100
190k
  artBox = cropBox;
101
190k
  readBox(dict, "ArtBox", &artBox);
102
103
  // rotate
104
190k
  dict->lookup("Rotate", &obj1);
105
190k
  if (obj1.isInt()) {
106
2.85k
    rotate = obj1.getInt();
107
2.85k
  }
108
190k
  obj1.free();
109
2.11G
  while (rotate < 0) {
110
2.11G
    rotate += 360;
111
2.11G
  }
112
77.3M
  while (rotate >= 360) {
113
77.1M
    rotate -= 360;
114
77.1M
  }
115
116
  // misc attributes
117
190k
  dict->lookup("LastModified", &lastModified);
118
190k
  dict->lookup("BoxColorInfo", &boxColorInfo);
119
190k
  dict->lookup("Group", &group);
120
190k
  dict->lookup("Metadata", &metadata);
121
190k
  dict->lookup("PieceInfo", &pieceInfo);
122
190k
  dict->lookup("SeparationInfo", &separationInfo);
123
190k
  if (dict->lookup("UserUnit", &obj1)->isNum()) {
124
1.80k
    userUnit = obj1.getNum();
125
1.80k
    if (userUnit < 1) {
126
1.47k
      userUnit = 1;
127
1.47k
    }
128
189k
  } else {
129
189k
    userUnit = 1;
130
189k
  }
131
190k
  obj1.free();
132
133
  // resource dictionary
134
190k
  Object childResDictObj;
135
190k
  dict->lookup("Resources", &childResDictObj);
136
190k
  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
37.5k
    resources.initDict(xref);
140
37.5k
    Dict *resDict = resources.getDict();
141
37.5k
    Dict *parentResDict = attrs->resources.getDict();
142
160k
    for (int i = 0; i < parentResDict->getLength(); ++i) {
143
122k
      char *resType = parentResDict->getKey(i);
144
122k
      Object subdictObj1;
145
122k
      if (parentResDict->getVal(i, &subdictObj1)->isDict()) {
146
61.4k
  Dict *subdict1 = subdictObj1.getDict();
147
61.4k
  Object subdictObj2;
148
61.4k
  subdictObj2.initDict(xref);
149
61.4k
  Dict *subdict2 = subdictObj2.getDict();
150
126k
  for (int j = 0; j < subdict1->getLength(); ++j) {
151
64.6k
    subdict1->getValNF(j, &obj1);
152
64.6k
    subdict2->add(copyString(subdict1->getKey(j)), &obj1);
153
64.6k
  }
154
61.4k
  resDict->add(copyString(resType), &subdictObj2);
155
61.4k
      }
156
122k
      subdictObj1.free();
157
122k
    }
158
37.5k
    Dict *childResDict = childResDictObj.getDict();
159
303k
    for (int i = 0; i < childResDict->getLength(); ++i) {
160
266k
      char *resType = childResDict->getKey(i);
161
266k
      Object subdictObj1;
162
266k
      if (childResDict->getVal(i, &subdictObj1)->isDict()) {
163
111k
  Object subdictObj2;
164
111k
  if (resDict->lookup(resType, &subdictObj2)->isDict()) {
165
61.3k
    Dict *subdict1 = subdictObj1.getDict();
166
61.3k
    Dict *subdict2 = subdictObj2.getDict();
167
125k
    for (int j = 0; j < subdict1->getLength(); ++j) {
168
64.4k
      subdict1->getValNF(j, &obj1);
169
64.4k
      subdict2->add(copyString(subdict1->getKey(j)), &obj1);
170
64.4k
    }
171
61.3k
    subdictObj2.free();
172
61.3k
  } else {
173
50.0k
    subdictObj2.free();
174
50.0k
    resDict->add(copyString(resType), subdictObj1.copy(&subdictObj2));
175
50.0k
  }
176
111k
      }
177
266k
      subdictObj1.free();
178
266k
    }
179
153k
  } else if (attrs && attrs->resources.isDict()) {
180
777
    attrs->resources.copy(&resources);
181
152k
  } else if (childResDictObj.isDict()) {
182
1.55k
    childResDictObj.copy(&resources);
183
150k
  } else {
184
150k
    resources.initNull();
185
150k
  }
186
190k
  childResDictObj.free();
187
190k
}
188
189
17.1k
PageAttrs::PageAttrs() {
190
17.1k
  mediaBox.x1 = mediaBox.y1 = 0;
191
17.1k
  mediaBox.x2 = mediaBox.y2 = 50;
192
17.1k
  cropBox = mediaBox;
193
17.1k
  haveCropBox = gFalse;
194
17.1k
  bleedBox = cropBox;
195
17.1k
  trimBox = cropBox;
196
17.1k
  artBox = cropBox;
197
17.1k
  rotate = 0;
198
17.1k
  lastModified.initNull();
199
17.1k
  boxColorInfo.initNull();
200
17.1k
  group.initNull();
201
17.1k
  metadata.initNull();
202
17.1k
  pieceInfo.initNull();
203
17.1k
  separationInfo.initNull();
204
17.1k
  userUnit = 1;
205
17.1k
  resources.initNull();
206
17.1k
}
207
208
208k
PageAttrs::~PageAttrs() {
209
208k
  lastModified.free();
210
208k
  boxColorInfo.free();
211
208k
  group.free();
212
208k
  metadata.free();
213
208k
  pieceInfo.free();
214
208k
  separationInfo.free();
215
208k
  resources.free();
216
208k
}
217
218
188k
void PageAttrs::clipBoxes() {
219
188k
  cropBox.clipTo(&mediaBox);
220
188k
  bleedBox.clipTo(&mediaBox);
221
188k
  trimBox.clipTo(&mediaBox);
222
188k
  artBox.clipTo(&mediaBox);
223
188k
}
224
225
954k
GBool PageAttrs::readBox(Dict *dict, const char *key, PDFRectangle *box) {
226
954k
  PDFRectangle tmp;
227
954k
  double t;
228
954k
  Object obj1, obj2;
229
954k
  GBool ok;
230
231
954k
  dict->lookup(key, &obj1);
232
954k
  if (obj1.isArray() && obj1.arrayGetLength() == 4) {
233
82.5k
    ok = gTrue;
234
82.5k
    obj1.arrayGet(0, &obj2);
235
82.5k
    if (obj2.isNum()) {
236
82.2k
      tmp.x1 = obj2.getNum();
237
82.2k
    } else {
238
322
      ok = gFalse;
239
322
    }
240
82.5k
    obj2.free();
241
82.5k
    obj1.arrayGet(1, &obj2);
242
82.5k
    if (obj2.isNum()) {
243
81.4k
      tmp.y1 = obj2.getNum();
244
81.4k
    } else {
245
1.06k
      ok = gFalse;
246
1.06k
    }
247
82.5k
    obj2.free();
248
82.5k
    obj1.arrayGet(2, &obj2);
249
82.5k
    if (obj2.isNum()) {
250
81.5k
      tmp.x2 = obj2.getNum();
251
81.5k
    } else {
252
1.01k
      ok = gFalse;
253
1.01k
    }
254
82.5k
    obj2.free();
255
82.5k
    obj1.arrayGet(3, &obj2);
256
82.5k
    if (obj2.isNum()) {
257
82.1k
      tmp.y2 = obj2.getNum();
258
82.1k
    } else {
259
388
      ok = gFalse;
260
388
    }
261
82.5k
    obj2.free();
262
82.5k
    if (ok) {
263
      // limit the box coords so they can be converted to 32-bit ints later
264
80.7k
      if (tmp.x1 < -1e9) {
265
540
  tmp.x1 = -1e9;
266
80.1k
      } else if (tmp.x1 > 1e9) {
267
590
  tmp.x1 = 1e9;
268
590
      }
269
80.7k
      if (tmp.y1 < -1e9) {
270
1.53k
  tmp.y1 = -1e9;
271
79.1k
      } else if (tmp.y1 > 1e9) {
272
920
  tmp.y1 = 1e9;
273
920
      }
274
80.7k
      if (tmp.x2 < -1e9) {
275
377
  tmp.x2 = -1e9;
276
80.3k
      } else if (tmp.x2 > 1e9) {
277
569
  tmp.x2 = 1e9;
278
569
      }
279
80.7k
      if (tmp.y2 < -1e9) {
280
1.10k
  tmp.y2 = -1e9;
281
79.5k
      } else if (tmp.y2 > 1e9) {
282
584
  tmp.y2 = 1e9;
283
584
      }
284
80.7k
      if (tmp.x1 > tmp.x2) {
285
76.1k
  t = tmp.x1; tmp.x1 = tmp.x2; tmp.x2 = t;
286
76.1k
      }
287
80.7k
      if (tmp.y1 > tmp.y2) {
288
74.6k
  t = tmp.y1; tmp.y1 = tmp.y2; tmp.y2 = t;
289
74.6k
      }
290
80.7k
      *box = tmp;
291
80.7k
    }
292
871k
  } else {
293
871k
    ok = gFalse;
294
871k
  }
295
954k
  obj1.free();
296
954k
  return ok;
297
954k
}
298
299
//------------------------------------------------------------------------
300
// Page
301
//------------------------------------------------------------------------
302
303
188k
Page::Page(PDFDoc *docA, int numA, Dict *pageDict, PageAttrs *attrsA) {
304
188k
  ok = gTrue;
305
188k
  doc = docA;
306
188k
  xref = doc->getXRef();
307
188k
  num = numA;
308
309
  // get attributes
310
188k
  attrs = attrsA;
311
188k
  attrs->clipBoxes();
312
313
  // annotations
314
188k
  pageDict->lookupNF("Annots", &annots);
315
188k
  if (!(annots.isRef() || annots.isArray() || annots.isNull())) {
316
215
    error(errSyntaxError, -1,
317
215
    "Page annotations object (page {0:d}) is wrong type ({1:s})",
318
215
    num, annots.getTypeName());
319
215
    annots.free();
320
215
    goto err2;
321
215
  }
322
323
  // contents
324
188k
  pageDict->lookupNF("Contents", &contents);
325
188k
  if (!(contents.isRef() || contents.isArray() ||
326
169k
  contents.isNull())) {
327
1.02k
    error(errSyntaxError, -1,
328
1.02k
    "Page contents object (page {0:d}) is wrong type ({1:s})",
329
1.02k
    num, contents.getTypeName());
330
1.02k
    contents.free();
331
1.02k
    goto err1;
332
1.02k
  }
333
334
  // thumbnail
335
187k
  pageDict->lookupNF("Thumb", &thumbnail);
336
187k
  if (!thumbnail.isRef()) {
337
187k
    if (!thumbnail.isNull()) {
338
348
      thumbnail.free();
339
348
      thumbnail.initNull();
340
348
    }
341
187k
  }
342
343
187k
  return;
344
345
215
 err2:
346
215
  annots.initNull();
347
1.23k
 err1:
348
1.23k
  contents.initNull();
349
1.23k
  thumbnail.initNull();
350
1.23k
  ok = gFalse;
351
1.23k
}
352
353
17.1k
Page::Page(PDFDoc *docA, int numA) {
354
17.1k
  doc = docA;
355
17.1k
  xref = doc->getXRef();
356
17.1k
  num = numA;
357
17.1k
  attrs = new PageAttrs();
358
17.1k
  annots.initNull();
359
17.1k
  contents.initNull();
360
17.1k
  thumbnail.initNull();
361
17.1k
  ok = gTrue;
362
17.1k
}
363
364
205k
Page::~Page() {
365
205k
  delete attrs;
366
205k
  annots.free();
367
205k
  contents.free();
368
205k
  thumbnail.free();
369
205k
}
370
371
0
Links *Page::getLinks() {
372
0
  Links *links;
373
0
  Object obj;
374
375
0
  links = new Links(getAnnots(&obj), doc->getCatalog()->getBaseURI());
376
0
  obj.free();
377
0
  return links;
378
0
}
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
0
       void *abortCheckCbkData) {
386
0
  displaySlice(out, localParams, hDPI, vDPI, rotate, useMediaBox, crop,
387
0
         -1, -1, -1, -1, printing,
388
0
         abortCheckCbk, abortCheckCbkData);
389
0
}
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
0
      void *abortCheckCbkData) {
398
0
#ifndef PDF_PARSER_ONLY
399
0
  PDFRectangle *mediaBox, *cropBox;
400
0
  PDFRectangle box;
401
0
  Gfx *gfx;
402
0
  Object obj;
403
0
  AcroForm *form;
404
0
  int i;
405
406
0
  if (!out->checkPageSlice(this, hDPI, vDPI, rotate, useMediaBox, crop,
407
0
         sliceX, sliceY, sliceW, sliceH,
408
0
         printing, abortCheckCbk, abortCheckCbkData)) {
409
0
    return;
410
0
  }
411
412
0
  traceBegin(this, "begin page");
413
414
0
  rotate += getRotate();
415
0
  if (rotate >= 360) {
416
0
    rotate -= 360;
417
0
  } else if (rotate < 0) {
418
0
    rotate += 360;
419
0
  }
420
421
0
  makeBox(hDPI, vDPI, rotate, useMediaBox, out->upsideDown(),
422
0
    sliceX, sliceY, sliceW, sliceH, &box, &crop);
423
0
  cropBox = getCropBox();
424
425
0
  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
0
  gfx = new Gfx(doc, out, localParams, num, attrs->getResourceDict(),
435
0
    hDPI, vDPI, &box, crop ? cropBox : (PDFRectangle *)NULL,
436
0
    rotate, abortCheckCbk, abortCheckCbkData);
437
0
  contents.fetch(xref, &obj);
438
0
  if (!obj.isNull()) {
439
0
    gfx->saveState();
440
0
    gfx->display(&contents);
441
0
    gfx->endOfPage();
442
0
  }
443
0
  obj.free();
444
445
  // draw (non-form) annotations
446
0
  if (globalParams->getDrawAnnotations()) {
447
0
    Annots *annots2 = doc->getAnnots();
448
0
    annots2->generateAnnotAppearances(num);
449
0
    int n = annots2->getNumAnnots(num);
450
0
    if (n > 0) {
451
0
      if (globalParams->getPrintCommands()) {
452
0
  printf("***** Annotations\n");
453
0
      }
454
0
      for (i = 0; i < n; ++i) {
455
0
  if (abortCheckCbk && (*abortCheckCbk)(abortCheckCbkData)) {
456
0
    break;
457
0
  }
458
0
  annots2->getAnnot(num, i)->draw(gfx, printing);
459
0
      }
460
0
    }
461
0
  }
462
463
  // draw form fields
464
0
  if (globalParams->getDrawFormFields()) {
465
0
    if ((form = doc->getCatalog()->getForm())) {
466
0
      if (!(abortCheckCbk && (*abortCheckCbk)(abortCheckCbkData))) {
467
0
  form->draw(num, gfx, printing);
468
0
      }
469
0
    }
470
0
  }
471
472
0
  delete gfx;
473
0
#endif // PDF_PARSER_ONLY
474
475
0
  traceEnd(this, "end page");
476
0
}
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
0
       PDFRectangle *box, GBool *crop) {
482
0
  PDFRectangle *mediaBox, *cropBox, *baseBox;
483
0
  double kx, ky;
484
485
0
  mediaBox = getMediaBox();
486
0
  cropBox = getCropBox();
487
0
  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
0
  } else if (useMediaBox) {
533
0
    *box = *mediaBox;
534
0
  } else {
535
0
    *box = *cropBox;
536
0
    *crop = gFalse;
537
0
  }
538
0
}
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