[GRASS5] [PATCH] ps.map - legend in columns

Markus, Glynn, Hamish ...

A small patch to ps.map that adds a 'cols' option to vlegend, so the
legend can be printed in columns. Not too sofisticated - the columns will
overwrite each other if the legend text is too long or there are too many
columns. But I needed this so I tought I might as well submit it.

The default is still 1 column.

It also fixes the missing 'width' option for vlegend.

rgds
Morten

%<--------------------------------------------------------------------------------------
diff -uw grass57_exp_2004_02_28/ps/ps.map/ps_vlegend.c grass57_my/ps/ps.map/ps_vlegend.c
--- grass57_exp_2004_02_28/ps/ps.map/ps_vlegend.c 2003-03-26 16:56:15.000000000 +0100
+++ grass57_my/ps/ps.map/ps_vlegend.c 2004-03-04 02:53:37.000000000 +0100
@@ -2,6 +2,7 @@
**
** 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
*/

#include "vector.h"
@@ -10,8 +11,8 @@

int PS_vlegend (void)
{
- int i, j, k, l, lcount, nopos;
- double x, y, fontsize, dy, xo, yo, margin, width;
+ int h, i, j, k, l, lc, st, lcount, nopos;
+ double x, y, fontsize, dx, dy, xo, yo, xs, ys, margin, width;
     int **vec, *nvec;

     G_debug (2, "vect_legend(): count = %d", vector.count );
@@ -71,9 +72,22 @@
     margin = 0.4 * fontsize;
     if (x < PS.map_left + margin) x = PS.map_left + margin;

+ if (lcount < vector.cols) vector.cols = lcount;
+ dx = (PS.map_right - x) / vector.cols;
+ xs = x; /*save x and y*/
+ ys = y;
+ lc = (int) lcount/vector.cols; /* lines per column */
+
+ for ( h = 0; h < vector.cols; h++) {
+
+ y = ys;
+ if (h) x+=dx;
+ st = ((h+1)*lc < lcount) ? ((h+1)*lc) : lcount;
+
     /* make PostScript array "a" of name-mapset strings */
     fprintf(PS.fp, "/a [\n");
- for ( i = 0; i < lcount; i++ ) {
+
+ for ( i = h*lc; i < st; i++ ) {
         G_debug (4, " row = %d", i );
   if ( nvec[i] > 0 ) { /* used position */
       j = vec[i][nvec[i]-1]; /* vector with label */
@@ -101,11 +115,11 @@
       /* 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 - lcount * dy - margin, y);
+ x - margin, y - lc * dy - margin, y);
     }

     /* make the legend */
- for ( j = 0; j < lcount; j++ ) { /* each row */
+ for ( j = h*lc; j < st; j++ ) { /*each row */
         G_debug (4, " row = %d", j );
   y -= dy; /* set position of next row */
   for (k = 0; k < nvec[j]; k++) {
@@ -166,8 +180,12 @@

   /* plot the text */
   set_rgb_color(BLACK);
- fprintf(PS.fp, "a %d get %.1f %.1f MS\n", j, x + width, y);
+ fprintf(PS.fp, "a %d get %.1f %.1f MS\n", j - h*lc, x + width, y);
     }
+ } /*h*/
+ x = xs;
+ y = ys - lc*dy;
+
     fprintf(PS.fp, " 0 setdash\n");

     if (PS.min_y > y) PS.min_y = y;
diff -uw grass57_exp_2004_02_28/ps/ps.map/r_vlegend.c grass57_my/ps/ps.map/r_vlegend.c
--- grass57_exp_2004_02_28/ps/ps.map/r_vlegend.c 2003-03-26 16:56:15.000000000 +0100
+++ grass57_my/ps/ps.map/r_vlegend.c 2004-03-03 20:43:22.000000000 +0100
@@ -16,6 +16,8 @@
     "where x y",
     "font fontname",
     "fontsize fontsize",
+ "width width",
+ "cols cols",
     ""
};

@@ -24,12 +26,13 @@
{
     char buf[1024];
     char *key, *data;
- int fontsize;
+ int fontsize, cols;
     double x, y, width;

     fontsize = 0;
     x = y = 0.0;
     width = -1;
+ cols = 1;
     while (input(2, buf, help))
     {
   if (!key_data(buf, &key, &data)) continue;
@@ -65,6 +68,13 @@
       continue;
   }

+ if (KEY("cols"))
+ {
+ cols = atoi (data);
+ if (cols < 1 || cols > 10) cols = 1;
+ continue;
+ }
+
   error(key, data, "illegal vlegend sub-request");
     }
     vector.x = x;
@@ -74,5 +84,7 @@
     if (width > 0) vector.width = width;
     else vector.width = 3 * fontsize / 72.0;

+ vector.cols = cols;
+
     return 0;
}
diff -uw grass57_exp_2004_02_28/ps/ps.map/vector.h grass57_my/ps/ps.map/vector.h
--- grass57_exp_2004_02_28/ps/ps.map/vector.h 2003-10-11 06:15:12.000000000 +0200
+++ grass57_my/ps/ps.map/vector.h 2004-03-04 02:34:20.000000000 +0100
@@ -1,5 +1,6 @@
/** Modified by: Janne Soimasuo August 1994 line_cat added **/
/** Modified by: Radim Blazek Jan 2000 acolor, label added **/
+/** Modified by: Morten Hulden Mar 2004 cols added to vector **/
#include "clr.h"
#define PI 3.14159265
/* #define MAXVECTORS 20 */
@@ -81,6 +82,7 @@
     int fontsize; /* legend font size */
     char *font; /* legend font */
     double width; /* width of legend symbols */
+ int cols; /* number of colums */
     LAYER *layer;
} ;

(attachments)

ps.map_patch_mhu20040304.txt (4.23 KB)

On Thu, 4 Mar 2004, Morten Hulden wrote:

A small patch to ps.map that adds a 'cols' option to vlegend,

...

Sorry, forgot special case if (lcount%vector.cols) lc++;

Morten

Here is the patch again:

diff -uw grass57_exp_2004_02_28/ps/ps.map/ps_vlegend.c grass57_my/ps/ps.map/ps_vlegend.c
--- grass57_exp_2004_02_28/ps/ps.map/ps_vlegend.c 2003-03-26 16:56:15.000000000 +0100
+++ grass57_my/ps/ps.map/ps_vlegend.c 2004-03-04 02:53:37.000000000 +0100
@@ -2,6 +2,7 @@
**
** 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
*/

#include "vector.h"
@@ -10,8 +11,8 @@

int PS_vlegend (void)
{
- int i, j, k, l, lcount, nopos;
- double x, y, fontsize, dy, xo, yo, margin, width;
+ int h, i, j, k, l, lc, st, lcount, nopos;
+ double x, y, fontsize, dx, dy, xo, yo, xs, ys, margin, width;
     int **vec, *nvec;

     G_debug (2, "vect_legend(): count = %d", vector.count );
@@ -71,9 +72,22 @@
     margin = 0.4 * fontsize;
     if (x < PS.map_left + margin) x = PS.map_left + margin;

+ if (lcount < vector.cols) vector.cols = lcount;
+ dx = (PS.map_right - x) / vector.cols;
+ xs = x; /*save x and y*/
+ ys = y;
+ lc = (int) lcount/vector.cols; /* lines per column */
+ if (lcount%vector.cols) lc++;
+
+ for ( h = 0; h < vector.cols; h++) {
+
+ y = ys;
+ if (h) x+=dx;
+ st = ((h+1)*lc < lcount) ? ((h+1)*lc) : lcount;
+
     /* make PostScript array "a" of name-mapset strings */
     fprintf(PS.fp, "/a [\n");
- for ( i = 0; i < lcount; i++ ) {
+
+ for ( i = h*lc; i < st; i++ ) {
         G_debug (4, " row = %d", i );
   if ( nvec[i] > 0 ) { /* used position */
       j = vec[i][nvec[i]-1]; /* vector with label */
@@ -101,11 +115,11 @@
       /* 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 - lcount * dy - margin, y);
+ x - margin, y - lc * dy - margin, y);
     }

     /* make the legend */
- for ( j = 0; j < lcount; j++ ) { /* each row */
+ for ( j = h*lc; j < st; j++ ) { /*each row */
         G_debug (4, " row = %d", j );
   y -= dy; /* set position of next row */
   for (k = 0; k < nvec[j]; k++) {
@@ -166,8 +180,12 @@

   /* plot the text */
   set_rgb_color(BLACK);
- fprintf(PS.fp, "a %d get %.1f %.1f MS\n", j, x + width, y);
+ fprintf(PS.fp, "a %d get %.1f %.1f MS\n", j - h*lc, x + width, y);
     }
+ } /*h*/
+ x = xs;
+ y = ys - lc*dy;
+
     fprintf(PS.fp, " 0 setdash\n");

     if (PS.min_y > y) PS.min_y = y;
diff -uw grass57_exp_2004_02_28/ps/ps.map/r_vlegend.c grass57_my/ps/ps.map/r_vlegend.c
--- grass57_exp_2004_02_28/ps/ps.map/r_vlegend.c 2003-03-26 16:56:15.000000000 +0100
+++ grass57_my/ps/ps.map/r_vlegend.c 2004-03-03 20:43:22.000000000 +0100
@@ -16,6 +16,8 @@
     "where x y",
     "font fontname",
     "fontsize fontsize",
+ "width width",
+ "cols cols",
     ""
};

@@ -24,12 +26,13 @@
{
     char buf[1024];
     char *key, *data;
- int fontsize;
+ int fontsize, cols;
     double x, y, width;

     fontsize = 0;
     x = y = 0.0;
     width = -1;
+ cols = 1;
     while (input(2, buf, help))
     {
   if (!key_data(buf, &key, &data)) continue;
@@ -65,6 +68,13 @@
       continue;
   }

+ if (KEY("cols"))
+ {
+ cols = atoi (data);
+ if (cols < 1 || cols > 10) cols = 1;
+ continue;
+ }
+
   error(key, data, "illegal vlegend sub-request");
     }
     vector.x = x;
@@ -74,5 +84,7 @@
     if (width > 0) vector.width = width;
     else vector.width = 3 * fontsize / 72.0;

+ vector.cols = cols;
+
     return 0;
}
diff -uw grass57_exp_2004_02_28/ps/ps.map/vector.h grass57_my/ps/ps.map/vector.h
--- grass57_exp_2004_02_28/ps/ps.map/vector.h 2003-10-11 06:15:12.000000000 +0200
+++ grass57_my/ps/ps.map/vector.h 2004-03-04 02:34:20.000000000 +0100
@@ -1,5 +1,6 @@
/** Modified by: Janne Soimasuo August 1994 line_cat added **/
/** Modified by: Radim Blazek Jan 2000 acolor, label added **/
+/** Modified by: Morten Hulden Mar 2004 cols added to vector **/
#include "clr.h"
#define PI 3.14159265
/* #define MAXVECTORS 20 */
@@ -81,6 +82,7 @@
     int fontsize; /* legend font size */
     char *font; /* legend font */
     double width; /* width of legend symbols */
+ int cols; /* number of colums */
     LAYER *layer;
} ;

(attachments)

ps.map_patch_mhu20040304.txt (4.26 KB)

Thanks for the (updated) patch, Morten.
I have submitted it to CVS.

Best regards

Markus

On Thu, Mar 04, 2004 at 03:23:45AM +0100, Morten Hulden wrote:

Markus, Glynn, Hamish ...

A small patch to ps.map that adds a 'cols' option to vlegend, so the
legend can be printed in columns. Not too sofisticated - the columns will
overwrite each other if the legend text is too long or there are too many
columns. But I needed this so I tought I might as well submit it.

The default is still 1 column.

It also fixes the missing 'width' option for vlegend.

rgds
Morten