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