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_destroy(struct graphics_gc *gc)
120 gc->meth.gc_destroy(gc->priv);
124 graphics_gc_set_foreground(struct graphics_gc *gc, struct color *c)
126 gc->meth.gc_set_foreground(gc->priv, c);
130 graphics_gc_set_background(struct graphics_gc *gc, struct color *c)
132 gc->meth.gc_set_background(gc->priv, c);
136 graphics_gc_set_linewidth(struct graphics_gc *gc, int width)
138 gc->meth.gc_set_linewidth(gc->priv, width);
141 struct graphics_image *
142 graphics_image_new(struct graphics *gra, char *path)
144 struct graphics_image *this_;
146 this_=g_new0(struct graphics_image,1);
147 this_->priv=gra->meth.image_new(gra->priv, &this_->meth, path, &this_->width, &this_->height, &this_->hot);
156 graphics_draw_restore(struct graphics *this_, struct point *p, int w, int h)
158 this_->meth.draw_restore(this_->priv, p, w, h);
162 graphics_draw_mode(struct graphics *this_, enum draw_mode_num mode)
164 this_->meth.draw_mode(this_->priv, mode);
168 graphics_draw_lines(struct graphics *this_, struct graphics_gc *gc, struct point *p, int count)
170 this_->meth.draw_lines(this_->priv, gc->priv, p, count);
174 graphics_draw_circle(struct graphics *this_, struct graphics_gc *gc, struct point *p, int r)
176 this_->meth.draw_circle(this_->priv, gc->priv, p, r);
181 graphics_draw_rectangle(struct graphics *this_, struct graphics_gc *gc, struct point *p, int w, int h)
183 this_->meth.draw_rectangle(this_->priv, gc->priv, p, w, h);
193 popup_view_html(struct popup_item *item, char *file)
196 sprintf(command,"firefox %s", file);
201 graphics_popup(struct display_list *list, struct popup_item **popup)
207 struct popup_item *curr_item,*last=NULL;
209 mr=map_rect_new(item->map, NULL, NULL, 0);
210 printf("id hi=0x%x lo=0x%x\n", item->id_hi, item->id_lo);
211 item=map_rect_get_item_byid(mr, item->id_hi, item->id_lo);
213 if (item_attr_get(item, attr_name, &attr)) {
214 curr_item=popup_item_new_text(popup,attr.u.str,1);
215 if (item_attr_get(item, attr_info_html, &attr)) {
216 popup_item_new_func(&last,"HTML Info",1, popup_view_html, g_strdup(attr.u.str));
218 if (item_attr_get(item, attr_price_html, &attr)) {
219 popup_item_new_func(&last,"HTML Preis",2, popup_view_html, g_strdup(attr.u.str));
221 curr_item->submenu=last;
224 map_rect_destroy(mr);
237 xdisplay_free_list(gpointer key, gpointer value, gpointer user_data)
243 struct displayitem *di=l->data;
244 if (! di->displayed && di->item.type < type_line)
245 dbg(1,"warning: item '%s' not displayed\n", item_to_name(di->item.type));
254 xdisplay_free(GHashTable *display_list)
256 g_hash_table_foreach_remove(display_list, xdisplay_free_list, NULL);
260 display_add(struct displaylist *displaylist, struct item *item, int count, struct point *pnt, char *label)
262 struct displayitem *di;
267 len=sizeof(*di)+count*sizeof(*pnt);
269 len+=strlen(label)+1;
273 di=(struct displayitem *)p;
275 p+=sizeof(*di)+count*sizeof(*pnt);
279 strcpy(di->label, label);
283 memcpy(di->pnt, pnt, count*sizeof(*pnt));
285 l=g_hash_table_lookup(displaylist->dl, GINT_TO_POINTER(item->type));
286 l=g_list_prepend(l, di);
287 g_hash_table_insert(displaylist->dl, GINT_TO_POINTER(item->type), l);
292 label_line(struct graphics *gra, struct graphics_gc *fg, struct graphics_gc *bg, struct graphics_font *font, struct point *p, int count, char *label)
298 tl=strlen(label)*400;
299 for (i = 0 ; i < count-1 ; i++) {
304 l=(int)sqrt((float)(dx*dx+dy*dy));
321 printf("display_text: '%s', %d, %d, %d, %d %d\n", label, x, y, dx*0x10000/l, dy*0x10000/l, l);
323 gra->meth.draw_text(gra->priv, fg->priv, bg->priv, font->priv, label, &p_t, dx*0x10000/l, dy*0x10000/l);
330 xdisplay_draw_elements(struct graphics *gra, GHashTable *display_list, struct itemtype *itm)
333 GList *l,*ls,*es,*types;
335 struct graphics_gc *gc;
336 struct graphics_image *img;
344 type=GPOINTER_TO_INT(types->data);
345 ls=g_hash_table_lookup(display_list, GINT_TO_POINTER(type));
350 struct displayitem *di;
354 gc=graphics_gc_new(gra);
355 gc->meth.gc_set_foreground(gc->priv, &e->color);
358 case element_polygon:
359 gra->meth.draw_polygon(gra->priv, gc->priv, di->pnt, di->count);
361 case element_polyline:
362 if (e->u.polyline.width > 1)
363 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
364 gra->meth.draw_lines(gra->priv, gc->priv, di->pnt, di->count);
367 if (e->u.circle.width > 1)
368 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
369 gra->meth.draw_circle(gra->priv, gc->priv, &di->pnt[0], e->u.circle.radius);
370 if (di->label && e->label_size) {
373 if (! gra->font[e->label_size])
374 gra->font[e->label_size]=graphics_font_new(gra, e->label_size*20);
375 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);
380 if (! gra->font[e->label_size])
381 gra->font[e->label_size]=graphics_font_new(gra, e->label_size*20);
382 label_line(gra, gra->gc[2], gra->gc[1], gra->font[e->label_size], di->pnt, di->count, di->label);
387 char *icon=g_strjoin(NULL,getenv("NAVIT_SHAREDIR"), "/xpm/", e->u.icon.src, NULL);
388 img=graphics_image_new(gra, icon);
391 g_warning("failed to load icon '%s'\n", e->u.icon.src);
394 p.x=di->pnt[0].x - img->hot.x;
395 p.y=di->pnt[0].y - img->hot.y;
396 gra->meth.draw_image(gra->priv, gra->gc[0]->priv, &p, img->priv);
400 printf("image: '%s'\n", di->label);
401 gra->meth.draw_image_warp(gra->priv, gra->gc[0]->priv, di->pnt, di->count, di->label);
404 printf("Unhandled element type %d\n", e->type);
409 types=g_list_next(types);
416 xdisplay_draw_layer(GHashTable *display_list, struct graphics *gra, struct layer *lay, int order)
419 struct itemtype *itm;
424 if (order >= itm->order_min && order <= itm->order_max)
425 xdisplay_draw_elements(gra, display_list, itm);
426 itms=g_list_next(itms);
431 xdisplay_draw_layout(GHashTable *display_list, struct graphics *gra, struct layout *l, int order)
439 xdisplay_draw_layer(display_list, gra, lay, order);
440 lays=g_list_next(lays);
445 xdisplay_draw(GHashTable *display_list, struct graphics *gra, GList *layouts, int order)
451 xdisplay_draw_layout(display_list, gra, l, order);
453 layouts=g_list_next(layouts);
457 extern void *route_selection;
460 do_draw_map(struct displaylist *displaylist, struct transformation *t, struct map *m, struct map_selection *sel)
466 int i, conv,count,max=16384;
467 struct point pnt[max];
468 struct coord ca[max];
472 pro=map_projection(m);
473 conv=map_requires_conversion(m);
474 transform_rect(t, pro, &sel->u.c_rect);
476 mr=map_rect_new(m, route_selection);
478 mr=map_rect_new(m, sel);
479 while ((item=map_rect_get_item(mr))) {
480 if (item->type < type_line) {
481 item_coord_get(item, &c, 1);
482 if (!transform(t, pro, &c, &pnt[0])) {
483 dbg(1,"not visible\n");
488 count = item_coord_get(item, ca, max);
493 for (i=1; i < count; i++) {
494 coord_rect_extend(&r, &ca[i]);
496 if (!transform_contains(t, pro, &r)) {
497 dbg(1,"poly not visible\n");
500 count = transform_array(t, pro, ca, pnt, count, 1);
503 g_assert(count < max);
505 if (!item_attr_get(item, attr_label, &attr))
507 if (conv && attr.u.str && attr.u.str[0]) {
508 char *str=map_convert_string(m, attr.u.str);
509 display_add(displaylist, item, count, pnt, str);
510 map_convert_free(str);
512 display_add(displaylist, item, count, pnt, attr.u.str);
514 map_rect_destroy(mr);
518 do_draw(struct displaylist *displaylist, struct transformation *t, GList *mapsets, int order, struct route *route)
520 struct map_selection sel;
523 struct mapset_handle *h;
528 sel.order[layer_town]=1*order;
529 sel.order[layer_street]=order;
530 sel.order[layer_poly]=1*order;
533 while ((m=mapset_next(h, 1))) {
534 do_draw_map(displaylist, t, m, &sel);
538 m = route_get_map(route);
540 do_draw_map(displaylist, t, m, &sel);
545 graphics_ready(struct graphics *this_)
551 graphics_displaylist_draw(struct graphics *gra, struct displaylist *displaylist, struct transformation *trans, GList *layouts, struct route *route)
553 int order=transform_get_order(trans);
554 gra->meth.draw_mode(gra->priv, draw_mode_begin);
556 route_draw(route, trans, displaylist);
557 xdisplay_draw(displaylist->dl, gra, layouts, order);
558 gra->meth.draw_mode(gra->priv, draw_mode_end);
562 graphics_displaylist_move(struct displaylist *displaylist, int dx, int dy)
564 struct displaylist_handle *dlh;
565 struct displayitem *di;
568 dlh=graphics_displaylist_open(displaylist);
569 while ((di=graphics_displaylist_next(dlh))) {
570 for (i = 0 ; i < di->count ; i++) {
575 graphics_displaylist_close(dlh);
580 graphics_draw(struct graphics *gra, struct displaylist *displaylist, GList *mapsets, struct transformation *trans, GList *layouts, struct route *route)
582 int order=transform_get_order(trans);
587 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));
590 xdisplay_free(displaylist->dl);
591 dbg(1,"order=%d\n", order);
595 for (i = 0 ; i < data_window_type_end; i++) {
596 data_window_begin(co->data_window[i]);
600 do_draw(displaylist, trans, mapsets, order, route);
601 profile(1,"do_draw");
602 graphics_displaylist_draw(gra, displaylist, trans, layouts, route);
603 profile(1,"xdisplay_draw");
607 for (i = 0 ; i < data_window_type_end; i++) {
608 data_window_end(co->data_window[i]);
615 struct displaylist_handle {
616 GList *hl_head,*hl,*l;
620 struct displaylist_handle *
621 graphics_displaylist_open(struct displaylist *displaylist)
623 struct displaylist_handle *ret;
625 ret=g_new0(struct displaylist_handle, 1);
626 ret->hl_head=ret->hl=g_hash_to_list(displaylist->dl);
632 graphics_displaylist_next(struct displaylist_handle *dlh)
634 struct displayitem *ret;
638 dlh->l=dlh->hl->data;
639 dlh->hl=g_list_next(dlh->hl);
642 dlh->l=g_list_next(dlh->l);
647 graphics_displaylist_close(struct displaylist_handle *dlh)
649 g_list_free(dlh->hl_head);
654 graphics_displaylist_new(void)
656 struct displaylist *ret=g_new(struct displaylist, 1);
658 ret->dl=g_hash_table_new(NULL,NULL);
664 graphics_displayitem_get_item(struct displayitem *di)
670 graphics_displayitem_get_label(struct displayitem *di)
676 within_dist_point(struct point *p0, struct point *p1, int dist)
678 if (p0->x == 32767 || p0->y == 32767 || p1->x == 32767 || p1->y == 32767)
680 if (p0->x == -32768 || p0->y == -32768 || p1->x == -32768 || p1->y == -32768)
682 if ((p0->x-p1->x)*(p0->x-p1->x) + (p0->y-p1->y)*(p0->y-p1->y) <= dist*dist) {
689 within_dist_line(struct point *p, struct point *line_p0, struct point *line_p1, int dist)
695 vx=line_p1->x-line_p0->x;
696 vy=line_p1->y-line_p0->y;
702 return within_dist_point(p, line_p0, dist);
705 return within_dist_point(p, line_p1, dist);
707 line_p.x=line_p0->x+vx*c1/c2;
708 line_p.y=line_p0->y+vy*c1/c2;
709 return within_dist_point(p, &line_p, dist);
713 within_dist_polyline(struct point *p, struct point *line_pnt, int count, int dist, int close)
716 for (i = 0 ; i < count-1 ; i++) {
717 if (within_dist_line(p,line_pnt+i,line_pnt+i+1,dist)) {
722 return (within_dist_line(p,line_pnt,line_pnt+count-1,dist));
727 within_dist_polygon(struct point *p, struct point *poly_pnt, int count, int dist)
730 for (i = 0, j = count-1; i < count; j = i++) {
731 if ((((poly_pnt[i].y <= p->y) && ( p->y < poly_pnt[j].y )) ||
732 ((poly_pnt[j].y <= p->y) && ( p->y < poly_pnt[i].y))) &&
733 (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))
737 return within_dist_polyline(p, poly_pnt, count, dist, 1);
742 graphics_displayitem_within_dist(struct displayitem *di, struct point *p, int dist)
744 if (di->item.type < type_line) {
745 return within_dist_point(p, &di->pnt[0], dist);
747 if (di->item.type < type_area) {
748 return within_dist_polyline(p, di->pnt, di->count, dist, 0);
750 return within_dist_polygon(p, di->pnt, di->count, dist);