cairo: Move chart code into cairo-chart.
[pspp] / src / output / html.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2009, 2010, 2011, 2012, 2013, 2014, 2017,
3    2020 Free Software Foundation, Inc.
4
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
17
18 #include <config.h>
19
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 #include <locale.h>
27
28 #include "data/file-name.h"
29 #include "data/file-handle-def.h"
30 #include "libpspp/assertion.h"
31 #include "libpspp/cast.h"
32 #include "libpspp/compiler.h"
33 #include "libpspp/i18n.h"
34 #include "libpspp/message.h"
35 #include "libpspp/version.h"
36 #include "output/cairo-chart.h"
37 #include "output/chart-item.h"
38 #include "output/driver-provider.h"
39 #include "output/message-item.h"
40 #include "output/options.h"
41 #include "output/output-item-provider.h"
42 #include "output/table-provider.h"
43 #include "output/table-item.h"
44 #include "output/text-item.h"
45
46 #include "gl/minmax.h"
47 #include "gl/xalloc.h"
48
49 #include "gettext.h"
50 #define _(msgid) gettext (msgid)
51
52 struct html_driver
53   {
54     struct output_driver driver;
55 #ifdef HAVE_CAIRO
56     struct cell_color fg;
57     struct cell_color bg;
58 #endif
59     struct file_handle *handle;
60     char *chart_file_name;
61
62     FILE *file;
63     size_t chart_cnt;
64
65     bool bare;
66     bool css;
67     bool borders;
68   };
69
70 static const struct output_driver_class html_driver_class;
71
72 static void html_output_table (struct html_driver *, const struct table_item *);
73 static void escape_string (FILE *file, const char *text,
74                            const char *space, const char *newline);
75 static void print_title_tag (FILE *file, const char *name,
76                              const char *content);
77
78 static struct html_driver *
79 html_driver_cast (struct output_driver *driver)
80 {
81   assert (driver->class == &html_driver_class);
82   return UP_CAST (driver, struct html_driver, driver);
83 }
84
85 static struct driver_option *
86 opt (struct output_driver *d, struct string_map *options, const char *key,
87      const char *default_value)
88 {
89   return driver_option_get (d, options, key, default_value);
90 }
91
92 static void
93 put_header (struct html_driver *html)
94 {
95   fputs ("<!doctype html>\n", html->file);
96   fprintf (html->file, "<html");
97   char *ln = get_language ();
98   if (ln)
99     fprintf (html->file, " lang=\"%s\"", ln);
100   free (ln);
101   fprintf (html->file, ">\n");
102   fputs ("<head>\n", html->file);
103   print_title_tag (html->file, "title", _("PSPP Output"));
104   fprintf (html->file, "<meta name=\"generator\" content=\"%s\">\n", version);
105   fputs ("<meta http-equiv=\"content-type\" "
106          "content=\"text/html; charset=utf-8\">\n", html->file);
107
108   if (html->css)
109     {
110       fputs ("<style>\n"
111              "<!--\n"
112              "body {\n"
113              "  background: white;\n"
114              "  color: black;\n"
115              "  padding: 0em 12em 0em 3em;\n"
116              "  margin: 0\n"
117              "}\n"
118              "body>p {\n"
119              "  margin: 0pt 0pt 0pt 0em\n"
120              "}\n"
121              "body>p + p {\n"
122              "  text-indent: 1.5em;\n"
123              "}\n"
124              "h1 {\n"
125              "  font-size: 150%;\n"
126              "  margin-left: -1.33em\n"
127              "}\n"
128              "h2 {\n"
129              "  font-size: 125%;\n"
130              "  font-weight: bold;\n"
131              "  margin-left: -.8em\n"
132              "}\n"
133              "h3 {\n"
134              "  font-size: 100%;\n"
135              "  font-weight: bold;\n"
136              "  margin-left: -.5em }\n"
137              "h4 {\n"
138              "  font-size: 100%;\n"
139              "  margin-left: 0em\n"
140              "}\n"
141              "h1, h2, h3, h4, h5, h6 {\n"
142              "  font-family: sans-serif;\n"
143              "  color: blue\n"
144              "}\n"
145              "html {\n"
146              "  margin: 0\n"
147              "}\n"
148              "code {\n"
149              "  font-family: sans-serif\n"
150              "}\n"
151              "table {\n"
152              "  border-collapse: collapse;\n"
153              "  margin-bottom: 1em\n"
154              "}\n"
155              "th { background: #dddddd; font-weight: normal; font-style: oblique }\n"
156              "caption {\n"
157              "  text-align: left\n"
158              "}\n"
159
160              "a:link {\n"
161              "  color: #1f00ff;\n"
162              "}\n"
163              "a:visited {\n"
164              "  color: #9900dd;\n"
165              "}\n"
166              "a:active {\n"
167              "  color: red;\n"
168              "}\n"
169              "-->\n"
170              "</style>\n",
171              html->file);
172     }
173   fputs ("</head>\n", html->file);
174   fputs ("<body>\n", html->file);
175 }
176
177 static struct output_driver *
178 html_create (struct file_handle *fh, enum settings_output_devices device_type,
179              struct string_map *o)
180 {
181   struct output_driver *d;
182   struct html_driver *html;
183
184   html = xzalloc (sizeof *html);
185   d = &html->driver;
186   output_driver_init (&html->driver, &html_driver_class, fh_get_file_name (fh),
187                       device_type);
188   html->bare = parse_boolean (opt (d, o, "bare", "false"));
189   html->css = parse_boolean (opt (d, o, "css", "true"));
190   html->borders = parse_boolean (opt (d, o, "borders", "true"));
191
192   html->handle = fh;
193   html->chart_file_name = parse_chart_file_name (opt (d, o, "charts",
194                                                       fh_get_file_name (fh)));
195   html->file = NULL;
196   html->chart_cnt = 1;
197 #ifdef HAVE_CAIRO
198   html->bg = parse_color (opt (d, o, "background-color", "#FFFFFFFFFFFF"));
199   html->fg = parse_color (opt (d, o, "foreground-color", "#000000000000"));
200 #endif
201   html->file = fn_open (html->handle, "w");
202   if (html->file == NULL)
203     {
204       msg_error (errno, _("error opening output file `%s'"), fh_get_file_name (html->handle));
205       goto error;
206     }
207
208   if (!html->bare)
209     put_header (html);
210
211   return d;
212
213  error:
214   output_driver_destroy (d);
215   return NULL;
216 }
217
218 /* Emits <NAME>CONTENT</NAME> to the output, escaping CONTENT as
219    necessary for HTML. */
220 static void
221 print_title_tag (FILE *file, const char *name, const char *content)
222 {
223   if (content != NULL)
224     {
225        fprintf (file, "<%s>", name);
226       escape_string (file, content, " ", " - ");
227       fprintf (file, "</%s>\n", name);
228     }
229 }
230
231 static void
232 html_destroy (struct output_driver *driver)
233 {
234   struct html_driver *html = html_driver_cast (driver);
235
236   if (html->file != NULL)
237     {
238       if (!html->bare)
239         fprintf (html->file,
240                  "</body>\n"
241                  "</html>\n"
242                  "<!-- end of file -->\n");
243       fn_close (html->handle, html->file);
244     }
245   free (html->chart_file_name);
246   fh_unref (html->handle);
247   free (html);
248 }
249
250 static void
251 html_submit (struct output_driver *driver,
252              const struct output_item *output_item)
253 {
254   struct html_driver *html = html_driver_cast (driver);
255
256   if (is_table_item (output_item))
257     {
258       struct table_item *table_item = to_table_item (output_item);
259       html_output_table (html, table_item);
260     }
261 #ifdef HAVE_CAIRO
262   else if (is_chart_item (output_item) && html->chart_file_name != NULL)
263     {
264       struct chart_item *chart_item = to_chart_item (output_item);
265       char *file_name;
266
267       file_name = xr_draw_png_chart (chart_item, html->chart_file_name,
268                                      html->chart_cnt++,
269                                      &html->fg,
270                                      &html->bg
271                                 );
272       if (file_name != NULL)
273         {
274           const char *title = chart_item_get_title (chart_item);
275           fprintf (html->file, "<img src=\"%s\" alt=\"chart: %s\">",
276                    file_name, title ? title : _("No description"));
277           free (file_name);
278         }
279     }
280 #endif  /* HAVE_CAIRO */
281   else if (is_text_item (output_item))
282     {
283       struct text_item *text_item = to_text_item (output_item);
284       const char *s = text_item_get_text (text_item);
285
286       switch (text_item_get_type (text_item))
287         {
288         case TEXT_ITEM_PAGE_TITLE:
289           break;
290
291         case TEXT_ITEM_TITLE:
292           {
293             int level = MIN (5, output_get_group_level ()) + 1;
294             char tag[3] = { 'H', level + '1', '\0' };
295             print_title_tag (html->file, tag, s);
296           }
297           break;
298
299         case TEXT_ITEM_SYNTAX:
300           fprintf (html->file, "<pre class=\"syntax\">");
301           escape_string (html->file, s, " ", "<br>");
302           fprintf (html->file, "</pre>\n");
303           break;
304
305         case TEXT_ITEM_LOG:
306           print_title_tag (html->file, "pre", s); /* should be <P><TT> */
307           break;
308         }
309     }
310   else if (is_message_item (output_item))
311     {
312       const struct message_item *message_item = to_message_item (output_item);
313       char *s = msg_to_string (message_item_get_msg (message_item));
314       print_title_tag (html->file, "p", s);
315       free (s);
316     }
317 }
318
319 /* Write TEXT to file F, escaping characters as necessary for HTML.  Spaces are
320    replaced by SPACE, which should be " " or "&nbsp;" New-lines are replaced by
321    NEWLINE, which might be "<BR>" or "\n" or something else appropriate. */
322 static void
323 escape_string (FILE *file, const char *text,
324                const char *space, const char *newline)
325 {
326   for (;;)
327     {
328       char c = *text++;
329       switch (c)
330         {
331         case 0:
332           return;
333         case '\n':
334           fputs (newline, file);
335           break;
336         case '&':
337           fputs ("&amp;", file);
338           break;
339         case '<':
340           fputs ("&lt;", file);
341           break;
342         case '>':
343           fputs ("&gt;", file);
344           break;
345         case ' ':
346           fputs (space, file);
347           break;
348         case '"':
349           fputs ("&quot;", file);
350           break;
351         default:
352           putc (c, file);
353           break;
354         }
355     }
356 }
357
358 static void
359 escape_tag (FILE *file, const char *tag,
360             const char *text, const char *space, const char *newline)
361 {
362   if (!text || !*text)
363     return;
364
365   fprintf (file, "<%s>", tag);
366   escape_string (file, text, space, newline);
367   fprintf (file, "</%s>", tag);
368 }
369
370 static const char *
371 border_to_css (int border)
372 {
373   switch (border)
374     {
375     case TABLE_STROKE_NONE:
376       return NULL;
377
378     case TABLE_STROKE_SOLID:
379       return "solid";
380
381     case TABLE_STROKE_DASHED:
382       return "dashed";
383
384     case TABLE_STROKE_THICK:
385       return "thick solid";
386
387     case TABLE_STROKE_THIN:
388       return "thin solid";
389
390     case TABLE_STROKE_DOUBLE:
391       return "double";
392
393     default:
394       return NULL;
395     }
396
397 }
398
399 struct css_style
400 {
401   FILE *file;
402   int n_styles;
403 };
404
405 static struct css_style *
406 style_start (FILE *file)
407 {
408   struct css_style *cs = XMALLOC (struct css_style);
409   cs->file = file;
410   cs->n_styles = 0;
411   fputs (" style=\"", file);
412   return cs;
413 }
414
415 static void
416 style_end (struct css_style *cs)
417 {
418   fputs ("\"", cs->file);
419   free (cs);
420 }
421
422 static void
423 put_style (struct css_style *st, const char *name, const char *value)
424 {
425   if (st->n_styles++ > 0)
426     fputs ("; ", st->file);
427   fprintf (st->file, "%s: %s", name, value);
428 }
429
430 static void
431 put_border (struct css_style *st, int style, const char *border_name)
432 {
433   const char *css = border_to_css (style);
434   if (css)
435     {
436       if (st->n_styles++ > 0)
437         fputs ("; ", st->file);
438       fprintf (st->file, "border-%s: %s", border_name, css);
439     }
440 }
441
442 static void
443 put_tfoot (struct html_driver *html, const struct table *t, bool *tfoot)
444 {
445   if (!*tfoot)
446     {
447       fputs ("<tfoot>\n", html->file);
448       fputs ("<tr>\n", html->file);
449       fprintf (html->file, "<td colspan=%d>\n", table_nc (t));
450       *tfoot = true;
451     }
452   else
453     fputs ("\n<br>", html->file);
454 }
455
456 static void
457 html_put_footnote_markers (struct html_driver *html,
458                            const struct footnote **footnotes,
459                            size_t n_footnotes)
460 {
461   if (n_footnotes > 0)
462     {
463       fputs ("<sup>", html->file);
464       for (size_t i = 0; i < n_footnotes; i++)
465         {
466           const struct footnote *f = footnotes[i];
467
468           if (i > 0)
469             putc (',', html->file);
470           escape_string (html->file, f->marker, " ", "<br>");
471         }
472       fputs ("</sup>", html->file);
473     }
474 }
475
476 static void
477 html_put_table_item_text (struct html_driver *html,
478                           const struct table_item_text *text)
479 {
480   escape_string (html->file, text->content, " ", "<br>");
481   html_put_footnote_markers (html, text->footnotes, text->n_footnotes);
482 }
483
484 static void
485 html_put_table_item_layers (struct html_driver *html,
486                             const struct table_item_layers *layers)
487 {
488   for (size_t i = 0; i < layers->n_layers; i++)
489     {
490       if (i)
491         fputs ("<br>\n", html->file);
492
493       const struct table_item_layer *layer = &layers->layers[i];
494       escape_string (html->file, layer->content, " ", "<br>");
495       html_put_footnote_markers (html, layer->footnotes, layer->n_footnotes);
496     }
497 }
498
499 static void
500 html_output_table (struct html_driver *html, const struct table_item *item)
501 {
502   const struct table *t = table_item_get_table (item);
503   bool tfoot = false;
504   int y;
505
506   fputs ("<table>\n", html->file);
507
508   const struct table_item_text *caption = table_item_get_caption (item);
509   if (caption)
510     {
511       put_tfoot (html, t, &tfoot);
512       html_put_table_item_text (html, caption);
513     }
514   const struct footnote **f;
515   size_t n_footnotes = table_collect_footnotes (item, &f);
516
517   for (size_t i = 0; i < n_footnotes; i++)
518     {
519       put_tfoot (html, t, &tfoot);
520       escape_tag (html->file, "sup", f[i]->marker, " ", "<br>");
521       escape_string (html->file, f[i]->content, " ", "<br>");
522     }
523   free (f);
524   if (tfoot)
525     {
526       fputs ("</td>\n", html->file);
527       fputs ("</tr>\n", html->file);
528       fputs ("</tfoot>\n", html->file);
529     }
530
531   const struct table_item_text *title = table_item_get_title (item);
532   const struct table_item_layers *layers = table_item_get_layers (item);
533   if (title || layers)
534     {
535       fputs ("<caption>", html->file);
536       if (title)
537         html_put_table_item_text (html, title);
538       if (title && layers)
539         fputs ("<br>\n", html->file);
540       if (layers)
541         html_put_table_item_layers (html, layers);
542       fputs ("</caption>\n", html->file);
543     }
544
545   fputs ("<tbody>\n", html->file);
546
547   for (y = 0; y < table_nr (t); y++)
548     {
549       int x;
550
551       fputs ("<tr>\n", html->file);
552       for (x = 0; x < table_nc (t);)
553         {
554           struct table_cell cell;
555           const char *tag;
556
557           table_get_cell (t, x, y, &cell);
558           if (x != cell.d[TABLE_HORZ][0] || y != cell.d[TABLE_VERT][0])
559             goto next_1;
560
561           /* output <td> or <th> tag. */
562           bool is_header = (y < table_ht (t)
563                        || y >= table_nr (t) - table_hb (t)
564                        || x < table_hl (t)
565                        || x >= table_nc (t) - table_hr (t));
566           tag = is_header ? "th" : "td";
567           fprintf (html->file, "<%s", tag);
568
569           struct css_style *style = style_start (html->file);
570           enum table_halign halign = table_halign_interpret (
571             cell.style->cell_style.halign, cell.options & TAB_NUMERIC);
572
573           switch (halign)
574             {
575             case TABLE_HALIGN_RIGHT:
576               put_style (style, "text-align", "right");
577               break;
578             case TABLE_HALIGN_CENTER:
579               put_style (style, "text-align", "center");
580               break;
581             default:
582               /* Do nothing */
583               break;
584             }
585
586           if (cell.style->cell_style.valign != TABLE_VALIGN_TOP)
587             {
588               put_style (style, "vertical-align",
589                          (cell.style->cell_style.valign == TABLE_VALIGN_BOTTOM
590                           ? "bottom" : "middle"));
591             }
592
593           int colspan = table_cell_colspan (&cell);
594           int rowspan = table_cell_rowspan (&cell);
595
596           if (html->borders)
597             {
598               /* Cell borders. */
599               struct cell_color color;
600
601               int top = table_get_rule (t, TABLE_VERT, x, y, &color);
602               put_border (style, top, "top");
603
604               if (y + rowspan == table_nr (t))
605                 {
606                   int bottom = table_get_rule (t, TABLE_VERT, x, y + rowspan,
607                                            &color);
608                   put_border (style, bottom, "bottom");
609                 }
610
611               int left = table_get_rule (t, TABLE_HORZ, x, y, &color);
612               put_border (style, left, "left");
613
614               if (x + colspan == table_nc (t))
615                 {
616                   int right = table_get_rule (t, TABLE_HORZ, x + colspan, y,
617                                           &color);
618                   put_border (style, right, "right");
619                 }
620             }
621           style_end (style);
622
623           if (colspan > 1)
624             fprintf (html->file, " colspan=\"%d\"", colspan);
625
626           if (rowspan > 1)
627             fprintf (html->file, " rowspan=\"%d\"", rowspan);
628
629           putc ('>', html->file);
630
631           /* Output cell contents. */
632           const char *s = cell.text;
633           if (cell.options & TAB_FIX)
634             escape_tag (html->file, "tt", s, "&nbsp;", "<br>");
635           else
636             {
637               s += strspn (s, CC_SPACES);
638               escape_string (html->file, s, " ", "<br>");
639             }
640
641           if (cell.n_subscripts)
642             {
643               fputs ("<sub>", html->file);
644               for (size_t i = 0; i < cell.n_subscripts; i++)
645                 {
646                   if (i)
647                     putc (',', html->file);
648                   escape_string (html->file, cell.subscripts[i],
649                                  "&nbsp;", "<br>");
650                 }
651               fputs ("</sub>", html->file);
652             }
653           if (cell.superscript)
654             escape_tag (html->file, "sup", cell.superscript, "&nbsp;", "<br>");
655           html_put_footnote_markers (html, cell.footnotes, cell.n_footnotes);
656
657           /* output </th> or </td>. */
658           fprintf (html->file, "</%s>\n", tag);
659
660         next_1:
661           x = cell.d[TABLE_HORZ][1];
662         }
663       fputs ("</tr>\n", html->file);
664     }
665
666   fputs ("</tbody>\n", html->file);
667   fputs ("</table>\n\n", html->file);
668 }
669
670 struct output_driver_factory html_driver_factory =
671   { "html", "pspp.html", html_create };
672
673 static const struct output_driver_class html_driver_class =
674   {
675     "html",
676     html_destroy,
677     html_submit,
678     NULL,
679   };