4c644b86b3ecc19c0b07dd0938239ee3a69d22d9
[swftools.git] / pdf2swf / xpdf / Link.h
1 //========================================================================
2 //
3 // Link.h
4 //
5 // Copyright 1996-2002 Glyph & Cog, LLC
6 //
7 //========================================================================
8
9 #ifndef LINK_H
10 #define LINK_H
11
12 #ifdef __GNUC__
13 #pragma interface
14 #endif
15
16 #include "Object.h"
17
18 class GString;
19 class Array;
20 class Dict;
21
22 //------------------------------------------------------------------------
23 // LinkAction
24 //------------------------------------------------------------------------
25
26 enum LinkActionKind {
27   actionGoTo,                   // go to destination
28   actionGoToR,                  // go to destination in new file
29   actionLaunch,                 // launch app (or open document)
30   actionURI,                    // URI
31   actionNamed,                  // named action
32   actionUnknown                 // anything else
33 };
34
35 class LinkAction {
36 public:
37
38   // Destructor.
39   virtual ~LinkAction() {}
40
41   // Was the LinkAction created successfully?
42   virtual GBool isOk() = 0;
43
44   // Check link action type.
45   virtual LinkActionKind getKind() = 0;
46 };
47
48 //------------------------------------------------------------------------
49 // LinkDest
50 //------------------------------------------------------------------------
51
52 enum LinkDestKind {
53   destXYZ,
54   destFit,
55   destFitH,
56   destFitV,
57   destFitR,
58   destFitB,
59   destFitBH,
60   destFitBV
61 };
62
63 class LinkDest {
64 public:
65
66   // Build a LinkDest from the array.
67   LinkDest(Array *a);
68
69   // Copy a LinkDest.
70   LinkDest *copy() { return new LinkDest(this); }
71
72   // Was the LinkDest created successfully?
73   GBool isOk() { return ok; }
74
75   // Accessors.
76   LinkDestKind getKind() { return kind; }
77   GBool isPageRef() { return pageIsRef; }
78   int getPageNum() { return pageNum; }
79   Ref getPageRef() { return pageRef; }
80   double getLeft() { return left; }
81   double getBottom() { return bottom; }
82   double getRight() { return right; }
83   double getTop() { return top; }
84   double getZoom() { return zoom; }
85   GBool getChangeLeft() { return changeLeft; }
86   GBool getChangeTop() { return changeTop; }
87   GBool getChangeZoom() { return changeZoom; }
88
89 private:
90
91   LinkDestKind kind;            // destination type
92   GBool pageIsRef;              // is the page a reference or number?
93   union {
94     Ref pageRef;                // reference to page
95     int pageNum;                // one-relative page number
96   };
97   double left, bottom;          // position
98   double right, top;
99   double zoom;                  // zoom factor
100   GBool changeLeft, changeTop;  // for destXYZ links, which position
101   GBool changeZoom;             //   components to change
102   GBool ok;                     // set if created successfully
103
104   LinkDest(LinkDest *dest);
105 };
106
107 //------------------------------------------------------------------------
108 // LinkGoTo
109 //------------------------------------------------------------------------
110
111 class LinkGoTo: public LinkAction {
112 public:
113
114   // Build a LinkGoTo from a destination (dictionary, name, or string).
115   LinkGoTo(Object *destObj);
116
117   // Destructor.
118   virtual ~LinkGoTo();
119
120   // Was the LinkGoTo created successfully?
121   virtual GBool isOk() { return dest || namedDest; }
122
123   // Accessors.
124   virtual LinkActionKind getKind() { return actionGoTo; }
125   LinkDest *getDest() { return dest; }
126   GString *getNamedDest() { return namedDest; }
127
128 private:
129
130   LinkDest *dest;               // regular destination (NULL for remote
131                                 //   link with bad destination)
132   GString *namedDest;           // named destination (only one of dest and
133                                 //   and namedDest may be non-NULL)
134 };
135
136 //------------------------------------------------------------------------
137 // LinkGoToR
138 //------------------------------------------------------------------------
139
140 class LinkGoToR: public LinkAction {
141 public:
142
143   // Build a LinkGoToR from a file spec (dictionary) and destination
144   // (dictionary, name, or string).
145   LinkGoToR(Object *fileSpecObj, Object *destObj);
146
147   // Destructor.
148   virtual ~LinkGoToR();
149
150   // Was the LinkGoToR created successfully?
151   virtual GBool isOk() { return fileName && (dest || namedDest); }
152
153   // Accessors.
154   virtual LinkActionKind getKind() { return actionGoToR; }
155   GString *getFileName() { return fileName; }
156   LinkDest *getDest() { return dest; }
157   GString *getNamedDest() { return namedDest; }
158
159 private:
160
161   GString *fileName;            // file name
162   LinkDest *dest;               // regular destination (NULL for remote
163                                 //   link with bad destination)
164   GString *namedDest;           // named destination (only one of dest and
165                                 //   and namedDest may be non-NULL)
166 };
167
168 //------------------------------------------------------------------------
169 // LinkLaunch
170 //------------------------------------------------------------------------
171
172 class LinkLaunch: public LinkAction {
173 public:
174
175   // Build a LinkLaunch from an action dictionary.
176   LinkLaunch(Object *actionObj);
177
178   // Destructor.
179   virtual ~LinkLaunch();
180
181   // Was the LinkLaunch created successfully?
182   virtual GBool isOk() { return fileName != NULL; }
183
184   // Accessors.
185   virtual LinkActionKind getKind() { return actionLaunch; }
186   GString *getFileName() { return fileName; }
187   GString *getParams() { return params; }
188
189 private:
190
191   GString *fileName;            // file name
192   GString *params;              // parameters
193 };
194
195 //------------------------------------------------------------------------
196 // LinkURI
197 //------------------------------------------------------------------------
198
199 class LinkURI: public LinkAction {
200 public:
201
202   // Build a LinkURI given the URI (string) and base URI.
203   LinkURI(Object *uriObj, GString *baseURI);
204
205   // Destructor.
206   virtual ~LinkURI();
207
208   // Was the LinkURI created successfully?
209   virtual GBool isOk() { return uri != NULL; }
210
211   // Accessors.
212   virtual LinkActionKind getKind() { return actionURI; }
213   GString *getURI() { return uri; }
214
215 private:
216
217   GString *uri;                 // the URI
218 };
219
220 //------------------------------------------------------------------------
221 // LinkNamed
222 //------------------------------------------------------------------------
223
224 class LinkNamed: public LinkAction {
225 public:
226
227   // Build a LinkNamed given the action name.
228   LinkNamed(Object *nameObj);
229
230   virtual ~LinkNamed();
231
232   virtual GBool isOk() { return name != NULL; }
233
234   virtual LinkActionKind getKind() { return actionNamed; }
235   GString *getName() { return name; }
236
237 private:
238
239   GString *name;
240 };
241
242 //------------------------------------------------------------------------
243 // LinkUnknown
244 //------------------------------------------------------------------------
245
246 class LinkUnknown: public LinkAction {
247 public:
248
249   // Build a LinkUnknown with the specified action type.
250   LinkUnknown(char *actionA);
251
252   // Destructor.
253   virtual ~LinkUnknown();
254
255   // Was the LinkUnknown create successfully?
256   virtual GBool isOk() { return action != NULL; }
257
258   // Accessors.
259   virtual LinkActionKind getKind() { return actionUnknown; }
260   GString *getAction() { return action; }
261
262 private:
263
264   GString *action;              // action subtype
265 };
266
267 //------------------------------------------------------------------------
268 // Link
269 //------------------------------------------------------------------------
270
271 class Link {
272 public:
273
274   // Construct a link, given its dictionary.
275   Link(Dict *dict, GString *baseURI);
276
277   // Destructor.
278   ~Link();
279
280   // Was the link created successfully?
281   GBool isOk() { return ok; }
282
283   // Check if point is inside the link rectangle.
284   GBool inRect(double x, double y)
285     { return x1 <= x && x <= x2 && y1 <= y && y <= y2; }
286
287   // Get action.
288   LinkAction *getAction() { return action; }
289
290   // Get border corners and width.
291   void getBorder(double *xa1, double *ya1, double *xa2, double *ya2,
292                  double *wa)
293     { *xa1 = x1; *ya1 = y1; *xa2 = x2; *ya2 = y2; *wa = borderW; }
294
295 private:
296
297   double x1, y1;                // lower left corner
298   double x2, y2;                // upper right corner
299   double borderW;               // border width
300   LinkAction *action;           // action
301   GBool ok;                     // is link valid?
302 };
303
304 //------------------------------------------------------------------------
305 // Links
306 //------------------------------------------------------------------------
307
308 class Links {
309 public:
310
311   // Extract links from array of annotations.
312   Links(Object *annots, GString *baseURI);
313
314   // Destructor.
315   ~Links();
316
317   // Iterate through list of links.
318   int getNumLinks() { return numLinks; }
319   Link *getLink(int i) { return links[i]; }
320
321   // If point <x>,<y> is in a link, return the associated action;
322   // else return NULL.
323   LinkAction *find(double x, double y);
324
325   // Return true if <x>,<y> is in a link.
326   GBool onLink(double x, double y);
327
328 private:
329
330   Link **links;
331   int numLinks;
332 };
333
334 #endif