+ PangoLayoutIter *iter;
+ int best UNUSED = 0;
+
+ /* Choose a breakpoint between lines instead of in the middle of one. */
+ iter = pango_layout_get_iter (font->layout);
+ do
+ {
+ PangoRectangle extents;
+ int y0, y1;
+ int bottom;
+
+ pango_layout_iter_get_line_extents (iter, NULL, &extents);
+ pango_layout_iter_get_line_yrange (iter, &y0, &y1);
+ extents.x = pango_to_xr (extents.x);
+ extents.y = pango_to_xr (y0);
+ extents.width = pango_to_xr (extents.width);
+ extents.height = pango_to_xr (y1 - y0);
+ bottom = y + extents.y + extents.height;
+ if (bottom < bb[V][1])
+ {
+ if (brk && clip[H][0] != clip[H][1])
+ best = bottom;
+ if (brk)
+ *brk = bottom;
+ }
+ else
+ break;
+ }
+ while (pango_layout_iter_next_line (iter));
+ pango_layout_iter_free (iter);
+
+ /* If enabled, draws a green line across the chosen breakpoint, which can
+ be useful for debugging issues with breaking. */
+ if (0)
+ {
+ if (best && !xr->nest)
+ {
+ cairo_save (xr->cairo);
+ cairo_set_source_rgb (xr->cairo, 0, 1, 0);
+ dump_line (xr, -xr->left_margin, best, xr->width + xr->right_margin, best);
+ cairo_restore (xr->cairo);
+ }
+ }
+ }
+
+ pango_layout_set_attributes (font->layout, NULL);
+ return y + h;
+}
+
+static int
+xr_layout_cell_subtable (struct xr_driver *xr,
+ const struct cell_contents *contents,
+ int footnote_idx UNUSED,
+ int bb[TABLE_N_AXES][2],
+ int clip[TABLE_N_AXES][2], int *widthp, int *brk)
+{
+ int single_width, double_width;
+ struct render_params params;
+ struct render_pager *p;
+ int r[TABLE_N_AXES][2];
+ int width, height;
+ int i;
+
+ params.draw_line = xr_draw_line;
+ params.measure_cell_width = xr_measure_cell_width;
+ params.measure_cell_height = xr_measure_cell_height;
+ params.adjust_break = NULL;
+ params.draw_cell = xr_draw_cell;
+ params.aux = xr;
+ params.size[H] = bb[H][1] - bb[H][0];
+ params.size[V] = bb[V][1] - bb[V][0];
+ params.font_size[H] = xr->char_width;
+ params.font_size[V] = xr->char_height;
+
+ single_width = 2 * xr->line_gutter + xr->line_width;
+ double_width = 2 * xr->line_gutter + xr->line_space + 2 * xr->line_width;
+ for (i = 0; i < TABLE_N_AXES; i++)
+ {
+ params.line_widths[i][RENDER_LINE_NONE] = 0;
+ params.line_widths[i][RENDER_LINE_SINGLE] = single_width;
+ params.line_widths[i][RENDER_LINE_DOUBLE] = double_width;
+ }
+
+ xr->nest++;
+ p = render_pager_create (¶ms, contents->table);
+ width = render_pager_get_size (p, H);
+ height = render_pager_get_size (p, V);
+ if (bb[V][0] + height >= bb[V][1])
+ *brk = bb[V][0] + render_pager_get_best_breakpoint (p, bb[V][1] - bb[V][0]);
+
+ /* r = intersect(bb, clip) - bb. */
+ for (i = 0; i < TABLE_N_AXES; i++)
+ {
+ r[i][0] = MAX (bb[i][0], clip[i][0]) - bb[i][0];
+ r[i][1] = MIN (bb[i][1], clip[i][1]) - bb[i][0];
+ }
+
+ if (r[H][0] < r[H][1] && r[V][0] < r[V][1])
+ {
+ unsigned int alignment = contents->options & TAB_ALIGNMENT;
+ int save_x = xr->x;
+
+ cairo_save (xr->cairo);
+ xr_clip (xr, clip);
+ xr->x += bb[H][0];
+ if (alignment == TAB_RIGHT)
+ xr->x += params.size[H] - width;
+ else if (alignment == TAB_CENTER)
+ xr->x += (params.size[H] - width) / 2;
+ xr->y += bb[V][0];
+ render_pager_draw_region (p, r[H][0], r[V][0],
+ r[H][1] - r[H][0], r[V][1] - r[V][0]);
+ xr->y -= bb[V][0];
+ xr->x = save_x;
+ cairo_restore (xr->cairo);
+ }
+ render_pager_destroy (p);
+ xr->nest--;
+
+ if (width > *widthp)
+ *widthp = width;
+ return bb[V][0] + height;
+}
+
+static void
+xr_layout_cell (struct xr_driver *xr, const struct table_cell *cell,
+ int footnote_idx,
+ int bb_[TABLE_N_AXES][2], int clip[TABLE_N_AXES][2],
+ int *width, int *height, int *brk)
+{
+ int bb[TABLE_N_AXES][2];
+ size_t i;
+
+ *width = 0;
+ *height = 0;
+ if (brk)
+ *brk = 0;
+
+ memcpy (bb, bb_, sizeof bb);
+
+ /* If enabled, draws a blue rectangle around the cell extents, which can be
+ useful for debugging layout. */
+ if (0)
+ {
+ if (clip[H][0] != clip[H][1])
+ {
+ int offset = (xr->nest) * XR_POINT;
+
+ cairo_save (xr->cairo);
+ cairo_set_source_rgb (xr->cairo, 0, 0, 1);
+ dump_rectangle (xr,
+ bb[H][0] + offset, bb[V][0] + offset,
+ bb[H][1] - offset, bb[V][1] - offset);
+ cairo_restore (xr->cairo);
+ }
+ }
+
+ for (i = 0; i < cell->n_contents && bb[V][0] < bb[V][1]; i++)
+ {
+ const struct cell_contents *contents = &cell->contents[i];
+
+ if (brk)
+ *brk = bb[V][0];
+ if (i > 0)
+ {
+ bb[V][0] += xr->char_height / 2;
+ if (bb[V][0] >= bb[V][1])
+ break;
+ if (brk)
+ *brk = bb[V][0];
+ }
+
+ if (contents->text)
+ bb[V][0] = xr_layout_cell_text (xr, contents, footnote_idx, bb, clip,
+ bb[V][0], width, brk);
+ else
+ bb[V][0] = xr_layout_cell_subtable (xr, contents, footnote_idx,
+ bb, clip, width, brk);
+ footnote_idx += contents->n_footnotes;
+ }
+ *height = bb[V][0] - bb_[V][0];
+}
+\f
+struct output_driver_factory pdf_driver_factory =
+ { "pdf", "pspp.pdf", xr_pdf_create };
+struct output_driver_factory ps_driver_factory =
+ { "ps", "pspp.ps", xr_ps_create };
+struct output_driver_factory svg_driver_factory =
+ { "svg", "pspp.svg", xr_svg_create };
+
+static const struct output_driver_class cairo_driver_class =
+{
+ "cairo",
+ xr_destroy,
+ xr_submit,
+ xr_flush,
+};
+\f
+/* GUI rendering helpers. */
+
+struct xr_rendering
+ {
+ struct output_item *item;
+
+ /* Table items. */
+ struct render_pager *p;
+ struct xr_driver *xr;
+ };
+
+#define CHART_WIDTH 500
+#define CHART_HEIGHT 375
+
+
+
+struct xr_driver *
+xr_driver_create (cairo_t *cairo, struct string_map *options)
+{
+ struct xr_driver *xr = xr_allocate ("cairo", 0, options);
+ if (!xr_set_cairo (xr, cairo))
+ {
+ output_driver_destroy (&xr->driver);
+ return NULL;
+ }
+ return xr;
+}
+
+/* Destroy XR, which should have been created with xr_driver_create(). Any
+ cairo_t added to XR is not destroyed, because it is owned by the client. */
+void
+xr_driver_destroy (struct xr_driver *xr)
+{
+ if (xr != NULL)
+ {
+ xr->cairo = NULL;
+ output_driver_destroy (&xr->driver);
+ }
+}
+
+static struct xr_rendering *
+xr_rendering_create_text (struct xr_driver *xr, const char *text, cairo_t *cr)
+{
+ struct table_item *table_item;
+ struct xr_rendering *r;
+
+ table_item = table_item_create (table_from_string (TAB_LEFT, text),
+ NULL, NULL);
+ r = xr_rendering_create (xr, &table_item->output_item, cr);
+ table_item_unref (table_item);
+
+ return r;
+}
+
+void
+xr_rendering_apply_options (struct xr_rendering *xr, struct string_map *o)
+{
+ if (is_table_item (xr->item))
+ apply_options (xr->xr, o);
+}
+
+struct xr_rendering *
+xr_rendering_create (struct xr_driver *xr, const struct output_item *item,
+ cairo_t *cr)
+{
+ struct xr_rendering *r = NULL;
+
+ if (is_text_item (item))
+ r = xr_rendering_create_text (xr, text_item_get_text (to_text_item (item)),
+ cr);
+ else if (is_message_item (item))
+ {
+ const struct message_item *message_item = to_message_item (item);
+ const struct msg *msg = message_item_get_msg (message_item);
+ char *s = msg_to_string (msg, NULL);
+ r = xr_rendering_create_text (xr, s, cr);
+ free (s);
+ }
+ else if (is_table_item (item))
+ {
+ r = xzalloc (sizeof *r);
+ r->item = output_item_ref (item);
+ r->xr = xr;
+ xr_set_cairo (xr, cr);
+ r->p = render_pager_create (xr->params, to_table_item (item));
+ }
+ else if (is_chart_item (item))
+ {
+ r = xzalloc (sizeof *r);
+ r->item = output_item_ref (item);
+ }
+
+ return r;
+}
+
+void
+xr_rendering_destroy (struct xr_rendering *r)
+{
+ if (r)
+ {
+ output_item_unref (r->item);
+ render_pager_destroy (r->p);
+ free (r);
+ }
+}
+
+void
+xr_rendering_measure (struct xr_rendering *r, int *w, int *h)
+{
+ if (is_table_item (r->item))
+ {
+ *w = render_pager_get_size (r->p, H) / XR_POINT;
+ *h = render_pager_get_size (r->p, V) / XR_POINT;
+ }
+ else
+ {
+ *w = CHART_WIDTH;
+ *h = CHART_HEIGHT;
+ }
+}
+
+static void xr_draw_chart (const struct chart_item *, cairo_t *,
+ double x, double y, double width, double height);
+
+/* Draws onto CR */
+void
+xr_rendering_draw_all (struct xr_rendering *r, cairo_t *cr)
+{
+ if (is_table_item (r->item))
+ {
+ struct xr_driver *xr = r->xr;
+
+ xr_set_cairo (xr, cr);
+
+ render_pager_draw (r->p);
+