Index: display/d.grid/d.grid.html
===================================================================
--- display/d.grid/d.grid.html	(revision 68535)
+++ display/d.grid/d.grid.html	(working copy)
@@ -40,7 +40,12 @@
 or <em>XY</em> projection.
 <p>
 Colors may be standard named GRASS colors (red, green, aqua, etc.) or
-a numerical R:G:B triplet, where component values range from 0-255.<br>
+a numerical R:G:B triplet, where component values range from 0-255.
+Grid color can be set with option <b>color</b>. Border color can be 
+se with option <b>border_color</b>. Text color can be set with option
+<b>text_color</b>. Background color can be set with option 
+<b>bg_color</b>.
+<br>
 
 <p>
 The grid drawing may be turned off by using the <em>-n</em> flag.<br>
@@ -94,6 +99,7 @@
 Geogrid support: Bob Covill<br>
 Border support: Markus Neteler<br>
 Text and RGB support: Hamish Bowman<br>
+Improvements as part of GSoC 2016 by Adam Laza, CTU in Prague 
 
 <p>
 <i>Last changed: $Date$</i>
Index: display/d.grid/local_proto.h
===================================================================
--- display/d.grid/local_proto.h	(revision 68535)
+++ display/d.grid/local_proto.h	(working copy)
@@ -5,9 +5,10 @@
 #define DIRN_LON  2
 
 /* plot.c */
-int plot_grid(double, double, double, int, int, int, int, int, double, int);
+int plot_grid(double, double, double, int, int, int, int, int, int, double,
+              int);
 int plot_geogrid(double, struct pj_info, struct pj_info, int, int, int, int,
-                 int, double, int);
+                 int, int, double, int);
 void init_proj(struct pj_info *, struct pj_info *, int);
 void get_ll_bounds(double *, double *, double *, double *, struct Cell_head,
                    struct pj_info, struct pj_info);
Index: display/d.grid/main.c
===================================================================
--- display/d.grid/main.c	(revision 68535)
+++ display/d.grid/main.c	(working copy)
@@ -33,12 +33,13 @@
     int colorg = 0;
     int colorb = 0;
     int colort = 0;
+    int colorbg = 0;
     double size = 0., gsize = 0.;       /* initialize to zero */
     double east, north;
     int do_text, fontsize, mark_type, line_width, dirn;
     struct GModule *module;
     struct Option *opt1, *opt2, *opt3, *opt4, *fsize, *tcolor, *lwidth,
-        *direction;
+        *direction, *bgcolor;
     struct Flag *noborder, *notext, *geogrid, *nogrid, *wgs84, *cross,
         *fiducial, *dot, *align;
     struct pj_info info_in;     /* Proj structures */
@@ -105,6 +106,12 @@
     tcolor->label = _("Text color");
     tcolor->guisection = _("Color");
 
+    bgcolor = G_define_standard_option(G_OPT_CN);
+    bgcolor->key = "bgcolor";
+    bgcolor->answer = "none";
+    bgcolor->label = _("Background color");
+    bgcolor->guisection = _("Color");
+
     fsize = G_define_option();
     fsize->key = "fontsize";
     fsize->type = TYPE_INTEGER;
@@ -269,6 +276,8 @@
     colorb = D_parse_color(opt4->answer, FALSE);
     /* Parse and select text color */
     colort = D_parse_color(tcolor->answer, FALSE);
+    /* Parse and select background color */
+    colorbg = D_parse_color(bgcolor->answer, TRUE);
 
 
     D_setup(0);
@@ -279,12 +288,12 @@
             /* initialzie proj stuff */
             init_proj(&info_in, &info_out, wgs84->answer);
             plot_geogrid(gsize, info_in, info_out, do_text, colorg, colort,
-                         fontsize, mark_type, line_width, dirn);
+                         colorbg, fontsize, mark_type, line_width, dirn);
         }
         else {
             /* Do the grid plotting */
-            plot_grid(size, east, north, do_text, colorg, colort, fontsize,
-                      mark_type, line_width, dirn);
+            plot_grid(size, east, north, do_text, colorg, colort, colorbg,
+                      fontsize, mark_type, line_width, dirn);
         }
     }
 
Index: display/d.grid/plot.c
===================================================================
--- display/d.grid/plot.c	(revision 68535)
+++ display/d.grid/plot.c	(working copy)
@@ -10,8 +10,8 @@
 
 
 int plot_grid(double grid_size, double east, double north, int do_text,
-              int gcolor, int tcolor, int fontsize, int mark_type,
-              double line_width, int direction)
+              int gcolor, int tcolor, int bgcolor, int fontsize,
+              int mark_type, double line_width, int direction)
 {
     double x, y, y0;
     double e1, e2;
@@ -18,6 +18,7 @@
     struct Cell_head window;
     double row_dist, colm_dist;
     char text[128];
+    double tx, ty, bt, bb, bl, br, w, h;
 
     G_get_set_window(&window);
 
@@ -43,31 +44,11 @@
                 D_line_abs(x, window.north, x, window.south);
                 D_line_width(0);        /* reset so text doesn't use it */
             }
-
-            if (do_text) {
-                D_use_color(tcolor);
-                G_format_easting(x, text, G_projection());
-                D_text_rotation(270.0);
-                D_text_size(fontsize, fontsize);
-
-                /* Positioning -
-                   x: 4 pixels to the right of the grid line, + 0.5 rounding factor.
-                   y: End of text is 7 pixels up from bottom of screen, +.5 rounding.
-                   fontsize*.81 = actual text width FOR DEFAULT FONT (NOT FreeType)
-                 */
-                D_pos_abs(x + 4.5 * D_get_d_to_u_xconv(), D_get_u_south()
-                          -
-                          D_get_d_to_u_yconv() * (strlen(text) * fontsize *
-                                                  0.81 + 7.5));
-
-                D_text(text);
-            }
             x += grid_size;
         }
         D_text_rotation(0.0);   /* reset */
     }
 
-
     /* Draw horizontal grids
      *
      * For latlon, must draw in shorter sections
@@ -93,30 +74,11 @@
                 D_line_abs(e2, y, window.west, y);
                 D_line_width(0);        /* reset so text doesn't use it */
             }
-
-            if (do_text) {
-                D_use_color(tcolor);
-                G_format_northing(y, text, G_projection());
-                D_text_size(fontsize, fontsize);
-
-                /* Positioning -
-                   x: End of text is 7 pixels left from right edge of screen, +.5 rounding.
-                   fontsize*.81 = actual text width FOR DEFAULT FONT (NOT FreeType)
-                   y: 4 pixels above each grid line, +.5 rounding.
-                 */
-                D_pos_abs(D_get_u_east()
-                          -
-                          D_get_d_to_u_xconv() * (strlen(text) * fontsize *
-                                                  0.81 + 7.5),
-                          y - D_get_d_to_u_yconv() * 4.5);
-
-                D_text(text);
-            }
             y += grid_size;
         }
+        D_text_rotation(0.0);
     }
 
-
     /* draw marks not grid lines */
     if (mark_type != MARK_GRID) {
         /* reset x and y */
@@ -145,12 +107,124 @@
         }
     }
 
+    /* Draw vertical labels */
+    if (do_text) {
+        if (window.west > east)
+            x = ceil((window.west - east) / grid_size) * grid_size + east;
+        else
+            x = east - ceil((east - window.west) / grid_size) * grid_size;
+
+        if (direction != DIRN_LAT) {
+            while (x <= window.east) {
+                D_use_color(tcolor);
+                G_format_easting(x, text, G_projection());
+                D_text_rotation(270.0);
+                D_text_size(fontsize, fontsize);
+
+                /* Positioning -
+                   x: 4 pixels to the right of the grid line, + 0.5 rounding factor.
+                   y: End of text is 7 pixels up from bottom of screen, +.5 rounding.
+                   fontsize*.81 = actual text width FOR DEFAULT FONT (NOT FreeType)
+                 */
+                D_pos_abs(x + 4.5 * D_get_d_to_u_xconv(), D_get_u_south()
+                          - D_get_d_to_u_yconv() * (strlen(text)
+                          * fontsize * 0.81 + 7.5));
+
+                tx = x + 4.5 * D_get_d_to_u_xconv();
+                ty = D_get_u_south() -
+                    D_get_d_to_u_yconv() * (strlen(text) * fontsize * 0.81 + 7.5);
+
+                if (bgcolor != 0) {
+                    D_get_text_box(text, &bt, &bb, &bl, &br);
+                    w = br - bl;
+                    h = bt - bb;
+                    bl = tx - w/2;
+                    bt = ty + h/10;
+                    br = tx + w + w/2;
+                    bb = ty - h - h/10;
+                    D_use_color(bgcolor);
+                    D_box_abs(bl, bt, br, bb);
+                }
+
+                D_use_color(tcolor);
+                D_pos_abs(tx, ty);
+                D_text(text);
+                x += grid_size;
+            }
+            D_text_rotation(0.0);   /* reset */
+        }
+    }
+
+    /* Draw horizontal labels */
+    if (do_text) {
+        if (window.south > north)
+            y = ceil((window.south - north) / grid_size) * grid_size + north;
+        else
+            y = north - ceil((north - window.south) / grid_size) * grid_size;
+
+        if (direction != DIRN_LON) {
+            while (y <= window.north) {
+
+                D_use_color(tcolor);
+                G_format_northing(y, text, G_projection());
+                D_text_size(fontsize, fontsize);
+
+                /* Positioning -
+                   x: End of text is 7 pixels left from right edge of screen, +.5 rounding.
+                   fontsize*.81 = actual text width FOR DEFAULT FONT (NOT FreeType)
+                   y: 4 pixels above each grid line, +.5 rounding.
+                 */
+                D_pos_abs(D_get_u_east()
+                          -
+                          D_get_d_to_u_xconv() * (strlen(text) * fontsize *
+                                                  0.81 + 7.5),
+                          y - D_get_d_to_u_yconv() * 4.5);
+
+                tx = D_get_u_east() -
+                     D_get_d_to_u_xconv() * (strlen(text) * fontsize * 0.81 +
+                                             7.5);
+                ty = y - D_get_d_to_u_yconv() * 4.5;
+
+                if (bgcolor != 0) {
+                    D_get_text_box(text, &bt, &bb, &bl, &br);
+                    w = br - bl;
+                    h = bt - bb;
+
+                    if (w > 0)
+                    w += 0.2 * fontsize;
+                    else
+                    /* D_text() does not draw " ". */
+                    w = 0.8 * fontsize;
+                    if (h > 0)
+                    h += 0.2 * fontsize;
+                    else
+                    /* D_text() does not draw " ". */
+                    h = 0.8 * fontsize;
+
+                    bl = tx - w/10;
+                    bt = ty + h + h/2;
+                    br = tx + w + w/10;
+                    bb = ty - h/2;
+                    D_use_color(bgcolor);
+                    D_box_abs(bl, bt, br, bb);
+                }
+
+                D_use_color(tcolor);
+                D_pos_abs(tx, ty);
+                D_text(text);
+
+                y += grid_size;
+            }
+            D_text_rotation(0.0);
+        }
+    }
+
     return 0;
 }
 
 
 int plot_geogrid(double size, struct pj_info info_in, struct pj_info info_out,
-                 int do_text, int gcolor, int tcolor, int fontsize,
+                 int do_text, int gcolor, int tcolor, int bgcolor, int fontsize,
                  int mark_type, double line_width, int direction)
 {
     double g;
@@ -167,6 +241,7 @@
     double row_dist, colm_dist;
     float font_angle;
     struct Cell_head window;
+    double tx, ty, bt, bb, bl, br, w, h;
 
     /* geo current region */
     G_get_set_window(&window);
@@ -224,19 +299,7 @@
 
             D_line_width(0);
         }
-
-        if (do_text) {
-            /* Set text color */
-            D_use_color(tcolor);
-
-            G_format_northing(g, text, PROJECTION_LL);
-            D_text_rotation(font_angle);
-            D_text_size(fontsize, fontsize);
-            D_pos_abs(D_get_u_west() + D_get_d_to_u_xconv() * border_off,
-                      start_coord - D_get_d_to_u_yconv() * grid_off);
-            D_text(text);
         }
-    }
 
     /* Lines of Longitude */
     g = floor(east / size) * size;
@@ -293,20 +356,7 @@
 
             D_line_width(0);
         }
-        if (do_text) {
-            /* Set text color */
-            D_use_color(tcolor);
-
-            G_format_easting(g, text, PROJECTION_LL);
-            D_text_rotation(font_angle);
-            D_text_size(fontsize, fontsize);
-            D_pos_abs(start_coord + D_get_d_to_u_xconv() * (grid_off + 1.5),
-                      D_get_u_north() + D_get_d_to_u_yconv() *
-                      (border_off + extra_y_off));
-            D_text(text);
         }
-    }
-
     D_text_rotation(0.0);       /* reset */
 
     /* draw marks not grid lines */
@@ -334,6 +384,148 @@
         /* also TODO: rotate cross and fiducial marks by the converge angle; see g.region -n */
     }
 
+    /* geo current region */
+    G_get_set_window(&window);
+
+    /* Adjust south and east back by one pixel for display bug? */
+    row_dist = D_d_to_u_row(0.) - D_d_to_u_row(1.);
+    colm_dist = D_d_to_u_col(1.) - D_d_to_u_col(0.);
+    window.south += row_dist;
+    window.east -= colm_dist;
+
+    /* get lat long min max */
+    /* probably need something like boardwalk ?? */
+    get_ll_bounds(&west, &east, &south, &north, window, info_in, info_out);
+
+    G_debug(3, "REGION BOUNDS N=%f S=%f E=%f W=%f", north, south, east, west);
+
+
+    /* Labels of Latitude */
+    if (do_text) {
+        g = floor(north / size) * size;
+        e1 = east;
+        for (j = 0; g >= south; j++, g -= size) {
+            start_coord = -9999.;
+            if (g == north || g == south || direction == DIRN_LON)
+                continue;
+
+            /* Set grid color */
+            for (ll = 0; ll < SEGS; ll++) {
+                n1 = n2 = g;
+                e1 = west + (ll * ((east - west) / SEGS));
+                e2 = e1 + ((east - west) / SEGS);
+                if (pj_do_proj(&e1, &n1, &info_in, &info_out) < 0)
+                    G_fatal_error(_("Error in pj_do_proj"));
+
+                check_coords(e1, n1, &lon, &lat, 1, window, info_in, info_out);
+                e1 = lon;
+                n1 = lat;
+                if (pj_do_proj(&e2, &n2, &info_in, &info_out) < 0)
+                    G_fatal_error(_("Error in pj_do_proj"));
+
+                check_coords(e2, n2, &lon, &lat, 1, window, info_in, info_out);
+                e2 = lon;
+                n2 = lat;
+                if (start_coord == -9999.) {
+                    start_coord = n1;
+                    font_angle = get_heading((e1 - e2), (n1 - n2));
+                }
+            }
+
+            G_format_northing(g, text, PROJECTION_LL);
+            D_text_rotation(font_angle);
+            D_text_size(fontsize, fontsize);
+            tx = D_get_u_west() + D_get_d_to_u_xconv() * border_off;
+            ty = start_coord - D_get_d_to_u_yconv() * grid_off;
+
+            if (bgcolor != 0) {
+                D_get_text_box(text, &bt, &bb, &bl, &br);
+                w = br - bl;
+                h = bt - bb;
+                bl = tx - w/10;
+                bt = ty + h + h/2;
+                br = tx + w + w/10;
+                bb = ty - h/2;
+                D_use_color(bgcolor);
+                D_box_abs(bl, bt, br, bb);
+            }
+
+            D_use_color(tcolor);
+            D_pos_abs(tx, ty);
+            D_text(text);
+        }
+    }
+
+
+    /* Labels of Longitude */
+    if (do_text) {
+        g = floor(east / size) * size;
+        n1 = north;
+        for (j = 0; g > west; j++, g -= size) {
+            start_coord = -9999.;
+            extra_y_off = 0.0;
+            if (g == east || g == west || direction == DIRN_LAT)
+                continue;
+
+            for (ll = 0; ll < SEGS; ll++) {
+                e1 = e2 = g;
+                n1 = north - (ll * ((north - south) / SEGS));
+                n2 = n1 - ((north - south) / SEGS);
+
+                if (pj_do_proj(&e1, &n1, &info_in, &info_out) < 0)
+                    G_fatal_error(_("Error in pj_do_proj"));
+
+                check_coords(e1, n1, &lon, &lat, 2, window, info_in, info_out);
+                e1 = lon;
+                n1 = lat;
+                if (pj_do_proj(&e2, &n2, &info_in, &info_out) < 0)
+                    G_fatal_error(_("Error in pj_do_proj"));
+
+                check_coords(e2, n2, &lon, &lat, 2, window, info_in, info_out);
+                e2 = lon;
+                n2 = lat;
+
+                if ((start_coord == -9999.) && (D_u_to_a_row(n1) > 0)) {
+                    font_angle = get_heading((e1 - e2), (n1 - n2));
+                    start_coord = e1;
+
+                    /* font rotates by bottom-left corner, try to keep top-left cnr on screen */
+                    if (font_angle - 270 > 0) {
+                        extra_y_off =
+                            sin((font_angle - 270) * M_PI / 180) * fontsize;
+                        if (D_u_to_d_row(n1) - D_get_d_north() <
+                            extra_y_off + grid_off)
+                            start_coord = -9999.;   /* wait until the next point south */
+                    }
+                }
+            }
+
+            G_format_easting(g, text, PROJECTION_LL);
+            D_text_rotation(font_angle);
+            D_text_size(fontsize, fontsize);
+            tx = start_coord + D_get_d_to_u_xconv() * (grid_off + 1.5);
+            ty = D_get_u_north() + D_get_d_to_u_yconv() * (border_off +
+                                                           extra_y_off);
+
+            if (bgcolor != 0) {
+                D_get_text_box(text, &bt, &bb, &bl, &br);
+                w = br - bl;
+                h = bt - bb;
+                bl = tx - w/2;
+                bt = ty + h/10;
+                br = tx + w + w/2;
+                bb = ty - h - h/10;
+                D_use_color(bgcolor);
+                D_box_abs(bl, bt, br, bb);
+            }
+
+            D_use_color(tcolor);
+            D_pos_abs(tx, ty);
+            D_text(text);
+        }
+        D_text_rotation(0.0);       /* reset */
+    }
+
     return 0;
 
 }

Index: .
===================================================================
--- .	(revision 68535)
+++ .	(working copy)

