pivot-table: Implement hiding footnotes.
[pspp] / src / output / tex.c
index cd90c3ab468855bedf7fce436954e00f8a9ad72c..c5591011f8e81ea99b57d78897c0cf5f63fa8648 100644 (file)
 #include "libpspp/message.h"
 #include "libpspp/temp-file.h"
 #include "libpspp/version.h"
-#ifdef HAVE_CAIRO
 #include "output/cairo-chart.h"
-#endif
 #include "output/chart-item.h"
 #include "output/driver-provider.h"
+#include "output/image-item.h"
 #include "output/message-item.h"
 #include "output/options.h"
 #include "output/output-item-provider.h"
+#include "output/pivot-output.h"
+#include "output/pivot-table.h"
 #include "output/table-provider.h"
 #include "output/table-item.h"
 #include "output/text-item.h"
@@ -73,10 +74,8 @@ struct tex_driver
     /* A hash table containing any Tex macros which need to be emitted.  */
     struct hmap macros;
     bool require_graphics;
-#ifdef HAVE_CAIRO
     struct cell_color fg;
     struct cell_color bg;
-#endif
     struct file_handle *handle;
     char *chart_file_name;
 
@@ -138,10 +137,8 @@ tex_create (struct file_handle *fh, enum settings_output_devices device_type,
   tex->chart_file_name = parse_chart_file_name (opt (d, o, "charts",
                                                       fh_get_file_name (fh)));
   tex->chart_cnt = 1;
-#ifdef HAVE_CAIRO
   tex->bg = parse_color (opt (d, o, "background-color", "#FFFFFFFFFFFF"));
   tex->fg = parse_color (opt (d, o, "foreground-color", "#000000000000"));
-#endif
 
   tex->file = fn_open (tex->handle, "w");
   if (tex->file == NULL)
@@ -321,7 +318,18 @@ tex_submit (struct output_driver *driver,
       struct table_item *table_item = to_table_item (output_item);
       tex_output_table (tex, table_item);
     }
-#ifdef HAVE_CAIRO
+  else if (is_image_item (output_item) && tex->chart_file_name != NULL)
+    {
+      struct image_item *image_item = to_image_item (output_item);
+      char *file_name = xr_write_png_image (
+        image_item->image, tex->chart_file_name, tex->chart_cnt++);
+      if (file_name != NULL)
+        {
+          shipout (&tex->token_list, "\\includegraphics{%s}\n", file_name);
+          tex->require_graphics = true;
+          free (file_name);
+        }
+    }
   else if (is_chart_item (output_item) && tex->chart_file_name != NULL)
     {
       struct chart_item *chart_item = to_chart_item (output_item);
@@ -339,7 +347,6 @@ tex_submit (struct output_driver *driver,
           free (file_name);
         }
     }
-#endif  /* HAVE_CAIRO */
   else if (is_text_item (output_item))
     {
       struct text_item *text_item = to_text_item (output_item);
@@ -378,71 +385,97 @@ tex_submit (struct output_driver *driver,
 
 static void
 tex_put_footnote_markers (struct tex_driver *tex,
-                          struct footnote **footnotes,
+                          const struct pivot_table *pt,
+                          const size_t *footnote_indexes,
                           size_t n_footnotes)
 {
-  if (n_footnotes > 0)
-    shipout (&tex->token_list, "$^{");
+  size_t n_visible = 0;
   for (size_t i = 0; i < n_footnotes; i++)
     {
-      const struct footnote *f = footnotes[i];
+      const struct pivot_footnote *f = pt->footnotes[footnote_indexes[i]];
+      if (f->show)
+        {
+          if (!n_visible++)
+            shipout (&tex->token_list, "$^{");
 
-      tex_escape_string (tex, f->marker, true);
+          char *marker = pivot_footnote_marker_string (f, pt);
+          tex_escape_string (tex, marker, true);
+          free (marker);
+        }
     }
-  if (n_footnotes > 0)
+  if (n_visible)
     shipout (&tex->token_list, "}$");
 }
 
 static void
-tex_put_table_cell (struct tex_driver *tex, const struct table_cell *cell)
+tex_put_table_cell (struct tex_driver *tex, const struct pivot_table *pt,
+                    const struct table_cell *cell)
 {
-  tex_escape_string (tex, cell->text, false);
-  tex_put_footnote_markers (tex, cell->footnotes, cell->n_footnotes);
+  struct string s = DS_EMPTY_INITIALIZER;
+  pivot_value_format_body (cell->value, pt, &s);
+  tex_escape_string (tex, ds_cstr (&s), false);
+  ds_destroy (&s);
+
+  tex_put_footnote_markers (tex, pt,
+                            cell->value->footnote_indexes,
+                            cell->value->n_footnotes);
 }
 
 static void
-tex_output_table (struct tex_driver *tex, const struct table_item *item)
+tex_output_table_layer (struct tex_driver *tex, const struct pivot_table *pt,
+                        const size_t *layer_indexes)
 {
   /* Tables are rendered in TeX with the \halign command.
      This is described in the TeXbook Ch. 22 */
-
-  const struct table *t = table_item_get_table (item);
+  struct table *title, *layers, *body, *caption;
+  struct pivot_footnote **footnotes;
+  size_t n_footnotes;
+  pivot_output (pt, layer_indexes, true, &title, &layers, &body,
+                &caption, NULL, &footnotes, &n_footnotes);
 
   shipout (&tex->token_list, "\n{\\parindent=0pt\n");
 
-  const struct table_item_text *caption = table_item_get_caption (item);
   if (caption)
     {
       shipout (&tex->token_list, "{\\sl ");
-      tex_escape_string (tex, caption->content, false);
+      struct table_cell cell;
+      table_get_cell (caption, 0, 0, &cell);
+      tex_put_table_cell (tex, pt, &cell);
       shipout (&tex->token_list, "}\n\n");
     }
-  struct footnote **f;
-  size_t n_footnotes = table_collect_footnotes (item, &f);
 
-  const struct table_cell *title = table_item_get_title (item);
-  const struct table_item_layers *layers = table_item_get_layers (item);
   if (title || layers)
     {
       if (title)
         {
           shipout (&tex->token_list, "{\\bf ");
-          tex_put_table_cell (tex, title);
-          shipout (&tex->token_list, "}");
+          struct table_cell cell;
+          table_get_cell (title, 0, 0, &cell);
+          tex_put_table_cell (tex, pt, &cell);
+          shipout (&tex->token_list, "}\\par\n");
         }
+
       if (layers)
-        abort ();
-      shipout (&tex->token_list, "\\par\n");
+        {
+          for (size_t y = 0; y < layers->n[V]; y++)
+            {
+              shipout (&tex->token_list, "{");
+              struct table_cell cell;
+              table_get_cell (layers, 0, y, &cell);
+              tex_put_table_cell (tex, pt, &cell);
+              shipout (&tex->token_list, "}\\par\n");
+            }
+        }
     }
 
   shipout (&tex->token_list, "\\offinterlineskip\\halign{\\strut%%\n");
 
   /* Generate the preamble */
-  for (int x = 0; x < t->n[H]; ++x)
+  for (int x = 0; x < body->n[H]; ++x)
     {
-      shipout (&tex->token_list, "{\\vbox{\\cell{%d}#}}", t->n[H]);
+      shipout (&tex->token_list, "{\\vbox{\\cell{%d}#}}", body->n[H]);
 
-      if (x < t->n[H] - 1)
+      if (x < body->n[H] - 1)
         {
           shipout (&tex->token_list, "\\hskip\\psppcolumnspace\\hfil");
           shipout (&tex->token_list, "&\\vrule\n");
@@ -452,17 +485,17 @@ tex_output_table (struct tex_driver *tex, const struct table_item *item)
     }
 
   /* Emit the row data */
-  for (int y = 0; y < t->n[V]; y++)
+  for (int y = 0; y < body->n[V]; y++)
     {
       enum { H = TABLE_HORZ, V = TABLE_VERT };
-      bool is_column_header = y < t->h[V][0] || y >= t->n[V] - t->h[V][1];
+      bool is_column_header = y < body->h[V][0] || y >= body->n[V] - body->h[V][1];
       int prev_x = -1;
       int skipped = 0;
-      for (int x = 0; x < t->n[H];)
+      for (int x = 0; x < body->n[H];)
         {
           struct table_cell cell;
 
-          table_get_cell (t, x, y, &cell);
+          table_get_cell (body, x, y, &cell);
 
           int colspan = table_cell_colspan (&cell);
           if (x > 0)
@@ -475,14 +508,16 @@ tex_output_table (struct tex_driver *tex, const struct table_item *item)
           if (x != cell.d[TABLE_HORZ][0] || y != cell.d[TABLE_VERT][0])
             goto next_1;
 
-          /* bool is_header = (y < t->h[V][0] */
-          /*                   || y >= t->n[V] - t->h[V][1] */
-          /*                   || x < t->h[H][0] */
-          /*                   || x >= t->n[H] - t->h[H][1]); */
+          /* bool is_header = (y < body->h[V][0] */
+          /*                   || y >= body->n[V] - body->h[V][1] */
+          /*                   || x < body->h[H][0] */
+          /*                   || x >= body->n[H] - body->h[H][1]); */
+
+          struct string s = DS_EMPTY_INITIALIZER;
+          bool numeric = pivot_value_format_body (cell.value, pt, &s);
 
-          enum table_halign halign =
-            table_halign_interpret (cell.style->cell_style.halign,
-                                    cell.options & TAB_NUMERIC);
+          enum table_halign halign = table_halign_interpret (
+            cell.cell_style->halign, numeric);
 
           /* int rowspan = table_cell_rowspan (&cell); */
 
@@ -504,8 +539,11 @@ tex_output_table (struct tex_driver *tex, const struct table_item *item)
             shipout (&tex->token_list, "\\right{");
 
           /* Output cell contents. */
-          tex_escape_string (tex, cell.text, true);
-          tex_put_footnote_markers (tex, cell.footnotes, cell.n_footnotes);
+          tex_escape_string (tex, ds_cstr (&s), true);
+          ds_destroy (&s);
+
+          tex_put_footnote_markers (tex, pt, cell.value->footnote_indexes,
+                                    cell.value->n_footnotes);
           if (halign == TABLE_HALIGN_CENTER || halign == TABLE_HALIGN_RIGHT)
             {
               shipout (&tex->token_list, "}");
@@ -529,14 +567,33 @@ tex_output_table (struct tex_driver *tex, const struct table_item *item)
 
   for (int i = 0; i < n_footnotes; ++i)
     {
+      char *marker = pivot_footnote_marker_string (footnotes[i], pt);
+      char *content = pivot_value_to_string (footnotes[i]->content, pt);
+
       shipout (&tex->token_list, "$^{");
-      tex_escape_string (tex, f[i]->marker, false);
+      tex_escape_string (tex, marker, false);
       shipout (&tex->token_list, "}$");
-      tex_escape_string (tex, f[i]->content, false);
+      tex_escape_string (tex, content, false);
+
+      free (content);
+      free (marker);
     }
-  free (f);
 
   shipout (&tex->token_list, "}\n\\vskip 3ex\n\n");
+
+  table_unref (title);
+  table_unref (layers);
+  table_unref (body);
+  table_unref (caption);
+  free (footnotes);
+}
+
+static void
+tex_output_table (struct tex_driver *tex, const struct table_item *item)
+{
+  size_t *layer_indexes;
+  PIVOT_OUTPUT_FOR_EACH_LAYER (layer_indexes, item->pt, true)
+    tex_output_table_layer (tex, item->pt, layer_indexes);
 }
 
 struct output_driver_factory tex_driver_factory =