Diff of /trunk/src/osm-gps-map-osd-classic.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 94 by harbaum, Thu Sep 3 11:04:05 2009 UTC revision 103 by harbaum, Wed Sep 9 11:50:50 2009 UTC
# Line 25  Line 25 
25  /* OSD_DIAMETER */  /* OSD_DIAMETER */
26  /* OSD_X, OSD_Y */  /* OSD_X, OSD_Y */
27    
28    #ifndef OSD_SCALE_FONT_SIZE
29    #define OSD_SCALE_FONT_SIZE 12
30    #endif
31    #define OSD_SCALE_W   (10*OSD_SCALE_FONT_SIZE)
32    #define OSD_SCALE_H   (5*OSD_SCALE_FONT_SIZE/2)
33    
34  #ifndef USE_CAIRO  #ifndef USE_CAIRO
35  #error "OSD control display lacks a non-cairo implementation!"  #error "OSD control display lacks a non-cairo implementation!"
36  #endif  #endif
# Line 39  typedef struct { Line 45  typedef struct {
45      /* the offscreen representation of the OSD */      /* the offscreen representation of the OSD */
46      cairo_surface_t *overlay;      cairo_surface_t *overlay;
47    
48    #ifdef OSD_SCALE
49        cairo_surface_t *scale;
50        int scale_zoom;
51    #endif
52    
53  #ifdef OSD_SOURCE_SEL  #ifdef OSD_SOURCE_SEL
54      /* values to handle the "source" menu */      /* values to handle the "source" menu */
55      cairo_surface_t *map_source;      cairo_surface_t *map_source;
# Line 442  osd_source_reallocate(osm_gps_map_osd_t Line 453  osd_source_reallocate(osm_gps_map_osd_t
453    
454      int w = OSD_S_W, h = OSD_S_H;      int w = OSD_S_W, h = OSD_S_H;
455      if(priv->expanded) {      if(priv->expanded) {
         /* ... and right of it the waypoint id */  
456          cairo_text_extents_t extents;          cairo_text_extents_t extents;
457    
458          /* determine content size */          /* determine content size */
# Line 476  osd_source_reallocate(osm_gps_map_osd_t Line 486  osd_source_reallocate(osm_gps_map_osd_t
486          cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w+2, h+2);          cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w+2, h+2);
487    
488      osd_render_source_sel(osd);      osd_render_source_sel(osd);
   
489  }  }
490    
491  #define OSD_HZ      15  #define OSD_HZ      15
# Line 720  osd_zoom_labels(cairo_t *cr, gint x, gin Line 729  osd_zoom_labels(cairo_t *cr, gint x, gin
729      cairo_line_to (cr, x + Z_RIGHT + Z_LEN, y + Z_MID);      cairo_line_to (cr, x + Z_RIGHT + Z_LEN, y + Z_MID);
730  }  }
731    
732    /* various parameters used to create the scale */
733    #define OSD_SCALE_H2   (OSD_SCALE_H/2)
734    #define OSD_SCALE_TICK (2*OSD_SCALE_FONT_SIZE/3)
735    #define OSD_SCALE_M    (OSD_SCALE_H2 - OSD_SCALE_TICK)
736    #define OSD_SCALE_I    (OSD_SCALE_H2 + OSD_SCALE_TICK)
737    #define OSD_SCALE_FD   (OSD_SCALE_FONT_SIZE/4)
738    
739    static void
740    osd_render_scale(osm_gps_map_osd_t *osd)
741    {
742        osd_priv_t *priv = (osd_priv_t*)osd->priv;
743    
744        /* this only needs to be rendered if the zoom has changed */
745        gint zoom;
746        g_object_get(OSM_GPS_MAP(osd->widget), "zoom", &zoom, NULL);
747        if(zoom == priv->scale_zoom)
748            return;
749    
750        priv->scale_zoom = zoom;
751    
752        float m_per_pix = osm_gps_map_get_scale(OSM_GPS_MAP(osd->widget));
753    
754        /* first fill with transparency */
755        cairo_t *cr = cairo_create(priv->scale);
756        cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
757        cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.0);
758        // pink for testing:    cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.2);
759        cairo_paint(cr);
760        cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
761    
762        /* determine the size of the scale width in meters */
763        float width = (OSD_SCALE_W-OSD_SCALE_FONT_SIZE/6) * m_per_pix;
764    
765        /* scale this to useful values */
766        int exp = logf(width)*M_LOG10E;
767        int mant = width/pow(10,exp);
768        int width_metric = mant * pow(10,exp);
769        char *dist_str = NULL;
770        if(width_metric<1000)
771            dist_str = g_strdup_printf("%u m", width_metric);
772        else
773            dist_str = g_strdup_printf("%u km", width_metric/1000);
774        width_metric /= m_per_pix;
775    
776        /* and now the hard part: scale for useful imperial values :-( */
777        /* try to convert to feet, 1ft == 0.3048 m */
778        width /= 0.3048;
779        float imp_scale = 0.3048;
780        char *dist_imp_unit = "ft";
781    
782        if(width >= 100) {
783            /* 1yd == 3 feet */
784            width /= 3.0;
785            imp_scale *= 3.0;
786            dist_imp_unit = "yd";
787    
788            if(width >= 1760.0) {
789                /* 1mi == 1760 yd */
790                width /= 1760.0;
791                imp_scale *= 1760.0;
792                dist_imp_unit = "mi";
793            }
794        }
795    
796        /* also convert this to full tens/hundreds */
797        exp = logf(width)*M_LOG10E;
798        mant = width/pow(10,exp);
799        int width_imp = mant * pow(10,exp);
800        char *dist_str_imp = g_strdup_printf("%u %s", width_imp, dist_imp_unit);
801    
802        /* convert back to pixels */
803        width_imp *= imp_scale;
804        width_imp /= m_per_pix;
805    
806        cairo_select_font_face (cr, "Sans",
807                                CAIRO_FONT_SLANT_NORMAL,
808                                CAIRO_FONT_WEIGHT_BOLD);
809        cairo_set_font_size (cr, OSD_SCALE_FONT_SIZE);
810        cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
811    
812        cairo_text_extents_t extents;
813        cairo_text_extents (cr, dist_str, &extents);
814    
815        cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
816        cairo_set_line_width (cr, OSD_SCALE_FONT_SIZE/6);
817        cairo_move_to (cr, 2*OSD_SCALE_FD, OSD_SCALE_H2-OSD_SCALE_FD);
818        cairo_text_path (cr, dist_str);
819        cairo_stroke (cr);
820        cairo_move_to (cr, 2*OSD_SCALE_FD,
821                       OSD_SCALE_H2+OSD_SCALE_FD + extents.height);
822        cairo_text_path (cr, dist_str_imp);
823        cairo_stroke (cr);
824    
825        cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
826        cairo_move_to (cr, 2*OSD_SCALE_FD, OSD_SCALE_H2-OSD_SCALE_FD);
827        cairo_show_text (cr, dist_str);
828        cairo_move_to (cr, 2*OSD_SCALE_FD,
829                       OSD_SCALE_H2+OSD_SCALE_FD + extents.height);
830        cairo_show_text (cr, dist_str_imp);
831    
832        g_free(dist_str);
833        g_free(dist_str_imp);
834    
835        /* draw white line */
836        cairo_set_line_cap  (cr, CAIRO_LINE_CAP_ROUND);
837        cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
838        cairo_set_line_width (cr, OSD_SCALE_FONT_SIZE/3);
839        cairo_move_to (cr, OSD_SCALE_FONT_SIZE/6, OSD_SCALE_M);
840        cairo_rel_line_to (cr, 0,  OSD_SCALE_TICK);
841        cairo_rel_line_to (cr, width_metric, 0);
842        cairo_rel_line_to (cr, 0, -OSD_SCALE_TICK);
843        cairo_stroke(cr);
844        cairo_move_to (cr, OSD_SCALE_FONT_SIZE/6, OSD_SCALE_I);
845        cairo_rel_line_to (cr, 0, -OSD_SCALE_TICK);
846        cairo_rel_line_to (cr, width_imp, 0);
847        cairo_rel_line_to (cr, 0, +OSD_SCALE_TICK);
848        cairo_stroke(cr);
849    
850        /* draw black line */
851        cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
852        cairo_set_line_width (cr, OSD_SCALE_FONT_SIZE/6);
853        cairo_move_to (cr, OSD_SCALE_FONT_SIZE/6, OSD_SCALE_M);
854        cairo_rel_line_to (cr, 0,  OSD_SCALE_TICK);
855        cairo_rel_line_to (cr, width_metric, 0);
856        cairo_rel_line_to (cr, 0, -OSD_SCALE_TICK);
857        cairo_stroke(cr);
858        cairo_move_to (cr, OSD_SCALE_FONT_SIZE/6, OSD_SCALE_I);
859        cairo_rel_line_to (cr, 0, -OSD_SCALE_TICK);
860        cairo_rel_line_to (cr, width_imp, 0);
861        cairo_rel_line_to (cr, 0, +OSD_SCALE_TICK);
862        cairo_stroke(cr);
863    
864        cairo_destroy(cr);
865    }
866    
867  static void  static void
868  osd_render(osm_gps_map_osd_t *osd) {  osd_render(osm_gps_map_osd_t *osd)
869    {
870      osd_priv_t *priv = (osd_priv_t*)osd->priv;      osd_priv_t *priv = (osd_priv_t*)osd->priv;
871    
872  #ifndef OSD_COLOR  #ifndef OSD_COLOR
# Line 806  osd_render(osm_gps_map_osd_t *osd) { Line 951  osd_render(osm_gps_map_osd_t *osd) {
951  #ifdef OSD_SOURCE_SEL  #ifdef OSD_SOURCE_SEL
952      osd_render_source_sel(osd);      osd_render_source_sel(osd);
953  #endif  #endif
954    
955    #ifdef OSD_SCALE
956        osd_render_scale(osd);
957    #endif
958  }  }
959    
960  static void  static void
# Line 827  osd_draw(osm_gps_map_osd_t *osd, GdkDraw Line 976  osd_draw(osm_gps_map_osd_t *osd, GdkDraw
976                                             OSD_S_W+2, OSD_S_H+2);                                             OSD_S_W+2, OSD_S_H+2);
977  #endif  #endif
978    
979    #ifdef OSD_SCALE
980            priv->scale =
981                cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
982                                           OSD_SCALE_W, OSD_SCALE_H);
983            priv->scale_zoom = -1;
984    #endif
985    
986          /* ... and render it */          /* ... and render it */
987          osd_render(osd);          osd_render(osd);
988      }      }
# Line 866  osd_draw(osm_gps_map_osd_t *osd, GdkDraw Line 1022  osd_draw(osm_gps_map_osd_t *osd, GdkDraw
1022      cairo_paint(cr);      cairo_paint(cr);
1023  #endif  #endif
1024    
1025    #ifdef OSD_SCALE
1026        x =  OSD_X;
1027        y = -OSD_Y;
1028        if(x < 0) x += osd->widget->allocation.width - OSD_SCALE_W;
1029        if(y < 0) y += osd->widget->allocation.height - OSD_SCALE_H;
1030    
1031        cairo_set_source_surface(cr, priv->scale, x, y);
1032        cairo_paint(cr);
1033    #endif
1034    
1035      cairo_destroy(cr);      cairo_destroy(cr);
1036  }  }
1037    
# Line 885  osd_free(osm_gps_map_osd_t *osd) Line 1051  osd_free(osm_gps_map_osd_t *osd)
1051           cairo_surface_destroy(priv->map_source);           cairo_surface_destroy(priv->map_source);
1052  #endif  #endif
1053    
1054    #ifdef OSD_SCALE
1055        if (priv->scale)
1056             cairo_surface_destroy(priv->scale);
1057    #endif
1058    
1059      g_free(priv);      g_free(priv);
1060  }  }
1061    

Legend:
Removed from v.94  
changed lines
  Added in v.103