output: Use Cairo and Pango to draw charts, instead of libplot.
[pspp-builds.git] / 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 /* Generic option types. */
142 enum
143   {
144     string_arg,
145     nonneg_int_arg
146   };
147
148 /* All the options that the HTML driver supports. */
149 static const struct outp_option option_tab[] =
150   {
151     {"output-file",             string_arg,     0},
152     {"chart-files",            string_arg,     1},
153     {NULL, 0, 0},
154   };
155
156 static bool
157 handle_option (void *this_, const char *key, const struct string *val)
158 {
159   struct outp_driver *this = this_;
160   struct html_driver_ext *x = this->ext;
161   int subcat;
162
163   switch (outp_match_keyword (key, option_tab, &subcat))
164     {
165     case -1:
166       error (0, 0,
167              _("unknown configuration parameter `%s' for HTML device driver"),
168              key);
169       break;
170     case string_arg:
171       switch (subcat)
172         {
173         case 0:
174           free (x->file_name);
175           x->file_name = ds_xstrdup (val);
176           break;
177         case 1:
178           if (ds_find_char (val, '#') != SIZE_MAX)
179             {
180               free (x->chart_file_name);
181               x->chart_file_name = ds_xstrdup (val);
182             }
183           else
184             error (0, 0, _("`chart-files' value must contain `#'"));
185           break;
186         default:
187           NOT_REACHED ();
188         }
189       break;
190     default:
191       NOT_REACHED ();
192     }
193
194   return true;
195 }
196
197 static void output_tab_table (struct outp_driver *, struct tab_table *);
198
199 static void
200 html_output_chart (struct outp_driver *this, const struct chart *chart)
201 {
202   struct html_driver_ext *x = this->ext;
203   char *file_name;
204
205   file_name = chart_draw_png (chart, x->chart_file_name, x->chart_cnt++);
206   fprintf (x->file, "<IMG SRC=\"%s\"/>", file_name);
207   free (file_name);
208 }
209
210 static void
211 html_submit (struct outp_driver *this, struct som_entity *s)
212 {
213   extern struct som_table_class tab_table_class;
214
215   assert (s->class == &tab_table_class ) ;
216
217   switch (s->type)
218     {
219     case SOM_TABLE:
220       output_tab_table ( this, (struct tab_table *) s->ext);
221       break;
222     default:
223       NOT_REACHED ();
224     }
225 }
226
227 /* Write LENGTH characters in TEXT to file F, escaping characters
228    as necessary for HTML.  Spaces are replaced by SPACE, which
229    should be " " or "&nbsp;". */
230 static void
231 escape_string (FILE *file,
232                const char *text, size_t length,
233                const char *space)
234 {
235   while (length-- > 0)
236     {
237       char c = *text++;
238       switch (c)
239         {
240         case '&':
241           fputs ("&amp;", file);
242           break;
243         case '<':
244           fputs ("&lt;", file);
245           break;
246         case '>':
247           fputs ("&gt;", file);
248           break;
249         case ' ':
250           fputs (space, file);
251           break;
252         default:
253           putc (c, file);
254           break;
255         }
256     }
257 }
258
259 /* Outputs content for a cell with options OPTS and contents
260    TEXT. */
261 void
262 html_put_cell_contents (struct outp_driver *this,
263                         unsigned int opts, const struct substring text)
264 {
265   struct html_driver_ext *x = this->ext;
266
267   if (!(opts & TAB_EMPTY))
268     {
269       if (opts & TAB_EMPH)
270         fputs ("<EM>", x->file);
271       if (opts & TAB_FIX)
272         {
273           fputs ("<TT>", x->file);
274           escape_string (x->file, ss_data (text), ss_length (text), "&nbsp;");
275           fputs ("</TT>", x->file);
276         }
277       else
278         {
279           size_t initial_spaces = ss_span (text, ss_cstr (CC_SPACES));
280           escape_string (x->file,
281                          ss_data (text) + initial_spaces,
282                          ss_length (text) - initial_spaces,
283                          " ");
284         }
285       if (opts & TAB_EMPH)
286         fputs ("</EM>", x->file);
287     }
288 }
289
290 /* Write table T to THIS output driver. */
291 static void
292 output_tab_table (struct outp_driver *this, struct tab_table *t)
293 {
294   struct html_driver_ext *x = this->ext;
295
296   if (t->nr == 1 && t->nc == 1)
297     {
298       fputs ("<P>", x->file);
299       html_put_cell_contents (this, t->ct[0], *t->cc);
300       fputs ("</P>\n", x->file);
301
302       return;
303     }
304
305   fputs ("<TABLE BORDER=1>\n", x->file);
306
307   if (t->title != NULL)
308     {
309       fprintf (x->file, "  <CAPTION>");
310       escape_string (x->file, t->title, strlen (t->title), " ");
311       fputs ("</CAPTION>\n", x->file);
312     }
313
314   {
315     int r;
316     unsigned char *ct = t->ct;
317
318     for (r = 0; r < t->nr; r++)
319       {
320         int c;
321
322         fputs ("  <TR>\n", x->file);
323         for (c = 0; c < t->nc; c++, ct++)
324           {
325             struct substring *cc;
326             const char *tag;
327             struct tab_joined_cell *j = NULL;
328
329             cc = t->cc + c + r * t->nc;
330             if (*ct & TAB_JOIN)
331               {
332                 j = (struct tab_joined_cell *) ss_data (*cc);
333                 cc = &j->contents;
334                 if (j->x1 != c || j->y1 != r)
335                   continue;
336               }
337
338             /* Output <TD> or <TH> tag. */
339             tag = (r < t->t || r >= t->nr - t->b
340                    || c < t->l || c >= t->nc - t->r) ? "TH" : "TD";
341             fprintf (x->file, "    <%s ALIGN=%s",
342                      tag,
343                      (*ct & TAB_ALIGN_MASK) == TAB_LEFT ? "LEFT"
344                      : (*ct & TAB_ALIGN_MASK) == TAB_RIGHT ? "RIGHT"
345                      : "CENTER");
346             if (*ct & TAB_JOIN)
347               {
348                 if (j->x2 - j->x1 > 1)
349                   fprintf (x->file, " COLSPAN=%d", j->x2 - j->x1);
350                 if (j->y2 - j->y1 > 1)
351                   fprintf (x->file, " ROWSPAN=%d", j->y2 - j->y1);
352               }
353             putc ('>', x->file);
354
355             /* Output cell contents. */
356             html_put_cell_contents (this, *ct, *cc);
357
358             /* Output </TH> or </TD>. */
359             fprintf (x->file, "</%s>\n", tag);
360           }
361         fputs ("  </TR>\n", x->file);
362       }
363   }
364
365   fputs ("</TABLE>\n\n", x->file);
366 }
367
368
369
370 /* HTML driver class. */
371 const struct outp_class html_class =
372   {
373     "html",
374     1,
375
376     html_open_driver,
377     html_close_driver,
378
379     NULL,
380     NULL,
381     NULL,
382
383     html_output_chart,
384
385     html_submit,
386
387     NULL,
388     NULL,
389     NULL,
390   };