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.
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.
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.
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/>. */
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"
37 #include "output/cairo-chart.h"
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"
48 #include "gl/minmax.h"
49 #include "gl/xalloc.h"
52 #define _(msgid) gettext (msgid)
54 /* This file uses TABLE_HORZ and TABLE_VERT enough to warrant abbreviating. */
60 struct output_driver driver;
65 struct file_handle *handle;
66 char *chart_file_name;
76 static const struct output_driver_class html_driver_class;
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,
84 static struct html_driver *
85 html_driver_cast (struct output_driver *driver)
87 assert (driver->class == &html_driver_class);
88 return UP_CAST (driver, struct html_driver, driver);
91 static struct driver_option *
92 opt (struct output_driver *d, struct string_map *options, const char *key,
93 const char *default_value)
95 return driver_option_get (d, options, key, default_value);
99 put_header (struct html_driver *html)
101 fputs ("<!doctype html>\n", html->file);
102 fprintf (html->file, "<html");
103 char *ln = get_language ();
105 fprintf (html->file, " lang=\"%s\"", 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);
119 " background: white;\n"
121 " padding: 0em 12em 0em 3em;\n"
125 " margin: 0pt 0pt 0pt 0em\n"
128 " text-indent: 1.5em;\n"
131 " font-size: 150%;\n"
132 " margin-left: -1.33em\n"
135 " font-size: 125%;\n"
136 " font-weight: bold;\n"
137 " margin-left: -.8em\n"
140 " font-size: 100%;\n"
141 " font-weight: bold;\n"
142 " margin-left: -.5em }\n"
144 " font-size: 100%;\n"
145 " margin-left: 0em\n"
147 "h1, h2, h3, h4, h5, h6 {\n"
148 " font-family: sans-serif;\n"
155 " font-family: sans-serif\n"
158 " border-collapse: collapse;\n"
159 " margin-bottom: 1em\n"
162 " text-align: left\n"
164 "th { font-weight: normal }\n"
178 fputs ("</head>\n", html->file);
179 fputs ("<body>\n", html->file);
182 static struct output_driver *
183 html_create (struct file_handle *fh, enum settings_output_devices device_type,
184 struct string_map *o)
186 struct output_driver *d;
187 struct html_driver *html;
189 html = xzalloc (sizeof *html);
191 output_driver_init (&html->driver, &html_driver_class, fh_get_file_name (fh),
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"));
198 html->chart_file_name = parse_chart_file_name (opt (d, o, "charts",
199 fh_get_file_name (fh)));
203 html->bg = parse_color (opt (d, o, "background-color", "#FFFFFFFFFFFF"));
204 html->fg = parse_color (opt (d, o, "foreground-color", "#000000000000"));
206 html->file = fn_open (html->handle, "w");
207 if (html->file == NULL)
209 msg_error (errno, _("error opening output file `%s'"), fh_get_file_name (html->handle));
219 output_driver_destroy (d);
223 /* Emits <NAME>CONTENT</NAME> to the output, escaping CONTENT as
224 necessary for HTML. */
226 print_title_tag (FILE *file, const char *name, const char *content)
230 fprintf (file, "<%s>", name);
231 escape_string (file, content, " ", " - ");
232 fprintf (file, "</%s>\n", name);
237 html_destroy (struct output_driver *driver)
239 struct html_driver *html = html_driver_cast (driver);
241 if (html->file != NULL)
247 "<!-- end of file -->\n");
248 fn_close (html->handle, html->file);
250 free (html->chart_file_name);
251 fh_unref (html->handle);
256 html_submit (struct output_driver *driver,
257 const struct output_item *output_item)
259 struct html_driver *html = html_driver_cast (driver);
261 if (is_table_item (output_item))
263 struct table_item *table_item = to_table_item (output_item);
264 html_output_table (html, table_item);
267 else if (is_chart_item (output_item) && html->chart_file_name != NULL)
269 struct chart_item *chart_item = to_chart_item (output_item);
272 file_name = xr_draw_png_chart (chart_item, html->chart_file_name,
277 if (file_name != NULL)
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"));
285 #endif /* HAVE_CAIRO */
286 else if (is_text_item (output_item))
288 struct text_item *text_item = to_text_item (output_item);
289 const char *s = text_item_get_text (text_item);
291 switch (text_item_get_type (text_item))
293 case TEXT_ITEM_PAGE_TITLE:
296 case TEXT_ITEM_TITLE:
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);
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");
311 print_title_tag (html->file, "pre", s); /* should be <P><TT> */
315 else if (is_message_item (output_item))
317 const struct message_item *message_item = to_message_item (output_item);
318 char *s = msg_to_string (message_item_get_msg (message_item));
319 print_title_tag (html->file, "p", s);
324 /* Write TEXT to file F, escaping characters as necessary for HTML. Spaces are
325 replaced by SPACE, which should be " " or " " New-lines are replaced by
326 NEWLINE, which might be "<BR>" or "\n" or something else appropriate. */
328 escape_string (FILE *file, const char *text,
329 const char *space, const char *newline)
339 fputs (newline, file);
342 fputs ("&", file);
345 fputs ("<", file);
348 fputs (">", file);
354 fputs (""", file);
364 escape_tag (FILE *file, const char *tag,
365 const char *text, const char *space, const char *newline)
370 fprintf (file, "<%s>", tag);
371 escape_string (file, text, space, newline);
372 fprintf (file, "</%s>", tag);
376 border_to_css (int border)
380 case TABLE_STROKE_NONE:
383 case TABLE_STROKE_SOLID:
386 case TABLE_STROKE_DASHED:
389 case TABLE_STROKE_THICK:
390 return "thick solid";
392 case TABLE_STROKE_THIN:
395 case TABLE_STROKE_DOUBLE:
411 style_start (struct css_style *cs, FILE *file)
413 *cs = (struct css_style) {
420 style_end (struct css_style *cs)
422 if (cs->n_styles > 0)
423 fputs ("'", cs->file);
427 put_style (struct css_style *st, const char *name, const char *value)
429 bool first = !st->n_styles++;
430 fprintf (st->file, "%s%s: %s", first ? " style='" : "; ", name, value);
434 format_color (const struct cell_color color,
435 const struct cell_color default_color,
436 char *buf, size_t bufsize)
438 bool retval = !cell_color_equal (&color, &default_color);
441 if (color.alpha == 255)
442 snprintf (buf, bufsize, "#%02x%02x%02x", color.r, color.g, color.b);
444 snprintf (buf, bufsize, "rgba(%d, %d, %d, %.3f)",
445 color.r, color.g, color.b, color.alpha / 255.0);
451 put_border (struct css_style *st, int style, const struct cell_color *color,
452 const char *border_name)
454 const char *css = border_to_css (style);
457 if (st->n_styles++ > 0)
458 fputs ("; ", st->file);
459 fprintf (st->file, "border-%s: %s", border_name, css);
462 if (format_color (*color, (struct cell_color) CELL_COLOR_BLACK,
464 fprintf (st->file, " %s", buf);
469 put_tfoot (struct html_driver *html, const struct table *t, bool *tfoot)
473 fputs ("<tfoot>\n", html->file);
474 fputs ("<tr>\n", html->file);
475 fprintf (html->file, "<td colspan=%d>\n", t->n[H]);
479 fputs ("\n<br>", html->file);
483 html_put_footnote_markers (struct html_driver *html,
484 const struct footnote **footnotes,
489 fputs ("<sup>", html->file);
490 for (size_t i = 0; i < n_footnotes; i++)
492 const struct footnote *f = footnotes[i];
495 putc (',', html->file);
496 escape_string (html->file, f->marker, " ", "<br>");
498 fputs ("</sup>", html->file);
503 html_put_table_item_text (struct html_driver *html,
504 const struct table_item_text *text)
506 escape_string (html->file, text->content, " ", "<br>");
507 html_put_footnote_markers (html, text->footnotes, text->n_footnotes);
511 html_put_table_cell_text (struct html_driver *html,
512 const struct table_cell *cell)
514 const char *s = cell->text;
515 if (cell->options & TAB_FIX)
516 escape_tag (html->file, "tt", s, " ", "<br>");
519 s += strspn (s, CC_SPACES);
520 escape_string (html->file, s, " ", "<br>");
523 if (cell->n_subscripts)
525 fputs ("<sub>", html->file);
526 for (size_t i = 0; i < cell->n_subscripts; i++)
529 putc (',', html->file);
530 escape_string (html->file, cell->subscripts[i],
533 fputs ("</sub>", html->file);
535 html_put_footnote_markers (html, cell->footnotes, cell->n_footnotes);
539 html_put_table_item_layers (struct html_driver *html,
540 const struct table_item_layers *layers)
542 for (size_t i = 0; i < layers->n_layers; i++)
545 fputs ("<br>\n", html->file);
547 const struct table_item_layer *layer = &layers->layers[i];
548 escape_string (html->file, layer->content, " ", "<br>");
549 html_put_footnote_markers (html, layer->footnotes, layer->n_footnotes);
554 html_output_table (struct html_driver *html, const struct table_item *item)
556 const struct table *t = table_item_get_table (item);
560 fputs ("<table", html->file);
563 fputs (" title=\"", html->file);
564 escape_string (html->file, item->notes, " ", "\n");
565 putc ('"', html->file);
567 fputs (">\n", html->file);
569 const struct table_item_text *caption = table_item_get_caption (item);
572 put_tfoot (html, t, &tfoot);
573 html_put_table_item_text (html, caption);
575 const struct footnote **f;
576 size_t n_footnotes = table_collect_footnotes (item, &f);
578 for (size_t i = 0; i < n_footnotes; i++)
580 put_tfoot (html, t, &tfoot);
581 escape_tag (html->file, "sup", f[i]->marker, " ", "<br>");
582 escape_string (html->file, f[i]->content, " ", "<br>");
587 fputs ("</td>\n", html->file);
588 fputs ("</tr>\n", html->file);
589 fputs ("</tfoot>\n", html->file);
592 const struct table_item_text *title = table_item_get_title (item);
593 const struct table_item_layers *layers = table_item_get_layers (item);
596 fputs ("<caption>", html->file);
598 html_put_table_item_text (html, title);
600 fputs ("<br>\n", html->file);
602 html_put_table_item_layers (html, layers);
603 fputs ("</caption>\n", html->file);
606 fputs ("<tbody>\n", html->file);
608 for (y = 0; y < t->n[V]; y++)
612 fputs ("<tr>\n", html->file);
613 for (x = 0; x < t->n[H];)
615 struct table_cell cell;
618 table_get_cell (t, x, y, &cell);
619 if (x != cell.d[TABLE_HORZ][0] || y != cell.d[TABLE_VERT][0])
622 /* output <td> or <th> tag. */
623 bool is_header = (y < t->h[V][0]
624 || y >= t->n[V] - t->h[V][1]
626 || x >= t->n[H] - t->h[H][1]);
627 tag = is_header ? "th" : "td";
628 fprintf (html->file, "<%s", tag);
630 struct css_style style;
631 style_start (&style, html->file);
632 enum table_halign halign = table_halign_interpret (
633 cell.style->cell_style.halign, cell.options & TAB_NUMERIC);
637 case TABLE_HALIGN_RIGHT:
638 put_style (&style, "text-align", "right");
640 case TABLE_HALIGN_CENTER:
641 put_style (&style, "text-align", "center");
648 if (cell.options & TAB_ROTATE)
649 put_style (&style, "writing-mode", "sideways-lr");
651 if (cell.style->cell_style.valign != TABLE_VALIGN_TOP)
653 put_style (&style, "vertical-align",
654 (cell.style->cell_style.valign == TABLE_VALIGN_BOTTOM
655 ? "bottom" : "middle"));
658 const struct font_style *fs = &cell.style->font_style;
660 if (format_color (fs->bg[y % 2],
661 (struct cell_color) CELL_COLOR_WHITE,
662 bgcolor, sizeof bgcolor))
663 put_style (&style, "background", bgcolor);
666 if (format_color (fs->fg[y % 2],
667 (struct cell_color) CELL_COLOR_BLACK,
668 fgcolor, sizeof fgcolor))
669 put_style (&style, "color", fgcolor);
673 put_style (&style, "font-family", "\"");
674 escape_string (html->file, fs->typeface, " ", "\n");
675 putc ('"', html->file);
678 put_style (&style, "font-weight", "bold");
680 put_style (&style, "font-style", "italic");
682 put_style (&style, "text-decoration", "underline");
686 snprintf (buf, sizeof buf, "%dpt", fs->size);
687 put_style (&style, "font-size", buf);
690 int colspan = table_cell_colspan (&cell);
691 int rowspan = table_cell_rowspan (&cell);
696 struct cell_color color;
698 int top = table_get_rule (t, TABLE_VERT, x, y, &color);
699 put_border (&style, top, &color, "top");
701 if (y + rowspan == t->n[V])
703 int bottom = table_get_rule (t, TABLE_VERT, x, y + rowspan,
705 put_border (&style, bottom, &color, "bottom");
708 int left = table_get_rule (t, TABLE_HORZ, x, y, &color);
709 put_border (&style, left, &color, "left");
711 if (x + colspan == t->n[H])
713 int right = table_get_rule (t, TABLE_HORZ, x + colspan, y,
715 put_border (&style, right, &color, "right");
721 fprintf (html->file, " colspan=\"%d\"", colspan);
724 fprintf (html->file, " rowspan=\"%d\"", rowspan);
726 putc ('>', html->file);
728 html_put_table_cell_text (html, &cell);
730 /* output </th> or </td>. */
731 fprintf (html->file, "</%s>\n", tag);
734 x = cell.d[TABLE_HORZ][1];
736 fputs ("</tr>\n", html->file);
739 fputs ("</tbody>\n", html->file);
740 fputs ("</table>\n\n", html->file);
743 struct output_driver_factory html_driver_factory =
744 { "html", "pspp.html", html_create };
746 static const struct output_driver_class html_driver_class =