Annot.cpp
1//========================================================================
2//
3// Annot.cpp
4//
5// Copyright 2000-2002 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
24Annot::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
81Annot::~Annot() {
82 appearance.free();
83}
84
85void 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
98Annots::Annots(XRef *xref, Object *annotsObj) {
99 Annot *annot;
100 Object obj1, obj2;
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 obj1.dictLookup("Subtype", &obj2);
112 if (obj2.isName("Widget") ||
113 obj2.isName("Stamp")) {
114 annot = new Annot(xref, obj1.getDict());
115 if (annot->isOk()) {
116 if (nAnnots >= size) {
117 size += 16;
118 annots = (Annot **)grealloc(annots, size * sizeof(Annot *));
119 }
120 annots[nAnnots++] = annot;
121 } else {
122 delete annot;
123 }
124 }
125 obj2.free();
126 }
127 obj1.free();
128 }
129 }
130}
131
132Annots::~Annots() {
133 int i;
134
135 for (i = 0; i < nAnnots; ++i) {
136 delete annots[i];
137 }
138 gfree(annots);
139}