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