b9ddd3e7f9491976a85c4dcfbeb9d1c821ed4a02
[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/i18n.h"
29 #include "libpspp/string-map.h"
30 #include "output/ascii.h"
31 #include "output/driver.h"
32 #include "output/table.h"
33 #include "output/table-item.h"
34
35 #include "gl/error.h"
36 #include "gl/progname.h"
37 #include "gl/xalloc.h"
38 #include "gl/xvasprintf.h"
39
40 /* --emphasis: ASCII driver emphasis option. */
41 static bool bold;
42 static bool underline;
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 *);
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   i18n_init ();
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       table_item_submit (table_item_create (table, NULL, NULL));
118       free (tables);
119     }
120   else
121     draw (input);
122
123   if (input != stdin)
124     fclose (input);
125
126   output_engine_pop ();
127   fh_done ();
128
129   return 0;
130 }
131
132 static void
133 configure_drivers (int width, int length, int min_break)
134 {
135   struct string_map options, tmp;
136   struct output_driver *driver;
137
138   string_map_init (&options);
139   string_map_insert (&options, "format", "txt");
140   string_map_insert (&options, "output-file", "-");
141   string_map_insert_nocopy (&options, xstrdup ("width"),
142                             xasprintf ("%d", width));
143   if (min_break >= 0)
144     string_map_insert_nocopy (&options, xstrdup ("min-hbreak"),
145                               xasprintf ("%d", min_break));
146   if (bold || underline)
147     string_map_insert (&options, "emphasis", "true");
148   if (box != NULL)
149     string_map_insert (&options, "box", box);
150
151   /* Render to stdout. */
152   if (render_stdout)
153     {
154       string_map_clone (&tmp, &options);
155       ascii_driver = driver = output_driver_create (&tmp);
156       if (driver == NULL)
157         exit (EXIT_FAILURE);
158       output_driver_register (driver);
159       string_map_destroy (&tmp);
160     }
161
162   if (draw_mode)
163    {
164      string_map_destroy (&options);
165      return;
166    }
167
168   /* Render to <base>.txt. */
169   if (render_txt)
170     {
171       string_map_clear (&options);
172       string_map_insert_nocopy (&options, xstrdup ("output-file"),
173                                 xasprintf ("%s.txt", output_base));
174       driver = output_driver_create (&options);
175       if (driver == NULL)
176         exit (EXIT_FAILURE);
177       output_driver_register (driver);
178     }
179
180 #ifdef HAVE_CAIRO
181   /* Render to <base>.pdf. */
182   if (render_pdf)
183     {
184       string_map_clear (&options);
185       string_map_insert_nocopy (&options, xstrdup ("output-file"),
186                                  xasprintf ("%s.pdf", output_base));
187       string_map_insert (&options, "top-margin", "0");
188       string_map_insert (&options, "bottom-margin", "0");
189       string_map_insert (&options, "left-margin", "0");
190       string_map_insert (&options, "right-margin", "0");
191       string_map_insert_nocopy (&options, xstrdup ("paper-size"),
192                                 xasprintf ("%dx%dpt", width * 5, length * 8));
193       if (min_break >= 0)
194         {
195           string_map_insert_nocopy (&options, xstrdup ("min-hbreak"),
196                                     xasprintf ("%d", min_break * 5));
197           string_map_insert_nocopy (&options, xstrdup ("min-vbreak"),
198                                     xasprintf ("%d", min_break * 8));
199         }
200       driver = output_driver_create (&options);
201       if (driver == NULL)
202         exit (EXIT_FAILURE);
203       output_driver_register (driver);
204     }
205 #endif
206
207   /* Render to <base>.csv. */
208   if (render_csv)
209     {
210       string_map_clear (&options);
211       string_map_insert_nocopy (&options, xstrdup ("output-file"),
212                                  xasprintf ("%s.csv", output_base));
213       driver = output_driver_create (&options);
214       if (driver == NULL)
215         exit (EXIT_FAILURE);
216       output_driver_register (driver);
217     }
218
219   /* Render to <base>.odt. */
220   string_map_replace_nocopy (&options, xstrdup ("output-file"),
221                              xasprintf ("%s.odt", output_base));
222   driver = output_driver_create (&options);
223   if (driver == NULL)
224     exit (EXIT_FAILURE);
225   output_driver_register (driver);
226
227   string_map_destroy (&options);
228 }
229
230 static const char *
231 parse_options (int argc, char **argv)
232 {
233   int width = 79;
234   int length = 66;
235   int min_break = -1;
236
237   for (;;)
238     {
239       enum {
240         OPT_WIDTH = UCHAR_MAX + 1,
241         OPT_LENGTH,
242         OPT_MIN_BREAK,
243         OPT_EMPHASIS,
244         OPT_BOX,
245         OPT_HELP
246       };
247       static const struct option options[] =
248         {
249           {"width", required_argument, NULL, OPT_WIDTH},
250           {"length", required_argument, NULL, OPT_LENGTH},
251           {"min-break", required_argument, NULL, OPT_MIN_BREAK},
252           {"emphasis", required_argument, NULL, OPT_EMPHASIS},
253           {"box", required_argument, NULL, OPT_BOX},
254           {"draw-mode", no_argument, &draw_mode, 1},
255           {"no-txt", no_argument, &render_txt, 0},
256           {"no-stdout", no_argument, &render_stdout, 0},
257           {"pdf", no_argument, &render_pdf, 1},
258           {"csv", no_argument, &render_csv, 1},
259           {"output", required_argument, NULL, 'o'},
260           {"help", no_argument, NULL, OPT_HELP},
261           {NULL, 0, NULL, 0},
262         };
263
264       int c = getopt_long (argc, argv, "o:", options, NULL);
265       if (c == -1)
266         break;
267
268       switch (c)
269         {
270         case OPT_WIDTH:
271           width = atoi (optarg);
272           break;
273
274         case OPT_LENGTH:
275           length = atoi (optarg);
276           break;
277
278         case OPT_MIN_BREAK:
279           min_break = atoi (optarg);
280           break;
281
282         case OPT_EMPHASIS:
283           if (!strcmp (optarg, "bold"))
284             {
285               bold = true;
286               underline = false;
287             }
288           else if (!strcmp (optarg, "underline"))
289             {
290               bold = false;
291               underline = true;
292             }
293           else if (!strcmp (optarg, "none"))
294             {
295               bold = underline = false;
296             }
297           else
298             error (1, 0, "argument to --emphasis must be \"bold\" or "
299                    "\"underline\" or \"none\"");
300           break;
301
302         case OPT_BOX:
303           box = optarg;
304           break;
305
306         case 'o':
307           output_base = optarg;
308           break;
309
310         case OPT_HELP:
311           usage ();
312
313         case 0:
314           break;
315
316         case '?':
317           exit(EXIT_FAILURE);
318           break;
319
320         default:
321           NOT_REACHED ();
322         }
323
324     }
325
326   configure_drivers (width, length, min_break);
327
328   if (optind + 1 != argc)
329     error (1, 0, "exactly one non-option argument required; "
330            "use --help for help");
331   return argv[optind];
332 }
333
334 static void
335 usage (void)
336 {
337   printf ("%s, to test rendering of PSPP tables\n"
338           "usage: %s [OPTIONS] INPUT\n"
339           "\nOptions:\n"
340           "  --width=WIDTH   set page width in characters\n"
341           "  --length=LINE   set page length in lines\n",
342           program_name, program_name);
343   exit (EXIT_SUCCESS);
344 }
345
346 static void
347 replace_newlines (char *p)
348 {
349   char *q;
350
351   for (q = p; *p != '\0';)
352     if (*p == '\\' && p[1] == 'n')
353       {
354         *q++ = '\n';
355         p += 2;
356       }
357     else
358       *q++ = *p++;
359   *q = '\0';
360 }
361
362 static struct table *
363 read_table (FILE *stream)
364 {
365   struct table *tab;
366   char buffer[1024];
367   int input[6];
368   int n_input = 0;
369   int nr, nc, hl, hr, ht, hb;
370   int r, c;
371   size_t n_footnotes = 0;
372
373   if (fgets (buffer, sizeof buffer, stream) == NULL
374       || (n_input = sscanf (buffer, "%d %d %d %d %d %d",
375                             &input[0], &input[1], &input[2],
376                             &input[3], &input[4], &input[5])) < 2)
377     error (1, 0, "syntax error reading row and column count");
378
379   nr = input[0];
380   nc = input[1];
381   hl = n_input >= 3 ? input[2] : 0;
382   hr = n_input >= 4 ? input[3] : 0;
383   ht = n_input >= 5 ? input[4] : 0;
384   hb = n_input >= 6 ? input[5] : 0;
385
386   tab = table_create (nc, nr, hl, hr, ht, hb);
387   for (r = 0; r < nr; r++)
388     for (c = 0; c < nc; c++)
389       if (table_cell_is_empty (tab, c, r))
390         {
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 #define S(H) { AREA_STYLE_INITIALIZER__, .cell_style.halign = H }
417           static const struct area_style left_style = S (TABLE_HALIGN_LEFT);
418           static const struct area_style right_style = S (TABLE_HALIGN_RIGHT);
419           static const struct area_style center_style
420             = S (TABLE_HALIGN_CENTER);
421
422           const struct area_style *style = &right_style;
423           while (*text && strchr ("<>^,@()|", *text))
424             switch (*text++)
425               {
426               case '<':
427                 table_vline (tab, TABLE_STROKE_SOLID, c, r, r + rs - 1);
428                 break;
429
430               case '>':
431                 table_vline (tab, TABLE_STROKE_SOLID, c + cs, r, r + rs - 1);
432                 break;
433
434               case '^':
435                 table_hline (tab, TABLE_STROKE_SOLID, c, c + cs - 1, r);
436                 break;
437
438               case ',':
439                 table_hline (tab, TABLE_STROKE_SOLID, c, c + cs - 1, r + rs);
440                 break;
441
442               case '@':
443                 table_box (tab, TABLE_STROKE_SOLID, TABLE_STROKE_SOLID,
444                            -1, -1, c, r, c + cs - 1, r + rs - 1);
445                 break;
446
447               case '(':
448                 style = &left_style;
449                 break;
450
451               case ')':
452                 style = &right_style;
453                 break;
454
455               case '|':
456                 style = &center_style;
457                 break;
458
459               default:
460                 NOT_REACHED ();
461               }
462
463           replace_newlines (text);
464
465           char *pos = text;
466           char *content;
467           int i;
468
469           for (i = 0; (content = strsep (&pos, "#")) != NULL; i++)
470             if (!i)
471               {
472                 table_joint_text (tab, c, r, c + cs - 1, r + rs - 1, 0,
473                                   content);
474                 table_add_style (tab, c, r, style);
475               }
476             else
477               {
478                 char marker[2] = { 'a' + n_footnotes, '\0' };
479                 struct footnote *f = table_create_footnote (
480                   tab, n_footnotes, content, marker,
481                   area_style_clone (tab->container, &left_style));
482                 table_add_footnote (tab, c, r, f);
483                 n_footnotes++;
484               }
485         }
486
487   return tab;
488 }
489
490 static void
491 draw (FILE *stream)
492 {
493   char buffer[1024];
494   int line = 0;
495
496   while (fgets (buffer, sizeof buffer, stream))
497     {
498       char text[sizeof buffer];
499       int length;
500       int emph;
501       int x, y;
502
503       line++;
504       if (strchr ("#\r\n", buffer[0]))
505         continue;
506
507       if (sscanf (buffer, "%d %d %d %[^\n]", &x, &y, &emph, text) == 4)
508         ascii_test_write (ascii_driver, text, x, y, emph ? bold : false,
509                           emph ? underline : false);
510       else if (sscanf (buffer, "set-length %d %d", &y, &length) == 2)
511         ascii_test_set_length (ascii_driver, y, length);
512       else
513         error (1, 0, "line %d has invalid format", line);
514     }
515   ascii_test_flush (ascii_driver);
516 }