table-item: Change title from table_item_text to table_cell.
[pspp] / src / output / render.c
index a7cc3e67b6e6e5b812a7a4c6e74711784c815b29..b3ef3062511c9daec499e335cab93d032001848e 100644 (file)
@@ -26,6 +26,7 @@
 #include "libpspp/hash-functions.h"
 #include "libpspp/hmap.h"
 #include "libpspp/pool.h"
+#include "output/pivot-table.h"
 #include "output/render.h"
 #include "output/table-item.h"
 #include "output/table.h"
@@ -78,8 +79,8 @@ struct render_page
        cp[H][2] = cp[H][1] + the width of the leftmost column.
        cp[H][3] = cp[H][2] + the width of the second-from-left vertical rule.
        and so on:
-       cp[H][2 * nc] = x position of the rightmost vertical rule.
-       cp[H][2 * nc + 1] = total table width including all rules.
+       cp[H][2 * n[H]] = x position of the rightmost vertical rule.
+       cp[H][2 * n[H] + 1] = total table width including all rules.
 
        Similarly, cp[V] represents y positions within the table.
        cp[V][0] = 0.
@@ -87,8 +88,8 @@ struct render_page
        cp[V][2] = cp[V][1] + the height of the topmost row.
        cp[V][3] = cp[V][2] + the height of the second-from-top horizontal rule.
        and so on:
-       cp[V][2 * nr] = y position of the bottommost horizontal rule.
-       cp[V][2 * nr + 1] = total table height including all rules.
+       cp[V][2 * n[V]] = y position of the bottommost horizontal rule.
+       cp[V][2 * n[V] + 1] = total table height including all rules.
 
        Rules and columns can have width or height 0, in which case consecutive
        values in this array are equal. */
@@ -188,6 +189,13 @@ axis_width (const struct render_page *page, int axis, int ofs0, int ofs1)
   return page->cp[axis][ofs1] - page->cp[axis][ofs0];
 }
 
+/* Returns the total width of PAGE along AXIS. */
+static int
+table_width (const struct render_page *page, int axis)
+{
+  return page->cp[axis][2 * page->n[axis] + 1];
+}
+
 /* Returns the width of the headers in PAGE along AXIS. */
 static int
 headers_width (const struct render_page *page, int axis)
@@ -511,7 +519,7 @@ measure_rule (const struct render_params *params, const struct table *table,
   int width = 0;
   for (size_t i = 0; i < TABLE_N_STROKES; i++)
     if (rules & (1u << i))
-      width = MAX (width, params->line_widths[a][rule_to_render_type (i)]);
+      width = MAX (width, params->line_widths[rule_to_render_type (i)]);
   return width;
 }
 
@@ -706,8 +714,8 @@ render_page_create (const struct render_params *params, struct table *table,
 {
   enum { MIN, MAX };
 
-  int nc = table_nc (table);
-  int nr = table_nr (table);
+  int nc = table->n[H];
+  int nr = table->n[V];
 
   /* Figure out rule widths. */
   int *rules[TABLE_N_AXES];
@@ -736,8 +744,8 @@ render_page_create (const struct render_params *params, struct table *table,
             if (table_cell_colspan (&cell) == 1)
               {
                 int w[2];
-                params->measure_cell_width (params->aux, &cell,
-                                            &w[MIN], &w[MAX]);
+                params->ops->measure_cell_width (params->aux, &cell,
+                                                 &w[MIN], &w[MAX]);
                 for (int i = 0; i < 2; i++)
                   if (columns[i][x].unspanned < w[i])
                     columns[i][x].unspanned = w[i];
@@ -760,7 +768,8 @@ render_page_create (const struct render_params *params, struct table *table,
           {
             int w[2];
 
-            params->measure_cell_width (params->aux, &cell, &w[MIN], &w[MAX]);
+            params->ops->measure_cell_width (params->aux, &cell,
+                                             &w[MIN], &w[MAX]);
             for (int i = 0; i < 2; i++)
               distribute_spanned_width (w[i], &columns[i][cell.d[H][0]],
                                         rules[H], table_cell_colspan (&cell));
@@ -781,7 +790,7 @@ render_page_create (const struct render_params *params, struct table *table,
   /* Decide final column widths. */
   int table_widths[2];
   for (int i = 0; i < 2; i++)
-    table_widths[i] = calculate_table_width (table_nc (table),
+    table_widths[i] = calculate_table_width (table->n[H],
                                              columns[i], rules[H]);
 
   struct render_page *page;
@@ -820,7 +829,8 @@ render_page_create (const struct render_params *params, struct table *table,
             if (table_cell_rowspan (&cell) == 1)
               {
                 int w = joined_width (page, H, cell.d[H][0], cell.d[H][1]);
-                int h = params->measure_cell_height (params->aux, &cell, w);
+                int h = params->ops->measure_cell_height (params->aux,
+                                                          &cell, w);
                 if (h > r->unspanned)
                   r->unspanned = r->width = h;
               }
@@ -845,7 +855,7 @@ render_page_create (const struct render_params *params, struct table *table,
         if (y == cell.d[V][0] && table_cell_rowspan (&cell) > 1)
           {
             int w = joined_width (page, H, cell.d[H][0], cell.d[H][1]);
-            int h = params->measure_cell_height (params->aux, &cell, w);
+            int h = params->ops->measure_cell_height (params->aux, &cell, w);
             distribute_spanned_width (h, &rows[cell.d[V][0]], rules[V],
                                       table_cell_rowspan (&cell));
           }
@@ -1056,7 +1066,7 @@ render_rule (const struct render_page *page, const int ofs[TABLE_N_AXES],
        }
       bb[V][0] = ofs[V] + page->cp[V][d[V]];
       bb[V][1] = ofs[V] + page->cp[V][d[V] + 1];
-      page->params->draw_line (page->params->aux, bb, styles, colors);
+      page->params->ops->draw_line (page->params->aux, bb, styles, colors);
     }
 }
 
@@ -1082,7 +1092,7 @@ render_cell (const struct render_page *page, const int ofs[TABLE_N_AXES],
   int valign_offset = 0;
   if (valign != TABLE_VALIGN_TOP)
     {
-      int height = page->params->measure_cell_height (
+      int height = page->params->ops->measure_cell_height (
         page->params->aux, cell, bb[H][1] - bb[H][0]);
       int extra = bb[V][1] - bb[V][0] - height;
       if (extra > 0)
@@ -1125,8 +1135,8 @@ render_cell (const struct render_page *page, const int ofs[TABLE_N_AXES],
                    || page->n[V] - (cell->d[V][0] + 1) < page->h[V][1]
                    ? 0
                    : (cell->d[V][0] - page->h[V][0]) & 1);
-  page->params->draw_cell (page->params->aux, cell, color_idx,
-                           bb, valign_offset, spill, clip);
+  page->params->ops->draw_cell (page->params->aux, cell, color_idx,
+                                bb, valign_offset, spill, clip);
 }
 
 /* Draws the cells of PAGE indicated in BB. */
@@ -1370,14 +1380,14 @@ render_break_next (struct render_break *b, int size)
                  being broken have a better internal breakpoint than the exact
                  number of pixels available, which might look bad e.g. because
                  it breaks in the middle of a line of text. */
-              if (axis == TABLE_VERT && page->params->adjust_break)
+              if (axis == TABLE_VERT && page->params->ops->adjust_break)
                 for (int x = 0; x < page->n[H];)
                   {
                     struct table_cell cell;
 
                     render_get_cell (page, x, z, &cell);
                     int w = joined_width (page, H, cell.d[H][0], cell.d[H][1]);
-                    int better_pixel = page->params->adjust_break (
+                    int better_pixel = page->params->ops->adjust_break (
                       page->params->aux, &cell, w, pixel);
                     x = cell.d[H][1];
 
@@ -1471,7 +1481,12 @@ cell_is_breakable (const struct render_break *b, int cell)
 struct render_pager
   {
     const struct render_params *params;
+    double scale;
 
+    /* An array of "render_page"s to be rendered, in order, vertically.  From
+       the user's perspective, there's only one table per render_pager, but the
+       implementation treats the title, table body, caption, footnotes,
+       etc. each as a table, and that's why we have an array here. */
     struct render_page **pages;
     size_t n_pages, allocated_pages;
 
@@ -1503,7 +1518,7 @@ render_pager_start_page (struct render_pager *p)
 static void
 add_footnote_page (struct render_pager *p, const struct table_item *item)
 {
-  const struct footnote **f;
+  struct footnote **f;
   size_t n_footnotes = table_collect_footnotes (item, &f);
   if (!n_footnotes)
     return;
@@ -1520,6 +1535,22 @@ add_footnote_page (struct render_pager *p, const struct table_item *item)
   free (f);
 }
 
+static void
+add_table_cell_page (struct render_pager *p, const struct table_cell *cell,
+                     int min_width)
+{
+  if (!cell)
+    return;
+
+  struct table *tab = table_create (1, 1, 0, 0, 0, 0);
+  table_text (tab, 0, 0, 0, cell->text);
+  for (size_t i = 0; i < cell->n_footnotes; i++)
+    table_add_footnote (tab, 0, 0, cell->footnotes[i]);
+  if (cell->style)
+    tab->styles[0] = table_area_style_clone (tab->container, cell->style);
+  render_pager_add_table (p, tab, min_width);
+}
+
 static void
 add_text_page (struct render_pager *p, const struct table_item_text *t,
                int min_width)
@@ -1564,32 +1595,57 @@ render_pager_create (const struct render_params *params,
 {
   const struct table *table = table_item_get_table (table_item);
 
-  struct render_pager *p = xzalloc (sizeof *p);
-  p->params = params;
-
+  /* Figure out the width of the body of the table.  Use this to determine the
+     base scale. */
   struct render_page *page = render_page_create (params, table_ref (table), 0);
-  struct render_break b;
-  render_break_init (&b, page, H);
-  struct render_page *subpage = render_break_next (&b, p->params->size[H]);
-  int title_width = subpage ? subpage->cp[H][2 * subpage->n[H] + 1] : 0;
-  render_page_unref (subpage);
-  render_break_destroy (&b);
-
-  /* Title. */
-  add_text_page (p, table_item_get_title (table_item), title_width);
-
-  /* Layers. */
-  add_layers_page (p, table_item_get_layers (table_item), title_width);
+  int body_width = table_width (page, H);
+  double scale = 1.0;
+  if (body_width > params->size[H])
+    {
+      if (table_item->pt
+          && table_item->pt->look->shrink_to_fit[H]
+          && params->ops->scale)
+        scale = params->size[H] / (double) body_width;
+      else
+        {
+          struct render_break b;
+          render_break_init (&b, page, H);
+          struct render_page *subpage
+            = render_break_next (&b, params->size[H]);
+          body_width = subpage ? subpage->cp[H][2 * subpage->n[H] + 1] : 0;
+          render_page_unref (subpage);
+          render_break_destroy (&b);
+        }
+    }
 
-  /* Body. */
+  /* Create the pager. */
+  struct render_pager *p = xmalloc (sizeof *p);
+  *p = (struct render_pager) { .params = params, .scale = scale };
+  add_table_cell_page (p, table_item_get_title (table_item), body_width);
+  add_layers_page (p, table_item_get_layers (table_item), body_width);
   render_pager_add_table (p, table_ref (table_item_get_table (table_item)), 0);
-
-  /* Caption. */
   add_text_page (p, table_item_get_caption (table_item), 0);
-
-  /* Footnotes. */
   add_footnote_page (p, table_item);
 
+  /* If we're shrinking tables to fit the page length, then adjust the scale
+     factor.
+
+     XXX This will sometimes shrink more than needed, because adjusting the
+     scale factor allows for cells to be "wider", which means that sometimes
+     they won't break across as much vertical space, thus shrinking the table
+     vertically more than the scale would imply.  Shrinking only as much as
+     necessary would require an iterative search. */
+  if (table_item->pt
+      && table_item->pt->look->shrink_to_fit[V]
+      && params->ops->scale)
+    {
+      int total_height = 0;
+      for (size_t i = 0; i < p->n_pages; i++)
+        total_height += table_width (p->pages[i], V);
+      if (total_height * p->scale >= params->size[V])
+        p->scale *= params->size[V] / (double) total_height;
+    }
+
   render_pager_start_page (p);
 
   return p;
@@ -1633,7 +1689,8 @@ render_pager_has_next (const struct render_pager *p_)
         }
       else
         render_break_init (
-          &p->y_break, render_break_next (&p->x_break, p->params->size[H]), V);
+          &p->y_break, render_break_next (&p->x_break,
+                                          p->params->size[H] / p->scale), V);
     }
   return true;
 }
@@ -1647,6 +1704,12 @@ render_pager_has_next (const struct render_pager *p_)
 int
 render_pager_draw_next (struct render_pager *p, int space)
 {
+  if (p->scale != 1.0)
+    {
+      p->params->ops->scale (p->params->aux, p->scale);
+      space /= p->scale;
+    }
+
   int ofs[TABLE_N_AXES] = { 0, 0 };
   size_t start_page = SIZE_MAX;
 
@@ -1665,6 +1728,10 @@ render_pager_draw_next (struct render_pager *p, int space)
       ofs[V] += render_page_get_size (page, V);
       render_page_unref (page);
     }
+
+  if (p->scale != 1.0)
+    ofs[V] *= p->scale;
+
   return ofs[V];
 }