Hi,

I am modified some files of ps.map to improve vlegend. Thus, I change the mean 
of 'border' to the width of the line border, added 'color' y 'fcolor' 
(pscolors) to custom border and background colors, respectively, added 
a 'ref' to place the vlegend respect a point of reference as in the text 
command, and modified the 'where' to understand negative values (it need 
improve to use percent values) to place in te geographic region.

All this permit a easy place the vlegend in the lower-right corner of map as 
(now only is correct with one column):

vlegend
    where -100 0                --> future  100% 0%
    ref right lower
    font Monaco
    fontsize 8
    border 1
    color black
    fcolor white
    end

Anyone worling in the same direction?

Best

-- 
E. Jorge Tizado
--- originales/ps_vlegend.c	2009-02-04 16:43:54.000000000 +0100
+++ mejoras/ps_vlegend.c	2009-02-04 16:53:27.000000000 +0100
@@ -1,8 +1,8 @@
 /* Function: vect_legend
  **
  ** Author: Paul W. Carlson     April 1992
- ** Modified by: Radim Blazek Jan 2000 area, label added 
- ** Modified by: Morten Hulden Mar 2004, support for legend in columns added 
+ ** Modified by: Radim Blazek Jan 2000 area, label added
+ ** Modified by: Morten Hulden Mar 2004, support for legend in columns added
  */
 
 #include "vector.h"
@@ -26,7 +26,7 @@
 
     /* vec[][] is array of vectors on each row of legend, first to be drawn is first in array,
      *         label is stored in last in row (last plotted, first in the script)
-     * nvec[] is number of vector on each row 
+     * nvec[] is number of vector on each row
      *        index for both start at 0, lpos in script starts from 1
      * If position is not used by any vector it is used for next not used vector without
      * lpos or lpos > vector.count */
@@ -70,23 +70,21 @@
 
     /* get text location */
     dy = 1.5 * fontsize;
+    margin = 0.4 * fontsize;
 
-    if (vector.x > 0.0)
-	x = 72.0 * vector.x;
+    if (vector.x > 0.0) {
+	    x = 72.0 * vector.x;
+        if (x < PS.left_marg*72. + margin)
+            x = PS.left_marg*72. + margin;
+    }
     else
-	x = PS.map_left;
+    x = PS.map_left - (PS.map_right - PS.map_left) * (vector.x / 100.);
 
-    if (vector.y > 0.0)
-	y = 72.0 * (PS.page_height - vector.y);
-    else if (vector.x <= 0.0)
-	y = PS.min_y;
+    if (vector.y > 0.0) {
+	    y = 72.0 * (PS.page_height - vector.y);
+    }
     else
-	y = PS.map_bot;
-
-    margin = 0.4 * fontsize;
-
-    if (x < PS.left_marg*72 + margin)
-	x = PS.left_marg*72 + margin;
+	y = PS.map_bot - (PS.map_top - PS.map_bot) * (vector.y / 100.);
 
     if (lcount < vector.cols)
 	vector.cols = lcount;
@@ -134,35 +132,41 @@
 	if (width <= 0.0)
 	    width = 2.4 * fontsize;
 
-	/* HB 2006: always figure width & draw so the border will always work. */
-	/* if vector legend is on map... */
-	/*      if (y > PS.map_bot && y <= PS.map_top && x < PS.map_right) {   */
 	fprintf(PS.fp, "/mg %.1f def\n", margin);
 
 	/* get width of widest string in PostScript variable "w" */
 	fprintf(PS.fp, "/w 0 def 0 1 a length 1 sub { /i XD\n");
 	fprintf(PS.fp, "a i get SW pop /t XD t w gt {/w t def} if } for\n");
-	fprintf(PS.fp, "/w w %.1f add mg add %.1f add def\n", x, width);
+    fprintf(PS.fp, "/w w mg 2 mul add %.1f add def\n", width); // Define la anchura real, sin borde
+    fprintf(PS.fp, "/h mg 2 mul %.1f add def\n", lc * dy);     // Define la altura real, sin borde
 
-	/* make white background for text */
-	fprintf(PS.fp, "1 1 1 C ");
-	fprintf(PS.fp, "%.1f %.1f w %.1f B fill \n",
-		x - margin, y - lc * dy - margin, y);
-	/*      } */
+    fprintf(PS.fp, "/x %.1f w %d mul %d div sub def\n", x,
+            (vector.xref == LEFT ? 0 : 1),
+            (vector.xref == CENTER ? 2 : 1));
+
+    fprintf(PS.fp, "/y %.1f h %d mul %d div add def\n", y,
+            (vector.yref == UPPER ? 0 : 1),
+            (vector.yref == CENTER ? 2 : 1));
+
+	/* make color background for text */
+    set_ps_color(&(vector.fcolor));
+    fprintf(PS.fp, "x dup w add y dup h sub 3 1 roll B fill\n");
 
 	/* draw the border, if set */
-	if (vector.border != -1) {
-	    set_rgb_color(vector.border);
-
-	    fprintf(PS.fp, "%.1f %.1f w %.1f B\n",
-		    x - margin, y - lc * dy - margin, y);
+	if (vector.border > 0.) {
+        fprintf(PS.fp, "%.8f W\n", vector.border);
+        set_ps_color(&(vector.color));
+        fprintf(PS.fp, "x dup w add y dup h sub 3 1 roll B\n");
 	    fprintf(PS.fp, "D\n");
 	}
 
+    /* Initial position of legend data, en PostScript file */
+    fprintf(PS.fp, "/x x mg add def\n");
+
 	/* make the legend */
 	for (j = h * lc; j < st; j++) {	/* each row */
 	    G_debug(4, "  row = %d", j);
-	    y -= dy;		/* set position of next row */
+        fprintf(PS.fp, "/y y %.5f sub def\n", dy); /* set position of next row */
 	    for (k = 0; k < nvec[j]; k++) {
 		i = vec[j][k];
 
@@ -173,101 +177,82 @@
 		   (vector.layer[i].hwidth < 1. && vector.layer[i].colors[0]  == WHITE))
 		   {
 		   fprintf(PS.fp, "0.5 setgray ");
-		   fprintf(PS.fp, "%.1f %.1f %.1f %.1f B fill \n", 
+		   fprintf(PS.fp, "%.1f %.1f %.1f %.1f B fill \n",
 		   x, y, x + 72.0, y + fontsize);
 		   }
 		 */
 
 		if (vector.layer[i].type == VAREAS) {	/* added for areas */
 		    /* plot rectangle */
-		    yo = y - 0.1 * fontsize;
+		    yo = 0.1 * fontsize;
 
 		    if (vector.layer[i].pat != NULL ||
-			!(color_none(&vector.layer[i].fcolor))) {
-
-			if (vector.layer[i].pat != NULL) {	/* use pattern */
-			    sc = 0.5 * vector.layer[i].scale;	/* half scale */
+			!(color_none(&vector.layer[i].fcolor)))
+            {
+			    if (vector.layer[i].pat != NULL) /* use pattern */
+                {
+			        sc = 0.5 * vector.layer[i].scale;	/* half scale */
+			        /* load pattern */
+			        eps_bbox(vector.layer[i].pat, &llx, &lly, &urx, &ury);
+			        sprintf(pat, "APATTEPS%d", i);
+			        pat_save(PS.fp, vector.layer[i].pat, pat);
+
+			        fprintf(PS.fp, "<<  /PatternType 1\n    /PaintType 1\n    /TilingType 1\n");
+			        fprintf(PS.fp, "    /BBox [%f %f %f %f]\n", llx * sc, lly * sc, urx * sc, ury * sc);
+			        fprintf(PS.fp, "    /XStep %f\n    /YStep %f\n", (urx - llx) * sc, (ury - lly) * sc);
+			        fprintf(PS.fp, "    /PaintProc\n      { begin\n");
+			        fprintf(PS.fp, "        %f %f scale\n", sc, sc);
+			        set_ps_color(&(vector.layer[i].fcolor));
+			        fprintf(PS.fp, "        %.8f W\n", vector.layer[i].pwidth);
+			        fprintf(PS.fp, "        %s\n", pat);
+			        fprintf(PS.fp, "        end\n");
+			        fprintf(PS.fp, "      } bind\n>>\n");
+			        sprintf(pat, "APATT%d", i);
+			        fprintf(PS.fp, " matrix\n makepattern /%s exch def\n", pat);
+			        fprintf(PS.fp, "/Pattern setcolorspace\n %s setcolor\n", pat);
+			    }
+			    else	/* solid fill color */
+			        set_ps_color(&(vector.layer[i].fcolor));
 
-			    /* load pattern */
-			    eps_bbox(vector.layer[i].pat, &llx, &lly, &urx,
-				     &ury);
-			    sprintf(pat, "APATTEPS%d", i);
-			    pat_save(PS.fp, vector.layer[i].pat, pat);
-
-			    fprintf(PS.fp,
-				    "<<  /PatternType 1\n    /PaintType 1\n    /TilingType 1\n");
-			    fprintf(PS.fp, "    /BBox [%f %f %f %f]\n",
-				    llx * sc, lly * sc, urx * sc, ury * sc);
-			    fprintf(PS.fp, "    /XStep %f\n    /YStep %f\n",
-				    (urx - llx) * sc, (ury - lly) * sc);
-			    fprintf(PS.fp, "    /PaintProc\n      { begin\n");
-			    fprintf(PS.fp, "        %f %f scale\n", sc, sc);
-			    set_ps_color(&(vector.layer[i].fcolor));
-			    fprintf(PS.fp, "        %.8f W\n",
-				    vector.layer[i].pwidth);
-			    fprintf(PS.fp, "        %s\n", pat);
-			    fprintf(PS.fp, "        end\n");
-			    fprintf(PS.fp, "      } bind\n>>\n");
-			    sprintf(pat, "APATT%d", i);
-			    fprintf(PS.fp,
-				    " matrix\n makepattern /%s exch def\n",
-				    pat);
-			    fprintf(PS.fp,
-				    "/Pattern setcolorspace\n %s setcolor\n",
-				    pat);
-			}
-			else	/* solid fill color */
-			    set_ps_color(&(vector.layer[i].fcolor));
-
-			fprintf(PS.fp, "%.1f %.1f %.1f %.1f rectfill\n",
-				x + width / 5, yo, 3 * width / 5,
-				0.8 * fontsize);
+			    fprintf(PS.fp, "x %.1f add y %.1f sub %.1f %.1f rectfill\n", width/5, yo, 3*width/5, 0.8*fontsize);
 		    }
-		    if (!color_none(&vector.layer[i].color) &&
-			vector.layer[i].width > 0) {
-			fprintf(PS.fp, "%.8f W\n", vector.layer[i].width);
-			set_ps_color(&(vector.layer[i].color));
-			fprintf(PS.fp, "[] 0 setdash\n");
-			fprintf(PS.fp, "%.1f %.1f %.1f %.1f rectstroke\n",
-				x + width / 5, yo, 3 * width / 5,
-				0.8 * fontsize);
+		    if (!color_none(&vector.layer[i].color) && vector.layer[i].width > 0)
+            {
+			    fprintf(PS.fp, "%.8f W\n", vector.layer[i].width);
+			    set_ps_color(&(vector.layer[i].color));
+			    fprintf(PS.fp, "[] 0 setdash\n");
+			    fprintf(PS.fp, "x %.1f add y %.1f sub %.1f %.1f rectstroke\n", width/5, yo, 3*width /5, 0.8*fontsize);
 		    }
 		}
 		else if (vector.layer[i].type == VLINES) {
-		    yo = y + 0.35 * fontsize - vector.layer[i].offset;
+		    yo = 0.35 * fontsize - vector.layer[i].offset;
 		    /* do highlight, if any */
 		    if (vector.layer[i].hwidth) {
-			set_ps_color(&(vector.layer[i].hcolor));
-			fprintf(PS.fp, "%.8f W\n",
-				vector.layer[i].width +
-				2 * vector.layer[i].hwidth);
-			fprintf(PS.fp, "[] 0 setdash\n");
-			fprintf(PS.fp, "%.1f %.1f %.1f %.1f L\n", x + width,
-				yo, x, yo);
+			    set_ps_color(&(vector.layer[i].hcolor));
+			    fprintf(PS.fp, "%.8f W\n", vector.layer[i].width + 2 * vector.layer[i].hwidth);
+			    fprintf(PS.fp, "[] 0 setdash\n");
+			    fprintf(PS.fp, "x %.1f add y %.1f add dup x exch L\n", width, yo);
 		    }
-
 		    /* plot the primary color line */
 		    set_ps_color(&(vector.layer[i].color));
 		    fprintf(PS.fp, "%.8f W\n", vector.layer[i].width);
 		    fprintf(PS.fp, "%s setdash\n", vector.layer[i].setdash);
-		    fprintf(PS.fp, "%.1f %.1f %.1f %.1f L\n", x + width, yo,
-			    x, yo);
+		    fprintf(PS.fp, "x %.1f add y %.1f add dup x exch L\n", width, yo);
 		}
 		else if (vector.layer[i].type == VPOINTS) {
-		    /* TODO */
-		    yo = y + 0.5 * fontsize - vector.layer[i].offset;
-		    xo = x + width / 2;
-		    symbol_draw(vector.layer[i].symbol_ps, xo, yo,
-				vector.layer[i].size, vector.layer[i].rotate,
-				vector.layer[i].width);
-		}
+            fprintf(PS.fp, "gsave\n");
+            fprintf(PS.fp, "x %.5f add y %.5f add translate\n", width / 2, 0.5 * fontsize - vector.layer[i].offset);
+            fprintf(PS.fp, "%.5f dup scale\n", vector.layer[i].size);
+            fprintf(PS.fp, "%.5f rotate\n", vector.layer[i].rotate);
+            fprintf(PS.fp, "%.8f W\n", vector.layer[i].width / vector.layer[i].size);
+            fprintf(PS.fp, "%s\n", vector.layer[i].symbol_ps);
+            fprintf(PS.fp, "grestore\n");
+        }
 
 	    }
-
 	    /* plot the text */
 	    set_rgb_color(BLACK);
-	    fprintf(PS.fp, "a %d get %.1f %.1f MS\n",
-			j - h * lc, x + width, y);
+	    fprintf(PS.fp, "a %d get x %.1f add y MS\n", j - h * lc, width);
 	}
     }	/* h */
 
@@ -276,8 +261,8 @@
 
     fprintf(PS.fp, "[] 0 setdash\n");
 
-    if (PS.min_y > y)
-	PS.min_y = y;
+//     if (PS.min_y > y)
+// 	PS.min_y = y;
 
     return 0;
 }
--- originales/local_proto.h	2009-02-04 16:43:54.000000000 +0100
+++ mejoras/local_proto.h	2009-02-04 12:38:06.000000000 +0100
@@ -254,6 +254,11 @@
 int scan_resolution(char *, double *);
 
 /* scan_ref.c */
+#define LEFT 0
+#define RIGHT 1
+#define LOWER 0
+#define UPPER 1
+#define CENTER 2
 int scan_ref(char *, int *, int *);
 int lowercase(register char *);
 
--- originales/r_vlegend.c	2009-02-04 16:43:54.000000000 +0100
+++ mejoras/r_vlegend.c	2009-02-04 12:38:29.000000000 +0100
@@ -17,7 +17,10 @@
     "fontsize   fontsize",
     "width	sample box width",
     "cols	number of columns",
-    "border	color|none",
+    "border	border width",
+    "color  border color",
+    "fcolor background color",
+    "ref    upper|lower|center left|right|center",
     "span	column separation",
     ""
 };
@@ -26,15 +29,22 @@
 {
     char buf[1024];
     char *key, *data;
-    int fontsize, cols, border;
-    double x, y, width, cseparation;
+    int fontsize, cols;
+    int ret, r, g, b;
+    int xref, yref;
+    double x, y, width, cseparation, border;
 
     fontsize = 0;
     x = y = 0.0;
     width = -1;
     cols = 1;
-    border = -1;
+    border = -1.;
     cseparation = -1;
+    xref = LEFT;
+    yref = UPPER;
+
+    set_color(&(vector.color), 0, 0, 0);
+    set_color(&(vector.fcolor), 255, 255, 255);
 
     while (input(2, buf, help)) {
 	if (!key_data(buf, &key, &data))
@@ -49,6 +59,15 @@
 		continue;
 	}
 
+    if (KEY("ref")) {
+        if (!scan_ref(data, &xref, &yref)) {
+        xref = LEFT;
+        yref = UPPER;
+        error(key, data, "illegal ref request (vlegend)");
+        }
+        continue;
+    }
+
 	if (KEY("fontsize")) {
 	    fontsize = atoi(data);
 	    continue;
@@ -73,15 +92,38 @@
 	    continue;
 	}
 
-	if (KEY("border")) {
-	    border = get_color_number(data);
-	    if (border < 0) {
-		if (border != -999)	/* here -999 is "none" */
-		    error(key, data, "illegal border request");
-		border = -1;
-	    }
-	    continue;
-	}
+    if (KEY("border")) {
+        G_strip(data);
+        if (strcmp(data,"none")==0)
+            border = -1;
+        else
+            border = atof(data);
+        continue;
+    }
+
+    if (KEY("color")) {
+        ret = G_str_to_color(data, &r, &g, &b);
+        if (ret == 1)
+        set_color(&(vector.color), r, g, b);
+        else if (ret == 2)
+        unset_color(&(vector.color));
+        else
+        error(key, data, "illegal color request (vlegend)");
+
+        continue;
+    }
+
+    if (KEY("fcolor")) {
+        ret = G_str_to_color(data, &r, &g, &b);
+        if (ret == 1)
+        set_color(&(vector.fcolor), r, g, b);
+        else if (ret == 2)
+        unset_color(&(vector.fcolor));
+        else
+        error(key, data, "illegal fcolor request (vlegend)");
+
+        continue;
+    }
 
 	if (KEY("span")) {
 	    G_strip(data);
@@ -93,13 +135,15 @@
     }
     vector.x = x;
     vector.y = y;
+    vector.xref = xref;
+    vector.yref = yref;
     if (fontsize)
 	vector.fontsize = fontsize;
 
     if (width > 0)
-	vector.width = width;
+        vector.width = width;
     else
-	vector.width = 3 * fontsize / 72.0;
+        vector.width = 3 * fontsize / 72.0;
 
     vector.cols = cols;
     vector.border = border;
--- originales/vector.h	2009-02-04 16:43:54.000000000 +0100
+++ mejoras/vector.h	2009-02-04 12:34:03.000000000 +0100
@@ -99,11 +99,14 @@
     int count;			/* number of recorded layers */
     int alloc;			/* allocated space */
     double x, y;		/* legend position */
+    int xref, yref;     /* position relative to */
     int fontsize;		/* legend font size */
     char *font;			/* legend font */
     double width;		/* width of legend symbols */
     int cols;			/* number of colums  */
-    int border;			/* border color number or -1 for none */
+    double border;      /* border width, < 0 for none */
+    PSCOLOR color;      /* border color */
+    PSCOLOR fcolor;     /* background color */
     double span;		/* column separation in inches */
     LAYER *layer;
 };
_______________________________________________
grass-dev mailing list
[email protected]
http://lists.osgeo.org/mailman/listinfo/grass-dev

Reply via email to