Add support for PNG images in .spv files.
[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 #ifdef HAVE_CAIRO
37 #include "output/cairo-chart.h"
38 #endif
39 #include "output/chart-item.h"
40 #include "output/driver-provider.h"
41 #include "output/image-item.h"
42 #include "output/message-item.h"
43 #include "output/options.h"
44 #include "output/output-item-provider.h"
45 #include "output/pivot-output.h"
46 #include "output/pivot-table.h"
47 #include "output/table-provider.h"
48 #include "output/table-item.h"
49 #include "output/text-item.h"
50
51 #include "gl/minmax.h"
52 #include "gl/xalloc.h"
53
54 #include "gettext.h"
55 #define _(msgid) gettext (msgid)
56
57 /* This file uses TABLE_HORZ and TABLE_VERT enough to warrant abbreviating. */
58 #define H TABLE_HORZ
59 #define V TABLE_VERT
60
61 struct html_driver
62   {
63     struct output_driver driver;
64 #ifdef HAVE_CAIRO
65     struct cell_color fg;
66     struct cell_color bg;
67 #endif
68     struct file_handle *handle;
69     char *chart_file_name;
70
71     FILE *file;
72     size_t chart_cnt;
73
74     bool bare;
75     bool css;
76     bool borders;
77   };
78
79 static const struct output_driver_class html_driver_class;
80
81 static void html_output_table (struct html_driver *, const struct table_item *);
82 static void escape_string (FILE *file, const char *text,
83                            const char *space, const char *newline);
84 static void print_title_tag (FILE *file, const char *name,
85                              const char *content);
86
87 static struct html_driver *
88 html_driver_cast (struct output_driver *driver)
89 {
90   assert (driver->class == &html_driver_class);
91   return UP_CAST (driver, struct html_driver, driver);
92 }
93
94 static struct driver_option *
95 opt (struct output_driver *d, struct string_map *options, const char *key,
96      const char *default_value)
97 {
98   return driver_option_get (d, options, key, default_value);
99 }
100
101 static void
102 put_header (struct html_driver *html)
103 {
104   fputs ("<!doctype html>\n", html->file);
105   fprintf (html->file, "<html");
106   char *ln = get_language ();
107   if (ln)
108     fprintf (html->file, " lang=\"%s\"", ln);
109   free (ln);
110   fprintf (html->file, ">\n");
111   fputs ("<head>\n", html->file);
112   print_title_tag (html->file, "title", _("PSPP Output"));
113   fprintf (html->file, "<meta name=\"generator\" content=\"%s\">\n", version);
114   fputs ("<meta http-equiv=\"content-type\" "
115          "content=\"text/html; charset=utf-8\">\n", html->file);
116
117   if (html->css)
118     {
119       fputs ("<style>\n"
120              "<!--\n"
121              "body {\n"
122              "  background: white;\n"
123              "  color: black;\n"
124              "  padding: 0em 12em 0em 3em;\n"
125              "  margin: 0\n"
126              "}\n"
127              "body>p {\n"
128              "  margin: 0pt 0pt 0pt 0em\n"
129              "}\n"
130              "body>p + p {\n"
131              "  text-indent: 1.5em;\n"
132              "}\n"
133              "h1 {\n"
134              "  font-size: 150%;\n"
135              "  margin-left: -1.33em\n"
136              "}\n"
137              "h2 {\n"
138              "  font-size: 125%;\n"
139              "  font-weight: bold;\n"
140              "  margin-left: -.8em\n"
141              "}\n"
142              "h3 {\n"
143              "  font-size: 100%;\n"
144              "  font-weight: bold;\n"
145              "  margin-left: -.5em }\n"
146              "h4 {\n"
147              "  font-size: 100%;\n"
148              "  margin-left: 0em\n"
149              "}\n"
150              "h1, h2, h3, h4, h5, h6 {\n"
151              "  font-family: sans-serif;\n"
152              "  color: blue\n"
153              "}\n"
154              "html {\n"
155              "  margin: 0\n"
156              "}\n"
157              "code {\n"
158              "  font-family: sans-serif\n"
159              "}\n"
160              "table {\n"
161              "  border-collapse: collapse;\n"
162              "  margin-bottom: 1em\n"
163              "}\n"
164              "caption {\n"
165              "  text-align: left\n"
166              "}\n"
167              "th { font-weight: normal }\n"
168              "a:link {\n"
169              "  color: #1f00ff;\n"
170              "}\n"
171              "a:visited {\n"
172              "  color: #9900dd;\n"
173              "}\n"
174              "a:active {\n"
175              "  color: red;\n"
176              "}\n"
177              "-->\n"
178              "</style>\n",
179              html->file);
180     }
181   fputs ("</head>\n", html->file);
182   fputs ("<body>\n", html->file);
183 }
184
185 static struct output_driver *
186 html_create (struct file_handle *fh, enum settings_output_devices device_type,
187              struct string_map *o)
188 {
189   struct output_driver *d;
190   struct html_driver *html;
191
192   html = xzalloc (sizeof *html);
193   d = &html->driver;
194   output_driver_init (&html->driver, &html_driver_class, fh_get_file_name (fh),
195                       device_type);
196   html->bare = parse_boolean (opt (d, o, "bare", "false"));
197   html->css = parse_boolean (opt (d, o, "css", "true"));
198   html->borders = parse_boolean (opt (d, o, "borders", "true"));
199
200   html->handle = fh;
201   html->chart_file_name = parse_chart_file_name (opt (d, o, "charts",
202                                                       fh_get_file_name (fh)));
203   html->file = NULL;
204   html->chart_cnt = 1;
205 #ifdef HAVE_CAIRO
206   html->bg = parse_color (opt (d, o, "background-color", "#FFFFFFFFFFFF"));
207   html->fg = parse_color (opt (d, o, "foreground-color", "#000000000000"));
208 #endif
209   html->file = fn_open (html->handle, "w");
210   if (html->file == NULL)
211     {
212       msg_error (errno, _("error opening output file `%s'"), fh_get_file_name (html->handle));
213       goto error;
214     }
215
216   if (!html->bare)
217     put_header (html);
218
219   return d;
220
221  error:
222   output_driver_destroy (d);
223   return NULL;
224 }
225
226 /* Emits <NAME>CONTENT</NAME> to the output, escaping CONTENT as
227    necessary for HTML. */
228 static void
229 print_title_tag (FILE *file, const char *name, const char *content)
230 {
231   if (content != NULL)
232     {
233        fprintf (file, "<%s>", name);
234       escape_string (file, content, " ", " - ");
235       fprintf (file, "</%s>\n", name);
236     }
237 }
238
239 static void
240 html_destroy (struct output_driver *driver)
241 {
242   struct html_driver *html = html_driver_cast (driver);
243
244   if (html->file != NULL)
245     {
246       if (!html->bare)
247         fprintf (html->file,
248                  "</body>\n"
249                  "</html>\n"
250                  "<!-- end of file -->\n");
251       fn_close (html->handle, html->file);
252     }
253   free (html->chart_file_name);
254   fh_unref (html->handle);
255   free (html);
256 }
257
258 static void
259 html_submit (struct output_driver *driver,
260              const struct output_item *output_item)
261 {
262   struct html_driver *html = html_driver_cast (driver);
263
264   if (is_table_item (output_item))
265     {
266       struct table_item *table_item = to_table_item (output_item);
267       html_output_table (html, table_item);
268     }
269 #ifdef HAVE_CAIRO
270   else if (is_image_item (output_item) && html->chart_file_name != NULL)
271     {
272       struct image_item *image_item = to_image_item (output_item);
273       char *file_name = xr_write_png_image (
274         image_item->image, html->chart_file_name, ++html->chart_cnt);
275       if (file_name != NULL)
276         {
277           fprintf (html->file, "<img src=\"%s\">", file_name);
278           free (file_name);
279         }
280     }
281   else if (is_chart_item (output_item) && html->chart_file_name != NULL)
282     {
283       struct chart_item *chart_item = to_chart_item (output_item);
284       char *file_name;
285
286       file_name = xr_draw_png_chart (chart_item, html->chart_file_name,
287                                      html->chart_cnt++,
288                                      &html->fg,
289                                      &html->bg
290                                 );
291       if (file_name != NULL)
292         {
293           const char *title = chart_item_get_title (chart_item);
294           fprintf (html->file, "<img src=\"%s\" alt=\"chart: %s\">",
295                    file_name, title ? title : _("No description"));
296           free (file_name);
297         }
298     }
299 #endif  /* HAVE_CAIRO */
300   else if (is_text_item (output_item))
301     {
302       struct text_item *text_item = to_text_item (output_item);
303       const char *s = text_item_get_text (text_item);
304
305       switch (text_item_get_type (text_item))
306         {
307         case TEXT_ITEM_PAGE_TITLE:
308           break;
309
310         case TEXT_ITEM_TITLE:
311           {
312             int level = MIN (5, output_get_group_level ()) + 1;
313             char tag[3] = { 'H', level + '1', '\0' };
314             print_title_tag (html->file, tag, s);
315           }
316           break;
317
318         case TEXT_ITEM_SYNTAX:
319           fprintf (html->file, "<pre class=\"syntax\">");
320           escape_string (html->file, s, " ", "<br>");
321           fprintf (html->file, "</pre>\n");
322           break;
323
324         case TEXT_ITEM_LOG:
325           fprintf (html->file, "<p>");
326           escape_string (html->file, s, " ", "<br>");
327           fprintf (html->file, "</p>\n");
328           break;
329         }
330     }
331   else if (is_message_item (output_item))
332     {
333       const struct message_item *message_item = to_message_item (output_item);
334       char *s = msg_to_string (message_item_get_msg (message_item));
335       fprintf (html->file, "<p>");
336       escape_string (html->file, s, " ", "<br>");
337       fprintf (html->file, "</p>\n");
338       free (s);
339     }
340 }
341
342 /* Write TEXT to file F, escaping characters as necessary for HTML.  Spaces are
343    replaced by SPACE, which should be " " or "&nbsp;" New-lines are replaced by
344    NEWLINE, which might be "<BR>" or "\n" or something else appropriate. */
345 static void
346 escape_string (FILE *file, const char *text,
347                const char *space, const char *newline)
348 {
349   for (;;)
350     {
351       char c = *text++;
352       switch (c)
353         {
354         case 0:
355           return;
356         case '\n':
357           fputs (newline, file);
358           break;
359         case '&':
360           fputs ("&amp;", file);
361           break;
362         case '<':
363           fputs ("&lt;", file);
364           break;
365         case '>':
366           fputs ("&gt;", file);
367           break;
368         case ' ':
369           fputs (space, file);
370           break;
371         case '"':
372           fputs ("&quot;", file);
373           break;
374         default:
375           putc (c, file);
376           break;
377         }
378     }
379 }
380
381 static const char *
382 border_to_css (int border)
383 {
384   switch (border)
385     {
386     case TABLE_STROKE_NONE:
387       return NULL;
388
389     case TABLE_STROKE_SOLID:
390       return "solid";
391
392     case TABLE_STROKE_DASHED:
393       return "dashed";
394
395     case TABLE_STROKE_THICK:
396       return "thick solid";
397
398     case TABLE_STROKE_THIN:
399       return "thin solid";
400
401     case TABLE_STROKE_DOUBLE:
402       return "double";
403
404     default:
405       return NULL;
406     }
407
408 }
409
410 struct css_style
411 {
412   FILE *file;
413   int n_styles;
414 };
415
416 static void
417 style_start (struct css_style *cs, FILE *file)
418 {
419   *cs = (struct css_style) {
420     .file = file,
421     .n_styles = 0,
422   };
423 }
424
425 static void
426 style_end (struct css_style *cs)
427 {
428   if (cs->n_styles > 0)
429     fputs ("'", cs->file);
430 }
431
432 static void
433 next_style (struct css_style *st)
434 {
435   bool first = !st->n_styles++;
436   fputs (first ? " style='" : "; ", st->file);
437 }
438
439 static void
440 put_style (struct css_style *st, const char *name, const char *value)
441 {
442   next_style (st);
443   fprintf (st->file, "%s: %s", name, value);
444 }
445
446 static bool
447 format_color (const struct cell_color color,
448               const struct cell_color default_color,
449               char *buf, size_t bufsize)
450 {
451   bool retval = !cell_color_equal (&color, &default_color);
452   if (retval)
453     {
454       if (color.alpha == 255)
455         snprintf (buf, bufsize, "#%02x%02x%02x", color.r, color.g, color.b);
456       else
457         snprintf (buf, bufsize, "rgba(%d, %d, %d, %.3f)",
458                   color.r, color.g, color.b, color.alpha / 255.0);
459     }
460   return retval;
461 }
462
463 static void
464 put_border (const struct table *table, const struct table_cell *cell,
465             struct css_style *style,
466             enum table_axis axis, int h, int v,
467             const char *border_name)
468 {
469   struct cell_color color;
470   const char *css = border_to_css (
471     table_get_rule (table, axis, cell->d[H][h], cell->d[V][v], &color));
472   if (css)
473     {
474       next_style (style);
475       fprintf (style->file, "border-%s: %s", border_name, css);
476
477       char buf[32];
478       if (format_color (color, (struct cell_color) CELL_COLOR_BLACK,
479                         buf, sizeof buf))
480         fprintf (style->file, " %s", buf);
481     }
482 }
483
484 static void
485 html_put_table_cell (struct html_driver *html, const struct pivot_table *pt,
486                      const struct table_cell *cell,
487                      const char *tag, const struct table *t)
488 {
489   fprintf (html->file, "<%s", tag);
490
491   struct css_style style;
492   style_start (&style, html->file);
493
494   struct string body = DS_EMPTY_INITIALIZER;
495   bool numeric = pivot_value_format_body (cell->value, pt, &body);
496
497   enum table_halign halign = table_halign_interpret (cell->cell_style->halign,
498                                                      numeric);
499
500   switch (halign)
501     {
502     case TABLE_HALIGN_RIGHT:
503       put_style (&style, "text-align", "right");
504       break;
505     case TABLE_HALIGN_CENTER:
506       put_style (&style, "text-align", "center");
507       break;
508     default:
509       /* Do nothing */
510       break;
511     }
512
513   if (cell->options & TAB_ROTATE)
514     put_style (&style, "writing-mode", "sideways-lr");
515
516   if (cell->cell_style->valign != TABLE_VALIGN_TOP)
517     {
518       put_style (&style, "vertical-align",
519                  (cell->cell_style->valign == TABLE_VALIGN_BOTTOM
520                   ? "bottom" : "middle"));
521     }
522
523   const struct font_style *fs = cell->font_style;
524   char bgcolor[32];
525   if (format_color (fs->bg[cell->d[V][0] % 2],
526                     (struct cell_color) CELL_COLOR_WHITE,
527                     bgcolor, sizeof bgcolor))
528     put_style (&style, "background", bgcolor);
529
530   char fgcolor[32];
531   if (format_color (fs->fg[cell->d[V][0] % 2],
532                     (struct cell_color) CELL_COLOR_BLACK,
533                     fgcolor, sizeof fgcolor))
534     put_style (&style, "color", fgcolor);
535
536   if (fs->typeface)
537     {
538       put_style (&style, "font-family", "\"");
539       escape_string (html->file, fs->typeface, " ", "\n");
540       putc ('"', html->file);
541     }
542   if (fs->bold)
543     put_style (&style, "font-weight", "bold");
544   if (fs->italic)
545     put_style (&style, "font-style", "italic");
546   if (fs->underline)
547     put_style (&style, "text-decoration", "underline");
548   if (fs->size)
549     {
550       char buf[32];
551       snprintf (buf, sizeof buf, "%dpt", fs->size);
552       put_style (&style, "font-size", buf);
553     }
554
555   if (t && html->borders)
556     {
557       put_border (t, cell, &style, V, 0, 0, "top");
558       put_border (t, cell, &style, H, 0, 0, "left");
559
560       if (cell->d[V][1] == t->n[V])
561         put_border (t, cell, &style, V, 0, 1, "bottom");
562       if (cell->d[H][1] == t->n[H])
563         put_border (t, cell, &style, H, 1, 0, "right");
564     }
565   style_end (&style);
566
567   int colspan = table_cell_colspan (cell);
568   if (colspan > 1)
569     fprintf (html->file, " colspan=\"%d\"", colspan);
570
571   int rowspan = table_cell_rowspan (cell);
572   if (rowspan > 1)
573     fprintf (html->file, " rowspan=\"%d\"", rowspan);
574
575   putc ('>', html->file);
576
577   const char *s = ds_cstr (&body);
578   s += strspn (s, CC_SPACES);
579   escape_string (html->file, s, " ", "<br>");
580   ds_destroy (&body);
581
582   if (cell->value->n_subscripts)
583     {
584       fputs ("<sub>", html->file);
585       for (size_t i = 0; i < cell->value->n_subscripts; i++)
586         {
587           if (i)
588             putc (',', html->file);
589           escape_string (html->file, cell->value->subscripts[i],
590                          "&nbsp;", "<br>");
591         }
592       fputs ("</sub>", html->file);
593     }
594   if (cell->value->n_footnotes > 0)
595     {
596       fputs ("<sup>", html->file);
597       for (size_t i = 0; i < cell->value->n_footnotes; i++)
598         {
599           if (i > 0)
600             putc (',', html->file);
601
602           size_t idx = cell->value->footnote_indexes[i];
603           const struct pivot_footnote *f = pt->footnotes[idx];
604           char *marker = pivot_value_to_string (f->marker, pt);
605           escape_string (html->file, marker, " ", "<br>");
606           free (marker);
607         }
608       fputs ("</sup>", html->file);
609     }
610
611   /* output </th> or </td>. */
612   fprintf (html->file, "</%s>\n", tag);
613 }
614
615 static void
616 html_output_table_layer (struct html_driver *html, const struct pivot_table *pt,
617                          const size_t *layer_indexes)
618 {
619   struct table *title, *layers, *body, *caption, *footnotes;
620   pivot_output (pt, layer_indexes, true, &title, &layers, &body,
621                 &caption, &footnotes, NULL, NULL);
622
623   fputs ("<table", html->file);
624   if (pt->notes)
625     {
626       fputs (" title=\"", html->file);
627       escape_string (html->file, pt->notes, " ", "\n");
628       putc ('"', html->file);
629     }
630   fputs (">\n", html->file);
631
632   if (title)
633     {
634       struct table_cell cell;
635       table_get_cell (title, 0, 0, &cell);
636       html_put_table_cell (html, pt, &cell, "caption", NULL);
637     }
638
639   if (layers)
640     {
641       fputs ("<thead>\n", html->file);
642       for (size_t y = 0; y < layers->n[V]; y++)
643         {
644           fputs ("<tr>\n", html->file);
645
646           struct table_cell cell;
647           table_get_cell (layers, 0, y, &cell);
648           cell.d[H][1] = body->n[H];
649           html_put_table_cell (html, pt, &cell, "td", NULL);
650
651           fputs ("</tr>\n", html->file);
652         }
653       fputs ("</thead>\n", html->file);
654     }
655
656   fputs ("<tbody>\n", html->file);
657   for (int y = 0; y < body->n[V]; y++)
658     {
659       fputs ("<tr>\n", html->file);
660       for (int x = 0; x < body->n[H]; )
661         {
662           struct table_cell cell;
663           table_get_cell (body, x, y, &cell);
664           if (x == cell.d[TABLE_HORZ][0] && y == cell.d[TABLE_VERT][0])
665             {
666               bool is_header = (y < body->h[V][0]
667                                 || y >= body->n[V] - body->h[V][1]
668                                 || x < body->h[H][0]
669                                 || x >= body->n[H] - body->h[H][1]);
670               const char *tag = is_header ? "th" : "td";
671               html_put_table_cell (html, pt, &cell, tag, body);
672             }
673
674           x = cell.d[TABLE_HORZ][1];
675         }
676       fputs ("</tr>\n", html->file);
677     }
678   fputs ("</tbody>\n", html->file);
679
680   if (caption || footnotes)
681     {
682       fprintf (html->file, "<tfoot>\n");
683
684       if (caption)
685         {
686           fputs ("<tr>\n", html->file);
687
688           struct table_cell cell;
689           table_get_cell (caption, 0, 0, &cell);
690           cell.d[H][1] = body->n[H];
691           html_put_table_cell (html, pt, &cell, "td", NULL);
692
693           fputs ("</tr>\n", html->file);
694         }
695
696       if (footnotes)
697         for (size_t y = 0; y < footnotes->n[V]; y++)
698           {
699             fputs ("<tr>\n", html->file);
700
701             struct table_cell cell;
702             table_get_cell (footnotes, 0, y, &cell);
703             cell.d[H][1] = body->n[H];
704             html_put_table_cell (html, pt, &cell, "td", NULL);
705
706             fputs ("</tr>\n", html->file);
707           }
708       fputs ("</tfoot>\n", html->file);
709     }
710
711   fputs ("</table>\n\n", html->file);
712
713   table_unref (title);
714   table_unref (layers);
715   table_unref (body);
716   table_unref (caption);
717   table_unref (footnotes);
718 }
719
720 static void
721 html_output_table (struct html_driver *html, const struct table_item *item)
722 {
723   size_t *layer_indexes;
724   PIVOT_OUTPUT_FOR_EACH_LAYER (layer_indexes, item->pt, true)
725     html_output_table_layer (html, item->pt, layer_indexes);
726 }
727
728 struct output_driver_factory html_driver_factory =
729   { "html", "pspp.html", html_create };
730
731 static const struct output_driver_class html_driver_class =
732   {
733     "html",
734     html_destroy,
735     html_submit,
736     NULL,
737   };