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