summaryrefslogtreecommitdiffstats
path: root/kviewshell/plugins/djvu/libdjvu/GMapAreas.cpp
blob: ef364e7984119ce95d56096d178fae66ec0c4ef2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
//C-  -*- C++ -*-
//C- -------------------------------------------------------------------
//C- DjVuLibre-3.5
//C- Copyright (c) 2002  Leon Bottou and Yann Le Cun.
//C- Copyright (c) 2001  AT&T
//C-
//C- This software is subject to, and may be distributed under, the
//C- GNU General Public License, Version 2. The license should have
//C- accompanied the software or you may obtain a copy of the license
//C- from the Free Software Foundation at http://www.fsf.org .
//C-
//C- This program is distributed in the hope that it will be useful,
//C- but WITHOUT ANY WARRANTY; without even the implied warranty of
//C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//C- GNU General Public License for more details.
//C- 
//C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library
//C- distributed by Lizardtech Software.  On July 19th 2002, Lizardtech 
//C- Software authorized us to replace the original DjVu(r) Reference 
//C- Library notice by the following text (see doc/lizard2002.djvu):
//C-
//C-  ------------------------------------------------------------------
//C- | DjVu (r) Reference Library (v. 3.5)
//C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
//C- | The DjVu Reference Library is protected by U.S. Pat. No.
//C- | 6,058,214 and patents pending.
//C- |
//C- | This software is subject to, and may be distributed under, the
//C- | GNU General Public License, Version 2. The license should have
//C- | accompanied the software or you may obtain a copy of the license
//C- | from the Free Software Foundation at http://www.fsf.org .
//C- |
//C- | The computer code originally released by LizardTech under this
//C- | license and unmodified by other parties is deemed "the LIZARDTECH
//C- | ORIGINAL CODE."  Subject to any third party intellectual property
//C- | claims, LizardTech grants recipient a worldwide, royalty-free, 
//C- | non-exclusive license to make, use, sell, or otherwise dispose of 
//C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the 
//C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU 
//C- | General Public License.   This grant only confers the right to 
//C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to 
//C- | the extent such infringement is reasonably necessary to enable 
//C- | recipient to make, have made, practice, sell, or otherwise dispose 
//C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to 
//C- | any greater extent that may be necessary to utilize further 
//C- | modifications or combinations.
//C- |
//C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
//C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
//C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
//C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
//C- +------------------------------------------------------------------
// 
// $Id: GMapAreas.cpp,v 1.9 2004/05/05 15:12:42 leonb Exp $
// $Name: release_3_5_15 $

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#if NEED_GNUG_PRAGMAS
# pragma implementation
#endif

#include "GMapAreas.h"
#include "GException.h"
#include "debug.h"

#include <math.h>
#include <stdio.h>


#ifdef HAVE_NAMESPACES
namespace DJVU {
# ifdef NOT_DEFINED // Just to fool emacs c++ mode
}
#endif
#endif


/****************************************************************************
***************************** GMapArea definition ***************************
****************************************************************************/

const char GMapArea::MAPAREA_TAG[] = 		"maparea";
const char GMapArea::RECT_TAG[] = 		"rect";
const char GMapArea::POLY_TAG[] = 		"poly";
const char GMapArea::OVAL_TAG[] = 		"oval";
const char GMapArea::NO_BORDER_TAG[] = 		"none";
const char GMapArea::XOR_BORDER_TAG[] = 	"xor";
const char GMapArea::SOLID_BORDER_TAG[] = 	"border";
const char GMapArea::SHADOW_IN_BORDER_TAG[] = 	"shadow_in";
const char GMapArea::SHADOW_OUT_BORDER_TAG[] = 	"shadow_out";
const char GMapArea::SHADOW_EIN_BORDER_TAG[] = 	"shadow_ein";
const char GMapArea::SHADOW_EOUT_BORDER_TAG[] = "shadow_eout";
const char GMapArea::BORDER_AVIS_TAG[] = 	"border_avis";
const char GMapArea::HILITE_TAG[] = 		"hilite";
const char GMapArea::URL_TAG[] = 		"url";
const char GMapArea::TARGET_SELF[] = 		"_self";
static const char zero_width[] = ERR_MSG("GMapAreas.zero_width");
static const char zero_height[] = ERR_MSG("GMapAreas.zero_height");
static const char width_1[] = ERR_MSG("GMapAreas.width_1");
static const char width_3_32 [] = ERR_MSG("GMapAreas.width_3-32");
static const char error_poly_border [] = ERR_MSG("GMapAreas.poly_border");
static const char error_poly_hilite [] = ERR_MSG("GMapAreas.poly_hilite");
static const char error_oval_border [] = ERR_MSG("GMapAreas.oval_border");
static const char error_oval_hilite [] = ERR_MSG("GMapAreas.oval_hilite");
static const char error_too_few_points [] = ERR_MSG("GMapAreas.too_few_points");
static const char error_intersect [] = ERR_MSG("GMapAreas.intersect");

GMapArea::~GMapArea() {}

GMapRect::~GMapRect() {}

GMapPoly::~GMapPoly() {}

GMapOval::~GMapOval() {}

void
GMapArea::initialize_bounds(void)
{
   xmin=gma_get_xmin();
   xmax=gma_get_xmax();
   ymin=gma_get_ymin();
   ymax=gma_get_ymax();
   bounds_initialized=true;
}

int
GMapArea::get_xmin(void) const
{
   if (!bounds_initialized)
     const_cast<GMapArea *>(this)->initialize_bounds();
   return xmin;
}

int
GMapArea::get_ymin(void) const
{
   if (!bounds_initialized)
     const_cast<GMapArea *>(this)->initialize_bounds();
   return ymin;
}

int
GMapArea::get_xmax(void) const
{
   if (!bounds_initialized)
     const_cast<GMapArea *>(this)->initialize_bounds();
   return xmax;
}

int
GMapArea::get_ymax(void) const
{
   if (!bounds_initialized)
     const_cast<GMapArea *>(this)->initialize_bounds();
   return ymax;
}

GRect
GMapArea::get_bound_rect(void) const
{
   return GRect(get_xmin(), get_ymin(), get_xmax()-get_xmin(),
		get_ymax()-get_ymin());
}

void
GMapArea::move(int dx, int dy)
{
   if (dx || dy)
   {
     if (bounds_initialized)
     {
        xmin+=dx;
        ymin+=dy;
        xmax+=dx;
        ymax+=dy;
     }
     gma_move(dx, dy);
   }
}

void
GMapArea::resize(int new_width, int new_height)
{
   if (get_xmax()-get_xmin()!=new_width ||
       get_ymax()-get_ymin()!=new_height)
   {
     gma_resize(new_width, new_height);
     bounds_initialized=false;
   }
}

void
GMapArea::transform(const GRect & grect)
{
   if (grect.xmin!=get_xmin() || grect.ymin!=get_ymin() ||
       grect.xmax!=get_xmax() || grect.ymax!=get_ymax())
   {
     gma_transform(grect);
     bounds_initialized=false;
   }
}

char const * const
GMapArea::check_object(void)
{
   char const *retval;
   if (get_xmax()==get_xmin())
   {
     retval=zero_width;
   }
   else if (get_ymax()==get_ymin())
   {
     retval=zero_height;
   }
   else if ((border_type==XOR_BORDER ||
       border_type==SOLID_BORDER) && border_width!=1)
   {
     retval=width_1;
   }
   else if ((border_type==SHADOW_IN_BORDER ||
       border_type==SHADOW_OUT_BORDER ||
       border_type==SHADOW_EIN_BORDER ||
       border_type==SHADOW_EOUT_BORDER)&&
       (border_width<3 || border_width>32))
   {
     retval=width_3_32;
   }else
   {
     retval=gma_check_object();
   }
   return retval;
}

bool
GMapArea::is_point_inside(int x, int y) const
{
   if (!bounds_initialized)
     const_cast<GMapArea *>(this)->initialize_bounds();
   return (x>=xmin && x<xmax && y>=ymin && y<ymax) ?
	      gma_is_point_inside(x, y) : false;
}

GUTF8String
GMapArea::print(void)
{
      // Make this hard check to make sure, that *no* illegal GMapArea
      // can be stored into a file.
   const char * const errors=check_object();
   if (errors[0])
   {
     G_THROW(errors);
   }
   
   int i;
   GUTF8String tmp;
   GUTF8String url1, target1, comment1;
   const GUTF8String url_str=url;
   for(i=0;i<(int) url_str.length();i++)
   {
      char ch=url_str[i];
      if (ch=='"')
        url1+='\\';
      url1+=ch;
   }
   for(i=0;i<(int) target.length();i++)
   {
      char ch=target[i];
      if (ch=='"')
        target1+='\\';
      target1+=ch;
   }
   for(i=0;i<(int) comment.length();i++)
   {
      char ch=comment[i];
      if (ch=='"')
        comment1+='\\';
      comment1+=ch;
   }
   
   GUTF8String border_color_str;
   border_color_str.format("#%02X%02X%02X",
	   (border_color & 0xff0000) >> 16,
	   (border_color & 0xff00) >> 8,
	   (border_color & 0xff));

   static const GUTF8String left('(');
   static const GUTF8String right(')');
   static const GUTF8String space(' ');
   static const GUTF8String quote('"');
   GUTF8String border_type_str;
   switch(border_type)
   {
      case NO_BORDER:
        border_type_str=left+NO_BORDER_TAG+right;
        break;
      case XOR_BORDER:
        border_type_str=left+XOR_BORDER_TAG+right;
        break;
      case SOLID_BORDER:
        border_type_str=left+SOLID_BORDER_TAG+space+border_color_str+right;
        break;
      case SHADOW_IN_BORDER:
        border_type_str=left+SHADOW_IN_BORDER_TAG+space+GUTF8String(border_width)+right;
        break;
      case SHADOW_OUT_BORDER:
        border_type_str=left+SHADOW_OUT_BORDER_TAG+space+GUTF8String(border_width)+right;
        break;
      case SHADOW_EIN_BORDER:
        border_type_str=left+SHADOW_EIN_BORDER_TAG+space+GUTF8String(border_width)+right;
        break;
      case SHADOW_EOUT_BORDER:
        border_type_str=left+SHADOW_EOUT_BORDER_TAG+space+GUTF8String(border_width)+right;
        break;
      default:
        border_type_str=left+XOR_BORDER_TAG+right;
        break;
   }

   GUTF8String hilite_str;
   if (hilite_color!=0xffffffff)
   {
      hilite_str.format("(%s #%02X%02X%02X)",
	      HILITE_TAG, (hilite_color & 0xff0000) >> 16,
	      (hilite_color & 0xff00) >> 8,
	      (hilite_color & 0xff));
   }
   
   GUTF8String URL;
   if (target1==TARGET_SELF)
   {
      URL=quote+url1+quote;
   }else
   {
      URL=left+URL_TAG+space+quote+url1+quote+space+quote+target1+quote+right;
   }

   GUTF8String total=left+MAPAREA_TAG+space+URL+space+quote+comment1+quote+space+gma_print()+border_type_str;
   if (border_always_visible)
     total+=space+left+BORDER_AVIS_TAG+right;
   if ( hilite_str.length() > 0 )
     total+=space+hilite_str;
   total+=right;
   return total;
}

/*
void 
GMapArea::map(GRectMapper &mapper)
{
    get_bound_rect();
    GRect rect = GRect(xmin, ymin, xmax, ymax);
    mapper.map(rect);
    xmin = rect.xmin;
    ymin = rect.ymin;
    xmax = rect.xmax;
    ymax = rect.ymax;
    clear_bounds();
}
void 
GMapArea::unmap(GRectMapper &mapper)
{
    get_bound_rect();
    GRect rect = GRect(xmin, ymin, xmax, ymax);
    mapper.unmap(rect);
    xmin = rect.xmin;
    ymin = rect.ymin;
    xmax = rect.xmax;
    ymax = rect.ymax;
    clear_bounds();
}
*/


/// Virtual function generating a list of defining coordinates
/// (default are the opposite corners of the enclosing rectangle)
void GMapArea::get_coords( GList<int> & CoordList ) const
{
  CoordList.append( get_xmin() );
  CoordList.append( get_ymin() );
  CoordList.append( get_xmax() );
  CoordList.append( get_ymax() );
}


/****************************************************************************
**************************** GMapRect definition ****************************
****************************************************************************/

void
GMapRect::gma_resize(int new_width, int new_height)
{
   xmax=xmin+new_width;
   ymax=ymin+new_height;
}

void
GMapRect::gma_transform(const GRect & grect)
{
   xmin=grect.xmin; ymin=grect.ymin;
   xmax=grect.xmax; ymax=grect.ymax;
}

GUTF8String
GMapRect::gma_print(void)
{
   GUTF8String buffer;
   return buffer.format("(%s %d %d %d %d) ",
	   RECT_TAG, xmin, ymin, xmax-xmin, ymax-ymin);
}

void 
GMapRect::map(GRectMapper &mapper)
{
    get_bound_rect();
    GRect rect;
    rect.xmin = xmin;
    rect.xmax = xmax;
    rect.ymin = ymin;
    rect.ymax = ymax;
    mapper.map(rect);
    xmin = rect.xmin;
    ymin = rect.ymin;
    xmax = rect.xmax;
    ymax = rect.ymax;
    clear_bounds();
}
void 
GMapRect::unmap(GRectMapper &mapper)
{
    get_bound_rect();
    GRect rect;
    rect.xmin = xmin;
    rect.xmax = xmax;
    rect.ymin = ymin;
    rect.ymax = ymax;
    mapper.unmap(rect);
    xmin = rect.xmin;
    ymin = rect.ymin;
    xmax = rect.xmax;
    ymax = rect.ymax;
    clear_bounds();
}

/****************************************************************************
**************************** GMapPoly definition ****************************
****************************************************************************/

inline int
GMapPoly::sign(int x) { return x<0 ? -1 : x>0 ? 1 : 0; }

bool
GMapPoly::does_side_cross_rect(const GRect & grect, int side)
{
   int x1=xx[side], x2=xx[(side+1)%points];
   int y1=yy[side], y2=yy[(side+1)%points];
   int xmin=x1<x2 ? x1 : x2;
   int ymin=y1<y2 ? y1 : y2;
   int xmax=x1+x2-xmin;
   int ymax=y1+y2-ymin;

   if (xmax<grect.xmin || xmin>grect.xmax ||
       ymax<grect.ymin || ymin>grect.ymax) return false;

   return
      x1>=grect.xmin && x1<=grect.xmax && y1>=grect.ymin && y1<=grect.ymax ||
      x2>=grect.xmin && x2<=grect.xmax && y2>=grect.ymin && y2<=grect.ymax ||
      do_segments_intersect(grect.xmin, grect.ymin, grect.xmax, grect.ymax,
			    x1, y1, x2, y2) ||
      do_segments_intersect(grect.xmax, grect.ymin, grect.xmin, grect.ymax,
			    x1, y1, x2, y2);
}

bool
GMapPoly::is_projection_on_segment(int x, int y, int x1, int y1, int x2, int y2)
{
   int res1=(x-x1)*(x2-x1)+(y-y1)*(y2-y1);
   int res2=(x-x2)*(x2-x1)+(y-y2)*(y2-y1);
   return sign(res1)*sign(res2)<=0;
}

bool
GMapPoly::do_segments_intersect(int x11, int y11, int x12, int y12,
				int x21, int y21, int x22, int y22)
{
   int res11=(x11-x21)*(y22-y21)-(y11-y21)*(x22-x21);
   int res12=(x12-x21)*(y22-y21)-(y12-y21)*(x22-x21);
   int res21=(x21-x11)*(y12-y11)-(y21-y11)*(x12-x11);
   int res22=(x22-x11)*(y12-y11)-(y22-y11)*(x12-x11);
   if (!res11 && !res12)
   {
      // Segments are on the same line
      return
	 is_projection_on_segment(x11, y11, x21, y21, x22, y22) ||
	 is_projection_on_segment(x12, y12, x21, y21, x22, y22) ||
	 is_projection_on_segment(x21, y21, x11, y11, x12, y12) ||
	 is_projection_on_segment(x22, y22, x11, y11, x12, y12);
   }
   int sign1=sign(res11)*sign(res12);
   int sign2=sign(res21)*sign(res22);
   return sign1<=0 && sign2<=0;
}

bool
GMapPoly::are_segments_parallel(int x11, int y11, int x12, int y12,
				int x21, int y21, int x22, int y22)
{
   return (x12-x11)*(y22-y21)-(y12-y11)*(x22-x21)==0;
}

char const * const
GMapPoly::check_data(void)
{
  if (open && points<2 || !open && points<3) 
    return error_too_few_points;
  for(int i=0;i<sides;i++)
  {
    for(int j=i+2;j<sides;j++)
    {
      if (i != (j+1)%points )
      {
        if (do_segments_intersect(xx[i], yy[i], xx[i+1], yy[i+1],
				      xx[j], yy[j], xx[(j+1)%points], yy[(j+1)%points]))
        {
          return error_intersect;
        }
      }
    }
  }
  return "";
}

void
GMapPoly::optimize_data(void)
{
   // Removing segments of length zero
   int i;
   for(i=0;i<sides;i++)
   {
      while(xx[i]==xx[(i+1)%points] && yy[i]==yy[(i+1)%points])
      {
	 for(int k=(i+1)%points;k<points-1;k++)
	 {
	    xx[k]=xx[k+1]; yy[k]=yy[k+1];
	 }
	 points--; sides--;
	 if (!points) return;
      }
   }
   // Concatenating consequitive parallel segments
   for(i=0;i<sides;i++)
   {
      while((open && i+1<sides || !open) &&
	    are_segments_parallel(xx[i], yy[i],
				  xx[(i+1)%points], yy[(i+1)%points],
				  xx[(i+1)%points], yy[(i+1)%points],
				  xx[(i+2)%points], yy[(i+2)%points]))
      {
	 for(int k=(i+1)%points;k<points-1;k++)
	 {
	    xx[k]=xx[k+1]; yy[k]=yy[k+1];
	 }
	 points--; sides--;
	 if (!points) return;
      }
   }
}

bool
GMapPoly::gma_is_point_inside(const int xin, const int yin) const
{
   if (open)
     return false;
   
   int xfar=get_xmax()+(get_xmax()-get_xmin());
   
   int intersections=0;
   for(int i=0;i<points;i++)
   {
      int res1=yy[i]-yin;
      if (!res1) continue;
      int res2, isaved=i;
      while(!(res2=yy[(i+1)%points]-yin)) i++;
      if (isaved!=i)
      {
	 // Some points fell exactly on the line
	 if ((xx[(isaved+1)%points]-xin)*
	     (xx[i%points]-xin)<=0)
	 {
	    // Test point is exactly on the boundary
	    return true;
	 }
      }
      if (res1<0 && res2>0 || res1>0 && res2<0)
      {
	 int x1=xx[i%points], y1=yy[i%points];
	 int x2=xx[(i+1)%points], y2=yy[(i+1)%points];
	 int _res1=(xin-x1)*(y2-y1)-(yin-y1)*(x2-x1);
	 int _res2=(xfar-x1)*(y2-y1)-(yin-y1)*(x2-x1);
	 if (!_res1 || !_res2)
	 {
	    // The point is on this boundary
	    return true;
	 }
	 if (sign(_res1)*sign(_res2)<0) intersections++;
      }
   }
   return (intersections % 2)!=0;
}

int
GMapPoly::gma_get_xmin(void) const
{
   int x=xx[0];
   for(int i=1;i<points;i++)
      if (x>xx[i]) x=xx[i];
   return x;
}

int
GMapPoly::gma_get_xmax(void) const
{
   int x=xx[0];
   for(int i=1;i<points;i++)
      if (x<xx[i]) x=xx[i];
   return x+1;
}

int
GMapPoly::gma_get_ymin(void) const
{
   int y=yy[0];
   for(int i=1;i<points;i++)
      if (y>yy[i]) y=yy[i];
   return y;
}

int
GMapPoly::gma_get_ymax(void) const
{
   int y=yy[0];
   for(int i=1;i<points;i++)
      if (y<yy[i]) y=yy[i];
   return y+1;
}

void
GMapPoly::gma_move(int dx, int dy)
{
   for(int i=0;i<points;i++)
   {
      xx[i]+=dx; yy[i]+=dy;
   }
}

void
GMapPoly::gma_resize(int new_width, int new_height)
{
   int width=get_xmax()-get_xmin();
   int height=get_ymax()-get_ymin();
   int xmin=get_xmin(), ymin=get_ymin();
   for(int i=0;i<points;i++)
   {
      xx[i]=xmin+(xx[i]-xmin)*new_width/width;
      yy[i]=ymin+(yy[i]-ymin)*new_height/height;
   }
}

void
GMapPoly::gma_transform(const GRect & grect)
{
   int width=get_xmax()-get_xmin();
   int height=get_ymax()-get_ymin();
   int xmin=get_xmin(), ymin=get_ymin();
   for(int i=0;i<points;i++)
   {
      xx[i]=grect.xmin+(xx[i]-xmin)*grect.width()/width;
      yy[i]=grect.ymin+(yy[i]-ymin)*grect.height()/height;
   }
}

char const * const
GMapPoly::gma_check_object(void) const
{
   const char * str;
   str=(border_type!=NO_BORDER &&
        border_type!=SOLID_BORDER &&
        border_type!=XOR_BORDER) ? error_poly_border:
       ((hilite_color!=0xffffffff) ? error_poly_hilite:"");
   return str;
}

GMapPoly::GMapPoly(const int * _xx, const int * _yy, int _points, bool _open) :
   open(_open), points(_points)
{
   sides=points-(open!=0);
   
   xx.resize(points-1); yy.resize(points-1);
   for(int i=0;i<points;i++)
   {
      xx[i]=_xx[i]; yy[i]=_yy[i];
   }
   optimize_data();
   char const * const res=check_data();
   if (res[0])
     G_THROW(res);
}

int      
GMapPoly::add_vertex(int x, int y)
{
    points++;
    sides=points-(open!=0);

    xx.resize(points-1); yy.resize(points-1);
    xx[points-1] = x;
    yy[points-1] = y;

    return points;
}

void
GMapPoly::close_poly()
{
    open = false;
    sides=points;
}

GUTF8String
GMapPoly::gma_print(void)
{
   static const GUTF8String space(' ');
   GUTF8String res=GUTF8String('(')+POLY_TAG+space;
   for(int i=0;i<points;i++)
   {
      GUTF8String buffer;
      res+=buffer.format("%d %d ", xx[i], yy[i]);
   }
   res.setat(res.length()-1, ')');
   res+=space;
   return res;
}

/// Virtual function generating a list of defining coordinates
void GMapPoly::get_coords( GList<int> & CoordList ) const
{
  for(int i = 0 ; i < points ; i++)
  {
    CoordList.append( xx[i] );
    CoordList.append( yy[i] );
  }
}

void 
GMapPoly::map(GRectMapper &mapper)
{
    get_bound_rect();
    for(int i=0; i<points; i++)
    {
        mapper.map(xx[i], yy[i]);
    }
    clear_bounds();
}

void 
GMapPoly::unmap(GRectMapper &mapper)
{
    get_bound_rect();
    for(int i=0; i<points; i++)
    {
        mapper.unmap(xx[i], yy[i]);
    }
    clear_bounds();
}



/****************************************************************************
**************************** GMapOval definition ****************************
****************************************************************************/

void
GMapOval::gma_resize(int new_width, int new_height)
{
   xmax=xmin+new_width;
   ymax=ymin+new_height;
   initialize();
}

void
GMapOval::gma_transform(const GRect & grect)
{
   xmin=grect.xmin; ymin=grect.ymin;
   xmax=grect.xmax; ymax=grect.ymax;
   initialize();
}

bool
GMapOval::gma_is_point_inside(const int x, const int y) const
{
   return
      sqrt((double)((x-xf1)*(x-xf1)+(y-yf1)*(y-yf1))) +
      sqrt((double)((x-xf2)*(x-xf2)+(y-yf2)*(y-yf2))) <= 2*rmax;
}

char const * const
GMapOval::gma_check_object(void) const
{
   return (border_type!=NO_BORDER &&
       border_type!=SOLID_BORDER &&
       border_type!=XOR_BORDER)?error_oval_border:
      ((hilite_color!=0xffffffff) ? error_oval_hilite:"");
}

void
GMapOval::initialize(void)
{
   int xc=(xmax+xmin)/2;
   int yc=(ymax+ymin)/2;
   int f;
   
   a=(xmax-xmin)/2;
   b=(ymax-ymin)/2;
   if (a>b)
   {
      rmin=b; rmax=a;
      f=(int) sqrt((double)(rmax*rmax-rmin*rmin));
      xf1=xc+f; xf2=xc-f; yf1=yf2=yc;
   } else
   {
      rmin=a; rmax=b;
      f=(int) sqrt((double)(rmax*rmax-rmin*rmin));
      yf1=yc+f; yf2=yc-f; xf1=xf2=xc;
   }
}

GMapOval::GMapOval(const GRect & rect) : xmin(rect.xmin), ymin(rect.ymin),
   xmax(rect.xmax), ymax(rect.ymax)
{
   initialize();
}

GUTF8String
GMapOval::gma_print(void)
{
   GUTF8String buffer;
   return buffer.format("(%s %d %d %d %d) ",
	   OVAL_TAG, xmin, ymin, xmax-xmin, ymax-ymin);
}

void 
GMapOval::map(GRectMapper &mapper)
{
    get_bound_rect();
    GRect rect;
    rect.xmin = xmin;
    rect.xmax = xmax;
    rect.ymin = ymin;
    rect.ymax = ymax;
    mapper.map(rect);
    xmin = rect.xmin;
    ymin = rect.ymin;
    xmax = rect.xmax;
    ymax = rect.ymax;
    clear_bounds();
    initialize();
}

void 
GMapOval::unmap(GRectMapper &mapper)
{
    get_bound_rect();
    GRect rect;
    rect.xmin = xmin;
    rect.xmax = xmax;
    rect.ymin = ymin;
    rect.ymax = ymax;
    mapper.unmap(rect);
    xmin = rect.xmin;
    ymin = rect.ymin;
    xmax = rect.xmax;
    ymax = rect.ymax;
    clear_bounds();
    initialize();
}

GMapArea::GMapArea(void) : target("_self"), border_type(NO_BORDER),
   border_always_visible(false), border_color(0xff), border_width(1),
   hilite_color(0xffffffff), bounds_initialized(0) {}

GMapRect::GMapRect(void) : xmin(0), ymin(0), xmax(0), ymax(0) {}

GMapRect::GMapRect(const GRect & rect) : xmin(rect.xmin), ymin(rect.ymin),
   xmax(rect.xmax), ymax(rect.ymax) {}

GMapRect &
GMapRect::operator=(const GRect & rect)
{
   xmin=rect.xmin;
   xmax=rect.xmax;
   ymin=rect.ymin;
   ymax=rect.ymax;
   return *this;
}

void
GMapRect::gma_move(int dx, int dy)
{
   xmin+=dx;
   xmax+=dx;
   ymin+=dy;
   ymax+=dy;
}

bool
GMapRect::gma_is_point_inside(const int x, const int y) const
{
   return (x>=xmin)&&(x<xmax)&&(y>=ymin)&&(y<ymax);
}

GP<GMapArea>
GMapRect::get_copy(void) const { return new GMapRect(*this); }

GMapPoly::GMapPoly(void) : points(0), sides(0) {}

void
GMapPoly::move_vertex(int i, int x, int y)
{
   xx[i]=x; yy[i]=y;
   clear_bounds();
}

GP<GMapArea>
GMapPoly::get_copy(void) const { return new GMapPoly(*this); }

GMapOval::GMapOval(void) : xmin(0), ymin(0), xmax(0), ymax(0) {}

void
GMapOval::gma_move(int dx, int dy)
{
   xmin+=dx; xmax+=dx; ymin+=dy; ymax+=dy;
   xf1+=dx; yf1+=dy; xf2+=dx; yf2+=dy;
}

GP<GMapArea>
GMapOval::get_copy(void) const
{
  return new GMapOval(*this);
}

static GUTF8String
GMapArea2xmltag(const GMapArea &area,const GUTF8String &coords)
{
  GUTF8String retval("<AREA coords=\""
    +coords+"\" tqshape=\""+area.get_tqshape_name()+"\" "
    +"alt=\""+area.comment.toEscaped()+"\" ");
  if(area.url.length())
  {
    retval+="href=\""+area.url+"\" ";
  }else
  {
    retval+="nohref=\"nohref\" ";
  }
  if(area.target.length())
  {
    retval+="target=\""+area.target.toEscaped()+"\" ";
  }
  //  highlight
  if( area.hilite_color != GMapArea::NO_HILITE &&
      area.hilite_color != GMapArea::XOR_HILITE )
  {
    retval+=GUTF8String().format( "highlight=\"#%06X\" ", area.hilite_color );
  }
  const char *b_type="none";
  switch( area.border_type )
  {
  case GMapArea::NO_BORDER:
    b_type = "none";
    break;
  case GMapArea::XOR_BORDER:
    b_type = "xor";
    break;
  case GMapArea::SOLID_BORDER:
    b_type = "solid";
    break;
  case GMapArea::SHADOW_IN_BORDER:
    b_type = "shadowin";
    break;
  case GMapArea::SHADOW_OUT_BORDER:
    b_type = "shadowout";
    break;
  case GMapArea::SHADOW_EIN_BORDER:
    b_type = "etchedin";
    break;
  case GMapArea::SHADOW_EOUT_BORDER:
    b_type = "etchedout";
    break;
  }
  retval=retval+"bordertype=\""+b_type+"\" ";
  if( area.border_type != GMapArea::NO_BORDER)
  {
    retval+="bordercolor=\""+GUTF8String().format("#%06X",area.border_color)
      +"\" border=\""+GUTF8String(area.border_width)+"\" ";
  }
  if(area.border_always_visible )
    retval=retval+"visible=\"visible\" ";
  return retval+"/>\n";
}

GUTF8String
GMapRect::get_xmltag(const int height) const
{
  return GMapArea2xmltag( *this, GUTF8String(get_xmin())
    +","+GUTF8String(height-1-get_ymax())
    +","+GUTF8String(get_xmax())
    +","+GUTF8String(height-1-get_ymin()));
#if 0
  GUTF8String retval;
  return retval;
#endif
}

GUTF8String
GMapOval::get_xmltag(const int height) const
{ 
  return GMapArea2xmltag( *this, GUTF8String(get_xmin())
    +","+GUTF8String(height-1-get_ymax())
    +","+GUTF8String(get_xmax())
    +","+GUTF8String(height-1-get_ymin()));
#if 0
  GUTF8String retval;
  return retval;
#endif
}

GUTF8String
GMapPoly::get_xmltag(const int height) const
{
  GList<int> CoordList;
  get_coords(CoordList);
  GPosition pos=CoordList;
  GUTF8String retval;
  if(pos)
  {
    GUTF8String coords(CoordList[pos]);
    while(++pos)
    {
      coords+=","+GUTF8String(height-1-CoordList[pos]);
      if(! ++pos)
        break;
      coords+=","+GUTF8String(CoordList[pos]);
    }
    retval=GMapArea2xmltag( *this, coords);
  }
  return retval;
}


#ifdef HAVE_NAMESPACES
}
# ifndef NOT_USING_DJVU_NAMESPACE
using namespace DJVU;
# endif
#endif