output: Use Cairo and Pango to draw charts, instead of libplot.
[pspp-builds.git] / src / output / charts / plot-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/charts/plot-chart.h>
20
21 #include <assert.h>
22 #include <float.h>
23 #include <math.h>
24 #include <pango/pango-font.h>
25 #include <pango/pango-layout.h>
26 #include <pango/pango.h>
27 #include <pango/pangocairo.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <stdio.h>
31 #include <string.h>
32
33 #include <libpspp/assertion.h>
34 #include <libpspp/str.h>
35 #include <math/chart-geometry.h>
36 #include <output/chart-provider.h>
37 #include <output/manager.h>
38 #include <output/output.h>
39
40 #include "xalloc.h"
41
42 const struct chart_colour data_colour[N_CHART_COLOURS] =
43   {
44     { 165, 42, 42 },            /* brown */
45     { 255, 0, 0 },              /* red */
46     { 255, 165, 0 },            /* orange */
47     { 255, 255, 0 },            /* yellow */
48     { 0, 255, 0 },              /* green */
49     { 0, 0, 255 },              /* blue */
50     { 238, 130, 238 },          /* violet */
51     { 190, 190, 190 },          /* grey */
52     { 255, 192, 203 },          /* pink */
53   };
54
55 void
56 chart_draw_marker (cairo_t *cr, double x, double y, enum marker_type marker,
57                    double size)
58 {
59   cairo_save (cr);
60   cairo_translate (cr, x, y);
61   cairo_scale (cr, size / 2.0, size / 2.0);
62   cairo_set_line_width (cr, cairo_get_line_width (cr) / (size / 2.0));
63   switch (marker)
64     {
65     case MARKER_CIRCLE:
66       cairo_arc (cr, 0, 0, 1.0, 0, 2 * M_PI);
67       cairo_stroke (cr);
68       break;
69
70     case MARKER_ASTERISK:
71       cairo_move_to (cr, 0, -1.0); /* | */
72       cairo_line_to (cr, 0, 1.0);
73       cairo_move_to (cr, -M_SQRT1_2, -M_SQRT1_2); /* / */
74       cairo_line_to (cr, M_SQRT1_2, M_SQRT1_2);
75       cairo_move_to (cr, -M_SQRT1_2, M_SQRT1_2); /* \ */
76       cairo_line_to (cr, M_SQRT1_2, -M_SQRT1_2);
77       cairo_stroke (cr);
78       break;
79
80     case MARKER_SQUARE:
81       cairo_rectangle (cr, -1.0, -1.0, 2.0, 2.0);
82       cairo_stroke (cr);
83       break;
84     }
85   cairo_restore (cr);
86 }
87
88 void
89 chart_label (cairo_t *cr, int horz_justify, int vert_justify,
90              const char *string)
91 {
92   PangoFontDescription *desc;
93   PangoLayout *layout;
94   double x, y;
95
96   desc = pango_font_description_from_string ("sans serif");
97   if (desc == NULL)
98     {
99       cairo_new_path (cr);
100       return;
101     }
102   pango_font_description_set_absolute_size (desc, 15 * PANGO_SCALE);
103
104   cairo_save (cr);
105   cairo_get_current_point (cr, &x, &y);
106   cairo_translate (cr, x, y);
107   cairo_move_to (cr, 0, 0);
108   cairo_scale (cr, 1.0, -1.0);
109
110   layout = pango_cairo_create_layout (cr);
111   pango_layout_set_font_description (layout, desc);
112   pango_layout_set_text (layout, string, -1);
113   if (horz_justify != 'l')
114     {
115       int width_pango;
116       double width;
117
118       pango_layout_get_size (layout, &width_pango, NULL);
119       width = (double) width_pango / PANGO_SCALE;
120       if (horz_justify == 'r')
121         cairo_rel_move_to (cr, -width, 0);
122       else
123         cairo_rel_move_to (cr, -width / 2.0, 0);
124     }
125   if (vert_justify != 't')
126     {
127       int height_pango;
128       double height;
129
130       pango_layout_get_size (layout, NULL, &height_pango);
131       height = (double) height_pango / PANGO_SCALE;
132       if (vert_justify == 'b' || vert_justify == 'x')
133         cairo_rel_move_to (cr, 0, -height);
134       else if (vert_justify == 'c')
135         cairo_rel_move_to (cr, 0, -height / 2.0);
136     }
137   pango_cairo_show_layout (cr, layout);
138   g_object_unref (layout);
139
140   cairo_restore (cr);
141
142   cairo_new_path (cr);
143
144   pango_font_description_free (desc);
145 }
146
147 /* Draw a tick mark at position
148    If label is non zero, then print it at the tick mark
149 */
150 void
151 draw_tick (cairo_t *cr, const struct chart_geometry *geom,
152            enum tick_orientation orientation,
153            double position,
154            const char *label, ...)
155 {
156   const int tickSize = 10;
157   double x, y;
158
159   cairo_move_to (cr, geom->data_left, geom->data_bottom);
160
161   if (orientation == TICK_ABSCISSA)
162     {
163       cairo_rel_move_to (cr, position, 0);
164       cairo_rel_line_to (cr, 0, -tickSize);
165     }
166   else if (orientation == TICK_ORDINATE)
167     {
168       cairo_rel_move_to (cr, 0, position);
169       cairo_rel_line_to (cr, -tickSize, 0);
170     }
171   else
172     NOT_REACHED ();
173   cairo_get_current_point (cr, &x, &y);
174
175   cairo_stroke (cr);
176
177   if (label != NULL)
178     {
179       va_list ap;
180       char *s;
181
182       cairo_move_to (cr, x, y);
183
184       va_start (ap, label);
185       s = xvasprintf (label, ap);
186       if (orientation == TICK_ABSCISSA)
187         chart_label (cr, 'c', 't', s);
188       else if (orientation == TICK_ORDINATE)
189         {
190           if (fabs (position) < DBL_EPSILON)
191             cairo_rel_move_to (cr, 0, 10);
192           chart_label (cr, 'r', 'c', s);
193         }
194       free (s);
195       va_end (ap);
196     }
197 }
198
199
200 /* Write the title on a chart*/
201 void
202 chart_write_title (cairo_t *cr, const struct chart_geometry *geom,
203                    const char *title, ...)
204 {
205   va_list ap;
206   char *s;
207
208   cairo_save (cr);
209   // pl_ffontsize_r (cr, geom->font_size * 1.5); /* XXX */
210   cairo_move_to (cr, geom->data_left, geom->title_bottom);
211
212   va_start(ap, title);
213   s = xvasprintf (title, ap);
214   chart_label (cr, 'l', 'x', s);
215   free (s);
216   va_end (ap);
217
218   cairo_restore (cr);
219 }
220
221
222 /* Set the scale for the abscissa */
223 void
224 chart_write_xscale (cairo_t *cr, struct chart_geometry *geom,
225                     double min, double max, int ticks)
226 {
227   double x;
228
229   const double tick_interval =
230     chart_rounded_tick ((max - min) / (double) ticks);
231
232   geom->x_max = ceil (max / tick_interval) * tick_interval;
233   geom->x_min = floor (min / tick_interval) * tick_interval;
234   geom->abscissa_scale = fabs(geom->data_right - geom->data_left) /
235     fabs(geom->x_max - geom->x_min);
236
237   for (x = geom->x_min; x <= geom->x_max; x += tick_interval)
238     draw_tick (cr, geom, TICK_ABSCISSA,
239                (x - geom->x_min) * geom->abscissa_scale, "%g", x);
240 }
241
242
243 /* Set the scale for the ordinate */
244 void
245 chart_write_yscale (cairo_t *cr, struct chart_geometry *geom,
246                     double smin, double smax, int ticks)
247 {
248   double y;
249
250   const double tick_interval =
251     chart_rounded_tick ((smax - smin) / (double) ticks);
252
253   geom->y_max = ceil (smax / tick_interval) * tick_interval;
254   geom->y_min = floor (smin / tick_interval) * tick_interval;
255
256   geom->ordinate_scale =
257     (fabs (geom->data_top - geom->data_bottom)
258      / fabs (geom->y_max - geom->y_min));
259
260   for (y = geom->y_min; y <= geom->y_max; y += tick_interval)
261     draw_tick (cr, geom, TICK_ORDINATE,
262                (y - geom->y_min) * geom->ordinate_scale, "%g", y);
263 }
264
265 /* Write the abscissa label */
266 void
267 chart_write_xlabel (cairo_t *cr, const struct chart_geometry *geom,
268                     const char *label)
269 {
270   cairo_move_to (cr, geom->data_left, geom->abscissa_top);
271   chart_label (cr, 'l', 't', label);
272 }
273
274 /* Write the ordinate label */
275 void
276 chart_write_ylabel (cairo_t *cr, const struct chart_geometry *geom,
277                     const char *label)
278 {
279   cairo_save (cr);
280   cairo_translate (cr, -geom->data_bottom, -geom->ordinate_right);
281   cairo_move_to (cr, 0, 0);
282   cairo_rotate (cr, M_PI / 2.0);
283   chart_label (cr, 'l', 'x', label);
284   cairo_restore (cr);
285 }