Coverage Report

Created: 2026-02-04 06:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/xpdf-4.06/xpdf/Annot.cc
Line
Count
Source
1
//========================================================================
2
//
3
// Annot.cc
4
//
5
// Copyright 2000-2022 Glyph & Cog, LLC
6
//
7
//========================================================================
8
9
#include <aconf.h>
10
11
#include <stdlib.h>
12
#include <math.h>
13
#include <limits.h>
14
#include "gmem.h"
15
#include "gmempp.h"
16
#include "GList.h"
17
#include "Error.h"
18
#include "Object.h"
19
#include "Catalog.h"
20
#include "Gfx.h"
21
#include "GfxFont.h"
22
#include "Lexer.h"
23
#include "PDFDoc.h"
24
#include "OptionalContent.h"
25
#include "AcroForm.h"
26
#include "BuiltinFontTables.h"
27
#include "FontEncodingTables.h"
28
#include "Annot.h"
29
30
// the MSVC math.h doesn't define this
31
#ifndef M_PI
32
#define M_PI 3.14159265358979323846
33
#endif
34
35
//------------------------------------------------------------------------
36
37
10.4k
#define annotFlagHidden    0x0002
38
1.07k
#define annotFlagPrint     0x0004
39
9.14k
#define annotFlagNoView    0x0020
40
41
// distance of Bezier control point from center for circle approximation
42
// = (4 * (sqrt(2) - 1) / 3) * r
43
0
#define bezierCircle 0.55228475
44
45
0
#define lineEndSize1    6
46
44
#define lineEndSize2   10
47
198
#define lineArrowAngle (M_PI / 6)
48
49
//------------------------------------------------------------------------
50
// AnnotBorderStyle
51
//------------------------------------------------------------------------
52
53
AnnotBorderStyle::AnnotBorderStyle(AnnotBorderType typeA, double widthA,
54
           double *dashA, int dashLengthA,
55
31.4k
           double *colorA, int nColorCompsA) {
56
31.4k
  type = typeA;
57
31.4k
  width = widthA;
58
31.4k
  dash = dashA;
59
31.4k
  dashLength = dashLengthA;
60
31.4k
  color[0] = colorA[0];
61
31.4k
  color[1] = colorA[1];
62
31.4k
  color[2] = colorA[2];
63
31.4k
  color[3] = colorA[3];
64
31.4k
  nColorComps = nColorCompsA;
65
31.4k
}
66
67
31.4k
AnnotBorderStyle::~AnnotBorderStyle() {
68
31.4k
  if (dash) {
69
56
    gfree(dash);
70
56
  }
71
31.4k
}
72
73
//------------------------------------------------------------------------
74
// Annot
75
//------------------------------------------------------------------------
76
77
31.4k
Annot::Annot(PDFDoc *docA, Dict *dict, Ref *refA) {
78
31.4k
  Object apObj, asObj, obj1, obj2, obj3;
79
31.4k
  AnnotBorderType borderType;
80
31.4k
  double borderWidth;
81
31.4k
  double *borderDash;
82
31.4k
  int borderDashLength;
83
31.4k
  double borderColor[4];
84
31.4k
  int nBorderColorComps;
85
31.4k
  double t;
86
31.4k
  int i;
87
88
31.4k
  ok = gTrue;
89
31.4k
  doc = docA;
90
31.4k
  xref = doc->getXRef();
91
31.4k
  ref = *refA;
92
31.4k
  type = NULL;
93
31.4k
  appearanceState = NULL;
94
31.4k
  appearBuf = NULL;
95
31.4k
  borderStyle = NULL;
96
97
  //----- parse the type
98
99
31.4k
  if (dict->lookup("Subtype", &obj1)->isName()) {
100
11.6k
    type = new GString(obj1.getName());
101
11.6k
  }
102
31.4k
  obj1.free();
103
104
  //----- parse the rectangle
105
106
31.4k
  if (dict->lookup("Rect", &obj1)->isArray() &&
107
10.9k
      obj1.arrayGetLength() == 4) {
108
10.4k
    xMin = yMin = xMax = yMax = 0;
109
10.4k
    if (obj1.arrayGet(0, &obj2)->isNum()) {
110
10.2k
      xMin = obj2.getNum();
111
10.2k
    }
112
10.4k
    obj2.free();
113
10.4k
    if (obj1.arrayGet(1, &obj2)->isNum()) {
114
10.2k
      yMin = obj2.getNum();
115
10.2k
    }
116
10.4k
    obj2.free();
117
10.4k
    if (obj1.arrayGet(2, &obj2)->isNum()) {
118
7.54k
      xMax = obj2.getNum();
119
7.54k
    }
120
10.4k
    obj2.free();
121
10.4k
    if (obj1.arrayGet(3, &obj2)->isNum()) {
122
10.0k
      yMax = obj2.getNum();
123
10.0k
    }
124
10.4k
    obj2.free();
125
10.4k
    if (xMin > xMax) {
126
3.21k
      t = xMin; xMin = xMax; xMax = t;
127
3.21k
    }
128
10.4k
    if (yMin > yMax) {
129
297
      t = yMin; yMin = yMax; yMax = t;
130
297
    }
131
21.0k
  } else {
132
21.0k
    error(errSyntaxError, -1, "Bad bounding box for annotation");
133
21.0k
    ok = gFalse;
134
21.0k
  }
135
31.4k
  obj1.free();
136
137
  //----- parse the flags
138
139
31.4k
  if (dict->lookup("F", &obj1)->isInt()) {
140
6.81k
    flags = obj1.getInt();
141
24.6k
  } else {
142
24.6k
    flags = 0;
143
24.6k
  }
144
31.4k
  obj1.free();
145
146
  //----- parse the border style
147
148
31.4k
  borderType = annotBorderSolid;
149
31.4k
  borderWidth = 1;
150
31.4k
  borderDash = NULL;
151
31.4k
  borderDashLength = 0;
152
31.4k
  nBorderColorComps = 3;
153
31.4k
  borderColor[0] = 0;
154
31.4k
  borderColor[1] = 0;
155
31.4k
  borderColor[2] = 1;
156
31.4k
  borderColor[3] = 0;
157
31.4k
  if (dict->lookup("BS", &obj1)->isDict()) {
158
660
    if (obj1.dictLookup("S", &obj2)->isName()) {
159
555
      if (obj2.isName("S")) {
160
438
  borderType = annotBorderSolid;
161
438
      } else if (obj2.isName("D")) {
162
11
  borderType = annotBorderDashed;
163
106
      } else if (obj2.isName("B")) {
164
14
  borderType = annotBorderBeveled;
165
92
      } else if (obj2.isName("I")) {
166
53
  borderType = annotBorderInset;
167
53
      } else if (obj2.isName("U")) {
168
7
  borderType = annotBorderUnderlined;
169
7
      }
170
555
    }
171
660
    obj2.free();
172
660
    if (obj1.dictLookup("W", &obj2)->isNum()) {
173
615
      borderWidth = obj2.getNum();
174
615
    }
175
660
    obj2.free();
176
660
    if (obj1.dictLookup("D", &obj2)->isArray()) {
177
5
      borderDashLength = obj2.arrayGetLength();
178
5
      borderDash = (double *)gmallocn(borderDashLength, sizeof(double));
179
10
      for (i = 0; i < borderDashLength; ++i) {
180
5
  if (obj2.arrayGet(i, &obj3)->isNum()) {
181
3
    borderDash[i] = obj3.getNum();
182
3
  } else {
183
2
    borderDash[i] = 1;
184
2
  }
185
5
  obj3.free();
186
5
      }
187
5
    }
188
660
    obj2.free();
189
30.7k
  } else {
190
30.7k
    obj1.free();
191
30.7k
    if (dict->lookup("Border", &obj1)->isArray()) {
192
1.59k
      if (obj1.arrayGetLength() >= 3) {
193
1.37k
  if (obj1.arrayGet(2, &obj2)->isNum()) {
194
1.23k
    borderWidth = obj2.getNum();
195
1.23k
  }
196
1.37k
  obj2.free();
197
1.37k
  if (obj1.arrayGetLength() >= 4) {
198
448
    if (obj1.arrayGet(3, &obj2)->isArray()) {
199
51
      borderType = annotBorderDashed;
200
51
      borderDashLength = obj2.arrayGetLength();
201
51
      borderDash = (double *)gmallocn(borderDashLength, sizeof(double));
202
901
      for (i = 0; i < borderDashLength; ++i) {
203
850
        if (obj2.arrayGet(i, &obj3)->isNum()) {
204
216
    borderDash[i] = obj3.getNum();
205
634
        } else {
206
634
    borderDash[i] = 1;
207
634
        }
208
850
        obj3.free();
209
850
      }
210
397
    } else {
211
      // Adobe draws no border at all if the last element is of
212
      // the wrong type.
213
397
      borderWidth = 0;
214
397
    }
215
448
    obj2.free();
216
448
  }
217
1.37k
      } else {
218
  // an empty Border array also means "no border"
219
228
  borderWidth = 0;
220
228
      }
221
1.59k
    }
222
30.7k
  }
223
31.4k
  obj1.free();
224
  // Acrobat ignores borders with unreasonable widths
225
31.4k
  if (borderWidth > 1 && (borderWidth > xMax - xMin ||
226
418
        borderWidth > yMax - yMin)) {
227
58
    borderWidth = 0;
228
58
  }
229
31.4k
  if (dict->lookup("C", &obj1)->isArray() &&
230
2.44k
      (obj1.arrayGetLength() == 1 ||
231
2.43k
       obj1.arrayGetLength() == 3 ||
232
2.07k
       obj1.arrayGetLength() == 4)) {
233
2.07k
    nBorderColorComps = obj1.arrayGetLength();
234
8.66k
    for (i = 0; i < nBorderColorComps; ++i) {
235
6.58k
      if (obj1.arrayGet(i, &obj2)->isNum()) {
236
4.13k
  borderColor[i] = obj2.getNum();
237
4.13k
      } else {
238
2.44k
  borderColor[i] = 0;
239
2.44k
      }
240
6.58k
      obj2.free();
241
6.58k
    }
242
2.07k
  }
243
31.4k
  obj1.free();
244
31.4k
  borderStyle = new AnnotBorderStyle(borderType, borderWidth,
245
31.4k
             borderDash, borderDashLength,
246
31.4k
             borderColor, nBorderColorComps);
247
248
  //----- get the appearance state
249
250
31.4k
  dict->lookup("AP", &apObj);
251
31.4k
  dict->lookup("AS", &asObj);
252
31.4k
  if (asObj.isName()) {
253
281
    appearanceState = new GString(asObj.getName());
254
31.1k
  } else if (apObj.isDict()) {
255
1.08k
    apObj.dictLookup("N", &obj1);
256
1.08k
    if (obj1.isDict() && obj1.dictGetLength() == 1) {
257
13
      appearanceState = new GString(obj1.dictGetKey(0));
258
13
    }
259
1.08k
    obj1.free();
260
1.08k
  }
261
31.4k
  if (!appearanceState) {
262
31.1k
    appearanceState = new GString("Off");
263
31.1k
  }
264
31.4k
  asObj.free();
265
266
  //----- get the annotation appearance
267
268
31.4k
  if (apObj.isDict()) {
269
1.35k
    apObj.dictLookup("N", &obj1);
270
1.35k
    apObj.dictLookupNF("N", &obj2);
271
1.35k
    if (obj1.isDict()) {
272
418
      if (obj1.dictLookupNF(appearanceState->getCString(), &obj3)->isRef()) {
273
225
  obj3.copy(&appearance);
274
225
      }
275
418
      obj3.free();
276
940
    } else if (obj2.isRef()) {
277
775
      obj2.copy(&appearance);
278
775
    } else if (obj2.isStream()) {
279
0
      obj2.copy(&appearance);
280
0
    }
281
1.35k
    obj1.free();
282
1.35k
    obj2.free();
283
1.35k
  }
284
31.4k
  apObj.free();
285
286
  //----- get the optional content entry
287
288
31.4k
  dict->lookupNF("OC", &ocObj);
289
31.4k
}
290
291
31.4k
Annot::~Annot() {
292
31.4k
  if (type) {
293
11.6k
    delete type;
294
11.6k
  }
295
31.4k
  if (appearanceState) {
296
31.4k
    delete appearanceState;
297
31.4k
  }
298
31.4k
  appearance.free();
299
31.4k
  if (appearBuf) {
300
289
    delete appearBuf;
301
289
  }
302
31.4k
  if (borderStyle) {
303
31.4k
    delete borderStyle;
304
31.4k
  }
305
31.4k
  ocObj.free();
306
31.4k
}
307
308
10.4k
void Annot::generateAnnotAppearance(Object *annotObj) {
309
10.4k
  Object obj;
310
10.4k
  appearance.fetch(doc->getXRef(), &obj);
311
10.4k
  GBool alreadyHaveAppearance = obj.isStream();
312
10.4k
  obj.free();
313
10.4k
  if (alreadyHaveAppearance) {
314
398
    return;
315
398
  }
316
317
10.0k
  if (!type || (type->cmp("Line") &&
318
9.68k
    type->cmp("PolyLine") &&
319
9.57k
    type->cmp("Polygon") &&
320
9.67k
    type->cmp("FreeText"))) {
321
9.67k
    return;
322
9.67k
  }
323
324
335
  Object annotObj2;
325
335
  if (!annotObj) {
326
335
    getObject(&annotObj2);
327
335
    annotObj = &annotObj2;
328
335
  }
329
335
  if (!annotObj->isDict()) {
330
46
    annotObj2.free();
331
46
    return;
332
46
  }
333
334
289
  if (!type->cmp("Line")) {
335
92
    generateLineAppearance(annotObj);
336
197
  } else if (!type->cmp("PolyLine")) {
337
108
    generatePolyLineAppearance(annotObj);
338
108
  } else if (!type->cmp("Polygon")) {
339
0
    generatePolygonAppearance(annotObj);
340
89
  } else if (!type->cmp("FreeText")) {
341
89
    generateFreeTextAppearance(annotObj);
342
89
  }
343
344
289
  annotObj2.free();
345
289
}
346
347
//~ this doesn't draw the caption
348
92
void Annot::generateLineAppearance(Object *annotObj) {
349
92
  Object gfxStateDict, appearDict, obj1, obj2;
350
92
  MemStream *appearStream;
351
92
  double x1, y1, x2, y2, dx, dy, len, w;
352
92
  double lx1, ly1, lx2, ly2;
353
92
  double tx1, ty1, tx2, ty2;
354
92
  double ax1, ay1, ax2, ay2;
355
92
  double bx1, by1, bx2, by2;
356
92
  double leaderLen, leaderExtLen, leaderOffLen;
357
92
  AnnotLineEndType lineEnd1, lineEnd2;
358
92
  GBool fill;
359
360
92
  appearBuf = new GString();
361
362
  //----- check for transparency
363
92
  if (annotObj->dictLookup("CA", &obj1)->isNum()) {
364
42
    gfxStateDict.initDict(doc->getXRef());
365
42
    gfxStateDict.dictAdd(copyString("ca"), obj1.copy(&obj2));
366
42
    appearBuf->append("/GS1 gs\n");
367
42
  }
368
92
  obj1.free();
369
370
  //----- set line style, colors
371
92
  setLineStyle(borderStyle, &w);
372
92
  setStrokeColor(borderStyle->getColor(), borderStyle->getNumColorComps());
373
92
  fill = gFalse;
374
92
  if (annotObj->dictLookup("IC", &obj1)->isArray()) {
375
63
    if (setFillColor(&obj1)) {
376
61
      fill = gTrue;
377
61
    }
378
63
  }
379
92
  obj1.free();
380
381
  //----- get line properties
382
92
  if (annotObj->dictLookup("L", &obj1)->isArray() &&
383
87
      obj1.arrayGetLength() == 4) {
384
81
    if (obj1.arrayGet(0, &obj2)->isNum()) {
385
79
      x1 = obj2.getNum();
386
79
    } else {
387
2
      obj2.free();
388
2
      obj1.free();
389
2
      return;
390
2
    }
391
79
    obj2.free();
392
79
    if (obj1.arrayGet(1, &obj2)->isNum()) {
393
71
      y1 = obj2.getNum();
394
71
    } else {
395
8
      obj2.free();
396
8
      obj1.free();
397
8
      return;
398
8
    }
399
71
    obj2.free();
400
71
    if (obj1.arrayGet(2, &obj2)->isNum()) {
401
70
      x2 = obj2.getNum();
402
70
    } else {
403
1
      obj2.free();
404
1
      obj1.free();
405
1
      return;
406
1
    }
407
70
    obj2.free();
408
70
    if (obj1.arrayGet(3, &obj2)->isNum()) {
409
63
      y2 = obj2.getNum();
410
63
    } else {
411
7
      obj2.free();
412
7
      obj1.free();
413
7
      return;
414
7
    }
415
63
    obj2.free();
416
63
  } else {
417
11
    obj1.free();
418
11
    return;
419
11
  }
420
63
  obj1.free();
421
63
  lineEnd1 = lineEnd2 = annotLineEndNone;
422
63
  if (annotObj->dictLookup("LE", &obj1)->isArray() &&
423
48
      obj1.arrayGetLength() == 2) {
424
44
    lineEnd1 = parseLineEndType(obj1.arrayGet(0, &obj2));
425
44
    obj2.free();
426
44
    lineEnd2 = parseLineEndType(obj1.arrayGet(1, &obj2));
427
44
    obj2.free();
428
44
  }
429
63
  obj1.free();
430
63
  if (annotObj->dictLookup("LL", &obj1)->isNum()) {
431
0
    leaderLen = obj1.getNum();
432
63
  } else {
433
63
    leaderLen = 0;
434
63
  }
435
63
  obj1.free();
436
63
  if (annotObj->dictLookup("LLE", &obj1)->isNum()) {
437
0
    leaderExtLen = obj1.getNum();
438
63
  } else {
439
63
    leaderExtLen = 0;
440
63
  }
441
63
  obj1.free();
442
63
  if (annotObj->dictLookup("LLO", &obj1)->isNum()) {
443
0
    leaderOffLen = obj1.getNum();
444
63
  } else {
445
63
    leaderOffLen = 0;
446
63
  }
447
63
  obj1.free();
448
449
  //----- compute positions
450
63
  x1 -= xMin;
451
63
  y1 -= yMin;
452
63
  x2 -= xMin;
453
63
  y2 -= yMin;
454
63
  dx = x2 - x1;
455
63
  dy = y2 - y1;
456
63
  len = sqrt(dx*dx + dy*dy);
457
63
  if (len > 0) {
458
63
    dx /= len;
459
63
    dy /= len;
460
63
  }
461
63
  if (leaderLen != 0) {
462
0
    ax1 = x1 + leaderOffLen * dy;
463
0
    ay1 = y1 - leaderOffLen * dx;
464
0
    lx1 = ax1 + leaderLen * dy;
465
0
    ly1 = ay1 - leaderLen * dx;
466
0
    bx1 = lx1 + leaderExtLen * dy;
467
0
    by1 = ly1 - leaderExtLen * dx;
468
0
    ax2 = x2 + leaderOffLen * dy;
469
0
    ay2 = y2 - leaderOffLen * dx;
470
0
    lx2 = ax2 + leaderLen * dy;
471
0
    ly2 = ay2 - leaderLen * dx;
472
0
    bx2 = lx2 + leaderExtLen * dy;
473
0
    by2 = ly2 - leaderExtLen * dx;
474
63
  } else {
475
63
    lx1 = x1;
476
63
    ly1 = y1;
477
63
    lx2 = x2;
478
63
    ly2 = y2;
479
63
    ax1 = ay1 = ax2 = ay2 = 0; // make gcc happy
480
63
    bx1 = by1 = bx2 = by2 = 0;
481
63
  }
482
63
  adjustLineEndpoint(lineEnd1, lx1, ly1, dx, dy, w, &tx1, &ty1);
483
63
  adjustLineEndpoint(lineEnd2, lx2, ly2, -dx, -dy, w, &tx2, &ty2);
484
485
  //----- draw leaders
486
63
  if (leaderLen != 0) {
487
0
    appearBuf->appendf("{0:.4f} {1:.4f} m {2:.4f} {3:.4f} l\n",
488
0
           ax1, ay1, bx1, by1);
489
0
    appearBuf->appendf("{0:.4f} {1:.4f} m {2:.4f} {3:.4f} l\n",
490
0
           ax2, ay2 , bx2, by2);
491
0
  }
492
493
  //----- draw the line
494
63
  appearBuf->appendf("{0:.4f} {1:.4f} m {2:.4f} {3:.4f} l\n",
495
63
         tx1, ty1, tx2, ty2);
496
63
  appearBuf->append("S\n");
497
498
  //----- draw the arrows
499
63
  if (borderStyle->getType() == annotBorderDashed) {
500
0
    appearBuf->append("[] 0 d\n");
501
0
  }
502
63
  drawLineArrow(lineEnd1, lx1, ly1, dx, dy, w, fill);
503
63
  drawLineArrow(lineEnd2, lx2, ly2, -dx, -dy, w, fill);
504
505
  //----- build the appearance stream dictionary
506
63
  appearDict.initDict(doc->getXRef());
507
63
  appearDict.dictAdd(copyString("Length"),
508
63
         obj1.initInt(appearBuf->getLength()));
509
63
  appearDict.dictAdd(copyString("Subtype"), obj1.initName("Form"));
510
63
  obj1.initArray(doc->getXRef());
511
63
  obj1.arrayAdd(obj2.initReal(0));
512
63
  obj1.arrayAdd(obj2.initReal(0));
513
63
  obj1.arrayAdd(obj2.initReal(xMax - xMin));
514
63
  obj1.arrayAdd(obj2.initReal(yMax - yMin));
515
63
  appearDict.dictAdd(copyString("BBox"), &obj1);
516
63
  if (gfxStateDict.isDict()) {
517
30
    obj1.initDict(doc->getXRef());
518
30
    obj2.initDict(doc->getXRef());
519
30
    obj2.dictAdd(copyString("GS1"), &gfxStateDict);
520
30
    obj1.dictAdd(copyString("ExtGState"), &obj2);
521
30
    appearDict.dictAdd(copyString("Resources"), &obj1);
522
30
  }
523
524
  //----- build the appearance stream
525
63
  appearStream = new MemStream(appearBuf->getCString(), 0,
526
63
             appearBuf->getLength(), &appearDict);
527
63
  appearance.free();
528
63
  appearance.initStream(appearStream);
529
63
}
530
531
//~ this doesn't handle line ends (arrows)
532
108
void Annot::generatePolyLineAppearance(Object *annotObj) {
533
108
  Object gfxStateDict, appearDict, obj1, obj2;
534
108
  MemStream *appearStream;
535
108
  double x1, y1, w;
536
108
  int i;
537
538
108
  appearBuf = new GString();
539
540
  //----- check for transparency
541
108
  if (annotObj->dictLookup("CA", &obj1)->isNum()) {
542
87
    gfxStateDict.initDict(doc->getXRef());
543
87
    gfxStateDict.dictAdd(copyString("ca"), obj1.copy(&obj2));
544
87
    appearBuf->append("/GS1 gs\n");
545
87
  }
546
108
  obj1.free();
547
548
  //----- set line style, colors
549
108
  setLineStyle(borderStyle, &w);
550
108
  setStrokeColor(borderStyle->getColor(), borderStyle->getNumColorComps());
551
  // fill = gFalse;
552
  // if (annotObj->dictLookup("IC", &obj1)->isArray()) {
553
  //   if (setFillColor(&obj1)) {
554
  //     fill = gTrue;
555
  //   }
556
  // }
557
  // obj1.free();
558
559
  //----- draw line
560
108
  if (!annotObj->dictLookup("Vertices", &obj1)->isArray()) {
561
7
    obj1.free();
562
7
    return;
563
7
  }
564
344
  for (i = 0; i+1 < obj1.arrayGetLength(); i += 2) {
565
293
    if (!obj1.arrayGet(i, &obj2)->isNum()) {
566
13
      obj2.free();
567
13
      obj1.free();
568
13
      return;
569
13
    }
570
280
    x1 = obj2.getNum();
571
280
    obj2.free();
572
280
    if (!obj1.arrayGet(i+1, &obj2)->isNum()) {
573
37
      obj2.free();
574
37
      obj1.free();
575
37
      return;
576
37
    }
577
243
    y1 = obj2.getNum();
578
243
    obj2.free();
579
243
    x1 -= xMin;
580
243
    y1 -= yMin;
581
243
    if (i == 0) {
582
69
      appearBuf->appendf("{0:.4f} {1:.4f} m\n", x1, y1);
583
174
    } else {
584
174
      appearBuf->appendf("{0:.4f} {1:.4f} l\n", x1, y1);
585
174
    }
586
243
  }
587
51
  appearBuf->append("S\n");
588
51
  obj1.free();
589
590
  //----- build the appearance stream dictionary
591
51
  appearDict.initDict(doc->getXRef());
592
51
  appearDict.dictAdd(copyString("Length"),
593
51
         obj1.initInt(appearBuf->getLength()));
594
51
  appearDict.dictAdd(copyString("Subtype"), obj1.initName("Form"));
595
51
  obj1.initArray(doc->getXRef());
596
51
  obj1.arrayAdd(obj2.initReal(0));
597
51
  obj1.arrayAdd(obj2.initReal(0));
598
51
  obj1.arrayAdd(obj2.initReal(xMax - xMin));
599
51
  obj1.arrayAdd(obj2.initReal(yMax - yMin));
600
51
  appearDict.dictAdd(copyString("BBox"), &obj1);
601
51
  if (gfxStateDict.isDict()) {
602
32
    obj1.initDict(doc->getXRef());
603
32
    obj2.initDict(doc->getXRef());
604
32
    obj2.dictAdd(copyString("GS1"), &gfxStateDict);
605
32
    obj1.dictAdd(copyString("ExtGState"), &obj2);
606
32
    appearDict.dictAdd(copyString("Resources"), &obj1);
607
32
  }
608
609
  //----- build the appearance stream
610
51
  appearStream = new MemStream(appearBuf->getCString(), 0,
611
51
             appearBuf->getLength(), &appearDict);
612
51
  appearance.free();
613
51
  appearance.initStream(appearStream);
614
51
}
615
616
0
void Annot::generatePolygonAppearance(Object *annotObj) {
617
0
  Object gfxStateDict, appearDict, obj1, obj2;
618
0
  MemStream *appearStream;
619
0
  double x1, y1;
620
0
  int i;
621
622
0
  appearBuf = new GString();
623
624
  //----- check for transparency
625
0
  if (annotObj->dictLookup("CA", &obj1)->isNum()) {
626
0
    gfxStateDict.initDict(doc->getXRef());
627
0
    gfxStateDict.dictAdd(copyString("ca"), obj1.copy(&obj2));
628
0
    appearBuf->append("/GS1 gs\n");
629
0
  }
630
0
  obj1.free();
631
632
  //----- set fill color
633
0
  if (!annotObj->dictLookup("IC", &obj1)->isArray()  ||
634
0
      !setFillColor(&obj1)) {
635
0
    obj1.free();
636
0
    return;
637
0
  }
638
0
  obj1.free();
639
640
  //----- fill polygon
641
0
  if (!annotObj->dictLookup("Vertices", &obj1)->isArray()) {
642
0
    obj1.free();
643
0
    return;
644
0
  }
645
0
  for (i = 0; i+1 < obj1.arrayGetLength(); i += 2) {
646
0
    if (!obj1.arrayGet(i, &obj2)->isNum()) {
647
0
      obj2.free();
648
0
      obj1.free();
649
0
      return;
650
0
    }
651
0
    x1 = obj2.getNum();
652
0
    obj2.free();
653
0
    if (!obj1.arrayGet(i+1, &obj2)->isNum()) {
654
0
      obj2.free();
655
0
      obj1.free();
656
0
      return;
657
0
    }
658
0
    y1 = obj2.getNum();
659
0
    obj2.free();
660
0
    x1 -= xMin;
661
0
    y1 -= yMin;
662
0
    if (i == 0) {
663
0
      appearBuf->appendf("{0:.4f} {1:.4f} m\n", x1, y1);
664
0
    } else {
665
0
      appearBuf->appendf("{0:.4f} {1:.4f} l\n", x1, y1);
666
0
    }
667
0
  }
668
0
  appearBuf->append("f\n");
669
0
  obj1.free();
670
671
  //----- build the appearance stream dictionary
672
0
  appearDict.initDict(doc->getXRef());
673
0
  appearDict.dictAdd(copyString("Length"),
674
0
         obj1.initInt(appearBuf->getLength()));
675
0
  appearDict.dictAdd(copyString("Subtype"), obj1.initName("Form"));
676
0
  obj1.initArray(doc->getXRef());
677
0
  obj1.arrayAdd(obj2.initReal(0));
678
0
  obj1.arrayAdd(obj2.initReal(0));
679
0
  obj1.arrayAdd(obj2.initReal(xMax - xMin));
680
0
  obj1.arrayAdd(obj2.initReal(yMax - yMin));
681
0
  appearDict.dictAdd(copyString("BBox"), &obj1);
682
0
  if (gfxStateDict.isDict()) {
683
0
    obj1.initDict(doc->getXRef());
684
0
    obj2.initDict(doc->getXRef());
685
0
    obj2.dictAdd(copyString("GS1"), &gfxStateDict);
686
0
    obj1.dictAdd(copyString("ExtGState"), &obj2);
687
0
    appearDict.dictAdd(copyString("Resources"), &obj1);
688
0
  }
689
690
  //----- build the appearance stream
691
0
  appearStream = new MemStream(appearBuf->getCString(), 0,
692
0
             appearBuf->getLength(), &appearDict);
693
0
  appearance.free();
694
0
  appearance.initStream(appearStream);
695
0
}
696
697
//~ this doesn't handle rich text
698
//~ this doesn't handle the callout
699
//~ this doesn't handle the RD field
700
89
void Annot::generateFreeTextAppearance(Object *annotObj) {
701
89
  Object gfxStateDict, appearDict, obj1, obj2;
702
89
  Object resources, gsResources, fontResources, defaultFont;
703
89
  GString *text, *da;
704
89
  double lineWidth;
705
89
  int quadding, rot;
706
89
  MemStream *appearStream;
707
708
89
  appearBuf = new GString();
709
710
  //----- check for transparency
711
89
  if (annotObj->dictLookup("CA", &obj1)->isNum()) {
712
44
    gfxStateDict.initDict(doc->getXRef());
713
44
    gfxStateDict.dictAdd(copyString("ca"), obj1.copy(&obj2));
714
44
    appearBuf->append("/GS1 gs\n");
715
44
  }
716
89
  obj1.free();
717
718
  //----- draw the text
719
89
  if (annotObj->dictLookup("Contents", &obj1)->isString()) {
720
80
    text = obj1.getString()->copy();
721
80
  } else {
722
9
    text = new GString();
723
9
  }
724
89
  obj1.free();
725
89
  if (annotObj->dictLookup("Q", &obj1)->isInt()) {
726
2
    quadding = obj1.getInt();
727
87
  } else {
728
87
    quadding = 0;
729
87
  }
730
89
  obj1.free();
731
89
  if (annotObj->dictLookup("DA", &obj1)->isString()) {
732
30
    da = obj1.getString()->copy();
733
59
  } else {
734
59
    da = new GString();
735
59
  }
736
89
  obj1.free();
737
  // the "Rotate" field is not defined in the PDF spec, but Acrobat
738
  // looks at it
739
89
  if (annotObj->dictLookup("Rotate", &obj1)->isInt()) {
740
1
    rot = obj1.getInt();
741
88
  } else {
742
88
    rot = 0;
743
88
  }
744
89
  obj1.free();
745
89
  drawText(text, da, quadding, 0, rot);
746
89
  delete text;
747
89
  delete da;
748
749
  //----- draw the border
750
89
  if (borderStyle->getWidth() != 0) {
751
25
    setLineStyle(borderStyle, &lineWidth);
752
25
    appearBuf->appendf("{0:.4f} {1:.4f} {2:.4f} {3:.4f} re s\n",
753
25
           0.5 * lineWidth, 0.5 * lineWidth,
754
25
           xMax - xMin - lineWidth, yMax - yMin - lineWidth);
755
25
  }
756
757
  //----- build the appearance stream dictionary
758
89
  appearDict.initDict(doc->getXRef());
759
89
  appearDict.dictAdd(copyString("Length"),
760
89
         obj1.initInt(appearBuf->getLength()));
761
89
  appearDict.dictAdd(copyString("Subtype"), obj1.initName("Form"));
762
89
  obj1.initArray(doc->getXRef());
763
89
  obj1.arrayAdd(obj2.initReal(0));
764
89
  obj1.arrayAdd(obj2.initReal(0));
765
89
  obj1.arrayAdd(obj2.initReal(xMax - xMin));
766
89
  obj1.arrayAdd(obj2.initReal(yMax - yMin));
767
89
  appearDict.dictAdd(copyString("BBox"), &obj1);
768
89
  resources.initDict(doc->getXRef());
769
89
  defaultFont.initDict(doc->getXRef());
770
89
  defaultFont.dictAdd(copyString("Type"), obj1.initName("Font"));
771
89
  defaultFont.dictAdd(copyString("Subtype"), obj1.initName("Type1"));
772
89
  defaultFont.dictAdd(copyString("BaseFont"), obj1.initName("Helvetica"));
773
89
  defaultFont.dictAdd(copyString("Encoding"), obj1.initName("WinAnsiEncoding"));
774
89
  fontResources.initDict(doc->getXRef());
775
89
  fontResources.dictAdd(copyString("xpdf_default_font"), &defaultFont);
776
89
  resources.dictAdd(copyString("Font"), &fontResources);
777
89
  if (gfxStateDict.isDict()) {
778
44
    gsResources.initDict(doc->getXRef());
779
44
    gsResources.dictAdd(copyString("GS1"), &gfxStateDict);
780
44
    resources.dictAdd(copyString("ExtGState"), &gsResources);
781
44
  }
782
89
  appearDict.dictAdd(copyString("Resources"), &resources);
783
784
  //----- build the appearance stream
785
89
  appearStream = new MemStream(appearBuf->getCString(), 0,
786
89
             appearBuf->getLength(), &appearDict);
787
89
  appearance.free();
788
89
  appearance.initStream(appearStream);
789
89
}
790
791
225
void Annot::setLineStyle(AnnotBorderStyle *bs, double *lineWidth) {
792
225
  double *dash;
793
225
  double w;
794
225
  int dashLength, i;
795
796
225
  if ((w = borderStyle->getWidth()) <= 0) {
797
9
    w = 0.1;
798
9
  }
799
225
  *lineWidth = w;
800
225
  appearBuf->appendf("{0:.4f} w\n", w);
801
  // this treats beveled/inset/underline as solid
802
225
  if (borderStyle->getType() == annotBorderDashed) {
803
1
    borderStyle->getDash(&dash, &dashLength);
804
1
    appearBuf->append("[");
805
23
    for (i = 0; i < dashLength; ++i) {
806
22
      appearBuf->appendf(" {0:.4f}", dash[i]);
807
22
    }
808
1
    appearBuf->append("] 0 d\n");
809
1
  }
810
225
  appearBuf->append("0 j\n0 J\n");
811
225
}
812
813
200
void Annot::setStrokeColor(double *color, int nComps) {
814
200
  switch (nComps) {
815
0
  case 0:
816
0
    appearBuf->append("0 G\n");
817
0
    break;
818
1
  case 1:
819
1
    appearBuf->appendf("{0:.2f} G\n", color[0]);
820
1
    break;
821
185
  case 3:
822
185
    appearBuf->appendf("{0:.2f} {1:.2f} {2:.2f} RG\n",
823
185
           color[0], color[1], color[2]);
824
185
    break;
825
14
  case 4:
826
14
    appearBuf->appendf("{0:.2f} {1:.2f} {2:.2f} {3:.2f} K\n",
827
14
           color[0], color[1], color[2], color[3]);
828
14
    break;
829
200
  }
830
200
}
831
832
63
GBool Annot::setFillColor(Object *colorObj) {
833
63
  Object obj;
834
63
  double color[4];
835
63
  int i;
836
837
63
  if (!colorObj->isArray()) {
838
0
    return gFalse;
839
0
  }
840
257
  for (i = 0; i < colorObj->arrayGetLength() && i < 4; ++i) {
841
194
    if (colorObj->arrayGet(i, &obj)->isNum()) {
842
178
      color[i] = obj.getNum();
843
178
    } else {
844
16
      color[i] = 0;
845
16
    }
846
194
    obj.free();
847
194
  }
848
63
  switch (colorObj->arrayGetLength()) {
849
3
  case 1:
850
3
    appearBuf->appendf("{0:.2f} g\n", color[0]);
851
3
    return gTrue;
852
45
  case 3:
853
45
    appearBuf->appendf("{0:.2f} {1:.2f} {2:.2f} rg\n",
854
45
           color[0], color[1], color[2]);
855
45
    return gTrue;
856
13
  case 4:
857
13
    appearBuf->appendf("{0:.2f} {1:.2f} {2:.2f} {3:.3f} k\n",
858
13
           color[0], color[1],
859
13
           color[2], color[3]);
860
13
    return gTrue;
861
63
  }
862
2
  return gFalse;
863
63
}
864
865
88
AnnotLineEndType Annot::parseLineEndType(Object *obj) {
866
88
  if (obj->isName("None")) {
867
56
    return annotLineEndNone;
868
56
  } else if (obj->isName("Square")) {
869
0
    return annotLineEndSquare;
870
32
  } else if (obj->isName("Circle")) {
871
0
    return annotLineEndCircle;
872
32
  } else if (obj->isName("Diamond")) {
873
0
    return annotLineEndDiamond;
874
32
  } else if (obj->isName("OpenArrow")) {
875
0
    return annotLineEndOpenArrow;
876
32
  } else if (obj->isName("ClosedArrow")) {
877
22
    return annotLineEndClosedArrow;
878
22
  } else if (obj->isName("Butt")) {
879
0
    return annotLineEndButt;
880
10
  } else if (obj->isName("ROpenArrow")) {
881
0
    return annotLineEndROpenArrow;
882
10
  } else if (obj->isName("RClosedArrow")) {
883
0
    return annotLineEndRClosedArrow;
884
10
  } else if (obj->isName("Slash")) {
885
0
    return annotLineEndSlash;
886
10
  } else {
887
10
    return annotLineEndNone;
888
10
  }
889
88
}
890
891
void Annot::adjustLineEndpoint(AnnotLineEndType lineEnd,
892
             double x, double y, double dx, double dy,
893
126
             double w, double *tx, double *ty) {
894
126
  switch (lineEnd) {
895
104
  case annotLineEndNone:
896
104
    w = 0;
897
104
    break;
898
0
  case annotLineEndSquare:
899
0
    w *= lineEndSize1;
900
0
    break;
901
0
  case annotLineEndCircle:
902
0
    w *= lineEndSize1;
903
0
    break;
904
0
  case annotLineEndDiamond:
905
0
    w *= lineEndSize1;
906
0
    break;
907
0
  case annotLineEndOpenArrow:
908
0
    w = 0;
909
0
    break;
910
22
  case annotLineEndClosedArrow:
911
22
    w *= lineEndSize2 * cos(lineArrowAngle);
912
22
    break;
913
0
  case annotLineEndButt:
914
0
    w = 0;
915
0
    break;
916
0
  case annotLineEndROpenArrow:
917
0
    w *= lineEndSize2 * cos(lineArrowAngle);
918
0
    break;
919
0
  case annotLineEndRClosedArrow:
920
0
    w *= lineEndSize2 * cos(lineArrowAngle);
921
0
    break;
922
0
  case annotLineEndSlash:
923
0
    w = 0;
924
0
    break;
925
126
  }
926
126
  *tx = x + w * dx;
927
126
  *ty = y + w * dy;
928
126
}
929
930
void Annot::drawLineArrow(AnnotLineEndType lineEnd,
931
        double x, double y, double dx, double dy,
932
126
        double w, GBool fill) {
933
126
  switch (lineEnd) {
934
104
  case annotLineEndNone:
935
104
    break;
936
0
  case annotLineEndSquare:
937
0
    w *= lineEndSize1;
938
0
    appearBuf->appendf("{0:.4f} {1:.4f} m\n",
939
0
           x + w*dx + 0.5*w*dy,
940
0
           y + w*dy - 0.5*w*dx);
941
0
    appearBuf->appendf("{0:.4f} {1:.4f} l\n",
942
0
           x + 0.5*w*dy,
943
0
           y - 0.5*w*dx);
944
0
    appearBuf->appendf("{0:.4f} {1:.4f} l\n",
945
0
           x - 0.5*w*dy,
946
0
           y + 0.5*w*dx);
947
0
    appearBuf->appendf("{0:.4f} {1:.4f} l\n",
948
0
           x + w*dx - 0.5*w*dy,
949
0
           y + w*dy + 0.5*w*dx);
950
0
    appearBuf->append(fill ? "b\n" : "s\n");
951
0
    break;
952
0
  case annotLineEndCircle:
953
0
    w *= lineEndSize1;
954
0
    drawCircle(x + 0.5*w*dx, y + 0.5*w*dy, 0.5*w, fill ? "b" : "s");
955
0
    break;
956
0
  case annotLineEndDiamond:
957
0
    w *= lineEndSize1;
958
0
    appearBuf->appendf("{0:.4f} {1:.4f} m\n", x, y);
959
0
    appearBuf->appendf("{0:.4f} {1:.4f} l\n",
960
0
           x + 0.5*w*dx - 0.5*w*dy,
961
0
           y + 0.5*w*dy + 0.5*w*dx);
962
0
    appearBuf->appendf("{0:.4f} {1:.4f} l\n",
963
0
           x + w*dx,
964
0
           y + w*dy);
965
0
    appearBuf->appendf("{0:.4f} {1:.4f} l\n",
966
0
           x + 0.5*w*dx + 0.5*w*dy,
967
0
           y + 0.5*w*dy - 0.5*w*dx);
968
0
    appearBuf->append(fill ? "b\n" : "s\n");
969
0
    break;
970
0
  case annotLineEndOpenArrow:
971
0
    w *= lineEndSize2;
972
0
    appearBuf->appendf("{0:.4f} {1:.4f} m\n",
973
0
           x + w*cos(lineArrowAngle)*dx + w*sin(lineArrowAngle)*dy,
974
0
           y + w*cos(lineArrowAngle)*dy - w*sin(lineArrowAngle)*dx);
975
0
    appearBuf->appendf("{0:.4f} {1:.4f} l\n", x, y);
976
0
    appearBuf->appendf("{0:.4f} {1:.4f} l\n",
977
0
           x + w*cos(lineArrowAngle)*dx - w*sin(lineArrowAngle)*dy,
978
0
           y + w*cos(lineArrowAngle)*dy + w*sin(lineArrowAngle)*dx);
979
0
    appearBuf->append("S\n");
980
0
    break;
981
22
  case annotLineEndClosedArrow:
982
22
    w *= lineEndSize2;
983
22
    appearBuf->appendf("{0:.4f} {1:.4f} m\n",
984
22
           x + w*cos(lineArrowAngle)*dx + w*sin(lineArrowAngle)*dy,
985
22
           y + w*cos(lineArrowAngle)*dy - w*sin(lineArrowAngle)*dx);
986
22
    appearBuf->appendf("{0:.4f} {1:.4f} l\n", x, y);
987
22
    appearBuf->appendf("{0:.4f} {1:.4f} l\n",
988
22
           x + w*cos(lineArrowAngle)*dx - w*sin(lineArrowAngle)*dy,
989
22
           y + w*cos(lineArrowAngle)*dy + w*sin(lineArrowAngle)*dx);
990
22
    appearBuf->append(fill ? "b\n" : "s\n");
991
22
    break;
992
0
  case annotLineEndButt:
993
0
    w *= lineEndSize1;
994
0
    appearBuf->appendf("{0:.4f} {1:.4f} m\n",
995
0
           x + 0.5*w*dy,
996
0
           y - 0.5*w*dx);
997
0
    appearBuf->appendf("{0:.4f} {1:.4f} l\n",
998
0
           x - 0.5*w*dy,
999
0
           y + 0.5*w*dx);
1000
0
    appearBuf->append("S\n");
1001
0
    break;
1002
0
  case annotLineEndROpenArrow:
1003
0
    w *= lineEndSize2;
1004
0
    appearBuf->appendf("{0:.4f} {1:.4f} m\n",
1005
0
           x + w*sin(lineArrowAngle)*dy,
1006
0
           y - w*sin(lineArrowAngle)*dx);
1007
0
    appearBuf->appendf("{0:.4f} {1:.4f} l\n",
1008
0
           x + w*cos(lineArrowAngle)*dx,
1009
0
           y + w*cos(lineArrowAngle)*dy);
1010
0
    appearBuf->appendf("{0:.4f} {1:.4f} l\n",
1011
0
           x - w*sin(lineArrowAngle)*dy,
1012
0
           y + w*sin(lineArrowAngle)*dx);
1013
0
    appearBuf->append("S\n");
1014
0
    break;
1015
0
  case annotLineEndRClosedArrow:
1016
0
    w *= lineEndSize2;
1017
0
    appearBuf->appendf("{0:.4f} {1:.4f} m\n",
1018
0
           x + w*sin(lineArrowAngle)*dy,
1019
0
           y - w*sin(lineArrowAngle)*dx);
1020
0
    appearBuf->appendf("{0:.4f} {1:.4f} l\n",
1021
0
           x + w*cos(lineArrowAngle)*dx,
1022
0
           y + w*cos(lineArrowAngle)*dy);
1023
0
    appearBuf->appendf("{0:.4f} {1:.4f} l\n",
1024
0
           x - w*sin(lineArrowAngle)*dy,
1025
0
           y + w*sin(lineArrowAngle)*dx);
1026
0
    appearBuf->append(fill ? "b\n" : "s\n");
1027
0
    break;
1028
0
  case annotLineEndSlash:
1029
0
    w *= lineEndSize1;
1030
0
    appearBuf->appendf("{0:.4f} {1:.4f} m\n",
1031
0
           x + 0.5*w*cos(lineArrowAngle)*dy
1032
0
             - 0.5*w*sin(lineArrowAngle)*dx,
1033
0
           y - 0.5*w*cos(lineArrowAngle)*dx
1034
0
             - 0.5*w*sin(lineArrowAngle)*dy);
1035
0
    appearBuf->appendf("{0:.4f} {1:.4f} l\n",
1036
0
           x - 0.5*w*cos(lineArrowAngle)*dy
1037
0
             + 0.5*w*sin(lineArrowAngle)*dx,
1038
0
           y + 0.5*w*cos(lineArrowAngle)*dx
1039
0
             + 0.5*w*sin(lineArrowAngle)*dy);
1040
0
    appearBuf->append("S\n");
1041
0
    break;
1042
126
  }
1043
126
}
1044
1045
// Draw an (approximate) circle of radius <r> centered at (<cx>, <cy>).
1046
// <cmd> is used to draw the circle ("f", "s", or "b").
1047
0
void Annot::drawCircle(double cx, double cy, double r, const char *cmd) {
1048
0
  appearBuf->appendf("{0:.4f} {1:.4f} m\n",
1049
0
         cx + r, cy);
1050
0
  appearBuf->appendf("{0:.4f} {1:.4f} {2:.4f} {3:.4f} {4:.4f} {5:.4f} c\n",
1051
0
         cx + r, cy + bezierCircle * r,
1052
0
         cx + bezierCircle * r, cy + r,
1053
0
         cx, cy + r);
1054
0
  appearBuf->appendf("{0:.4f} {1:.4f} {2:.4f} {3:.4f} {4:.4f} {5:.4f} c\n",
1055
0
         cx - bezierCircle * r, cy + r,
1056
0
         cx - r, cy + bezierCircle * r,
1057
0
         cx - r, cy);
1058
0
  appearBuf->appendf("{0:.4f} {1:.4f} {2:.4f} {3:.4f} {4:.4f} {5:.4f} c\n",
1059
0
         cx - r, cy - bezierCircle * r,
1060
0
         cx - bezierCircle * r, cy - r,
1061
0
         cx, cy - r);
1062
0
  appearBuf->appendf("{0:.4f} {1:.4f} {2:.4f} {3:.4f} {4:.4f} {5:.4f} c\n",
1063
0
         cx + bezierCircle * r, cy - r,
1064
0
         cx + r, cy - bezierCircle * r,
1065
0
         cx + r, cy);
1066
0
  appearBuf->appendf("{0:s}\n", cmd);
1067
0
}
1068
1069
// Draw the top-left half of an (approximate) circle of radius <r>
1070
// centered at (<cx>, <cy>).
1071
0
void Annot::drawCircleTopLeft(double cx, double cy, double r) {
1072
0
  double r2;
1073
1074
0
  r2 = r / sqrt(2.0);
1075
0
  appearBuf->appendf("{0:.4f} {1:.4f} m\n",
1076
0
         cx + r2, cy + r2);
1077
0
  appearBuf->appendf("{0:.4f} {1:.4f} {2:.4f} {3:.4f} {4:.4f} {5:.4f} c\n",
1078
0
         cx + (1 - bezierCircle) * r2,
1079
0
         cy + (1 + bezierCircle) * r2,
1080
0
         cx - (1 - bezierCircle) * r2,
1081
0
         cy + (1 + bezierCircle) * r2,
1082
0
         cx - r2,
1083
0
         cy + r2);
1084
0
  appearBuf->appendf("{0:.4f} {1:.4f} {2:.4f} {3:.4f} {4:.4f} {5:.4f} c\n",
1085
0
         cx - (1 + bezierCircle) * r2,
1086
0
         cy + (1 - bezierCircle) * r2,
1087
0
         cx - (1 + bezierCircle) * r2,
1088
0
         cy - (1 - bezierCircle) * r2,
1089
0
         cx - r2,
1090
0
         cy - r2);
1091
0
  appearBuf->append("S\n");
1092
0
}
1093
1094
// Draw the bottom-right half of an (approximate) circle of radius <r>
1095
// centered at (<cx>, <cy>).
1096
0
void Annot::drawCircleBottomRight(double cx, double cy, double r) {
1097
0
  double r2;
1098
1099
0
  r2 = r / sqrt(2.0);
1100
0
  appearBuf->appendf("{0:.4f} {1:.4f} m\n",
1101
0
         cx - r2, cy - r2);
1102
0
  appearBuf->appendf("{0:.4f} {1:.4f} {2:.4f} {3:.4f} {4:.4f} {5:.4f} c\n",
1103
0
         cx - (1 - bezierCircle) * r2,
1104
0
         cy - (1 + bezierCircle) * r2,
1105
0
         cx + (1 - bezierCircle) * r2,
1106
0
         cy - (1 + bezierCircle) * r2,
1107
0
         cx + r2,
1108
0
         cy - r2);
1109
0
  appearBuf->appendf("{0:.4f} {1:.4f} {2:.4f} {3:.4f} {4:.4f} {5:.4f} c\n",
1110
0
         cx + (1 + bezierCircle) * r2,
1111
0
         cy - (1 - bezierCircle) * r2,
1112
0
         cx + (1 + bezierCircle) * r2,
1113
0
         cy + (1 - bezierCircle) * r2,
1114
0
         cx + r2,
1115
0
         cy + r2);
1116
0
  appearBuf->append("S\n");
1117
0
}
1118
1119
void Annot::drawText(GString *text, GString *da, int quadding, double margin,
1120
89
         int rot) {
1121
89
  GString *text2, *tok;
1122
89
  GList *daToks;
1123
89
  const char *charName;
1124
89
  double dx, dy, fontSize, fontSize2, x, y, w;
1125
89
  Gushort charWidth;
1126
89
  int tfPos, tmPos, i, j, c;
1127
1128
  // check for a Unicode string
1129
  //~ this currently drops all non-Latin1 characters
1130
89
  if (text->getLength() >= 2 &&
1131
80
      text->getChar(0) == '\xfe' && text->getChar(1) == '\xff') {
1132
9
    text2 = new GString();
1133
4.86k
    for (i = 2; i+1 < text->getLength(); i += 2) {
1134
4.85k
      c = ((text->getChar(i) & 0xff) << 8) + (text->getChar(i+1) & 0xff);
1135
4.85k
      if (c <= 0xff) {
1136
286
  text2->append((char)c);
1137
4.57k
      } else {
1138
4.57k
  text2->append('?');
1139
4.57k
      }
1140
4.85k
    }
1141
80
  } else {
1142
80
    text2 = text;
1143
80
  }
1144
1145
  // parse the default appearance string
1146
89
  tfPos = tmPos = -1;
1147
89
  if (da) {
1148
89
    daToks = new GList();
1149
89
    i = 0;
1150
526
    while (i < da->getLength()) {
1151
1.11k
      while (i < da->getLength() && Lexer::isSpace(da->getChar(i))) {
1152
674
  ++i;
1153
674
      }
1154
437
      if (i < da->getLength()) {
1155
432
  for (j = i + 1;
1156
4.47k
       j < da->getLength() && !Lexer::isSpace(da->getChar(j));
1157
4.03k
       ++j) ;
1158
432
  daToks->append(new GString(da, i, j - i));
1159
432
  i = j;
1160
432
      }
1161
437
    }
1162
462
    for (i = 2; i < daToks->getLength(); ++i) {
1163
373
      if (i >= 2 && !((GString *)daToks->get(i))->cmp("Tf")) {
1164
17
  tfPos = i - 2;
1165
356
      } else if (i >= 6 && !((GString *)daToks->get(i))->cmp("Tm")) {
1166
0
  tmPos = i - 6;
1167
0
      }
1168
373
    }
1169
89
  } else {
1170
0
    daToks = NULL;
1171
0
  }
1172
1173
  // get the font and font size
1174
89
  fontSize = 0;
1175
89
  if (tfPos >= 0) {
1176
    //~ where do we look up the font?
1177
17
    tok = (GString *)daToks->get(tfPos);
1178
17
    tok->clear();
1179
17
    tok->append("/xpdf_default_font");
1180
17
    tok = (GString *)daToks->get(tfPos + 1);
1181
17
    fontSize = atof(tok->getCString());
1182
72
  } else {
1183
72
    error(errSyntaxError, -1,
1184
72
    "Missing 'Tf' operator in annotation's DA string");
1185
72
    daToks->append(new GString("/xpdf_default_font"));
1186
72
    daToks->append(new GString("10"));
1187
72
    daToks->append(new GString("Tf"));
1188
72
  }
1189
1190
  // setup
1191
89
  appearBuf->append("q\n");
1192
89
  if (rot == 90) {
1193
0
    appearBuf->appendf("0 1 -1 0 {0:.4f} 0 cm\n", xMax - xMin);
1194
0
    dx = yMax - yMin;
1195
0
    dy = xMax - xMin;
1196
89
  } else if (rot == 180) {
1197
0
    appearBuf->appendf("-1 0 0 -1 {0:.4f} {1:.4f} cm\n",
1198
0
           xMax - xMin, yMax - yMin);
1199
0
    dx = xMax - yMax;
1200
0
    dy = yMax - yMin;
1201
89
  } else if (rot == 270) {
1202
0
    appearBuf->appendf("0 -1 1 0 0 {0:.4f} cm\n", yMax - yMin);
1203
0
    dx = yMax - yMin;
1204
0
    dy = xMax - xMin;
1205
89
  } else { // assume rot == 0
1206
89
    dx = xMax - xMin;
1207
89
    dy = yMax - yMin;
1208
89
  }
1209
89
  appearBuf->append("BT\n");
1210
1211
  // compute string width
1212
  //~ this assumes we're substituting Helvetica/WinAnsiEncoding for everything
1213
89
  w = 0;
1214
41.2k
  for (i = 0; i < text2->getLength(); ++i) {
1215
41.1k
    charName = winAnsiEncoding[text->getChar(i) & 0xff];
1216
41.1k
    if (charName && builtinFonts[4].widths->getWidth(charName, &charWidth)) {
1217
33.5k
      w += charWidth;
1218
33.5k
    } else {
1219
7.59k
      w += 0.5;
1220
7.59k
    }
1221
41.1k
  }
1222
1223
  // compute font autosize
1224
89
  if (fontSize == 0) {
1225
73
    fontSize = dy - 2 * margin;
1226
73
    fontSize2 = (dx - 2 * margin) / w;
1227
73
    if (fontSize2 < fontSize) {
1228
67
      fontSize = fontSize2;
1229
67
    }
1230
73
    fontSize = floor(fontSize);
1231
73
    if (tfPos >= 0) {
1232
1
      tok = (GString *)daToks->get(tfPos + 1);
1233
1
      tok->clear();
1234
1
      tok->appendf("{0:.4f}", fontSize);
1235
1
    }
1236
73
  }
1237
1238
  // compute text start position
1239
89
  w *= fontSize;
1240
89
  switch (quadding) {
1241
87
  case 0:
1242
89
  default:
1243
89
    x = margin + 2;
1244
89
    break;
1245
0
  case 1:
1246
0
    x = (dx - w) / 2;
1247
0
    break;
1248
0
  case 2:
1249
0
    x = dx - margin - 2 - w;
1250
0
    break;
1251
89
  }
1252
89
  y = 0.5 * dy - 0.4 * fontSize;
1253
1254
  // set the font matrix
1255
89
  if (tmPos >= 0) {
1256
0
    tok = (GString *)daToks->get(tmPos + 4);
1257
0
    tok->clear();
1258
0
    tok->appendf("{0:.4f}", x);
1259
0
    tok = (GString *)daToks->get(tmPos + 5);
1260
0
    tok->clear();
1261
0
    tok->appendf("{0:.4f}", y);
1262
0
  }
1263
  
1264
  // write the DA string
1265
89
  if (daToks) {
1266
737
    for (i = 0; i < daToks->getLength(); ++i) {
1267
648
      appearBuf->append((GString *)daToks->get(i))->append(' ');
1268
648
    }
1269
89
  }
1270
1271
  // write the font matrix (if not part of the DA string)
1272
89
  if (tmPos < 0) {
1273
89
    appearBuf->appendf("1 0 0 1 {0:.4f} {1:.4f} Tm\n", x, y);
1274
89
  }
1275
1276
  // write the text string
1277
89
  appearBuf->append('(');
1278
41.2k
  for (i = 0; i < text2->getLength(); ++i) {
1279
41.1k
    c = text2->getChar(i) & 0xff;
1280
41.1k
    if (c == '(' || c == ')' || c == '\\') {
1281
309
      appearBuf->append('\\');
1282
309
      appearBuf->append((char)c);
1283
40.8k
    } else if (c < 0x20 || c >= 0x80) {
1284
15.8k
      appearBuf->appendf("\\{0:03o}", c);
1285
24.9k
    } else {
1286
24.9k
      appearBuf->append((char)c);
1287
24.9k
    }
1288
41.1k
  }
1289
89
  appearBuf->append(") Tj\n");
1290
1291
  // cleanup
1292
89
  appearBuf->append("ET\n");
1293
89
  appearBuf->append("Q\n");
1294
1295
89
  if (daToks) {
1296
89
    deleteGList(daToks, GString);
1297
89
  }
1298
89
  if (text2 != text) {
1299
9
    delete text2;
1300
9
  }
1301
89
}
1302
1303
10.4k
void Annot::draw(Gfx *gfx, GBool printing) {
1304
10.4k
  GBool oc, isLink;
1305
1306
  // check the flags
1307
10.4k
  if ((flags & annotFlagHidden) ||
1308
10.2k
      (printing && !(flags & annotFlagPrint)) ||
1309
9.90k
      (!printing && (flags & annotFlagNoView))) {
1310
511
    return;
1311
511
  }
1312
1313
  // check the optional content entry
1314
9.89k
  if (doc->getOptionalContent()->evalOCObject(&ocObj, &oc) && !oc) {
1315
0
    return;
1316
0
  }
1317
1318
  // draw the appearance stream
1319
9.89k
  isLink = type && !type->cmp("Link");
1320
9.89k
  gfx->drawAnnot(&appearance, isLink ? borderStyle : (AnnotBorderStyle *)NULL,
1321
9.89k
     xMin, yMin, xMax, yMax);
1322
9.89k
}
1323
1324
335
Object *Annot::getObject(Object *obj) {
1325
335
  if (ref.num >= 0) {
1326
289
    xref->fetch(ref.num, ref.gen, obj);
1327
289
  } else {
1328
46
    obj->initNull();
1329
46
  }
1330
335
  return obj;
1331
335
}
1332
1333
//------------------------------------------------------------------------
1334
// PageAnnots
1335
//------------------------------------------------------------------------
1336
1337
class PageAnnots {
1338
public:
1339
1340
  PageAnnots();
1341
  ~PageAnnots();
1342
1343
  GList *annots;    // list of annots on the page [Annot]
1344
  GBool appearancesGenerated; // set after appearances have been generated
1345
};
1346
1347
232k
PageAnnots::PageAnnots() {
1348
232k
  annots = new GList();
1349
232k
  appearancesGenerated = gFalse;
1350
232k
}
1351
1352
232k
PageAnnots::~PageAnnots() {
1353
232k
  deleteGList(annots, Annot);
1354
232k
}
1355
1356
//------------------------------------------------------------------------
1357
// Annots
1358
//------------------------------------------------------------------------
1359
1360
10.2k
Annots::Annots(PDFDoc *docA) {
1361
10.2k
  doc = docA;
1362
10.2k
  pageAnnots = (PageAnnots **)gmallocn(doc->getNumPages(), sizeof(PageAnnots*));
1363
242k
  for (int page = 1; page <= doc->getNumPages(); ++page) {
1364
232k
    pageAnnots[page - 1] = NULL;
1365
232k
  }
1366
10.2k
  formFieldRefsSize = 0;
1367
10.2k
  formFieldRefs = NULL;
1368
10.2k
#if MULTITHREADED
1369
10.2k
  gInitMutex(&mutex);
1370
10.2k
#endif
1371
10.2k
}
1372
1373
10.2k
Annots::~Annots() {
1374
242k
  for (int page = 1; page <= doc->getNumPages(); ++page) {
1375
232k
    delete pageAnnots[page - 1];
1376
232k
  }
1377
10.2k
  gfree(pageAnnots);
1378
10.2k
  gfree(formFieldRefs);
1379
10.2k
#if MULTITHREADED
1380
10.2k
  gDestroyMutex(&mutex);
1381
10.2k
#endif
1382
10.2k
}
1383
1384
474k
void Annots::loadAnnots(int page) {
1385
474k
#if MULTITHREADED
1386
474k
  gLockMutex(&mutex);
1387
474k
#endif
1388
474k
  if (pageAnnots[page - 1]) {
1389
242k
#if MULTITHREADED
1390
242k
    gUnlockMutex(&mutex);
1391
242k
#endif
1392
242k
    return;
1393
242k
  }
1394
1395
232k
  pageAnnots[page - 1] = new PageAnnots();
1396
1397
232k
  Object annotsObj;
1398
232k
  doc->getCatalog()->getPage(page)->getAnnots(&annotsObj);
1399
232k
  if (!annotsObj.isArray()) {
1400
222k
    annotsObj.free();
1401
222k
#if MULTITHREADED
1402
222k
    gUnlockMutex(&mutex);
1403
222k
#endif
1404
222k
    return;
1405
222k
  }
1406
1407
9.75k
  loadFormFieldRefs();
1408
1409
458k
  for (int i = 0; i < annotsObj.arrayGetLength(); ++i) {
1410
449k
    Object annotObj;
1411
449k
    Ref annotRef;
1412
449k
    if (annotsObj.arrayGetNF(i, &annotObj)->isRef()) {
1413
88.6k
      annotRef = annotObj.getRef();
1414
88.6k
      annotObj.free();
1415
88.6k
      annotsObj.arrayGet(i, &annotObj);
1416
360k
    } else {
1417
360k
      annotRef.num = annotRef.gen = -1;
1418
360k
    }
1419
449k
    if (!annotObj.isDict()) {
1420
413k
      annotObj.free();
1421
413k
      continue;
1422
413k
    }
1423
1424
    // skip any annotations which are used as AcroForm fields --
1425
    // they'll be rendered by the AcroForm module
1426
35.2k
    if (annotRef.num >= 0 && annotRef.num < formFieldRefsSize &&
1427
6.51k
  formFieldRefs[annotRef.num]) {
1428
3.80k
      annotObj.free();
1429
3.80k
      continue;
1430
3.80k
    }
1431
1432
31.4k
    Annot *annot = new Annot(doc, annotObj.getDict(), &annotRef);
1433
31.4k
    annotObj.free();
1434
31.4k
    if (annot->isOk()) {
1435
10.4k
      pageAnnots[page - 1]->annots->append(annot);
1436
21.0k
    } else {
1437
21.0k
      delete annot;
1438
21.0k
    }
1439
31.4k
  }
1440
1441
9.75k
  annotsObj.free();
1442
1443
9.75k
#if MULTITHREADED
1444
9.75k
  gUnlockMutex(&mutex);
1445
9.75k
#endif
1446
9.75k
}
1447
1448
// Build a set of object refs for AcroForm fields.
1449
9.75k
void Annots::loadFormFieldRefs() {
1450
9.75k
  if (formFieldRefs) {
1451
1.46k
    return;
1452
1.46k
  }
1453
1454
8.28k
  AcroForm *form = doc->getCatalog()->getForm();
1455
8.28k
  if (!form) {
1456
7.73k
    return;
1457
7.73k
  }
1458
1459
546
  int newFormFieldRefsSize = 256;
1460
3.54k
  for (int i = 0; i < form->getNumFields(); ++i) {
1461
3.00k
    AcroFormField *field = form->getField(i);
1462
3.00k
    Object fieldRef;
1463
3.00k
    field->getFieldRef(&fieldRef);
1464
3.00k
    if (fieldRef.isRef()) {
1465
2.95k
      if (fieldRef.getRefNum() >= formFieldRefsSize) {
1466
568
  while (fieldRef.getRefNum() >= newFormFieldRefsSize &&
1467
70
         newFormFieldRefsSize <= INT_MAX / 2) {
1468
70
    newFormFieldRefsSize *= 2;
1469
70
  }
1470
498
  if (fieldRef.getRefNum() >= newFormFieldRefsSize) {
1471
0
    continue;
1472
0
  }
1473
498
  formFieldRefs = (char *)grealloc(formFieldRefs, newFormFieldRefsSize);
1474
168k
  for (int j = formFieldRefsSize; j < newFormFieldRefsSize; ++j) {
1475
167k
    formFieldRefs[j] = (char)0;
1476
167k
  }
1477
498
  formFieldRefsSize = newFormFieldRefsSize;
1478
498
      }
1479
2.95k
      formFieldRefs[fieldRef.getRefNum()] = (char)1;
1480
2.95k
    }
1481
3.00k
    fieldRef.free();
1482
3.00k
  }
1483
546
}
1484
1485
232k
int Annots::getNumAnnots(int page) {
1486
232k
  loadAnnots(page);
1487
232k
  return pageAnnots[page - 1]->annots->getLength();
1488
232k
}
1489
1490
10.4k
Annot *Annots::getAnnot(int page, int idx) {
1491
10.4k
  loadAnnots(page);
1492
10.4k
  return (Annot *)pageAnnots[page - 1]->annots->get(idx);
1493
10.4k
}
1494
1495
0
Annot *Annots::find(int page, double x, double y) {
1496
0
  loadAnnots(page);
1497
0
  PageAnnots *pa = pageAnnots[page - 1];
1498
0
  for (int i = pa->annots->getLength() - 1; i >= 0; --i) {
1499
0
    Annot *annot = (Annot *)pa->annots->get(i);
1500
0
    if (annot->inRect(x, y)) {
1501
0
      return annot;
1502
0
    }
1503
0
  }
1504
0
  return NULL;
1505
0
}
1506
1507
0
int Annots::findIdx(int page, double x, double y) {
1508
0
  loadAnnots(page);
1509
0
  PageAnnots *pa = pageAnnots[page - 1];
1510
0
  for (int i = pa->annots->getLength() - 1; i >= 0; --i) {
1511
0
    Annot *annot = (Annot *)pa->annots->get(i);
1512
0
    if (annot->inRect(x, y)) {
1513
0
      return i;
1514
0
    }
1515
0
  }
1516
0
  return -1;
1517
0
}
1518
1519
0
void Annots::add(int page, Object *annotObj) {
1520
0
  if (!annotObj->isDict()) {
1521
0
    return;
1522
0
  }
1523
0
  Ref annotRef = {-1, -1};
1524
0
  Annot *annot = new Annot(doc, annotObj->getDict(), &annotRef);
1525
0
  if (annot->isOk()) {
1526
0
    annot->generateAnnotAppearance(annotObj);
1527
0
    pageAnnots[page - 1]->annots->append(annot);
1528
0
  } else {
1529
0
    delete annot;
1530
0
  }
1531
0
}
1532
1533
232k
void Annots::generateAnnotAppearances(int page) {
1534
232k
  loadAnnots(page);
1535
232k
  PageAnnots *pa = pageAnnots[page - 1];
1536
232k
#if MULTITHREADED
1537
232k
  gLockMutex(&mutex);
1538
232k
#endif
1539
232k
  if (!pa->appearancesGenerated) {
1540
242k
    for (int i = 0; i < pa->annots->getLength(); ++i) {
1541
10.4k
      Annot *annot = (Annot *)pa->annots->get(i);
1542
10.4k
      annot->generateAnnotAppearance(NULL);
1543
10.4k
    }
1544
232k
    pa->appearancesGenerated = gTrue;
1545
232k
  }
1546
232k
#if MULTITHREADED
1547
232k
  gUnlockMutex(&mutex);
1548
232k
#endif
1549
232k
}