output: Make chart geometry the responsibility of the output driver.
[pspp] / src / output / html.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2009 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 #include "chart.h"
19 #include "htmlP.h"
20 #include <errno.h>
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include <ctype.h>
24 #include <time.h>
25 #include <unistd.h>
26
27 #include <libpspp/assertion.h>
28 #include <libpspp/compiler.h>
29 #include <data/file-name.h>
30 #include <output/chart-provider.h>
31 #include <output/output.h>
32 #include <output/manager.h>
33 #include <output/table.h>
34 #include <libpspp/version.h>
35
36 #include "error.h"
37 #include "xalloc.h"
38
39 #include "gettext.h"
40 #define _(msgid) gettext (msgid)
41
42 /* HTML driver options: (defaults listed first)
43
44    output-file="pspp.html"
45    chart-files="pspp-#.png"
46 */
47
48 static void escape_string (FILE *file,
49                            const char *text, size_t length,
50                            const char *space);
51 static bool handle_option (void *this,
52                            const char *key, const struct string *val);
53 static void print_title_tag (FILE *file, const char *name,
54                              const char *content);
55
56 static bool
57 html_open_driver (const char *name, int types, struct substring options)
58 {
59   struct outp_driver *this;
60   struct html_driver_ext *x;
61
62   this = outp_allocate_driver (&html_class, name, types);
63   this->ext = x = xmalloc (sizeof *x);
64   x->file_name = xstrdup ("pspp.html");
65   x->chart_file_name = xstrdup ("pspp-#.png");
66   x->file = NULL;
67   x->chart_cnt = 1;
68
69   outp_parse_options (name, options, handle_option, this);
70
71   x->file = fn_open (x->file_name, "w");
72   if (x->file == NULL)
73     {
74       error (0, errno, _("opening HTML output file: %s"), x->file_name);
75       goto error;
76     }
77
78   fputs ("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
79          "   \"http://www.w3.org/TR/html4/loose.dtd\">\n", x->file);
80   fputs ("<HTML>\n", x->file);
81   fputs ("<HEAD>\n", x->file);
82   /* The <TITLE> tag is required, so we use a default if the user
83      didn't provide one. */
84   print_title_tag (x->file,
85                    "TITLE", outp_title ? outp_title : _("PSPP Output"));
86   fprintf (x->file, "<META NAME=\"generator\" CONTENT=\"%s\">\n", version);
87   fputs ("<META HTTP-EQUIV=\"Content-Type\" "
88          "CONTENT=\"text/html; charset=ISO-8859-1\">\n", x->file);
89   fputs ("</HEAD>\n", x->file);
90   fputs ("<BODY BGCOLOR=\"#ffffff\" TEXT=\"#000000\"\n", x->file);
91   fputs (" LINK=\"#1f00ff\" ALINK=\"#ff0000\" VLINK=\"#9900dd\">\n", x->file);
92   print_title_tag (x->file, "H1", outp_title);
93   print_title_tag (x->file, "H2", outp_subtitle);
94
95   outp_register_driver (this);
96   return true;
97
98  error:
99   this->class->close_driver (this);
100   outp_free_driver (this);
101   return false;
102 }
103
104 /* Emits <NAME>CONTENT</NAME> to the output, escaping CONTENT as
105    necessary for HTML. */
106 static void
107 print_title_tag (FILE *file, const char *name, const char *content)
108 {
109   if (content != NULL)
110     {
111       fprintf (file, "<%s>", name);
112       escape_string (file, content, strlen (content), " ");
113       fprintf (file, "</%s>\n", name);
114     }
115 }
116
117 static bool
118 html_close_driver (struct outp_driver *this)
119 {
120   struct html_driver_ext *x = this->ext;
121   bool ok;
122
123   if (x->file != NULL)
124     {
125       fprintf (x->file,
126                "</BODY>\n"
127                "</HTML>\n"
128                "<!-- end of file -->\n");
129       ok = fn_close (x->file_name, x->file) == 0;
130       x->file = NULL;
131     }
132   else
133     ok = true;
134   free (x->chart_file_name);
135   free (x->file_name);
136   free (x);
137
138   return ok;
139 }
140
141 /* Link the image contained in FILE_NAME to the
142    HTML stream in FILE. */
143 static void
144 link_image (FILE *file, char *file_name)
145 {
146   fprintf (file, "<IMG SRC=\"%s\"/>", file_name);
147  }
148
149 /* Generic option types. */
150 enum
151   {
152     string_arg,
153     nonneg_int_arg
154   };
155
156 /* All the options that the HTML driver supports. */
157 static const struct outp_option option_tab[] =
158   {
159     {"output-file",             string_arg,     0},
160     {"chart-files",            string_arg,     1},
161     {NULL, 0, 0},
162   };
163
164 static bool
165 handle_option (void *this_, const char *key, const struct string *val)
166 {
167   struct outp_driver *this = this_;
168   struct html_driver_ext *x = this->ext;
169   int subcat;
170
171   switch (outp_match_keyword (key, option_tab, &subcat))
172     {
173     case -1:
174       error (0, 0,
175              _("unknown configuration parameter `%s' for HTML device driver"),
176              key);
177       break;
178     case string_arg:
179       switch (subcat)
180         {
181         case 0:
182           free (x->file_name);
183           x->file_name = ds_xstrdup (val);
184           break;
185         case 1:
186           if (ds_find_char (val, '#') != SIZE_MAX)
187             {
188               free (x->chart_file_name);
189               x->chart_file_name = ds_xstrdup (val);
190             }
191           else
192             error (0, 0, _("`chart-files' value must contain `#'"));
193           break;
194         default:
195           NOT_REACHED ();
196         }
197       break;
198     default:
199       NOT_REACHED ();
200     }
201
202   return true;
203 }
204
205 static void output_tab_table (struct outp_driver *, struct tab_table *);
206
207 static void
208 html_output_chart (struct outp_driver *this, const struct chart *chart)
209 {
210   struct html_driver_ext *x = this->ext;
211   struct chart_geometry geom;
212   char *file_name;
213   plPlotter *lp;
214
215   /* Draw chart in separate file. */
216   if (!chart_create_file ("png", x->chart_file_name, x->chart_cnt,
217                           NULL, &file_name, &lp))
218     return;
219   x->chart_cnt++;
220   chart_geometry_init (lp, &geom, 1000.0, 1000.0);
221   chart_draw (chart, lp, &geom);
222   chart_geometry_free (lp);
223   pl_deletepl_r (lp);
224
225   link_image (x->file, file_name);
226
227   free (file_name);
228 }
229
230 static void
231 html_submit (struct outp_driver *this, struct som_entity *s)
232 {
233   extern struct som_table_class tab_table_class;
234
235   assert (s->class == &tab_table_class ) ;
236
237   switch (s->type)
238     {
239     case SOM_TABLE:
240       output_tab_table ( this, (struct tab_table *) s->ext);
241       break;
242     default:
243       NOT_REACHED ();
244     }
245 }
246
247 /* Write LENGTH characters in TEXT to file F, escaping characters
248    as necessary for HTML.  Spaces are replaced by SPACE, which
249    should be " " or "&nbsp;". */
250 static void
251 escape_string (FILE *file,
252                const char *text, size_t length,
253                const char *space)
254 {
255   while (length-- > 0)
256     {
257       char c = *text++;
258       switch (c)
259         {
260         case '&':
261           fputs ("&amp;", file);
262           break;
263         case '<':
264           fputs ("&lt;", file);
265           break;
266         case '>':
267           fputs ("&gt;", file);
268           break;
269         case ' ':
270           fputs (space, file);
271           break;
272         default:
273           putc (c, file);
274           break;
275         }
276     }
277 }
278
279 /* Outputs content for a cell with options OPTS and contents
280    TEXT. */
281 void
282 html_put_cell_contents (struct outp_driver *this,
283                         unsigned int opts, const struct substring text)
284 {
285   struct html_driver_ext *x = this->ext;
286
287   if (!(opts & TAB_EMPTY))
288     {
289       if (opts & TAB_EMPH)
290         fputs ("<EM>", x->file);
291       if (opts & TAB_FIX)
292         {
293           fputs ("<TT>", x->file);
294           escape_string (x->file, ss_data (text), ss_length (text), "&nbsp;");
295           fputs ("</TT>", x->file);
296         }
297       else
298         {
299           size_t initial_spaces = ss_span (text, ss_cstr (CC_SPACES));
300           escape_string (x->file,
301                          ss_data (text) + initial_spaces,
302                          ss_length (text) - initial_spaces,
303                          " ");
304         }
305       if (opts & TAB_EMPH)
306         fputs ("</EM>", x->file);
307     }
308 }
309
310 /* Write table T to THIS output driver. */
311 static void
312 output_tab_table (struct outp_driver *this, struct tab_table *t)
313 {
314   struct html_driver_ext *x = this->ext;
315
316   if (t->nr == 1 && t->nc == 1)
317     {
318       fputs ("<P>", x->file);
319       html_put_cell_contents (this, t->ct[0], *t->cc);
320       fputs ("</P>\n", x->file);
321
322       return;
323     }
324
325   fputs ("<TABLE BORDER=1>\n", x->file);
326
327   if (t->title != NULL)
328     {
329       fprintf (x->file, "  <CAPTION>");
330       escape_string (x->file, t->title, strlen (t->title), " ");
331       fputs ("</CAPTION>\n", x->file);
332     }
333
334   {
335     int r;
336     unsigned char *ct = t->ct;
337
338     for (r = 0; r < t->nr; r++)
339       {
340         int c;
341
342         fputs ("  <TR>\n", x->file);
343         for (c = 0; c < t->nc; c++, ct++)
344           {
345             struct substring *cc;
346             const char *tag;
347             struct tab_joined_cell *j = NULL;
348
349             cc = t->cc + c + r * t->nc;
350             if (*ct & TAB_JOIN)
351               {
352                 j = (struct tab_joined_cell *) ss_data (*cc);
353                 cc = &j->contents;
354                 if (j->x1 != c || j->y1 != r)
355                   continue;
356               }
357
358             /* Output <TD> or <TH> tag. */
359             tag = (r < t->t || r >= t->nr - t->b
360                    || c < t->l || c >= t->nc - t->r) ? "TH" : "TD";
361             fprintf (x->file, "    <%s ALIGN=%s",
362                      tag,
363                      (*ct & TAB_ALIGN_MASK) == TAB_LEFT ? "LEFT"
364                      : (*ct & TAB_ALIGN_MASK) == TAB_RIGHT ? "RIGHT"
365                      : "CENTER");
366             if (*ct & TAB_JOIN)
367               {
368                 if (j->x2 - j->x1 > 1)
369                   fprintf (x->file, " COLSPAN=%d", j->x2 - j->x1);
370                 if (j->y2 - j->y1 > 1)
371                   fprintf (x->file, " ROWSPAN=%d", j->y2 - j->y1);
372               }
373             putc ('>', x->file);
374
375             /* Output cell contents. */
376             html_put_cell_contents (this, *ct, *cc);
377
378             /* Output </TH> or </TD>. */
379             fprintf (x->file, "</%s>\n", tag);
380           }
381         fputs ("  </TR>\n", x->file);
382       }
383   }
384
385   fputs ("</TABLE>\n\n", x->file);
386 }
387
388
389
390 /* HTML driver class. */
391 const struct outp_class html_class =
392   {
393     "html",
394     1,
395
396     html_open_driver,
397     html_close_driver,
398
399     NULL,
400     NULL,
401     NULL,
402
403     html_output_chart,
404
405     html_submit,
406
407     NULL,
408     NULL,
409     NULL,
410   };