cairo: Move chart code into cairo-chart.
[pspp] / src / output / cairo.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include "output/cairo.h"
20
21 #include "libpspp/assertion.h"
22 #include "libpspp/cast.h"
23 #include "libpspp/hash-functions.h"
24 #include "libpspp/message.h"
25 #include "libpspp/pool.h"
26 #include "libpspp/start-date.h"
27 #include "libpspp/str.h"
28 #include "libpspp/string-map.h"
29 #include "libpspp/version.h"
30 #include "data/file-handle-def.h"
31 #include "output/cairo-chart.h"
32 #include "output/cairo-fsm.h"
33 #include "output/chart-item-provider.h"
34 #include "output/driver-provider.h"
35 #include "output/group-item.h"
36 #include "output/message-item.h"
37 #include "output/options.h"
38 #include "output/page-eject-item.h"
39 #include "output/page-setup-item.h"
40 #include "output/render.h"
41 #include "output/table-item.h"
42 #include "output/table.h"
43 #include "output/text-item.h"
44
45 #include <cairo/cairo-pdf.h>
46 #include <cairo/cairo-ps.h>
47 #include <cairo/cairo-svg.h>
48
49 #include <cairo/cairo.h>
50 #include <inttypes.h>
51 #include <math.h>
52 #include <pango/pango-font.h>
53 #include <pango/pango-layout.h>
54 #include <pango/pango.h>
55 #include <pango/pangocairo.h>
56 #include <stdlib.h>
57
58 #include "gl/c-ctype.h"
59 #include "gl/c-strcase.h"
60 #include "gl/intprops.h"
61 #include "gl/minmax.h"
62 #include "gl/xalloc.h"
63
64 #include "gettext.h"
65 #define _(msgid) gettext (msgid)
66
67 /* This file uses TABLE_HORZ and TABLE_VERT enough to warrant abbreviating. */
68 #define H TABLE_HORZ
69 #define V TABLE_VERT
70
71 /* The unit used for internal measurements is inch/(72 * XR_POINT).
72    (Thus, XR_POINT units represent one point.) */
73 #define XR_POINT PANGO_SCALE
74
75 /* Conversions to and from points. */
76 static double
77 xr_to_pt (int x)
78 {
79   return x / (double) XR_POINT;
80 }
81
82 /* Dimensions for drawing lines in tables. */
83 #define XR_LINE_WIDTH (XR_POINT / 2) /* Width of an ordinary line. */
84 #define XR_LINE_SPACE XR_POINT       /* Space between double lines. */
85
86 /* Output types. */
87 enum xr_output_type
88   {
89     XR_PDF,
90     XR_PS,
91     XR_SVG
92   };
93
94 /* Cairo output driver. */
95 struct xr_driver
96   {
97     struct output_driver driver;
98
99     /* User parameters. */
100     PangoFontDescription *fonts[XR_N_FONTS];
101
102     int width;                  /* Page width minus margins. */
103     int length;                 /* Page length minus margins and header. */
104
105     int left_margin;            /* Left margin in inch/(72 * XR_POINT). */
106     int right_margin;           /* Right margin in inch/(72 * XR_POINT). */
107     int top_margin;             /* Top margin in inch/(72 * XR_POINT). */
108     int bottom_margin;          /* Bottom margin in inch/(72 * XR_POINT). */
109
110     int min_break[TABLE_N_AXES]; /* Min cell size to break across pages. */
111     int object_spacing;         /* Space between output objects. */
112
113     struct cell_color bg;       /* Background color */
114     struct cell_color fg;       /* Foreground color */
115     bool transparent;           /* true -> do not render background */
116     bool systemcolors;          /* true -> do not change colors     */
117
118     int initial_page_number;
119
120     struct page_heading headings[2]; /* Top and bottom headings. */
121     int headings_height[2];
122
123     /* Internal state. */
124     struct xr_fsm_style *style;
125     double font_scale;
126     int char_width, char_height;
127     cairo_t *cairo;
128     cairo_surface_t *surface;
129     int page_number;            /* Current page number. */
130     int y;
131     struct xr_fsm *fsm;
132   };
133
134 static const struct output_driver_class cairo_driver_class;
135
136 static void xr_driver_destroy_fsm (struct xr_driver *);
137 static void xr_driver_run_fsm (struct xr_driver *);
138 \f
139 /* Output driver basics. */
140
141 static struct xr_driver *
142 xr_driver_cast (struct output_driver *driver)
143 {
144   assert (driver->class == &cairo_driver_class);
145   return UP_CAST (driver, struct xr_driver, driver);
146 }
147
148 static struct driver_option *
149 opt (struct output_driver *d, struct string_map *options, const char *key,
150      const char *default_value)
151 {
152   return driver_option_get (d, options, key, default_value);
153 }
154
155 static PangoFontDescription *
156 parse_font (const char *font, int default_size, bool bold, bool italic)
157 {
158   if (!c_strcasecmp (font, "Monospaced"))
159     font = "Monospace";
160
161   PangoFontDescription *desc = pango_font_description_from_string (font);
162   if (desc == NULL)
163     return NULL;
164
165   /* If the font description didn't include an explicit font size, then set it
166      to DEFAULT_SIZE, which is in inch/72000 units. */
167   if (!(pango_font_description_get_set_fields (desc) & PANGO_FONT_MASK_SIZE))
168     pango_font_description_set_size (desc,
169                                      (default_size / 1000.0) * PANGO_SCALE);
170
171   pango_font_description_set_weight (desc, (bold
172                                             ? PANGO_WEIGHT_BOLD
173                                             : PANGO_WEIGHT_NORMAL));
174   pango_font_description_set_style (desc, (italic
175                                            ? PANGO_STYLE_ITALIC
176                                            : PANGO_STYLE_NORMAL));
177
178   return desc;
179 }
180
181 static PangoFontDescription *
182 parse_font_option (struct output_driver *d, struct string_map *options,
183                    const char *key, const char *default_value,
184                    int default_size, bool bold, bool italic)
185 {
186   char *string = parse_string (opt (d, options, key, default_value));
187   PangoFontDescription *desc = parse_font (string, default_size, bold, italic);
188   if (!desc)
189     {
190       msg (MW, _("`%s': bad font specification"), string);
191
192       /* Fall back to DEFAULT_VALUE, which had better be a valid font
193          description. */
194       desc = parse_font (default_value, default_size, bold, italic);
195       assert (desc != NULL);
196     }
197   free (string);
198
199   return desc;
200 }
201
202 static void
203 apply_options (struct xr_driver *xr, struct string_map *o)
204 {
205   struct output_driver *d = &xr->driver;
206
207   /* In inch/72000 units used by parse_paper_size() and parse_dimension(). */
208   int left_margin, right_margin;
209   int top_margin, bottom_margin;
210   int paper_width, paper_length;
211   int font_size;
212   int min_break[TABLE_N_AXES];
213
214   /* Scale factor from inch/72000 to inch/(72 * XR_POINT). */
215   const double scale = XR_POINT / 1000.;
216
217   int i;
218
219   for (i = 0; i < XR_N_FONTS; i++)
220     if (xr->fonts[i] != NULL)
221       pango_font_description_free (xr->fonts[i]);
222
223   font_size = parse_int (opt (d, o, "font-size", "10000"), 1000, 1000000);
224   xr->fonts[XR_FONT_FIXED] = parse_font_option
225     (d, o, "fixed-font", "monospace", font_size, false, false);
226   xr->fonts[XR_FONT_PROPORTIONAL] = parse_font_option (
227     d, o, "prop-font", "sans serif", font_size, false, false);
228
229   xr->fg = parse_color (opt (d, o, "foreground-color", "#000000000000"));
230   xr->bg = parse_color (opt (d, o, "background-color", "#FFFFFFFFFFFF"));
231
232   xr->transparent = parse_boolean (opt (d, o, "transparent", "false"));
233   xr->systemcolors = parse_boolean (opt (d, o, "systemcolors", "false"));
234
235   /* Get dimensions.  */
236   parse_paper_size (opt (d, o, "paper-size", ""), &paper_width, &paper_length);
237   left_margin = parse_dimension (opt (d, o, "left-margin", ".5in"));
238   right_margin = parse_dimension (opt (d, o, "right-margin", ".5in"));
239   top_margin = parse_dimension (opt (d, o, "top-margin", ".5in"));
240   bottom_margin = parse_dimension (opt (d, o, "bottom-margin", ".5in"));
241
242   min_break[H] = parse_dimension (opt (d, o, "min-hbreak", NULL)) * scale;
243   min_break[V] = parse_dimension (opt (d, o, "min-vbreak", NULL)) * scale;
244
245   int object_spacing = (parse_dimension (opt (d, o, "object-spacing", NULL))
246                         * scale);
247
248   /* Convert to inch/(XR_POINT * 72). */
249   xr->left_margin = left_margin * scale;
250   xr->right_margin = right_margin * scale;
251   xr->top_margin = top_margin * scale;
252   xr->bottom_margin = bottom_margin * scale;
253   xr->width = (paper_width - left_margin - right_margin) * scale;
254   xr->length = (paper_length - top_margin - bottom_margin) * scale;
255   xr->min_break[H] = min_break[H] >= 0 ? min_break[H] : xr->width / 2;
256   xr->min_break[V] = min_break[V] >= 0 ? min_break[V] : xr->length / 2;
257   xr->object_spacing = object_spacing >= 0 ? object_spacing : XR_POINT * 12;
258
259   /* There are no headings so headings_height can stay 0. */
260 }
261
262 static struct xr_driver *
263 xr_allocate (const char *name, int device_type, struct string_map *o,
264              double font_scale)
265 {
266   struct xr_driver *xr = xzalloc (sizeof *xr);
267   struct output_driver *d = &xr->driver;
268
269   output_driver_init (d, &cairo_driver_class, name, device_type);
270
271   /* This is a nasty kluge for an issue that does not make sense.  On any
272      surface other than a screen (e.g. for output to PDF or PS or SVG), the
273      fonts are way too big by default.  A "9-point" font seems to appear about
274      16 points tall.  We use a scale factor for these surfaces to help, but the
275      underlying issue is a mystery. */
276   xr->font_scale = font_scale;
277
278   apply_options (xr, o);
279
280   return xr;
281 }
282
283 static int
284 pango_to_xr (int pango)
285 {
286   return (XR_POINT != PANGO_SCALE
287           ? ceil (pango * (1. * XR_POINT / PANGO_SCALE))
288           : pango);
289 }
290
291 static int
292 xr_to_pango (int xr)
293 {
294   return (XR_POINT != PANGO_SCALE
295           ? ceil (xr * (1. / XR_POINT * PANGO_SCALE))
296           : xr);
297 }
298
299 static void
300 xr_measure_fonts (cairo_t *cairo, PangoFontDescription *fonts[XR_N_FONTS],
301                   int *char_width, int *char_height)
302 {
303   *char_width = 0;
304   *char_height = 0;
305   for (int i = 0; i < XR_N_FONTS; i++)
306     {
307       PangoLayout *layout = pango_cairo_create_layout (cairo);
308       pango_layout_set_font_description (layout, fonts[i]);
309
310       pango_layout_set_text (layout, "0", 1);
311
312       int cw, ch;
313       pango_layout_get_size (layout, &cw, &ch);
314       *char_width = MAX (*char_width, pango_to_xr (cw));
315       *char_height = MAX (*char_height, pango_to_xr (ch));
316
317       g_object_unref (G_OBJECT (layout));
318     }
319 }
320
321 static int
322 get_layout_height (PangoLayout *layout)
323 {
324   int w, h;
325   pango_layout_get_size (layout, &w, &h);
326   return h;
327 }
328
329 static int
330 xr_render_page_heading (cairo_t *cairo, const PangoFontDescription *font,
331                         const struct page_heading *ph, int page_number,
332                         int width, bool draw, int base_y)
333 {
334   PangoLayout *layout = pango_cairo_create_layout (cairo);
335   pango_layout_set_font_description (layout, font);
336
337   int y = 0;
338   for (size_t i = 0; i < ph->n; i++)
339     {
340       const struct page_paragraph *pp = &ph->paragraphs[i];
341
342       char *markup = output_driver_substitute_heading_vars (pp->markup,
343                                                             page_number);
344       pango_layout_set_markup (layout, markup, -1);
345       free (markup);
346
347       pango_layout_set_alignment (
348         layout,
349         (pp->halign == TABLE_HALIGN_LEFT ? PANGO_ALIGN_LEFT
350          : pp->halign == TABLE_HALIGN_CENTER ? PANGO_ALIGN_CENTER
351          : pp->halign == TABLE_HALIGN_MIXED ? PANGO_ALIGN_LEFT
352          : PANGO_ALIGN_RIGHT));
353       pango_layout_set_width (layout, xr_to_pango (width));
354       if (draw)
355         {
356           cairo_save (cairo);
357           cairo_translate (cairo, 0, xr_to_pt (y + base_y));
358           pango_cairo_show_layout (cairo, layout);
359           cairo_restore (cairo);
360         }
361
362       y += pango_to_xr (get_layout_height (layout));
363     }
364
365   g_object_unref (G_OBJECT (layout));
366
367   return y;
368 }
369
370 static int
371 xr_measure_headings (cairo_surface_t *surface,
372                      const PangoFontDescription *font,
373                      const struct page_heading headings[2],
374                      int width, int object_spacing, int height[2])
375 {
376   cairo_t *cairo = cairo_create (surface);
377   int total = 0;
378   for (int i = 0; i < 2; i++)
379     {
380       int h = xr_render_page_heading (cairo, font, &headings[i], -1,
381                                       width, false, 0);
382
383       /* If the top heading is nonempty, add some space below it. */
384       if (h && i == 0)
385         h += object_spacing;
386
387       if (height)
388         height[i] = h;
389       total += h;
390     }
391   cairo_destroy (cairo);
392   return total;
393 }
394
395 static bool
396 xr_check_fonts (cairo_surface_t *surface,
397                 PangoFontDescription *fonts[XR_N_FONTS],
398                 int usable_width, int usable_length)
399 {
400   cairo_t *cairo = cairo_create (surface);
401   int char_width, char_height;
402   xr_measure_fonts (cairo, fonts, &char_width, &char_height);
403   cairo_destroy (cairo);
404
405   bool ok = true;
406   enum { MIN_WIDTH = 3, MIN_LENGTH = 3 };
407   if (usable_width / char_width < MIN_WIDTH)
408     {
409       msg (ME, _("The defined page is not wide enough to hold at least %d "
410                  "characters in the default font.  In fact, there's only "
411                  "room for %d characters."),
412            MIN_WIDTH, usable_width / char_width);
413       ok = false;
414     }
415   if (usable_length / char_height < MIN_LENGTH)
416     {
417       msg (ME, _("The defined page is not long enough to hold at least %d "
418                  "lines in the default font.  In fact, there's only "
419                  "room for %d lines."),
420            MIN_LENGTH, usable_length / char_height);
421       ok = false;
422     }
423   return ok;
424 }
425
426 static void
427 xr_set_cairo (struct xr_driver *xr, cairo_t *cairo)
428 {
429   xr->cairo = cairo;
430
431   cairo_set_line_width (xr->cairo, xr_to_pt (XR_LINE_WIDTH));
432
433   xr_measure_fonts (xr->cairo, xr->fonts, &xr->char_width, &xr->char_height);
434
435   if (xr->style == NULL)
436     {
437       xr->style = xmalloc (sizeof *xr->style);
438       *xr->style = (struct xr_fsm_style) {
439         .ref_cnt = 1,
440         .size = { [H] = xr->width, [V] = xr->length },
441         .min_break = { [H] = xr->min_break[H], [V] = xr->min_break[V] },
442         .use_system_colors = xr->systemcolors,
443         .transparent = xr->transparent,
444         .font_scale = xr->font_scale,
445       };
446
447       for (size_t i = 0; i < XR_N_FONTS; i++)
448         xr->style->fonts[i] = pango_font_description_copy (xr->fonts[i]);
449     }
450
451   if (!xr->systemcolors)
452     cairo_set_source_rgb (xr->cairo,
453                           xr->fg.r / 255.0, xr->fg.g / 255.0, xr->fg.b / 255.0);
454 }
455
456 static struct output_driver *
457 xr_create (struct file_handle *fh, enum settings_output_devices device_type,
458            struct string_map *o, enum xr_output_type file_type)
459 {
460   const char *file_name = fh_get_file_name (fh);
461   struct xr_driver *xr = xr_allocate (file_name, device_type, o, 72.0 / 128.0);
462   double width_pt = xr_to_pt (xr->width + xr->left_margin + xr->right_margin);
463   double length_pt = xr_to_pt (xr->length + xr->top_margin + xr->bottom_margin);
464   if (file_type == XR_PDF)
465     xr->surface = cairo_pdf_surface_create (file_name, width_pt, length_pt);
466   else if (file_type == XR_PS)
467     xr->surface = cairo_ps_surface_create (file_name, width_pt, length_pt);
468   else if (file_type == XR_SVG)
469     xr->surface = cairo_svg_surface_create (file_name, width_pt, length_pt);
470   else
471     NOT_REACHED ();
472
473   cairo_status_t status = cairo_surface_status (xr->surface);
474   if (status != CAIRO_STATUS_SUCCESS)
475     {
476       msg (ME, _("error opening output file `%s': %s"),
477            file_name, cairo_status_to_string (status));
478       goto error;
479     }
480
481   if (!xr_check_fonts (xr->surface, xr->fonts, xr->width, xr->length))
482     goto error;
483
484   fh_unref (fh);
485   return &xr->driver;
486
487  error:
488   fh_unref (fh);
489   output_driver_destroy (&xr->driver);
490   return NULL;
491 }
492
493 static struct output_driver *
494 xr_pdf_create (struct  file_handle *fh, enum settings_output_devices device_type,
495                struct string_map *o)
496 {
497   return xr_create (fh, device_type, o, XR_PDF);
498 }
499
500 static struct output_driver *
501 xr_ps_create (struct  file_handle *fh, enum settings_output_devices device_type,
502                struct string_map *o)
503 {
504   return xr_create (fh, device_type, o, XR_PS);
505 }
506
507 static struct output_driver *
508 xr_svg_create (struct file_handle *fh, enum settings_output_devices device_type,
509                struct string_map *o)
510 {
511   return xr_create (fh, device_type, o, XR_SVG);
512 }
513
514 static void
515 xr_destroy (struct output_driver *driver)
516 {
517   struct xr_driver *xr = xr_driver_cast (driver);
518   size_t i;
519
520   xr_driver_destroy_fsm (xr);
521
522   if (xr->cairo != NULL)
523     {
524       cairo_surface_finish (xr->surface);
525       cairo_status_t status = cairo_status (xr->cairo);
526       if (status != CAIRO_STATUS_SUCCESS)
527         fprintf (stderr,  _("error drawing output for %s driver: %s"),
528                  output_driver_get_name (driver),
529                  cairo_status_to_string (status));
530       cairo_surface_destroy (xr->surface);
531
532       cairo_destroy (xr->cairo);
533     }
534
535   for (i = 0; i < XR_N_FONTS; i++)
536     if (xr->fonts[i] != NULL)
537       pango_font_description_free (xr->fonts[i]);
538
539   xr_fsm_style_unref (xr->style);
540   free (xr);
541 }
542
543 static void
544 xr_flush (struct output_driver *driver)
545 {
546   struct xr_driver *xr = xr_driver_cast (driver);
547
548   cairo_surface_flush (cairo_get_target (xr->cairo));
549 }
550
551 static void
552 xr_update_page_setup (struct output_driver *driver,
553                       const struct page_setup *ps)
554 {
555   struct xr_driver *xr = xr_driver_cast (driver);
556
557   xr->initial_page_number = ps->initial_page_number;
558   xr->object_spacing = ps->object_spacing * 72 * XR_POINT;
559
560   if (xr->cairo)
561     return;
562
563   int usable[TABLE_N_AXES];
564   for (int i = 0; i < 2; i++)
565     usable[i] = (ps->paper[i]
566                  - (ps->margins[i][0] + ps->margins[i][1])) * 72 * XR_POINT;
567
568   int headings_height[2];
569   usable[V] -= xr_measure_headings (
570     xr->surface, xr->fonts[XR_FONT_PROPORTIONAL], ps->headings,
571     usable[H], xr->object_spacing, headings_height);
572
573   enum table_axis h = ps->orientation == PAGE_LANDSCAPE;
574   enum table_axis v = !h;
575   if (!xr_check_fonts (xr->surface, xr->fonts, usable[h], usable[v]))
576     return;
577
578   for (int i = 0; i < 2; i++)
579     {
580       page_heading_uninit (&xr->headings[i]);
581       page_heading_copy (&xr->headings[i], &ps->headings[i]);
582       xr->headings_height[i] = headings_height[i];
583     }
584   xr->width = usable[h];
585   xr->length = usable[v];
586   xr->left_margin = ps->margins[h][0] * 72 * XR_POINT;
587   xr->right_margin = ps->margins[h][1] * 72 * XR_POINT;
588   xr->top_margin = ps->margins[v][0] * 72 * XR_POINT;
589   xr->bottom_margin = ps->margins[v][1] * 72 * XR_POINT;
590   cairo_pdf_surface_set_size (xr->surface,
591                               ps->paper[h] * 72.0, ps->paper[v] * 72.0);
592 }
593
594 static void
595 xr_submit (struct output_driver *driver, const struct output_item *output_item)
596 {
597   struct xr_driver *xr = xr_driver_cast (driver);
598
599   if (is_page_setup_item (output_item))
600     {
601       xr_update_page_setup (driver,
602                             to_page_setup_item (output_item)->page_setup);
603       return;
604     }
605
606   if (!xr->cairo)
607     {
608       xr->page_number = xr->initial_page_number - 1;
609       xr_set_cairo (xr, cairo_create (xr->surface));
610       cairo_save (xr->cairo);
611       xr_driver_next_page (xr, xr->cairo);
612     }
613
614   xr_driver_output_item (xr, output_item);
615   while (xr_driver_need_new_page (xr))
616     {
617       cairo_restore (xr->cairo);
618       cairo_show_page (xr->cairo);
619       cairo_save (xr->cairo);
620       xr_driver_next_page (xr, xr->cairo);
621     }
622 }
623 \f
624 /* Functions for rendering a series of output items to a series of Cairo
625    contexts, with pagination.
626
627    Used by PSPPIRE for printing, and by the basic Cairo output driver above as
628    its underlying implementation.
629
630    See the big comment in cairo.h for intended usage. */
631
632 /* Gives new page CAIRO to XR for output. */
633 void
634 xr_driver_next_page (struct xr_driver *xr, cairo_t *cairo)
635 {
636   if (!xr->transparent)
637     {
638       cairo_save (cairo);
639       cairo_set_source_rgb (cairo,
640                             xr->bg.r / 255.0, xr->bg.g / 255.0, xr->bg.b / 255.0);
641       cairo_rectangle (cairo, 0, 0, xr->width, xr->length);
642       cairo_fill (cairo);
643       cairo_restore (cairo);
644     }
645   cairo_translate (cairo,
646                    xr_to_pt (xr->left_margin),
647                    xr_to_pt (xr->top_margin + xr->headings_height[0]));
648
649   xr->page_number++;
650   xr->cairo = cairo;
651   xr->y = 0;
652
653   xr_render_page_heading (xr->cairo, xr->fonts[XR_FONT_PROPORTIONAL],
654                           &xr->headings[0], xr->page_number, xr->width, true,
655                           -xr->headings_height[0]);
656   xr_render_page_heading (xr->cairo, xr->fonts[XR_FONT_PROPORTIONAL],
657                           &xr->headings[1], xr->page_number, xr->width, true,
658                           xr->length);
659
660   xr_driver_run_fsm (xr);
661 }
662
663 /* Start rendering OUTPUT_ITEM to XR.  Only valid if XR is not in the middle of
664    rendering a previous output item, that is, only if xr_driver_need_new_page()
665    returns false. */
666 void
667 xr_driver_output_item (struct xr_driver *xr,
668                        const struct output_item *output_item)
669 {
670   assert (xr->fsm == NULL);
671   xr->fsm = xr_fsm_create (output_item, xr->style, xr->cairo);
672   xr_driver_run_fsm (xr);
673 }
674
675 /* Returns true if XR is in the middle of rendering an output item and needs a
676    new page to be appended using xr_driver_next_page() to make progress,
677    otherwise false. */
678 bool
679 xr_driver_need_new_page (const struct xr_driver *xr)
680 {
681   return xr->fsm != NULL;
682 }
683
684 /* Returns true if the current page doesn't have any content yet. */
685 bool
686 xr_driver_is_page_blank (const struct xr_driver *xr)
687 {
688   return xr->y == 0;
689 }
690
691 static void
692 xr_driver_destroy_fsm (struct xr_driver *xr)
693 {
694   xr_fsm_destroy (xr->fsm);
695   xr->fsm = NULL;
696 }
697
698 static void
699 xr_driver_run_fsm (struct xr_driver *xr)
700 {
701   if (xr->fsm != NULL)
702     {
703       cairo_save (xr->cairo);
704       cairo_translate (xr->cairo, 0, xr_to_pt (xr->y));
705       int used = xr_fsm_draw_slice (xr->fsm, xr->cairo, xr->length - xr->y);
706       xr->y += used;
707       cairo_restore (xr->cairo);
708
709       if (xr_fsm_is_empty (xr->fsm))
710         xr_driver_destroy_fsm (xr);
711     }
712 }
713 \f
714 struct output_driver_factory pdf_driver_factory =
715   { "pdf", "pspp.pdf", xr_pdf_create };
716 struct output_driver_factory ps_driver_factory =
717   { "ps", "pspp.ps", xr_ps_create };
718 struct output_driver_factory svg_driver_factory =
719   { "svg", "pspp.svg", xr_svg_create };
720
721 static const struct output_driver_class cairo_driver_class =
722 {
723   "cairo",
724   xr_destroy,
725   xr_submit,
726   xr_flush,
727 };
728 \f
729 struct xr_driver *
730 xr_driver_create (cairo_t *cairo, struct string_map *options)
731 {
732   struct xr_driver *xr = xr_allocate ("cairo", 0, options, 1.0);
733   xr_set_cairo (xr, cairo);
734   return xr;
735 }
736
737 /* Destroy XR, which should have been created with xr_driver_create().  Any
738    cairo_t added to XR is not destroyed, because it is owned by the client. */
739 void
740 xr_driver_destroy (struct xr_driver *xr)
741 {
742   if (xr != NULL)
743     {
744       xr->cairo = NULL;
745       output_driver_destroy (&xr->driver);
746     }
747 }