Make minimum breaks configurable.
[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 /* --pdf: Also render PDF output. */
51 static int render_pdf;
52
53 /* ASCII driver, for ASCII driver test mode. */
54 static struct output_driver *ascii_driver;
55
56 static const char *parse_options (int argc, char **argv);
57 static void usage (void) NO_RETURN;
58 static struct table *read_table (FILE *);
59 static void draw (FILE *);
60
61 int
62 main (int argc, char **argv)
63 {
64   const char *input_file_name;
65   FILE *input;
66
67   set_program_name (argv[0]);
68   input_file_name = parse_options (argc, argv);
69
70   if (!strcmp (input_file_name, "-"))
71     input = stdin;
72   else
73     {
74       input = fopen (input_file_name, "r");
75       if (input == NULL)
76         error (1, errno, "%s: open failed", input_file_name);
77     }
78
79   if (!draw_mode)
80     {
81       struct table *table;
82
83       table = read_table (input);
84
85       if (transpose)
86         table = table_transpose (table);
87
88       table_item_submit (table_item_create (table, NULL));
89     }
90   else
91     draw (input);
92
93   if (input != stdin)
94     fclose (input);
95
96   output_close ();
97
98   return 0;
99 }
100
101 static void
102 configure_drivers (int width, int length, int min_break)
103 {
104   struct string_map options, tmp;
105   struct output_driver *driver;
106
107   string_map_init (&options);
108   string_map_insert (&options, "format", "txt");
109   string_map_insert (&options, "output-file", "-");
110   string_map_insert_nocopy (&options, xstrdup ("width"),
111                             xasprintf ("%d", width));
112   string_map_insert_nocopy (&options, xstrdup ("length"),
113                             xasprintf ("%d", length));
114   if (min_break >= 0)
115     {
116       string_map_insert_nocopy (&options, xstrdup ("min-hbreak"),
117                                 xasprintf ("%d", min_break));
118       string_map_insert_nocopy (&options, xstrdup ("min-vbreak"),
119                                 xasprintf ("%d", min_break));
120     }
121   if (emphasis != NULL)
122     string_map_insert (&options, "emphasis", emphasis);
123   if (box != NULL)
124     string_map_insert (&options, "box", box);
125
126   /* Render to stdout. */
127   string_map_clone (&tmp, &options);
128   ascii_driver = driver = output_driver_create (&tmp);
129   if (driver == NULL)
130     exit (EXIT_FAILURE);
131   output_driver_register (driver);
132   string_map_destroy (&tmp);
133
134   if (draw_mode)
135    {
136     string_map_destroy (&options);
137     return;
138    }
139
140   /* Render to render.txt. */
141   string_map_replace (&options, "output-file", "render.txt");
142   driver = output_driver_create (&options);
143   if (driver == NULL)
144     exit (EXIT_FAILURE);
145   output_driver_register (driver);
146
147 #ifdef HAVE_CAIRO
148   if (render_pdf)
149     {
150       string_map_insert (&options, "output-file", "render.pdf");
151       string_map_insert (&options, "top-margin", "0");
152       string_map_insert (&options, "bottom-margin", "0");
153       string_map_insert (&options, "left-margin", "0");
154       string_map_insert (&options, "right-margin", "0");
155       string_map_insert_nocopy (&options, xstrdup ("paper-size"),
156                                 xasprintf ("%dx%dpt", width * 5, length * 8));
157       if (min_break >= 0)
158         {
159           string_map_insert_nocopy (&options, xstrdup ("min-hbreak"),
160                                     xasprintf ("%d", min_break * 5));
161           string_map_insert_nocopy (&options, xstrdup ("min-vbreak"),
162                                     xasprintf ("%d", min_break * 8));
163         }
164       driver = output_driver_create (&options);
165       if (driver == NULL)
166         exit (EXIT_FAILURE);
167       output_driver_register (driver);
168     }
169 #endif
170
171   string_map_insert (&options, "output-file", "render.odt");
172   driver = output_driver_create (&options);
173   if (driver == NULL)
174     exit (EXIT_FAILURE);
175   output_driver_register (driver);
176
177   string_map_destroy (&options);
178 }
179
180 static const char *
181 parse_options (int argc, char **argv)
182 {
183   int width = 79;
184   int length = 66;
185   int min_break = -1;
186
187   for (;;)
188     {
189       enum {
190         OPT_WIDTH = UCHAR_MAX + 1,
191         OPT_LENGTH,
192         OPT_MIN_BREAK,
193         OPT_EMPHASIS,
194         OPT_BOX,
195         OPT_HELP
196       };
197       static const struct option options[] =
198         {
199           {"width", required_argument, NULL, OPT_WIDTH},
200           {"length", required_argument, NULL, OPT_LENGTH},
201           {"min-break", required_argument, NULL, OPT_MIN_BREAK},
202           {"transpose", no_argument, &transpose, 1},
203           {"emphasis", required_argument, NULL, OPT_EMPHASIS},
204           {"box", required_argument, NULL, OPT_BOX},
205           {"draw-mode", no_argument, &draw_mode, 1},
206           {"pdf", no_argument, &render_pdf, 1},
207           {"help", no_argument, NULL, OPT_HELP},
208           {NULL, 0, NULL, 0},
209         };
210
211       int c = getopt_long (argc, argv, "", options, NULL);
212       if (c == -1)
213         break;
214
215       switch (c)
216         {
217         case OPT_WIDTH:
218           width = atoi (optarg);
219           break;
220
221         case OPT_LENGTH:
222           length = atoi (optarg);
223           break;
224
225         case OPT_MIN_BREAK:
226           min_break = atoi (optarg);
227           break;
228
229         case OPT_EMPHASIS:
230           emphasis = optarg;
231           break;
232
233         case OPT_BOX:
234           box = optarg;
235           break;
236
237         case OPT_HELP:
238           usage ();
239
240         case 0:
241           break;
242
243         case '?':
244           exit(EXIT_FAILURE);
245           break;
246
247         default:
248           NOT_REACHED ();
249         }
250
251     }
252
253   configure_drivers (width, length, min_break);
254
255   if (optind + 1 != argc)
256     error (1, 0, "exactly one non-option argument required; "
257            "use --help for help");
258   return argv[optind];
259 }
260
261 static void
262 usage (void)
263 {
264   printf ("%s, to test rendering of PSPP tables\n"
265           "usage: %s [OPTIONS] INPUT\n"
266           "\nOptions:\n"
267           "  --width=WIDTH   set page width in characters\n"
268           "  --length=LINE   set page length in lines\n",
269           program_name, program_name);
270   exit (EXIT_SUCCESS);
271 }
272
273 static void
274 replace_newlines (char *p)
275 {
276   char *q;
277
278   for (q = p; *p != '\0'; )
279     if (*p == '\\' && p[1] == 'n')
280       {
281         *q++ = '\n';
282         p += 2;
283       }
284     else
285       *q++ = *p++;
286   *q = '\0';
287 }
288
289 static struct table *
290 read_table (FILE *stream)
291 {
292   struct tab_table *tab;
293   char buffer[1024];
294   int input[6];
295   int n_input = 0;
296   int nr, nc, hl, hr, ht, hb;
297   int r, c;
298
299   if (fgets (buffer, sizeof buffer, stream) == NULL
300       || (n_input = sscanf (buffer, "%d %d %d %d %d %d",
301                             &input[0], &input[1], &input[2],
302                             &input[3], &input[4], &input[5])) < 2)
303     error (1, 0, "syntax error reading row and column count");
304
305   nr = input[0];
306   nc = input[1];
307   hl = n_input >= 3 ? input[2] : 0;
308   hr = n_input >= 4 ? input[3] : 0;
309   ht = n_input >= 5 ? input[4] : 0;
310   hb = n_input >= 6 ? input[5] : 0;
311
312   tab = tab_create (nc, nr);
313   tab_headers (tab, hl, hr, ht, hb);
314   for (r = 0; r < nr; r++)
315     for (c = 0; c < nc; c++)
316       if (tab_cell_is_empty (tab, c, r))
317         {
318           char *new_line;
319           char *text;
320           int rs, cs;
321
322           if (fgets (buffer, sizeof buffer, stream) == NULL)
323             error (1, 0, "unexpected end of input reading row %d, column %d",
324                    r, c);
325           new_line = strchr (buffer, '\n');
326           if (new_line != NULL)
327             *new_line = '\0';
328
329           text = buffer;
330           if (sscanf (text, "%d*%d", &rs, &cs) == 2)
331             {
332               while (*text != ' ' && *text != '\0')
333                 text++;
334               if (*text == ' ')
335                 text++;
336             }
337           else
338             {
339               rs = 1;
340               cs = 1;
341             }
342
343           while (*text && strchr ("<>^,@", *text))
344             switch (*text++)
345               {
346               case '<':
347                 tab_vline (tab, TAL_1, c, r, r + rs - 1);
348                 break;
349
350               case '>':
351                 tab_vline (tab, TAL_1, c + cs, r, r + rs - 1);
352                 break;
353
354               case '^':
355                 tab_hline (tab, TAL_1, c, c + cs - 1, r);
356                 break;
357
358               case ',':
359                 tab_hline (tab, TAL_1, c, c + cs - 1, r + rs);
360                 break;
361
362               case '@':
363                 tab_box (tab, TAL_1, TAL_1, -1, -1, c, r,
364                          c + cs - 1, r + rs - 1);
365                 break;
366
367               default:
368                 NOT_REACHED ();
369               }
370
371           replace_newlines (text);
372
373           tab_joint_text (tab, c, r, c + cs - 1, r + rs - 1, 0, text);
374         }
375
376   if (getc (stream) != EOF)
377     error (1, 0, "unread data at end of input");
378
379   return &tab->table;
380 }
381
382 static void
383 draw (FILE *stream)
384 {
385   char buffer[1024];
386   int line = 0;
387
388   while (fgets (buffer, sizeof buffer, stream))
389     {
390       char text[sizeof buffer];
391       int length;
392       int emph;
393       int x, y;
394
395       line++;
396       if (strchr ("#\r\n", buffer[0]))
397         continue;
398
399       if (sscanf (buffer, "%d %d %d %[^\n]", &x, &y, &emph, text) == 4)
400         ascii_test_write (ascii_driver, text, x, y, emph ? TAB_EMPH : 0);
401       else if (sscanf (buffer, "set-length %d %d", &y, &length) == 2)
402         ascii_test_set_length (ascii_driver, y, length);
403       else
404         error (1, 0, "line %d has invalid format", line);
405     }
406 }