output: Implement nested tables.
[pspp] / tests / output / render-test.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <errno.h>
20 #include <getopt.h>
21 #include <limits.h>
22 #include <stdio.h>
23 #include <string.h>
24
25 #include "libpspp/assertion.h"
26 #include "libpspp/compiler.h"
27 #include "libpspp/string-map.h"
28 #include "output/ascii.h"
29 #include "output/driver.h"
30 #include "output/tab.h"
31 #include "output/table-item.h"
32
33 #include "gl/error.h"
34 #include "gl/progname.h"
35 #include "gl/xalloc.h"
36 #include "gl/xvasprintf.h"
37
38 /* --transpose: Transpose the table before outputting? */
39 static int transpose;
40
41 /* --emphasis: ASCII driver emphasis option. */
42 static char *emphasis;
43
44 /* --box: ASCII driver box option. */
45 static char *box;
46
47 /* --draw-mode: special ASCII driver test mode. */
48 static int draw_mode;
49
50 /* --no-txt: Whether to render to <base>.txt. */
51 static int render_txt = true;
52
53 /* --no-stdout: Whether to render to stdout. */
54 static int render_stdout = true;
55
56 /* --pdf: Also render PDF output. */
57 static int render_pdf;
58
59 /* ASCII driver, for ASCII driver test mode. */
60 static struct output_driver *ascii_driver;
61
62 /* -o, --output: Base name for output files. */
63 static const char *output_base = "render";
64
65 static const char *parse_options (int argc, char **argv);
66 static void usage (void) NO_RETURN;
67 static struct table *read_table (FILE *, struct table **tables, size_t n_tables);
68 static void draw (FILE *);
69
70 int
71 main (int argc, char **argv)
72 {
73   const char *input_file_name;
74   FILE *input;
75
76   set_program_name (argv[0]);
77   input_file_name = parse_options (argc, argv);
78
79   if (!strcmp (input_file_name, "-"))
80     input = stdin;
81   else
82     {
83       input = fopen (input_file_name, "r");
84       if (input == NULL)
85         error (1, errno, "%s: open failed", input_file_name);
86     }
87
88   if (!draw_mode)
89     {
90       struct table **tables = NULL;
91       size_t allocated_tables = 0;
92       size_t n_tables = 0;
93       struct table *table;
94
95       for (;;)
96         {
97           int ch;
98
99           if (n_tables >= allocated_tables)
100             tables = x2nrealloc (tables, &allocated_tables, sizeof *tables);
101
102           tables[n_tables] = read_table (input, tables, n_tables);
103           n_tables++;
104
105           ch = getc (input);
106           if (ch == EOF)
107             break;
108           ungetc (ch, input);
109         }
110
111       table = tables[n_tables - 1];
112       if (transpose)
113         table = table_transpose (table);
114       table_item_submit (table_item_create (table, NULL));
115     }
116   else
117     draw (input);
118
119   if (input != stdin)
120     fclose (input);
121
122   output_close ();
123
124   return 0;
125 }
126
127 static void
128 configure_drivers (int width, int length, int min_break)
129 {
130   struct string_map options, tmp;
131   struct output_driver *driver;
132
133   string_map_init (&options);
134   string_map_insert (&options, "format", "txt");
135   string_map_insert (&options, "output-file", "-");
136   string_map_insert_nocopy (&options, xstrdup ("width"),
137                             xasprintf ("%d", width));
138   string_map_insert_nocopy (&options, xstrdup ("length"),
139                             xasprintf ("%d", length));
140   if (min_break >= 0)
141     {
142       string_map_insert_nocopy (&options, xstrdup ("min-hbreak"),
143                                 xasprintf ("%d", min_break));
144       string_map_insert_nocopy (&options, xstrdup ("min-vbreak"),
145                                 xasprintf ("%d", min_break));
146     }
147   if (emphasis != NULL)
148     string_map_insert (&options, "emphasis", emphasis);
149   if (box != NULL)
150     string_map_insert (&options, "box", box);
151
152   /* Render to stdout. */
153   if (render_stdout)
154     {
155       string_map_clone (&tmp, &options);
156       ascii_driver = driver = output_driver_create (&tmp);
157       if (driver == NULL)
158         exit (EXIT_FAILURE);
159       output_driver_register (driver);
160       string_map_destroy (&tmp);
161     }
162
163   if (draw_mode)
164    {
165      string_map_destroy (&options);
166      return;
167    }
168
169   /* Render to <base>.txt. */
170   if (render_txt)
171     {
172       string_map_clear (&options);
173       string_map_insert_nocopy (&options, xstrdup ("output-file"),
174                                 xasprintf ("%s.txt", output_base));
175       driver = output_driver_create (&options);
176       if (driver == NULL)
177         exit (EXIT_FAILURE);
178       output_driver_register (driver);
179     }
180
181 #ifdef HAVE_CAIRO
182   /* Render to <base>.pdf. */
183   if (render_pdf)
184     {
185       string_map_clear (&options);
186       string_map_insert_nocopy (&options, xstrdup ("output-file"),
187                                  xasprintf ("%s.pdf", output_base));
188       string_map_insert (&options, "top-margin", "0");
189       string_map_insert (&options, "bottom-margin", "0");
190       string_map_insert (&options, "left-margin", "0");
191       string_map_insert (&options, "right-margin", "0");
192       string_map_insert_nocopy (&options, xstrdup ("paper-size"),
193                                 xasprintf ("%dx%dpt", width * 5, length * 8));
194       if (min_break >= 0)
195         {
196           string_map_insert_nocopy (&options, xstrdup ("min-hbreak"),
197                                     xasprintf ("%d", min_break * 5));
198           string_map_insert_nocopy (&options, xstrdup ("min-vbreak"),
199                                     xasprintf ("%d", min_break * 8));
200         }
201       driver = output_driver_create (&options);
202       if (driver == NULL)
203         exit (EXIT_FAILURE);
204       output_driver_register (driver);
205     }
206 #endif
207
208   /* Render to <base>.odt. */
209   string_map_replace_nocopy (&options, xstrdup ("output-file"),
210                              xasprintf ("%s.odt", output_base));
211   driver = output_driver_create (&options);
212   if (driver == NULL)
213     exit (EXIT_FAILURE);
214   output_driver_register (driver);
215
216   string_map_destroy (&options);
217 }
218
219 static const char *
220 parse_options (int argc, char **argv)
221 {
222   int width = 79;
223   int length = 66;
224   int min_break = -1;
225
226   for (;;)
227     {
228       enum {
229         OPT_WIDTH = UCHAR_MAX + 1,
230         OPT_LENGTH,
231         OPT_MIN_BREAK,
232         OPT_EMPHASIS,
233         OPT_BOX,
234         OPT_HELP
235       };
236       static const struct option options[] =
237         {
238           {"width", required_argument, NULL, OPT_WIDTH},
239           {"length", required_argument, NULL, OPT_LENGTH},
240           {"min-break", required_argument, NULL, OPT_MIN_BREAK},
241           {"transpose", no_argument, &transpose, 1},
242           {"emphasis", required_argument, NULL, OPT_EMPHASIS},
243           {"box", required_argument, NULL, OPT_BOX},
244           {"draw-mode", no_argument, &draw_mode, 1},
245           {"no-txt", no_argument, &render_txt, 0},
246           {"no-stdout", no_argument, &render_stdout, 0},
247           {"pdf", no_argument, &render_pdf, 1},
248           {"output", required_argument, NULL, 'o'},
249           {"help", no_argument, NULL, OPT_HELP},
250           {NULL, 0, NULL, 0},
251         };
252
253       int c = getopt_long (argc, argv, "o:", options, NULL);
254       if (c == -1)
255         break;
256
257       switch (c)
258         {
259         case OPT_WIDTH:
260           width = atoi (optarg);
261           break;
262
263         case OPT_LENGTH:
264           length = atoi (optarg);
265           break;
266
267         case OPT_MIN_BREAK:
268           min_break = atoi (optarg);
269           break;
270
271         case OPT_EMPHASIS:
272           emphasis = optarg;
273           break;
274
275         case OPT_BOX:
276           box = optarg;
277           break;
278
279         case 'o':
280           output_base = optarg;
281           break;
282
283         case OPT_HELP:
284           usage ();
285
286         case 0:
287           break;
288
289         case '?':
290           exit(EXIT_FAILURE);
291           break;
292
293         default:
294           NOT_REACHED ();
295         }
296
297     }
298
299   configure_drivers (width, length, min_break);
300
301   if (optind + 1 != argc)
302     error (1, 0, "exactly one non-option argument required; "
303            "use --help for help");
304   return argv[optind];
305 }
306
307 static void
308 usage (void)
309 {
310   printf ("%s, to test rendering of PSPP tables\n"
311           "usage: %s [OPTIONS] INPUT\n"
312           "\nOptions:\n"
313           "  --width=WIDTH   set page width in characters\n"
314           "  --length=LINE   set page length in lines\n",
315           program_name, program_name);
316   exit (EXIT_SUCCESS);
317 }
318
319 static void
320 replace_newlines (char *p)
321 {
322   char *q;
323
324   for (q = p; *p != '\0'; )
325     if (*p == '\\' && p[1] == 'n')
326       {
327         *q++ = '\n';
328         p += 2;
329       }
330     else
331       *q++ = *p++;
332   *q = '\0';
333 }
334
335 static struct table *
336 read_table (FILE *stream, struct table **tables, size_t n_tables)
337 {
338   struct tab_table *tab;
339   char buffer[1024];
340   int input[6];
341   int n_input = 0;
342   int nr, nc, hl, hr, ht, hb;
343   int r, c;
344
345   if (fgets (buffer, sizeof buffer, stream) == NULL
346       || (n_input = sscanf (buffer, "%d %d %d %d %d %d",
347                             &input[0], &input[1], &input[2],
348                             &input[3], &input[4], &input[5])) < 2)
349     error (1, 0, "syntax error reading row and column count");
350
351   nr = input[0];
352   nc = input[1];
353   hl = n_input >= 3 ? input[2] : 0;
354   hr = n_input >= 4 ? input[3] : 0;
355   ht = n_input >= 5 ? input[4] : 0;
356   hb = n_input >= 6 ? input[5] : 0;
357
358   tab = tab_create (nc, nr);
359   tab_headers (tab, hl, hr, ht, hb);
360   for (r = 0; r < nr; r++)
361     for (c = 0; c < nc; c++)
362       if (tab_cell_is_empty (tab, c, r))
363         {
364           unsigned int opt;
365           char *new_line;
366           unsigned int i;
367           char *text;
368           int rs, cs;
369
370           if (fgets (buffer, sizeof buffer, stream) == NULL)
371             error (1, 0, "unexpected end of input reading row %d, column %d",
372                    r, c);
373           new_line = strchr (buffer, '\n');
374           if (new_line != NULL)
375             *new_line = '\0';
376
377           text = buffer;
378           if (sscanf (text, "%d*%d", &rs, &cs) == 2)
379             {
380               while (*text != ' ' && *text != '\0')
381                 text++;
382               if (*text == ' ')
383                 text++;
384             }
385           else
386             {
387               rs = 1;
388               cs = 1;
389             }
390
391           opt = 0;
392           while (*text && strchr ("<>^,@()|", *text))
393             switch (*text++)
394               {
395               case '<':
396                 tab_vline (tab, TAL_1, c, r, r + rs - 1);
397                 break;
398
399               case '>':
400                 tab_vline (tab, TAL_1, c + cs, r, r + rs - 1);
401                 break;
402
403               case '^':
404                 tab_hline (tab, TAL_1, c, c + cs - 1, r);
405                 break;
406
407               case ',':
408                 tab_hline (tab, TAL_1, c, c + cs - 1, r + rs);
409                 break;
410
411               case '@':
412                 tab_box (tab, TAL_1, TAL_1, -1, -1, c, r,
413                          c + cs - 1, r + rs - 1);
414                 break;
415
416               case '(':
417                 opt &= ~TAB_ALIGNMENT;
418                 opt |= TAB_LEFT;
419                 break;
420
421               case ')':
422                 opt &= ~TAB_ALIGNMENT;
423                 opt |= TAB_RIGHT;
424                 break;
425
426               case '|':
427                 opt &= ~TAB_ALIGNMENT;
428                 opt |= TAB_CENTER;
429                 break;
430
431               default:
432                 NOT_REACHED ();
433               }
434
435           replace_newlines (text);
436
437           if (sscanf (text, "{%u}", &i) == 1)
438             {
439               struct table *table;
440
441               if (i >= n_tables)
442                 error (1, 0, "bad table number %u", i);
443               table = table_ref (tables[i]);
444
445               text = strchr (text, '}') + 1;
446               while (*text)
447                 switch (*text++)
448                   {
449                   case 's':
450                     table = table_stomp (table);
451                     break;
452
453                   case 't':
454                     table = table_transpose (table);
455                     break;
456
457                   default:
458                     error (1, 0, "unexpected subtable modifier \"%c\"", *text);
459                   }
460               tab_subtable (tab, c, r, c + cs - 1, r + rs - 1, opt, table);
461             }
462           else
463             tab_joint_text (tab, c, r, c + cs - 1, r + rs - 1, opt, text);
464         }
465
466   return &tab->table;
467 }
468
469 static void
470 draw (FILE *stream)
471 {
472   char buffer[1024];
473   int line = 0;
474
475   while (fgets (buffer, sizeof buffer, stream))
476     {
477       char text[sizeof buffer];
478       int length;
479       int emph;
480       int x, y;
481
482       line++;
483       if (strchr ("#\r\n", buffer[0]))
484         continue;
485
486       if (sscanf (buffer, "%d %d %d %[^\n]", &x, &y, &emph, text) == 4)
487         ascii_test_write (ascii_driver, text, x, y, emph ? TAB_EMPH : 0);
488       else if (sscanf (buffer, "set-length %d %d", &y, &length) == 2)
489         ascii_test_set_length (ascii_driver, y, length);
490       else
491         error (1, 0, "line %d has invalid format", line);
492     }
493 }