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

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

revision 102 by harbaum, Tue Sep 8 19:08:37 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  #define OSD_SCALE  #ifndef OSD_SCALE_FONT_SIZE
29    #define OSD_SCALE_FONT_SIZE 12
30  #define OSD_SCALE_W  120  #endif
31  #define OSD_SCALE_H   20  #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!"
# Line 46  typedef struct { Line 47  typedef struct {
47    
48  #ifdef OSD_SCALE  #ifdef OSD_SCALE
49      cairo_surface_t *scale;      cairo_surface_t *scale;
50        int scale_zoom;
51  #endif  #endif
52    
53  #ifdef OSD_SOURCE_SEL  #ifdef OSD_SOURCE_SEL
# Line 727  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  static char *  /* various parameters used to create the scale */
733  dist_str_metric(int dist)  #define OSD_SCALE_H2   (OSD_SCALE_H/2)
734  {  #define OSD_SCALE_TICK (2*OSD_SCALE_FONT_SIZE/3)
735      if(dist<1000)  #define OSD_SCALE_M    (OSD_SCALE_H2 - OSD_SCALE_TICK)
736          return g_strdup_printf("%u m",  dist);  #define OSD_SCALE_I    (OSD_SCALE_H2 + OSD_SCALE_TICK)
737    #define OSD_SCALE_FD   (OSD_SCALE_FONT_SIZE/4)
     return g_strdup_printf("%u km", dist/1000);  
 }  
   
 #define OSD_SCALE_FONT_SIZE 12  
738    
739  static void  static void
740  osd_render_scale(osm_gps_map_osd_t *osd)  osd_render_scale(osm_gps_map_osd_t *osd)
741  {  {
742      osd_priv_t *priv = (osd_priv_t*)osd->priv;      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));      float m_per_pix = osm_gps_map_get_scale(OSM_GPS_MAP(osd->widget));
753    
754      /* first fill with transparency */      /* first fill with transparency */
755      cairo_t *cr = cairo_create(priv->scale);      cairo_t *cr = cairo_create(priv->scale);
756      cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);      cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
757      cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.0);      cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.0);
758      //    cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.2);      // pink for testing:    cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.2);
759      cairo_paint(cr);      cairo_paint(cr);
760      cairo_set_operator(cr, CAIRO_OPERATOR_OVER);      cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
761    
762      /* determine the size of the scale width in meters */      /* determine the size of the scale width in meters */
763      float width = (OSD_SCALE_W-2) * m_per_pix;      float width = (OSD_SCALE_W-OSD_SCALE_FONT_SIZE/6) * m_per_pix;
     printf("%d pixels is %f m\n", OSD_SCALE_W-2, width);  
764    
765      /* scale this to useful values */      /* scale this to useful values */
766      int exp = logf(width)*M_LOG10E;      int exp = logf(width)*M_LOG10E;
767      int mant = width/pow(10,exp);      int mant = width/pow(10,exp);
768      int width_metric = mant * pow(10,exp);      int width_metric = mant * pow(10,exp);
769      char *dist_str = dist_str_metric(width_metric);      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;      width_metric /= m_per_pix;
775    
776      /* and now the hard part: scale for useful imperial values :-( */      /* and now the hard part: scale for useful imperial values :-( */
# Line 783  osd_render_scale(osm_gps_map_osd_t *osd) Line 793  osd_render_scale(osm_gps_map_osd_t *osd)
793          }          }
794      }      }
795    
796      printf("this is %f %s\n", width, dist_imp_unit);      /* also convert this to full tens/hundreds */
797      printf("this is %f pixels\n", width * imp_scale / m_per_pix);      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",      cairo_select_font_face (cr, "Sans",
807                              CAIRO_FONT_SLANT_NORMAL,                              CAIRO_FONT_SLANT_NORMAL,
# Line 796  osd_render_scale(osm_gps_map_osd_t *osd) Line 813  osd_render_scale(osm_gps_map_osd_t *osd)
813      cairo_text_extents (cr, dist_str, &extents);      cairo_text_extents (cr, dist_str, &extents);
814    
815      cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);      cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
816      cairo_move_to (cr, OSD_SCALE_FONT_SIZE/3, OSD_SCALE_FONT_SIZE);      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);      cairo_text_path (cr, dist_str);
819      cairo_set_line_width (cr, 2);      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);      cairo_stroke (cr);
824    
825      cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);      cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
826      cairo_move_to (cr, OSD_SCALE_FONT_SIZE/3, OSD_SCALE_FONT_SIZE);      cairo_move_to (cr, 2*OSD_SCALE_FD, OSD_SCALE_H2-OSD_SCALE_FD);
827      cairo_show_text (cr, dist_str);      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 */      /* draw white line */
836      cairo_set_line_cap  (cr, CAIRO_LINE_CAP_ROUND);      cairo_set_line_cap  (cr, CAIRO_LINE_CAP_ROUND);
837      cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);      cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
838      cairo_set_line_width (cr, 4);      cairo_set_line_width (cr, OSD_SCALE_FONT_SIZE/3);
839      cairo_move_to (cr, 2, 2*OSD_SCALE_FONT_SIZE/3);      cairo_move_to (cr, OSD_SCALE_FONT_SIZE/6, OSD_SCALE_M);
840      cairo_rel_line_to (cr, 0,  2*OSD_SCALE_FONT_SIZE/3);      cairo_rel_line_to (cr, 0,  OSD_SCALE_TICK);
841      cairo_rel_line_to (cr, width_metric, 0);      cairo_rel_line_to (cr, width_metric, 0);
842      cairo_rel_line_to (cr, 0, -2*OSD_SCALE_FONT_SIZE/3);      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);      cairo_stroke(cr);
849    
850      /* draw black line */      /* draw black line */
851      cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);      cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
852      cairo_set_line_width (cr, 2);      cairo_set_line_width (cr, OSD_SCALE_FONT_SIZE/6);
853      cairo_move_to (cr, 2, 2*OSD_SCALE_FONT_SIZE/3);      cairo_move_to (cr, OSD_SCALE_FONT_SIZE/6, OSD_SCALE_M);
854      cairo_rel_line_to (cr, 0,  2*OSD_SCALE_FONT_SIZE/3);      cairo_rel_line_to (cr, 0,  OSD_SCALE_TICK);
855      cairo_rel_line_to (cr, width_metric, 0);      cairo_rel_line_to (cr, width_metric, 0);
856      cairo_rel_line_to (cr, 0, -2*OSD_SCALE_FONT_SIZE/3);      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);      cairo_stroke(cr);
   
     /* xyz */  
863    
864      cairo_destroy(cr);      cairo_destroy(cr);
865  }  }
# Line 944  osd_draw(osm_gps_map_osd_t *osd, GdkDraw Line 979  osd_draw(osm_gps_map_osd_t *osd, GdkDraw
979  #ifdef OSD_SCALE  #ifdef OSD_SCALE
980          priv->scale =          priv->scale =
981              cairo_image_surface_create(CAIRO_FORMAT_ARGB32,              cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
982                                         OSD_SCALE_W+2, OSD_SCALE_H+2);                                         OSD_SCALE_W, OSD_SCALE_H);
983            priv->scale_zoom = -1;
984  #endif  #endif
985    
986          /* ... and render it */          /* ... and render it */

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