pspp-output: Add new --nth-commands option.
[pspp] / src / output / pivot-output.c
index d805f42bb36e87bb77b4c0d15a4e9b289b18c719..158116117f02b0a10ee098d77e8c3d776798fdf8 100644 (file)
@@ -23,7 +23,6 @@
 #include "data/settings.h"
 #include "libpspp/assertion.h"
 #include "libpspp/pool.h"
-#include "output/tab.h"
 #include "output/table.h"
 #include "output/table-item.h"
 #include "output/text-item.h"
@@ -91,7 +90,7 @@ area_style_override (struct pool *pool,
 }
 
 static void
-fill_cell (struct tab_table *t, int x1, int y1, int x2, int y2,
+fill_cell (struct table *t, int x1, int y1, int x2, int y2,
            const struct area_style *style, int style_idx,
            const struct pivot_value *value, struct footnote **footnotes,
            enum settings_value_show show_values,
@@ -112,19 +111,27 @@ fill_cell (struct tab_table *t, int x1, int y1, int x2, int y2,
       if (rotate_label)
         opts |= TAB_ROTATE;
     }
-  tab_joint_text (t, x1, y1, x2, y2, opts, ds_cstr (&s));
+  table_joint_text (t, x1, y1, x2, y2, opts, ds_cstr (&s));
   ds_destroy (&s);
 
   if (value)
     {
       if (value->cell_style || value->font_style)
-        tab_add_style (t, x1, y1,
-                       area_style_override (t->container, style,
-                                            value->cell_style,
-                                            value->font_style));
+        table_add_style (t, x1, y1,
+                         area_style_override (t->container, style,
+                                              value->cell_style,
+                                              value->font_style));
 
       for (size_t i = 0; i < value->n_footnotes; i++)
-        tab_add_footnote (t, x1, y1, footnotes[value->footnotes[i]->idx]);
+        {
+          struct footnote *f = footnotes[value->footnotes[i]->idx];
+          if (f)
+            table_add_footnote (t, x1, y1, f);
+        }
+
+      if (value->n_subscripts)
+        table_add_subscripts (t, x1, y1,
+                              value->subscripts, value->n_subscripts);
     }
 }
 
@@ -145,13 +152,16 @@ pivot_value_to_table_item_text (const struct pivot_value *value,
   *text = (struct table_item_text) {
     .content = ds_steal_cstr (&s),
     .footnotes = xnmalloc (value->n_footnotes, sizeof *text->footnotes),
-    .n_footnotes = value->n_footnotes,
     .style = area_style_override (
       NULL, area, value->cell_style, value->font_style),
   };
 
   for (size_t i = 0; i < value->n_footnotes; i++)
-    text->footnotes[i] = footnotes[value->footnotes[i]->idx];
+    {
+      struct footnote *f = footnotes[value->footnotes[i]->idx];
+      if (f)
+        text->footnotes[text->n_footnotes++] = f;
+    }
 
   return text;
 }
@@ -164,19 +174,19 @@ get_table_rule (const struct table_border_style *styles,
 }
 
 static void
-draw_line (struct tab_table *t, const struct table_border_style *styles,
+draw_line (struct table *t, const struct table_border_style *styles,
            enum pivot_border style_idx,
            enum table_axis axis, int a, int b0, int b1)
 {
   int rule = get_table_rule (styles, style_idx);
   if (axis == H)
-    tab_hline (t, rule, b0, b1, a);
+    table_hline (t, rule, b0, b1, a);
   else
-    tab_vline (t, rule, a, b0, b1);
+    table_vline (t, rule, a, b0, b1);
 }
 
 static void
-compose_headings (struct tab_table *t,
+compose_headings (struct table *t,
                   const struct pivot_axis *a_axis, enum table_axis a,
                   const struct pivot_axis *b_axis,
                   const struct table_border_style *borders,
@@ -209,7 +219,7 @@ compose_headings (struct tab_table *t,
 
       for (int row_ofs = 0; row_ofs < d->label_depth; row_ofs++)
         {
-          for (size_t x1 = 0; x1 < n_columns; )
+          for (size_t x1 = 0; x1 < n_columns;)
             {
               const struct pivot_category *c = find_category (
                 d, dim_index, column_enumeration + x1 * stride, row_ofs);
@@ -252,7 +262,7 @@ compose_headings (struct tab_table *t,
                            ? dim_col_vert
                            : cat_col_vert);
                       draw_line (t, borders, style, b, x2 + a_ofs, y1,
-                                 t->table.n[b] - 1);
+                                 t->n[b] - 1);
                     }
                   if (pivot_category_is_leaf (c) && x1 > 0)
                     {
@@ -261,7 +271,7 @@ compose_headings (struct tab_table *t,
                            ? dim_col_vert
                            : cat_col_vert);
                       draw_line (t, borders, style, b, x1 + a_ofs, y1,
-                                 t->table.n[b] - 1);
+                                 t->n[b] - 1);
                     }
                 }
               if (c->parent && c->parent->show_label)
@@ -286,7 +296,7 @@ compose_headings (struct tab_table *t,
 
       if (dim_index > 1)
         draw_line (t, borders, dim_col_horz, a, bottom_row + 1, a_ofs,
-                   t->table.n[a] - 1);
+                   t->n[a] - 1);
 
       bottom_row -= d->label_depth;
     }
@@ -309,9 +319,9 @@ pivot_table_submit_layer (const struct pivot_table *pt,
     [H] = pt->axes[PIVOT_AXIS_ROW].label_depth,
     [V] = pt->axes[PIVOT_AXIS_COLUMN].label_depth,
   };
-  struct tab_table *table = tab_create (body[H] + stub[H],
-                                        body[V] + stub[V]);
-  tab_headers (table, stub[H], 0, stub[V], 0);
+  struct table *table = table_create (body[H] + stub[H],
+                                      body[V] + stub[V],
+                                      stub[H], 0, stub[V], 0);
 
   for (size_t i = 0; i < PIVOT_N_AREAS; i++)
     table->styles[i] = area_style_override (table->container, &pt->areas[i],
@@ -329,18 +339,23 @@ pivot_table_submit_layer (const struct pivot_table *pt,
       out->b = in->color.b;
     }
 
-  struct footnote **footnotes = xcalloc (pt->n_footnotes, sizeof *footnotes);
+  struct footnote **footnotes = XCALLOC (pt->n_footnotes,  struct footnote *);
   for (size_t i = 0; i < pt->n_footnotes; i++)
     {
-      char *content = pivot_value_to_string (
-        pt->footnotes[i]->content, pt->show_values, pt->show_variables);
-      char *marker = pivot_value_to_string (
-        pt->footnotes[i]->marker, pt->show_values, pt->show_variables);
-      footnotes[i] = tab_create_footnote (
+      const struct pivot_footnote *pf = pt->footnotes[i];
+
+      if (!pf->show)
+        continue;
+
+      char *content = pivot_value_to_string (pf->content, pt->show_values,
+                                             pt->show_variables);
+      char *marker = pivot_value_to_string (pf->marker, pt->show_values,
+                                            pt->show_variables);
+      footnotes[i] = table_create_footnote (
         table, i, content, marker,
         area_style_override (table->container, &pt->areas[PIVOT_AREA_FOOTER],
-                             pt->footnotes[i]->content->cell_style,
-                             pt->footnotes[i]->content->font_style));
+                             pf->content->cell_style,
+                             pf->content->font_style));
       free (marker);
       free (content);
     }
@@ -373,7 +388,7 @@ pivot_table_submit_layer (const struct pivot_table *pt,
                     pt->show_values, pt->show_variables,
                     false, pt->rotate_outer_row_labels);
 
-  size_t *dindexes = xcalloc (pt->n_dimensions, sizeof *dindexes);
+  size_t *dindexes = XCALLOC (pt->n_dimensions, size_t);
   size_t y = 0;
   PIVOT_ENUMERATION_FOR_EACH (pindexes[PIVOT_AXIS_ROW], row_enumeration,
                               &pt->axes[PIVOT_AXIS_ROW])
@@ -405,35 +420,35 @@ pivot_table_submit_layer (const struct pivot_table *pt,
                pt->corner_text, footnotes,
                pt->show_values, pt->show_variables, false);
 
-  if (tab_nc (table) && tab_nr (table))
+  if (table_nc (table) && table_nr (table))
     {
-      tab_hline (
+      table_hline (
         table, get_table_rule (pt->borders, PIVOT_BORDER_INNER_TOP),
-        0, tab_nc (table) - 1, 0);
-      tab_hline (
+        0, table_nc (table) - 1, 0);
+      table_hline (
         table, get_table_rule (pt->borders, PIVOT_BORDER_INNER_BOTTOM),
-        0, tab_nc (table) - 1, tab_nr (table));
-      tab_vline (
+        0, table_nc (table) - 1, table_nr (table));
+      table_vline (
         table, get_table_rule (pt->borders, PIVOT_BORDER_INNER_LEFT),
-        0, 0, tab_nr (table) - 1);
-      tab_vline (
+        0, 0, table_nr (table) - 1);
+      table_vline (
         table, get_table_rule (pt->borders, PIVOT_BORDER_INNER_RIGHT),
-        tab_nc (table), 0, tab_nr (table) - 1);
+        table_nc (table), 0, table_nr (table) - 1);
 
       if (stub[V])
-        tab_hline (
+        table_hline (
           table, get_table_rule (pt->borders, PIVOT_BORDER_DATA_TOP),
-          0, tab_nc (table) - 1, stub[V]);
+          0, table_nc (table) - 1, stub[V]);
       if (stub[H])
-        tab_vline (
+        table_vline (
           table, get_table_rule (pt->borders, PIVOT_BORDER_DATA_LEFT),
-          stub[H], 0, tab_nr (table) - 1);
+          stub[H], 0, table_nr (table) - 1);
 
     }
   free (column_enumeration);
   free (row_enumeration);
 
-  struct table_item *ti = table_item_create (&table->table, NULL, NULL);
+  struct table_item *ti = table_item_create (table, NULL, NULL);
 
   if (pt->title)
     {
@@ -467,11 +482,15 @@ pivot_table_submit_layer (const struct pivot_table *pt,
           pivot_value_format_body (name, pt->show_values, pt->show_variables,
                                    &s);
           layer->content = ds_steal_cstr (&s);
-          layer->n_footnotes = name->n_footnotes;
-          layer->footnotes = xnmalloc (layer->n_footnotes,
+          layer->n_footnotes = 0;
+          layer->footnotes = xnmalloc (name->n_footnotes,
                                        sizeof *layer->footnotes);
           for (size_t i = 0; i < name->n_footnotes; i++)
-            layer->footnotes[i] = footnotes[name->footnotes[i]->idx];
+            {
+              struct footnote *f = footnotes[name->footnotes[i]->idx];
+              if (f)
+                layer->footnotes[layer->n_footnotes++] = f;
+            }
         }
     }
   if (layers)
@@ -480,7 +499,7 @@ pivot_table_submit_layer (const struct pivot_table *pt,
       table_item_layers_destroy (layers);
     }
 
-  if (pt->caption)
+  if (pt->caption && pt->show_caption)
     {
       struct table_item_text *caption = pivot_value_to_table_item_text (
         pt->caption, &pt->areas[PIVOT_AREA_CAPTION], footnotes,
@@ -490,6 +509,8 @@ pivot_table_submit_layer (const struct pivot_table *pt,
     }
 
   free (footnotes);
+  ti->pt = pivot_table_ref (pt);
+
   table_item_submit (ti);
 }