Use the msg function to report errors wherever possible.
[pspp] / src / output / cairo-chart.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2004, 2009, 2010, 2011 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-chart.h"
20
21 #include <assert.h>
22 #include <cairo/cairo.h>
23 #include <pango/pango.h>
24 #include <pango/pangocairo.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/assertion.h"
33 #include "math/chart-geometry.h"
34 #include "output/cairo.h"
35 #include "output/chart-item.h"
36
37 #include "gl/xalloc.h"
38 #include "gl/xvasprintf.h"
39
40 #include "gettext.h"
41 #define _(msgid) gettext (msgid)
42
43 void
44 xrchart_geometry_init (cairo_t *cr, struct xrchart_geometry *geom,
45                        double width, double length)
46 {
47   /* Set default chart geometry. */
48   geom->axis[SCALE_ORDINATE].data_max = 0.900 * length;
49   geom->axis[SCALE_ORDINATE].data_min = 0.120 * length;
50
51   geom->axis[SCALE_ABSCISSA].data_min = 0.150 * width;
52   geom->axis[SCALE_ABSCISSA].data_max = 0.800 * width;
53   geom->abscissa_bottom = 0.070 * length;
54   geom->ordinate_left = 0.050 * width;
55   geom->title_bottom = 0.920 * length;
56   geom->legend_left = 0.810 * width;
57   geom->legend_right = width;
58   geom->font_size = 15.0;
59   geom->in_path = false;
60   geom->dataset = NULL;
61   geom->n_datasets = 0;
62
63   geom->fill_colour.red = 255;
64   geom->fill_colour.green = 0;
65   geom->fill_colour.blue = 0;
66
67   cairo_set_line_width (cr, 1.0);
68
69   cairo_rectangle (cr, geom->axis[SCALE_ABSCISSA].data_min, geom->axis[SCALE_ORDINATE].data_min,
70                    geom->axis[SCALE_ABSCISSA].data_max - geom->axis[SCALE_ABSCISSA].data_min,
71                    geom->axis[SCALE_ORDINATE].data_max - geom->axis[SCALE_ORDINATE].data_min);
72   cairo_stroke (cr);
73 }
74
75 void
76 xrchart_geometry_free (cairo_t *cr UNUSED, struct xrchart_geometry *geom)
77 {
78   int i;
79
80   for (i = 0 ; i < geom->n_datasets; ++i)
81     free (geom->dataset[i]);
82   free (geom->dataset);
83 }
84
85 #if ! PANGO_VERSION_CHECK (1, 22, 0)
86 int pango_layout_get_baseline (PangoLayout    *layout);
87
88 /* Shamelessly copied from the pango source */
89 int
90 pango_layout_get_baseline (PangoLayout    *layout)
91 {
92   int baseline;
93
94   /* XXX this is so inefficient */
95   PangoLayoutIter *iter = pango_layout_get_iter (layout);
96   baseline = pango_layout_iter_get_baseline (iter);
97   pango_layout_iter_free (iter);
98
99   return baseline;
100 }
101 #endif
102
103
104
105 const struct xrchart_colour data_colour[XRCHART_N_COLOURS] =
106   {
107     { 165, 42, 42 },            /* brown */
108     { 255, 0, 0 },              /* red */
109     { 255, 165, 0 },            /* orange */
110     { 255, 255, 0 },            /* yellow */
111     { 0, 255, 0 },              /* green */
112     { 0, 0, 255 },              /* blue */
113     { 238, 130, 238 },          /* violet */
114     { 190, 190, 190 },          /* grey */
115     { 255, 192, 203 },          /* pink */
116   };
117
118 void
119 xrchart_draw_marker (cairo_t *cr, double x, double y,
120                      enum xrmarker_type marker, double size)
121 {
122   cairo_save (cr);
123   cairo_translate (cr, x, y);
124   cairo_scale (cr, size / 2.0, size / 2.0);
125   cairo_set_line_width (cr, cairo_get_line_width (cr) / (size / 2.0));
126   switch (marker)
127     {
128     case XRMARKER_CIRCLE:
129       cairo_arc (cr, 0, 0, 1.0, 0, 2 * M_PI);
130       cairo_stroke (cr);
131       break;
132
133     case XRMARKER_ASTERISK:
134       cairo_move_to (cr, 0, -1.0); /* | */
135       cairo_line_to (cr, 0, 1.0);
136       cairo_move_to (cr, -M_SQRT1_2, -M_SQRT1_2); /* / */
137       cairo_line_to (cr, M_SQRT1_2, M_SQRT1_2);
138       cairo_move_to (cr, -M_SQRT1_2, M_SQRT1_2); /* \ */
139       cairo_line_to (cr, M_SQRT1_2, -M_SQRT1_2);
140       cairo_stroke (cr);
141       break;
142
143     case XRMARKER_SQUARE:
144       cairo_rectangle (cr, -1.0, -1.0, 2.0, 2.0);
145       cairo_stroke (cr);
146       break;
147     }
148   cairo_restore (cr);
149 }
150
151 void
152 xrchart_label_rotate (cairo_t *cr, int horz_justify, int vert_justify,
153                       double font_size, const char *string, double angle)
154 {
155   PangoFontDescription *desc;
156   PangoLayout *layout;
157   double x, y;
158
159   desc = pango_font_description_from_string ("sans serif");
160   if (desc == NULL)
161     {
162       cairo_new_path (cr);
163       return;
164     }
165   pango_font_description_set_absolute_size (desc, font_size * PANGO_SCALE);
166
167   cairo_save (cr);
168   cairo_rotate (cr, angle);
169   cairo_get_current_point (cr, &x, &y);
170   cairo_translate (cr, x, y);
171   cairo_move_to (cr, 0, 0);
172   cairo_scale (cr, 1.0, -1.0);
173
174   layout = pango_cairo_create_layout (cr);
175   pango_layout_set_font_description (layout, desc);
176   pango_layout_set_text (layout, string, -1);
177   if (horz_justify != 'l')
178     {
179       int width_pango;
180       double width;
181
182       pango_layout_get_size (layout, &width_pango, NULL);
183       width = (double) width_pango / PANGO_SCALE;
184       if (horz_justify == 'r')
185         cairo_rel_move_to (cr, -width, 0);
186       else
187         cairo_rel_move_to (cr, -width / 2.0, 0);
188     }
189   if (vert_justify == 'x')
190     {
191       int baseline_pango = pango_layout_get_baseline (layout);
192       double baseline = (double) baseline_pango / PANGO_SCALE;
193       cairo_rel_move_to (cr, 0, -baseline);
194     }
195   else if (vert_justify != 't')
196     {
197       int height_pango;
198       double height;
199
200       pango_layout_get_size (layout, NULL, &height_pango);
201       height = (double) height_pango / PANGO_SCALE;
202       if (vert_justify == 'b')
203         cairo_rel_move_to (cr, 0, -height);
204       else if (vert_justify == 'c')
205         cairo_rel_move_to (cr, 0, -height / 2.0);
206     }
207   pango_cairo_show_layout (cr, layout);
208   g_object_unref (layout);
209
210   cairo_restore (cr);
211
212   cairo_new_path (cr);
213
214   pango_font_description_free (desc);
215 }
216
217 void
218 xrchart_label (cairo_t *cr, int horz_justify, int vert_justify,
219                double font_size, const char *string)
220 {
221   xrchart_label_rotate (cr, horz_justify, vert_justify, font_size, string, 0);
222 }
223
224
225 /* Draw a tick mark at position
226    If label is non null, then print it at the tick mark
227 */
228 static void
229 draw_tick_internal (cairo_t *cr, const struct xrchart_geometry *geom,
230                     enum tick_orientation orientation,
231                     bool rotated,
232                     double position,
233                     const char *s);
234
235 void
236 draw_tick (cairo_t *cr, const struct xrchart_geometry *geom,
237            enum tick_orientation orientation,
238            bool rotated,
239            double position,
240            const char *label, ...)
241 {
242   va_list ap;
243   char *s;
244   va_start (ap, label);
245   s = xvasprintf (label, ap);
246
247   if (fabs (position) < DBL_EPSILON)
248     position = 0;
249
250   draw_tick_internal (cr, geom, orientation, rotated, position, s);
251   free (s);
252   va_end (ap);
253 }
254
255
256 static void
257 draw_tick_internal (cairo_t *cr, const struct xrchart_geometry *geom,
258                     enum tick_orientation orientation,
259                     bool rotated,
260                     double position,
261                     const char *s)
262 {
263   const int tickSize = 10;
264   double x, y;
265
266   cairo_move_to (cr, geom->axis[SCALE_ABSCISSA].data_min, geom->axis[SCALE_ORDINATE].data_min);
267
268   if (orientation == SCALE_ABSCISSA)
269     {
270       cairo_rel_move_to (cr, position, 0);
271       cairo_rel_line_to (cr, 0, -tickSize);
272     }
273   else if (orientation == SCALE_ORDINATE)
274     {
275       cairo_rel_move_to (cr, 0, position);
276       cairo_rel_line_to (cr, -tickSize, 0);
277     }
278   else
279     NOT_REACHED ();
280   cairo_get_current_point (cr, &x, &y);
281
282   cairo_stroke (cr);
283
284   if (s != NULL)
285     {
286       cairo_move_to (cr, x, y);
287
288       if (orientation == SCALE_ABSCISSA)
289         {
290           if ( rotated) 
291             xrchart_label_rotate (cr, 'l', 'c', geom->font_size, s, -G_PI_4);
292           else
293             xrchart_label (cr, 'c', 't', geom->font_size, s);
294         }
295       else if (orientation == SCALE_ORDINATE)
296         {
297           if (fabs (position) < DBL_EPSILON)
298             cairo_rel_move_to (cr, 0, 10);
299           xrchart_label (cr, 'r', 'c', geom->font_size, s);
300         }
301     }
302 }
303
304
305 /* Write the title on a chart*/
306 void
307 xrchart_write_title (cairo_t *cr, const struct xrchart_geometry *geom,
308                    const char *title, ...)
309 {
310   va_list ap;
311   char *s;
312
313   cairo_save (cr);
314   cairo_move_to (cr, geom->axis[SCALE_ABSCISSA].data_min, geom->title_bottom);
315
316   va_start(ap, title);
317   s = xvasprintf (title, ap);
318   xrchart_label (cr, 'l', 'x', geom->font_size * 1.5, s);
319   free (s);
320   va_end (ap);
321
322   cairo_restore (cr);
323 }
324
325
326
327 static void
328 xrchart_write_scale (cairo_t *cr, struct xrchart_geometry *geom,
329                      double smin, double smax, int ticks, enum tick_orientation orient)
330 {
331   int s;
332
333   const double tick_interval =
334     chart_rounded_tick ((smax - smin) / (double) ticks);
335
336   int upper = ceil (smax / tick_interval);
337   int lower = floor (smin / tick_interval);
338
339   geom->axis[orient].max = tick_interval * upper;
340   geom->axis[orient].min = tick_interval * lower;
341
342   geom->axis[orient].scale = (fabs (geom->axis[orient].data_max - geom->axis[orient].data_min)
343      / fabs (geom->axis[orient].max - geom->axis[orient].min));
344
345   for (s = 0 ; s < upper - lower; ++s)
346     {
347       double pos = (s + lower) * tick_interval;
348       draw_tick (cr, geom, orient, false,
349                  s * tick_interval * geom->axis[orient].scale, "%g", pos);
350     }
351 }
352
353 /* Set the scale for the ordinate */
354 void
355 xrchart_write_yscale (cairo_t *cr, struct xrchart_geometry *geom,
356                     double smin, double smax, int ticks)
357 {
358   xrchart_write_scale (cr, geom, smin, smax, ticks, SCALE_ORDINATE);
359 }
360
361 /* Set the scale for the abscissa */
362 void
363 xrchart_write_xscale (cairo_t *cr, struct xrchart_geometry *geom,
364                     double smin, double smax, int ticks)
365 {
366   xrchart_write_scale (cr, geom, smin, smax, ticks, SCALE_ABSCISSA);
367 }
368
369
370 /* Write the abscissa label */
371 void
372 xrchart_write_xlabel (cairo_t *cr, const struct xrchart_geometry *geom,
373                     const char *label)
374 {
375   cairo_move_to (cr, geom->axis[SCALE_ABSCISSA].data_min, geom->abscissa_bottom);
376   xrchart_label (cr, 'l', 't', geom->font_size, label);
377 }
378
379 /* Write the ordinate label */
380 void
381 xrchart_write_ylabel (cairo_t *cr, const struct xrchart_geometry *geom,
382                     const char *label)
383 {
384   cairo_save (cr);
385   cairo_translate (cr, geom->ordinate_left,   geom->axis[SCALE_ORDINATE].data_min);
386   cairo_rotate (cr, M_PI / 2.0);
387
388   xrchart_label (cr, 'l', 'x', geom->font_size, label);
389   cairo_restore (cr);
390 }
391
392
393 void
394 xrchart_write_legend (cairo_t *cr, const struct xrchart_geometry *geom)
395 {
396   int i;
397   const int vstep = geom->font_size * 2;
398   const int xpad = 10;
399   const int ypad = 10;
400   const int swatch = 20;
401   const int legend_top = geom->axis[SCALE_ORDINATE].data_max;
402   const int legend_bottom = legend_top -
403     (vstep * geom->n_datasets + 2 * ypad );
404
405   cairo_save (cr);
406
407   cairo_rectangle (cr, geom->legend_left, legend_top,
408                    geom->legend_right - xpad - geom->legend_left,
409                    legend_bottom - legend_top);
410   cairo_stroke (cr);
411
412   for (i = 0 ; i < geom->n_datasets ; ++i )
413     {
414       const int ypos = legend_top - vstep * (i + 1);
415       const int xpos = geom->legend_left + xpad;
416       const struct xrchart_colour *colour;
417
418       cairo_move_to (cr, xpos, ypos);
419
420       cairo_save (cr);
421       colour = &data_colour [ i % XRCHART_N_COLOURS];
422       cairo_set_source_rgb (cr,
423                             colour->red / 255.0,
424                             colour->green / 255.0,
425                             colour->blue / 255.0);
426       cairo_rectangle (cr, xpos, ypos, swatch, swatch);
427       cairo_fill_preserve (cr);
428       cairo_stroke (cr);
429       cairo_restore (cr);
430
431       cairo_move_to (cr, xpos + swatch * 1.5, ypos);
432       xrchart_label (cr, 'l', 'x', geom->font_size, geom->dataset[i]);
433     }
434
435   cairo_restore (cr);
436 }
437
438 /* Start a new vector called NAME */
439 void
440 xrchart_vector_start (cairo_t *cr, struct xrchart_geometry *geom, const char *name)
441 {
442   const struct xrchart_colour *colour;
443
444   cairo_save (cr);
445
446   colour = &data_colour[geom->n_datasets % XRCHART_N_COLOURS];
447   cairo_set_source_rgb (cr,
448                         colour->red / 255.0,
449                         colour->green / 255.0,
450                         colour->blue / 255.0);
451
452   geom->n_datasets++;
453   geom->dataset = xrealloc (geom->dataset,
454                             geom->n_datasets * sizeof (*geom->dataset));
455
456   geom->dataset[geom->n_datasets - 1] = strdup (name);
457 }
458
459 /* Plot a data point */
460 void
461 xrchart_datum (cairo_t *cr, const struct xrchart_geometry *geom,
462              int dataset UNUSED, double x, double y)
463 {
464   double x_pos = (x - geom->axis[SCALE_ABSCISSA].min) * geom->axis[SCALE_ABSCISSA].scale + geom->axis[SCALE_ABSCISSA].data_min;
465   double y_pos = (y - geom->axis[SCALE_ORDINATE].min) * geom->axis[SCALE_ORDINATE].scale + geom->axis[SCALE_ORDINATE].data_min;
466
467   xrchart_draw_marker (cr, x_pos, y_pos, XRMARKER_SQUARE, 15);
468 }
469
470 void
471 xrchart_vector_end (cairo_t *cr, struct xrchart_geometry *geom)
472 {
473   cairo_stroke (cr);
474   cairo_restore (cr);
475   geom->in_path = false;
476 }
477
478 /* Plot a data point */
479 void
480 xrchart_vector (cairo_t *cr, struct xrchart_geometry *geom, double x, double y)
481 {
482   const double x_pos =
483     (x - geom->axis[SCALE_ABSCISSA].min) * geom->axis[SCALE_ABSCISSA].scale + geom->axis[SCALE_ABSCISSA].data_min ;
484
485   const double y_pos =
486     (y - geom->axis[SCALE_ORDINATE].min) * geom->axis[SCALE_ORDINATE].scale + geom->axis[SCALE_ORDINATE].data_min ;
487
488   if (geom->in_path)
489     cairo_line_to (cr, x_pos, y_pos);
490   else
491     {
492       cairo_move_to (cr, x_pos, y_pos);
493       geom->in_path = true;
494     }
495 }
496
497
498
499 /* Draw a line with slope SLOPE and intercept INTERCEPT.
500    between the points limit1 and limit2.
501    If lim_dim is XRCHART_DIM_Y then the limit{1,2} are on the
502    y axis otherwise the x axis
503 */
504 void
505 xrchart_line(cairo_t *cr, const struct xrchart_geometry *geom,
506            double slope, double intercept,
507            double limit1, double limit2, enum xrchart_dim lim_dim)
508 {
509   double x1, y1;
510   double x2, y2;
511
512   if ( lim_dim == XRCHART_DIM_Y )
513     {
514       x1 = ( limit1 - intercept ) / slope;
515       x2 = ( limit2 - intercept ) / slope;
516       y1 = limit1;
517       y2 = limit2;
518     }
519   else
520     {
521       x1 = limit1;
522       x2 = limit2;
523       y1 = slope * x1 + intercept;
524       y2 = slope * x2 + intercept;
525     }
526
527   y1 = (y1 - geom->axis[SCALE_ORDINATE].min) * geom->axis[SCALE_ORDINATE].scale + geom->axis[SCALE_ORDINATE].data_min;
528   y2 = (y2 - geom->axis[SCALE_ORDINATE].min) * geom->axis[SCALE_ORDINATE].scale + geom->axis[SCALE_ORDINATE].data_min;
529   x1 = (x1 - geom->axis[SCALE_ABSCISSA].min) * geom->axis[SCALE_ABSCISSA].scale + geom->axis[SCALE_ABSCISSA].data_min;
530   x2 = (x2 - geom->axis[SCALE_ABSCISSA].min) * geom->axis[SCALE_ABSCISSA].scale + geom->axis[SCALE_ABSCISSA].data_min;
531
532   cairo_move_to (cr, x1, y1);
533   cairo_line_to (cr, x2, y2);
534   cairo_stroke (cr);
535 }