Make minimum breaks configurable.
[pspp] / src / output / ascii.c
index 756235c5cbf6cae7183e19d4a287ff6566840c7a..201248b448effe8c775fbced3cf448b5bc10548c 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 1997-9, 2000, 2007, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
+   Copyright (C) 1997-9, 2000, 2007, 2009, 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -48,7 +48,6 @@
 #include "output/table-item.h"
 #include "output/text-item.h"
 
-#include "gl/error.h"
 #include "gl/minmax.h"
 #include "gl/xalloc.h"
 
@@ -120,13 +119,14 @@ static const ucs4_t unicode_box_chars[N_BOX] =
     0x2564, 0x256a, 0x256c,
     0x2564, 0x256a, 0x256c,
     0x2554, 0x2560, 0x2560,
+    0x2560, 0x256c, 0x256c,
     0x2566, 0x256c, 0x256c,
   };
 
 static inline int
 make_box_index (int left, int right, int top, int bottom)
 {
-  return ((right * 3 + bottom) * 3 + left) * 3 + top;
+  return ((right * RENDER_N_LINES + bottom) * RENDER_N_LINES + left) * RENDER_N_LINES + top;
 }
 
 /* How to emphasize text. */
@@ -150,6 +150,12 @@ struct ascii_driver
     enum emphasis_style emphasis; /* How to emphasize text. */
     char *chart_file_name;      /* Name of files used for charts. */
 
+#ifdef HAVE_CAIRO
+    /* Colours for charts */
+    struct xr_color fg;
+    struct xr_color bg;
+#endif
+
     int width;                  /* Page width. */
     int length;                 /* Page length minus margins and header. */
     bool auto_width;            /* Use viewwidth as page width? */
@@ -158,6 +164,8 @@ struct ascii_driver
     int top_margin;            /* Top margin in lines. */
     int bottom_margin;         /* Bottom margin in lines. */
 
+    int min_break[TABLE_N_AXES]; /* Min cell size to break across pages. */
+
     const ucs4_t *box;          /* Line & box drawing characters. */
 
     /* Internal state. */
@@ -229,6 +237,7 @@ ascii_create (const char *file_name, enum settings_output_devices device_type,
               struct string_map *o)
 {
   enum { BOX_ASCII, BOX_UNICODE } box;
+  int min_break[TABLE_N_AXES];
   struct output_driver *d;
   struct ascii_driver *a;
   int paper_length;
@@ -251,12 +260,20 @@ ascii_create (const char *file_name, enum settings_output_devices device_type,
   a->top_margin = parse_int (opt (d, o, "top-margin", "0"), 0, INT_MAX);
   a->bottom_margin = parse_int (opt (d, o, "bottom-margin", "0"), 0, INT_MAX);
 
+  min_break[H] = parse_int (opt (d, o, "min-hbreak", "-1"), -1, INT_MAX);
+  min_break[V] = parse_int (opt (d, o, "min-vbreak", "-1"), -1, INT_MAX);
+
   a->width = parse_page_size (opt (d, o, "width", "79"));
   paper_length = parse_page_size (opt (d, o, "length", "66"));
   a->auto_width = a->width < 0;
   a->auto_length = paper_length < 0;
   a->length = paper_length - vertical_margins (a);
-
+  a->min_break[H] = min_break[H] >= 0 ? min_break[H] : a->width / 2;
+  a->min_break[V] = min_break[V] >= 0 ? min_break[V] : a->length / 2;
+#ifdef HAVE_CAIRO
+  parse_color (d, o, "background-color", "#FFFFFFFFFFFF", &a->bg);
+  parse_color (d, o, "foreground-color", "#000000000000", &a->fg);
+#endif
   box = parse_enum (opt (d, o, "box", "ascii"),
                     "ascii", BOX_ASCII,
                     "unicode", BOX_UNICODE,
@@ -303,7 +320,7 @@ parse_page_size (struct driver_option *option)
           if (dim >= 1 && errno != ERANGE && *tail == '\0')
             dim = value;
           else
-            error (0, 0, _("%s: %s must be positive integer or `auto'"),
+            msg (MW, _("%s: %s must be positive integer or `auto'"),
                    option->driver_name, option->name);
         }
     }
@@ -335,7 +352,7 @@ update_page_size (struct ascii_driver *a, bool issue_error)
   if (a->width < MIN_WIDTH || a->length < MIN_LENGTH)
     {
       if (issue_error)
-        error (0, 0,
+        msg (ME,
                _("ascii: page excluding margins and headers "
                  "must be at least %d characters wide by %d lines long, but "
                  "as configured is only %d characters by %d lines"),
@@ -384,8 +401,7 @@ ascii_flush (struct output_driver *driver)
       ascii_close_page (a);
 
       if (fn_close (a->file_name, a->file) != 0)
-        error (0, errno, _("ascii: closing output file `%s'"),
-               a->file_name);
+        msg_error (errno, _("ascii: closing output file `%s'"), a->file_name);
       a->file = NULL;
     }
 }
@@ -436,6 +452,8 @@ ascii_output_table_item (struct ascii_driver *a,
       params.line_widths[H][i] = width;
       params.line_widths[V][i] = width;
     }
+  for (i = 0; i < TABLE_N_AXES; i++)
+    params.min_break[i] = a->min_break[i];
 
   if (a->file == NULL && !ascii_open_page (a))
     return;
@@ -521,7 +539,9 @@ ascii_submit (struct output_driver *driver,
       char *file_name;
 
       file_name = xr_draw_png_chart (chart_item, a->chart_file_name,
-                                     a->chart_cnt++);
+                                     a->chart_cnt++,
+                                    &a->fg, 
+                                    &a->bg);
       if (file_name != NULL)
         {
           struct text_item *text_item;
@@ -857,6 +877,7 @@ ascii_layout_cell (struct ascii_driver *a, const struct table_cell *cell,
       size_t last_break_ofs = 0;
       int last_break_width = 0;
       int width = 0;
+      size_t graph_ofs;
       size_t ofs;
 
       for (ofs = 0; ofs < n; )
@@ -892,25 +913,27 @@ ascii_layout_cell (struct ascii_driver *a, const struct table_cell *cell,
             }
           ofs += mblen;
         }
-      if (b[ofs] != UC_BREAK_MANDATORY)
-        {
-          while (ofs > 0 && isspace (line[ofs - 1]))
-            {
-              ofs--;
-              width--;
-            }
-        }
-      if (width > *widthp)
-        *widthp = width;
+
+      /* Trim any trailing spaces off the end of the text to be drawn. */
+      for (graph_ofs = ofs; graph_ofs > 0; graph_ofs--)
+        if (!isspace (line[graph_ofs - 1]))
+          break;
+      width -= ofs - graph_ofs;
 
       /* Draw text. */
-      text_draw (a, cell->options, bb, clip, y, line, ofs, width);
+      text_draw (a, cell->options, bb, clip, y, line, graph_ofs, width);
 
-      /* Next line. */
-      pos += ofs;
-      if (ofs < n && isspace (line[ofs]))
-        pos++;
+      /* If a new-line ended the line, just skip the new-line.  Otherwise, skip
+         past any spaces past the end of the line (but not past a new-line). */
+      if (b[ofs] == UC_BREAK_MANDATORY)
+        ofs++;
+      else
+        while (ofs < n && isspace (line[ofs]) && b[ofs] != UC_BREAK_MANDATORY)
+          ofs++;
 
+      if (width > *widthp)
+        *widthp = width;
+      pos += ofs;
     }
   *heightp = y - bb[V][0];
 
@@ -998,7 +1021,7 @@ ascii_open_page (struct ascii_driver *a)
         }
       else
         {
-          error (0, errno, _("ascii: opening output file `%s'"),
+          msg_error (errno, _("ascii: opening output file `%s'"),
                  a->file_name);
           a->error = true;
           return false;