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.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
xrchart_geometry_init(cairo_t * cr,struct xrchart_geometry * geom,double width,double length)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
xrchart_geometry_free(cairo_t * cr UNUSED,struct xrchart_geometry * geom)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
pango_layout_get_baseline(PangoLayout * layout)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
xrchart_draw_marker(cairo_t * cr,double x,double y,enum xrmarker_type marker,double size)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
xrchart_label_rotate(cairo_t * cr,int horz_justify,int vert_justify,double font_size,const char * string,double angle)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
xrchart_label(cairo_t * cr,int horz_justify,int vert_justify,double font_size,const char * string)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
draw_tick(cairo_t * cr,const struct xrchart_geometry * geom,enum tick_orientation orientation,bool rotated,double position,const char * label,...)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
draw_tick_internal(cairo_t * cr,const struct xrchart_geometry * geom,enum tick_orientation orientation,bool rotated,double position,const char * s)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
xrchart_write_title(cairo_t * cr,const struct xrchart_geometry * geom,const char * title,...)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
xrchart_text_extents(cairo_t * cr,const struct xrchart_geometry * geom,const char * utf8,double * width,double * height)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 bool
xrchart_write_scale(cairo_t * cr,struct xrchart_geometry * geom,double smin,double smax,enum tick_orientation orient)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 if (smax == smin)
384 return false;
385
386 chart_get_scale (smax, smin, &lower, &interval, &ticks);
387
388 tick_format_string = chart_get_ticks_format (lower, interval, ticks, &tickscale);
389
390 upper = lower + interval * (ticks+1);
391
392 geom->axis[orient].max = upper;
393 geom->axis[orient].min = lower;
394
395 struct xrchart_axis *axis = &geom->axis[orient];
396 geom->axis[orient].scale = (fabs ((double) axis->data_max - axis->data_min)
397 / fabs (axis->max - axis->min));
398
399 if (orient == SCALE_ABSCISSA)
400 {
401 char *test_text;
402 double lower_txt_width, upper_txt_width, unused, width;
403 test_text = xasprintf(tick_format_string, upper*tickscale);
404 xrchart_text_extents (cr, geom, test_text, &upper_txt_width, &unused);
405 free(test_text);
406 test_text = xasprintf(tick_format_string, lower*tickscale);
407 xrchart_text_extents (cr, geom, test_text, &lower_txt_width, &unused);
408 free(test_text);
409 width = MAX(lower_txt_width, upper_txt_width);
410 tickoversize = width > 0.9 *
411 ((double)(geom->axis[SCALE_ABSCISSA].data_max - geom->axis[SCALE_ABSCISSA].data_min))/(ticks+1);
412 }
413
414 double pos = lower;
415
416 for (s = 0 ; s <= ticks; ++s)
417 {
418 draw_tick (cr, geom, orient, tickoversize,
419 s * interval * geom->axis[orient].scale,
420 tick_format_string, pos*tickscale);
421 pos += interval;
422 }
423 free(tick_format_string);
424
425 return true;
426 }
427
428 /* Set the scale for the ordinate */
429 bool
xrchart_write_yscale(cairo_t * cr,struct xrchart_geometry * geom,double smin,double smax)430 xrchart_write_yscale (cairo_t *cr, struct xrchart_geometry *geom,
431 double smin, double smax)
432 {
433 return xrchart_write_scale (cr, geom, smin, smax, SCALE_ORDINATE);
434 }
435
436 /* Set the scale for the abscissa */
437 bool
xrchart_write_xscale(cairo_t * cr,struct xrchart_geometry * geom,double smin,double smax)438 xrchart_write_xscale (cairo_t *cr, struct xrchart_geometry *geom,
439 double smin, double smax)
440 {
441 return xrchart_write_scale (cr, geom, smin, smax, SCALE_ABSCISSA);
442 }
443
444
445 /* Write the abscissa label */
446 void
xrchart_write_xlabel(cairo_t * cr,const struct xrchart_geometry * geom,const char * label)447 xrchart_write_xlabel (cairo_t *cr, const struct xrchart_geometry *geom,
448 const char *label)
449 {
450 cairo_move_to (cr, geom->axis[SCALE_ABSCISSA].data_min, geom->abscissa_bottom);
451 xrchart_label (cr, 'l', 't', geom->font_size, label);
452 }
453
454 /* Write the ordinate label */
455 void
xrchart_write_ylabel(cairo_t * cr,const struct xrchart_geometry * geom,const char * label)456 xrchart_write_ylabel (cairo_t *cr, const struct xrchart_geometry *geom,
457 const char *label)
458 {
459 cairo_save (cr);
460 cairo_translate (cr, geom->ordinate_left, geom->axis[SCALE_ORDINATE].data_min);
461 cairo_rotate (cr, M_PI / 2.0);
462
463 xrchart_label (cr, 'l', 'x', geom->font_size, label);
464 cairo_restore (cr);
465 }
466
467
468 void
xrchart_write_legend(cairo_t * cr,const struct xrchart_geometry * geom)469 xrchart_write_legend (cairo_t *cr, const struct xrchart_geometry *geom)
470 {
471 int i;
472 const int vstep = geom->font_size * 2;
473 const int xpad = 10;
474 const int ypad = 10;
475 const int swatch = 20;
476 const int legend_top = geom->axis[SCALE_ORDINATE].data_max;
477 const int legend_bottom = legend_top -
478 (vstep * geom->n_datasets + 2 * ypad);
479
480 cairo_save (cr);
481
482 cairo_rectangle (cr, geom->legend_left, legend_top,
483 geom->legend_right - xpad - geom->legend_left,
484 legend_bottom - legend_top);
485 cairo_stroke (cr);
486
487 for (i = 0 ; i < geom->n_datasets ; ++i)
488 {
489 const int ypos = legend_top - vstep * (i + 1);
490 const int xpos = geom->legend_left + xpad;
491 const struct xrchart_colour *colour;
492
493 cairo_move_to (cr, xpos, ypos);
494
495 cairo_save (cr);
496 colour = &data_colour [ i % XRCHART_N_COLOURS];
497 cairo_set_source_rgb (cr,
498 colour->red / 255.0,
499 colour->green / 255.0,
500 colour->blue / 255.0);
501 cairo_rectangle (cr, xpos, ypos, swatch, swatch);
502 cairo_fill_preserve (cr);
503 cairo_stroke (cr);
504 cairo_restore (cr);
505
506 cairo_move_to (cr, xpos + swatch * 1.5, ypos);
507 xrchart_label (cr, 'l', 'x', geom->font_size, geom->dataset[i]);
508 }
509
510 cairo_restore (cr);
511 }
512
513 /* Start a new vector called NAME */
514 void
xrchart_vector_start(cairo_t * cr,struct xrchart_geometry * geom,const char * name)515 xrchart_vector_start (cairo_t *cr, struct xrchart_geometry *geom, const char *name)
516 {
517 const struct xrchart_colour *colour;
518
519 cairo_save (cr);
520
521 colour = &data_colour[geom->n_datasets % XRCHART_N_COLOURS];
522 cairo_set_source_rgb (cr,
523 colour->red / 255.0,
524 colour->green / 255.0,
525 colour->blue / 255.0);
526
527 geom->n_datasets++;
528 geom->dataset = xrealloc (geom->dataset,
529 geom->n_datasets * sizeof (*geom->dataset));
530
531 geom->dataset[geom->n_datasets - 1] = strdup (name);
532 }
533
534 /* Plot a data point */
535 void
xrchart_datum(cairo_t * cr,const struct xrchart_geometry * geom,int dataset UNUSED,double x,double y)536 xrchart_datum (cairo_t *cr, const struct xrchart_geometry *geom,
537 int dataset UNUSED, double x, double y)
538 {
539 double x_pos = (x - geom->axis[SCALE_ABSCISSA].min) * geom->axis[SCALE_ABSCISSA].scale + geom->axis[SCALE_ABSCISSA].data_min;
540 double y_pos = (y - geom->axis[SCALE_ORDINATE].min) * geom->axis[SCALE_ORDINATE].scale + geom->axis[SCALE_ORDINATE].data_min;
541
542 xrchart_draw_marker (cr, x_pos, y_pos, XRMARKER_CIRCLE, 10);
543 }
544
545 void
xrchart_vector_end(cairo_t * cr,struct xrchart_geometry * geom)546 xrchart_vector_end (cairo_t *cr, struct xrchart_geometry *geom)
547 {
548 cairo_stroke (cr);
549 cairo_restore (cr);
550 geom->in_path = false;
551 }
552
553 /* Plot a data point */
554 void
xrchart_vector(cairo_t * cr,struct xrchart_geometry * geom,double x,double y)555 xrchart_vector (cairo_t *cr, struct xrchart_geometry *geom, double x, double y)
556 {
557 const double x_pos =
558 (x - geom->axis[SCALE_ABSCISSA].min) * geom->axis[SCALE_ABSCISSA].scale + geom->axis[SCALE_ABSCISSA].data_min ;
559
560 const double y_pos =
561 (y - geom->axis[SCALE_ORDINATE].min) * geom->axis[SCALE_ORDINATE].scale + geom->axis[SCALE_ORDINATE].data_min ;
562
563 if (geom->in_path)
564 cairo_line_to (cr, x_pos, y_pos);
565 else
566 {
567 cairo_move_to (cr, x_pos, y_pos);
568 geom->in_path = true;
569 }
570 }
571
572
573
574 /* Draw a line with slope SLOPE and intercept INTERCEPT.
575 between the points limit1 and limit2.
576 If lim_dim is XRCHART_DIM_Y then the limit{1,2} are on the
577 y axis otherwise the x axis
578 */
579 void
xrchart_line(cairo_t * cr,const struct xrchart_geometry * geom,double slope,double intercept,double limit1,double limit2,enum xrchart_dim lim_dim)580 xrchart_line(cairo_t *cr, const struct xrchart_geometry *geom,
581 double slope, double intercept,
582 double limit1, double limit2, enum xrchart_dim lim_dim)
583 {
584 double x1, y1;
585 double x2, y2;
586
587 if (lim_dim == XRCHART_DIM_Y)
588 {
589 x1 = (limit1 - intercept) / slope;
590 x2 = (limit2 - intercept) / slope;
591 y1 = limit1;
592 y2 = limit2;
593 }
594 else
595 {
596 x1 = limit1;
597 x2 = limit2;
598 y1 = slope * x1 + intercept;
599 y2 = slope * x2 + intercept;
600 }
601
602 y1 = (y1 - geom->axis[SCALE_ORDINATE].min) * geom->axis[SCALE_ORDINATE].scale + geom->axis[SCALE_ORDINATE].data_min;
603 y2 = (y2 - geom->axis[SCALE_ORDINATE].min) * geom->axis[SCALE_ORDINATE].scale + geom->axis[SCALE_ORDINATE].data_min;
604 x1 = (x1 - geom->axis[SCALE_ABSCISSA].min) * geom->axis[SCALE_ABSCISSA].scale + geom->axis[SCALE_ABSCISSA].data_min;
605 x2 = (x2 - geom->axis[SCALE_ABSCISSA].min) * geom->axis[SCALE_ABSCISSA].scale + geom->axis[SCALE_ABSCISSA].data_min;
606
607 cairo_move_to (cr, x1, y1);
608 cairo_line_to (cr, x2, y2);
609 cairo_stroke (cr);
610 }
611
612