render: Coding style fix.
[pspp] / src / output / render.c
index b90afcce2446cc0a0c9707bba060747a575d9118..08f89a177ed1cc1c3c073974923dff0f4c57e354 100644 (file)
@@ -26,9 +26,9 @@
 #include "libpspp/hash-functions.h"
 #include "libpspp/hmap.h"
 #include "libpspp/pool.h"
+#include "output/pivot-output.h"
+#include "output/pivot-table.h"
 #include "output/render.h"
-#include "output/tab.h"
-#include "output/table-item.h"
 #include "output/table.h"
 
 #include "gl/minmax.h"
@@ -56,9 +56,20 @@ struct render_page
     struct table *table;                /* Table rendered. */
     int ref_cnt;
 
-    /* Local copies of table->n and table->h, for convenience. */
-    int n[TABLE_N_AXES];
+    /* Region of 'table' to render.
+
+       The horizontal cells rendered are the leftmost h[H][0], then
+       r[H][0] through r[H][1], exclusive, then the rightmost h[H][1].
+
+       The vertical cells rendered are the topmost h[V][0], then r[V][0]
+       through r[V][1], exclusive, then the bottommost h[V][1].
+
+       n[H] = h[H][0] + (r[H][1] - r[H][0]) + h[H][1]
+       n[V] = h[V][0] + (r[V][1] - r[V][0]) + h[V][1]
+    */
     int h[TABLE_N_AXES][2];
+    int r[TABLE_N_AXES][2];
+    int n[TABLE_N_AXES];
 
     /* "Cell positions".
 
@@ -68,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.
@@ -77,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. */
@@ -178,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)
@@ -452,17 +470,17 @@ rule_to_render_type (unsigned char type)
 {
   switch (type)
     {
-    case TAL_NONE:
+    case TABLE_STROKE_NONE:
       return RENDER_LINE_NONE;
-    case TAL_SOLID:
+    case TABLE_STROKE_SOLID:
       return RENDER_LINE_SINGLE;
-    case TAL_DASHED:
+    case TABLE_STROKE_DASHED:
       return RENDER_LINE_DASHED;
-    case TAL_THICK:
+    case TABLE_STROKE_THICK:
       return RENDER_LINE_THICK;
-    case TAL_THIN:
+    case TABLE_STROKE_THIN:
       return RENDER_LINE_THIN;
-    case TAL_DOUBLE:
+    case TABLE_STROKE_DOUBLE:
       return RENDER_LINE_DOUBLE;
     default:
       NOT_REACHED ();
@@ -486,48 +504,43 @@ measure_rule (const struct render_params *params, const struct table *table,
   for (d[b] = 0; d[b] < table->n[b]; d[b]++)
     rules |= 1u << table_get_rule (table, a, d[H], d[V], &color);
 
-  /* Turn off TAL_NONE because it has width 0 and we needn't bother.  However,
-     if the device doesn't support margins, make sure that there is at least a
-     small gap between cells (but we don't need any at the left or right edge
-     of the table). */
-  if (rules & (1u << TAL_NONE))
+  /* Turn off TABLE_STROKE_NONE because it has width 0 and we needn't bother.
+     However, if the device doesn't support margins, make sure that there is at
+     least a small gap between cells (but we don't need any at the left or
+     right edge of the table). */
+  if (rules & (1u << TABLE_STROKE_NONE))
     {
-      rules &= ~(1u << TAL_NONE);
+      rules &= ~(1u << TABLE_STROKE_NONE);
       if (z > 0 && z < table->n[a] && !params->supports_margins && a == H)
-        rules |= 1u << TAL_SOLID;
+        rules |= 1u << TABLE_STROKE_SOLID;
     }
 
   /* Calculate maximum width of the rules that are present. */
   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;
 }
 
 /* Allocates and returns a new render_page using PARAMS and TABLE.  Allocates
-   space for all of the members of the new page, but the caller must initialize
-   the 'cp' member itself. */
+   space for rendering a table with dimensions given in N.  The caller must
+   initialize most of the members itself. */
 static struct render_page *
-render_page_allocate (const struct render_params *params,
-                      struct table *table)
+render_page_allocate__ (const struct render_params *params,
+                        struct table *table, int n[TABLE_N_AXES])
 {
   struct render_page *page = xmalloc (sizeof *page);
   page->params = params;
   page->table = table;
   page->ref_cnt = 1;
-  page->n[H] = table->n[H];
-  page->n[V] = table->n[V];
-  page->h[H][0] = table->h[H][0];
-  page->h[H][1] = table->h[H][1];
-  page->h[V][0] = table->h[V][0];
-  page->h[V][1] = table->h[V][1];
+  page->n[H] = n[H];
+  page->n[V] = n[V];
 
   for (int i = 0; i < TABLE_N_AXES; i++)
     {
-      page->cp[i] = xmalloc ((2 * page->n[i] + 2) * sizeof *page->cp[i]);
-      page->join_crossing[i] = xzalloc ((page->n[i] + 1)
-                                        * sizeof *page->join_crossing[i]);
+      page->cp[i] = xcalloc ((2 * n[i] + 2) , sizeof *page->cp[i]);
+      page->join_crossing[i] = xcalloc ((n[i] + 1) , sizeof *page->join_crossing[i]);
     }
 
   hmap_init (&page->overflows);
@@ -536,6 +549,23 @@ render_page_allocate (const struct render_params *params,
   return page;
 }
 
+/* Allocates and returns a new render_page using PARAMS and TABLE.  Allocates
+   space for all of the members of the new page, but the caller must initialize
+   the 'cp' member itself. */
+static struct render_page *
+render_page_allocate (const struct render_params *params, struct table *table)
+{
+  struct render_page *page = render_page_allocate__ (params, table, table->n);
+  for (enum table_axis a = 0; a < TABLE_N_AXES; a++)
+    {
+      page->h[a][0] = table->h[a][0];
+      page->h[a][1] = table->h[a][1];
+      page->r[a][0] = table->h[a][0];
+      page->r[a][1] = table->n[a] - table->h[a][1];
+    }
+  return page;
+}
+
 /* Allocates and returns a new render_page for PARAMS and TABLE, initializing
    cp[H] in the new page from ROWS and RULES.  The caller must still initialize
    cp[V]. */
@@ -593,7 +623,6 @@ create_page_with_interpolated_widths (const struct render_params *params,
   assert (page->cp[H][n * 2 + 1] == params->size[H]);
   return page;
 }
-
 \f
 static void
 set_join_crossings (struct render_page *page, enum table_axis axis,
@@ -603,6 +632,75 @@ set_join_crossings (struct render_page *page, enum table_axis axis,
     page->join_crossing[axis][z] = rules[z];
 }
 
+/* Maps a contiguous range of cells from a page to the underlying table along
+   the horizpntal or vertical dimension. */
+struct map
+  {
+    int p0;                     /* First ordinate in the page. */
+    int t0;                     /* First ordinate in the table. */
+    int n;                      /* Number of ordinates in page and table. */
+  };
+
+/* Initializes M to a mapping from PAGE to PAGE->table along axis A.  The
+   mapping includes ordinate Z (in PAGE). */
+static void
+get_map (const struct render_page *page, enum table_axis a, int z,
+         struct map *m)
+{
+  if (z < page->h[a][0])
+    {
+      m->p0 = 0;
+      m->t0 = 0;
+      m->n = page->h[a][0];
+    }
+  else if (z < page->n[a] - page->h[a][1])
+    {
+      m->p0 = page->h[a][0];
+      m->t0 = page->r[a][0];
+      m->n = page->r[a][1] - page->r[a][0];
+    }
+  else
+    {
+      m->p0 = page->n[a] - page->h[a][1];
+      m->t0 = page->table->n[a] - page->table->h[a][1];
+      m->n = page->h[a][1];
+    }
+}
+
+/* Initializes CELL with the contents of the table cell at column X and row Y
+   within PAGE.  When CELL is no longer needed, the caller is responsible for
+   freeing it by calling table_cell_free(CELL).
+
+   The caller must ensure that CELL is destroyed before TABLE is unref'ed.
+
+   This is equivalent to table_get_cell(), except X and Y are in terms of the
+   page's rows and columns rather than the underlying table's. */
+static void
+render_get_cell (const struct render_page *page, int x, int y,
+                 struct table_cell *cell)
+{
+  int d[TABLE_N_AXES] = { [H] = x, [V] = y };
+  struct map map[TABLE_N_AXES];
+
+  for (enum table_axis a = 0; a < TABLE_N_AXES; a++)
+    {
+      struct map *m = &map[a];
+      get_map (page, a, d[a], m);
+      d[a] += m->t0 - m->p0;
+    }
+  table_get_cell (page->table, d[H], d[V], cell);
+
+  for (enum table_axis a = 0; a < TABLE_N_AXES; a++)
+    {
+      struct map *m = &map[a];
+
+      for (int i = 0; i < 2; i++)
+        cell->d[a][i] -= m->t0 - m->p0;
+      cell->d[a][0] = MAX (cell->d[a][0], m->p0);
+      cell->d[a][1] = MIN (cell->d[a][1], m->p0 + m->n);
+    }
+}
+
 /* Creates and returns a new render_page for rendering TABLE on a device
    described by PARAMS.
 
@@ -615,8 +713,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];
@@ -633,9 +731,9 @@ render_page_create (const struct render_params *params, struct table *table,
      span multiple columns. */
   struct render_row *columns[2];
   for (int i = 0; i < 2; i++)
-    columns[i] = xzalloc (nc * sizeof *columns[i]);
+    columns[i] = xcalloc (nc, sizeof *columns[i]);
   for (int y = 0; y < nr; y++)
-    for (int x = 0; x < nc; )
+    for (int x = 0; x < nc;)
       {
         struct table_cell cell;
 
@@ -645,15 +743,14 @@ 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];
               }
           }
         x = cell.d[H][1];
-        table_cell_free (&cell);
       }
 
   /* Distribute widths of spanned columns. */
@@ -661,7 +758,7 @@ render_page_create (const struct render_params *params, struct table *table,
     for (int x = 0; x < nc; x++)
       columns[i][x].width = columns[i][x].unspanned;
   for (int y = 0; y < nr; y++)
-    for (int x = 0; x < nc; )
+    for (int x = 0; x < nc;)
       {
         struct table_cell cell;
 
@@ -670,13 +767,13 @@ 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));
           }
         x = cell.d[H][1];
-        table_cell_free (&cell);
       }
   if (min_width > 0)
     for (int i = 0; i < 2; i++)
@@ -692,7 +789,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;
@@ -718,20 +815,21 @@ render_page_create (const struct render_params *params, struct table *table,
     }
 
   /* Calculate heights of cells that do not span multiple rows. */
-  struct render_row *rows = xzalloc (nr * sizeof *rows);
+  struct render_row *rows = XCALLOC (nr, struct render_row);
   for (int y = 0; y < nr; y++)
-    for (int x = 0; x < nc; )
+    for (int x = 0; x < nc;)
       {
         struct render_row *r = &rows[y];
         struct table_cell cell;
 
-        table_get_cell (table, x, y, &cell);
+        render_get_cell (page, x, y, &cell);
         if (y == cell.d[V][0])
           {
             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;
               }
@@ -742,27 +840,25 @@ render_page_create (const struct render_params *params, struct table *table,
               set_join_crossings (page, H, &cell, rules[H]);
           }
         x = cell.d[H][1];
-        table_cell_free (&cell);
       }
   for (int i = 0; i < 2; i++)
     free (columns[i]);
 
   /* Distribute heights of spanned rows. */
   for (int y = 0; y < nr; y++)
-    for (int x = 0; x < nc; )
+    for (int x = 0; x < nc;)
       {
         struct table_cell cell;
 
-        table_get_cell (table, x, y, &cell);
+        render_get_cell (page, x, y, &cell);
         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));
           }
         x = cell.d[H][1];
-        table_cell_free (&cell);
       }
 
   /* Decide final row heights. */
@@ -776,9 +872,9 @@ render_page_create (const struct render_params *params, struct table *table,
       if (hw * 2 >= page->params->size[axis]
           || hw + max_cell_width (page, axis) > page->params->size[axis])
         {
-          page->table = table_unshare (page->table);
-          page->table->h[axis][0] = page->table->h[axis][1] = 0;
           page->h[axis][0] = page->h[axis][1] = 0;
+          page->r[axis][0] = 0;
+          page->r[axis][1] = page->n[axis];
         }
     }
 
@@ -848,13 +944,47 @@ render_page_get_best_breakpoint (const struct render_page *page, int height)
 \f
 /* Drawing render_pages. */
 
-static inline enum render_line_style
+/* This is like table_get_rule() except:
+
+   - D is in terms of the page's rows and column rather than the underlying
+     table's.
+
+   - The result is in the form of a render_line_style. */
+static enum render_line_style
 get_rule (const struct render_page *page, enum table_axis axis,
-          const int d[TABLE_N_AXES], struct cell_color *color)
+          const int d_[TABLE_N_AXES], struct cell_color *color)
 {
-  return rule_to_render_type (table_get_rule (page->table,
-                                              axis, d[H] / 2, d[V] / 2,
-                                              color));
+  int d[TABLE_N_AXES] = { d_[0] / 2, d_[1] / 2 };
+  int d2 = -1;
+
+  enum table_axis a = axis;
+  if (d[a] < page->h[a][0])
+    /* Nothing to do */;
+  else if (d[a] <= page->n[a] - page->h[a][1])
+    {
+      if (page->h[a][0] && d[a] == page->h[a][0])
+        d2 = page->h[a][0];
+      else if (page->h[a][1] && d[a] == page->n[a] - page->h[a][1])
+        d2 = page->table->n[a] - page->h[a][1];
+      d[a] += page->r[a][0] - page->h[a][0];
+    }
+  else
+    d[a] += ((page->table->n[a] - page->table->h[a][1])
+             - (page->n[a] - page->h[a][1]));
+
+  enum table_axis b = !axis;
+  struct map m;
+  get_map (page, b, d[b], &m);
+  d[b] += m.t0 - m.p0;
+
+  int r = table_get_rule (page->table, axis, d[H], d[V], color);
+  if (d2 >= 0)
+    {
+      d[a] = d2;
+      int r2 = table_get_rule (page->table, axis, d[H], d[V], color);
+      r = table_stroke_combine (r, r2);
+    }
+  return rule_to_render_type (r);
 }
 
 static bool
@@ -871,10 +1001,10 @@ render_direction_rtl (void)
      this string with "output-direction-rtl".  Otherwise either leave it
      untranslated or copy it verbatim. */
   const char *dir = _("output-direction-ltr");
-  if ( 0 == strcmp ("output-direction-rtl", dir))
+  if (0 == strcmp ("output-direction-rtl", dir))
     return true;
 
-  if ( 0 != strcmp ("output-direction-ltr", dir))
+  if (0 != strcmp ("output-direction-ltr", dir))
     fprintf (stderr, "This localisation has been incorrectly translated.  "
              "Complain to the translator.\n");
 
@@ -910,7 +1040,7 @@ render_rule (const struct render_page *page, const int ofs[TABLE_N_AXES],
               styles[a][0] = get_rule (page, a, e, &colors[a][0]);
             }
 
-          if (d[b] / 2 < page->table->n[b])
+          if (d[b] / 2 < page->n[b])
             styles[a][1] = get_rule (page, a, d, &colors[a][1]);
         }
       else
@@ -935,7 +1065,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);
     }
 }
 
@@ -957,17 +1087,18 @@ render_cell (const struct render_page *page, const int ofs[TABLE_N_AXES],
   bb[V][0] = clip[V][0] = ofs[V] + page->cp[V][cell->d[V][0] * 2 + 1];
   bb[V][1] = clip[V][1] = ofs[V] + page->cp[V][cell->d[V][1] * 2];
 
-  enum table_valign valign = cell->style->cell_style.valign;
+  enum table_valign valign = cell->cell_style->valign;
+  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)
         {
           if (valign == TABLE_VALIGN_CENTER)
             extra /= 2;
-          bb[V][0] += extra;
+          valign_offset += extra;
         }
     }
 
@@ -1003,8 +1134,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, 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. */
@@ -1013,16 +1144,15 @@ render_page_draw_cells (const struct render_page *page,
                         int ofs[TABLE_N_AXES], int bb[TABLE_N_AXES][2])
 {
   for (int y = bb[V][0]; y < bb[V][1]; y++)
-    for (int x = bb[H][0]; x < bb[H][1]; )
+    for (int x = bb[H][0]; x < bb[H][1];)
       if (!is_rule (x) && !is_rule (y))
         {
           struct table_cell cell;
 
-          table_get_cell (page->table, x / 2, y / 2, &cell);
+          render_get_cell (page, x / 2, y / 2, &cell);
           if (y / 2 == bb[V][0] / 2 || y / 2 == cell.d[V][0])
             render_cell (page, ofs, &cell);
           x = rule_ofs (cell.d[H][1]);
-          table_cell_free (&cell);
         }
       else
         x++;
@@ -1249,17 +1379,16 @@ 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)
-                for (int x = 0; x < page->n[H]; )
+              if (axis == TABLE_VERT && page->params->ops->adjust_break)
+                for (int x = 0; x < page->n[H];)
                   {
                     struct table_cell cell;
 
-                    table_get_cell (page->table, x, z, &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];
-                    table_cell_free (&cell);
 
                     if (better_pixel < pixel)
                       {
@@ -1351,25 +1480,25 @@ cell_is_breakable (const struct render_break *b, int cell)
 struct render_pager
   {
     const struct render_params *params;
+    double scale;
 
-    struct render_page **pages;
-    size_t n_pages, allocated_pages;
+    /* An array of "render_page"s to be rendered, in order, vertically.  There
+       may be up to 5 pages, for the pivot table's title, layers, body,
+       captions, and footnotes. */
+    struct render_page *pages[5];
+    size_t n_pages;
 
     size_t cur_page;
     struct render_break x_break;
     struct render_break y_break;
   };
 
-static const struct render_page *
+static void
 render_pager_add_table (struct render_pager *p, struct table *table,
                         int min_width)
 {
-  if (p->n_pages >= p->allocated_pages)
-    p->pages = x2nrealloc (p->pages, &p->allocated_pages, sizeof *p->pages);
-
-  struct render_page *page = render_page_create (p->params, table, min_width);
-  p->pages[p->n_pages++] = page;
-  return page;
+  if (table)
+    p->pages[p->n_pages++] = render_page_create (p->params, table, min_width);
 }
 
 static void
@@ -1380,81 +1509,67 @@ render_pager_start_page (struct render_pager *p)
   render_break_init_empty (&p->y_break);
 }
 
-static void
-add_footnote_page (struct render_pager *p, const struct table_item *item)
-{
-  const struct footnote **f;
-  size_t n_footnotes = table_collect_footnotes (item, &f);
-  if (!n_footnotes)
-    return;
-
-  struct tab_table *t = tab_create (2, n_footnotes);
-
-  for (size_t i = 0; i < n_footnotes; i++)
-    if (f[i])
-      {
-        tab_text_format (t, 0, i, TAB_LEFT, "%s.", f[i]->marker);
-        tab_text (t, 1, i, TAB_LEFT, f[i]->content);
-        if (f[i]->style)
-          {
-            tab_add_style (t, 0, i, f[i]->style);
-            tab_add_style (t, 1, i, f[i]->style);
-          }
-      }
-  render_pager_add_table (p, &t->table, 0);
-
-  free (f);
-}
-
-static void
-add_text_page (struct render_pager *p, const struct table_item_text *t,
-               int min_width)
-{
-  if (!t)
-    return;
-
-  struct tab_table *tab = tab_create (1, 1);
-  tab_text (tab, 0, 0, 0, t->content);
-  for (size_t i = 0; i < t->n_footnotes; i++)
-    tab_add_footnote (tab, 0, 0, t->footnotes[i]);
-  if (t->style)
-    tab->styles[0] = area_style_clone (tab->container, t->style);
-  render_pager_add_table (p, &tab->table, min_width);
-}
-
-/* Creates and returns a new render_pager for rendering TABLE_ITEM on the
-   device with the given PARAMS. */
+/* Creates and returns a new render_pager for rendering PT on the device
+   with the given PARAMS. */
 struct render_pager *
 render_pager_create (const struct render_params *params,
-                     const struct table_item *table_item)
+                     const struct pivot_table *pt,
+                     const size_t *layer_indexes)
 {
-  const struct table *table = table_item_get_table (table_item);
-
-  struct render_pager *p = xzalloc (sizeof *p);
-  p->params = params;
-
-  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_text_page (p, table_item_get_layers (table_item), title_width);
-
-  /* Body. */
-  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);
+  if (!layer_indexes)
+    layer_indexes = pt->current_layer;
+
+  struct table *title, *layers, *body, *caption, *footnotes;
+  pivot_output (pt, layer_indexes, params->printing,
+                &title, &layers, &body, &caption, &footnotes, NULL, NULL);
+
+  /* Figure out the width of the body of the table.  Use this to determine the
+     base scale. */
+  struct render_page *body_page = render_page_create (params, body, 0);
+  int body_width = table_width (body_page, H);
+  double scale = 1.0;
+  if (body_width > params->size[H])
+    {
+      if (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, render_page_ref (body_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);
+        }
+    }
 
-  /* Footnotes. */
-  add_footnote_page (p, table_item);
+  /* Create the pager. */
+  struct render_pager *p = xmalloc (sizeof *p);
+  *p = (struct render_pager) { .params = params, .scale = scale };
+  render_pager_add_table (p, title, body_width);
+  render_pager_add_table (p, layers, body_width);
+  p->pages[p->n_pages++] = body_page;
+  render_pager_add_table (p, caption, 0);
+  render_pager_add_table (p, footnotes, 0);
+  assert (p->n_pages <= sizeof p->pages / sizeof *p->pages);
+
+  /* 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 (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);
 
@@ -1471,7 +1586,6 @@ render_pager_destroy (struct render_pager *p)
       render_break_destroy (&p->y_break);
       for (size_t i = 0; i < p->n_pages; i++)
         render_page_unref (p->pages[i]);
-      free (p->pages);
       free (p);
     }
 }
@@ -1499,7 +1613,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;
 }
@@ -1513,6 +1628,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;
 
@@ -1531,6 +1652,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];
 }
 
@@ -1654,9 +1779,20 @@ render_page_select (const struct render_page *page, enum table_axis axis,
     }
 
   /* Allocate subpage. */
-  struct render_page *subpage = render_page_allocate (
-    page->params, table_select_slice (table_ref (page->table),
-                                      a, z0, z1, true));
+  int trim[2] = { z0 - page->h[a][0], (page->n[a] - page->h[a][1]) - z1 };
+  int n[TABLE_N_AXES] = { [H] = page->n[H], [V] = page->n[V] };
+  n[a] -= trim[0] + trim[1];
+  struct render_page *subpage = render_page_allocate__ (
+    page->params, table_ref (page->table), n);
+  for (enum table_axis k = 0; k < TABLE_N_AXES; k++)
+    {
+      subpage->h[k][0] = page->h[k][0];
+      subpage->h[k][1] = page->h[k][1];
+      subpage->r[k][0] = page->r[k][0];
+      subpage->r[k][1] = page->r[k][1];
+    }
+  subpage->r[a][0] += trim[0];
+  subpage->r[a][1] -= trim[1];
 
   /* An edge is cut off if it was cut off in PAGE or if we're trimming pixels
      off that side of the page and there are no headers. */
@@ -1687,10 +1823,8 @@ render_page_select (const struct render_page *page, enum table_axis axis,
   *dcp = 0;
   for (int z = 0; z <= rule_ofs (subpage->h[a][0]); z++, dcp++)
     {
-      if (z == 0 && subpage->is_edge_cutoff[a][0])
-        dcp[1] = dcp[0];
-      else
-        dcp[1] = dcp[0] + (scp[z + 1] - scp[z]);
+      int w = !z && subpage->is_edge_cutoff[a][0] ? 0 : scp[z + 1] - scp[z];
+      dcp[1] = dcp[0] + w;
     }
   for (int z = cell_ofs (z0); z <= cell_ofs (z1 - 1); z++, dcp++)
     {
@@ -1730,14 +1864,14 @@ render_page_select (const struct render_page *page, enum table_axis axis,
   };
 
   if (!page->h[a][0] || z0 > page->h[a][0] || p0)
-    for (int z = 0; z < page->n[b]; )
+    for (int z = 0; z < page->n[b];)
       {
         int d[TABLE_N_AXES];
         d[a] = z0;
         d[b] = z;
 
         struct table_cell cell;
-        table_get_cell (page->table, d[H], d[V], &cell);
+        render_get_cell (page, d[H], d[V], &cell);
         bool overflow0 = p0 || cell.d[a][0] < z0;
         bool overflow1 = cell.d[a][1] > z1 || (cell.d[a][1] == z1 && p1);
         if (overflow0 || overflow1)
@@ -1762,18 +1896,17 @@ render_page_select (const struct render_page *page, enum table_axis axis,
               }
           }
         z = cell.d[b][1];
-        table_cell_free (&cell);
       }
 
   if (!page->h[a][1] || z1 < page->n[a] - page->h[a][1] || p1)
-    for (int z = 0; z < page->n[b]; )
+    for (int z = 0; z < page->n[b];)
       {
         int d[TABLE_N_AXES];
         d[a] = z1 - 1;
         d[b] = z;
 
         struct table_cell cell;
-        table_get_cell (page->table, d[H], d[V], &cell);
+        render_get_cell (page, d[H], d[V], &cell);
         if ((cell.d[a][1] > z1 || (cell.d[a][1] == z1 && p1))
             && find_overflow_for_cell (&s, &cell) == NULL)
           {
@@ -1782,7 +1915,6 @@ render_page_select (const struct render_page *page, enum table_axis axis,
                                                    cell_ofs (cell.d[a][1]));
           }
         z = cell.d[b][1];
-        table_cell_free (&cell);
       }
 
   /* Copy overflows from PAGE into subpage. */
@@ -1795,7 +1927,6 @@ render_page_select (const struct render_page *page, enum table_axis axis,
       if (cell.d[a][1] > z0 && cell.d[a][0] < z1
           && find_overflow_for_cell (&s, &cell) == NULL)
         insert_overflow (&s, &cell);
-      table_cell_free (&cell);
     }
 
   return subpage;
@@ -1844,7 +1975,7 @@ static struct render_overflow *
 insert_overflow (struct render_page_selection *s,
                  const struct table_cell *cell)
 {
-  struct render_overflow *of = xzalloc (sizeof *of);
+  struct render_overflow *of = XZALLOC (struct render_overflow);
   cell_to_subpage (s, cell, of->d);
   hmap_insert (&s->subpage->overflows, &of->node,
                hash_cell (of->d[H], of->d[V]));