+ return cell_width (page, axis, cell) >= page->params->min_break[axis];
+}
+\f
+/* render_pager. */
+
+struct render_pager
+ {
+ const struct render_params *params;
+
+ struct render_page **pages;
+ size_t n_pages, allocated_pages;
+
+ size_t cur_page;
+ struct render_break x_break;
+ struct render_break y_break;
+ };
+
+static const struct render_page *
+render_pager_add_table (struct render_pager *p, struct table *table)
+{
+ struct render_page *page;
+
+ if (p->n_pages >= p->allocated_pages)
+ p->pages = x2nrealloc (p->pages, &p->allocated_pages, sizeof *p->pages);
+ page = p->pages[p->n_pages++] = render_page_create (p->params, table);
+ return page;
+}
+
+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);
+}
+
+static void
+add_footnote_page (struct render_pager *p, const struct render_page *body)
+{
+ const struct table *table = body->table;
+ int nc = table_nc (table);
+ int nr = table_nr (table);
+ int footnote_idx = 0;
+ struct tab_table *t;
+ int x, y;
+
+ if (!body->n_footnotes)
+ return;
+
+ t = tab_create (2, body->n_footnotes);
+ for (y = 0; y < nr; y++)
+ for (x = 0; x < nc; )
+ {
+ struct table_cell cell;
+
+ table_get_cell (table, x, y, &cell);
+ if (y == cell.d[V][0])
+ {
+ size_t i;
+
+ for (i = 0; i < cell.n_contents; i++)
+ {
+ const struct cell_contents *cc = &cell.contents[i];
+ size_t j;
+
+ for (j = 0; j < cc->n_footnotes; j++)
+ {
+ const char *f = cc->footnotes[j];
+
+ tab_text (t, 0, footnote_idx, TAB_LEFT, "");
+ tab_footnote (t, 0, footnote_idx, "(none)");
+ tab_text (t, 1, footnote_idx, TAB_LEFT, f);
+ footnote_idx++;
+ }
+ }
+ }
+ x = cell.d[H][1];
+ table_cell_free (&cell);
+ }
+ render_pager_add_table (p, &t->table);
+}
+
+/* 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)
+{
+ const char *caption = table_item_get_caption (table_item);
+ const char *title = table_item_get_title (table_item);
+ const struct render_page *body_page;
+ struct render_pager *p;
+
+ p = xzalloc (sizeof *p);
+ p->params = params;
+
+ /* Title. */
+ if (title)
+ render_pager_add_table (p, table_from_string (TAB_LEFT, title));
+
+ /* Body. */
+ body_page = render_pager_add_table (p, table_ref (table_item_get_table (
+ table_item)));
+
+ /* Caption. */
+ if (caption)
+ render_pager_add_table (p, table_from_string (TAB_LEFT, caption));
+
+ /* Footnotes. */
+ add_footnote_page (p, body_page);
+
+ 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->params->size[H]), 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];
+ int size = render_page_get_size (page, V);
+
+ clip[V][0] = MAX (y, ofs[V]) - ofs[V];
+ clip[V][1] = MIN (y + h, ofs[V] + size) - ofs[V];
+ if (clip[V][1] > clip[V][0])
+ render_page_draw_region (page, ofs, clip);
+
+ ofs[V] += size;
+ }
+}
+
+/* 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;