10 #include "projection.h"
13 #include "transform.h"
23 struct graphics_priv *priv;
24 struct graphics_methods meth;
25 struct graphics_font *font[16];
26 struct graphics_gc *gc[3];
35 graphics_new(const char *type, struct attr **attrs)
37 struct graphics *this_;
38 struct graphics_priv * (*new)(struct graphics_methods *meth, struct attr **attrs);
40 new=plugin_get_graphics_type(type);
43 this_=g_new0(struct graphics, 1);
44 this_->priv=(*new)(&this_->meth, attrs);
49 graphics_overlay_new(struct graphics *parent, struct point *p, int w, int h)
51 struct graphics *this_;
52 this_=g_new0(struct graphics, 1);
53 this_->priv=parent->meth.overlay_new(parent->priv, &this_->meth, p, w, h);
59 graphics_init(struct graphics *this_)
61 this_->gc[0]=graphics_gc_new(this_);
62 graphics_gc_set_background(this_->gc[0], &(struct color) { 0xffff, 0xefef, 0xb7b7 });
63 graphics_gc_set_foreground(this_->gc[0], &(struct color) { 0xffff, 0xefef, 0xb7b7 });
64 this_->gc[1]=graphics_gc_new(this_);
65 graphics_gc_set_background(this_->gc[1], &(struct color) { 0x0000, 0x0000, 0x0000 });
66 graphics_gc_set_foreground(this_->gc[1], &(struct color) { 0xffff, 0xffff, 0xffff });
67 this_->gc[2]=graphics_gc_new(this_);
68 graphics_gc_set_background(this_->gc[2], &(struct color) { 0xffff, 0xffff, 0xffff });
69 graphics_gc_set_foreground(this_->gc[2], &(struct color) { 0xffff, 0xffff, 0xffff });
70 this_->meth.background_gc(this_->priv, this_->gc[0]->priv);
74 graphics_get_data(struct graphics *this_, char *type)
76 return (this_->meth.get_data(this_->priv, type));
80 graphics_register_resize_callback(struct graphics *this_, void (*callback)(void *data, int w, int h), void *data)
82 this_->meth.register_resize_callback(this_->priv, callback, data);
86 graphics_register_button_callback(struct graphics *this_, void (*callback)(void *data, int pressed, int button, struct point *p), void *data)
88 this_->meth.register_button_callback(this_->priv, callback, data);
92 graphics_register_motion_callback(struct graphics *this_, void (*callback)(void *data, struct point *p), void *data)
94 this_->meth.register_motion_callback(this_->priv, callback, data);
97 struct graphics_font *
98 graphics_font_new(struct graphics *gra, int size)
100 struct graphics_font *this_;
102 this_=g_new0(struct graphics_font,1);
103 this_->priv=gra->meth.font_new(gra->priv, &this_->meth, size);
108 graphics_gc_new(struct graphics *gra)
110 struct graphics_gc *this_;
112 this_=g_new0(struct graphics_gc,1);
113 this_->priv=gra->meth.gc_new(gra->priv, &this_->meth);
118 graphics_gc_set_foreground(struct graphics_gc *gc, struct color *c)
120 gc->meth.gc_set_foreground(gc->priv, c);
124 graphics_gc_set_background(struct graphics_gc *gc, struct color *c)
126 gc->meth.gc_set_background(gc->priv, c);
130 graphics_gc_set_linewidth(struct graphics_gc *gc, int width)
132 gc->meth.gc_set_linewidth(gc->priv, width);
135 struct graphics_image *
136 graphics_image_new(struct graphics *gra, char *path)
138 struct graphics_image *this_;
140 this_=g_new0(struct graphics_image,1);
141 this_->priv=gra->meth.image_new(gra->priv, &this_->meth, path, &this_->width, &this_->height, &this_->hot);
150 graphics_draw_restore(struct graphics *this_, struct point *p, int w, int h)
152 this_->meth.draw_restore(this_->priv, p, w, h);
156 graphics_draw_mode(struct graphics *this_, enum draw_mode_num mode)
158 this_->meth.draw_mode(this_->priv, mode);
162 graphics_draw_lines(struct graphics *this_, struct graphics_gc *gc, struct point *p, int count)
164 this_->meth.draw_lines(this_->priv, gc->priv, p, count);
168 graphics_draw_circle(struct graphics *this_, struct graphics_gc *gc, struct point *p, int r)
170 this_->meth.draw_circle(this_->priv, gc->priv, p, r);
175 graphics_draw_rectangle(struct graphics *this_, struct graphics_gc *gc, struct point *p, int w, int h)
177 this_->meth.draw_rectangle(this_->priv, gc->priv, p, w, h);
187 popup_view_html(struct popup_item *item, char *file)
190 sprintf(command,"firefox %s", file);
195 graphics_popup(struct display_list *list, struct popup_item **popup)
201 struct popup_item *curr_item,*last=NULL;
203 mr=map_rect_new(item->map, NULL, NULL, 0);
204 printf("id hi=0x%x lo=0x%x\n", item->id_hi, item->id_lo);
205 item=map_rect_get_item_byid(mr, item->id_hi, item->id_lo);
207 if (item_attr_get(item, attr_name, &attr)) {
208 curr_item=popup_item_new_text(popup,attr.u.str,1);
209 if (item_attr_get(item, attr_info_html, &attr)) {
210 popup_item_new_func(&last,"HTML Info",1, popup_view_html, g_strdup(attr.u.str));
212 if (item_attr_get(item, attr_price_html, &attr)) {
213 popup_item_new_func(&last,"HTML Preis",2, popup_view_html, g_strdup(attr.u.str));
215 curr_item->submenu=last;
218 map_rect_destroy(mr);
231 xdisplay_free_list(gpointer key, gpointer value, gpointer user_data)
237 struct displayitem *di=l->data;
238 if (! di->displayed && di->item.type < type_line)
239 dbg(1,"warning: item '%s' not displayed\n", item_to_name(di->item.type));
248 xdisplay_free(GHashTable *display_list)
250 g_hash_table_foreach_remove(display_list, xdisplay_free_list, NULL);
254 display_add(struct displaylist *displaylist, struct item *item, int count, struct point *pnt, char *label)
256 struct displayitem *di;
261 len=sizeof(*di)+count*sizeof(*pnt);
263 len+=strlen(label)+1;
267 di=(struct displayitem *)p;
269 p+=sizeof(*di)+count*sizeof(*pnt);
273 strcpy(di->label, label);
277 memcpy(di->pnt, pnt, count*sizeof(*pnt));
279 l=g_hash_table_lookup(displaylist->dl, GINT_TO_POINTER(item->type));
280 l=g_list_prepend(l, di);
281 g_hash_table_insert(displaylist->dl, GINT_TO_POINTER(item->type), l);
286 label_line(struct graphics *gra, struct graphics_gc *fg, struct graphics_gc *bg, struct graphics_font *font, struct point *p, int count, char *label)
292 tl=strlen(label)*400;
293 for (i = 0 ; i < count-1 ; i++) {
298 l=(int)sqrt((float)(dx*dx+dy*dy));
315 printf("display_text: '%s', %d, %d, %d, %d %d\n", label, x, y, dx*0x10000/l, dy*0x10000/l, l);
317 gra->meth.draw_text(gra->priv, fg->priv, bg->priv, font->priv, label, &p_t, dx*0x10000/l, dy*0x10000/l);
324 xdisplay_draw_elements(struct graphics *gra, GHashTable *display_list, struct itemtype *itm)
327 GList *l,*ls,*es,*types;
329 struct graphics_gc *gc;
330 struct graphics_image *img;
338 type=GPOINTER_TO_INT(types->data);
339 ls=g_hash_table_lookup(display_list, GINT_TO_POINTER(type));
344 struct displayitem *di;
348 gc=graphics_gc_new(gra);
349 gc->meth.gc_set_foreground(gc->priv, &e->color);
352 case element_polygon:
353 gra->meth.draw_polygon(gra->priv, gc->priv, di->pnt, di->count);
355 case element_polyline:
356 if (e->u.polyline.width > 1)
357 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
358 gra->meth.draw_lines(gra->priv, gc->priv, di->pnt, di->count);
361 if (e->u.circle.width > 1)
362 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
363 gra->meth.draw_circle(gra->priv, gc->priv, &di->pnt[0], e->u.circle.radius);
364 if (di->label && e->label_size) {
367 if (! gra->font[e->label_size])
368 gra->font[e->label_size]=graphics_font_new(gra, e->label_size*20);
369 gra->meth.draw_text(gra->priv, gra->gc[2]->priv, gra->gc[1]->priv, gra->font[e->label_size]->priv, di->label, &p, 0x10000, 0);
374 if (! gra->font[e->label_size])
375 gra->font[e->label_size]=graphics_font_new(gra, e->label_size*20);
376 label_line(gra, gra->gc[2], gra->gc[1], gra->font[e->label_size], di->pnt, di->count, di->label);
381 char *icon=g_strjoin(NULL,getenv("NAVIT_SHAREDIR"), "/xpm/", e->u.icon.src, NULL);
382 img=graphics_image_new(gra, icon);
385 g_warning("failed to load icon '%s'\n", e->u.icon.src);
388 p.x=di->pnt[0].x - img->hot.x;
389 p.y=di->pnt[0].y - img->hot.y;
390 gra->meth.draw_image(gra->priv, gra->gc[0]->priv, &p, img->priv);
394 printf("image: '%s'\n", di->label);
395 gra->meth.draw_image_warp(gra->priv, gra->gc[0]->priv, di->pnt, di->count, di->label);
398 printf("Unhandled element type %d\n", e->type);
403 types=g_list_next(types);
410 xdisplay_draw_layer(GHashTable *display_list, struct graphics *gra, struct layer *lay, int order)
413 struct itemtype *itm;
418 if (order >= itm->order_min && order <= itm->order_max)
419 xdisplay_draw_elements(gra, display_list, itm);
420 itms=g_list_next(itms);
425 xdisplay_draw_layout(GHashTable *display_list, struct graphics *gra, struct layout *l, int order)
433 xdisplay_draw_layer(display_list, gra, lay, order);
434 lays=g_list_next(lays);
439 xdisplay_draw(GHashTable *display_list, struct graphics *gra, GList *layouts, int order)
445 xdisplay_draw_layout(display_list, gra, l, order);
447 layouts=g_list_next(layouts);
451 extern void *route_selection;
454 do_draw_map(struct displaylist *displaylist, struct transformation *t, struct map *m, struct map_selection *sel)
460 int i, conv,count,max=16384;
461 struct point pnt[max];
462 struct coord ca[max];
466 pro=map_projection(m);
467 conv=map_requires_conversion(m);
468 transform_rect(t, pro, &sel->u.c_rect);
470 mr=map_rect_new(m, route_selection);
472 mr=map_rect_new(m, sel);
473 while ((item=map_rect_get_item(mr))) {
474 if (item->type < type_line) {
475 item_coord_get(item, &c, 1);
476 if (!transform(t, pro, &c, &pnt[0])) {
477 dbg(1,"not visible\n");
482 count = item_coord_get(item, ca, max);
487 for (i=1; i < count; i++) {
488 coord_rect_extend(&r, &ca[i]);
490 if (!transform_contains(t, pro, &r)) {
491 dbg(1,"poly not visible\n");
494 count = transform_array(t, pro, ca, pnt, count, 1);
497 g_assert(count < max);
499 if (!item_attr_get(item, attr_label, &attr))
501 if (conv && attr.u.str && attr.u.str[0]) {
502 char *str=map_convert_string(m, attr.u.str);
503 display_add(displaylist, item, count, pnt, str);
504 map_convert_free(str);
506 display_add(displaylist, item, count, pnt, attr.u.str);
508 map_rect_destroy(mr);
512 do_draw(struct displaylist *displaylist, struct transformation *t, GList *mapsets, int order, struct route *route)
514 struct map_selection sel;
517 struct mapset_handle *h;
522 sel.order[layer_town]=1*order;
523 sel.order[layer_street]=order;
524 sel.order[layer_poly]=1*order;
527 while ((m=mapset_next(h, 1))) {
528 do_draw_map(displaylist, t, m, &sel);
532 m = route_get_map(route);
534 do_draw_map(displaylist, t, m, &sel);
539 graphics_ready(struct graphics *this_)
545 graphics_displaylist_draw(struct graphics *gra, struct displaylist *displaylist, struct transformation *trans, GList *layouts, struct route *route)
547 int order=transform_get_order(trans);
548 gra->meth.draw_mode(gra->priv, draw_mode_begin);
550 route_draw(route, trans, displaylist);
551 xdisplay_draw(displaylist->dl, gra, layouts, order);
552 gra->meth.draw_mode(gra->priv, draw_mode_end);
556 graphics_displaylist_move(struct displaylist *displaylist, int dx, int dy)
558 struct displaylist_handle *dlh;
559 struct displayitem *di;
562 dlh=graphics_displaylist_open(displaylist);
563 while ((di=graphics_displaylist_next(dlh))) {
564 for (i = 0 ; i < di->count ; i++) {
569 graphics_displaylist_close(dlh);
574 graphics_draw(struct graphics *gra, struct displaylist *displaylist, GList *mapsets, struct transformation *trans, GList *layouts, struct route *route)
576 int order=transform_get_order(trans);
581 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));
584 xdisplay_free(displaylist->dl);
585 dbg(1,"order=%d\n", order);
589 for (i = 0 ; i < data_window_type_end; i++) {
590 data_window_begin(co->data_window[i]);
594 do_draw(displaylist, trans, mapsets, order, route);
595 profile(1,"do_draw");
596 graphics_displaylist_draw(gra, displaylist, trans, layouts, route);
597 profile(1,"xdisplay_draw");
601 for (i = 0 ; i < data_window_type_end; i++) {
602 data_window_end(co->data_window[i]);
609 struct displaylist_handle {
610 GList *hl_head,*hl,*l;
614 struct displaylist_handle *
615 graphics_displaylist_open(struct displaylist *displaylist)
617 struct displaylist_handle *ret;
619 ret=g_new0(struct displaylist_handle, 1);
620 ret->hl_head=ret->hl=g_hash_to_list(displaylist->dl);
626 graphics_displaylist_next(struct displaylist_handle *dlh)
628 struct displayitem *ret;
632 dlh->l=dlh->hl->data;
633 dlh->hl=g_list_next(dlh->hl);
636 dlh->l=g_list_next(dlh->l);
641 graphics_displaylist_close(struct displaylist_handle *dlh)
643 g_list_free(dlh->hl_head);
648 graphics_displaylist_new(void)
650 struct displaylist *ret=g_new(struct displaylist, 1);
652 ret->dl=g_hash_table_new(NULL,NULL);
658 graphics_displayitem_get_item(struct displayitem *di)
664 graphics_displayitem_get_label(struct displayitem *di)
670 within_dist_point(struct point *p0, struct point *p1, int dist)
672 if (p0->x == 32767 || p0->y == 32767 || p1->x == 32767 || p1->y == 32767)
674 if (p0->x == -32768 || p0->y == -32768 || p1->x == -32768 || p1->y == -32768)
676 if ((p0->x-p1->x)*(p0->x-p1->x) + (p0->y-p1->y)*(p0->y-p1->y) <= dist*dist) {
683 within_dist_line(struct point *p, struct point *line_p0, struct point *line_p1, int dist)
689 vx=line_p1->x-line_p0->x;
690 vy=line_p1->y-line_p0->y;
696 return within_dist_point(p, line_p0, dist);
699 return within_dist_point(p, line_p1, dist);
701 line_p.x=line_p0->x+vx*c1/c2;
702 line_p.y=line_p0->y+vy*c1/c2;
703 return within_dist_point(p, &line_p, dist);
707 within_dist_polyline(struct point *p, struct point *line_pnt, int count, int dist, int close)
710 for (i = 0 ; i < count-1 ; i++) {
711 if (within_dist_line(p,line_pnt+i,line_pnt+i+1,dist)) {
716 return (within_dist_line(p,line_pnt,line_pnt+count-1,dist));
721 within_dist_polygon(struct point *p, struct point *poly_pnt, int count, int dist)
724 for (i = 0, j = count-1; i < count; j = i++) {
725 if ((((poly_pnt[i].y <= p->y) && ( p->y < poly_pnt[j].y )) ||
726 ((poly_pnt[j].y <= p->y) && ( p->y < poly_pnt[i].y))) &&
727 (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))
731 return within_dist_polyline(p, poly_pnt, count, dist, 1);
736 graphics_displayitem_within_dist(struct displayitem *di, struct point *p, int dist)
738 if (di->item.type < type_line) {
739 return within_dist_point(p, &di->pnt[0], dist);
741 if (di->item.type < type_area) {
742 return within_dist_polyline(p, di->pnt, di->count, dist, 0);
744 return within_dist_polygon(p, di->pnt, di->count, dist);