Coverage Report

Created: 2026-12-01 06:37

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
761k
void PDFRectangle::clipTo(PDFRectangle *rect) {
37
761k
  if (x1 < rect->x1) {
38
3.47k
    x1 = rect->x1;
39
758k
  } else if (x1 > rect->x2) {
40
2.11k
    x1 = rect->x2;
41
2.11k
  }
42
761k
  if (x2 < rect->x1) {
43
2.32k
    x2 = rect->x1;
44
759k
  } else if (x2 > rect->x2) {
45
3.30k
    x2 = rect->x2;
46
3.30k
  }
47
761k
  if (y1 < rect->y1) {
48
3.20k
    y1 = rect->y1;
49
758k
  } else if (y1 > rect->y2) {
50
2.06k
    y1 = rect->y2;
51
2.06k
  }
52
761k
  if (y2 < rect->y1) {
53
3.18k
    y2 = rect->y1;
54
758k
  } else if (y2 > rect->y2) {
55
2.41k
    y2 = rect->y2;
56
2.41k
  }
57
761k
}
58
59
//------------------------------------------------------------------------
60
// PageAttrs
61
//------------------------------------------------------------------------
62
63
191k
PageAttrs::PageAttrs(PageAttrs *attrs, Dict *dict, XRef *xref) {
64
191k
  Object obj1;
65
66
  // get old/default values
67
191k
  if (attrs) {
68
9.29k
    mediaBox = attrs->mediaBox;
69
9.29k
    cropBox = attrs->cropBox;
70
9.29k
    haveCropBox = attrs->haveCropBox;
71
9.29k
    rotate = attrs->rotate;
72
182k
  } 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
182k
    mediaBox.x1 = 0;
76
182k
    mediaBox.y1 = 0;
77
182k
    mediaBox.x2 = 612;
78
182k
    mediaBox.y2 = 792;
79
182k
    cropBox.x1 = cropBox.y1 = cropBox.x2 = cropBox.y2 = 0;
80
182k
    haveCropBox = gFalse;
81
182k
    rotate = 0;
82
182k
  }
83
84
  // media box
85
191k
  readBox(dict, "MediaBox", &mediaBox);
86
87
  // crop box
88
191k
  if (readBox(dict, "CropBox", &cropBox)) {
89
2.42k
    haveCropBox = gTrue;
90
2.42k
  }
91
191k
  if (!haveCropBox) {
92
189k
    cropBox = mediaBox;
93
189k
  }
94
95
  // other boxes
96
191k
  bleedBox = cropBox;
97
191k
  readBox(dict, "BleedBox", &bleedBox);
98
191k
  trimBox = cropBox;
99
191k
  readBox(dict, "TrimBox", &trimBox);
100
191k
  artBox = cropBox;
101
191k
  readBox(dict, "ArtBox", &artBox);
102
103
  // rotate
104
191k
  dict->lookup("Rotate", &obj1);
105
191k
  if (obj1.isInt()) {
106
1.14k
    rotate = obj1.getInt();
107
1.14k
  }
108
191k
  obj1.free();
109
6.59M
  while (rotate < 0) {
110
6.40M
    rotate += 360;
111
6.40M
  }
112
1.25M
  while (rotate >= 360) {
113
1.06M
    rotate -= 360;
114
1.06M
  }
115
116
  // misc attributes
117
191k
  dict->lookup("LastModified", &lastModified);
118
191k
  dict->lookup("BoxColorInfo", &boxColorInfo);
119
191k
  dict->lookup("Group", &group);
120
191k
  dict->lookup("Metadata", &metadata);
121
191k
  dict->lookup("PieceInfo", &pieceInfo);
122
191k
  dict->lookup("SeparationInfo", &separationInfo);
123
191k
  if (dict->lookup("UserUnit", &obj1)->isNum()) {
124
721
    userUnit = obj1.getNum();
125
721
    if (userUnit < 1) {
126
657
      userUnit = 1;
127
657
    }
128
190k
  } else {
129
190k
    userUnit = 1;
130
190k
  }
131
191k
  obj1.free();
132
133
  // resource dictionary
134
191k
  Object childResDictObj;
135
191k
  dict->lookup("Resources", &childResDictObj);
136
191k
  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
6.77k
    resources.initDict(xref);
140
6.77k
    Dict *resDict = resources.getDict();
141
6.77k
    Dict *parentResDict = attrs->resources.getDict();
142
29.0k
    for (int i = 0; i < parentResDict->getLength(); ++i) {
143
22.3k
      char *resType = parentResDict->getKey(i);
144
22.3k
      Object subdictObj1;
145
22.3k
      if (parentResDict->getVal(i, &subdictObj1)->isDict()) {
146
12.7k
  Dict *subdict1 = subdictObj1.getDict();
147
12.7k
  Object subdictObj2;
148
12.7k
  subdictObj2.initDict(xref);
149
12.7k
  Dict *subdict2 = subdictObj2.getDict();
150
49.7k
  for (int j = 0; j < subdict1->getLength(); ++j) {
151
36.9k
    subdict1->getValNF(j, &obj1);
152
36.9k
    subdict2->add(copyString(subdict1->getKey(j)), &obj1);
153
36.9k
  }
154
12.7k
  resDict->add(copyString(resType), &subdictObj2);
155
12.7k
      }
156
22.3k
      subdictObj1.free();
157
22.3k
    }
158
6.77k
    Dict *childResDict = childResDictObj.getDict();
159
33.3k
    for (int i = 0; i < childResDict->getLength(); ++i) {
160
26.5k
      char *resType = childResDict->getKey(i);
161
26.5k
      Object subdictObj1;
162
26.5k
      if (childResDict->getVal(i, &subdictObj1)->isDict()) {
163
14.4k
  Object subdictObj2;
164
14.4k
  if (resDict->lookup(resType, &subdictObj2)->isDict()) {
165
12.7k
    Dict *subdict1 = subdictObj1.getDict();
166
12.7k
    Dict *subdict2 = subdictObj2.getDict();
167
49.6k
    for (int j = 0; j < subdict1->getLength(); ++j) {
168
36.9k
      subdict1->getValNF(j, &obj1);
169
36.9k
      subdict2->add(copyString(subdict1->getKey(j)), &obj1);
170
36.9k
    }
171
12.7k
    subdictObj2.free();
172
12.7k
  } else {
173
1.70k
    subdictObj2.free();
174
1.70k
    resDict->add(copyString(resType), subdictObj1.copy(&subdictObj2));
175
1.70k
  }
176
14.4k
      }
177
26.5k
      subdictObj1.free();
178
26.5k
    }
179
184k
  } else if (attrs && attrs->resources.isDict()) {
180
411
    attrs->resources.copy(&resources);
181
184k
  } else if (childResDictObj.isDict()) {
182
33.5k
    childResDictObj.copy(&resources);
183
150k
  } else {
184
150k
    resources.initNull();
185
150k
  }
186
191k
  childResDictObj.free();
187
191k
}
188
189
26.3k
PageAttrs::PageAttrs() {
190
26.3k
  mediaBox.x1 = mediaBox.y1 = 0;
191
26.3k
  mediaBox.x2 = mediaBox.y2 = 50;
192
26.3k
  cropBox = mediaBox;
193
26.3k
  haveCropBox = gFalse;
194
26.3k
  bleedBox = cropBox;
195
26.3k
  trimBox = cropBox;
196
26.3k
  artBox = cropBox;
197
26.3k
  rotate = 0;
198
26.3k
  lastModified.initNull();
199
26.3k
  boxColorInfo.initNull();
200
26.3k
  group.initNull();
201
26.3k
  metadata.initNull();
202
26.3k
  pieceInfo.initNull();
203
26.3k
  separationInfo.initNull();
204
26.3k
  userUnit = 1;
205
26.3k
  resources.initNull();
206
26.3k
}
207
208
217k
PageAttrs::~PageAttrs() {
209
217k
  lastModified.free();
210
217k
  boxColorInfo.free();
211
217k
  group.free();
212
217k
  metadata.free();
213
217k
  pieceInfo.free();
214
217k
  separationInfo.free();
215
217k
  resources.free();
216
217k
}
217
218
190k
void PageAttrs::clipBoxes() {
219
190k
  cropBox.clipTo(&mediaBox);
220
190k
  bleedBox.clipTo(&mediaBox);
221
190k
  trimBox.clipTo(&mediaBox);
222
190k
  artBox.clipTo(&mediaBox);
223
190k
}
224
225
957k
GBool PageAttrs::readBox(Dict *dict, const char *key, PDFRectangle *box) {
226
957k
  PDFRectangle tmp;
227
957k
  double t;
228
957k
  Object obj1, obj2;
229
957k
  GBool ok;
230
231
957k
  dict->lookup(key, &obj1);
232
957k
  if (obj1.isArray() && obj1.arrayGetLength() == 4) {
233
37.7k
    ok = gTrue;
234
37.7k
    obj1.arrayGet(0, &obj2);
235
37.7k
    if (obj2.isNum()) {
236
37.3k
      tmp.x1 = obj2.getNum();
237
37.3k
    } else {
238
365
      ok = gFalse;
239
365
    }
240
37.7k
    obj2.free();
241
37.7k
    obj1.arrayGet(1, &obj2);
242
37.7k
    if (obj2.isNum()) {
243
37.0k
      tmp.y1 = obj2.getNum();
244
37.0k
    } else {
245
693
      ok = gFalse;
246
693
    }
247
37.7k
    obj2.free();
248
37.7k
    obj1.arrayGet(2, &obj2);
249
37.7k
    if (obj2.isNum()) {
250
37.4k
      tmp.x2 = obj2.getNum();
251
37.4k
    } else {
252
286
      ok = gFalse;
253
286
    }
254
37.7k
    obj2.free();
255
37.7k
    obj1.arrayGet(3, &obj2);
256
37.7k
    if (obj2.isNum()) {
257
37.4k
      tmp.y2 = obj2.getNum();
258
37.4k
    } else {
259
347
      ok = gFalse;
260
347
    }
261
37.7k
    obj2.free();
262
37.7k
    if (ok) {
263
      // limit the box coords so they can be converted to 32-bit ints later
264
36.8k
      if (tmp.x1 < -1e9) {
265
551
  tmp.x1 = -1e9;
266
36.3k
      } else if (tmp.x1 > 1e9) {
267
286
  tmp.x1 = 1e9;
268
286
      }
269
36.8k
      if (tmp.y1 < -1e9) {
270
398
  tmp.y1 = -1e9;
271
36.4k
      } else if (tmp.y1 > 1e9) {
272
33.2k
  tmp.y1 = 1e9;
273
33.2k
      }
274
36.8k
      if (tmp.x2 < -1e9) {
275
297
  tmp.x2 = -1e9;
276
36.5k
      } else if (tmp.x2 > 1e9) {
277
299
  tmp.x2 = 1e9;
278
299
      }
279
36.8k
      if (tmp.y2 < -1e9) {
280
320
  tmp.y2 = -1e9;
281
36.5k
      } else if (tmp.y2 > 1e9) {
282
439
  tmp.y2 = 1e9;
283
439
      }
284
36.8k
      if (tmp.x1 > tmp.x2) {
285
1.84k
  t = tmp.x1; tmp.x1 = tmp.x2; tmp.x2 = t;
286
1.84k
      }
287
36.8k
      if (tmp.y1 > tmp.y2) {
288
33.2k
  t = tmp.y1; tmp.y1 = tmp.y2; tmp.y2 = t;
289
33.2k
      }
290
36.8k
      *box = tmp;
291
36.8k
    }
292
919k
  } else {
293
919k
    ok = gFalse;
294
919k
  }
295
957k
  obj1.free();
296
957k
  return ok;
297
957k
}
298
299
//------------------------------------------------------------------------
300
// Page
301
//------------------------------------------------------------------------
302
303
190k
Page::Page(PDFDoc *docA, int numA, Dict *pageDict, PageAttrs *attrsA) {
304
190k
  ok = gTrue;
305
190k
  doc = docA;
306
190k
  xref = doc->getXRef();
307
190k
  num = numA;
308
309
  // get attributes
310
190k
  attrs = attrsA;
311
190k
  attrs->clipBoxes();
312
313
  // annotations
314
190k
  pageDict->lookupNF("Annots", &annots);
315
190k
  if (!(annots.isRef() || annots.isArray() || annots.isNull())) {
316
222
    error(errSyntaxError, -1,
317
222
    "Page annotations object (page {0:d}) is wrong type ({1:s})",
318
222
    num, annots.getTypeName());
319
222
    annots.free();
320
222
    goto err2;
321
222
  }
322
323
  // contents
324
190k
  pageDict->lookupNF("Contents", &contents);
325
190k
  if (!(contents.isRef() || contents.isArray() ||
326
182k
  contents.isNull())) {
327
1.06k
    error(errSyntaxError, -1,
328
1.06k
    "Page contents object (page {0:d}) is wrong type ({1:s})",
329
1.06k
    num, contents.getTypeName());
330
1.06k
    contents.free();
331
1.06k
    goto err1;
332
1.06k
  }
333
334
  // thumbnail
335
189k
  pageDict->lookupNF("Thumb", &thumbnail);
336
189k
  if (!thumbnail.isRef()) {
337
156k
    if (!thumbnail.isNull()) {
338
421
      thumbnail.free();
339
421
      thumbnail.initNull();
340
421
    }
341
156k
  }
342
343
189k
  return;
344
345
222
 err2:
346
222
  annots.initNull();
347
1.28k
 err1:
348
1.28k
  contents.initNull();
349
1.28k
  thumbnail.initNull();
350
1.28k
  ok = gFalse;
351
1.28k
}
352
353
26.3k
Page::Page(PDFDoc *docA, int numA) {
354
26.3k
  doc = docA;
355
26.3k
  xref = doc->getXRef();
356
26.3k
  num = numA;
357
26.3k
  attrs = new PageAttrs();
358
26.3k
  annots.initNull();
359
26.3k
  contents.initNull();
360
26.3k
  thumbnail.initNull();
361
26.3k
  ok = gTrue;
362
26.3k
}
363
364
216k
Page::~Page() {
365
216k
  delete attrs;
366
216k
  annots.free();
367
216k
  contents.free();
368
216k
  thumbnail.free();
369
216k
}
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