output: Use Cairo and Pango to draw charts, instead of libplot.
[pspp-builds.git] / src / output / chart.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2004, 2009 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/chart.h>
20 #include <output/chart-provider.h>
21
22 #include <assert.h>
23 #include <cairo/cairo.h>
24 #include <errno.h>
25 #include <float.h>
26 #include <math.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include <libpspp/str.h>
33 #include <output/manager.h>
34 #include <output/output.h>
35
36 #include "error.h"
37 #include "xalloc.h"
38
39 #include "gettext.h"
40 #define _(msgid) gettext (msgid)
41
42 extern struct som_table_class tab_table_class;
43
44 void
45 chart_init (struct chart *chart, const struct chart_class *class)
46 {
47   chart->class = class;
48   chart->ref_cnt = 1;
49 }
50
51 void
52 chart_geometry_init (cairo_t *cr, struct chart_geometry *geom,
53                      double width, double length)
54 {
55   /* Set default chartetry. */
56   geom->data_top = 0.900 * length;
57   geom->data_right = 0.800 * width;
58   geom->data_bottom = 0.120 * length;
59   geom->data_left = 0.150 * width;
60   geom->abscissa_top = 0.070 * length;
61   geom->ordinate_right = 0.120 * width;
62   geom->title_bottom = 0.920 * length;
63   geom->legend_left = 0.810 * width;
64   geom->legend_right = width;
65   geom->font_size = 10;
66
67   geom->fill_colour.red = 255;
68   geom->fill_colour.green = 0;
69   geom->fill_colour.blue = 0;
70
71   cairo_set_line_width (cr, 1.0);
72
73   cairo_rectangle (cr, geom->data_left, geom->data_bottom,
74                    geom->data_right - geom->data_left,
75                    geom->data_top - geom->data_bottom);
76   cairo_stroke (cr);
77 }
78
79 void
80 chart_geometry_free (cairo_t *cr UNUSED)
81 {
82 }
83
84 void
85 chart_draw (const struct chart *chart, cairo_t *cr,
86             struct chart_geometry *geom)
87 {
88   chart->class->draw (chart, cr, geom);
89 }
90
91 char *
92 chart_draw_png (const struct chart *chart, const char *file_name_template,
93                 int number)
94 {
95   const int width = 640;
96   const int length = 480;
97
98   struct chart_geometry geom;
99   cairo_surface_t *surface;
100   cairo_status_t status;
101   const char *number_pos;
102   char *file_name;
103   cairo_t *cr;
104
105   number_pos = strchr (file_name_template, '#');
106   if (number_pos != NULL)
107     file_name = xasprintf ("%.*s%d%s", (int) (number_pos - file_name_template),
108                            file_name_template, number, number_pos + 1);
109   else
110     file_name = xstrdup (file_name_template);
111
112   surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, length);
113   cr = cairo_create (surface);
114
115   cairo_translate (cr, 0.0, length);
116   cairo_scale (cr, 1.0, -1.0);
117
118   cairo_save (cr);
119   cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
120   cairo_rectangle (cr, 0, 0, width, length);
121   cairo_fill (cr);
122   cairo_restore (cr);
123
124   cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
125
126   chart_geometry_init (cr, &geom, width, length);
127   chart_draw (chart, cr, &geom);
128   chart_geometry_free (cr);
129
130   status = cairo_surface_write_to_png (surface, file_name);
131   if (status != CAIRO_STATUS_SUCCESS)
132     error (0, 0, _("writing output file \"%s\": %s"),
133            file_name, cairo_status_to_string (status));
134
135   cairo_destroy (cr);
136   cairo_surface_destroy (surface);
137
138   return file_name;
139 }
140
141
142 struct chart *
143 chart_ref (const struct chart *chart_)
144 {
145   struct chart *chart = (struct chart *) chart_;
146   chart->ref_cnt++;
147   return chart;
148 }
149
150 void
151 chart_unref (struct chart *chart)
152 {
153   if (chart != NULL)
154     {
155       assert (chart->ref_cnt > 0);
156       if (--chart->ref_cnt == 0)
157         chart->class->destroy (chart);
158     }
159 }
160
161 void
162 chart_submit (struct chart *chart)
163 {
164 #ifdef HAVE_CAIRO
165   struct outp_driver *d;
166
167   for (d = outp_drivers (NULL); d; d = outp_drivers (d))
168     if (d->class->output_chart != NULL)
169       d->class->output_chart (d, chart);
170 #endif
171
172   chart_unref (chart);
173 }