245780dab375fb9cdffd2e0cb719d18d38656c7c
[swftools.git] / pdf2swf / xpdf / Annot.cc
1 //========================================================================
2 //
3 // Annot.cc
4 //
5 // Copyright 2000-2003 Glyph & Cog, LLC
6 //
7 //========================================================================
8
9 #include <aconf.h>
10
11 #ifdef USE_GCC_PRAGMAS
12 #pragma implementation
13 #endif
14
15 #include "gmem.h"
16 #include "Object.h"
17 #include "Gfx.h"
18 #include "Annot.h"
19
20 //------------------------------------------------------------------------
21 // Annot
22 //------------------------------------------------------------------------
23
24 Annot::Annot(XRef *xrefA, Dict *dict) {
25   Object apObj, asObj, obj1, obj2;
26   double t;
27
28   ok = gFalse;
29   xref = xrefA;
30
31   if (dict->lookup("AP", &apObj)->isDict()) {
32     if (dict->lookup("AS", &asObj)->isName()) {
33       if (apObj.dictLookup("N", &obj1)->isDict()) {
34         if (obj1.dictLookupNF(asObj.getName(), &obj2)->isRef()) {
35           obj2.copy(&appearance);
36           ok = gTrue;
37         }
38         obj2.free();
39       }
40       obj1.free();
41     } else {
42       if (apObj.dictLookupNF("N", &obj1)->isRef()) {
43         obj1.copy(&appearance);
44         ok = gTrue;
45       }
46       obj1.free();
47     }
48     asObj.free();
49   }
50   apObj.free();
51
52   if (dict->lookup("Rect", &obj1)->isArray() &&
53       obj1.arrayGetLength() == 4) {
54     //~ should check object types here
55     obj1.arrayGet(0, &obj2);
56     xMin = obj2.getNum();
57     obj2.free();
58     obj1.arrayGet(1, &obj2);
59     yMin = obj2.getNum();
60     obj2.free();
61     obj1.arrayGet(2, &obj2);
62     xMax = obj2.getNum();
63     obj2.free();
64     obj1.arrayGet(3, &obj2);
65     yMax = obj2.getNum();
66     obj2.free();
67     if (xMin > xMax) {
68       t = xMin; xMin = xMax; xMax = t;
69     }
70     if (yMin > yMax) {
71       t = yMin; yMin = yMax; yMax = t;
72     }
73   } else {
74     //~ this should return an error
75     xMin = yMin = 0;
76     xMax = yMax = 1;
77   }
78   obj1.free();
79 }
80
81 Annot::~Annot() {
82   appearance.free();
83 }
84
85 void Annot::draw(Gfx *gfx) {
86   Object obj;
87
88   if (appearance.fetch(xref, &obj)->isStream()) {
89     gfx->doAnnot(&obj, xMin, yMin, xMax, yMax);
90   }
91   obj.free();
92 }
93
94 //------------------------------------------------------------------------
95 // Annots
96 //------------------------------------------------------------------------
97
98 Annots::Annots(XRef *xref, Object *annotsObj) {
99   Annot *annot;
100   Object obj1;
101   int size;
102   int i;
103
104   annots = NULL;
105   size = 0;
106   nAnnots = 0;
107
108   if (annotsObj->isArray()) {
109     for (i = 0; i < annotsObj->arrayGetLength(); ++i) {
110       if (annotsObj->arrayGet(i, &obj1)->isDict()) {
111         annot = new Annot(xref, obj1.getDict());
112         if (annot->isOk()) {
113           if (nAnnots >= size) {
114             size += 16;
115             annots = (Annot **)grealloc(annots, size * sizeof(Annot *));
116           }
117           annots[nAnnots++] = annot;
118         } else {
119           delete annot;
120         }
121       }
122       obj1.free();
123     }
124   }
125 }
126
127 Annots::~Annots() {
128   int i;
129
130   for (i = 0; i < nAnnots; ++i) {
131     delete annots[i];
132   }
133   gfree(annots);
134 }