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