source: trunk/libdjvu/GMapAreas.h @ 209

Last change on this file since 209 was 206, checked in by Eugene Romanenko, 14 years ago

DJVU plugin: djvulibre updated to version 3.5.19

File size: 20.9 KB
Line 
1//C-  -*- C++ -*-
2//C- -------------------------------------------------------------------
3//C- DjVuLibre-3.5
4//C- Copyright (c) 2002  Leon Bottou and Yann Le Cun.
5//C- Copyright (c) 2001  AT&T
6//C-
7//C- This software is subject to, and may be distributed under, the
8//C- GNU General Public License, either Version 2 of the license,
9//C- or (at your option) any later version. The license should have
10//C- accompanied the software or you may obtain a copy of the license
11//C- from the Free Software Foundation at http://www.fsf.org .
12//C-
13//C- This program is distributed in the hope that it will be useful,
14//C- but WITHOUT ANY WARRANTY; without even the implied warranty of
15//C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16//C- GNU General Public License for more details.
17//C-
18//C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library from
19//C- Lizardtech Software.  Lizardtech Software has authorized us to
20//C- replace the original DjVu(r) Reference Library notice by the following
21//C- text (see doc/lizard2002.djvu and doc/lizardtech2007.djvu):
22//C-
23//C-  ------------------------------------------------------------------
24//C- | DjVu (r) Reference Library (v. 3.5)
25//C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
26//C- | The DjVu Reference Library is protected by U.S. Pat. No.
27//C- | 6,058,214 and patents pending.
28//C- |
29//C- | This software is subject to, and may be distributed under, the
30//C- | GNU General Public License, either Version 2 of the license,
31//C- | or (at your option) any later version. The license should have
32//C- | accompanied the software or you may obtain a copy of the license
33//C- | from the Free Software Foundation at http://www.fsf.org .
34//C- |
35//C- | The computer code originally released by LizardTech under this
36//C- | license and unmodified by other parties is deemed "the LIZARDTECH
37//C- | ORIGINAL CODE."  Subject to any third party intellectual property
38//C- | claims, LizardTech grants recipient a worldwide, royalty-free,
39//C- | non-exclusive license to make, use, sell, or otherwise dispose of
40//C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the
41//C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU
42//C- | General Public License.   This grant only confers the right to
43//C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to
44//C- | the extent such infringement is reasonably necessary to enable
45//C- | recipient to make, have made, practice, sell, or otherwise dispose
46//C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to
47//C- | any greater extent that may be necessary to utilize further
48//C- | modifications or combinations.
49//C- |
50//C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
51//C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
52//C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
53//C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
54//C- +------------------------------------------------------------------
55//
56// $Id: GMapAreas.h,v 1.9 2007/03/25 20:48:31 leonb Exp $
57// $Name: release_3_5_19 $
58
59#ifndef _GMAPAREAS_H
60#define _GMAPAREAS_H
61#ifdef HAVE_CONFIG_H
62#include "config.h"
63#endif
64#if NEED_GNUG_PRAGMAS
65# pragma interface
66#endif
67
68
69#include "GSmartPointer.h"
70#include "GContainer.h"
71#include "GString.h"
72#include "GRect.h"
73#include "GURL.h"
74
75#ifdef HAVE_NAMESPACES
76namespace DJVU {
77# ifdef NOT_DEFINED // Just to fool emacs c++ mode
78}
79#endif
80#endif
81
82
83/** @name GMapAreas.h
84
85    Files #"GMapAreas.h"# and #"GMapAreas.cpp"# implement base objects
86    used by the plugin to display and manage hyperlinks and highlighted
87    areas inside a \Ref{DjVuImage} page.
88
89    The currently supported areas can be rectangular (\Ref{GMapRect}),
90    elliptical (\Ref{GMapOval}) and polygonal (\Ref{GMapPoly}). Every
91    map area besides the definition of its shape contains information
92    about display style and optional {\bf URL}, which it may refer to.
93    If this {\bf URL} is not empty then the map area will work like a
94    hyperlink.
95
96    The classes also implement some useful functions to ease geometry
97    manipulations
98
99    @memo Definition of base map area classes
100    @author Andrei Erofeev <eaf@geocities.com>
101    @version
102    #$Id: GMapAreas.h,v 1.9 2007/03/25 20:48:31 leonb Exp $# */
103//@{
104
105
106// ---------- GMAPAREA ---------
107
108/** This is the base object for all map areas. It defines some standard
109    interface to access the geometrical properties of the areas and
110    describes the area itsef:
111    \begin{itemize}
112       \item #url# If the optional #URL# is specified, the map area will
113             also work as a hyperlink meaning that if you click it with
114             your mouse pointer, the browser will be advised to load
115             the page referenced by the #URL#.
116       \item #target# Defines where the specified #URL# should be loaded
117       \item #comment# This is a string displayed in a status line or in
118             a popup window when the mouse pointer moves over the hyperlink
119             area
120       \item #border_type#, #border_color# and #border_width# describes
121             how the area border should be drawn
122       \item #area_color# describes how the area should be highlighted.
123    \end{itemize}
124
125    The map areas can be displayed using two different techniques, which
126    can be combined together:
127    \begin{itemize}
128       \item Visible border. The border of a map area can be drawn in several
129             different ways (like #XOR_BORDER# or #SHADOW_IN_BORDER#).
130             It can be made always visible, or appearing only when the
131             mouse pointer moves over the map area.
132       \item Highlighted contents. Contents of rectangular map areas can
133             also be highlighted with some given color.
134    \end{itemize}
135*/
136
137class GMapArea : public GPEnabled
138{
139protected:
140   GMapArea(void);
141public:
142//      // Default creator.
143//   static GP<GMapArea> create(void) {return new GMapArea();}
144
145      /// Virtual destructor.
146   virtual ~GMapArea();
147
148   static const char MAPAREA_TAG [];
149   static const char RECT_TAG [];
150   static const char POLY_TAG [];
151   static const char OVAL_TAG [];
152   static const char NO_BORDER_TAG [];
153   static const char XOR_BORDER_TAG [];
154   static const char SOLID_BORDER_TAG [];
155   static const char SHADOW_IN_BORDER_TAG [];
156   static const char SHADOW_OUT_BORDER_TAG [];
157   static const char SHADOW_EIN_BORDER_TAG [];
158   static const char SHADOW_EOUT_BORDER_TAG [];
159   static const char BORDER_AVIS_TAG [];
160   static const char HILITE_TAG [];
161   static const char URL_TAG [];
162   static const char TARGET_SELF [];
163
164   enum BorderType { NO_BORDER=0, XOR_BORDER=1, SOLID_BORDER=2,
165                     SHADOW_IN_BORDER=3, SHADOW_OUT_BORDER=4,
166                     SHADOW_EIN_BORDER=5, SHADOW_EOUT_BORDER=6 };
167
168   enum Special_Hilite_Color{ NO_HILITE=0xFFFFFFFF, XOR_HILITE=0xFF000000};
169
170   // Enumeration for reporting the type of map area. "MapUnknown" is reported
171   // for objects of type GMapArea (there shouldn't be any).
172   enum MapAreaType { UNKNOWN, RECT, OVAL, POLY };
173
174      /** Optional URL which this map area can be associated with.
175          If it's not empty then clicking this map area with the mouse
176          will make the browser load the HTML page referenced by
177          this #url#.  Note: This may also be a relative URL, so the
178          GURL class is not used. */
179   GUTF8String  url;
180      /** The target for the #URL#. Standard targets are:
181          \begin{itemize}
182             \item #_blank# - Load the link in a new blank window
183             \item #_self# - Load the link into the plugin window
184             \item #_top# - Load the link into the top-level frame
185          \end{itemize} */
186   GUTF8String  target;
187      /** Comment (displayed in a status line or as a popup hint when
188          the mouse pointer moves over the map area */
189   GUTF8String  comment;
190      /** Border type. Defines how the map area border should be drawn
191          \begin{itemize}
192             \item #NO_BORDER# - No border drawn
193             \item #XOR_BORDER# - The border is drawn using XOR method.
194             \item #SOLID_BORDER# - The border is drawn as a solid line
195                   of a given color.
196             \item #SHADOW_IN_BORDER# - Supported for \Ref{GMapRect} only.
197                   The map area area looks as if it was "pushed-in".
198             \item #SHADOW_OUT_BORDER# - The opposite of #SHADOW_OUT_BORDER#
199             \item #SHADOW_EIN_BORDER# - Also for \Ref{GMapRect} only.
200                   Is translated as "shadow etched in"
201             \item #SHADOW_EOUT_BORDER# - The opposite of #SHADOW_EIN_BORDER#.
202          \end{itemize} */
203   BorderType   border_type;
204      /** If #TRUE#, the border will be made always visible. Otherwise
205          it will be drawn when the mouse moves over the map area. */
206   bool         border_always_visible;
207      /// Border color (when relevant) in #0x00RRGGBB# format
208   unsigned long int    border_color;
209      /// Border width in pixels
210   int          border_width;
211      /** Specified a color for highlighting the internal area of the map
212          area. Will work with rectangular map areas only. The color is
213          specified in \#00RRGGBB format. A special value of \#FFFFFFFF disables
214          highlighting and \#FF000000 is for XOR highlighting. */
215   unsigned long int    hilite_color;
216
217      /// Returns 1 if the given point is inside the hyperlink area
218   bool         is_point_inside(int x, int y) const;
219
220      /// Returns xmin of the bounding rectangle
221   int          get_xmin(void) const;
222      /// Returns ymin of the bounding rectangle
223   int          get_ymin(void) const;
224      /** Returns xmax of the bounding rectangle. In other words, if #X# is
225          a coordinate of the last point in the right direction, the
226          function will return #X+1# */
227   int          get_xmax(void) const;
228      /** Returns xmax of the bounding rectangle. In other words, if #Y# is
229          a coordinate of the last point in the top direction, the
230          function will return #Y+1# */
231   int          get_ymax(void) const;
232      /// Returns the hyperlink bounding rectangle
233   GRect        get_bound_rect(void) const;
234      /** Moves the hyperlink along the given vector. Is used by the
235          hyperlinks editor. */
236   void         move(int dx, int dy);
237      /** Resizes the hyperlink to fit new bounding rectangle while
238          keeping the (xmin, ymin) points at rest. */
239   void         resize(int new_width, int new_height);
240      /** Transforms the hyperlink to be within the specified rectangle */
241   void         transform(const GRect & grect);
242      /** Checks if the object is OK. Especially useful with \Ref{GMapPoly}
243          where edges may intersect. If there is a problem it returns a
244          string describing it. */
245   char const * const check_object(void);
246      /** Stores the contents of the hyperlink object in a lisp-like format
247          for saving into #ANTa# chunk (see \Ref{DjVuAnno}) */
248   GUTF8String  print(void);
249
250   virtual GUTF8String get_xmltag(const int height) const=0;
251
252      /// Virtual function returning the shape type.
253   virtual MapAreaType const get_shape_type( void ) const { return UNKNOWN; };
254      /// Virtual function returning the shape name.
255   virtual char const * const   get_shape_name(void) const=0;
256      /// Virtual function generating a copy of this object
257   virtual GP<GMapArea> get_copy(void) const=0;
258      /// Virtual function generating a list of defining coordinates
259      /// (default are the opposite corners of the enclosing rectangle)
260   virtual void get_coords( GList<int> & CoordList ) const;
261   /// Virtual function maps maparea from one area to another using mapper
262   virtual void map(GRectMapper &mapper)=0;
263   /// Virtual function unmaps maparea from one area to another using mapper
264   virtual void unmap(GRectMapper &mapper)=0;
265
266protected:
267   virtual int          gma_get_xmin(void) const=0;
268   virtual int          gma_get_ymin(void) const=0;
269   virtual int          gma_get_xmax(void) const=0;
270   virtual int          gma_get_ymax(void) const=0;
271   virtual void         gma_move(int dx, int dy)=0;
272   virtual void         gma_resize(int new_width, int new_height)=0;
273   virtual void         gma_transform(const GRect & grect)=0;
274   virtual bool         gma_is_point_inside(const int x, const int y) const=0;
275   virtual char const * const   gma_check_object(void) const=0;
276   virtual GUTF8String  gma_print(void)=0;
277   
278   void         clear_bounds(void) { bounds_initialized=0; }
279private:
280   int          xmin, xmax, ymin, ymax;
281   bool         bounds_initialized;
282
283   void         initialize_bounds(void);
284};
285
286// ---------- GMAPRECT ---------
287
288/** Implements rectangular map areas. This is the only kind of map areas
289    supporting #SHADOW_IN_BORDER#, #SHADOW_OUT_BORDER#, #SHADOW_EIN_BORDER#
290    and #SHADOW_EOUT_BORDER# types of border and area highlighting. */
291
292class GMapRect: public GMapArea
293{
294protected:
295   GMapRect(void);
296   GMapRect(const GRect & rect);
297public:
298   /// Default creator.
299   static GP<GMapRect> create(void) {return new GMapRect();}
300   /// Create with the specified GRect.
301   static GP<GMapRect> create(const GRect &rect) {return new GMapRect(rect);}
302
303   virtual ~GMapRect();
304
305      /// Returns the width of the rectangle
306   int          get_width(void) const { return xmax-xmin; }
307      /// Returns the height of the rectangle
308   int          get_height(void) const { return ymax-ymin; }
309
310      /// Changes the #GMapRect#'s geometry
311   GMapRect & operator=(const GRect & rect);
312
313      /// Returns \Ref{GRect} describing the map area's rectangle
314   operator GRect(void);
315   
316   virtual GUTF8String get_xmltag(const int height) const;
317      /// Returns MapRect
318   virtual MapAreaType const get_shape_type( void ) const { return RECT; };
319      /// Returns #"rect"#
320   virtual char const * const   get_shape_name(void) const;
321      /// Returns a copy of the rectangle
322   virtual GP<GMapArea> get_copy(void) const;
323      /// Virtual function maps rectangle from one area to another using mapper
324   virtual void map(GRectMapper &mapper);
325      /// Virtual function unmaps rectangle from one area to another using mapper
326   virtual void unmap(GRectMapper &mapper);
327protected:
328   int                  xmin, ymin, xmax, ymax;
329   virtual int          gma_get_xmin(void) const;
330   virtual int          gma_get_ymin(void) const;
331   virtual int          gma_get_xmax(void) const;
332   virtual int          gma_get_ymax(void) const;
333   virtual void         gma_move(int dx, int dy);
334   virtual void         gma_resize(int new_width, int new_height);
335   virtual void         gma_transform(const GRect & grect);
336   virtual bool         gma_is_point_inside(const int x, const int y) const;
337   virtual char const * const gma_check_object(void) const;
338   virtual GUTF8String  gma_print(void);
339};
340
341// ---------- GMAPPOLY ---------
342
343/** Implements polygonal map areas. The only supported types of border
344    are #NO_BORDER#, #XOR_BORDER# and #SOLID_BORDER#. Its contents can not
345    be highlighted either. It's worth mentioning here that despite its
346    name the polygon may be open, which basically makes it a broken line.
347    This very specific mode is used by the hyperlink editor when creating
348    the polygonal hyperlink. */
349
350class GMapPoly : public GMapArea
351{
352protected:
353   GMapPoly(void);
354   GMapPoly(const int * xx, const int * yy, int points, bool open=false);
355public:
356   /// Default creator
357   static GP<GMapPoly> create(void) {return new GMapPoly();}
358
359   /// Create from specified coordinates.
360   static GP<GMapPoly> create(
361     const int xx[], const int yy[], const int points, const bool open=false)
362   {return new GMapPoly(xx,yy,points,open);}
363
364   /// Virtual destructor.
365   virtual ~GMapPoly();
366
367      /// Returns 1 if side #side# crosses the specified rectangle #rect#.
368   bool         does_side_cross_rect(const GRect & grect, int side);
369
370      /// Returns the number of vertices in the polygon
371   int          get_points_num(void) const;
372
373      /// Returns the number sides in the polygon
374   int          get_sides_num(void) const;
375
376      /// Returns x coordinate of vertex number #i#
377   int          get_x(int i) const;
378   
379      /// Returns y coordinate of vertex number #i#
380   int          get_y(int i) const;
381
382      /// Moves vertex #i# to position (#x#, #y#)
383   void         move_vertex(int i, int x, int y);
384
385      /// Adds a new vertex and returns number of vertices in the polygon
386   int      add_vertex(int x, int y);
387
388      /// Closes the polygon if it is not closed
389   void     close_poly();
390      /// Optimizes the polygon
391   void         optimize_data(void);
392      /// Checks validity of the polygon
393   char const * const   check_data(void);
394
395   virtual GUTF8String get_xmltag(const int height) const;
396      /// Returns MapPoly
397   virtual MapAreaType const get_shape_type( void ) const { return POLY; };
398      /// Returns #"poly"# all the time
399   virtual char const * const   get_shape_name(void) const;
400      /// Returns a copy of the polygon
401   virtual GP<GMapArea> get_copy(void) const;
402      /// Virtual function generating a list of defining coordinates
403   void get_coords( GList<int> & CoordList ) const;
404      /// Virtual function maps polygon from one area to another using mapper
405   virtual void map(GRectMapper &mapper);
406   /// Virtual function unmaps polygon from one area to another using mapper
407   virtual void unmap(GRectMapper &mapper);
408protected:
409   virtual int          gma_get_xmin(void) const;
410   virtual int          gma_get_ymin(void) const;
411   virtual int          gma_get_xmax(void) const;
412   virtual int          gma_get_ymax(void) const;
413   virtual void         gma_move(int dx, int dy);
414   virtual void         gma_resize(int new_width, int new_height);
415   virtual void         gma_transform(const GRect & grect);
416   virtual bool         gma_is_point_inside(const int x, const int y) const;
417   virtual char const * const gma_check_object(void) const;
418   virtual GUTF8String  gma_print(void);
419private:
420   bool         open;
421   int          points, sides;
422   GTArray<int> xx, yy;
423   static int   sign(int x);
424   static bool  is_projection_on_segment(int x, int y, int x1, int y1, int x2, int y2);
425   static bool  do_segments_intersect(int x11, int y11, int x12, int y12,
426                                      int x21, int y21, int x22, int y22);
427   static bool  are_segments_parallel(int x11, int y11, int x12, int y12,
428                                      int x21, int y21, int x22, int y22);
429};
430
431// ---------- GMAPOVAL ---------
432
433/** Implements elliptical map areas. The only supported types of border
434    are #NO_BORDER#, #XOR_BORDER# and #SOLID_BORDER#. Its contents can not
435    be highlighted either. */
436
437class GMapOval: public GMapArea
438{
439protected:
440   GMapOval(void);
441   GMapOval(const GRect & rect);
442public:
443   /// Default creator.
444   static GP<GMapOval> create(void) {return new GMapOval();}
445
446   /// Create from the specified GRect.
447   static GP<GMapOval> create(const GRect &rect) {return new GMapOval(rect);}
448
449   /// Virtual destructor.
450   virtual ~GMapOval();
451
452      /// Returns (xmax-xmin)/2
453   int          get_a(void) const;
454      /// Returns (ymax-ymin)/2
455   int          get_b(void) const;
456      /// Returns the lesser of \Ref{get_a}() and \Ref{get_b}()
457   int          get_rmin(void) const;
458      /// Returns the greater of \Ref{get_a}() and \Ref{get_b}()
459   int          get_rmax(void) const;
460
461   virtual GUTF8String get_xmltag(const int height) const;
462      /// Returns MapOval
463   virtual MapAreaType const get_shape_type( void ) const { return OVAL; };
464      /// Returns #"oval"#
465   virtual char const * const get_shape_name(void) const;
466      /// Returns a copy of the oval
467   virtual GP<GMapArea> get_copy(void) const;
468      /// Virtual function maps oval from one area to another using mapper
469   virtual void map(GRectMapper &mapper);
470      /// Virtual function unmaps oval from one area to another using mapper
471   virtual void unmap(GRectMapper &mapper);
472protected:
473   virtual int          gma_get_xmin(void) const;
474   virtual int          gma_get_ymin(void) const;
475   virtual int          gma_get_xmax(void) const;
476   virtual int          gma_get_ymax(void) const;
477   virtual void         gma_move(int dx, int dy);
478   virtual void         gma_resize(int new_width, int new_height);
479   virtual void         gma_transform(const GRect & grect);
480   virtual bool         gma_is_point_inside(const int x, const int y) const;
481   virtual char const * const   gma_check_object(void) const;
482   virtual GUTF8String  gma_print(void);
483private:
484   int          rmax, rmin;
485   int          a, b;
486   int          xf1, yf1, xf2, yf2;
487   int          xmin, ymin, xmax, ymax;
488   
489   void         initialize(void);
490};
491
492inline
493GMapRect::operator GRect(void)
494{
495  return GRect(xmin, ymin, xmax-xmin, ymax-ymin);
496}
497
498inline int
499GMapRect::gma_get_xmin(void) const { return xmin; }
500
501inline int
502GMapRect::gma_get_ymin(void) const { return ymin; }
503
504inline int
505GMapRect::gma_get_xmax(void) const { return xmax; }
506
507inline int
508GMapRect::gma_get_ymax(void) const { return ymax; }
509
510inline char const * const
511GMapRect::gma_check_object(void)  const{ return ""; }
512
513inline char const * const 
514GMapRect::get_shape_name(void) const { return RECT_TAG; }
515
516inline int
517GMapPoly::get_points_num(void) const { return points; }
518
519inline int
520GMapPoly::get_sides_num(void) const { return sides; }
521
522inline int
523GMapPoly::get_x(int i) const { return xx[i]; }
524
525inline int
526GMapPoly::get_y(int i) const { return yy[i]; }
527
528inline char const * const
529GMapPoly::get_shape_name(void) const { return POLY_TAG; }
530
531inline int
532GMapOval::get_a(void) const { return a; }
533
534inline int
535GMapOval::get_b(void) const { return b; }
536
537inline int
538GMapOval::get_rmin(void) const { return rmin; }
539
540inline int
541GMapOval::get_rmax(void) const { return rmax; }
542
543inline int
544GMapOval::gma_get_xmin(void) const { return xmin; }
545
546inline int
547GMapOval::gma_get_ymin(void) const { return ymin; }
548
549inline int
550GMapOval::gma_get_xmax(void) const { return xmax; }
551
552inline int
553GMapOval::gma_get_ymax(void) const { return ymax; }
554
555inline char const * const
556GMapOval::get_shape_name(void) const { return OVAL_TAG; }
557
558//@}
559
560
561#ifdef HAVE_NAMESPACES
562}
563# ifndef NOT_USING_DJVU_NAMESPACE
564using namespace DJVU;
565# endif
566#endif
567#endif
Note: See TracBrowser for help on using the repository browser.