]> git.cworth.org Git - acre/blob - acre.c
Close up some memory leaks.
[acre] / acre.c
1 /* acre - A cairo-based library for creating plots and charts.
2  *
3  * Copyright © 2009 Carl Worth <cworth@cworth.org>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
18  */
19
20 #define _ISOC99_SOURCE /* for round() */
21 #define _XOPEN_SOURCE 500
22 #define _GNU_SOURCE /* for asprintf() */
23
24 #include "acre.h"
25 #include "xmalloc.h"
26
27 #include <string.h>
28 #include <stdarg.h>
29 #include <stdbool.h>
30 #include <math.h>
31
32 #include <lcms.h>
33
34 typedef struct _acre_data_point_2d {
35     double x;
36     double y;
37 } acre_data_point_2d_t;
38
39 struct _acre_data {
40     char *name;
41
42     acre_data_point_2d_t min;
43     acre_data_point_2d_t max;
44
45     acre_data_point_2d_t *points;
46     unsigned int points_size;
47     unsigned int num_points;
48 };
49
50 typedef struct _acre_axis {
51     char *label;
52
53     /* Range of data */
54     double data_min;
55     double data_max;
56
57     /* Range of data to be viewed. */
58     double view_min;
59     double view_max;
60
61     /* Has the view range been set? */
62     bool view_range_set;
63 } acre_axis_t;
64
65 typedef struct _acre_color {
66     double red;
67     double green;
68     double blue;
69 } acre_color_t;
70
71 struct _acre {
72     char *title;
73     acre_axis_t x_axis;
74     acre_axis_t y_axis;
75
76     acre_data_t **data;
77     unsigned int data_size;
78     unsigned int num_data;
79
80     /* Data for drawing. */
81     cairo_t *cr;
82     PangoFontDescription *font;
83     acre_color_t *colors;
84     int colors_size;
85     int num_colors;
86
87     /* Total size including labels. */
88     int width;
89     int height;
90
91     /* Position and size of chart alone. */
92     PangoRectangle chart;
93 };
94
95 static void
96 _find_x_range_given_y_range (acre_t *acre,
97                              double *x_min, double *x_max,
98                              double y_min, double y_max);
99
100 static void
101 _find_y_range_given_x_range (acre_t *acre,
102                              double *y_min, double *y_max,
103                              double x_min, double x_max);
104
105 /* Create a new, empty plot. */
106 acre_t *
107 acre_create (void)
108 {
109     acre_t *acre;
110
111     acre = xmalloc (sizeof (acre_t));
112
113     acre->title = NULL;
114
115     acre->x_axis.label = NULL;
116     acre->x_axis.data_min = 0.0;
117     acre->x_axis.data_max = 0.0;
118     acre->x_axis.view_min = 0.0;
119     acre->x_axis.view_max = 0.0;
120     acre->x_axis.view_range_set = false;
121
122     acre->y_axis.label = NULL;
123     acre->y_axis.data_min = 0.0;
124     acre->y_axis.data_max = 0.0;
125     acre->y_axis.view_min = 0.0;
126     acre->y_axis.view_max = 0.0;
127     acre->y_axis.view_range_set = false;
128
129     acre->data = NULL;
130     acre->data_size = 0;
131     acre->num_data = 0;
132
133     acre->cr = NULL;
134     acre->font = NULL;
135     acre->colors = NULL;
136     acre->num_colors = 0;
137     acre->colors_size = 0;
138
139     acre->width = 0;
140     acre->height = 0;
141
142     acre->chart.x = 0;
143     acre->chart.y = 0;
144     acre->chart.width = 0;
145     acre->chart.height = 0;
146
147     return acre;
148 }
149
150 /* Destroy a plot. */
151 void
152 acre_destroy (acre_t *acre)
153 {
154     unsigned int i;
155
156     free (acre->title);
157     free (acre->x_axis.label);
158     free (acre->y_axis.label);
159
160     for (i = 0; i < acre->num_data; i++)
161         acre_data_destroy (acre->data[i]);
162
163     free (acre->data);
164
165     free (acre->colors);
166
167     free (acre);
168 }
169
170 void
171 acre_set_title (acre_t *acre, const char *title)
172 {
173     free (acre->title);
174
175     acre->title = strdup (title);
176 }
177
178 void
179 acre_set_x_axis_label (acre_t *acre, const char *label)
180 {
181     free (acre->x_axis.label);
182
183     acre->x_axis.label = strdup (label);
184 }
185
186 void
187 acre_set_y_axis_label (acre_t *acre, const char *label)
188 {
189     free (acre->y_axis.label);
190
191     acre->y_axis.label = strdup (label);
192 }
193
194 void
195 acre_get_x_axis_data_range (acre_t *acre, double *x_min, double *x_max)
196 {
197     if (x_min)
198         *x_min = acre->x_axis.data_min;
199
200     if (x_max)
201         *x_max = acre->x_axis.data_max;
202 }
203
204 void
205 acre_get_x_axis_range (acre_t *acre, double *x_min, double *x_max)
206 {
207     /* If an X range has been set, return that. */
208     if (acre->x_axis.view_range_set) {
209         if (x_min)
210             *x_min = acre->x_axis.view_min;
211
212         if (x_max)
213             *x_max = acre->x_axis.view_max;
214
215         return;
216     }
217
218     /* Otherwise, if a Y range has been set, use that to compute X. */
219     if (acre->y_axis.view_range_set) {
220         _find_x_range_given_y_range (acre, x_min, x_max,
221                                      acre->y_axis.view_min,
222                                      acre->y_axis.view_max);
223
224         return;
225     }
226
227     /* Neither view range set. Return full, data-based X range. */
228     acre_get_x_axis_data_range (acre, x_min, x_max);
229 }
230
231 void
232 acre_set_x_axis_range (acre_t *acre, double x_min, double x_max)
233 {
234     acre->x_axis.view_min = x_min;
235     acre->x_axis.view_max = x_max;
236
237     acre->x_axis.view_range_set = true;
238 }
239
240 void
241 acre_set_x_axis_range_auto (acre_t *acre)
242 {
243     acre->x_axis.view_range_set = false;
244 }
245
246 void
247 acre_get_y_axis_data_range (acre_t *acre, double *y_min, double *y_max)
248 {
249     if (y_min)
250         *y_min = acre->y_axis.data_min;
251
252     if (y_max)
253         *y_max = acre->y_axis.data_max;
254 }
255
256 void
257 acre_get_y_axis_range (acre_t *acre, double *y_min, double *y_max)
258 {
259     /* If a Y range has been set, return that. */
260     if (acre->y_axis.view_range_set) {
261         if (y_min)
262             *y_min = acre->y_axis.view_min;
263
264         if (y_max)
265             *y_max = acre->y_axis.view_max;
266
267         return;
268     }
269
270     /* Otherwise, if an X range has been set, use that to compute Y. */
271     if (acre->x_axis.view_range_set) {
272         _find_y_range_given_x_range (acre, y_min, y_max,
273                                      acre->x_axis.view_min,
274                                      acre->x_axis.view_max);
275
276         return;
277     }
278
279     /* Neither view range set. Return full data-based Y range. */
280     acre_get_y_axis_data_range (acre, y_min, y_max);
281 }
282
283 void
284 acre_set_y_axis_range (acre_t *acre, double y_min, double y_max)
285 {
286     acre->y_axis.view_min = y_min;
287     acre->y_axis.view_max = y_max;
288
289     acre->y_axis.view_range_set = true;
290 }
291
292 void
293 acre_set_y_axis_range_auto (acre_t *acre)
294 {
295     acre->y_axis.view_range_set = false;
296 }
297
298 /* Add a dataset to the plot. The plot assumes ownership of the
299  * dataset so it is not necessary to call acre_data_destroy on it. */
300 void
301 acre_add_data (acre_t *acre, acre_data_t *data)
302 {
303     if (acre->num_data >= acre->data_size) {
304         acre->data_size *= 2;
305         if (acre->data_size == 0)
306             acre->data_size = 1;
307         acre->data = xrealloc_ab (acre->data,
308                                   acre->data_size,
309                                   sizeof (acre_data_t *));
310     }
311
312     acre->data[acre->num_data] = data;
313
314     if (acre->num_data == 0) {
315         acre->x_axis.data_min = data->min.x;
316         acre->y_axis.data_min = data->min.y;
317
318         acre->x_axis.data_max = data->max.x;
319         acre->y_axis.data_max = data->max.y;
320     } else {
321         if (data->min.x < acre->x_axis.data_min)
322             acre->x_axis.data_min = data->min.x;
323         if (data->min.y < acre->y_axis.data_min)
324             acre->y_axis.data_min = data->min.y;
325
326         if (data->max.x > acre->x_axis.data_max)
327             acre->x_axis.data_max = data->max.x;
328         if (data->max.y > acre->y_axis.data_max)
329             acre->y_axis.data_max = data->max.y;
330     }
331
332     acre->num_data++;
333 }
334
335 #define ACRE_FONT_FAMILY "sans"
336 #define ACRE_FONT_SIZE 12
337 #define ACRE_TITLE_FONT_SIZE 20
338 #define ACRE_PAD (ACRE_FONT_SIZE)
339 #define ACRE_TICK_MAJOR_SIZE 6
340 #define ACRE_TICK_MINOR_SIZE 3
341 #define ACRE_X_TICK_VALUE_PAD 2
342 #define ACRE_Y_TICK_VALUE_PAD 4
343 #define ACRE_LEGEND_PAD 4
344 #define ACRE_LEGEND_LINE_SIZE 10
345
346 static PangoLayout *
347 _create_layout (acre_t *acre, const char *text)
348 {
349     PangoLayout *layout;
350
351     if (text == NULL)
352             text = "";
353
354     layout = pango_cairo_create_layout (acre->cr);
355     pango_layout_set_font_description (layout, acre->font);
356     pango_layout_set_text (layout, text, -1);
357     pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
358
359     return layout;
360 }
361
362 #define PRINTF_FORMAT(fmt_index, va_index) __attribute__ ((__format__(__printf__, fmt_index, va_index)))
363
364 static PangoLayout *
365 _create_layout_vprintf (acre_t *acre, const char *fmt, va_list ap)
366 {
367     PangoLayout *layout;
368     char *text;
369
370     vasprintf (&text, fmt, ap);
371
372     layout = _create_layout (acre, text);
373
374     free (text);
375
376     return layout;
377 }
378
379 static PangoLayout *
380 _create_layout_printf (acre_t *acre, const char *fmt, ...)
381     PRINTF_FORMAT (2, 3);
382
383 static PangoLayout *
384 _create_layout_printf (acre_t *acre, const char *fmt, ...)
385 {
386     va_list ap;
387     PangoLayout *layout;
388
389     va_start (ap, fmt);
390
391     layout = _create_layout_vprintf (acre, fmt, ap);
392
393     va_end (ap);
394
395     return layout;
396 }
397
398 static void
399 _destroy_layout (PangoLayout *layout)
400 {
401     g_object_unref (layout);
402 }
403
404 static void
405 _show_layout (cairo_t *cr, PangoLayout *layout)
406 {
407     pango_cairo_show_layout (cr, layout);
408
409     _destroy_layout (layout);
410 }
411
412 static void
413 _draw_title_and_labels (acre_t *acre)
414 {
415     cairo_t *cr = acre->cr;
416     PangoFontDescription *title_font;
417     PangoLayout *title_layout, *x_axis_layout, *y_axis_layout;
418     PangoLayout *min_y, *max_y;
419     int min_y_width, max_y_width, y_axis_value_width;
420     int title_width, title_height;
421     int x_axis_width, x_axis_height;
422     int y_axis_width, y_axis_height;
423     PangoRectangle new_chart;
424
425     cairo_save (cr);
426
427     acre->font = pango_font_description_new ();
428     pango_font_description_set_family (acre->font, ACRE_FONT_FAMILY);
429     pango_font_description_set_absolute_size (acre->font,
430                                               ACRE_FONT_SIZE * PANGO_SCALE);
431
432     title_font = pango_font_description_new ();
433     pango_font_description_set_family (title_font, ACRE_FONT_FAMILY);
434     pango_font_description_set_absolute_size (title_font,
435                                               ACRE_TITLE_FONT_SIZE * PANGO_SCALE);
436
437     title_layout = _create_layout (acre, acre->title);
438     pango_layout_set_font_description (title_layout, title_font);
439     pango_font_description_free (title_font);
440
441     x_axis_layout = _create_layout (acre, acre->x_axis.label);
442     y_axis_layout = _create_layout (acre, acre->y_axis.label);
443
444     min_y = _create_layout_printf (acre, "%g",
445                                    round (acre->y_axis.view_min));
446     max_y = _create_layout_printf (acre, "%g",
447                                    round (acre->y_axis.view_max));
448
449     pango_layout_get_pixel_size (min_y, &min_y_width, NULL);
450     pango_layout_get_pixel_size (max_y, &max_y_width, NULL);
451     y_axis_value_width = MAX (min_y_width, max_y_width);
452
453     _destroy_layout (min_y);
454     _destroy_layout (max_y);
455
456     /* Iterate with the layout of the title and axis labels until they
457      * are stable, (this requires iteration since we don't know what
458      * to set their widths to in advance due to the wrapping of the
459      * other elements). */
460     while (1) {
461         pango_layout_set_width (title_layout, acre->chart.width * PANGO_SCALE);
462         pango_layout_set_width (x_axis_layout, acre->chart.width * PANGO_SCALE);
463         pango_layout_set_width (y_axis_layout, acre->chart.height * PANGO_SCALE);
464
465         pango_layout_get_pixel_size (title_layout, &title_width, &title_height);
466         pango_layout_get_pixel_size (x_axis_layout, &x_axis_width, &x_axis_height);
467         pango_layout_get_pixel_size (y_axis_layout, &y_axis_width, &y_axis_height);
468
469         new_chart.x = ACRE_PAD + y_axis_height +
470             ACRE_PAD + y_axis_value_width + ACRE_Y_TICK_VALUE_PAD;
471         new_chart.width = acre->width - acre->chart.x - ACRE_PAD;
472
473         new_chart.y = ACRE_PAD + title_height + ACRE_PAD;
474         new_chart.height = acre->height - acre->chart.y - 
475             (ACRE_X_TICK_VALUE_PAD + ACRE_FONT_SIZE +
476              ACRE_PAD + x_axis_height + ACRE_PAD);
477
478         if (new_chart.x == acre->chart.x &&
479             new_chart.y == acre->chart.y &&
480             new_chart.width == acre->chart.width &&
481             new_chart.height == acre->chart.height)
482         {
483             break;
484         }
485
486         acre->chart.x = new_chart.x;
487         acre->chart.y = new_chart.y;
488         acre->chart.width = new_chart.width;
489         acre->chart.height = new_chart.height;
490     }
491
492     cairo_set_source_rgb (cr, 0, 0, 0);
493
494     cairo_move_to (cr, acre->chart.x, ACRE_PAD);
495     _show_layout (cr, title_layout);
496
497     cairo_save (cr);
498     {
499         cairo_translate (cr, ACRE_PAD, acre->chart.y + acre->chart.height);
500         cairo_rotate (cr, - M_PI / 2.0);
501         cairo_move_to (cr, 0, 0);
502         _show_layout (cr, y_axis_layout);
503     }
504     cairo_restore (cr);
505
506     cairo_move_to (cr, acre->chart.x,
507                    acre->chart.y + acre->chart.height +
508                    ACRE_FONT_SIZE + ACRE_PAD);
509     _show_layout (cr, x_axis_layout);
510
511     cairo_restore (cr);
512 }
513
514 /* For a given axis range, compute a step size (in data space) to
515  * generate a suitable number of ticks (5 or so). */
516 static double
517 _step_for_range (double range, int *minor_divisions)
518 {
519     double step, scale_factor;
520
521     /* We want roughly 5 major ticks for the chart. */
522     step = range / 5;
523
524     /* Normalize the step so we can easily snap it to a desirable
525      * value. */
526     scale_factor = pow (10.0, floor (log10 (step)));
527     step /= scale_factor;
528
529     /* We want increments of 1, 2.5, 5, or 10 (times some power of
530      * 10). The threshold values between these are computed
531      * logarithmically. */
532     if (step < 3.535533905932738) {
533         if (step < 1.58113883008419) {
534             step = 1.0;
535             *minor_divisions = 4;
536         } else {
537             step = 2.5;
538             *minor_divisions = 5;
539         }
540     } else {
541         if (step < 7.071067811865475) {
542             step = 5.0;
543             *minor_divisions = 5;
544         } else {
545             step = 10.0;
546             *minor_divisions = 4;
547         }
548     }
549
550     /* Un-normalize and we now have the data value that we want to
551      * step at. */
552     return step * scale_factor;
553 }
554
555 /* Given an axis range, we can compute a desired data-space step
556  * amount for the major ticks (see _step_for_range). To get
557  * nice-looking pixel-snapped ticks we want to expand the range
558  * slightly. */
559 static void
560 _expand_range_for_width (double *axis_min, double *axis_max, int pixel_range)
561 {
562     double range, new_range, step, step_minor, pixel_step;
563     int minor_divisions;
564
565     range = *axis_max - *axis_min;
566
567     step = _step_for_range (range, &minor_divisions);
568     step_minor = step / minor_divisions;
569
570     pixel_step = step_minor * (pixel_range / range);
571
572     /* We expand the range by the ratio of the pixel step to the floor
573      * of the pixel_step.
574      */
575     new_range = range * pixel_step / floor (pixel_step);
576
577     /* And spread the increase out on either side of the range. */
578     *axis_min -= (new_range - range) / 2.0;
579     *axis_max += (new_range - range) / 2.0;
580 }
581
582 /* Setup a transformation in acre->cr such that data values plotted
583  * will appear where they should within the chart.
584  */
585 static void
586 _set_transform_to_data_space (acre_t *acre)
587 {
588     cairo_t *cr = acre->cr;
589
590     cairo_translate (cr,
591                      acre->chart.x,
592                      acre->chart.y + acre->chart.height);
593     cairo_scale (cr,
594                  acre->chart.width / (acre->x_axis.view_max - acre->x_axis.view_min),
595                  - acre->chart.height /(acre->y_axis.view_max - acre->y_axis.view_min));
596     cairo_translate (cr, -acre->x_axis.view_min, -acre->y_axis.view_min);
597 }
598
599 static void
600 _find_x_range_given_y_range (acre_t *acre,
601                              double *x_min, double *x_max,
602                              double y_min, double y_max)
603 {
604     acre_data_t *data;
605     unsigned d, i;
606     bool first;
607
608     first = true;
609
610     for (d = 0; d < acre->num_data; d++) {
611         data = acre->data[d];
612         for (i = 0; i < data->num_points; i++) {
613             if (data->points[i].y >= y_min &&
614                 data->points[i].y <= y_max)
615             {
616                 if (first) {
617                     *x_min = data->points[i].x;
618                     *x_max = data->points[i].x;
619                     first = false;
620                 } else {
621                     if (data->points[i].x < *x_min)
622                         *x_min = data->points[i].x;
623                     if (data->points[i].x > *x_max)
624                         *x_max = data->points[i].x;
625                 }
626             }
627         }
628     }
629
630     /* If nothing is visible, punt to full X data range. */
631     if (first) {
632         *x_min = acre->x_axis.data_min;
633         *x_max = acre->x_axis.data_max;
634     }
635 }
636
637 static void
638 _find_y_range_given_x_range (acre_t *acre,
639                              double *y_min, double *y_max,
640                              double x_min, double x_max)
641 {
642     acre_data_t *data;
643     unsigned d, i;
644     bool first;
645
646     first = true;
647
648     for (d = 0; d < acre->num_data; d++) {
649         data = acre->data[d];
650         for (i = 0; i < data->num_points; i++) {
651             if (data->points[i].x >= x_min &&
652                 data->points[i].x <= x_max)
653             {
654                 if (first) {
655                     *y_min = data->points[i].y;
656                     *y_max = data->points[i].y;
657                     first = false;
658                 } else {
659                     if (data->points[i].y < *y_min)
660                         *y_min = data->points[i].y;
661                     if (data->points[i].y > *y_max)
662                         *y_max = data->points[i].y;
663                 }
664             }
665         }
666     }
667
668     /* If nothing is visible, punt to full Y data range. */
669     if (first) {
670         *y_min = acre->y_axis.data_min;
671         *y_max = acre->y_axis.data_max;
672     }
673 }
674
675 static void
676 _compute_axis_ranges (acre_t *acre)
677 {
678     double x_adjust, y_adjust;
679     cairo_t *cr = acre->cr;
680
681     /* If neither view range is set, set both to data ranges. */
682     if (! acre->x_axis.view_range_set && ! acre->y_axis.view_range_set)
683     {
684         acre->x_axis.view_min = acre->x_axis.data_min;
685         acre->x_axis.view_max = acre->x_axis.data_max;
686
687         acre->y_axis.view_min = acre->y_axis.data_min;
688         acre->y_axis.view_max = acre->y_axis.data_max;
689     } else {
690         /* Otherwise, auto-fit unset range based on data. */
691         if (acre->x_axis.view_range_set && ! acre->y_axis.view_range_set) {
692             _find_y_range_given_x_range (acre,
693                                          &acre->y_axis.view_min,
694                                          &acre->y_axis.view_max,
695                                          acre->x_axis.view_min,
696                                          acre->x_axis.view_max);
697         }
698         else if (acre->y_axis.view_range_set && ! acre->x_axis.view_range_set) {
699             _find_x_range_given_y_range (acre,
700                                          &acre->x_axis.view_min,
701                                          &acre->x_axis.view_max,
702                                          acre->y_axis.view_min,
703                                          acre->y_axis.view_max);
704         }
705     }
706
707     /* Then, increase the axis ranges just enough so that the step
708      * sizes for the ticks will be integers.
709      */
710     _expand_range_for_width (&acre->x_axis.view_min,
711                              &acre->x_axis.view_max,
712                              acre->chart.width);
713
714     _expand_range_for_width (&acre->y_axis.view_min,
715                              &acre->y_axis.view_max,
716                              acre->chart.height);
717
718     /* Finally, we also translate the axis ranges slightly so that the
719      * ticks land on half-integer device-pixel positions.
720      */
721     cairo_save (cr);
722     {
723         _set_transform_to_data_space (acre);
724
725         x_adjust = 0.0;
726         y_adjust = 0.0;
727         cairo_user_to_device (cr, &x_adjust, &y_adjust);
728         x_adjust = (round (x_adjust + 0.5) - 0.5) - x_adjust;
729         y_adjust = (round (y_adjust + 0.5) - 0.5) - y_adjust;
730         cairo_device_to_user_distance (cr, &x_adjust, &y_adjust);
731
732         acre->x_axis.view_min -= x_adjust;
733         acre->x_axis.view_max -= x_adjust;
734
735         acre->y_axis.view_min -= y_adjust;
736         acre->y_axis.view_max -= y_adjust;
737     }
738     cairo_restore (cr);
739 }
740
741 static void
742 _choose_colors (acre_t *acre)
743 {
744     cmsHPROFILE lab_profile, srgb_profile;
745     cmsHTRANSFORM lab_to_srgb;
746     int i;
747     double theta, radius, srgb[3];
748     cmsCIELab lab;
749
750     lab_profile = cmsCreateLabProfile (NULL); /* D50 */
751     srgb_profile = cmsCreate_sRGBProfile ();
752
753     lab_to_srgb = cmsCreateTransform (lab_profile, TYPE_Lab_DBL,
754                                       srgb_profile, TYPE_RGB_DBL,
755                                       INTENT_PERCEPTUAL, 0);
756
757     acre->num_colors = acre->num_data;
758
759     if (acre->num_colors > acre->colors_size) {
760         acre->colors_size = acre->num_colors;
761         acre->colors = xrealloc (acre->colors,
762                                  acre->colors_size * sizeof (acre_color_t));
763     }
764
765     lab.L = 36;
766     radius = 130;
767     for (i = 0; i < acre->num_colors; i++) {
768         theta = 0.713 + 2 * M_PI * (double) i / acre->num_colors;
769         lab.a = radius * cos (theta);
770         lab.b = radius * sin (theta);
771
772         cmsDoTransform (lab_to_srgb, &lab, srgb, 1);
773
774         acre->colors[i].red = srgb[0];
775         acre->colors[i].green = srgb[1];
776         acre->colors[i].blue = srgb[2];
777     }
778
779     cmsDeleteTransform (lab_to_srgb);
780     cmsCloseProfile (lab_profile);
781     cmsCloseProfile (srgb_profile);
782 }
783
784 static void
785 _draw_data (acre_t *acre)
786 {
787     cairo_t *cr = acre->cr;
788     unsigned int d, i;
789     acre_data_t *data;
790
791     cairo_save (cr);
792
793     cairo_rectangle (cr,
794                      acre->chart.x, acre->chart.y,
795                      acre->chart.width, acre->chart.height);
796     cairo_clip (cr);
797
798     cairo_set_source_rgb (cr, 0, 0, 0);
799
800     _set_transform_to_data_space (acre);
801
802     for (d = 0; d < acre->num_data; d++) {
803         int color = d % acre->num_colors;
804         cairo_set_source_rgb (cr,
805                               acre->colors[color].red,
806                               acre->colors[color].green,
807                               acre->colors[color].blue);
808         data = acre->data[d];
809
810         cairo_new_path (cr);
811
812         for (i = 0; i < data->num_points; i++) {
813             cairo_line_to (cr,
814                            data->points[i].x,
815                            data->points[i].y);
816         }
817         cairo_save (cr);
818         {
819             cairo_identity_matrix (cr);
820             cairo_set_line_width (cr, 1.0);
821             cairo_stroke (cr);
822         }
823         cairo_restore (cr);
824     }
825
826     cairo_restore (cr);
827 }
828
829 typedef enum _ticks { ACRE_TICKS_X, ACRE_TICKS_Y } acre_ticks_t;
830
831 static void
832 _draw_ticks (acre_t *acre,
833              double axis_min, double axis_max,
834              acre_ticks_t ticks)
835 {
836     cairo_t *cr = acre->cr;
837     double t, step, sub_step;
838     int minor_divisions;
839
840     cairo_save (cr);
841
842     _set_transform_to_data_space (acre);
843
844     step = _step_for_range (axis_max - axis_min, &minor_divisions);
845     sub_step = step / minor_divisions;
846
847     for (t = (floor (axis_min / sub_step) + 1) * sub_step;
848          t <= axis_max;
849          t += sub_step)
850     {
851         int tick_size;
852         if (fabs((t / step) - round (t / step)) < 0.5 * (sub_step / step))
853             tick_size = ACRE_TICK_MAJOR_SIZE;
854         else
855             tick_size = ACRE_TICK_MINOR_SIZE;
856
857         /* tick */
858         cairo_save (cr);
859         {
860             if (ticks == ACRE_TICKS_X)
861                 cairo_move_to (cr, t, acre->y_axis.view_min);
862             else
863                 cairo_move_to (cr, acre->x_axis.view_min, t);
864
865             cairo_identity_matrix (cr);
866
867             if (ticks == ACRE_TICKS_X) {
868                 cairo_rel_line_to (cr, 0, 0.5);
869                 cairo_rel_line_to (cr, 0, -tick_size - 0.5);
870             } else {
871                 cairo_rel_line_to (cr, -0.5, 0);
872                 cairo_rel_line_to (cr, tick_size + 0.5, 0);
873             }
874
875             cairo_set_line_width (cr, 1.0);
876             cairo_stroke (cr);
877         }
878         cairo_restore (cr);
879
880         /* label */
881         if (tick_size == ACRE_TICK_MAJOR_SIZE)
882         {
883             PangoLayout *layout;
884             int width, height;
885
886             cairo_save (cr);
887
888             layout = _create_layout_printf (acre, "%g", t);
889
890             if (ticks == ACRE_TICKS_X)
891                 cairo_move_to (cr, t, acre->y_axis.view_min);
892             else
893                 cairo_move_to (cr, acre->x_axis.view_min, t);
894
895             cairo_identity_matrix (cr);
896             pango_layout_get_pixel_size (layout, &width, &height);
897
898             if (ticks == ACRE_TICKS_X)
899                 cairo_rel_move_to (cr, -width / 2, ACRE_X_TICK_VALUE_PAD);
900             else
901                 cairo_rel_move_to (cr, -width - ACRE_Y_TICK_VALUE_PAD,
902                                    -height/2);
903
904             _show_layout (cr, layout);
905
906             cairo_restore (cr);
907         }
908     }
909
910     cairo_restore (cr);
911 }
912
913 static void
914 _draw_legend (acre_t *acre)
915 {
916     PangoLayout *layout;
917     int label_width, max_label_width = 0;
918     int width, height;
919     unsigned int i;
920     cairo_t *cr = acre->cr;
921
922     cairo_save (cr);
923
924     for (i = 0; i < acre->num_data; i++) {
925         layout = _create_layout (acre, acre->data[i]->name);
926         pango_layout_get_pixel_size (layout, &label_width, NULL);
927         _destroy_layout (layout);
928         if (label_width > max_label_width)
929             max_label_width = label_width;
930     }
931
932     width = ACRE_LEGEND_PAD + ACRE_LEGEND_LINE_SIZE + ACRE_LEGEND_PAD + 
933         max_label_width + ACRE_LEGEND_PAD;
934     height = ACRE_LEGEND_PAD +
935         acre->num_data * (ACRE_FONT_SIZE + ACRE_LEGEND_PAD);
936
937     cairo_translate (cr, acre->chart.x, acre->chart.y);
938
939     cairo_translate (cr,
940                      acre->chart.width - ACRE_LEGEND_PAD - width,
941                      ACRE_LEGEND_PAD);
942
943     cairo_rectangle (cr, -0.5, -0.5, width + 1.0, height + 1.0);
944     cairo_set_source_rgb (cr, 0, 0, 0);
945     cairo_set_line_width (cr, 1.0);
946     cairo_stroke (cr);
947
948     cairo_translate (cr, ACRE_LEGEND_PAD, ACRE_LEGEND_PAD);
949
950     for (i = 0; i < acre->num_data; i++) {
951         cairo_rectangle (cr,
952                          0, ACRE_LEGEND_LINE_SIZE / 2,
953                          ACRE_LEGEND_LINE_SIZE, ACRE_LEGEND_LINE_SIZE / 2);
954         cairo_set_source_rgb (cr,
955                               acre->colors[i % acre->num_colors].red,
956                               acre->colors[i % acre->num_colors].green,
957                               acre->colors[i % acre->num_colors].blue);
958         cairo_fill (cr);
959
960         layout = _create_layout (acre, acre->data[i]->name);
961         cairo_move_to (cr, ACRE_LEGEND_LINE_SIZE + ACRE_LEGEND_PAD, 0);
962         cairo_set_source_rgb (cr, 0, 0, 0);
963         _show_layout (cr, layout);
964
965         cairo_translate (cr, 0, ACRE_LEGEND_PAD + ACRE_FONT_SIZE);
966     }
967
968     cairo_restore (cr);
969 }
970
971 static void
972 _draw_frame_and_ticks (acre_t *acre)
973 {
974     cairo_t *cr = acre->cr;
975
976     cairo_save (cr);
977
978     cairo_set_source_rgb (cr, 0, 0, 0); /* black */
979
980     /* ticks */
981     _draw_ticks (acre, acre->x_axis.view_min, acre->x_axis.view_max, ACRE_TICKS_X);
982     _draw_ticks (acre, acre->y_axis.view_min, acre->y_axis.view_max, ACRE_TICKS_Y);
983
984     /* frame */
985     cairo_rectangle (cr,
986                      acre->chart.x - 0.5, acre->chart.y - 0.5,
987                      acre->chart.width + 1.0, acre->chart.height + 1.0);
988     cairo_set_line_width (cr, 1.0);
989     cairo_stroke (cr);
990
991     cairo_restore (cr);
992 }
993
994 /* Draw the plot to the given cairo context within a user-space
995  * rectangle from (0, 0) to (width, height). This size includes all
996  * space for extra-plot elements (such as the title, the axis labels,
997  * etc.)
998  */
999 void
1000 acre_draw (acre_t *acre, cairo_t *cr, int width, int height)
1001 {
1002     acre->cr = cr;
1003
1004     acre->width = width;
1005     acre->height = height;
1006
1007     acre->chart.width = width;
1008     acre->chart.height = height;
1009
1010     cairo_save (cr);
1011
1012     cairo_set_source_rgb (cr, 1, 1, 1);
1013
1014     _choose_colors (acre);
1015
1016     /* We compute the axis ranges before doing label layout so that we
1017      * can account for the width of the y-axis value labels. */
1018     _compute_axis_ranges (acre);
1019
1020     _draw_title_and_labels (acre);
1021
1022     /* And we recompute the axis ranges now that the title and axis
1023      * label space is all measured and accounted for. */
1024     _compute_axis_ranges (acre);
1025
1026     _draw_data (acre);
1027
1028     if (acre->num_data > 1)
1029         _draw_legend (acre);
1030
1031     _draw_frame_and_ticks (acre);
1032 }
1033
1034 /* Create a new dataset---a collection of (x, y) datapoints. A single
1035  * plot can contain multiple datasets, (see acre_add_data). */
1036 acre_data_t *
1037 acre_data_create (void)
1038 {
1039     acre_data_t *data;
1040
1041     data = xmalloc (sizeof (acre_data_t));
1042
1043     data->name = NULL;
1044
1045     data->points = NULL;
1046     data->points_size = 0;
1047     data->num_points = 0;
1048
1049     return data;
1050 }
1051
1052 /* Destroy an acre dataset. Do not call this function if the dataset
1053  * has been added to an acre_t plot with acre_add_data. */
1054 void
1055 acre_data_destroy (acre_data_t *data)
1056 {
1057     free (data->name);
1058
1059     free (data->points);
1060
1061     free (data);
1062 }
1063
1064 /* Set the label for this dataset (to appear in the plot's key). */
1065 void
1066 acre_data_set_name (acre_data_t *data, const char *name)
1067 {
1068     free (data->name);
1069
1070     data->name = strdup (name);
1071 }
1072
1073 /* Add a datapoint to the given dataset. */
1074 void
1075 acre_data_add_point_2d (acre_data_t *data, double x, double y)
1076 {
1077     if (data->num_points >= data->points_size) {
1078         data->points_size *= 2;
1079         if (data->points_size == 0)
1080             data->points_size = 16;
1081         data->points = xrealloc_ab (data->points,
1082                                     data->points_size,
1083                                     sizeof (acre_data_point_2d_t));
1084     }
1085
1086     data->points[data->num_points].x = x;
1087     data->points[data->num_points].y = y;
1088
1089     if (data->num_points == 0) {
1090         data->min.x = x;
1091         data->min.y = y;
1092
1093         data->max.x = x;
1094         data->max.y = y;
1095     } else {
1096         if (x < data->min.x)
1097             data->min.x = x;
1098         if (y < data->min.y)
1099             data->min.y = y;
1100
1101         if (x > data->max.x)
1102             data->max.x = x;
1103         if (y > data->max.y)
1104             data->max.y = y;
1105     }
1106
1107     data->num_points++;
1108 }