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