+ next_style (style);
+ fprintf (style->file, "border-%s: %s", border_name, css);
+
+ char buf[32];
+ if (format_color (color, (struct cell_color) CELL_COLOR_BLACK,
+ buf, sizeof buf))
+ fprintf (style->file, " %s", buf);
+ }
+}
+
+static void
+html_put_table_cell (struct html_driver *html, const struct pivot_table *pt,
+ const struct table_cell *cell,
+ const char *tag, const struct table *t)
+{
+ fprintf (html->file, "<%s", tag);
+
+ struct css_style style;
+ style_start (&style, html->file);
+
+ struct string body = DS_EMPTY_INITIALIZER;
+ bool numeric = pivot_value_format_body (cell->value, pt, &body);
+
+ enum table_halign halign = table_halign_interpret (cell->cell_style->halign,
+ numeric);
+
+ switch (halign)
+ {
+ case TABLE_HALIGN_RIGHT:
+ put_style (&style, "text-align", "right");
+ break;
+ case TABLE_HALIGN_CENTER:
+ put_style (&style, "text-align", "center");
+ break;
+ default:
+ /* Do nothing */
+ break;
+ }
+
+ if (cell->options & TAB_ROTATE)
+ put_style (&style, "writing-mode", "sideways-lr");
+
+ if (cell->cell_style->valign != TABLE_VALIGN_TOP)
+ {
+ put_style (&style, "vertical-align",
+ (cell->cell_style->valign == TABLE_VALIGN_BOTTOM
+ ? "bottom" : "middle"));
+ }
+
+ const struct font_style *fs = cell->font_style;
+ char bgcolor[32];
+ if (format_color (fs->bg[cell->d[V][0] % 2],
+ (struct cell_color) CELL_COLOR_WHITE,
+ bgcolor, sizeof bgcolor))
+ put_style (&style, "background", bgcolor);
+
+ char fgcolor[32];
+ if (format_color (fs->fg[cell->d[V][0] % 2],
+ (struct cell_color) CELL_COLOR_BLACK,
+ fgcolor, sizeof fgcolor))
+ put_style (&style, "color", fgcolor);
+
+ if (fs->typeface)
+ {
+ put_style (&style, "font-family", "\"");
+ escape_string (html->file, fs->typeface, " ", "\n");
+ putc ('"', html->file);
+ }
+ if (fs->bold)
+ put_style (&style, "font-weight", "bold");
+ if (fs->italic)
+ put_style (&style, "font-style", "italic");
+ if (fs->underline)
+ put_style (&style, "text-decoration", "underline");
+ if (fs->size)
+ {
+ char buf[32];
+ snprintf (buf, sizeof buf, "%dpt", fs->size);
+ put_style (&style, "font-size", buf);
+ }
+
+ if (t && html->borders)
+ {
+ put_border (t, cell, &style, V, 0, 0, "top");
+ put_border (t, cell, &style, H, 0, 0, "left");
+
+ if (cell->d[V][1] == t->n[V])
+ put_border (t, cell, &style, V, 0, 1, "bottom");
+ if (cell->d[H][1] == t->n[H])
+ put_border (t, cell, &style, H, 1, 0, "right");
+ }
+ style_end (&style);
+
+ int colspan = table_cell_colspan (cell);
+ if (colspan > 1)
+ fprintf (html->file, " colspan=\"%d\"", colspan);
+
+ int rowspan = table_cell_rowspan (cell);
+ if (rowspan > 1)
+ fprintf (html->file, " rowspan=\"%d\"", rowspan);
+
+ putc ('>', html->file);
+
+ const char *s = ds_cstr (&body);
+ s += strspn (s, CC_SPACES);
+ escape_string (html->file, s, " ", "<br>");
+ ds_destroy (&body);
+
+ if (cell->value->n_subscripts)
+ {
+ fputs ("<sub>", html->file);
+ for (size_t i = 0; i < cell->value->n_subscripts; i++)
+ {
+ if (i)
+ putc (',', html->file);
+ escape_string (html->file, cell->value->subscripts[i],
+ " ", "<br>");
+ }
+ fputs ("</sub>", html->file);
+ }
+ if (cell->value->n_footnotes > 0)
+ {
+ fputs ("<sup>", html->file);
+ size_t n_footnotes = 0;
+ for (size_t i = 0; i < cell->value->n_footnotes; i++)
+ {
+ const struct pivot_footnote *f
+ = pt->footnotes[cell->value->footnote_indexes[i]];
+ if (f->show)
+ {
+ if (n_footnotes++ > 0)
+ putc (',', html->file);
+
+ char *marker = pivot_footnote_marker_string (f, pt);
+ escape_string (html->file, marker, " ", "<br>");
+ free (marker);
+ }
+ }
+ fputs ("</sup>", html->file);
+ }
+
+ /* output </th> or </td>. */
+ fprintf (html->file, "</%s>\n", tag);
+}
+
+static void
+html_output_table_layer (struct html_driver *html, const struct pivot_table *pt,
+ const size_t *layer_indexes)
+{
+ struct table *title, *layers, *body, *caption, *footnotes;
+ pivot_output (pt, layer_indexes, true, &title, &layers, &body,
+ &caption, &footnotes, NULL, NULL);
+
+ fputs ("<table", html->file);
+ if (pt->notes)
+ {
+ fputs (" title=\"", html->file);
+ escape_string (html->file, pt->notes, " ", "\n");
+ putc ('"', html->file);
+ }
+ fputs (">\n", html->file);
+
+ if (title)
+ {
+ struct table_cell cell;
+ table_get_cell (title, 0, 0, &cell);
+ html_put_table_cell (html, pt, &cell, "caption", NULL);
+ }
+
+ if (layers)
+ {
+ fputs ("<thead>\n", html->file);
+ for (size_t y = 0; y < layers->n[V]; y++)
+ {
+ fputs ("<tr>\n", html->file);
+
+ struct table_cell cell;
+ table_get_cell (layers, 0, y, &cell);
+ cell.d[H][1] = body->n[H];
+ html_put_table_cell (html, pt, &cell, "td", NULL);
+
+ fputs ("</tr>\n", html->file);
+ }
+ fputs ("</thead>\n", html->file);
+ }
+
+ fputs ("<tbody>\n", html->file);
+ for (int y = 0; y < body->n[V]; y++)
+ {
+ fputs ("<tr>\n", html->file);
+ for (int x = 0; x < body->n[H]; )