692e4914eb9da3be5c7453cf82ee90e880b108b2
[navit-package] / navit / graphics.c
1 /**
2  * Navit, a modular navigation system.
3  * Copyright (C) 2005-2008 Navit Team
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * version 2 as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA  02110-1301, USA.
18  */
19
20 //##############################################################################################################
21 //#
22 //# File: graphics.c
23 //# Description: 
24 //# Comment: 
25 //# Authors: Martin Schaller (04/2008)
26 //#
27 //##############################################################################################################
28
29 #include <glib.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <math.h>
33 #include "debug.h"
34 #include "string.h"
35 #include "draw_info.h"
36 #include "point.h"
37 #include "graphics.h"
38 #include "projection.h"
39 #include "item.h"
40 #include "map.h"
41 #include "coord.h"
42 #include "transform.h"
43 #include "plugin.h"
44 #include "profile.h"
45 #include "mapset.h"
46 #include "layout.h"
47 #include "route.h"
48 #include "util.h"
49 #include "callback.h"
50 #include "file.h"
51
52 static char *navit_sharedir;
53
54 //##############################################################################################################
55 //# Description: 
56 //# Comment: 
57 //# Authors: Martin Schaller (04/2008)
58 //##############################################################################################################
59 struct graphics
60 {
61         struct graphics_priv *priv;
62         struct graphics_methods meth;
63         char *default_font;
64         struct graphics_font *font[16];
65         struct graphics_gc *gc[3];
66         struct attr **attrs;
67         struct callback_list *cbl;
68         int ready;
69         struct point_rect r;
70 };
71
72
73 struct displaylist {
74         GHashTable *dl;
75 };
76
77
78 void
79 graphics_set_rect(struct graphics *gra, struct point_rect *pr)
80 {
81         gra->r=*pr;
82 }
83
84 /**
85  * Creates a new graphics object
86  * attr type required
87  * @param <>
88  * @returns <>
89  * @author Martin Schaller (04/2008)
90 */
91 struct graphics * graphics_new(struct attr *parent, struct attr **attrs)
92 {
93         struct graphics *this_;
94         struct attr *type_attr;
95         struct graphics_priv * (*graphicstype_new)(struct navit *nav, struct graphics_methods *meth, struct attr **attrs, struct callback_list *cbl);
96
97         if (! (type_attr=attr_search(attrs, NULL, attr_type))) {
98                 return NULL;
99         }
100
101         graphicstype_new=plugin_get_graphics_type(type_attr->u.str);
102         if (! graphicstype_new)
103                 return NULL;
104         this_=g_new0(struct graphics, 1);
105         this_->cbl=callback_list_new();
106         this_->priv=(*graphicstype_new)(parent->u.navit, &this_->meth, attrs, this_->cbl);
107         this_->attrs=attr_list_dup(attrs);
108         return this_;
109 }
110
111 /**
112  * FIXME
113  * @param <>
114  * @returns <>
115  * @author Martin Schaller (04/2008)
116 */
117 int graphics_get_attr(struct graphics *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
118 {
119         return attr_generic_get_attr(this_->attrs, NULL, type, attr, iter);
120 }
121
122 /**
123  * FIXME
124  * @param <>
125  * @returns <>
126  * @author Martin Schaller (04/2008)
127 */
128 struct graphics * graphics_overlay_new(struct graphics *parent, struct point *p, int w, int h, int alpha, int wraparound)
129 {
130         struct graphics *this_;
131         if (!parent->meth.overlay_new)
132                 return NULL;
133         this_=g_new0(struct graphics, 1);
134         this_->priv=parent->meth.overlay_new(parent->priv, &this_->meth, p, w, h, alpha, wraparound);
135         if (!this_->priv) {
136                 g_free(this_);
137                 this_=NULL;
138         }
139         return this_;
140 }
141
142 /**
143  * FIXME
144  * @param <>
145  * @returns <>
146  * @author Martin Schaller (04/2008)
147 */
148 void graphics_init(struct graphics *this_)
149 {
150         this_->gc[0]=graphics_gc_new(this_);
151         graphics_gc_set_background(this_->gc[0], &(struct color) { 0xffff, 0xefef, 0xb7b7, 0xffff});
152         graphics_gc_set_foreground(this_->gc[0], &(struct color) { 0xffff, 0xefef, 0xb7b7, 0xffff });
153         this_->gc[1]=graphics_gc_new(this_);
154         graphics_gc_set_background(this_->gc[1], &(struct color) { 0x0000, 0x0000, 0x0000, 0xffff });
155         graphics_gc_set_foreground(this_->gc[1], &(struct color) { 0xffff, 0xffff, 0xffff, 0xffff });
156         this_->gc[2]=graphics_gc_new(this_);
157         graphics_gc_set_background(this_->gc[2], &(struct color) { 0xffff, 0xffff, 0xffff, 0xffff });
158         graphics_gc_set_foreground(this_->gc[2], &(struct color) { 0x0000, 0x0000, 0x0000, 0xffff });
159         graphics_background_gc(this_, this_->gc[0]);
160         navit_sharedir = getenv("NAVIT_SHAREDIR");
161 }
162
163 /**
164  * FIXME
165  * @param <>
166  * @returns <>
167  * @author Martin Schaller (04/2008)
168 */
169 void * graphics_get_data(struct graphics *this_, char *type)
170 {
171         return (this_->meth.get_data(this_->priv, type));
172 }
173
174 void graphics_add_callback(struct graphics *this_, struct callback *cb)
175 {
176         callback_list_add(this_->cbl, cb);
177 }
178
179 void graphics_remove_callback(struct graphics *this_, struct callback *cb)
180 {
181         callback_list_remove(this_->cbl, cb);
182 }
183
184 /**
185  * FIXME
186  * @param <>
187  * @returns <>
188  * @author Martin Schaller (04/2008)
189 */
190 struct graphics_font * graphics_font_new(struct graphics *gra, int size, int flags)
191 {
192         struct graphics_font *this_;
193
194         this_=g_new0(struct graphics_font,1);
195         this_->priv=gra->meth.font_new(gra->priv, &this_->meth, gra->default_font, size, flags);
196         return this_;
197 }
198
199 /**
200  * Free all loaded fonts.
201  * Used when switching layouts.
202  * @param gra The graphics instance
203  * @returns nothing
204  * @author Sarah Nordstrom (05/2008)
205  */
206 void graphics_font_destroy_all(struct graphics *gra) 
207
208         int i; 
209         for(i = 0 ; i < sizeof(gra->font) / sizeof(gra->font[0]); i++) { 
210                 if(!gra->font[i]) continue; 
211                 gra->font[i]->meth.font_destroy(gra->font[i]->priv); 
212                 gra->font[i] = NULL; 
213         }
214 }
215
216 /**
217  * FIXME
218  * @param <>
219  * @returns <>
220  * @author Martin Schaller (04/2008)
221 */
222 struct graphics_gc * graphics_gc_new(struct graphics *gra)
223 {
224         struct graphics_gc *this_;
225
226         this_=g_new0(struct graphics_gc,1);
227         this_->priv=gra->meth.gc_new(gra->priv, &this_->meth);
228         return this_;
229 }
230
231 /**
232  * FIXME
233  * @param <>
234  * @returns <>
235  * @author Martin Schaller (04/2008)
236 */
237 void graphics_gc_destroy(struct graphics_gc *gc)
238 {
239         gc->meth.gc_destroy(gc->priv);
240         g_free(gc);
241 }
242
243 /**
244  * FIXME
245  * @param <>
246  * @returns <>
247  * @author Martin Schaller (04/2008)
248 */
249 void graphics_gc_set_foreground(struct graphics_gc *gc, struct color *c)
250 {
251         gc->meth.gc_set_foreground(gc->priv, c);
252 }
253
254 /**
255  * FIXME
256  * @param <>
257  * @returns <>
258  * @author Martin Schaller (04/2008)
259 */
260 void graphics_gc_set_background(struct graphics_gc *gc, struct color *c)
261 {
262         gc->meth.gc_set_background(gc->priv, c);
263 }
264
265 /**
266  * FIXME
267  * @param <>
268  * @returns <>
269  * @author Martin Schaller (04/2008)
270 */
271 void graphics_gc_set_linewidth(struct graphics_gc *gc, int width)
272 {
273         gc->meth.gc_set_linewidth(gc->priv, width);
274 }
275
276 /**
277  * FIXME
278  * @param <>
279  * @returns <>
280  * @author Martin Schaller (04/2008)
281 */
282 void graphics_gc_set_dashes(struct graphics_gc *gc, int width, int offset, unsigned char dash_list[], int n)
283 {
284         if (gc->meth.gc_set_dashes)
285                 gc->meth.gc_set_dashes(gc->priv, width, offset, dash_list, n);
286 }
287
288 /**
289  * Create a new image from file path scaled to w and h pixels
290  * @param gra the graphics instance
291  * @param path path of the image to load
292  * @param w width to rescale to
293  * @param h height to rescale to
294  * @returns <>
295  * @author Martin Schaller (04/2008)
296 */
297 struct graphics_image * graphics_image_new_scaled(struct graphics *gra, char *path, int w, int h)
298 {
299         struct graphics_image *this_;
300
301         this_=g_new0(struct graphics_image,1);
302         this_->height=h;
303         this_->width=w;
304         this_->priv=gra->meth.image_new(gra->priv, &this_->meth, path, &this_->width, &this_->height, &this_->hot, 0);
305         if (! this_->priv) {
306                 g_free(this_);
307                 this_=NULL;
308         }
309         return this_;
310 }
311
312 /**
313  * Create a new image from file path scaled to w and h pixels and possibly rotated
314  * @param gra the graphics instance
315  * @param path path of the image to load
316  * @param w width to rescale to
317  * @param h height to rescale to
318  * @param rotate angle to rotate the image. Warning, graphics might only support 90 degree steps here
319  * @returns <>
320  * @author Martin Schaller (04/2008)
321 */
322 struct graphics_image * graphics_image_new_scaled_rotated(struct graphics *gra, char *path, int w, int h, int rotate)
323 {
324         struct graphics_image *this_;
325
326         this_=g_new0(struct graphics_image,1);
327         this_->height=h;
328         this_->width=w;
329         this_->priv=gra->meth.image_new(gra->priv, &this_->meth, path, &this_->width, &this_->height, &this_->hot, rotate);
330         if (! this_->priv) {
331                 g_free(this_);
332                 this_=NULL;
333         }
334         return this_;
335 }
336
337 /**
338  * Create a new image from file path
339  * @param gra the graphics instance
340  * @param path path of the image to load
341  * @returns <>
342  * @author Martin Schaller (04/2008)
343 */
344 struct graphics_image * graphics_image_new(struct graphics *gra, char *path)
345 {
346         return graphics_image_new_scaled(gra, path, -1, -1);
347 }
348
349 /**
350  * FIXME
351  * @param <>
352  * @returns <>
353  * @author Martin Schaller (04/2008)
354 */
355 void graphics_image_free(struct graphics *gra, struct graphics_image *img)
356 {
357         if (gra->meth.image_free)
358                 gra->meth.image_free(gra->priv, img->priv);
359         g_free(img);
360 }
361
362 /**
363  * FIXME
364  * @param <>
365  * @returns <>
366  * @author Martin Schaller (04/2008)
367 */
368 void graphics_draw_restore(struct graphics *this_, struct point *p, int w, int h)
369 {
370         this_->meth.draw_restore(this_->priv, p, w, h);
371 }
372
373 /**
374  * FIXME
375  * @param <>
376  * @returns <>
377  * @author Martin Schaller (04/2008)
378 */
379 void graphics_draw_mode(struct graphics *this_, enum draw_mode_num mode)
380 {
381         this_->meth.draw_mode(this_->priv, mode);
382 }
383
384 /**
385  * FIXME
386  * @param <>
387  * @returns <>
388  * @author Martin Schaller (04/2008)
389 */
390 void graphics_draw_lines(struct graphics *this_, struct graphics_gc *gc, struct point *p, int count)
391 {
392         this_->meth.draw_lines(this_->priv, gc->priv, p, count);
393 }
394
395 /**
396  * FIXME
397  * @param <>
398  * @returns <>
399  * @author Martin Schaller (04/2008)
400 */
401 void graphics_draw_circle(struct graphics *this_, struct graphics_gc *gc, struct point *p, int r)
402 {
403         this_->meth.draw_circle(this_->priv, gc->priv, p, r);
404 }
405
406 /**
407  * FIXME
408  * @param <>
409  * @returns <>
410  * @author Martin Schaller (04/2008)
411 */
412 void graphics_draw_rectangle(struct graphics *this_, struct graphics_gc *gc, struct point *p, int w, int h)
413 {
414         this_->meth.draw_rectangle(this_->priv, gc->priv, p, w, h);
415 }
416
417 /**
418  * FIXME
419  * @param <>
420  * @returns <>
421  * @author Martin Schaller (04/2008)
422 */
423 void graphics_draw_text(struct graphics *this_, struct graphics_gc *gc1, struct graphics_gc *gc2, struct graphics_font *font, char *text, struct point *p, int dx, int dy)
424 {
425         this_->meth.draw_text(this_->priv, gc1->priv, gc2 ? gc2->priv : NULL, font->priv, text, p, dx, dy);
426 }
427
428 /**
429  * FIXME
430  * @param <>
431  * @returns <>
432  * @author Martin Schaller (04/2008)
433 */
434 void graphics_get_text_bbox(struct graphics *this_, struct graphics_font *font, char *text, int dx, int dy, struct point *ret, int estimate)
435 {
436         this_->meth.get_text_bbox(this_->priv, font->priv, text, dx, dy, ret, estimate);
437 }
438
439 /**
440  * FIXME
441  * @param <>
442  * @returns <>
443  * @author Martin Schaller (04/2008)
444 */
445 void graphics_overlay_disable(struct graphics *this_, int disable)
446 {
447         if (this_->meth.overlay_disable)
448                 this_->meth.overlay_disable(this_->priv, disable);
449 }
450
451 /**
452  * FIXME
453  * @param <>
454  * @returns <>
455  * @author Martin Schaller (04/2008)
456 */
457 void graphics_draw_image(struct graphics *this_, struct graphics_gc *gc, struct point *p, struct graphics_image *img)
458 {
459         this_->meth.draw_image(this_->priv, gc->priv, p, img->priv);
460 }
461
462
463 //##############################################################################################################
464 //# Description:
465 //# Comment:
466 //# Authors: Martin Schaller (04/2008)
467 //##############################################################################################################
468 int
469 graphics_draw_drag(struct graphics *this_, struct point *p)
470 {
471         if (!this_->meth.draw_drag)
472                 return 0;
473         this_->meth.draw_drag(this_->priv, p);
474         return 1;
475 }
476
477 void
478 graphics_background_gc(struct graphics *this_, struct graphics_gc *gc)
479 {
480         this_->meth.background_gc(this_->priv, gc ? gc->priv : NULL);
481 }
482
483 #include "attr.h"
484 #include "popup.h"
485 #include <stdio.h>
486
487
488 #if 0
489 //##############################################################################################################
490 //# Description: 
491 //# Comment: 
492 //# Authors: Martin Schaller (04/2008)
493 //##############################################################################################################
494 static void popup_view_html(struct popup_item *item, char *file)
495 {
496         char command[1024];
497         sprintf(command,"firefox %s", file);
498         system(command);
499 }
500
501 struct transformatin *tg;
502 enum projection pg;
503
504 //##############################################################################################################
505 //# Description: 
506 //# Comment: 
507 //# Authors: Martin Schaller (04/2008)
508 //##############################################################################################################
509 static void graphics_popup(struct display_list *list, struct popup_item **popup)
510 {
511         struct item *item;
512         struct attr attr;
513         struct map_rect *mr;
514         struct coord c;
515         struct popup_item *curr_item,*last=NULL;
516         item=list->data;
517         mr=map_rect_new(item->map, NULL, NULL, 0);
518         printf("id hi=0x%x lo=0x%x\n", item->id_hi, item->id_lo);
519         item=map_rect_get_item_byid(mr, item->id_hi, item->id_lo);
520         if (item) {
521                 if (item_attr_get(item, attr_name, &attr)) {
522                         curr_item=popup_item_new_text(popup,attr.u.str,1);
523                         if (item_attr_get(item, attr_info_html, &attr)) {
524                                 popup_item_new_func(&last,"HTML Info",1, popup_view_html, g_strdup(attr.u.str));
525                         }
526                         if (item_attr_get(item, attr_price_html, &attr)) {
527                                 popup_item_new_func(&last,"HTML Preis",2, popup_view_html, g_strdup(attr.u.str));
528                         }
529                         curr_item->submenu=last;
530                 }
531         }
532         map_rect_destroy(mr);
533 }
534 #endif
535
536 /**
537  * FIXME
538  * @param <>
539  * @returns <>
540  * @author Martin Schaller (04/2008)
541 */
542 struct displayitem {
543         struct item item;
544         char *label;
545         int displayed;
546         int count;
547         struct coord c[0];
548 };
549
550 /**
551  * FIXME
552  * @param <>
553  * @returns <>
554  * @author Martin Schaller (04/2008)
555 */
556 static int xdisplay_free_list(gpointer key, gpointer value, gpointer user_data)
557 {
558         GHashTable *hash=value;
559         if (hash) 
560                 g_hash_table_destroy(hash);
561         return TRUE;
562 }
563
564 /**
565  * FIXME
566  * @param <>
567  * @returns <>
568  * @author Martin Schaller (04/2008)
569 */
570 static void xdisplay_free(GHashTable *display_list)
571 {
572         g_hash_table_foreach_remove(display_list, xdisplay_free_list, NULL);
573 }
574
575 static guint
576 displayitem_hash(gconstpointer key)
577 {
578         const struct displayitem *di=key;
579         return (di->item.id_hi^di->item.id_lo^((int) di->item.map));
580 }
581
582 static gboolean
583 displayitem_equal(gconstpointer a, gconstpointer b)
584 {
585         const struct displayitem *dia=a;
586         const struct displayitem *dib=b;
587         if (item_is_equal(dia->item, dib->item))
588                 return TRUE;
589         return FALSE;
590 }
591
592
593 /**
594  * FIXME
595  * @param <>
596  * @returns <>
597  * @author Martin Schaller (04/2008)
598 */
599 static void display_add(struct displaylist *displaylist, struct item *item, int count, struct coord *c, char *label)
600 {
601         struct displayitem *di;
602         int len;
603         GHashTable *h;
604         char *p;
605
606         len=sizeof(*di)+count*sizeof(*c);
607         if (label)
608                 len+=strlen(label)+1;
609
610         p=g_malloc(len);
611
612         di=(struct displayitem *)p;
613         di->displayed=0;
614         p+=sizeof(*di)+count*sizeof(*c);
615         di->item=*item;
616         if (label) {
617                 di->label=p;
618                 strcpy(di->label, label);
619         } else 
620                 di->label=NULL;
621         di->count=count;
622         memcpy(di->c, c, count*sizeof(*c));
623
624         h=g_hash_table_lookup(displaylist->dl, GINT_TO_POINTER(item->type));
625         if (! h) {
626                 h=g_hash_table_new_full(displayitem_hash, displayitem_equal, g_free, NULL);
627                 g_hash_table_insert(displaylist->dl, GINT_TO_POINTER(item->type), h);
628         }
629         g_hash_table_replace(h, di, NULL);
630 }
631
632
633 /**
634  * FIXME
635  * @param <>
636  * @returns <>
637  * @author Martin Schaller (04/2008)
638 */
639 static void label_line(struct graphics *gra, struct graphics_gc *fg, struct graphics_gc *bg, struct graphics_font *font, struct point *p, int count, char *label)
640 {
641         int i,x,y,tl,tlm,th,thm,tlsq,l;
642         float lsq;
643         double dx,dy;
644         struct point p_t;
645         struct point pb[5];
646
647         if (gra->meth.get_text_bbox) {
648                 gra->meth.get_text_bbox(gra->priv, font->priv, label, 0x10000, 0x0, pb, 1);
649                 tl=(pb[2].x-pb[0].x);
650                 th=(pb[0].y-pb[1].y);
651         } else {
652                 tl=strlen(label)*4;
653                 th=8;
654         }
655         tlm=tl*128;
656         thm=th*144;
657         tlsq = tlm*tlm;
658         for (i = 0 ; i < count-1 ; i++) {
659                 dx=p[i+1].x-p[i].x;
660                 dx*=128;
661                 dy=p[i+1].y-p[i].y;
662                 dy*=128;
663                 lsq = dx*dx+dy*dy;
664                 if (lsq > tlsq) {
665                         l=(int)sqrtf(lsq);
666                         x=p[i].x;
667                         y=p[i].y;
668                         if (dx < 0) {
669                                 dx=-dx;
670                                 dy=-dy;
671                                 x=p[i+1].x;
672                                 y=p[i+1].y;
673                         }
674                         x+=(l-tlm)*dx/l/256;
675                         y+=(l-tlm)*dy/l/256;
676                         x-=dy*thm/l/256;
677                         y+=dx*thm/l/256;
678                         p_t.x=x;
679                         p_t.y=y;
680 #if 0
681                         dbg(0,"display_text: '%s', %d, %d, %d, %d %d\n", label, x, y, dx*0x10000/l, dy*0x10000/l, l);
682 #endif
683                         if (x < gra->r.rl.x && x + tl > gra->r.lu.x && y + tl > gra->r.lu.y && y - tl < gra->r.rl.y) 
684                                 gra->meth.draw_text(gra->priv, fg->priv, bg->priv, font->priv, label, &p_t, dx*0x10000/l, dy*0x10000/l);
685                 }
686         }
687 }
688
689 static void display_draw_arrow(struct point *p, int dx, int dy, int l, struct graphics_gc *gc, struct graphics *gra)
690 {
691         struct point pnt[3];
692         pnt[0]=pnt[1]=pnt[2]=*p;
693         pnt[0].x+=-dx*l/65536+dy*l/65536;
694         pnt[0].y+=-dy*l/65536-dx*l/65536;
695         pnt[2].x+=-dx*l/65536-dy*l/65536;
696         pnt[2].y+=-dy*l/65536+dx*l/65536;
697         gra->meth.draw_lines(gra->priv, gc->priv, pnt, 3);
698 }
699
700 static void display_draw_arrows(struct graphics *gra, struct graphics_gc *gc, struct point *pnt, int count)
701 {
702         int i,dx,dy,l;
703         struct point p;
704         for (i = 0 ; i < count-1 ; i++) {
705                 dx=pnt[i+1].x-pnt[i].x; 
706                 dy=pnt[i+1].y-pnt[i].y;
707                 l=sqrt(dx*dx+dy*dy);
708                 if (l) {
709                         dx=dx*65536/l;
710                         dy=dy*65536/l;
711                         p=pnt[i];
712                         p.x+=dx*15/65536;
713                         p.y+=dy*15/65536;
714                         display_draw_arrow(&p, dx, dy, 10, gc, gra);
715                         p=pnt[i+1];
716                         p.x-=dx*15/65536;
717                         p.y-=dy*15/65536;
718                         display_draw_arrow(&p, dx, dy, 10, gc, gra);
719                 }
720         }
721 }
722
723 static int
724 intersection(struct point * a1, int adx, int ady, struct point * b1, int bdx, int bdy,
725               struct point * res)
726 {
727         int n, a, b;
728         n = bdy * adx - bdx * ady;
729         a = bdx * (a1->y - b1->y) - bdy * (a1->x - b1->x);
730         b = adx * (a1->y - b1->y) - ady * (a1->x - b1->x);
731         if (n < 0) {
732                 n = -n;
733                 a = -a;
734                 b = -b;
735         }
736 #if 0
737         if (a < 0 || b < 0)
738                 return 0;
739         if (a > n || b > n)
740                 return 0;
741 #endif
742         if (n == 0)
743                 return 0;
744         res->x = a1->x + a * adx / n;
745         res->y = a1->y + a * ady / n;
746         return 1;
747 }
748
749 struct circle {
750         short x,y,fowler;
751 } circle64[]={
752 {0,128,0},
753 {13,127,13},
754 {25,126,25},
755 {37,122,38},
756 {49,118,53},
757 {60,113,67},
758 {71,106,85},
759 {81,99,104},
760 {91,91,128},
761 {99,81,152},
762 {106,71,171},
763 {113,60,189},
764 {118,49,203},
765 {122,37,218},
766 {126,25,231},
767 {127,13,243},
768 {128,0,256},
769 {127,-13,269},
770 {126,-25,281},
771 {122,-37,294},
772 {118,-49,309},
773 {113,-60,323},
774 {106,-71,341},
775 {99,-81,360},
776 {91,-91,384},
777 {81,-99,408},
778 {71,-106,427},
779 {60,-113,445},
780 {49,-118,459},
781 {37,-122,474},
782 {25,-126,487},
783 {13,-127,499},
784 {0,-128,512},
785 {-13,-127,525},
786 {-25,-126,537},
787 {-37,-122,550},
788 {-49,-118,565},
789 {-60,-113,579},
790 {-71,-106,597},
791 {-81,-99,616},
792 {-91,-91,640},
793 {-99,-81,664},
794 {-106,-71,683},
795 {-113,-60,701},
796 {-118,-49,715},
797 {-122,-37,730},
798 {-126,-25,743},
799 {-127,-13,755},
800 {-128,0,768},
801 {-127,13,781},
802 {-126,25,793},
803 {-122,37,806},
804 {-118,49,821},
805 {-113,60,835},
806 {-106,71,853},
807 {-99,81,872},
808 {-91,91,896},
809 {-81,99,920},
810 {-71,106,939},
811 {-60,113,957},
812 {-49,118,971},
813 {-37,122,986},
814 {-25,126,999},
815 {-13,127,1011},
816 };
817
818 static void
819 draw_circle(struct point *pnt, int diameter, int scale, int start, int len, struct point *res, int *pos, int dir)
820 {
821         struct circle *c;
822
823 #if 0
824         dbg(0,"diameter=%d start=%d len=%d pos=%d dir=%d\n", diameter, start, len, *pos, dir);
825 #endif
826         int count=64;
827         int end=start+len;
828         int i,step;
829         c=circle64;
830         if (diameter > 128)
831                 step=1;
832         else if (diameter > 64)
833                 step=2;
834         else if (diameter > 24)
835                 step=4;
836         else if (diameter > 8)
837                 step=8;
838         else
839                 step=16;
840         if (len > 0) {
841                 while (start < 0) {
842                         start+=1024;
843                         end+=1024;
844                 }
845                 while (end > 0) {
846                         i=0;
847                         while (i < count && c[i].fowler <= start)
848                                 i+=step;
849                         while (i < count && c[i].fowler < end) {
850                                 res[*pos].x=pnt->x+((c[i].x*diameter+128)>>8);
851                                 res[*pos].y=pnt->y+((c[i].y*diameter+128)>>8);
852                                 (*pos)+=dir;
853                                 i+=step;
854                         }
855                         end-=1024;
856                         start-=1024;
857                 }
858         } else {
859                 while (start > 1024) {
860                         start-=1024;
861                         end-=1024;
862                 }
863                 while (end < 1024) {
864                         i=count-1;
865                         while (i >= 0 && c[i].fowler >= start)
866                                 i-=step;
867                         while (i >= 0 && c[i].fowler > end) {
868                                 res[*pos].x=pnt->x+((c[i].x*diameter+128)>>8);
869                                 res[*pos].y=pnt->y+((c[i].y*diameter+128)>>8);
870                                 (*pos)+=dir;
871                                 i-=step;
872                         }
873                         start+=1024;
874                         end+=1024;
875                 }
876         }
877 }
878
879
880 static int
881 fowler(int dy, int dx)
882 {
883         int adx, ady;           /* Absolute Values of Dx and Dy */
884         int code;               /* Angular Region Classification Code */
885
886         adx = (dx < 0) ? -dx : dx;      /* Compute the absolute values. */
887         ady = (dy < 0) ? -dy : dy;
888
889         code = (adx < ady) ? 1 : 0;
890         if (dx < 0)
891                 code += 2;
892         if (dy < 0)
893                 code += 4;
894
895         switch (code) {
896         case 0:
897                 return (dx == 0) ? 0 : 128*ady / adx;   /* [  0, 45] */
898         case 1:
899                 return (256 - (128*adx / ady)); /* ( 45, 90] */
900         case 3:
901                 return (256 + (128*adx / ady)); /* ( 90,135) */
902         case 2:
903                 return (512 - (128*ady / adx)); /* [135,180] */
904         case 6:
905                 return (512 + (128*ady / adx)); /* (180,225] */
906         case 7:
907                 return (768 - (128*adx / ady)); /* (225,270) */
908         case 5:
909                 return (768 + (128*adx / ady)); /* [270,315) */
910         case 4:
911                 return (1024 - (128*ady / adx));/* [315,360) */
912         }
913         return 0;
914 }
915 static int
916 int_sqrt(unsigned int n)
917 {
918         unsigned int h, p= 0, q= 1, r= n;
919         while ( q <= n )
920                 q <<= 2;
921         while ( q != 1 ) {
922                 q >>= 2;
923                 h = p + q;
924                 p >>= 1;
925                 if ( r >= h ) {
926                         p += q;
927                         r -= h;
928                 }
929         }
930         return p;
931 }
932
933 struct offset {
934         int px,py,nx,ny;
935 };
936
937 static void
938 calc_offsets(int wi, int l, int dx, int dy, struct offset *res)
939 {
940         int x,y;
941         x = (dx * wi) / l;
942         y = (dy * wi) / l;
943         if (x < 0) {
944                 res->nx = -x/2;
945                 res->px = (x-1)/2;
946         } else {
947                 res->nx = -(x+1)/2;
948                 res->px = x/2;
949         }
950         if (y < 0) {
951                 res->ny = -y/2;
952                 res->py = (y-1)/2;
953         } else {
954                 res->ny = -(y+1)/2;
955                 res->py = y/2;
956         }
957 }
958
959 static void
960 graphics_draw_polyline_as_polygon(struct graphics *gra, struct graphics_gc *gc, struct point *pnt, int count, int *width, int step)
961 {
962         int maxpoints=200;
963         struct point res[maxpoints], pos, poso, neg, nego;
964         int i, dx=0, dy=0, l=0, dxo=0, dyo=0;
965         struct offset o,oo;
966         int fow=0, fowo=0, delta;
967         int wi, ppos = maxpoints/2, npos = maxpoints/2;
968         int state,prec=5;
969         int max_circle_points=20;
970         for (i = 0; i < count; i++) {
971                 wi=*width;
972                 width+=step;
973                 if (i < count - 1) {
974                         dx = (pnt[i + 1].x - pnt[i].x);
975                         dy = (pnt[i + 1].y - pnt[i].y);
976                         l = int_sqrt(dx * dx + dy * dy);
977                         fow=fowler(-dy, dx);
978                 }
979                 if (! l) 
980                         l=1;
981                 calc_offsets(wi, l, dx, dy, &o);
982                 pos.x = pnt[i].x + o.ny;
983                 pos.y = pnt[i].y + o.px;
984                 neg.x = pnt[i].x + o.py;
985                 neg.y = pnt[i].y + o.nx;
986                 if (! i)
987                         state=0;
988                 else if (i == count-1) 
989                         state=2;
990                 else if (npos < max_circle_points || ppos >= maxpoints-max_circle_points)
991                         state=3;
992                 else
993                         state=1;
994                 switch (state) {
995                 case 1:
996                        if (fowo != fow) {
997                                 poso.x = pnt[i].x + oo.ny;
998                                 poso.y = pnt[i].y + oo.px;
999                                 nego.x = pnt[i].x + oo.py;
1000                                 nego.y = pnt[i].y + oo.nx;
1001                                 delta=fowo-fow;
1002                                 if (delta < 0)
1003                                         delta+=1024;
1004                                 if (delta < 512) {
1005                                         if (intersection(&pos, dx, dy, &poso, dxo, dyo, &res[ppos]))
1006                                                 ppos++;
1007                                         res[--npos] = nego;
1008                                         --npos;
1009                                         draw_circle(&pnt[i], wi, prec, fowo-512, -delta, res, &npos, -1);
1010                                         res[npos] = neg;
1011                                 } else {
1012                                         res[ppos++] = poso;
1013                                         draw_circle(&pnt[i], wi, prec, fowo, 1024-delta, res, &ppos, 1);
1014                                         res[ppos++] = pos;
1015                                         if (intersection(&neg, dx, dy, &nego, dxo, dyo, &res[npos - 1]))
1016                                                 npos--;
1017                                 }
1018                         }
1019                         break;
1020                 case 2:
1021                 case 3:
1022                         res[--npos] = neg;
1023                         --npos;
1024                         draw_circle(&pnt[i], wi, prec, fow-512, -512, res, &npos, -1);
1025                         res[npos] = pos;
1026                         res[ppos++] = pos;
1027                         dbg_assert(npos > 0);
1028                         dbg_assert(ppos < maxpoints);
1029                         gra->meth.draw_polygon(gra->priv, gc->priv, res+npos, ppos-npos);
1030                         if (state == 2)
1031                                 break;
1032                         npos=maxpoints/2;
1033                         ppos=maxpoints/2;
1034                 case 0:
1035                         res[ppos++] = neg;
1036                         draw_circle(&pnt[i], wi, prec, fow+512, 512, res, &ppos, 1);
1037                         res[ppos++] = pos;
1038                         break;
1039                 }
1040                 if (step) {
1041                         wi=*width;
1042                         calc_offsets(wi, l, dx, dy, &oo);
1043                 } else 
1044                         oo=o;
1045                 dxo = -dx;
1046                 dyo = -dy;
1047                 fowo=fow;
1048         }
1049 }
1050
1051 struct transformation *tg;
1052 enum projection pg;
1053
1054 struct wpoint {
1055         int x,y,w;
1056 };
1057
1058 static int
1059 clipcode(struct wpoint *p, struct point_rect *r)
1060 {
1061         int code=0;
1062         if (p->x < r->lu.x)
1063                 code=1;
1064         if (p->x > r->rl.x)
1065                 code=2;
1066         if (p->y < r->lu.y)
1067                 code |=4;
1068         if (p->y > r->rl.y)
1069                 code |=8;
1070         return code;
1071 }
1072
1073
1074 static int
1075 clip_line(struct wpoint *p1, struct wpoint *p2, struct point_rect *r)
1076 {
1077         int code1,code2,ret=1;
1078         int dx,dy,dw;
1079         code1=clipcode(p1, r);
1080         if (code1)
1081                 ret |= 2;
1082         code2=clipcode(p2, r);
1083         if (code2)
1084                 ret |= 4;
1085         dx=p2->x-p1->x;
1086         dy=p2->y-p1->y;
1087         dw=p2->w-p1->w;
1088         while (code1 || code2) {
1089                 if (code1 & code2)
1090                         return 0;
1091                 if (code1 & 1) {
1092                         p1->y+=(r->lu.x-p1->x)*dy/dx;
1093                         p1->w+=(r->lu.x-p1->x)*dw/dx;
1094                         p1->x=r->lu.x;
1095                 } else if (code1 & 2) {
1096                         p1->y+=(r->rl.x-p1->x)*dy/dx;
1097                         p1->w+=(r->rl.x-p1->x)*dw/dx;
1098                         p1->x=r->rl.x;
1099                 } else if (code1 & 4) {
1100                         p1->x+=(r->lu.y-p1->y)*dx/dy;
1101                         p1->w+=(r->lu.y-p1->y)*dw/dy;
1102                         p1->y=r->lu.y;
1103                 } else if (code1 & 8) {
1104                         p1->x+=(r->rl.y-p1->y)*dx/dy;
1105                         p1->w+=(r->rl.y-p1->y)*dw/dy;
1106                         p1->y=r->rl.y;
1107                 }
1108                 code1=clipcode(p1, r);
1109                 if (code1 & code2)
1110                         return 0;
1111                 if (code2 & 1) {
1112                         p2->y+=(r->lu.x-p2->x)*dy/dx;
1113                         p2->w+=(r->lu.x-p2->x)*dw/dx;
1114                         p2->x=r->lu.x;
1115                 } else if (code2 & 2) {
1116                         p2->y+=(r->rl.x-p2->x)*dy/dx;
1117                         p2->w+=(r->rl.x-p2->x)*dw/dx;
1118                         p2->x=r->rl.x;
1119                 } else if (code2 & 4) {
1120                         p2->x+=(r->lu.y-p2->y)*dx/dy;
1121                         p2->w+=(r->lu.y-p2->y)*dw/dy;
1122                         p2->y=r->lu.y;
1123                 } else if (code2 & 8) {
1124                         p2->x+=(r->rl.y-p2->y)*dx/dy;
1125                         p2->w+=(r->rl.y-p2->y)*dw/dy;
1126                         p2->y=r->rl.y;
1127                 }
1128                 code2=clipcode(p2, r);
1129         }
1130         return ret;
1131 }
1132
1133 static void
1134 graphics_draw_polyline_clipped(struct graphics *gra, struct graphics_gc *gc, struct point *pa, int count, int *width, int step, int poly)
1135 {
1136         struct point p[count+1];
1137         int w[count*step+1];
1138         struct wpoint p1,p2;
1139         int i,code,out=0;
1140         int wmax;
1141         struct point_rect r=gra->r;
1142
1143         wmax=width[0];
1144         if (step) {
1145                 for (i = 1 ; i < count ; i++) {
1146                         if (width[i*step] > wmax)
1147                                 wmax=width[i*step];
1148                 }
1149         }
1150         if (wmax <= 0)
1151                 return;
1152         r.lu.x-=wmax;
1153         r.lu.y-=wmax;
1154         r.rl.x+=wmax;
1155         r.rl.y+=wmax;
1156         for (i = 0 ; i < count ; i++) {
1157                 if (i) {
1158                         p1.x=pa[i-1].x;
1159                         p1.y=pa[i-1].y;
1160                         p1.w=width[(i-1)*step];
1161                         p2.x=pa[i].x;
1162                         p2.y=pa[i].y;
1163                         p2.w=width[i*step];
1164                         /* 0 = invisible, 1 = completely visible, 3 = start point clipped, 5 = end point clipped, 7 both points clipped */
1165                         code=clip_line(&p1, &p2, &r);
1166                         if (((code == 1 || code == 5) && i == 1) || (code & 2)) {
1167                                 p[out].x=p1.x;
1168                                 p[out].y=p1.y;
1169                                 w[out*step]=p1.w;
1170                                 out++;
1171                         }
1172                         if (code) {
1173                                 p[out].x=p2.x;
1174                                 p[out].y=p2.y;
1175                                 w[out*step]=p2.w;
1176                                 out++;
1177                         }
1178                         if (i == count-1 || (code & 4)) {
1179                                 if (out > 1) {
1180                                         if (poly) {     
1181                                                 graphics_draw_polyline_as_polygon(gra, gc, p, out, w, step);
1182                                         } else
1183                                                 gra->meth.draw_lines(gra->priv, gc->priv, p, out);
1184                                         out=0;
1185                                 }
1186                         }
1187                 }
1188         }
1189 }
1190
1191 static int
1192 is_inside(struct point *p, struct point_rect *r, int edge)
1193 {
1194         switch(edge) {
1195         case 0:
1196                 return p->x >= r->lu.x;
1197         case 1:
1198                 return p->x <= r->rl.x;
1199         case 2:
1200                 return p->y >= r->lu.y;
1201         case 3:
1202                 return p->y <= r->rl.y;
1203         default:
1204                 return 0;
1205         }
1206 }
1207
1208 static void
1209 poly_intersection(struct point *p1, struct point *p2, struct point_rect *r, int edge, struct point *ret)
1210 {
1211         int dx=p2->x-p1->x;
1212         int dy=p2->y-p1->y;
1213         switch(edge) {
1214         case 0:
1215                 ret->y=p1->y+(r->lu.x-p1->x)*dy/dx;
1216                 ret->x=r->lu.x;
1217                 break;
1218         case 1:
1219                 ret->y=p1->y+(r->rl.x-p1->x)*dy/dx;
1220                 ret->x=r->rl.x;
1221                 break;
1222         case 2:
1223                 ret->x=p1->x+(r->lu.y-p1->y)*dx/dy;
1224                 ret->y=r->lu.y;
1225                 break;
1226         case 3:
1227                 ret->x=p1->x+(r->rl.y-p1->y)*dx/dy;
1228                 ret->y=r->rl.y;
1229                 break;
1230         }
1231 }
1232
1233 static void
1234 graphics_draw_polygon_clipped(struct graphics *gra, struct graphics_gc *gc, struct point *pin, int count_in)
1235 {
1236         struct point_rect r=gra->r;
1237         struct point *pout,*p,*s,pi;
1238         struct point p1[count_in+1];
1239         struct point p2[count_in+1];
1240         int count_out,edge=3;
1241         int i;
1242 #if 0
1243         r.lu.x+=20;
1244         r.lu.y+=20;
1245         r.rl.x-=20;
1246         r.rl.y-=20;
1247 #endif
1248
1249         pout=p1;
1250         for (edge = 0 ; edge < 4 ; edge++) {
1251                 p=pin;
1252                 s=pin+count_in-1;
1253                 count_out=0;
1254                 for (i = 0 ; i < count_in ; i++) {
1255                         if (is_inside(p, &r, edge)) {
1256                                 if (! is_inside(s, &r, edge)) {
1257                                         poly_intersection(s,p,&r,edge,&pi);
1258                                         pout[count_out++]=pi;
1259                                 }
1260                                 pout[count_out++]=*p;
1261                         } else {
1262                                 if (is_inside(s, &r, edge)) {
1263                                         poly_intersection(p,s,&r,edge,&pi);
1264                                         pout[count_out++]=pi;
1265                                 }
1266                         }
1267                         s=p;
1268                         p++;
1269                 }
1270                 count_in=count_out;
1271                 if (pin == p1) {
1272                         pin=p2;
1273                         pout=p1;
1274                 } else {
1275                         pin=p1;
1276                         pout=p2;
1277                 }
1278         }
1279         gra->meth.draw_polygon(gra->priv, gc->priv, pin, count_in);
1280 }
1281
1282 struct display_context
1283 {
1284         struct graphics *gra;
1285         struct element *e;
1286         struct graphics_gc *gc;
1287         struct graphics_image *img;
1288 };
1289
1290 static void
1291 display_context_init(struct display_context *dc)
1292 {
1293         dc->gc=NULL;
1294         dc->img=NULL;
1295 }
1296
1297 static void
1298 display_context_free(struct display_context *dc)
1299 {
1300         g_free(dc->gc);
1301         g_free(dc->img);
1302         dc->gc=NULL;
1303         dc->img=NULL;
1304 }
1305
1306 static void
1307 displayitem_draw(struct displayitem *di, void *dummy, struct display_context *dc)
1308 {
1309         int width[16384];
1310         int count;
1311         struct point pa[16384];
1312         struct graphics *gra=dc->gra;
1313         struct graphics_gc *gc=dc->gc;
1314         struct element *e=dc->e;
1315         struct graphics_image *img=dc->img;
1316         struct point p;
1317         char path[PATH_MAX];
1318
1319         di->displayed=1;
1320         if (! gc) {
1321                 gc=graphics_gc_new(gra);
1322                 gc->meth.gc_set_foreground(gc->priv, &e->color);
1323                 dc->gc=gc;
1324         }
1325         if (dc->e->type == element_polyline) {
1326                 count=transform(tg, pg, di->c, pa, di->count, 1, e->u.polyline.width, width);
1327         }
1328         else
1329                 count=transform(tg, pg, di->c, pa, di->count, 1, 0, NULL);
1330         switch (e->type) {
1331         case element_polygon:
1332 #if 0
1333                 {
1334                         int i;
1335                         for (i = 0 ; i < count ; i++) {
1336                                 dbg(0,"pa[%d]=%d,%d\n", i, pa[i].x, pa[i].y);
1337                         }
1338                 }
1339                 dbg(0,"element_polygon count=%d\n",count);
1340 #endif
1341 #if 1
1342                 graphics_draw_polygon_clipped(gra, gc, pa, count);
1343 #endif
1344                 break;
1345         case element_polyline:
1346 #if 0
1347                 if (e->u.polyline.width > 1) {
1348                         graphics_draw_polyline_as_polygon(gra, gc, pa, count, width, 0);
1349                 } else {
1350 #else
1351                 {
1352 #if 0
1353                          if (e->u.polyline.width > 1)
1354                                      gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
1355 #else
1356                         gc->meth.gc_set_linewidth(gc->priv, 1);
1357 #endif
1358
1359                         
1360 #endif
1361                         if (e->u.polyline.width > 0 && e->u.polyline.dash_num > 0)
1362                                 graphics_gc_set_dashes(gc, e->u.polyline.width, 
1363                                                        e->u.polyline.offset,
1364                                                        e->u.polyline.dash_table,
1365                                                        e->u.polyline.dash_num);
1366 #if 0
1367                         if (di->label && !strcmp(di->label, "Bahnhofstr.") && di->item.type != type_street_1_city) {
1368                                 dbg(0,"0x%x,0x%x %s\n", di->item.id_hi, di->item.id_lo, item_to_name(di->item.type));
1369 #endif
1370                         graphics_draw_polyline_clipped(gra, gc, pa, count, width, 1, e->u.polyline.width > 1);
1371 #if 0
1372                         }
1373 #endif
1374                 }
1375                 break;
1376         case element_circle:
1377                 if (count) {
1378                         if (e->u.circle.width > 1) 
1379                                 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
1380                         gra->meth.draw_circle(gra->priv, gc->priv, pa, e->u.circle.radius);
1381                         if (di->label && e->text_size) {
1382                                 p.x=pa[0].x+3;
1383                                 p.y=pa[0].y+10;
1384                                 if (! gra->font[e->text_size])
1385                                         gra->font[e->text_size]=graphics_font_new(gra, e->text_size*20, 0);
1386                                 gra->meth.draw_text(gra->priv, gra->gc[2]->priv, gra->gc[1]->priv, gra->font[e->text_size]->priv, di->label, &p, 0x10000, 0);
1387                         }
1388                 }
1389                 break;
1390         case element_text:
1391                 if (count && di->label) {
1392                         if (! gra->font[e->text_size])
1393                                 gra->font[e->text_size]=graphics_font_new(gra, e->text_size*20, 0);
1394                         label_line(gra, gra->gc[2], gra->gc[1], gra->font[e->text_size], pa, count, di->label);
1395                 }
1396                 break;
1397         case element_icon:
1398                 if (count) {
1399                         if (!img) {
1400                                 if (e->u.icon.src[0] == '/')
1401                                         strcpy(path,e->u.icon.src);
1402                                 else
1403                                         sprintf(path,"%s/xpm/%s", navit_sharedir, e->u.icon.src);
1404                                 img=graphics_image_new_scaled_rotated(gra, path, e->u.icon.width, e->u.icon.height, e->u.icon.rotation);
1405                                 if (img)
1406                                         dc->img=img;
1407                                 else
1408                                         dbg(0,"failed to load icon '%s'\n", e->u.icon.src);
1409                         }
1410                         if (img) {
1411                                 p.x=pa[0].x - img->hot.x;
1412                                 p.y=pa[0].y - img->hot.y;
1413                                 gra->meth.draw_image(gra->priv, gra->gc[0]->priv, &p, img->priv);
1414                         }
1415                 }
1416                 break;
1417         case element_image:
1418                 dbg(1,"image: '%s'\n", di->label);
1419                 if (gra->meth.draw_image_warp)
1420                         gra->meth.draw_image_warp(gra->priv, gra->gc[0]->priv, pa, count, di->label);
1421                 else
1422                         dbg(0,"draw_image_warp not supported by graphics driver drawing '%s'\n", di->label);
1423                 break;
1424         case element_arrows:
1425                 display_draw_arrows(gra,gc,pa,count);
1426                 break;
1427         default:
1428                 printf("Unhandled element type %d\n", e->type);
1429         
1430         }
1431 }
1432 /**
1433  * FIXME
1434  * @param <>
1435  * @returns <>
1436  * @author Martin Schaller (04/2008)
1437 */
1438 static void xdisplay_draw_elements(struct graphics *gra, GHashTable *display_list, struct itemgra *itm)
1439 {
1440         struct element *e;
1441         GList *es,*types;
1442         GHashTable *h;
1443         enum item_type type;
1444         struct display_context dc;
1445         display_context_init(&dc);
1446
1447         dc.gra=gra;
1448
1449         es=itm->elements;
1450         while (es) {
1451                 e=es->data;
1452                 dc.e=e;
1453                 types=itm->type;
1454                 while (types) {
1455                         type=GPOINTER_TO_INT(types->data);
1456                         h=g_hash_table_lookup(display_list, GINT_TO_POINTER(type));
1457                         if (h) {
1458                                 g_hash_table_foreach(h, (GHFunc)displayitem_draw, &dc);
1459                                 display_context_free(&dc);
1460                         }
1461                         types=g_list_next(types);
1462                 }
1463                 es=g_list_next(es);
1464         }
1465 }
1466
1467 void
1468 graphics_draw_itemgra(struct graphics *gra, struct itemgra *itm, struct transformation *t)
1469 {
1470         GList *es;
1471         struct point p;
1472         struct coord c;
1473         char *label=NULL;
1474         struct graphics_gc *gc = NULL;
1475         struct graphics_image *img;
1476         char path[PATH_MAX];
1477         es=itm->elements;
1478         c.x=0;
1479         c.y=0;
1480         while (es) {
1481                 struct element *e=es->data;
1482                 int count=e->coord_count;
1483                 struct point pnt[count+1];
1484                 if (count)
1485                         transform(t, projection_screen, e->coord, pnt, count, 0, 0, NULL);
1486                 else {
1487                         transform(t, projection_screen, &c, pnt, 1, 0, 0, NULL);
1488                         count=1;
1489                 }
1490                 gc=graphics_gc_new(gra);
1491                 gc->meth.gc_set_foreground(gc->priv, &e->color);
1492                 switch (e->type) {
1493                 case element_polyline:
1494                         if (e->u.polyline.width > 1) 
1495                                 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
1496                         if (e->u.polyline.width > 0 && e->u.polyline.dash_num > 0)
1497                                 graphics_gc_set_dashes(gc, e->u.polyline.width, 
1498                                                        e->u.polyline.offset,
1499                                                        e->u.polyline.dash_table,
1500                                                        e->u.polyline.dash_num);
1501                         gra->meth.draw_lines(gra->priv, gc->priv, pnt, count);
1502                         break;
1503                 case element_circle:
1504                         if (e->u.circle.width > 1) 
1505                                 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
1506                         gra->meth.draw_circle(gra->priv, gc->priv, &pnt[0], e->u.circle.radius);
1507                         if (label && e->text_size) {
1508                                 p.x=pnt[0].x+3;
1509                                 p.y=pnt[0].y+10;
1510                         if (! gra->font[e->text_size])
1511                                 gra->font[e->text_size]=graphics_font_new(gra, e->text_size*20, 0);
1512                                 gra->meth.draw_text(gra->priv, gra->gc[2]->priv, gra->gc[1]->priv, gra->font[e->text_size]->priv, label, &p, 0x10000, 0);
1513                         }
1514                         break;
1515                 case element_icon:
1516                         if (e->u.icon.src[0] == '/') 
1517                                 strcpy(path,e->u.icon.src);
1518                         else
1519                                 sprintf(path,"%s/xpm/%s", navit_sharedir, e->u.icon.src);
1520                         img=graphics_image_new_scaled_rotated(gra, path, e->u.icon.width, e->u.icon.height, e->u.icon.rotation);
1521                         if (! img)
1522                                 dbg(0,"failed to load icon '%s'\n", e->u.icon.src);
1523                         else {
1524                                 p.x=pnt[0].x - img->hot.x;
1525                                 p.y=pnt[0].y - img->hot.y;
1526                                 gra->meth.draw_image(gra->priv, gc->priv, &p, img->priv);
1527                                 graphics_image_free(gra, img);
1528                         }
1529                         break;
1530                 default:
1531                         dbg(0,"dont know how to draw %d\n", e->type);
1532                 }
1533                 graphics_gc_destroy(gc);
1534                 es=g_list_next(es);
1535         }
1536 }
1537
1538 /**
1539  * FIXME
1540  * @param <>
1541  * @returns <>
1542  * @author Martin Schaller (04/2008)
1543 */
1544 static void xdisplay_draw_layer(GHashTable *display_list, struct graphics *gra, struct layer *lay, int order)
1545 {
1546         GList *itms;
1547         struct itemgra *itm;
1548
1549         itms=lay->itemgras;
1550         while (itms) {
1551                 itm=itms->data;
1552                 if (order >= itm->order.min && order <= itm->order.max) 
1553                         xdisplay_draw_elements(gra, display_list, itm);
1554                 itms=g_list_next(itms);
1555         }
1556 }
1557
1558 /**
1559  * FIXME
1560  * @param <>
1561  * @returns <>
1562  * @author Martin Schaller (04/2008)
1563 */
1564 static void xdisplay_draw(GHashTable *display_list, struct graphics *gra, struct layout *l, int order)
1565 {
1566         GList *lays;
1567         struct layer *lay;
1568         
1569         lays=l->layers;
1570         while (lays) {
1571                 lay=lays->data;
1572                 xdisplay_draw_layer(display_list, gra, lay, order);
1573                 lays=g_list_next(lays);
1574         }
1575 }
1576
1577 /**
1578  * FIXME
1579  * @param <>
1580  * @returns <>
1581  * @author Martin Schaller (04/2008)
1582 */
1583 extern void *route_selection;
1584
1585 /**
1586  * FIXME
1587  * @param <>
1588  * @returns <>
1589  * @author Martin Schaller (04/2008)
1590 */
1591 static void do_draw_map(struct displaylist *displaylist, struct transformation *t, struct map *m, int order)
1592 {
1593         enum projection pro;
1594         struct map_rect *mr;
1595         struct item *item;
1596         int conv,count,max=16384;
1597         struct coord ca[max];
1598         struct attr attr;
1599         struct map_selection *sel;
1600
1601         pro=map_projection(m);
1602         conv=map_requires_conversion(m);
1603         sel=transform_get_selection(t, pro, order);
1604         tg=t;
1605         pg=pro;
1606 #if 0
1607         sel=NULL;
1608 #endif
1609         if (route_selection)
1610                 mr=map_rect_new(m, route_selection);
1611         else
1612                 mr=map_rect_new(m, sel);
1613         if (! mr) {
1614                 map_selection_destroy(sel);
1615                 return;
1616         }
1617         while ((item=map_rect_get_item(mr))) {
1618 #if 0
1619                 if (num < 7599 || num > 7599)
1620                         continue;
1621 #endif
1622 #if 0
1623                 if (item->id_hi != 0xb0031 || item->id_lo != 0x20c9aeea)
1624                         continue;
1625 #endif
1626                 count=item_coord_get_within_selection(item, ca, item->type < type_line ? 1: max, sel);
1627                 if (! count)
1628                         continue;
1629                 if (item->type >= type_line && count < 2) {
1630                         dbg(1,"poly from map has only %d points\n", count);
1631                         continue;
1632                 }
1633                 if (item->type < type_line) {
1634 #if 0
1635                         if (! map_selection_contains_point(sel, &ca[0])) {
1636                                 dbg(1,"point not visible\n");
1637                                 continue;
1638                         }
1639 #endif
1640                 } else if (item->type < type_area) {
1641 #if 0
1642                         if (! map_selection_contains_polyline(sel, ca, count)) {
1643                                 dbg(1,"polyline not visible\n");
1644                                 continue;
1645                         }
1646 #endif
1647                 } else {
1648 #if 0
1649                         if (! map_selection_contains_polygon(sel, ca, count)) {
1650                                 dbg(1,"polygon not visible\n");
1651                                 continue;
1652                         }
1653 #endif
1654                 }
1655                 if (count == max) 
1656                         dbg(0,"point count overflow %d\n", count);
1657                 if (!item_attr_get(item, attr_label, &attr))
1658                         attr.u.str=NULL;
1659                 if (conv && attr.u.str && attr.u.str[0]) {
1660                         char *str=map_convert_string(m, attr.u.str);
1661                         display_add(displaylist, item, count, ca, str);
1662                         map_convert_free(str);
1663                 } else
1664                         display_add(displaylist, item, count, ca, attr.u.str);
1665         }
1666         map_rect_destroy(mr);
1667         map_selection_destroy(sel);
1668 }
1669
1670 /**
1671  * FIXME
1672  * @param <>
1673  * @returns <>
1674  * @author Martin Schaller (04/2008)
1675 */
1676 static void do_draw(struct displaylist *displaylist, struct transformation *t, GList *mapsets, int order)
1677 {
1678         struct mapset *ms;
1679         struct map *m;
1680         struct mapset_handle *h;
1681
1682         if (! mapsets)
1683                 return;
1684         ms=mapsets->data;
1685         h=mapset_open(ms);
1686         while ((m=mapset_next(h, 1))) {
1687                 do_draw_map(displaylist, t, m, order);
1688         }
1689         mapset_close(h);
1690 }
1691
1692 /**
1693  * FIXME
1694  * @param <>
1695  * @returns <>
1696  * @author Martin Schaller (04/2008)
1697 */
1698 int graphics_ready(struct graphics *this_)
1699 {
1700         return this_->ready;
1701 }
1702
1703 /**
1704  * FIXME
1705  * @param <>
1706  * @returns <>
1707  * @author Martin Schaller (04/2008)
1708 */
1709 void graphics_displaylist_draw(struct graphics *gra, struct displaylist *displaylist, struct transformation *trans, struct layout *l, int callback)
1710 {
1711         int order=transform_get_order(trans);
1712         struct point p;
1713         tg=trans;
1714         p.x=0;
1715         p.y=0;
1716         // FIXME find a better place to set the background color
1717         if (l) {
1718                 graphics_gc_set_background(gra->gc[0], &l->color);
1719                 graphics_gc_set_foreground(gra->gc[0], &l->color);
1720                 gra->default_font = g_strdup(l->font);
1721         }
1722         graphics_background_gc(gra, gra->gc[0]);
1723         gra->meth.draw_mode(gra->priv, draw_mode_begin);
1724         gra->meth.draw_rectangle(gra->priv, gra->gc[0]->priv, &p, 32767, 32767);
1725         if (l) 
1726                 xdisplay_draw(displaylist->dl, gra, l, order+l->order_delta);
1727         if (callback)
1728                 callback_list_call_attr_0(gra->cbl, attr_postdraw);
1729         gra->meth.draw_mode(gra->priv, draw_mode_end);
1730 }
1731
1732 #if 0
1733 /**
1734  * FIXME
1735  * @param <>
1736  * @returns <>
1737  * @author Martin Schaller (04/2008)
1738 */
1739 void graphics_displaylist_move(struct displaylist *displaylist, int dx, int dy)
1740 {
1741         struct displaylist_handle *dlh;
1742         struct displayitem *di;
1743         int i;
1744
1745         dlh=graphics_displaylist_open(displaylist);
1746         while ((di=graphics_displaylist_next(dlh))) {
1747                 for (i = 0 ; i < di->count ; i++) {
1748                         di->pnt[i].x+=dx;
1749                         di->pnt[i].y+=dy;
1750                 }
1751         }
1752         graphics_displaylist_close(dlh);
1753 }
1754 #endif
1755
1756 /**
1757  * FIXME
1758  * @param <>
1759  * @returns <>
1760  * @author Martin Schaller (04/2008)
1761 */
1762 void graphics_draw(struct graphics *gra, struct displaylist *displaylist, GList *mapsets, struct transformation *trans, struct layout *l)
1763 {
1764         int order=transform_get_order(trans);
1765
1766         dbg(1,"enter");
1767
1768 #if 0
1769         printf("scale=%d center=0x%x,0x%x mercator scale=%f\n", scale, co->trans->center.x, co->trans->center.y, transform_scale(co->trans->center.y));
1770 #endif
1771
1772         xdisplay_free(displaylist->dl);
1773         dbg(1,"order=%d\n", order);
1774
1775
1776 #if 0
1777         for (i = 0 ; i < data_window_type_end; i++) {
1778                 data_window_begin(co->data_window[i]);  
1779         }
1780 #endif
1781         profile(0,NULL);
1782         if (l)
1783                 order+=l->order_delta;
1784         if (mapsets)
1785                 do_draw(displaylist, trans, mapsets, order);
1786 //      profile(1,"do_draw");
1787         graphics_displaylist_draw(gra, displaylist, trans, l, 1);
1788         profile(1,"xdisplay_draw");
1789         profile(0,"end");
1790   
1791 #if 0
1792         for (i = 0 ; i < data_window_type_end; i++) {
1793                 data_window_end(co->data_window[i]);    
1794         }
1795 #endif
1796         gra->ready=1;
1797 }
1798
1799 /**
1800  * FIXME
1801  * @param <>
1802  * @returns <>
1803  * @author Martin Schaller (04/2008)
1804 */
1805 struct displaylist_handle {
1806         GList *hl_head,*hl,*l_head,*l;
1807 };
1808
1809 /**
1810  * FIXME
1811  * @param <>
1812  * @returns <>
1813  * @author Martin Schaller (04/2008)
1814 */
1815 struct displaylist_handle * graphics_displaylist_open(struct displaylist *displaylist)
1816 {
1817         struct displaylist_handle *ret;
1818
1819         ret=g_new0(struct displaylist_handle, 1);
1820         ret->hl_head=ret->hl=g_hash_to_list(displaylist->dl);
1821         ret->l_head=ret->l=g_hash_to_list_keys(ret->hl->data);
1822
1823         return ret;
1824 }
1825
1826 /**
1827  * FIXME
1828  * @param <>
1829  * @returns <>
1830  * @author Martin Schaller (04/2008)
1831 */
1832 struct displayitem * graphics_displaylist_next(struct displaylist_handle *dlh)
1833 {
1834         struct displayitem *ret;
1835         if (! dlh->l) {
1836                 dlh->hl=g_list_next(dlh->hl);
1837                 if (!dlh->hl)
1838                         return NULL;
1839                 g_list_free(dlh->l_head);
1840                 dlh->l_head=dlh->l=g_hash_to_list_keys(dlh->hl->data);
1841         }
1842         ret=dlh->l->data;
1843         dlh->l=g_list_next(dlh->l);
1844         return ret;
1845 }
1846
1847 /**
1848  * FIXME
1849  * @param <>
1850  * @returns <>
1851  * @author Martin Schaller (04/2008)
1852 */
1853 void graphics_displaylist_close(struct displaylist_handle *dlh)
1854 {
1855         g_list_free(dlh->hl_head);
1856         g_list_free(dlh->l_head);
1857         g_free(dlh);
1858 }
1859
1860 /**
1861  * FIXME
1862  * @param <>
1863  * @returns <>
1864  * @author Martin Schaller (04/2008)
1865 */
1866 struct displaylist * graphics_displaylist_new(void)
1867 {
1868         struct displaylist *ret=g_new(struct displaylist, 1);
1869
1870         ret->dl=g_hash_table_new(NULL,NULL);
1871
1872         return ret;
1873 }
1874
1875 /**
1876  * FIXME
1877  * @param <>
1878  * @returns <>
1879  * @author Martin Schaller (04/2008)
1880 */
1881 struct item * graphics_displayitem_get_item(struct displayitem *di)
1882 {
1883         return &di->item;       
1884 }
1885
1886 /**
1887  * FIXME
1888  * @param <>
1889  * @returns <>
1890  * @author Martin Schaller (04/2008)
1891 */
1892 char * graphics_displayitem_get_label(struct displayitem *di)
1893 {
1894         return di->label;
1895 }
1896
1897 /**
1898  * FIXME
1899  * @param <>
1900  * @returns <>
1901  * @author Martin Schaller (04/2008)
1902 */
1903 static int within_dist_point(struct point *p0, struct point *p1, int dist)
1904 {
1905         if (p0->x == 32767 || p0->y == 32767 || p1->x == 32767 || p1->y == 32767)
1906                 return 0;
1907         if (p0->x == -32768 || p0->y == -32768 || p1->x == -32768 || p1->y == -32768)
1908                 return 0;
1909         if ((p0->x-p1->x)*(p0->x-p1->x) + (p0->y-p1->y)*(p0->y-p1->y) <= dist*dist) {
1910                 return 1;
1911         }
1912         return 0;
1913 }
1914
1915 /**
1916  * FIXME
1917  * @param <>
1918  * @returns <>
1919  * @author Martin Schaller (04/2008)
1920 */
1921 static int within_dist_line(struct point *p, struct point *line_p0, struct point *line_p1, int dist)
1922 {
1923         int vx,vy,wx,wy;
1924         int c1,c2;
1925         struct point line_p;
1926
1927         vx=line_p1->x-line_p0->x;
1928         vy=line_p1->y-line_p0->y;
1929         wx=p->x-line_p0->x;
1930         wy=p->y-line_p0->y;
1931
1932         c1=vx*wx+vy*wy;
1933         if ( c1 <= 0 )
1934                 return within_dist_point(p, line_p0, dist);
1935         c2=vx*vx+vy*vy;
1936         if ( c2 <= c1 )
1937                 return within_dist_point(p, line_p1, dist);
1938
1939         line_p.x=line_p0->x+vx*c1/c2;
1940         line_p.y=line_p0->y+vy*c1/c2;
1941         return within_dist_point(p, &line_p, dist);
1942 }
1943
1944 /**
1945  * FIXME
1946  * @param <>
1947  * @returns <>
1948  * @author Martin Schaller (04/2008)
1949 */
1950 static int within_dist_polyline(struct point *p, struct point *line_pnt, int count, int dist, int close)
1951 {
1952         int i;
1953         for (i = 0 ; i < count-1 ; i++) {
1954                 if (within_dist_line(p,line_pnt+i,line_pnt+i+1,dist)) {
1955                         return 1;
1956                 }
1957         }
1958         if (close)
1959                 return (within_dist_line(p,line_pnt,line_pnt+count-1,dist));
1960         return 0;
1961 }
1962
1963 /**
1964  * FIXME
1965  * @param <>
1966  * @returns <>
1967  * @author Martin Schaller (04/2008)
1968 */
1969 static int within_dist_polygon(struct point *p, struct point *poly_pnt, int count, int dist)
1970 {
1971         int i, j, c = 0;
1972         for (i = 0, j = count-1; i < count; j = i++) {
1973                 if ((((poly_pnt[i].y <= p->y) && ( p->y < poly_pnt[j].y )) ||
1974                 ((poly_pnt[j].y <= p->y) && ( p->y < poly_pnt[i].y))) &&
1975                 (p->x < (poly_pnt[j].x - poly_pnt[i].x) * (p->y - poly_pnt[i].y) / (poly_pnt[j].y - poly_pnt[i].y) + poly_pnt[i].x)) 
1976                         c = !c;
1977         }
1978         if (! c)
1979                 return within_dist_polyline(p, poly_pnt, count, dist, 1);
1980         return c;
1981 }
1982
1983 /**
1984  * FIXME
1985  * @param <>
1986  * @returns <>
1987  * @author Martin Schaller (04/2008)
1988 */
1989 int graphics_displayitem_within_dist(struct displayitem *di, struct point *p, int dist)
1990 {
1991         struct point pa[16384];
1992         int count;
1993
1994         count=transform(tg, pg, di->c, pa, di->count, 1, 0, NULL);
1995         
1996         if (di->item.type < type_line) {
1997                 return within_dist_point(p, &pa[0], dist);
1998         }
1999         if (di->item.type < type_area) {
2000                 return within_dist_polyline(p, pa, count, dist, 0);
2001         }
2002         return within_dist_polygon(p, pa, count, dist);
2003 }