bugfix: memset's 3rd parameter in generated C code caused segfault
[swftools.git] / pdf2swf / xpdf / FormWidget.cc
1 //========================================================================
2 //
3 // FormWidget.cc
4 //
5 // Copyright 2000 Derek B. Noonburg
6 //
7 //========================================================================
8
9 #ifdef __GNUC__
10 #pragma implementation
11 #endif
12
13 #include "gmem.h"
14 #include "Object.h"
15 #include "Gfx.h"
16 #include "FormWidget.h"
17
18 //------------------------------------------------------------------------
19 // FormWidget
20 //------------------------------------------------------------------------
21
22 FormWidget::FormWidget(Dict *dict) {
23   Object obj1, obj2;
24   double t;
25
26   ok = gFalse;
27
28   if (dict->lookup("AP", &obj1)->isDict()) {
29     obj1.dictLookupNF("N", &obj2);
30     //~ this doesn't handle appearances with multiple states --
31     //~ need to look at AS key to get state and then get the
32     //~ corresponding entry from the N dict
33     if (obj2.isRef()) {
34       obj2.copy(&appearance);
35       ok = gTrue;
36     }
37     obj2.free();
38   }
39   obj1.free();
40
41   if (dict->lookup("Rect", &obj1)->isArray() &&
42       obj1.arrayGetLength() == 4) {
43     //~ should check object types here
44     obj1.arrayGet(0, &obj2);
45     xMin = obj2.getNum();
46     obj2.free();
47     obj1.arrayGet(1, &obj2);
48     yMin = obj2.getNum();
49     obj2.free();
50     obj1.arrayGet(2, &obj2);
51     xMax = obj2.getNum();
52     obj2.free();
53     obj1.arrayGet(3, &obj2);
54     yMax = obj2.getNum();
55     obj2.free();
56     if (xMin > xMax) {
57       t = xMin; xMin = xMax; xMax = t;
58     }
59     if (yMin > yMax) {
60       t = yMin; yMin = yMax; yMax = t;
61     }
62   } else {
63     //~ this should return an error
64     xMin = yMin = 0;
65     xMax = yMax = 1;
66   }
67   obj1.free();
68 }
69
70 FormWidget::~FormWidget() {
71   appearance.free();
72 }
73
74 void FormWidget::draw(Gfx *gfx) {
75   Object obj;
76
77   if (appearance.fetch(&obj)->isStream()) {
78     gfx->doWidgetForm(&obj, xMin, yMin, xMax, yMax);
79   }
80   obj.free();
81 }
82
83 //------------------------------------------------------------------------
84 // FormWidgets
85 //------------------------------------------------------------------------
86
87 FormWidgets::FormWidgets(Object *annots) {
88   FormWidget *widget;
89   Object obj1, obj2;
90   int size;
91   int i;
92
93   widgets = NULL;
94   size = 0;
95   nWidgets = 0;
96
97   if (annots->isArray()) {
98     for (i = 0; i < annots->arrayGetLength(); ++i) {
99       if (annots->arrayGet(i, &obj1)->isDict()) {
100         obj1.dictLookup("Subtype", &obj2);
101         if (obj2.isName("Widget") ||
102             obj2.isName("Stamp")) {
103           widget = new FormWidget(obj1.getDict());
104           if (widget->isOk()) {
105             if (nWidgets >= size) {
106               size += 16;
107               widgets = (FormWidget **)grealloc(widgets,
108                                                 size * sizeof(FormWidget *));
109             }
110             widgets[nWidgets++] = widget;
111           } else {
112             delete widget;
113           }
114         }
115         obj2.free();
116       }
117       obj1.free();
118     }
119   }
120 }
121
122 FormWidgets::~FormWidgets() {
123   int i;
124
125   for (i = 0; i < nWidgets; ++i) {
126     delete widgets[i];
127   }
128   gfree(widgets);
129 }