[GRASS-SVN] r68877 - grass-addons/grass7/display/d.vect.legend

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Jul 6 22:33:07 PDT 2016


Author: lazaa
Date: 2016-07-06 22:33:07 -0700 (Wed, 06 Jul 2016)
New Revision: 68877

Added:
   grass-addons/grass7/display/d.vect.legend/map.png
Modified:
   grass-addons/grass7/display/d.vect.legend/draw.c
   grass-addons/grass7/display/d.vect.legend/local_proto.h
   grass-addons/grass7/display/d.vect.legend/main.c
   grass-addons/grass7/display/d.vect.legend/pipe.csv
Log:
Background, columns, diferents fonts...

Modified: grass-addons/grass7/display/d.vect.legend/draw.c
===================================================================
--- grass-addons/grass7/display/d.vect.legend/draw.c	2016-07-07 03:26:13 UTC (rev 68876)
+++ grass-addons/grass7/display/d.vect.legend/draw.c	2016-07-07 05:33:07 UTC (rev 68877)
@@ -9,30 +9,36 @@
  */
 
 #include <string.h>
+#include <math.h>
 #include <grass/display.h>
 #include <grass/glocale.h>
 #include <grass/colors.h>
 #include "local_proto.h"
 
-void draw(char *file_name, char *sep, double LL, double LT, char *title, int bgcolor, int bcolor,
-          int do_bg, char* tit_font, int tit_size, char *sub_font, int sub_size, char *font,
-          int fontsize, int fontcolor)
+void draw(char *file_name, char *sep, double LL, double LT, char *title, int cols, int bgcolor,
+          int bcolor, int do_bg, char* tit_font, int tit_size, char *sub_font, int sub_size,
+          char *font, int fontsize, int fontcolor)
 {
     double db, dt, dl, dr;
     double bb, bt, bl, br;
     double x0, y0;
-    double offs_y, row_ind;
+    double offs_y, row_ind, offs_x;
+    double x, y;
     FILE *file_in;
     char buf[512];
     int got_new;
     SYMBOL *Symb;
     char *symb_name, *line_color_str, *fill_color_str, *label;
     double size, line_width;
-    double row_w, row_h;
+    double row_w, text_h, title_h, title_w;
     RGBA_Color *line_color, *fill_color;
     int ret, R, G, B;
     char *part, *sub_delim;
     double maxlblw, sym_lbl_space;
+    double symb_h, symb_w;
+    int item_count, item;
+    double it_per_col;
+    double margin, bg_h, bg_w;
 
 
 
@@ -40,19 +46,17 @@
     x0 = dl + (int)((dr - dl) * LL / 100.);
     y0 = dt + (int)((db - dt) * (100 - LT) / 100.);
 
-    maxlblw = 0;
-    offs_y = 0;
-
     /* Draw title */
     if (strlen(title) > 0) {
         D_text_size(tit_size, tit_size);
         D_font(tit_font);
         D_get_text_box(title, &bb, &bt, &bl, &br);
-        row_h = bb - bt;
-        offs_y += row_h;
-        maxlblw = br - bl;
+        title_h = bb - bt;
+        title_w = br - bl;
         if (! do_bg) {
-            D_pos_abs(x0, y0 + offs_y);
+            x = x0;
+            y = y0 + title_h;
+            D_pos_abs(x, y);
             D_use_color(fontcolor);
             D_text(title);
         }
@@ -62,15 +66,53 @@
     if (!file_in)
         G_fatal_error(_("Unable to open input file <%s>"), file_name);
 
+    /* Get number of legend row(item) */
+    item_count = 0;
+    got_new = G_getl2(buf, sizeof(buf), file_in);
+    G_strip(buf);
+    while (got_new) {
+        item_count++;
+        got_new = G_getl2(buf, sizeof(buf), file_in);
+        G_strip(buf);
+    }
+    rewind(file_in);
+    it_per_col = ceil(item_count / (cols * 1.0));
+
+//    D_pos_abs(x0 + 200, y0 + offs_y);
+//    D_use_color(fontcolor);
+//    label = G_malloc(strlen(buf)+1);
+//    sprintf(label, "%d", item_count);
+//    D_text(label);
+
+
+    bg_h = 0;
+    maxlblw = 0;
+    offs_y = 0;
     row_ind = 5;
     sym_lbl_space = 5;
+    item = 0;
+    offs_x = 0;
+    margin = 10;
     sub_delim = G_malloc(strlen(buf)+1);
-    snprintf(sub_delim, sizeof(sub_delim), "%s%s%s%s%s", sep, sep, sep, sep, sep);
+    snprintf(sub_delim, sizeof(strlen(buf)+1), "%s%s%s%s%s", sep, sep, sep, sep, sep);
 
     got_new = G_getl2(buf, sizeof(buf), file_in);
     G_strip(buf);
+
     while (got_new) {
+        if (item < it_per_col)
+            item++;
+        else {
+            if (bg_h < offs_y)
+                bg_h = offs_y + symb_h/2.;
+            offs_x += maxlblw + margin;
+            offs_y = 0;
+            maxlblw = 0;
+            item = 1;
+
+        }
         if (strstr(buf, sub_delim) != NULL) {
+            /* Group subtitle */
             label = G_malloc(strlen(buf)+1);
             part = strtok(buf, sep);
             sscanf(part, "%s", label);
@@ -78,20 +120,24 @@
             D_text_size(sub_size, sub_size);
             D_font(sub_font);
             D_get_text_box(label, &bb, &bt, &bl, &br);
-            row_h = bb - bt;
-            row_w = br - bb;
-            offs_y += row_h + row_ind;
+            text_h = bb - bt;
+            row_w = br - bl;
+            offs_y += text_h + row_ind;
+            if (bg_h < offs_y)
+                bg_h = offs_y + symb_h/2.;
             if (row_w > maxlblw)
                 maxlblw = row_w;
 
             if (! do_bg) {
-                D_pos_abs(x0, y0 + offs_y);
+                x = x0 + offs_x;
+                y = y0 + title_h + offs_y;
+                D_pos_abs(x, y);
                 D_use_color(fontcolor);
                 D_text(label);
             }
         }
         else {
-            /* Parse the line */
+            /* Items */
             symb_name = G_malloc(strlen(buf) + 1);
             line_color_str = G_malloc(strlen(buf) + 1);
             fill_color_str = G_malloc(strlen(buf) + 1);
@@ -146,17 +192,26 @@
             D_text_size(fontsize, fontsize);
             D_font(font);
             D_get_text_box(label, &bb, &bt, &bl, &br);
-            row_h = bb - bt;
-            row_w = row_h + sym_lbl_space + br - bb;
-            offs_y += row_h + row_ind;
+
+            symb_h = Symb->yscale * size * 2;
+            symb_w = Symb->xscale * size * 2;
+            text_h = bb - bt;
+            row_w = symb_w + sym_lbl_space + br - bl;
+            offs_y += symb_h + row_ind;
+            if (bg_h < offs_y)
+                bg_h = offs_y + symb_h/2.;
             if (row_w > maxlblw)
                 maxlblw = row_w;
 
             if (! do_bg) {
                 S_stroke(Symb, size, 0, 0);
-                D_symbol(Symb, x0 + row_h/2, y0 + offs_y - row_h/2, line_color, fill_color);
+                x = x0 + offs_x + symb_h/2;
+                y = y0 + title_h + offs_y - symb_h/2;
+                D_symbol(Symb, x, y, line_color, fill_color);
 
-                D_pos_abs(x0 + row_h + sym_lbl_space, y0 + offs_y);
+                x = x0 + offs_x + symb_w + sym_lbl_space;
+                y = y0 + title_h + offs_y - symb_h/2. + text_h/2.;
+                D_pos_abs(x, y);
                 D_use_color(fontcolor);
                 D_text(label);
             }
@@ -166,28 +221,21 @@
         G_strip(buf);
     }
 
-    //            // Pomocny ctverec
-    //            D_begin();
-    //            D_move_abs(x0, y0 + offs_y);
-    //            D_cont_abs(x0 + row_h, y0 + offs_y);
-    //            D_cont_abs(x0 + row_h, y0 + offs_y - row_h);
-    //            D_cont_abs(x0, y0 + offs_y - row_h);
-    //            D_close();
-    //            D_end();
-    //            D_stroke();
-    //            // Pomocny ctverec
-
     fclose(file_in);
 
     /* Draw background */
     if (do_bg) {
-        double x0bg, y0bg, x1bg, y1bg, bg_margin;
-        bg_margin = 10;
-        x0bg = x0 - bg_margin;
-        y0bg = y0 - bg_margin;
-        x1bg = x0 + maxlblw + bg_margin;
-        y1bg = y0 + offs_y + bg_margin;
+        double x0bg, y0bg, x1bg, y1bg;
+        if (title_w > offs_x + maxlblw)
+            bg_w = title_w;
+        else
+            bg_w = offs_x + maxlblw;
 
+        x0bg = x0 - margin;
+        y0bg = y0 - margin;
+        x1bg = x0 + bg_w + margin;
+        y1bg = y0 + bg_h + margin;
+
         if (bgcolor) {
             D_use_color(bgcolor);
             D_box_abs(x0bg, y0bg, x1bg, y1bg);

Modified: grass-addons/grass7/display/d.vect.legend/local_proto.h
===================================================================
--- grass-addons/grass7/display/d.vect.legend/local_proto.h	2016-07-07 03:26:13 UTC (rev 68876)
+++ grass-addons/grass7/display/d.vect.legend/local_proto.h	2016-07-07 05:33:07 UTC (rev 68877)
@@ -1,3 +1,3 @@
 /* draw.c */
-void draw(char *, char *, double, double, char *, int, int,
+void draw(char *, char *, double, double, char *, int, int, int,
           int, char *, int, char *, int, char *, int, int);

Modified: grass-addons/grass7/display/d.vect.legend/main.c
===================================================================
--- grass-addons/grass7/display/d.vect.legend/main.c	2016-07-07 03:26:13 UTC (rev 68876)
+++ grass-addons/grass7/display/d.vect.legend/main.c	2016-07-07 05:33:07 UTC (rev 68877)
@@ -21,9 +21,9 @@
 int main(int argc, char **argv)
 {
     struct GModule *module;
-    struct Option *opt_input, *opt_sep, *opt_at, *opt_font, *opt_fontsize, *opt_fontcolor,
-            *opt_title, *opt_tit_font, *opt_tit_fontsize, *opt_sub_font, *opt_sub_fontsize,
-            *opt_bcolor, *opt_bgcolor;
+    struct Option *opt_input, *opt_sep, *opt_at, *opt_cols, *opt_font, *opt_fontsize,
+            *opt_fontcolor, *opt_title, *opt_tit_font, *opt_tit_fontsize, *opt_sub_font,
+            *opt_sub_fontsize, *opt_bcolor, *opt_bgcolor;
     struct Flag *fl_bg;
 
     double LL, LT;
@@ -32,6 +32,7 @@
     char *sep;
     int fontsize, fontcolor, tit_size, sub_size;
     char *font, *tit_font, *sub_font;
+    int cols;
 
 
     /* Initialize the GIS calls */
@@ -53,8 +54,6 @@
     opt_input->guisection = _("Input");
 
     opt_sep = G_define_standard_option(G_OPT_F_SEP);
-    opt_sep->key = "delim";
-    opt_sep->description = _("Text separator");
     opt_sep->guisection = _("Input");
 
     opt_at = G_define_option();
@@ -68,6 +67,15 @@
     _("Screen position of legend to be drawn (percentage, [0,0] is lower left)");
     opt_at->guisection = _("Position");
 
+    opt_cols = G_define_option();
+    opt_cols->key = "columns";
+    opt_cols->type = TYPE_INTEGER;
+    opt_cols->answer = "1";
+    opt_cols->required = NO;
+    opt_cols->description =
+    _("Number of legend columns");
+    opt_cols->guisection = _("Position");
+
     opt_title = G_define_option();
     opt_title->key = "title";
     opt_title->type = TYPE_STRING;
@@ -169,6 +177,11 @@
     else
         title = "";
 
+    if (opt_cols->answer)
+        sscanf(opt_cols->answer, "%d", &cols);
+    else
+        cols = 1;
+
     /* Background */
     do_bg = fl_bg->answer;
     bcolor = D_parse_color(opt_bcolor->answer, TRUE);
@@ -205,9 +218,9 @@
     fontcolor = D_parse_color(opt_fontcolor->answer, FALSE); /*default color: black */
 
     if (do_bg)
-        draw(file_name, sep, LL, LT, title, bgcolor, bcolor, 1, tit_font, tit_size, sub_font, sub_size, font, fontsize, fontcolor);
+        draw(file_name, sep, LL, LT, title, cols, bgcolor, bcolor, 1, tit_font, tit_size, sub_font, sub_size, font, fontsize, fontcolor);
 
-    draw(file_name, sep, LL, LT, title, bgcolor, bcolor, 0, tit_font, tit_size, sub_font, sub_size, font, fontsize, fontcolor);
+    draw(file_name, sep, LL, LT, title, cols, bgcolor, bcolor, 0, tit_font, tit_size, sub_font, sub_size, font, fontsize, fontcolor);
 
     D_close_driver();
 

Added: grass-addons/grass7/display/d.vect.legend/map.png
===================================================================
(Binary files differ)


Property changes on: grass-addons/grass7/display/d.vect.legend/map.png
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream

Modified: grass-addons/grass7/display/d.vect.legend/pipe.csv
===================================================================
--- grass-addons/grass7/display/d.vect.legend/pipe.csv	2016-07-07 03:26:13 UTC (rev 68876)
+++ grass-addons/grass7/display/d.vect.legend/pipe.csv	2016-07-07 05:33:07 UTC (rev 68877)
@@ -1,11 +1,11 @@
-|||||buildings
-basic/box|red|grey|15|2|hospital
-basic/box|red|yellow|15|3|firefighters
-basic/box|blue|yellow|15|3|police
-|||||land_use
-basic/octagon|green|blue|15|20|water
-basic/octagon|yellow|brown|15|3|fields
-basic/octagon|black|gray|15|3|urban
-|||||transport
-geology/strike_parallel|green|none|7|1|lajna
-geology/strike_line|blue|none|7|1|znak4
+|||||subgroup
+basic/box|red|grey|30|2|hospital
+basic/box|red|yellow|30|3|firefighters
+basic/box|blue|yellow|30|3|police
+|||||subgroup
+basic/octagon|green|blue|30|20|wat
+basic/octagon|yellow|brown|30|3|fies
+basic/octagon|black|gray|30|3|urb
+|||||subgroup
+geology/strike_parallel|green|none|15|1|lajna
+geology/strike_line|blue|none|15|1|znak4



More information about the grass-commit mailing list