Coverage Report

Created: 2025-07-12 06:54

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