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