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;
79 graphics_set_rect(struct graphics *gra, struct point_rect *pr)
85 * Creates a new graphics object
89 * @author Martin Schaller (04/2008)
91 struct graphics * graphics_new(struct attr *parent, struct attr **attrs)
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);
97 if (! (type_attr=attr_search(attrs, NULL, attr_type))) {
101 graphicstype_new=plugin_get_graphics_type(type_attr->u.str);
102 if (! graphicstype_new)
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);
115 * @author Martin Schaller (04/2008)
117 int graphics_get_attr(struct graphics *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
119 return attr_generic_get_attr(this_->attrs, NULL, type, attr, iter);
126 * @author Martin Schaller (04/2008)
128 struct graphics * graphics_overlay_new(struct graphics *parent, struct point *p, int w, int h, int alpha, int wraparound)
130 struct graphics *this_;
131 if (!parent->meth.overlay_new)
133 this_=g_new0(struct graphics, 1);
134 this_->priv=parent->meth.overlay_new(parent->priv, &this_->meth, p, w, h, alpha, wraparound);
146 * @author Martin Schaller (04/2008)
148 void graphics_init(struct graphics *this_)
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");
167 * @author Martin Schaller (04/2008)
169 void * graphics_get_data(struct graphics *this_, char *type)
171 return (this_->meth.get_data(this_->priv, type));
174 void graphics_add_callback(struct graphics *this_, struct callback *cb)
176 callback_list_add(this_->cbl, cb);
179 void graphics_remove_callback(struct graphics *this_, struct callback *cb)
181 callback_list_remove(this_->cbl, cb);
188 * @author Martin Schaller (04/2008)
190 struct graphics_font * graphics_font_new(struct graphics *gra, int size, int flags)
192 struct graphics_font *this_;
194 this_=g_new0(struct graphics_font,1);
195 this_->priv=gra->meth.font_new(gra->priv, &this_->meth, gra->default_font, size, flags);
200 * Free all loaded fonts.
201 * Used when switching layouts.
202 * @param gra The graphics instance
204 * @author Sarah Nordstrom (05/2008)
206 void graphics_font_destroy_all(struct graphics *gra)
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);
220 * @author Martin Schaller (04/2008)
222 struct graphics_gc * graphics_gc_new(struct graphics *gra)
224 struct graphics_gc *this_;
226 this_=g_new0(struct graphics_gc,1);
227 this_->priv=gra->meth.gc_new(gra->priv, &this_->meth);
235 * @author Martin Schaller (04/2008)
237 void graphics_gc_destroy(struct graphics_gc *gc)
239 gc->meth.gc_destroy(gc->priv);
247 * @author Martin Schaller (04/2008)
249 void graphics_gc_set_foreground(struct graphics_gc *gc, struct color *c)
251 gc->meth.gc_set_foreground(gc->priv, c);
258 * @author Martin Schaller (04/2008)
260 void graphics_gc_set_background(struct graphics_gc *gc, struct color *c)
262 gc->meth.gc_set_background(gc->priv, c);
269 * @author Martin Schaller (04/2008)
271 void graphics_gc_set_linewidth(struct graphics_gc *gc, int width)
273 gc->meth.gc_set_linewidth(gc->priv, width);
280 * @author Martin Schaller (04/2008)
282 void graphics_gc_set_dashes(struct graphics_gc *gc, int width, int offset, unsigned char dash_list[], int n)
284 if (gc->meth.gc_set_dashes)
285 gc->meth.gc_set_dashes(gc->priv, width, offset, dash_list, n);
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
295 * @author Martin Schaller (04/2008)
297 struct graphics_image * graphics_image_new_scaled(struct graphics *gra, char *path, int w, int h)
299 struct graphics_image *this_;
301 this_=g_new0(struct graphics_image,1);
304 this_->priv=gra->meth.image_new(gra->priv, &this_->meth, path, &this_->width, &this_->height, &this_->hot, 0);
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
320 * @author Martin Schaller (04/2008)
322 struct graphics_image * graphics_image_new_scaled_rotated(struct graphics *gra, char *path, int w, int h, int rotate)
324 struct graphics_image *this_;
326 this_=g_new0(struct graphics_image,1);
329 this_->priv=gra->meth.image_new(gra->priv, &this_->meth, path, &this_->width, &this_->height, &this_->hot, rotate);
338 * Create a new image from file path
339 * @param gra the graphics instance
340 * @param path path of the image to load
342 * @author Martin Schaller (04/2008)
344 struct graphics_image * graphics_image_new(struct graphics *gra, char *path)
346 return graphics_image_new_scaled(gra, path, -1, -1);
353 * @author Martin Schaller (04/2008)
355 void graphics_image_free(struct graphics *gra, struct graphics_image *img)
357 if (gra->meth.image_free)
358 gra->meth.image_free(gra->priv, img->priv);
366 * @author Martin Schaller (04/2008)
368 void graphics_draw_restore(struct graphics *this_, struct point *p, int w, int h)
370 this_->meth.draw_restore(this_->priv, p, w, h);
377 * @author Martin Schaller (04/2008)
379 void graphics_draw_mode(struct graphics *this_, enum draw_mode_num mode)
381 this_->meth.draw_mode(this_->priv, mode);
388 * @author Martin Schaller (04/2008)
390 void graphics_draw_lines(struct graphics *this_, struct graphics_gc *gc, struct point *p, int count)
392 this_->meth.draw_lines(this_->priv, gc->priv, p, count);
399 * @author Martin Schaller (04/2008)
401 void graphics_draw_circle(struct graphics *this_, struct graphics_gc *gc, struct point *p, int r)
403 this_->meth.draw_circle(this_->priv, gc->priv, p, r);
410 * @author Martin Schaller (04/2008)
412 void graphics_draw_rectangle(struct graphics *this_, struct graphics_gc *gc, struct point *p, int w, int h)
414 this_->meth.draw_rectangle(this_->priv, gc->priv, p, w, h);
421 * @author Martin Schaller (04/2008)
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)
425 this_->meth.draw_text(this_->priv, gc1->priv, gc2 ? gc2->priv : NULL, font->priv, text, p, dx, dy);
432 * @author Martin Schaller (04/2008)
434 void graphics_get_text_bbox(struct graphics *this_, struct graphics_font *font, char *text, int dx, int dy, struct point *ret, int estimate)
436 this_->meth.get_text_bbox(this_->priv, font->priv, text, dx, dy, ret, estimate);
443 * @author Martin Schaller (04/2008)
445 void graphics_overlay_disable(struct graphics *this_, int disable)
447 if (this_->meth.overlay_disable)
448 this_->meth.overlay_disable(this_->priv, disable);
455 * @author Martin Schaller (04/2008)
457 void graphics_draw_image(struct graphics *this_, struct graphics_gc *gc, struct point *p, struct graphics_image *img)
459 this_->meth.draw_image(this_->priv, gc->priv, p, img->priv);
463 //##############################################################################################################
466 //# Authors: Martin Schaller (04/2008)
467 //##############################################################################################################
469 graphics_draw_drag(struct graphics *this_, struct point *p)
471 if (!this_->meth.draw_drag)
473 this_->meth.draw_drag(this_->priv, p);
478 graphics_background_gc(struct graphics *this_, struct graphics_gc *gc)
480 this_->meth.background_gc(this_->priv, gc ? gc->priv : NULL);
489 //##############################################################################################################
492 //# Authors: Martin Schaller (04/2008)
493 //##############################################################################################################
494 static void popup_view_html(struct popup_item *item, char *file)
497 sprintf(command,"firefox %s", file);
501 struct transformatin *tg;
504 //##############################################################################################################
507 //# Authors: Martin Schaller (04/2008)
508 //##############################################################################################################
509 static void graphics_popup(struct display_list *list, struct popup_item **popup)
515 struct popup_item *curr_item,*last=NULL;
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);
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));
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));
529 curr_item->submenu=last;
532 map_rect_destroy(mr);
540 * @author Martin Schaller (04/2008)
554 * @author Martin Schaller (04/2008)
556 static int xdisplay_free_list(gpointer key, gpointer value, gpointer user_data)
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));
576 * @author Martin Schaller (04/2008)
578 static void xdisplay_free(GHashTable *display_list)
580 g_hash_table_foreach_remove(display_list, xdisplay_free_list, NULL);
587 * @author Martin Schaller (04/2008)
589 static void display_add(struct displaylist *displaylist, struct item *item, int count, struct coord *c, char *label)
591 struct displayitem *di;
596 len=sizeof(*di)+count*sizeof(*c);
598 len+=strlen(label)+1;
602 di=(struct displayitem *)p;
604 p+=sizeof(*di)+count*sizeof(*c);
608 strcpy(di->label, label);
612 memcpy(di->c, c, count*sizeof(*c));
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);
624 * @author Martin Schaller (04/2008)
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)
628 int i,x,y,tl,tlm,th,thm,tlsq,l;
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);
645 for (i = 0 ; i < count-1 ; i++) {
668 dbg(0,"display_text: '%s', %d, %d, %d, %d %d\n", label, x, y, dx*0x10000/l, dy*0x10000/l, l);
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);
676 static void display_draw_arrow(struct point *p, int dx, int dy, int l, struct graphics_gc *gc, struct graphics *gra)
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);
687 static void display_draw_arrows(struct graphics *gra, struct graphics_gc *gc, struct point *pnt, int count)
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;
701 display_draw_arrow(&p, dx, dy, 10, gc, gra);
705 display_draw_arrow(&p, dx, dy, 10, gc, gra);
711 intersection(struct point * a1, int adx, int ady, struct point * b1, int bdx, int bdy,
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);
731 res->x = a1->x + a * adx / n;
732 res->y = a1->y + a * ady / n;
758 draw_circle(struct point *pnt, int diameter, int scale, int start, int len, struct point *res, int *pos, int dir)
763 dbg(0,"diameter=%d start=%d len=%d pos=%d dir=%d\n", diameter, start, len, *pos, dir);
775 while (i < 16 && c[i].fowler <= start)
777 while (i < 16 && c[i].fowler < end) {
778 res[*pos].x=pnt->x+c[i].x*diameter/20;
779 res[*pos].y=pnt->y+c[i].y*diameter/20;
787 while (start > 1024) {
793 while (i >= 0 && c[i].fowler >= end)
795 while (i >= 0 && c[i].fowler > start) {
796 res[*pos].x=pnt->x+c[i].x*diameter/20;
797 res[*pos].y=pnt->y+c[i].y*diameter/20;
809 fowler(int dy, int dx)
811 int adx, ady; /* Absolute Values of Dx and Dy */
812 int code; /* Angular Region Classification Code */
814 adx = (dx < 0) ? -dx : dx; /* Compute the absolute values. */
815 ady = (dy < 0) ? -dy : dy;
817 code = (adx < ady) ? 1 : 0;
825 return (dx == 0) ? 0 : 128*ady / adx; /* [ 0, 45] */
827 return (256 - (128*adx / ady)); /* ( 45, 90] */
829 return (256 + (128*adx / ady)); /* ( 90,135) */
831 return (512 - (128*ady / adx)); /* [135,180] */
833 return (512 + (128*ady / adx)); /* (180,225] */
835 return (768 - (128*adx / ady)); /* (225,270) */
837 return (768 + (128*adx / ady)); /* [270,315) */
839 return (1024 - (128*ady / adx));/* [315,360) */
844 int_sqrt(unsigned int n)
846 unsigned int h, p= 0, q= 1, r= n;
866 calc_offsets(int wi, int l, int dx, int dy, struct offset *res)
888 graphics_draw_polyline_as_polygon(struct graphics *gra, struct graphics_gc *gc, struct point *pnt, int count, int *width, int step)
891 struct point res[maxpoints], pos, poso, neg, nego;
892 int i, dx=0, dy=0, l=0, dxo=0, dyo=0;
894 int fow=0, fowo=0, delta;
895 int wi, ppos = maxpoints/2, npos = maxpoints/2;
897 int max_circle_points=20;
898 for (i = 0; i < count; i++) {
902 dx = (pnt[i + 1].x - pnt[i].x);
903 dy = (pnt[i + 1].y - pnt[i].y);
904 l = int_sqrt(dx * dx + dy * dy);
909 calc_offsets(wi, l, dx, dy, &o);
910 pos.x = pnt[i].x + o.ny;
911 pos.y = pnt[i].y + o.px;
912 neg.x = pnt[i].x + o.py;
913 neg.y = pnt[i].y + o.nx;
916 else if (i == count-1)
918 else if (npos < max_circle_points || ppos >= maxpoints-max_circle_points)
925 poso.x = pnt[i].x + oo.ny;
926 poso.y = pnt[i].y + oo.px;
927 nego.x = pnt[i].x + oo.py;
928 nego.y = pnt[i].y + oo.nx;
933 if (intersection(&pos, dx, dy, &poso, dxo, dyo, &res[ppos]))
937 draw_circle(&pnt[i], wi, prec, fowo-512, -delta, res, &npos, -1);
941 draw_circle(&pnt[i], wi, prec, fowo, 1024-delta, res, &ppos, 1);
943 if (intersection(&neg, dx, dy, &nego, dxo, dyo, &res[npos - 1]))
952 draw_circle(&pnt[i], wi, prec, fow-512, -512, res, &npos, -1);
955 dbg_assert(npos > 0);
956 dbg_assert(ppos < maxpoints);
957 gra->meth.draw_polygon(gra->priv, gc->priv, res+npos, ppos-npos);
964 draw_circle(&pnt[i], wi, prec, fow+512, 512, res, &ppos, 1);
970 calc_offsets(wi, l, dx, dy, &oo);
979 struct transformation *tg;
987 clipcode(struct wpoint *p, struct point_rect *r)
1003 clip_line(struct wpoint *p1, struct wpoint *p2, struct point_rect *r)
1005 int code1,code2,ret=1;
1007 code1=clipcode(p1, r);
1010 code2=clipcode(p2, r);
1016 while (code1 || code2) {
1020 p1->y+=(r->lu.x-p1->x)*dy/dx;
1021 p1->w+=(r->lu.x-p1->x)*dw/dx;
1023 } else if (code1 & 2) {
1024 p1->y+=(r->rl.x-p1->x)*dy/dx;
1025 p1->w+=(r->rl.x-p1->x)*dw/dx;
1027 } else if (code1 & 4) {
1028 p1->x+=(r->lu.y-p1->y)*dx/dy;
1029 p1->w+=(r->lu.y-p1->y)*dw/dy;
1031 } else if (code1 & 8) {
1032 p1->x+=(r->rl.y-p1->y)*dx/dy;
1033 p1->w+=(r->rl.y-p1->y)*dw/dy;
1036 code1=clipcode(p1, r);
1040 p2->y+=(r->lu.x-p2->x)*dy/dx;
1041 p2->w+=(r->lu.x-p2->x)*dw/dx;
1043 } else if (code2 & 2) {
1044 p2->y+=(r->rl.x-p2->x)*dy/dx;
1045 p2->w+=(r->rl.x-p2->x)*dw/dx;
1047 } else if (code2 & 4) {
1048 p2->x+=(r->lu.y-p2->y)*dx/dy;
1049 p2->w+=(r->lu.y-p2->y)*dw/dy;
1051 } else if (code2 & 8) {
1052 p2->x+=(r->rl.y-p2->y)*dx/dy;
1053 p2->w+=(r->rl.y-p2->y)*dw/dy;
1056 code2=clipcode(p2, r);
1062 graphics_draw_polyline_clipped(struct graphics *gra, struct graphics_gc *gc, struct point *pa, int count, int *width, int step, int poly)
1064 struct point p[count+1];
1065 int w[count*step+1];
1066 struct wpoint p1,p2;
1069 struct point_rect r=gra->r;
1073 for (i = 1 ; i < count ; i++) {
1074 if (width[i*step] > wmax)
1084 for (i = 0 ; i < count ; i++) {
1088 p1.w=width[(i-1)*step];
1092 /* 0 = invisible, 1 = completely visible, 3 = start point clipped, 5 = end point clipped, 7 both points clipped */
1093 code=clip_line(&p1, &p2, &r);
1094 if (((code == 1 || code == 5) && i == 1) || (code & 2)) {
1106 if (i == count-1 || (code & 4)) {
1109 graphics_draw_polyline_as_polygon(gra, gc, p, out, w, step);
1111 gra->meth.draw_lines(gra->priv, gc->priv, p, out);
1120 is_inside(struct point *p, struct point_rect *r, int edge)
1124 return p->x >= r->lu.x;
1126 return p->x <= r->rl.x;
1128 return p->y >= r->lu.y;
1130 return p->y <= r->rl.y;
1137 poly_intersection(struct point *p1, struct point *p2, struct point_rect *r, int edge, struct point *ret)
1143 ret->y=p1->y+(r->lu.x-p1->x)*dy/dx;
1147 ret->y=p1->y+(r->rl.x-p1->x)*dy/dx;
1151 ret->x=p1->x+(r->lu.y-p1->y)*dx/dy;
1155 ret->x=p1->x+(r->rl.y-p1->y)*dx/dy;
1162 graphics_draw_polygon_clipped(struct graphics *gra, struct graphics_gc *gc, struct point *pin, int count_in)
1164 struct point_rect r=gra->r;
1165 struct point *pout,*p,*s,pi;
1166 struct point p1[count_in+1];
1167 struct point p2[count_in+1];
1168 int count_out,edge=3;
1178 for (edge = 0 ; edge < 4 ; edge++) {
1182 for (i = 0 ; i < count_in ; i++) {
1183 if (is_inside(p, &r, edge)) {
1184 if (! is_inside(s, &r, edge)) {
1185 poly_intersection(s,p,&r,edge,&pi);
1186 pout[count_out++]=pi;
1188 pout[count_out++]=*p;
1190 if (is_inside(s, &r, edge)) {
1191 poly_intersection(p,s,&r,edge,&pi);
1192 pout[count_out++]=pi;
1207 gra->meth.draw_polygon(gra->priv, gc->priv, pin, count_in);
1216 * @author Martin Schaller (04/2008)
1218 static void xdisplay_draw_elements(struct graphics *gra, GHashTable *display_list, struct itemgra *itm)
1221 GList *l,*ls,*es,*types;
1222 enum item_type type;
1223 struct graphics_gc *gc = NULL;
1224 struct graphics_image *img=NULL;
1226 char path[PATH_MAX];
1227 struct point pa[16384];
1236 type=GPOINTER_TO_INT(types->data);
1237 ls=g_hash_table_lookup(display_list, GINT_TO_POINTER(type));
1240 graphics_gc_destroy(gc);
1242 graphics_image_free(gra, img);
1246 struct displayitem *di;
1250 gc=graphics_gc_new(gra);
1251 gc->meth.gc_set_foreground(gc->priv, &e->color);
1253 if (e->type == element_polyline) {
1254 count=transform(tg, pg, di->c, pa, di->count, 1, e->u.polyline.width, width);
1257 count=transform(tg, pg, di->c, pa, di->count, 1, 0, NULL);
1259 case element_polygon:
1263 for (i = 0 ; i < count ; i++) {
1264 dbg(0,"pa[%d]=%d,%d\n", i, pa[i].x, pa[i].y);
1267 dbg(0,"element_polygon count=%d\n",count);
1270 graphics_draw_polygon_clipped(gra, gc, pa, count);
1273 case element_polyline:
1275 if (e->u.polyline.width > 1) {
1276 graphics_draw_polyline_as_polygon(gra, gc, pa, count, width, 0);
1280 if (e->u.polyline.width > 1)
1281 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
1285 if (e->u.polyline.width > 0 && e->u.polyline.dash_num > 0)
1286 graphics_gc_set_dashes(gc, e->u.polyline.width,
1287 e->u.polyline.offset,
1288 e->u.polyline.dash_table,
1289 e->u.polyline.dash_num);
1291 if (di->label && !strcmp(di->label, "Bahnhofstr.") && di->item.type != type_street_1_city) {
1292 dbg(0,"0x%x,0x%x %s\n", di->item.id_hi, di->item.id_lo, item_to_name(di->item.type));
1294 graphics_draw_polyline_clipped(gra, gc, pa, count, width, 1, e->u.polyline.width > 1);
1300 case element_circle:
1302 if (e->u.circle.width > 1)
1303 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
1304 gra->meth.draw_circle(gra->priv, gc->priv, pa, e->u.circle.radius);
1305 if (di->label && e->text_size) {
1308 if (! gra->font[e->text_size])
1309 gra->font[e->text_size]=graphics_font_new(gra, e->text_size*20, 0);
1310 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);
1315 if (count && di->label) {
1316 if (! gra->font[e->text_size])
1317 gra->font[e->text_size]=graphics_font_new(gra, e->text_size*20, 0);
1318 label_line(gra, gra->gc[2], gra->gc[1], gra->font[e->text_size], pa, count, di->label);
1324 if (e->u.icon.src[0] == '/')
1325 strcpy(path,e->u.icon.src);
1327 sprintf(path,"%s/xpm/%s", navit_sharedir, e->u.icon.src);
1328 img=graphics_image_new_scaled_rotated(gra, path, e->u.icon.width, e->u.icon.height, e->u.icon.rotation);
1330 dbg(0,"failed to load icon '%s'\n", e->u.icon.src);
1333 p.x=pa[0].x - img->hot.x;
1334 p.y=pa[0].y - img->hot.y;
1335 gra->meth.draw_image(gra->priv, gra->gc[0]->priv, &p, img->priv);
1340 dbg(1,"image: '%s'\n", di->label);
1341 if (gra->meth.draw_image_warp)
1342 gra->meth.draw_image_warp(gra->priv, gra->gc[0]->priv, pa, count, di->label);
1344 dbg(0,"draw_image_warp not supported by graphics driver drawing '%s'\n", di->label);
1346 case element_arrows:
1347 display_draw_arrows(gra,gc,pa,count);
1350 printf("Unhandled element type %d\n", e->type);
1355 types=g_list_next(types);
1360 graphics_gc_destroy(gc);
1362 graphics_image_free(gra, img);
1366 graphics_draw_itemgra(struct graphics *gra, struct itemgra *itm, struct transformation *t)
1372 struct graphics_gc *gc = NULL;
1373 struct graphics_image *img;
1374 char path[PATH_MAX];
1379 struct element *e=es->data;
1380 int count=e->coord_count;
1381 struct point pnt[count+1];
1383 transform(t, projection_screen, e->coord, pnt, count, 0, 0, NULL);
1385 transform(t, projection_screen, &c, pnt, 1, 0, 0, NULL);
1388 gc=graphics_gc_new(gra);
1389 gc->meth.gc_set_foreground(gc->priv, &e->color);
1391 case element_polyline:
1392 if (e->u.polyline.width > 1)
1393 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
1394 if (e->u.polyline.width > 0 && e->u.polyline.dash_num > 0)
1395 graphics_gc_set_dashes(gc, e->u.polyline.width,
1396 e->u.polyline.offset,
1397 e->u.polyline.dash_table,
1398 e->u.polyline.dash_num);
1399 gra->meth.draw_lines(gra->priv, gc->priv, pnt, count);
1401 case element_circle:
1402 if (e->u.circle.width > 1)
1403 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
1404 gra->meth.draw_circle(gra->priv, gc->priv, &pnt[0], e->u.circle.radius);
1405 if (label && e->text_size) {
1408 if (! gra->font[e->text_size])
1409 gra->font[e->text_size]=graphics_font_new(gra, e->text_size*20, 0);
1410 gra->meth.draw_text(gra->priv, gra->gc[2]->priv, gra->gc[1]->priv, gra->font[e->text_size]->priv, label, &p, 0x10000, 0);
1414 if (e->u.icon.src[0] == '/')
1415 strcpy(path,e->u.icon.src);
1417 sprintf(path,"%s/xpm/%s", navit_sharedir, e->u.icon.src);
1418 img=graphics_image_new_scaled_rotated(gra, path, e->u.icon.width, e->u.icon.height, e->u.icon.rotation);
1420 dbg(0,"failed to load icon '%s'\n", e->u.icon.src);
1422 p.x=pnt[0].x - img->hot.x;
1423 p.y=pnt[0].y - img->hot.y;
1424 gra->meth.draw_image(gra->priv, gc->priv, &p, img->priv);
1425 graphics_image_free(gra, img);
1429 dbg(0,"dont know how to draw %d\n", e->type);
1431 graphics_gc_destroy(gc);
1440 * @author Martin Schaller (04/2008)
1442 static void xdisplay_draw_layer(GHashTable *display_list, struct graphics *gra, struct layer *lay, int order)
1445 struct itemgra *itm;
1450 if (order >= itm->order.min && order <= itm->order.max)
1451 xdisplay_draw_elements(gra, display_list, itm);
1452 itms=g_list_next(itms);
1460 * @author Martin Schaller (04/2008)
1462 static void xdisplay_draw(GHashTable *display_list, struct graphics *gra, struct layout *l, int order)
1470 xdisplay_draw_layer(display_list, gra, lay, order);
1471 lays=g_list_next(lays);
1479 * @author Martin Schaller (04/2008)
1481 extern void *route_selection;
1487 * @author Martin Schaller (04/2008)
1489 static void do_draw_map(struct displaylist *displaylist, struct transformation *t, struct map *m, int order)
1491 enum projection pro;
1492 struct map_rect *mr;
1494 int conv,count,max=16384;
1495 struct coord ca[max];
1497 struct map_selection *sel;
1498 struct coord_rect r;
1500 pro=map_projection(m);
1501 conv=map_requires_conversion(m);
1502 sel=transform_get_selection(t, pro, order);
1508 if (route_selection)
1509 mr=map_rect_new(m, route_selection);
1511 mr=map_rect_new(m, sel);
1513 map_selection_destroy(sel);
1516 while ((item=map_rect_get_item(mr))) {
1518 if (num < 7599 || num > 7599)
1522 if (item->id_hi != 0xb0031 || item->id_lo != 0x20c9aeea)
1525 count=item_coord_get_with_bbox(item, ca, item->type < type_line ? 1: max, &r);
1526 if (item->type >= type_line && count < 2) {
1527 dbg(1,"poly from map has only %d points\n", count);
1530 if (! map_selection_contains_rect(sel, &r))
1532 if (item->type < type_line) {
1534 if (! map_selection_contains_point(sel, &ca[0])) {
1535 dbg(1,"point not visible\n");
1539 } else if (item->type < type_area) {
1541 if (! map_selection_contains_polyline(sel, ca, count)) {
1542 dbg(1,"polyline not visible\n");
1548 if (! map_selection_contains_polygon(sel, ca, count)) {
1549 dbg(1,"polygon not visible\n");
1555 dbg(0,"point count overflow\n", count);
1556 if (!item_attr_get(item, attr_label, &attr))
1558 if (conv && attr.u.str && attr.u.str[0]) {
1559 char *str=map_convert_string(m, attr.u.str);
1560 display_add(displaylist, item, count, ca, str);
1561 map_convert_free(str);
1563 display_add(displaylist, item, count, ca, attr.u.str);
1565 map_rect_destroy(mr);
1566 map_selection_destroy(sel);
1573 * @author Martin Schaller (04/2008)
1575 static void do_draw(struct displaylist *displaylist, struct transformation *t, GList *mapsets, int order)
1579 struct mapset_handle *h;
1585 while ((m=mapset_next(h, 1))) {
1586 do_draw_map(displaylist, t, m, order);
1595 * @author Martin Schaller (04/2008)
1597 int graphics_ready(struct graphics *this_)
1599 return this_->ready;
1606 * @author Martin Schaller (04/2008)
1608 void graphics_displaylist_draw(struct graphics *gra, struct displaylist *displaylist, struct transformation *trans, struct layout *l, int callback)
1610 int order=transform_get_order(trans);
1615 // FIXME find a better place to set the background color
1617 graphics_gc_set_background(gra->gc[0], &l->color);
1618 graphics_gc_set_foreground(gra->gc[0], &l->color);
1619 gra->default_font = g_strdup(l->font);
1621 graphics_background_gc(gra, gra->gc[0]);
1622 gra->meth.draw_mode(gra->priv, draw_mode_begin);
1623 gra->meth.draw_rectangle(gra->priv, gra->gc[0]->priv, &p, 32767, 32767);
1625 xdisplay_draw(displaylist->dl, gra, l, order+l->order_delta);
1627 callback_list_call_attr_0(gra->cbl, attr_postdraw);
1628 gra->meth.draw_mode(gra->priv, draw_mode_end);
1636 * @author Martin Schaller (04/2008)
1638 void graphics_displaylist_move(struct displaylist *displaylist, int dx, int dy)
1640 struct displaylist_handle *dlh;
1641 struct displayitem *di;
1644 dlh=graphics_displaylist_open(displaylist);
1645 while ((di=graphics_displaylist_next(dlh))) {
1646 for (i = 0 ; i < di->count ; i++) {
1651 graphics_displaylist_close(dlh);
1659 * @author Martin Schaller (04/2008)
1661 void graphics_draw(struct graphics *gra, struct displaylist *displaylist, GList *mapsets, struct transformation *trans, struct layout *l)
1663 int order=transform_get_order(trans);
1668 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));
1671 xdisplay_free(displaylist->dl);
1672 dbg(1,"order=%d\n", order);
1676 for (i = 0 ; i < data_window_type_end; i++) {
1677 data_window_begin(co->data_window[i]);
1682 order+=l->order_delta;
1684 do_draw(displaylist, trans, mapsets, order);
1685 // profile(1,"do_draw");
1686 graphics_displaylist_draw(gra, displaylist, trans, l, 1);
1687 profile(1,"xdisplay_draw");
1691 for (i = 0 ; i < data_window_type_end; i++) {
1692 data_window_end(co->data_window[i]);
1702 * @author Martin Schaller (04/2008)
1704 struct displaylist_handle {
1705 GList *hl_head,*hl,*l;
1712 * @author Martin Schaller (04/2008)
1714 struct displaylist_handle * graphics_displaylist_open(struct displaylist *displaylist)
1716 struct displaylist_handle *ret;
1718 ret=g_new0(struct displaylist_handle, 1);
1719 ret->hl_head=ret->hl=g_hash_to_list(displaylist->dl);
1728 * @author Martin Schaller (04/2008)
1730 struct displayitem * graphics_displaylist_next(struct displaylist_handle *dlh)
1732 struct displayitem *ret;
1736 dlh->l=dlh->hl->data;
1737 dlh->hl=g_list_next(dlh->hl);
1740 dlh->l=g_list_next(dlh->l);
1748 * @author Martin Schaller (04/2008)
1750 void graphics_displaylist_close(struct displaylist_handle *dlh)
1752 g_list_free(dlh->hl_head);
1760 * @author Martin Schaller (04/2008)
1762 struct displaylist * graphics_displaylist_new(void)
1764 struct displaylist *ret=g_new(struct displaylist, 1);
1766 ret->dl=g_hash_table_new(NULL,NULL);
1775 * @author Martin Schaller (04/2008)
1777 struct item * graphics_displayitem_get_item(struct displayitem *di)
1786 * @author Martin Schaller (04/2008)
1788 char * graphics_displayitem_get_label(struct displayitem *di)
1797 * @author Martin Schaller (04/2008)
1799 static int within_dist_point(struct point *p0, struct point *p1, int dist)
1801 if (p0->x == 32767 || p0->y == 32767 || p1->x == 32767 || p1->y == 32767)
1803 if (p0->x == -32768 || p0->y == -32768 || p1->x == -32768 || p1->y == -32768)
1805 if ((p0->x-p1->x)*(p0->x-p1->x) + (p0->y-p1->y)*(p0->y-p1->y) <= dist*dist) {
1815 * @author Martin Schaller (04/2008)
1817 static int within_dist_line(struct point *p, struct point *line_p0, struct point *line_p1, int dist)
1821 struct point line_p;
1823 vx=line_p1->x-line_p0->x;
1824 vy=line_p1->y-line_p0->y;
1830 return within_dist_point(p, line_p0, dist);
1833 return within_dist_point(p, line_p1, dist);
1835 line_p.x=line_p0->x+vx*c1/c2;
1836 line_p.y=line_p0->y+vy*c1/c2;
1837 return within_dist_point(p, &line_p, dist);
1844 * @author Martin Schaller (04/2008)
1846 static int within_dist_polyline(struct point *p, struct point *line_pnt, int count, int dist, int close)
1849 for (i = 0 ; i < count-1 ; i++) {
1850 if (within_dist_line(p,line_pnt+i,line_pnt+i+1,dist)) {
1855 return (within_dist_line(p,line_pnt,line_pnt+count-1,dist));
1863 * @author Martin Schaller (04/2008)
1865 static int within_dist_polygon(struct point *p, struct point *poly_pnt, int count, int dist)
1868 for (i = 0, j = count-1; i < count; j = i++) {
1869 if ((((poly_pnt[i].y <= p->y) && ( p->y < poly_pnt[j].y )) ||
1870 ((poly_pnt[j].y <= p->y) && ( p->y < poly_pnt[i].y))) &&
1871 (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))
1875 return within_dist_polyline(p, poly_pnt, count, dist, 1);
1883 * @author Martin Schaller (04/2008)
1885 int graphics_displayitem_within_dist(struct displayitem *di, struct point *p, int dist)
1887 struct point pa[16384];
1890 count=transform(tg, pg, di->c, pa, di->count, 1, 0, NULL);
1892 if (di->item.type < type_line) {
1893 return within_dist_point(p, &pa[0], dist);
1895 if (di->item.type < type_area) {
1896 return within_dist_polyline(p, pa, count, dist, 0);
1898 return within_dist_polygon(p, pa, count, dist);