Coverage Report

Created: 2026-02-26 07:15

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.0k
#define annotFlagHidden    0x0002
38
1.79k
#define annotFlagPrint     0x0004
39
8.07k
#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
82
#define lineEndSize2   10
47
369
#define lineArrowAngle (M_PI / 6)
48
49
//------------------------------------------------------------------------
50
// AnnotBorderStyle
51
//------------------------------------------------------------------------
52
53
AnnotBorderStyle::AnnotBorderStyle(AnnotBorderType typeA, double widthA,
54
           double *dashA, int dashLengthA,
55
30.9k
           double *colorA, int nColorCompsA) {
56
30.9k
  type = typeA;
57
30.9k
  width = widthA;
58
30.9k
  dash = dashA;
59
30.9k
  dashLength = dashLengthA;
60
30.9k
  color[0] = colorA[0];
61
30.9k
  color[1] = colorA[1];
62
30.9k
  color[2] = colorA[2];
63
30.9k
  color[3] = colorA[3];
64
30.9k
  nColorComps = nColorCompsA;
65
30.9k
}
66
67
30.9k
AnnotBorderStyle::~AnnotBorderStyle() {
68
30.9k
  if (dash) {
69
79
    gfree(dash);
70
79
  }
71
30.9k
}
72
73
//------------------------------------------------------------------------
74
// Annot
75
//------------------------------------------------------------------------
76
77
30.9k
Annot::Annot(PDFDoc *docA, Dict *dict, Ref *refA) {
78
30.9k
  Object apObj, asObj, obj1, obj2, obj3;
79
30.9k
  AnnotBorderType borderType;
80
30.9k
  double borderWidth;
81
30.9k
  double *borderDash;
82
30.9k
  int borderDashLength;
83
30.9k
  double borderColor[4];
84
30.9k
  int nBorderColorComps;
85
30.9k
  double t;
86
30.9k
  int i;
87
88
30.9k
  ok = gTrue;
89
30.9k
  doc = docA;
90
30.9k
  xref = doc->getXRef();
91
30.9k
  ref = *refA;
92
30.9k
  type = NULL;
93
30.9k
  appearanceState = NULL;
94
30.9k
  appearBuf = NULL;
95
30.9k
  borderStyle = NULL;
96
97
  //----- parse the type
98
99
30.9k
  if (dict->lookup("Subtype", &obj1)->isName()) {
100
12.0k
    type = new GString(obj1.getName());
101
12.0k
  }
102
30.9k
  obj1.free();
103
104
  //----- parse the rectangle
105
106
30.9k
  if (dict->lookup("Rect", &obj1)->isArray() &&
107
11.1k
      obj1.arrayGetLength() == 4) {
108
10.0k
    xMin = yMin = xMax = yMax = 0;
109
10.0k
    if (obj1.arrayGet(0, &obj2)->isNum()) {
110
9.31k
      xMin = obj2.getNum();
111
9.31k
    }
112
10.0k
    obj2.free();
113
10.0k
    if (obj1.arrayGet(1, &obj2)->isNum()) {
114
9.68k
      yMin = obj2.getNum();
115
9.68k
    }
116
10.0k
    obj2.free();
117
10.0k
    if (obj1.arrayGet(2, &obj2)->isNum()) {
118
6.55k
      xMax = obj2.getNum();
119
6.55k
    }
120
10.0k
    obj2.free();
121
10.0k
    if (obj1.arrayGet(3, &obj2)->isNum()) {
122
9.63k
      yMax = obj2.getNum();
123
9.63k
    }
124
10.0k
    obj2.free();
125
10.0k
    if (xMin > xMax) {
126
3.78k
      t = xMin; xMin = xMax; xMax = t;
127
3.78k
    }
128
10.0k
    if (yMin > yMax) {
129
388
      t = yMin; yMin = yMax; yMax = t;
130
388
    }
131
20.8k
  } else {
132
20.8k
    error(errSyntaxError, -1, "Bad bounding box for annotation");
133
20.8k
    ok = gFalse;
134
20.8k
  }
135
30.9k
  obj1.free();
136
137
  //----- parse the flags
138
139
30.9k
  if (dict->lookup("F", &obj1)->isInt()) {
140
6.57k
    flags = obj1.getInt();
141
24.3k
  } else {
142
24.3k
    flags = 0;
143
24.3k
  }
144
30.9k
  obj1.free();
145
146
  //----- parse the border style
147
148
30.9k
  borderType = annotBorderSolid;
149
30.9k
  borderWidth = 1;
150
30.9k
  borderDash = NULL;
151
30.9k
  borderDashLength = 0;
152
30.9k
  nBorderColorComps = 3;
153
30.9k
  borderColor[0] = 0;
154
30.9k
  borderColor[1] = 0;
155
30.9k
  borderColor[2] = 1;
156
30.9k
  borderColor[3] = 0;
157
30.9k
  if (dict->lookup("BS", &obj1)->isDict()) {
158
1.14k
    if (obj1.dictLookup("S", &obj2)->isName()) {
159
920
      if (obj2.isName("S")) {
160
673
  borderType = annotBorderSolid;
161
673
      } else if (obj2.isName("D")) {
162
24
  borderType = annotBorderDashed;
163
223
      } else if (obj2.isName("B")) {
164
20
  borderType = annotBorderBeveled;
165
203
      } else if (obj2.isName("I")) {
166
57
  borderType = annotBorderInset;
167
146
      } else if (obj2.isName("U")) {
168
16
  borderType = annotBorderUnderlined;
169
16
      }
170
920
    }
171
1.14k
    obj2.free();
172
1.14k
    if (obj1.dictLookup("W", &obj2)->isNum()) {
173
1.01k
      borderWidth = obj2.getNum();
174
1.01k
    }
175
1.14k
    obj2.free();
176
1.14k
    if (obj1.dictLookup("D", &obj2)->isArray()) {
177
35
      borderDashLength = obj2.arrayGetLength();
178
35
      borderDash = (double *)gmallocn(borderDashLength, sizeof(double));
179
1.04k
      for (i = 0; i < borderDashLength; ++i) {
180
1.00k
  if (obj2.arrayGet(i, &obj3)->isNum()) {
181
412
    borderDash[i] = obj3.getNum();
182
593
  } else {
183
593
    borderDash[i] = 1;
184
593
  }
185
1.00k
  obj3.free();
186
1.00k
      }
187
35
    }
188
1.14k
    obj2.free();
189
29.8k
  } else {
190
29.8k
    obj1.free();
191
29.8k
    if (dict->lookup("Border", &obj1)->isArray()) {
192
1.76k
      if (obj1.arrayGetLength() >= 3) {
193
1.45k
  if (obj1.arrayGet(2, &obj2)->isNum()) {
194
1.30k
    borderWidth = obj2.getNum();
195
1.30k
  }
196
1.45k
  obj2.free();
197
1.45k
  if (obj1.arrayGetLength() >= 4) {
198
443
    if (obj1.arrayGet(3, &obj2)->isArray()) {
199
45
      borderType = annotBorderDashed;
200
45
      borderDashLength = obj2.arrayGetLength();
201
45
      borderDash = (double *)gmallocn(borderDashLength, sizeof(double));
202
1.32k
      for (i = 0; i < borderDashLength; ++i) {
203
1.27k
        if (obj2.arrayGet(i, &obj3)->isNum()) {
204
329
    borderDash[i] = obj3.getNum();
205
949
        } else {
206
949
    borderDash[i] = 1;
207
949
        }
208
1.27k
        obj3.free();
209
1.27k
      }
210
398
    } else {
211
      // Adobe draws no border at all if the last element is of
212
      // the wrong type.
213
398
      borderWidth = 0;
214
398
    }
215
443
    obj2.free();
216
443
  }
217
1.45k
      } else {
218
  // an empty Border array also means "no border"
219
307
  borderWidth = 0;
220
307
      }
221
1.76k
    }
222
29.8k
  }
223
30.9k
  obj1.free();
224
  // Acrobat ignores borders with unreasonable widths
225
30.9k
  if (borderWidth > 1 && (borderWidth > xMax - xMin ||
226
518
        borderWidth > yMax - yMin)) {
227
83
    borderWidth = 0;
228
83
  }
229
30.9k
  if (dict->lookup("C", &obj1)->isArray() &&
230
2.54k
      (obj1.arrayGetLength() == 1 ||
231
2.49k
       obj1.arrayGetLength() == 3 ||
232
1.86k
       obj1.arrayGetLength() == 4)) {
233
1.86k
    nBorderColorComps = obj1.arrayGetLength();
234
7.43k
    for (i = 0; i < nBorderColorComps; ++i) {
235
5.56k
      if (obj1.arrayGet(i, &obj2)->isNum()) {
236
5.12k
  borderColor[i] = obj2.getNum();
237
5.12k
      } else {
238
441
  borderColor[i] = 0;
239
441
      }
240
5.56k
      obj2.free();
241
5.56k
    }
242
1.86k
  }
243
30.9k
  obj1.free();
244
30.9k
  borderStyle = new AnnotBorderStyle(borderType, borderWidth,
245
30.9k
             borderDash, borderDashLength,
246
30.9k
             borderColor, nBorderColorComps);
247
248
  //----- get the appearance state
249
250
30.9k
  dict->lookup("AP", &apObj);
251
30.9k
  dict->lookup("AS", &asObj);
252
30.9k
  if (asObj.isName()) {
253
280
    appearanceState = new GString(asObj.getName());
254
30.6k
  } else if (apObj.isDict()) {
255
1.28k
    apObj.dictLookup("N", &obj1);
256
1.28k
    if (obj1.isDict() && obj1.dictGetLength() == 1) {
257
25
      appearanceState = new GString(obj1.dictGetKey(0));
258
25
    }
259
1.28k
    obj1.free();
260
1.28k
  }
261
30.9k
  if (!appearanceState) {
262
30.6k
    appearanceState = new GString("Off");
263
30.6k
  }
264
30.9k
  asObj.free();
265
266
  //----- get the annotation appearance
267
268
30.9k
  if (apObj.isDict()) {
269
1.56k
    apObj.dictLookup("N", &obj1);
270
1.56k
    apObj.dictLookupNF("N", &obj2);
271
1.56k
    if (obj1.isDict()) {
272
459
      if (obj1.dictLookupNF(appearanceState->getCString(), &obj3)->isRef()) {
273
214
  obj3.copy(&appearance);
274
214
      }
275
459
      obj3.free();
276
1.11k
    } else if (obj2.isRef()) {
277
948
      obj2.copy(&appearance);
278
948
    } else if (obj2.isStream()) {
279
0
      obj2.copy(&appearance);
280
0
    }
281
1.56k
    obj1.free();
282
1.56k
    obj2.free();
283
1.56k
  }
284
30.9k
  apObj.free();
285
286
  //----- get the optional content entry
287
288
30.9k
  dict->lookupNF("OC", &ocObj);
289
30.9k
}
290
291
30.9k
Annot::~Annot() {
292
30.9k
  if (type) {
293
12.0k
    delete type;
294
12.0k
  }
295
30.9k
  if (appearanceState) {
296
30.9k
    delete appearanceState;
297
30.9k
  }
298
30.9k
  appearance.free();
299
30.9k
  if (appearBuf) {
300
592
    delete appearBuf;
301
592
  }
302
30.9k
  if (borderStyle) {
303
30.9k
    delete borderStyle;
304
30.9k
  }
305
30.9k
  ocObj.free();
306
30.9k
}
307
308
10.0k
void Annot::generateAnnotAppearance(Object *annotObj) {
309
10.0k
  Object obj;
310
10.0k
  appearance.fetch(doc->getXRef(), &obj);
311
10.0k
  GBool alreadyHaveAppearance = obj.isStream();
312
10.0k
  obj.free();
313
10.0k
  if (alreadyHaveAppearance) {
314
454
    return;
315
454
  }
316
317
9.60k
  if (!type || (type->cmp("Line") &&
318
9.06k
    type->cmp("PolyLine") &&
319
8.93k
    type->cmp("Polygon") &&
320
8.93k
    type->cmp("FreeText"))) {
321
8.83k
    return;
322
8.83k
  }
323
324
771
  Object annotObj2;
325
771
  if (!annotObj) {
326
771
    getObject(&annotObj2);
327
771
    annotObj = &annotObj2;
328
771
  }
329
771
  if (!annotObj->isDict()) {
330
179
    annotObj2.free();
331
179
    return;
332
179
  }
333
334
592
  if (!type->cmp("Line")) {
335
181
    generateLineAppearance(annotObj);
336
411
  } else if (!type->cmp("PolyLine")) {
337
138
    generatePolyLineAppearance(annotObj);
338
273
  } else if (!type->cmp("Polygon")) {
339
0
    generatePolygonAppearance(annotObj);
340
273
  } else if (!type->cmp("FreeText")) {
341
273
    generateFreeTextAppearance(annotObj);
342
273
  }
343
344
592
  annotObj2.free();
345
592
}
346
347
//~ this doesn't draw the caption
348
181
void Annot::generateLineAppearance(Object *annotObj) {
349
181
  Object gfxStateDict, appearDict, obj1, obj2;
350
181
  MemStream *appearStream;
351
181
  double x1, y1, x2, y2, dx, dy, len, w;
352
181
  double lx1, ly1, lx2, ly2;
353
181
  double tx1, ty1, tx2, ty2;
354
181
  double ax1, ay1, ax2, ay2;
355
181
  double bx1, by1, bx2, by2;
356
181
  double leaderLen, leaderExtLen, leaderOffLen;
357
181
  AnnotLineEndType lineEnd1, lineEnd2;
358
181
  GBool fill;
359
360
181
  appearBuf = new GString();
361
362
  //----- check for transparency
363
181
  if (annotObj->dictLookup("CA", &obj1)->isNum()) {
364
56
    gfxStateDict.initDict(doc->getXRef());
365
56
    gfxStateDict.dictAdd(copyString("ca"), obj1.copy(&obj2));
366
56
    appearBuf->append("/GS1 gs\n");
367
56
  }
368
181
  obj1.free();
369
370
  //----- set line style, colors
371
181
  setLineStyle(borderStyle, &w);
372
181
  setStrokeColor(borderStyle->getColor(), borderStyle->getNumColorComps());
373
181
  fill = gFalse;
374
181
  if (annotObj->dictLookup("IC", &obj1)->isArray()) {
375
136
    if (setFillColor(&obj1)) {
376
120
      fill = gTrue;
377
120
    }
378
136
  }
379
181
  obj1.free();
380
381
  //----- get line properties
382
181
  if (annotObj->dictLookup("L", &obj1)->isArray() &&
383
161
      obj1.arrayGetLength() == 4) {
384
148
    if (obj1.arrayGet(0, &obj2)->isNum()) {
385
131
      x1 = obj2.getNum();
386
131
    } else {
387
17
      obj2.free();
388
17
      obj1.free();
389
17
      return;
390
17
    }
391
131
    obj2.free();
392
131
    if (obj1.arrayGet(1, &obj2)->isNum()) {
393
119
      y1 = obj2.getNum();
394
119
    } else {
395
12
      obj2.free();
396
12
      obj1.free();
397
12
      return;
398
12
    }
399
119
    obj2.free();
400
119
    if (obj1.arrayGet(2, &obj2)->isNum()) {
401
115
      x2 = obj2.getNum();
402
115
    } else {
403
4
      obj2.free();
404
4
      obj1.free();
405
4
      return;
406
4
    }
407
115
    obj2.free();
408
115
    if (obj1.arrayGet(3, &obj2)->isNum()) {
409
109
      y2 = obj2.getNum();
410
109
    } else {
411
6
      obj2.free();
412
6
      obj1.free();
413
6
      return;
414
6
    }
415
109
    obj2.free();
416
109
  } else {
417
33
    obj1.free();
418
33
    return;
419
33
  }
420
109
  obj1.free();
421
109
  lineEnd1 = lineEnd2 = annotLineEndNone;
422
109
  if (annotObj->dictLookup("LE", &obj1)->isArray() &&
423
92
      obj1.arrayGetLength() == 2) {
424
81
    lineEnd1 = parseLineEndType(obj1.arrayGet(0, &obj2));
425
81
    obj2.free();
426
81
    lineEnd2 = parseLineEndType(obj1.arrayGet(1, &obj2));
427
81
    obj2.free();
428
81
  }
429
109
  obj1.free();
430
109
  if (annotObj->dictLookup("LL", &obj1)->isNum()) {
431
0
    leaderLen = obj1.getNum();
432
109
  } else {
433
109
    leaderLen = 0;
434
109
  }
435
109
  obj1.free();
436
109
  if (annotObj->dictLookup("LLE", &obj1)->isNum()) {
437
0
    leaderExtLen = obj1.getNum();
438
109
  } else {
439
109
    leaderExtLen = 0;
440
109
  }
441
109
  obj1.free();
442
109
  if (annotObj->dictLookup("LLO", &obj1)->isNum()) {
443
0
    leaderOffLen = obj1.getNum();
444
109
  } else {
445
109
    leaderOffLen = 0;
446
109
  }
447
109
  obj1.free();
448
449
  //----- compute positions
450
109
  x1 -= xMin;
451
109
  y1 -= yMin;
452
109
  x2 -= xMin;
453
109
  y2 -= yMin;
454
109
  dx = x2 - x1;
455
109
  dy = y2 - y1;
456
109
  len = sqrt(dx*dx + dy*dy);
457
109
  if (len > 0) {
458
109
    dx /= len;
459
109
    dy /= len;
460
109
  }
461
109
  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
109
  } else {
475
109
    lx1 = x1;
476
109
    ly1 = y1;
477
109
    lx2 = x2;
478
109
    ly2 = y2;
479
109
    ax1 = ay1 = ax2 = ay2 = 0; // make gcc happy
480
109
    bx1 = by1 = bx2 = by2 = 0;
481
109
  }
482
109
  adjustLineEndpoint(lineEnd1, lx1, ly1, dx, dy, w, &tx1, &ty1);
483
109
  adjustLineEndpoint(lineEnd2, lx2, ly2, -dx, -dy, w, &tx2, &ty2);
484
485
  //----- draw leaders
486
109
  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
109
  appearBuf->appendf("{0:.4f} {1:.4f} m {2:.4f} {3:.4f} l\n",
495
109
         tx1, ty1, tx2, ty2);
496
109
  appearBuf->append("S\n");
497
498
  //----- draw the arrows
499
109
  if (borderStyle->getType() == annotBorderDashed) {
500
13
    appearBuf->append("[] 0 d\n");
501
13
  }
502
109
  drawLineArrow(lineEnd1, lx1, ly1, dx, dy, w, fill);
503
109
  drawLineArrow(lineEnd2, lx2, ly2, -dx, -dy, w, fill);
504
505
  //----- build the appearance stream dictionary
506
109
  appearDict.initDict(doc->getXRef());
507
109
  appearDict.dictAdd(copyString("Length"),
508
109
         obj1.initInt(appearBuf->getLength()));
509
109
  appearDict.dictAdd(copyString("Subtype"), obj1.initName("Form"));
510
109
  obj1.initArray(doc->getXRef());
511
109
  obj1.arrayAdd(obj2.initReal(0));
512
109
  obj1.arrayAdd(obj2.initReal(0));
513
109
  obj1.arrayAdd(obj2.initReal(xMax - xMin));
514
109
  obj1.arrayAdd(obj2.initReal(yMax - yMin));
515
109
  appearDict.dictAdd(copyString("BBox"), &obj1);
516
109
  if (gfxStateDict.isDict()) {
517
45
    obj1.initDict(doc->getXRef());
518
45
    obj2.initDict(doc->getXRef());
519
45
    obj2.dictAdd(copyString("GS1"), &gfxStateDict);
520
45
    obj1.dictAdd(copyString("ExtGState"), &obj2);
521
45
    appearDict.dictAdd(copyString("Resources"), &obj1);
522
45
  }
523
524
  //----- build the appearance stream
525
109
  appearStream = new MemStream(appearBuf->getCString(), 0,
526
109
             appearBuf->getLength(), &appearDict);
527
109
  appearance.free();
528
109
  appearance.initStream(appearStream);
529
109
}
530
531
//~ this doesn't handle line ends (arrows)
532
138
void Annot::generatePolyLineAppearance(Object *annotObj) {
533
138
  Object gfxStateDict, appearDict, obj1, obj2;
534
138
  MemStream *appearStream;
535
138
  double x1, y1, w;
536
138
  int i;
537
538
138
  appearBuf = new GString();
539
540
  //----- check for transparency
541
138
  if (annotObj->dictLookup("CA", &obj1)->isNum()) {
542
102
    gfxStateDict.initDict(doc->getXRef());
543
102
    gfxStateDict.dictAdd(copyString("ca"), obj1.copy(&obj2));
544
102
    appearBuf->append("/GS1 gs\n");
545
102
  }
546
138
  obj1.free();
547
548
  //----- set line style, colors
549
138
  setLineStyle(borderStyle, &w);
550
138
  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
138
  if (!annotObj->dictLookup("Vertices", &obj1)->isArray()) {
561
14
    obj1.free();
562
14
    return;
563
14
  }
564
395
  for (i = 0; i+1 < obj1.arrayGetLength(); i += 2) {
565
322
    if (!obj1.arrayGet(i, &obj2)->isNum()) {
566
19
      obj2.free();
567
19
      obj1.free();
568
19
      return;
569
19
    }
570
303
    x1 = obj2.getNum();
571
303
    obj2.free();
572
303
    if (!obj1.arrayGet(i+1, &obj2)->isNum()) {
573
32
      obj2.free();
574
32
      obj1.free();
575
32
      return;
576
32
    }
577
271
    y1 = obj2.getNum();
578
271
    obj2.free();
579
271
    x1 -= xMin;
580
271
    y1 -= yMin;
581
271
    if (i == 0) {
582
89
      appearBuf->appendf("{0:.4f} {1:.4f} m\n", x1, y1);
583
182
    } else {
584
182
      appearBuf->appendf("{0:.4f} {1:.4f} l\n", x1, y1);
585
182
    }
586
271
  }
587
73
  appearBuf->append("S\n");
588
73
  obj1.free();
589
590
  //----- build the appearance stream dictionary
591
73
  appearDict.initDict(doc->getXRef());
592
73
  appearDict.dictAdd(copyString("Length"),
593
73
         obj1.initInt(appearBuf->getLength()));
594
73
  appearDict.dictAdd(copyString("Subtype"), obj1.initName("Form"));
595
73
  obj1.initArray(doc->getXRef());
596
73
  obj1.arrayAdd(obj2.initReal(0));
597
73
  obj1.arrayAdd(obj2.initReal(0));
598
73
  obj1.arrayAdd(obj2.initReal(xMax - xMin));
599
73
  obj1.arrayAdd(obj2.initReal(yMax - yMin));
600
73
  appearDict.dictAdd(copyString("BBox"), &obj1);
601
73
  if (gfxStateDict.isDict()) {
602
43
    obj1.initDict(doc->getXRef());
603
43
    obj2.initDict(doc->getXRef());
604
43
    obj2.dictAdd(copyString("GS1"), &gfxStateDict);
605
43
    obj1.dictAdd(copyString("ExtGState"), &obj2);
606
43
    appearDict.dictAdd(copyString("Resources"), &obj1);
607
43
  }
608
609
  //----- build the appearance stream
610
73
  appearStream = new MemStream(appearBuf->getCString(), 0,
611
73
             appearBuf->getLength(), &appearDict);
612
73
  appearance.free();
613
73
  appearance.initStream(appearStream);
614
73
}
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
273
void Annot::generateFreeTextAppearance(Object *annotObj) {
701
273
  Object gfxStateDict, appearDict, obj1, obj2;
702
273
  Object resources, gsResources, fontResources, defaultFont;
703
273
  GString *text, *da;
704
273
  double lineWidth;
705
273
  int quadding, rot;
706
273
  MemStream *appearStream;
707
708
273
  appearBuf = new GString();
709
710
  //----- check for transparency
711
273
  if (annotObj->dictLookup("CA", &obj1)->isNum()) {
712
176
    gfxStateDict.initDict(doc->getXRef());
713
176
    gfxStateDict.dictAdd(copyString("ca"), obj1.copy(&obj2));
714
176
    appearBuf->append("/GS1 gs\n");
715
176
  }
716
273
  obj1.free();
717
718
  //----- draw the text
719
273
  if (annotObj->dictLookup("Contents", &obj1)->isString()) {
720
218
    text = obj1.getString()->copy();
721
218
  } else {
722
55
    text = new GString();
723
55
  }
724
273
  obj1.free();
725
273
  if (annotObj->dictLookup("Q", &obj1)->isInt()) {
726
0
    quadding = obj1.getInt();
727
273
  } else {
728
273
    quadding = 0;
729
273
  }
730
273
  obj1.free();
731
273
  if (annotObj->dictLookup("DA", &obj1)->isString()) {
732
39
    da = obj1.getString()->copy();
733
234
  } else {
734
234
    da = new GString();
735
234
  }
736
273
  obj1.free();
737
  // the "Rotate" field is not defined in the PDF spec, but Acrobat
738
  // looks at it
739
273
  if (annotObj->dictLookup("Rotate", &obj1)->isInt()) {
740
1
    rot = obj1.getInt();
741
272
  } else {
742
272
    rot = 0;
743
272
  }
744
273
  obj1.free();
745
273
  drawText(text, da, quadding, 0, rot);
746
273
  delete text;
747
273
  delete da;
748
749
  //----- draw the border
750
273
  if (borderStyle->getWidth() != 0) {
751
61
    setLineStyle(borderStyle, &lineWidth);
752
61
    appearBuf->appendf("{0:.4f} {1:.4f} {2:.4f} {3:.4f} re s\n",
753
61
           0.5 * lineWidth, 0.5 * lineWidth,
754
61
           xMax - xMin - lineWidth, yMax - yMin - lineWidth);
755
61
  }
756
757
  //----- build the appearance stream dictionary
758
273
  appearDict.initDict(doc->getXRef());
759
273
  appearDict.dictAdd(copyString("Length"),
760
273
         obj1.initInt(appearBuf->getLength()));
761
273
  appearDict.dictAdd(copyString("Subtype"), obj1.initName("Form"));
762
273
  obj1.initArray(doc->getXRef());
763
273
  obj1.arrayAdd(obj2.initReal(0));
764
273
  obj1.arrayAdd(obj2.initReal(0));
765
273
  obj1.arrayAdd(obj2.initReal(xMax - xMin));
766
273
  obj1.arrayAdd(obj2.initReal(yMax - yMin));
767
273
  appearDict.dictAdd(copyString("BBox"), &obj1);
768
273
  resources.initDict(doc->getXRef());
769
273
  defaultFont.initDict(doc->getXRef());
770
273
  defaultFont.dictAdd(copyString("Type"), obj1.initName("Font"));
771
273
  defaultFont.dictAdd(copyString("Subtype"), obj1.initName("Type1"));
772
273
  defaultFont.dictAdd(copyString("BaseFont"), obj1.initName("Helvetica"));
773
273
  defaultFont.dictAdd(copyString("Encoding"), obj1.initName("WinAnsiEncoding"));
774
273
  fontResources.initDict(doc->getXRef());
775
273
  fontResources.dictAdd(copyString("xpdf_default_font"), &defaultFont);
776
273
  resources.dictAdd(copyString("Font"), &fontResources);
777
273
  if (gfxStateDict.isDict()) {
778
176
    gsResources.initDict(doc->getXRef());
779
176
    gsResources.dictAdd(copyString("GS1"), &gfxStateDict);
780
176
    resources.dictAdd(copyString("ExtGState"), &gsResources);
781
176
  }
782
273
  appearDict.dictAdd(copyString("Resources"), &resources);
783
784
  //----- build the appearance stream
785
273
  appearStream = new MemStream(appearBuf->getCString(), 0,
786
273
             appearBuf->getLength(), &appearDict);
787
273
  appearance.free();
788
273
  appearance.initStream(appearStream);
789
273
}
790
791
380
void Annot::setLineStyle(AnnotBorderStyle *bs, double *lineWidth) {
792
380
  double *dash;
793
380
  double w;
794
380
  int dashLength, i;
795
796
380
  if ((w = borderStyle->getWidth()) <= 0) {
797
10
    w = 0.1;
798
10
  }
799
380
  *lineWidth = w;
800
380
  appearBuf->appendf("{0:.4f} w\n", w);
801
  // this treats beveled/inset/underline as solid
802
380
  if (borderStyle->getType() == annotBorderDashed) {
803
13
    borderStyle->getDash(&dash, &dashLength);
804
13
    appearBuf->append("[");
805
13
    for (i = 0; i < dashLength; ++i) {
806
0
      appearBuf->appendf(" {0:.4f}", dash[i]);
807
0
    }
808
13
    appearBuf->append("] 0 d\n");
809
13
  }
810
380
  appearBuf->append("0 j\n0 J\n");
811
380
}
812
813
319
void Annot::setStrokeColor(double *color, int nComps) {
814
319
  switch (nComps) {
815
0
  case 0:
816
0
    appearBuf->append("0 G\n");
817
0
    break;
818
13
  case 1:
819
13
    appearBuf->appendf("{0:.2f} G\n", color[0]);
820
13
    break;
821
288
  case 3:
822
288
    appearBuf->appendf("{0:.2f} {1:.2f} {2:.2f} RG\n",
823
288
           color[0], color[1], color[2]);
824
288
    break;
825
18
  case 4:
826
18
    appearBuf->appendf("{0:.2f} {1:.2f} {2:.2f} {3:.2f} K\n",
827
18
           color[0], color[1], color[2], color[3]);
828
18
    break;
829
319
  }
830
319
}
831
832
136
GBool Annot::setFillColor(Object *colorObj) {
833
136
  Object obj;
834
136
  double color[4];
835
136
  int i;
836
837
136
  if (!colorObj->isArray()) {
838
0
    return gFalse;
839
0
  }
840
501
  for (i = 0; i < colorObj->arrayGetLength() && i < 4; ++i) {
841
365
    if (colorObj->arrayGet(i, &obj)->isNum()) {
842
317
      color[i] = obj.getNum();
843
317
    } else {
844
48
      color[i] = 0;
845
48
    }
846
365
    obj.free();
847
365
  }
848
136
  switch (colorObj->arrayGetLength()) {
849
29
  case 1:
850
29
    appearBuf->appendf("{0:.2f} g\n", color[0]);
851
29
    return gTrue;
852
76
  case 3:
853
76
    appearBuf->appendf("{0:.2f} {1:.2f} {2:.2f} rg\n",
854
76
           color[0], color[1], color[2]);
855
76
    return gTrue;
856
15
  case 4:
857
15
    appearBuf->appendf("{0:.2f} {1:.2f} {2:.2f} {3:.3f} k\n",
858
15
           color[0], color[1],
859
15
           color[2], color[3]);
860
15
    return gTrue;
861
136
  }
862
16
  return gFalse;
863
136
}
864
865
162
AnnotLineEndType Annot::parseLineEndType(Object *obj) {
866
162
  if (obj->isName("None")) {
867
88
    return annotLineEndNone;
868
88
  } else if (obj->isName("Square")) {
869
0
    return annotLineEndSquare;
870
74
  } else if (obj->isName("Circle")) {
871
0
    return annotLineEndCircle;
872
74
  } else if (obj->isName("Diamond")) {
873
0
    return annotLineEndDiamond;
874
74
  } else if (obj->isName("OpenArrow")) {
875
0
    return annotLineEndOpenArrow;
876
74
  } else if (obj->isName("ClosedArrow")) {
877
41
    return annotLineEndClosedArrow;
878
41
  } else if (obj->isName("Butt")) {
879
0
    return annotLineEndButt;
880
33
  } else if (obj->isName("ROpenArrow")) {
881
0
    return annotLineEndROpenArrow;
882
33
  } else if (obj->isName("RClosedArrow")) {
883
0
    return annotLineEndRClosedArrow;
884
33
  } else if (obj->isName("Slash")) {
885
0
    return annotLineEndSlash;
886
33
  } else {
887
33
    return annotLineEndNone;
888
33
  }
889
162
}
890
891
void Annot::adjustLineEndpoint(AnnotLineEndType lineEnd,
892
             double x, double y, double dx, double dy,
893
218
             double w, double *tx, double *ty) {
894
218
  switch (lineEnd) {
895
177
  case annotLineEndNone:
896
177
    w = 0;
897
177
    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
41
  case annotLineEndClosedArrow:
911
41
    w *= lineEndSize2 * cos(lineArrowAngle);
912
41
    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
218
  }
926
218
  *tx = x + w * dx;
927
218
  *ty = y + w * dy;
928
218
}
929
930
void Annot::drawLineArrow(AnnotLineEndType lineEnd,
931
        double x, double y, double dx, double dy,
932
218
        double w, GBool fill) {
933
218
  switch (lineEnd) {
934
177
  case annotLineEndNone:
935
177
    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
41
  case annotLineEndClosedArrow:
982
41
    w *= lineEndSize2;
983
41
    appearBuf->appendf("{0:.4f} {1:.4f} m\n",
984
41
           x + w*cos(lineArrowAngle)*dx + w*sin(lineArrowAngle)*dy,
985
41
           y + w*cos(lineArrowAngle)*dy - w*sin(lineArrowAngle)*dx);
986
41
    appearBuf->appendf("{0:.4f} {1:.4f} l\n", x, y);
987
41
    appearBuf->appendf("{0:.4f} {1:.4f} l\n",
988
41
           x + w*cos(lineArrowAngle)*dx - w*sin(lineArrowAngle)*dy,
989
41
           y + w*cos(lineArrowAngle)*dy + w*sin(lineArrowAngle)*dx);
990
41
    appearBuf->append(fill ? "b\n" : "s\n");
991
41
    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
218
  }
1043
218
}
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
273
         int rot) {
1121
273
  GString *text2, *tok;
1122
273
  GList *daToks;
1123
273
  const char *charName;
1124
273
  double dx, dy, fontSize, fontSize2, x, y, w;
1125
273
  Gushort charWidth;
1126
273
  int tfPos, tmPos, i, j, c;
1127
1128
  // check for a Unicode string
1129
  //~ this currently drops all non-Latin1 characters
1130
273
  if (text->getLength() >= 2 &&
1131
218
      text->getChar(0) == '\xfe' && text->getChar(1) == '\xff') {
1132
14
    text2 = new GString();
1133
6.41k
    for (i = 2; i+1 < text->getLength(); i += 2) {
1134
6.40k
      c = ((text->getChar(i) & 0xff) << 8) + (text->getChar(i+1) & 0xff);
1135
6.40k
      if (c <= 0xff) {
1136
368
  text2->append((char)c);
1137
6.03k
      } else {
1138
6.03k
  text2->append('?');
1139
6.03k
      }
1140
6.40k
    }
1141
259
  } else {
1142
259
    text2 = text;
1143
259
  }
1144
1145
  // parse the default appearance string
1146
273
  tfPos = tmPos = -1;
1147
273
  if (da) {
1148
273
    daToks = new GList();
1149
273
    i = 0;
1150
1.16k
    while (i < da->getLength()) {
1151
2.17k
      while (i < da->getLength() && Lexer::isSpace(da->getChar(i))) {
1152
1.29k
  ++i;
1153
1.29k
      }
1154
887
      if (i < da->getLength()) {
1155
883
  for (j = i + 1;
1156
7.08k
       j < da->getLength() && !Lexer::isSpace(da->getChar(j));
1157
6.20k
       ++j) ;
1158
883
  daToks->append(new GString(da, i, j - i));
1159
883
  i = j;
1160
883
      }
1161
887
    }
1162
1.07k
    for (i = 2; i < daToks->getLength(); ++i) {
1163
806
      if (i >= 2 && !((GString *)daToks->get(i))->cmp("Tf")) {
1164
24
  tfPos = i - 2;
1165
782
      } else if (i >= 6 && !((GString *)daToks->get(i))->cmp("Tm")) {
1166
1
  tmPos = i - 6;
1167
1
      }
1168
806
    }
1169
273
  } else {
1170
0
    daToks = NULL;
1171
0
  }
1172
1173
  // get the font and font size
1174
273
  fontSize = 0;
1175
273
  if (tfPos >= 0) {
1176
    //~ where do we look up the font?
1177
23
    tok = (GString *)daToks->get(tfPos);
1178
23
    tok->clear();
1179
23
    tok->append("/xpdf_default_font");
1180
23
    tok = (GString *)daToks->get(tfPos + 1);
1181
23
    fontSize = atof(tok->getCString());
1182
250
  } else {
1183
250
    error(errSyntaxError, -1,
1184
250
    "Missing 'Tf' operator in annotation's DA string");
1185
250
    daToks->append(new GString("/xpdf_default_font"));
1186
250
    daToks->append(new GString("10"));
1187
250
    daToks->append(new GString("Tf"));
1188
250
  }
1189
1190
  // setup
1191
273
  appearBuf->append("q\n");
1192
273
  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
273
  } 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
273
  } 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
273
  } else { // assume rot == 0
1206
273
    dx = xMax - xMin;
1207
273
    dy = yMax - yMin;
1208
273
  }
1209
273
  appearBuf->append("BT\n");
1210
1211
  // compute string width
1212
  //~ this assumes we're substituting Helvetica/WinAnsiEncoding for everything
1213
273
  w = 0;
1214
78.1k
  for (i = 0; i < text2->getLength(); ++i) {
1215
77.8k
    charName = winAnsiEncoding[text->getChar(i) & 0xff];
1216
77.8k
    if (charName && builtinFonts[4].widths->getWidth(charName, &charWidth)) {
1217
63.4k
      w += charWidth;
1218
63.4k
    } else {
1219
14.4k
      w += 0.5;
1220
14.4k
    }
1221
77.8k
  }
1222
1223
  // compute font autosize
1224
273
  if (fontSize == 0) {
1225
253
    fontSize = dy - 2 * margin;
1226
253
    fontSize2 = (dx - 2 * margin) / w;
1227
253
    if (fontSize2 < fontSize) {
1228
203
      fontSize = fontSize2;
1229
203
    }
1230
253
    fontSize = floor(fontSize);
1231
253
    if (tfPos >= 0) {
1232
3
      tok = (GString *)daToks->get(tfPos + 1);
1233
3
      tok->clear();
1234
3
      tok->appendf("{0:.4f}", fontSize);
1235
3
    }
1236
253
  }
1237
1238
  // compute text start position
1239
273
  w *= fontSize;
1240
273
  switch (quadding) {
1241
273
  case 0:
1242
273
  default:
1243
273
    x = margin + 2;
1244
273
    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
273
  }
1252
273
  y = 0.5 * dy - 0.4 * fontSize;
1253
1254
  // set the font matrix
1255
273
  if (tmPos >= 0) {
1256
1
    tok = (GString *)daToks->get(tmPos + 4);
1257
1
    tok->clear();
1258
1
    tok->appendf("{0:.4f}", x);
1259
1
    tok = (GString *)daToks->get(tmPos + 5);
1260
1
    tok->clear();
1261
1
    tok->appendf("{0:.4f}", y);
1262
1
  }
1263
  
1264
  // write the DA string
1265
273
  if (daToks) {
1266
1.90k
    for (i = 0; i < daToks->getLength(); ++i) {
1267
1.63k
      appearBuf->append((GString *)daToks->get(i))->append(' ');
1268
1.63k
    }
1269
273
  }
1270
1271
  // write the font matrix (if not part of the DA string)
1272
273
  if (tmPos < 0) {
1273
272
    appearBuf->appendf("1 0 0 1 {0:.4f} {1:.4f} Tm\n", x, y);
1274
272
  }
1275
1276
  // write the text string
1277
273
  appearBuf->append('(');
1278
78.1k
  for (i = 0; i < text2->getLength(); ++i) {
1279
77.8k
    c = text2->getChar(i) & 0xff;
1280
77.8k
    if (c == '(' || c == ')' || c == '\\') {
1281
805
      appearBuf->append('\\');
1282
805
      appearBuf->append((char)c);
1283
77.0k
    } else if (c < 0x20 || c >= 0x80) {
1284
34.3k
      appearBuf->appendf("\\{0:03o}", c);
1285
42.6k
    } else {
1286
42.6k
      appearBuf->append((char)c);
1287
42.6k
    }
1288
77.8k
  }
1289
273
  appearBuf->append(") Tj\n");
1290
1291
  // cleanup
1292
273
  appearBuf->append("ET\n");
1293
273
  appearBuf->append("Q\n");
1294
1295
273
  if (daToks) {
1296
273
    deleteGList(daToks, GString);
1297
273
  }
1298
273
  if (text2 != text) {
1299
14
    delete text2;
1300
14
  }
1301
273
}
1302
1303
10.0k
void Annot::draw(Gfx *gfx, GBool printing) {
1304
10.0k
  GBool oc, isLink;
1305
1306
  // check the flags
1307
10.0k
  if ((flags & annotFlagHidden) ||
1308
9.87k
      (printing && !(flags & annotFlagPrint)) ||
1309
9.20k
      (!printing && (flags & annotFlagNoView))) {
1310
870
    return;
1311
870
  }
1312
1313
  // check the optional content entry
1314
9.19k
  if (doc->getOptionalContent()->evalOCObject(&ocObj, &oc) && !oc) {
1315
0
    return;
1316
0
  }
1317
1318
  // draw the appearance stream
1319
9.19k
  isLink = type && !type->cmp("Link");
1320
9.19k
  gfx->drawAnnot(&appearance, isLink ? borderStyle : (AnnotBorderStyle *)NULL,
1321
9.19k
     xMin, yMin, xMax, yMax);
1322
9.19k
}
1323
1324
771
Object *Annot::getObject(Object *obj) {
1325
771
  if (ref.num >= 0) {
1326
592
    xref->fetch(ref.num, ref.gen, obj);
1327
592
  } else {
1328
179
    obj->initNull();
1329
179
  }
1330
771
  return obj;
1331
771
}
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
193k
PageAnnots::PageAnnots() {
1348
193k
  annots = new GList();
1349
193k
  appearancesGenerated = gFalse;
1350
193k
}
1351
1352
193k
PageAnnots::~PageAnnots() {
1353
193k
  deleteGList(annots, Annot);
1354
193k
}
1355
1356
//------------------------------------------------------------------------
1357
// Annots
1358
//------------------------------------------------------------------------
1359
1360
15.5k
Annots::Annots(PDFDoc *docA) {
1361
15.5k
  doc = docA;
1362
15.5k
  pageAnnots = (PageAnnots **)gmallocn(doc->getNumPages(), sizeof(PageAnnots*));
1363
347k
  for (int page = 1; page <= doc->getNumPages(); ++page) {
1364
332k
    pageAnnots[page - 1] = NULL;
1365
332k
  }
1366
15.5k
  formFieldRefsSize = 0;
1367
15.5k
  formFieldRefs = NULL;
1368
15.5k
#if MULTITHREADED
1369
15.5k
  gInitMutex(&mutex);
1370
15.5k
#endif
1371
15.5k
}
1372
1373
15.5k
Annots::~Annots() {
1374
347k
  for (int page = 1; page <= doc->getNumPages(); ++page) {
1375
332k
    delete pageAnnots[page - 1];
1376
332k
  }
1377
15.5k
  gfree(pageAnnots);
1378
15.5k
  gfree(formFieldRefs);
1379
15.5k
#if MULTITHREADED
1380
15.5k
  gDestroyMutex(&mutex);
1381
15.5k
#endif
1382
15.5k
}
1383
1384
397k
void Annots::loadAnnots(int page) {
1385
397k
#if MULTITHREADED
1386
397k
  gLockMutex(&mutex);
1387
397k
#endif
1388
397k
  if (pageAnnots[page - 1]) {
1389
203k
#if MULTITHREADED
1390
203k
    gUnlockMutex(&mutex);
1391
203k
#endif
1392
203k
    return;
1393
203k
  }
1394
1395
193k
  pageAnnots[page - 1] = new PageAnnots();
1396
1397
193k
  Object annotsObj;
1398
193k
  doc->getCatalog()->getPage(page)->getAnnots(&annotsObj);
1399
193k
  if (!annotsObj.isArray()) {
1400
184k
    annotsObj.free();
1401
184k
#if MULTITHREADED
1402
184k
    gUnlockMutex(&mutex);
1403
184k
#endif
1404
184k
    return;
1405
184k
  }
1406
1407
9.69k
  loadFormFieldRefs();
1408
1409
453k
  for (int i = 0; i < annotsObj.arrayGetLength(); ++i) {
1410
443k
    Object annotObj;
1411
443k
    Ref annotRef;
1412
443k
    if (annotsObj.arrayGetNF(i, &annotObj)->isRef()) {
1413
88.5k
      annotRef = annotObj.getRef();
1414
88.5k
      annotObj.free();
1415
88.5k
      annotsObj.arrayGet(i, &annotObj);
1416
355k
    } else {
1417
355k
      annotRef.num = annotRef.gen = -1;
1418
355k
    }
1419
443k
    if (!annotObj.isDict()) {
1420
408k
      annotObj.free();
1421
408k
      continue;
1422
408k
    }
1423
1424
    // skip any annotations which are used as AcroForm fields --
1425
    // they'll be rendered by the AcroForm module
1426
34.9k
    if (annotRef.num >= 0 && annotRef.num < formFieldRefsSize &&
1427
6.85k
  formFieldRefs[annotRef.num]) {
1428
4.03k
      annotObj.free();
1429
4.03k
      continue;
1430
4.03k
    }
1431
1432
30.9k
    Annot *annot = new Annot(doc, annotObj.getDict(), &annotRef);
1433
30.9k
    annotObj.free();
1434
30.9k
    if (annot->isOk()) {
1435
10.0k
      pageAnnots[page - 1]->annots->append(annot);
1436
20.8k
    } else {
1437
20.8k
      delete annot;
1438
20.8k
    }
1439
30.9k
  }
1440
1441
9.69k
  annotsObj.free();
1442
1443
9.69k
#if MULTITHREADED
1444
9.69k
  gUnlockMutex(&mutex);
1445
9.69k
#endif
1446
9.69k
}
1447
1448
// Build a set of object refs for AcroForm fields.
1449
9.69k
void Annots::loadFormFieldRefs() {
1450
9.69k
  if (formFieldRefs) {
1451
1.48k
    return;
1452
1.48k
  }
1453
1454
8.20k
  AcroForm *form = doc->getCatalog()->getForm();
1455
8.20k
  if (!form) {
1456
7.54k
    return;
1457
7.54k
  }
1458
1459
666
  int newFormFieldRefsSize = 256;
1460
4.09k
  for (int i = 0; i < form->getNumFields(); ++i) {
1461
3.42k
    AcroFormField *field = form->getField(i);
1462
3.42k
    Object fieldRef;
1463
3.42k
    field->getFieldRef(&fieldRef);
1464
3.42k
    if (fieldRef.isRef()) {
1465
3.38k
      if (fieldRef.getRefNum() >= formFieldRefsSize) {
1466
682
  while (fieldRef.getRefNum() >= newFormFieldRefsSize &&
1467
70
         newFormFieldRefsSize <= INT_MAX / 2) {
1468
70
    newFormFieldRefsSize *= 2;
1469
70
  }
1470
612
  if (fieldRef.getRefNum() >= newFormFieldRefsSize) {
1471
0
    continue;
1472
0
  }
1473
612
  formFieldRefs = (char *)grealloc(formFieldRefs, newFormFieldRefsSize);
1474
197k
  for (int j = formFieldRefsSize; j < newFormFieldRefsSize; ++j) {
1475
197k
    formFieldRefs[j] = (char)0;
1476
197k
  }
1477
612
  formFieldRefsSize = newFormFieldRefsSize;
1478
612
      }
1479
3.38k
      formFieldRefs[fieldRef.getRefNum()] = (char)1;
1480
3.38k
    }
1481
3.42k
    fieldRef.free();
1482
3.42k
  }
1483
666
}
1484
1485
193k
int Annots::getNumAnnots(int page) {
1486
193k
  loadAnnots(page);
1487
193k
  return pageAnnots[page - 1]->annots->getLength();
1488
193k
}
1489
1490
10.0k
Annot *Annots::getAnnot(int page, int idx) {
1491
10.0k
  loadAnnots(page);
1492
10.0k
  return (Annot *)pageAnnots[page - 1]->annots->get(idx);
1493
10.0k
}
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
193k
void Annots::generateAnnotAppearances(int page) {
1534
193k
  loadAnnots(page);
1535
193k
  PageAnnots *pa = pageAnnots[page - 1];
1536
193k
#if MULTITHREADED
1537
193k
  gLockMutex(&mutex);
1538
193k
#endif
1539
193k
  if (!pa->appearancesGenerated) {
1540
203k
    for (int i = 0; i < pa->annots->getLength(); ++i) {
1541
10.0k
      Annot *annot = (Annot *)pa->annots->get(i);
1542
10.0k
      annot->generateAnnotAppearance(NULL);
1543
10.0k
    }
1544
193k
    pa->appearancesGenerated = gTrue;
1545
193k
  }
1546
193k
#if MULTITHREADED
1547
193k
  gUnlockMutex(&mutex);
1548
193k
#endif
1549
193k
}