output: Add footnote support.
[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 /* --csv: Also render CSV output. */
60 static int render_csv;
61
62 /* ASCII driver, for ASCII driver test mode. */
63 static struct output_driver *ascii_driver;
64
65 /* -o, --output: Base name for output files. */
66 static const char *output_base = "render";
67
68 static const char *parse_options (int argc, char **argv);
69 static void usage (void) NO_RETURN;
70 static struct table *read_table (FILE *, struct table **tables, size_t n_tables);
71 static void draw (FILE *);
72
73 int
74 main (int argc, char **argv)
75 {
76   const char *input_file_name;
77   FILE *input;
78
79   set_program_name (argv[0]);
80   output_engine_push ();
81   input_file_name = parse_options (argc, argv);
82
83   if (!strcmp (input_file_name, "-"))
84     input = stdin;
85   else
86     {
87       input = fopen (input_file_name, "r");
88       if (input == NULL)
89         error (1, errno, "%s: open failed", input_file_name);
90     }
91
92   if (!draw_mode)
93     {
94       struct table **tables = NULL;
95       size_t allocated_tables = 0;
96       size_t n_tables = 0;
97       struct table *table;
98
99       for (;;)
100         {
101           int ch;
102
103           if (n_tables >= allocated_tables)
104             tables = x2nrealloc (tables, &allocated_tables, sizeof *tables);
105
106           tables[n_tables] = read_table (input, tables, n_tables);
107           n_tables++;
108
109           ch = getc (input);
110           if (ch == EOF)
111             break;
112           ungetc (ch, input);
113         }
114
115       table = tables[n_tables - 1];
116       if (transpose)
117         table = table_transpose (table);
118       table_item_submit (table_item_create (table, NULL));
119     }
120   else
121     draw (input);
122
123   if (input != stdin)
124     fclose (input);
125
126   output_engine_pop ();
127
128   return 0;
129 }
130
131 static void
132 configure_drivers (int width, int length, int min_break)
133 {
134   struct string_map options, tmp;
135   struct output_driver *driver;
136
137   string_map_init (&options);
138   string_map_insert (&options, "format", "txt");
139   string_map_insert (&options, "output-file", "-");
140   string_map_insert_nocopy (&options, xstrdup ("width"),
141                             xasprintf ("%d", width));
142   string_map_insert_nocopy (&options, xstrdup ("length"),
143                             xasprintf ("%d", length));
144   if (min_break >= 0)
145     {
146       string_map_insert_nocopy (&options, xstrdup ("min-hbreak"),
147                                 xasprintf ("%d", min_break));
148       string_map_insert_nocopy (&options, xstrdup ("min-vbreak"),
149                                 xasprintf ("%d", min_break));
150     }
151   if (emphasis != NULL)
152     string_map_insert (&options, "emphasis", emphasis);
153   if (box != NULL)
154     string_map_insert (&options, "box", box);
155
156   /* Render to stdout. */
157   if (render_stdout)
158     {
159       string_map_clone (&tmp, &options);
160       ascii_driver = driver = output_driver_create (&tmp);
161       if (driver == NULL)
162         exit (EXIT_FAILURE);
163       output_driver_register (driver);
164       string_map_destroy (&tmp);
165     }
166
167   if (draw_mode)
168    {
169      string_map_destroy (&options);
170      return;
171    }
172
173   /* Render to <base>.txt. */
174   if (render_txt)
175     {
176       string_map_clear (&options);
177       string_map_insert_nocopy (&options, xstrdup ("output-file"),
178                                 xasprintf ("%s.txt", output_base));
179       driver = output_driver_create (&options);
180       if (driver == NULL)
181         exit (EXIT_FAILURE);
182       output_driver_register (driver);
183     }
184
185 #ifdef HAVE_CAIRO
186   /* Render to <base>.pdf. */
187   if (render_pdf)
188     {
189       string_map_clear (&options);
190       string_map_insert_nocopy (&options, xstrdup ("output-file"),
191                                  xasprintf ("%s.pdf", output_base));
192       string_map_insert (&options, "top-margin", "0");
193       string_map_insert (&options, "bottom-margin", "0");
194       string_map_insert (&options, "left-margin", "0");
195       string_map_insert (&options, "right-margin", "0");
196       string_map_insert_nocopy (&options, xstrdup ("paper-size"),
197                                 xasprintf ("%dx%dpt", width * 5, length * 8));
198       if (min_break >= 0)
199         {
200           string_map_insert_nocopy (&options, xstrdup ("min-hbreak"),
201                                     xasprintf ("%d", min_break * 5));
202           string_map_insert_nocopy (&options, xstrdup ("min-vbreak"),
203                                     xasprintf ("%d", min_break * 8));
204         }
205       driver = output_driver_create (&options);
206       if (driver == NULL)
207         exit (EXIT_FAILURE);
208       output_driver_register (driver);
209     }
210 #endif
211
212   /* Render to <base>.csv. */
213   if (render_csv)
214     {
215       string_map_clear (&options);
216       string_map_insert_nocopy (&options, xstrdup ("output-file"),
217                                  xasprintf ("%s.csv", output_base));
218       driver = output_driver_create (&options);
219       if (driver == NULL)
220         exit (EXIT_FAILURE);
221       output_driver_register (driver);
222     }
223
224   /* Render to <base>.odt. */
225   string_map_replace_nocopy (&options, xstrdup ("output-file"),
226                              xasprintf ("%s.odt", output_base));
227   driver = output_driver_create (&options);
228   if (driver == NULL)
229     exit (EXIT_FAILURE);
230   output_driver_register (driver);
231
232   string_map_destroy (&options);
233 }
234
235 static const char *
236 parse_options (int argc, char **argv)
237 {
238   int width = 79;
239   int length = 66;
240   int min_break = -1;
241
242   for (;;)
243     {
244       enum {
245         OPT_WIDTH = UCHAR_MAX + 1,
246         OPT_LENGTH,
247         OPT_MIN_BREAK,
248         OPT_EMPHASIS,
249         OPT_BOX,
250         OPT_HELP
251       };
252       static const struct option options[] =
253         {
254           {"width", required_argument, NULL, OPT_WIDTH},
255           {"length", required_argument, NULL, OPT_LENGTH},
256           {"min-break", required_argument, NULL, OPT_MIN_BREAK},
257           {"transpose", no_argument, &transpose, 1},
258           {"emphasis", required_argument, NULL, OPT_EMPHASIS},
259           {"box", required_argument, NULL, OPT_BOX},
260           {"draw-mode", no_argument, &draw_mode, 1},
261           {"no-txt", no_argument, &render_txt, 0},
262           {"no-stdout", no_argument, &render_stdout, 0},
263           {"pdf", no_argument, &render_pdf, 1},
264           {"csv", no_argument, &render_csv, 1},
265           {"output", required_argument, NULL, 'o'},
266           {"help", no_argument, NULL, OPT_HELP},
267           {NULL, 0, NULL, 0},
268         };
269
270       int c = getopt_long (argc, argv, "o:", options, NULL);
271       if (c == -1)
272         break;
273
274       switch (c)
275         {
276         case OPT_WIDTH:
277           width = atoi (optarg);
278           break;
279
280         case OPT_LENGTH:
281           length = atoi (optarg);
282           break;
283
284         case OPT_MIN_BREAK:
285           min_break = atoi (optarg);
286           break;
287
288         case OPT_EMPHASIS:
289           emphasis = optarg;
290           break;
291
292         case OPT_BOX:
293           box = optarg;
294           break;
295
296         case 'o':
297           output_base = optarg;
298           break;
299
300         case OPT_HELP:
301           usage ();
302
303         case 0:
304           break;
305
306         case '?':
307           exit(EXIT_FAILURE);
308           break;
309
310         default:
311           NOT_REACHED ();
312         }
313
314     }
315
316   configure_drivers (width, length, min_break);
317
318   if (optind + 1 != argc)
319     error (1, 0, "exactly one non-option argument required; "
320            "use --help for help");
321   return argv[optind];
322 }
323
324 static void
325 usage (void)
326 {
327   printf ("%s, to test rendering of PSPP tables\n"
328           "usage: %s [OPTIONS] INPUT\n"
329           "\nOptions:\n"
330           "  --width=WIDTH   set page width in characters\n"
331           "  --length=LINE   set page length in lines\n",
332           program_name, program_name);
333   exit (EXIT_SUCCESS);
334 }
335
336 static void
337 replace_newlines (char *p)
338 {
339   char *q;
340
341   for (q = p; *p != '\0'; )
342     if (*p == '\\' && p[1] == 'n')
343       {
344         *q++ = '\n';
345         p += 2;
346       }
347     else
348       *q++ = *p++;
349   *q = '\0';
350 }
351
352 static struct table *
353 read_table (FILE *stream, struct table **tables, size_t n_tables)
354 {
355   struct tab_table *tab;
356   char buffer[1024];
357   int input[6];
358   int n_input = 0;
359   int nr, nc, hl, hr, ht, hb;
360   int r, c;
361
362   if (fgets (buffer, sizeof buffer, stream) == NULL
363       || (n_input = sscanf (buffer, "%d %d %d %d %d %d",
364                             &input[0], &input[1], &input[2],
365                             &input[3], &input[4], &input[5])) < 2)
366     error (1, 0, "syntax error reading row and column count");
367
368   nr = input[0];
369   nc = input[1];
370   hl = n_input >= 3 ? input[2] : 0;
371   hr = n_input >= 4 ? input[3] : 0;
372   ht = n_input >= 5 ? input[4] : 0;
373   hb = n_input >= 6 ? input[5] : 0;
374
375   tab = tab_create (nc, nr);
376   tab_headers (tab, hl, hr, ht, hb);
377   for (r = 0; r < nr; r++)
378     for (c = 0; c < nc; c++)
379       if (tab_cell_is_empty (tab, c, r))
380         {
381           unsigned int opt;
382           char *new_line;
383           unsigned int i;
384           char *text;
385           int rs, cs;
386
387           if (fgets (buffer, sizeof buffer, stream) == NULL)
388             error (1, 0, "unexpected end of input reading row %d, column %d",
389                    r, c);
390           new_line = strchr (buffer, '\n');
391           if (new_line != NULL)
392             *new_line = '\0';
393
394           text = buffer;
395           if (sscanf (text, "%d*%d", &rs, &cs) == 2)
396             {
397               while (*text != ' ' && *text != '\0')
398                 text++;
399               if (*text == ' ')
400                 text++;
401             }
402           else
403             {
404               rs = 1;
405               cs = 1;
406             }
407
408           opt = 0;
409           while (*text && strchr ("<>^,@()|", *text))
410             switch (*text++)
411               {
412               case '<':
413                 tab_vline (tab, TAL_1, c, r, r + rs - 1);
414                 break;
415
416               case '>':
417                 tab_vline (tab, TAL_1, c + cs, r, r + rs - 1);
418                 break;
419
420               case '^':
421                 tab_hline (tab, TAL_1, c, c + cs - 1, r);
422                 break;
423
424               case ',':
425                 tab_hline (tab, TAL_1, c, c + cs - 1, r + rs);
426                 break;
427
428               case '@':
429                 tab_box (tab, TAL_1, TAL_1, -1, -1, c, r,
430                          c + cs - 1, r + rs - 1);
431                 break;
432
433               case '(':
434                 opt &= ~TAB_ALIGNMENT;
435                 opt |= TAB_LEFT;
436                 break;
437
438               case ')':
439                 opt &= ~TAB_ALIGNMENT;
440                 opt |= TAB_RIGHT;
441                 break;
442
443               case '|':
444                 opt &= ~TAB_ALIGNMENT;
445                 opt |= TAB_CENTER;
446                 break;
447
448               default:
449                 NOT_REACHED ();
450               }
451
452           replace_newlines (text);
453
454           if (sscanf (text, "{%u}", &i) == 1)
455             {
456               struct table *table;
457
458               if (i >= n_tables)
459                 error (1, 0, "bad table number %u", i);
460               table = table_ref (tables[i]);
461
462               text = strchr (text, '}') + 1;
463               while (*text)
464                 switch (*text++)
465                   {
466                   case 's':
467                     table = table_stomp (table);
468                     break;
469
470                   case 't':
471                     table = table_transpose (table);
472                     break;
473
474                   default:
475                     error (1, 0, "unexpected subtable modifier \"%c\"", *text);
476                   }
477               tab_subtable (tab, c, r, c + cs - 1, r + rs - 1, opt,
478                             table_item_create (table, NULL));
479             }
480           else
481             {
482               char *pos = text;
483               char *content;
484               int i;
485
486               for (i = 0; (content = strsep (&pos, "#")) != NULL; i++)
487                 if (!i)
488                   tab_joint_text (tab, c, r, c + cs - 1, r + rs - 1, opt,
489                                   content);
490                 else
491                   tab_footnote (tab, c, r, content);
492             }
493         }
494
495   return &tab->table;
496 }
497
498 static void
499 draw (FILE *stream)
500 {
501   char buffer[1024];
502   int line = 0;
503
504   while (fgets (buffer, sizeof buffer, stream))
505     {
506       char text[sizeof buffer];
507       int length;
508       int emph;
509       int x, y;
510
511       line++;
512       if (strchr ("#\r\n", buffer[0]))
513         continue;
514
515       if (sscanf (buffer, "%d %d %d %[^\n]", &x, &y, &emph, text) == 4)
516         ascii_test_write (ascii_driver, text, x, y, emph ? TAB_EMPH : 0);
517       else if (sscanf (buffer, "set-length %d %d", &y, &length) == 2)
518         ascii_test_set_length (ascii_driver, y, length);
519       else
520         error (1, 0, "line %d has invalid format", line);
521     }
522 }