Handle multiple postcomputes.
[pspp] / src / output / cairo-chart.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2004, 2009, 2010, 2011, 2014, 2015,
3    2020 Free Software Foundation, Inc.
4
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
17
18 #include <config.h>
19
20 #include "output/cairo-chart.h"
21 #include "math/chart-geometry.h"
22
23 #include <cairo/cairo-ps.h>
24 #include <cairo/cairo.h>
25 #include <pango/pango.h>
26 #include <pango/pangocairo.h>
27 #include <float.h>
28 #include <math.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "libpspp/assertion.h"
35 #include "libpspp/message.h"
36 #include "math/chart-geometry.h"
37 #include "output/charts/barchart.h"
38 #include "output/charts/boxplot.h"
39 #include "output/charts/np-plot.h"
40 #include "output/charts/piechart.h"
41 #include "output/charts/plot-hist.h"
42 #include "output/charts/roc-chart.h"
43 #include "output/charts/scatterplot.h"
44 #include "output/charts/scree.h"
45 #include "output/charts/spreadlevel-plot.h"
46 #include "output/table.h"
47
48 #include "gl/xalloc.h"
49 #include "gl/xvasprintf.h"
50
51 #include "gettext.h"
52 #define _(msgid) gettext (msgid)
53
54 void
55 xrchart_geometry_init (cairo_t *cr, struct xrchart_geometry *geom,
56                        double width, double length)
57 {
58   /* Set default chart geometry. */
59   geom->axis[SCALE_ORDINATE].data_max = 0.900 * length;
60   geom->axis[SCALE_ORDINATE].data_min = 0.200 * length;
61
62   geom->axis[SCALE_ABSCISSA].data_min = 0.150 * width;
63   geom->axis[SCALE_ABSCISSA].data_max = 0.800 * width;
64   geom->abscissa_bottom = 0.070 * length;
65   geom->ordinate_left = 0.050 * width;
66   geom->title_bottom = 0.920 * length;
67   geom->legend_left = 0.810 * width;
68   geom->legend_right = width;
69   geom->font_size = 15.0;
70   geom->in_path = false;
71   geom->dataset = NULL;
72   geom->n_datasets = 0;
73
74   geom->fill_colour = data_colour[0];
75
76   cairo_set_line_width (cr, 1.0);
77
78   cairo_rectangle (cr, geom->axis[SCALE_ABSCISSA].data_min, geom->axis[SCALE_ORDINATE].data_min,
79                    geom->axis[SCALE_ABSCISSA].data_max - geom->axis[SCALE_ABSCISSA].data_min,
80                    geom->axis[SCALE_ORDINATE].data_max - geom->axis[SCALE_ORDINATE].data_min);
81   cairo_stroke (cr);
82 }
83
84 void
85 xrchart_geometry_free (cairo_t *cr UNUSED, struct xrchart_geometry *geom)
86 {
87   int i;
88
89   for (i = 0 ; i < geom->n_datasets; ++i)
90     free (geom->dataset[i]);
91   free (geom->dataset);
92 }
93
94 #if ! PANGO_VERSION_CHECK (1, 22, 0)
95 int pango_layout_get_baseline (PangoLayout    *layout);
96
97 /* Shamelessly copied from the pango source */
98 int
99 pango_layout_get_baseline (PangoLayout    *layout)
100 {
101   int baseline;
102
103   /* XXX this is so inefficient */
104   PangoLayoutIter *iter = pango_layout_get_iter (layout);
105   baseline = pango_layout_iter_get_baseline (iter);
106   pango_layout_iter_free (iter);
107
108   return baseline;
109 }
110 #endif
111
112 /*
113     These colours come from:
114     http://tango.freedesktop.org/static/cvs/tango-art-tools/palettes/Tango-Palette.gpl */
115 const struct xrchart_colour data_colour[XRCHART_N_COLOURS] =
116   {
117     {252, 233,  79},    /* Butter 1 */
118     {138, 226,  52},    /* Chameleon 1 */
119     {252, 175,  62},    /* Orange 1 */
120     {114, 159, 207},    /* Sky Blue 1 */
121     {173, 127, 168},    /* Plum 1 */
122     {233, 185, 110},    /* Chocolate 1 */
123     {239,  41,  41},    /* Scarlet Red 1 */
124     {238, 238, 236},    /* Aluminium 1 */
125
126     {237, 212,   0},    /* Butter 2 */
127     {115, 210,  22},    /* Chameleon 2 */
128     {245, 121,   0},    /* Orange 2 */
129     {52,  101, 164},    /* Sky Blue 2 */
130     {117,  80, 123},    /* Plum 2 */
131     {193, 125,  17},    /* Chocolate 2 */
132     {204,   0,   0},    /* Scarlet Red 2 */
133
134     {136, 138, 133},    /* Aluminium 4 */
135
136     {196, 160,   0},    /* Butter 3 */
137     {78,  154,   6},    /* Chameleon 3 */
138     {206,  92,   0},    /* Orange 3 */
139     {32,   74, 135},    /* Sky Blue 3 */
140     {92,   53, 102},    /* Plum 3 */
141     {143,  89,   2},    /* Chocolate 3 */
142     {164,   0,   0},    /* Scarlet Red 3 */
143     {85,   87,  83},    /* Aluminium 5 */
144
145     {211, 215, 207},    /* Aluminium 2 */
146     {186, 189, 182},    /* Aluminium 3 */
147     {46,   52,  54},    /* Aluminium 6 */
148   };
149
150 void
151 xrchart_draw_marker (cairo_t *cr, double x, double y,
152                      enum xrmarker_type marker, double size)
153 {
154   cairo_save (cr);
155   cairo_translate (cr, x, y);
156   cairo_scale (cr, size / 2.0, size / 2.0);
157   cairo_set_line_width (cr, cairo_get_line_width (cr) / (size / 2.0));
158   switch (marker)
159     {
160     case XRMARKER_CIRCLE:
161       cairo_arc (cr, 0, 0, 1.0, 0, 2 * M_PI);
162       cairo_stroke (cr);
163       break;
164
165     case XRMARKER_ASTERISK:
166       cairo_move_to (cr, 0, -1.0); /* | */
167       cairo_line_to (cr, 0, 1.0);
168       cairo_move_to (cr, -M_SQRT1_2, -M_SQRT1_2); /* / */
169       cairo_line_to (cr, M_SQRT1_2, M_SQRT1_2);
170       cairo_move_to (cr, -M_SQRT1_2, M_SQRT1_2); /* \ */
171       cairo_line_to (cr, M_SQRT1_2, -M_SQRT1_2);
172       cairo_stroke (cr);
173       break;
174
175     case XRMARKER_SQUARE:
176       cairo_rectangle (cr, -1.0, -1.0, 2.0, 2.0);
177       cairo_stroke (cr);
178       break;
179     }
180   cairo_restore (cr);
181 }
182
183 void
184 xrchart_label_rotate (cairo_t *cr, int horz_justify, int vert_justify,
185                       double font_size, const char *string, double angle)
186 {
187   PangoFontDescription *desc;
188   PangoLayout *layout;
189   double x, y;
190
191   desc = pango_font_description_from_string ("Sans");
192   if (desc == NULL)
193     {
194       cairo_new_path (cr);
195       return;
196     }
197   pango_font_description_set_absolute_size (desc, font_size * PANGO_SCALE);
198
199   cairo_save (cr);
200   cairo_rotate (cr, angle);
201   cairo_get_current_point (cr, &x, &y);
202   cairo_translate (cr, x, y);
203   cairo_move_to (cr, 0, 0);
204   cairo_scale (cr, 1.0, -1.0);
205
206   layout = pango_cairo_create_layout (cr);
207   pango_layout_set_font_description (layout, desc);
208   pango_layout_set_markup (layout, string, -1);
209   if (horz_justify != 'l')
210     {
211       int width_pango;
212       double width;
213
214       pango_layout_get_size (layout, &width_pango, NULL);
215       width = (double) width_pango / PANGO_SCALE;
216       if (horz_justify == 'r')
217         cairo_rel_move_to (cr, -width, 0);
218       else
219         cairo_rel_move_to (cr, -width / 2.0, 0);
220     }
221   if (vert_justify == 'x')
222     {
223       int baseline_pango = pango_layout_get_baseline (layout);
224       double baseline = (double) baseline_pango / PANGO_SCALE;
225       cairo_rel_move_to (cr, 0, -baseline);
226     }
227   else if (vert_justify != 't')
228     {
229       int height_pango;
230       double height;
231
232       pango_layout_get_size (layout, NULL, &height_pango);
233       height = (double) height_pango / PANGO_SCALE;
234       if (vert_justify == 'b')
235         cairo_rel_move_to (cr, 0, -height);
236       else if (vert_justify == 'c')
237         cairo_rel_move_to (cr, 0, -height / 2.0);
238     }
239   pango_cairo_show_layout (cr, layout);
240   g_object_unref (layout);
241
242   cairo_restore (cr);
243
244   cairo_new_path (cr);
245
246   pango_font_description_free (desc);
247 }
248
249 void
250 xrchart_label (cairo_t *cr, int horz_justify, int vert_justify,
251                double font_size, const char *string)
252 {
253   xrchart_label_rotate (cr, horz_justify, vert_justify, font_size, string, 0);
254 }
255
256
257 /* Draw a tick mark at position
258    If label is non null, then print it at the tick mark
259 */
260 static void
261 draw_tick_internal (cairo_t *cr, const struct xrchart_geometry *geom,
262                     enum tick_orientation orientation,
263                     bool rotated,
264                     double position,
265                     const char *s);
266
267 void
268 draw_tick (cairo_t *cr, const struct xrchart_geometry *geom,
269            enum tick_orientation orientation,
270            bool rotated,
271            double position,
272            const char *label, ...)
273 {
274   va_list ap;
275   char *s;
276   va_start (ap, label);
277   s = xvasprintf (label, ap);
278
279   if (fabs (position) < DBL_EPSILON)
280     position = 0;
281
282   draw_tick_internal (cr, geom, orientation, rotated, position, s);
283   free (s);
284   va_end (ap);
285 }
286
287
288 static void
289 draw_tick_internal (cairo_t *cr, const struct xrchart_geometry *geom,
290                     enum tick_orientation orientation,
291                     bool rotated,
292                     double position,
293                     const char *s)
294 {
295   const int tickSize = 10;
296   double x, y;
297
298   cairo_move_to (cr, geom->axis[SCALE_ABSCISSA].data_min, geom->axis[SCALE_ORDINATE].data_min);
299
300   if (orientation == SCALE_ABSCISSA)
301     {
302       cairo_rel_move_to (cr, position, 0);
303       cairo_rel_line_to (cr, 0, -tickSize);
304     }
305   else if (orientation == SCALE_ORDINATE)
306     {
307       cairo_rel_move_to (cr, 0, position);
308       cairo_rel_line_to (cr, -tickSize, 0);
309     }
310   else
311     NOT_REACHED ();
312   cairo_get_current_point (cr, &x, &y);
313
314   cairo_stroke (cr);
315
316   if (s != NULL)
317     {
318       cairo_move_to (cr, x, y);
319
320       if (orientation == SCALE_ABSCISSA)
321         {
322           if (rotated)
323             xrchart_label_rotate (cr, 'l', 'c', geom->font_size, s, -G_PI_4);
324           else
325             xrchart_label (cr, 'c', 't', geom->font_size, s);
326         }
327       else if (orientation == SCALE_ORDINATE)
328         {
329           xrchart_label (cr, 'r', 'c', geom->font_size, s);
330         }
331     }
332 }
333
334
335 /* Write the title on a chart*/
336 void
337 xrchart_write_title (cairo_t *cr, const struct xrchart_geometry *geom,
338                    const char *title, ...)
339 {
340   va_list ap;
341   char *s;
342
343   cairo_save (cr);
344   cairo_move_to (cr, geom->axis[SCALE_ABSCISSA].data_min, geom->title_bottom);
345
346   va_start(ap, title);
347   s = xvasprintf (title, ap);
348   xrchart_label (cr, 'l', 'x', geom->font_size * 1.5, s);
349   free (s);
350   va_end (ap);
351
352   cairo_restore (cr);
353 }
354
355 static void
356 xrchart_text_extents (cairo_t *cr, const struct xrchart_geometry *geom,
357                       const char *utf8,
358                       double *width, double *height)
359 {
360   PangoFontDescription *desc;
361   PangoLayout *layout;
362   int width_pango;
363   int height_pango;
364
365   desc = pango_font_description_from_string ("Sans");
366   if (desc == NULL)
367       return;
368   pango_font_description_set_absolute_size (desc, geom->font_size * PANGO_SCALE);
369   layout = pango_cairo_create_layout (cr);
370   pango_layout_set_font_description (layout, desc);
371   pango_layout_set_markup (layout, utf8, -1);
372   pango_layout_get_size (layout, &width_pango, &height_pango);
373   *width = (double) width_pango / PANGO_SCALE;
374   *height = (double) height_pango / PANGO_SCALE;
375   g_object_unref (layout);
376   pango_font_description_free (desc);
377 }
378
379 static bool
380 xrchart_write_scale (cairo_t *cr, struct xrchart_geometry *geom,
381                      double smin, double smax, enum tick_orientation orient)
382 {
383   int s;
384   int ticks;
385
386   double interval;
387   double lower;
388   double upper;
389   double tickscale;
390   char *tick_format_string;
391   bool tickoversize = false;
392
393   if (smax == smin)
394     return false;
395
396   chart_get_scale (smax, smin, &lower, &interval, &ticks);
397
398   tick_format_string = chart_get_ticks_format (lower, interval, ticks, &tickscale);
399
400   upper = lower + interval * (ticks+1);
401
402   geom->axis[orient].max = upper;
403   geom->axis[orient].min = lower;
404
405   struct xrchart_axis *axis = &geom->axis[orient];
406   geom->axis[orient].scale = (fabs ((double) axis->data_max - axis->data_min)
407                               / fabs (axis->max - axis->min));
408
409   if (orient == SCALE_ABSCISSA)
410     {
411       char *test_text;
412       double lower_txt_width, upper_txt_width, unused, width;
413       test_text = xasprintf(tick_format_string, upper*tickscale);
414       xrchart_text_extents (cr, geom, test_text, &upper_txt_width, &unused);
415       free(test_text);
416       test_text = xasprintf(tick_format_string, lower*tickscale);
417       xrchart_text_extents (cr, geom, test_text, &lower_txt_width, &unused);
418       free(test_text);
419       width = MAX(lower_txt_width, upper_txt_width);
420       tickoversize = width > 0.9 *
421         ((double)(geom->axis[SCALE_ABSCISSA].data_max - geom->axis[SCALE_ABSCISSA].data_min))/(ticks+1);
422     }
423
424   double pos = lower;
425
426   for (s = 0 ; s <= ticks; ++s)
427     {
428       draw_tick (cr, geom, orient, tickoversize,
429                  s * interval * geom->axis[orient].scale,
430                  tick_format_string, pos*tickscale);
431       pos += interval;
432     }
433   free(tick_format_string);
434
435   return true;
436 }
437
438 /* Set the scale for the ordinate */
439 bool
440 xrchart_write_yscale (cairo_t *cr, struct xrchart_geometry *geom,
441                     double smin, double smax)
442 {
443   return xrchart_write_scale (cr, geom, smin, smax, SCALE_ORDINATE);
444 }
445
446 /* Set the scale for the abscissa */
447 bool
448 xrchart_write_xscale (cairo_t *cr, struct xrchart_geometry *geom,
449                       double smin, double smax)
450 {
451   return xrchart_write_scale (cr, geom, smin, smax, SCALE_ABSCISSA);
452 }
453
454
455 /* Write the abscissa label */
456 void
457 xrchart_write_xlabel (cairo_t *cr, const struct xrchart_geometry *geom,
458                     const char *label)
459 {
460   cairo_move_to (cr, geom->axis[SCALE_ABSCISSA].data_min, geom->abscissa_bottom);
461   xrchart_label (cr, 'l', 't', geom->font_size, label);
462 }
463
464 /* Write the ordinate label */
465 void
466 xrchart_write_ylabel (cairo_t *cr, const struct xrchart_geometry *geom,
467                     const char *label)
468 {
469   cairo_save (cr);
470   cairo_translate (cr, geom->ordinate_left,   geom->axis[SCALE_ORDINATE].data_min);
471   cairo_rotate (cr, M_PI / 2.0);
472
473   xrchart_label (cr, 'l', 'x', geom->font_size, label);
474   cairo_restore (cr);
475 }
476
477
478 void
479 xrchart_write_legend (cairo_t *cr, const struct xrchart_geometry *geom)
480 {
481   int i;
482   const int vstep = geom->font_size * 2;
483   const int xpad = 10;
484   const int ypad = 10;
485   const int swatch = 20;
486   const int legend_top = geom->axis[SCALE_ORDINATE].data_max;
487   const int legend_bottom = legend_top -
488     (vstep * geom->n_datasets + 2 * ypad);
489
490   cairo_save (cr);
491
492   cairo_rectangle (cr, geom->legend_left, legend_top,
493                    geom->legend_right - xpad - geom->legend_left,
494                    legend_bottom - legend_top);
495   cairo_stroke (cr);
496
497   for (i = 0 ; i < geom->n_datasets ; ++i)
498     {
499       const int ypos = legend_top - vstep * (i + 1);
500       const int xpos = geom->legend_left + xpad;
501       const struct xrchart_colour *colour;
502
503       cairo_move_to (cr, xpos, ypos);
504
505       cairo_save (cr);
506       colour = &data_colour [ i % XRCHART_N_COLOURS];
507       cairo_set_source_rgb (cr,
508                             colour->red / 255.0,
509                             colour->green / 255.0,
510                             colour->blue / 255.0);
511       cairo_rectangle (cr, xpos, ypos, swatch, swatch);
512       cairo_fill_preserve (cr);
513       cairo_stroke (cr);
514       cairo_restore (cr);
515
516       cairo_move_to (cr, xpos + swatch * 1.5, ypos);
517       xrchart_label (cr, 'l', 'x', geom->font_size, geom->dataset[i]);
518     }
519
520   cairo_restore (cr);
521 }
522
523 /* Start a new vector called NAME */
524 void
525 xrchart_vector_start (cairo_t *cr, struct xrchart_geometry *geom, const char *name)
526 {
527   const struct xrchart_colour *colour;
528
529   cairo_save (cr);
530
531   colour = &data_colour[geom->n_datasets % XRCHART_N_COLOURS];
532   cairo_set_source_rgb (cr,
533                         colour->red / 255.0,
534                         colour->green / 255.0,
535                         colour->blue / 255.0);
536
537   geom->n_datasets++;
538   geom->dataset = xrealloc (geom->dataset,
539                             geom->n_datasets * sizeof (*geom->dataset));
540
541   geom->dataset[geom->n_datasets - 1] = strdup (name);
542 }
543
544 /* Plot a data point */
545 void
546 xrchart_datum (cairo_t *cr, const struct xrchart_geometry *geom,
547              int dataset UNUSED, double x, double y)
548 {
549   double x_pos = (x - geom->axis[SCALE_ABSCISSA].min) * geom->axis[SCALE_ABSCISSA].scale + geom->axis[SCALE_ABSCISSA].data_min;
550   double y_pos = (y - geom->axis[SCALE_ORDINATE].min) * geom->axis[SCALE_ORDINATE].scale + geom->axis[SCALE_ORDINATE].data_min;
551
552   xrchart_draw_marker (cr, x_pos, y_pos, XRMARKER_CIRCLE, 10);
553 }
554
555 void
556 xrchart_vector_end (cairo_t *cr, struct xrchart_geometry *geom)
557 {
558   cairo_stroke (cr);
559   cairo_restore (cr);
560   geom->in_path = false;
561 }
562
563 /* Plot a data point */
564 void
565 xrchart_vector (cairo_t *cr, struct xrchart_geometry *geom, double x, double y)
566 {
567   const double x_pos =
568     (x - geom->axis[SCALE_ABSCISSA].min) * geom->axis[SCALE_ABSCISSA].scale + geom->axis[SCALE_ABSCISSA].data_min ;
569
570   const double y_pos =
571     (y - geom->axis[SCALE_ORDINATE].min) * geom->axis[SCALE_ORDINATE].scale + geom->axis[SCALE_ORDINATE].data_min ;
572
573   if (geom->in_path)
574     cairo_line_to (cr, x_pos, y_pos);
575   else
576     {
577       cairo_move_to (cr, x_pos, y_pos);
578       geom->in_path = true;
579     }
580 }
581
582
583
584 /* Draw a line with slope SLOPE and intercept INTERCEPT.
585    between the points limit1 and limit2.
586    If lim_dim is XRCHART_DIM_Y then the limit{1,2} are on the
587    y axis otherwise the x axis
588 */
589 void
590 xrchart_line(cairo_t *cr, const struct xrchart_geometry *geom,
591            double slope, double intercept,
592            double limit1, double limit2, enum xrchart_dim lim_dim)
593 {
594   double x1, y1;
595   double x2, y2;
596
597   if (lim_dim == XRCHART_DIM_Y)
598     {
599       x1 = (limit1 - intercept) / slope;
600       x2 = (limit2 - intercept) / slope;
601       y1 = limit1;
602       y2 = limit2;
603     }
604   else
605     {
606       x1 = limit1;
607       x2 = limit2;
608       y1 = slope * x1 + intercept;
609       y2 = slope * x2 + intercept;
610     }
611
612   y1 = (y1 - geom->axis[SCALE_ORDINATE].min) * geom->axis[SCALE_ORDINATE].scale + geom->axis[SCALE_ORDINATE].data_min;
613   y2 = (y2 - geom->axis[SCALE_ORDINATE].min) * geom->axis[SCALE_ORDINATE].scale + geom->axis[SCALE_ORDINATE].data_min;
614   x1 = (x1 - geom->axis[SCALE_ABSCISSA].min) * geom->axis[SCALE_ABSCISSA].scale + geom->axis[SCALE_ABSCISSA].data_min;
615   x2 = (x2 - geom->axis[SCALE_ABSCISSA].min) * geom->axis[SCALE_ABSCISSA].scale + geom->axis[SCALE_ABSCISSA].data_min;
616
617   cairo_move_to (cr, x1, y1);
618   cairo_line_to (cr, x2, y2);
619   cairo_stroke (cr);
620 }
621 \f
622 void
623 xr_draw_chart (const struct chart *chart, cairo_t *cr,
624                double width, double height)
625 {
626   struct xrchart_geometry geom;
627
628   cairo_save (cr);
629   cairo_translate (cr, 0, height);
630   cairo_scale (cr, 1.0, -1.0);
631   xrchart_geometry_init (cr, &geom, width, height);
632   if (is_boxplot (chart))
633     xrchart_draw_boxplot (chart, cr, &geom);
634   else if (is_histogram_chart (chart))
635     xrchart_draw_histogram (chart, cr, &geom);
636   else if (is_np_plot_chart (chart))
637     xrchart_draw_np_plot (chart, cr, &geom);
638   else if (is_piechart (chart))
639     xrchart_draw_piechart (chart, cr, &geom);
640   else if (is_barchart (chart))
641     xrchart_draw_barchart (chart, cr, &geom);
642   else if (is_roc_chart (chart))
643     xrchart_draw_roc (chart, cr, &geom);
644   else if (is_scree (chart))
645     xrchart_draw_scree (chart, cr, &geom);
646   else if (is_spreadlevel_plot_chart (chart))
647     xrchart_draw_spreadlevel (chart, cr, &geom);
648   else if (is_scatterplot_chart (chart))
649     xrchart_draw_scatterplot (chart, cr, &geom);
650   else
651     NOT_REACHED ();
652   xrchart_geometry_free (cr, &geom);
653
654   cairo_restore (cr);
655 }
656
657 cairo_surface_t *
658 xr_draw_image_chart (const struct chart *chart,
659                      const struct cell_color *fg,
660                      const struct cell_color *bg)
661 {
662   const int width = 640;
663   const int length = 480;
664
665   cairo_surface_t *surface = cairo_image_surface_create (
666     CAIRO_FORMAT_RGB24, width, length);
667   cairo_t *cr = cairo_create (surface);
668
669   cairo_set_source_rgb (cr, bg->r / 255.0, bg->g / 255.0, bg->b / 255.0);
670   cairo_paint (cr);
671
672   cairo_set_source_rgb (cr, fg->r / 255.0, fg->g / 255.0, fg->b / 255.0);
673   xr_draw_chart (chart, cr, width, length);
674
675   cairo_destroy (cr);
676
677   return surface;
678 }
679
680 char *
681 xr_write_png_image (cairo_surface_t *surface,
682                     const char *file_name_template, int number)
683 {
684   const char *number_pos = strchr (file_name_template, '#');
685   char *file_name;
686   if (number_pos != NULL)
687     file_name = xasprintf ("%.*s%d%s.png",
688                            (int) (number_pos - file_name_template),
689                            file_name_template, number, number_pos + 1);
690   else
691     file_name = xasprintf ("%s.png", file_name_template);
692
693   cairo_status_t status = cairo_surface_write_to_png (surface, file_name);
694   if (status != CAIRO_STATUS_SUCCESS)
695     msg (ME, _("error writing output file `%s': %s"),
696            file_name, cairo_status_to_string (status));
697
698   return file_name;
699 }
700
701 char *
702 xr_draw_png_chart (const struct chart *chart,
703                    const char *file_name_template, int number,
704                    const struct cell_color *fg,
705                    const struct cell_color *bg)
706 {
707   cairo_surface_t *surface = xr_draw_image_chart (chart, fg, bg);
708   char *file_name = xr_write_png_image (surface, file_name_template, number);
709   cairo_surface_destroy (surface);
710   return file_name;
711 }
712
713 char *
714 xr_draw_eps_chart (const struct chart *chart,
715                    const char *file_name_template, int number,
716                    const struct cell_color *fg,
717                    const struct cell_color *bg)
718 {
719   const int width = 640;
720   const int length = 480;
721
722   cairo_surface_t *surface;
723   const char *number_pos;
724   char *file_name;
725   cairo_t *cr;
726
727   number_pos = strchr (file_name_template, '#');
728   if (number_pos != NULL)
729     file_name = xasprintf ("%.*s%d%s.eps", (int) (number_pos - file_name_template),
730                            file_name_template, number, number_pos + 1);
731   else
732     file_name = xasprintf ("%s.eps", file_name_template);
733
734   surface = cairo_ps_surface_create (file_name, width, length);
735   cairo_ps_surface_set_eps (surface, true);
736   cr = cairo_create (surface);
737
738   cairo_set_source_rgb (cr, bg->r / 255.0, bg->g / 255.0, bg->b / 255.0);
739   cairo_paint (cr);
740
741   cairo_set_source_rgb (cr, fg->r / 255.0, fg->g / 255.0, fg->b / 255.0);
742
743   xr_draw_chart (chart, cr, width, length);
744
745   cairo_destroy (cr);
746   cairo_surface_destroy (surface);
747
748   return file_name;
749 }
750