2 * Navit, a modular navigation system.
3 * Copyright (C) 2005-2008 Navit Team
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.
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.
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.
20 //##############################################################################################################
25 //# Authors: Martin Schaller (04/2008)
27 //##############################################################################################################
35 #include "draw_info.h"
38 #include "projection.h"
42 #include "transform.h"
52 static char *navit_sharedir;
54 //##############################################################################################################
57 //# Authors: Martin Schaller (04/2008)
58 //##############################################################################################################
61 struct graphics_priv *priv;
62 struct graphics_methods meth;
64 struct graphics_font *font[16];
65 struct graphics_gc *gc[3];
67 struct callback_list *cbl;
71 struct display_context
75 struct graphics_gc *gc;
76 struct graphics_image *img;
78 struct transformation *trans;
86 struct layout *layout;
87 struct display_context dc;
90 struct mapset_handle *msh;
93 struct map_selection *sel;
95 struct callback *idle_cb;
96 struct event_idle *idle_ev;
101 graphics_set_rect(struct graphics *gra, struct point_rect *pr)
107 * Creates a new graphics object
111 * @author Martin Schaller (04/2008)
113 struct graphics * graphics_new(struct attr *parent, struct attr **attrs)
115 struct graphics *this_;
116 struct attr *type_attr;
117 struct graphics_priv * (*graphicstype_new)(struct navit *nav, struct graphics_methods *meth, struct attr **attrs, struct callback_list *cbl);
119 if (! (type_attr=attr_search(attrs, NULL, attr_type))) {
123 graphicstype_new=plugin_get_graphics_type(type_attr->u.str);
124 if (! graphicstype_new)
126 this_=g_new0(struct graphics, 1);
127 this_->cbl=callback_list_new();
128 this_->priv=(*graphicstype_new)(parent->u.navit, &this_->meth, attrs, this_->cbl);
129 this_->attrs=attr_list_dup(attrs);
137 * @author Martin Schaller (04/2008)
139 int graphics_get_attr(struct graphics *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
141 return attr_generic_get_attr(this_->attrs, NULL, type, attr, iter);
148 * @author Martin Schaller (04/2008)
150 struct graphics * graphics_overlay_new(struct graphics *parent, struct point *p, int w, int h, int alpha, int wraparound)
152 struct graphics *this_;
153 if (!parent->meth.overlay_new)
155 this_=g_new0(struct graphics, 1);
156 this_->priv=parent->meth.overlay_new(parent->priv, &this_->meth, p, w, h, alpha, wraparound);
168 * @author Martin Schaller (04/2008)
170 void graphics_init(struct graphics *this_)
172 this_->gc[0]=graphics_gc_new(this_);
173 graphics_gc_set_background(this_->gc[0], &(struct color) { 0xffff, 0xefef, 0xb7b7, 0xffff});
174 graphics_gc_set_foreground(this_->gc[0], &(struct color) { 0xffff, 0xefef, 0xb7b7, 0xffff });
175 this_->gc[1]=graphics_gc_new(this_);
176 graphics_gc_set_background(this_->gc[1], &(struct color) { 0x0000, 0x0000, 0x0000, 0xffff });
177 graphics_gc_set_foreground(this_->gc[1], &(struct color) { 0xffff, 0xffff, 0xffff, 0xffff });
178 this_->gc[2]=graphics_gc_new(this_);
179 graphics_gc_set_background(this_->gc[2], &(struct color) { 0xffff, 0xffff, 0xffff, 0xffff });
180 graphics_gc_set_foreground(this_->gc[2], &(struct color) { 0x0000, 0x0000, 0x0000, 0xffff });
181 graphics_background_gc(this_, this_->gc[0]);
182 navit_sharedir = getenv("NAVIT_SHAREDIR");
189 * @author Martin Schaller (04/2008)
191 void * graphics_get_data(struct graphics *this_, char *type)
193 return (this_->meth.get_data(this_->priv, type));
196 void graphics_add_callback(struct graphics *this_, struct callback *cb)
198 callback_list_add(this_->cbl, cb);
201 void graphics_remove_callback(struct graphics *this_, struct callback *cb)
203 callback_list_remove(this_->cbl, cb);
210 * @author Martin Schaller (04/2008)
212 struct graphics_font * graphics_font_new(struct graphics *gra, int size, int flags)
214 struct graphics_font *this_;
216 this_=g_new0(struct graphics_font,1);
217 this_->priv=gra->meth.font_new(gra->priv, &this_->meth, gra->default_font, size, flags);
222 * Free all loaded fonts.
223 * Used when switching layouts.
224 * @param gra The graphics instance
226 * @author Sarah Nordstrom (05/2008)
228 void graphics_font_destroy_all(struct graphics *gra)
231 for(i = 0 ; i < sizeof(gra->font) / sizeof(gra->font[0]); i++) {
232 if(!gra->font[i]) continue;
233 gra->font[i]->meth.font_destroy(gra->font[i]->priv);
242 * @author Martin Schaller (04/2008)
244 struct graphics_gc * graphics_gc_new(struct graphics *gra)
246 struct graphics_gc *this_;
248 this_=g_new0(struct graphics_gc,1);
249 this_->priv=gra->meth.gc_new(gra->priv, &this_->meth);
257 * @author Martin Schaller (04/2008)
259 void graphics_gc_destroy(struct graphics_gc *gc)
261 gc->meth.gc_destroy(gc->priv);
269 * @author Martin Schaller (04/2008)
271 void graphics_gc_set_foreground(struct graphics_gc *gc, struct color *c)
273 gc->meth.gc_set_foreground(gc->priv, c);
280 * @author Martin Schaller (04/2008)
282 void graphics_gc_set_background(struct graphics_gc *gc, struct color *c)
284 gc->meth.gc_set_background(gc->priv, c);
291 * @author Martin Schaller (04/2008)
293 void graphics_gc_set_linewidth(struct graphics_gc *gc, int width)
295 gc->meth.gc_set_linewidth(gc->priv, width);
302 * @author Martin Schaller (04/2008)
304 void graphics_gc_set_dashes(struct graphics_gc *gc, int width, int offset, unsigned char dash_list[], int n)
306 if (gc->meth.gc_set_dashes)
307 gc->meth.gc_set_dashes(gc->priv, width, offset, dash_list, n);
311 * Create a new image from file path scaled to w and h pixels
312 * @param gra the graphics instance
313 * @param path path of the image to load
314 * @param w width to rescale to
315 * @param h height to rescale to
317 * @author Martin Schaller (04/2008)
319 struct graphics_image * graphics_image_new_scaled(struct graphics *gra, char *path, int w, int h)
321 struct graphics_image *this_;
323 this_=g_new0(struct graphics_image,1);
326 this_->priv=gra->meth.image_new(gra->priv, &this_->meth, path, &this_->width, &this_->height, &this_->hot, 0);
335 * Create a new image from file path scaled to w and h pixels and possibly rotated
336 * @param gra the graphics instance
337 * @param path path of the image to load
338 * @param w width to rescale to
339 * @param h height to rescale to
340 * @param rotate angle to rotate the image. Warning, graphics might only support 90 degree steps here
342 * @author Martin Schaller (04/2008)
344 struct graphics_image * graphics_image_new_scaled_rotated(struct graphics *gra, char *path, int w, int h, int rotate)
346 struct graphics_image *this_;
348 this_=g_new0(struct graphics_image,1);
351 this_->priv=gra->meth.image_new(gra->priv, &this_->meth, path, &this_->width, &this_->height, &this_->hot, rotate);
360 * Create a new image from file path
361 * @param gra the graphics instance
362 * @param path path of the image to load
364 * @author Martin Schaller (04/2008)
366 struct graphics_image * graphics_image_new(struct graphics *gra, char *path)
368 return graphics_image_new_scaled(gra, path, -1, -1);
375 * @author Martin Schaller (04/2008)
377 void graphics_image_free(struct graphics *gra, struct graphics_image *img)
379 if (gra->meth.image_free)
380 gra->meth.image_free(gra->priv, img->priv);
388 * @author Martin Schaller (04/2008)
390 void graphics_draw_restore(struct graphics *this_, struct point *p, int w, int h)
392 this_->meth.draw_restore(this_->priv, p, w, h);
399 * @author Martin Schaller (04/2008)
401 void graphics_draw_mode(struct graphics *this_, enum draw_mode_num mode)
403 this_->meth.draw_mode(this_->priv, mode);
410 * @author Martin Schaller (04/2008)
412 void graphics_draw_lines(struct graphics *this_, struct graphics_gc *gc, struct point *p, int count)
414 this_->meth.draw_lines(this_->priv, gc->priv, p, count);
421 * @author Martin Schaller (04/2008)
423 void graphics_draw_circle(struct graphics *this_, struct graphics_gc *gc, struct point *p, int r)
425 this_->meth.draw_circle(this_->priv, gc->priv, p, r);
432 * @author Martin Schaller (04/2008)
434 void graphics_draw_rectangle(struct graphics *this_, struct graphics_gc *gc, struct point *p, int w, int h)
436 this_->meth.draw_rectangle(this_->priv, gc->priv, p, w, h);
443 * @author Martin Schaller (04/2008)
445 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)
447 this_->meth.draw_text(this_->priv, gc1->priv, gc2 ? gc2->priv : NULL, font->priv, text, p, dx, dy);
454 * @author Martin Schaller (04/2008)
456 void graphics_get_text_bbox(struct graphics *this_, struct graphics_font *font, char *text, int dx, int dy, struct point *ret, int estimate)
458 this_->meth.get_text_bbox(this_->priv, font->priv, text, dx, dy, ret, estimate);
465 * @author Martin Schaller (04/2008)
467 void graphics_overlay_disable(struct graphics *this_, int disable)
469 if (this_->meth.overlay_disable)
470 this_->meth.overlay_disable(this_->priv, disable);
477 * @author Martin Schaller (04/2008)
479 void graphics_draw_image(struct graphics *this_, struct graphics_gc *gc, struct point *p, struct graphics_image *img)
481 this_->meth.draw_image(this_->priv, gc->priv, p, img->priv);
485 //##############################################################################################################
488 //# Authors: Martin Schaller (04/2008)
489 //##############################################################################################################
491 graphics_draw_drag(struct graphics *this_, struct point *p)
493 if (!this_->meth.draw_drag)
495 this_->meth.draw_drag(this_->priv, p);
500 graphics_background_gc(struct graphics *this_, struct graphics_gc *gc)
502 this_->meth.background_gc(this_->priv, gc ? gc->priv : NULL);
511 //##############################################################################################################
514 //# Authors: Martin Schaller (04/2008)
515 //##############################################################################################################
516 static void popup_view_html(struct popup_item *item, char *file)
519 sprintf(command,"firefox %s", file);
523 struct transformatin *tg;
526 //##############################################################################################################
529 //# Authors: Martin Schaller (04/2008)
530 //##############################################################################################################
531 static void graphics_popup(struct display_list *list, struct popup_item **popup)
537 struct popup_item *curr_item,*last=NULL;
539 mr=map_rect_new(item->map, NULL, NULL, 0);
540 printf("id hi=0x%x lo=0x%x\n", item->id_hi, item->id_lo);
541 item=map_rect_get_item_byid(mr, item->id_hi, item->id_lo);
543 if (item_attr_get(item, attr_name, &attr)) {
544 curr_item=popup_item_new_text(popup,attr.u.str,1);
545 if (item_attr_get(item, attr_info_html, &attr)) {
546 popup_item_new_func(&last,"HTML Info",1, popup_view_html, g_strdup(attr.u.str));
548 if (item_attr_get(item, attr_price_html, &attr)) {
549 popup_item_new_func(&last,"HTML Preis",2, popup_view_html, g_strdup(attr.u.str));
551 curr_item->submenu=last;
554 map_rect_destroy(mr);
562 * @author Martin Schaller (04/2008)
576 * @author Martin Schaller (04/2008)
578 static int xdisplay_free_list(gpointer key, gpointer value, gpointer user_data)
580 GHashTable *hash=value;
582 g_hash_table_destroy(hash);
590 * @author Martin Schaller (04/2008)
592 static void xdisplay_free(GHashTable *display_list)
594 g_hash_table_foreach_remove(display_list, xdisplay_free_list, NULL);
598 displayitem_hash(gconstpointer key)
600 const struct displayitem *di=key;
601 return (di->item.id_hi^di->item.id_lo^((int) di->item.map));
605 displayitem_equal(gconstpointer a, gconstpointer b)
607 const struct displayitem *dia=a;
608 const struct displayitem *dib=b;
609 if (item_is_equal(dia->item, dib->item))
619 * @author Martin Schaller (04/2008)
621 static void display_add(struct displaylist *displaylist, struct item *item, int count, struct coord *c, char *label)
623 struct displayitem *di;
628 len=sizeof(*di)+count*sizeof(*c);
630 len+=strlen(label)+1;
634 di=(struct displayitem *)p;
636 p+=sizeof(*di)+count*sizeof(*c);
640 strcpy(di->label, label);
644 memcpy(di->c, c, count*sizeof(*c));
646 h=g_hash_table_lookup(displaylist->dl, GINT_TO_POINTER(item->type));
648 h=g_hash_table_new_full(displayitem_hash, displayitem_equal, g_free, NULL);
649 g_hash_table_insert(displaylist->dl, GINT_TO_POINTER(item->type), h);
651 g_hash_table_replace(h, di, NULL);
659 * @author Martin Schaller (04/2008)
661 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)
663 int i,x,y,tl,tlm,th,thm,tlsq,l;
669 if (gra->meth.get_text_bbox) {
670 gra->meth.get_text_bbox(gra->priv, font->priv, label, 0x10000, 0x0, pb, 1);
671 tl=(pb[2].x-pb[0].x);
672 th=(pb[0].y-pb[1].y);
680 for (i = 0 ; i < count-1 ; i++) {
703 dbg(0,"display_text: '%s', %d, %d, %d, %d %d\n", label, x, y, dx*0x10000/l, dy*0x10000/l, l);
705 if (x < gra->r.rl.x && x + tl > gra->r.lu.x && y + tl > gra->r.lu.y && y - tl < gra->r.rl.y)
706 gra->meth.draw_text(gra->priv, fg->priv, bg->priv, font->priv, label, &p_t, dx*0x10000/l, dy*0x10000/l);
711 static void display_draw_arrow(struct point *p, int dx, int dy, int l, struct graphics_gc *gc, struct graphics *gra)
714 pnt[0]=pnt[1]=pnt[2]=*p;
715 pnt[0].x+=-dx*l/65536+dy*l/65536;
716 pnt[0].y+=-dy*l/65536-dx*l/65536;
717 pnt[2].x+=-dx*l/65536-dy*l/65536;
718 pnt[2].y+=-dy*l/65536+dx*l/65536;
719 gra->meth.draw_lines(gra->priv, gc->priv, pnt, 3);
722 static void display_draw_arrows(struct graphics *gra, struct graphics_gc *gc, struct point *pnt, int count)
726 for (i = 0 ; i < count-1 ; i++) {
727 dx=pnt[i+1].x-pnt[i].x;
728 dy=pnt[i+1].y-pnt[i].y;
736 display_draw_arrow(&p, dx, dy, 10, gc, gra);
740 display_draw_arrow(&p, dx, dy, 10, gc, gra);
746 intersection(struct point * a1, int adx, int ady, struct point * b1, int bdx, int bdy,
750 n = bdy * adx - bdx * ady;
751 a = bdx * (a1->y - b1->y) - bdy * (a1->x - b1->x);
752 b = adx * (a1->y - b1->y) - ady * (a1->x - b1->x);
766 res->x = a1->x + a * adx / n;
767 res->y = a1->y + a * ady / n;
841 draw_circle(struct point *pnt, int diameter, int scale, int start, int len, struct point *res, int *pos, int dir)
846 dbg(0,"diameter=%d start=%d len=%d pos=%d dir=%d\n", diameter, start, len, *pos, dir);
854 else if (diameter > 64)
856 else if (diameter > 24)
858 else if (diameter > 8)
869 while (i < count && c[i].fowler <= start)
871 while (i < count && c[i].fowler < end) {
872 res[*pos].x=pnt->x+((c[i].x*diameter+128)>>8);
873 res[*pos].y=pnt->y+((c[i].y*diameter+128)>>8);
881 while (start > 1024) {
887 while (i >= 0 && c[i].fowler >= start)
889 while (i >= 0 && c[i].fowler > end) {
890 res[*pos].x=pnt->x+((c[i].x*diameter+128)>>8);
891 res[*pos].y=pnt->y+((c[i].y*diameter+128)>>8);
903 fowler(int dy, int dx)
905 int adx, ady; /* Absolute Values of Dx and Dy */
906 int code; /* Angular Region Classification Code */
908 adx = (dx < 0) ? -dx : dx; /* Compute the absolute values. */
909 ady = (dy < 0) ? -dy : dy;
911 code = (adx < ady) ? 1 : 0;
919 return (dx == 0) ? 0 : 128*ady / adx; /* [ 0, 45] */
921 return (256 - (128*adx / ady)); /* ( 45, 90] */
923 return (256 + (128*adx / ady)); /* ( 90,135) */
925 return (512 - (128*ady / adx)); /* [135,180] */
927 return (512 + (128*ady / adx)); /* (180,225] */
929 return (768 - (128*adx / ady)); /* (225,270) */
931 return (768 + (128*adx / ady)); /* [270,315) */
933 return (1024 - (128*ady / adx));/* [315,360) */
938 int_sqrt(unsigned int n)
940 unsigned int h, p= 0, q= 1, r= n;
960 calc_offsets(int wi, int l, int dx, int dy, struct offset *res)
982 graphics_draw_polyline_as_polygon(struct graphics *gra, struct graphics_gc *gc, struct point *pnt, int count, int *width, int step)
985 struct point res[maxpoints], pos, poso, neg, nego;
986 int i, dx=0, dy=0, l=0, dxo=0, dyo=0;
988 int fow=0, fowo=0, delta;
989 int wi, ppos = maxpoints/2, npos = maxpoints/2;
991 int max_circle_points=20;
992 for (i = 0; i < count; i++) {
996 dx = (pnt[i + 1].x - pnt[i].x);
997 dy = (pnt[i + 1].y - pnt[i].y);
998 l = int_sqrt(dx * dx + dy * dy);
1003 calc_offsets(wi, l, dx, dy, &o);
1004 pos.x = pnt[i].x + o.ny;
1005 pos.y = pnt[i].y + o.px;
1006 neg.x = pnt[i].x + o.py;
1007 neg.y = pnt[i].y + o.nx;
1010 else if (i == count-1)
1012 else if (npos < max_circle_points || ppos >= maxpoints-max_circle_points)
1019 poso.x = pnt[i].x + oo.ny;
1020 poso.y = pnt[i].y + oo.px;
1021 nego.x = pnt[i].x + oo.py;
1022 nego.y = pnt[i].y + oo.nx;
1027 if (intersection(&pos, dx, dy, &poso, dxo, dyo, &res[ppos]))
1031 draw_circle(&pnt[i], wi, prec, fowo-512, -delta, res, &npos, -1);
1035 draw_circle(&pnt[i], wi, prec, fowo, 1024-delta, res, &ppos, 1);
1037 if (intersection(&neg, dx, dy, &nego, dxo, dyo, &res[npos - 1]))
1046 draw_circle(&pnt[i], wi, prec, fow-512, -512, res, &npos, -1);
1049 dbg_assert(npos > 0);
1050 dbg_assert(ppos < maxpoints);
1051 gra->meth.draw_polygon(gra->priv, gc->priv, res+npos, ppos-npos);
1058 draw_circle(&pnt[i], wi, prec, fow+512, 512, res, &ppos, 1);
1064 calc_offsets(wi, l, dx, dy, &oo);
1079 clipcode(struct wpoint *p, struct point_rect *r)
1095 clip_line(struct wpoint *p1, struct wpoint *p2, struct point_rect *r)
1097 int code1,code2,ret=1;
1099 code1=clipcode(p1, r);
1102 code2=clipcode(p2, r);
1108 while (code1 || code2) {
1112 p1->y+=(r->lu.x-p1->x)*dy/dx;
1113 p1->w+=(r->lu.x-p1->x)*dw/dx;
1115 } else if (code1 & 2) {
1116 p1->y+=(r->rl.x-p1->x)*dy/dx;
1117 p1->w+=(r->rl.x-p1->x)*dw/dx;
1119 } else if (code1 & 4) {
1120 p1->x+=(r->lu.y-p1->y)*dx/dy;
1121 p1->w+=(r->lu.y-p1->y)*dw/dy;
1123 } else if (code1 & 8) {
1124 p1->x+=(r->rl.y-p1->y)*dx/dy;
1125 p1->w+=(r->rl.y-p1->y)*dw/dy;
1128 code1=clipcode(p1, r);
1132 p2->y+=(r->lu.x-p2->x)*dy/dx;
1133 p2->w+=(r->lu.x-p2->x)*dw/dx;
1135 } else if (code2 & 2) {
1136 p2->y+=(r->rl.x-p2->x)*dy/dx;
1137 p2->w+=(r->rl.x-p2->x)*dw/dx;
1139 } else if (code2 & 4) {
1140 p2->x+=(r->lu.y-p2->y)*dx/dy;
1141 p2->w+=(r->lu.y-p2->y)*dw/dy;
1143 } else if (code2 & 8) {
1144 p2->x+=(r->rl.y-p2->y)*dx/dy;
1145 p2->w+=(r->rl.y-p2->y)*dw/dy;
1148 code2=clipcode(p2, r);
1154 graphics_draw_polyline_clipped(struct graphics *gra, struct graphics_gc *gc, struct point *pa, int count, int *width, int step, int poly)
1156 struct point p[count+1];
1157 int w[count*step+1];
1158 struct wpoint p1,p2;
1161 struct point_rect r=gra->r;
1165 for (i = 1 ; i < count ; i++) {
1166 if (width[i*step] > wmax)
1176 for (i = 0 ; i < count ; i++) {
1180 p1.w=width[(i-1)*step];
1184 /* 0 = invisible, 1 = completely visible, 3 = start point clipped, 5 = end point clipped, 7 both points clipped */
1185 code=clip_line(&p1, &p2, &r);
1186 if (((code == 1 || code == 5) && i == 1) || (code & 2)) {
1198 if (i == count-1 || (code & 4)) {
1201 graphics_draw_polyline_as_polygon(gra, gc, p, out, w, step);
1203 gra->meth.draw_lines(gra->priv, gc->priv, p, out);
1212 is_inside(struct point *p, struct point_rect *r, int edge)
1216 return p->x >= r->lu.x;
1218 return p->x <= r->rl.x;
1220 return p->y >= r->lu.y;
1222 return p->y <= r->rl.y;
1229 poly_intersection(struct point *p1, struct point *p2, struct point_rect *r, int edge, struct point *ret)
1235 ret->y=p1->y+(r->lu.x-p1->x)*dy/dx;
1239 ret->y=p1->y+(r->rl.x-p1->x)*dy/dx;
1243 ret->x=p1->x+(r->lu.y-p1->y)*dx/dy;
1247 ret->x=p1->x+(r->rl.y-p1->y)*dx/dy;
1254 graphics_draw_polygon_clipped(struct graphics *gra, struct graphics_gc *gc, struct point *pin, int count_in)
1256 struct point_rect r=gra->r;
1257 struct point *pout,*p,*s,pi;
1258 struct point p1[count_in+1];
1259 struct point p2[count_in+1];
1260 int count_out,edge=3;
1270 for (edge = 0 ; edge < 4 ; edge++) {
1274 for (i = 0 ; i < count_in ; i++) {
1275 if (is_inside(p, &r, edge)) {
1276 if (! is_inside(s, &r, edge)) {
1277 poly_intersection(s,p,&r,edge,&pi);
1278 pout[count_out++]=pi;
1280 pout[count_out++]=*p;
1282 if (is_inside(s, &r, edge)) {
1283 poly_intersection(p,s,&r,edge,&pi);
1284 pout[count_out++]=pi;
1299 gra->meth.draw_polygon(gra->priv, gc->priv, pin, count_in);
1304 display_context_free(struct display_context *dc)
1313 displayitem_draw(struct displayitem *di, void *dummy, struct display_context *dc)
1317 struct point pa[16384];
1318 struct graphics *gra=dc->gra;
1319 struct graphics_gc *gc=dc->gc;
1320 struct element *e=dc->e;
1321 struct graphics_image *img=dc->img;
1323 char path[PATH_MAX];
1327 gc=graphics_gc_new(gra);
1328 gc->meth.gc_set_foreground(gc->priv, &e->color);
1331 if (dc->e->type == element_polyline) {
1332 count=transform(dc->trans, dc->pro, di->c, pa, di->count, 1, e->u.polyline.width, width);
1335 count=transform(dc->trans, dc->pro, di->c, pa, di->count, 1, 0, NULL);
1337 case element_polygon:
1341 for (i = 0 ; i < count ; i++) {
1342 dbg(0,"pa[%d]=%d,%d\n", i, pa[i].x, pa[i].y);
1345 dbg(0,"element_polygon count=%d\n",count);
1348 graphics_draw_polygon_clipped(gra, gc, pa, count);
1351 case element_polyline:
1353 if (e->u.polyline.width > 1) {
1354 graphics_draw_polyline_as_polygon(gra, gc, pa, count, width, 0);
1359 if (e->u.polyline.width > 1)
1360 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
1362 gc->meth.gc_set_linewidth(gc->priv, 1);
1367 if (e->u.polyline.width > 0 && e->u.polyline.dash_num > 0)
1368 graphics_gc_set_dashes(gc, e->u.polyline.width,
1369 e->u.polyline.offset,
1370 e->u.polyline.dash_table,
1371 e->u.polyline.dash_num);
1373 if (di->label && !strcmp(di->label, "Bahnhofstr.") && di->item.type != type_street_1_city) {
1374 dbg(0,"0x%x,0x%x %s\n", di->item.id_hi, di->item.id_lo, item_to_name(di->item.type));
1376 graphics_draw_polyline_clipped(gra, gc, pa, count, width, 1, e->u.polyline.width > 1);
1382 case element_circle:
1384 if (e->u.circle.width > 1)
1385 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
1386 gra->meth.draw_circle(gra->priv, gc->priv, pa, e->u.circle.radius);
1387 if (di->label && e->text_size) {
1390 if (! gra->font[e->text_size])
1391 gra->font[e->text_size]=graphics_font_new(gra, e->text_size*20, 0);
1392 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);
1397 if (count && di->label) {
1398 if (! gra->font[e->text_size])
1399 gra->font[e->text_size]=graphics_font_new(gra, e->text_size*20, 0);
1400 label_line(gra, gra->gc[2], gra->gc[1], gra->font[e->text_size], pa, count, di->label);
1406 if (e->u.icon.src[0] == '/')
1407 strcpy(path,e->u.icon.src);
1409 sprintf(path,"%s/xpm/%s", navit_sharedir, e->u.icon.src);
1410 img=graphics_image_new_scaled_rotated(gra, path, e->u.icon.width, e->u.icon.height, e->u.icon.rotation);
1414 dbg(0,"failed to load icon '%s'\n", e->u.icon.src);
1417 p.x=pa[0].x - img->hot.x;
1418 p.y=pa[0].y - img->hot.y;
1419 gra->meth.draw_image(gra->priv, gra->gc[0]->priv, &p, img->priv);
1424 dbg(1,"image: '%s'\n", di->label);
1425 if (gra->meth.draw_image_warp)
1426 gra->meth.draw_image_warp(gra->priv, gra->gc[0]->priv, pa, count, di->label);
1428 dbg(0,"draw_image_warp not supported by graphics driver drawing '%s'\n", di->label);
1430 case element_arrows:
1431 display_draw_arrows(gra,gc,pa,count);
1434 printf("Unhandled element type %d\n", e->type);
1442 * @author Martin Schaller (04/2008)
1444 static void xdisplay_draw_elements(struct graphics *gra, struct displaylist *display_list, struct itemgra *itm)
1449 enum item_type type;
1454 display_list->dc.e=e;
1457 type=GPOINTER_TO_INT(types->data);
1458 h=g_hash_table_lookup(display_list->dl, GINT_TO_POINTER(type));
1460 g_hash_table_foreach(h, (GHFunc)displayitem_draw, &display_list->dc);
1461 display_context_free(&display_list->dc);
1463 types=g_list_next(types);
1470 graphics_draw_itemgra(struct graphics *gra, struct itemgra *itm, struct transformation *t)
1476 struct graphics_gc *gc = NULL;
1477 struct graphics_image *img;
1478 char path[PATH_MAX];
1483 struct element *e=es->data;
1484 int count=e->coord_count;
1485 struct point pnt[count+1];
1487 transform(t, projection_screen, e->coord, pnt, count, 0, 0, NULL);
1489 transform(t, projection_screen, &c, pnt, 1, 0, 0, NULL);
1492 gc=graphics_gc_new(gra);
1493 gc->meth.gc_set_foreground(gc->priv, &e->color);
1495 case element_polyline:
1496 if (e->u.polyline.width > 1)
1497 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
1498 if (e->u.polyline.width > 0 && e->u.polyline.dash_num > 0)
1499 graphics_gc_set_dashes(gc, e->u.polyline.width,
1500 e->u.polyline.offset,
1501 e->u.polyline.dash_table,
1502 e->u.polyline.dash_num);
1503 gra->meth.draw_lines(gra->priv, gc->priv, pnt, count);
1505 case element_circle:
1506 if (e->u.circle.width > 1)
1507 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
1508 gra->meth.draw_circle(gra->priv, gc->priv, &pnt[0], e->u.circle.radius);
1509 if (label && e->text_size) {
1512 if (! gra->font[e->text_size])
1513 gra->font[e->text_size]=graphics_font_new(gra, e->text_size*20, 0);
1514 gra->meth.draw_text(gra->priv, gra->gc[2]->priv, gra->gc[1]->priv, gra->font[e->text_size]->priv, label, &p, 0x10000, 0);
1518 if (e->u.icon.src[0] == '/')
1519 strcpy(path,e->u.icon.src);
1521 sprintf(path,"%s/xpm/%s", navit_sharedir, e->u.icon.src);
1522 img=graphics_image_new_scaled_rotated(gra, path, e->u.icon.width, e->u.icon.height, e->u.icon.rotation);
1524 dbg(0,"failed to load icon '%s'\n", e->u.icon.src);
1526 p.x=pnt[0].x - img->hot.x;
1527 p.y=pnt[0].y - img->hot.y;
1528 gra->meth.draw_image(gra->priv, gc->priv, &p, img->priv);
1529 graphics_image_free(gra, img);
1533 dbg(0,"dont know how to draw %d\n", e->type);
1535 graphics_gc_destroy(gc);
1544 * @author Martin Schaller (04/2008)
1546 static void xdisplay_draw_layer(struct displaylist *display_list, struct graphics *gra, struct layer *lay, int order)
1549 struct itemgra *itm;
1554 if (order >= itm->order.min && order <= itm->order.max)
1555 xdisplay_draw_elements(gra, display_list, itm);
1556 itms=g_list_next(itms);
1564 * @author Martin Schaller (04/2008)
1566 static void xdisplay_draw(struct displaylist *display_list, struct graphics *gra, struct layout *l, int order)
1574 xdisplay_draw_layer(display_list, gra, lay, order);
1575 lays=g_list_next(lays);
1583 * @author Martin Schaller (04/2008)
1585 extern void *route_selection;
1588 do_draw(struct displaylist *displaylist, int cancel)
1591 int count,max=16384,workload=0;
1592 struct coord ca[max];
1596 if (!displaylist->msh) {
1597 displaylist->msh=mapset_open(displaylist->ms);
1599 if (!displaylist->m) {
1600 displaylist->m=mapset_next(displaylist->msh, 1);
1601 if (!displaylist->m) {
1602 mapset_close(displaylist->msh);
1603 displaylist->msh=NULL;
1606 displaylist->dc.pro=map_projection(displaylist->m);
1607 displaylist->conv=map_requires_conversion(displaylist->m);
1608 displaylist->sel=transform_get_selection(displaylist->dc.trans, displaylist->dc.pro, displaylist->order);
1609 displaylist->mr=map_rect_new(displaylist->m, displaylist->sel);
1611 if (displaylist->mr) {
1612 while ((item=map_rect_get_item(displaylist->mr))) {
1613 count=item_coord_get_within_selection(item, ca, item->type < type_line ? 1: max, displaylist->sel);
1617 dbg(0,"point count overflow %d\n", count);
1618 if (!item_attr_get(item, attr_label, &attr))
1620 if (displaylist->conv && attr.u.str && attr.u.str[0]) {
1621 char *str=map_convert_string(displaylist->m, attr.u.str);
1622 display_add(displaylist, item, count, ca, str);
1623 map_convert_free(str);
1625 display_add(displaylist, item, count, ca, attr.u.str);
1627 if (workload == displaylist->workload)
1630 map_rect_destroy(displaylist->mr);
1632 map_selection_destroy(displaylist->sel);
1633 displaylist->mr=NULL;
1634 displaylist->sel=NULL;
1635 displaylist->m=NULL;
1637 event_remove_idle(displaylist->idle_ev);
1638 displaylist->idle_ev=NULL;
1639 displaylist->busy=0;
1640 graphics_displaylist_draw(displaylist->dc.gra, displaylist, displaylist->dc.trans, displaylist->layout, 1);
1641 map_rect_destroy(displaylist->mr);
1642 map_selection_destroy(displaylist->sel);
1643 mapset_close(displaylist->msh);
1644 displaylist->mr=NULL;
1645 displaylist->sel=NULL;
1646 displaylist->m=NULL;
1647 callback_call_1(displaylist->cb, cancel);
1654 * @author Martin Schaller (04/2008)
1656 void graphics_displaylist_draw(struct graphics *gra, struct displaylist *displaylist, struct transformation *trans, struct layout *l, int callback)
1658 int order=transform_get_order(trans);
1660 displaylist->dc.trans=trans;
1663 // FIXME find a better place to set the background color
1665 graphics_gc_set_background(gra->gc[0], &l->color);
1666 graphics_gc_set_foreground(gra->gc[0], &l->color);
1667 gra->default_font = g_strdup(l->font);
1669 graphics_background_gc(gra, gra->gc[0]);
1670 gra->meth.draw_mode(gra->priv, draw_mode_begin);
1671 gra->meth.draw_rectangle(gra->priv, gra->gc[0]->priv, &p, 32767, 32767);
1673 xdisplay_draw(displaylist, gra, l, order+l->order_delta);
1675 callback_list_call_attr_0(gra->cbl, attr_postdraw);
1676 gra->meth.draw_mode(gra->priv, draw_mode_end);
1683 * @author Martin Schaller (04/2008)
1685 void graphics_draw(struct graphics *gra, struct displaylist *displaylist, GList *mapsets, struct transformation *trans, struct layout *l, int async, struct callback *cb)
1687 int order=transform_get_order(trans);
1690 if (async == 1 && displaylist->busy)
1692 xdisplay_free(displaylist->dl);
1693 dbg(1,"order=%d\n", order);
1695 displaylist->dc.gra=gra;
1696 displaylist->ms=mapsets->data;
1697 displaylist->dc.trans=trans;
1698 displaylist->workload=async ? 1000 : 0;
1701 order+=l->order_delta;
1702 displaylist->order=order;
1703 displaylist->busy=1;
1704 displaylist->layout=l;
1706 if (! displaylist->idle_cb)
1707 displaylist->idle_cb=callback_new_2(do_draw, displaylist, 0);
1708 displaylist->idle_ev=event_add_idle(50, displaylist->idle_cb);
1710 do_draw(displaylist, 0);
1717 * @author Martin Schaller (04/2008)
1719 struct displaylist_handle {
1720 GList *hl_head,*hl,*l_head,*l;
1727 * @author Martin Schaller (04/2008)
1729 struct displaylist_handle * graphics_displaylist_open(struct displaylist *displaylist)
1731 struct displaylist_handle *ret;
1733 ret=g_new0(struct displaylist_handle, 1);
1734 ret->hl_head=ret->hl=g_hash_to_list(displaylist->dl);
1735 ret->l_head=ret->l=g_hash_to_list_keys(ret->hl->data);
1744 * @author Martin Schaller (04/2008)
1746 struct displayitem * graphics_displaylist_next(struct displaylist_handle *dlh)
1748 struct displayitem *ret;
1750 dlh->hl=g_list_next(dlh->hl);
1753 g_list_free(dlh->l_head);
1754 dlh->l_head=dlh->l=g_hash_to_list_keys(dlh->hl->data);
1757 dlh->l=g_list_next(dlh->l);
1765 * @author Martin Schaller (04/2008)
1767 void graphics_displaylist_close(struct displaylist_handle *dlh)
1769 g_list_free(dlh->hl_head);
1770 g_list_free(dlh->l_head);
1778 * @author Martin Schaller (04/2008)
1780 struct displaylist * graphics_displaylist_new(void)
1782 struct displaylist *ret=g_new0(struct displaylist, 1);
1784 ret->dl=g_hash_table_new(NULL,NULL);
1793 * @author Martin Schaller (04/2008)
1795 struct item * graphics_displayitem_get_item(struct displayitem *di)
1804 * @author Martin Schaller (04/2008)
1806 char * graphics_displayitem_get_label(struct displayitem *di)
1815 * @author Martin Schaller (04/2008)
1817 static int within_dist_point(struct point *p0, struct point *p1, int dist)
1819 if (p0->x == 32767 || p0->y == 32767 || p1->x == 32767 || p1->y == 32767)
1821 if (p0->x == -32768 || p0->y == -32768 || p1->x == -32768 || p1->y == -32768)
1823 if ((p0->x-p1->x)*(p0->x-p1->x) + (p0->y-p1->y)*(p0->y-p1->y) <= dist*dist) {
1833 * @author Martin Schaller (04/2008)
1835 static int within_dist_line(struct point *p, struct point *line_p0, struct point *line_p1, int dist)
1839 struct point line_p;
1841 vx=line_p1->x-line_p0->x;
1842 vy=line_p1->y-line_p0->y;
1848 return within_dist_point(p, line_p0, dist);
1851 return within_dist_point(p, line_p1, dist);
1853 line_p.x=line_p0->x+vx*c1/c2;
1854 line_p.y=line_p0->y+vy*c1/c2;
1855 return within_dist_point(p, &line_p, dist);
1862 * @author Martin Schaller (04/2008)
1864 static int within_dist_polyline(struct point *p, struct point *line_pnt, int count, int dist, int close)
1867 for (i = 0 ; i < count-1 ; i++) {
1868 if (within_dist_line(p,line_pnt+i,line_pnt+i+1,dist)) {
1873 return (within_dist_line(p,line_pnt,line_pnt+count-1,dist));
1881 * @author Martin Schaller (04/2008)
1883 static int within_dist_polygon(struct point *p, struct point *poly_pnt, int count, int dist)
1886 for (i = 0, j = count-1; i < count; j = i++) {
1887 if ((((poly_pnt[i].y <= p->y) && ( p->y < poly_pnt[j].y )) ||
1888 ((poly_pnt[j].y <= p->y) && ( p->y < poly_pnt[i].y))) &&
1889 (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))
1893 return within_dist_polyline(p, poly_pnt, count, dist, 1);
1901 * @author Martin Schaller (04/2008)
1903 int graphics_displayitem_within_dist(struct displaylist *displaylist, struct displayitem *di, struct point *p, int dist)
1905 struct point pa[16384];
1908 count=transform(displaylist->dc.trans, displaylist->dc.pro, di->c, pa, di->count, 1, 0, NULL);
1910 if (di->item.type < type_line) {
1911 return within_dist_point(p, &pa[0], dist);
1913 if (di->item.type < type_area) {
1914 return within_dist_polyline(p, pa, count, dist, 0);
1916 return within_dist_polygon(p, pa, count, dist);