render: Fold caption drawing into rendering engine.
[pspp] / src / output / render.c
index 503d5f692d7d7d3d3cfb654d869573c5f30978f4..734914dbf5b039835c961361d7a9fac33361e0d0 100644 (file)
@@ -18,6 +18,7 @@
 
 #include <math.h>
 #include <stdio.h>
+#include <stdint.h>
 #include <stdlib.h>
 #include <string.h>
 
@@ -25,6 +26,7 @@
 #include "libpspp/hash-functions.h"
 #include "libpspp/hmap.h"
 #include "output/render.h"
+#include "output/table-item.h"
 #include "output/table.h"
 
 #include "gl/minmax.h"
 
    May represent the layout of an entire table presented to
    render_page_create(), or a rectangular subregion of a table broken out using
-   render_break_next() to allow a table to be broken across multiple pages. */
+   render_break_next() to allow a table to be broken across multiple pages.
+
+   A page's size is not limited to the size passed in as part of render_params.
+   render_pager breaks a render_page into smaller render_pages that will fit in
+   the available space. */
 struct render_page
   {
     const struct render_params *params; /* Parameters of the target device. */
@@ -120,6 +126,12 @@ struct render_page
     int *join_crossing[TABLE_N_AXES];
   };
 
+static struct render_page *render_page_create (const struct render_params *,
+                                               const struct table *);
+
+struct render_page *render_page_ref (const struct render_page *page_);
+static void render_page_unref (struct render_page *);
+
 /* Returns the offset in struct render_page's cp[axis] array of the rule with
    index RULE_IDX.  That is, if RULE_IDX is 0, then the offset is that of the
    leftmost or topmost rule; if RULE_IDX is 1, then the offset is that of the
@@ -607,7 +619,7 @@ set_join_crossings (struct render_page *page, enum table_axis axis,
    The new render_page will be suitable for rendering on a device whose page
    size is PARAMS->size, but the caller is responsible for actually breaking it
    up to fit on such a device, using the render_break abstraction.  */
-struct render_page *
+static struct render_page *
 render_page_create (const struct render_params *params,
                     const struct table *table_)
 {
@@ -794,7 +806,7 @@ render_page_ref (const struct render_page *page_)
 
 /* Decreases PAGE's reference count and destroys PAGE if this causes the
    reference count to fall to zero. */
-void
+static void
 render_page_unref (struct render_page *page)
 {
   if (page != NULL && --page->ref_cnt == 0)
@@ -862,7 +874,8 @@ is_rule (int z)
 }
 
 static void
-render_rule (const struct render_page *page, const int d[TABLE_N_AXES])
+render_rule (const struct render_page *page, const int ofs[TABLE_N_AXES],
+             const int d[TABLE_N_AXES])
 {
   enum render_line_style styles[TABLE_N_AXES][2];
   enum table_axis a;
@@ -901,25 +914,26 @@ render_rule (const struct render_page *page, const int d[TABLE_N_AXES])
     {
       int bb[TABLE_N_AXES][2];
 
-      bb[H][0] = page->cp[H][d[H]];
-      bb[H][1] = page->cp[H][d[H] + 1];
-      bb[V][0] = page->cp[V][d[V]];
-      bb[V][1] = page->cp[V][d[V] + 1];
+      bb[H][0] = ofs[H] + page->cp[H][d[H]];
+      bb[H][1] = ofs[H] + page->cp[H][d[H] + 1];
+      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);
     }
 }
 
 static void
-render_cell (const struct render_page *page, const struct table_cell *cell)
+render_cell (const struct render_page *page, const int ofs[TABLE_N_AXES],
+             const struct table_cell *cell)
 {
   const struct render_overflow *of;
   int bb[TABLE_N_AXES][2];
   int clip[TABLE_N_AXES][2];
 
-  bb[H][0] = clip[H][0] = page->cp[H][cell->d[H][0] * 2 + 1];
-  bb[H][1] = clip[H][1] = page->cp[H][cell->d[H][1] * 2];
-  bb[V][0] = clip[V][0] = page->cp[V][cell->d[V][0] * 2 + 1];
-  bb[V][1] = clip[V][1] = page->cp[V][cell->d[V][1] * 2];
+  bb[H][0] = clip[H][0] = ofs[H] + page->cp[H][cell->d[H][0] * 2 + 1];
+  bb[H][1] = clip[H][1] = ofs[H] + page->cp[H][cell->d[H][1] * 2];
+  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];
 
   of = find_overflow (page, cell->d[H][0], cell->d[V][0]);
   if (of)
@@ -932,13 +946,13 @@ render_cell (const struct render_page *page, const struct table_cell *cell)
             {
               bb[axis][0] -= of->overflow[axis][0];
               if (cell->d[axis][0] == 0 && !page->is_edge_cutoff[axis][0])
-                clip[axis][0] = page->cp[axis][cell->d[axis][0] * 2];
+                clip[axis][0] = ofs[axis] + page->cp[axis][cell->d[axis][0] * 2];
             }
           if (of->overflow[axis][1])
             {
               bb[axis][1] += of->overflow[axis][1];
               if (cell->d[axis][1] == page->n[axis] && !page->is_edge_cutoff[axis][1])
-                clip[axis][1] = page->cp[axis][cell->d[axis][1] * 2 + 1];
+                clip[axis][1] = ofs[axis] + page->cp[axis][cell->d[axis][1] * 2 + 1];
             }
         }
     }
@@ -949,7 +963,7 @@ render_cell (const struct render_page *page, const struct table_cell *cell)
 /* Draws the cells of PAGE indicated in BB. */
 static void
 render_page_draw_cells (const struct render_page *page,
-                        int bb[TABLE_N_AXES][2])
+                        int ofs[TABLE_N_AXES], int bb[TABLE_N_AXES][2])
 {
   int x, y;
 
@@ -960,7 +974,7 @@ render_page_draw_cells (const struct render_page *page,
           int d[TABLE_N_AXES];
           d[H] = x;
           d[V] = y;
-          render_rule (page, d);
+          render_rule (page, ofs, d);
           x++;
         }
       else
@@ -969,7 +983,7 @@ render_page_draw_cells (const struct render_page *page,
 
           table_get_cell (page->table, x / 2, y / 2, &cell);
           if (y / 2 == bb[V][0] / 2 || y / 2 == cell.d[V][0])
-            render_cell (page, &cell);
+            render_cell (page, ofs, &cell);
           x = rule_ofs (cell.d[H][1]);
           table_cell_free (&cell);
         }
@@ -978,7 +992,7 @@ render_page_draw_cells (const struct render_page *page,
 /* Renders PAGE, by calling the 'draw_line' and 'draw_cell' functions from the
    render_params provided to render_page_create(). */
 void
-render_page_draw (const struct render_page *page)
+render_page_draw (const struct render_page *page, int ofs[TABLE_N_AXES])
 {
   int bb[TABLE_N_AXES][2];
 
@@ -987,7 +1001,7 @@ render_page_draw (const struct render_page *page)
   bb[V][0] = 0;
   bb[V][1] = page->n[V] * 2 + 1;
 
-  render_page_draw_cells (page, bb);
+  render_page_draw_cells (page, ofs, bb);
 }
 
 /* Returns the greatest value i, 0 <= i < n, such that cp[i] <= x0. */
@@ -1045,20 +1059,30 @@ get_clip_max_extent (int x1, const int cp[], int n)
    render_page_create(). */
 void
 render_page_draw_region (const struct render_page *page,
-                         int x, int y, int w, int h)
+                         int ofs[TABLE_N_AXES], int clip[TABLE_N_AXES][2])
 {
   int bb[TABLE_N_AXES][2];
 
-  bb[H][0] = get_clip_min_extent (x, page->cp[H], page->n[H] * 2 + 1);
-  bb[H][1] = get_clip_max_extent (x + w, page->cp[H], page->n[H] * 2 + 1);
-  bb[V][0] = get_clip_min_extent (y, page->cp[V], page->n[V] * 2 + 1);
-  bb[V][1] = get_clip_max_extent (y + h, page->cp[V], page->n[V] * 2 + 1);
+  bb[H][0] = get_clip_min_extent (clip[H][0], page->cp[H], page->n[H] * 2 + 1);
+  bb[H][1] = get_clip_max_extent (clip[H][1], page->cp[H], page->n[H] * 2 + 1);
+  bb[V][0] = get_clip_min_extent (clip[V][0], page->cp[V], page->n[V] * 2 + 1);
+  bb[V][1] = get_clip_max_extent (clip[V][1], page->cp[V], page->n[V] * 2 + 1);
 
-  render_page_draw_cells (page, bb);
+  render_page_draw_cells (page, ofs, bb);
 }
 \f
 /* Breaking up tables to fit on a page. */
 
+/* An iterator for breaking render_pages into smaller chunks. */
+struct render_break
+  {
+    struct render_page *page;   /* Page being broken up. */
+    enum table_axis axis;       /* Axis along which 'page' is being broken. */
+    int z;                      /* Next cell along 'axis'. */
+    int pixel;                  /* Pixel offset within cell 'z' (usually 0). */
+    int hw;                     /* Width of headers of 'page' along 'axis'. */
+  };
+
 static int needed_size (const struct render_break *, int cell);
 static bool cell_is_breakable (const struct render_break *, int cell);
 static struct render_page *render_page_select (const struct render_page *,
@@ -1066,15 +1090,12 @@ static struct render_page *render_page_select (const struct render_page *,
                                                int z0, int p0,
                                                int z1, int p1);
 
-/* Initializes render_break B for breaking PAGE along AXIS.
-
-   Ownership of PAGE is transferred to B.  The caller must use
-   render_page_ref() if it needs to keep a copy of PAGE. */
-void
-render_break_init (struct render_break *b, struct render_page *page,
+/* Initializes render_break B for breaking PAGE along AXIS. */
+static void
+render_break_init (struct render_break *b, const struct render_page *page,
                    enum table_axis axis)
 {
-  b->page = page;
+  b->page = render_page_ref (page);
   b->axis = axis;
   b->z = page->h[axis][0];
   b->pixel = 0;
@@ -1083,7 +1104,7 @@ render_break_init (struct render_break *b, struct render_page *page,
 
 /* Initializes B as a render_break structure for which
    render_break_has_next() always returns false. */
-void
+static void
 render_break_init_empty (struct render_break *b)
 {
   b->page = NULL;
@@ -1094,7 +1115,7 @@ render_break_init_empty (struct render_break *b)
 }
 
 /* Frees B and unrefs the render_page that it owns. */
-void
+static void
 render_break_destroy (struct render_break *b)
 {
   if (b != NULL)
@@ -1106,7 +1127,7 @@ render_break_destroy (struct render_break *b)
 
 /* Returns true if B still has cells that are yet to be returned,
    false if all of B's page has been processed. */
-bool
+static bool
 render_break_has_next (const struct render_break *b)
 {
   const struct render_page *page = b->page;
@@ -1115,25 +1136,12 @@ render_break_has_next (const struct render_break *b)
   return page != NULL && b->z < page->n[axis] - page->h[axis][1];
 }
 
-/* Returns the minimum SIZE argument that, if passed to render_break_next(),
-   will avoid a null return value (if cells are still left). */
-int
-render_break_next_size (const struct render_break *b)
-{
-  const struct render_page *page = b->page;
-  enum table_axis axis = b->axis;
-
-  return (!render_break_has_next (b) ? 0
-          : !cell_is_breakable (b, b->z) ? needed_size (b, b->z + 1)
-          : b->hw + page->params->font_size[axis]);
-}
-
 /* Returns a new render_page that is up to SIZE pixels wide along B's axis.
    Returns a null pointer if B has already been completely broken up, or if
    SIZE is too small to reasonably render any cells.  The latter will never
    happen if SIZE is at least as large as the page size passed to
    render_page_create() along B's axis. */
-struct render_page *
+static struct render_page *
 render_break_next (struct render_break *b, int size)
 {
   const struct render_page *page = b->page;
@@ -1299,6 +1307,200 @@ cell_is_breakable (const struct render_break *b, int cell)
   return cell_width (page, axis, cell) >= page->params->min_break[axis];
 }
 \f
+/* render_pager. */
+
+struct render_pager
+  {
+    int width;
+    struct render_page **pages;
+    size_t cur_page, n_pages;
+    struct render_break x_break;
+    struct render_break y_break;
+  };
+
+static void
+render_pager_add_table (struct render_pager *p, struct table *table,
+                        const struct render_params *params,
+                        size_t *allocated_pages)
+{
+  if (p->n_pages >= *allocated_pages)
+    p->pages = x2nrealloc (p->pages, allocated_pages, sizeof *p->pages);
+  p->pages[p->n_pages++] = render_page_create (params, table);
+}
+
+static void
+render_pager_start_page (struct render_pager *p)
+{
+  render_break_init (&p->x_break, p->pages[p->cur_page++], H);
+  render_break_init_empty (&p->y_break);
+}
+
+/* Creates and returns a new render_pager for rendering TABLE_ITEM on the
+   device with the given PARAMS. */
+struct render_pager *
+render_pager_create (const struct render_params *params,
+                     const struct table_item *table_item)
+{
+  struct render_pager *p;
+  size_t allocated_pages = 0;
+  const char *caption;
+
+  p = xzalloc (sizeof *p);
+  p->width = params->size[H];
+
+  caption = table_item_get_caption (table_item);
+  if (caption)
+    render_pager_add_table (p, table_from_string (TAB_LEFT, caption), params,
+                            &allocated_pages);
+  render_pager_add_table (p, table_ref (table_item_get_table (table_item)), params,
+                          &allocated_pages);
+
+  render_pager_start_page (p);
+
+  return p;
+}
+
+/* Destroys P. */
+void
+render_pager_destroy (struct render_pager *p)
+{
+  if (p)
+    {
+      size_t i;
+
+      render_break_destroy (&p->x_break);
+      render_break_destroy (&p->y_break);
+      for (i = 0; i < p->n_pages; i++)
+        render_page_unref (p->pages[i]);
+      free (p->pages);
+      free (p);
+    }
+}
+
+/* Returns true if P has content remaining to render, false if rendering is
+   done. */
+bool
+render_pager_has_next (const struct render_pager *p_)
+{
+  struct render_pager *p = CONST_CAST (struct render_pager *, p_);
+
+  while (!render_break_has_next (&p->y_break))
+    {
+      render_break_destroy (&p->y_break);
+      if (!render_break_has_next (&p->x_break))
+        {
+          render_break_destroy (&p->x_break);
+          if (p->cur_page >= p->n_pages)
+            {
+              render_break_init_empty (&p->x_break);
+              render_break_init_empty (&p->y_break);
+              return false;
+            }
+          render_pager_start_page (p);
+        }
+      else
+        render_break_init (&p->y_break,
+                           render_break_next (&p->x_break, p->width), V);
+    }
+  return true;
+}
+
+/* Draws a chunk of content from P to fit in a space that has vertical size
+   SPACE and the horizontal size specified in the render_params passed to
+   render_page_create().  Returns the amount of space actually used by the
+   rendered chunk, which will be 0 if SPACE is too small to render anything or
+   if no content remains (use render_pager_has_next() to distinguish these
+   cases). */
+int
+render_pager_draw_next (struct render_pager *p, int space)
+{
+  int ofs[TABLE_N_AXES] = { 0, 0 };
+  size_t start_page = SIZE_MAX;
+
+  while (render_pager_has_next (p))
+    {
+      struct render_page *page;
+
+      if (start_page == p->cur_page)
+        break;
+      start_page = p->cur_page;
+
+      page = render_break_next (&p->y_break, space - ofs[V]);
+      if (!page)
+        break;
+
+      render_page_draw (page, ofs);
+      ofs[V] += render_page_get_size (page, V);
+      render_page_unref (page);
+    }
+  return ofs[V];
+}
+
+/* Draws all of P's content. */
+void
+render_pager_draw (const struct render_pager *p)
+{
+  render_pager_draw_region (p, 0, 0, INT_MAX, INT_MAX);
+}
+
+/* Draws the region of P's content that lies in the region (X,Y)-(X+W,Y+H).
+   Some extra content might be drawn; the device should perform clipping as
+   necessary. */
+void
+render_pager_draw_region (const struct render_pager *p,
+                          int x, int y, int w, int h)
+{
+  int ofs[TABLE_N_AXES] = { 0, 0 };
+  int clip[TABLE_N_AXES][2];
+  size_t i;
+
+  clip[H][0] = x;
+  clip[H][1] = x + w;
+  for (i = 0; i < p->n_pages; i++)
+    {
+      const struct render_page *page = p->pages[i];
+
+      clip[V][0] = MAX (y, ofs[V]) - ofs[V];
+      clip[V][1] = MIN (y + h, ofs[V] + render_page_get_size (page, V)) - ofs[V];
+      if (clip[V][1] > clip[V][0])
+        render_page_draw_region (page, ofs, clip);
+    }
+}
+
+/* Returns the size of P's content along AXIS; i.e. the content's width if AXIS
+   is TABLE_HORZ and its length if AXIS is TABLE_VERT. */
+int
+render_pager_get_size (const struct render_pager *p, enum table_axis axis)
+{
+  int size = 0;
+  size_t i;
+
+  for (i = 0; i < p->n_pages; i++)
+    {
+      int subsize = render_page_get_size (p->pages[i], axis);
+      size = axis == H ? MAX (size, subsize) : size + subsize;
+    }
+
+  return size;
+}
+
+int
+render_pager_get_best_breakpoint (const struct render_pager *p, int height)
+{
+  int y = 0;
+  size_t i;
+
+  for (i = 0; i < p->n_pages; i++)
+    {
+      int size = render_page_get_size (p->pages[i], V);
+      if (y + size >= height)
+        return render_page_get_best_breakpoint (p->pages[i], height - y) + y;
+      y += size;
+    }
+
+  return height;
+}
+\f
 /* render_page_select() and helpers. */
 
 struct render_page_selection