debian/changelog update
[navit-package] / navit / graphics.c
index 4703fa3..111456d 100644 (file)
@@ -30,6 +30,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <math.h>
+#include "config.h"
 #include "debug.h"
 #include "string.h"
 #include "draw_info.h"
@@ -50,7 +51,6 @@
 #include "file.h"
 #include "event.h"
 
-static char *navit_sharedir;
 
 //##############################################################################################################
 //# Description: 
@@ -78,7 +78,10 @@ struct display_context
        struct graphics_gc *gc;
        struct graphics_image *img;
        enum projection pro;
+       int mindist;
        struct transformation *trans;
+       enum item_type type;
+       int maxlen;
 };
 
 struct displaylist {
@@ -103,6 +106,16 @@ struct displaylist {
 static void draw_circle(struct point *pnt, int diameter, int scale, int start, int len, struct point *res, int *pos, int dir);
 static void graphics_process_selection(struct graphics *gra, struct displaylist *dl);
 
+int
+graphics_set_attr(struct graphics *gra, struct attr *attr)
+{
+       int ret=1;
+       dbg(0,"enter\n");
+       if (gra->meth.set_attr)
+               ret=gra->meth.set_attr(gra->priv, attr);
+        return ret != 0;
+}
+
 void
 graphics_set_rect(struct graphics *gra, struct point_rect *pr)
 {
@@ -196,6 +209,8 @@ graphics_overlay_resize(struct graphics *this_, struct point *p, int w, int h, i
 */
 void graphics_init(struct graphics *this_)
 {
+       if (this_->gc[0])
+               return;
        this_->gc[0]=graphics_gc_new(this_);
        graphics_gc_set_background(this_->gc[0], &(struct color) { 0xffff, 0xefef, 0xb7b7, 0xffff});
        graphics_gc_set_foreground(this_->gc[0], &(struct color) { 0xffff, 0xefef, 0xb7b7, 0xffff });
@@ -206,7 +221,6 @@ void graphics_init(struct graphics *this_)
        graphics_gc_set_background(this_->gc[2], &(struct color) { 0xffff, 0xffff, 0xffff, 0xffff });
        graphics_gc_set_foreground(this_->gc[2], &(struct color) { 0x0000, 0x0000, 0x0000, 0xffff });
        graphics_background_gc(this_, this_->gc[0]);
-       navit_sharedir = getenv("NAVIT_SHAREDIR");
 }
 
 /**
@@ -660,7 +674,7 @@ static guint
 displayitem_hash(gconstpointer key)
 {
        const struct displayitem *di=key;
-       return (di->item.id_hi^di->item.id_lo^((int) di->item.map));
+       return (di->item.id_hi^di->item.id_lo^(GPOINTER_TO_INT(di->item.map)));
 }
 
 static gboolean
@@ -736,14 +750,14 @@ static void label_line(struct graphics *gra, struct graphics_gc *fg, struct grap
                tl=strlen(label)*4;
                th=8;
        }
-       tlm=tl*128;
-       thm=th*144;
+       tlm=tl*32;
+       thm=th*36;
        tlsq = tlm*tlm;
        for (i = 0 ; i < count-1 ; i++) {
                dx=p[i+1].x-p[i].x;
-               dx*=128;
+               dx*=32;
                dy=p[i+1].y-p[i].y;
-               dy*=128;
+               dy*=32;
                lsq = dx*dx+dy*dy;
                if (lsq > tlsq) {
                        l=(int)sqrtf(lsq);
@@ -755,10 +769,10 @@ static void label_line(struct graphics *gra, struct graphics_gc *fg, struct grap
                                x=p[i+1].x;
                                y=p[i+1].y;
                        }
-                       x+=(l-tlm)*dx/l/256;
-                       y+=(l-tlm)*dy/l/256;
-                       x-=dy*thm/l/256;
-                       y+=dx*thm/l/256;
+                       x+=(l-tlm)*dx/l/64;
+                       y+=(l-tlm)*dy/l/64;
+                       x-=dy*thm/l/64;
+                       y+=dx*thm/l/64;
                        p_t.x=x;
                        p_t.y=y;
 #if 0
@@ -1000,16 +1014,25 @@ static int
 int_sqrt(unsigned int n)
 {
        unsigned int h, p= 0, q= 1, r= n;
-       while ( q <= n )
-               q <<= 2;
-       while ( q != 1 ) {
+
+       /* avoid q rollover */
+       if(n >= (1<<(sizeof(n)*8-2))) {
+               q = 1<<(sizeof(n)*8-2);
+       } else {
+               while ( q <= n ) {
+                       q <<= 2;
+               }
                q >>= 2;
+       }
+
+       while ( q != 0 ) {
                h = p + q;
                p >>= 1;
                if ( r >= h ) {
                        p += q;
                        r -= h;
-               }
+               }
+               q >>= 2;
        }
        return p;
 }
@@ -1058,13 +1081,27 @@ graphics_draw_polyline_as_polygon(struct graphics *gra, struct graphics_gc *gc,
                wi=*width;
                width+=step;
                if (i < count - 1) {
+                       int dxs,dys,lscales;
+
                        dx = (pnt[i + 1].x - pnt[i].x);
                        dy = (pnt[i + 1].y - pnt[i].y);
+#if 0
                        l = int_sqrt(dx * dx * lscale * lscale + dy * dy * lscale * lscale);
+#else
+                       dxs=dx*dx;
+                       dys=dy*dy;
+                       lscales=lscale*lscale;
+                       if (dxs + dys > lscales)
+                               l = int_sqrt(dxs+dys)*lscale;
+                       else
+                               l = int_sqrt((dxs+dys)*lscales);
+#endif
                        fow=fowler(-dy, dx);
                }
                if (! l) 
                        l=1;
+               if (wi*lscale > 10000)
+                       lscale=10000/wi;
                dbg_assert(wi*lscale < 10000);
                calc_offsets(wi*lscale, l, dx, dy, &o);
                pos.x = pnt[i].x + o.ny;
@@ -1395,19 +1432,48 @@ get_font(struct graphics *gra, int size)
        return gra->font[size];
 }
 
+char *
+graphics_icon_path(char *icon)
+{
+       static char *navit_sharedir;
+       dbg(1,"enter %s\n",icon);
+       if (icon[0] == '/')
+               return g_strdup(icon);
+       else {
+#ifdef HAVE_API_ANDROID
+               return g_strdup_printf("res/drawable/%s", icon);
+#else
+               if (! navit_sharedir)
+                       navit_sharedir = getenv("NAVIT_SHAREDIR");
+               return g_strdup_printf("%s/xpm/%s", navit_sharedir, icon);
+#endif
+       }
+}
+
+static int
+limit_count(struct coord *c, int count)
+{
+       int i;
+       for (i = 1 ; i < count ; i++) {
+               if (c[i].x == c[0].x && c[i].y == c[0].y)
+                       return i+1;
+       }
+       return count;
+}
+
 
 static void
 displayitem_draw(struct displayitem *di, void *dummy, struct display_context *dc)
 {
-       int width[16384];
-       int i,count;
-       struct point pa[16384];
+       int width[dc->maxlen];
+       int i,count=di->count,mindist=dc->mindist;
+       struct point pa[dc->maxlen];
        struct graphics *gra=dc->gra;
        struct graphics_gc *gc=dc->gc;
        struct element *e=dc->e;
        struct graphics_image *img=dc->img;
        struct point p;
-       char path[PATH_MAX];
+       char *path;
 
        di->displayed=1;
        if (! gc) {
@@ -1415,11 +1481,14 @@ displayitem_draw(struct displayitem *di, void *dummy, struct display_context *dc
                gc->meth.gc_set_foreground(gc->priv, &e->color);
                dc->gc=gc;
        }
-       if (dc->e->type == element_polyline) {
-               count=transform(dc->trans, dc->pro, di->c, pa, di->count, 1, e->u.polyline.width, width);
-       }
+       if (item_type_is_area(dc->type) && (dc->e->type == element_polyline || dc->e->type == element_text))
+               count=limit_count(di->c, count);
+       if (dc->type == type_poly_water_tiled)
+               mindist=0;
+       if (dc->e->type == element_polyline) 
+               count=transform(dc->trans, dc->pro, di->c, pa, count, mindist, e->u.polyline.width, width);
        else
-               count=transform(dc->trans, dc->pro, di->c, pa, di->count, 1, 0, NULL);
+               count=transform(dc->trans, dc->pro, di->c, pa, count, mindist, 0, NULL);
        switch (e->type) {
        case element_polygon:
 #if 0
@@ -1498,11 +1567,9 @@ displayitem_draw(struct displayitem *di, void *dummy, struct display_context *dc
        case element_icon:
                if (count) {
                        if (!img) {
-                               if (e->u.icon.src[0] == '/')
-                                       strcpy(path,e->u.icon.src);
-                               else
-                                       sprintf(path,"%s/xpm/%s", navit_sharedir, e->u.icon.src);
+                               path=graphics_icon_path(e->u.icon.src); 
                                img=graphics_image_new_scaled_rotated(gra, path, e->u.icon.width, e->u.icon.height, e->u.icon.rotation);
+                               g_free(path);
                                if (img)
                                        dc->img=img;
                                else
@@ -1541,19 +1608,19 @@ static void xdisplay_draw_elements(struct graphics *gra, struct displaylist *dis
        struct element *e;
        GList *es,*types;
        GHashTable *h;
-       enum item_type type;
+       struct display_context *dc=&display_list->dc;
 
        es=itm->elements;
        while (es) {
                e=es->data;
-               display_list->dc.e=e;
+               dc->e=e;
                types=itm->type;
                while (types) {
-                       type=GPOINTER_TO_INT(types->data);
-                       h=g_hash_table_lookup(display_list->dl, GINT_TO_POINTER(type));
+                       dc->type=GPOINTER_TO_INT(types->data);
+                       h=g_hash_table_lookup(display_list->dl, GINT_TO_POINTER(dc->type));
                        if (h) {
-                               g_hash_table_foreach(h, (GHFunc)displayitem_draw, &display_list->dc);
-                               display_context_free(&display_list->dc);
+                               g_hash_table_foreach(h, (GHFunc)displayitem_draw, dc);
+                               display_context_free(dc);
                        }
                        types=g_list_next(types);
                }
@@ -1567,10 +1634,12 @@ graphics_draw_itemgra(struct graphics *gra, struct itemgra *itm, struct transfor
        GList *es;
        struct point p;
        struct coord c;
+#if 0
        char *label=NULL;
+#endif
        struct graphics_gc *gc = NULL;
        struct graphics_image *img;
-       char path[PATH_MAX];
+       char *path;
        es=itm->elements;
        c.x=0;
        c.y=0;
@@ -1618,11 +1687,9 @@ graphics_draw_itemgra(struct graphics *gra, struct itemgra *itm, struct transfor
                        # endif
                        break;
                case element_icon:
-                       if (e->u.icon.src[0] == '/') 
-                               strcpy(path,e->u.icon.src);
-                       else
-                               sprintf(path,"%s/xpm/%s", navit_sharedir, e->u.icon.src);
+                       path=graphics_icon_path(e->u.icon.src); 
                        img=graphics_image_new_scaled_rotated(gra, path, e->u.icon.width, e->u.icon.height, e->u.icon.rotation);
+                       g_free(path);
                        if (! img)
                                dbg(0,"failed to load icon '%s'\n", e->u.icon.src);
                        else {
@@ -1688,13 +1755,14 @@ static void xdisplay_draw(struct displaylist *display_list, struct graphics *gra
 extern void *route_selection;
 
 static void
-do_draw(struct displaylist *displaylist, int cancel)
+do_draw(struct displaylist *displaylist, int cancel, int flags)
 {
        struct item *item;
-       int count,max=16384,workload=0;
+       int count,max=displaylist->dc.maxlen,workload=0;
        struct coord ca[max];
        struct attr attr;
 
+       profile(0,NULL);
        while (!cancel) {
                if (!displaylist->msh) 
                        displaylist->msh=mapset_open(displaylist->ms);
@@ -1715,8 +1783,10 @@ do_draw(struct displaylist *displaylist, int cancel)
                                count=item_coord_get_within_selection(item, ca, item->type < type_line ? 1: max, displaylist->sel);
                                if (! count)
                                        continue;
-                               if (count == max) 
-                                       dbg(0,"point count overflow %d\n", count);
+                               if (count == max) {
+                                       dbg(0,"point count overflow %d for %s "ITEM_ID_FMT"\n", count,item_to_name(item->type),ITEM_ID_ARGS(*item));
+                                       displaylist->dc.maxlen=max*2;
+                               }
                                if (!item_attr_get(item, attr_label, &attr))
                                        attr.u.str=NULL;
                                if (displaylist->conv && attr.u.str && attr.u.str[0]) {
@@ -1736,12 +1806,16 @@ do_draw(struct displaylist *displaylist, int cancel)
                displaylist->sel=NULL;
                displaylist->m=NULL;
        }
+       profile(1,"process_selection\n");
        event_remove_idle(displaylist->idle_ev);
        displaylist->idle_ev=NULL;
+       callback_destroy(displaylist->idle_cb);
+       displaylist->idle_cb=NULL;
        displaylist->busy=0;
        graphics_process_selection(displaylist->dc.gra, displaylist);
+       profile(1,"draw\n");
        if (! cancel) 
-               graphics_displaylist_draw(displaylist->dc.gra, displaylist, displaylist->dc.trans, displaylist->layout, 1);
+               graphics_displaylist_draw(displaylist->dc.gra, displaylist, displaylist->dc.trans, displaylist->layout, flags);
        map_rect_destroy(displaylist->mr);
        map_selection_destroy(displaylist->sel);
        mapset_close(displaylist->msh);
@@ -1749,7 +1823,9 @@ do_draw(struct displaylist *displaylist, int cancel)
        displaylist->sel=NULL;
        displaylist->m=NULL;
        displaylist->msh=NULL;
+       profile(1,"callback\n");
        callback_call_1(displaylist->cb, cancel);
+       profile(0,"end\n");
 }
 
 /**
@@ -1758,11 +1834,12 @@ do_draw(struct displaylist *displaylist, int cancel)
  * @returns <>
  * @author Martin Schaller (04/2008)
 */
-void graphics_displaylist_draw(struct graphics *gra, struct displaylist *displaylist, struct transformation *trans, struct layout *l, int callback)
+void graphics_displaylist_draw(struct graphics *gra, struct displaylist *displaylist, struct transformation *trans, struct layout *l, int flags)
 {
        int order=transform_get_order(trans);
        displaylist->dc.trans=trans;
        displaylist->dc.gra=gra;
+       displaylist->dc.mindist=transform_get_scale(trans)/2;
        // FIXME find a better place to set the background color
        if (l) {
                graphics_gc_set_background(gra->gc[0], &l->color);
@@ -1770,16 +1847,18 @@ void graphics_displaylist_draw(struct graphics *gra, struct displaylist *display
                gra->default_font = g_strdup(l->font);
        }
        graphics_background_gc(gra, gra->gc[0]);
-       gra->meth.draw_mode(gra->priv, draw_mode_begin);
-       gra->meth.draw_rectangle(gra->priv, gra->gc[0]->priv, &gra->r.lu, gra->r.rl.x-gra->r.lu.x, gra->r.rl.y-gra->r.lu.y);
+       gra->meth.draw_mode(gra->priv, (flags & 8)?draw_mode_begin_clear:draw_mode_begin);
+       if (!(flags & 2))
+               gra->meth.draw_rectangle(gra->priv, gra->gc[0]->priv, &gra->r.lu, gra->r.rl.x-gra->r.lu.x, gra->r.rl.y-gra->r.lu.y);
        if (l) 
                xdisplay_draw(displaylist, gra, l, order+l->order_delta);
-       if (callback)
+       if (flags & 1)
                callback_list_call_attr_0(gra->cbl, attr_postdraw);
-       gra->meth.draw_mode(gra->priv, draw_mode_end);
+       if (!(flags & 4))
+               gra->meth.draw_mode(gra->priv, draw_mode_end);
 }
 
-static void graphics_load_mapset(struct graphics *gra, struct displaylist *displaylist, struct mapset *mapset, struct transformation *trans, struct layout *l, int async, struct callback *cb)
+static void graphics_load_mapset(struct graphics *gra, struct displaylist *displaylist, struct mapset *mapset, struct transformation *trans, struct layout *l, int async, struct callback *cb, int flags)
 {
        int order=transform_get_order(trans);
 
@@ -1787,7 +1866,7 @@ static void graphics_load_mapset(struct graphics *gra, struct displaylist *displ
        if (displaylist->busy) {
                if (async == 1)
                        return;
-               do_draw(displaylist, 1);
+               do_draw(displaylist, 1, flags);
        }
        xdisplay_free(displaylist->dl);
        dbg(1,"order=%d\n", order);
@@ -1804,9 +1883,10 @@ static void graphics_load_mapset(struct graphics *gra, struct displaylist *displ
        displaylist->layout=l;
        if (async) {
                if (! displaylist->idle_cb)
-                       displaylist->idle_cb=callback_new_2(callback_cast(do_draw), displaylist, 0);
+                       displaylist->idle_cb=callback_new_3(callback_cast(do_draw), displaylist, 0, flags);
                displaylist->idle_ev=event_add_idle(50, displaylist->idle_cb);
-       }
+       } else
+               do_draw(displaylist, 0, flags);
 }
 /**
  * FIXME
@@ -1814,11 +1894,9 @@ static void graphics_load_mapset(struct graphics *gra, struct displaylist *displ
  * @returns <>
  * @author Martin Schaller (04/2008)
 */
-void graphics_draw(struct graphics *gra, struct displaylist *displaylist, struct mapset *mapset, struct transformation *trans, struct layout *l, int async, struct callback *cb)
+void graphics_draw(struct graphics *gra, struct displaylist *displaylist, struct mapset *mapset, struct transformation *trans, struct layout *l, int async, struct callback *cb, int flags)
 {
-       graphics_load_mapset(gra, displaylist, mapset, trans, l, async, cb);
-       if (! async)
-               do_draw(displaylist, 0);
+       graphics_load_mapset(gra, displaylist, mapset, trans, l, async, cb, flags);
 }
 
 int
@@ -1826,7 +1904,7 @@ graphics_draw_cancel(struct graphics *gra, struct displaylist *displaylist)
 {
        if (!displaylist->busy)
                return 0;
-       do_draw(displaylist, 1);
+       do_draw(displaylist, 1, 0);
        return 1;
 }
 
@@ -1851,7 +1929,13 @@ struct displaylist_handle * graphics_displaylist_open(struct displaylist *displa
        struct displaylist_handle *ret;
 
        ret=g_new0(struct displaylist_handle, 1);
+       if (!displaylist->dl)
+               return NULL;
        ret->hl_head=ret->hl=g_hash_to_list(displaylist->dl);
+       if (!ret->hl) {
+               g_free(ret);
+               return NULL;
+       }
        ret->l_head=ret->l=g_hash_to_list_keys(ret->hl->data);
 
        return ret;
@@ -1866,6 +1950,8 @@ struct displaylist_handle * graphics_displaylist_open(struct displaylist *displa
 struct displayitem * graphics_displaylist_next(struct displaylist_handle *dlh)
 {
        struct displayitem *ret;
+       if (!dlh)
+               return NULL;
        if (! dlh->l) {
                dlh->hl=g_list_next(dlh->hl);
                if (!dlh->hl)
@@ -1886,9 +1972,11 @@ struct displayitem * graphics_displaylist_next(struct displaylist_handle *dlh)
 */
 void graphics_displaylist_close(struct displaylist_handle *dlh)
 {
-       g_list_free(dlh->hl_head);
-       g_list_free(dlh->l_head);
-       g_free(dlh);
+       if (dlh) {
+               g_list_free(dlh->hl_head);
+               g_list_free(dlh->l_head);
+               g_free(dlh);
+       }
 }
 
 /**
@@ -1902,6 +1990,7 @@ struct displaylist * graphics_displaylist_new(void)
        struct displaylist *ret=g_new0(struct displaylist, 1);
 
        ret->dl=g_hash_table_new(NULL,NULL);
+       ret->dc.maxlen=16384;
 
        return ret;
 }
@@ -1917,6 +2006,12 @@ struct item * graphics_displayitem_get_item(struct displayitem *di)
        return &di->item;       
 }
 
+int
+graphics_displayitem_get_coord_count(struct displayitem *di)
+{
+       return di->count;
+}
+
 /**
  * FIXME
  * @param <>
@@ -1928,6 +2023,12 @@ char * graphics_displayitem_get_label(struct displayitem *di)
        return di->label;
 }
 
+int
+graphics_displayitem_get_displayed(struct displayitem *di)
+{
+       return di->displayed;
+}
+
 /**
  * FIXME
  * @param <>
@@ -2045,7 +2146,7 @@ static int within_dist_polygon(struct point *p, struct point *poly_pnt, int coun
 */
 int graphics_displayitem_within_dist(struct displaylist *displaylist, struct displayitem *di, struct point *p, int dist)
 {
-       struct point pa[16384];
+       struct point pa[displaylist->dc.maxlen];
        int count;
 
        count=transform(displaylist->dc.trans, displaylist->dc.pro, di->c, pa, di->count, 1, 0, NULL);
@@ -2060,23 +2161,15 @@ int graphics_displayitem_within_dist(struct displaylist *displaylist, struct dis
 }
 
 
-static enum item_type
-graphics_selection_type(enum item_type type)
-{
-       if (type < type_line) 
-               return type_selected_point;
-       if (type < type_area) 
-               return type_selected_line;
-       return type_selected_area;
-       
-}
-
-
 static void
 graphics_process_selection_item(struct displaylist *dl, struct item *item)
 {
        struct displayitem di,*di_res;
        GHashTable *h;
+       int count,max=dl->dc.maxlen;
+       struct coord ca[max];
+       struct attr attr;
+       struct map_rect *mr;
 
        di.item=*item;
        di.label=NULL;
@@ -2086,24 +2179,38 @@ graphics_process_selection_item(struct displaylist *dl, struct item *item)
        if (h) {
                di_res=g_hash_table_lookup(h, &di);
                if (di_res) {
-                       di.item.type=graphics_selection_type(di.item.type);
+                       di.item.type=(enum item_type)item->priv_data;
                        display_add(dl, &di.item, di_res->count, di_res->c, NULL);
+                       return;
                }
        }
+       mr=map_rect_new(item->map, NULL);
+       item=map_rect_get_item_byid(mr, item->id_hi, item->id_lo);
+       count=item_coord_get(item, ca, item->type < type_line ? 1: max);
+       if (!item_attr_get(item, attr_label, &attr))
+               attr.u.str=NULL;
+       if (dl->conv && attr.u.str && attr.u.str[0]) {
+               char *str=map_convert_string(item->map, attr.u.str);
+               display_add(dl, item, count, ca, str);
+               map_convert_free(str);
+       } else
+               display_add(dl, item, count, ca, attr.u.str);
+       map_rect_destroy(mr);
 }
 
 void
-graphics_add_selection(struct graphics *gra, struct item *item, struct displaylist *dl)
+graphics_add_selection(struct graphics *gra, struct item *item, enum item_type type, struct displaylist *dl)
 {
        struct item *item_dup=g_new(struct item, 1);
        *item_dup=*item;
+       item_dup->priv_data=(void *)type;
        gra->selection=g_list_append(gra->selection, item_dup);
        if (dl)
                graphics_process_selection_item(dl, item_dup);
 }
 
 void
-graphics_remove_selection(struct graphics *gra, struct item *item, struct displaylist *dl)
+graphics_remove_selection(struct graphics *gra, struct item *item, enum item_type type, struct displaylist *dl)
 {
        GList *curr;
        int found;
@@ -2121,7 +2228,7 @@ graphics_remove_selection(struct graphics *gra, struct item *item, struct displa
                                        di.label=NULL;
                                        di.displayed=0;
                                        di.count=0;
-                                       di.item.type=graphics_selection_type(di.item.type);
+                                       di.item.type=type;
                                        h=g_hash_table_lookup(dl->dl, GINT_TO_POINTER(di.item.type));
                                        if (h)
                                                g_hash_table_remove(h, &di);
@@ -2140,8 +2247,10 @@ graphics_remove_selection(struct graphics *gra, struct item *item, struct displa
 void
 graphics_clear_selection(struct graphics *gra, struct displaylist *dl)
 {
-       while (gra->selection) 
-               graphics_remove_selection(gra, (struct item *)gra->selection->data, dl);
+       while (gra->selection) {
+               struct item *item=(struct item *)gra->selection->data;
+               graphics_remove_selection(gra, item, (enum item_type)item->priv_data,dl);
+       }
 }
 
 static void