Fix:Core:Improved circle drawing
[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         GList *h, *l;
559         h=value;
560         l=h;
561         while (l) {
562                 struct displayitem *di=l->data;
563                 if (! di->displayed && di->item.type < type_line) 
564                         dbg(1,"warning: item '%s' not displayed\n", item_to_name(di->item.type));
565                 g_free(l->data);
566                 l=g_list_next(l);
567         }
568         g_list_free(h);
569         return TRUE;
570 }
571
572 /**
573  * FIXME
574  * @param <>
575  * @returns <>
576  * @author Martin Schaller (04/2008)
577 */
578 static void xdisplay_free(GHashTable *display_list)
579 {
580         g_hash_table_foreach_remove(display_list, xdisplay_free_list, NULL);
581 }
582
583 /**
584  * FIXME
585  * @param <>
586  * @returns <>
587  * @author Martin Schaller (04/2008)
588 */
589 static void display_add(struct displaylist *displaylist, struct item *item, int count, struct coord *c, char *label)
590 {
591         struct displayitem *di;
592         int len;
593         GList *l;
594         char *p;
595
596         len=sizeof(*di)+count*sizeof(*c);
597         if (label)
598                 len+=strlen(label)+1;
599
600         p=g_malloc(len);
601
602         di=(struct displayitem *)p;
603         di->displayed=0;
604         p+=sizeof(*di)+count*sizeof(*c);
605         di->item=*item;
606         if (label) {
607                 di->label=p;
608                 strcpy(di->label, label);
609         } else 
610                 di->label=NULL;
611         di->count=count;
612         memcpy(di->c, c, count*sizeof(*c));
613
614         l=g_hash_table_lookup(displaylist->dl, GINT_TO_POINTER(item->type));
615         l=g_list_prepend(l, di);
616         g_hash_table_insert(displaylist->dl, GINT_TO_POINTER(item->type), l);
617 }
618
619
620 /**
621  * FIXME
622  * @param <>
623  * @returns <>
624  * @author Martin Schaller (04/2008)
625 */
626 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)
627 {
628         int i,x,y,tl,tlm,th,thm,tlsq,l;
629         float lsq;
630         double dx,dy;
631         struct point p_t;
632         struct point pb[5];
633
634         if (gra->meth.get_text_bbox) {
635                 gra->meth.get_text_bbox(gra->priv, font->priv, label, 0x10000, 0x0, pb, 1);
636                 tl=(pb[2].x-pb[0].x);
637                 th=(pb[0].y-pb[1].y);
638         } else {
639                 tl=strlen(label)*4;
640                 th=8;
641         }
642         tlm=tl*128;
643         thm=th*144;
644         tlsq = tlm*tlm;
645         for (i = 0 ; i < count-1 ; i++) {
646                 dx=p[i+1].x-p[i].x;
647                 dx*=128;
648                 dy=p[i+1].y-p[i].y;
649                 dy*=128;
650                 lsq = dx*dx+dy*dy;
651                 if (lsq > tlsq) {
652                         l=(int)sqrtf(lsq);
653                         x=p[i].x;
654                         y=p[i].y;
655                         if (dx < 0) {
656                                 dx=-dx;
657                                 dy=-dy;
658                                 x=p[i+1].x;
659                                 y=p[i+1].y;
660                         }
661                         x+=(l-tlm)*dx/l/256;
662                         y+=(l-tlm)*dy/l/256;
663                         x-=dy*thm/l/256;
664                         y+=dx*thm/l/256;
665                         p_t.x=x;
666                         p_t.y=y;
667 #if 0
668                         dbg(0,"display_text: '%s', %d, %d, %d, %d %d\n", label, x, y, dx*0x10000/l, dy*0x10000/l, l);
669 #endif
670                         if (x < gra->r.rl.x && x + tl > gra->r.lu.x && y + tl > gra->r.lu.y && y - tl < gra->r.rl.y) 
671                                 gra->meth.draw_text(gra->priv, fg->priv, bg->priv, font->priv, label, &p_t, dx*0x10000/l, dy*0x10000/l);
672                 }
673         }
674 }
675
676 static void display_draw_arrow(struct point *p, int dx, int dy, int l, struct graphics_gc *gc, struct graphics *gra)
677 {
678         struct point pnt[3];
679         pnt[0]=pnt[1]=pnt[2]=*p;
680         pnt[0].x+=-dx*l/65536+dy*l/65536;
681         pnt[0].y+=-dy*l/65536-dx*l/65536;
682         pnt[2].x+=-dx*l/65536-dy*l/65536;
683         pnt[2].y+=-dy*l/65536+dx*l/65536;
684         gra->meth.draw_lines(gra->priv, gc->priv, pnt, 3);
685 }
686
687 static void display_draw_arrows(struct graphics *gra, struct graphics_gc *gc, struct point *pnt, int count)
688 {
689         int i,dx,dy,l;
690         struct point p;
691         for (i = 0 ; i < count-1 ; i++) {
692                 dx=pnt[i+1].x-pnt[i].x; 
693                 dy=pnt[i+1].y-pnt[i].y;
694                 l=sqrt(dx*dx+dy*dy);
695                 if (l) {
696                         dx=dx*65536/l;
697                         dy=dy*65536/l;
698                         p=pnt[i];
699                         p.x+=dx*15/65536;
700                         p.y+=dy*15/65536;
701                         display_draw_arrow(&p, dx, dy, 10, gc, gra);
702                         p=pnt[i+1];
703                         p.x-=dx*15/65536;
704                         p.y-=dy*15/65536;
705                         display_draw_arrow(&p, dx, dy, 10, gc, gra);
706                 }
707         }
708 }
709
710 static int
711 intersection(struct point * a1, int adx, int ady, struct point * b1, int bdx, int bdy,
712               struct point * res)
713 {
714         int n, a, b;
715         n = bdy * adx - bdx * ady;
716         a = bdx * (a1->y - b1->y) - bdy * (a1->x - b1->x);
717         b = adx * (a1->y - b1->y) - ady * (a1->x - b1->x);
718         if (n < 0) {
719                 n = -n;
720                 a = -a;
721                 b = -b;
722         }
723 #if 0
724         if (a < 0 || b < 0)
725                 return 0;
726         if (a > n || b > n)
727                 return 0;
728 #endif
729         if (n == 0)
730                 return 0;
731         res->x = a1->x + a * adx / n;
732         res->y = a1->y + a * ady / n;
733         return 1;
734 }
735
736 struct circle {
737         short x,y,fowler;
738 } circle64[]={
739 {0,128,0},
740 {13,127,13},
741 {25,126,25},
742 {37,122,38},
743 {49,118,53},
744 {60,113,67},
745 {71,106,85},
746 {81,99,104},
747 {91,91,128},
748 {99,81,152},
749 {106,71,171},
750 {113,60,189},
751 {118,49,203},
752 {122,37,218},
753 {126,25,231},
754 {127,13,243},
755 {128,0,256},
756 {127,-13,269},
757 {126,-25,281},
758 {122,-37,294},
759 {118,-49,309},
760 {113,-60,323},
761 {106,-71,341},
762 {99,-81,360},
763 {91,-91,384},
764 {81,-99,408},
765 {71,-106,427},
766 {60,-113,445},
767 {49,-118,459},
768 {37,-122,474},
769 {25,-126,487},
770 {13,-127,499},
771 {0,-128,512},
772 {-13,-127,525},
773 {-25,-126,537},
774 {-37,-122,550},
775 {-49,-118,565},
776 {-60,-113,579},
777 {-71,-106,597},
778 {-81,-99,616},
779 {-91,-91,640},
780 {-99,-81,664},
781 {-106,-71,683},
782 {-113,-60,701},
783 {-118,-49,715},
784 {-122,-37,730},
785 {-126,-25,743},
786 {-127,-13,755},
787 {-128,0,768},
788 {-127,13,781},
789 {-126,25,793},
790 {-122,37,806},
791 {-118,49,821},
792 {-113,60,835},
793 {-106,71,853},
794 {-99,81,872},
795 {-91,91,896},
796 {-81,99,920},
797 {-71,106,939},
798 {-60,113,957},
799 {-49,118,971},
800 {-37,122,986},
801 {-25,126,999},
802 {-13,127,1011},
803 };
804
805 static void
806 draw_circle(struct point *pnt, int diameter, int scale, int start, int len, struct point *res, int *pos, int dir)
807 {
808         struct circle *c;
809
810 #if 0
811         dbg(0,"diameter=%d start=%d len=%d pos=%d dir=%d\n", diameter, start, len, *pos, dir);
812 #endif
813         int count=64;
814         int end=start+len;
815         int i,step;
816         c=circle64;
817         if (diameter > 128)
818                 step=1;
819         else if (diameter > 64)
820                 step=2;
821         else if (diameter > 24)
822                 step=4;
823         else if (diameter > 8)
824                 step=8;
825         else
826                 step=16;
827         if (len > 0) {
828                 while (start < 0) {
829                         start+=1024;
830                         end+=1024;
831                 }
832                 while (end > 0) {
833                         i=0;
834                         while (i < count && c[i].fowler <= start)
835                                 i+=step;
836                         while (i < count && c[i].fowler < end) {
837                                 res[*pos].x=pnt->x+((c[i].x*diameter+128)>>8);
838                                 res[*pos].y=pnt->y+((c[i].y*diameter+128)>>8);
839                                 (*pos)+=dir;
840                                 i+=step;
841                         }
842                         end-=1024;
843                         start-=1024;
844                 }
845         } else {
846                 while (start > 1024) {
847                         start-=1024;
848                         end-=1024;
849                 }
850                 while (end < 1024) {
851                         i=count-1;
852                         while (i >= 0 && c[i].fowler >= start)
853                                 i-=step;
854                         while (i >= 0 && c[i].fowler > end) {
855                                 res[*pos].x=pnt->x+((c[i].x*diameter+128)>>8);
856                                 res[*pos].y=pnt->y+((c[i].y*diameter+128)>>8);
857                                 (*pos)+=dir;
858                                 i-=step;
859                         }
860                         start+=1024;
861                         end+=1024;
862                 }
863         }
864 }
865
866
867 static int
868 fowler(int dy, int dx)
869 {
870         int adx, ady;           /* Absolute Values of Dx and Dy */
871         int code;               /* Angular Region Classification Code */
872
873         adx = (dx < 0) ? -dx : dx;      /* Compute the absolute values. */
874         ady = (dy < 0) ? -dy : dy;
875
876         code = (adx < ady) ? 1 : 0;
877         if (dx < 0)
878                 code += 2;
879         if (dy < 0)
880                 code += 4;
881
882         switch (code) {
883         case 0:
884                 return (dx == 0) ? 0 : 128*ady / adx;   /* [  0, 45] */
885         case 1:
886                 return (256 - (128*adx / ady)); /* ( 45, 90] */
887         case 3:
888                 return (256 + (128*adx / ady)); /* ( 90,135) */
889         case 2:
890                 return (512 - (128*ady / adx)); /* [135,180] */
891         case 6:
892                 return (512 + (128*ady / adx)); /* (180,225] */
893         case 7:
894                 return (768 - (128*adx / ady)); /* (225,270) */
895         case 5:
896                 return (768 + (128*adx / ady)); /* [270,315) */
897         case 4:
898                 return (1024 - (128*ady / adx));/* [315,360) */
899         }
900         return 0;
901 }
902 static int
903 int_sqrt(unsigned int n)
904 {
905         unsigned int h, p= 0, q= 1, r= n;
906         while ( q <= n )
907                 q <<= 2;
908         while ( q != 1 ) {
909                 q >>= 2;
910                 h = p + q;
911                 p >>= 1;
912                 if ( r >= h ) {
913                         p += q;
914                         r -= h;
915                 }
916         }
917         return p;
918 }
919
920 struct offset {
921         int px,py,nx,ny;
922 };
923
924 static void
925 calc_offsets(int wi, int l, int dx, int dy, struct offset *res)
926 {
927         int x,y;
928         x = (dx * wi) / l;
929         y = (dy * wi) / l;
930         if (x < 0) {
931                 res->nx = -x/2;
932                 res->px = (x-1)/2;
933         } else {
934                 res->nx = -(x+1)/2;
935                 res->px = x/2;
936         }
937         if (y < 0) {
938                 res->ny = -y/2;
939                 res->py = (y-1)/2;
940         } else {
941                 res->ny = -(y+1)/2;
942                 res->py = y/2;
943         }
944 }
945
946 static void
947 graphics_draw_polyline_as_polygon(struct graphics *gra, struct graphics_gc *gc, struct point *pnt, int count, int *width, int step)
948 {
949         int maxpoints=200;
950         struct point res[maxpoints], pos, poso, neg, nego;
951         int i, dx=0, dy=0, l=0, dxo=0, dyo=0;
952         struct offset o,oo;
953         int fow=0, fowo=0, delta;
954         int wi, ppos = maxpoints/2, npos = maxpoints/2;
955         int state,prec=5;
956         int max_circle_points=20;
957         for (i = 0; i < count; i++) {
958                 wi=*width;
959                 width+=step;
960                 if (i < count - 1) {
961                         dx = (pnt[i + 1].x - pnt[i].x);
962                         dy = (pnt[i + 1].y - pnt[i].y);
963                         l = int_sqrt(dx * dx + dy * dy);
964                         fow=fowler(-dy, dx);
965                 }
966                 if (! l) 
967                         l=1;
968                 calc_offsets(wi, l, dx, dy, &o);
969                 pos.x = pnt[i].x + o.ny;
970                 pos.y = pnt[i].y + o.px;
971                 neg.x = pnt[i].x + o.py;
972                 neg.y = pnt[i].y + o.nx;
973                 if (! i)
974                         state=0;
975                 else if (i == count-1) 
976                         state=2;
977                 else if (npos < max_circle_points || ppos >= maxpoints-max_circle_points)
978                         state=3;
979                 else
980                         state=1;
981                 switch (state) {
982                 case 1:
983                        if (fowo != fow) {
984                                 poso.x = pnt[i].x + oo.ny;
985                                 poso.y = pnt[i].y + oo.px;
986                                 nego.x = pnt[i].x + oo.py;
987                                 nego.y = pnt[i].y + oo.nx;
988                                 delta=fowo-fow;
989                                 if (delta < 0)
990                                         delta+=1024;
991                                 if (delta < 512) {
992                                         if (intersection(&pos, dx, dy, &poso, dxo, dyo, &res[ppos]))
993                                                 ppos++;
994                                         res[--npos] = nego;
995                                         --npos;
996                                         draw_circle(&pnt[i], wi, prec, fowo-512, -delta, res, &npos, -1);
997                                         res[npos] = neg;
998                                 } else {
999                                         res[ppos++] = poso;
1000                                         draw_circle(&pnt[i], wi, prec, fowo, 1024-delta, res, &ppos, 1);
1001                                         res[ppos++] = pos;
1002                                         if (intersection(&neg, dx, dy, &nego, dxo, dyo, &res[npos - 1]))
1003                                                 npos--;
1004                                 }
1005                         }
1006                         break;
1007                 case 2:
1008                 case 3:
1009                         res[--npos] = neg;
1010                         --npos;
1011                         draw_circle(&pnt[i], wi, prec, fow-512, -512, res, &npos, -1);
1012                         res[npos] = pos;
1013                         res[ppos++] = pos;
1014                         dbg_assert(npos > 0);
1015                         dbg_assert(ppos < maxpoints);
1016                         gra->meth.draw_polygon(gra->priv, gc->priv, res+npos, ppos-npos);
1017                         if (state == 2)
1018                                 break;
1019                         npos=maxpoints/2;
1020                         ppos=maxpoints/2;
1021                 case 0:
1022                         res[ppos++] = neg;
1023                         draw_circle(&pnt[i], wi, prec, fow+512, 512, res, &ppos, 1);
1024                         res[ppos++] = pos;
1025                         break;
1026                 }
1027                 if (step) {
1028                         wi=*width;
1029                         calc_offsets(wi, l, dx, dy, &oo);
1030                 } else 
1031                         oo=o;
1032                 dxo = -dx;
1033                 dyo = -dy;
1034                 fowo=fow;
1035         }
1036 }
1037
1038 struct transformation *tg;
1039 enum projection pg;
1040
1041 struct wpoint {
1042         int x,y,w;
1043 };
1044
1045 static int
1046 clipcode(struct wpoint *p, struct point_rect *r)
1047 {
1048         int code=0;
1049         if (p->x < r->lu.x)
1050                 code=1;
1051         if (p->x > r->rl.x)
1052                 code=2;
1053         if (p->y < r->lu.y)
1054                 code |=4;
1055         if (p->y > r->rl.y)
1056                 code |=8;
1057         return code;
1058 }
1059
1060
1061 static int
1062 clip_line(struct wpoint *p1, struct wpoint *p2, struct point_rect *r)
1063 {
1064         int code1,code2,ret=1;
1065         int dx,dy,dw;
1066         code1=clipcode(p1, r);
1067         if (code1)
1068                 ret |= 2;
1069         code2=clipcode(p2, r);
1070         if (code2)
1071                 ret |= 4;
1072         dx=p2->x-p1->x;
1073         dy=p2->y-p1->y;
1074         dw=p2->w-p1->w;
1075         while (code1 || code2) {
1076                 if (code1 & code2)
1077                         return 0;
1078                 if (code1 & 1) {
1079                         p1->y+=(r->lu.x-p1->x)*dy/dx;
1080                         p1->w+=(r->lu.x-p1->x)*dw/dx;
1081                         p1->x=r->lu.x;
1082                 } else if (code1 & 2) {
1083                         p1->y+=(r->rl.x-p1->x)*dy/dx;
1084                         p1->w+=(r->rl.x-p1->x)*dw/dx;
1085                         p1->x=r->rl.x;
1086                 } else if (code1 & 4) {
1087                         p1->x+=(r->lu.y-p1->y)*dx/dy;
1088                         p1->w+=(r->lu.y-p1->y)*dw/dy;
1089                         p1->y=r->lu.y;
1090                 } else if (code1 & 8) {
1091                         p1->x+=(r->rl.y-p1->y)*dx/dy;
1092                         p1->w+=(r->rl.y-p1->y)*dw/dy;
1093                         p1->y=r->rl.y;
1094                 }
1095                 code1=clipcode(p1, r);
1096                 if (code1 & code2)
1097                         return 0;
1098                 if (code2 & 1) {
1099                         p2->y+=(r->lu.x-p2->x)*dy/dx;
1100                         p2->w+=(r->lu.x-p2->x)*dw/dx;
1101                         p2->x=r->lu.x;
1102                 } else if (code2 & 2) {
1103                         p2->y+=(r->rl.x-p2->x)*dy/dx;
1104                         p2->w+=(r->rl.x-p2->x)*dw/dx;
1105                         p2->x=r->rl.x;
1106                 } else if (code2 & 4) {
1107                         p2->x+=(r->lu.y-p2->y)*dx/dy;
1108                         p2->w+=(r->lu.y-p2->y)*dw/dy;
1109                         p2->y=r->lu.y;
1110                 } else if (code2 & 8) {
1111                         p2->x+=(r->rl.y-p2->y)*dx/dy;
1112                         p2->w+=(r->rl.y-p2->y)*dw/dy;
1113                         p2->y=r->rl.y;
1114                 }
1115                 code2=clipcode(p2, r);
1116         }
1117         return ret;
1118 }
1119
1120 static void
1121 graphics_draw_polyline_clipped(struct graphics *gra, struct graphics_gc *gc, struct point *pa, int count, int *width, int step, int poly)
1122 {
1123         struct point p[count+1];
1124         int w[count*step+1];
1125         struct wpoint p1,p2;
1126         int i,code,out=0;
1127         int wmax;
1128         struct point_rect r=gra->r;
1129
1130         wmax=width[0];
1131         if (step) {
1132                 for (i = 1 ; i < count ; i++) {
1133                         if (width[i*step] > wmax)
1134                                 wmax=width[i*step];
1135                 }
1136         }
1137         if (wmax <= 0)
1138                 return;
1139         r.lu.x-=wmax;
1140         r.lu.y-=wmax;
1141         r.rl.x+=wmax;
1142         r.rl.y+=wmax;
1143         for (i = 0 ; i < count ; i++) {
1144                 if (i) {
1145                         p1.x=pa[i-1].x;
1146                         p1.y=pa[i-1].y;
1147                         p1.w=width[(i-1)*step];
1148                         p2.x=pa[i].x;
1149                         p2.y=pa[i].y;
1150                         p2.w=width[i*step];
1151                         /* 0 = invisible, 1 = completely visible, 3 = start point clipped, 5 = end point clipped, 7 both points clipped */
1152                         code=clip_line(&p1, &p2, &r);
1153                         if (((code == 1 || code == 5) && i == 1) || (code & 2)) {
1154                                 p[out].x=p1.x;
1155                                 p[out].y=p1.y;
1156                                 w[out*step]=p1.w;
1157                                 out++;
1158                         }
1159                         if (code) {
1160                                 p[out].x=p2.x;
1161                                 p[out].y=p2.y;
1162                                 w[out*step]=p2.w;
1163                                 out++;
1164                         }
1165                         if (i == count-1 || (code & 4)) {
1166                                 if (out > 1) {
1167                                         if (poly) {     
1168                                                 graphics_draw_polyline_as_polygon(gra, gc, p, out, w, step);
1169                                         } else
1170                                                 gra->meth.draw_lines(gra->priv, gc->priv, p, out);
1171                                         out=0;
1172                                 }
1173                         }
1174                 }
1175         }
1176 }
1177
1178 static int
1179 is_inside(struct point *p, struct point_rect *r, int edge)
1180 {
1181         switch(edge) {
1182         case 0:
1183                 return p->x >= r->lu.x;
1184         case 1:
1185                 return p->x <= r->rl.x;
1186         case 2:
1187                 return p->y >= r->lu.y;
1188         case 3:
1189                 return p->y <= r->rl.y;
1190         default:
1191                 return 0;
1192         }
1193 }
1194
1195 static void
1196 poly_intersection(struct point *p1, struct point *p2, struct point_rect *r, int edge, struct point *ret)
1197 {
1198         int dx=p2->x-p1->x;
1199         int dy=p2->y-p1->y;
1200         switch(edge) {
1201         case 0:
1202                 ret->y=p1->y+(r->lu.x-p1->x)*dy/dx;
1203                 ret->x=r->lu.x;
1204                 break;
1205         case 1:
1206                 ret->y=p1->y+(r->rl.x-p1->x)*dy/dx;
1207                 ret->x=r->rl.x;
1208                 break;
1209         case 2:
1210                 ret->x=p1->x+(r->lu.y-p1->y)*dx/dy;
1211                 ret->y=r->lu.y;
1212                 break;
1213         case 3:
1214                 ret->x=p1->x+(r->rl.y-p1->y)*dx/dy;
1215                 ret->y=r->rl.y;
1216                 break;
1217         }
1218 }
1219
1220 static void
1221 graphics_draw_polygon_clipped(struct graphics *gra, struct graphics_gc *gc, struct point *pin, int count_in)
1222 {
1223         struct point_rect r=gra->r;
1224         struct point *pout,*p,*s,pi;
1225         struct point p1[count_in+1];
1226         struct point p2[count_in+1];
1227         int count_out,edge=3;
1228         int i;
1229 #if 0
1230         r.lu.x+=20;
1231         r.lu.y+=20;
1232         r.rl.x-=20;
1233         r.rl.y-=20;
1234 #endif
1235
1236         pout=p1;
1237         for (edge = 0 ; edge < 4 ; edge++) {
1238                 p=pin;
1239                 s=pin+count_in-1;
1240                 count_out=0;
1241                 for (i = 0 ; i < count_in ; i++) {
1242                         if (is_inside(p, &r, edge)) {
1243                                 if (! is_inside(s, &r, edge)) {
1244                                         poly_intersection(s,p,&r,edge,&pi);
1245                                         pout[count_out++]=pi;
1246                                 }
1247                                 pout[count_out++]=*p;
1248                         } else {
1249                                 if (is_inside(s, &r, edge)) {
1250                                         poly_intersection(p,s,&r,edge,&pi);
1251                                         pout[count_out++]=pi;
1252                                 }
1253                         }
1254                         s=p;
1255                         p++;
1256                 }
1257                 count_in=count_out;
1258                 if (pin == p1) {
1259                         pin=p2;
1260                         pout=p1;
1261                 } else {
1262                         pin=p1;
1263                         pout=p2;
1264                 }
1265         }
1266         gra->meth.draw_polygon(gra->priv, gc->priv, pin, count_in);
1267 }
1268
1269
1270
1271 /**
1272  * FIXME
1273  * @param <>
1274  * @returns <>
1275  * @author Martin Schaller (04/2008)
1276 */
1277 static void xdisplay_draw_elements(struct graphics *gra, GHashTable *display_list, struct itemgra *itm)
1278 {
1279         struct element *e;
1280         GList *l,*ls,*es,*types;
1281         enum item_type type;
1282         struct graphics_gc *gc = NULL;
1283         struct graphics_image *img=NULL;
1284         struct point p;
1285         char path[PATH_MAX];
1286         struct point pa[16384];
1287         int width[16384];
1288         int count;
1289
1290         es=itm->elements;
1291         while (es) {
1292                 e=es->data;
1293                 types=itm->type;
1294                 while (types) {
1295                         type=GPOINTER_TO_INT(types->data);
1296                         ls=g_hash_table_lookup(display_list, GINT_TO_POINTER(type));
1297                         l=ls;
1298                         if (gc)
1299                                 graphics_gc_destroy(gc);
1300                         if (img)
1301                                 graphics_image_free(gra, img);
1302                         gc=NULL;
1303                         img=NULL;
1304                         while (l) {
1305                                 struct displayitem *di;
1306                                 di=l->data;
1307                                 di->displayed=1;
1308                                 if (! gc) {
1309                                         gc=graphics_gc_new(gra);
1310                                         gc->meth.gc_set_foreground(gc->priv, &e->color);
1311                                 }
1312                                 if (e->type == element_polyline) {
1313                                         count=transform(tg, pg, di->c, pa, di->count, 1, e->u.polyline.width, width);
1314                                 }
1315                                 else
1316                                         count=transform(tg, pg, di->c, pa, di->count, 1, 0, NULL);
1317                                 switch (e->type) {
1318                                 case element_polygon:
1319 #if 0
1320                                         {
1321                                                 int i;
1322                                                 for (i = 0 ; i < count ; i++) {
1323                                                         dbg(0,"pa[%d]=%d,%d\n", i, pa[i].x, pa[i].y);
1324                                                 }
1325                                         }
1326                                         dbg(0,"element_polygon count=%d\n",count);
1327 #endif
1328 #if 1
1329                                         graphics_draw_polygon_clipped(gra, gc, pa, count);
1330 #endif
1331                                         break;
1332                                 case element_polyline:
1333 #if 0
1334                                         if (e->u.polyline.width > 1) {
1335                                                 graphics_draw_polyline_as_polygon(gra, gc, pa, count, width, 0);
1336                                         } else {
1337 #else
1338                                         {
1339 #if 0
1340                                                  if (e->u.polyline.width > 1)
1341                                                              gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
1342 #else
1343                                                 gc->meth.gc_set_linewidth(gc->priv, 1);
1344 #endif
1345
1346                                                 
1347 #endif
1348                                                 if (e->u.polyline.width > 0 && e->u.polyline.dash_num > 0)
1349                                                         graphics_gc_set_dashes(gc, e->u.polyline.width, 
1350                                                                                e->u.polyline.offset,
1351                                                                                e->u.polyline.dash_table,
1352                                                                                e->u.polyline.dash_num);
1353 #if 0
1354                                                 if (di->label && !strcmp(di->label, "Bahnhofstr.") && di->item.type != type_street_1_city) {
1355                                                         dbg(0,"0x%x,0x%x %s\n", di->item.id_hi, di->item.id_lo, item_to_name(di->item.type));
1356 #endif
1357                                                 graphics_draw_polyline_clipped(gra, gc, pa, count, width, 1, e->u.polyline.width > 1);
1358 #if 0
1359                                                 }
1360 #endif
1361                                         }
1362                                         break;
1363                                 case element_circle:
1364                                         if (count) {
1365                                                 if (e->u.circle.width > 1) 
1366                                                         gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
1367                                                 gra->meth.draw_circle(gra->priv, gc->priv, pa, e->u.circle.radius);
1368                                                 if (di->label && e->text_size) {
1369                                                         p.x=pa[0].x+3;
1370                                                         p.y=pa[0].y+10;
1371                                                         if (! gra->font[e->text_size])
1372                                                                 gra->font[e->text_size]=graphics_font_new(gra, e->text_size*20, 0);
1373                                                         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);
1374                                                 }
1375                                         }
1376                                         break;
1377                                 case element_text:
1378                                         if (count && di->label) {
1379                                                 if (! gra->font[e->text_size])
1380                                                         gra->font[e->text_size]=graphics_font_new(gra, e->text_size*20, 0);
1381                                                 label_line(gra, gra->gc[2], gra->gc[1], gra->font[e->text_size], pa, count, di->label);
1382                                         }
1383                                         break;
1384                                 case element_icon:
1385                                         if (count) {
1386                                                 if (!img) {
1387                                                         if (e->u.icon.src[0] == '/')
1388                                                                 strcpy(path,e->u.icon.src);
1389                                                         else
1390                                                                 sprintf(path,"%s/xpm/%s", navit_sharedir, e->u.icon.src);
1391                                                         img=graphics_image_new_scaled_rotated(gra, path, e->u.icon.width, e->u.icon.height, e->u.icon.rotation);
1392                                                         if (! img)
1393                                                                 dbg(0,"failed to load icon '%s'\n", e->u.icon.src);
1394                                                 }
1395                                                 if (img) {
1396                                                         p.x=pa[0].x - img->hot.x;
1397                                                         p.y=pa[0].y - img->hot.y;
1398                                                         gra->meth.draw_image(gra->priv, gra->gc[0]->priv, &p, img->priv);
1399                                                 }
1400                                         }
1401                                         break;
1402                                 case element_image:
1403                                         dbg(1,"image: '%s'\n", di->label);
1404                                         if (gra->meth.draw_image_warp)
1405                                                 gra->meth.draw_image_warp(gra->priv, gra->gc[0]->priv, pa, count, di->label);
1406                                         else
1407                                                 dbg(0,"draw_image_warp not supported by graphics driver drawing '%s'\n", di->label);
1408                                         break;
1409                                 case element_arrows:
1410                                         display_draw_arrows(gra,gc,pa,count);
1411                                         break;
1412                                 default:
1413                                         printf("Unhandled element type %d\n", e->type);
1414                                 
1415                                 }
1416                                 l=g_list_next(l);
1417                         }
1418                         types=g_list_next(types);
1419                 }
1420                 es=g_list_next(es);
1421         }
1422         if (gc)
1423                 graphics_gc_destroy(gc);
1424         if (img)
1425                 graphics_image_free(gra, img);
1426 }
1427
1428 void
1429 graphics_draw_itemgra(struct graphics *gra, struct itemgra *itm, struct transformation *t)
1430 {
1431         GList *es;
1432         struct point p;
1433         struct coord c;
1434         char *label=NULL;
1435         struct graphics_gc *gc = NULL;
1436         struct graphics_image *img;
1437         char path[PATH_MAX];
1438         es=itm->elements;
1439         c.x=0;
1440         c.y=0;
1441         while (es) {
1442                 struct element *e=es->data;
1443                 int count=e->coord_count;
1444                 struct point pnt[count+1];
1445                 if (count)
1446                         transform(t, projection_screen, e->coord, pnt, count, 0, 0, NULL);
1447                 else {
1448                         transform(t, projection_screen, &c, pnt, 1, 0, 0, NULL);
1449                         count=1;
1450                 }
1451                 gc=graphics_gc_new(gra);
1452                 gc->meth.gc_set_foreground(gc->priv, &e->color);
1453                 switch (e->type) {
1454                 case element_polyline:
1455                         if (e->u.polyline.width > 1) 
1456                                 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
1457                         if (e->u.polyline.width > 0 && e->u.polyline.dash_num > 0)
1458                                 graphics_gc_set_dashes(gc, e->u.polyline.width, 
1459                                                        e->u.polyline.offset,
1460                                                        e->u.polyline.dash_table,
1461                                                        e->u.polyline.dash_num);
1462                         gra->meth.draw_lines(gra->priv, gc->priv, pnt, count);
1463                         break;
1464                 case element_circle:
1465                         if (e->u.circle.width > 1) 
1466                                 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
1467                         gra->meth.draw_circle(gra->priv, gc->priv, &pnt[0], e->u.circle.radius);
1468                         if (label && e->text_size) {
1469                                 p.x=pnt[0].x+3;
1470                                 p.y=pnt[0].y+10;
1471                         if (! gra->font[e->text_size])
1472                                 gra->font[e->text_size]=graphics_font_new(gra, e->text_size*20, 0);
1473                                 gra->meth.draw_text(gra->priv, gra->gc[2]->priv, gra->gc[1]->priv, gra->font[e->text_size]->priv, label, &p, 0x10000, 0);
1474                         }
1475                         break;
1476                 case element_icon:
1477                         if (e->u.icon.src[0] == '/') 
1478                                 strcpy(path,e->u.icon.src);
1479                         else
1480                                 sprintf(path,"%s/xpm/%s", navit_sharedir, e->u.icon.src);
1481                         img=graphics_image_new_scaled_rotated(gra, path, e->u.icon.width, e->u.icon.height, e->u.icon.rotation);
1482                         if (! img)
1483                                 dbg(0,"failed to load icon '%s'\n", e->u.icon.src);
1484                         else {
1485                                 p.x=pnt[0].x - img->hot.x;
1486                                 p.y=pnt[0].y - img->hot.y;
1487                                 gra->meth.draw_image(gra->priv, gc->priv, &p, img->priv);
1488                                 graphics_image_free(gra, img);
1489                         }
1490                         break;
1491                 default:
1492                         dbg(0,"dont know how to draw %d\n", e->type);
1493                 }
1494                 graphics_gc_destroy(gc);
1495                 es=g_list_next(es);
1496         }
1497 }
1498
1499 /**
1500  * FIXME
1501  * @param <>
1502  * @returns <>
1503  * @author Martin Schaller (04/2008)
1504 */
1505 static void xdisplay_draw_layer(GHashTable *display_list, struct graphics *gra, struct layer *lay, int order)
1506 {
1507         GList *itms;
1508         struct itemgra *itm;
1509
1510         itms=lay->itemgras;
1511         while (itms) {
1512                 itm=itms->data;
1513                 if (order >= itm->order.min && order <= itm->order.max) 
1514                         xdisplay_draw_elements(gra, display_list, itm);
1515                 itms=g_list_next(itms);
1516         }
1517 }
1518
1519 /**
1520  * FIXME
1521  * @param <>
1522  * @returns <>
1523  * @author Martin Schaller (04/2008)
1524 */
1525 static void xdisplay_draw(GHashTable *display_list, struct graphics *gra, struct layout *l, int order)
1526 {
1527         GList *lays;
1528         struct layer *lay;
1529         
1530         lays=l->layers;
1531         while (lays) {
1532                 lay=lays->data;
1533                 xdisplay_draw_layer(display_list, gra, lay, order);
1534                 lays=g_list_next(lays);
1535         }
1536 }
1537
1538 /**
1539  * FIXME
1540  * @param <>
1541  * @returns <>
1542  * @author Martin Schaller (04/2008)
1543 */
1544 extern void *route_selection;
1545
1546 /**
1547  * FIXME
1548  * @param <>
1549  * @returns <>
1550  * @author Martin Schaller (04/2008)
1551 */
1552 static void do_draw_map(struct displaylist *displaylist, struct transformation *t, struct map *m, int order)
1553 {
1554         enum projection pro;
1555         struct map_rect *mr;
1556         struct item *item;
1557         int conv,count,max=16384;
1558         struct coord ca[max];
1559         struct attr attr;
1560         struct map_selection *sel;
1561
1562         pro=map_projection(m);
1563         conv=map_requires_conversion(m);
1564         sel=transform_get_selection(t, pro, order);
1565         tg=t;
1566         pg=pro;
1567 #if 0
1568         sel=NULL;
1569 #endif
1570         if (route_selection)
1571                 mr=map_rect_new(m, route_selection);
1572         else
1573                 mr=map_rect_new(m, sel);
1574         if (! mr) {
1575                 map_selection_destroy(sel);
1576                 return;
1577         }
1578         while ((item=map_rect_get_item(mr))) {
1579 #if 0
1580                 if (num < 7599 || num > 7599)
1581                         continue;
1582 #endif
1583 #if 0
1584                 if (item->id_hi != 0xb0031 || item->id_lo != 0x20c9aeea)
1585                         continue;
1586 #endif
1587                 count=item_coord_get_within_selection(item, ca, item->type < type_line ? 1: max, sel);
1588                 if (! count)
1589                         continue;
1590                 if (item->type >= type_line && count < 2) {
1591                         dbg(1,"poly from map has only %d points\n", count);
1592                         continue;
1593                 }
1594                 if (item->type < type_line) {
1595 #if 0
1596                         if (! map_selection_contains_point(sel, &ca[0])) {
1597                                 dbg(1,"point not visible\n");
1598                                 continue;
1599                         }
1600 #endif
1601                 } else if (item->type < type_area) {
1602 #if 0
1603                         if (! map_selection_contains_polyline(sel, ca, count)) {
1604                                 dbg(1,"polyline not visible\n");
1605                                 continue;
1606                         }
1607 #endif
1608                 } else {
1609 #if 0
1610                         if (! map_selection_contains_polygon(sel, ca, count)) {
1611                                 dbg(1,"polygon not visible\n");
1612                                 continue;
1613                         }
1614 #endif
1615                 }
1616                 if (count == max) 
1617                         dbg(0,"point count overflow %d\n", count);
1618                 if (!item_attr_get(item, attr_label, &attr))
1619                         attr.u.str=NULL;
1620                 if (conv && attr.u.str && attr.u.str[0]) {
1621                         char *str=map_convert_string(m, attr.u.str);
1622                         display_add(displaylist, item, count, ca, str);
1623                         map_convert_free(str);
1624                 } else
1625                         display_add(displaylist, item, count, ca, attr.u.str);
1626         }
1627         map_rect_destroy(mr);
1628         map_selection_destroy(sel);
1629 }
1630
1631 /**
1632  * FIXME
1633  * @param <>
1634  * @returns <>
1635  * @author Martin Schaller (04/2008)
1636 */
1637 static void do_draw(struct displaylist *displaylist, struct transformation *t, GList *mapsets, int order)
1638 {
1639         struct mapset *ms;
1640         struct map *m;
1641         struct mapset_handle *h;
1642
1643         if (! mapsets)
1644                 return;
1645         ms=mapsets->data;
1646         h=mapset_open(ms);
1647         while ((m=mapset_next(h, 1))) {
1648                 do_draw_map(displaylist, t, m, order);
1649         }
1650         mapset_close(h);
1651 }
1652
1653 /**
1654  * FIXME
1655  * @param <>
1656  * @returns <>
1657  * @author Martin Schaller (04/2008)
1658 */
1659 int graphics_ready(struct graphics *this_)
1660 {
1661         return this_->ready;
1662 }
1663
1664 /**
1665  * FIXME
1666  * @param <>
1667  * @returns <>
1668  * @author Martin Schaller (04/2008)
1669 */
1670 void graphics_displaylist_draw(struct graphics *gra, struct displaylist *displaylist, struct transformation *trans, struct layout *l, int callback)
1671 {
1672         int order=transform_get_order(trans);
1673         struct point p;
1674         tg=trans;
1675         p.x=0;
1676         p.y=0;
1677         // FIXME find a better place to set the background color
1678         if (l) {
1679                 graphics_gc_set_background(gra->gc[0], &l->color);
1680                 graphics_gc_set_foreground(gra->gc[0], &l->color);
1681                 gra->default_font = g_strdup(l->font);
1682         }
1683         graphics_background_gc(gra, gra->gc[0]);
1684         gra->meth.draw_mode(gra->priv, draw_mode_begin);
1685         gra->meth.draw_rectangle(gra->priv, gra->gc[0]->priv, &p, 32767, 32767);
1686         if (l) 
1687                 xdisplay_draw(displaylist->dl, gra, l, order+l->order_delta);
1688         if (callback)
1689                 callback_list_call_attr_0(gra->cbl, attr_postdraw);
1690         gra->meth.draw_mode(gra->priv, draw_mode_end);
1691 }
1692
1693 #if 0
1694 /**
1695  * FIXME
1696  * @param <>
1697  * @returns <>
1698  * @author Martin Schaller (04/2008)
1699 */
1700 void graphics_displaylist_move(struct displaylist *displaylist, int dx, int dy)
1701 {
1702         struct displaylist_handle *dlh;
1703         struct displayitem *di;
1704         int i;
1705
1706         dlh=graphics_displaylist_open(displaylist);
1707         while ((di=graphics_displaylist_next(dlh))) {
1708                 for (i = 0 ; i < di->count ; i++) {
1709                         di->pnt[i].x+=dx;
1710                         di->pnt[i].y+=dy;
1711                 }
1712         }
1713         graphics_displaylist_close(dlh);
1714 }
1715 #endif
1716
1717 /**
1718  * FIXME
1719  * @param <>
1720  * @returns <>
1721  * @author Martin Schaller (04/2008)
1722 */
1723 void graphics_draw(struct graphics *gra, struct displaylist *displaylist, GList *mapsets, struct transformation *trans, struct layout *l)
1724 {
1725         int order=transform_get_order(trans);
1726
1727         dbg(1,"enter");
1728
1729 #if 0
1730         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));
1731 #endif
1732         
1733         xdisplay_free(displaylist->dl);
1734         dbg(1,"order=%d\n", order);
1735
1736
1737 #if 0
1738         for (i = 0 ; i < data_window_type_end; i++) {
1739                 data_window_begin(co->data_window[i]);  
1740         }
1741 #endif
1742         profile(0,NULL);
1743         if (l)
1744                 order+=l->order_delta;
1745         if (mapsets)
1746                 do_draw(displaylist, trans, mapsets, order);
1747 //      profile(1,"do_draw");
1748         graphics_displaylist_draw(gra, displaylist, trans, l, 1);
1749         profile(1,"xdisplay_draw");
1750         profile(0,"end");
1751   
1752 #if 0
1753         for (i = 0 ; i < data_window_type_end; i++) {
1754                 data_window_end(co->data_window[i]);    
1755         }
1756 #endif
1757         gra->ready=1;
1758 }
1759
1760 /**
1761  * FIXME
1762  * @param <>
1763  * @returns <>
1764  * @author Martin Schaller (04/2008)
1765 */
1766 struct displaylist_handle {
1767         GList *hl_head,*hl,*l;
1768 };
1769
1770 /**
1771  * FIXME
1772  * @param <>
1773  * @returns <>
1774  * @author Martin Schaller (04/2008)
1775 */
1776 struct displaylist_handle * graphics_displaylist_open(struct displaylist *displaylist)
1777 {
1778         struct displaylist_handle *ret;
1779
1780         ret=g_new0(struct displaylist_handle, 1);
1781         ret->hl_head=ret->hl=g_hash_to_list(displaylist->dl);
1782
1783         return ret;
1784 }
1785
1786 /**
1787  * FIXME
1788  * @param <>
1789  * @returns <>
1790  * @author Martin Schaller (04/2008)
1791 */
1792 struct displayitem * graphics_displaylist_next(struct displaylist_handle *dlh)
1793 {
1794         struct displayitem *ret;
1795         if (! dlh->l) {
1796                 if (!dlh->hl)
1797                         return NULL;
1798                 dlh->l=dlh->hl->data;
1799                 dlh->hl=g_list_next(dlh->hl);
1800         }
1801         ret=dlh->l->data;
1802         dlh->l=g_list_next(dlh->l);
1803         return ret;
1804 }
1805
1806 /**
1807  * FIXME
1808  * @param <>
1809  * @returns <>
1810  * @author Martin Schaller (04/2008)
1811 */
1812 void graphics_displaylist_close(struct displaylist_handle *dlh)
1813 {
1814         g_list_free(dlh->hl_head);
1815         g_free(dlh);
1816 }
1817
1818 /**
1819  * FIXME
1820  * @param <>
1821  * @returns <>
1822  * @author Martin Schaller (04/2008)
1823 */
1824 struct displaylist * graphics_displaylist_new(void)
1825 {
1826         struct displaylist *ret=g_new(struct displaylist, 1);
1827
1828         ret->dl=g_hash_table_new(NULL,NULL);
1829
1830         return ret;
1831 }
1832
1833 /**
1834  * FIXME
1835  * @param <>
1836  * @returns <>
1837  * @author Martin Schaller (04/2008)
1838 */
1839 struct item * graphics_displayitem_get_item(struct displayitem *di)
1840 {
1841         return &di->item;       
1842 }
1843
1844 /**
1845  * FIXME
1846  * @param <>
1847  * @returns <>
1848  * @author Martin Schaller (04/2008)
1849 */
1850 char * graphics_displayitem_get_label(struct displayitem *di)
1851 {
1852         return di->label;
1853 }
1854
1855 /**
1856  * FIXME
1857  * @param <>
1858  * @returns <>
1859  * @author Martin Schaller (04/2008)
1860 */
1861 static int within_dist_point(struct point *p0, struct point *p1, int dist)
1862 {
1863         if (p0->x == 32767 || p0->y == 32767 || p1->x == 32767 || p1->y == 32767)
1864                 return 0;
1865         if (p0->x == -32768 || p0->y == -32768 || p1->x == -32768 || p1->y == -32768)
1866                 return 0;
1867         if ((p0->x-p1->x)*(p0->x-p1->x) + (p0->y-p1->y)*(p0->y-p1->y) <= dist*dist) {
1868                 return 1;
1869         }
1870         return 0;
1871 }
1872
1873 /**
1874  * FIXME
1875  * @param <>
1876  * @returns <>
1877  * @author Martin Schaller (04/2008)
1878 */
1879 static int within_dist_line(struct point *p, struct point *line_p0, struct point *line_p1, int dist)
1880 {
1881         int vx,vy,wx,wy;
1882         int c1,c2;
1883         struct point line_p;
1884
1885         vx=line_p1->x-line_p0->x;
1886         vy=line_p1->y-line_p0->y;
1887         wx=p->x-line_p0->x;
1888         wy=p->y-line_p0->y;
1889
1890         c1=vx*wx+vy*wy;
1891         if ( c1 <= 0 )
1892                 return within_dist_point(p, line_p0, dist);
1893         c2=vx*vx+vy*vy;
1894         if ( c2 <= c1 )
1895                 return within_dist_point(p, line_p1, dist);
1896
1897         line_p.x=line_p0->x+vx*c1/c2;
1898         line_p.y=line_p0->y+vy*c1/c2;
1899         return within_dist_point(p, &line_p, dist);
1900 }
1901
1902 /**
1903  * FIXME
1904  * @param <>
1905  * @returns <>
1906  * @author Martin Schaller (04/2008)
1907 */
1908 static int within_dist_polyline(struct point *p, struct point *line_pnt, int count, int dist, int close)
1909 {
1910         int i;
1911         for (i = 0 ; i < count-1 ; i++) {
1912                 if (within_dist_line(p,line_pnt+i,line_pnt+i+1,dist)) {
1913                         return 1;
1914                 }
1915         }
1916         if (close)
1917                 return (within_dist_line(p,line_pnt,line_pnt+count-1,dist));
1918         return 0;
1919 }
1920
1921 /**
1922  * FIXME
1923  * @param <>
1924  * @returns <>
1925  * @author Martin Schaller (04/2008)
1926 */
1927 static int within_dist_polygon(struct point *p, struct point *poly_pnt, int count, int dist)
1928 {
1929         int i, j, c = 0;
1930         for (i = 0, j = count-1; i < count; j = i++) {
1931                 if ((((poly_pnt[i].y <= p->y) && ( p->y < poly_pnt[j].y )) ||
1932                 ((poly_pnt[j].y <= p->y) && ( p->y < poly_pnt[i].y))) &&
1933                 (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)) 
1934                         c = !c;
1935         }
1936         if (! c)
1937                 return within_dist_polyline(p, poly_pnt, count, dist, 1);
1938         return c;
1939 }
1940
1941 /**
1942  * FIXME
1943  * @param <>
1944  * @returns <>
1945  * @author Martin Schaller (04/2008)
1946 */
1947 int graphics_displayitem_within_dist(struct displayitem *di, struct point *p, int dist)
1948 {
1949         struct point pa[16384];
1950         int count;
1951
1952         count=transform(tg, pg, di->c, pa, di->count, 1, 0, NULL);
1953         
1954         if (di->item.type < type_line) {
1955                 return within_dist_point(p, &pa[0], dist);
1956         }
1957         if (di->item.type < type_area) {
1958                 return within_dist_polyline(p, pa, count, dist, 0);
1959         }
1960         return within_dist_polygon(p, pa, count, dist);
1961 }